This repository has been archived by the owner on Jan 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
64 lines (53 loc) · 2.02 KB
/
database.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
import sqlite3
def write_db(cves: list):
"""Write the CVE data to the database."""
print("Updating CVE knowledge database...")
conn = sqlite3.connect('cve.sqlite')
cursor = conn.cursor()
cursor.execute("DROP TABLE IF EXISTS cve")
cursor.execute("DROP TABLE IF EXISTS cpe_match")
cursor.execute("CREATE TABLE cve (cve_id TEXT PRIMARY KEY)")
cursor.execute("""
CREATE TABLE cpe_match (
cve_id TEXT, vendor TEXT, product TEXT,
version TEXT, version_start TEXT, version_end TEXT,
FOREIGN KEY (cve_id) REFERENCES cve(cve_id)
)""")
prev_cve_id = None
for cve in cves:
# Sometimes the same CVE is listed twice in the feed
cve_id = cve['id']
if prev_cve_id == cve_id:
continue
prev_cve_id = cve_id
# Insert CVE main table
cursor.execute("INSERT INTO cve (cve_id) VALUES(?)", (cve_id,))
# Insert CPE match to instance table
for cpe_match in cve['cpe_match']:
cursor.execute("""
INSERT INTO cpe_match (
cve_id, vendor, product,
version, version_start, version_end
) VALUES (?, ?, ?, ?, ?, ?)
""", (
cve_id, cpe_match['vendor'], cpe_match['product'],
cpe_match.get('version'),
cpe_match.get('version_start'),
cpe_match.get('version_end')))
conn.commit()
conn.close()
def cleanup_db():
"""Remove duplicate CPE matches from the database."""
conn = sqlite3.connect('cve.sqlite')
cursor = conn.cursor()
cursor.execute("""
DELETE FROM cpe_match WHERE ROWID NOT IN (
SELECT MIN(ROWID) FROM cpe_match
GROUP BY vendor, product, version, version_start, version_end
)""")
cursor.execute("SELECT COUNT(*) FROM cpe_match")
count = cursor.fetchone()[0]
print(f"Amount of distinct CPE matches in the database: {count}.")
conn.commit()
cursor.close()
conn.close()