-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathget-nvd-json.py
executable file
·83 lines (64 loc) · 2.32 KB
/
get-nvd-json.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
#!/usr/bin/env python
import requests
from requests.adapters import HTTPAdapter
from urllib3.util import Retry
import os
import sys
import json
from tqdm import tqdm
def main():
# Define the retry strategy
retry_strategy = Retry(
total=6, # Maximum number of retries
backoff_factor=4,
status_forcelist=[429, 500, 502, 503, 504], # HTTP status codes to retry on
)
# Create an HTTP adapter with the retry strategy and mount it to session
adapter = HTTPAdapter(max_retries=retry_strategy)
# Create a new session object
session = requests.Session()
session.mount('http://', adapter)
session.mount('https://', adapter)
#nvd_url = "https://services.nvd.nist.gov/rest/json/cves/1.0"
nvd_url = "https://services.nvd.nist.gov/rest/json/cves/2.0"
payload = {
"startIndex": 0,
"resultsPerPage": 2000
}
headers = {}
if "NVD_API_KEY" in os.environ:
headers['apiKey'] = os.environ['NVD_API_KEY']
with open("nvd-out.json", "w") as fh:
# Get page 1
req = session.get(nvd_url, params=payload, headers=headers)
if req.status_code != 200:
print(req)
sys.exit(1)
response = req.json()
results = response["vulnerabilities"]
for i in results:
fh.write(json.dumps(i["cve"]))
fh.write("\n")
total = response["totalResults"]
payload["startIndex"] = payload["startIndex"] + response["resultsPerPage"]
progress_bar = tqdm(total=total)
progress_bar.update(response["resultsPerPage"])
#Loop over pages
while response["resultsPerPage"] > 0:
req = session.get(nvd_url, params=payload, headers=headers)
if req.status_code != 200:
print(req)
sys.exit(1)
response = req.json()
payload["startIndex"] = payload["startIndex"] + response["resultsPerPage"]
results = response["vulnerabilities"]
for i in results:
fh.write(json.dumps(i["cve"]))
fh.write("\n")
progress_bar.update(response["resultsPerPage"])
progress_bar.close()
# json_out = json.dumps(results, indent=2)
# with open("nvd-out.json", "w") as fh:
# fh.write(json_out)
if __name__ == "__main__":
main()