1. Resources
  2. Detection rules
  3. Startup or Shutdown Script Added to Compute Engine Metadata

Startup or Shutdown Script Added to Compute Engine Metadata detection rules

An attacker who can write Compute Engine metadata sets a startup or shutdown script on a VM or on the whole project, so that their code runs as root at the next boot or shutdown with the access of the VM's service account.

Platform
Google Cloud
Severity
High
Log source
Google Cloud Audit Logs
Last updated
Rule formats
Sigmaand queries for: SplunkMicrosoft SentinelIBM QRadar
What an attacker needs
The right to write the metadata of a VM or of the project, and a reboot, a stop or a new VM so that the script runs.
Impact
Code execution as root on the VM, or on every VM of the project, with the access of the attached service account and again at every boot.
Response
Remove the script key before the next reboot, and rebuild any VM that has booted or stopped since the change from a trusted image.
Mitigation
Limit the roles that can write VM and project metadata, and attach service accounts with minimal roles to VMs.
API method
*compute.instances.setMetadata

How the attack works

Compute Engine runs scripts that are stored in metadata. The value of the startup-script key, or the file that startup-script-url points to, is copied to the VM and run as the root user every time the VM boots. shutdown-script and shutdown-script-url run as root on Linux and as the System account on Windows when the VM stops or restarts, and Windows VMs have their own keys such as windows-startup-script-ps1. The script can be set on one VM or in project metadata, where Google documents that it applies to all VMs in the project unless a VM has its own.

For an attacker this turns a control plane permission into code execution inside the guest. Whoever holds compute.instances.setMetadata on a VM, or compute.projects.setCommonInstanceMetadata on the project, does not need to log on to the machine. They set the key and wait for a reboot, or cause one. The script runs with full rights on the VM and can use the permissions of the attached service account, which Google itself names as a privilege escalation risk for startup scripts. Because the key stays in metadata, the script runs again at every boot until someone removes it. Compute Engine writes the metadata change to the Admin Activity audit log but redacts the metadata values, so the entry shows who changed which keys and not what the script contains. Google's Event Threat Detection has three findings for this, one for a VM and two for project-wide startup and shutdown scripts.

What an attacker needs

  • compute.instances.setMetadata on a VM or compute.projects.setCommonInstanceMetadata on the project, which broad roles such as Owner, Editor and Compute Admin include.
  • A reboot, a stop or a new VM in the project. A startup script runs at boot, a shutdown script when the VM stops or restarts.
  • For the escalation, a VM whose attached service account has more access than the caller.

Impact

Code execution as root, or as the System account on Windows, on the affected VM or on every VM in the project that does not have its own script. From there the attacker reads the access tokens of the attached service account from the metadata server and acts with its roles, reads data on the disk and installs anything else. The script is a persistence mechanism as well, since it runs at every boot for as long as the key exists.

Detection rules

Startup or Shutdown Script Key Added or Changed in VM Metadata

Detects a metadata write to a single Compute Engine VM (compute.instances.setMetadata, any API version) that adds or modifies a startup or shutdown script key (Admin Activity audit log, always written). Compute Engine runs these scripts as root, or as the System account on Windows, at the next boot or shutdown, with the access of the VM's service account. Compute Engine redacts the metadata values in the logged request. The rule reads the names of the changed keys from protoPayload.metadata.instanceMetaData.addedMetadataKey and modifiedMetadataKey, the fields Google gives in the response steps of its Event Threat Detection finding for startup scripts. Google shows these fields in a log filter and publishes no example entry, so check them against one of your own entries before relying on the rule. The values match startup-script, startup-script-url, shutdown-script, shutdown-script-url and the Windows keys that contain those names. Unlike Google's finding, the rule also fires for newly created VMs. Writes to project metadata are not matched. See the rule for project-wide metadata on the page about SSH keys in project metadata.

LevelMediumStatusExperimentalLog sourceGoogle Cloud Audit Logs
title: Startup or Shutdown Script Key Added or Changed in VM Metadata
id: 155f8592-4929-40f0-a187-25e5c262649e
status: experimental
description: >-
  Detects a metadata write to a single Compute Engine VM
  (compute.instances.setMetadata, any API version) that adds or modifies a
  startup or shutdown script key (Admin Activity audit log, always
  written). Compute Engine runs these scripts as root, or as the System
  account on Windows, at the next boot or shutdown, with the access of the
  VM's service account. Compute Engine redacts the metadata values in the
  logged request. The rule reads the names of the changed keys from
  protoPayload.metadata.instanceMetaData.addedMetadataKey and
  modifiedMetadataKey, the fields Google gives in the response steps of its
  Event Threat Detection finding for startup scripts. Google shows these
  fields in a log filter and publishes no example entry, so check them
  against one of your own entries before relying on the rule. The values
  match startup-script, startup-script-url, shutdown-script,
  shutdown-script-url and the Windows keys that contain those names. Unlike
  Google's finding, the rule also fires for newly created VMs. Writes to
  project metadata are not matched. See the rule for project-wide metadata
  on the page about SSH keys in project metadata.
references:
  - https://docs.cloud.google.com/security-command-center/docs/findings/threats/gce-admin-add-startup-script
  - https://docs.cloud.google.com/compute/docs/logging/audit-logging
  - https://docs.cloud.google.com/compute/docs/instances/startup-scripts/linux
  - https://docs.cloud.google.com/compute/docs/shutdownscript
author: Forestall
date: 2026-11-18
tags:
  - attack.persistence
  - attack.privilege-escalation
  - attack.t1037
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:
      - startup-script
      - shutdown-script
      - sysprep-specialize-script
  key_modified:
    protoPayload.metadata.instanceMetaData.modifiedMetadataKey|contains:
      - startup-script
      - shutdown-script
      - sysprep-specialize-script
  condition: selection and 1 of key_*
falsepositives:
  - Deployment tools and infrastructure-as-code runs that set or update startup scripts as part of a release. Exclude the pipeline identity by principalEmail in a filter_* selection and review script changes made by people, above all on VMs with privileged service accounts.
level: medium

False positives

  • Deployment tools and infrastructure-as-code runs that set or update startup scripts as part of a release. Exclude the pipeline identity by principalEmail in a filter_* selection and review script changes made by people, above all on VMs with privileged service accounts.

Detection logic

Patterns that need more than one event, a baseline or knowledge of your own environment. Each is a hypothesis to build and tune in your SIEM, not a ready rule.

Project-wide metadata write that adds or changes a script key

Detection hypothesisCompares with an earlier value

A script in project metadata reaches every VM of the project, so it is the more valuable target, but Google documents no field that names the changed keys for a project metadata write. Comparing the project's script keys before and after each write finds the change that the rule on single VMs cannot see.

  1. An entry from compute.googleapis.com whose protoPayload.methodName ends with compute.projects.setCommonInstanceMetadata. Note the project in protoPayload.resourceName and the caller.
  2. Read the project's current metadata and compare the keys startup-script, startup-script-url, shutdown-script, shutdown-script-url and the Windows script keys with the last state you stored for that project.
  3. Alert when one of these keys is new or its value changed, and store the new state for the next comparison.

Eventscompute.projects.setCommonInstanceMetadata

LimitsIt needs a stored copy of each project's metadata and a job that reads it after every write. The audit entry itself cannot be used for the comparison, because the metadata values are redacted. A change of the object behind a URL key does not write a metadata entry at all.

Script key set on a VM and the VM restarted soon after by the same principal

Detection hypothesisSequence of events

A startup script does nothing until the VM boots. An attacker who does not want to wait restarts the VM. A script key change followed by a stop, start or reset of the same VM by the same caller is the change being made to take effect.

  1. A match of the rule on this page. Note the VM in protoPayload.resourceName and the caller in protoPayload.authenticationInfo.principalEmail.
  2. Within 60 minutes an entry by the same caller for the same VM whose protoPayload.methodName ends with compute.instances.stop, compute.instances.reset or compute.instances.start.
  3. After the restart, collect the audit entries whose caller is the service account attached to that VM.

Eventscompute.instances.setMetadatacompute.instances.stopcompute.instances.resetcompute.instances.start

LimitsEngineers who change a startup script restart the VM to test it, so the content of the script decides. A restart from inside the guest is not an API call and is not seen. The 60 minutes are a starting point.

Triage

  • Read protoPayload.metadata in the entry for the names of the keys that were added or modified, and identify the caller (protoPayload.authenticationInfo.principalEmail) and the VM or project in protoPayload.resourceName.
  • Read the current value of the script key on the VM or in project metadata, since the audit entry does not contain it. For a URL key, read the object it points to and check who can write to it.
  • Check whether the caller normally manages this VM, and whether the change came from the deployment pipeline.
  • Check whether the VM was restarted or stopped after the change, which decides whether the script has already run, and by whom.
  • Find the service account attached to the VM and look for audit entries with that account as caller after the reboot, starting with allow policy changes and service account key creation.

Response

Once the attack is confirmed:

  1. Remove the script key from the VM or from project metadata, or restore the approved script, before the next reboot.
  2. If the VM has booted or stopped since the change, treat it as compromised. Preserve what is needed for investigation and rebuild it from a trusted image.
  3. Treat the attached service account as used by the attacker. Review its activity since the reboot and remove what it created, such as keys, role bindings and resources.
  4. For a project-wide script, identify every VM that started or stopped while the key was present and handle each of them the same way.
  5. Remove the metadata write permissions from the identity that made the change, and contain that identity.

Mitigation

  • Limit the roles that include compute.instances.setMetadata and compute.projects.setCommonInstanceMetadata, and keep the Editor basic role out of production projects.
  • Attach a user-managed service account with minimal roles to every VM, so that code running on the VM gains little from its identity.
  • Manage startup scripts through reviewed infrastructure-as-code and bake them into images where possible, so that a script key in metadata that the pipeline did not set stands out.
  • Protect the Cloud Storage objects that startup-script-url keys point to. Google warns that a script object that is less protected than the metadata lets someone change the script without touching the VM.
  • Review project metadata regularly. A project-wide script reaches every VM, including ones created later.

Frequently asked questions

Does the audit log show the script that was added?

No. Google documents that for `instance.setMetadata` and `project.setCommonInstanceMetadata` requests the metadata portion of the request body is redacted. The entry shows the caller, the VM or project and, in `protoPayload.metadata`, the names of the keys that changed. The script itself has to be read from the metadata of the VM or the project, or from the object a URL key points to.

Why does the rule cover only single VMs and not project metadata?

Google's finding documentation gives the fields that name the changed keys for a VM, `protoPayload.metadata.instanceMetaData.addedMetadataKey` and `modifiedMetadataKey`. It gives no such field for project metadata. A project metadata write is therefore covered by the rule Google Cloud Project-Wide Compute Metadata Changed on the page about SSH keys in project metadata, which matches every write, and by the detection logic on this page.

When does a script in metadata actually run?

Google documents that a startup script runs as the root user when the VM boots, and that shutdown scripts run as root on Linux and as the System account on Windows during the limited shutdown period, for both shutdown and reboot. Setting the key does nothing until then. An attacker either waits or restarts the VM, which is a second audit entry worth looking for.

Which technique does Google assign to this?

Google's finding for a project-wide startup script points to the MITRE ATT&CK technique Boot or Logon Initialization Scripts, which this page uses. The finding for a single VM fires only for VMs created more than seven days earlier, to leave out scripts set while a VM is being built. The rule on this page has no such age condition and also fires for new VMs.

References

We respect your privacy

We use cookies to keep this site secure and working properly. With your permission, we also use optional cookies to understand usage and improve the experience. Cookie Policy

You can change your choice at any time.