-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnmap_parse.py
More file actions
184 lines (157 loc) · 5.71 KB
/
Copy pathnmap_parse.py
File metadata and controls
184 lines (157 loc) · 5.71 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
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
182
183
184
# NMAP Parse - Tyler Boykin
#
#- Parses NMAP output to CSV
#
# (Currently) Supported Formats
# - XML
# - Greppable
#
# This is another capstone of mine, with emphasis on pure python
#
# TO DO LIST:
# - Normal Output
# - Automatically check and install
# python modules
# - Expand output to more info
#
###################################
import xmltodict, os, sys, getopt
def usage():
print("""
++++++++++++++++++++++++++++++++
NMAP Parser - Tyler Boykin
usage
- python3 ./nmap_parser.py [OPTION] -i [IN FILE] -o [OUTFILE]
- Example: python3 nmap_parse.py -g -i /my/file -o /home/myout.csv
Options:
-x|--xml Parse NMAP XML Output
-n|--normal Parse Normal NMAP Output
-g|--grepable Parse Grepable NMAP Output
-i|--infile Input File
-o|--outfile Output File (ie../home/myout.csv)
-h|--help Halp plz..
++++++++++++++++++++++++++++++++
""")
###### XML ##########
def xml_parse(INFILE,OUTFILE):
print("XML PARSE: INPUT {} OUTPUT {}".format(INFILE,OUTFILE))
# PRE-FLIGHT CHECK FOR OUTFILE
print("Checking if outfile already exists...")
if not os.path.exists(OUTFILE):
print("Creating outfile: "+OUTFILE)
f = open(OUTFILE,'w')
f.write("IP_ADDR,PORT_NUM,PORT_STATE,PORT_PROTO,PORT_SERV,VENDOR\n")
f.close()
# FILE HANDLER / XML HANDLER
fileH4 = open(INFILE,'r')
xmlH = xmltodict.parse(fileH4.read())
# ITERATING THROUGH EACH HOSTS VALUES
for i in range(0, len(xmlH['nmaprun']['host'])):
IP = xmlH['nmaprun']['host'][i]['address'][0]['@addr'].strip()
for x in range(0,len(xmlH['nmaprun']['host'][i]['ports']['port'])-1):
PORT_NUM = xmlH['nmaprun']['host'][i]['ports']['port'][x]['@portid'].strip()
PORT_STATE = xmlH['nmaprun']['host'][i]['ports']['port'][x]['state']['@state'].strip()
PORT_PROTO = xmlH['nmaprun']['host'][i]['ports']['port'][x]['@protocol'].strip()
PORT_SERV = xmlH['nmaprun']['host'][i]['ports']['port'][x]['service']['@name'].strip()
VENDOR = xmlH['nmaprun']['host'][i]['address'][1]['@vendor'].strip()
OUTPUT = IP+","+PORT_NUM+","+PORT_STATE+","+PORT_PROTO+","+PORT_SERV+","+VENDOR
APPEND = "echo "+OUTPUT+" >> "+OUTFILE
callH4 = os.popen(APPEND)
fileH4.close()
sys.exit(0)
###### GREPABLE OUTPUT #####
def grep_parse(INFILE,OUTFILE):
print("GREP PARSE: INPUT {} OUTPUT {}".format(INFILE,OUTFILE))
# PRE-FLIGHT CHECK FOR OUTFILE
print("Checking if outfile already exists...")
if not os.path.exists(OUTFILE):
print("Creating outfile: "+OUTFILE)
fileH3 = open(OUTFILE,'w')
fileH3.write("IP_ADDR,PORT_NUM,PORT_STATE,PORT_PROTO,PORT_SERV,OS_INFO\n")
fileH3.close()
# GRABBING THE IP
IP_ADDR = "cat "+INFILE+" | grep 'Ports' | cut -d':' -f2 | cut -d' ' -f2"
callH1 = os.popen(IP_ADDR)
cHresult1 = callH1.read().strip().split('\n')
IP_ADDR = cHresult1
# GRABBING EVERYTHING ELSE
for IP in IP_ADDR:
PORT_NUM = "cat "+INFILE+" | grep "+IP+" | grep 'Ports' | cut -d':' -f3"
callH2 = os.popen(PORT_NUM)
cHresult2 = callH2.read().strip().split('\n')
PORTS = cHresult2
PORTS = PORTS[0].split(',')
OS_INFO = "cat "+INFILE+" | grep "+IP+" | grep 'Ports' | cut -d':' -f5"
callH3 = os.popen(OS_INFO)
cHresult3 = callH3.read().split('|')
OS_INFO = cHresult3[0].strip()
# ITERATING THROUGH THE RESULT AND ASSIGNING VALUES
for i in range(0,len(PORTS)):
PORTS[i] = PORTS[i].split('/')
PORT_NUM = PORTS[i][0].strip()
PORT_STATE = PORTS[i][1].strip()
PORT_PROTO = PORTS[i][2].strip()
PORT_SERV = PORTS[i][4].strip()
OUTPUT = IP+","+PORT_NUM+","+PORT_STATE+","+PORT_PROTO+","+PORT_SERV+","+OS_INFO
APPEND = "echo "+OUTPUT+" >> "+OUTFILE
callH3 = os.popen(APPEND)
sys.exit(0)
####### NORMAL OUTPUT PARSING (UNDER CONSTRUCTION) ##
def normal_parse(INFILE,OUTFILE):
print("NORMAL PARSE: INPUT {} OUTPUT {}".format(INFILE,OUTFILE))
sys.exit(0)
####### MAIN #######
def main():
INFILE=''
OUTFILE=''
XML=False
GREP=False
NORMAL=False
## ARGUMENT PARSING ##
try:
opts,args = getopt.getopt(sys.argv[1:],"hxgni:o:", ["help","xml","grepable","normal","infile","outfile"])
except getopt.GetoptError:
print(err)
usage()
sys.exit(2)
## ARGUMENT TO VARIABLE ASSIGNMENT ##
for o,a in opts:
if o in ("-h","--help"):
usage()
sys.exit(0)
elif o in ("-x","--xml"):
XML=True
elif o in ("-g","--grepable"):
GREP=True
elif o in ("-n","--normal"):
NORMAL=True
elif o in ("-i","--infile"):
INFILE=a
elif o in ("-o","--outfile"):
OUTFILE=a
else:
assert False, "Unhandled Option!"
## PARAM VERIFICATION AND FUNCTION CALL #
if XML and OUTFILE and INFILE:
if os.path.exists(INFILE):
xml_parse(INFILE, OUTFILE)
else:
print("XML Input File Does Not Exist!")
sys.exit(2)
elif GREP and OUTFILE and INFILE:
if os.path.exists(INFILE):
grep_parse(INFILE, OUTFILE)
else:
print("GREP Input File Does Not Exist!")
sys.exit(2)
elif NORMAL and OUTFILE and INFILE:
if os.path.exists(INFILE):
normal_parse(INFILE, OUTFILE)
else:
print("NORMAL Input File Does Not Exist!")
sys.exit(2)
else:
print("\nPlease Provide All Required Arguments!\n[FORMAT] [INFILE] [OUTFILE]\n")
usage()
sys.exit(2)
main()