我现在iptables阻止所有的UDP通信,但是我想只允许某些DNS查询通过。
我们以google.com为例。
我正在尝试使用string匹配来查找请求中的域名,并允许它。 这是我想出来的。
iptables -A OUTPUT -o eth0 -p udp --sport 53 -m string --string "google.com" --algo bm -j ACCEPT
我也试过 – --dport 53而不是 – --sport 。 没有骰子。
如果有人知道如何做,或看看我错了哪里?
点“”。 在DNS查询中不表示为字符,而是表示为随后的string的长度。 例如www.google.com被查询为
0x03 www 0x06 google 0x03 com
您可以通过将域名与–hex-string匹配来轻松地允许/阻止DNS查询。 在你的情况下:
-m string --algo bm --hex-string '|06 676f6f676c65 03 636f6d|' -j ACCEPT
将接受每个包含“.google.com”的DNS数据包。
我经常使用这种技术来对付DNS查询放大攻击。
来源: DNS RFC 1035
为了补充nrc的anwser,其中是一个将域转换为hexstring的快速命令:
DOMAIN=google.com perl -e 'print map {chr(length($_)).$_} split /\./, "'$DOMAIN'" | xxd -p
所以,在你的情况下:
DOMAIN=google.com HEX=$(perl -e 'print map {chr(length($_)).$_} split /\./, "'$DOMAIN'"' | xxd -p) iptables -A OUTPUT -o eth0 -p udp --sport 53 \ -m string --hex-string "|$HEX|" --algo bm -j ACCEPT