Skip to content

Instantly share code, notes, and snippets.

@jgamblin
Last active November 2, 2021 09:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jgamblin/dfafbb94323fa1be39bc5552ecc36d04 to your computer and use it in GitHub Desktop.
Save jgamblin/dfafbb94323fa1be39bc5552ecc36d04 to your computer and use it in GitHub Desktop.
NVD CVEs to Indivual JSON files
import requests
import json
import gzip
import os
import codecs
import time
from datetime import timedelta
start = time.time()
count = 0
baseURL = "https://nvd.nist.gov/feeds/json/cve/1.0/"
year = 2002
while year <= 2019:
yearstr = str(year)
os.mkdir(yearstr)
os.chdir(yearstr)
feed = ('nvdcve-1.0-'+yearstr+'.json.gz')
url = baseURL + feed
r = requests.get(url)
open(feed, 'wb').write(r.content)
with gzip.open(feed, 'rt', encoding="utf8") as fd:
data = json.load(fd)
for item in data['CVE_Items']:
if 'cve' in item:
if 'CVE_data_meta' in item['cve']:
if 'ID' in item['cve']['CVE_data_meta']:
count = count + 1
filename = (item['cve']['CVE_data_meta']['ID'] + '.json')
try:
with open(filename, 'wb') as f:
json.dump(item, codecs.getwriter('utf-8')(f),
ensure_ascii=False)
f.close()
except TypeError:
continue
os.chdir('..')
year = year + 1
elapsed = (time.time() - start)
print("CVEs Turned Into JSON Files:")
print(count)
print("Done In:")
print(str(timedelta(seconds=elapsed)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment