Created
April 8, 2024 14:01
-
-
Save d0now/28cc44df0ff6a98061d9139b9c9f2e80 to your computer and use it in GitHub Desktop.
Get all CVEs from CPE
This file contains 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 pathlib | |
def query(cpe: str, results_per_page=20, startIndex=0): | |
return requests.get( | |
"https://services.nvd.nist.gov/rest/json/cves/2.0", | |
params={ | |
'cpeName': cpe, | |
'resultsPerPage': results_per_page, | |
'startIndex': startIndex, | |
'isVulnerable': "", | |
} | |
) | |
def query_repeat(cpe: str, results_per_page=20, startIndex=0): | |
while True: | |
try: | |
response = query(cpe, results_per_page=results_per_page, startIndex=startIndex) | |
except requests.exceptions.ConnectTimeout: | |
print("timeout: retrying...") | |
continue | |
if response.status_code != 200: | |
print("not ok: retrying...") | |
continue | |
return response | |
def get_all_cves_from_cpe(cpe: str, results_per_page=20): | |
print(f"Initial fetch...") | |
response = query_repeat(cpe, results_per_page=results_per_page) | |
for i in range(0, response.json()['totalResults'], results_per_page): | |
print(f"Fetching {i}...") | |
response = query_repeat(cpe, results_per_page=results_per_page, startIndex=i) | |
yield response.json()['vulnerabilities'] | |
def main(args): | |
cves = [] | |
for now in get_all_cves_from_cpe(args.cpe, args.results_per_page): | |
cves.extend(now) | |
with open(args.out, 'w') as f: | |
json.dump(cves, f, indent=2) | |
if __name__ == "__main__": | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("cpe") | |
parser.add_argument("out", type=pathlib.Path) | |
parser.add_argument("--results-per-page", type=int, default=100) | |
args = parser.parse_args() | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment