mohamedahmedsalah002/Advanced-RAG-Windows-Forensics-Incident-Response
GitHub: mohamedahmedsalah002/Advanced-RAG-Windows-Forensics-Incident-Response
基于 Windows 取证与事件响应专业书籍构建的 RAG 问答系统,支持结构感知分块、FAISS 向量检索和带引用的 LLM 流式回答。
Stars: 0 | Forks: 0
# 高级 RAG — Windows 取证与事件响应
这是一个基于两本网络安全书籍构建的生产级 **检索增强生成** 系统,具备结构感知分块、FAISS 向量搜索、SQLite 元数据存储、Few-shot 提示,以及由 Groq 或 OpenAI 驱动的 Streamlit UI。
## 功能介绍
你可以提出诸如 *"How do you investigate persistence on a compromised host?"*(如何调查受损主机上的持久化?)这样的问题,系统将执行以下操作:
1. 使用 `BAAI/bge-small-en-v1.5` 对你的问题进行 Embedding
2. 通过 FAISS(余弦相似度)搜索 766 个已索引的文本块
3. 从 SQLite 获取完整的文本块内容及元数据
4. 将 3 个专家级的 Few-shot 问答示例注入到提示词中
5. 从 Groq (Llama 3.3 70B) 或 OpenAI 流式传输答案
6. 显示引用来源:书名、章节、页码范围
## 项目结构
```
├── ingest/ # Phase 1 — PDF text extraction
│ └── extract_pdf.py # Reads PDFs from data/raw/, writes chunks_raw.jsonl
│
├── chunking/ # Phase 2 — Structure-aware chunking
│ └── structure_aware.py # Splits by headings, ~250–450 tokens, 12% overlap
│
├── indexes/ # Phase 3 — Embedding + FAISS + SQLite
│ ├── build_index.py # Embeds chunks, builds faiss.index + metadata.sqlite
│ └── verify_index.py # End-to-end verification: FAISS → id_map → SQLite
│
├── retriever/ # Query-time retrieval
│ └── search.py # Embed query → FAISS search → fetch from SQLite
│
├── prompts/ # Prompt engineering
│ ├── few_shot_examples.json # 15 expert Q&A pairs (Windows Forensics & IR)
│ └── few_shot_loader.py # Builds full LLM prompt with few-shot + context
│
├── api/ # User interface
│ └── streamlit_app.py # Streamlit UI (Groq + OpenAI support, streaming)
│
├── data/
│ ├── raw/ # Put your PDF books here
│ └── processed/ # chunks_raw.jsonl and chunks.jsonl (auto-generated)
│
├── storage/ # FAISS index + SQLite (auto-generated)
├── models/ # Local embedding model (auto-downloaded)
├── config.yaml # Model name, embedding dim, batch size, paths
├── docker-compose.yml # Docker setup for the index build pipeline
├── Dockerfile
└── requirements.txt
```
## 所用书籍
| 书籍 | 领域 |
|------|--------|
| *Effective Threat Investigation for SOC Analysts* — Mostafa Yahia | SOC, Windows Event Logs, Threat Investigation |
| *Practical Windows Forensics* — Shaaban & Sapronov | DFIR, Registry, Memory, NTFS Forensics |
## 快速开始
### 1. 安装依赖
```
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```
### 2. 添加你的 PDF 文件
```
data/raw/
├── Effective Threat Investigation for SOC Analysts .pdf
└── practical_windows_forensic.pdf
```
### 3. 运行阶段 1 — 从 PDF 提取文本
```
python -m ingest.extract_pdf
# 输出:data/processed/chunks_raw.jsonl (616 个页面级 chunks)
```
### 4. 运行阶段 2 — 结构感知分块
```
python -m chunking.structure_aware
# 输出:data/processed/chunks.jsonl (828 个检索就绪 chunks)
```
### 5. 运行阶段 3 — 构建 FAISS 索引
```
docker compose build rag-app
docker compose run --rm rag-app python indexes/build_index.py
# 输出:storage/faiss.index, storage/id_map.json, storage/metadata.sqlite
```
将存储产物复制到本地文件夹:
```
docker run --rm \
-v advancedrag_rag_storage:/storage \
-v $(pwd)/storage:/dest \
alpine sh -c "cp /storage/* /dest/"
```
### 6. 在本地下载 Embedding 模型
```
docker run --rm \
-v $(pwd)/models:/models_out \
advancedrag-rag-app \
bash -c "python -c \"from sentence_transformers import SentenceTransformer; SentenceTransformer('BAAI/bge-small-en-v1.5').save('/models_out/bge-small-en-v1.5')\""
```
### 7. 运行 Streamlit UI
```
streamlit run api/streamlit_app.py
```
打开 [http://localhost:8501](http://localhost:8501),在侧边栏选择 **Groq** 或 **OpenAI**,粘贴你的 API Key,即可开始提问。
## 配置
编辑 `config.yaml` 以更改 Embedding 模型、批次大小或 FAISS 索引类型:
```
versions:
EMBEDDING_MODEL_NAME: BAAI/bge-small-en-v1.5
EMBEDDING_MODEL_LOCAL_PATH: models/bge-small-en-v1.5
EMBEDDING_DIM: 384
pipeline:
batch_size: 64
normalize_embeddings: true
faiss:
index_type: flatip # flatip (exact) | hnsw (approximate, faster at scale)
```
## LLM 提供商
| 提供商 | Key 前缀 | 推荐模型 | 备注 |
|----------|-----------|-------------------|-------|
| Groq | `gsk_...` | `llama-3.3-70b-versatile` | 免费层,速度极快 |
| OpenAI | `sk-...` | `gpt-4o-mini` | 付费 |
## 技术栈
| 组件 | 技术 |
|-----------|-----------|
| PDF 提取 | `pdfplumber` |
| 分块 | 自定义结构感知(标题 + 代码块感知) |
| Embeddings | 通过 `sentence-transformers` 使用 `BAAI/bge-small-en-v1.5` |
| Vector DB | `faiss-cpu` (IndexFlatIP) |
| Metadata DB | SQLite (内置,无需服务器) |
| LLM | Groq (Llama 3.3 70B) / OpenAI |
| UI | Streamlit |
| 容器化 | Docker + Docker Compose |
标签:BGE Embeddings, DLL 劫持, FAISS, Few-Shot Prompting, HTTPS请求, Kubernetes, Llama 3, OpenAI, PDF解析, Petitpotam, Python, RAG, SQLite, Streamlit, Sysdig, Windows取证, 人工智能, 内存规避, 向量搜索, 大语言模型, 库, 应急响应, 数字取证, 无后门, 检索增强生成, 用户模式Hook绕过, 知识库问答, 结构感知分块, 网络安全, 自动化脚本, 访问控制, 请求拦截, 逆向工具, 隐私保护