File size: 43,347 Bytes
27f6252 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 | #!/usr/bin/env python3
"""
Processes CVE dataset and creates a comprehensive knowledge base
"""
import json
import logging
import time
import zipfile
from pathlib import Path
from typing import List, Dict, Optional
from datetime import datetime
import sys
# Import the centralized config
sys.path.append(str(Path(__file__).parent.parent.parent))
from config import Config
# Import the new extractors and enrichers
from src.processors.extractors import (
extract_zip_files,
load_cwe_capec_mitre_mapping,
load_csaf_data,
load_exploitdb_data,
load_kev_data
)
from src.processors.enrichers import enrich_cve_with_cti, enrich_all_cves
from src.processors.filters import filter_rejected_cves
from src.processors.savers import save_processed_data
# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
class CVEProcessor:
"""Processes all CVE data files and creates a comprehensive knowledge base"""
def __init__(self):
# Initialize config
self.config = Config()
# CVE data paths
self.data_dir = self.config.base_data_dir / "CVE"
self.zip_dir = self.data_dir / "zip"
self.json_dir = self.data_dir / "json"
self.processed_dir = self.data_dir / "processed"
self.correlations_dir = self.data_dir / "correlations"
# Create directories if they don't exist
self.json_dir.mkdir(exist_ok=True)
self.processed_dir.mkdir(exist_ok=True)
self.correlations_dir.mkdir(exist_ok=True)
# CTI data paths
self.cti_raw_dir = self.config.cti_data_dir
self.cti_docs_dir = self.config.cti_docs_dir
# Data storage
self.all_cves = []
self.kev_data = []
self.csaf_data = []
self.exploitdb_data = []
self.cwe_capec_mitre_mapping = {}
self.csaf_by_cve = {}
self.exploitdb_by_cve = {}
self.kev_by_cve = {}
def extract_zip_files(self):
extract_zip_files(self.zip_dir, self.json_dir, logger)
def load_cwe_capec_mitre_mapping(self):
# Try multiple possible locations for the mapping file
mapping_locations = [
self.cti_raw_dir / "cwe_capec_mitre_mapping.json",
self.cti_docs_dir / "cwe_capec_mitre_mapping.json",
self.cti_raw_dir / "mappings" / "cwe_capec_mitre_mapping.json"
]
for location in mapping_locations:
if location.exists():
self.cwe_capec_mitre_mapping = load_cwe_capec_mitre_mapping(location, logger)
logger.info(f"Loaded CWE-CAPEC-MITRE mapping from: {location}")
return
logger.warning("CWE-CAPEC-MITRE mapping file not found in any location")
self.cwe_capec_mitre_mapping = {}
def load_csaf_data(self):
# Try multiple possible locations for CSAF data
csaf_locations = [
self.cti_docs_dir / "csaf",
self.cti_raw_dir / "csaf",
self.cti_raw_dir / "CSAF"
]
for location in csaf_locations:
if location.exists():
self.csaf_data, self.csaf_by_cve = load_csaf_data(location, logger)
logger.info(f"Loaded CSAF data from: {location}")
return
logger.warning("CSAF data not found in any location")
self.csaf_data, self.csaf_by_cve = [], {}
def load_exploitdb_data(self):
# Try multiple possible locations for ExploitDB data
exploitdb_locations = [
self.cti_docs_dir / "exploitdb",
self.cti_raw_dir / "exploitdb",
self.cti_raw_dir / "ExploitDB"
]
for location in exploitdb_locations:
if location.exists():
self.exploitdb_data, self.exploitdb_by_cve = load_exploitdb_data(location, logger)
logger.info(f"Loaded ExploitDB data from: {location}")
return
logger.warning("ExploitDB data not found in any location")
self.exploitdb_data, self.exploitdb_by_cve = [], {}
def load_kev_data(self):
# Try multiple possible locations for KEV data
kev_locations = [
self.cti_docs_dir / "kev",
self.cti_raw_dir / "kev",
self.cti_raw_dir / "KEV",
self.cti_raw_dir / "known_exploited_vulnerabilities.csv"
]
for location in kev_locations:
if location.exists():
if location.suffix == '.csv':
# Direct CSV file
self.kev_data, self.kev_by_cve = load_kev_data(location.parent, logger)
else:
# Directory with JSON files
self.kev_data, self.kev_by_cve = load_kev_data(location, logger)
logger.info(f"Loaded KEV data from: {location}")
return
logger.warning("KEV data not found in any location")
self.kev_data, self.kev_by_cve = [], {}
def _clean_cvss_data(self, cvss_data: Dict) -> Dict:
"""Remove null values from CVSS data"""
if not cvss_data:
return {}
cleaned = {}
for key, value in cvss_data.items():
if value is not None:
cleaned[key] = value
return cleaned
def _fix_product_name(self, product: str) -> str:
"""Fix escaped slashes in product names"""
if not product:
return product
# Fix escaped slashes: \\/ -> //
fixed = product.replace('\\/', '//')
return fixed
def _extract_and_fix_product_from_cpe(self, cpe: str) -> str:
"""Extract product name from CPE and fix escaped slashes"""
if not cpe:
return ""
# Parse CPE to extract product information
parts = cpe.split(':')
if len(parts) >= 5:
product = parts[4]
return self._fix_product_name(product)
return ""
def _clean_cpe_match(self, cpe_match: Dict) -> Dict:
"""Clean CPE match data by removing null values"""
if not cpe_match:
return {}
cleaned = {}
for key, value in cpe_match.items():
if value is not None:
cleaned[key] = value
return cleaned
def process_all_cve_files(self, max_cves: Optional[int] = None) -> List[Dict]:
"""Process all CVE files and extract structured data"""
logger.info("Processing all CVE files...")
# Get all JSON files
v1_files = list(self.json_dir.glob("nvdcve-1.1-*.json"))
v2_files = list(self.json_dir.glob("nvdcve-2.0-*.json"))
logger.info(f"Found {len(v1_files)} v1.1 files and {len(v2_files)} v2.0 files")
# Process v1.1 files first (they're smaller and easier to process)
logger.info("Processing v1.1 files...")
for i, json_file in enumerate(sorted(v1_files)):
logger.info(f"Processing v1.1 file {i+1}/{len(v1_files)}: {json_file.name}")
cves = self._process_v1_file(json_file)
self.all_cves.extend(cves)
if max_cves and len(self.all_cves) >= max_cves:
logger.info(f"Reached max CVEs limit: {max_cves}")
break
# Process v2.0 files (only if we haven't reached the limit)
if not max_cves or len(self.all_cves) < max_cves:
logger.info("Processing v2.0 files...")
for i, json_file in enumerate(sorted(v2_files)):
logger.info(f"Processing v2.0 file {i+1}/{len(v2_files)}: {json_file.name}")
cves = self._process_v2_file(json_file)
self.all_cves.extend(cves)
if max_cves and len(self.all_cves) >= max_cves:
logger.info(f"Reached max CVEs limit: {max_cves}")
break
# Filter out rejected CVEs
self.all_cves = filter_rejected_cves(self.all_cves, logger)
logger.info(f"Processed {len(self.all_cves)} total CVEs (after filtering)")
return self.all_cves
def _process_v1_file(self, json_file: Path) -> List[Dict]:
"""Process a v1.1 format CVE file"""
try:
with open(json_file, 'r', encoding='utf-8') as f:
data = json.load(f)
cve_items = data.get('CVE_Items', [])
processed_cves = []
for cve_item in cve_items:
cve_data = cve_item.get('cve', {})
configurations = cve_item.get('configurations', {})
cve_id = cve_data.get('CVE_data_meta', {}).get('ID', '')
if cve_id:
processed_cve = self._process_v1_cve(cve_item, configurations)
if processed_cve:
processed_cves.append(processed_cve)
return processed_cves
except Exception as e:
logger.error(f"Error processing {json_file.name}: {e}")
return []
def _process_v2_file(self, json_file: Path) -> List[Dict]:
"""Process a v2.0 format CVE file"""
try:
with open(json_file, 'r', encoding='utf-8') as f:
data = json.load(f)
vulnerabilities = data.get('vulnerabilities', [])
processed_cves = []
for vuln in vulnerabilities:
cve_data = vuln.get('cve', {})
configurations = cve_data.get('configurations', [])
cve_id = cve_data.get('id', '')
if cve_id:
processed_cve = self._process_v2_cve(cve_data, configurations)
if processed_cve:
processed_cves.append(processed_cve)
return processed_cves
except Exception as e:
logger.error(f"Error processing {json_file.name}: {e}")
return []
def _process_v1_cve(self, cve_item: Dict, configurations: Dict) -> Optional[Dict]:
"""Process a single v1.1 CVE"""
try:
cve_data = cve_item.get('cve', {})
cve_id = cve_data.get('CVE_data_meta', {}).get('ID', '')
# Get description
description_data = cve_data.get('description', {}).get('description_data', [])
description = ""
for desc in description_data:
if desc.get('lang') == 'en':
description = desc.get('value', '')
break
# Get CVSS scores and vectors with version information
impact = cve_item.get('impact', {})
base_metric_v3 = impact.get('baseMetricV3', {})
cvss_v3 = base_metric_v3.get('cvssV3', {})
base_metric_v2 = impact.get('baseMetricV2', {})
cvss_v2 = base_metric_v2.get('cvssV2', {})
# Get CWE references
problemtype_data = cve_data.get('problemtype', {}).get('problemtype_data', [])
cwe_ids = []
for problem in problemtype_data:
for desc in problem.get('description', []):
cwe_id = desc.get('value', '')
if cwe_id.startswith('CWE-'):
cwe_ids.append(cwe_id)
# Get references
references_data = cve_data.get('references', {}).get('reference_data', [])
ref_urls = [ref.get('url', '') for ref in references_data if ref.get('url')]
# Get CPE configurations (detailed) - now using the passed configurations parameter
cpe_configurations = []
affected_products = []
# Handle the nested structure properly
nodes = configurations.get('nodes', [])
for node in nodes:
config_entry = {
'operator': node.get('operator', ''),
'cpe_match': []
}
# Process cpe_match entries in this node
for cpe_match in node.get('cpe_match', []):
cpe = cpe_match.get('cpe23Uri', '')
if cpe:
# Parse CPE to extract product information and fix escaped slashes
product = self._extract_and_fix_product_from_cpe(cpe)
if product:
affected_products.append(product)
# Fix the CPE URI itself
fixed_cpe = self._fix_product_name(cpe)
# Clean CPE match data
cleaned_cpe_match = self._clean_cpe_match({
'cpe23Uri': fixed_cpe,
'versionStartIncluding': cpe_match.get('versionStartIncluding'),
'versionStartExcluding': cpe_match.get('versionStartExcluding'),
'versionEndIncluding': cpe_match.get('versionEndIncluding'),
'versionEndExcluding': cpe_match.get('versionEndExcluding'),
'vulnerable': cpe_match.get('vulnerable', True)
})
config_entry['cpe_match'].append(cleaned_cpe_match)
# Also check children nodes recursively
children = node.get('children', [])
for child in children:
for cpe_match in child.get('cpe_match', []):
cpe = cpe_match.get('cpe23Uri', '')
if cpe:
# Parse CPE to extract product information and fix escaped slashes
product = self._extract_and_fix_product_from_cpe(cpe)
if product:
affected_products.append(product)
# Fix the CPE URI itself
fixed_cpe = self._fix_product_name(cpe)
# Clean CPE match data
cleaned_cpe_match = self._clean_cpe_match({
'cpe23Uri': fixed_cpe,
'versionStartIncluding': cpe_match.get('versionStartIncluding'),
'versionStartExcluding': cpe_match.get('versionStartExcluding'),
'versionEndIncluding': cpe_match.get('versionEndIncluding'),
'versionEndExcluding': cpe_match.get('versionEndExcluding'),
'vulnerable': cpe_match.get('vulnerable', True)
})
config_entry['cpe_match'].append(cleaned_cpe_match)
if config_entry['cpe_match']:
cpe_configurations.append(config_entry)
# Clean CVSS data - only include non-null values
cleaned_cvss_v3 = self._clean_cvss_data({
'version': '3.0',
'base_score': cvss_v3.get('baseScore'),
'vector_string': cvss_v3.get('vectorString'),
'base_severity': cvss_v3.get('baseSeverity'),
'attack_vector': cvss_v3.get('attackVector'),
'attack_complexity': cvss_v3.get('attackComplexity'),
'privileges_required': cvss_v3.get('privilegesRequired'),
'user_interaction': cvss_v3.get('userInteraction'),
'scope': cvss_v3.get('scope'),
'confidentiality_impact': cvss_v3.get('confidentialityImpact'),
'integrity_impact': cvss_v3.get('integrityImpact'),
'availability_impact': cvss_v3.get('availabilityImpact')
})
cleaned_cvss_v2 = self._clean_cvss_data({
'version': '2.0',
'base_score': cvss_v2.get('baseScore'),
'vector_string': cvss_v2.get('vectorString'),
'severity': base_metric_v2.get('severity'),
'access_vector': cvss_v2.get('accessVector'),
'access_complexity': cvss_v2.get('accessComplexity'),
'authentication': cvss_v2.get('authentication'),
'confidentiality_impact': cvss_v2.get('confidentialityImpact'),
'integrity_impact': cvss_v2.get('integrityImpact'),
'availability_impact': cvss_v2.get('availabilityImpact')
})
result = {
'cve_id': cve_id,
'description': description,
'cwe_ids': list(set(cwe_ids)),
'affected_products': list(set(affected_products)),
'cpe_configurations': cpe_configurations,
'references': ref_urls,
'published_date': cve_item.get('publishedDate', ''),
'last_modified_date': cve_item.get('lastModifiedDate', ''),
'source': 'NVD v1.1'
}
# Only add CVSS data if it has actual values
if cleaned_cvss_v3:
result['cvss_v3'] = cleaned_cvss_v3
if cleaned_cvss_v2:
result['cvss_v2'] = cleaned_cvss_v2
return result
except Exception as e:
logger.error(f"Error processing v1 CVE: {e}")
return None
def _process_v2_cve(self, cve_data: Dict, configurations: Dict) -> Optional[Dict]:
"""Process a single v2.0 CVE"""
try:
cve_id = cve_data.get('id', '')
# Get description
descriptions = cve_data.get('descriptions', [])
description = ""
for desc in descriptions:
if desc.get('lang') == 'en':
description = desc.get('value', '')
break
# Get CVSS scores and vectors with version information
metrics = cve_data.get('metrics', {})
# CVSS v3.1 (preferred) or v3.0
cvss_v3_data = None
cvss_v3_version = None
if metrics.get('cvssMetricV31'):
cvss_v3_data = metrics.get('cvssMetricV31', [{}])[0]
cvss_v3_version = '3.1'
elif metrics.get('cvssMetricV30'):
cvss_v3_data = metrics.get('cvssMetricV30', [{}])[0]
cvss_v3_version = '3.0'
cvss_v3 = {}
if cvss_v3_data:
cvss_data = cvss_v3_data.get('cvssData', {})
cvss_v3 = {
'version': cvss_v3_version,
'base_score': cvss_data.get('baseScore'),
'vector_string': cvss_data.get('vectorString'),
'base_severity': cvss_data.get('baseSeverity'),
'attack_vector': cvss_data.get('attackVector'),
'attack_complexity': cvss_data.get('attackComplexity'),
'privileges_required': cvss_data.get('privilegesRequired'),
'user_interaction': cvss_data.get('userInteraction'),
'scope': cvss_data.get('scope'),
'confidentiality_impact': cvss_data.get('confidentialityImpact'),
'integrity_impact': cvss_data.get('integrityImpact'),
'availability_impact': cvss_data.get('availabilityImpact')
}
# CVSS v2
cvss_v2 = {}
if metrics.get('cvssMetricV2'):
cvss_v2_data = metrics.get('cvssMetricV2', [{}])[0]
cvss_v2_info = cvss_v2_data.get('cvssData', {})
cvss_v2 = {
'version': '2.0',
'base_score': cvss_v2_info.get('baseScore'),
'vector_string': cvss_v2_info.get('vectorString'),
'severity': cvss_v2_data.get('baseSeverity'),
'access_vector': cvss_v2_info.get('accessVector'),
'access_complexity': cvss_v2_info.get('accessComplexity'),
'authentication': cvss_v2_info.get('authentication'),
'confidentiality_impact': cvss_v2_info.get('confidentialityImpact'),
'integrity_impact': cvss_v2_info.get('integrityImpact'),
'availability_impact': cvss_v2_info.get('availabilityImpact')
}
# Get CWE references
weaknesses = cve_data.get('weaknesses', [])
cwe_ids = []
for weakness in weaknesses:
for desc in weakness.get('description', []):
if desc.get('lang') == 'en':
cwe_id = desc.get('value', '')
if cwe_id.startswith('CWE-'):
cwe_ids.append(cwe_id)
# Get references
references = cve_data.get('references', [])
ref_urls = [ref.get('url', '') for ref in references if ref.get('url')]
# Get CPE configurations (detailed) - now using the passed configurations parameter
cpe_configurations = []
affected_products = []
# Handle v2.0 configurations structure (configurations is a list)
if isinstance(configurations, list):
for config in configurations:
for node in config.get('nodes', []):
config_entry = {
'operator': node.get('operator', ''),
'negate': node.get('negate', False),
'cpe_match': []
}
# Process cpeMatch entries in this node
for cpe_match in node.get('cpeMatch', []):
cpe = cpe_match.get('criteria', '') # v2.0 uses 'criteria' instead of 'cpe23Uri'
if cpe:
# Parse CPE to extract product information and fix escaped slashes
product = self._extract_and_fix_product_from_cpe(cpe)
if product:
affected_products.append(product)
# Fix the CPE URI itself
fixed_cpe = self._fix_product_name(cpe)
# Clean CPE match data
cleaned_cpe_match = self._clean_cpe_match({
'cpe23Uri': fixed_cpe, # Keep consistent field name
'versionStartIncluding': cpe_match.get('versionStartIncluding'),
'versionStartExcluding': cpe_match.get('versionStartExcluding'),
'versionEndIncluding': cpe_match.get('versionEndIncluding'),
'versionEndExcluding': cpe_match.get('versionEndExcluding'),
'vulnerable': cpe_match.get('vulnerable', True),
'matchCriteriaId': cpe_match.get('matchCriteriaId') # v2.0 specific
})
config_entry['cpe_match'].append(cleaned_cpe_match)
# Also check children nodes recursively
children = node.get('children', [])
for child in children:
for cpe_match in child.get('cpeMatch', []):
cpe = cpe_match.get('criteria', '')
if cpe:
# Parse CPE to extract product information and fix escaped slashes
product = self._extract_and_fix_product_from_cpe(cpe)
if product:
affected_products.append(product)
# Fix the CPE URI itself
fixed_cpe = self._fix_product_name(cpe)
# Clean CPE match data
cleaned_cpe_match = self._clean_cpe_match({
'cpe23Uri': fixed_cpe, # Keep consistent field name
'versionStartIncluding': cpe_match.get('versionStartIncluding'),
'versionStartExcluding': cpe_match.get('versionStartExcluding'),
'versionEndIncluding': cpe_match.get('versionEndIncluding'),
'versionEndExcluding': cpe_match.get('versionEndExcluding'),
'vulnerable': cpe_match.get('vulnerable', True),
'matchCriteriaId': cpe_match.get('matchCriteriaId') # v2.0 specific
})
config_entry['cpe_match'].append(cleaned_cpe_match)
if config_entry['cpe_match']:
cpe_configurations.append(config_entry)
# Clean CVSS data - only include non-null values
cleaned_cvss_v3 = self._clean_cvss_data(cvss_v3)
cleaned_cvss_v2 = self._clean_cvss_data(cvss_v2)
result = {
'cve_id': cve_id,
'description': description,
'cwe_ids': list(set(cwe_ids)),
'affected_products': list(set(affected_products)),
'cpe_configurations': cpe_configurations,
'references': ref_urls,
'published_date': cve_data.get('published', ''),
'last_modified_date': cve_data.get('lastModified', ''),
'source': 'NVD v2.0'
}
# Only add CVSS data if it has actual values
if cleaned_cvss_v3:
result['cvss_v3'] = cleaned_cvss_v3
if cleaned_cvss_v2:
result['cvss_v2'] = cleaned_cvss_v2
return result
except Exception as e:
logger.error(f"Error processing v2 CVE: {e}")
return None
def create_enhanced_documents(self) -> List[Dict]:
"""Create enhanced documents for the RAG system"""
logger.info("Creating enhanced documents for RAG system...")
enhanced_docs = []
# Create CVE documents
for cve in self.all_cves:
cve_id = cve.get('cve_id', '')
description = cve.get('description', '')
if not description:
continue
# Build comprehensive content
content_parts = [description]
# Add CVSS information with version details
cvss_v3 = cve.get('cvss_v3', {})
cvss_v2 = cve.get('cvss_v2', {})
if cvss_v3.get('base_score'):
content_parts.append(f"CVSS v{cvss_v3.get('version', '3.x')} Score: {cvss_v3['base_score']} ({cvss_v3.get('base_severity', 'Unknown')})")
if cvss_v3.get('vector_string'):
content_parts.append(f"CVSS v{cvss_v3.get('version', '3.x')} Vector: {cvss_v3['vector_string']}")
if cvss_v2.get('base_score'):
content_parts.append(f"CVSS v2.0 Score: {cvss_v2['base_score']} ({cvss_v2.get('severity', 'Unknown')})")
if cvss_v2.get('vector_string'):
content_parts.append(f"CVSS v2.0 Vector: {cvss_v2['vector_string']}")
# Add affected products
affected_products = cve.get('affected_products', [])
if affected_products:
content_parts.append(f"Affected Products: {', '.join(affected_products[:5])}") # Limit to 5
# Add CWE information
cwe_ids = cve.get('cwe_ids', [])
if cwe_ids:
content_parts.append(f"Related CWEs: {', '.join(cwe_ids)}")
# Add CAPEC information
capec_entries = cve.get('capec_entries', [])
if capec_entries:
content_parts.append(f"Related CAPECs: {', '.join(capec_entries[:3])}") # Limit to 3
# Add MITRE ATT&CK techniques and tactics
mitre_techniques = cve.get('mitre_techniques', [])
mitre_tactics = cve.get('mitre_tactics', [])
if mitre_techniques:
content_parts.append(f"MITRE ATT&CK Techniques: {', '.join(mitre_techniques[:3])}") # Limit to 3
if mitre_tactics:
content_parts.append(f"MITRE ATT&CK Tactics: {', '.join(mitre_tactics[:3])}") # Limit to 3
# Add correlation information
if cve.get('is_in_kev'):
content_parts.append("WARNING: This vulnerability is in CISA's Known Exploited Vulnerabilities (KEV) catalog")
csaf_correlations = cve.get('csaf_correlations', [])
if csaf_correlations:
csaf_ids = [csaf.get('id', '') for csaf in csaf_correlations if csaf.get('id')]
if csaf_ids:
content_parts.append(f"CSAF Advisories: {', '.join(csaf_ids[:3])}") # Limit to 3
exploitdb_correlations = cve.get('exploitdb_correlations', [])
if exploitdb_correlations:
exploitdb_ids = [exp.get('id', '') for exp in exploitdb_correlations if exp.get('id')]
if exploitdb_ids:
content_parts.append(f"ExploitDB IDs: {', '.join(exploitdb_ids[:3])}") # Limit to 3
# Add references
references = cve.get('references', [])
if references:
content_parts.append(f"References: {', '.join(references[:3])}") # Limit to 3
# Create document
doc = {
'id': cve_id,
'title': f"CVE {cve_id}",
'content': "\n".join(content_parts),
'source': cve.get('source', 'NVD'),
'document_type': 'CVE',
'cve_refs': [cve_id],
'cwe_refs': cwe_ids,
'capec_entries': capec_entries,
'mitre_techniques': mitre_techniques,
'mitre_tactics': mitre_tactics,
'cvss_v3': cvss_v3,
'cvss_v2': cvss_v2,
'affected_products': affected_products,
'cpe_configurations': cve.get('cpe_configurations', []),
'published_date': cve.get('published_date', ''),
'last_modified_date': cve.get('last_modified_date', ''),
'is_in_kev': cve.get('is_in_kev', False),
'csaf_correlations_count': len(cve.get('csaf_correlations', [])),
'exploitdb_correlations_count': len(cve.get('exploitdb_correlations', [])),
'tags': self._extract_tags_from_cve(cve)
}
enhanced_docs.append(doc)
# Create KEV documents
for kev in self.kev_data:
cve_id = kev.get('id', '') # KEV files use 'id' field
if cve_id:
content_parts = [
f"Known Exploited Vulnerability: {cve_id}",
f"Vendor/Project: {kev.get('vendor_project', 'Unknown')}",
f"Product: {kev.get('product', 'Unknown')}",
f"Vulnerability Name: {kev.get('vulnerability_name', 'Unknown')}",
f"Date Added to KEV: {kev.get('date_added', 'Unknown')}",
f"Required Action: {kev.get('required_action', 'None specified')}",
f"Due Date: {kev.get('due_date', 'None specified')}",
]
if kev.get('known_ransomware'):
content_parts.append(f"Known Ransomware Campaign Use: {kev.get('known_ransomware')}")
if kev.get('notes'):
content_parts.append(f"Notes: {kev.get('notes')}")
doc = {
'id': f"KEV-{cve_id}",
'title': f"Known Exploited Vulnerability - {cve_id}",
'content': "\n".join(content_parts),
'source': 'CISA KEV',
'document_type': 'KEV',
'cve_refs': [cve_id],
'severity': 'Critical',
'tags': ['exploited', 'critical', 'kev', 'cisa']
}
enhanced_docs.append(doc)
logger.info(f"Created {len(enhanced_docs)} enhanced documents")
return enhanced_docs
def _extract_tags_from_cve(self, cve: Dict) -> List[str]:
"""Extract relevant tags from CVE data"""
tags = []
# Add severity tag (handle None values)
severity = cve.get('severity')
if severity:
tags.append(severity.lower())
# Add CWE-based tags
cwe_ids = cve.get('cwe_ids', [])
for cwe_id in cwe_ids:
if '787' in cwe_id: # Out-of-bounds Write
tags.extend(['buffer_overflow', 'memory_corruption'])
elif '125' in cwe_id: # Out-of-bounds Read
tags.extend(['buffer_overflow', 'information_disclosure'])
elif '89' in cwe_id: # SQL Injection
tags.extend(['sql_injection', 'database'])
elif '79' in cwe_id: # XSS
tags.extend(['xss', 'cross_site_scripting'])
elif '352' in cwe_id: # CSRF
tags.extend(['csrf', 'cross_site_request_forgery'])
# Add product-based tags
affected_products = cve.get('affected_products', [])
for product in affected_products:
product_lower = product.lower()
if 'web' in product_lower or 'server' in product_lower:
tags.append('web_application')
elif 'database' in product_lower or 'sql' in product_lower:
tags.append('database')
elif 'os' in product_lower or 'operating_system' in product_lower:
tags.append('operating_system')
# Add CAPEC tags
capec_entries = cve.get('capec_entries', [])
if capec_entries:
tags.append('capec')
tags.extend([f"capec_{capec.lower()}" for capec in capec_entries[:3]]) # Limit to 3
# Add MITRE ATT&CK tags
mitre_techniques = cve.get('mitre_techniques', [])
mitre_tactics = cve.get('mitre_tactics', [])
if mitre_techniques:
tags.append('mitre_attack')
tags.extend([f"technique_{tech.lower()}" for tech in mitre_techniques[:3]]) # Limit to 3
if mitre_tactics:
tags.extend([f"tactic_{tactic.lower()}" for tactic in mitre_tactics[:3]]) # Limit to 3
# Add KEV tag
if cve.get('is_in_kev'):
tags.extend(['kev', 'exploited', 'critical'])
return list(set(tags)) # Remove duplicates
def save_processed_data(self, enhanced_docs: List[Dict]):
"""Save processed data by year in both knowledge_base and CVE/processed directories"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
# Group documents by year
docs_by_year = {}
for doc in enhanced_docs:
if doc['document_type'] == 'CVE':
# Extract year from CVE ID (e.g., CVE-2024-1234 -> 2024)
cve_id = doc['id']
if cve_id.startswith('CVE-'):
try:
year = cve_id.split('-')[1]
if year not in docs_by_year:
docs_by_year[year] = []
docs_by_year[year].append(doc)
except (IndexError, ValueError):
# If we can't parse the year, put in 'unknown' category
if 'unknown' not in docs_by_year:
docs_by_year['unknown'] = []
docs_by_year['unknown'].append(doc)
else:
# Non-CVE documents (KEV, etc.) go to 'other' category
if 'other' not in docs_by_year:
docs_by_year['other'] = []
docs_by_year['other'].append(doc)
# Save by year in both locations
knowledge_base_dir = self.config.knowledge_base_dir
knowledge_base_dir.mkdir(parents=True, exist_ok=True)
total_saved = 0
for year, year_docs in docs_by_year.items():
if not year_docs:
continue
# Save to knowledge_base directory
kb_file = knowledge_base_dir / f"enhanced_documents_cve_{year}.json"
with open(kb_file, 'w', encoding='utf-8') as f:
json.dump(year_docs, f, indent=2, ensure_ascii=False)
# Save to CVE/processed directory
processed_file = self.processed_dir / f"enhanced_documents_cve_{year}.json"
with open(processed_file, 'w', encoding='utf-8') as f:
json.dump(year_docs, f, indent=2, ensure_ascii=False)
total_saved += len(year_docs)
logger.info(f"Saved {len(year_docs)} documents for year {year}")
# Also save complete dataset with timestamp
docs_file = self.processed_dir / f"enhanced_documents_{timestamp}.json"
with open(docs_file, 'w', encoding='utf-8') as f:
json.dump(enhanced_docs, f, indent=2, ensure_ascii=False)
# Calculate correlation statistics
kev_count = sum(1 for cve in self.all_cves if cve.get('is_in_kev'))
csaf_correlations = sum(len(cve.get('csaf_correlations', [])) for cve in self.all_cves)
exploitdb_correlations = sum(len(cve.get('exploitdb_correlations', [])) for cve in self.all_cves)
cwe_with_mapping = sum(1 for cve in self.all_cves if any(cwe in self.cwe_capec_mitre_mapping for cwe in cve.get('cwe_ids', [])))
capec_enriched = sum(1 for cve in self.all_cves if cve.get('capec_entries'))
mitre_enriched = sum(1 for cve in self.all_cves if cve.get('mitre_techniques'))
total_capec_entries = sum(len(cve.get('capec_entries', [])) for cve in self.all_cves)
total_mitre_techniques = sum(len(cve.get('mitre_techniques', [])) for cve in self.all_cves)
total_mitre_tactics = sum(len(cve.get('mitre_tactics', [])) for cve in self.all_cves)
# Save summary
summary = {
'timestamp': timestamp,
'total_cves_processed': len(self.all_cves),
'total_kev_entries': len(self.kev_data),
'total_enhanced_documents': len(enhanced_docs),
'cve_documents': len([d for d in enhanced_docs if d['document_type'] == 'CVE']),
'kev_documents': len([d for d in enhanced_docs if d['document_type'] == 'KEV']),
'documents_by_year': {year: len(docs) for year, docs in docs_by_year.items()},
'correlation_statistics': {
'cves_in_kev': kev_count,
'total_csaf_correlations': csaf_correlations,
'total_exploitdb_correlations': exploitdb_correlations,
'cves_with_cwe_mapping': cwe_with_mapping,
'cwe_capec_mitre_mappings_loaded': len(self.cwe_capec_mitre_mapping),
'cves_with_capec': capec_enriched,
'cves_with_mitre_techniques': mitre_enriched,
'total_capec_entries': total_capec_entries,
'total_mitre_techniques': total_mitre_techniques,
'total_mitre_tactics': total_mitre_tactics
},
'files': {
'enhanced_documents_timestamped': str(docs_file),
'knowledge_base_directory': str(knowledge_base_dir),
'processed_directory': str(self.processed_dir)
}
}
summary_file = self.processed_dir / f"processing_summary_{timestamp}.json"
with open(summary_file, 'w', encoding='utf-8') as f:
json.dump(summary, f, indent=2, ensure_ascii=False)
logger.info(f"Processed data saved to {self.processed_dir} and {knowledge_base_dir}")
logger.info(f"Summary: {len(self.all_cves)} CVEs, {len(self.kev_data)} KEV entries, {len(enhanced_docs)} enhanced documents")
logger.info(f"Correlations: {kev_count} CVEs in KEV, {csaf_correlations} CSAF correlations, {exploitdb_correlations} ExploitDB correlations")
logger.info(f"CWE-CAPEC-MITRE: {capec_enriched} CVEs with CAPEC, {mitre_enriched} CVEs with MITRE techniques")
logger.info(f"Total enrichments: {total_capec_entries} CAPEC entries, {total_mitre_techniques} MITRE techniques, {total_mitre_tactics} MITRE tactics")
logger.info(f"Saved {total_saved} documents across {len(docs_by_year)} year categories")
def run_full_processing(self, max_cves: Optional[int] = None):
"""Run the complete processing pipeline"""
logger.info("Starting full CVE processing pipeline...")
# Load CTI data first
logger.info("Loading CTI correlation data...")
self.load_cwe_capec_mitre_mapping()
self.load_csaf_data()
self.load_exploitdb_data()
self.load_kev_data()
# Process all CVE files
self.process_all_cve_files(max_cves)
# Enrich CVEs with CTI correlations
self.all_cves = enrich_all_cves(
self.all_cves,
self.kev_by_cve,
self.csaf_by_cve,
self.exploitdb_by_cve,
self.cwe_capec_mitre_mapping,
logger
)
# Create enhanced documents
enhanced_docs = self.create_enhanced_documents()
# Save everything using the class method
self.save_processed_data(enhanced_docs)
logger.info("Full CVE processing completed!")
def main():
"""Main function"""
processor = CVEProcessor()
# Process all CVEs
processor.run_full_processing(max_cves=None)
if __name__ == "__main__":
main()
|