This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
simplenessusparser.py
282 lines (254 loc) · 10.3 KB
/
simplenessusparser.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
import csv
from collections import OrderedDict
import xlsxwriter
import sys
if len(sys.argv) < 3:
print('Missing arguments:\n\t{} [input_file.csv] [output_file.xlsx]'.format(sys.argv[0]))
sys.exit()
networks_hosts_count = {}
ports_protocols = {}
high_critical_ports_protocols = {}
high_critical_detailed = {}
all_hosts = set()
misconfigured_count = 0
outdated_count = 0
cols_replacements = (('0', 'A'), ('1', 'B'), ('2', 'C'), ('3', 'D'), ('4', 'E'), ('5', 'F'), ('6', 'G'))
def col_replace(column):
for old, new in cols_replacements:
column = str(column).replace(old, new)
return column
with open(sys.argv[1]) as csv_file:
csv_reader = csv.DictReader(csv_file, delimiter=',')
counter = 0
for row in csv_reader:
if counter > 0:
name = row['Name']
host = row['Host']
risk = row['Risk']
port = row['Port']
solution = row['Solution']
protocol = row['Protocol']
network = '.'.join(host.split('.')[:-1])
if network not in networks_hosts_count:
networks_hosts_count[network] = {'None': 0, 'Low': 0, 'Medium': 0, 'High': 0, 'Critical': 0, 'Hosts': 0}
if host not in all_hosts:
all_hosts.add(host)
networks_hosts_count[network]['Hosts'] += 1
networks_hosts_count[network][risk] += 1
if risk != 'None':
if port != '0':
if port not in ports_protocols:
ports_protocols[port] = {}
ports_protocols[port][protocol] = 1
elif protocol not in ports_protocols[port]:
ports_protocols[port][protocol] = 1
else:
ports_protocols[port][protocol] += 1
if risk == 'Critical' or risk == 'High':
if port != 0:
if port not in high_critical_ports_protocols:
high_critical_ports_protocols[port] = {}
high_critical_ports_protocols[port][protocol] = 1
elif protocol not in high_critical_ports_protocols[port]:
high_critical_ports_protocols[port][protocol] = 1
else:
high_critical_ports_protocols[port][protocol] += 1
if name not in high_critical_detailed:
high_critical_detailed[name] = {}
high_critical_detailed[name]['counter'] = 1
high_critical_detailed[name]['hosts'] = set()
high_critical_detailed[name]['hosts'].add(host)
high_critical_detailed[name]['solution'] = solution
else:
high_critical_detailed[name]['counter'] += 1
high_critical_detailed[name]['hosts'].add(host)
if 'update' in solution or 'Update' in solution or 'upgrade' in solution or 'Upgrade' in solution:
outdated_count += 1
else:
misconfigured_count += 1
counter += 1
total_vulns = networks_hosts_count[network]['Low'] + networks_hosts_count[network]['Medium'] + \
networks_hosts_count[network]['High'] + networks_hosts_count[network]['Critical']
# xlsx file output
row = 0
col = 0
workbook = xlsxwriter.Workbook(sys.argv[2])
worksheet = workbook.add_worksheet()
centered = workbook.add_format({'align': 'center'})
red_centered = workbook.add_format({'font_color': 'red', 'align': 'center'})
green = workbook.add_format({'font_color': 'green'})
bold = workbook.add_format({'bold': True})
bold_centered = workbook.add_format({'bold': True, 'locked': True, 'align': 'center'})
worksheet.write(row, col, 'Hosts')
col += 1
worksheet.write(row, col, len(all_hosts), centered)
col += 2
worksheet.write(row, col, 'Networks')
col += 1
worksheet.write(row, col, len(networks_hosts_count), centered)
for network in networks_hosts_count:
col = 0
row += 2
worksheet.write(row, col, 'Network')
col += 1
worksheet.write(row, col, network, centered)
col += 2
worksheet.write(row, col, 'Hosts')
col += 1
worksheet.write(row, col, networks_hosts_count[network]['Hosts'], centered)
col -= 1
row += 2
worksheet.write(row, col, 'Info')
col += 1
worksheet.write(row, col, networks_hosts_count[network]['None'], centered)
col = 1
worksheet.write(row, col, 'Low')
col += 1
worksheet.write(row, col, networks_hosts_count[network]['Low'], centered)
col = 1
row += 1
worksheet.write(row, col, 'Medium')
col += 1
worksheet.write(row, col, networks_hosts_count[network]['Medium'], centered)
col = 1
row += 1
worksheet.write(row, col, 'High')
col += 1
worksheet.write(row, col, networks_hosts_count[network]['High'], red_centered)
col = 1
row += 1
worksheet.write(row, col, 'Critical')
col += 1
worksheet.write(row, col, networks_hosts_count[network]['Critical'], red_centered)
col = 1
row += 1
worksheet.write(row, col, 'Total', bold)
col += 1
worksheet.write_formula(row, col, '=SUM({}{}:{}{})'.format(col_replace(col), row - 3, col_replace(col), row),
bold_centered, total_vulns)
row += 3
col = 0
worksheet.write(row, col, 'Vulnerabilities by port')
row += 2
col += 1
worksheet.write(row, col, 'Port', centered)
col += 1
worksheet.write(row, col, 'Protocol', centered)
col += 1
worksheet.write(row, col, 'Total', bold_centered)
for port in ports_protocols:
for protocol in ports_protocols[port]:
row += 1
col = 1
worksheet.write(row, col, port, centered)
col += 1
worksheet.write(row, col, protocol, centered)
col += 1
worksheet.write(row, col, ports_protocols[port][protocol], bold_centered)
row += 3
col = 0
worksheet.write(row, col, 'Critical / High vulnerabilities by port')
row += 2
col += 1
worksheet.write(row, col, 'Port', centered)
col += 1
worksheet.write(row, col, 'Protocol', centered)
col += 1
worksheet.write(row, col, 'Total', bold_centered)
for port in high_critical_ports_protocols:
for protocol in high_critical_ports_protocols[port]:
row += 1
col = 1
worksheet.write(row, col, port, centered)
col += 1
worksheet.write(row, col, protocol, centered)
col += 1
worksheet.write(row, col, high_critical_ports_protocols[port][protocol], bold_centered)
row += 3
col = 0
worksheet.write(row, col, 'Critical / High vulnerabilities detailed')
for name in high_critical_detailed:
row += 2
col = 1
worksheet.write(row, col, 'Vuln')
col += 1
worksheet.write(row, col, name, red_centered)
row += 1
col = 1
worksheet.write(row, col, 'Number of affected hosts')
col += 1
worksheet.write(row, col, high_critical_detailed[name]['counter'], centered)
row += 1
col = 1
worksheet.write(row, col, 'Affected hosts addresses')
col += 1
hosts = ''
for host in high_critical_detailed[name]['hosts']:
hosts += '{}; '.format(host)
worksheet.write(row, col, hosts, centered)
row += 1
col = 1
worksheet.write(row, col, 'Solution')
col += 1
solution = high_critical_detailed[name]['solution'].split()
solution = ' '.join(solution)
worksheet.write(row, col, solution, green)
row += 3
col = 0
other_causes_count = total_vulns - misconfigured_count - outdated_count
worksheet.write(row, col, 'Following data is not accurate!!!')
row += 2
col = 1
worksheet.write(row, col, 'Misconfigurations')
col += 1
worksheet.write(row, col, misconfigured_count, centered)
col = 1
row += 1
worksheet.write(row, col, 'Outdated software')
col += 1
worksheet.write(row, col, outdated_count, centered)
col = 1
row += 1
worksheet.write(row, col, 'Other causes')
col += 1
worksheet.write(row, col, other_causes_count, centered)
workbook.close()
# console output
if len(networks_hosts_count) < 2:
print('\n\nScanned {} hosts in {} network'.format(len(all_hosts), len(networks_hosts_count)))
else:
print('\n\nScanned {} hosts in {} networks'.format(len(all_hosts), len(networks_hosts_count)))
for network in networks_hosts_count:
total_vulns = 0
for x in networks_hosts_count[network]:
if x != 'None' and x != 'Hosts':
total_vulns += networks_hosts_count[network][x]
print('\n\nNetwork {}.X\t\tHosts:\t{}\t\tInfo:\t{}'.format(network, networks_hosts_count[network]['Hosts'],
networks_hosts_count[network]['None']))
print('\n\tLow:\t\t{}\n\tMedium:\t\t{}\n\tHigh:\t\t{}\n\tCritical:\t{}\n\tTotal:\t\t{}'.format(
networks_hosts_count[network]['Low'], networks_hosts_count[network]['Medium'],
networks_hosts_count[network]['High'], networks_hosts_count[network]['Critical'], total_vulns))
print('\n\nVulnerabilities by port:\n')
ports_protocols = OrderedDict(sorted(ports_protocols.items(), key=lambda y: int(y[0])))
for port in ports_protocols:
for protocol in ports_protocols[port]:
print('\t{}\t{} \t\t--> {}'.format(port, protocol, ports_protocols[port][protocol]))
print('\n\nCritical / High vulnerabilities by port:\n')
high_critical_ports_protocols = OrderedDict(sorted(high_critical_ports_protocols.items(), key=lambda y: int(y[0])))
for port in high_critical_ports_protocols:
for protocol in high_critical_ports_protocols[port]:
print('\t{} {} \t\t--> {}'.format(port, protocol, high_critical_ports_protocols[port][protocol]))
print('\n\nCritical / High vulnerabilities detailed:\n')
for name in high_critical_detailed:
print('\tVuln:\t\t\t\t\t\t{}'.format(name))
print('\tNumber of affected hosts:\t{}'.format(high_critical_detailed[name]['counter']))
hosts = ''
for host in high_critical_detailed[name]['hosts']:
hosts += '{}; '.format(host)
print('\tAffected hosts addresses:\t{}'.format(hosts))
solution = high_critical_detailed[name]['solution'].split()
solution = ' '.join(solution)
print('\tSolution:\t\t\t\t\t{}'.format(solution))
print('\n\n\nFollowing data is not accurate!!!')
print('\n\tMisconfigurations:\t{}\n\tOutdated software:\t{}\n\tOther causes:\t{}'.format(
misconfigured_count, outdated_count, other_causes_count))