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.
Threat model
Section titled “Threat model”The attacker gets in with a Google user, a leaked service account JSON (direct access), or a token stolen from a VM’s metadata. From there, the goal is impersonation: use permissions like iam.serviceAccounts.getAccessToken or actAs to obtain tokens of more privileged SAs and escalate to Owner. Permissions are inherited downward through the hierarchy, so a broad role at the organization or folder level propagates to every project beneath it.
flowchart TD
A[User / SA JSON / VM token] --> B[Enumerate IAM policies]
B --> C{Impersonation permissions?}
C -->|getAccessToken / actAs| D[Privileged SA token]
C -->|setIamPolicy| E[Grant yourself Owner]
D --> F[Create resource running as admin SA]
F --> G[Project Owner]
E --> G
G --> H[Organization Owner]
Theory: hierarchy and identities
Section titled “Theory: hierarchy and identities”- Hierarchy: Organization → Folders → Projects → Resources. Permissions are inherited downward.
- Identities: Google users, groups, and service accounts (SA: identity of apps and VMs).
- Roles: primitive (
Owner/Editor/Viewer, very broad), predefined (scoped per service), and custom.
# ways ingcloud auth login # usergcloud auth activate-service-account --key-file=sa.json # SA (leaked JSON = access)# a leaked service account JSON (see recon-code) is direct access# SA token from a VM (see cloud-metadata)Red Team
Section titled “Red Team”Enumeration
Section titled “Enumeration”gcloud auth list ; gcloud config listgcloud projects listgcloud projects get-iam-policy <project-id>gcloud iam service-accounts list --project <project-id># tools# gcp_scanner, GCPBucketBrute, Hayat, ScoutSuite (supports GCP)The heart: service account impersonation
Section titled “The heart: service account impersonation”# if you hold iam.serviceAccounts.getAccessToken over an SA -> you get its tokengcloud auth print-access-token \ --impersonate-service-account=<SA>@<project>.iam.gserviceaccount.com# iam.serviceAccountKeys.create -> create a key for an SA (persistence/access)gcloud iam service-accounts keys create k.json \ --iam-account=<SA>@<project>.iam.gserviceaccount.comIf the target SA is more privileged (Editor/Owner), you impersonate it and escalate. With iam.serviceAccounts.actAs + creating a resource (function/VM/job) that runs as that SA, you execute code with its permissions.
Typical escalation permissions
Section titled “Typical escalation permissions”resourcemanager.projects.setIamPolicy→ grant yourself project Owner.iam.serviceAccounts.getAccessToken/actAs→ impersonate a privileged SA.iam.serviceAccountKeys.create→ SA key (persistent access).cloudfunctions.functions.create+actAs→ function running as an admin SA (see Serverless Security).
Common services
Section titled “Common services”# Cloud Storage (see cloud-s3)gsutil ls ; gsutil ls gs://<bucket> # public/readable buckets# Compute Engine: project metadata (SSH keys, startup scripts)gcloud compute instances listgcloud compute project-info describe # sometimes credentials in metadata# Secret Managergcloud secrets listgcloud secrets versions access latest --secret=<name>Blue Team
Section titled “Blue Team”- Least privilege: avoid broad primitive roles (Owner/Editor); use scoped predefined roles.
- Control impersonation: restrict
iam.serviceAccounts.getAccessToken/actAs; disable SA key creation with the Organization Policyiam.disableServiceAccountKeyCreation. - SAs without static keys: use Workload Identity; never expose a service account JSON.
- Organization Policies to constrain (domain-restricted sharing, no SA keys, no public IPs).
- Cloud Audit Logs (Admin Activity + Data Access) + Security Command Center; alerts on
setIamPolicyandserviceAccountKeys.create.
Checklist
Section titled “Checklist”- Access (user / SA JSON / VM token) and enumeration (gcloud)
- IAM policy of projects and SAs (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 (Serverless Security)
- Map up to project/organization Owner