kbajish/ai-security-gateway

GitHub: kbajish/ai-security-gateway

面向 LLM 应用的中间件安全网关,通过规则、机器学习和本地大模型三层混合检测机制防御提示注入、越狱和敏感信息泄露,并满足 OWASP、EU AI Act 等多项合规要求。

Stars: 0 | Forks: 0

# 🛡️ AI 安全网关 ![CI](https://static.pigsec.cn/wp-content/uploads/repos/2026/04/81a907047d104607.svg) ![Python](https://img.shields.io/badge/python-3.10-blue) ![License](https://img.shields.io/badge/license-MIT-green) ![Docker](https://img.shields.io/badge/docker-compose-blue) ![OWASP](https://img.shields.io/badge/OWASP-LLM_Top_10-red) ![EU AI Act](https://img.shields.io/badge/EU_AI_Act-aligned-orange) 一个# 🛡️ AI 安全网关 ![CI](https://static.pigsec.cn/wp-content/uploads/repos/2026/04/81a907047d104607.svg) ![Python](https://img.shields.io/badge/python-3.10-blue) ![License](https://img.shields.io/badge/license-MIT-green) ![Docker](https://img.shields.io/badge/docker-compose-blue) ![OWASP](https://img.shields.io/badge/OWASP-LLM_Top_10-red) ![EU AI Act](https://img.shields.io/badge/EU_AI_Act-aligned-orange) 一个用于 LLM 应用的中间件安全网关,可拦截请求和响应,在将流量转发到受保护的 AI 系统之前,应用三层混合检测流水线——基于规则、基于 ML 和基于 LLM。专为受监管行业中企业 AI 部署相关的安全模式和合规框架而设计。 符合 OWASP LLM Top 10、EU AI Act、NIST AI RMF、MITRE ATLAS 和 BSI IT-Grundschutz。 本项目演示了 LLM 安全网关的架构和安全模式。它旨在作为参考实现和学习资源,而不是一个可直接用于生产部署的系统。 ## 🎯 防护目标 | 威胁 | OWASP LLM | 检测层 | |---|---|---| | 提示注入 | LLM01 | Rules + ML + LLM | | 敏感数据泄露 | LLM02 | spaCy NER + Regex | | 越狱尝试 | LLM01 | Rules + LLM | | 有毒/不安全输出 | LLM09 | Output guardrails | | 响应中的 PII | LLM06 | Output guardrails | ## 🚀 核心特性 - 🔍 3层混合检测 — 基于规则的模式匹配 + ML 分类器 + LLM 语义推理 - 💉 提示注入检测 — 使用 regex 规则和 TF-IDF + LogisticRegression 分类器 - 🕵️ PII 检测 — 结合 spaCy NER 与 regex 识别 IBAN、信用卡和德国税务 ID - 🔓 越狱检测 — 50+ 种已知模式结合 Ollama 语义意图分类 - 🛡️ 输出防护 — LLM 响应上的 PII 泄露检测和毒性过滤 - 📋 符合 GDPR 的审计日志 — 使用 SHA-256 哈希处理输入,在存储前对 PII 进行脱敏 - ⚖️ 加权风险评分器 — 可配置的 Allow / Sanitize / Block 阈值 - 📊 Streamlit SOC 仪表板 — 被拦截的请求、风险评分和审计日志查看器 - ⚡ FastAPI 网关端点 - 🐳 Docker Compose 部署 - 🔄 GitHub Actions CI/CD — 24 项测试通过 ## 🧠 系统架构 ``` Incoming request ↓ src/detection/rule_detector.py — regex + pattern rules (zero latency) src/detection/ml_detector.py — TF-IDF + LogisticRegression + spaCy NER src/detection/llm_detector.py — Ollama semantic intent classification src/detection/pii_detector.py — NER + regex PII detection ↓ src/scoring/risk_scorer.py — weighted risk aggregation src/policy/engine.py — Allow / Sanitize / Block decision ↓ Protected AI system (RAG / Agent / any LLM API) ↓ src/guardrails/output_guard.py — output PII + toxicity filtering ↓ src/audit/logger.py — GDPR-aligned audit logging (SQLite) ↓ api/main.py — FastAPI gateway ↓ dashboard/app.py — Streamlit SOC dashboard ``` ## ⚙️ 工作原理 每个传入的请求都会按顺序经过三个检测层。基于规则的层针对已知的注入字符串、越狱前缀和 PII 格式应用 regex 模式——这无需任何模型推理即可处理明显的威胁。ML 层使用在注入样本上训练的 TF-IDF + LogisticRegression 分类器和用于实体检测的 spaCy NER。LLM 层使用 Ollama 对需要上下文推理的情况进行语义分类,以识别对抗性意图。 加权风险评分器使用可配置的权重将所有检测分数聚合为单个风险值。策略引擎根据可配置的阈值将此值映射为 Allow、Sanitize 或 Block。被允许的请求将转发到受保护的 AI 系统。然后,在响应返回给用户之前,输出防护会对其进行 PII 泄露和有害内容检查。每个请求和决策都会被写入符合 GDPR 的审计日志中,且原始输入永远不会被持久化存储。 ## 📊 风险评分 ``` risk_score = ( 0.35 * injection_score + 0.25 * jailbreak_score + 0.25 * pii_score + 0.15 * toxicity_score ) # 策略阈值(可通过 .env 配置) if risk_score >= 0.7: → BLOCK if risk_score >= 0.3: → SANITIZE if risk_score < 0.3: → ALLOW ``` ## 🏛️ 框架对齐情况 | 框架 | 具体实现 | |---|---| | OWASP LLM Top 10 | LLM01 注入、LLM02 数据泄露、LLM06 敏感信息、LLM09 过度依赖 | | EU AI Act | 第13条 透明度日志、第9条 风险管理、第17条 质量体系 | | NIST AI RMF | GOVERN、MAP、MEASURE、MANAGE 功能 | | MITRE ATLAS | AML.T0051 提示注入、AML.T0048 社会危害 | | BSI IT-Grundschutz | OPS.1.1.4 日志记录、CON.8 数据安全 | ## 📊 仪表板概览 Streamlit SOC 仪表板提供: - 🚨 实时安全检查,包含决策、风险评分和检测明细 - 📊 风险评分仪表 — 根据阈值区域进行颜色编码 - 🔍 PII 实体面板,显示检测到的实体和类型 - 📋 符合 GDPR 的审计日志,带有 SHA-256 哈希输入和颜色编码的决策 - 📈 运行统计 — 总请求数、拦截数、过滤数、允许数、平均风险评分 ## 🛠 技术栈 | 层级 | 工具 | |---|---| | 规则检测 | Python regex,自定义模式库 | | ML 检测 | scikit-learn (TF-IDF + LogisticRegression) | | NLP / PII | spaCy | | LLM 检测 | LangChain + Ollama (llama3.2,本地,无需 API 密钥) | | 风险评分 | 自定义加权评分器 | | 审计 | SQLite,符合 GDPR | | 后端 | FastAPI, Uvicorn | | 仪表板 | Streamlit, Plotly | | 实验跟踪 | MLflow | | 容器化 | Docker Compose | | CI/CD | GitHub Actions | | 测试 | pytest (24 项测试) | ## 📂 项目结构 ``` ai-security-gateway/ │ ├── src/ │ ├── detection/ │ │ ├── rule_detector.py # Regex + pattern rules │ │ ├── ml_detector.py # TF-IDF + LogisticRegression │ │ ├── llm_detector.py # Ollama semantic classification │ │ └── pii_detector.py # spaCy NER + regex PII │ ├── scoring/ │ │ └── risk_scorer.py # Weighted risk aggregation │ ├── policy/ │ │ └── engine.py # Allow / Sanitize / Block │ ├── guardrails/ │ │ └── output_guard.py # Output PII + toxicity filter │ └── audit/ │ └── logger.py # GDPR audit trail │ ├── api/ │ └── main.py # FastAPI gateway │ ├── dashboard/ │ └── app.py # Streamlit SOC dashboard │ ├── tests/ │ ├── test_detectors.py # Detection module tests (16 tests) │ └── test_imports.py # Import tests (8 tests) │ ├── .github/ │ └── workflows/ │ └── ci.yml # GitHub Actions pipeline │ ├── mlruns/ # MLflow tracking (not committed) ├── data/audit/ # SQLite audit database (not committed) ├── docker-compose.yml ├── Dockerfile.api ├── Dockerfile.dashboard ├── .env.example ├── requirements.api.txt ├── requirements.dashboard.txt ├── requirements.dev.txt ├── AI_SECURITY_COMPLIANCE.md └── README.md ``` ## ▶️ 开始使用 ### 1. 克隆仓库 ``` git clone https://github.com/kbajish/ai-security-gateway.git cd ai-security-gateway ``` ### 2. 创建并激活虚拟环境 ``` python -m venv .venv .venv\Scripts\activate # Windows source .venv/bin/activate # Linux/Mac ``` ### 3. 安装依赖 ``` pip install -r requirements.dev.txt python -m spacy download en_core_web_sm ``` ### 4. 复制环境配置 ``` cp .env.example .env ``` ### 5. 启动所有服务 ``` docker compose up --build ``` ### 6. 访问服务 | 服务 | URL | |-----------|----------------------------| | API | http://localhost:8000 | | API 文档 | http://localhost:8000/docs | | 仪表板 | http://localhost:8501 | | MLflow | http://localhost:5000 | ## 🧪 API 端点 | 方法 | 端点 | 描述 | |---|---|---| | `POST` | `/gateway/check` | 完整的安全检查 — 返回风险评分、决策和检测明细 | | `POST` | `/gateway/scan` | 不含 LLM 层的快速扫描 — 仅限 rule + ML | | `POST` | `/gateway/output` | 在返回给用户之前检查 LLM 输出 | | `GET` | `/audit` | 获取符合 GDPR 的审计日志 | | `GET` | `/audit/stats` | 聚合统计 — 总数、平均值、决策分布 | | `GET` | `/health` | 健康检查 | ## 🧪 测试 ``` pytest tests/ -v # 24 通过 ``` 测试涵盖规则检测、ML 分类器、输出防护、风险评分器和策略引擎——所有测试均无需运行 Ollama 实例。 ## 📈 可能的扩展 - 使用 IBM ART (Adversarial Robustness Toolbox) 进行对抗性鲁棒性测试 - 实时威胁情报源集成 - 网关端点上的基于角色的访问控制 (RBAC) - Kafka 流处理以应对高吞吐量请求 - 采用托管审计存储和保留策略的云部署 ## 👤 作者 经验丰富的 IT 专业人士,拥有开发、网络安全和 ERP 系统背景,并擅长工业 AI。专注于构建设计优良、具备可解释性、集成 LLM、安全可靠并践行 MLOps 的 AI 系统。
标签:AI安全网关, AI风控, AI风险缓解, Apex, API安全, BSI IT-Grundschutz, CISA项目, DLP, Docker, docker-compose, EU AI Act, GDPR, Jailbreak防御, JSON输出, Kubernetes, Linux系统监控, LLM应用防火墙, LLM评估, MITRE ATLAS, Naabu, NER, NIST AI RMF, NLP, Ollama, OWASP LLM Top 10, PII脱敏, Python, spaCy, TF-IDF, 个人隐私信息检测, 中间件, 企业级AI安全, 内容安全, 反向代理, 合规框架, 命名实体识别, 大语言模型安全, 安全网关, 安全规则引擎, 安全防御评估, 意图识别, 提示词注入防护, 无后门, 有害输出过滤, 机器学习, 机密管理, 模型安全, 深度学习, 网络安全, 请求拦截, 越狱检测, 逆向工具, 逻辑回归, 隐私保护, 零日漏洞检测