系統環境:RHEL5 [ 2.6.18-8.el5xen ]
軟體環境:
<a href="http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.25.19.tar.bz2">http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.25.19.tar.bz2</a>
<a href="http://www.netfilter.org/projects/iptables/files/iptables-1.4.2.tar.bz2">http://www.netfilter.org/projects/iptables/files/iptables-1.4.2.tar.bz2</a>
<a href="http://ie.archive.ubuntu.com/sourceforge/l/l7/l7-filter/netfilter-layer7-v2.20.tar.gz">http://ie.archive.ubuntu.com/sourceforge/l/l7/l7-filter/netfilter-layer7-v2.20.tar.gz</a>
<a href="http://ie.archive.ubuntu.com/sourceforge/l/l7/l7-filter/l7-protocols-2008-10-04.tar.gz">http://ie.archive.ubuntu.com/sourceforge/l/l7/l7-filter/l7-protocols-2008-10-04.tar.gz</a>
目标功能:
為iptables增加layer7更新檔,實作應用層過濾。
################################################################
一、重新編譯核心
1、合并kernel+layer7更新檔
shell> tar -jxvf linux-2.6.25.19.tar.bz2 -C /usr/src/
shell> tar -zxvf netfilter-layer7-v2.20.tar.gz -C /usr/src/
shell> cd /usr/src/linux-2.6.25.19/
shell> patch -p1 < /usr/src/netfilter-layer7-v2.20/kernel-2.6.25-layer7-2.20.patch
檢視目前的核心
<a href="http://blog.51cto.com/attachment/201308/145327977.png" target="_blank"></a>
<a href="http://blog.51cto.com/attachment/201308/145330639.png" target="_blank"></a>
将新的核心和更新檔下載下傳到linux上
<a href="http://blog.51cto.com/attachment/201308/145330400.png" target="_blank"></a>
拆包核心和更新檔
[root@localhost ~]# tar -jxvf linux-2.6.25.19.tar.bz2 -C /usr/src/
[root@localhost ~]# tar -jxvf netfilter-layer7-v2.20.tar.gz -C /usr/src/
<a href="http://blog.51cto.com/attachment/201308/145332222.png" target="_blank"></a>
2.配置核心
shell> cp /boot/config-2.6.18-8.el5 .config
<a href="http://blog.51cto.com/attachment/201308/145333812.png" target="_blank"></a>
//配置核心時,在“Networking ---> Networking Options ---> Network Packet filtering framework (Netfilter) ”處主要注意兩個地方:
1) ---> Code Netfilter Configuration
//将“Netfilter connection tracking suport (NEW)”選擇編譯為子產品(M),需選取此項才能看到layer7支援的配置。
//将layer7、string、state、time、IPsec、iprange、connlimit……等編譯成子產品,根據需要看着辦。
<a href="http://blog.51cto.com/attachment/201308/145334843.png" target="_blank"></a>
<a href="http://blog.51cto.com/attachment/201308/145337111.png" target="_blank"></a>
2) ---> IP: Netfilter Configuration
//将“IPv4 connection tracking support (require for NAT)”編譯成子產品。
//将“Full NAT”下的“MASQUERADE target support”和“REDIRECT target support”編譯成子產品。
<a href="http://blog.51cto.com/attachment/201308/145339409.png" target="_blank"></a>
<a href="http://blog.51cto.com/attachment/201308/145343147.png" target="_blank"></a>
3、編譯及安裝子產品、新核心
shell> make && make modules_install && make install
//編譯安裝成後後,重新開機選擇使用新的核心(2.6.25.19)引導系統
<a href="http://blog.51cto.com/attachment/201308/145344752.png" target="_blank"></a>
這個過程會花費很多時間,不急,慢慢來
<a href="http://blog.51cto.com/attachment/201308/145345364.png" target="_blank"></a>
二、重新編譯iptables
1、解除安裝現有iptables
shell> rpm -e iptables iptstat --nodeps
<a href="http://blog.51cto.com/attachment/201308/145346765.png" target="_blank"></a>
2、合并iptables+layer7更新檔
shell> tar jxvf iptables-1.4.2.tar.bz2 -C /usr/src/
<a href="http://blog.51cto.com/attachment/201308/145346210.png" target="_blank"></a>
shell> cd /usr/src/netfilter-layer7-v2.20/iptables-1.4.1.1-for-kernel-2.6.20forward/
shell> cp libxt_layer7.c libxt_layer7.man /usr/src/iptables-1.4.2/extensions/
<a href="http://blog.51cto.com/attachment/201308/145347241.png" target="_blank"></a>
3、編譯安裝
shell> cd /usr/src/iptables-1.4.2/
<a href="http://blog.51cto.com/attachment/201308/145348674.png" target="_blank"></a>
<a href="http://blog.51cto.com/attachment/201308/145349692.png" target="_blank"></a>
4、安裝l7-protocols模式包
shell> tar zxvf l7-protocols-2008-10-04.tar.gz -C /etc/
shell> mv /etc/l7-protocols-2008-10-04 /etc/l7-protocols
<a href="http://blog.51cto.com/attachment/201308/145349296.png" target="_blank"></a>
<a href="http://blog.51cto.com/attachment/201308/145350559.png" target="_blank"></a>
三、layer7規則示例
1、layer7 match
shell> iptables -A FORWARD -m layer7 --l7proto qq -j DROP
shell> iptables -A FORWARD -m layer7 --l7proto msnmessenger -j DROP
shell> iptables -A FORWARD -m layer7 --l7proto msn-filetransfer -j DROP
shell> iptables -A FORWARD -m layer7 --l7proto xunlei -j DROP
shell> iptables -A FORWARD -m layer7 --l7proto edonkey -j DROP
shell> iptables -A FORWARD -m layer7 --l7proto bittorrent -j DROP
2、string match
shell> iptables -A FORWARD -p udp --dport 53 -m string --string "tencent" --algo bm -j DROP
shell> iptables -A FORWARD -p udp --dport 53 -m string --string "verycd" --algo bm -j DROP
shell> iptables -A FORWARD -p tcp --dport 80 -m string --string "sex" --algo bm -j DROP
3、state match
shell> iptables -A FORWARD -m state --state NEW -p tcp ! --syn -j DROP
shell> iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
4、connlimit match
shell> iptables -A FORWARD -p tcp --syn -m connlimit --connlimit-above 100 --connlimit-mask 24 -j DROP
5、time match
shell> iptables -A FORWARD -p tcp --dport 80 -m time --timestart 8:00 --timestop 17:00 --weekdays Mon,Tue,Wed,Thu,Fri -j ACCEPT
本文轉自Tar0 51CTO部落格,原文連結:http://blog.51cto.com/tar0cissp/1268652,如需轉載請自行聯系原作者