透明 DNS 转发器
透明 DNS 转发器
重要
此功能在 FortiOS 7.4.0 及更新版本上支持。
功能介绍
FortiGate 可以使用透明条件 DNS 转发器(Transparent conditional DNS forwarder)拦截特定域名的 DNS 查询并转发到特定 DNS 服务器,或是直接替换查询结果并回复给客户端。例如当客户端配置的 DNS 服务器位于公网时,为了将目标地址解析到本地的服务器 IP,FortiGate 可以拦截请求并将其重新路由到本地 DNS 服务器进行解析(如果配置了 DNS 数据库条目,也可以直接返回 DNS 结果)。
这样可以更好地控制 DNS 请求,尤其是管理员无法管理客户端设置的 DNS 服务器配置时。通过解析服务器的本地 IP 而不是公网 IP 来提高网络效率,也可以用于特定域名在公网 DNS 服务器的解析结果不符合用户预期的情况。
网络拓扑

- 内网 PC 填写的 DNS 服务器均为公网 DNS 服务器的 IP
223.5.5.5。 - FortiGate 通过 VIP 将 Web Server 的端口映射到 WAN1(port1)上。
- Web Server 的域名为
www.fortinet.com.cn,通过公网 DNS 服务器解析的结果为 FortiGate 配置的 VIP 地址。 - 需要实现的效果:
- 外网客户端访问 Web Server 时,直接通过
www.fortinet.com.cn解析到 FortiGate 配置的 VIP IP,通过 FortiGate 的 VIP 映射访问即可。 - 内网客户端访问 Server 时:
- 通过公网的 DNS 服务器 223.5.5.5 解析
www.fortinet.com.cn的 IP 地址时,FortiGate 拦截 DNS 请求,并直接回复 DNS 结果为 Web Server 的内网 IP10.10.2.100,内网客户端直接通过解析到的 Server 内网 IP 访问服务器。 - 其他
fortinet.com.cn域的主机名(如support.fortinet.com.cn)的 DNS 请求也会被 FortiGate 拦截,但转发到内网 DNS Server10.10.2.200进行解析,不直接回复 DNS 请求,也不通过外网的 DNS 服务器解析。
- 通过公网的 DNS 服务器 223.5.5.5 解析
- 外网客户端访问 Web Server 时,直接通过
配置步骤
基础网络配置、上网策略、与 Server 的 VIP 配置(略)。
在 FortiGate 上配置 DNS 数据库(需要先在“系统管理 → 可见功能”中开启“DNS 数据库”)。进入“网络 → DNS 数据库”页面,新建 DNS 数据库。

在弹出的新建窗口中,配置区域名称,域名填写为
fortinet.com.cn,关闭“权威”选项,DNS 转发器填写内网的 DNS 服务器10.10.2.200(没有匹配上 DNS 数据库条目的主机名会被转发至此 DNS 服务器进行解析)。
在 DNS 条目中点击新建按钮,配置主机名为
www,IP 地址为 Web Server 的内网 IP10.10.2.100。
点击确认按钮下发 DNS 数据库配置。

config system dns-database edit "fortinet.com.cn" set domain "fortinet.com.cn" set authoritative disable set forwarder "10.10.2.200" config dns-entry edit 1 set hostname "www" set ip 10.10.2.100 next end next endFortiGate # diagnose test application dnsproxy 8 //查看DNS数据库信息// worker idx: 0 vfid=0 name=fortinet.com.cn domain=fortinet.com.cn ttl=86400 authoritative=0 view=shadow type=primary serial=172498465 refresh=0 forwarder(s): 10.10.2.200 source-ip(s): 0.0.0.0 :: SOA: fortinet.com.cn (primary: dns.fortinet.com.cn, contact: host@fortinet.com.cn, serial: 172498465)(86400) A: www.fortinet.com.cn-->10.10.2.100(86400)如果需要指定 DNS Database 使用的源 IP,例如在 IPSec 环境中,需要指定 DNS 的源 IP 为 Tunnel 接口的 IP,需要在 CLI 中加入如下配置(此例中不需要配置)。
config system dns-database edit "fortinet.com.cn" set source-ip <IPv4_address> next end新建 DNS Filter 配置文件,并通过
transparent-dns-database功能引用上述配置的 DNS Database(在此例中,我们仅使用transparent-dns-database功能,所以关闭了 DNS Filter 的其他过滤功能)。重要
DNS Filter 配置文件中的 transparent-dns-database 可以引用多个 DNS Database。后续删除 DNS Database 时,需要先取消此处的引用。
config dnsfilter profile edit "fortinet.com.cn" config ftgd-dns set options ftgd-disable end set transparent-dns-database "fortinet.com.cn" next end在 FortiGate 上新建防火墙策略,源地址为内网 PC 网段,服务为 DNS 流量,检查模式需要配置为“基于代理”,DNS 过滤器选择上步配置的 DNS Filter 配置文件,SSL 检查选择“certificate-inspection”。注意此条策略放置在上网策略之前)。

config firewall policy edit 1 set name "Conditional_Dns_Forwarder" set srcintf "port2" set dstintf "port1" set action accept set srcaddr "10.10.1.0/24" set dstaddr "all" set schedule "always" set service "DNS" set utm-status enable set inspection-mode proxy set ssl-ssh-profile "certificate-inspection" set dnsfilter-profile "fortinet.com.cn" set nat enable next end
结果验证
公网客户端 PC 使用 DNS 服务器
223.5.5.5解析 Web Server 的域名www.fortinet.com.cn(流量不经过 FortiGate),得到结果为 Server 对应的 VIP 地址39.105.115.226。::: important nslookup www.fortinet.com.cn ::: Server: 223.5.5.5 Address: 223.5.5.5#53 Non-authoritative answer: Name: www.fortinet.com.cn Address: 39.105.115.226内网客户端 PC 使用 DNS 服务器
223.5.5.5解析www.fortinet.com.cn的域名(流量经过 FortiGate),得到结果为 Web Server 内网 IP10.10.2.100。DNS 流量被 FortiGate 拦截,由 FortiGate 代替公网 DNS 服务器回复 DNS 查询结果,这个过程对于客户端是透明的。::: important nslookup -type=a www.fortinet.com.cn 223.5.5.5 ::: 服务器: public1.alidns.com Address: 223.5.5.5 非权威应答: 名称: www.fortinet.com.cn Address: 10.10.2.100内网客户端 PC 使用 DNS 服务器
223.5.5.5解析support.fortinet.com.cn的域名(DNS 数据库条目中不存在的主机名):DNS 流量被 FortiGate 拦截,由 FortiGate 转发至内网 DNS 服务器
10.10.2.200进行解析,这个过程对于客户端是透明的。FortiGate # diagnose sniffer packet any 'host 223.5.5.5 or host 10.10.2.200' 4 Using Original Sniffing Mode interfaces=[any] filters=[host 223.5.5.5 or host 10.10.2.200] ... 7.354793 port2 in 10.10.1.100.65195 -> 223.5.5.5.53: udp 41 7.355069 port3 out 10.10.2.1.3313 -> 10.10.2.200.53: udp 41 7.355655 port3 in 10.10.2.200.53 -> 10.10.2.1.3313: udp 57 7.355764 port2 out 223.5.5.5.53 -> 10.10.1.100.65195: udp 57 ...解析结果为内网 DNS 服务器返回的结果
10.10.2.130,这个结果是内网 DNS 服务器返回的。C:\Users\Administrator.SUMMERICE2019>nslookup -type=a support.fortinet.com.cn 服务器: public1.alidns.com Address: 223.5.5.5 名称: support.fortinet.com.cn Address: 10.10.2.130查看 FortiGate 的 DNS 缓存,已经记录了
support.fortinet.com.cn的 DNS 缓存信息,在 TTL(最后一个:符号后的数字)结束前,客户端再次请求该域名时,FortiGate 会直接响应 DNS 请求。FortiGate # diagnose test application dnsproxy 7 worker idx: 0 ... vfid=0, name=support.fortinet.com.cn, ttl=86400:86250:1650 10.10.2.130 (ttl=86400) ...