-
Notifications
You must be signed in to change notification settings - Fork 2k
/
masscan+nmap.py
82 lines (68 loc) · 2.55 KB
/
masscan+nmap.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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# __author__: www.t00ls.net-rabbittb
# modified by mrxn
# you must needed install python-nmap first with pip3 or pip
import os
import time
import json
import nmap
import sys
PORT_list = [
80, 8080, 8089, 23, 21, 5001, 7001 - 7010, 8888, 6666, 1080, 27017, 6379,
1433, 3306, 1352, 1521, 11211, 9200, 9300, 9090, 8069, 5900, 443, 5432,
5632, 4848, 2181
]
ports = "80,8080,8089,23,21,5001,7001-7010,8888,6666,1080,27017,6379,1433,3306,1352,1521,11211,9200,9300,9090,8069,5900,443,5432,5632,4848,2181"
def masScan(ip_file, ports):
ip_file = ip_file
json_name = str(time.time()) + "_masscan.log"
payload = "masscan -iL {0} -p {2} -oJ {1} --rate 3000".format(
ip_file, json_name, ports)
print(payload)
os.system(payload)
return masscan_parse(json_name)
def masscan_parse(log_name):
res_dic = {}
with open(log_name) as f:
for line in f:
if line.startswith('{'):
temp = json.loads(line[:-2])
if temp['ip'] in res_dic.keys():
res_dic[temp['ip']].append(str(temp['ports'][0]['port']))
else:
res_dic[temp['ip']] = [str(temp['ports'][0]['port'])]
return res_dic
def callback_result(host, scan_result):
if host in scan_result['scan'].keys(
) and 'tcp' in scan_result['scan'][host]:
for x in scan_result['scan'][host]['tcp']:
res = {
'host': host,
'port': x,
'service': scan_result['scan'][host]['tcp'][x]['name'],
'product': scan_result['scan'][host]['tcp'][x]['product'],
'version': scan_result['scan'][host]['tcp'][x]['version']
}
print(res)
return res
def nmapScan(target_list):
scanner = nmap.PortScannerAsync()
for target in target_list:
scanner.scan(target,
arguments='-sV -PS -p' + ','.join(target_list[target]),
callback=callback_result)
while scanner.still_scanning():
scanner.wait(2)
if __name__ == "__main__":
ports = "80,8080,8089,23,21,5001,7001-7010,8888,6666,1080,27017,6379,1433,3306,1352,1521,11211,9200,9300,9090,8069,5900,443,5432,5632,4848,2181"
start = time.time()
# res = masScan("1.txt", ports)
if len(sys.argv) == 2:
res = masScan("{ipfile}".format(ipfile=sys.argv[1]), ports)
nmapScan(res)
elif len(sys.argv) == 1:
print('please input iplist file')
else:
print('error')
print(time.time() - start)