使用 WAF 自定义规则
WAF 自定义规则通过针对 HTTP 流量和应用自定义标准来阻止、质疑、记录或允许某些请求,从而提供强大的功能和灵活性。
您可以创建多种类型的 WAF 自定义规则。 但是,站点上的活动规则数受客户套餐的限制。 请参阅 CIS 计划,了解有关权益的更多信息。
每个套餐的活动规则数是固定的。 目前,您无法购买更主动的规则。
在开始之前,最好回顾一下 使用字段、函数和表达式。
您可以使用用户界面、CLI、API 或 Terraform 创建、更新和删除自定义规则。
在控制台中使用 WAF 自定义规则
在控制台中创建自定义规则
请按照以下步骤在控制台中创建自定义规则:
WAF自定义规则使用现有的防火墙规则页面进行配置。 之前在您的域上创建的任何旧版防火墙规则都会自动转换为WAF自定义规则。
-
导航至安全性 > 防火墙规则。
-
单击创建。
-
输入可选描述。
-
(可选)根据需要输入优先级。 优先级为零表示优先级为空,且最后评估。
-
使用入局请求部分中的 UI 构建器来添加条件。 要构建具有多个条件的表达式,请单击以下任一项:
- 以及 ——评估使用条件_和_逻辑
- 或--评估使用_或_逻辑的条件或先前_和_的条件组
在构建条件时可以看到“表达式预览”以纯文本显示表达式。
在“表达式预览”中,可以单击以手动编辑表达式,而不使用“直观表达式构建器”,也可以在这两者之间切换。 不过,根据手动构建表达式的复杂程度,Visual Expression Builder 可能无法呈现该表达式。
-
从 “响应”列表菜单中选择一项操作。
-
要保存规则,请通过单击以下任一选项来选择最适用的选项:
- 保存为草稿以保存您的规则,但将其禁用。
- 保存并部署以保存规则并激活它。
在控制台中更新自定义规则
按照以下步骤更新控制台中的现有自定义规则:
- 导航至安全性 > 防火墙规则。
- 在防火墙规则表中,找到要修改的规则,然后单击该行右侧的操作菜单。
- 选择编辑。
- 更改规则。
- 要保存规则,请通过单击以下任一选项来选择最适用的选项:
- 保存为草稿以保存您的规则,但将其禁用。
- 保存并部署以保存规则并激活它。
要暂停或激活现有规则列表中的任何规则,请单击已启用切换按钮。
在控制台中删除自定义规则
按照以下步骤删除控制台中的现有自定义规则:
- 导航至安全性 > 防火墙规则。
- 在防火墙规则表中找到要修改的规则,然后单击该行右侧的操作菜单。
- 选择删除。
- 确认要执行规则删除操作。
通过 CLI 使用 WAF 自定义规则
通过 CLI 创建自定义规则
要通过 CLI 创建自定义规则,请按照以下步骤操作:
-
使用 CLI 登录您的账户。 输入密码后,系统会提示输入要使用的账户和地区:
ibmcloud login --sso
-
运行以下命令创建自定义规则:
ibmcloud cis custom-waf rule-create DNS_DOMAIN_ID --match EXPRESSION --action ACTION [--description DESCRIPTION] [--enabled true|false] [-i, --instance INSTANCE] [--output FORMAT]
ibmcloud cis custom-waf rule-create DNS_DOMAIN_ID ((--json @JSON_FILE | JSON_STRING) [-i, --instance INSTANCE] [--output FORMAT]
Where:
`DNS_DOMAIN_ID`
: The ID of DNS domain.
`--match`
: Specifies the conditions that must be matched for the rule to run. For match value, reference documentation `https://cloud.ibm.com/docs/cis?topic=cis-fields-and-expressions`
`--action`
:The rule action to perform. Valid values: "block", "challenge", "js_challenge", "managed_challenge", "log", "skip". For "block" and "skip" actions, use JSON file or JSON string instead.
`--enabled`
: Indicates if the rule is active. Default is "false".
`--description`
: A brief description of the rule.
`--json`
: The JSON file or JSON string used to describe a custom rule.
- The required fields in JSON data are `expression`, `action`.
`expression`: Specifies the conditions that must be matched for the rule to run.
`action`: The rule action to perform. Valid values: "block", "challenge", "js_challenge", "managed_challenge", "log", "skip".
- The optional fields are `description`, `enabled`, `logging`, `action_parameters`.
`action_parameters`: The rule action parameters.
`ruleset`: Skip all remaining rules or one or more WAF managed rulesets. Valid value: `current`.
`phases`: Skips WAF components for matching requests. Valid values: "http_ratelimit", "http_request_firewall_managed", "http_request_sbfm".
`products`: Skips specific security products for matching requests. Valid values: "waf", "rateLimit", "securityLevel", "hot", "bic", "uaBlock", "zoneLockdown".
`response`: Define a custom response for 'block' action.
`status_code`: Choose an HTTP status code for the response, in the range 400-499.
`content_type`: The content type of a custom response. Valid response types are :`text/html`,`text/plain`, `application/json`, `text/xml`.
`content`: The response body.
`description`: Briefly describes the rule.
`enabled`: Indicates if the rule is active.
`logging`: Log requests matching the skip rule. This field is only available for the "skip" action.
- `enabled`: When disabled, matched requests don't appear in firewall events.
Sample JSON data:
{
"description": "test-custom-rule",
"expression": "(http.cookie contains \"test\")",
"action": "skip",
"logging": {
"enabled": true
},
"action_parameters": {
"ruleset": "current",
"phases": [
"http_ratelimit",
"http_request_firewall_managed",
"http_request_sbfm"
],
"products": [
"waf",
"rateLimit",
"securityLevel",
"hot",
"bic",
"uaBlock",
"zoneLockdown"
]
},
"enabled": true
}
`-i, --instance`
: Instance name or ID. If not set, the context instance specified by `ibmcloud cis instance-set INSTANCE` is used.
`--output`
: Specify output format, only `JSON` is supported.
### Updating a custom rule from the CLI {: #update-custom-rule-cli}
Run the following command to update a custom rule in the CLI:
```sh {: pre}
ibmcloud cis custom-waf rule-update DNS_DOMAIN_ID [--match EXPRESSION] [--action ACTION] [--description DESCRIPTION] [--enabled true|false] [-i, --instance INSTANCE] [--output FORMAT]
ibmcloud cis custom-waf rule-update DNS_DOMAIN_ID (--json @JSON_FILE | JSON_STRING) [-i, --instance INSTANCE] [--output FORMAT]
其中:
DNS_DOMAIN_ID
- DNS 域的 ID。
RULE_ID
- 规则的 ID。
--match
- 指定规则运行时必须匹配的条件。 有关匹配值,请参考文档
https://cloud.ibm.com/docs/cis?topic=cis-fields-and-expressions
。
--action
:要执行的规则操作。 有效值:"block"、"challenge"、"js_challenge"、"managed_challenge"、"log"、"skip"。对于 "block "和 "skip "操作,请使用 JSON 文件或 JSON 字符串代替。
--enabled
-
指示规则是否处于活动状态。 默认为 "false"。
--description
-
规则的简要说明。
--json
-
用于描述定制规则的 JSON 文件或 JSON 字符串。
-
JSON 数据中的必填字段是
expression
,action
。expression
:指定规则运行时必须匹配的条件。action
: 要执行的规则操作。 有效值:"block"、"challenge"、"js_challenge"、"managed_challenge"、"log"、"skip"。 -
可选字段为
description
,enabled
,logging
,action_parameters
。action_parameters
:规则操作参数。ruleset
: 跳过所有剩余规则,或者跳过一条或多条 WAF 托管规则集。 有效值:current
。phases
: 跳过 WAF 组件以匹配请求。 有效值:"http_ratelimit"、"http_request_firewall_managed"、"http_request_sbfm"。products
: 跳过特定安全产品的匹配请求。 有效值:"waf", "rateLimit", "securityLevel", "hot", "bic", "uaBlock", "zoneLockdown"。response
: 为“阻止”操作定义定制响应。status_code
: 在 400-499 范围内为响应选择 HTTP 状态码。content_type
: 自定义回复的内容类型。 有效的响应类型为:text/html
,text/plain
,application/json
,text/xml
。content
:回复正文。description
: 简要说明规则。enabled
: 指示规则是否处于活动状态。logging
: 与跳过规则匹配的日志请求。 该字段仅适用于“跳过”操作。enabled
注意:禁用时,匹配的请求不会出现在防火墙事件中。
样本 JSON 数据:
{ "description": "test-custom-rule", "expression": "(http.cookie contains \"test\")", "action": "block", "action_parameters": { "response": { "status_code": 429, "content_type": "text/xml", "content": "reject" } }, "enabled": true }
-
-i, --instance
-
实例名称或标识。 如果未设置,则使用
ibmcloud cis instance-set INSTANCE
指定的上下文实例。 --output
-
指定输出格式,仅支持
JSON
。
从 CLI 中删除自定义规则
运行以下命令在 CLI 中删除自定义规则:
ibmcloud cis custom-waf rule-delete DNS_DOMAIN_ID RULE_ID [-f, --force] [-i, --instance INSTANCE] [--output FORMAT]
其中:
DNS_DOMAIN_ID
- DNS 域的 ID。
RULE_ID
- 自定义规则的 ID。
-i, --instance
- 实例名称或标识。 如果未设置,则使用
ibmcloud cis instance-set INSTANCE
指定的上下文实例。 -f, --force
- 尝试在不提示确认的情况下删除定制规则。
--output
- 指定输出格式,仅支持
JSON
。
命令示例
-
要创建自定义规则:
ibmcloud cis custom-waf rule-create 601b728b86e630c744c81740f72570c3 --action challenge --description "rule 1" --enabled true --match "(http.host eq \"www.example.com\")"
-
更新自定义规则:
ibmcloud cis custom-waf rule-update 601b728b86e630c744c81740f72570c3 4d37cb6f87654e96a18bc531628a4d27 --enabled true
-
要删除自定义规则:
ibmcloud cis custom-waf rule-delete 601b728b86e630c744c81740f72570c3 4d37cb6f87654e96a18bc531628a4d27
通过应用程序接口使用 WAF 自定义规则
获取 API 的自定义规则入口点
所有自定义规则 API 操作都需要 RULESET_ID
自定义规则阶段的入口点规则集。 该入口点规则集可能已经存在,如果不存在,则需要创建。
按照以下步骤获取自定义规则入口点规则集:
-
使用正确的变量设置 API 环境。
-
在变量中存储以下值,以便在 API 命令中使用:
CRN
:服务实例的完整 URL 编码云资源名称 (CRN)。ZONE_ID
:域名 ID。 -
启动所有变量后,获取入口点规则集:
curl -X GET "https://api.cis.cloud.ibm.com/v1/$CRN/zones/$ZONE_ID/rulesets/phases/http_request_firewall_custom/entrypoint" \
--header "X-Auth-User-Token: Bearer <API_TOKEN>" \
--header "Content-Type: application/json"
规则集 ID 将出现在成功请求的响应中。 如果上述调用返回 404 Not Found 响应,请使用以下 API 为自定义规则阶段创建入口点规则集:
curl -x POST https://api.cis.cloud.ibm.com/v1/$CRN/zones/$ZONE_ID/rulesets \
--header "X-Auth-User-Token: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"name": "Zone-level phase entry point",
"kind": "zone",
"description": "Custom rule entry point ruleset.",
"phase": "http_request_firewall_custom"
}'
使用 API 创建自定义规则
请按照以下步骤使用 API 创建自定义规则:
-
使用正确的变量设置 API 环境。
-
在变量中存储以下值,以便在 API 命令中使用:
CRN
:服务实例的完整 URL 编码云资源名称 (CRN)。ZONE_ID
:域名 ID。RULESET_ID
:自定义规则入口点规则集的 ID。 -
启动所有变量后,创建自定义规则:
curl -X POST "https://api.cis.cloud.ibm.com/v1/$CRN/zones/$ZONE_ID/rulesets/$RULESET_ID/rules" \
--header "X-Auth-User-Token: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"description": "My custom rule with plain text response",
"expression": "(ip.src.country eq \"GB\" or ip.src.country eq \"FR\") and cf.waf.score lt 20",
"action": "block",
"action_parameters": {
"response": {
"status_code": 403,
"content": "Your request was blocked.",
"content_type": "text/plain"
}
}
}'
使用 API 更新自定义规则
请按照以下步骤使用 API 更新现有的自定义规则:
-
使用正确的变量设置 API 环境。
-
在变量中存储以下值,以便在 API 命令中使用:
CRN
:服务实例的完整 URL 编码云资源名称 (CRN)。ZONE_ID
:域名 ID。RULESET_ID
:自定义规则入口点规则集的 ID。RULE_ID
:要修改的自定义规则的 ID。 -
启动所有变量后,更新自定义规则:
curl -X PATCH "https://api.cis.cloud.ibm.com/v1/$CRN/zones/$ZONE_ID/rulesets/$RULESET_ID/rules/$RULE_ID" \
--header "X-Auth-User-Token: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"enabled": false,
"description": "block GB and FR or based on IP Reputation (temporarily disabled)"
}'
使用 API 删除自定义规则
请按照以下步骤使用 API 删除现有自定义规则:
-
使用正确的变量设置 API 环境。
-
在变量中存储以下值,以便在 API 命令中使用:
CRN
:服务实例的完整 URL 编码云资源名称 (CRN)。ZONE_ID
:域名 ID。RULESET_ID
:自定义规则入口点规则集的 ID。RULE_ID
:要修改的自定义规则的 ID。 -
启动所有变量后,删除自定义规则:
curl -X DELETE "https://api.cis.cloud.ibm.com/v1/$CRN/zones/$ZONE_ID/rulesets/$RULESET_ID/rules/$RULE_ID" \
--header "X-Auth-User-Token: Bearer <API_TOKEN>" \
--header "Content-Type: application/json"
使用 Terraform 处理 WAF 自定义规则
使用 Terraform 创建自定义规则
下面的示例使用 Terraform 创建了一个自定义规则:
# First get the entrypoint ruleset ID for the phase `http_request_firewall_custom`.
data "ibm_cis_ruleset_entrypoint_versions" "test"{
cis_id = ibm_cis.instance.id
domain_id = data.ibm_cis_domain.cis_domain.domain_id
phase = "http_request_firewall_custom"
}
# To create a custom rule:
resource ibm_cis_ruleset_rule "config" {
cis_id = ibm_cis.instance.id
domain_id = data.ibm_cis_domain.cis_domain.domain_id
ruleset_id = "data.ibm_cis_ruleset_entrypoint_versions.ruleset_id"
rule {
action = "block"
description = "var.description"
expression = "true"
enabled = "false"
action_parameters {
response {
status_code = var.status_code
content = var.content
content_type = "text/plain"
}
}
position {
index = var.index
after = <id of any existing rule>
before = <id of any existing rule>
}
}
}
有关参数和属性的更多信息,请参阅 ibm_cis_ruleset_rule
中的
通过修改前面用于创建自定义规则的示例并运行 terraform apply
命令,即可使用 Terraform 更新自定义规则。 要删除规则,只需删除配置并运行 terraform apply
。