如果在公司裡做網路系統管理員,老闆可能會讓你屏蔽掉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/srctar netfilter-la -C /usr/srctar iptables -C /usr/srccd /usr/src
2)給核心打補丁
ln -sv linux- linuxcd linuxpatch -p1 <../netfilter-layer7-v2.22/kernel-2.6.25-2.6.28-layer7-2.22.patchcp /boot/config-2.6.18-164.el5 ./.config #複製你當前主機的核心設定檔,這樣編譯的時候只要加上l7模組就可以了make menuconfig
選如下的項
make && make modules install && make install
2.編譯並且安裝完成後(需要重啟載入新核心),卸載原有版本iptables,安裝新版本
# 卸載之前先拷貝設定檔cp /etc/init.d/iptables ~/ipteblescp /etc/sysconfig/iptables-config ~/# 卸載rpm -e iptables-ipv6 iptables iptate --nodepsls /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 installtar l7-protocols -C /etccd /etc/l7-protocolsmake 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 wsgtrsysqq^.?.?\x02.+\x03$
3.複製第一步備份的iptables設定檔到當前新核心的iptables設定檔位置
cp iptables /etc/init.dcp 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緩衝