AdityaBelhekar/AgentShield
GitHub: AdityaBelhekar/AgentShield
AI Agent 运行时安全防护层,以无侵入方式实时检测并拦截提示词注入、目标漂移、工具滥用等攻击。
Stars: 1 | Forks: 0
[](LICENSE)
[](https://python.org)
[](CHANGELOG.md)
[](CONTRIBUTING.md)
[](https://github.com/AdityaBelhekar/AgentShield/stargazers)
```
from agentshield import shield
protected = shield(your_agent, policy="no_exfiltration")
protected.run("Summarize this document") # ✓ Fully protected
```
## 存在的问题
AI agents 正在几乎没有任何安全层的情况下被部署到生产环境中。
它们可能遭受 **prompt 注入**。它们的目标可能被 **劫持**。它们的记忆可能被 **投毒**。它们可能被串联起来 **窃取数据**,而开发者却对此一无所知。
整个生态系统构建 agents 的速度远快于保护它们的速度。
**AgentShield 解决了这个问题。**
## 功能介绍
AgentShield 是一个 **可通过 pip 安装的 Python SDK**,它包装任何 AI agent 并实时监控其整个执行 pipeline —— **无需触碰您的 agent 代码** 即可检测并阻止攻击。
```
┌─────────────────────────────────────────────────────────────┐
│ YOUR AGENT CODE │
│ (LangChain / AutoGen / CrewAI) │
└──────────────────────────┬──────────────────────────────────┘
│ shield()
┌──────────────────────────▼──────────────────────────────────┐
│ INTERCEPTOR LAYER │
│ Tool hooks │ LLM hooks │ Memory hooks │
└──────────────────────────┬──────────────────────────────────┘
│ events
┌──────────────────────────▼──────────────────────────────────┐
│ DETECTION ENGINE │
│ Injection │ Drift │ Chain │ Poison │ DNA │ Provenance │
└──────────────────────────┬──────────────────────────────────┘
│ threats
┌──────────────────────────▼──────────────────────────────────┐
│ POLICY COMPILER + CRYPTOGRAPHIC AUDIT │
│ YAML rules │ Hash-chained tamper-proof logs │
└──────────────────────────┬──────────────────────────────────┘
│ Redis pub/sub
┌──────────────────────────▼──────────────────────────────────┐
│ REACT SECURITY CONSOLE │
│ Live graph │ Threat alerts │ Forensic trace │
└─────────────────────────────────────────────────────────────┘
```
## 防御的攻击向量
| 攻击 | 原理 | AgentShield 如何阻止 |
|--------|-------------|--------------------------|
| 🔴 **Prompt 注入** | 隐藏在工具输出或用户输入中的恶意指令 | 模式匹配 + 语义相似度 + canary tokens |
| 🔴 **目标漂移** | Agent 缓慢偏离原始任务 | 与原始任务的 embedding 余弦距离 |
| 🔴 **工具链升级** | read_file → read_file → send_report = 数据窃取 | 禁止序列检测 |
| 🔴 **记忆投毒** | 跨 session 破坏 agent 记忆 | 统计 z-score 异常检测 |
| 🟡 **行为异常** | 任何偏离 **您的** agent 正常状态的行为 | Agent DNA 指纹识别 |
| 🔴 **Agent 间注入** | Agent A 通过共享记忆操纵 Agent B | 跨 agent 边界的来源追踪 |
## 独特之处
|
### 🧬 Agent DNA 指纹识别
AgentShield 会随着时间推移了解 **您的特定 agent** 的“正常”状态。即使没有已知的攻击签名,也能检测到行为偏差 —— 无需标记的训练数据。
|
### 🕵️ Canary 注入
不可见的 honeypot tokens 会被静默嵌入到 agent 上下文中。如果 LLM 将其回显,则检测到主动操纵,且 **误报率接近于零**。
|
|
### 🔍 Prompt 来源追踪
进入 LLM 上下文的每一段文本都会被标记其 **来源和信任级别**。来自不受信任 URL 的内容在到达模型之前会受到额外审查。
|
### 🔐 密码学审计追踪
每个事件都通过哈希链与前一个事件相连。**防篡改日志** 适用于合规性、事件响应和法律抗辩。
|
|
### ⚔️ 红队 CLI
在攻击者之前攻击您自己的 agent。
`agentshield redteam --agent ./my_agent.py`
生成包含绕过发现的完整安全报告。
|
### 🌐 多框架支持
目前支持 LangChain。LlamaIndex、AutoGen、CrewAI、原生 OpenAI 和 Anthropic API 将在 v0.5 版本中推出。
|
## 快速开始
```
pip install agentshield
```
```
from agentshield import shield
from agentshield.config import AgentShieldConfig
# Works with your existing agent — zero changes to agent code
agent = your_existing_langchain_agent()
# Wrap it
protected = shield(agent, policy="no_exfiltration")
# Run it — fully monitored and protected
result = protected.run("Summarize the quarterly report")
```
### 在本地运行完整 stack
```
git clone https://github.com/AdityaBelhekar/AgentShield
cd AgentShield
cp .env.example .env # add your API keys
make docker-up # Redis + backend + frontend
```
打开 **http://localhost:5173** —— 实时安全控制台。
## 内置策略
| 策略 | 作用 |
|--------|-------------|
| `no_exfiltration` | 阻止所有 read → send 工具链模式 |
| `strict` | 包含 no_exfiltration 的所有功能 + 更低的漂移阈值 |
| `monitor_only` | 检测所有内容,但不阻止任何内容。适合生产环境推出。 |
或者用 YAML 编写您自己的策略:
```
version: "2.0"
agent_id: "customer-support-bot"
capabilities:
tools:
allowed: ["search_kb", "lookup_order", "send_email"]
denied: ["read_file", "execute_code"]
on_threat:
PROMPT_INJECTION:
action: BLOCK
notify: ["slack:#security-alerts"]
GOAL_DRIFT:
high: BLOCK
medium: ALERT
```
## 项目路线图
```
✅ Phase 0 — Foundation & tooling
🔄 Phase 1 — Event system & data models
⬜ Phase 2 — Interceptor layer (LangChain)
⬜ Phase 3 — Core detection engine
⬜ Phase 4 — Advanced detection (DNA, Canary, Provenance)
⬜ Phase 5 — Policy compiler
⬜ Phase 6 — Backend API + Redis bridge
⬜ Phase 7 — React security console
⬜ Phase 8 — Cryptographic audit trail
⬜ Phase 9 — Red Team CLI
⬜ Phase 10 — Multi-framework adapters
⬜ Phase 11 — Observability & integrations
⬜ Phase 12 — Docs, PyPI release, GitHub Actions
```
## 安全
发现漏洞?**请勿公开 issue。**
私下报告至 → belhekaraditya96@gmail.com
查看 [SECURITY.md](SECURITY.md) 了解完整策略。
## 许可证
MIT © [Aditya Belhekar](https://github.com/AdityaBelhekar)

**如果这帮助您保护了您的 agents,请给个 ⭐**
[GitHub](https://github.com/AdityaBelhekar/AgentShield) · [报告 Bug](https://github.com/AdityaBelhekar/AgentShield/issues) · [请求功能](https://github.com/AdityaBelhekar/AgentShield/issues)
标签:AI基础设施, AI安全, API密钥检测, AutoGen, Chat Copilot, CrewAI, DNS 反向解析, LangChain, LLM防火墙, Python SDK, 大模型安全, 工具滥用防护, 提示词注入检测, 搜索引擎查询, 数据防泄露, 目标劫持检测, 网络安全, 记忆投毒防护, 请求拦截, 轻量级, 输入验证, 运行时防护, 逆向工具, 隐私保护