-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
181 lines (149 loc) · 5.13 KB
/
main.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
import asyncio
from queue import Queue
from multiprocessing import Manager
from typing import Dict
from nmap.nmap import PortScannerAsync
from common.messaging.vumos import ScheduledVumosService, VumosService
loop = asyncio.get_event_loop()
async def perform_scan(service: VumosService, flags: str, ranges: str):
# Calculate ip address list
ip_ranges = ranges.replace(',', ' ')
targets = Manager().Queue()
services = Manager().Queue()
# Result processor function
def on_host_result(host, result):
if not result:
return
scan = result["scan"]
# Send data
if len(scan.keys()) > 0:
print(f"Scanned [{host}]")
scan = scan[host]
parsed_keys = [
"hostnames",
"addresses",
"tcp"
]
# Notify found host
domains = []
hostnames = []
if 'hostnames' in scan:
hostnames = scan['hostnames']
for hostname in hostnames:
if hostname['name'] != "":
domains.append(hostname['name'])
extra = {}
for key in scan.keys():
if not key in parsed_keys:
extra[key] = scan[key]
targets.put((host, domains, extra))
# Notify found services
tcp = {}
if 'tcp' in scan:
tcp = scan['tcp']
for port in tcp.keys():
found = tcp[port]
parsed_keys = [
"hostnames",
"addresses",
"tcp"
]
name = found['product']
if 'extrainfo' in found:
name += f" {found['extrainfo']}"
services.put((
host,
port,
name,
found['name'],
found['version'],
{
"nmap": {
"state": found['state'],
"reason": found['reason'],
"conf": found['conf'],
"cpe": found['cpe']
}
}
))
# Create nmap instance and run scan
nmap = PortScannerAsync()
nmap.scan(hosts=ip_ranges,
arguments=flags, callback=on_host_result)
# Wait for scan finish
while nmap.still_scanning() or (not targets.empty()) or (not services.empty()):
while not targets.empty():
await service.send_target_data(*targets.get())
await asyncio.sleep(0.5)
while not services.empty():
await service.send_service_data(*services.get())
await asyncio.sleep(0.5)
await asyncio.sleep(1)
async def task(service: ScheduledVumosService, _: None = None):
print("Start Scanning")
await perform_scan(service, service.get_config('flags'), service.get_config('ip_ranges'))
print(f"Finished Scanning")
async def scan_range(service: ScheduledVumosService, arguments: Dict):
await perform_scan(service, arguments['flags'], arguments['ip_ranges'])
# Initialize Vumos service
service = ScheduledVumosService(
"Ranged Periodic Nmap Scanner",
"A nmap scanner (Only IP addresses and ports) that performs extensive scans in IP ranges periodically",
conditions=lambda s: True, task=task, parameters=[
{
"name": "Flags",
"description": "Flags to be used when scanning",
"key": "flags",
"value": {
"type": "string",
"default": "-A -f"
}
},
{
"name": "Redo Days",
"description": "Days between scanning runs for each host",
"key": "redo_days",
"value": {
"type": "integer",
"default": 7
}
},
{
"name": "IP Ranges",
"description": "Comma separated CIDR IP ranges to scan",
"key": "ip_ranges",
"value": {
"type": "string",
"default": "10.10.10.10"
}
}],
actions=[
{
"name": "Scan range",
"description": "Pontually scans an ip range given",
"arguments": [
{
"name": "Flags",
"description": "Flags to be used when scanning",
"key": "flags",
"value": {
"type": "string",
"default": "-A -f"
}
},
{
"name": "IP Ranges",
"description": "The IP ranges to be scanned",
"key": "ip_ranges",
"value": {
"type": "string"
}
}
],
"key": "scan_range",
}
],
pool_interval=3600 * 24 * 7)
service.on_action("scan_range", scan_range)
loop.run_until_complete(service.connect(loop))
service.loop(loop)