cve-kgrag-db / code /README.md
DuyTa's picture
Add code/: full CVE-KGRAG project source snapshot
27f6252 verified
|
Raw
History Blame Contribute Delete
8.95 kB


GitHub repo size GitHub last commit License

CVE-KGRAG

CVE Knowledge Graph & Security Intelligence System with Enhanced RAG

This project combines a comprehensive knowledge graph for structured vulnerability data and relationships with an enhanced Retrieval-Augmented Generation (RAG) system for semantic search. We automate the process of curation, processing and correlation of CVE, CPE, CWE, CAPEC, MITRE ATT&CK, ExploitDB, CISA and other threat intelligence data.

Integration

  • CVE-KGRAG is used as the threat-intelligence backend for simulation projects such as APTArena/CyGATE.
  • Typical outputs consumed by simulation pipelines include exploitability signals, tactic mappings, and vulnerability relationship context.

Current Statistics (Latest)

Knowledge Graph Coverage (1999-2025)

  • 190,310 CVEs with rich metadata (CVSS, affected products, CWE, CAPEC, MITRE mappings)
  • 124,290 products from 19,692 vendors
  • 458 CWEs, 428 CAPECs, 169 MITRE techniques, 37 MITRE tactics
  • 2.4M+ relationships between entities
  • 80% have CVSS v3 scores (152,676 CVEs)
  • 1,060 CVEs in Known Exploited Vulnerabilities (KEV) list

NetworkX Graph Statistics

  • 335,178 nodes (CVEs, Products, Vendors, CWEs, CAPECs)
  • 1,126,306 edges (relationships between entities)
  • 246 vulnerability clusters based on CWE and product relationships
  • Graph density: 0.000010 (sparse, efficient graph structure)

Quick Start

Prerequisites

# Install Python dependencies
pip install -r requirements.txt

# Optional: if you have NVIDIA GPU and want CUDA-specific PyTorch wheels,
# install torch/torchvision/torchaudio separately from the official PyTorch index.

# Install Ollama from https://ollama.ai
# Then pull required models:
ollama pull llama3.1:8b
ollama pull llama3.1:70b  # Optional: for higher quality responses

Complete Setup Workflow

Step 1: Download CVE Data (1999-2025)

# Download all CVE data from NVD
python scripts/download_all_cves.py

Step 2: Process and Build Knowledge Graph

# Collect and process threat intelligence data
python src/collectors/main_collector.py

# Process all CVE data with enrichment
python src/processors/process_all_cves.py

# Parse CPEs and extract products/vendors
python src/constructors/run_cpe_extraction.py

# Build the knowledge graph (JSON-based, no Neo4j required)
python src/constructors/kg_builder_without_neo4j.py

Step 3: Start Services & Load Knowledge Graph into Neo4j

# Start Neo4j and Qdrant (Docker required)
docker compose up -d

# Bulk-load all KG JSON files into Neo4j (~190K CVEs, 2.4M relationships)
python -m src.constructors.neo4j_graph_service --bulk-load

# Verify Neo4j counts
python -m src.constructors.neo4j_graph_service --stats

Step 4: Export Chunks & Build Qdrant Hybrid Index

# Export structure-aware chunks for all four data types
# (CVE by section, MITRE techniques, CAPEC patterns, CWE weaknesses)
python -m src.generators.export_kg_for_rag_direct --full

# Build Qdrant collections with dense + BM25 sparse vectors (hybrid search)
python -m src.generators.rag_system --build

Step 5: LLM Training (Optional)

Step 5a: Prepare Training Dataset

# Create training dataset from knowledge graph
python src/training/dataset_preparation.py

# Analyze data quality if needed
python src/training/run_data_analysis.py

Step 5b: Check System Requirements

# Verify system can handle training
python src/training/system_check.py

Step 5c: Run Fine-Tuning

python src/training/production_training.py

Step 5d: Test Fine-Tuned Model

# Test the fine-tuned model
python src/training/hf_inference_engine.py

Step 6: Start Services

# Terminal 1: Start API Server
python -m uvicorn src.api.main:app --host 0.0.0.0 --port 8000 --reload

# Terminal 2: Start Gradio UI (Optional)
python src/ui/gradio_app.py

Test the System

# Hybrid search — CVE collection (dense + BM25 sparse, RRF fusion)
python -m src.generators.rag_system --search "Log4j JNDI injection"

# Lexical wins: exact CVE-ID lookup
python -m src.generators.rag_system --search "CVE-2021-44228"

# Search MITRE techniques collection
python -m src.generators.rag_system --search "T1059 command scripting" --collection mitre

# Graph: find related CVEs via Neo4j
python -m src.constructors.neo4j_graph_service --similar CVE-2021-44228

# Test API endpoints
curl -X POST http://localhost:8000/api/v1/search \
  -H "Content-Type: application/json" \
  -d '{"query": "SQL injection vulnerabilities", "top_k": 5}'

# Graph endpoint
curl -X POST http://localhost:8000/api/v1/graph/similar \
  -H "Content-Type: application/json" \
  -d '{"cve_id": "CVE-2021-44228", "k": 5}'

# Cross-collection search
curl -X POST http://localhost:8000/api/v1/search/mitre \
  -H "Content-Type: application/json" \
  -d '{"query": "lateral movement", "top_k": 5}'

Access Interfaces

Architecture Overview

Data Pipeline

Raw CVE Data (NVD) → Processed CVE Data → CPE Extraction → Knowledge Graph → NetworkX Graph → Vector DB

Knowledge Graph Structure

Nodes: CVEs, Products, Vendors, CWEs, CAPECs, MITRE Techniques, MITRE Tactics
Relationships: CVE→Product, CVE→CWE, CVE→CAPEC, CVE→MITRE, Product→Vendor

Enhanced RAG System Architecture

┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   Query Input   │───▶│  FastAPI API    │───▶│  Hybrid Search  │
└─────────────────┘    └─────────────────┘    └─────────────────┘
                                │                       │
                                ▼                       ▼
                       ┌─────────────────┐    ┌─────────────────┐
                       │  Gradio UI      │    │  Graph Features │
                       └─────────────────┘    └─────────────────┘
                                │                       │
                                ▼                       ▼
                       ┌─────────────────┐    ┌─────────────────┐
                       │  Qdrant + BM25  │    │  Neo4j Graph DB │
                       └─────────────────┘    └─────────────────┘
                                │                       │
                                ▼                       ▼
                       ┌─────────────────┐    ┌─────────────────┐
                       │  Dense + Sparse │    │  Graph-based    │
                       │  RRF Fusion     │    │  Similarity     │
                       └─────────────────┘    └─────────────────┘
                                │
                                ▼
                       ┌─────────────────┐
                       │  LLM Response   │
                       │  (OpenAI compat)│
                       └─────────────────┘

Configuration

  • Main config: config.py
  • RAG config: src/generators/rag_config.py
  • LLM providers: llms/ (factory-based: OpenAI-compatible, Ollama, vLLM)
  • Example Cypher queries: doc/Neo4j_Queries.md

API Endpoints

  • POST /api/v1/query - Full RAG queries with LLM responses
  • POST /api/v1/search - Vector search only
  • POST /api/v1/summary - Statistical analysis
  • GET /api/v1/health - System health check