himanshugujjar01/LLM-Security-Gateway

GitHub: himanshugujjar01/LLM-Security-Gateway

基于 FastAPI 的 LLM 应用安全网关,通过检测 Prompt Injection 攻击、脱敏 PII 数据和限流鉴权来保护大型语言模型应用免受常见安全威胁。

Stars: 0 | Forks: 0

# LLM Security Gateway ## 概述 LLM Security Gateway 是一个基于 FastAPI 的安全层,旨在保护大型语言模型(LLM)应用程序免受常见安全威胁,例如 Prompt Injection 攻击、个人身份信息(PII)泄露以及未经授权的 API 访问。 该网关会检查传入的请求,检测恶意 prompt,脱敏敏感数据,记录安全事件,并提供一个用于监控安全事件的仪表板。 ## 功能 ### Prompt Injection 检测 * 检测常见的 Prompt Injection 模式。 * 在恶意请求到达 LLM 之前将其拦截。 * 记录所有检测到的注入尝试。 ### PII 检测与脱敏 * 检测并脱敏: * 电子邮件地址 * 电话号码 * Aadhaar 号码 * 信用卡号码 * 防止敏感数据泄露。 ### API Key 身份验证 * 使用 API key 验证保护 endpoint。 * 拦截未经授权的访问。 ### 安全日志 * 将安全事件记录在 `security.log` 中。 * 追踪: * Prompt Injection 尝试 * PII 检测 * 请求处理事件 ### 安全仪表板 * 提供实时的安全统计数据。 * 展示: * 安全事件总数 * Prompt Injection 尝试 ### 限流 * 保护 API 免遭滥用和过量请求。 ## 项目结构 ``` app/ ├── auth/ │ └── api_key.py ├── dashboard/ │ └── dashboard.py ├── logs/ │ └── security.log ├── middleware/ │ └── rate_limiter.py ├── security/ │ ├── pii_detector.py │ ├── prompt_injection.py │ └── output_filter.py ├── services/ │ └── logger.py ├── config.py └── main.py ``` ## 安装 ### 克隆仓库 ``` git clone cd llm-security-gateway ``` ### 安装依赖 ``` pip install -r requirements.txt ``` ### 运行应用 ``` uvicorn app.main:app --reload ``` 应用程序将启动于: ``` http://127.0.0.1:8000 ``` Swagger 文档: ``` http://127.0.0.1:8000/docs ``` ## API Endpoint ### Chat Endpoint ``` POST /chat ``` Headers: ``` x-api-key: my-secret-key ``` 请求: ``` { "message": "My email is user@example.com" } ``` 响应: ``` { "original": "My email is user@example.com", "redacted": "My email is [EMAIL]" } ``` ### 安全统计 ``` GET /stats ``` 响应: ``` { "total_events": 10, "prompt_injections": 2 } ``` 自动化事件遏制 ### 功能 - 检测 Prompt Injection 攻击 - 自动触发遏制操作 - 模拟主机隔离 - 记录安全事件 ### 示例响应 { "status": "blocked", "reason": "Prompt Injection Attempt Detected", "containment": { "status": "isolated", "hostname": "PC-001", "action": "network containment executed" } } ## 威胁情报检测 网关会根据包含已知恶意关键词的威胁情报源检查传入的 prompt。 ### 当前指标 - malware - ransomware - keylogger - steal credentials - bypass security ## 安全工作流 1. 用户发送请求。 2. 验证 API Key。 3. 运行 Prompt Injection 检测。 4. 运行 PII 检测与脱敏。 5. 运行输出过滤。 6. 记录安全事件。 7. 返回已净化的响应。 ## 使用的技术 * Python * FastAPI * Uvicorn * 正则表达式(Regex) * Logging 模块 ## 未来增强功能 * JWT 身份验证 * 基于 Redis 的限流 * 威胁情报集成 * Grafana 仪表板 * 基于 Machine Learning 的 Prompt Injection 检测 * SIEM 集成 ## 作者 Himanshu Cybersecurity 实习项目 LLM Security Gateway
标签:API安全, AV绕过, DLL 劫持, FastAPI, JSON输出, LLM安全网关, 大语言模型, 安全防护, 数据脱敏, 逆向工具