如果在公司里做网络管理员,老板可能会让你屏蔽掉qq和xunlei,那么如果通过iptables来实现这些功能?
首先,要知道qq和xunlei都是特定的服务,它们在传送数据的时候,必然会由一些特征值在数据中,那么我们的iptables如果想要拦截这些数据,就需要知道它们的数据特征值。而l7-filter就是为了这个目的而存在。(当然,不止qq和xunlei,很多协议它都支持)iptables-save > /etc/sysconfig/iptables.bak
恢复的时候使用
iptables-restore < /etc/sysconfig/iptables.bak1)下载inetfilter包,iptables 1.4.6和内核源代码包,(建议使用iptables 1.4.6版本 内核 2.6.28.10)解压缩:
tar xf linux- -C /usr/src tar netfilter-la -C /usr/src tar iptables -C /usr/src cd /usr/src2)给内核打补丁
ln -sv linux- linux cd linux patch -p1 <../netfilter-layer7-v2.22/kernel-2.6.25-2.6.28-layer7-2.22.patch cp /boot/config-2.6.18-164.el5 ./.config #复制你当前主机的内核配置文件,这样编译的时候只要加上l7模块就可以了 make menuconfig选如下的项
make && make modules install && make install
# 卸载之前先拷贝配置文件 cp /etc/init.d/iptables ~/iptebles cp /etc/sysconfig/iptables-config ~/ # 卸载 rpm -e iptables-ipv6 iptables iptate --nodeps ls /usr/src/iptables-1.4.6/extensions # 里面都是各种iptables的补丁 # 编译时指向自己编译的内核 ./configure --prefix=/usr -with-ksource=/usr/src/linux
# 你可以下载最新版本 解压到/etc后直接make install tar l7-protocols -C /etc cd /etc/l7-protocols make install
ls /etc/l7-protocols/protocol #里面是各种协议的特征值我们来看一下qq的特征值 cat qq.pat # Tencent QQ Protocol - Chinese instant messenger protocol - http://www.qq.com # Pattern attributes: good notsofast fast # Protocol groups: chat # Wiki: http://www.protocolinfo.org/wiki/QQ # Copyright (C) 2008 Matthew Strait, Ethan Sommer; See ../LICENSE # # Over six million people use QQ in China, according to wsgtrsys. # # This pattern has been tested and is believed to work well. # # QQ uses three (two?) methods to connect to server(s?). # one is udp, and another is tcp # udp protocol: the first byte is 02 and last byte is 03 # tcp protocol: the second byte is 02 and last byte is 03 # tony on protocolinfo.org says that now the *third* byte is 02: # "but when I tested on my PC, I found that when qq2007/qq2008 # use tcp protocol, the third byte instead of the second is always 02. # # So the QQ protocol changed again, or I have made a mistake, I wonder # that." # So now the pattern allows any of the first three bytes to be 02. Delete # one of the ".?" to restore to the old behaviour. # pattern written by www.routerclub.com wsgtrsys qq ^.?.?\x02.+\x03$
cp iptables /etc/init.d cp iptables-config /etc/sysconfig/ # 修改iptabels里定义的iptables命令路径为/usr/sbin/$IPTABLES,在第21行 vim /etc/init.d/iptables
iptables -t filter -A FORWARD -s 192.168.10.0/24 -m layer7 --l7-proto qq -j REJECT