-
Notifications
You must be signed in to change notification settings - Fork 2
/
nmap.py
84 lines (70 loc) · 2.86 KB
/
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
83
84
import argparse
from subprocess import check_output
import re
from json import dumps
from datetime import datetime
parser = argparse.ArgumentParser('Calls nmap with given arguments \
and pushes the result to a database')
parser.add_argument('--search-address',
type=str,
help='IP address to search with nmap',
required=True)
parser.add_argument('--nmap-option',
type=str,
help='Option to use with nmap')
parser.add_argument('--search-mask',
type=int,
help='Subnet mask used to specify a subnet to search')
args = parser.parse_args()
# Generate nmap command
nmap_str = 'nmap'
if args.nmap_option is not None:
nmap_str += ' -' + args.nmap_option
nmap_str += ' ' + args.search_address
if args.search_mask is not None:
nmap_str += '/' + str(args.search_mask)
# Call nmap, retrieve raw result
result = check_output(nmap_str, shell=True)
res_iter = iter(result.split('\n'))
# Parse result
hosts = []
while True:
try:
line = next(res_iter)
if re.match('Nmap\sscan\sreport\sfor', line) is not None:
host_data = {}
host_data['open_ports'] = []
host_data['timestamp'] = datetime.now().isoformat()
raw_data = line.split()
if len(raw_data) == 5:
host_data['ip'] = raw_data[4]
elif len(raw_data) == 6:
host_data['name'] = raw_data[4]
host_data['ip'] = raw_data[5].replace('(', '').replace(')', '')
else:
continue
while line.strip():
line = next(res_iter)
# Match port list header
if re.match('PORT\s+STATE', line):
# Look up each open port
while True:
line = next(res_iter)
port_match = re.match('(\d+)/(\w+)\s+(?:(open)|)', line)
if port_match is not None:
# Only parse open ports
if port_match.group(3) is not None:
port = int(port_match.group(1))
host_data['open_ports'].append(port)
else:
break
# Match MAC address
mac_match = re.match(r'MAC\sAddress:\s((?:\w\w:){5}\w\w)(\s\((?:\w|\s)+\))?', line)
if mac_match is not None:
host_data['mac'] = mac_match.group(1)
if mac_match.group(2) is not None and mac_match.group(2) != '(Unknown)':
host_data['mac_provider'] = mac_match.group(2).replace('(', '').replace(')', '')
hosts.append(host_data)
except StopIteration:
break
print(dumps(hosts))