はじめに
こんにちは。 今回は、AWS CLIを利用して、WAFから特定のルールを外してみたいと思います。
AWS CLI コマンドの解説
今回、必要なのはルールの更新のみです。 ついては、入力が必要なのは以下の通り。
update-web-acl --name <value> --scope <value> --id <value> --default-action <value> --rules <value> --visibility-config <value> --lock-token <value>
name
Web ACLの名前を指定する。
scope
CloudFront用かどうかを指定する。 CloudFrontであれば、 --scope CLOUDFRONT を指定する。 ALB、API GW、AppSync等であれば、 --scope REGIONAL を指定する。
id
Web ACL の ID を指定する。
default-action
デフォルトアクションを指定する。 AllowもしくはBlockを選択。特に詳しく作り込まない場合は以下のような記載でOK。
--default-action Block={}
詳しいことは折りたたんで記載。(この文字列クリックで展開)
Block -> (structure)
- CustomResponse -> (structure): ウェブリクエストに対するカスタムレスポンスを指定
- ResponseCode -> (integer): クライアントに返すHTTPレスポンスコード
- CustomResponseBodyKey -> (string): クライアントに返すレスポンスボディ
- ResponseHeaders -> (list): 利用するHTTPヘッダ
- Name -> (string): ヘッダ名
- Value -> (string): ヘッダのバリュー
Allow -> (structure)
- CustomRequestHandling -> (structure): カスタム処理の定義
JSON Syntax
{ "Block": { "CustomResponse": { "ResponseCode": integer, "CustomResponseBodyKey": "string", "ResponseHeaders": [ { "Name": "string", "Value": "string" } ... ] } }, "Allow": { "CustomRequestHandling": { "InsertHeaders": [ { "Name": "string", "Value": "string" } ... ] } } }
rules
Web ACLに設定するルール。既存のルールを変更する場合は、get-web-aclコマンドで取得してこよう。 詳細の解説は(あまりにも解説項目が多すぎるため)割愛。
visibility-config
CloudWatchメトリクスの定義。 SampledRequestsEnabled -> (boolean): ルールに一致するWebリクストのサンプリングを保存するかどうか
CloudWatchMetricsEnabled -> (boolean): CloudWatchにメトリクスを送信するかどうか
MetricName -> (string): CloudWatchメトリクスの名前
lock-token
ロックに利用するトークン。
やってみる
既存ルールの取得
まず、既存のルールを取得します。
[cloudshell-user@ip-10-1-1-10 ~]$ aws wafv2 get-web-acl --name <WebACLName> --scope CLOUDFRONT --id <WebACLID> { "WebACL": { "Name": "testname", "Id": "12345678-abcd-efgh-hijk-1234567890a", "ARN": "arn:aws:wafv2:us-east-1:123456789:global/webacl/<WebACLName>/12345678-abcd-efgh-hijk-1234567890a", "DefaultAction": { "Allow": {} }, "Description": "", "Rules": [ { "Name": "RuleA", "Priority": 0, "Statement": { "ByteMatchStatement": { "SearchString": "ABCDEFG", "FieldToMatch": { "SingleHeader": { "Name": "x-forwarded-for" } }, "TextTransformations": [ { "Priority": 0, "Type": "NONE" } ], "PositionalConstraint": "EXACTLY" } }, "Action": { "Block": {} }, "VisibilityConfig": { "SampledRequestsEnabled": false, "CloudWatchMetricsEnabled": false, "MetricName": "<WebACLName>" } }, { "Name": "AWS-AWSManagedRulesBotControlRuleSet", "Priority": 1, "Statement": { "ManagedRuleGroupStatement": { "VendorName": "AWS", "Name": "AWSManagedRulesBotControlRuleSet" } }, "OverrideAction": { "None": {} }, "VisibilityConfig": { "SampledRequestsEnabled": true, "CloudWatchMetricsEnabled": true, "MetricName": "AWS-AWSManagedRulesBotControlRuleSet" } } ], "VisibilityConfig": { "SampledRequestsEnabled": false, "CloudWatchMetricsEnabled": true, "MetricName": "<WebACLName>" }, "Capacity": 52, "ManagedByFirewallManager": false, "LabelNamespace": "awswaf:1234567890a:webacl:<WebACLName>:" }, "LockToken": "1234567-a123-b123-c123-1234567890" }
ルールの作成
次に、"Rules"部分を丸っとコピーして、変更後のルールを作成します。 今回はRuleAを削除してみることにしました。
変更前
[ { "Name": "RuleA", "Priority": 0, "Statement": { "ByteMatchStatement": { "SearchString": "ABCDEFG", "FieldToMatch": { "SingleHeader": { "Name": "x-forwarded-for" } }, "TextTransformations": [ { "Priority": 0, "Type": "NONE" } ], "PositionalConstraint": "EXACTLY" } }, "Action": { "Block": {} }, "VisibilityConfig": { "SampledRequestsEnabled": false, "CloudWatchMetricsEnabled": false, "MetricName": "<WebACLName>" } }, { "Name": "AWS-AWSManagedRulesBotControlRuleSet", "Priority": 1, "Statement": { "ManagedRuleGroupStatement": { "VendorName": "AWS", "Name": "AWSManagedRulesBotControlRuleSet" } }, "OverrideAction": { "None": {} }, "VisibilityConfig": { "SampledRequestsEnabled": true, "CloudWatchMetricsEnabled": true, "MetricName": "AWS-AWSManagedRulesBotControlRuleSet" } } ]
変更後
[ { "Name": "AWS-AWSManagedRulesBotControlRuleSet", "Priority": 1, "Statement": { "ManagedRuleGroupStatement": { "VendorName": "AWS", "Name": "AWSManagedRulesBotControlRuleSet" } }, "OverrideAction": { "None": {} }, "VisibilityConfig": { "SampledRequestsEnabled": true, "CloudWatchMetricsEnabled": true, "MetricName": "AWS-AWSManagedRulesBotControlRuleSet" } } ]
更新の実施
上記で作成したファイルをもとに、更新のコマンドを入力します。 lock-token には、 get-web-acl コマンドを入力したときの戻り値に記載されていたものを利用します。 (もし手元になければ、再度 get-web-acl コマンドを実行してください。)
aws wafv2 update-web-acl --name <WebACLName> --scope CLOUDFRONT --id 12345678-abcd-efgh-hijk-1234567890a --default-action Allow={} --visibility-config SampledRequestsEnabled=false,CloudWatchMetricsEnabled=false,MetricName=<WebACLName> --rules file://rules_new.json --lock-token 1234567-a123-b123-c123-1234567890