您的位置:首页 > 编程语言

一段github上的一段py 使用iplist.net去反查域名,看看到底有多少个域名指向了一个IP

2015-03-24 20:18 483 查看
使用iplist.net去反查域名,看看到底有多少个域名指向了一个IP

#################################################################################
# iplist.py - Lookup IPs -> Domain and Domain -> IPs from iplist.net #
# Copyrighted: Primal Security Podcast - www.primalsecurity.net #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
#################################################################################

#!/usr/bin/env python

####################################
# Curent URL format: #
# http://iplist.net/74.125.228.73/ #
####################################

import os, urllib, sys, optparse, re

# Function to check file
def checkFile(cfile):
if not os.path.isfile(cfile):
print '[-] ' + cfile + ' does not exist.'
exit(0)

if not os.access(cfile, os.R_OK):
print '[-] ' + cfile + ' access denied.'
exit(0)

print '[+] Fetching URLs from ' +cfile

# Function to perform the lookup
def iplook(ips):
if iplist != None:
iFile = open(iplist, 'r')
for ip in iFile:
ei = ip.split()
i = ei[0]
httpR = urllib.urlopen("http://iplist.net/"+i+"/")
f = httpR.readlines()
for line in f:
if "<h2" in line:
if "</table" in line:
# Formatting line for domain
htm = line.split("<")
html = htm[2]
dom = html.split(">")
domain = str(dom[1])
# Formatting line for IP
i = line.split("/")
ip = str(i[4])
else:
print "##############################"
htm = line.split("<")
html = htm[1]
dom = html.split(">")
domain = str(dom[1])
i = line.split("/")
print i
ip = str(i[3])
l = '%-25s --> %20s' % (domain,ip)
print l

else:
httpR = urllib.urlopen("http://iplist.net/"+ips+"/")
f = httpR.readlines()
for line in f:
if "<h2" in line:
if "</table" in line:
# Formatting line for domain
htm = line.split("<")
html = htm[2]
dom = html.split(">")
domain = str(dom[1])
# Formatting line for IP
i = line.split("/")
ip = str(i[4])
else:
htm = line.split("<")
html = htm[1]
dom = html.split(">")
domain = str(dom[1])
i = line.split("/")
ip = str(i[3])
l = '%-25s --> %20s' % (domain,ip)
print l

def main():
parser = optparse.OptionParser(sys.argv[0] +'-r <file_with_ips> || -i <ip_addr>')
parser.add_option('-i', dest='ip', type='string',help ='specify a target IP')
parser.add_option('-r', dest='ips', type='string',help='specify target file with IPs')
(options, args) = parser.parse_args()
global iplist
global ip
iplist = options.ips
ip = options.ip

if (iplist == None) and (ip == None):
print parser.usage
exit(0)

if iplist != None:
checkFile(iplist)
iplook(iplist)

else:
iplook(ip)

if __name__ == "__main__":
main()


这段代码的结构十分清晰,支持两种输入 一种是-i后面加ip,另一种是-r后面加存有ip的文件。如果是文件则从文件中依次读取出ip 然后通过urllib.urlopen进行访问,从得到html文件中 找出规律 从而 得出结果。
github:  https://github.com/primalsecn/python_code/blob/master/iplist.py#L49
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐