-
Notifications
You must be signed in to change notification settings - Fork 106
/
nmap_doc_generator.py
136 lines (130 loc) · 5.62 KB
/
nmap_doc_generator.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
#!/usr/bin/env python
'''
Author: Chris Duffy
Date: May 2015
Name: nmap_doc_generator.py
Purpose: A script that takes data from parsed nmap XML files and writes it into XLSX files
Copyright (c) 2015, Christopher Duffy All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met: * Redistributions
of source code must retain the above copyright notice, this list of conditions and
the following disclaimer. * Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution. * Neither the
name of the nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL CHRISTOPHER DUFFY BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''
import sys
try:
import xlsxwriter
except:
sys.exit("[!] Install the xlsx writer library as root or through sudo: pip install xlsxwriter")
class Nmap_doc_generator():
def __init__(self, verbose, hosts_dict, filename, simple):
self.hosts_dict = hosts_dict
self.filename = filename
self.verbose = verbose
self.simple = simple
try:
self.run()
except Exception as e:
print(e)
def run(self):
print ("") #DEBUG
# Run the appropriate module
if self.verbose > 0:
print ("[*] Building %s.xlsx") % (self.filename)
self.generate_xlsx()
def generate_xlsx(self):
if "xls" or "xlsx" not in self.filename:
self.filename = self.filename + ".xlsx"
workbook = xlsxwriter.Workbook(self.filename)
# Row one formatting
format1 = workbook.add_format({'bold': True})
# Header color
# Find colors: http://www.w3schools.com/tags/ref_colorpicker.asp
if self.simple:
format1.set_bg_color('#538DD5')
else:
format1.set_bg_color('#33CC33') # Report Format
# Even row formatting
format2 = workbook.add_format({'text_wrap': True})
format2.set_align('left')
format2.set_align('top')
format2.set_border(1)
# Odd row formatting
format3 = workbook.add_format({'text_wrap': True})
format3.set_align('left')
format3.set_align('top')
# Row color
if self.simple:
format3.set_bg_color('#C5D9F1')
else:
format3.set_bg_color('#99FF33') # Report Format
format3.set_border(1)
if self.verbose > 0:
print ("[*] Creating Workbook: %s") % (self.filename)
# Generate Worksheet 1
worksheet = workbook.add_worksheet("All Ports")
# Column width for worksheet 1
worksheet.set_column(0, 0, 20)
worksheet.set_column(1, 1, 17)
worksheet.set_column(2, 2, 22)
worksheet.set_column(3, 3, 8)
worksheet.set_column(4, 4, 26)
worksheet.set_column(5, 5, 13)
worksheet.set_column(6, 6, 12)
# Define starting location for Worksheet one
row = 1
col = 0
# Generate Row 1 for worksheet one
worksheet.write('A1', "Hostname", format1)
worksheet.write('B1', "Address", format1)
worksheet.write('C1', "Hardware Address", format1)
worksheet.write('D1', "Port", format1)
worksheet.write('E1', "Service Name", format1)
worksheet.write('F1', "Protocol", format1)
worksheet.write('G1', "Port State", format1)
worksheet.autofilter('A1:G1')
# Populate Worksheet 1
for key, value in self.hosts_dict.items():
try:
hostname = value[0]
address = value[1]
protocol = value[2]
port = value[3]
service_name = value[4]
hwaddress = value[5]
state = value[6]
except:
if self.verbose > 3:
print("[!] An error occurred parsing host ID: %s for Worksheet 1") % (key)
try:
if row % 2 != 0:
temp_format = format2
else:
temp_format = format3
worksheet.write(row, col, hostname, temp_format)
worksheet.write(row, col + 1, address, temp_format)
worksheet.write(row, col + 2, hwaddress, temp_format)
worksheet.write(row, col + 3, port, temp_format)
worksheet.write(row, col + 4, service_name, temp_format)
worksheet.write(row, col + 5, protocol, temp_format)
worksheet.write(row, col + 6, state, temp_format)
row += 1
except:
if self.verbose > 3:
print("[!] An error occurred writing data for Worksheet 1")
try:
workbook.close()
except:
sys.exit("[!] Permission to write to the file or location provided was denied")