最新消息:

iptables 封禁 dns请求

DNS admin 4468浏览 0评论

Iptables使用string match的--string选项是无法直接匹配dns查询中的域名进行操作的。

我们先抓包看下

 # tcpdump -i lo udp port 53 -vv -nn -X
tcpdump: listening on lo, link-type EN10MB (Ethernet), capture size 65535 bytes
16:04:06.176000 IP (tos 0x0, ttl 64, id 41951, offset 0, flags [none], proto UDP (17), length 66)
    127.0.0.1.56885 > 127.0.0.1.53: [bad udp cksum 546f!] 51618+ A? testdns.topjishu.com. (38)
  0x0000:  4500 0042 a3df 0000 4011 d8c9 7f00 0001  E..B....@.......
  0x0010:  7f00 0001 de35 0035 002e fe41 c9a2 0100  .....5.5...A....
  0x0020:  0001 0000 0000 0000 0774 6573 7464 6e73  .........testdns
  0x0030:  0874 6f70 6a69 7368 7503 636f 6d00 0001  .topjishu.com...
  0x0040:  0001                                     ..

可以看到, 域名 testdns.topjishu.com 的dns查询包,并不是常规的对这个域名做hex处理,其中并不包含点字符(dot character).

dns包中,不包含点字符,而是对点字符进行分割,每部分的开始处加入这一段的字符长度。

  • testdns  长度  7
  • topjishu 长度 8
  • com  长度 3

使用 iptables –hex-string 来封禁域名

--hex-string "|07|testdns|08|topjishu|03|com"

iptables封禁命令

iptables -I INPUT -p udp --dport 53 -m string --hex-string "|07|testdns|08|topjishu|03|com" --algo bm --to 1480 -j DROP
iptables -I INPUT -p tcp --dport 53 -m string --hex-string "|07|testdns|08|topjishu|03|com" --algo bm --to 1480 -j DROP

 

另外,这里--algo指定匹配字符串的算法

--algo {bm|kmp}
Select the pattern matching strategy. (bm = Boyer-Moore, kmp = Knuth-Pratt-Morris)

有两个选择,bm算法和kmp算法,kmp算是一个众所周知的字符串匹配算法了,而bm是另一个更高效、精妙的算法。

详细可以看看:

参考:

转载请注明:爱开源 » iptables 封禁 dns请求

您必须 登录 才能发表评论!