-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmasnmap.py
164 lines (136 loc) · 4.31 KB
/
masnmap.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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
###
# # File: masnmap.py
# # Project: Finger
# # Created Date: Tue Mar 08 2022
# # Author: Ryan
# # mail: [email protected]
# # github: https://github.com/ryanInf
# # Last Modified:
# # Modified By:
# #------------------------------------------
# # Copyright (c) 2022
# #------------------------------------------
# #
### source: https://github.com/starnightcyber/masnmap/blob/main/masnmap.py
import nmap
import datetime
import json
from queue import Queue
from multiprocessing import Pool
import os
import Finger
from config.data import Urls,Save
ip_file = 'ips.txt'
# masscan_exe = '/usr/local/bin/masscan'
masscan_exe = '/usr/bin/masscan'
masscan_rate = 2000
masscan_file = 'masscan.json'
task_queue = Queue()
result_queue = Queue()
process_num = 50
total_ports = 0
services_info = []
def run_masscan():
command = 'sudo {} -iL {} -p 1-65535 -oJ {} --rate {}'.format(masscan_exe, ip_file, masscan_file, masscan_rate)
msg = 'executing ==> {}'.format(command)
print(msg)
os.system(command)
pass
def extract_masscan():
"""
extract masscan result file masscan.json into ip:port format, and add to queue
"""
# with open(masscan_file, 'r') as fr:
# tmp_lines = fr.readlines()
# lines = tmp_lines[1:-1]
# global total_ports
# total_ports = len(lines)
# for line in lines:
# tmp = line.strip(',\n')
# line_json = json.loads(tmp)
# # print(line_json)
# # extract ip & port
# ip = line_json['ip']
# port = line_json['ports'][0]['port']
# # combine ip:port, and add to queue
# ip_port = '{}:{}'.format(ip, port)
# task_queue.put(ip_port)
# print(ip_port)
# exit()
#### Masscan version 1.3.2
if not os.path.getsize(masscan_file):
pass
else:
with open(masscan_file, 'r') as fr:
lines = json.load(fr)
total_ports = len(lines)
for line in lines:
ip = line['ip']
port = line['ports'][0]['port']
ip_port = '{}:{}'.format(ip, port)
task_queue.put(ip_port)
print(ip_port)
pass
def nmap_scan(ip_port, index):
# print('scan ==> {}'.format(ip_port))
try:
ip, port = ip_port.split(':')
nm = nmap.PortScanner()
ret = nm.scan(ip, port, arguments='-n -Pn -sS -sV')
service = ret['scan'][ip]['tcp'][int(port)]['name']
service = service.replace(':', ':')
msg = '{}:{}:{}:{}'.format(index, ip, port, service)
print(msg)
return msg
except:
print('sth bad happen ...')
def setcallback(msg):
services_info.append(msg)
def run_nmap():
pool = Pool(process_num) # 创建进程池
index = 0
while not task_queue.empty():
index += 1
ip_port = task_queue.get(timeout=1.0)
pool.apply_async(nmap_scan, args=(ip_port, index), callback=setcallback)
pool.close()
pool.join()
def save_results():
print('save_results ...')
print("services {} lines".format(len(services_info)))
with open("services.txt", 'w') as fw:
for line in services_info:
fw.write(line+'\n')
def finger_print():
Finger.CheckEnv()
Urls.url = []
Save.format = 'json'
for line in services_info:
index, ip, port, service = line.split(':')
if "http" == service or "https" == service:
_url = "{0}://{1}:{2}".format(service, ip, port)
else :
_url = "{0}://{1}:{2}".format('http', ip, port)
Urls.url.append(_url)
run = Finger.Request()
Finger.IpAttributable()
Finger.Output()
def main():
# Step 1, run masscan to detect all the open port on all ips
run_masscan()
# Step 2, extract masscan result file:masscan.json to ip:port format
extract_masscan()
# Step 3, using nmap to scan ip:port
run_nmap()
finger_print()
# Step 4, save results
# save_results()
if __name__ == '__main__':
start = datetime.datetime.now()
main()
end = datetime.datetime.now()
spend_time = (end - start).seconds
msg = 'It takes {} process {} seconds to run ... {} tasks'.format(process_num, spend_time, total_ports)
print(msg)