Skip to content

Commit 43e8982

Browse files
committed
wip: bit of code to import from nist
1 parent a18d5c9 commit 43e8982

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

importer.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python
2+
3+
import requests
4+
from typing import Dict, List, Optional
5+
from datetime import datetime
6+
7+
8+
def select_cpe_name(cve) -> Optional[str]:
9+
if "configurations" not in cve:
10+
return None
11+
12+
for configuration in cve["configurations"]:
13+
if "nodes" not in configuration:
14+
continue
15+
node = configuration["nodes"][0]
16+
if "cpeMatch" not in node:
17+
continue
18+
for cpe in node["cpeMatch"]:
19+
if not cpe["vulnerable"]:
20+
continue
21+
criteria = cpe["criteria"].split(":")
22+
pkgname = criteria[4]
23+
return pkgname
24+
return None
25+
26+
27+
def select_description(cve) -> str:
28+
for current_description in cve["descriptions"]:
29+
if current_description["lang"] == "en":
30+
description = current_description["value"].strip()
31+
return description
32+
return ""
33+
34+
def select_references(cve) -> List[str]:
35+
references = []
36+
for reference in cve["references"]:
37+
references.append(reference["url"])
38+
return references
39+
40+
41+
if __name__ == "__main__":
42+
url: str = "https://services.nvd.nist.gov/rest/json/cves/2.0/"
43+
start_index: int = 241712
44+
45+
params: Dict[str, str] = {}
46+
47+
while True:
48+
params["startIndex"] = start_index
49+
# params["cveId"] = "CVE-2022-3256"
50+
51+
print(f"query {start_index}")
52+
resp = requests.get(url, params=params)
53+
resp.raise_for_status()
54+
55+
result = resp.json()
56+
57+
results_per_page = result["resultsPerPage"]
58+
start_index = result["startIndex"]
59+
total_results = result["totalResults"]
60+
61+
for vulnerability in result["vulnerabilities"]:
62+
if "cve" not in vulnerability:
63+
continue
64+
65+
cve = vulnerability["cve"]
66+
id = cve["id"]
67+
68+
published = datetime.strptime(cve["published"], "%Y-%m-%dT%H:%M:%S.%f")
69+
last_modified = datetime.strptime(cve["lastModified"], "%Y-%m-%dT%H:%M:%S.%f")
70+
71+
description = select_description(cve)
72+
pkgname = select_cpe_name(cve)
73+
references = select_references(cve)
74+
75+
print(id, description, pkgname)
76+
77+
if start_index + results_per_page >= total_results:
78+
break
79+
start_index += results_per_page

0 commit comments

Comments
 (0)