#!/usr/bin/env python3 """ Merge CVE data from NVD zip (original) and fkie-cad xz (GitHub release). Creates a unified dataset using the most recent 'lastModified' timestamp. """ import json import os import re from pathlib import Path def merge_cves(nvd_data, fkie_data): """ Merge CVE data from two sources. Returns merged vulnerabilities list. """ # Build dictionary for NVD vulnerabilities nvd_vulns = nvd_data.get('vulnerabilities', []) nvd_dict = {} for v in nvd_vulns: cve = v.get('cve', v) if isinstance(v, dict) else {} cve_id = cve.get('id') if cve_id: nvd_dict[cve_id] = v # Build dictionary for fkie vulnerabilities fkie_vulns = fkie_data.get('cve_items', []) if not fkie_vulns: fkie_vulns = fkie_data.get('vulnerabilities', []) fkie_dict = {} for v in fkie_vulns: cve = v.get('cve', v) if isinstance(v, dict) else {} cve_id = cve.get('id') if cve_id: fkie_dict[cve_id] = v # Merge merged_dict = {} all_ids = set(nvd_dict.keys()) | set(fkie_dict.keys()) nvd_only_count = 0 fkie_only_count = 0 updated_from_fkie = 0 updated_from_nvd = 0 identical = 0 for cve_id in all_ids: in_nvd = cve_id in nvd_dict in_fkie = cve_id in fkie_dict if in_nvd and not in_fkie: merged_dict[cve_id] = nvd_dict[cve_id] nvd_only_count += 1 elif in_fkie and not in_nvd: merged_dict[cve_id] = fkie_dict[cve_id] fkie_only_count += 1 else: # Present in both, compare lastModified n_cve = nvd_dict[cve_id].get('cve', nvd_dict[cve_id]) if isinstance(nvd_dict[cve_id], dict) else {} f_cve = fkie_dict[cve_id].get('cve', fkie_dict[cve_id]) if isinstance(fkie_dict[cve_id], dict) else {} n_last = n_cve.get('lastModified', '') f_last = f_cve.get('lastModified', '') if f_last > n_last: merged_dict[cve_id] = fkie_dict[cve_id] updated_from_fkie += 1 elif n_last > f_last: merged_dict[cve_id] = nvd_dict[cve_id] updated_from_nvd += 1 else: merged_dict[cve_id] = nvd_dict[cve_id] identical += 1 return { "merged_list": list(merged_dict.values()), "stats": { "total": len(merged_dict), "nvd_only": nvd_only_count, "fkie_only": fkie_only_count, "updated_from_fkie": updated_from_fkie, "updated_from_nvd": updated_from_nvd, "identical": identical } } def main(): base = Path(__file__).resolve().parent.parent / "data" / "CVE" nvd_dir = base / "json_from_zip" fkie_dir = base / "json_fkie_backup" out_dir = base / "json_merged" out_dir.mkdir(exist_ok=True, parents=True) pattern = re.compile(r'nvdcve-2\.0-(\d{4})\.json') # Get years from both nvd_years = {m.group(1) for f in nvd_dir.iterdir() if (m := pattern.match(f.name))} if nvd_dir.exists() else set() fkie_years = {m.group(1) for f in fkie_dir.iterdir() if (m := pattern.match(f.name))} if fkie_dir.exists() else set() all_years = sorted(nvd_years | fkie_years) print(f"Found years to process: {all_years}") print(f"Output directory: {out_dir}\n") total_stats = { "total": 0, "nvd_only": 0, "fkie_only": 0, "updated_from_fkie": 0, "updated_from_nvd": 0, "identical": 0 } for year in all_years: print(f"Processing year {year}...") nvd_file = nvd_dir / f"nvdcve-2.0-{year}.json" fkie_file = fkie_dir / f"nvdcve-2.0-{year}.json" nvd_data = {} if nvd_file.exists(): with open(nvd_file, 'r') as f: nvd_data = json.load(f) fkie_data = {} if fkie_file.exists(): with open(fkie_file, 'r') as f: fkie_data = json.load(f) # Merge logic result = merge_cves(nvd_data, fkie_data) stats = result["stats"] # Base JSON structure merged_json = {} if nvd_data: merged_json = nvd_data.copy() elif fkie_data: merged_json = fkie_data.copy() merged_json["vulnerabilities"] = result["merged_list"] merged_json["totalResults"] = len(result["merged_list"]) # Update format/timestamp if needed if fkie_data and nvd_data: t_nvd = nvd_data.get('timestamp', '') t_fkie = fkie_data.get('timestamp', '') merged_json['timestamp'] = max(t_nvd, t_fkie) # Write merged file out_file = out_dir / f"nvdcve-2.0-{year}.json" with open(out_file, 'w') as f: json.dump(merged_json, f, separators=(',', ':')) print(f" [DONE] Total: {stats['total']} " f"(NVD only: {stats['nvd_only']}, fkie only: {stats['fkie_only']}, " f"updated from fkie: {stats['updated_from_fkie']}, " f"updated from NVD: {stats['updated_from_nvd']}, " f"identical: {stats['identical']})") for k in total_stats: total_stats[k] += stats[k] print("\n" + "="*50) print("GLOBAL MERGE SUMMARY") print("="*50) print(f"Total CVEs processed: {total_stats['total']}") print(f"Total only in NVD: {total_stats['nvd_only']}") print(f"Total only in fkie: {total_stats['fkie_only']}") print(f"Shared - identical: {total_stats['identical']}") print(f"Shared - updated by fkie: {total_stats['updated_from_fkie']}") print(f"Shared - updated by NVD: {total_stats['updated_from_nvd']}") if __name__ == "__main__": main()