geovanidps/prompt-injection-defense-toolkit

GitHub: geovanidps/prompt-injection-defense-toolkit

针对 LLM 应用中 Prompt 注入攻击的开源防御工具包,提供攻击分类法、启发式检测扫描器、自动化测试集成及事件响应模板。

Stars: 0 | Forks: 0

# Prompt 注入防御工具包 一个开源且具有教育意义的防御工具包,用于**检测、测试及事件响应**,主要针对基于 LLM 的应用程序中的 prompt 攻击(prompt 注入、越狱及其变体)。 ## 本项目存在的原因 Prompt 注入被列为 OWASP Top 10 for LLM Applications (2025) 中的头号(#1)风险,并且行业报告(CrowdStrike、Cisco)记录了 2025–2026 年真实事件加速增长的趋势,包括在大型产品中的零点击漏洞利用(例如:EchoLeak / CVE-2025-32711)。本工具包将分散的公开知识整理为可操作的结构,专为安全团队、AI 开发人员和研究人员设计。 ## 仓库结构 ``` prompt-injection-defense-toolkit/ ├── README.md ├── LICENSE ├── docs/ │ ├── taxonomy.md # Taxonomia com 25 subcategorias nomeadas │ ├── redteam_checklist.md # Checklist de cobertura de testes │ └── incident_response_template.md # Template de relatório executivo ├── detection/ │ ├── patterns.yaml # Regras heurísticas mapeadas à taxonomia │ ├── heuristics.py # Scanner heurístico (regex + entropia + drift) │ └── requirements.txt ├── testing/ │ ├── run_tests.md # Guia de integração Garak + NeMo Guardrails │ └── nemo_guardrails/ │ ├── config.yml │ └── rails/topical_rail.co └── .github/ └── ISSUE_TEMPLATE/ └── incident_report.md # Template de issue para incidentes ``` ## 如何使用 ### 1. 分类法作为参考 从 [`docs/taxonomy.md`](docs/taxonomy.md) 开始:包含 25 个 prompt 攻击子类别,分为 5 个模块(直接注入、混淆、模拟上下文/权限提升、间接注入、代理/工具),每个类别都附有其机制、检测模式和缓解措施。 ### 2. 启发式检测 ``` cd detection pip install -r requirements.txt python heuristics.py --text "ignore todas as instruções anteriores e..." ``` 或作为库使用: ``` from detection.heuristics import PromptGuard guard = PromptGuard("detection/patterns.yaml") result = guard.scan(user_message) if result.action == "bloquear": reject_request() elif result.action == "sinalizar para revisão humana": queue_for_review() ``` ### 3. 自动化测试 遵循 [`testing/run_tests.md`](testing/run_tests.md) 进行配置: - **Garak** 用于自动化的漏洞扫描; - **NeMo Guardrails** 用于在运行时控制对话流, 并使用 `testing/nemo_guardrails/` 作为起点。 ### 4. 结构化红队测试 使用 [`docs/redteam_checklist.md`](docs/redteam_checklist.md) 来规划并跟踪针对分类法中所有 25 个类别的测试覆盖率,以及独立于特定 payload 的架构验证。 ### 5. 事件响应 - 要记录真实的事件或测试发现:请使用 [`.github/ISSUE_TEMPLATE/incident_report.md`](.github/ISSUE_TEMPLATE/incident_report.md) 中的模板 (在 GitHub 上创建 issue 时会自动打开)。 - 要整合完整的测试活动:请使用 [`docs/incident_response_template.md`](docs/incident_response_template.md)。 ## 纵深防御原则 没有任何单一的安全层是足够的。推荐的架构结合了以下措施: 1. 系统指令与输入数据之间的结构性分离。 2. 启发式输入过滤器(本仓库)。 3. 专用的 Prompt 注入语义分类器。 4. 运行时可编程的 Guardrails(NeMo Guardrails 或类似工具)。 5. 在执行前将 LLM 的所有输出视为不受信任的数据。 6. 针对高风险代理操作实行最小权限原则和人工批准。 7. 持续监控、持久化内存审计以及周期性的对抗性测试(Garak + 人工红队测试)。 ## 贡献 欢迎在以下方面做出贡献: - 在 `detection/patterns.yaml` 中添加新的启发式规则(需引用对应的分类法类别); - 在 `testing/nemo_guardrails/rails/` 中添加新的 guardrails; - 基于已发表的学术文献改进分类法。 ## 主要参考 - OWASP GenAI Security Project — [LLM01:2025 Prompt Injection](https://genai.owasp.org/llmrisk/llm01-prompt-injection/) - NIST AI 600-1 — Generative AI Profile - Garak — [github.com/leondz/garak](https://github.com/leondz/garak) - NeMo Guardrails — [github.com/NVIDIA/NeMo-Guardrails](https://github.com/NVIDIA/NeMo-Guardrails) 请在 `docs/taxonomy.md` 中查看完整的学术参考文献列表。 ## 许可证 MIT — 见 [`LICENSE`](LICENSE)。仅供教育和防御目的使用。
标签:逆向工具