This repository has been archived by the owner on Nov 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_vulnerability_scan.py
executable file
·90 lines (79 loc) · 3.43 KB
/
01_vulnerability_scan.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
#!/usr/bin/env python
from __future__ import print_function
import sys
import json
from util import get_url
import csv
data = {}
ADVISORY_ID = "advisory_id"
ADVISORY_TITLE = "advisory_title"
BUG_IDS = "bug_ids"
CVES = "cves"
CVRF_URL = "cvrfUrl"
CVSS_BASE_SCORE = "cvss_base_score"
CWE = "cwe"
FIRST_PUBLISHED = "first_published"
IPS_SIGNATURES = "ips_signatures"
LAST_UPDATED = "last_updated"
PRODUCT_NAMES = "product_names"
PUBLICATION_URL = "publication_url"
SIR = "sir"
SUMMARY = "summary"
def list_single_device(ip):
return get_url("network-device/ip-address/%s" % ip)
def list_network_devices():
return get_url("network-device")
def append_to_json(filepath, data):
# construct JSON fragment as new file ending
new_ending = ", " + "{"+ json.dumps(data)[1:-1] + "}\n" + "]"
# edit the file in situ - first open it in read/write mode
with open(filepath, 'r+') as f:
f.seek(0, 2) # move to end of file
index = f.tell() # find index of last byte
# walking back from the end of file, find the index
# of the original JSON's closing '}'
while not f.read().startswith(']'):
index -= 1
if index == 0:
raise ValueError("can't find JSON object in {!r}".format(filepath))
f.seek(index)
# starting at the original ending } position, write out
# the new ending
f.seek(index)
f.write(new_ending)
if __name__ == "__main__":
if len(sys.argv) > 1:
response = list_single_device(sys.argv[1])
print(json.dumps(response, indent=2))
else:
response = list_network_devices()
vulnfile="vulnerabilities.json"
file = open(vulnfile, 'r')
jdata = json.load(file)
for element in jdata:
for device in response['response']:
if any(device['series'] in s for s in element['product_names']) or any(device['softwareVersion'] in p for p in element['product_names']):
append_to_json('vulnerability.json', element)
with open("vulnerability.json", "r") as sample:
data = json.load(sample)
unique = { each['advisory_id'] : each for each in data}.values()
with open('vulnerability.csv', 'w', newline='') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
csvwriter.writerow([ADVISORY_ID, ADVISORY_TITLE, BUG_IDS, CVES, CVRF_URL, CVSS_BASE_SCORE, CWE, FIRST_PUBLISHED, IPS_SIGNATURES, LAST_UPDATED, PRODUCT_NAMES, PUBLICATION_URL, SIR, SUMMARY])
for item in unique:
bug_ids = ""
cves = ""
cwe = ""
ips_sig = ""
prod_names = ""
for i in item[BUG_IDS]:
bug_ids += "{}; ".format(i)
for i in item[CVES]:
cves += "{}; ".format(i)
for i in item[CWE]:
cwe += "{}; ".format(i)
for i in item[IPS_SIGNATURES]:
ips_sig += "{}; ".format(i)
for i in item[PRODUCT_NAMES]:
prod_names += "{}; ".format(i)
csvwriter.writerow([item[ADVISORY_ID], item[ADVISORY_TITLE], bug_ids, cves, item[CVRF_URL], item[CVSS_BASE_SCORE], cwe, item[FIRST_PUBLISHED], ips_sig, item[LAST_UPDATED], prod_names, item[PUBLICATION_URL], item[SIR], item[SUMMARY]])