forked from aress31/ness6nmap2xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathness6nmap2xlsx.py
631 lines (539 loc) · 18.9 KB
/
ness6nmap2xlsx.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#!/usr/bin/env python3
# Copyright (C) 2017 Alexandre Teyar
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this output_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.
import argparse
import logging
import sys
import time
import xlsxwriter
from nessrest import ness6rest
from parsers import nessus
from parsers import nmap
# custom levels for the logging lib
RESULT = 21
# help required for:
# add findings breakdown
# add new worksheet 'Vulnerabilities' including 'Remediation'
# checking files extension
# forbidding '--list' and '-oX' arguments to be used together
# making 'folders' or 'scans' or list 'mandatory'
def parse_args():
""" Parse and validate the command line
"""
parser = argparse.ArgumentParser(
description=(
"Parse Nessus results into an Excel spreadsheet for quicker "
"and easier reporting"
)
)
# generic options
parser.add_argument(
"-d",
"--debug",
action="store_const",
const=logging.DEBUG,
default=logging.INFO,
dest="loglevel",
help="enable debug output",
required=False
)
parser.add_argument(
"-oX",
"-output--xml",
dest="output_file",
help="output results in XLSX to the given filename",
required=False,
type=str
)
subparsers = parser.add_subparsers(
dest="subcommand"
)
subparsers.required = True
# Nessus subparser
nessus_parser = subparsers.add_parser("nessus")
nessus_exclusion_group = nessus_parser.add_mutually_exclusive_group(
required=False
)
nessus_parser.add_argument(
"-c",
"--config",
dest="config_file",
help="Configuration file for custom vulnerabilities",
type=argparse.FileType("r")
)
nessus_exclusion_group.add_argument(
"-f",
"--folders",
dest="folders",
help="folder(s) to process (support regular expressions)",
nargs="+",
type=str
)
nessus_parser.add_argument(
"--host",
default="localhost",
dest="host",
help="hostname (default value: localhost)",
required=False,
type=str
)
nessus_parser.add_argument(
"-l",
"--login",
dest="login",
help="login for Nessus authentication",
required=True,
type=str
)
nessus_parser.add_argument(
"--list",
choices=[
"folders",
"scans"
],
dest="list",
help="list folder(s) or scan(s) (support regular expressions)",
required=False,
type=str
)
nessus_parser.add_argument(
"-p",
"--password",
dest="password",
help="password for Nessus authentication",
required=True,
type=str
)
nessus_parser.add_argument(
"--port",
default="8834",
dest="port",
help="port (default value: 8834)",
required=False,
type=str
)
nessus_exclusion_group.add_argument(
"-s",
"--scans",
dest="scans",
help="scan(s) to process (support regular expressions)",
nargs="+",
type=str
)
# Nmap subparser
nmap_parser = subparsers.add_parser("nmap")
nmap_parser.add_argument(
"-iX",
"--input-xml",
dest="input_files",
help="XML scan results files(s)",
nargs="+",
required=True,
type=argparse.FileType("r")
)
return parser.parse_args()
# Help required for:
# adding column formula to convert 'severity' to 'text'
def write_worksheet(workbook, worksheet, freeze_panes_count,
table_headers, table_data):
""" Create an Excel worksheet containing the 'table_headers'
and 'table_data' dataset
"""
if not table_data:
logging.warning("'{}' has not been created - empty dataset".format(
worksheet
))
return
else:
worksheet = workbook.add_worksheet("{}".format(worksheet))
column_count = 0
row_count = 0
table_column_count = column_count + len(table_headers) - 1
table_row_count = row_count + len(table_data)
logging.debug("{}".format(table_headers))
for table_datum in table_data:
logging.debug("{}".format(table_datum))
worksheet.add_table(
row_count,
column_count,
table_row_count,
table_column_count,
{
"banded_rows": True,
"columns": table_headers,
"data": table_data,
"first_column": True,
"style": "Table Style Medium 1"
}
)
# freeze the two first columns in the worksheet
for i in range(freeze_panes_count):
worksheet.freeze_panes(0, i + 1)
def parse_ness_host_vulns(workbook, scanner, scans, config_file=None):
table_data = []
table_headers = [
{"header": "Scan"},
{"header": "Host IP"},
{"header": "Port"},
{"header": "Vulnerability"},
{"header": "Severity Rating"}
]
for scan in scans:
host_vulns = nessus.get_host_vulns(scanner, scan)
if config_file:
host_vulns = nessus.post_process_vulns(
config_file, host_vulns, type=0
)
for host_id, plugin_id in host_vulns.items():
for plugin_id, values in plugin_id.items():
for value in values:
table_data.append(
[
value["scan"],
value["host_ip"],
";".join(value["plugin_output"]["ports"]),
value["plugin_name"],
value["severity"]
]
)
write_worksheet(workbook, "Hosts vs Vulnerabilties", 2,
table_headers, table_data)
def parse_ness_vuln_hosts(workbook, scanner, scans, config_file=None):
table_data = []
table_headers = [
{"header": "Scan"},
{"header": "Vulnerability"},
{"header": "IP Count"},
{"header": "Host IP"},
{"header": "Port Count"},
{"header": "Port"},
{"header": "Severity Rating"}
]
for scan in scans:
vuln_hosts = nessus.get_vuln_hosts(scanner, scan)
if config_file:
vuln_hosts = nessus.post_process_vulns(
config_file, vuln_hosts, type=1
)
for value in vuln_hosts.values():
# unify, sort and stringify
table_data.append(
[
";".join(sorted(set(value["scan"]))),
value["plugin_name"],
len(value["host_ip"]),
";".join(sorted(
set(value["host_ip"]),
key=lambda x: tuple(map(int, x.split('.')))
)),
len(set(value["plugin_output"]["ports"])),
";".join(sorted(
set(value["plugin_output"]["ports"]),
key=lambda x: int(x.split("/")[0])
)),
value["severity"]
]
)
write_worksheet(workbook, "Vulnerability vs Hosts", 2,
table_headers, table_data)
def parse_vulns(workbook, scanner, scans, config_file=None):
table_data = []
table_headers = [
{"header": "Scan"},
{"header": "Vulnerability"},
{"header": "IP Count"},
{"header": "Host IP"},
{"header": "Port Count"},
{"header": "Port"},
{"header": "Severity Rating"}
]
for scan in scans:
vuln_hosts = nessus.get_vuln_hosts(scanner, scan)
if config_file:
vuln_hosts = nessus.post_process_vulns(
config_file, vuln_hosts, type=1
)
for value in vuln_hosts.values():
# unify, sort and stringify
table_data.append(
[
";".join(sorted(set(value["scan"]))),
value["plugin_name"],
len(value["host_ip"]),
";".join(sorted(
set(value["host_ip"]),
key=lambda x: tuple(map(int, x.split('.')))
)),
len(set(value["plugin_output"]["ports"])),
";".join(sorted(
set(value["plugin_output"]["ports"]),
key=lambda x: int(x.split("/")[0])
)),
value["severity"]
]
)
write_worksheet(workbook, "Vulnerability vs Hosts", 2,
table_headers, table_data)
def parse_ness_host_oss(workbook, scanner, scans):
table_data = []
table_headers = [
{"header": "Scan"},
{"header": "Host IP"},
{"header": "Operating System"},
{"header": "Confidence Level"},
{"header": "Method"},
]
for scan in scans:
host_os = nessus.get_host_oss(scanner, scan)
for value in host_os.values():
table_data.append(
[
value["scan"],
value["host_ip"],
value["operating_system"],
value["confidence_level"],
value["method"]
]
)
write_worksheet(workbook, "Host vs OSs", 2, table_headers, table_data)
def parse_ness_os_hosts(workbook, scanner, scans):
table_data = []
table_headers = [
{"header": "Scan"},
{"header": "Operating System"},
{"header": "Host IP Count"},
{"header": "Host IP"},
{"header": "Method"}
]
for scan in scans:
os_hosts = nessus.get_os_hosts(scanner, scan)
for operating_system, value in sorted(os_hosts.items()):
# unify, sort and stringify
table_data.append(
[
";".join(sorted(set(value["scan"]))),
operating_system,
len(value["host_ip"]),
";".join(sorted(
set(value["host_ip"]),
key=lambda x: tuple(map(int, x.split('.')))
)),
";".join(sorted(set(value["method"])))
]
)
write_worksheet(workbook, "OS vs Hosts", 2, table_headers, table_data)
def parse_nmap_host_services(workbook, input_files):
table_data = []
table_headers = [
{"header": "File"},
{"header": "Host IP"},
{"header": "Port"},
{"header": "Protocol"},
{"header": "Service"},
{"header": "State"},
{"header": "Banner"},
{"header": "Reason"}
]
for input_file in input_files:
host_services = nmap.get_host_services(input_file)
for host_ip, values in sorted(host_services.items()):
for value in values:
table_data.append(
[
value["file"],
host_ip,
value["port"],
value["protocol"],
value["service"],
value["state"],
value["banner"],
value["reason"]
]
)
write_worksheet(workbook, "Host vs Services", 2,
table_headers, table_data)
def parse_nmap_host_oss(workbook, input_files):
table_data = []
table_headers = [
{"header": "File"},
{"header": "Host IP"},
{"header": "Operating System"},
{"header": "Accuracy"}
]
for input_file in input_files:
host_os = nmap.get_host_oss(input_file)
for host_ip, value in sorted(host_os.items()):
table_data.append(
[
value["file"],
host_ip,
value["name"],
value["accuracy"]
]
)
write_worksheet(workbook, "Host vs OSs", 2, table_headers, table_data)
def parse_nmap_os_hosts(workbook, input_files):
table_data = []
table_headers = [
{"header": "File"},
{"header": "Operating System"},
{"header": "Host IP Count"},
{"header": "Host IP"}
]
for input_file in input_files:
host_os = nmap.get_os_hosts(input_file)
for operating_system, value in sorted(host_os.items()):
# unify, sort and stringify
table_data.append(
[
";".join(sorted(set(value["file"]))),
operating_system,
len(value["host_ip"]),
";".join(sorted(
set(value["host_ip"]),
key=lambda x: tuple(map(int, x.split('.')))
))
]
)
write_worksheet(workbook, "OS vs Hosts", 2, table_headers, table_data)
def main():
try:
args = parse_args()
logging.addLevelName(RESULT, "RESULT")
logging.basicConfig(
format="%(levelname)-8s %(message)s",
handlers=[
logging.StreamHandler(sys.stdout)
],
level=args.loglevel
)
# disable the logging for the 'urllib3' lib
logging.getLogger("urllib3").setLevel(logging.CRITICAL)
# help required to:
# tidy-up this piece of code
if args.subcommand == "nessus":
if args.output_file and not args.list:
output_file = "{}.xlsx".format(args.output_file)
elif not args.output_file and not args.list:
output_file = "nessus-results_{}".format(
time.strftime("%Y%m%d-%H%M%S")
)
else:
output_file = "N/A"
elif args.subcommand == "nmap":
if args.output_file:
output_file = "{}".format(args.output_file)
else:
output_file = "nmap-results_{}".format(
time.strftime("%Y%m%d-%H%M%S")
)
if args.subcommand == "nessus":
# variables summary
logging.info("Nessus login: {}".format(args.login))
logging.info("Nessus password: {}".format(args.password))
if args.folders:
logging.info("Nessus folder(s): {}".format(
";".join(sorted(args.folders))
))
if args.scans:
logging.info("Nessus scan(s): {}".format(";".join(
sorted(args.scans))
))
logging.info("Nessus URL: https://{}:{}".format(
args.host, args.port
))
if args.config_file:
logging.info(
"Configuration file for Nessus vulnerabilities: {}".format(
args.config_file.name
)
)
logging.info("XLSX results output_file: {}.xlsx".format(
output_file
))
scanner = ness6rest.Scanner(
insecure=True,
login=args.login,
password=args.password,
url="https://{}:{}".format(args.host, args.port)
)
if args.list:
if args.list == "folders":
if args.folders:
results = nessus.get_folders(scanner, args.folders)
else:
results = nessus.get_all_folders(scanner)
elif args.list == "scans":
if args.folders:
results = nessus.fetch_scans(scanner, args.folders)
elif args.scans:
results = nessus.get_scans(scanner, args.scans)
else:
results = nessus.get_all_scans(scanner)
for result in results:
logging.log(RESULT, "{}".format(result["name"]))
elif args.folders or args.scans:
if args.folders:
scans = nessus.fetch_scans(scanner, args.folders)
elif args.scans:
scans = nessus.get_scans(scanner, args.scans)
if scans:
workbook = xlsxwriter.Workbook("{}.xlsx".format(
output_file
))
logging.log(
RESULT,
"generating 'Host vs Vulnerabilities' worksheet..."
)
parse_ness_host_vulns(
workbook, scanner, scans, config_file=args.config_file
)
logging.log(
RESULT,
"generating 'Vulnerability vs Hosts' worksheet..."
)
parse_ness_vuln_hosts(
workbook, scanner, scans, config_file=args.config_file
)
logging.log(
RESULT,
"generating 'Host vs OSs' worksheet..."
)
parse_ness_host_oss(workbook, scanner, scans)
logging.log(
RESULT,
"generating 'OS vs Hosts' worksheet..."
)
parse_ness_os_hosts(workbook, scanner, scans)
workbook.close()
elif args.subcommand == "nmap":
# variables summary
# help required to:
# add regex support
input_files = []
for input_file in args.input_files:
input_files.append(input_file.name)
logging.info("Nmap XML results file(s): {}".format(
";".join(sorted(input_files))
))
logging.info("XLSX results file: {}.xlsx".format(output_file))
workbook = xlsxwriter.Workbook("{}.xlsx".format(output_file))
logging.log(RESULT, "generating 'Host vs Services' worksheet...")
parse_nmap_host_services(workbook, input_files)
logging.log(RESULT, "generating 'Host vs OSs' worksheet...")
parse_nmap_host_oss(workbook, input_files)
logging.log(RESULT, "generating 'OS vs Hosts' worksheet...")
parse_nmap_os_hosts(workbook, input_files)
workbook.close()
except KeyboardInterrupt:
logging.exception("'CTRL+C' pressed, exiting...")
if __name__ == "__main__":
main()