GCP Pentesting
Google Cloud Platform organizes everything around projects and uses service accounts (SA) as the identities of applications. Escalation in GCP almost always revolves around impersonating service accounts: with the right permissions you can obtain tokens of more privileged SAs and chain up to Owner of the project or organization. Understanding SAs, roles, and the hierarchy (organization → folder → project → resource) is the key.
Hierarchy and identities
Section titled “Hierarchy and identities”Organization -> Folders -> Projects -> Resources# identitiesUsers (Google), Groups, Service Accounts (SA: identity of apps/VMs)# roles: primitive (Owner/Editor/Viewer), predefined, custom# permissions are inherited downward in the hierarchyCredentials and access
Section titled “Credentials and access”# ways ingcloud auth login # usergcloud auth activate-service-account --key-file=sa.json # SA (leaked JSON = access)# SA tokens from a VM (see cloud-metadata)# a leaked service account JSON (recon-code) is direct accessEnumeration
Section titled “Enumeration”gcloud auth list ; gcloud config listgcloud projects list ; gcloud projects get-iam-policy <proj>gcloud iam service-accounts listgcloud <service> ... list # resources by service# toolsgcp_scanner, GCPBucketBrute, Hayat, ScoutSuite (supports GCP)The heart: service account impersonation
Section titled “The heart: service account impersonation”# if you have iam.serviceAccounts.getAccessToken over an SA -> get its tokengcloud auth print-access-token --impersonate-service-account=<SA>@<proj>.iam.gserviceaccount.com# iam.serviceAccounts.actAs + create a resource (function/VM/job) running as that SA# iam.serviceAccountKeys.create -> create a key for an SA (persistence/access)gcloud iam service-accounts keys create k.json --iam-account=<SA>If the target SA is more privileged (Editor/Owner), you impersonate it and escalate.
Typical escalation permissions
Section titled “Typical escalation permissions”resourcemanager.projects.setIamPolicy -> grant yourself project Owneriam.serviceAccounts.getAccessToken/actAs -> impersonate a privileged SAiam.serviceAccountKeys.create -> SA key (persistent access)# via services: deploymentmanager, cloudfunctions/run/compute with a privileged SAcloudfunctions.functions.create + actAs -> function as an admin SA (cloud-serverless)Common services
Section titled “Common services”# Cloud Storage (see cloud-s3)gsutil ls ; gsutil ls gs://<bucket> # public/readable buckets# Compute Enginegcloud compute instances list ; project metadata (SSH keys, startup scripts)gcloud compute project-info describe # sometimes credentials in metadata# Secret Managergcloud secrets list ; gcloud secrets versions access latest --secret=X# Cloud Functions / Cloud Run (cloud-serverless)For the defense
Section titled “For the defense”- Least privilege: avoid broad primitive roles (Owner/Editor); scoped predefined roles.
- Control impersonation: restrict
iam.serviceAccounts.getAccessToken/actAs; disable SA key creation (iam.disableServiceAccountKeyCreation). - SAs without static keys: use Workload Identity; don’t expose service account JSON.
- Organization Policies to restrict (domain restriction, no SA keys, no public IPs).
- Cloud Audit Logs (Admin Activity + Data Access) + Security Command Center; alerts on setIamPolicy/keys.create.
Testing checklist
Section titled “Testing checklist”- Access (user / SA JSON / VM token) and enumeration (gcloud)
- Project and SA IAM policy (inherited roles)
- Impersonate SAs (getAccessToken/actAs) toward more privilege
- serviceAccountKeys.create (access/persistence)
- setIamPolicy → project Owner
- Public GCS buckets (Buckets and Public Storage), Compute metadata (keys)
- Secret Manager and functions with a privileged SA
- Map up to project/organization Owner