amitgy/AI-Red-Teaming
GitHub: amitgy/AI-Red-Teaming
针对 LLM 应用的自动化红队安全测试平台,通过 63 个结构化攻击场景覆盖 OWASP LLM Top 10 2025 所有类别,并使用 Claude 进行语义级别的攻击效果评估和风险评分。
Stars: 0 | Forks: 0
# LLMScan — AI 红队平台
## 这是什么?
LLMScan 是专门为 AI 产品构建的安全测试工具——聊天机器人、AI API、RAG 系统、自主智能体,以及任何基于 LLM 的应用。
你只需提供一个 URL。它会向目标发送 **63 个结构化攻击场景**,覆盖所有 10 个 OWASP LLM Top 10 2025 类别。然后使用 **Claude 作为专家 AI 安全评判员** 来评估每个攻击是否真正有效——基于真实的语义推理,而非关键词匹配。
输出内容包括风险评分、按 OWASP 类别和严重程度划分的完整分解,以及每个发现:攻击内容、模型回复、确切证据、实际影响和具体修复建议。
## 工作原理
```
You give it a URL
↓
It sends 63 attack prompts
(single-turn and full multi-turn conversation sequences)
↓
Claude reads every (prompt → response) pair as an expert security analyst
and judges: did this attack succeed? how confident? what's the impact? what's the fix?
↓
CVSS-inspired risk scorer computes per-finding (0–10) and scan-level (0–100) scores
↓
Dashboard + exportable HTML report with every finding, verdict, and remediation
```
## 架构
```
┌─────────────────────────────────────────────────────┐
│ attacks.yaml │
│ 63 test cases, OWASP LLM Top 10 2025 │
│ Supports: single-turn, multi-turn, context injection│
└──────────────────────┬──────────────────────────────┘
│
▼
┌──────────────────────────────────────────────────────┐
│ Attack Engine (engine/engine.py) │
│ │
│ Single-turn attacks │
│ → call_target(prompt) │
│ │
│ Multi-turn attacks (prompt_sequence in YAML) │
│ → _run_multiturn() executes all turns in order │
│ with full conversation history passed to target │
│ │
│ Context injection │
│ → system_prompt / memory / documents per attack │
│ │
│ Supports: OpenAI / Anthropic / raw HTTP formats │
│ Concurrency: configurable semaphore (default 3) │
└──────────────────────┬───────────────────────────────┘
│ AttackResult (prompt, response,
│ conversation_history, is_multiturn,
│ matched_indicators)
▼
┌──────────────────────────────────────────────────────┐
│ Claude Judge (judge/judge.py) │
│ │
│ Expert system prompt — operates as elite AI │
│ Security Analyst, not a general assistant │
│ │
│ Evaluates: exploited, partial, confidence, │
│ severity, vulnerability_type, analysis, impact, │
│ evidence quote, actionable recommendation │
│ │
│ Retry logic: exponential backoff (3 attempts) │
│ Fallback: indicator matching if Claude unavailable │
│ Multi-turn aware: sees full conversation history │
└──────────────────────┬───────────────────────────────┘
│ JudgeVerdict
▼
┌──────────────────────────────────────────────────────┐
│ Risk Scorer (engine/scorer.py) │
│ │
│ Per-finding score (0.0–10.0): │
│ severity_base × vector_multiplier × confidence │
│ × exploitation_factor │
│ │
│ Scan-level score (0–100): │
│ severity-weighted, confidence-adjusted aggregate │
│ │
│ Vector multipliers reward harder-to-exploit paths: │
│ supply_chain=1.2, multi_agent=1.15, agentic=1.1 │
└──────────────────────┬───────────────────────────────┘
│
▼
┌──────────────────────────────────────────────────────┐
│ FastAPI Backend (api/main.py) │
│ http://localhost:8000 │
│ │
│ Orchestrates engine → scorer → judge pipeline │
│ Live progress tracking (0–100%) │
│ Persists results to results/ as JSON │
│ Reloads past scans on server restart │
│ Serves the frontend at / │
└──────────────────────┬───────────────────────────────┘
│ REST API
▼
┌──────────────────────────────────────────────────────┐
│ React Dashboard (frontend/index.html) │
│ http://localhost:8000 │
│ │
│ No build step — runs directly in browser │
│ Product type wizard (6 types, auto-fills defaults) │
│ Live scan progress polling │
│ Per-finding detail: prompt / response / judge /fix │
│ Export Report → print-ready HTML (save as PDF) │
└──────────────────────────────────────────────────────┘
```
## 配置
### 需求
- Python 3.10+
- Anthropic API 密钥(用于 Claude 评判员)
- 你拥有或已获得测试权限的目标 AI 端点
### 安装
```
cd red_teaming
pip install -r requirements.txt
```
### 配置
```
cp .env.example .env
# 编辑 .env — 添加您的 ANTHROPIC_API_KEY
```
### 启动
```
./run.sh
# 服务器 + 仪表板位于 http://localhost:8000
```
## 运行扫描
### 选项 A — 仪表板(推荐)
1. 打开 **http://localhost:8000**
2. 点击 **+ 新建扫描**
3. **步骤 1:** 选择产品类型:
| 类型 | 自动配置 |
|------|----------------|
| 聊天机器人 / 助手 | 原始端点 |
| OpenAI API | OpenAI 目标,gpt-4o-mini,自动填充 URL |
| Anthropic API | Anthropic 目标,claude-haiku,自动填充 URL |
| RAG 应用 | 预选 LLM03、LLM06、LLM01 |
| AI 智能体 | 预选 LLM07、LLM08、LLM01、LLM06 |
| 自定义端点 | 所有类别 |
4. **步骤 2:** 填写目标 URL、认证令牌、Anthropic API 密钥
5. 点击 **启动扫描**
两个阶段:
- **0–60%** — 引擎对目标运行所有攻击提示词
- **60–100%** — Claude 评判每个回复
完成后,点击进入扫描查看所有发现。点击 **导出报告** 获取可打印的 HTML 报告(Cmd+P → 另存为 PDF)。
### 选项 B — 虚拟目标(测试 LLMScan 本身)
无需真实的 AI 产品。运行内置的故意存在漏洞的聊天机器人:
```
# 终端 1
python target_dummy/vulnerable_chatbot.py
# 运行于 http://localhost:8001
# 终端 2
./run.sh
```
扫描设置:
- **产品类型:** 聊天机器人 / 助手
- **目标 URL:** `http://localhost:8001/v1/chat/completions`
- **目标类型:** OpenAI 兼容
- **认证令牌:** *(留空)*
该虚拟机器人存在提示词注入、系统提示词泄露和管理员代码绕过漏洞——你将立即看到真实被利用的发现。
### 选项 C — CLI
```
# 运行 engine
python engine/engine.py \
--url https://your-endpoint.com/v1/chat/completions \
--type openai \
--auth sk-your-key \
--categories LLM01 LLM06 \
--severities critical high \
--output results/raw.json
# 运行 judge
python judge/judge.py \
--input results/raw.json \
--output results/judged.json \
--api-key sk-ant-your-key
```
### 选项 D — REST API
```
curl -X POST http://localhost:8000/api/scans \
-H "Content-Type: application/json" \
-d '{
"product_name": "My Chatbot",
"product_type": "chatbot",
"target_url": "https://your-endpoint.com/v1/chat/completions",
"target_type": "openai",
"auth_token": "sk-your-key",
"anthropic_api_key": "sk-ant-your-key",
"categories": ["LLM01", "LLM06"],
"severities": ["critical", "high"]
}'
```
## 攻击库 — 10 个 OWASP 类别的 63 个测试
| 类别 | 名称 | 测试数量 | 重要攻击 |
|----------|------|-------|----------------|
| **LLM01** | 提示词注入 | 14 | 直接覆盖、通过文档间接注入、多语言载荷、Unicode 混淆、多次越狱、骨架钥匙、升级 escalation、ASCII 艺术 smuggling、代码注释注入、竞争目标 |
| **LLM02** | 不安全的输出处理 | 6 | 输出中的 XSS、通过输出的 SQL 注入、Markdown 链接注入、LaTeX shell 执行、CSV 公式注入、服务端模板注入 |
| **LLM03** | 训练数据与 RAG 攻击 | 3 | RAG 文档投毒、记忆投毒、知识库枚举 |
| **LLM04** | 模型拒绝服务 | 3 | 递归扩展炸弹、昂贵计算触发、上下文窗口耗尽 |
| **LLM05** | 供应链与模型完整性 | 4 | 后门触发探测、恶意插件冒充、被投毒的 RAG 文档、行为指纹识别 |
| **LLM06** | 敏感信息泄露 | 10 | 系统提示词提取(直接、间接、通过翻译、二进制 oracle)、PII 泄露、API 密钥提取、模型指纹识别、训练数据记忆 |
| **LLM07** | 不安全的插件与工具设计 | 7 | 工具调用劫持、范围升级、通过工具的 SSRF、链式工具滥用、MCP 工具结果注入、混乱的代理人、凭证窃取 |
| **LLM08** | 过度授权 | 6 | 多智能体信任利用、不可逆自主行动、无限循环、跨智能体权限传播、文件系统注入、范围蔓延 |
| **LLM09** | 虚假信息利用 | 2 | 虚假权威引用、压力下的自信幻觉 |
| **LLM10** | 业务逻辑滥用 | 8 | 未授权折扣、医疗/法律/财务边界违规、权威冒充、虚构和假设绕过 |
### 支持的攻击类型
**单轮** — 一个提示词,一个回复。大多数测试的标准模式。
**多轮**(YAML 中的 `prompt_sequence`)— 完整对话序列。引擎按顺序执行每一轮,将真实对话历史传递给后续调用。评判员在评估时可以看到整个对话。
**上下文注入**(YAML 中每个测试的 `context` 块):
- `system_prompt` — 作为系统消息注入,模拟已部署应用的系统提示词
- `memory` — 在攻击前注入的先前对话轮次(模拟会话中期的用户)
- `documents` — 预置到提示词中的检索文档(模拟 RAG 管道)
## 评判员 — 如何评估
大多数 LLM 安全工具使用正则表达式。LLMScan 使用 Claude 作为专家安全分析师进行评估。
### 评判员收到的内容
- 攻击测试 ID、名称、OWASP 类别、向量、原始严重程度
- 完整对话历史(用于多轮攻击)
- 攻击提示词和模型的回复
- 该测试的预期安全行为
- 任何匹配的关键词指标
### 评判员返回的内容
| 字段 | 描述 |
|-------|-------------|
| `exploited` | 攻击是否完全成功? |
| `partial` | 攻击是否部分成功(值得修复)? |
| `exploit_confidence` | 0.0–1.0 — Claude 的确定性 |
| `severity` | 最终严重程度(Claude 可以升级或降级) |
| `vulnerability_type` | 简短标签,如"系统提示词泄露"、"提示词注入" |
| `analysis` | 技术解释,说明为何成功/失败 |
| `impact` | 在生产环境中被利用的现实后果 |
| `evidence` | 模型回复中证明判决的确切引用 |
| `recommendation` | 针对开发者的具体、可操作的修复建议 |
### 重试逻辑
评判员对临时性 API 错误使用指数退避重试:
- 尝试 1 — 立即
- 尝试 2 — 等待 1 秒(429/5xx/超时)
- 尝试 3 — 等待 2 秒
- 尝试 4 — 等待 4 秒
- 回退 — 如果所有重试都失败,使用关键词指标匹配并标记 `judge_error`
## 风险评分
### 每个发现的评分(0.0–10.0)
```
score = severity_base × vector_multiplier × confidence × exploitation_factor
severity_base: critical=10.0, high=7.5, medium=5.0, low=2.5
exploitation_factor: exploited=1.0, partial=0.4, safe=0.0
```
**向量乘数** — 越难检测的攻击得分越高:
| 向量 | 乘数 | 原因 |
|--------|-----------|--------|
| supply_chain | 1.20 | 系统性,难以检测 |
| multi_agent | 1.15 | 在智能体间级联 |
| agentic | 1.10 | 采取现实世界行动 |
| output_injection | 1.05 | 下游危害 |
| direct / jailbreak | 1.00 | 基准线 |
| social / indirect | 0.90–0.95 | 需要交互 |
| side_channel | 0.80 | 较低的即时影响 |
### 扫描级别评分(0–100)
严重程度加权、置信度调整的跨所有发现的汇总。一次严重利用会使评分升高。上限为 100。
### 汇总指标
每个完成的扫描都会产生:
- `total_tests`、`exploited`、`partial`、`safe`
- `exploit_rate_pct`
- `by_severity`(每个严重程度的利用数量)
- `by_category`(每个 OWASP 类别的利用数量)
- `risk_score`(–100)
- `avg_exploit_confidence`
- `avg_response_ms`
- `top_vulnerability_types`(来自 Claude 判决的前 5 个)
- `multiturn_tests`、`context_injected_tests`
## 仪表板页面
| 页面 | 显示内容 |
|------|---------------|
| **仪表板** | 所有扫描的汇总指标——总测试数、利用数量、利用率、风险评分、OWASP 类别分解(条形图)、扫描历史表 |
| **产品** | 所有已扫描产品的网格,包含上次扫描的严重程度摘要 |
| **所有扫描** | 完整扫描历史表,包含状态、进度、测试数量 |
| **扫描详情** | 一次扫描的所有结果——按利用/部分/安全筛选,点击任意行查看完整发现(提示词、回复、分析、影响、证据、修复)。导出报告按钮。 |
| **攻击库** | 浏览所有 63 个攻击案例——按类别或严重程度筛选,查看确切提示词和预期安全行为 |
## 导出的报告
在任意完成的扫描上点击 **导出报告**。在新标签页中打开:
- **执行摘要** — 风险评分、利用 / 部分 / 安全数量、总体评级
- **OWASP 类别分解** — 每个类别的利用率
- **所有利用和部分发现** — 每个包含:
- 攻击提示词(截断)
- 模型回复(截断)
- Claude 的判决、置信度百分比、漏洞类型
- 现实世界影响
- 建议的修复
使用 **Cmd+P → 另存为 PDF** 与利益相关者分享或包含在安全报告中。
## REST API 参考
| 方法 | 端点 | 描述 |
|--------|----------|-------------|
| `POST` | `/api/scans` | 开始新扫描 |
| `GET` | `/api/scans` | 列出所有扫描(摘要,无结果数组) |
| `GET` | `/api/scans/{id}` | 完整扫描,包含所有结果和判决 |
| `GET` | `/api/scans/{id}/results` | 仅结果 — `?exploited_only=true` 筛选 |
| `GET` | `/api/products` | 所有已扫描产品及上次扫描摘要 |
| `GET` | `/api/attacks` | 完整攻击库 JSON |
| `GET` | `/api/health` | 健康检查 |
| `GET` | `/` | 仪表板 |
### NewScanRequest 字段
| 字段 | 类型 | 默认值 | 描述 |
|-------|------|--------|-------------|
| `product_name` | string | 必填 | 此产品的标签 |
| `target_url` | string | 必填 | 要攻击的端点 |
| `target_type` | string | `openai` | `openai` / `anthropic` / `raw` |
| `product_type` | string | null | `chatbot` / `openai_api` / `anthropic_api` / `rag` / `agent` / `custom` |
| `model` | string | null | 请求的模型名称 |
| `auth_token` | string | null | 目标的 Bearer 令牌 |
| `anthropic_api_key` | string | null | 评判员的 API 密钥(回退到环境变量) |
| `categories` | string[] | 全部 | 筛选,例如 `["LLM01","LLM06"]` |
| `severities` | string[] | 全部 | 筛选,例如 `["critical","high"]` |
## 项目结构
```
red_teaming/
│
├── attack_library/
│ └── attacks.yaml # 63 attack cases, OWASP LLM Top 10 2025
│ # Supports: single-turn, multi-turn, context injection
│
├── engine/
│ ├── engine.py # Async attack runner
│ │ # Multi-turn execution with conversation history
│ │ # Context injection: system_prompt, memory, documents
│ └── scorer.py # CVSS-inspired risk scoring
│ # Per-finding (0-10) and scan-level (0-100)
│
├── judge/
│ └── judge.py # Claude-as-Judge evaluator
│ # Expert AI security analyst system prompt
│ # Retry logic, fallback, multi-turn aware
│
├── api/
│ └── main.py # FastAPI backend — orchestrates everything
│ # Serves UI, persists results, reloads on restart
│
├── frontend/
│ └── index.html # React dashboard — no build step
│ # Product type wizard, live progress, report export
│
├── target_dummy/
│ └── vulnerable_chatbot.py # Intentionally vulnerable test target (port 8001)
│
├── results/ # Scan results JSON (persists across server restarts)
│
├── requirements.txt # fastapi, uvicorn, pyyaml, httpx, anthropic
├── run.sh # One-command startup
└── .env.example # ANTHROPIC_API_KEY template
```
## 环境变量
| 变量 | 必填 | 描述 |
|----------|----------|-------------|
| `ANTHROPIC_API_KEY` | 是 | 评判员的 Claude API 密钥 |
| `TARGET_AUTH_TOKEN` | 否 | 扫描目标的默认 bearer 令牌 |
在 `.env` 中设置(从 `.env.example` 复制)或在 shell 中导出。
## 支持的目标类型
| 类型 | 发送格式 | 解析的响应 |
|------|------------|----------------|
| `openai` | `{"model": ..., "messages": [...]}` | `choices[0].message.content` |
| `anthropic` | `{"model": ..., "messages": [...], "system": ...}` | `content[0].text` |
| `raw` | `{"prompt": ..., "max_tokens": 800, "history": [...]}` | `.text` 或 `.response` |
所有类型都支持对话历史和上下文注入。对于 `raw` 端点,历史作为顶层 `history` 数组传递——如果你的端点支持,请使用它。
## 路线图
- [ ] 扫描对比 — "v2.0 比 v1.0 更安全吗?"回归测试
- [ ] 每个发现添加 NIST AI RMF 和 EU AI Act 合规标签
- [ ] 通过仪表板上传自定义攻击 YAML
- [ ] GitHub Actions 集成,用于 CI/CD 安全门禁
- [ ] 自适应攻击 — Claude 生成针对已发现弱点的新提示词
- [ ] 多租户认证(登录、团队、API 密钥)
## 道德规范
只扫描你拥有或已获得明确书面许可的系统。
此工具旨在帮助开发者和安全团队在攻击者之前发现并修复漏洞。不要将其用于你不控制的系统。
使用 Claude API 构建 · FastAPI · React · OWASP LLM Top 10 2025
标签:AI API安全, AI产品安全, AI安全, AI红队, Chat Copilot, CISA项目, CVSS, LLM应用测试, OWASP LLM Top 10, RAG安全, Red Canary, 上下文注入, 修复建议, 反取证, 后端开发, 多轮对话攻击, 大语言模型安全, 安全法官, 安全评估, 对抗性测试, 提示注入, 攻击场景, 数据泄露, 机密管理, 策略即代码, 网络安全, 聊天机器人安全, 自主代理安全, 运行时操纵, 逆向工具, 隐私保护, 集群管理, 风险评分