Created
March 21, 2024 02:38
-
-
Save masbicudo/85ac983cc313dbba1f39367e97c1b979 to your computer and use it in GitHub Desktop.
Search through all of NVD CVEs for CVEs mapped to multiple CWEs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import os | |
import time | |
def read_nvd(uri_params): | |
url = f'https://services.nvd.nist.gov/rest/json/cves/2.0?{uri_params}' | |
if os.path.isfile(f"nvd-cves.{uri_params}.json"): | |
with open(f"nvd-cves.{uri_params}.json", "r", encoding="utf-8") as fp: | |
data = json.load(fp) | |
else: | |
for x in range(10): # exponential backoff with 10 tries | |
ex = None | |
time.sleep(1) | |
resp = requests.get(url=url) | |
try: | |
data = resp.json() | |
break | |
except requests.JSONDecodeError as ex1: | |
ex = ex1 | |
time.sleep(2**x) | |
if ex is not None: | |
raise ex | |
with open(f"nvd-cves.{uri_params}.json", "w", encoding="utf-8") as fp: | |
json.dump(data, fp) | |
return data | |
def read_all_nvd(): | |
data = read_nvd("") | |
yield data | |
while data["resultsPerPage"] > 0: | |
data = read_nvd(f"startIndex={data['startIndex'] + data['resultsPerPage']}") | |
yield data | |
for data in read_all_nvd(): | |
for item in data["vulnerabilities"]: | |
if "weaknesses" in item["cve"]: | |
w_count = len(item["cve"]["weaknesses"]) | |
s_count = len({x["source"] for x in item["cve"]["weaknesses"]}) | |
if w_count > 1 and s_count < w_count: | |
print(f'{item["cve"]["id"]} (w_count({w_count}) > 1 and s_count({s_count}) < w_count)') | |
elif w_count > 1: | |
print(f'{item["cve"]["id"]} (w_count({w_count}) > 1)') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment