iptables之layer7

iptables防火墙是工作在网络层,针对TCP/IP数据包实施过滤和限制,属于典型的包过滤防火墙。以基于网络层的数据包过滤机制为主,同时提供少量的传输层、数据链路层的过滤功能。

iptables工作于2,3,4层,本身不具备7层过滤功能,第三方开发者为iptables新增了layer7模块,能检测每一个报文的内部应用层协议是什么,并根据协议对请求进行过滤,但不支持对应用层协议的方法进行过滤

安装netfilter-layer7补丁包的作用是为Linux内核、iptables添加相应的补丁文件,重新编译安装后提供基于应用层(第7层)的扩展功能。通过独立的l7-protocols协议包提供对各种应用层协议的特征识别与定义,便于更新

实现过程

 a.给内核的netfilter打补丁layer7,重新编译内核
 b.给iptables打补丁layer7,重新编译iptables
 c.安装l7-protocols包
 d.使用iptables命令设置应用层规则,禁止QQ等聊天工具

使用的软件包

linux-2.6.35.9.tar.bz2 下载地址
iptables-1.4.21.tar.bz2 下载地址
netfilter-layer7-v2.23.tar.gz 下载地址
l7-protocols-2009-05-28.tar.gz 下载地址

下载好这4个包放在/root目录下

系统环境

1.系统版本,内核版本,iptables版本

[root@localhost ~]# cat /etc/centos-release 
CentOS release 6.9 (Final)
[root@localhost ~]# uname -r
2.6.32-696.el6.x86_64
[root@localhost ~]# rpm -qa | grep iptables
iptables-ipv6-1.4.7-16.el6.x86_64
iptables-1.4.7-16.el6.x86_64

2.编译时候依赖于Development tools” 和”Server Platform Development”两个组件,用yum安装即可

[root@localhost ~]# yum -y groupinstall "Development tools" "Server Platform Development"

给内核打补丁layer7,并重新编译

1.给内核打补丁

[root@localhost ~]# tar xf linux-2.6.35.9.tar.bz2 -C /usr/src
[root@localhost ~]# tar xf netfilter-layer7-v2.23.tar.gz 
[root@localhost ~]# cd /usr/src
[root@localhost src]# ln -sv linux-2.6.35.9 linux    <==为内核创建软链接linux,也可以重命名内核文件为linux,建议创建链接文件
[root@localhost src]# cd linux
[root@localhost linux]# patch -p1 < /root/netfilter-layer7-v2.23/kernel-2.6.35-layer7-2.23.patch    <==给内核打上layer7补丁

2.编译安装内核

[root@localhost linux]# cp /boot/config-2.6.32-696.el6.x86_64 .config   <==拷贝当前系统的config编译配置文件为模板编译内核
[root@localhost linux]# make menuconfig
    Networking support ---> Networking Options ---> Network Packet filtering framework (Netfilter) --->Core Netfilter Configuration
    下的“layer 7”match support选为M
    Networking support ---> Networking Options ---> Network Packet filtering framework (Netfilter)---> Netfilter Configuration
    下除了proc/sysctrl compatibility with old connection tracking为空外,其余均为M

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

[root@localhost linux]# make
[root@localhost linux]# make modules_install
[root@localhost linux]# make install

编译过程非常耗时,可是使用make –j 4加速编译过程,视虚拟机或者物理机的硬件性能而定

3.重启系统,从新版内核启动
这里写图片描述

查看内核版本

[root@localhost ~]# uname -r
2.6.35.9

为iptables打补丁,并重新编译

1.卸载系统自带的iptables
在卸载之前,先把iptables的服务脚本和配置文件备份一下,这样编译安装新版本的iptables可以复制过来再略微修改下就能直接使用

[root@localhost ~]# cp /etc/rc.d/init.d/iptables /tmp
[root@localhost ~]# cp /etc/sysconfig/iptables-config /tmp
[root@localhost ~]# rpm -e iptables iptables-ipv6 --nodeps

2.为iptables打补丁

[root@localhost ~]# tar xf iptables-1.4.21.tar.bz2
[root@localhost ~]# cp netfilter-layer7-v2.23/iptables-1.4.3forward-for-kernel-2.6.20forward/* iptables-1.4.21/extensions/

3.编译iptables并配置

[root@localhost ~]# cd iptables-1.4.21
[root@localhost iptables-1.4.21]# ./configure --prefix=/usr --with-ksource=/usr/src/linux    <==需要指定内核源码文件目录
[root@localhost iptables-1.4.21]# make && make install
[root@localhost ~]# iptables --version    <==查看iptables版本
iptables v1.4.21
[root@localhost ~]# which iptables    <==查看iptables路径,跟脚本文件的路径不一致,所以要修改脚本文件路径
/usr/sbin/iptables
[root@localhost ~]# cp /tmp/iptables-config /etc/sysconfig/
[root@localhost ~]# cp /tmp/iptables /etc/rc.d/init.d/
[root@localhost ~]# vim /iptables /etc/rc.d/init.d/iptables   <==修改脚本iptables路径

这里写图片描述

[root@localhost ~]# service iptables restart

4.为layer7模块提供其所识别的协议的特征码

[root@localhost ~]# tar xf l7-protocols-2009-05-28.tar.gz
[root@localhost ~]# cd l7-protocols-2009-05-28
[root@localhost l7-protocols-2009-05-28]# make install
mkdir -p /etc/l7-protocols
cp -R * /etc/l7-protocols

5.装载所需要的模块
使用layer7模块前,需要启用内核中的ACCT功能

[root@localhost ~]# vim /etc/sysctl.conf
net.netfilter.nf_conntrack_acct = 1
[root@localhost ~]# sysctl -p

注意:此内核参数需要装载了nf_conntrack模块后方能生效(一般都装载了)

测试

假设内网主机由服务器代理上网,为提高工作效率,禁止内网用户登录QQ

代理服务器:虚拟机,系统:CentOS 6.9 x64
10.1.1.20/24(eth0,网卡类型为桥接,可以访问网络),192.168.0.254/24(eth1,网卡类型为Vmnet2)

客户端:虚拟机,系统:Windows 7 x64
192.168.0.20/24(网卡类型为Vmnet2)

192.168.0.0/24网络在Vmnet2内

网络设置

代理服务器
这里写图片描述
网关和DNS均为10.1.1.1

客户端
这里写图片描述

代理服务器开启核心转发功能

[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@localhost ~]# sysctl -p

设置代理服务器防火墙NAT规则,使客户端能访问网络

[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to-source 10.1.1.20

这里写图片描述

验证是否能上网
这里写图片描述
此时是可以登录的,下线QQ,代理服务器设置防火墙规则,禁用QQ

lay7模块的使用语法:

iptables <specify table & chain> -m layer7 --l7proto <protocol name> -j <action>

代理服务器设置规则禁用QQ

[root@localhost ~]# iptables -A FORWARD -m layer7 --l7proto qq -j REJECT

这里写图片描述

此时客户端无法登录QQ了(已登录的QQ不受影响,其它应用也不受影响)
这里写图片描述

再次查看iptables
这里写图片描述
已经有报文匹配到了

至此iptables基于layer7实现应用层过滤以实现,需要禁止其他程序,请自行添加相应规则,支持的程序在/etc/l7-protocols/protocols/目录下

这里写图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值