

**推出 的新主控台體驗 AWS WAF**

您現在可以使用更新後的體驗，在主控台的任何位置存取 AWS WAF 功能。如需詳細資訊，請參閱[使用 主控台](https://docs.aws.amazon.com/waf/latest/developerguide/working-with-console.html)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 中使用邏輯規則陳述式 AWS WAF
<a name="waf-rule-statements-logical"></a>

本節說明什麼是邏輯規則陳述式及其運作方式。

使用邏輯規則陳述式來合併其他陳述式或否定其結果。每個邏輯規則陳述式至少需要一個巢狀化的陳述式。

若要邏輯合併或否定規則陳述式結果，您可以在邏輯規則陳述式下巢狀化陳述式。

邏輯規則陳述式是可巢狀的。您可以將它們巢狀到其他邏輯規則陳述式中，並在縮小範圍陳述式中使用它們。如需縮小範圍陳述式的資訊，請參閱 [在 中使用縮小範圍陳述式 AWS WAF](waf-rule-scope-down-statements.md)。

**注意**  
主控台上的視覺化編輯器支援一個層級的規則陳述式巢狀，這適用於許多需求。若要巢狀更多關卡，請在主控台上編輯規則的 JSON 表示法，或使用 APIs。

此表格說明邏輯規則陳述式，並提供計算每個 的保護套件 (Web ACL) 容量單位 (WCU) 用量的準則。如需 WCU 的相關資訊，請參閱 [中的 Web ACL 容量單位 WCUs) AWS WAF](aws-waf-capacity-units.md)。


| 邏輯陳述式  | Description | WCU | 
| --- | --- | --- | 
| [AND 邏輯](waf-rule-statement-type-and.md) | 結合巢狀陳述式與AND邏輯。 | 以巢狀化陳述式為基礎 | 
|  [NOT 邏輯](waf-rule-statement-type-not.md)  |  否定巢狀化陳述式的結果。  |  以巢狀化陳述式為基礎  | 
| [OR 邏輯](waf-rule-statement-type-or.md) | 結合巢狀陳述式與OR邏輯。 | 以巢狀化陳述式為基礎 | 

# AND 規則陳述式
<a name="waf-rule-statement-type-and"></a>

AND 規則陳述式結合了巢狀陳述式與邏輯AND操作，因此所有巢狀陳述式都必須相符，AND陳述式才能相符。這至少需要兩個巢狀陳述式。

## 規則陳述式特性
<a name="and-rule-statement-characteristics"></a>

**可巢狀** – 您可以巢狀此陳述式類型。

**WCUs** – 取決於巢狀陳述式。

## 尋找此規則陳述式的位置
<a name="and-rule-statement-where-to-find"></a>
+ 主控台上的**規則建置器** – **如果請求**，請選擇**符合所有陳述式 (AND)**，然後填入巢狀陳述式。
+ **API** – [AndStatement](https://docs.aws.amazon.com/waf/latest/APIReference/API_AndStatement.html)

## 範例
<a name="and-rule-statement-examples"></a>

以下清單顯示使用 AND和 NOT 邏輯規則陳述式來消除 SQL Injection 攻擊陳述式相符項目的誤報。在此範例中，假設我們可以撰寫單一位元組比對陳述式，以符合導致誤報的請求。

AND 陳述式會比對不符合位元組比對陳述式且符合 SQL Injection 攻擊陳述式的請求。

```
{
      "Name": "SQLiExcludeFalsePositives",
      "Priority": 0,
      "Statement": {
        "AndStatement": {
          "Statements": [
            {
              "NotStatement": {
                "Statement": {
                  "ByteMatchStatement": {
                    "SearchString": "string identifying a false positive",
                    "FieldToMatch": {
                      "Body": {
                        "OversizeHandling": "MATCH"
                      }
                    },
                    "TextTransformations": [
                      {
                        "Priority": 0,
                        "Type": "NONE"
                      }
                    ],
                    "PositionalConstraint": "CONTAINS"
                  }
                }
              }
            },
            {
              "SqliMatchStatement": {
                "FieldToMatch": {
                  "Body": {
                    "OversizeHandling": "MATCH"
                  }
                },
                "TextTransformations": [
                  {
                    "Priority": 0,
                    "Type": "NONE"
                  }
                ]
              }
            }
          ]
        }
      },
      "Action": {
        "Block": {}
      },
      "VisibilityConfig": {
        "SampledRequestsEnabled": true,
        "CloudWatchMetricsEnabled": true,
        "MetricName": "SQLiExcludeFalsePositives"
      }
    }
```

使用主控台規則視覺化編輯器，您可以在 或 陳述NOT式下巢狀化非邏輯陳述式OR或AND陳述式。NOT 陳述式的巢狀化會顯示在先前的範例中。

使用主控台規則視覺化編輯器，您可以在邏輯規則陳述式下巢狀化最可巢狀的陳述式，例如先前範例中顯示的陳述式。您無法使用視覺化編輯器來巢狀化 OR或 AND陳述式。若要設定此類型的巢狀，您需要以 JSON 提供規則陳述式。例如，下列 JSON 規則清單包含 OR陳述式的巢狀AND陳述式。

```
{
  "Name": "match_rule",
  "Priority": 0,
  "Statement": {
    "AndStatement": {
      "Statements": [
        {
          "LabelMatchStatement": {
            "Scope": "LABEL",
            "Key": "awswaf:managed:aws:bot-control:bot:category:monitoring"
          }
        },
        {
          "NotStatement": {
            "Statement": {
              "LabelMatchStatement": {
                "Scope": "LABEL",
                "Key": "awswaf:managed:aws:bot-control:bot:name:pingdom"
              }
            }
          }
        },
        {
          "OrStatement": {
            "Statements": [
              {
                "GeoMatchStatement": {
                  "CountryCodes": [
                    "JM",
                    "JP"
                  ]
                }
              },
              {
                "ByteMatchStatement": {
                  "SearchString": "JCountryString",
                  "FieldToMatch": {
                    "Body": {}
                  },
                  "TextTransformations": [
                    {
                      "Priority": 0,
                      "Type": "NONE"
                    }
                  ],
                  "PositionalConstraint": "CONTAINS"
                }
              }
            ]
          }
        }
      ]
    }
  },
  "Action": {
    "Block": {}
  },
  "VisibilityConfig": {
    "SampledRequestsEnabled": true,
    "CloudWatchMetricsEnabled": true,
    "MetricName": "match_rule"
  }
}
```

# NOT 規則陳述式
<a name="waf-rule-statement-type-not"></a>

NOT 規則陳述式在邏輯上否定單一巢狀陳述式的結果，因此巢狀陳述式必須與NOT陳述式不相符，反之亦然。這需要一個巢狀化陳述式。

例如，如果您想要封鎖不是來自特定國家/地區的請求，請建立動作設定為封鎖的NOT陳述式，並巢狀指定國家/地區的地理比對陳述式。

## 規則陳述式特性
<a name="not-rule-statement-characteristics"></a>

**可巢狀** – 您可以巢狀化此陳述式類型。

**WCUs** – 取決於巢狀陳述式。

## 尋找此規則陳述式的位置
<a name="not-rule-statement-where-to-find"></a>
+ 主控台上的**規則建置器** – 對於**如果請求**，請選擇**不符合陳述式 (NOT)**，然後填入巢狀陳述式。
+ **API** – [NotStatement](https://docs.aws.amazon.com/waf/latest/APIReference/API_NotStatement.html)

# OR 規則陳述式
<a name="waf-rule-statement-type-or"></a>

OR 規則陳述式結合了巢狀陳述式與OR邏輯，因此其中一個巢狀陳述式必須符合，OR陳述式才能相符。這至少需要兩個巢狀陳述式。

例如，如果您想要封鎖來自特定國家/地區的請求或包含特定查詢字串的請求，您可以建立 OR陳述式，並在其中巢狀化一個國家/地區的地理比對陳述式，以及查詢字串的字串比對陳述式。

如果您想要封鎖*不是*來自特定國家/地區或包含特定查詢字串的請求，您可以修改先前的OR陳述式，將地理比對陳述式巢狀在NOT陳述式內的一個層級。此巢狀層級需要您使用 JSON 格式，因為主控台僅支援一個巢狀層級。

## 規則陳述式特性
<a name="or-rule-statement-characteristics"></a>

**可巢狀** – 您可以巢狀化此陳述式類型。

**WCUs** – 取決於巢狀陳述式。

## 尋找此規則陳述式的位置
<a name="or-rule-statement-where-to-find"></a>
+ 主控台上的**規則建置器** – **如果請求**，請選擇**至少符合其中一個陳述式 (OR)**，然後填入巢狀陳述式。
+ **API** – [OrStatement](https://docs.aws.amazon.com/waf/latest/APIReference/API_OrStatement.html)

**範例**  
下列清單顯示使用 OR來結合其他兩個陳述式。如果其中一個巢狀OR陳述式相符，則陳述式為相符。

```
{
  "Name": "neitherOfTwo",
  "Priority": 1,
  "Action": {
    "Block": {}
  },
  "VisibilityConfig": {
    "SampledRequestsEnabled": true,
    "CloudWatchMetricsEnabled": true,
    "MetricName": "neitherOfTwo"
  },
  "Statement": {
    "OrStatement": {
      "Statements": [
        {
          "GeoMatchStatement": {
            "CountryCodes": [
              "CA"
            ]
          }
        },
        {
          "IPSetReferenceStatement": {
            "ARN": "arn:aws:wafv2:us-east-1:111111111111:regional/ipset/test-ip-set-22222222/33333333-4444-5555-6666-777777777777"
          }
        }
      ]
    }
  }
}
```

使用主控台規則視覺化編輯器，您可以在邏輯規則陳述式下巢狀化最可巢狀的陳述式，但無法使用視覺化編輯器來巢狀化 OR或 AND陳述式。若要設定此類型的巢狀，您需要以 JSON 提供規則陳述式。例如，下列 JSON 規則清單包含 OR陳述式的巢狀AND陳述式。

```
{
  "Name": "match_rule",
  "Priority": 0,
  "Statement": {
    "AndStatement": {
      "Statements": [
        {
          "LabelMatchStatement": {
            "Scope": "LABEL",
            "Key": "awswaf:managed:aws:bot-control:bot:category:monitoring"
          }
        },
        {
          "NotStatement": {
            "Statement": {
              "LabelMatchStatement": {
                "Scope": "LABEL",
                "Key": "awswaf:managed:aws:bot-control:bot:name:pingdom"
              }
            }
          }
        },
        {
          "OrStatement": {
            "Statements": [
              {
                "GeoMatchStatement": {
                  "CountryCodes": [
                    "JM",
                    "JP"
                  ]
                }
              },
              {
                "ByteMatchStatement": {
                  "SearchString": "JCountryString",
                  "FieldToMatch": {
                    "Body": {}
                  },
                  "TextTransformations": [
                    {
                      "Priority": 0,
                      "Type": "NONE"
                    }
                  ],
                  "PositionalConstraint": "CONTAINS"
                }
              }
            ]
          }
        }
      ]
    }
  },
  "Action": {
    "Block": {}
  },
  "VisibilityConfig": {
    "SampledRequestsEnabled": true,
    "CloudWatchMetricsEnabled": true,
    "MetricName": "match_rule"
  }
}
```