-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_search.py
136 lines (110 loc) · 3.93 KB
/
string_search.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
import requests, csv
from bs4 import BeautifulSoup
from collections import defaultdict
class CVEDetailsSearch:
def __init__(self):
return None
def main(self):
cve_url = input('Paste the link from CVEDetails you want to search here: ')
search_string = input('Insert the value you\'re looking for. Simply press enter if you don\'t want to search for anything: ')
filename = input('Insert filename for the CSV we\'ll create. If none is entered \'temp_cve\' will be used: ')
req_text = self.get_link_text(cve_url)
vuln_list = self.get_vuln_list(req_text)
link_list = self.get_paging_list(req_text)
for link in link_list:
link_text = self.get_link_text(link)
new_vuln_list = self.get_vuln_list(link_text)
# We extend the current list with the list we get
vuln_list = {**vuln_list, **new_vuln_list}
v_list = self.filter_list(vuln_list, search_string)
if filename:
self.write_to_csv(v_list,filename)
else:
self.write_to_csv(v_list)
def filter_list(self, vuln_list, search_string):
# If there is no search term we skip the search.
if not search_string:
return vuln_list
# Look for the item in the description, if it exists we do nothing, otherwise we delete the csv.
keys_to_drop = []
for key, vals in vuln_list.items():
if not search_string in vals[8]:
keys_to_drop.append(key)
for key in keys_to_drop:
del vuln_list[key]
return vuln_list
def write_to_csv(self,values, filename = 'temp_cve'):
filename = filename + '.csv'
cve_list = []
for key, vals in values.items():
x = [key]
x.extend(vals)
cve_list.append(x)
cve_list = sorted(cve_list, key=lambda cve_list:float(cve_list[2]), reverse=True)
cve_list_titles = [['CVE', 'Short Description', 'Severity', 'Access', 'Complexity', 'Authentication', 'Confidentiality', 'Integrity', 'Availability', 'Full Description']]
cve_list = cve_list_titles + cve_list
with open(filename, 'w', newline='') as f:
writer = csv.writer(f, dialect='excel')
writer.writerows(cve_list)
print('The file \''+filename+'\' was created successfully')
def get_link_text(self,link):
if 'www.cvedetails.com' not in link:
link = 'https://www.cvedetails.com'+ link
r = requests.get(link)
if(r.status_code == 200):
return r.text
else:
raise Exception('The link did\'t work! This is the link that malfunctioned: \n\''+link+'\'')
def get_paging_list(self, page):
bs = BeautifulSoup(page, 'html.parser')
link_list = []
# Find the relevant paging div
paging = bs.find_all(id='pagingb')
paging = paging[0].find_all('a')
# Go through the links
for l in paging:
link = l['href']
link_list.append(link)
return link_list
def get_vuln_list(self, get_text):
bs = BeautifulSoup(get_text, 'html.parser')
cve_table = bs.find_all(id='vulnslisttable')[0]
# Removes the label row
cve_table = cve_table.find_all('tr')[1:]
# shows every second item and every second item after the first respectively
cve_rows = cve_table[::2]
cve_deets = cve_table[1::2]
cve_dict = defaultdict(list)
for cve, cve_details in zip(cve_rows,cve_deets):
x = cve.find_all('td')
# Dict index
cve_number = x[1].a.text
# 0
cve_resumed = x[4].text.strip()
# 1
cve_severity = x[7].div.text
# 2
cve_access = x[9].text
# 3
cve_complexity = x[10].text
# 4
cve_authentication = x[11].text
# 5
cve_conf = x[12].text
# 6
cve_integ = x[13].text
# 7
cve_avail = x[14].text
# If string for resumed vul is empty we state the resumed version is not available.
if not cve_resumed:
cve_resumed = 'N/A'
if cve_resumed.startswith('+'):
cve_resumed = cve_resumed[1:]
# 8
cve_description = cve_details.td.text.strip()
# Append values to CVE dictionary
cve_dict[cve_number].extend([cve_resumed,cve_severity,cve_access, cve_complexity, cve_authentication, cve_conf, cve_integ,cve_avail,cve_description])
return cve_dict
if __name__ == '__main__':
cve_search = CVEDetailsSearch()
cve_search.main()