-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnmap_report_parser.py
More file actions
85 lines (63 loc) · 3.29 KB
/
Copy pathnmap_report_parser.py
File metadata and controls
85 lines (63 loc) · 3.29 KB
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
#! -*- coding:utf-8 -*-
from libnmap.parser import NmapParser
import sys
import os.path
import json
class NmapReportParser(object):
def __init__(self,filename):
self.filename = filename
def get_report_xml(self):
self.nmap_report = NmapParser.parse_fromfile(self.filename)
def get_report_json(self):
json_file = json.load(open(self.filename))
self.nmap_report = NmapParser.parse_fromdict(json_file)
def all_product_list(self):
return sorted(set([ b.banner for a in self.nmap_report.hosts for b in a.services if 'product' in b.banner]))
def open_port_hosts_list(self,portnumber):
return [ a.address for a in self.nmap_report.hosts if (a.get_open_ports()) and portnumber in [b[0] for b in a.get_open_ports()] ]
def open_product_hosts_list(self,servicename,verbose=False):
if verbose:
hosts_list = [ [a.address, b.port] for a in self.nmap_report.hosts for b in a.services if servicename.lower() in str(b.banner).lower() ]
else:
hosts_list = [ [a.address, b.port, str(b.banner)] for a in self.nmap_report.hosts for b in a.services if servicename.lower() in str(b.banner).lower() ]
return hosts_list
def find_fingerprint_hosts_list(self,fingerprint,verbose=False):
if verbose:
return [ [a.address, b.port, b.servicefp] for a in self.nmap_report.hosts for b in a.services if b.open() and fingerprint.lower() in (str(b.get_dict()) + str(b.servicefp)).lower() ]
else:
return [ ':'.join([a.address, str(b.port)]) for a in self.nmap_report.hosts for b in a.services if b.open() and fingerprint.lower() in (str(b.get_dict()) + str(b.servicefp)).lower() ]
if __name__ == "__main__":
nmap_report = NmapReportParser(sys.argv[1])
extension = os.path.splitext(sys.argv[1])[1][1:]
if extension == 'xml':
nmap_report.get_report_xml()
if extension == 'json':
nmap_report.get_report_json()
print "---------------------usage-------------------"
print "'all' means print all kinds of product"
print "'80' means print port 80 opened host "
print "'tomcat' means print host which has tomcat ,not case sensitive "
#print "-------------------------------------------------"
while True:
print "----------------input command--------------"
cmd = sys.stdin.readline().rstrip()
if not cmd:
continue
try:
if cmd == 'all':
print "--------------------All Product-------------------"
for product in nmap_report.all_product_list():
print product.lstrip('product: ')
elif cmd.isdigit() and int(cmd) > 1 and int(cmd) < 65536:
print "--------------------Open Port {0}-------------------".format(cmd)
for host in nmap_report.open_port_hosts_list(int(cmd)):
print host
else:
print "--------------------Find Product {0}-------------------".format(cmd)
style = '{0}:{1}\t\t{2}'
for host in nmap_report.open_product_hosts_list(cmd):
print style.format(*host)
except Exception as err:
if isinstance(err, KeyboardInterrupt):
break
print repr(err)