/
nmapsummariser.py
executable file
·105 lines (85 loc) · 3.87 KB
/
nmapsummariser.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
#! /usr/bin/python
#
# calling: ./nmapsummariser.py <nmap_xml_file>
# for example: ./nmapsummariser.py nmap-full-tcp.xml
#
# Copyright 2015 cornerpirate.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from xml.dom.minidom import parse
import os
import sys
def hasOpenPort(ports):
for port in ports:
if len(port.getElementsByTagName("state")) > 0:
state_tag = port.getElementsByTagName("state")[0]
state = state_tag.getAttribute("state")
if cmp(state, "open") == 0:
return 1
def main():
files = []
# Check that they provided a parameter, if not end gracefully
if len(sys.argv) < 2:
print "Usage: "+sys.argv[0]+" <nmap_xml_file> "\
"\nUsage: "+sys.argv[0]+" <nmap_xml_file1> <nmap_xml_file22>"\
"\nAccepts multiple files."
sys.exit(0)
elif len(sys.argv) >= 2:
for i in range(1, len(sys.argv)):
files.append(sys.argv[i])
for f in files:
doc = parse(f)
hosts = doc.getElementsByTagName("host")
for host_tag in hosts:
address_tag = host_tag.getElementsByTagName("address")[0]
ip_addy = address_tag.getAttribute("addr")
hostname = "Unknown"
try:
hostname_tag = host_tag.getElementsByTagName("hostname")[0]
hostname = hostname_tag.getAttribute("name")
except:
a="a" # not all scans have hostname tags
try:
if len(host_tag.getElementsByTagName("ports"))==0:
print "No ports found"
sys.exit(-1)
_ports = host_tag.getElementsByTagName("ports")[0]
ports = _ports.getElementsByTagName("port")
if hasOpenPort(ports)==1:
## Display IP and hostname information
print "Scan Results for " + ip_addy + "\n"
print "Registered hostnames: " + hostname + "\n"
print "Port,State,Service,Product,Version,Extra"
for port in ports:
portnum = port.getAttribute("portid")
protocol = port.getAttribute("protocol")
if len(port.getElementsByTagName("state")) > 0:
state_tag = port.getElementsByTagName("state")[0]
state = state_tag.getAttribute("state")
if cmp(state, "open") == 0:
port_protocol = port.getAttribute("protocol")
service_name = "Unknown"
service_product = "Unknown"
service_version = "Unknown"
if len(port.getElementsByTagName("service")) !=0:
service_tag = port.getElementsByTagName("service")[0]
service_name = service_tag.getAttribute("name")
service_product = service_tag.getAttribute("product")
service_version = service_tag.getAttribute("version")
print "\"" + portnum + "\",\"" + state + "\",\"" + service_name + "\",\"" + service_product + ",\"" + service_version + "\""
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
if __name__ == "__main__":
main()