Mahmoud-Ossama/PurpleSOC

GitHub: Mahmoud-Ossama/PurpleSOC

一个在单一 Docker Compose 环境中集成自主红队攻击模拟与双引擎(深度学习+LLM)蓝队检测的 AI 驱动紫队网络安全平台。

Stars: 3 | Forks: 2


[![Typing SVG](https://readme-typing-svg.demolab.com?font=JetBrains+Mono&weight=600&size=17&duration=2800&pause=900&color=A855F7¢er=true&vCenter=true&multiline=true&width=860&height=72&lines=Dual-Engine+Detection+%7C+Red+Team+%2B+Blue+Team+in+One+Stack;99.25%25+ML+Accuracy+%7C+AUC-ROC+0.9994+%7C+88.6%25+FP+Reduction;Zero+Third-Party+Data+Routing+%7C+Complete+Data+Sovereignty)](https://git.io/typing-svg)
![Python](https://img.shields.io/badge/Python-3.11-3776AB?style=for-the-badge&logo=python&logoColor=white) ![TypeScript](https://img.shields.io/badge/TypeScript-5.0-3178C6?style=for-the-badge&logo=typescript&logoColor=white) ![Next.js](https://img.shields.io/badge/Next.js-14-000000?style=for-the-badge&logo=nextdotjs&logoColor=white) ![Docker](https://img.shields.io/badge/Docker-Compose-2496ED?style=for-the-badge&logo=docker&logoColor=white) ![MongoDB](https://img.shields.io/badge/MongoDB-7.0-47A248?style=for-the-badge&logo=mongodb&logoColor=white)
![TensorFlow](https://img.shields.io/badge/TensorFlow-Keras-FF6F00?style=for-the-badge&logo=tensorflow&logoColor=white) ![LangChain](https://img.shields.io/badge/LangChain-Agent-1C3C3C?style=for-the-badge&logo=chainlink&logoColor=white) ![FastAPI](https://img.shields.io/badge/FastAPI-0.110-009688?style=for-the-badge&logo=fastapi&logoColor=white) ![HAProxy](https://img.shields.io/badge/HAProxy-2.8-009639?style=for-the-badge&logoColor=white) ![PHP](https://img.shields.io/badge/PHP-8.1-777BB4?style=for-the-badge&logo=php&logoColor=white)

![ML Accuracy](https://img.shields.io/badge/ML_Accuracy-99.25%25-22c55e?style=for-the-badge&labelColor=14532d) ![AUC-ROC](https://img.shields.io/badge/AUC--ROC-0.9994-22c55e?style=for-the-badge&labelColor=14532d) ![F1 Score](https://img.shields.io/badge/F1--Score-0.9810-22c55e?style=for-the-badge&labelColor=14532d) ![FP Reduction](https://img.shields.io/badge/FP_Reduction-88.6%25-a855f7?style=for-the-badge&labelColor=4c1d95) ![Containers](https://img.shields.io/badge/Docker_Containers-7-60a5fa?style=for-the-badge&labelColor=1e3a5f) ![Test Flows](https://img.shields.io/badge/Test_Flows-565%2C576-f59e0b?style=for-the-badge&labelColor=78350f)

## 📋 目录 - [摘要](#-abstract) - [系统架构](#-system-architecture) - [技术栈](#-tech-stack) - [Docker 容器](#-docker-containers) - [🔴 红队 — REDTEAM Agent](#-red-team--redteam-agent) - [🔵 蓝队 — 双重检测引擎](#-blue-team--dual-detection-engines) - [📊 性能结果](#-performance-results) - [🎯 双引擎检测覆盖范围](#-dual-engine-detection-coverage) - [🚀 快速开始](#-getting-started) - [🗺️ 路线图](#-roadmap) - [👥 团队](#-team) - [📚 参考文献](#-references) ## 📖 摘要 **PurpleSOC** 是一个完整的、生产级的网络安全研究平台,它在一个完全容器化的 Docker Compose 环境中统一了攻击模拟(*红队*)和防御监控(*蓝队*)。 该系统的设计围绕三个核心见解: 在 **CICIDS-2017**(565,576 条预留测试流量)上训练的 Keras 深度神经网络与一个经过 prompt 工程设计的 **LLM 语义分析层**并行运行——攻击者必须同时欺骗两种完全不同的检测机制才能逃避检测。 ## 🏗️ 系统架构 ``` flowchart TB classDef redTeam fill:#7F1D1D,stroke:#EF4444,color:#FCA5A5,stroke-width:2px classDef blueTeam fill:#1E3A5F,stroke:#60A5FA,color:#BAE6FD,stroke-width:2px classDef infra fill:#1F2937,stroke:#9CA3AF,color:#E5E7EB,stroke-width:2px classDef mongo fill:#14532D,stroke:#22C55E,color:#BBF7D0,stroke-width:2px classDef soc fill:#4C1D95,stroke:#A78BFA,color:#DDD6FE,stroke-width:2px REDTEAM("🔴 REDTEAM Agent\nTypeScript · Temporal"):::redTeam BROWSER("👤 Legitimate Browser\nBenign Traffic"):::infra HAPROXY("⚖️ HAProxy :80\nReverse Proxy · Lua Logging"):::infra VULNSHOP("🛒 VulnShop :8080\nPHP · Apache · MySQL"):::infra CICFLOW("📡 CICFlowMeter\nJava · shared network namespace"):::infra MLDET("🤖 ML Detector :8000\nFastAPI · Keras DNN · τ=0.89"):::blueTeam LLMAGENT("🧠 LLM Agent\nLangChain · Groq · TCP:514"):::blueTeam MONGODB[("🗄️ MongoDB :27017\nCentral Store")]:::mongo DASHBOARD("🖥️ SOC Dashboard :3000\nNext.js · Tailwind CSS"):::soc REDTEAM & BROWSER -->|HTTP Attacks / Normal Traffic| HAPROXY HAPROXY -->|Proxy| VULNSHOP HAPROXY -.->|Raw Packets| CICFLOW HAPROXY -->|TCP:514 · JSON Logs| LLMAGENT CICFLOW -->|80+ Flow Features| MLDET MLDET -->|ML Classification → traffic_logs_col| MONGODB LLMAGENT -->|LLM Verdict → reports_col| MONGODB MONGODB -->|Read| DASHBOARD ``` ## 🛠️ 技术栈
[![Skills](https://skillicons.dev/icons?i=python,typescript,nextjs,react,tailwindcss,docker,mongodb,tensorflow&theme=dark&perline=8)](https://skillicons.dev) [![Skills](https://skillicons.dev/icons?i=java,php,nginx,linux,bash,vscode,git,github&theme=dark&perline=8)](https://skillicons.dev)

| 层级 | 技术 | 作用 | |---|---|---| | 🔴 **红队编排** | TypeScript · Node.js · Temporal | 持久化的五阶段渗透测试 pipeline | | 🔴 **红队 LLM 引擎** | OpenAI-compat SDK · Ollama · OpenRouter | 与提供商无关的 agent 循环 | | 🔴 **脆弱目标** | PHP 8.1 · Apache · MySQL | 完整的 OWASP Top 10 攻击面 | | ⚖️ **反向代理** | HAProxy 2.8 · Lua | 零延迟 JSON payload 镜像 | | 📡 **流量分析** | CICFlowMeter · Java · OpenJDK 11 | 80+ 双向流量特征 | | 🤖 **ML 检测** | Keras · TensorFlow · FastAPI · scikit-learn | DNN 二分类器,<8ms 推理 | | 🧠 **LLM 检测** | LangChain · Groq · Pydantic · PyMongo | 语义 payload 分析 | | 🗄️ **存储** | MongoDB 7.0 | 中央文档存储 | | 🖥️ **仪表板** | Next.js 14 · TypeScript · Tailwind CSS | 实时 SOC 分析师界面 | | 🐳 **编排** | Docker Compose | 7 容器隔离环境 |
## 🐳 Docker 容器 ``` graph LR subgraph COMPOSE["🐳 Docker Compose Environment"] direction TB subgraph RED["🔴 Red Team"] A["VulnShop\nphp:8.1-apache\n:8080"] B["REDTEAM Agent\nnode:20-alpine"] end subgraph ROUTING["⚙️ Traffic Layer"] C["HAProxy\nhaproxy:2.8\n:80 · :443 · :514"] D["CICFlowMeter\nopenjdk:11\nshared netns"] end subgraph BLUE["🔵 Blue Team"] E["ML Detector\npython:3.11-slim\n:8000"] F["LLM Agent\npython:3.11-slim"] end subgraph PLATFORM["🟣 Platform"] G[("MongoDB\nmongo:7.0\n:27017")] H["SOC Dashboard\nnode:20-alpine\n:3000"] end B --> C --> A C -.-> D --> E --> G C --> F --> G --> H end ``` | 容器 | 基础镜像 | 端口 | 职责 | |---|---|---|---| | **VulnShop** | `php:8.1-apache` | `8080` | 故意遗留漏洞的 PHP 电子商务应用 (OWASP Top 10) | | **HAProxy** | `haproxy:2.8` | `80, 443, 514` | 反向代理 + 内嵌 Lua 脚本,用于零延迟 JSON 日志记录 | | **CICFlowMeter** | `openjdk:11` | — | 共享 HAProxy 的 network namespace;捕获原始数据包 → 80+ 流量特征 | | **ML Detector** | `python:3.11-slim` | `8000` | FastAPI + Keras DNN;接收特征向量,返回攻击/良性判定 | | **LLM Agent** | `python:3.11-slim` | — | TCP:514 socket server → LangChain/Groq → 结构化威胁判定 → MongoDB | | **MongoDB** | `mongo:7.0` | `27017` | 双检测引擎的中央文档存储 | | **SOC Dashboard** | `node:20-alpine` | `3000` | Next.js 前端;实时威胁订阅源、地图、图表 | ## 🔴 红队 — REDTEAM Agent REDTEAM 是一个 TypeScript/Node.js 自主化渗透测试框架。它通过一个**由 Temporal 编排、LLM 驱动的五阶段 pipeline**,将静态代码分析与实时应用探测相结合。 ### 五阶段 Pipeline ``` flowchart TD classDef phase fill:#7F1D1D,stroke:#EF4444,color:#FCA5A5,stroke-width:2px classDef vuln fill:#78350F,stroke:#F59E0B,color:#FDE68A,stroke-width:2px classDef report fill:#4C1D95,stroke:#A78BFA,color:#DDD6FE,stroke-width:2px P1("📁 Phase 1 — Pre-Recon\nStatic code analysis\nMaps routes · auth · attack surface"):::phase P2("🌐 Phase 2 — Recon\nLive application correlation\nConfirms endpoints · discovers routes"):::phase subgraph P34["⚡ Phases 3–4 — Parallel per Vulnerability Class"] direction LR VA1("💉 Injection\nVuln → Queue → Exploit"):::vuln VA2("⚡ XSS\nVuln → Queue → Exploit"):::vuln VA3("🔑 Auth\nVuln → Queue → Exploit"):::vuln VA4("🚪 AuthZ\nVuln → Queue → Exploit"):::vuln VA5("🌍 SSRF\nVuln → Queue → Exploit"):::vuln end P5("📊 Phase 5 — Report\nAssembly · dedup · exec summary\ncomprehensive_security_assessment_report.md"):::report P1 --> P2 --> P34 --> P5 ``` ### Agent 模型层级分配 | Agent | 层级 | 模型 | 理由 | |---|---|---|---| | 预侦察 | **大型** | `qwen3:32b` | 深度静态分析,最大化上下文窗口 | | 侦察 | **中型** | `qwen3:8b` | 实时关联需要可靠的 tool calling | | 漏洞探测 agent (×5) | **中型** | `qwen3:8b` | 频繁使用工具;平衡速度与推理能力 | | 漏洞利用 agent (×5) | **中型** | `qwen3:8b` | 证据优先,条件执行 | | 报告 / 总结 | **小型** | `llama3.2:3b` | 仅提取信息;无复杂推理 | ### 支持的 LLM 提供商 | 提供商 | API Key | 最适用场景 | |---|---|---| | `ollama` | 否 | 本地 / 离线 / 开发 | | `openrouter` | 是 | 云端生产环境扫描 | | `agent_router` | 可选 | 自定义路由基础设施 | | `openai_compat` | 可选 | 任何兼容 OpenAI 的 endpoint |
🔌 提供商池 — 应对速率限制的弹性机制
使用 `REDTEAM_PROVIDER_1_*`, `REDTEAM_PROVIDER_2_*` 等配置多个提供商。 该池实现了带有**两级冷却时间**的自动故障转移: | 错误类型 | 基础冷却时间 | 最大冷却时间 | |---|---|---| | `rate_limit` (HTTP 429) | 60秒 | 5 分钟 | | `transient` (超时 / 网络 / 服务器) | 10秒 | 60 秒 | - 发生任何 `error.retryable === true` → 轮询至下一个提供商 - `maxAttempts = providers.length × 3` - 所有提供商使用 `300s` 超时以处理庞大的工具调用上下文
## 🔵 蓝队 — 双重检测引擎 ### 引擎 1 — 深度学习 IDS (`ml_detector/detector.py`) 在 **CICIDS-2017** 上训练的全连接神经网络,使用通过 Pearson 相关系数分析挑选出的 20 个具有区分度的 CICFlowMeter 特征,将网络流分类为攻击或良性。 #### 网络架构 ``` flowchart LR I("Input\n20 features") --> D1("Dense 256\nReLU") --> DR1("Dropout\np=0.30") DR1 --> D2("Dense 128\nReLU") --> DR2("Dropout\np=0.30") DR2 --> D3("Dense 64\nReLU") --> DR3("Dropout\np=0.25") DR3 --> O("Sigmoid\nOutput\nP(attack)") O --> T("Threshold\nτ = 0.89\n→ ATTACK") style I fill:#1E3A5F,stroke:#60A5FA,color:#BAE6FD style D1 fill:#164E63,stroke:#22D3EE,color:#A5F3FC style D2 fill:#164E63,stroke:#22D3EE,color:#A5F3FC style D3 fill:#164E63,stroke:#22D3EE,color:#A5F3FC style DR1 fill:#1F2937,stroke:#6B7280,color:#D1D5DB style DR2 fill:#1F2937,stroke:#6B7280,color:#D1D5DB style DR3 fill:#1F2937,stroke:#6B7280,color:#D1D5DB style O fill:#14532D,stroke:#22C55E,color:#BBF7D0 style T fill:#4C1D95,stroke:#A78BFA,color:#DDD6FE ``` #### 训练配置 | 参数 | 值 | |---|---| | 优化器 | Adam (lr=1e-3, β₁=0.9, β₂=0.999) | | 损失函数 | Binary cross-entropy | | Batch size | 512 | | 最大 epoch 数 | 100 (early stopping, patience=10) | | 数据集划分 | 70% 训练 / 15% 验证 / 15% 测试 (分层抽样) | | 预处理 | StandardScaler 仅在训练集上进行拟合 | | 阈值 τ | 0.89 (在 0.50→0.99 范围内最大化 F1-Score 扫描) | | 推理延迟 | ~8ms · ~120 req/s | #### 训练结果
| ![训练历史](https://raw.githubusercontent.com/Mahmoud-Ossama/PurpleSOC/main/models/__results___8_1.png) | |---| | *图 4.1 — 训练 epoch 中收敛的 Loss 曲线、准确率和 AUC-ROC* | | ![混淆矩阵 & ROC](https://raw.githubusercontent.com/Mahmoud-Ossama/PurpleSOC/main/models/__results___4_1.png) | |---| | *图 4.3 — 在 565,576 条预留测试流上的混淆矩阵(左)与 ROC 曲线 AUC=0.9994(右)* |
### 引擎 2 — LLM 语义 Agent (`agent/agent.py`) 一个 Python 服务,它从 HAProxy (TCP:514) 读取原始 HTTP payload,并使用经过 prompt 工程设计的 LLM 执行语义安全分析。该层检测**仅靠流统计无法察觉的应用层攻击**。 #### 威胁评分公式 ``` S = w_type × w_confidence × 10.0 ``` | 攻击类型 | 权重 | 理由 | |---|---|---| | `SQLi` | **1.00** | 完全控制数据库的潜在风险 | | `CMD_INJECTION` | **1.00** | 远程代码执行的潜在风险 | | `PATH_TRAVERSAL` | **0.90** | 文件系统访问;凭证泄露 | | `SSRF` | **0.85** | 内网跳板渗透 | | `XSS` | **0.75** | 客户端执行;会话窃取 | | `Benign` | **0.00** | 无威胁 | #### Prompt 工程 — v1 → v5 迭代 ``` xychart-beta horizontal title "False Positive Rate Reduction Through Prompt Engineering" x-axis ["v1 Naive", "v2 +Taxonomy", "v3 +JSON Format", "v4 +FP Examples", "v5 +Evidence Req"] y-axis "FP Rate (%)" 0 --> 20 bar [18.4, 11.7, 8.3, 3.9, 2.1] ``` | 版本 | FP 率 | FN 率 | 主要变更 | |---|---|---|---| | v1 (朴素) | 18.4% | 4.2% | 自由格式输出,无示例 | | v2 | 11.7% | 4.8% | + 正式的攻击类型分类体系 | | v | 8.3% | 4.5% | + 强制结构化 JSON 输出 | | v4 | 3.9% | 4.1% | + 精选的良性流量示例 (FP 指引) | | **v5** | **2.1%** | **3.8%** | **+ 强制要求证据引用** |
📋 生产环境系统 Prompt 结构 (v5)
``` ATTACK TAXONOMY: SQLi | XSS | PATH_TRAVERSAL | CMD_INJECTION | SSRF | Benign FALSE POSITIVE GUIDANCE: - "select" in a product search is NOT SQLi - HTML angle brackets in blog content are NOT XSS - ".." as part of a legitimate URL slug is NOT path traversal REQUIRED JSON OUTPUT (no preamble, no markdown fences): { "attack_type": "", "confidence": , "severity": "", "evidence": "", "reasoning": "<1–2 sentence plain-English explanation>", "recommended_action": "" } ```
## 📊 性能结果
### ML 检测器 — 预留测试集 (N = 565,576 条流量) | 指标 | 值 | 可视化 | |---|---|---| | 总体准确率 | **99.25%** | `████████████████████` 99.25% | | AUC-ROC | **0.9994** | `████████████████████` 99.94% | | F1-Score (攻击) | **0.9810** | `███████████████████░` 98.10% | | 精确率 (攻击) | **97.80%** | `███████████████████░` 97.80% | | 召回率 (攻击) | **98.39%** | `███████████████████░` 98.39% | | 类别 | 数量 | |---|---| | ✅ 真阳性 | 109,524 | | ✅ 真阴性 | 451,801 | | ❌ 假阳性 | 2,464 | | ❌ 假阴性 | 1,787 | | 🎯 决策阈值 | τ = 0.89 | ### 系统延迟与吞吐量 | 组件 | 平均延迟 | 吞吐量 | 备注 | |---|---|---|---| | HAProxy Lua 日志记录 | **< 1 ms** | > 10,000 req/s | 内存中处理,无磁盘 I/O | | CICFlowMeter | ~50 ms/flow | ~2,000 flows/s | 流后计算 | | ML 检测器 | **~8 ms** | ~120 req/s | 单次 Keras 前向传播 | | LLM Agent | 1.2–3.5 s | ~20 req/s | 主要瓶颈 (API 调用) | | MongoDB 写入 | ~5 ms | ~3,000 writes/s | 单节点 | | 仪表板轮询周期 | 5 秒间隔 | — | 可配置 |
## 🎯 双引擎检测覆盖范围 核心实证结论:**攻击者必须同时欺骗两种根本不同的检测机制**。逃避 ML 引擎需要统计上正常的流量;而逃避 LLM 引擎则需要干净的 payload——对于大多数攻击类别而言,这两个要求是相互矛盾的。 ``` vennDiagram ``` | 攻击场景 | 🤖 ML 引擎 | 🧠 LLM 引擎 | 原因 | |---|---|---|---| | 标准 SQL 注入 | ✅ | ✅ | 两者通过互补模态检测 | | URL 编码的 SQLi | ✅ | ✅ | LLM 在分析前进行解码 | | **慢速 SQLi (1 req / 5 min)** | ❌ | ✅ | 流统计数据表现正常;LLM 读取 payload | | 快速自动化扫描 (500/min) | ✅ | ✅ | 两者均检测到;ML 检测到速率异常 | | **良性流量中的 XSS** | ❌ | ✅ | 短的 GET 请求在统计上显得正常;LLM 抓取 `