File size: 8,950 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 |
<!-- PROJECT LOGO -->
<br />
<p align="center">
<a href="https://github.com/Yuning-J/CVE-KGRAG">
</a>
<br />
<!-- Badges -->
<img src="https://img.shields.io/github/repo-size/Yuning-J/CVE-KGRAG?style=for-the-badge" alt="GitHub repo size" height="25">
<img src="https://img.shields.io/github/last-commit/Yuning-J/CVE-KGRAG?style=for-the-badge" alt="GitHub last commit" height="25">
<img src="https://img.shields.io/github/license/Yuning-J/CVE-KGRAG?style=for-the-badge" alt="License" height="25">
<br />
<h3 align="center">CVE-KGRAG</h3>
<p align="center">
CVE Knowledge Graph & Security Intelligence System with Enhanced RAG
</p>
</p>
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**
```bash
# 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)**
```bash
# Download all CVE data from NVD
python scripts/download_all_cves.py
```
### **Step 2: Process and Build Knowledge Graph**
```bash
# 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**
```bash
# 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**
```bash
# 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**
```bash
# 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**
```bash
# Verify system can handle training
python src/training/system_check.py
```
### **Step 5c: Run Fine-Tuning**
```bash
python src/training/production_training.py
```
### **Step 5d: Test Fine-Tuned Model**
```bash
# Test the fine-tuned model
python src/training/hf_inference_engine.py
```
### **Step 6: Start Services**
```bash
# 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**
```bash
# 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**
- Gradio UI: http://localhost:7860
- API Documentation: http://localhost:8000/docs
- API Health Check: http://localhost:8000/api/v1/health
## 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
|