-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcscan.py
135 lines (108 loc) · 3.17 KB
/
cscan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#coding:gb2312
import os
import re
import Queue
import threading
q=Queue.Queue()
class getCSgement:
#初始化
def __init__(self,url):
if "http" in url:
pattern = re.compile(r'(?<=//).+(?<!/)')
match = pattern.search(url)
try:
url = match.group()
except:
print "正则error"
self.url = url
else:
self.url = url
def cSgment(self):
lookStr = self.nsLookUp(self.url)
listIp = self.fetIp(lookStr)
if len(listIp)==0:
return "networkbad"
if self.checkCdn(listIp):
strIp = ""
for i in listIp:
strIp = strIp + i + ","
return strIp[:-1] + " (可能使用了cdn)"
return self.makeCSeg(listIp)
#使用nslookup命令进行查询
def nsLookUp(self,url):
cmd = 'nslookup %s 8.8.8.8' % url
handle = os.popen(cmd , 'r')
result = handle.read()
return result
#获取nslookup命令查询的结果里面的ip
def fetIp(self,result):
ips = re.findall(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])', result)
if '8.8.8.8' in ips:
ips.remove('8.8.8.8')
return ips
#检测是否使用cdn
def checkCdn(self,ips):
if len(ips)>1:
return True
return False
#生成c段
def makeCSeg(self,ips):
if not self.checkCdn(ips):
ipStr = "".join(ips)
end = ipStr.rfind(".")
return ipStr[0:end+1] + "1-" + ipStr[0:end+1] + "254"
#开始扫描
def scaner():
while not q.empty():
url=q.get()
t = getCSgement(url)
result = t.cSgment()
if not "networkbad" in result:
print url + ":" + result
if not "cdn" in result:
writeFile("result.txt", result + "\n")
else:
t = getCSgement(url)
result2 = t.cSgment()
if not "networkbad" in result2:
print url + ":" + result2
if not "cdn" in result2:
writeFile("result.txt", result2 + "\n")
else:
print url + ":不能访问 或者 网络不稳定"
if q.empty():
delRep()
#保存记录
def writeFile(filename,context):
f= file(filename,"a+")
f.write(context)
f.close()
#去重复
def delRep():
buff = []
for ln in open('result.txt'):
if ln in buff:
continue
buff.append(ln)
with open('result2.txt', 'w') as handle:
handle.writelines(buff)
#判断文件是否创建
def isExist():
if not os.path.exists(r'result.txt'):
f = open('result.txt', 'w')
f.close()
else:
os.remove('result.txt')
if os.path.exists(r'result2.txt'):
os.remove('result2.txt')
if __name__=="__main__":
isExist()
#读取网址
lines = open("domains.txt","r")
for line in lines:
line=line.rstrip()
q.put(line)
#开启线程
for i in range(3):
t = threading.Thread(target=scaner)
t.start()