-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanParser.py
executable file
·102 lines (87 loc) · 2.89 KB
/
scanParser.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
#!/usr/bin/python3
"""
Why:
- https://nmap.org/book/output-formats-output-to-database.html
Summary:
- Take the XML results given from (masscan || nmap) and parse
- GUI visualization provided by Plotly Bubble chart
- SQL visualization provided by sqlite3
- Visualize the data in interesting ways as to find quirks and commonalities
- Uses the services list from Nmap for visualization points
- Prettifies XML scan results for better deciphering
- Expandable to use other datasets as well
"""
from lxml import etree
from lib import db_handler
from lib import list_handler
from lib import plot_handler
from lib import scan_handler
from lib import stats_handler
from lib import xml_handler
import argparse
import os
import sqlite3 as lite
import sys
## Grab the port lists
lHandler = list_handler.List()
lHandler.list_pick()
def pScan(xHandler):
"""Prettify scan results, useful for deciphering unknown xml scans"""
xHandler.pFy()
def vScan(xHandler):
"""Visualize scan results"""
## Map out the XML
try:
tree = etree.parse(xHandler.xmlInput)
sqlFile = '.'.join(xHandler.xmlInput.split('.')[:-1]) + '.sqlite3'
htmlFile = '.'.join(xHandler.xmlInput.split('.')[:-1]) + '_'
except:
sys.exit(1)
root = tree.getroot()
## prep
try:
os.remove(sqlFile)
except:
pass
## Setup the DB connections
con = lite.connect(sqlFile)
con.text_factory = str
db = con.cursor()
## Be friendly to GUI SQL visualization
db.execute("""CREATE TABLE `_` (`_` INTEGER)""")
## Generate initial tables
dBase = db_handler.Database(db)
dBase.scan_prep()
dBase.svc_prep()
con.commit()
## Generate scan info
scan = scan_handler.Scan(con, db, lHandler, root, xHandler)
con.commit()
## Generate stats and closeout
stats = stats_handler.Stats(db)
stats.by_addr()
stats.by_port()
stats.by_svc()
addrStats = stats.addrStats()
portStats = stats.portStats()
svcStats = stats.svcStats()
con.commit()
con.close()
## Plot the GUIs
plot_handler.Plotter(addrStats, htmlFile + 'byAddr.html', autoOpen = True)
plot_handler.Plotter(portStats, htmlFile + 'byPort.html', autoOpen = True)
plot_handler.Plotter(svcStats, htmlFile + 'bySvc.html', autoOpen = True)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description = 'scanParser - Visualize port scan data')
group = parser.add_mutually_exclusive_group(required = True)
group.add_argument('-v',
help = 'Visualize xml input file')
group.add_argument('-p',
help = 'Prettify raw xml output from various scanners')
args = parser.parse_args()
if args.v is not None:
xHandler = xml_handler.Xml(args.v)
vScan(xHandler)
if args.p is not None:
xHandler = xml_handler.Xml(args.p)
pScan(xHandler)