URL 过滤表达式
URL 过滤表达式
简介
本文介绍 WebFilter 在 URL 过滤时的三种表达式类型:
- 简单(Simple)
- 正则表达式(Regular Expression)
- 通配符(Wildcard)
提示
以下表达式的匹配机制也适用于 DNS Filter 中的 URL 表达式。
简单(Simple)
简单(Simple)的 URL 过滤器条目必须采用标准 URL 格式,可以包含子域名和路径。
举例:fortinet.com,fortinet.com/support、handbook.fortinet.com.cn、net.com等。
当 FortiGate 的防火墙策略处于不同的检测模式(inspection-mode)时,Simple 表达式的匹配方法会有所不同:
- 当防火墙策略配置为流模式(
set inspection-mode flow,默认配置)时:只要 simple 表达式的字符串出现在实际访问的 URL 中(哪怕只是其中一部分),就会判定为匹配。 - 当防火墙策略配置为代理模式(
set inspection-mode proxy)时:只有当实际访问的域名与 simple 表达式完全一致,或是 simple 表达式的子域名时,才会匹配。
| 防火墙策略检测模式 | 匹配规则说明 | Simple 表达式 | 匹配示例✅ | 不匹配示例❌ |
|---|---|---|---|---|
| flow(默认) | 只要 Simple 表达式的字符串出现在实际 URL 中任意位置,即判定为匹配 | net.com | net.comfortinet.comwww.net.comwww.fortinet.com | net1.com |
net.com/prod | www.fortinet.com/products/doc | |||
| proxy | 只有当访问的域名与 Simple 表达式完全一致,或是其子域名时,才算匹配(不检查后边) | net.com | net.comwww.net.comnet.com.cnwww.net.com.cn | forinet.comwww.fortinet.com |
net.com/prod | www.net.com/products/doc | www.fortinet.com/products/doc |
include-subdomains 选项
从 FortiOS 7.6.3 版本开始,当防火墙策略的检测模式配置为代理模式(set inspection-mode proxy)时,可以在使用 simple 条目时控制子域名匹配的选项。
config webfilter urlfilter
edit <id>
set include-subdomains {enable/disable}
next
endenable(默认):simple 表达式会匹配子域名。例如,net.com会匹配www.net.com、www.net.com.cn。disable:仅匹配确切的域名。例如,net.com只会会匹配net.com、net.com.cn,不会匹配www.net.com、www.net.com.cn。
通配符(Wildcard)
通配符(Wildcard)表达式可以根据指定的关键字灵活匹配多个 URL,不需要严格的格式,比 simple 类型更灵活。
| 匹配类型 | 匹配规则说明 | Wildcard 表达式 | 匹配示例✅ | 不匹配示例❌ |
|---|---|---|---|---|
| 基本匹配 | URL 开头左到右扫描,只要任意位置符合规则即匹配成功 | for*guard | fortiguard.com | |
| 中间匹配 | 若要匹配 URL 中间的字符,需要在开头加* | *or*guard | www.fortiguard.com | |
or*guard | www.fortiguard.com | |||
| 结尾匹配 | 条目末尾不需要 *,自动匹配末尾任意字符,*fortinet等效于 *fortinet* | *fortinet | www.fortinet.comsupport.fortinet.comhandbook.fortinet.com.cn | |
| 子域名匹配 | 条目以*.开头,会匹配该域名和所有子域名 | *.fortinet.com | fortinet.comwww.fortinet.comsupport.fortinet.com |
正则表达式(Regex)
正则表达式(Regex)是一种类似 Perl 语法的规则,用来匹配一个或多个特定的 URL,也可以用来排除不符合条件的 URL。
| 正则符号 | 表达式含义 | 正则表达式 | 匹配示例✅ | 不匹配示例❌ |
|---|---|---|---|---|
* | 匹配前一个字符出现 0 次或多次 | fortinet*.com | fortinettttt.com | fortinetsupport.com |
/i | 匹配时区分大小写 | /FORTINET/i | FORTINET | fortinet |
^ | 必须以特定字符串开头 | ^fo | fortinet.com | support.fortinet.comnotfortinet.com |
$ | 必须以特定字符串结尾 | \.com$ | fortinet.com | fortinet.com.cn |
. | 匹配任意单个字符 | fortinet.com | fortinetacomfortinetbcomfortinet.com | fortinetabccom |
.* | 匹配任意字符 | forti.\*com | fortinet.comfortinetabccom fortinet-xyz-123com | |
\ | 转义特殊字符 | fortinet\.com | fortinet.com | fortinetacomfortinetbcom |
| 组合用法 | ^fortinet\.com$ | fortinet.com | www.fortinet.comfortinet.com.cn | |
^forti.*\.com | fortiguard.comfortinet.comfortinet.com.cn | www.fortinet.comfortinet.netfortizcom |
附:
注意事项
带路径的 URL 识别
使用 URL 过滤功能时,如果需要识别的 HTTPS 网页携带目录(例如客户端访问的网页为https://www.baidu.com/more/),由于 FQDN 后的网站目录/more/是在 TLS 协商结束后客户端才会发起 GET 请求,访问/more/目录的 HTTP GET 请求被 TLS 加密,FortiGate 需要开启防火墙策略中 SSL 检测模式需要使用deep-inspection模式才能识别到请求目录/more的 HTTP 报文。
HTTP 网页不需要使用deep-inspection模式。
URL 中携带 #符号
当需要识别的 URL 中携带#字符,例如需要拦截的 url 为http://yundisk.dwstock.com:8686/ksops/#/login。
- 配置为 Simple 格式
http://yundisk.dwstock.com:8686/ksops/#/login时无法保存 ULR Filter 配置,提示错误。 - 当使用通配符格式
http://yundisk.dwstock.com:8686/ksops/*/login时无法实现拦截。
实际上客户端在请求 URL http://yundisk.dwstock.com:8686/ksops/#/login时,#及之后的部分并不会发送给 HTTP Server,如下抓包所示,客户端请求的 URI 并不包含#以及之后的字段/login。

#后的部分被称为 URI fragment,仅在客户端本地浏览器生效,不会发送给 Server(参考:https://en.wikipedia.org/wiki/URI_fragment )。