-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanparsing.py
332 lines (265 loc) · 10.5 KB
/
scanparsing.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
import xmltodict
import requests
import re
import json
import collections
import pandas as pd
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
import mpld3
from mpld3._server import serve
from scanning import date, time
#------EXPORTING NMAP AND OPENVAS-----------------------
#openvas importing
def open_vas(file):
cve_list_ovas = []
openvas_cve_by_port = {}
threat_levels = []
threats = []
with open('report-4dd0227c-bca8-4aad-8eaa-a4f04acf9f34.xml') as fd:
doc = xmltodict.parse(fd.read())
results = doc['report']['report']['results']['result']
for result in results:
threat = {}
threat['name'] = result['name']
threat['port'] = result['port']
threat['cvss_base'] = result['nvt']['cvss_base']
threat['cve'] = result['nvt']['cve']
threat['tags'] = result['nvt']['tags']
threat['threat'] = result['threat']
threat['severity'] = result['severity']
threat['description'] = result['description']
threats.append(threat)
cve = result['nvt']['cve'].split(", ")
for item in cve:
cve_list_ovas.append(item)
threat_levels.append(result['threat'])
if result['port'] in [*openvas_cve_by_port]:
openvas_cve_by_port[result['port']].append(item)
else:
openvas_cve_by_port[result['port']] = []
return cve_list_ovas, threat_levels, threats, openvas_cve_by_port
#nmap importing
def nmap():
cve_list_nmap = []
threats = []
with open('nmap.xml') as fd:
doc = xmltodict.parse(fd.read())
results = doc['nmaprun']['host']['ports']['port']
for port in results:
threat = {'port': {}, 'service': {}, 'script': {}}
threat['port']['port_number']=port['@portid']
threat['port']['protocol']=port['@protocol']
try:
threat['service']['name']=port['service']['@name']
except:
pass
try:
threat['service']['product']=port['service']['@product']
except:
pass
try:
threat['service']['version']=port['service']['@version']
except:
pass
try:
threat['script'] = port['script']['@output']
except:
pass
threats.append(threat)
nmap_cve = []
nmap_cve_by_port = {}
for port in threats:
try:
cves =re.findall(r"CVE-\d{4}-\d{4}", port['script'])
cve_without_cve = re.findall(r"\d{4}-\d{4}", port['script'])
nmap_cve_by_port[port['port']['port_number']] = cves
for item in cve_without_cve:
nmap_cve.append("CVE-" + item)
nmap_cve_by_port[port['port']['port_number']].append("CVE-" + item)
for cve in cves:
nmap_cve.append(cve)
except:
pass
return threats, nmap_cve, nmap_cve_by_port
#----------PARSING OPENVAS AND NMAP------------
#openvas parsing
openvas_cve, threat_levels, threats, openvas_cve_by_port = open_vas()
#print(openvas_cve_by_port)
openvas_ports = []
ports = []
threats_by_port_openvas = {}
for threat in threats:
openvas_ports.append(threat['port'])
ports = list(set(ports))
for port in openvas_ports:
threat_items = []
for item in threats:
threat_item = {}
if item['port'] == port:
threat_item['name'] = item['name']
threat_item['severity'] = float(item['severity'])
threat_items.append(threat_item)
threats_by_port_openvas[port] = threat_items
#nmap parsing
nmap_threats, nmap_cve,nmap_cve_by_port = nmap()
print(nmap_cve_by_port)
nmap_ports = []
threats_by_port_nmap = {}
for item in nmap_threats:
nmap_ports.append(int(item['port']['port_number']))
links = ['https://vuldb.com', 'https://cve.mitre.org', 'https://www.securityfocus.com/bid/', 'https://exchange.xforce.ibmcloud.com/', 'https://www.exploit-db.com', 'http://www.openvas.org', 'https://www.securitytracker.com', 'http://www.osvdb.org']
for port in nmap_ports:
threats = []
for item in nmap_threats:
if(int(item['port']['port_number']) == port):
if item['script']:
strings = item['script'].split('\n')
for string in strings:
for link in links:
if link in string :
strings.remove(string)
threats = strings
#-------------------------------------Merging Openvas and Nmap CVEs-------------------------------------------------
number_of_vulns_by_port = {}
#----------------
cves_by_port = {}
#-----------------
cves_by_port = nmap_cve_by_port
for port in [*openvas_cve_by_port]:
cve_ = []
if port[:-4] in [*cves_by_port]:
for item in openvas_cve_by_port[port]:
cves_by_port[port[:-4]].append(item)
else:
cve_.append(item)
else:
cves_by_port[port[:-4]] = cve_
for port in cves_by_port:
number_of_vulns_by_port[port] = len(cves_by_port[port])
print(number_of_vulns_by_port)
'''---------------------Merging Openvas and Nmap-----------------'''
#Removing duplicates from openvas and nmap
unique_openvas_cves = set(openvas_cve)
unique_nmap_cves = set(nmap_cve)
all_cves = list(unique_openvas_cves.union(unique_nmap_cves))
cve_severity = {}
cve_count_by_severity = {'Low':0, 'Moderate':0, 'Important':0, 'Unknown':0}
for cve in all_cves:
try:
severity = json.loads(requests.get("https://access.redhat.com/hydra/rest/securitydata/cve/" + cve + ".json").text)
if severity["threat_severity"]=='Low':
cve_severity[cve]=1
cve_count_by_severity['Low'] += 1
elif severity["threat_severity"]=='Moderate':
cve_severity[cve]=2
cve_count_by_severity['Moderate'] += 1
elif severity["threat_severity"]=='Important':
cve_severity[cve]=3
cve_count_by_severity['Important'] += 1
except:
cve_severity[cve] = "0"
cve_count_by_severity['Unknown'] += 1
pass
#-------------------------------GRAPHS AND FINAL ILLUSTRATIONS----------------------------------------------
#total vulnerable CVEs
#--------------------------------------------------------------------------------------------------
total_vulns_keys = []
for key in [*number_of_vulns_by_port]:
total_vulns_keys.append(key)
patches, texts = plt.pie([int(v) for v in number_of_vulns_by_port.values()], autopct=None)
labels=[k for k in total_vulns_keys]
sort_legend = True
if sort_legend:
patches, labels, dummy = zip(*sorted(zip(patches, labels, number_of_vulns_by_port.values()),
key=lambda x: x[2],
reverse=True))
plt.title("Percentage of vulnerabilities based on service")
plt.legend(patches, labels, loc='left center', bbox_to_anchor=(-0.1, 1.),
fontsize=8)
plt.savefig('pie.png', dpi=300)
plt.show()
#------------------------------------------------------------------------------------------------------
# # #CVE graph by severity
# # # set width of bar
barWidth = 0.25
# # # set height of bar
bars1 = [cve_count_by_severity['Important'], cve_count_by_severity['Important']]
bars2 = [cve_count_by_severity['Moderate'], cve_count_by_severity['Moderate']]
bars3 = [cve_count_by_severity['Low'], cve_count_by_severity['Low']]
# # # Set position of bar on X axis
r1 = np.arange(len(bars1))
r2 = [x + barWidth for x in r1]
r3 = [x + barWidth for x in r2]
# # # Make the plot
fig2 = plt.figure()
plt.title("Overview of the scanning history of the target")
plt.bar(r1, bars1, color='#ff2d00', width=barWidth, edgecolor='white', label='Important')
plt.bar(r2, bars2, color='#fff300', width=barWidth, edgecolor='white', label='Moderate')
plt.bar(r3, bars3, color='#00ff5d', width=barWidth, edgecolor='white', label='Low')
# # # Add xticks on the middle of the group bars
plt.xlabel('Severity', fontweight='bold')
plt.ylabel('Number of CVEs', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))], ['01/19/2020', '02/22/2020'])
# # # Create legend & Show graphic
plt.legend()
plt.savefig('history.png', dpi=300)
plt.show()
cves_port = cves_by_port
# # #Total CVEs detected and the list of CVEs
total_cves = 0
list_of_cves = []
cves_info_by_port = {}
for key in [*cves_port]:
cves_info_by_port[key] = []
for port in [*cves_port]:
for cve in cves_port[port]:
cve_info = {}
try:
severity = json.loads(requests.get("https://access.redhat.com/hydra/rest/securitydata/cve/" + cve + ".json").text)
cve_info["name"] = cve
cve_info["description"] = severity["bugzilla"]["description"]
if severity["threat_severity"]=='Low':
cve_info["severity"]=1
elif severity["threat_severity"]=='Moderate':
cve_info["severity"]=2
elif severity["threat_severity"]=='Important':
cve_info["severity"]=3
except:
cve_info["name"] = cve
cve_info["description"] = "unknonwn"
cve_info["severity"]= 0
cves_info_by_port[port].append(cve_info)
print(cves_info_by_port)
import plotly
import plotly.graph_objects as go
for key in [*cves_info_by_port]:
name = []
description = []
severity = []
severity_count = {'3':0, '2':0, '1':0}
for cve in cves_info_by_port[key]:
if cve['description'] == 'unknonwn':
continue
else:
try:
severity.append(cve['severity'])
name.append(cve['name'])
description.append(cve['description'])
except:
pass
for item in severity:
if item == 1:
severity_count['1']+=1
elif item == 2:
severity_count['2']+=1
elif item == 3:
severity_count['3']+=1
barlist = plt.bar(range(len(severity_count)), list(severity_count.values()),color=(0.2,0.4,0.6), align='center')
plt.title("Severity of CVEs of port " + key)
barlist[0].set_color('r')
barlist[1].set_color('y')
barlist[2].set_color('g')
plt.savefig("bar" + key + ".png", dpi=300)
plt.show()