爲iptables增加layer7補丁(Linux2.6.25內核)
2008-11-01 TsengYia#126.com
################################################################
系統環境:RHEL5 [ 2.6.18-8.el5xen]
軟件環境:
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-2.6.25.19.tar.gz2 -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
2、配置新內核
shell> cp/boot/config-2.6.18-8.el5 .config //偷個懶,沿用舊的內核配置
shell>[[email protected]]# make menuconfig
make menuconfig
//配置內核時,在“Networking ---> Networking Options ---> Network Packetfiltering framework (Netfilter) ”處主要注意兩個地方:
1) ---> Code NetfilterConfiguration
//將“Netfilterconnection tracking suport (NEW)”選擇編譯爲模塊(M),需選取此項才能看到layer7支持的配置。
//將layer7、string、state、time、IPsec、iprange、connlimit……等編譯成模塊,根據需要看着辦。
2) ---> IP: Netfilter Configuration
//將“IPv4connection tracking support (require for NAT)”編譯成模塊。
//將“FullNAT”下的“MASQUERADE target support”和“REDIRECT target support”編譯成模塊。
3、編譯及安裝模塊、新內核
shell> make &&make modules_install && make install
//編譯安裝成後後,重啓選擇使用新的內核(2.6.25.19)引導系統
二、重新編譯iptables
控制腳本的巧用
每個rpm包安裝的軟件包安裝的時候都有自己的控制腳本,我們可以巧用一下,原安裝的控制腳本
[root@localhost ~]# cp /etc/init.d/iptables /etc/init.d/iptables.bak
1、卸載現有iptables
[root@localhost ~]# rpm -e iptables –-nodeps 強制卸載iptables並取消依賴關係
2、合併iptables+layer7補丁
[root@localhost ~]# tar -jxvf iptables-1.4.2.tar.bz2-C /usr/local/src/
[root@localhost ~]# cd/usr/src/netfilter-layer7-v2.20/iptables-1.4.1.1-for-kernel-2.6.20forward/
[[email protected]]# cp */usr/local/src/iptables-1.4.2/extensions/
[[email protected]]# cd/usr/local/src/iptables-1.4.2/
3、編譯安裝
[root@localhost iptables-1.4.2]#./configure --prefix=/ --with-ksource=/usr/src/linux-2.6.25.19/
[[email protected]]# make
[[email protected]]# make install
[[email protected]]# cp /etc/init.d/iptables.bak /etc/init.d/iptables
4、安裝l7-protocols模式包
[root@localhost ~]# tar -zxvf l7-protocols-2008-10-04.tar.gz-C /etc/
[root@localhost ~]# mv/etc/l7-protocols-2008-10-04/ /etc/ l7-protocols
[root@localhost ~]#chkconfig iptables on
以上就是升級內核所有步驟.
案例
公司有三個部門
軟件部門 2.21-2.30
經理辦 2.31-2.40
上班時間 (週一---週五 08:20:00)
工程部門上班時間ftp 【192.168.1.166】{ FORWARD} 不qq{L7}不 允許http{FORWARD} 迅雷{L7} 下班後無限制
軟件部門 http 不允許非法站點sina {SQUID},不允許使用迅雷{L7} ,連接數 最多3個{FORWARD,SQUID}不允許聊天 {L7}
不允許看圖片{SQUID} 下班後無限制,
經理辦公室 http {FORWARD} qq{L7} 都可以,下班後無限制
dmz區域rdp server 服務器進行發佈
3個網卡的ip地址的設置
[root@localhost ~]#vim /etc/sysctl.conf
[root@localhost ~]# sysctl –p
1.首先內網用戶和dmz區域的都需要通過防火牆去鏈接網絡,我們只需做SNAT轉換即可
2.[root@localhost~]# iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
3.把默認策略更改成成拒絕所有,在此之前因爲我們是通過ssh鏈接上去的,首先先把自己網卡地址加入到規則表裏面(網卡192.168.2.1)
4.[root@localhost~]# iptables -t filter -A INPUT -s 192.168.2.1 -p tcp --dport 22 -j ACCEPT
5.[root@localhost~]# iptables -t filter -A OUTPUT -d 192.168.2.1 -p tcp --sport 22 -m state--state ESTABLISHED -j ACCEPT
6.[root@localhost~]# iptables –P INPUT DROP
7.[root@localhost~]# iptables –P OUTPUT DROP
8.[root@localhost~]# iptables -P FORWARD DROP
工程部192.168.2.10—2.20
上班時間可以使用ftp 192.168.1.166,不可以上QQ,不允許http,不允許使用迅雷下載,下班後無限制
[root@localhost ~]#iptables -t filter -A FORWARD -m time--timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -p tcp --dport21 –d 192.168.1.166 -j ACCEPT
[root@localhost ~]#iptables -t filter -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT(允許所有返回的數據包)
[root@localhost ~]#modprobe ip_nat_ftp (加載ftp模塊)
[root@localhost ~]#iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.10-192.168.2.20 -m time --timestart 08:00 --timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7 proto qq -j DROP(上班時間不允許上QQ)
[root@localhost ~]#iptables -t filter -A FORWARD -m iprange --src-range 192.168.2.10-192.168.2.20 -m time --timestart 08:00--timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7 protoxunlei -j DROP(不允許使用迅雷下載)
[root@localhost ~]#iptables -t filter -I FORWARD 2 -s192.168.2.0/24 -o eth1 -m time --timestart 20:01 --timestop 07:59 -j ACCEPT(針對所有部門下班後可無限制)
軟件部門
[root@localhost ~]#rpm -ivh /mnt/cdrom/Server/squid-2.6.STABLE21-3.el5.i386.rpm
[root@localhost ~]#service squid start
[root@localhost ~]#chkconfig squid on
[root@localhost ~]#iptables -t filter -I FORWARD 2 -m iprange --src-range192.168.2.21-192.168.2.40 -m time --timestart 08:00 --timestop 20:00 --weekdaysMon,Tue,Wed,Thu,Fri -p udp --dprot 53 -j ACCEPT(上班時間DNS通過)
[root@localhost ~]#iptables -t nat -A PREROUTING -i eth0 -ptcp --dport 80 -j REDIRECT --to-ports3128(只要用戶通過eth0接口tcp協議訪問的是80端口,就重定向到3128端口)
[root@localhost ~]#vim /etc/squid/squid.conf
圖8
圖11
圖12
[root@localhost ~]#squid -k parse(進行語法測試)
[root@localhost ~]#iptables -t filter -A INPUT -i eth0 -ptcp --dport 3128 -j ACCEPT(squid目標端口爲3128端口,允許通過)
[root@localhost ~]#iptables -t filter -A OUTPUT -m state --state ESTABLISHED -j ACCEPT(squid返回包)
圖10
Squid工作原理:當訪問http服務器的時候,發現自己的緩存裏面存在數據,則返回給用戶,沒有則向服務器索要。(發的時候需要走OUTPUT這個鏈表,返回的時候需要允許INPUT)
[root@localhost ~]#iptables -t filter -A OUTPUT -o eth1 -p tcp --dport 80 -j ACCEPT
[root@localhost ~]# iptables -t filter -AINPUT -m state --state ESTABLISHED -jACCEPT
[root@localhost ~]#iptables -t filter -A OUTPUT -p udp --dport 53 -j ACCEPT(squid需要做DNS解析)
[[email protected]]# vim /etc/squid/squid.conf
不能瀏覽圖片
圖13
不能訪問sina站點
圖14
限制最大連接數
圖15
[root@localhost ~]#service squid restart
拒絕用戶使用迅雷下載和QQ
[root@localhost ~]#iptables -t filter -A FORWARD -m layer7 --l7proto xunlei -j DROP
[root@localhost ~]#iptables -t filter -A FORWARD -m layer7 --l7proto qq -j DROP
經理辦公室
[[email protected]]# vim /etc/squid/squid.conf
圖16
[root@localhost ~]#service squid restart
允許經理辦公室週一到週五上班的時候可以上QQ
[root@localhost ~]#iptables -t filter -I FORWARD 1 -miprange --src-range 192.168.2.31-192.168.2.40 -m time --timestart 08:00--timestop 20:00 --weekdays Mon,Tue,Wed,Thu,Fri -m layer7 --l7proto qq -jACCEPT
圖17
DMZ區域
[root@localhost ~]#iptables -t nat -A PREROUTING -d 192.168.1.44 -p tcp --dport 3389 -j DNAT --to192.168.3.100 (做DNAT裝換)
[root@localhost ~]#iptables -t filter -I FORWARD 1 -i eth1 -d 192.168.3.100 -p tcp --dport 3389 -jACCEPT