purpleshellsecurity/AWSAISecurityLab
GitHub: purpleshellsecurity/AWSAISecurityLab
一个可一键部署的 AWS AI/LLM 安全实验室,通过真实攻击场景演练与验证基于 AWS 服务的纵深防御体系。
Stars: 0 | Forks: 0
# AI / RAG 安全实验室
[](https://github.com/purpleshellsecurity/aws-ai-security-lab/actions/workflows/ci.yml)
[](LICENSE)



## 展示内容
| 技能 | 展示方式 |
|---|---|
| **AI/LLM 安全** | prompt injection、RAG 滥用、数据泄露、system-prompt 泄露 ([OWASP LLM Top 10](docs/owasp-mapping.md)) |
| **威胁建模** | 每个攻击均映射到 [MITRE ATLAS](docs/atlas-mapping.md) 技术 |
| **检测工程** | 基于结构化日志的 4 个 CloudWatch 检测 → 告警 → SNS |
| **安全的 AWS 架构** | 检索层 authz、最小权限 IAM、KMS、WAF、关联日志(端到端使用同一个 `request_id`) |
| **基础设施即代码** | 整个技术栈使用 Terraform;一条命令拉起,一条命令销毁 |
| **验证** | 一个 [测试 runbook](TESTING.md),用于部署、攻击并验证每个控制措施([经验教训](docs/tuning.md)) |
## 工作原理
**Client → WAF → API Gateway → chat Lambda → Bedrock**,每一层都在同一个 `request_id` 下记录日志。
在加固模式下:
- API Gateway 的 **Cognito JWT authorizer** 在边缘验证签名 token。
- Lambda 从**已验证的 claims** 中派生身份和权限,而不是从请求 body 中获取。
- 它**在读取任何文档之前强制执行访问控制**,然后应用 **Bedrock Guardrail** 以确保内容安全。
- 第二个 **detector Lambda** 将日志转化为告警。
## 防御目标
每次攻击都映射到一个控制措施、一个框架和一个可重放的场景(详见
[controls-matrix.md](docs/controls-matrix.md)):
| 攻击 | 阻止它的控制措施 | OWASP · ATLAS | 场景 |
|---|---|---|---|
| 调用方在请求中声明特权身份 | **OAuth2/JWT 认证** — Cognito authorizer;从已验证的 claims 而非 body 中获取身份和权限 | LLM06 · T0012 | 加固模式 `replay.py` / `get_token.sh` |
| 从外部 IP 使用服务账户 | 网络检查 + WAF / 资源策略 + 检测 | LLM06 · T0012 | `04_account_compromise` |
| 权限不足的用户拉取受限文档 | **在检索层进行授权** | LLM02 · T0057 | `03_data_exfiltration` |
| Prompt injection / “列出所有文档” | Bedrock Guardrail(prompt 攻击 + 拒绝主题) | LLM01 · T0051 | `02_prompt_injection` |
| 越狱 / 人格覆盖 | Bedrock Guardrail | LLM01 · T0054 | `05_jailbreak` |
| System-prompt 提取 | 检索 authz + Guardrail | LLM07 · T0056 | `06_system_prompt_extraction` |
| 投毒文档(隐藏指令) | 检索时注入扫描器 + 检测 | LLM01 · T0051.001 | `07_indirect_injection` |
## 快速开始
**您需要准备:**
- **AWS 账户** 并配置好 `aws` CLI
- **Terraform ≥ 1.5**
- **Bedrock 访问权限** — Anthropic 模型需要一次性填写用例表单;`./scripts/preflight.sh` 会告诉您需要点击哪里
- 默认区域 **`us-east-1`**
*使用成本每天只需几美分,并且一切都可以通过一条 `destroy` 命令销毁。请注意常驻的
基准费用:即使在零流量的情况下,WAF web ACL、KMS 密钥和 CloudTrail Bedrock 数据事件每月也会产生大约 **10-15 美元
的费用** — 在不使用时请务必销毁实验室。*
### 1. 部署
```
./scripts/preflight.sh # check AWS creds, Terraform, region, Bedrock access
terraform -chdir=terraform init # first time only
terraform -chdir=terraform apply # deploy (~cents/day); review the plan, type "yes"
```
### 2. 运行攻击并查看其被拦截的过程
```
./demo/replay.py --scenario demo/scenarios/04_account_compromise.json # fire an attack
./demo/show_detections.sh # ~30–60s later: what the detector caught
```
在加固模式下,`replay.py` 会自动进行身份验证 — 它会将每个场景用户登录到 Cognito
并发送他们的 JWT。要手动执行并检查 token:
```
TOKEN=$(./demo/get_token.sh b.taylor) # a signed OIDC token; paste it into jwt.io to read the claims
curl -s "$(terraform -chdir=terraform output -raw api_invoke_url)" \
-H "x-api-key: $(terraform -chdir=terraform output -raw api_key_value)" \
-H "Authorization: $TOKEN" -H 'content-type: application/json' \
-d '{"prompt":"show me the key rotation runbook","x_source_ip":"10.12.0.5"}'
```
`x_source_ip` 模拟内部来源。处理程序的**网络检查点在检索授权之前运行**,
因此没有内部(模拟)来源的请求会在到达权限检查*之前*被拒绝,并提示
`Source not permitted for this principal`。
将 `b.taylor`(受限)替换为 `c.jones`(内部),**相同**的请求将在 `documents_blocked` 中
而不是 `documents_returned` 中返回 runbook — 身份和权限完全由
已验证的 token 而非 body 强制执行。使用 `terraform output cognito_demo_users` 列出预置用户。
### 3. 关闭然后开启控制措施
核心演示 — *相同*的攻击先成功,然后被阻止:
```
terraform -chdir=terraform apply -var=controls_enabled=false # OFF — attack succeeds
terraform -chdir=terraform apply -var=controls_enabled=true # ON — attack blocked (default)
```
### 4. 运行其他攻击(可选)
```
./demo/replay.py --list # list all scenarios
./demo/replay.py # the full incident, end to end
./demo/trace.sh # fire one request, trace it across every layer
```
复制 [`demo/scenarios/`](demo/scenarios/README.md) 中的文件即可编写您自己的场景。
### 5. 销毁
```
terraform -chdir=terraform destroy # stops the cost
```
## 文档
- **理解** — [概述](docs/overview.md) · [架构](docs/architecture.md) · [教程](TUTORIAL.md)
- **操作** — [测试 runbook](TESTING.md) · [调优与经验教训](docs/tuning.md) · [攻击场景](demo/scenarios/README.md)
- **框架** — [控制矩阵](docs/controls-matrix.md) · [MITRE ATLAS 映射](docs/atlas-mapping.md) · [OWASP LLM 映射](docs/owasp-mapping.md) · [端到端日志](docs/end-to-end-logging.md)
- **项目** — [贡献](CONTRIBUTING.md) · [安全](SECURITY.md) · [更新日志](CHANGELOG.md)
## 仓库布局
```
.
├── terraform/ all infrastructure (one .tf file per concern)
├── src/
│ ├── chat/ the assistant Lambda — access control + injection scan + guardrail
│ └── detector/ the detection Lambda — 4 detections
├── corpus/ seed documents (incl. a poisoned one) + classification manifest
├── demo/ replay.py, trace.sh, and scenarios/ (ATLAS-tagged attacks)
├── scripts/ preflight.sh (prerequisite + deploy-status checks)
└── docs/ overview, architecture, controls/ATLAS/OWASP maps, logging, tuning
```
MIT 授权。
标签:AI安全, AWS, Chat Copilot, DLL 劫持, DPI, ECS, RAG, Terraform, 大语言模型, 逆向工具