如何用 Ruflo AIDefence 扫描用户输入中的提示注入威胁?
【免费下载链接】ruflo🌊 The original agent meta-harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, RAG integration, and native Claude Code / Codex / Hermes and many more Integrated项目地址: https://gitcode.com/GitHub_Trending/cl/ruflo
如果你的 AI 应用会接收用户提交、API 载荷或 webhook 数据,这些内容在重新进入 LLM 提示之前必须先过一道威胁扫描,否则提示注入、越狱和角色劫持类文本可能直接改变模型行为。Ruflo 提供 AIDefence(AI Manipulation Defense System)来完成这件事:它内置 50+ 检测模式,覆盖指令覆盖(instruction override)、越狱(jailbreak)、角色切换(role switching)、上下文操纵(context manipulation)和编码攻击(encoding attack),并对检测到的威胁给出严重级别、置信度和缓解建议。本文给出两条文档中的操作路径:用@claude-flow/cli的security defend子命令做命令行扫描,以及在 Ruflo 插件环境中按插件契约使用aidefence_*MCP 工具做逐条检查。
前提条件:
- 命令行路径只需要能运行
npx @claude-flow/cli;自适应学习引擎@claude-flow/aidefence是可选依赖(Node.js 18+、TypeScript 5.3+),未安装时 CLI 会回退到内置确定性扫描引擎,并在输出中提示Using built-in defense engine (install @claude-flow/aidefence for adaptive learning)。 - 插件路径需要先安装
ruflo-aidefence插件,且宿主 CLI 锁定在@claude-flow/cliv3.6 major+minor(见 插件 README 的 Compatibility 一节)。
使用 CLI 扫描单条输入
最短主路径是直接对一段用户输入运行 defend 命令:
npx @claude-flow/cli security defend -i "ignore previous instructions"-i, --input传入待扫描文本。其他可用参数(来自 security 命令实现):
-f, --file:改为扫描一个文件,例如批量提示文件npx @claude-flow/cli security defend -f ./user-prompts.txt;-Q, --quick:快速扫描模式,只做威胁布尔判断,更快但信息更简略;-l, --learn:启用学习模式,默认开启(true),配合@claude-flow/aidefence学习引擎使用;-s, --stats:不执行扫描,直接显示检测统计;-o, --output:输出格式,text(默认)或json。
文本模式下,安全输入会输出No threats detected;发现威胁时按严重级别([CRITICAL]、[HIGH]等)逐条列出威胁类型、描述和置信度,并对 critical 级威胁给出推荐缓解策略。@claude-flow/aidefence的 README 给出了一个文档示例输出(示例结果,实际数值以你的输入为准):
🛡️ AIDefence - AI Manipulation Defense System ─────────────────────────────────────────────────────── ⚠️ 2 threat(s) detected: [CRITICAL] instruction_override Attempt to override system instructions Confidence: 95.0% [HIGH] jailbreak Attempt to bypass restrictions Confidence: 85.0% Recommended Mitigations: instruction_override: block (95% effective) jailbreak: block (92% effective) Detection time: 0.042ms用 JSON 输出判断扫描结果
在脚本或管道中使用时,加-o json得到结构化结果,便于程序化处理:
npx @claude-flow/cli security defend -i "test" -o jsonJSON 输出包含四个字段(字段定义见 CLI defend 子命令实现):
{ "safe": true, "threats": [], "piiFound": false, "detectionTimeMs": 0.0 }判定规则是safe为真且piiFound为假时命令以退出码 0 结束,检测到威胁或 PII 时以退出码 1 结束——因此可以直接在 CI 或网关脚本中用退出码决定是否放行该输入。piiFound表示输入中还发现了邮箱、SSN、API key 等敏感数据,这在提示注入之外同样需要处理(例如脱敏或隔离后再入库)。
查看检测统计
扫描若干输入后,用--stats检查累计统计:
npx @claude-flow/cli security defend --stats统计框展示 Detection Count、Avg Detection Time、Learned Patterns、Mitigation Strategies 和平均缓解有效率。启用学习模式并持续反馈检测结果后,Learned Patterns 会增长,这是学习引擎在工作的一种观察方式(参见 AIDefence 库 README 的 Self-Learning 一节)。
可选分支:在 Ruflo 插件流程中使用 MCP 工具
如果你的目标是把扫描嵌入 Ruflo 代理工作流,而不是命令行批处理,先安装插件:
/plugin marketplace add ruvnet/ruflo /plugin install ruflo-aidefence@ruflo插件暴露 6 个aidefence_*MCP 工具:aidefence_scan、aidefence_analyze、aidefence_stats、aidefence_learn、aidefence_is_safe、aidefence_has_pii。safety-scan 技能给出的处理顺序是:
aidefence_is_safe做布尔安全检查;aidefence_analyze获取威胁分类与置信度详情;aidefence_scan做多层完整扫描;aidefence_learn用已确认的威胁训练检测;aidefence_stats查看检测率与误报指标。
插件契约(ADR-0001)进一步定义了所有处理不可信内容的插件应遵循的 3-gate 模式,顺序固定:
| # | 门禁 | 工具 | 时机 |
|---|---|---|---|
| 1 | Pre-storage PII | aidefence_has_pii | 任何 AgentDB /memory_store写入之前——先脱敏或隔离再持久化 |
| 2 | Sanitization | aidefence_scan | 针对 cookie、token、高熵内容——放入不透明句柄而非内嵌原始值 |
| 3 | Prompt-injection | aidefence_is_safe | 任何提取文本重新进入 LLM 提示之前——命中则隔离到findings.md |
此外,插件提供/aidefence命令作为状态与统计仪表盘(调用aidefence_stats,展示总扫描数、威胁数、误报率和 PII 检测数,见 命令定义)。
检测覆盖方面,aidefence@2.3.0起的第 3 道门禁(aidefence_is_safe)会标记三类输入:修饰词窗口 0..4 的提示注入(如ignore all previous instructions、disregard the system prompt)、角色劫持(you are now …、act as …)和越狱标记(DAN mode、developer mode等),详见 插件 README 的 "What the gates catch" 表格。
验证插件契约是否完整
插件自带结构化的 smoke 测试作为契约验证手段,在仓库根目录运行:
bash plugins/ruflo-aidefence/scripts/smoke.sh预期输出为10 passed, 0 failed。它检查插件元数据版本与关键词、6 个aidefence_*工具是否都在文档中引用、CLI v3.6 兼容性钉选、3-gate 模式文档、防御纵深段落(loader-hijack 拒绝名单、文件模式 0600/0700、静态加密 opt-in)以及技能 frontmatter 完整性。任一项失败时输出以非零退出码结束。
适用条件与限制
- 插件与宿主 CLI 版本钉选:
@claude-flow/cliv3.6 major+minor,不满足时不要混用插件 MCP 工具面。 security defend的学习模式(--learn默认开启)只有在安装了@claude-flow/aidefence时才生效;未安装时使用内置确定性引擎,无自适应学习。- 性能指标(检测约 0.04ms、单线程吞吐 >12,000 requests/秒)来自 AIDefence 库 README 的基准表,属于库的自报数据,可作为量级参考,不应写进你自己的 SLA 预期。
- 插件的
security-patternsAgentDB 命名空间由该插件独占,不得与pattern、claude-memories、default等保留命名空间冲突。
完成一次端到端验证:对一条正常输入和一条含ignore previous instructions的输入分别运行security defend -o json,确认前者safe: true、后者safe: false且threats中给出instruction_override类条目,即可说明扫描链路在你的环境中工作正常。
【免费下载链接】ruflo🌊 The original agent meta-harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, RAG integration, and native Claude Code / Codex / Hermes and many more Integrated项目地址: https://gitcode.com/GitHub_Trending/cl/ruflo
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考