在RouterOS里如果要拦截一个基于UDP/TCP 53端口明文的DNS解析域名的请求,可以选择 L7 过滤或 DNS 缓存的 static

  • L7 过滤 DNS 请求 是在防火墙里看 DNS 查询报文内容,发现查询域名命中就直接丢包。
  • DNS static 是让 RouterOS 自己作为 DNS 服务器,通过A记录,对指定域名解析返回指定结果,比如返回127.0.0.1。

什么时候用 L7 过滤明文 DNS ?

1、客户端不使用 RouterOS 做 DNS,使用公共DNS查询,如下:

nslookup irouteros.com 223.5.5.5
nslookup mikrotik.com 8.8.8.8

这种请求不会经过 RouterOS 的 DNS 缓存,所以 /ip dns static 不会生效。

2、不想改客户端 DNS,也不想做 DNS redirect

如果你的网络环境无法修改客户端 DNS,也无法配置 DNS redirect 重定向,下面是 nat 重定向 DNS 请求规则

/ip firewall nat
add action=redirect chain=dstnat dst-port=53 protocol=tcp to-ports=53

DNS 请求流量经过 RouterOS 转发,那么L7 可以在 forward 链里识别并丢弃。如果现场环境比较复杂,暂时不能强制重定向 DNS 到路由器,可以先用 L7 做快速拦截。

缺点:

  1. L7 比较吃 CPU;
  2. RouterOS v6/v7 正则行为可能不一致;
  3. 只能处理普通明文 DNS,也就是 UDP/TCP 53;对 DoH/DoT 无效;

下面是RouterOS v7 使用 L7 防火墙过滤明文 DNS 请求示例:

要过滤irouteros.com 和 mikrotik.com ,创建L7 过滤的正则表达式规则

/ip firewall layer7-protocol
add name=l7-block-DNS regexp="(irouteros.*com|mikroitk.*com)"

添加到filter过滤规则中,针对UDP和TCP规则,注意你的filter规则的执行顺序

/ip firewall filter
add chain=forward action=drop protocol=udp dst-port=53 connection-state=new layer7-protocol=l7-block-DNS
add chain=forward action=drop protocol=tcp dst-port=53 connection-state=new layer7-protocol=l7-block-DNS