GCP - Add Custom SSH Metadata
Modifying the metadata
Metadata modification on an instance could lead to significant security risks if an attacker gains the necessary permissions.
The permissions commonly relevant to this path are compute.instances.setMetadata for one VM and compute.projects.setCommonInstanceMetadata for project-wide metadata. [1][4]
Incorporation of SSH Keys into Custom Metadata
On older GCP Linux images, the Python Linux Guest Environment for Google Compute Engine included the accounts daemon. That package is now superseded by google-guest-agent, whose current documentation describes the same metadata-based account and SSH-key management when OS Login is disabled. [2][3]
When metadata-based SSH-key management is active, the guest agent watches instance or project metadata for authorized-key changes. A new key can update an existing managed user's ~/.ssh/authorized_keys or provision a new user in the google-sudoers group, depending on the key format. [1][2][3]
Therefore, an attacker who can modify custom metadata may gain SSH access to a privileged account and compromise the host. This technique requires metadata-based SSH authorization: a VM with OS Login enabled ignores SSH keys stored in metadata. [1][3][4][5]
Add SSH key to existing privileged user
-
Examine Existing SSH Keys on the Instance:
-
Execute the command to describe the instance and its metadata to locate existing SSH keys. The relevant section in the output will be under
metadata, specifically thessh-keyskey. [1][4]gcloud compute instances describe [INSTANCE] --zone [ZONE] -
Pay attention to the format of the SSH keys: the username precedes the key, separated by a colon. [1][4]
-
-
Prepare a Text File for SSH Key Metadata:
-
Generate a New SSH Key for the Target User (
alicein this example): -
Update the Instance's SSH Key Metadata:
-
Access the Instance Using the New SSH Key:
Create a new privileged user and add a SSH key
If no interesting user is found, it's possible to create a new one which will be given sudo privileges when the Linux guest agent is managing metadata-based accounts. [1][3]
# define the new account username
NEWUSER="definitelynotahacker"
# create a key
ssh-keygen -t rsa -C "$NEWUSER" -f ./key -P ""
# create the input meta file
NEWKEY="$(cat ./key.pub)"
echo "$NEWUSER:$NEWKEY" > ./meta.txt
# update the instance metadata
gcloud compute instances add-metadata [INSTANCE_NAME] --metadata-from-file ssh-keys=meta.txt
# ssh to the new account
ssh -i ./key "$NEWUSER"@localhost
SSH keys at project level
It's possible to broaden the reach of SSH access to multiple Virtual Machines (VMs) in a cloud environment by applying SSH keys at the project level. This approach allows SSH access to any instance within the project that hasn't explicitly blocked project-wide SSH keys and is using metadata-based SSH authorization. Here's a summarized guide: [1][4][5]
-
Apply SSH Keys at the Project Level:
-
Use the
gcloud compute project-info add-metadatacommand to add SSH keys frommeta.txtto the project's metadata. This action ensures that the SSH keys are recognized across all VMs in the project, unless a VM has the "Block project-wide SSH keys" option enabled. [1][4]gcloud compute project-info add-metadata --metadata-from-file ssh-keys=meta.txt
-
-
SSH into Instances Using Project-Wide Keys:
- With project-wide SSH keys in place, you can SSH into any instance within the project. Instances that do not block project-wide keys will accept the SSH key, granting access. [1][4][5]
- A direct method to SSH into an instance is using the
gcloud compute ssh [INSTANCE]command. This command uses your current username and the SSH keys set at the project level to attempt access. [1]
References
- [1] Google Cloud privilege escalation & post-exploitation tactics
- [2] Python Linux Guest Environment for Google Compute Engine
- [3] Guest agent functionality | Compute Engine
- [4] Add SSH keys to VMs | Compute Engine
- [5] Best practices for controlling SSH login access | Compute Engine
[!TIP] Learn & practice AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Learn & practice GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
Learn & practice Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
Browse the full HackTricks Training catalog.Support HackTricks
- Check the subscription plans!
- Join the 💬 Discord group or the telegram group or follow us on Twitter 🐦 @hacktricks_live.
- Share hacking tricks by submitting PRs to the HackTricks and HackTricks Cloud github repos.


