跳至內容

如何在 AWS WAF 中建立複雜的自訂 JSON 規則?

3 分的閱讀內容
0

我想在 AWS WAF 中建立複雜的自訂 JSON 規則。

解決方法

建立複雜的自訂規則

請完成下列步驟:

  1. 開啟 AWS WAF 主控台。
  2. 在導覽窗格中,選擇 Rule groups (規則群組)。
  3. 選擇您的規則群組。
  4. 選擇 Manage rules (管理規則),然後選擇 Add rules (新增規則)。
  5. 選擇 Custom rule (自訂規則),然後選擇 Next (下一步)。
  6. 選擇 JSON,以開啟規則 JSON 編輯器。
  7. 使用自訂規則更新規則 JSON 編輯器。
  8. 選擇 Add rule (新增規則)。

注意: 若要建立使用巢狀陳述式的自訂規則,則必須使用 JSON 編輯器。巢狀陳述式結合 AND、OR 或 NOT 邏輯。如需更多資訊,請參閱 Using rule statement in AWS WAF (在 AWS WAF 中使用規則陳述式)。

您可以參考下列範例來建立自己的自訂規則邏輯。

範例 1

使用下列自訂規則陳述式,允許來自美國 - US 且 URI 為 /wp-admin/ 或 /wp-login/ 的請求:

{
  "Name": "test",
  "Priority": 100,
  "Statement": {
    "AndStatement": {
      "Statements": [
        {
          "GeoMatchStatement": {
            "CountryCodes": [
              "US"
            ]
          }
        },
        {
          "OrStatement": {
            "Statements": [
              {
                "ByteMatchStatement": {
                  "SearchString": "/wp-admin/",
                  "FieldToMatch": {
                    "UriPath": {}
                  },
                  "TextTransformations": [
                    {
                      "Priority": 0,
                      "Type": "NONE"
                    }
                  ],
                  "PositionalConstraint": "CONTAINS"
                }
              },
              {
                "ByteMatchStatement": {
                  "SearchString": "/wp-login/",
                  "FieldToMatch": {
                    "UriPath": {}
                  },
                  "TextTransformations": [
                    {
                      "Priority": 0,
                      "Type": "NONE"
                    }
                  ],
                  "PositionalConstraint": "CONTAINS"
                }
              }
            ]
          }
        }
      ]
    }
  },
  "Action": {
    "Allow": {}
  },
  "VisibilityConfig": {
    "SampledRequestsEnabled": true,
    "CloudWatchMetricsEnabled": true,
    "MetricName": "test"
  }
}

規則邏輯: 請求來自 US,且 URI 為 /a 或 /b。

範例 2

使用以下自訂規則陳述式,在請求主體中發現 XSS 特徵碼時,封鎖該請求。此規則排除 /test、/login 和 /admin URI 的檢查:

{
 "Name": "XSS_Block_Allow_Uploads",
 "Priority": 100,
 "Statement": {
     "AndStatement": {
     "Statements": [
       {
         "XssMatchStatement": {
           "FieldToMatch": {
             "Body": {}
           },
           "TextTransformations": [
             {
               "Priority": 0,
               "Type": "NONE"
             }
           ]
         }
       },
   {
    "NotStatement": {
     "Statement": {
      "OrStatement": {
       "Statements": [
                 {
                   "ByteMatchStatement": {
                     "SearchString": "/test",
                     "FieldToMatch": {
                       "UriPath": {}
                     },
                     "TextTransformations": [
                       {
                         "Priority": 0,
                         "Type": "NONE"
                       }
                     ],
                     "PositionalConstraint": "EXACTLY"
                   }
                 },
                 {
                   "ByteMatchStatement": {
                     "SearchString": "/admin",
                     "FieldToMatch": {
                       "UriPath": {}
                     },
                     "TextTransformations": [
                       {
                         "Priority": 0,
                         "Type": "NONE"
                       }
                     ],
                     "PositionalConstraint": "EXACTLY"
                   }
                 },
                 {
                   "ByteMatchStatement": {
                     "SearchString": "/login",
                     "FieldToMatch": {
                       "UriPath": {}
                     },
                     "TextTransformations": [
                       {
                         "Priority": 0,
                         "Type": "NONE"
                       }
                     ],
                     "PositionalConstraint": "EXACTLY"
                   }
                 }
               ]
             }
           }
         }
       }
     ]
   }
 },
 "Action": {
   "Block": {}
 },
 "VisibilityConfig": {
   "SampledRequestsEnabled": true,
   "CloudWatchMetricsEnabled": true,
   "MetricName": "XSS_Block_Allow_Uploads"
 }
}

規則邏輯: 請求主體有 XSS 特徵碼,且 URI 不是 /a、/b 或 /c。

範例 3

使用下列自訂規則陳述式來封鎖包含內部自訂標籤的請求。請求也不得包含特定主機、IP 位址和 URI 的組合:

{
  "Name": "Block_internal_unauthorized",
  "Priority": 100,
  "Statement": {
    "AndStatement": {
      "Statements": [
      {
          "LabelMatchStatement": {
            "Scope": "LABEL",
            "Key": "internal"
          }
        },
        {
          "NotStatement": {
            "Statement": {
              "AndStatement": {
                "Statements": [
                {
                    "ByteMatchStatement": {
                      "FieldToMatch": {
                        "UriPath": {}
                      },
                      "PositionalConstraint": "EXACTLY",
                      "SearchString": "/test",
                      "TextTransformations": [{
                        "Type": "NONE",
                        "Priority": 0
                      }]
                    }
                  },
                  {
                    "IPSetReferenceStatement": {
                      "ARN": "arn:aws:wafv2:us-east-1:555555555555:regional/ipset/internal_IPs/###-##-###"
                    }
                  },
                  {
                    "ByteMatchStatement": {
                      "FieldToMatch": {
                        "SingleHeader": {
                          "Name": "host"
                        }
                      },
                      "PositionalConstraint": "EXACTLY",
                      "SearchString": "example.com",
                      "TextTransformations": [{
                        "Type": "NONE",
                        "Priority": 0
                      }]
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "Action": {
   "Block": {}
 },
 "VisibilityConfig": {
   "SampledRequestsEnabled": true,
   "CloudWatchMetricsEnabled": true,
   "MetricName": "Block_internal_unauthorized"
 }
}

規則邏輯: 如果請求包含標籤且不包含 URI 和 IP 和主機,則將其封鎖。

AWS 官方已更新 7 個月前