AliMurad0/LLM-Security-Firewall

GitHub: AliMurad0/LLM-Security-Firewall

基于语义向量嵌入和 XGBoost 分类器的大语言模型 prompt 注入防护墙,在用户输入到达 LLM 前进行实时威胁检测与拦截。

Stars: 0 | Forks: 0

# 🛡️ LLM-Security-Firewall
![架构](https://raw.githubusercontent.com/AliMurad0/LLM-Security-Firewall/main/assets/architecture-overview.png) ### 🚨 用于大型语言模型(LLM)的语义 Prompt 注入防火墙 ![Python](https://img.shields.io/badge/Python-3.10+-blue?style=for-the-badge\&logo=python) ![XGBoost](https://img.shields.io/badge/Model-XGBoost-green?style=for-the-badge) ![Streamlit](https://img.shields.io/badge/UI-Streamlit-red?style=for-the-badge\&logo=streamlit) ![FastAPI](https://img.shields.io/badge/API-FastAPI-009688?style=for-the-badge\&logo=fastapi) ![LLM Security](https://img.shields.io/badge/Focus-LLM%20Security-black?style=for-the-badge) ![License](https://img.shields.io/badge/License-MIT-yellow?style=for-the-badge)
# 📌 概述 **LLM-Security-Firewall** 是一个轻量级的语义防火墙,旨在保护大型语言模型(LLM)免受以下威胁: * 🚫 Prompt 注入攻击 * 🔓 越狱(Jailbreak)尝试 * 🧠 角色扮演漏洞利用 * ⚠️ 指令覆盖攻击 * 🔐 系统 Prompt 泄露 该系统引入了**解耦安全架构**,可在 prompt 到达 LLM 之前对其进行拦截和分析。 与传统的关键词过滤系统不同,本项目采用了: * 🧠 语义向量嵌入 * ⚡ XGBoost 威胁分类 * 📊 实时概率分析 * 🛡️ 本地安全推理 这使得防火墙即使在使用混淆或改写手段进行攻击时,也能检测出恶意意图。 # 🏗️ 系统架构
![工作流](https://static.pigsec.cn/wp-content/uploads/repos/cas/5f/5f59493d10e1320b16672ba2f9d760e9cb61f51058ab3a2bedf9f67a0c750717.png)
防火墙通过一个 **4 阶段安全流水线(Pipeline)** 运行: | 阶段 | 描述 | | -------------------- | ---------------------------------------- | | 📥 输入与接收 | 用户通过 Streamlit UI 提交 prompt | | 🧠 语义分析 | MiniLM 将 prompt 转换为 embedding | | 🚦 逻辑门 | XGBoost 对恶意概率进行分类 | | ☁️ 安全决议 | 仅经过验证的 prompt 会到达 LLM | # 🔐 威胁检测逻辑
![决策流程](https://static.pigsec.cn/wp-content/uploads/repos/cas/1c/1c74e41617d1a733ab9814bb8173c98b22435659ee96886ddcbbc696ffeddda0.png)
安全核心执行以下操作: ``` Prompt → Embedding → XGBoost → Threat Score → Decision ``` ## ⚙️ 检测流水线 ### 步骤 1 — 语义向量化 使用以下方法将 prompt 转换为 **384 维语义向量**: ``` all-MiniLM-L6-v2 ``` ### 步骤 2 — 分类 使用 **XGBoost 分类器** 对 embedding 进行分析。 ### 步骤 3 — 安全决议 | 威胁分数 | 操作 | | ------------ | ----------------- | | > 0.5 | 🚫 拦截 Prompt | | ≤ 0.5 | ✅ 转发至 LLM | # 💬 用户交互流程
![聊天流程](https://static.pigsec.cn/wp-content/uploads/repos/cas/0d/0d99463f58701c880fb00aecdce7bb55603639f7c0bf2fb3e33c6230764a282d.png)
用户体验专为以下目标设计: * ⚡ 实时检测 * 📊 即时反馈 * 🛡️ 透明的安全状态 * ☁️ 安全的 API 路由 恶意 prompt 会在任何外部传输发生之前被本地拦截。 # ✨ 功能 ## 🛡️ 安全性 * Prompt 注入检测 * 越狱预防 * 语义威胁分析 * 基于概率的威胁评分 * 解耦的 LLM 防火墙 ## ⚡ 性能 * 轻量级 ML 推理 * 低延迟 (<50ms) * 对 CPU 友好的部署 * 实时分类 ## 📊 界面 * 交互式 Streamlit 仪表板 * Plotly 威胁计量表 * 会话威胁日志 * 安全/拦截视觉警报 ## ☁️ API 集成 * OpenRouter 集成 * 安全请求路由 * 条件性 API 访问 * 外部 LLM 保护层 # 🧠 机器学习流水线 | 组件 | 技术 | | -------------------- | --------------------- | | Embedding 模型 | Sentence Transformers | | 语义编码器 | all-MiniLM-L6-v2 | | 分类器 | XGBoost | | 前端 | Streamlit | | 后端 | FastAPI | | 可视化 | Plotly | | API 集成 | OpenRouter | | 编程语言 | Python | # 📈 模型性能
| 指标 | 分数 | | ------------------------- | ------ | | 🎯 准确率 (Accuracy) | 85.15% | | 📈 ROC-AUC | 0.8989 | | ⚖️ 加权 F1-Score | 83.85% | | 🛡️ 精确率 (恶意) | 86.20% | | 🚨 召回率 (恶意) | 81.50% |
# 📂 项目结构 ``` LLM-Security-Firewall/ │ ├── streamlit_app.py ├── api_server.py ├── config.py ├── requirements.txt ├── README.md ├── .gitignore ├── .env.example ├── LICENSE │ ├── models/ │ └── xgb_embed_model.json │ ├── notebook/ │ └── PromptInjectionSecurityFilter.ipynb │ ├── assets/ │ ├── architecture-overview.png │ ├── technical-workflow.png │ ├── security-decision-flow.png │ └── chat-flow-diagram.png │ └── docs/ ├── Project_Report.pdf └── PSF_Presentation.pptx ``` # 🚀 安装说明 ## 1️⃣ 克隆仓库 ``` git clone https://github.com/YOUR_USERNAME/LLM-Security-Firewall.git cd LLM-Security-Firewall ``` ## 2️⃣ 安装依赖项 ``` pip install -r requirements.txt ``` ## 3️⃣ 配置环境变量 创建一个 `.env` 文件: ``` OPENROUTER_API_KEY=your_api_key_here ``` # ▶️ 运行应用程序 ## 🔥 启动 Streamlit 前端 ``` streamlit run streamlit_app.py ``` ## ⚡ 启动 FastAPI 后端 ``` uvicorn api_server:app --reload ``` # 🔄 安全工作流 ``` User Prompt ↓ Semantic Vectorization ↓ XGBoost Threat Classification ↓ Threat Score Evaluation ↓ ┌───────────────┬────────────────┐ │ Malicious │ Safe │ │ Prompt │ Prompt │ ├───────────────┼────────────────┤ │ BLOCK │ Forward to LLM │ └───────────────┴────────────────┘ ``` # 📚 研究贡献 本项目证明了: * 轻量级机器学习模型能够有效地保护 LLM * 语义 embedding 优于传统的关键词过滤器 * 解耦架构降低了推理成本 * 实时 LLM 防火墙可以在标准消费级硬件上运行 # 🔮 未来改进 * 🧠 集成 FAISS 向量数据库 * 🌍 多语言攻击检测 * 🖼️ 多模态 prompt 注入防御 * 💬 多轮对话记忆分析 * ⚡ 自适应阈值优化 * ☁️ 云原生部署支持 # 📄 文档 详细的研究与实现文档位于: ``` docs/ ``` 包括: * 📘 研究报告 * 📊 项目演示文稿 * 🧪 实验结果 * 🧠 ML 工作流分析 # ⭐ 支持 如果您觉得这个项目有用: * ⭐ 为本仓库点 Star * 🍴 Fork 该项目 * 🛡️ 为 LLM 安全研究做出贡献 # 📜 许可证 该项目基于 **MIT License** 授权。
## 🛡️ 保护 AI 的未来 ### 为大型语言模型构建防御智能
标签:Apex, AV绕过, FastAPI, Kubernetes, XGBoost, 大语言模型安全, 提示词注入防护, 机器学习, 机密管理, 逆向工具