GCP - Federation Abuse

Support HackTricks

OIDC - Github Actions Abuse

GCP

Github repo에서 GCP 서비스 계정Github Actions에 대한 액세스를 제공하기 위해 다음 단계가 필요합니다:

  • 원하는 권한으로 github actions에서 액세스할 서비스 계정생성합니다:

projectId=FIXME
gcloud config set project $projectId

# Create the Service Account
gcloud iam service-accounts create "github-demo-sa"
saId="github-demo-sa@${projectId}.iam.gserviceaccount.com"

# Enable the IAM Credentials API
gcloud services enable iamcredentials.googleapis.com

# Give permissions to SA

gcloud projects add-iam-policy-binding $projectId \
--member="serviceAccount:$saId" \
--role="roles/iam.securityReviewer"
  • 새로운 워크로드 아이덴티티 풀 생성:

# Create a Workload Identity Pool
poolName=wi-pool

gcloud iam workload-identity-pools create $poolName \
--location global \
--display-name $poolName

poolId=$(gcloud iam workload-identity-pools describe $poolName \
--location global \
--format='get(name)')
  • Generate a new workload identity pool OIDC provider that trusts github actions (by org/repo name in this scenario):

attributeMappingScope=repository # could be sub (GitHub repository and branch) or repository_owner (GitHub organization)

gcloud iam workload-identity-pools providers create-oidc $poolName \
--location global \
--workload-identity-pool $poolName \
--display-name $poolName \
--attribute-mapping "google.subject=assertion.${attributeMappingScope},attribute.actor=assertion.actor,attribute.aud=assertion.aud,attribute.repository=assertion.repository" \
--issuer-uri "https://token.actions.githubusercontent.com"

providerId=$(gcloud iam workload-identity-pools providers describe $poolName \
--location global \
--workload-identity-pool $poolName \
--format='get(name)')
  • 마지막으로, 제공자의 주체가 서비스 주체를 사용할 수 있도록 허용합니다:

gitHubRepoName="repo-org/repo-name"
gcloud iam service-accounts add-iam-policy-binding $saId \
--role "roles/iam.workloadIdentityUser" \
--member "principalSet://iam.googleapis.com/${poolId}/attribute.${attributeMappingScope}/${gitHubRepoName}"

이전 멤버에서 **org-name/repo-name**을 서비스 계정에 접근할 수 있는 조건으로 지정하고 있다는 점에 유의하세요 (브랜치와 같이 더 제한적인 다른 매개변수도 사용할 수 있습니다).

그러나 와일드카드를 사용하여 모든 github가 서비스 계정에 접근할 수 있도록 허용하는 것도 가능합니다:

# Workload Identity Pool 생성
poolName=wi-pool2

gcloud iam workload-identity-pools create $poolName \
--location global \
--display-name $poolName

poolId=$(gcloud iam workload-identity-pools describe $poolName \
--location global \
--format='get(name)')

gcloud iam workload-identity-pools providers create-oidc $poolName \
--project="${projectId}" \
--location="global" \
--workload-identity-pool="$poolName" \
--display-name="Demo provider" \
--attribute-mapping="google.subject=assertion.sub,attribute.actor=assertion.actor,attribute.aud=assertion.aud" \
--issuer-uri="https://token.actions.githubusercontent.com"

providerId=$(gcloud iam workload-identity-pools providers describe $poolName \
--location global \
--workload-identity-pool $poolName \
--format='get(name)')

# 와일드카드 확인
gcloud iam service-accounts add-iam-policy-binding "${saId}" \
--project="${projectId}" \
--role="roles/iam.workloadIdentityUser" \
  --member="principalSet://iam.googleapis.com/${poolId}/*"

이 경우 누구나 github actions에서 서비스 계정에 접근할 수 있으므로, 항상 멤버가 어떻게 정의되어 있는지 확인하는 것이 중요합니다. 항상 다음과 같은 형식이어야 합니다:

attribute.{custom_attribute}:principalSet://iam.googleapis.com/projects/{project}/locations/{location}/workloadIdentityPools/{pool}/attribute.{custom_attribute}/{value}

Github

**${providerId}**와 **${saId}**를 각각의 값으로 변경하는 것을 잊지 마세요:

name: Check GCP action
on:
workflow_dispatch:
pull_request:
branches:
- main

permissions:
id-token: write

jobs:
Get_OIDC_ID_token:
runs-on: ubuntu-latest
steps:
- id: 'auth'
name: 'Authenticate to GCP'
uses: 'google-github-actions/auth@v2.1.3'
with:
create_credentials_file: 'true'
workload_identity_provider: '${providerId}' # In the providerId, the numerical project ID (12 digit number) should be used
service_account: '${saId}'                  # instead of the alphanumeric project ID. ex:
activate_credentials_file: true             # projects/123123123123/locations/global/workloadIdentityPools/iam-lab-7-gh-pool/providers/iam-lab-7-gh-pool-oidc-provider'
- id: 'gcloud'
name: 'gcloud'
run: |-
gcloud config set project <project-id>
gcloud config set account '${saId}'
gcloud auth login --brief --cred-file="${{ steps.auth.outputs.credentials_file_path }}"
gcloud auth list
gcloud projects list
gcloud secrets list
Support HackTricks

Last updated