Help Center/ Web Application Firewall/ User Guide/ Configuring Protection Policies/ Configuring Custom Precise Protection Rules
Updated on 2024-11-19 GMT+08:00

Configuring Custom Precise Protection Rules

You can combine common HTTP fields, such as IP, Path, Referer, User Agent, and Params in a protection rule to let WAF allow, block, or only log the requests that match the combined conditions. In addition, JavaScript challenge verification is supported. WAF returns a piece of JavaScript code that can be automatically executed by a normal browser to the client. If the client properly executes JavaScript code, WAF allows all requests from the client within a period of time (30 minutes by default). During this period, no verification is required. If the client fails to execute the code, WAF blocks the requests.

A reference table can be added to a precise protection rule. The reference table takes effect for all protected domain names.

If you have enabled enterprise projects, ensure that you have all operation permissions for the project where your WAF instance locates. Then, you can select the project from the Enterprise Project drop-down list and configure protection policies for the domain names in the project.

Prerequisites

Constraints

  • Full Detection is not included in the WAF standard edition or cloud WAF billed on a pay-per-use basis.
  • The reference table function is not included in the WAF standard edition or cloud WAF billed on a pay-per-use basis.
  • The Response Code, Response Length, Response Time, Response Header, and Response Body fields are not supported by the Cloud Mode - Load balancer access mode. Only the platinum edition WAF using the Cloud Mode - CNAME access mode supports response fields.
  • If you configure Protective Action to Block for a precise protection rule, you can configure a known attack source rule by referring to Configuring a Known Attack Source Rule to Block Specific Visitors for a Specified Duration. WAF will block requests matching the configured IP address, Cookie, or Params for a length of time configured as part of the rule.
  • The path content cannot contain the following special characters: (<>*)
  • It takes several minutes for a new rule to take effect. After the rule takes effect, protection events triggered by the rule will be displayed on the Events page.

Application Scenarios

Precise protection rules are used for anti-leeching and website management background protection.

Configuring a Precise Protection Rule

  1. Log in to the management console.
  2. Click in the upper left corner of the management console and select a region or project.
  3. Click in the upper left corner and choose Web Application Firewall under Security & Compliance.
  4. In the navigation pane on the left, choose Policies.
  5. Click the name of the target policy to go to the protection configuration page.
  6. Click the Precise Protection configuration area and toggle it on or off if needed.

    • : enabled.
    • : disabled.

  7. On the Precise Protection page, set Detection Mode.

    Two detection modes are available:
    • Instant detection: If a request matches a configured precise protection rule, WAF immediately ends threat detection and blocks the request.
    • Full detection: If a request matches a configured precise protection rule, WAF finishes its scan first and then blocks all requests that match the configured precise protection rule.

  8. In the upper left corner above the Precise Protection rule list, click Add Rule.
  9. In the displayed dialog box, add a rule by referring to Table 1.

    The settings shown in Figure 1 are used as an example. If a visitor tries to access a URL containing /admin, WAF will block the request.

    To ensure that WAF blocks only attack requests, configure Protective Action to Log only first and check whether normal requests are blocked on the Events page. If no normal requests are blocked, configure Protective Action to Block.

    Figure 1 Add Precise Protection Rule
    Table 1 Rule parameters

    Parameter

    Description

    Example Value

    Rule Name

    Name of the rule.

    waftest

    Rule Description

    A brief description of the rule. This parameter is optional.

    None

    Condition List

    Click Add Conditions and add conditions. At least one condition is required for a rule, but up to 30 conditions are allowed. If you add more than one condition, the rule will only take effect when all conditions are met.

    Parameters for configuring a condition are described as follows:
    • Field
    • Subfield: Configure this field only when IPv4, IPv6, Params, Cookie, Response Header, or Header is selected for Field.
      NOTICE:

      A subfield cannot exceed 2,048 bytes.

    • Logic: Select a logical relationship from the drop-down list.
      NOTE:
      • If Include any value, Exclude any value, Equal to any value, Not equal to any value, Prefix is any value, Prefix is not any of them, Suffix is any value, or Suffix is not any of them is selected, select an existing reference table in the Content drop-down list. For details, see Creating a Reference Table to Configure Protection Metrics in Batches.
      • Exclude any value, Not equal to any value, Prefix is not any of them, and Suffix is not any of them indicates, respectively, that WAF performs the protection action (block, allow, or log only) when the field in the access request does not contain, is not equal to, or the prefix or suffix is not any value set in the reference table. For example, assume that Path field is set to Exclude any value and the test reference table is selected. If test1, test2, and test3 are set in the test reference table, WAF performs the protection action when the path of the access request does not contain test1, test2, or test3.
    • Content: Enter or select the content of condition matching.
    NOTE:

    For more details about the configurations in general, see Table 1.

    • Path Include /admin
    • User Agent Prefix is not mozilla/5.0
    • IP Equal to 192.168.2.3
    • Cookie key1 Prefix is not jsessionid

    Protective Action

    • Block: The request that hit the rule will be blocked and a block response page is returned to the client that initiates the request. By default, WAF uses a unified block response page. You can also customize this page.
    • Allow: Requests that hit the rule are forwarded to backend servers.
    • Log only: Requests that hit the rule are not blocked, but will be logged. You can use WAF logs to query requests that hit the current rule and analyze the protection results of the rule. For example, check whether there are requests that are blocked mistakenly.
    • JS Challenge: WAF returns a piece of JavaScript code that can be automatically executed by a normal browser to the client. If the client properly executes the JavaScript code, WAF allows all requests from the client within a period of time (30 minutes by default). During this period, no verification is required. If the client fails to execute the code, WAF blocks the requests.

    Block

    Known Attack Source

    If you set Protective Action to Block, you can select a blocking type for a known attack source rule. Then, WAF blocks requests matching the configured IP, Cookie, or Params for a length of time that depends on the selected blocking type.

    Long-term IP address blocking

    Priority

    Rule priority. If you have added multiple rules, rules are matched by priority. The smaller the value you set, the higher the priority.

    NOTICE:

    If multiple precise access control rules have the same priority, WAF matches the rules in the sequence of time the rules are added.

    5

    Application Schedule

    Select Immediate to enable the rule immediately, or select Custom to configure when you wish the rule to be enabled.

    Immediate

    Block Page

    If Protective Action is set to Block, you can configure an error page you want to return to the visitors.

    • If you select Default settings, the default block page is displayed.
    • If you select Custom, a custom error message is displayed.

    Custom

    HTTP Return Code

    HTTP return codes can be configured when Block Page is set to Custom.

    418

    Response Header

    HTTP response headers can be configured when Block Page is set to Custom.

    Click Add Response Header Field and configure response header parameters.

    -

    Block Page Type

    If you select Custom for Block Page, select a type of the block page among options application/json, text/html, and text/xml.

    text/html

    Page Content

    If you select Custom for Block Page, configure the content to be returned.

    Page content styles corresponding to different page types are as follows:

    • text/html: <html><body>Forbidden</body></html>
    • application/json: {"msg": "Forbidden"}
    • text/xml: <?xml version="1.0" encoding="utf-8"?><error> <msg>Forbidden</msg></error>

  10. Click Confirm. You can then view the added precise protection rule in the protection rule list.

    • To disable a rule, click Disable in the Operation column of the rule. The default Rule Status is Enabled.
    • To modify a rule, click Modify in the row containing the rule.
    • To delete a rule, click Delete in the row containing the rule.

Protection Effect

To verify WAF is protecting your website (www.example.com) against the rule as shown in Figure 1:

  1. Clear the browser cache and enter the domain name in the address bar to check whether the website is accessible.

    • If the website is inaccessible, connect the website domain name to WAF by following the instructions in Website Settings.
    • If the website is accessible, go to 2.

  2. Clear the browser cache and enter http://www.example.com/admin (or any page containing /admin) in the address bar. Normally, WAF blocks the requests that meet the conditions and returns the block page.
  3. Return to the WAF console. In the navigation pane, choose Events. On the displayed page, view the event log.

Configuration Example - Blocking a Certain Type of Attack Requests

Analysis of a specific type of WordPress pingback attack shows that the User Agent field contains WordPress.

Figure 2 WordPress pingback attack

A precise rule as shown in the figure can block this type of attack.

Figure 3 User Agent configuration

Configuration Example - Blocking Requests to a Certain URL

If a large number of IP addresses are accessing a URL that does not exist, configure the following protection rule to block such requests to reduce resource usage on the origin server.

Figure 4 Blocking requests to a specific URL

Configuration Example - Blocking Requests with null Fields

You can configure precise protection rules to block requests having null fields.

Figure 5 Blocking requests with empty Referer

Configuration Example - Blocking Specified File Types (ZIP, TAR, and DOCX)

You can configure file types that match the path field to block specific files of certain types. For example, if you want to block .zip files, you can configure a precise protection rule as shown in Figure 6 to block access requests of .zip files.

Figure 6 Blocking requests of specific file types

Configuration Example - Preventing Hotlinking

You can configure a protection rule based on the Referer field to enable WAF to block hotlinking from a specific website. If you find out that, for example, requests from https://abc.blog.com are stealing images from your site, you can configure a rule to block such requests.

Figure 7 Preventing hotlinking

Configuration Example - Allowing a Specified IP Address to Access Your Website

You can configure two precise protection rules, one to block all requests, as shown in Figure 8, but then another one to allow the access from a specific IP address, as shown in Figure 9.

Figure 8 Blocking all requests
Figure 9 Allowing the access of a specified IP address

Configuration Example - Allowing a Specific IP Address to Access a Certain URL

You can configure multiple conditions in the Condition List field. If an access request meets the conditions in the list, WAF will allow the request from a specific IP address to access a specified URL.

Figure 10 Allowing specific IP addresses to access specified URLs