您的位置:首页 > 运维架构 > Linux

Linux里的防火墙(下):iptables的扩展模块——l7-filter的安装与功能实现

2011-08-21 17:38 633 查看
如果在公司里做网络管理员,老板可能会让你屏蔽掉qq和xunlei,那么如果通过iptables来实现这些功能?

首先,要知道qq和xunlei都是特定的服务,它们在传送数据的时候,必然会由一些特征值在数据中,那么我们的iptables如果想要拦截这些数据,就需要知道它们的数据特征值。而l7-filter就是为了这个目的而存在。(当然,不止qq和xunlei,很多协议它都支持)

iptables可以支持很多模块,而l7-filter 是一个iptabels的补丁。但是要想给iptables添加这项扩展功能,就需要给linux系统添加l7模块,也就是说需要重新编译内核。

下面我们就来实现这个过程。虽然过程复杂,但是我们可以给iptables在企业里的过滤田间很多功能。

lf-filter的官方网站:l7-filter-sourceforge.net l7-filter.clearfoundation.com

# sourceforge.net 世界上最大的开源项目网站,有数十万个开源项目。

要实现编译配置l7-filter, 我们需要准备:

Linux 2.6内核(l7并不能很好的支持新内核)

l7-protocols

iptables-1.4.6

netfilter-layer7-v2.22

实现过程:

0)先保存下本机的iptables表,如果有的话

iptables-save > /etc/sysconfig/iptables.bak

恢复的时候使用

iptables-restore < /etc/sysconfig/iptables.bak


1)下载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/src
2)给内核打补丁

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


2.编译并且安装完成后(需要重启加载新内核),卸载原有版本iptables,安装新版本

# 卸载之前先拷贝配置文件
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


3. 安装l7-protocols-2009-05-28.tar.gz

# 你可以下载最新版本    解压到/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$


3.复制第一步备份的iptables配置文件到当前新内核的iptables配置文件位置


cp iptables /etc/init.d
cp iptables-config /etc/sysconfig/

# 修改iptabels里定义的iptables命令路径为/usr/sbin/$IPTABLES,在第21行
vim /etc/init.d/iptables




试试效果吧,将qq给禁用掉(假设你的主机在192.168.10.0网段)

iptables -t filter -A FORWARD -s 192.168.10.0/24 -m layer7 --l7-proto qq -j REJECT


是不是不能上qq了呢?


补充:

hash里面保存的有你使用的所有命令,它保存使用过的命令路径缓存,这样下次打已经打过的命令速度就快了,但是它有一个坏处,由于我们已经换了iptables的路径,再打会保错

所以我们使用

hash -r 来清除hash缓存
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: