iptables - 如何从 tcpdump 输出中获取 from..to 偏移量以与 iptables 字符串匹配模块一起使用

标签 iptables tcpdump netfilter

我正在尝试使用字符串匹配模块获取“from”和“to”偏移量以在我的 iptables 规则中使用。这是我从 tcpdump 工具输出的数据包:

listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
05:49:49.631211 IP xxx.xxx.xxx.xxx.57625 > xxx.xxx.xxx.xxx.13333: Flags [S],   seq 1036987151, win 29200, options [mss 1460,sackOK,TS val 770422252 ecr 0,nop,wscale 7], length 0
0x0000:  4514 003c 9ee3 4000 3a06 9772 bca5 2e4d  E..<..@.:..r...M
0x0010:  b009 6f56 e119 2f4f 3dcf 2b0f 0000 0000  ..oV../O=.+.....
0x0020:  a002 7210 6e7e 0000 0204 05b4 0402 080a  ..r.n~..........
0x0030:  2deb b5ec 0000 0000 0103 0307            -........... 

我正在寻找它们的开始和结束位置的十六进制单位是:

bca5 2e4d b009 6f56

我的目标是让这个 iptables 规则正常工作:

 iptables -A INPUT -p tcp --dport 13333 -m string --from xx --to yy --algo bm --hex-string "|bca52e4db0096f56|" -j DROP

顺便说一句,我的规则在不使用从到偏移量的情况下已经可以正常工作了。

任何帮助将不胜感激。 最好的问候。

最佳答案

要回答您原来的问题,您可以使用:

 iptables -A INPUT -p tcp --dport 13333 -m string --from 12 --to 20 --algo bm --hex-string "|bca52e4db0096f56|" -j DROP

但是,如果你想通过添加这两个选项来提高 iptables 的效率,还有更多的东西可以得到最终的答案。

我的发现有点令人失望,似乎 --to 选项不起作用。

我在CentOS release 6.5 (Final)中粘贴了man iptables的原文相关内容:

--from offset
Set the offset from which it starts looking for any matching. If not passed, default is 0.
--to offset
Set the offset from which it starts looking for any matching. If not passed, default is the packet size.

如您所见,选项--to的描述是错误的,我实验使用--to选项的结果也是错误的。但是,--from 选项与描述一样工作正常。

最后的答案是坏包部分代表ip协议(protocol)的src ip和dest ip,所以你可以用(我在你的问题中没有使用你包内容中的ip对于您的隐私问题,但 ip 已经暴露,所以也许您可以更改您的问题):

 iptables -A INPUT -p tcp --dport 13333 -s xx.xx.xx.xx -d xx.xx.xx.xx -j DROP

更新,使用模块u32:

iptables -A INPUT -p tcp --dport 13333 -m u32 --u32 "12=0xbca52e4d && 16=0xb0096f56" -j DROP

关于iptables - 如何从 tcpdump 输出中获取 from..to 偏移量以与 iptables 字符串匹配模块一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37781903/

相关文章:

c - 修改抓包包头

c - 使用 Netfilter 封装和解封装 IPv4 数据包

linux - 为每个进程/服务创建 iptables 规则

c - netfilter_queue 虚假数据包

iptables 端口重定向不适用于本地主机

linux - 为什么sudo下运行的tcpdump超时不能生效?

python - 从 tcpdump 中剥离有效负载?

Linux Fedora 19 - iptables 启动失败

openwrt 上的 Tcpdump

networking - NetfilterQueue 实现