saikrishnarallabandi/jataayu

GitHub: saikrishnarallabandi/jataayu

基于 LLM 的 AI 智能体安全防护库,提供入站注入检测与出站隐私脱敏的双向安全能力。

Stars: 0 | Forks: 0

# 🦅 Jataayu *在《罗摩衍那》中,Jataayu 是一只发现 Ravana 绑架 Sita 的神鹰。它没有等待模式匹配。它看到了威胁,判断了局势,并独自行动——毫不犹豫。这就是 Jataayu。* **LLM 支持的 AI 智能体安全 —— 入站注入检测 + 出站隐私保护。** ## 问题所在 AI 智能体正在遭受攻击。这不是科幻小说——而是当下,在生产环境中。 - **Clinejection** —— 嵌入在 GitHub issues 中的恶意 prompt 注入,可劫持 Cline、Cursor 和 Claude Code 等编码智能体。攻击者提交一个 issue;你的智能体读取它;你的智能体执行攻击者的指令。 - **Web 投毒** —— 暗藏不可见指令的网站,可重定向浏览智能体。 - **邮件钓鱼** —— 精心构造的信息,诱导阅读邮件的智能体泄露数据。 大多数防御措施只关注 **IN(入站)**。但有一个没人谈论的第二个威胁: **OUT(出站)**。 你的智能体可以访问私密上下文——文件、消息、家庭详细信息、财务数据。当它在群聊中回复、评论 GitHub issue 或发送电子邮件时,可能会无意中将该上下文泄露给错误的受众。无需 prompt 注入。 Jataayu 守护这两个方向。 ## 安装 ``` pip install jataayu # 支持 LLM(用于慢路径分析) pip install "jataayu[llm]" # 支持 Ollama pip install "jataayu[ollama]" ``` ## 快速开始 ### Simple API(推荐) ``` from jataayu import jataayu_check_inbound, jataayu_check_outbound # --- 入站:检测外部内容中的注入攻击 --- result = jataayu_check_inbound(github_issue_body, surface="github-issue") if result["status"] == "HIGH": raise SecurityError(f"Blocked: {result['findings']}") elif result["status"] == "MEDIUM": log.warning(f"Suspicious: {result['findings']}") # 返回:{status: 'SAFE'|'LOW'|'MEDIUM'|'HIGH', findings: str, risk_score: float, ...} # --- 出站:在发送到共享界面之前去除 PII/机密信息 --- result = jataayu_check_outbound( draft_reply, surface="discord-channel", protected_names=["Alice", "Bob"], # names that must never leak ) if result["status"] == "BLOCK": safe_text = result["redacted"] # auto-sanitized version elif result["status"] == "WARN": safe_text = result["redacted"] # review before sending else: safe_text = draft_reply # SAFE — send as-is # 返回:{status: 'SAFE'|'WARN'|'BLOCK', findings: str, redacted: str|None, ...} ``` **支持的面(surfaces):** `github-issue`, `github-pr`, `github-comment`, `web-page`, `web-content`, `email`, `whatsapp`, `discord-channel`, `discord-group`, `telegram-group`, `group-chat`, `direct-message`, `coding-task`, `internal`, `public`, `unknown` ### 高级 API ``` from jataayu import InboundGuard, OutboundGuard, PrivacyConfig # --- 入站:在 Agent 处理之前捕获注入 --- guard = InboundGuard() result = guard.check(github_issue_body, surface="github-issue") if result.blocked: raise SecurityError(f"Blocked: {result.explanation}") elif not result.is_safe: log.warning(f"Suspicious: {result.explanation}") # --- 出站:在 Agent 发送之前捕获 PII --- config = PrivacyConfig( protected_names=["Alice", "Bob"], # names to always redact check_categories=["minors_info", "health", "financial"], ) outbound = OutboundGuard(config) safe_reply = outbound.sanitize(draft_reply, surface="group-chat") ``` ### Simple API(便捷函数) ``` from jataayu import check_inbound, check_outbound # 入站检查 — 在根据外部内容采取行动之前 status, findings = check_inbound( content="", surface="github-issue" ) if status == "HIGH": print(f"BLOCKED: {findings}") elif status == "MEDIUM": print(f"WARNING: {findings}") # 出站检查 — 在发送到共享界面之前 status, redacted = check_outbound( content="My daughter Veda loves coding", surface="discord-channel" ) if status == "BLOCK": print(f"Cannot send. Redacted: {redacted}") elif status == "WARN": print(f"Review before sending: {redacted}") ``` 返回: - **Inbound(入站)**: `(status, findings)` 其中 status 为 `LOW` | `MEDIUM` | `HIGH` - **Outbound(出站)**: `(status, output)` 其中 status 为 `SAFE` | `WARN` | `BLOCK` ### CLI ``` # 检查字符串中的注入威胁 jataayu check "Ignore all previous instructions." --surface github-issue # 从 stdin 管道读取 echo "$(cat issue_body.txt)" | jataayu check --surface github-issue # 检查出站内容的隐私违规 jataayu check --outbound "My daughter is 4 years old." --surface group-chat # 清理出站文本 jataayu sanitize "Call me at 555-867-5309" --surface discord-channel # 运行内置演示 jataayu demo jataayu demo --outbound ``` ## Surface Profiles(面配置) Jataayu 理解上下文的重要性。在 GitHub issue 中的 shell 命令是可疑的;但在编码任务中,它是预期内的。 | Surface | 信任度 | Inbound 严格 | Outbound 严格 | 备注 | |---|---|---|---|---| | `github-issue` | 🔴 低 | ✅ 是 | ❌ 否 | Clinejection 攻击面 | | `github-pr` | 🔴 低 | ✅ 是 | ❌ 否 | 代码和描述攻击 | | `web-content` / `web-page` | 🔴 低 | ✅ 是 | ❌ 否 | 不可见 prompt 注入 | | `email` | 🟡 中 | ✅ 是 | ✅ 是 | 钓鱼 + 数据泄露 | | `whatsapp` | 🟡 中 | ❌ 否 | ✅ 是 | 群组隐私至关重要 | | `telegram-group` | 🟡 中 | ❌ 否 | ✅ 是 | 群组隐私至关重要 | | `discord-channel` | 🟡 中 | ❌ 否 | ✅ 是 | 公共社区 | | `discord-group` | 🟡 中 | ❌ 否 | ✅ 是 | 半公开群组 DM | | `group-chat` | 🟡 中 | ❌ 否 | ✅ 是 | 通用群组面 | | `unknown` | 🟡 中 | ✅ 是 | ✅ 是 | 默认 —— 检查所有内容 | | `direct-message` | 🟢 高 | ❌ 否 | ❌ 否 | 私密,受信任 | | `coding-task` | 🟡 中 | ❌ 否 | ❌ 否 | 预期会有 Shell 命令 | | `internal` | 🟢 高 | ❌ 否 | ❌ 否 | Agent-to-agent 受信任 | ## 工作原理 ### 双路径架构 ``` Inbound text │ ▼ ┌─────────────┐ score ≥ 0.9 ┌──────────┐ │ Fast Path │ ─────────────── │ BLOCKED │ │ (patterns) │ └──────────┘ └─────────────┘ │ 0.35 ≤ score < 0.9 ▼ ┌─────────────┐ │ Slow Path │ LLM judgment (Ollama / OpenAI / Anthropic / OpenClaw) │ (LLM) │ └─────────────┘ │ ▼ ThreatResult ``` **Fast path(快速路径)**(微秒级):涵盖 20 多种正则表达式模式: - Prompt 注入(ignore-instructions, DAN, system-prompt 重定义) - 命令注入(shell 管道、子 shell、破坏性命令、远程执行) - 社会工程学(虚假权威、紧迫感、胁迫) - Unicode 同形字替换 - 编码混淆(base64, URL 编码) **Slow path(慢速路径)**(LLM):当快速路径给出中等置信度分数时调用。LLM 为模棱两可的情况提供细致的判断,并为出站内容生成经过净化的重写。 ## LLM 配置 ``` # 使用 Ollama(默认,本地,免费) export JATAAYU_LLM_BACKEND=ollama export JATAAYU_LLM_MODEL=llama3 # 使用 OpenAI export JATAAYU_LLM_BACKEND=openai export JATAAYU_LLM_API_KEY=sk-... export JATAAYU_LLM_MODEL=gpt-4o-mini # 使用 Anthropic export JATAAYU_LLM_BACKEND=anthropic export JATAAYU_LLM_API_KEY=sk-ant-... export JATAAYU_LLM_MODEL=claude-haiku-20240307 # 使用 OpenClaw gateway(自动读取 ~/.openclaw/openclaw.json) export JATAAYU_LLM_BACKEND=openclaw ``` 纯模式模式(无 LLM,无需 API key): ``` guard = InboundGuard(use_llm=False) ``` ## 面向 AI 智能体 参阅 [AGENTS.md](AGENTS.md) 了解如何将 Jataayu 集成到你的智能体工作流中。 ## 许可证 MIT —— 详见 [LICENSE](LICENSE)。 *以 [Jataayu](https://en.wikipedia.org/wiki/Jatayu) 命名,《罗摩衍那》中那只为了保护无辜者而独自奋战的崇高雄鹰。*
标签:AI 安全, AI风险缓解, API 安全, Claude, Cline, Cursor, CVE检测, DLL 劫持, DLP, DNS 反向解析, LLM 安全, LLM评估, Ollama, Petitpotam, PII 脱敏, Python 库, 上下文安全, 代码安全, 企业合规, 大语言模型, 密钥泄露防护, 提示词注入检测, 敏感信息过滤, 数据泄露防护, 文档结构分析, 机器学习安全, 漏洞枚举, 网络安全, 网络安全, 网络探测, 网页投毒, 逆向工具, 邮件钓鱼防护, 隐私保护, 隐私保护