前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >工控网络基础入门篇之iptables 的 string 模块

工控网络基础入门篇之iptables 的 string 模块

作者头像
剑指工控
发布2021-11-09 14:42:50
1.2K0
发布2021-11-09 14:42:50
举报
文章被收录于专栏:剑指工控

通过上一节的介绍,我们也看到了u32模块第一个缺点是只能针对特定位置的 4 个字节数据进行分析,如果位置不固定就没有办法了。如果要对位置不固定的数据进行分析,我们就要使用 string 模块,它可以帮我们在一个 IP 包里搜索任意位置,看是否有匹配的字符串。当然 u32 的固定位置分析也不能算是完全的缺点,只针对固定位置固定长度的数据分析意味着 u32 模块消耗的 CPU 资源非常少,因为我们基本上只需要做一些简单的与或操作和比较计算就可以了。而 string 模块强大的搜索功能,其代价就是消耗了更多的 CPU 资源。

和 u32 一样,string 模块搜索 IP 时也是使用 16 进制,因为原始数据就是这样的。因为计算复杂度很高,一条 string 匹配的 iptables 命令只能一次只能匹配一个 IP(字符串),所以我们得写 48 条命令:

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|042442B2|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|0807C62D|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|1759053C|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|253D369E|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|2E52AE44|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|31027B38|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|364C8701|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|3B1803AD|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|402158A1|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|4021632F|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|4042A3FB|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|4168CAFC|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|41A0DB71|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|422DFCED|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|480ECD63|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|480ECD68|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|4A7D2766|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|4A7D2771|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|4A7D7F66|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|4A7D9B66|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|4D04075C|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|4E10310F|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|5D2E0859|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|76053106|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|80797E8B|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|9F6A794B|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|A9840D67|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|BC050460|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|BDA31105|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|C043C606|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|C504040C|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|CA6A0102|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|CAB50755|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|CB620741|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|CBA1E6AB|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|CF0C5862|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|D0381F2B|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|D1244921|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|D155E58A|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|D1913632|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|D1DC1EAE|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|D35E4293|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|D5A9FB23|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|D8DDBCB6|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|D8EAB30D|" --from

60

--to

180

-j

DROP

iptables

-t

mangle

-I

PREROUTING -p udp --sport 53 -m string

--algo bm --hex - string "|F3B9BB27|" --from

60

--to

180

-j

DROP

iptables

-t mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|F9812E30|" --from

60

--to

180

-j

DROP

iptables

-t mangle

-I

PREROUTING -p udp --sport 53 -m string --algo bm --hex - string "|FD9D0EA5|" --from

60

--to

180

-j

DROP

相对来说 string 模块的命令就好理解多了,

• -m string

表示启用 string 模块进行匹配,

• –algo bm

表示启用贝叶 (Boyer-Moore) 字符串搜索算法,另一种算法是 kmp(Knuth-Pratt-Morris),具体应该只是效率和不同应用的区别,算法原理我们就不细究了。

• –hex-string

表示要匹配 HEX 格式的 IP 地址了,注意这里前面不要有 0x 表示 16 进制了,直接写 16 进制数据就行,后面跟着的就是 IP 地址的 16 进制格式。

• –from 60 –to 180

这表示的是搜索的偏置范围 (offset),从第 60 字节开始到 180 字节结束,一般来说 DNS 返回包的长度很少有超过 180 字节,而 A 记录 IP 值的位置因为 IP 和 UDP 协议报文格式的关系,基本上不可能出现在 60 字节之前,所以指定这个范围是足够的,同时也可以大大减轻搜索算法的运算压力。如果你担心有什么奇葩 DNS 返回的结果在这范围之外,删除这两个偏置选项就行了,这样会默认搜索整个字符串,但相应的搜索消耗的 CPU 资源就更多了。

和 u32 模块的使用一样,把这些规则保存为防火墙规则就可以过滤所有境内和境外 DNS 中的污染和劫持信息了,要注意 string 模块的过滤其实已经包含了 u32 模块的功能,不需要再添加 u32 模块的过滤规则了。

但是境内 DNS 的原始数据都是污染的,你过滤掉这些污染的信息后也不会给你返回正确的信息。而你希望的结果肯定是同时享受境内 DNS 的高速解析速度,又可以让境外 DNS 给我们某些敏感网站的正确解析结果。要实现这样的组合,就要在 iptables 的过滤规则之下,继续配合下一节提到的 Dnsmasq了。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-09-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 剑指工控 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档