天天看點

IPtables之五:加載layer7比對應用層資料包

要讓iptables實作應用層的過慮,首先要給核心和應用層的iptables定義工具分别打上layer7的更新檔,使之能實作實作應用層不同應用資料包的特征比對。

是以其一要重新給核心打更新檔,并在重新編譯核心的時候加載layer7相關的子產品。

其二,解除安裝應用層的規則定義工具iptables,并下載下傳iptables的源代碼重新編譯安裝 

這就實作了在核心層和使用者層支援了layer7子產品,而要實作實際應用中不同應用層協定的規則比對,還需要下載下傳安裝規則定義包

總結以上,要想在iptables中添加layer7層過慮子產品,需要以下的東東

1.layer7更新檔

2.協定比對包

3.iptables的源碼

4.linux的核心源碼

接下來,開始具體實作過程

先檢視下我目前的核心版本(centos6.2的系統)

<a href="http://lustlost.blog.51cto.com/attachment/201207/30/2600869_1343633336DEMW.png"></a>

先去官網下載下傳最和我版本号最接近的核心源碼

<a href="http://www.kernel.org/">http://www.kernel.org/</a>

我下載下傳的是linux-2.6.32.59的包,各位具體可以按照自己實際情況下載下傳對應的源碼包

iptables源代碼

<a href="ftp://ftp.netfilter.org/pub/iptables/iptables-1.4.9.tar.bz2">ftp://ftp.netfilter.org/pub/iptables/</a>

layer7更新檔和比對規則,注意更新檔包

<a href="http://sourceforge.net/project/showfiles.php?group_id=80085">http://sourceforge.net/</a>

這幾個包我打包成一個提供給大家下載下傳(核心源碼比較大,有需要的還是自行下載下傳吧)

首先先給linux核心添加layer7更新檔(layer7更新檔有2個,一個是核心更新檔,另一個是使用者空間的iptables程式的更新檔),重新編譯核心

step1:打核心更新檔,重新編譯核心

解包核心源代碼

[root@localhost ~]# tar -xf linux-2.6.32.59.tar.bz2 -C /usr/src/

cd過去

[root@localhost ~]# cd !$

建立軟連接配接

[root@localhost src]# ln -sv linux-2.6.32.59/ linux

"linux" -&amp;gt; "linux-2.6.32.59/"

解壓更新檔包

[root@localhost ~]# tar -xf netfilter-layer7-v2.22.tar.gz -C /usr/src/

給核心源碼打上更新檔

[root@localhost src]# cd /usr/src/ linux

[root@localhost src]# patch -p1 &amp;lt; /usr/src/netfilter-layer7-v2.22/kernel-2.6.25-2.6.28-layer7-2.22.patch

配置核心編譯檔案(我使用了原來安裝系統的.config配置檔案為藍本進行修改)

[root@localhost linux]# cp /boot/config-2.6.32-220.el6.i686 .config

[root@localhost linux]# make menuconfig    #確定已經安裝開發環境

打開網絡相關支援

<a href="http://lustlost.blog.51cto.com/attachment/201207/30/2600869_1343633336mZWE.png"></a>

網絡選項

<a href="http://lustlost.blog.51cto.com/attachment/201207/30/2600869_1343633337D5wm.png"></a>

網絡資料包過濾架構

<a href="http://lustlost.blog.51cto.com/attachment/201207/30/2600869_1343633338exAQ.png"></a>

核心配置

<a href="http://lustlost.blog.51cto.com/attachment/201207/30/2600869_1343633338SJqD.png"></a>

主要要勾選這一項,打開layer7子產品支援

<a href="http://lustlost.blog.51cto.com/attachment/201207/30/2600869_1343633339lj6A.png"></a>

儲存後開始編譯核心

[root@localhost linux]# make  #打開手機喝茶慢等。。。

安裝子產品

[root@localhost linux]# make modules_install

安裝新核心

[root@localhost linux]# make install

重新開機選擇新安裝的核心

<a href="http://lustlost.blog.51cto.com/attachment/201207/30/2600869_1343633339hpbr.png"></a>

step2:打iptables更新檔,重新編譯iptables

先解除安裝原來的iptables

[root@localhost ~]# rpm -e iptables iptables-ipv6 –nodeps #注意iptables可能依賴于其它的包,不要把其它依賴關系的包給解除安裝了

解壓iptables源碼包

[root@localhost ~]# tar xf iptables-1.4.9.tar.bz2 -C /usr/src/

給源碼包打上layer7子產品的更新檔,複制netfilter-layer7下關于iptables下的檔案到iptables源碼檔案夾下的extensions下

[root@localhost ~]# cp /usr/src/netfilter-layer7-v2.22/iptables-1.4.3forward-for-kernel-2.6.20forward/* /usr/src/iptables-1.4.9/extensions/

配置編譯配置檔案

[root@localhost iptables-1.4.9]# ./configure --prefix=/usr --with-ksource=/usr/src/linux

編譯安裝

[root@localhost iptables-1.4.9]# make &amp;&amp; make install

step3:安裝layer7的規則定義包

[root@localhost ~]# tar xf l7-protocols-2009-05-28.tar.gz

[root@localhost l7-protocols-2009-05-28]# make install

它會在/etc下建立l7-protocols檔案夾,裡面放置了layer7的配置檔案和規則定義檔案,在用iptables定義layer7規則的時候,就會調用這裡的配置檔案

資料包規則定義檔案在/etc/l7-protocols/protocols下,有實力也可以自己編寫規則放在這裡,然後用iptables直接調用就可以了

OK,至此就都完成了,其實也就重新編譯一下核心,重新編譯安裝iptables,最後安裝layer7規則定義

再安裝layer7支援子產品後,就可以根據已有的規則來定義

假設主機是網關,可以禁止QQ,迅雷等應用程式通路網際網路

[root@localhost ~]# iptables -A FORWARD -m layer7 --l7proto xunlei -j DROP

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

(好吧,我承認禁止人家qq是很不道德的。。。不過搭配速率控制的子產品,可以限制别人迅雷的下載下傳速度,這個還是比較好用的)

本文轉自lustlost 51CTO部落格,原文連結:http://blog.51cto.com/lustlost/947909,如需轉載請自行聯系原作者

繼續閱讀