-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCWebScanner.py
More file actions
183 lines (156 loc) · 6.19 KB
/
Copy pathCWebScanner.py
File metadata and controls
183 lines (156 loc) · 6.19 KB
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
176
177
178
179
180
181
182
183
import asyncio
import json
import os
import sys
import IPy
from asyncio import CancelledError
import aiodns
import aiohttp
import argparse
from bs4 import BeautifulSoup
from BaseObject import BaseObject
class CWebScanner(BaseObject):
def __init__(self):
BaseObject.__init__(self)
self.domains = []
self.queryResult = {}
self.resultList = []
args = self.argparser()
# 生成主域名列表,待检测域名入队
target = args.target
self.ports = args.port.split(",")
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)
self.headers = {}
self.buildHeader()
def argparser(self):
"""
解析参数
:return:参数解析结果
"""
parser = argparse.ArgumentParser(description='InfoScripts can help you collect target\'s information',
epilog='\tUsage:\npython3 ' + sys.argv[0] + " --target www.baidu.com --port 80,8080")
parser.add_argument('--target', '-t', help='A target like www.example.com or subdomains.txt', required=True)
parser.add_argument('--port', '-p', type=str, default="80", help='The port you chose to scan(default 80)', required=False)
args = parser.parse_args()
return args
async def handleTarget(self, target, resolver):
#处理给定扫描目标
rawTarget = target
try:
if int(target.split('.')[-1]) >= 0:
result = ""
for item in target.split('.')[:3]:
result += item
result += "."
result += '0/24'
return result
except:
result = await self.getIP(target, resolver)
if result != None:
target = result.host
if int(target.split('.')[-1]) >= 0:
result = ""
for item in target.split('.')[:3]:
result += item
result += "."
result += '0/24'
return result
else:
self.logger.error('[-]Input error: 输入有误: {}'.format(rawTarget))
exit(1)
async def getIP(self, domain, resolver):
"""
尝试获得域名的IP地址
:param domain:
:return:
"""
try:
answer = await resolver.query(domain, 'A')
return answer[0]
except:
self.logger.error('[-]CDNCheck-Check getIP: {} DNS A解析失败'.format(domain))
return None
def startQuery(self):
try:
tasks = []
newLoop = asyncio.new_event_loop()
asyncio.set_event_loop(newLoop)
loop = asyncio.get_event_loop()
resolver = aiodns.DNSResolver(loop=loop)
sem = asyncio.Semaphore(256)
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.scan(domain, resolver, sem)))
loop.run_until_complete(asyncio.wait(tasks))
except KeyboardInterrupt:
self.logger.info('[-]用户手动终止程序.')
except CancelledError:
pass
self.writeResult()
async def scan(self, target, resolver, sem):
async with sem:
target = await self.handleTarget(target, resolver)
ipLists = self.getIPList(target)
for port in self.ports:
for ip in ipLists:
url = "http://" + ip + ":" + port
response, header = await self.sendRequest(url)
if response != False:
self.resultList.append(ip)
self.queryResult[ip] = {}
self.queryResult[ip]["ip"] = ip
header = dict(header)
if "Server" in header.keys():
self.queryResult[ip]["server"] = header['Server']
if BeautifulSoup(response, 'lxml').title != None:
self.queryResult[ip]["title"] = BeautifulSoup(response, 'lxml').title.text.strip('\n').strip()
return None
def writeResult(self):
"""
保存结果
:return:
"""
with open(os.path.dirname(os.path.abspath(__file__)) + '/CheckResult/' + self.fileName + "/" + 'ipList' + '.txt', 'a') as fpIs:
with open(os.path.dirname(os.path.abspath(__file__)) + '/CheckResult/' + self.fileName + "/" + 'ipInfo' + '.txt', 'a') as fpInfo:
for ip in self.resultList:
fpIs.write(ip + "\n")
fpInfo.write(str(self.queryResult[ip]) + "\n")
def getIPList(self, target):
ipList = []
for ip in IPy.IP(target):
ipList.append(str(ip))
return ipList
async def sendRequest(self, url):
"""
发送http请求
:param url:
:return:
"""
sem = asyncio.Semaphore(1024)
try:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector()) as session:
async with sem:
async with session.get(url, timeout=20, headers=self.headers, verify_ssl=None) as req:
await asyncio.sleep(1)
response = await req.text('utf-8', 'ignore')
header = req.headers
req.close()
return response, header
except CancelledError:
pass
except ConnectionResetError:
pass
except Exception as e:
return False, False
if __name__ == '__main__':
cWebScanner = CWebScanner()
cWebScanner.startQuery()