VaradDurge/PromptFuzz

GitHub: VaradDurge/PromptFuzz

针对 LLM 应用的对抗性安全模糊测试工具,帮助开发者在上线前快速发现提示注入、越狱和数据提取等漏洞。

Stars: 1 | Forks: 0

# PromptFuzz **针对 LLM 应用的对抗性安全测试。** 在攻击者之前发现提示注入、越狱和数据提取漏洞。 [![PyPI version](https://img.shields.io/pypi/v/promptfuzz.svg)](https://pypi.org/project/promptfuzz/) [![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/) [![License: AGPL-3.0](https://img.shields.io/badge/License-AGPL%203.0-blue.svg)](https://www.gnu.org/licenses/agpl-3.0) [![Tests](https://github.com/varadk27/promptfuzz/actions/workflows/tests.yml/badge.svg)](https://github.com/varadk27/promptfuzz/actions) 你发布了一个 LLM 产品。你添加了系统提示。你认为它是安全的。 其实不然。**PromptFuzz 会在你的用户发现之前找出漏洞。** PromptFuzz 向你的应用发起 **165+ 个单轮对抗性提示** 以及 **12 个多轮攻击链** — 包括越狱、提示注入、数据提取、目标劫持、边缘情况以及 对话级别的升级序列 — 并在几秒钟内生成专业的 漏洞报告。 交互式向导:

## 安装 ``` pip install promptfuzz ``` 可选附加组件: ``` pip install "promptfuzz[openai]" # if your target uses OpenAI pip install "promptfuzz[anthropic]" # if your target uses Anthropic ``` 终端输出示例:

## 快速开始 ### 1. 交互式向导(推荐首次使用) 直接运行 `promptfuzz`,无需参数: ``` $ promptfuzz PromptFuzz v0.1.0 — LLM Security Testing ? What is your target? > HTTP/HTTPS endpoint (URL) Python function (module:function) ? Enter target URL: http://localhost:8000/chat ? Select attack categories: ◉ data_extraction — System prompt leaking, credential extraction ◉ injection — Prompt override, delimiter attacks ◉ jailbreak — Persona switches, DAN, roleplay bypasses ○ edge_cases — Unicode, long inputs, encoding attacks ○ goal_hijacking — Purpose redirection attacks ? Output format: > Terminal + HTML report (report.html) ? Minimum severity to report: > low (show everything) ───────────────────────────────────────── Target : http://localhost:8000/chat Attacks : 110 (data_extraction + injection + jailbreak) Output : Terminal + report.html Severity : low+ ───────────────────────────────────────── Press ENTER to start scan (Ctrl+C to cancel) ``` ### 2. `promptfuzz test` — 从终端运行的最快方式 ``` # 直接测试任意 URL — 无需 flags promptfuzz test https://api.mychatbot.com/chat # 保存 HTML 报告 promptfuzz test https://api.mychatbot.com/chat --output report.html # 测试本地 Python 函数 promptfuzz test myapp:chat_handler --categories jailbreak injection # CI 模式 — 如果发现任何 high/critical 漏洞则 exit code 1 promptfuzz test https://api.mychatbot.com/chat --fail-on high ``` ### 3. `promptfuzz scan` — 支持配置文件的完整功能 CLI ``` promptfuzz scan --target http://localhost:8000/chat --output report.html promptfuzz scan --config promptfuzz.yaml --fail-on high ``` ### 3. Python API ``` from promptfuzz import Fuzzer def my_chatbot(message: str) -> str: # your LLM call here return response fuzzer = Fuzzer( target=my_chatbot, context="customer support chatbot", categories=["jailbreak", "injection", "data_extraction"], ) result = fuzzer.run() result.report() # rich terminal output result.save("report.html") # HTML report ``` ## 测试 FastAPI 端点

PromptFuzz 适用于任何接受 POST 请求的 HTTP 端点。无需更改代码。 ``` # your_app.py from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class ChatRequest(BaseModel): message: str @app.post("/chat") async def chat(req: ChatRequest): reply = your_llm_call(req.message) return {"response": reply} ``` ``` # 启动您的应用 uvicorn your_app:app # 测试它 promptfuzz scan --target http://localhost:8000/chat ``` 运行器自动检测 `http://` 目标,发送 `{"message": "..."}` 作为请求体, 并从回复中读取 `"response"` 字段。这两个字段名称均可配置。 ## 攻击类别

### 单轮攻击(165 个提示) | 类别 | 数量 | 测试内容 | |---|---|---| | `jailbreak` | 40 | DAN 变体、角色切换、角色扮演绕过、编码技巧 | | `injection` | 40 | 经典覆盖、分隔符攻击、权限提升、指令走私 | | `data_extraction` | 30 | 系统提示泄露、凭据提取、反射攻击 | | `goal_hijacking` | 25 | 竞品推广、目的替换、忠诚度切换 | | `edge_cases` | 30 | Unicode 滥用、长输入、编码边缘情况、空字节 | | **总计** | **165** | | ### 多轮攻击链(12 条链) 多轮链模拟现实的对抗性对话,攻击者在其中进行探测、 建立关系并在多次交互中逐步升级 — 这是单次提示 无法达到的绕过效果。每条链动态分支:下一轮的选择取决于 模型是服从还是拒绝了上一轮。 | 链文件 | 链数 | 严重性 | 测试内容 | |---|---|---|---| | `escalation_chains` | 4 | critical / high | 建立关系 → 角色覆盖、权威升级、角色扮演升级、假设性框架 | | `extraction_chains` | 4 | critical / high | 能力探测 → 系统提示泄露、增量指令分割、虚假记忆注入、回显模式升级 | | `injection_chains` | 4 | critical / high / medium | 逐渐指令侵蚀、通过对话历史进行上下文投毒、分隔符探测 → 注入、通过话题漂移导致目标偏离 | | **总计** | **12** | | **每条链 3–4 轮** | ``` promptfuzz list-attacks # view full table with severity breakdown ``` ## CLI 参考 ``` promptfuzz scan [OPTIONS] --target, -t URL or module:function path --config, -c YAML config file (mutually exclusive with --target) --context Description of the target application --categories, -C Attack categories to run (repeatable) --output, -o Save HTML report to path --json Save JSON report to path --severity, -s Minimum severity to display [low|medium|high|critical] --fail-on, -f Exit code 1 if vulns at/above this severity are found --max-workers, -w Concurrent request workers (default: 5) --timeout, -T Per-attack timeout seconds (default: 30) --verbose, -v Enable verbose output ``` ## 配置文件 ``` # promptfuzz.yaml target: "http://localhost:8000/chat" context: "customer support chatbot" categories: - jailbreak - injection - data_extraction max_workers: 5 timeout: 30 headers: Authorization: "Bearer YOUR_TOKEN" input_field: message output_field: response ``` ``` promptfuzz scan --config promptfuzz.yaml --output report.html promptfuzz validate --config promptfuzz.yaml # validate before running ``` ## CI/CD 集成 ``` # .github/workflows/security.yml name: LLM Security on: [push, pull_request] jobs: promptfuzz: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - run: pip install promptfuzz - name: Start app run: uvicorn myapp:app & - name: Run security scan run: | promptfuzz scan \ --target http://localhost:8000/chat \ --categories jailbreak injection \ --fail-on high \ --output report.html - uses: actions/upload-artifact@v4 if: always() with: name: security-report path: report.html ``` `--fail-on high` 会在发现任何高危或严重漏洞时以代码 1 退出, 从而阻止合并。 ## 安全评分 | 分数 | 风险等级 | |---|---| | 80–100 | 低风险 | | 50–79 | 中风险 | | 20–49 | 高风险 | | 0–19 | 严重风险 | 公式:`max(0, 100 − (critical×25 + high×10 + medium×5 + low×2))` ## 许可证 AGPL-3.0 © PromptFuzz Contributors 个人项目、安全研究和开源软件可免费使用。 在闭源产品中进行商业使用需要商业许可证 — 请提交 issue 进行讨论。
标签:AI安全, Chat Copilot, Fuzzing, Jailbreak, LNA, Python, 大语言模型安全, 安全测试, 对抗攻击, 提示注入, 攻击性安全, 攻击链, 敏感信息检测, 数据提取, 文档结构分析, 无后门, 机密管理, 系统提示泄露, 逆向工具, 集群管理