GCP pentest: project and IAM enumeration
Brief
A Google Cloud project is mostly its IAM policy: who holds which role, and which service accounts those roles can act as. The first hour reads that policy, the service accounts behind the compute, and the buckets, and stops where acting as a service account begins.
On this page
- Situation
- One service account key file or one user sign-in for a project named in the scope letter. Every command is a read, the key is used only to read, and nothing here creates a key, a binding, an instance or an object. Metadata reads from inside a VM happen only on an instance the letter names.
- Goal
- A map of the project: every role binding, every service account with its keys and who can act as it, the identity each instance runs as, and each bucket's exposure.
- Outputs
- Representative examples on placeholder hosts, trimmed to the lines that matter. Not captured from a public fixture yet.
Check first
- Which identity the key or sign-in is, and whether the key is user-managed, in which case its age is the first finding.
- The project number as well as the id, because the default service accounts are named after it.
- Whether the organization enforces the constraint that stops default service accounts getting Editor; organizations created before May 2024 usually did not.
- Which Data Access audit logs are on, which the client answers, because the reads on this page are logged only when they are.
- The instances, if any, the letter lets us query from the inside.
Steps
GCP
Who am I in this project, and what can I actually do?
gcloud auth activate-service-account --key-file=deploy-key.json
gcloud config set project example-project-123456
gcloud projects describe example-project-123456 --format="value(projectNumber,parent.type,parent.id)"
gcloud iam service-accounts keys list --iam-account=deploy@example-project-123456.iam.gserviceaccount.com --managed-by=user
curl -s -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" \
"https://cloudresourcemanager.googleapis.com/v1/projects/example-project-123456:testIamPermissions" \
-d '{"permissions":["resourcemanager.projects.getIamPolicy","resourcemanager.projects.setIamPolicy","iam.serviceAccounts.actAs","iam.serviceAccountKeys.create","iam.serviceAccounts.getAccessToken","compute.instances.create","compute.instances.setMetadata","storage.buckets.list"]}'
Expected output
$ gcloud projects describe example-project-123456 --format="value(projectNumber,parent.type,parent.id)"
123456789012 organization <org-id>
$ gcloud iam service-accounts keys list --iam-account=deploy@example-project-123456.iam.gserviceaccount.com --managed-by=user
KEY_ID CREATED_AT EXPIRES_AT DISABLED DISABLE_REASON EXTENDED_STATUS
3f9a0c1e7b2d4e5f6a7b8c9d0e1f2a3b4c5d6e7f 2022-04-14T08:31:07Z 9999-12-31T23:59:59Z # <- a four year old key that never expires
$ curl ... :testIamPermissions -d '{"permissions":[...]}'
{
"permissions": [
"resourcemanager.projects.getIamPolicy",
"iam.serviceAccounts.actAs", # <- can run workloads as service accounts it is bound to
"compute.instances.create",
"storage.buckets.list"
]
}
testIamPermissions needs no permission of its own and answers only for the permissions you name, returning the subset the caller holds, so we ask about the ones that matter: setIamPolicy is ownership in all but name, actAs with instances.create runs code as any service account the caller may act as, and serviceAccountKeys.create or getAccessToken mints credentials for one. A 9999 expiry is normal for user-managed keys; the creation date is what counts, since a key that old has lived through every laptop and pipeline since.
Negative result. No user-managed keys on the account, or a key days old, and a permissions list that comes back holding only reads.
Fix. Enforce constraints/iam.managed.disableServiceAccountKeyCreation and move pipelines to workload identity federation. Where a key must exist, rotate it on a schedule and give iam.serviceAccounts.actAs on named service accounts, never at project level.
Who holds which role, and which service accounts can be acted as?
gcloud projects get-iam-policy example-project-123456 --flatten="bindings[].members" --format="table(bindings.role,bindings.members)"
gcloud iam service-accounts list --format="table(email,disabled)"
gcloud iam service-accounts get-iam-policy 123456789012-compute@developer.gserviceaccount.com \
--flatten="bindings[].members" --format="table(bindings.role,bindings.members)"
Expected output
$ gcloud projects get-iam-policy example-project-123456 --flatten="bindings[].members" --format="table(bindings.role,bindings.members)"
ROLE MEMBERS
roles/editor serviceAccount:123456789012-compute@developer.gserviceaccount.com # <- the default compute account is Editor
roles/editor serviceAccount:example-project-123456@appspot.gserviceaccount.com
roles/iam.serviceAccountUser user:tester@example.com # <- at project level: act as every service account above
roles/owner group:cloud-admins@example.com
roles/viewer user:tester@example.com
The two default accounts are the Compute Engine one, PROJECT_NUMBER-compute@developer.gserviceaccount.com, and the App Engine one, PROJECT_ID@appspot.gserviceaccount.com. Depending on the organization's policy they are granted Editor when they are created, and organizations made after 3 May 2024 enforce the constraint that stops it by default; older ones often never caught up. roles/iam.serviceAccountUser at project level plus compute.instances.create is the classic edge: start an instance as the default account and the caller is Editor. roles/iam.serviceAccountTokenCreator is the same edge without the instance. We mark those edges and stop; starting an instance or minting a token is not part of this page.
Negative result. No default service account holding Editor, serviceAccountUser and serviceAccountTokenCreator bound on individual service accounts rather than the project, and no allUsers or allAuthenticatedUsers anywhere in the policy.
Fix. Enforce constraints/iam.automaticIamGrantsForDefaultServiceAccounts, remove Editor from the default accounts and give each workload its own service account with the roles it uses. Bind the act-as roles on the service account resource, not the project, and work through the IAM recommender's role recommendations, which name the permissions a binding has not used.
Which identity does each instance run as, and what sits in the metadata?
gcloud compute instances list --format="json(name,zone,serviceAccounts)"
gcloud compute project-info describe --format="value(commonInstanceMetadata.items[].key)"
gcloud compute instances describe api-1 --zone us-central1-a --format="value(metadata.items[].key)"
Expected output
$ gcloud compute instances list --format="json(name,zone,serviceAccounts)"
[
{
"name": "api-1",
"serviceAccounts": [
{
"email": "123456789012-compute@developer.gserviceaccount.com",
"scopes": [
"https://www.googleapis.com/auth/cloud-platform" # <- the default Editor account with the full scope
]
}
],
"zone": "https://www.googleapis.com/compute/v1/projects/example-project-123456/zones/us-central1-a"
}
]
$ gcloud compute project-info describe --format="value(commonInstanceMetadata.items[].key)"
enable-oslogin;ssh-keys;startup-script # <- a project-wide startup script: ask before reading it, it often holds secrets
Every instance runs as a service account, and an attacker on the box holds that account's token. We map which account each instance uses and with which scopes, then list metadata keys only, because startup scripts and custom attributes are where database passwords end up and we do not want them in our terminal. From inside an instance the letter names, two reads confirm the identity without touching the token:
curl -s "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/email" -H "Metadata-Flavor: Google"
curl -s "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/scopes" -H "Metadata-Flavor: Google"
Expected output
123456789012-compute@developer.gserviceaccount.com # <- this VM speaks as the project's Editor
https://www.googleapis.com/auth/cloud-platform
Negative result. A dedicated service account per instance with narrow roles, no ssh-keys or startup-script at project level, and enable-oslogin set to true.
Fix. One service account per workload with the roles it needs; access scopes are a legacy control and cloud-platform with a narrow role is safer than narrow scopes on an Editor account. Enforce constraints/compute.requireOsLogin, block project-wide SSH keys, and keep secrets in Secret Manager rather than metadata.
Which buckets are public, or open to any Google account?
gcloud storage ls
gcloud storage buckets describe gs://example-exports --raw --format="json(iamConfiguration)"
gcloud storage buckets get-iam-policy gs://example-exports --format="json(bindings)"
Expected output
$ gcloud storage buckets describe gs://example-exports --raw --format="json(iamConfiguration)"
{
"iamConfiguration": {
"publicAccessPrevention": "inherited", # <- not enforced, so a binding can make the bucket public
"uniformBucketLevelAccess": {
"enabled": false # <- object ACLs also apply; the policy is not the whole story
}
}
}
$ gcloud storage buckets get-iam-policy gs://example-exports --format="json(bindings)"
{
"bindings": [
{
"members": [
"allUsers" # <- public
],
"role": "roles/storage.objectViewer"
}
]
}
allUsers is anyone on the internet; allAuthenticatedUsers is anyone with a Google account, which is the one that gets forgotten. Where uniform bucket-level access is off, object ACLs grant access too, so a clean policy can still sit on public objects; the client checks that from the console, or we fetch one object URL unauthenticated with written permission.
Negative result. publicAccessPrevention enforced and uniform access enabled on every bucket, and no allUsers or allAuthenticatedUsers binding.
Fix. Enforce constraints/storage.publicAccessPrevention and constraints/storage.uniformBucketLevelAccess at the organization, remove the two public members from every binding, and serve anything meant to be public through a bucket made for that alone.
The defender sees
Admin Activity audit logs are always written and cannot be turned off, but nothing on this page is an admin write. Reading the IAM policy, listing service accounts, instances and buckets are ADMIN_READ operations that land in the Data Access log, and Data Access logs are off by default for everything but BigQuery. With defaults, the first hour leaves no record at all, and the metadata reads inside a VM never leave the host. Where the client has turned them on, every entry carries principalEmail, the caller's address and, for a key, serviceAccountKeyName, so a deploy key reading IAM from a new address is a detection:
gcloud logging read 'logName:"cloudaudit.googleapis.com" AND protoPayload.authenticationInfo.principalEmail="deploy@example-project-123456.iam.gserviceaccount.com"' \
--limit=20 --format="table(timestamp,protoPayload.methodName,protoPayload.requestMetadata.callerIp)"
If that query returns nothing after the test, the first fix is ADMIN_READ Data Access logs for IAM, Resource Manager, Compute and Storage, with a sink to a project the workload owners cannot touch.
Done when
The caller's effective permissions are recorded, every binding and service account is listed with the escalation edges marked, instances are mapped to service accounts and scopes, buckets are classed public or not, and the audit log configuration is known.
Not covered
Acting as or impersonating a service account (see GCP service account impersonation), and organization and folder policy (see GCP organization policy); the outputs shown are representative examples on a placeholder project, not captured runs.