使用 Terraform 创建你自己的AWS IAM 权限提升靶场。

IAM 漏洞环境使用 Terraform 二进制文件和你的 AWS 凭证,在你选择的 AWS 账户中部署超过 250 个 IAM 资源。几分钟内,你就可以开始学习如何识别和利用允许权限提升的易受攻击 IAM 配置。
大家好。IAM 漏洞环境对于理解 AWS IAM 权限提升的基本构建块仍然非常有用。然而,在制作 IAM 漏洞环境几年后,我创建了 CloudFoxable,这是一个 CTF 风格版本,可以更全面地教你云渗透测试的基础知识。——@sethsec
加入我们的 RedSec Discord 服务器
🦊 当前支持的权限提升路径: 31
博客文章:IAM 漏洞环境——一个 AWS IAM 权限提升靶场
本快速入门概述了一种带有主观意见的方法,旨在尽可能快地在你的 AWS 账户中启动并运行 IAM 漏洞环境。你可能已经完成了其中许多步骤,或者你可能希望调整一些设置以适应当前配置。请查看此仓库中的其他用例部分,了解一些其他配置选项。
aws sts get-caller-identity 确认 CLI 按预期工作。git clone https://github.com/BishopFox/iam-vulnerablecd iam-vulnerable/terraform initexport TF_VAR_aws_local_profile=PROFILE_IN_AWS_CREDENTIALS_FILE_IF_OTHER_THAN_DEFAULTexport TF_VAR_aws_local_creds_file=FILE_LOCATION_IF_NON_DEFAULTterraform planterraform apply清理
每当你想要移除所有 IAM 漏洞环境创建的资源时,可以运行以下命令:
cd iam-vulnerable/terraform destroy替代清理(当 Terraform 状态丢失时)
如果你已使用 Terraform 部署了 iam-vulnerable,但不再拥有对状态文件的访问权限(并且 terraform destroy 无法正常工作),则可以使用以下清理脚本:```bash
./cleanup-scripts/cleanup_iam_vulnerable.py --dry-run
./cleanup-scripts/cleanup_iam_vulnerable.sh --dry-run
这些脚本将:
- 自动识别您 AWS 账户中所有存在 IAM 漏洞的资源
- 在操作前向您展示将要删除的内容
- 按正确顺序删除资源以避免依赖冲突
- 支持 AWS 配置文件并提供详细日志
**重要提醒**:始终先使用 `--dry-run` 运行,查看将被删除的内容。详细使用说明请参见 `cleanup-scripts/CLEANUP_README.md`。
## 刚刚创建了哪些资源?
Terraform 二进制文件已使用您的默认 AWS 账户配置文件凭证创建了:
* **31 个用户、角色和策略**,每个都有通往该游乐场账户管理权限的独特利用路径
* 一些额外的用户、组、角色和策略,用于完全实现某些利用路径
* 一些额外的用户、角色和策略,用于测试其他工具的检测能力
默认情况下,此 Terraform 模块创建的每个角色均可由您用来运行 Terraform 的用户或角色代入。
* 如果您希望 Terraform 使用除默认配置文件之外的配置文件,或希望硬编码 `assume_role_policy` ARN,请参阅[其他用例](#other-use-cases)。
## 这需要花费多少钱?
在**默认配置下部署 IAM Vulnerable 不会产生任何费用**。请参阅下一节了解如何启用确实会产生费用的非默认模块,以及部署后每个模块每月的大致费用。
# 模块化方法
IAM Vulnerable 将某些资源分组到模块中。部分模块默认启用(不会产生费用的模块),其他模块默认禁用(部署后会产生费用的模块)。这样,您可以根据需要启用特定模块。
例如,当您准备好尝试涉及 IAM 之外资源的利用路径(如 `ssm:StartSession`)时,可以通过取消 `iam-vulnerable/main.tf` 文件中相关模块的注释,并重新运行 `terraform apply` 来按需部署和拆除这些资源:```
# Uncomment the next four lines to create an ec2 instance and related resources
#module "ec2" {
# source = "./modules/non-free-resources/ec2"
# aws_assume_role_arn = (var.aws_assume_role_arn != "" ? var.aws_assume_role_arn : data.aws_caller_identity.current.arn)
#}
在你取消注释ec2模块后,运行:```
terraform init
terraform apply
您现已部署所需组件,可尝试 SSM 权限提升路径。
## 免费资源模块
部署在 `free-resources` 中的任何内容均无成本:
| 名称 | 默认状态 | 预估成本 | 描述 |
| --- | --- | --- | --- |
| privesc-paths | 已启用 | 无 | 包含所有 IAM 权限提升路径 |
| tool-testing | 已启用 | 无 | 包含评估不同 IAM 权限提升工具能力的测试用例 |
## 非免费资源模块
部署这些附加模块可能产生费用:
| 名称 | 默认状态 | 预估成本 | 描述 | 所需用途 |
| --- | --- | --- | --- | --- |
| EC2 | 已禁用 | :heavy_dollar_sign: <br> 每月 $4.50 | 创建一个 EC2 实例及一个允许任意来源 SSH 连接的安全组 | `ssm-SendCommand` <br> `ssm-StartSession` <br> `ec2InstanceConnect-SendSSHPublicKey` |
| Lambda | 已禁用 | :slightly_smiling_face: <br> 每月费用取决于使用量(成本应为零) | 创建一个 Lambda 函数 | `Lambda-EditExistingLambdaFunctionWithRole` |
| Glue | 已禁用 | :heavy_dollar_sign::heavy_dollar_sign::heavy_dollar_sign::heavy_dollar_sign: <br> 每小时 $4 | 创建一个 Glue 开发端点 | `Glue-UpdateExistingGlueDevEndpoint` |
| SageMaker | 已禁用 | 尚不确定 | 创建一个 SageMaker 笔记本 | `sageMakerCreatePresignedNotebookURL` |
| CloudFormation | 已禁用 | :slightly_smiling_face: <br> 通过 CloudFormation 创建的密钥每月 $0.40。栈本身无成本或几乎无成本 | 创建一个 CloudFormation 栈,该栈在 Secret Manager 中创建一个密钥 | `privesc-cloudFormationUpdateStack` |
# 支持的权限提升路径
| 路径名称 | IAM 脆弱配置文件名称 | 所需非默认模块 | 利用参考 |
| --- | --- | --- | --- |
| **类别:对其他用户的 IAM 权限** | | | |
| IAM-CreateAccessKey | privesc4 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 04](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) <br> :lock: [s3cur3.it IAMVulnerable - Part 3](https://s3cur3.it/home/practicing-aws-security-with-iamvulnerable-part-3) |
| IAM-CreateLoginProfile | privesc5 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 05](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) <br> :lock: [s3cur3.it IAMVulnerable - Part 3](https://s3cur3.it/home/practicing-aws-security-with-iamvulnerable-part-3) |
| IAM-UpdateLoginProfile | privesc6 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 06](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) <br> :lock: [s3cur3.it IAMVulnerable - Part 3](https://s3cur3.it/home/practicing-aws-security-with-iamvulnerable-part-3) |
| **类别:将角色传递(PassRole)给服务** | | | |
| CloudFormation-PassExistingRoleToCloudFormation | privesc20 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 20](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) |
| CodeBuild-CreateProjectPassRole | privesc-codeBuildProject | 无 | |
| DataPipeline-PassExistingRoleToNewDataPipeline | privesc21 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 21](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) |
| EC2-CreateInstanceWithExistingProfile | privesc3 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 03](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) <br> :lock: [s3cur3.it IAMVulnerable - Part 2](https://s3cur3.it/home/practicing-aws-security-with-iamvulnerable-part-2) |
| Glue-PassExistingRoleToNewGlueDevEndpoint | privesc18 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 18](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) |
| Lambda-PassExistingRoleToNewLambdaThenInvoke | privesc15 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 15](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) |
| Lambda-PassRoleToNewLambdaThenTrigger | privesc16 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 16](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) |
| SageMaker-CreateNotebookPassRole | privesc-sageNotebook | 无 | :rhinoceros: [AWS IAM Privilege Escalation - Method 2](https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation-part-2/) |
| SageMaker-CreateTrainingJobPassRole | privesc-sageTraining | 无 | |
| SageMaker-CreateProcessingJobPassRole | privesc-sageProcessing | 无 | |
| **类别:对策略的权限** | | | |
| IAM-AddUserToGroup | privesc13 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 13](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) |
| IAM-AttachGroupPolicy | privesc8 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 08](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) |
| IAM-AttachRolePolicy | privesc9 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 09](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) |
| IAM-AttachUserPolicy | privesc7 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 07](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) |
| IAM-CreateNewPolicyVersion | privesc1 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 01](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) <br> :lock: [s3cur3.it IAMVulnerable - Part 1](https://s3cur3.it/home/practicing-aws-security-with-iamvulnerable) |
| IAM-PutGroupPolicy | privesc11 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 11](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) |
| IAM-PutRolePolicy | privesc12 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 12](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) |
| IAM-PutUserPolicy | privesc10 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 10](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) |
| IAM-SetExistingDefaultPolicyVersion | privesc2 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 02](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) <br> :lock: [s3cur3.it IAMVulnerable - Part 2](https://s3cur3.it/home/practicing-aws-security-with-iamvulnerable-part-2) |
| **类别:使用 AWS 服务进行权限提升** | | | |
| EC2InstanceConnect-SendSSHPublicKey | privesc-instanceConnect | EC2 | 🔑 [AWS IAM privilege escalation paths](https://pswalia2u.medium.com/aws-iam-privilege-escalation-paths-cba36be1aa9e) |
| CloudFormation-UpdateStack | privesc-cfUpdateStack | CloudFormation | 🔑 [AWS IAM privilege escalation paths](https://pswalia2u.medium.com/aws-iam-privilege-escalation-paths-cba36be1aa9e) |
| Glue-UpdateExistingGlueDevEndpoint | privesc19 | Glue | :fox_face: [Well, That Escalated Quickly - Privesc 19](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) |
| Lambda-EditExistingLambdaFunctionWithRole | privesc17 | Lambda | :fox_face: [Well, That Escalated Quickly - Privesc 17](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) <br> :lock: [s3cur3.it IAMVulnerable - Part 4](https://s3cur3.it/home/practicing-aws-security-with-iamvulnerable-part-4) |
| SageMakerCreatePresignedNotebookURL | privesc-sageUpdateURL | SageMaker | :rhinoceros: [AWS IAM Privilege Escalation - Method 3](https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation-part-2/) |
| SSM-SendCommand | privesc-ssm-command | EC2 | 🔑 [AWS IAM privilege escalation paths](https://pswalia2u.medium.com/aws-iam-privilege-escalation-paths-cba36be1aa9e) |
| SSM-StartSession | privesc-ssm-session | EC2 | 🔑 [AWS IAM privilege escalation paths](https://pswalia2u.medium.com/aws-iam-privilege-escalation-paths-cba36be1aa9e) |
| STS-AssumeRole | privesc-assumerole | 无 | 🔑 [AWS IAM privilege escalation paths](https://pswalia2u.medium.com/aws-iam-privilege-escalation-paths-cba36be1aa9e) |
| **类别:更新 AssumeRole 策略** | | | |
| IAM-UpdatingAssumeRolePolicy | privesc14 | 无 | :fox_face: [Well, That Escalated Quickly - Privesc 14](https://labs.bishopfox.com/tech-blog/privilege-escalation-in-aws) |
# 其他使用场景
#### 默认 - 未配置 `terraform.tfvars`
* 使用默认 AWS 配置文件(Default)进行部署
* 所有创建的角色均可由运行 Terraform 的主体(在默认配置文件中指定)担任
#### 使用非默认配置文件运行 Terraform
* 将 `terraform.tfvars.example` 复制为 `terraform.tfvars`
* 取消注释 `#aws_local_profile = "profile_name"` 并输入要使用的配置文件名称
* 如果你使用了非默认配置文件,并且仍想使用 `aws_credentials_file_example` 文件,可以使用以下命令生成适用于非默认配置文件名称的 AWS 凭证文件(感谢 @scriptingislife)
* 记得将 `nondefaultuser` 替换为你所使用的配置文件名称:
* `tail -n +7 aws_credentials_file_example | sed -e "s/111111111111/$(aws sts get-caller-identity | grep Account | awk -F\" '{print $4}')/g;s/default/nondefaultuser/g" >> ~/.aws/credentials`
#### 使用除调用者以外的 ARN 作为可担任新创建角色的主体
* 将 `terraform.tfvars.example` 复制为 `terraform.tfvars`
* 取消注释 `#aws_assume_role_arn = "arn:aws:iam::112233445566:user/you"` 并输入要使用的 ARN
创建后,每个权限提升角色均可由你指定的主体(ARN)担任。
#### 在账户 X 中创建资源,但使用账户 Y 的 ARN 作为可担任新创建角色的主体
如果你配置了可假定其他账户角色的 AWS CLI 配置文件,则需要指定配置文件名称,并手动指定要用于承担不同角色的 ARN。
在以下示例中,资源将在与 `"prod-cross-org-access-role"` 关联的账户中创建,但 Terraform 创建的每个角色均可由属于另一个账户的 `"arn:aws:iam::112233445566:user/you"` 访问。```
aws_local_profile = "prod-cross-org-access-role"
aws_assume_role_arn = "arn:aws:iam::112233445566:user/you"
这些工具都使用 Terraform 在 AWS 上部署有意的脆弱基础设施。然而,IAM Vulnerable 的重点是 IAM 权限提升,而其他工具要么不涉及 IAM 权限提升,要么只涵盖部分场景。
所有这些工具都有助于识别 AWS 环境中现有的错误配置。有些工具(如 Pacu)还能帮助你利用这些错误配置。相比之下,IAM Vulnerable 创建了有意的脆弱基础设施。如果你真想学习如何使用 Principal Mapper (PMapper)、AWSPX、Pacu 和 Cloudsplaining 这类工具,那么 IAM Vulnerable 就是为你准备的。
我以前也害怕 Terraform 以及那些会在我的账户中创建资源的项目,那时我还不知道 Terraform 是如何工作的。以下是一些可能缓解你焦虑的方法:
terraform plan 命令是一个预演。它会在你实际运行 terraform apply 之前精确显示将要部署的内容。terraform destroy 销毁任何使用 terraform apply 部署的内容,从而获得一个干净的环境。iam-vulnerable 这样的 Terraform 目录中执行它。该工具会运行 terraform plan,并根据当前配置计算与该计划相关的月度成本。我就是用这个工具来填充上面模块成本估算表的。可以。每个工具都会独立维护其 Terraform 状态,但所有资源将在同一账户中创建、更新和删除,并且它们可以共存。
cp ~/.aws/credentials ~/.aws/credentials.backuptail -n +7 aws_credentials_file_example | sed s/111111111111/$(aws sts get-caller-identity | grep Account | awk -F\" '{print $4}')/g >> ~/.aws/credentials