iptables好是好,但是对于七层是上面的过滤本身不支持,需要安装第三方的模块方可支持。
L7-filter是一个iptables的外挂模块,它可以过滤很多种的L7协议,这样能够封杀P2P和及时通软件。
1.当前软件版本
OS:RedHat Enterprise Linux 5
kernel:2.16.25
iptables:1.3.5
2.软件下载
kernel:2.6.25
http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.25.tar.bz2
iptables:
http://www.netfilter.org/projects/iptables/files/iptables-1.4.0.tar.bz2
l7-filter:
http://sourceforge.net/project/showfiles.php?group_id=80085
netfilter-layer7-v2.20.tar.gz
l7-protocols-2008-04-23.tar.gz
3.给内核打L7-filter补丁
#tar zxvf netfilter-layer7-v2.20.tar.gz
#ar xvjf linux-2.6.25.tar.bz2
#ln -s linux-2.6.25 linux
#cd linux
#patch -p1 < /usr/src/netfilter-layer7-v2.20/kernel-2.6.25-layer7-2.20.patch
4.编译
#make oldconfig
#make menuconfig
使用make oldconfig可以继承老的kernel的配置,为自己的配置省去很多麻烦。
make menuconfig是文字界面下推荐一种方式,在这里可以选择你需要编译到核心的模块。
因为前面make oldconfig已经很多都继承老的配置,所以一般配置不要动。
有两项需要注意:
第一项:(在我的版本里面默认就是选上的)
General setup --->features --->
Prompt for development and/or incomplete code/drivers
第二项:(选择iptables里面关于L7filter的)
Networking ---> Networking options --->
Network packet filtering framework (Netfilter) --->
Advanced netfilter configuration
--- Network packet filtering framework (Netfilter)
[ ] Network packet filtering debugging
Advanced netfilter configuration>
Bridged IP/ARP packets filtering-->
Core Netfilter Configuration --->
IP: Netfilter Configuration --->
Bridge: Netfilter Configuration --->
Core Netfilter Configuration --->
里面有很多的选项,如果不知道选哪个,那就全选好了。
IP: Netfilter Configuration --->
<M> IP tables support (required for filtering/masq/NAT)
这项一定要选择
#make
#make modules
#make modules_install
#make install
#reboot
#uname -a
Linux mylinux 2.6.25 #1 SMP Wed Sep 10 03:41:14 CST 2008 i686 i686 i386 GNU/Linux
5.给iptables打补丁
#cd /usr/src
#tar xzjf iptables-1.4.0.tar.bz2
#tar xjvf iptables-1.4.0.tar.bz2
#cd netfilter-layer7-v2.20
# patch p1 < /usr/src/netfilter-layer7-v2.20/iptables-1.4-for-kernel-2.6.20forward-layer7-2.20.patch
# chmod +x extensions/ .layer7-test
# export KERNEL_DIR=/usr/src/linux-2.6.25
# export IPTABLES_DIR=/usr/src/iptables-1.4.0
#make BINDIR=/sbin LIBDIR=/lib MANDIR=/usr/share/man install
# iptables -V
iptables v1.4.0
6.安装l7-protocol
#cd /usr/src/
# tar xzvf l7-protocols-2008-04-23.tar.gz
#cd l7-protocols-2008-04-23
# make install
其实就是把响应的目录copy到 /etc/l7-protocols/
真正调用的是/etc/l7-protocols/protocols/下面的文件
可以打开下面具体的文件,里面是一些L7程序特征码的正则表达式形式
这样自己也可以按照这样的样子,写自己的特征码。
7.测试
iptables -I FORWARD -p udp --dport 53 -m string --string "tencent" --algo bm -j DROP
iptables -I FORWARD -p tcp -m multiport --dport 80,443 -m layer7 --l7proto qq -j DROP
iptables -I FORWARD -p udp --dport 8000 -j DROP
iptables -I FORWARD -p tcp -m layer7 --l7proto socks -j DROP
iptables -I FORWARD -p udp --dport 53 -m string --string "messenger" --algo bm -j DROP
iptables -I FORWARD -p tcp -m multiport --dport 80,443 -m layer7 --l7proto msnmessenger -j DROP
iptables -I FORWARD -p udp --dport 1863 -j DROP
经过测试:
上面的表达式可以禁止掉qq和msn
附注:
lsmod可以查看当前加载的模块
modprobe可以加载模块
和iptables相关的模块在下面两个目录
/lib/modules/2.6.25/kernel/net/netfilter/
/lib/modules/2.6.25/kernel/net/ipv4/netfilter/
[ 本帖最后由 sorghum 于 2008-9-11 12:33 编辑 ]