forked from lukeber4/usn-search_old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcve_update.py
150 lines (120 loc) · 4.62 KB
/
cve_update.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
import re
import datetime
import requests
from pymongo import MongoClient
from bs4 import BeautifulSoup
def verify_status(status):
if status == "released" or status == "ignored" or status == "not-affected" or status == "needed" or status == "ignored" or status == "needs-triage" or status == "DNE" or status=="pending":
return True
return False
def parse_cve_page(db, html, cve):
soup = BeautifulSoup(html, "lxml")
packages = soup.find_all("div", class_="pkg")
print("Storing {}...".format(cve))
if packages:
for package in packages:
package_name = package.find("div", class_="value").a.text
for tr in package.find_all("tr"):
if len(tr.find_all("td")[1].text.split()) >= 1:
ubuntu = tr.find_all("td")[0].text.partition(":")[0]
status = tr.find_all("td")[1].text.split()[0]
priority = soup.find("div", class_="item").find_all("div")[1].text
if 'released' in tr.find_all("td")[1].text and len(tr.find_all("td")[1].text.split()) > 1:
version = tr.find_all("td")[1].text.split()[1].split("(")[1].split(")")[0]
store_cve(db, package_name, ubuntu, version, cve, status, priority)
elif verify_status(status):
store_cve(db, package_name, ubuntu, "", cve, status, priority)
else:
store_cve(db, "", "", "", cve, "", "")
else:
store_cve(db, "", "", "", cve, "", "")
def export_cves():
cves = set()
client = MongoClient()
db = client.cve_ubuntu
result = db.cve_ubuntu.find({}, {'cve': 1, '_id': 0})
for document in result:
cves.add(document['cve'])
return cves
def check_cve_ubuntu(db):
missing = set()
this_year = datetime.datetime.now().year
cves = export_cves()
for year in range(1999, this_year+1):
count = 0
print("Checking {}... ".format(year), end="")
r = requests.get("http://people.canonical.com/~ubuntu-security/cve/{}/".format(year))
try:
r.raise_for_status()
target = re.compile("CVE-\d+-\d+")
cves_regex = re.findall(target, r.text)
for cve in cves_regex:
if cve.lower() not in cves: # cve doesnt exist, we need to create it
if cve not in missing:
missing.add(cve)
count += 1
print("{}/{}".format(len(cves_regex) - count, len(cves_regex)))
except requests.exceptions.HTTPError as e:
print('Error.')
print("Total missing: {}".format(len(missing)))
return missing
def download_cves(db, missing):
for cve in missing:
year = cve.split("-")[1]
r = requests.get("http://people.canonical.com/~ubuntu-security/cve/{}/{}.html".format(year, cve))
parse_cve_page(db, r.text, cve)
##
def store_cve(db, package, os, version, cve, status, priority):
db.cve_ubuntu.insert_one(
{
"package": package.lower(),
"os": os.lower(),
"version": version.lower(),
"cve": cve.lower(),
"status": status.lower(),
"priority": priority.lower(),
})
##
def get_components():
components = set()
client = MongoClient()
db = client.components
result = db.components.find({}, {'package': 1, '_id': 0})
for r in result:
components.add(r['package'])
return components
def get_components_cves(components):
client = MongoClient()
db = client.cve_ubuntu
cves = set()
for component in components:
print("Checking CVEs for {}... ".format(component), end="")
result = db.cve_ubuntu.find({'package': component.lower()}, {'cve': 1, '_id': 0})
for r in result:
cves.add(r['cve'])
print("Done.")
return cves
def check_cve(cve):
client = MongoClient()
db = client.cve_ubuntu
return db.cve_ubuntu.find_one({'cve': cve})
def delete_cve(cve):
client = MongoClient()
db = client.cve_ubuntu
return db.cve_ubuntu.delete_many({'cve': cve})
def update_cves(db, cves):
for cve in cves:
cache = check_cve(cve)
if cache:
delete_cve(cve)
year = cve.split("-")[1]
r = requests.get("http://people.canonical.com/~ubuntu-security/cve/{}/{}.html".format(year, cve.upper()))
parse_cve_page(db, r.text, cve)
def main():
client = MongoClient()
db = client.cve_ubuntu
components = get_components()
cves = get_components_cves(components)
update_cves(db, cves)
if __name__ == "__main__":
main()