概要
やりたいこと
AWS WAF を検知モード (Allow/Count) からブロックモード(Block)にしたい。
AWS Managed Rules for AWS WAF より、ルール運用のベストプラクティスは、最初にカウント(検知)モードで動かし傾向をみて、少しずつブロックモードに移行していくこと。
検知モードからブロックモードへ移行する手順を整理する。
AWS WAF の処理方法
まずは以下のドキュメントを読んで、 AWS WAF の動作を理解する。
終了アクションと非終了アクションがある。
- 終了アクション : Allow, Block、一致するウェブリクエストに対する全ての処理は停止される
- 非終了アクション : Count、ルールがリクエストと一致すると、AWS WAF はリクエストをカウントし、ウェブ ACL ルールセットに続くルールの処理を継続
AWS WAF の Action
アクション自体が 3 種類ある。優先度は OverrideAction >= RuleAction > DefaultAction。
- DefaultAction : どのルールにも一致しない場合のデフォルトアクション
- RuleAction : ルールに一致したときに実行されるアクション
- OverrideAction : RuleGroup, ManagedRule のアクション
RuleAction と OverrideAction の説明は以下のドキュメントが詳しい。
Default Action
ウェブリクエストがどのルールにも一致しない場合に実行するアクション。
Default Action は 2 つ。WAF を適用するサービスが、一部をブロックする運用なら Allow、一部を許可する運用なら Block を選んでおく。
- Allow : リクエストを許可する
- Block : リクエストをブロックする
RuleAction
単一のルールにリクエストが引っかかったときに実施されるアクション。
RuleAction は 3 つ。Allow だとログが記録されない。
- Allow : リクエストを許可する
- Block : リクエストをブロックする
- Count : ログを出力する、リクエストの許可もブロックもしない
OverrideAction
Rule Group や Managed Rules を利用している場合のアクションを設定する。
Managed Rules はデフォルトアクションがブロックモードとなる。そのため、ブロックしたくない場合は OverrideAction で Count を指定する必要がある。
OverrideAction は 2 種類。
- None : 上書きなしのため、そのままブロック
- Count : 検知した場合にブロックしないで、ログのみ出力
補足 : ExcludedRules
Managed Rules の一部のルールだけをカウントモードに上書きするには ExcludedRules を指定する。
CloudFormation で設定する場合、除外するルール名を指定する必要がある。
ルール名は WAF コンソールから見ることができる。

CloudFormation でデプロイ
AWSManagedRulesCommonRuleSet で実験する。
カウント(検知)モード
Managed Rule はデフォルトでブロックアクションとなるため、OverrideAction で Count を指定する。
- DefaultAction を Allow
- OverrideAction を Count
WebACL:
Type: AWS::WAFv2::WebACL
Properties:
Name: !Sub ${WebACLName}
Scope: REGIONAL
Description: This is an example WebACL
DefaultAction:
Allow: {} # Allow or Block
VisibilityConfig:
SampledRequestsEnabled: true
CloudWatchMetricsEnabled: true
MetricName: !Sub ${WebACLName}
Rules:
- Name: AWSManagedRulesCommonRuleSet
Priority: 0
OverrideAction:
Count: {} # None or Count
VisibilityConfig:
SampledRequestsEnabled: true
CloudWatchMetricsEnabled: true
MetricName: AWSManagedRulesCommonRuleSetMetric
Statement:
ManagedRuleGroupStatement:
VendorName: AWS
Name: AWSManagedRulesCommonRuleSet
ExcludedRules: []
結果。Action の Enable count mode がオンになっている。

サンプルリクエストを投げて、アクセスできることを確認。

2行目のログが該当のもの。ルールの項目は「-」となり、Count したログとして出力される。
何のルールで検知されたかわからない。。

ブロックモード
Managed Rule はデフォルトでブロックアクションとなるため、OverrideAction ではなにもしない None を指定する。
- DefaultAction を Allow
- OverrideAction を None
WebACL:
Type: AWS::WAFv2::WebACL
Properties:
Name: !Sub ${WebACLName}
Scope: REGIONAL
Description: This is an example WebACL
DefaultAction:
Allow: {} # Allow or Block
VisibilityConfig:
SampledRequestsEnabled: true
CloudWatchMetricsEnabled: true
MetricName: !Sub ${WebACLName}
Rules:
- Name: AWSManagedRulesCommonRuleSet
Priority: 0
OverrideAction:
None: {} # None or Count
VisibilityConfig:
SampledRequestsEnabled: true
CloudWatchMetricsEnabled: true
MetricName: AWSManagedRulesCommonRuleSetMetric
Statement:
ManagedRuleGroupStatement:
VendorName: AWS
Name: AWSManagedRulesCommonRuleSet
ExcludedRules: []
結果。Action の Enable count mode がオフになっている。

サンプルリクエストを投げると 403 が返却され、アクセスできないことを確認。

「AWS#AWSManagedRulesCommonRuleSet#NoUserAgent_HEADER」ルールにマッチし、Block したログとして出力される。

AWS Managed Rule の一部ルールだけカウント(検知)モード
AWSManagedRulesCommonRuleSet は複数のルールの集合体である。そのうちの一つの NoUserAgent_HEADER ルールだけアクションを変更する。
- DefaultAction を Allow
- OverrideAction を None
- ExcludedRules に NoUserAgent_HEADER
WebACL:
Type: AWS::WAFv2::WebACL
Properties:
Name: !Sub ${WebACLName}
Scope: REGIONAL
Description: This is an example WebACL
DefaultAction:
Allow: {} # Allow or Block
VisibilityConfig:
SampledRequestsEnabled: false
CloudWatchMetricsEnabled: true
MetricName: !Sub ${WebACLName}
Rules:
- Name: AWSManagedRulesCommonRuleSet
Priority: 0
OverrideAction:
None: {} # None or Count
VisibilityConfig:
SampledRequestsEnabled: true
CloudWatchMetricsEnabled: true
MetricName: AWSManagedRulesCommonRuleSetMetric
Statement:
ManagedRuleGroupStatement:
VendorName: AWS
Name: AWSManagedRulesCommonRuleSet
ExcludedRules: [NoUserAgent_HEADER]
結果。NoUserAgent_HEADER のみ Override rules action が有効になっている。全体としてはブロックモードで動いている。

サンプルリクエストを投げると、アクセスできることを確認。

ログは出てない。Default Action の Allow として許可され、SampledRequestsEnabled が false のため。
コメント