爲iptables增加layer7補丁(Linux
################################################################
系統環境:RHEL5 [
軟件環境:
http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.25.19.tar.bz2
http://www.netfilter.org/projects/iptables/files/iptables-1.4.2.tar.bz2
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/l7-protocols-2008-10-04.tar.gz
目標功能:
爲iptables增加layer7補丁,實現應用層過濾。
一、重新編譯內核
1、合併kernel+layer7補丁
shell> tar jxvf linux-
shell> tar zxvf netfilter-layer7-v2.20.tar.gz -C /usr/src/
shell> cd /usr/src/linux-
shell> patch -p1 < /usr/src/netfilter-layer7-v2.20/kernel-2.6.25-layer7-2.20.patch
2、配置新內核
shell> cp /boot/config-
shell> make menuconfig
//配置內核時,在“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……等編譯成模塊,根據需要看着辦。
2) ---> IP: Netfilter Configuration
//將“IPv4 connection tracking support (require for NAT)”編譯成模塊。
//將“Full NAT”下的“MASQUERADE target support”和“REDIRECT target support”編譯成模塊。
3、編譯及安裝模塊、新內核
shell> make && make modules_install && make install
//編譯安裝成後後,重啓選擇使用新的內核(
二、重新編譯iptables
1、卸載現有iptables
shell> rpm -e iptables iptstat --nodeps
2、合併iptables+layer7補丁
shell> tar jxvf iptables-
shell> cd /usr/src/netfilter-layer7-v2.20/iptables-
shell> cp libxt_layer7.c libxt_layer7.man /usr/src/iptables-1.4.2/extensions/
3、編譯安裝
shell> cd /usr/src/iptables-
shell> ./configure --prefix=/ --with-ksource=/usr/src/linux-
shell> make && make install
4、安裝l7-protocols模式包
shell> tar zxvf l7-protocols-
shell> mv /etc/l7-protocols-
三、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