1. Resources
  2. Identity Threats
  3. Google Cloud

Google Cloud Service Account Key Creation and Theft

Also known as: GCP service account key persistence, iam.serviceAccountKeys.create, Service account JSON key theft, Service account key upload backdoor

An attacker who can create or upload a key for a service account, or who finds an existing key file, holds a long-lived credential for that account that works from anywhere until someone deletes it.

Platform
Google Cloud
Impact
High
Last updated
Attack difficulty
Low
Detection difficulty
Medium
Privileges required: Specific rights or roles
iam.serviceAccountKeys.create on the target (Service Account Key Admin, Editor or a custom role). For theft: read access to a disk, repository, pipeline or bucket where a key file is stored.
What the attacker gains
A private key that authenticates as the service account from any network, with all its roles. By default the key never expires.
Threat groups
Storm-0501APT28APT29APT3APT33APT38APT5Ember BearFIN13Fox KittenHAFNIUMIndrik SpiderKe3changKimsukyand 15 more in MITRE ATT&CKGroups that MITRE ATT&CK records using T1098.001, T1552.004, T1552.001, T1078.004.

How it works

A service account can authenticate in two ways. Google can issue short-lived credentials for it, through an attached workload or through impersonation. Or someone can create a user-managed key: an RSA key pair whose private half is downloaded as a JSON file and from then on exists outside Google's custody. Whoever holds the file can sign a JWT, exchange it for an access token and act as the service account from any machine.

That makes the key file a long-lived secret with unusually good properties for an attacker:

  • By default, service account keys never expire.
  • A key is tied to the service account, not to the person who created it. Offboarding the creator changes nothing.
  • No multifactor step or device check applies to it.
  • Google can list a key's metadata, but nothing can list who holds a copy of the private key.

Three variants lead to the same credential:

  • Key created by the attacker. With iam.serviceAccountKeys.create on a more privileged service account, one command returns a new key. This is both privilege escalation and persistence: the attacker gains the account's roles and keeps them after their original access is cut.
  • Public key uploaded. The attacker generates the key pair locally and uploads only the public half. The private key never crosses the network and never appears in any response.
  • Existing key stolen. Key files are copied to laptops, baked into container images, committed to repositories, pasted into CI/CD variables and stored in buckets. Finding one is credential theft with no change in Google Cloud at all. The broader hunt for credential files on workstations is covered in gcloud and ADC credential file theft.

Prerequisites

For creation or upload:

  • A credential for a principal with iam.serviceAccountKeys.create on the target account. The Service Account Key Admin role (roles/iam.serviceAccountKeyAdmin) and the basic Editor role include it. Bound on a project, the role covers every service account in that project.
  • The organization must not enforce the constraints that block key creation (iam.disableServiceAccountKeyCreation) or key upload (iam.disableServiceAccountKeyUpload) for the project.
  • Fewer than 10 existing keys on the account.

For theft:

  • Read access to wherever a key file was left: a developer workstation, a build agent, a source repository and its history, a container image, a storage bucket or a ticket.
  • The key must still be enabled, and its service account must still exist and be enabled.

Attack steps

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

1. Choose the target

gcloud iam service-accounts list --project=lab-project-0001

# What the account can do: bindings that name it on the project
gcloud projects get-iam-policy lab-project-0001 \
    --flatten="bindings[].members" \
    --filter="bindings.members:serviceAccount:deploy-sa@lab-project-0001.iam.gserviceaccount.com" \
    --format="table(bindings.role)"

# Keys that already exist for it
gcloud iam service-accounts keys list \
    --iam-account=deploy-sa@lab-project-0001.iam.gserviceaccount.com --managed-by=user

An account that already has one user-managed key is the best cover: a second key looks like a rotation in progress.

2a. Create a key

gcloud iam service-accounts keys create key.json \
    --iam-account=deploy-sa@lab-project-0001.iam.gserviceaccount.com

The JSON file contains "type": "service_account", the private_key_id, the private_key in PEM form and the client_email. This is the only time Google returns the private key.

2b. Or upload a public key

openssl req -x509 -nodes -newkey rsa:2048 -days 365 \
    -keyout private_key.pem -out public_key.pem -subj "/CN=unused"

gcloud iam service-accounts keys upload public_key.pem \
    --iam-account=deploy-sa@lab-project-0001.iam.gserviceaccount.com

2c. Or find a key that already exists

# Key files on a host or in a checked-out repository
grep -rl '"type": "service_account"' /home /srv /opt 2>/dev/null

# Secrets in a repository and its history, or on a file system
trufflehog git https://github.com/example-org/example-repo --results=verified
trufflehog filesystem /path/to/dir

# An environment variable that points at a key file
printenv GOOGLE_APPLICATION_CREDENTIALS

3. Authenticate with the key

gcloud auth activate-service-account --key-file=key.json
gcloud auth list
gcloud projects list

# Client libraries pick the key up through Application Default Credentials
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json

From here the attacker works as the service account: reading data, changing IAM, or minting tokens for other accounts through impersonation if the account's roles allow it.

Impact

The attacker holds a credential for the service account that is independent of every other control on their original access. Disabling the compromised user, rotating that user's password and revoking sessions leave the key working. With an account that holds Owner, Editor or IAM administration roles, the key is control of the project or the organization.

A stolen key is worse to investigate than a created one. There is no creation event from the attacker, the key may be years old, and it may also be in legitimate use by a workload, so the attacker's requests and the workload's requests carry the same key ID. Google's own guidance lists this as a threat of its own: an actor who authenticates with a service account key can hide behind the service account's identity.

OPSEC and artifacts

  • Creation is always logged. google.iam.admin.v1.CreateServiceAccountKey is an Admin Activity entry, which cannot be turned off. It names the caller, the target account and the new key. See the CreateServiceAccountKey event.
  • Uploading instead of creating. The private key never appears in an API response, which defeats controls that watch for downloaded keys. The UploadServiceAccountKey event is still an Admin Activity entry, and the key is listed like any other user-managed key.
  • Hiding among existing keys. Operators pick accounts that already have a key. The inventory still shows each key's ID and validAfterTime, so an extra key with a creation time that matches no change record stands out to anyone who compares.
  • Stealing instead of creating. No audit entry is written for reading a file from a disk or a repository. The first trace in Google Cloud is the key being used.
  • Use carries the key ID. Many services record authenticationInfo.serviceAccountKeyName with the key's full resource name on requests authenticated through a key, together with requestMetadata.callerIp and callerSuppliedUserAgent. Google notes that some services, including Compute Engine and App Engine, do not log the key name.
  • Leaked keys can be disabled by Google. When Google detects a service account key in a public repository, the default response since June 2024 is to disable it and write an audit event. Google does not guarantee detection.
  • A deleted account can come back. Deleting the service account stops its keys, but an account can be undeleted for 30 days, so a later restore by the attacker belongs in the timeline.

Detection

  • Every key creation and upload. In an organization that has moved to keyless authentication, any CreateServiceAccountKey or UploadServiceAccountKey is an alert. Elsewhere, match each event to a change record and to the account's exception list. The service account key rules cover creation, upload and keys that Google disabled after exposure.
  • Caller differs from the owner. A key created by a principal outside the team that owns the service account, or by another service account.
  • Key used from a new place. Requests whose serviceAccountKeyName is known but whose callerIp or user agent is new, especially a gcloud or script user agent on a key that belongs to an application.
  • Key used after its account moved to keyless. Any request that carries a serviceAccountKeyName for an account whose workloads use attached identities.
  • Cleanup by the attacker. A key deleted soon after creation, or bursts of key deletion and disablement. See the disable and delete rules.
log_id("cloudaudit.googleapis.com/activity")
AND protoPayload.serviceName="iam.googleapis.com"
AND (protoPayload.methodName="google.iam.admin.v1.CreateServiceAccountKey"
     OR protoPayload.methodName="google.iam.admin.v1.UploadServiceAccountKey")

To follow one key afterwards, search for its resource name across the logs that are retained:

protoPayload.authenticationInfo.serviceAccountKeyName="//iam.googleapis.com/projects/lab-project-0001/serviceAccounts/deploy-sa@lab-project-0001.iam.gserviceaccount.com/keys/KEY_ID"

A creation record does not show that the key was used, and an empty key list today does not show that no key existed during the incident. Compare key metadata (validAfterTime, validBeforeTime, disabled) with the audit trail, and remember that access tokens obtained from a key before it was deleted stay valid until they expire.

Mitigation

  • Stop creating keys. Enforce iam.disableServiceAccountKeyCreation and iam.disableServiceAccountKeyUpload at the organization level and allow exceptions per project. Review the two constraints separately: blocking creation does not block upload.
  • Use keyless credentials. Attached service accounts inside Google Cloud, impersonation for people, and workload identity federation for workloads on other clouds, on premises and in CI/CD remove the need for a key file.
  • Limit who can manage keys. Remove Service Account Key Admin and Editor from broad groups, and bind key management on single service accounts, not on projects.
  • Expire the keys that must exist. constraints/iam.serviceAccountKeyExpiryHours sets a maximum lifetime for newly created keys. Pair it with secret rotation that the consuming workload supports.
  • Keep the exposure response on. Leave iam.serviceAccountKeyExposureResponse at DISABLE_KEY so that keys found in public repositories are disabled.
  • Find and retire stale keys. List user-managed keys across the organization, identify the consumer of each one from serviceAccountKeyName in the logs, and delete the keys nothing uses. One key per application copy makes the logs attributable.
  • Keep key files out of code and images. Scan repositories, images and pipelines for hardcoded credentials, and store the keys that remain in a secret manager.
  • After a suspected compromise, disable the key first (gcloud iam service-accounts keys disable), confirm that the workload is unaffected or switched over, then delete it, and review everything the account did while the key was valid.

Detection rules for this attack

Log events to collect

FAQ

Why are service account keys a persistence mechanism?

A user-managed key is a private key that Google hands over once and then no longer controls. By default it never expires, it works from any network, and it is not affected by password resets, session revocation or the removal of the person who created it. It stays valid until the key or the service account is disabled or deleted.

Which permission does an attacker need to create a key?

iam.serviceAccountKeys.create on the target service account. The Service Account Key Admin role and the basic Editor role include it, and a grant on the project applies to every service account in the project. Uploading a public key needs the matching upload permission and leaves the private key entirely on the attacker's side.

How many keys can a service account have, and does that limit the attack?

A service account can have up to 10 keys. That is rarely a barrier: most accounts have none or one, so an attacker's extra key fits easily and sits beside the legitimate one in the inventory.

Can audit logs show which key was used?

Often. Many services add authenticationInfo.serviceAccountKeyName to audit entries when a request was authenticated with a token obtained from a key, and the field gives the key's full resource name. Google notes that some services, including Compute Engine and App Engine, do not log it, so a missing value proves nothing.

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.