-
Notifications
You must be signed in to change notification settings - Fork 17
/
CDNByPass.py
175 lines (147 loc) · 5.66 KB
/
CDNByPass.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import asyncio
import json
import os
from asyncio import CancelledError
import aiohttp
import mmh3, codecs
from bs4 import BeautifulSoup
from BaseObject import BaseObject
from ShodanObject import ShodanObject
from PhpInfoCheck import PhpInfoCheck
from CensysObject import CensysObject
class CDNByPass(BaseObject):
def __init__(self):
BaseObject.__init__(self)
self.domains = []
self.queryResult = {}
self.shodanObject = ShodanObject()
args = self.argparser()
# 生成主域名列表,待检测域名入队
target = args.target
self.threads = args.threads
if not os.path.isfile(target):
# target = 'http://' + target
self.domains.append(target)
elif os.path.isfile(target):
with open(target, 'r+', encoding='utf-8') as f:
for domain in f:
domain = domain.strip()
if not domain.startswith(('http://', 'https://')):
self.domains.append(domain)
def startQuery(self):
try:
tasks = []
newLoop = asyncio.new_event_loop()
asyncio.set_event_loop(newLoop)
loop = asyncio.get_event_loop()
sem = asyncio.Semaphore(self.threads)
for domain in self.domains:
if os.path.exists(os.getcwd() + '/result/' + domain + '/') is False:
os.mkdir(os.getcwd() + '/result/' + domain + '/')
tasks.append(asyncio.ensure_future(self.cdnByPass(domain, sem)))
loop.run_until_complete(asyncio.wait(tasks))
except KeyboardInterrupt:
self.logger.info('[+]Break By User.')
except CancelledError:
pass
self.writeResult()
def writeResult(self):
"""
保存结果
:return:
"""
for domain in self.domains:
with open(os.path.dirname(os.path.abspath(__file__)) + '/result/' + domain + "/" + 'cdnPassInfo' + '.txt', 'a') as fpResult:
for item in self.queryResult[domain]:
fpResult.write(item + "\n")
async def cdnByPass(self, domain, sem):
async with sem:
self.queryResult[domain] = []
icoHashString = await self.calcFaviconHash(domain)
iconList = self.shodanObject.getIcoHashList(icoHashString)
for item in iconList:
self.queryResult[domain].append(item)
# if await self.checkIP(item):
# self.queryResult[domain].append(item)
titleString = await self.getDomainTitle(domain)
if titleString != False:
titleList = self.shodanObject.getTitleList("title:" + titleString)
for item in titleList:
self.queryResult[domain].append(item)
# if await self.checkIP(item):
# self.queryResult[domain].append(item)
phpinfoCheck = PhpInfoCheck()
phpinfoIP = await phpinfoCheck.getDomainIP(domain)
if phpinfoIP != None:
self.queryResult[domain].append(phpinfoIP)
# if await self.checkIP(phpinfoIP):
# self.queryResult[domain].append(phpinfoIP)
censysCheck = CensysObject()
censysIp = await censysCheck.getIP(domain)
if censysIp != None:
for item in censysIp:
self.queryResult[domain].append(item)
pass
async def getDomainTitle(self, domain):
"""
取出目标title
:param domain:
:return:
"""
response = await self.sendRequest('http://' + domain)
if response != False:
soup = BeautifulSoup(response, 'lxml')
return soup.find('title').string
else:
return False
async def checkIP(self, ip):
"""
验证ip是否可以正常访问
:param ip:
:return:
"""
targetFir = 'http://' + ip + '/'
targetSec = 'https://' + ip + '/'
tagetFirResult = await self.sendRequest(targetFir)
if tagetFirResult != False:
return True
tagetSecResult = await self.sendRequest(targetSec)
if tagetSecResult != False:
return True
async def calcFaviconHash(self, domain):
"""
计算目标站点的icon-hash
:param domain:
:return:
"""
requestUrl = 'http://' + domain + '/favicon.ico'
# responseContent = requests.get(requestUrl, verify=False).content
sem = asyncio.Semaphore(1024)
try:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector()) as session:
async with sem:
async with session.get(requestUrl, timeout=20, headers=self.headers) as req:
await asyncio.sleep(1)
responseContent = await req.content.read()
req.close()
except CancelledError:
pass
except ConnectionResetError:
pass
except Exception as e:
self.logger.error('[-]Resolve {} fail'.format(requestUrl))
return False
iconHash = mmh3.hash(codecs.lookup('base64').encode(responseContent)[0])
return 'http.favicon.hash:' + str(iconHash)
async def getIPFromPhpinfo(self, domain):
"""
从phpinfo中获取ip
:param domain:
:return:
"""
phpinfoCheck = PhpInfoCheck()
return await phpinfoCheck.getDomainIP('www.wzonline.zj.cn')
if __name__ == '__main__':
cdnByPass = CDNByPass()
cdnByPass.startQuery()
print(cdnByPass.queryResult)