Iptables防火墙string模块扩展匹配规则

 更新时间:2022年08月10日 14:06:14   作者:jiangxl  
这篇文章主要为大家介绍了Iptables防火墙string模块扩展匹配规则详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

Iptables防火墙string模块扩展匹配规则

String模块的作用是来匹配请求报文中指定的字符串,经常应用于拦截用户访问某些网站的场景,将防火墙当做路由器使用,例如上班时间不允许用户访问淘宝网站等等场景。

String模块的常用参数:

--string pattern:指定要匹配的字符串。

! --string pattern:反向匹配。

--algo:指定匹配的查询算法,有bm和kmp两种算法。

可以在参数前面加!号表示去反。

案例:当用户请求的数据报文中包含taobao.com,则拒绝通行,其余的正常放行。

用户通过防火墙流出然后访问目标端,因此需要在OUTPUT链添加规则。

1)首先来准备含有taobao.com数据报文的WEB网站。

1.安装httpd
[root@jxl-1 ~]# yum -y install httpd
[root@jxl-1 ~]# echo hahaha > /var/www/html/ha.html
[root@jxl-1 ~]# echo taobao.com > /var/www/html/taobao.html
[root@jxl-1 ~]# systemctl restart httpd
2.正常的两个页面
[root@jxl-1 ~]# curl 127.0.0.1/ha.html
hahaha
[root@jxl-1 ~]# curl 127.0.0.1/taobao.html
taobao.com

2)编写防火墙规则,拒绝数据包中包含taobao.com内容的数据包。

[root@jxl-1 ~]# iptables -t filter -I OUTPUT -p tcp -m string --string "taobao.com" --algo bm -j DROP

3)查看设置的防火墙规则。

[root@jxl-1 ~]# iptables -L -n -v --line-number
Chain INPUT (policy ACCEPT 7011 packets, 6693K bytes)
num   pkts bytes target     prot opt in     out     source               destination         
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
Chain OUTPUT (policy ACCEPT 7310 packets, 11M bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            STRING match  "taobao.com" ALGO name bm TO 65535

4)测试效果,发现hahaha的页面可以正常访问,taobao.com页面的请求将被拦截。

以上就是Iptables防火墙string模块扩展匹配规则的详细内容,更多关于Iptables防火墙string模块的资料请关注程序员之家其它相关文章!

相关文章

最新评论

?


http://www.vxiaotou.com