-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgnmapParse.py
81 lines (58 loc) · 2.35 KB
/
gnmapParse.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
import re,sys
from optparse import OptionParser,OptionGroup
#Protocols if no options passed
DEFAULT_PROTOCOLS = ['http','ssh','telnet','ftp']
#Parse gnmap file
def pGnmap(line, protocols):
#No protocols passed
if len(protocols) == 0:
protocols = DEFAULT_PROTOCOLS
for protocol in protocols:
# Check if protocol blank string
if protocol == "":
continue
# Remove spaces from protocol name
protocol = protocol.strip()
#debug
#print "PROTOCOL: %s" % protocol
if protocol in line:
pLines = str(re.findall('Ports:(\s.*)', line))
pLines = pLines.split(",")
for i in xrange(1,len(pLines)):
if protocol in pLines[i]:
host = re.findall(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", line)
host = ''.join(host)
port = ''.join(pLines[i].split('/')[0]).replace(' ','')
target = '{}{}{}'.format(host,':',port)
print target
def main (filename, protocols):
for fname in filename:
#debug
#print "Parsing File %s" % filename
tLine = False
try:
with open(fname) as infile:
for line in infile:
if tLine == False:
if 'Status: Up' in line:
tLine = True
continue
if tLine == True:
pGnmap(line, protocols)
tLine = False
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
if __name__ == '__main__':
header = "Gnmap Parser \n"
usage = "gnmapParse.py [--OPTIONS] *.gnmap"
parser = OptionParser(usage = usage, description="Protocol parser for gnmap files. Default protocols: http,ssh,telnet,ftp")
parser.add_option('--protocols', help='Parse Protocols: "http","ssh","telnet","ftp","pop3","domain","smtp"')
(options,args) = parser.parse_args()
protocols = []
# Print program header
if options.protocols:
protocols = options.protocols.split(",")
print "PROTOCOLS:%s" % protocols
if len(args) == 0:
print parser.print_help()
main(args, protocols)