How the attack works
Every Compute Engine VM can have a service account attached. Applications on the VM get access tokens for that account from the metadata server without any key, and act with the account's roles. The account is chosen when the VM is created and can be replaced later with the setServiceAccount method, which Google documents as requiring the VM to be stopped first. Attaching an account needs iam.serviceAccounts.actAs on it, which the Service Account User role contains. When no account is named, a new VM uses the Compute Engine default service account, which might hold the Editor role on the project.
The escalation is the same as for other resources that run as a service account. A principal who can create a VM, or change one they can log on to, and who can attach an account with more access than their own, obtains that account's access by reading a token on the VM. Google's guidance on service accounts describes attaching as a way to gain access a user would not have otherwise. The traces are Admin Activity audit entries, which are always written. Google's example of the v1.compute.instances.insert entry shows the caller and the attached account in request.serviceAccounts, v1.compute.instances.setServiceAccount records a replacement, and IAM writes an iam.serviceAccounts.actAs entry that names the account and the principal who attached it.
What an attacker needs
- Permission to create a VM, or
compute.instances.setServiceAccountandcompute.instances.stopon an existing VM. iam.serviceAccounts.actAson a service account that has more access than the caller, for example through the Service Account User role granted on a whole project.- A way to run code on the VM, such as SSH access or a startup script.
Impact
The roles of the attached service account, used from the VM with tokens that the metadata server issues on request. With the default service account and the Editor role that is most of the project. With an account that can change allow policies or create service account keys it becomes lasting control. Access continues for as long as the VM exists with that account attached.
Detection rules
Service Account of a Compute Engine VM Changed
Detects compute.instances.setServiceAccount calls in any API version (Admin Activity audit log, always written). The call replaces the service account, or the access scopes, of an existing VM. Code on the VM then obtains tokens for the new account from the metadata server, so the call can move a machine somebody controls under an identity with more access. Google documents that the VM has to be stopped first and that the caller needs compute.instances.setServiceAccount, and attaching an account needs iam.serviceAccounts.actAs on it. Google documents the method name and the log type but publishes no example of the entry, so the rule matches on the method name alone and does not read which account was attached or whether only the scopes changed. The VM is named in protoPayload.resourceName. The account is in the iam.serviceAccounts.actAs entry that the same caller produces, and in the VM's current configuration. Google marks the method as a long-running operation, which usually writes a start and a completion entry.
title: Service Account of a Compute Engine VM Changed
id: fdde9f2f-9878-407f-8f68-803f3794adbb
status: experimental
description: >-
Detects compute.instances.setServiceAccount calls in any API version
(Admin Activity audit log, always written). The call replaces the service
account, or the access scopes, of an existing VM. Code on the VM then
obtains tokens for the new account from the metadata server, so the call
can move a machine somebody controls under an identity with more access.
Google documents that the VM has to be stopped first and that the caller
needs compute.instances.setServiceAccount, and attaching an account needs
iam.serviceAccounts.actAs on it. Google documents the method name and the
log type but publishes no example of the entry, so the rule matches on
the method name alone and does not read which account was attached or
whether only the scopes changed. The VM is named in
protoPayload.resourceName. The account is in the iam.serviceAccounts.actAs
entry that the same caller produces, and in the VM's current
configuration. Google marks the method as a long-running operation, which
usually writes a start and a completion entry.
references:
- https://docs.cloud.google.com/compute/docs/logging/audit-logging
- https://docs.cloud.google.com/compute/docs/instances/change-service-account
- https://docs.cloud.google.com/iam/docs/attach-service-accounts
- https://docs.cloud.google.com/iam/docs/audit-logging/examples-service-accounts
author: Forestall
date: 2026-11-18
tags:
- attack.privilege-escalation
- attack.t1548.005
logsource:
product: gcp
service: gcp.audit
detection:
selection:
protoPayload.serviceName: compute.googleapis.com
protoPayload.methodName|endswith: compute.instances.setServiceAccount
condition: selection
falsepositives:
- Planned moves of a VM from the default service account to a dedicated one, and changes of access scopes during maintenance. Exclude the deployment pipeline by principalEmail in a filter_* selection and review every change made by a person.
level: medium
False positives
- Planned moves of a VM from the default service account to a dedicated one, and changes of access scopes during maintenance. Exclude the deployment pipeline by principalEmail in a filter_* selection and review every change made by a person.
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.
VM created by a person with a service account from the privileged list
The insert entry names both the person who created the VM and the service account it runs as. Compared with a list of privileged service accounts and a list of deployment identities, it shows a person putting a machine they control under a privileged identity.
- An entry from
compute.googleapis.comwhoseprotoPayload.methodNameends withcompute.instances.insert. Google's example gives the attached account inprotoPayload.request.serviceAccounts[0].emailand the caller inprotoPayload.authenticationInfo.principalEmail. - Compare the account with your list of privileged service accounts, and the caller with your deployment identities.
- Alert when the account is on the first list and the caller is not on the second. An
iam.serviceAccounts.actAsentry of the same caller at the same time, whoserequest.nameis the same account, confirms the attachment.
Eventsv1.compute.instances.insertiam.serviceAccounts.actAs
LimitsWithout a list of privileged service accounts the result is every VM creation. Google marks the insert method as a long-running operation, which usually writes a start and a completion entry, and its example shows the first one. VMs created from an instance template take their account from the template.
VM stopped, its service account replaced and the VM started again by one principal
Google documents that a VM has to be stopped before its service account can be changed. A stop, a replacement and a start of the same VM by the same caller within a short time is the complete sequence of moving a machine under another identity.
- An entry whose
protoPayload.methodNameends withcompute.instances.stop. Note the VM inprotoPayload.resourceNameand the caller inprotoPayload.authenticationInfo.principalEmail. - Within 30 minutes a match of the rule on this page,
compute.instances.setServiceAccount, by the same caller for the same VM. - Then an entry ending with
compute.instances.startfor the same VM. After the start, collect the audit entries whose caller is the newly attached service account.
Eventscompute.instances.stopcompute.instances.setServiceAccountcompute.instances.start
LimitsPlanned maintenance that moves a VM to a dedicated service account produces the same sequence, so the account that was attached decides. Google does not document which field of the setServiceAccount entry names the new account, so read it from the VM. The 30 minutes are a starting point.
Triage
- Identify the caller (protoPayload.authenticationInfo.principalEmail) and the VM in protoPayload.resourceName, and find the service account that was attached. For a new VM it is in request.serviceAccounts of the insert entry. For a change, read the VM's current configuration and the iam.serviceAccounts.actAs entry of the same caller at the same time.
- Check what that account can do, and whether the caller could already do the same without it.
- Check who can log on to the VM or set its metadata, and whether the caller did so after the change.
- Look for audit entries with the service account as caller that start after the VM was created or restarted, beginning with allow policy changes and service account key creation.
- If the change is not approved, treat the service account's access as used and continue with the response steps.
Response
Once the attack is confirmed:
- Stop the VM and detach the service account, or restore the account it had before. Delete a VM that was created only for this purpose after preserving what the investigation needs.
- Deal with tokens that were already issued. Google states that service account access tokens stay valid until they expire, by default up to 60 minutes, and cannot be revoked. If the account is privileged, disable it or apply a deny policy until they have expired, after checking which other workloads depend on it.
- Review the audit entries that have the service account as caller since the attachment, and remove what it created, such as service account keys, role bindings, service accounts and other workloads.
- Contain the principal that attached the account. Reset its credentials and remove its Service Account User role on the account and its rights on the VM.
- Check the other VMs in the project for service accounts that nobody assigned.
Mitigation
- Grant the Service Account User role on individual service accounts, not on a project or a folder, so that a principal can attach only the accounts meant for their workloads.
- Attach a user-managed service account with minimal roles to every VM, and keep privileged service accounts out of projects where many people can create VMs.
- Enforce the organization policy constraint
iam.automaticIamGrantsForDefaultServiceAccounts, so that the Compute Engine default service account does not receive the Editor role automatically. - Limit
compute.instances.setServiceAccountto the deployment pipeline. Changing the account of a running workload is rare outside planned maintenance. - Keep the constraint
iam.disableCrossProjectServiceAccountUsageenforced, which is its default, so that a service account cannot be attached to a VM in another project.
Frequently asked questions
Which audit entries show a service account being attached to a VM?
Three. Google's example of the `v1.compute.instances.insert` entry shows the caller in `authenticationInfo.principalEmail` and the attached account in `request.serviceAccounts[0].email`. A later replacement is a `v1.compute.instances.setServiceAccount` entry. In both cases IAM also writes an `iam.serviceAccounts.actAs` entry with the account and the principal, and Google's example of that entry is for a Compute Engine instance. All three are Admin Activity entries.
Why is there no rule for VM creation on this page?
Every new VM writes the same insert entry, and the attached account sits in a list inside the request, which a portable rule cannot read the same way in every SIEM. A rule on the method name alone would fire on every VM. The page therefore has a rule for the rare replacement of an account and describes the creation case as detection logic that joins the insert entry with the attachment entry.
Can the service account of a running VM be changed?
Not while it runs. Google documents that the VM has to be stopped before its service account and access scopes can be changed, and lists `compute.instances.setServiceAccount` and `compute.instances.stop` as the permissions needed. A stop, a `setServiceAccount` call and a start of the same VM by one caller is therefore the full sequence of a replacement.
How does the attacker use the attached account?
Through the VM. Applications on a VM obtain access tokens for the attached account from the metadata server, with no key involved. Anyone who can run code on the VM, by SSH, a startup script or other means, can request a token and call Google Cloud APIs as the account. Those calls appear in the audit logs with the service account as caller.
References
- MITRE ATT&CK T1548.005: Temporary Elevated Cloud Access
- Example logs for service accounts (logs for setting up a Compute Engine instance to run as a service account)
- Compute Engine audit logging
- Create a VM that uses a user-managed service account
- Change the service account and access scopes for a VM
- Attach service accounts to resources (the actAs permission and its audit logs)
- Best practices for using service accounts
- Respond to compromised Google Cloud credentials