GCP - Federation Abuse

htARTE (HackTricks AWS Red Team Expert)를 통해 AWS 해킹을 처음부터 전문가까지 배워보세요!

HackTricks를 지원하는 다른 방법:

OIDC - Github Actions 남용

GCP

Github 레포지토리에서 GCP 서비스 계정에게 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)')
  • 새로운 워크로드 식별 풀 OIDC 제공자를 생성합니다. 이 경우에는 github actions (조직/저장소 이름으로 신뢰)를 신뢰합니다:

gcloud iam workload-identity-pools providers create [PROVIDER_ID] \
    --workload-identity-pool=[POOL_ID] \
    --attribute-mapping=[ATTRIBUTE_MAPPING] \
    --attribute-condition=[ATTRIBUTE_CONDITION] \
    --allowed-issuer-prefixes=[ISSUER_PREFIXES]

예시:

gcloud iam workload-identity-pools providers create github-actions \
    --workload-identity-pool=my-pool \
    --attribute-mapping="google.subject=assertion.sub,google.audience=assertion.aud" \
    --attribute-condition="assertion.aud==github.com" \
    --allowed-issuer-prefixes="https://token.actions.githubusercontent.com"

위의 예시에서는 my-pool이라는 워크로드 식별 풀에 github-actions라는 OIDC 제공자를 생성합니다. google.subjectgoogle.audienceassertion.subassertion.aud에 매핑하고, assertion.audgithub.com인 경우에만 사용 가능하도록 설정합니다. 허용된 발급자 접두사는 https://token.actions.githubusercontent.com입니다.

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)')
  • 마지막으로, 공급자의 주체(principal)가 서비스 주체(service principal)를 사용할 수 있도록 허용합니다:

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="데모 공급자" \
--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@v0.3.1'
with:
create_credentials_file: 'true'
workload_identity_provider: '${providerId}'
service_account: '${saId}'
- id: 'gcloud'
name: 'gcloud'
run: |-
gcloud auth login --brief --cred-file="${{ steps.auth.outputs.credentials_file_path }}"
gcloud auth list
gcloud projects list
htARTE (HackTricks AWS Red Team Expert)를 통해 AWS 해킹을 처음부터 전문가까지 배워보세요!

HackTricks를 지원하는 다른 방법:

最終更新