linux內核編譯 打補丁(包括iptables)

 當你想實現通過iptables限制迅雷、QQ等應用層協議的時候就需要安裝layer7的補丁,而這個補丁的安裝涉及到了內核源碼編譯,打補丁,iptables重新編譯的,是個十分好的綜合學習過程。本文講述內核編譯和打補丁

 

介紹:

L7-filter (Application Layer Packet Classifier for Linux), 是 Linux netfilter 的外掛模塊, 它能使 Linux 的 iptables 支持 Layer 7 (Application 應用層) 過濾功能, 限制封殺 P2P、即時通訊軟件。

 

過程:

1       內核打補丁,重新編譯

2       iptables打補丁,重新編譯

3       安裝l7proto

 

1       首先查看內核版本,這個軟件對內核版本有要求

uname -r

 

2.      下載所需軟件包:

kernel 2.6.19.7

# wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.19.7.tar.bz2

iptables 1.3.7

# wget http://www.netfilter.org/projects/iptables/files/iptables-1.3.7.tar.bz2

L7-filter http://sourceforge.net/project/showfiles.php?group_id=80085

netfilter-layer7-v2.12.tar.gz

l7-protocols-2007-06-22.tar.gz

 

3.      配置編譯新內核

首先將所下載的軟件都放置於/usr/src目錄下

# tar zxvf netfilter-layer7-v2.12.tar.gz

# tar xjvf linux.2.6.19.7.tar.bz2

# ln -s linux.2.6.19.7 linux

# cd linux

# patch -p1 < /usr/src/netfilter-layer7-v2.9/kernel-2.6.18-2.6.19-layer7-2.9.patch (打L7-filter的內核補丁)

# make oldconfig

(如果之前曾經編譯過,需要先執行 make mrproper 。make oldconfig會自動對比之前的kernel config,根據之前版本的配置生成一個kernel config,這樣方便我們在編譯新核心的時候,無須在從頭修改所有的核心設置)

# make menuconfig 設定內核參數   如果出錯可能是因爲未安裝 ncurses-devel

核心不同,內核選項的排列方式有可能不一樣,仔細找一下可以找到:

Code maturity level options –> [*] Prompt for development and/or incomplete code/drivers

Networking –> Networking options –>

[*] Network packet filtering (replaces ipchains) –>

IP: Netfilter Configuration –>

<M> Connection tracking (required for masq/NAT)

[*] Connection tracking flow accounting

<M> IP tables support (required for filtering/masq/NAT)

<M> Layer 7 match support

 

# make              時間消耗長

# make modules

# make modules_install

# make install

# reboot

# uame -a

Linux jason.10235 2.6.19.7 #1 Fri Jul 6 11:56:11 CST 2007 i686 i686 i386 GNU/Linux

系統已經更新至新內核 2.6.19.7

 

 

4.      更新升級Iptalbes的Layer7補丁(注意備份iptables規則)

# cd /usr/src

# tar xjvf iptables-1.3.7.tar.bz2

# cd iptables-1.3.7

# patch -p < ../netfilter-layer7-v2.0/iptables-layer7-2.0.patch

# chmod +x extensions/.layer7-test

# export KERNEL_DIR=/usr/src/linux-2.6.19.7

# export IPTABLES_DIR=/usr/src/iptables-1.3.7

# make BINDIR=/sbin LIBDIR=/lib MANDIR=/usr/share/man install

# iptables -V

iptables v1.3.7 已經更新至新版本

 

 

 

5. 安裝Layer7 協議文件

 

# cd /usr/src

# tar zxvf l7-protocols-2007-06-22.tar.gz

# cd l7-protocols-2007-06-22

# make install

 

6.      使用iptables layer-7 filter

# iptables -t mangle -I PREROUTING -m layer7 –l7proto edonkey -j DROP (禁止edonkey)

# iptables -t mangle -I PREROUTING -m layer7 –l7proto bittorrent -j DROP (禁止bt)

# iptables -t mangle -I PREROUTING -m layer7 –l7proto qq -j DROP (禁止QQ通訊)

# iptables -t mangle -I PREROUTING -m layer7 –l7proto msnmessenger -j DROP (禁止edonkey)

# iptables -t mangle -I PREROUTING -m layer7 –l7proto xunlei -j DROP (禁止迅雷)

# iptables -t mangle -I PREROUTING -m layer7 –l7proto kugoo -j DROP (禁止kugoo)

# iptables -t mangle -I PREROUTING -m layer7 –l7proto yahoo -j DROP (禁止Yahoo! Messenger)

 

例子:

禁MSN傳送文件的方法

iptables 七層實現的。

iptables -A FORWARD -m layer7 –l7proto msn-filetransfer -j DROP

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章