-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmulti-scanner.py
executable file
·311 lines (264 loc) · 10.6 KB
/
multi-scanner.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/python
# Python script to test for live hosts, services runnning on the hosts, virtual websites, web servers
# Example- ./multi-scanner.py -t 127.0.0.1-3 -o test --pingsweep --virtualhosts -w wordlist.txt --service --discover-webservers
# 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/>.
import sys
import subprocess as SP
import os
import socket
import requests
from argparse import ArgumentParser
def main():
parser = ArgumentParser()
parser.add_argument(
"-t",
dest="target_hosts",
required=True,
help="Enter a IP range or IP address")
parser.add_argument(
"-o",
dest="output_directory",
required=True,
help="Set an output directory")
parser.add_argument(
"-w",
dest="wordlist",
required=False,
help="Set a wordlist that will be used to find virtual hosts",
default=False)
parser.add_argument(
"-p",
dest="port",
required=False,
help="Set a port to use to run the vhost fuzzer on. Only needs to be set if they are hosting the site on a different port to the default.",
default=80)
parser.add_argument(
"--pingsweep",
dest="ping_sweep",
action="store_true",
help="Performs a ping sweep and discovers live hosts.",
default=False)
parser.add_argument(
"--dns",
dest="find_dns_servers",
action="store_true",
help="Find DNS servers from output",
default=False)
parser.add_argument(
"--services",
dest="perform_service_scan",
action="store_true",
help="Perform service scan over targets.",
default=False)
parser.add_argument(
"--discover-webservers",
dest="discover_web_servers",
action="store_true",
help="Attempt to discover web servers from the targets.txt file. It will store the output to web_servers.txt",
default=False)
parser.add_argument(
"--virtualhosts",
dest="virtualhosts",
action="store_true",
required=False,
help="Attempt to discover virtual hosts using the specified wordlist.",
default=False)
parser.add_argument(
"--ss-output",
dest="service_scan_output",
required=False,
help="--discover-webservers will use the output provided in this argument to find web servers.",
default=False)
arguments = parser.parse_args()
output_directory = arguments.output_directory
target_hosts = arguments.target_hosts
output_file = output_directory + "/targets.txt"
web_servers_output = output_directory + "/web_servers.txt"
ss_output = arguments.service_scan_output
if len(sys.argv) == 1:
parser.error("No arguments. Please Provide arguments.")
sys.exit()
# HOST DISCOVERY
if arguments.ping_sweep is True:
print("[#] Performing ping sweep")
print("[+] Writing discovered targets to: %s" % output_file)
live_hosts = 0
f = open(output_file, 'w')
print("[+] Performing ping sweep over %s" % target_hosts)
SWEEP = "nmap -n -sS -p0- %s" % (target_hosts)
results = SP.check_output(SWEEP, shell=True, bufsize=1, stderr=SP.PIPE)
print (" [>] Ping Sweep results")
print results
lines = results.split("\n")
for line in lines:
line = line.strip()
line = line.rstrip()
if ("Nmap scan report for" in line):
ip_address = line.split(" ")[4]
if (live_hosts > 0):
f.write('\n')
f.write("%s" % (ip_address))
print(" [>] Discovered host: %s" % (ip_address))
live_hosts += 1
print("[*] Found %s live hosts" % (live_hosts))
print("[*] Created target list %s" % (output_file))
f.close()
# SERVICE SCAN
if arguments.perform_service_scan is True:
print("[#] Performing service scans")
print("[+] Starting nmap scan for %s" % (target_hosts))
SSCAN = "nmap -sV --top-ports 50 -iL %s -oA '%s/%s.ss'" % (
output_file, output_directory, target_hosts)
results = SP.check_output(SSCAN, shell=True, bufsize=1, stderr=SP.PIPE)
print(" [>] SERVICE SCAN RESULTS ")
print (results)
# discover web servers
if arguments.discover_web_servers is True:
print("[#] Attempting to find web servers")
print("[+] If successful it will store the output to web_servers.txt")
w = web_server_discover()
if arguments.perform_service_scan is True:
print("[#] Found Service scan output. Using this to determine web servers")
f = output_directory + "/" + target_hosts + ".ss.gnmap"
w.discover(f,web_servers_output)
else:
print ("[#] No service Scan flag used. Using output specified.")
if ss_output == False:
print("[!] No output provided")
else:
print("[+] Starting the web server discovery.")
f = ss_output
w.discover(f,web_servers_output)
# vhost fuzzer
if arguments.virtualhosts is True:
print("[#] Performing VHOST fuzzing")
if arguments.discover_web_servers is True:
scanner = virtual_host_scanner(web_servers_output,
arguments.output_directory,
arguments.port,
arguments.wordlist)
scanner.scan()
else:
scanner = virtual_host_scanner(output_file,
arguments.output_directory,
arguments.port,
arguments.wordlist)
scanner.scan()
# CLASSES
class bcolors:
R = '\33[91m'
G = '\33[92m'
Y = '\33[93m'
ENDC = '\033[0m'
class status_c:
def __int__(self):
self.code = code
def code_handler(self, code):
if code >= 200 and code <= 299:
return bcolors.G + str(code) + bcolors.ENDC
elif code >= 400 and code <= 499:
return bcolors.Y + str(code) + bcolors.ENDC
else:
return bcolors.R + str(code) + bcolors.ENDC
class web_server_discover:
def __int__(self,output, web_servers_output):
self.output = output
self.web_servers_output = web_servers_output
def discover(self, output, web_servers_output):
web_keys = ['80/open', '443/open', '8080/open', '//http//']
w = open(web_servers_output, "w")
f = open(output)
ips = []
lines = f.readlines()
print("[+] grabbing output from service scan")
for line in lines:
line = line.strip()
line = line.rstrip()
if "Ports:" in line:
for wk in web_keys:
if wk in line:
ip = line.split(" ")[1]
ips.append(ip)
i = list(set(ips))
print("[#] Writing ips found to output file.")
w.write('\n'.join(i))
w.write('\n')
w.close()
class vhost_functions:
def __int__(self):
self.ipaddress = ip
def lookup_hostname(self, ip):
try:
return socket.gethostbyaddr(ip)
except socket.herror:
return None, None, None
class virtual_host_scanner(object):
def __init__(
self,
target,
output,
port=80,
wordlist="wordlist.txt"):
self.target = target
self.output = output + '/' + 'virtualhosts_output.txt'
self.port = port
self.wordlist = wordlist
def scan(self):
print(
"[+] Starting virtual host scan for %s using port %s and wordlist %s" %
(self.target, str(
self.port), self.wordlist))
if not os.path.exists(self.wordlist):
print(
"[!] Wordlist %s doesn't exist, exiting virtual host scanner." %
self.wordlist)
return
virtual_host_list = open(self.wordlist).read().splitlines()
results = ''
f = open(self.output, "w")
s = status_c()
vf = vhost_functions()
for virtual_host in virtual_host_list:
ips = open(self.target, "r")
lines = ips.readlines()
lines = map(lambda s: s.strip(), lines)
for ip in lines:
DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) ' \
'AppleWebKit/537.36 (KHTML, like Gecko) ' \
'Chrome/61.0.3163.100 Safari/537.36'
DN_DATA = vf.lookup_hostname(ip)
DN = repr(DN_DATA[0]).replace("'", "")
virtual_hostname = virtual_host.replace('%s', self.target)
headers = {
'Host': virtual_hostname + "." + DN if self.port == 80 else '{}:{}'.format(
hostname,
self.port),
'Accept': '*/*',
'user-agent': DEFAULT_USER_AGENT}
dest_url = '{}://{}:{}'.format('https' if int(
self.port) == 443 else 'http', ip, self.port)
res = requests.get(dest_url, headers=headers, verify=False)
output = ' [>] Found: {} (code: {}, length: {}, content-type: {}, server: {}) on {}'.format(
virtual_hostname,
s.code_handler(
res.status_code),
res.headers.get('content-length'),
res.headers.get('content-type'),
res.headers.get('server'),
ip)
f.write('\n')
f.write('%s' % (output))
results += output + '\n'
print(output)
if __name__ == "__main__":
main()