| |
| """ |
| Download all available CVE data from NVD |
| """ |
|
|
| import os |
| import requests |
| import zipfile |
| from pathlib import Path |
| from tqdm import tqdm |
| import time |
|
|
| def download_file_with_retry(url, target_file, filename, max_retries=5): |
| """Helper function to download files with retry logic for unstable connections.""" |
| for attempt in range(1, max_retries + 1): |
| try: |
| if attempt > 1: |
| print(f"[INFO] Retrying download for {filename} (Attempt {attempt}/{max_retries})...") |
| else: |
| print(f"[INFO] Downloading {filename}...") |
| |
| headers = { |
| 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' |
| } |
| response = requests.get(url, stream=True, headers=headers, timeout=60) |
| response.raise_for_status() |
| |
| total_size = int(response.headers.get('content-length', 0)) |
| |
| with open(target_file, 'wb') as f: |
| with tqdm(total=total_size, unit='B', unit_scale=True, desc=filename) as pbar: |
| for chunk in response.iter_content(chunk_size=8192): |
| if chunk: |
| f.write(chunk) |
| pbar.update(len(chunk)) |
| |
| print(f"[INFO] Successfully downloaded: {target_file}") |
| return True |
| |
| except BaseException as e: |
| if isinstance(e, KeyboardInterrupt): |
| print(f"\n[INFO] Download interrupted by user. Cleaning up {filename}...") |
| else: |
| print(f"[ERROR] Failed to download {filename} on attempt {attempt}: {e}") |
| |
| |
| if target_file.exists(): |
| try: |
| target_file.unlink() |
| except Exception as cleanup_error: |
| print(f"[ERROR] Could not remove partial file {target_file}: {cleanup_error}") |
| |
| if isinstance(e, KeyboardInterrupt): |
| raise |
| |
| if attempt < max_retries: |
| time.sleep(2 * attempt) |
| else: |
| print(f"[ERROR] Reached maximum retries for {filename}. Skipping.") |
| return False |
|
|
| def download_cve_data(): |
| """Download all available CVE data from NVD""" |
| |
| |
| script_dir = Path(__file__).resolve().parent |
| target_dir = script_dir.parent / "data" / "CVE" / "zip" |
| target_dir.mkdir(parents=True, exist_ok=True) |
| |
| |
| base_url = "https://nvd.nist.gov/feeds/json/cve/2.0" |
| |
| |
| import datetime |
| current_year = datetime.datetime.now().year |
| |
| print(f"[INFO] Downloading CVE data from 2002 to {current_year}...") |
| |
| |
| for year in range(2002, current_year + 1): |
| filename = f"nvdcve-2.0-{year}.json.zip" |
| url = f"{base_url}/{filename}" |
| target_file = target_dir / filename |
| |
| if target_file.exists(): |
| |
| if target_file.stat().st_size > 1000: |
| print(f"[INFO] {filename} already exists, skipping...") |
| continue |
| else: |
| print(f"[INFO] {filename} exists but seems too small/corrupted, redownloading...") |
| |
| download_file_with_retry(url, target_file, filename) |
| |
| |
| modified_filename = "nvdcve-2.0-modified.json.zip" |
| modified_url = f"{base_url}/{modified_filename}" |
| modified_target_file = target_dir / modified_filename |
| |
| if modified_target_file.exists() and modified_target_file.stat().st_size > 1000: |
| print(f"[INFO] {modified_filename} already exists, skipping...") |
| else: |
| download_file_with_retry(modified_url, modified_target_file, modified_filename) |
| |
| print(f"[INFO] Download complete! Files saved to: {target_dir}") |
|
|
| if __name__ == "__main__": |
| download_cve_data() |
|
|