Iptables l7應用層過濾
好的,上次我們簡單的討論了下iptales的用法,其中有很多的用法我們都還沒說,例如非常重要的SNAT ,DNAT轉換,字元匹配過濾,同是定義多埠,最大連接數限制,訪問時間限制,匹配速率限制,還有就是現在企業中經常要用到的l7layer應用層過濾!!下面我們來討論這些非常重要有用但是又很具挑戰難度的東東
主要內容:
iptables的實踐應用,L7應用層過濾
目標:
熟練應用ipatbles的用法,要做到爛熟於心
我們接著上次繼續探討iptables更深層的用法.這次我們基於上次的理論介紹主要通過實驗來熟悉每個命令的用法.
實驗環境:
內網2:10.0.2.100 telnet vsftp http
內網1:10.0.1.100
外網:10.0.3.100
firewall:eth0:10.0.2.1 eth1:10.0.3.1 ssh
SNAT DNAT:
ü SNAT
ü --to-source IPport
ü DNAT
ü --to-destination IP:port
路由測試:
[root@station93 ~]# sysctl -p
net.ipv4.ip_forward = 1
NAT轉換測試:
外網訪問iptables10.0.3.1埠時,iptables均把地址DNAT成內網10.0.2.100伺服器的httpd服務,注意,這裡外網不知道內網有10.0.2.100這個地址,只知道10.0.3.1
DNAT 轉換;==========》一個IP可實現多個功能
[root@station93 ~]# iptables -t nat -A PREROUTING -s 10.0.3.100 -d 10.0.3.1 -p tcp --dport 80 -j DNAT --to-dest 10.0.2.100
[root@station93 ~]# iptables -t nat -A PREROUTING -s 10.0.3.100 -d 10.0.3.1 -p tcp --dport 22 -j DNAT --to-dest 10.0.1.100
SNAT轉化測試:=========è內網用戶只需要一個IP地址即可全部上網
內網用戶通過iptables外網網關實現SNAT轉化的功能
[root@station93 ~]# iptables -t nat -A POSTROUTING -s 10.0.2.100 -p tcp --dport 80 -j SNAT --to-source 10.0.3.1
[root@3 httpd]# tail access_log============》這裡查看日誌時我們會法相其實是10.0.3.1來訪問外網伺服器的,即iptables的外網網關
10.0.3.1 - - [09/Mar/2010:20:43:59 0800] "GET / HTTP/1.1" 403 3985 "-" "ELinks/0.11.1 (textmode; Linux; 77x24-2)"
基於狀態的限制
v -m state
ü NEW
ü ESTABLISHED
ü RELATED
ü INVALID
只允許3.100訪問2.100的web服務,不允許2.100自發的回應3.100的
主要用來防止本機中毒,自發的向外發出信息泄密
[root@station93 ~]# iptables -A FORWARD -s 10.0.3.100 -d 10.0.2.100 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
[root@station93 ~]# iptables -R FORWARD 2 -s 10.0.2.100 -d 10.0.3.100 -p tcp --sport 80 -m state --state ESTABLISHED,RELATED -j ACCEPT
測試:
[root@3 ~]# elinks 10.0.2.100 ======》出現測試頁
[root@2 ~]# elinks 10.0.3.100 ========》失敗
v -m multiport
ü --source-ports port1,port2,port3,…
ü --destination-ports port1,port2,port3,…
ü --ports port1,port2,port3,…
企業中防火牆的實際應用
在實際的企業應用中,我們以上學習的iptables只是使遠遠不夠的,舉個簡單的例子,我們在平時的工作中往往需要限制qq,msn,迅雷,快車等聊天下載工具的使用,而我們以上學的都是寫非常簡單點的限制,而這這些工具的伺服器的眾多,埠多變,我們通過普通的方法是很難控制住的,銀次我們需要給予Layer的第七層應用的控制,這裡我們需要重新編譯升級內核.
需要的軟體包:
v The 2.4 or 2.6 Linux kernel source (2.6 strongly preferred) from kernel.org
v The iptables source from netfilter.org
v "l7-filter kernel version" package (netfilter-layer7-vX.Y.tar.gz)
v "Protocol definitions" package (l7-protocols-YYYY-MM-DD.tar.gz)
步驟如下:
ü # tar zxvf linux-2.6.28.10.tar.gz -C /usr/src
ü # tar zxvf netfilter-layer7-v2.22.tar.gz -C /usr/src
ü # ln –s /usr/src/linux-2.6.28.10/ /usr/src/linux
ü # cd /usr/src/linux/
ü # patch -p1 < ../netfilter-layer7-v2.22/kernel-2.6.25-2.6.28-layer7-2.22.patch
# cp /boot/config-2.6.18-164.el5 /usr/src/linux/.config
# make menuconfig 這裡我們需要實現安裝好對應的開發包 pirut & 命令編輯
我們需要選擇好下面的這些模塊:
v Networking support → Networking Options →Network packet filtering framework →Code Netfilter Configuration
ü <M> Netfilter connection tracking support
ü <M> 「layer7」 match support
ü <M> 「string」 match support
ü <M> FTP protocol support
ü <M> 「time」 match support
ü <M> 「iprange」 match support
ü <M> 「connlimit」 match support
ü <M> 「state」 match support
ü <M> 「conntrack」 connection match support
ü <M> 「mac」 address match support
ü <M> "multiport" Multiple port match support
v Networking support → Networking Options →Network packet filtering framework → IP: Netfilter Configuration
ü <M> IPv4 connection tracking support (required for NAT)
ü <M> Full NAT
? <M> MASQUERADE target support <M> NETMAP target support <M> REDIRECT target support
v Start compiling and installing
ü make
ü make modules_install
ü make install
v Edit the /boot/grub/grub.conf, set the default booting kernel to the new kernel
v Restart
v # cp /etc/rc.d/init.d/iptables ~/iptables
v # rpm -e iptables-ipv6 iptables iptstate --nodeps
v # tar jxvf iptables-1.4.6.tar.bz2 –C /usr/src
v # cd /usr/src/iptables-1.4.6
v # cp ../netfilter-layer7-v2.22/iptables-1.4.3forward-for-kernel-2.6.20forward/libxt_layer7.* ./extensions/
v ./configure --prefix=/usr --with-ksource=/usr/src/linux
v make
v make install
v # tar zxvf l7-protocols-2009-05-28.tar.gz
v # cd l7-protocols-2009-05-28
v # make install
v # mv ~/iptables /etc/rc.d/init.d/
v #ln –sv /usr/sbin/iptables /sbin/iptables
v 注意這裡第一次啟動無法service iptables start 需要先setup,再service iptables start
2.6.28.10 內核中 ip_conntrack_ftp 改名為 nf_conntrack_ftp
加模塊: modprobe nf_conntrack_ftp
支持的協議種類:
v bittorrent
ü P2P filesharing / publishing tool
v edonkey
ü eDonkey2000 - P2P filesharing
v kugoo
ü KuGoo - a Chinese P2P program
v msn-filetransfer
ü MSN (Micosoft Network) Messenger file transfers
v msnmessenger
ü Microsoft Network chat client
v pplive
ü Chinese P2P streaming video
v qq
v xunlei
用法
ü iptables [specify table & chain] -m layer7 --l7proto [protocol name] -j [action]
禁止外網訪問firewall的web ,ssh,telnet服務
[root@station93 ~]# iptables -A INPUT -p tcp -m multiport --source-ports 22,25,80 -j DROP
v connlimit
v [!] --connlimit-above n 最大連接數
ü Allows you to restrict the number of parallel connections to a server per client IP address (or client address block).
ü Example:
? iptables -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 2 -j REJECT
? Allow 2 ssh connections per client host
限制外網的最大連接數是2
[root@station93 ~]# iptables -A OUTPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 2 -j REJECT
v String
ü 有兩種演算法:bm kmp
ü This modules matches a given string by using some pattern matching strategy.
ü It requires a linux kernel >= 2.6.14.
ü [!] --string pattern
? Matches the given pattern.
ü --algo {bm|kmp}
? Select the pattern matching strategy. (bm = Boyer-Moore, kmp = Knuth-Pratt-Morris)
ü Examples:
? iptables –A OUTPUT –p udp --dport 53 -m string --string 「verycd」 --algo bm -j DROP
? iptables -A OUTPUT -p tcp --dport 80 -m string --string 「***」 --algo kmp -j DROP
限制內網瀏覽外網上包含secert 字元串的網頁:
[root@3 html]# echo "secert" >> index2.html
[root@3 html]# cat index2.html
secret
this is the second test
secret
[root@station93 ~]# iptables -A FORWARD -s 10.0.2.100 -d 10.0.3.100 -p tcp --sport 80 -m string --string "secert" --algo bm -j DROP
v time
ü This matches if the packet arrival time/date is within a given range.
ü --datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
ü --datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
? Only match during the given time, The possible time range is 1970-01-01 T00:00:00 to 2038-01-19 T04:17:07
ü --timestart hh:mm[:ss]
ü --timestop hh:mm[:ss]
? The possible time range is 00:00:00 to 23:59:59.
? Leading zeroes are allowed.
v time
ü [!] --monthdays day[,day...]
? Only match on the given days of the month. Possible values are 1 to 31.
ü [!] --weekdays day[,day...]
? Only match on the given weekdays. Possible values are Mon, Tue, Wed, Thu, Fri, Sat, Sun, or values from 1 to 7, respectively.
ü Examples:
? -m time --weekdays Sa,Su
? -m time --timestart 12:30 --timestop 13:30
[root@station93 ~]# iptables -A FORWARD -m time --timestart 00:00 --timestop 8:00 -j DROP
v -j LOG
ü --log-level
level? Level of logging
ü --log-prefix prefix
? Prefix log messages with the specified prefix; up to 29 letters long, and useful for distinguishing messages in the logs.
ü --log-tcp-options
? Log options from the TCP packet header.
ü --log-ip-options
? Log options from the IP packet header.
[root@station93 ~]# iptables -A FORWARD -s 10.0.3.100 –d 10.0.2.100 –p tcp –dport -j LOG --log-prefix "abc"
基於l7layer的規則則十分類似,在其內部已經為我們定義好,我們只需要調用其就行了.
例如:禁止內網用戶在上班時間使用qq
[root@station93 ~]# iptables -A FORWARD -s 10.0.2.100 -d 10.0.3.100 -p tcp --dport 80 -m time --timestart 8:00 --timestop 18:00 -m layer7 --l7proto qq -j DROP
[火星人 ] linux下iptables lay7配置下已經有567次圍觀