How the attack works
On Compute Engine VMs without OS Login, the guest environment creates local Linux accounts for the public keys listed in the ssh-keys metadata value. Keys in project metadata (common instance metadata) reach every VM in the project that does not block project-wide keys; keys in instance metadata reach one VM. Writing project metadata needs compute.projects.setCommonInstanceMetadata, which broad roles such as Owner, Editor and Compute Admin include.
Attackers add their own key for interactive access that is not tied to any Google identity, usually with administrative rights on the VM, and then read the attached service account's access token from the metadata server. The metadata write is the attributable event: Compute Engine logs it as an Admin Activity entry (v1.compute.projects.setCommonInstanceMetadata) but redacts the metadata in the request, so the entry shows who changed project metadata and the names of the metadata keys that changed (such as ssh-keys), not which SSH key was added. For writes to a single VM, one rule reads those names from protoPayload.metadata.instanceMetaData and fires only when the ssh-keys key is added or modified, the condition Google's Event Threat Detection finding for SSH keys uses.
What an attacker needs
compute.projects.setCommonInstanceMetadataon the project (orcompute.instances.setMetadatafor a single VM).- VMs without OS Login and without project-wide keys blocked, reachable over SSH (external IP, IAP TCP forwarding or a foothold in the VPC).
Impact
A shell on every VM that accepts project-wide keys and the access tokens of their service accounts, with SSH sessions that no Google identity accounts for.
Detection rules
Google Cloud VM Instance Metadata Changed
Detects writes to the metadata of a single Compute Engine VM (compute.instances.setMetadata, any API version), which can add instance-level SSH keys or switch OS Login off for that VM (Admin Activity audit log). The metadata itself is redacted in the logged request; protoPayload.metadata names the metadata keys that changed. Noisier than the project-wide rule; use it for VMs with privileged service accounts.
title: Google Cloud VM Instance Metadata Changed
id: 015daf8c-629d-45df-8ff4-49dec75bc2cd
status: experimental
description: >-
Detects writes to the metadata of a single Compute Engine VM
(compute.instances.setMetadata, any API version), which can add instance-level
SSH keys or switch OS Login off for that VM (Admin Activity audit log). The
metadata itself is redacted in the logged request; protoPayload.metadata
names the metadata keys that changed. Noisier than the project-wide rule; use
it for VMs with privileged service accounts.
references:
- https://docs.cloud.google.com/compute/docs/logging/audit-logging
- https://docs.cloud.google.com/compute/docs/connect/add-ssh-keys
author: Forestall
date: 2026-11-10
tags:
- attack.persistence
- attack.privilege-escalation
- attack.t1098.004
logsource:
product: gcp
service: gcp.audit
detection:
selection:
protoPayload.serviceName: compute.googleapis.com
protoPayload.methodName|endswith: compute.instances.setMetadata
condition: selection
falsepositives:
- Console and gcloud SSH key management when OS Login is off, startup-script updates and automation that manages instance metadata; scope the rule to sensitive VMs with protoPayload.resourceName or exclude automation by principalEmail.
level: low
False positives
- Console and gcloud SSH key management when OS Login is off, startup-script updates and automation that manages instance metadata; scope the rule to sensitive VMs with protoPayload.resourceName or exclude automation by principalEmail.
Google Cloud Project-Wide Compute Metadata Changed
Detects writes to project-wide Compute Engine metadata (compute.projects.setCommonInstanceMetadata, any API version), where project-wide SSH keys are stored (Admin Activity audit log). Compute Engine redacts the metadata in the logged request, so the rule matches every project metadata write; protoPayload.metadata in the entry names the metadata keys that changed, which tells an ssh-keys change from other changes during triage.
title: Google Cloud Project-Wide Compute Metadata Changed
id: 7c26ae5e-5f4d-456b-914b-1647074467da
status: experimental
description: >-
Detects writes to project-wide Compute Engine metadata
(compute.projects.setCommonInstanceMetadata, any API version), where
project-wide SSH keys are stored (Admin Activity audit log). Compute Engine
redacts the metadata in the logged request, so the rule matches every
project metadata write; protoPayload.metadata in the entry names the metadata
keys that changed, which tells an ssh-keys change from other changes during
triage.
references:
- https://docs.cloud.google.com/compute/docs/logging/audit-logging
- https://docs.cloud.google.com/compute/docs/connect/add-ssh-keys
- https://docs.cloud.google.com/security-command-center/docs/findings/threats/gce-admin-add-ssh-key
author: Forestall
date: 2026-11-10
tags:
- attack.persistence
- attack.privilege-escalation
- attack.t1098.004
logsource:
product: gcp
service: gcp.audit
detection:
selection:
protoPayload.serviceName: compute.googleapis.com
protoPayload.methodName|endswith: compute.projects.setCommonInstanceMetadata
condition: selection
falsepositives:
- Key management by the Google Cloud console or gcloud when OS Login is off, and project metadata changes (startup scripts, OS Login flags) made by infrastructure-as-code; exclude known pipeline identities by principalEmail.
level: medium
False positives
- Key management by the Google Cloud console or gcloud when OS Login is off, and project metadata changes (startup scripts, OS Login flags) made by infrastructure-as-code; exclude known pipeline identities by principalEmail.
SSH Keys Metadata Key Added or Changed on a Google Cloud VM
Detects a metadata write to a single Compute Engine VM (compute.instances.setMetadata, any API version) that adds or modifies the ssh-keys metadata key (Admin Activity audit log, always written). On a VM without OS Login, the guest environment creates a local account for every public key in that value. Compute Engine redacts the metadata values in the logged request, so the rule reads the names of the changed keys from protoPayload.metadata.instanceMetaData.addedMetadataKey and modifiedMetadataKey, the fields and the value ssh-keys that Google gives in the log filter of its Event Threat Detection finding Persistence, GCE Admin Added SSH Key. Google publishes no example entry, so check the fields against one of your own entries before relying on the rule. Unlike Google's finding, which only fires for VMs created more than seven days earlier, the rule also fires for new VMs. Writes to project metadata are matched by the project-wide rule on this page.
title: SSH Keys Metadata Key Added or Changed on a Google Cloud VM
id: 41d4cb05-7d69-44fc-ba36-0e941bf61456
status: experimental
description: >-
Detects a metadata write to a single Compute Engine VM
(compute.instances.setMetadata, any API version) that adds or modifies
the ssh-keys metadata key (Admin Activity audit log, always written). On a
VM without OS Login, the guest environment creates a local account for
every public key in that value. Compute Engine redacts the metadata values
in the logged request, so the rule reads the names of the changed keys from
protoPayload.metadata.instanceMetaData.addedMetadataKey and
modifiedMetadataKey, the fields and the value ssh-keys that Google gives in
the log filter of its Event Threat Detection finding Persistence, GCE Admin
Added SSH Key. Google publishes no example entry, so check the fields
against one of your own entries before relying on the rule. Unlike Google's
finding, which only fires for VMs created more than seven days earlier,
the rule also fires for new VMs. Writes to project metadata are matched by
the project-wide rule on this page.
references:
- https://docs.cloud.google.com/security-command-center/docs/findings/threats/gce-admin-add-ssh-key
- https://docs.cloud.google.com/compute/docs/logging/audit-logging
- https://docs.cloud.google.com/compute/docs/connect/add-ssh-keys
- https://attack.mitre.org/techniques/T1098/004/
author: Forestall
date: 2026-11-18
tags:
- attack.persistence
- attack.privilege-escalation
- attack.t1098.004
logsource:
product: gcp
service: gcp.audit
detection:
selection:
protoPayload.serviceName: compute.googleapis.com
protoPayload.methodName|endswith: compute.instances.setMetadata
key_added:
protoPayload.metadata.instanceMetaData.addedMetadataKey|contains: ssh-keys
key_modified:
protoPayload.metadata.instanceMetaData.modifiedMetadataKey|contains: ssh-keys
condition: selection and 1 of key_*
falsepositives:
- SSH key management by the console or gcloud on VMs that do not use OS Login, which writes the ssh-keys value when a user connects for the first time. Exclude those users by principalEmail only where OS Login cannot be enforced, and give priority to changes on VMs that have existed for more than a few days and run with privileged service accounts.
- Deployment tools that set SSH keys when they create a VM. Exclude the pipeline identity by principalEmail in a filter_* selection.
level: medium
False positives
- SSH key management by the console or gcloud on VMs that do not use OS Login, which writes the ssh-keys value when a user connects for the first time. Exclude those users by principalEmail only where OS Login cannot be enforced, and give priority to changes on VMs that have existed for more than a few days and run with privileged service accounts.
- Deployment tools that set SSH keys when they create a VM. Exclude the pipeline identity by principalEmail in a filter_* selection.
Triage
- Read protoPayload.metadata in the entry, which lists the names of the metadata keys that were added or modified, to tell an ssh-keys change from a startup-script or flag change. Then identify the caller and compare the project's current ssh-keys metadata with its last known state; the audit entry does not contain the key itself.
- Look for new local accounts and SSH logins on the project's VMs after the change, and for API calls by their service accounts.
- Compute Engine changes run as long-running operations and can write a start and a completion entry for one change.
Response
Once the attack is confirmed:
- Remove the unknown key from the
ssh-keysvalue in project metadata, and check the instance metadata of each VM for keys added the same way. - Treat every VM that accepts project-wide keys as accessed. Removing the key from metadata does not undo what was done in a shell with administrative rights, so examine those VMs for added accounts and other changes, or rebuild them from a trusted image.
- Assume that the access tokens of the service accounts attached to those VMs were read. Such tokens cannot be revoked and remain valid until they expire, by default up to 60 minutes. Review the API calls of those service accounts, and disable a privileged one for that time.
- Remove the metadata write permissions (
compute.projects.setCommonInstanceMetadata,compute.instances.setMetadata) from the identity that made the change, and contain that identity. - After the cleanup, compare the
ssh-keysmetadata of the project and its VMs with the last known good state again, and watch for a new metadata write by any caller.
Mitigation
- Enforce OS Login with the
constraints/compute.requireOsLoginorganization policy, so that metadata keys are ignored. - Block project-wide SSH keys on VMs that must keep metadata-based keys.
- Limit the roles that can write project or instance metadata, and attach minimally privileged service accounts to VMs.
Frequently asked questions
Why does the audit entry not show which SSH key was added?
Compute Engine redacts the metadata in the logged request. The entry shows who changed project metadata and, in `protoPayload.metadata`, the names of the metadata keys that changed, such as `ssh-keys`. To find the key itself, compare the project's current `ssh-keys` value with its last known state.
Does the project metadata rule fire only when an SSH key is added?
No. Because the metadata is redacted in the log, the rule matches every write to project-wide metadata, including startup script and OS Login flag changes. The names of the changed metadata keys in `protoPayload.metadata` tell an `ssh-keys` change from the others during triage.
Which VMs does a key in project metadata reach?
Every VM in the project that does not use OS Login and does not block project-wide SSH keys, provided it can be reached over SSH. On VMs with OS Login enforced, keys in metadata are ignored. A key in instance metadata reaches only that one VM.
Why are SSH sessions that use a metadata key hard to attribute?
Access through a metadata key is not tied to any Google identity. The guest environment creates a local Linux account for the key, and the SSH session is authenticated on the VM itself. The metadata write is therefore the attributable event in Cloud Audit Logs, and what happened afterwards has to be read from the VMs and from the API calls of their service accounts.