Iptables是位于用戶空間,是linux系統上的防火墻管理配置規則的工具,主要用于添加、刪除、管理netfilter的規則,
Netfilter是位于內核中真正的防火墻,由5個鉤子組成,也叫五個規則鏈。 Netfilter的作用:起到過濾封包,轉換與映射IP地址和端口,拆分和修改封包內容,追蹤封包等功能
Iptables的四表五鏈
功能:(四表) filter: 過濾,防火墻; nat: network address translation, 網絡地址轉換; mangle:拆解報文,做出修改,封裝報文; raw:關閉nat表上啟用的連接追蹤機制; 功能的優先級次序:raw<---mangle<---nat<----filter
鏈(內置)(五鏈): PREROUTING:在路由之前 INPUT:到本機 FORWARD:由本機轉發 OUTPUT:由本機發出 POSTROUTING:路由之后 流入:PREROUTING --> INPUT 流出:OUTPUT --> POSTROUTING 轉發:PREROUTING --> FORWARD --> POSTROUTING
各功能(四表)的實現:
filter:在INPUT,FORWAD,OUTPUT nat:PREROUTING(DNAT),OUTPUT,POSROUTING mangle:PERROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING raw:PREROUTING,OUTPUT
iptables四表五鏈添加規則的注意項
添加規則時的考量點: 要實現哪種功能,判斷添加在哪張表上 報文流經的路徑,判斷添加在哪個鏈上 鏈:鏈上規則次序,即為檢查的次序,因此隱含一定的法則: 同類規則,匹配范圍小的放上面; 不同類規則,匹配需求頻率大的放在上面,匹配需求小的放下面 將那些可由一條規則描述的多個規則合并為一個; 默認規則(策略):
規則的組成部分:
組成部分:報文的匹配條件,匹配到之后處理動作 匹配條件:根據協議報文特征指定:基本匹配條件、擴展匹配條件 處理動作:內建處理機制、自定義處理機制 注意:報文不會經過自定義鏈,只能在內置鏈上通過規則進行引用后生效;
iptables命令:
格式: iptables [-t table] {-A|-D} chain_name rule-specification 添加或刪除指定鏈上的規則,添加時默認為添加到鏈尾 iptables [-t table] -I chain_name [rulenum] rule-specification 插入規則到指定鏈上,默認插入到鏈首 iptables [-t table] -R chain_name rulenum rule-specification 修改指定鏈上的規則 iptables [-t table] -D chain_name rulenum 刪除指定鏈上的規則 iptables [-t table] -S [chain_name [rulenum]] iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...] -L顯示規則、-Z清零規則上的統計數、-F清空指定表的規則 iptables [-t table] -N chain_name 添加指定表上的自定義鏈 iptables [-t table] -X [chain_name] 刪除自定義鏈 iptables [-t table] -P chain_name target 指定鏈的默認規則 iptables [-t table] -E old-chain-name new-chain-name rule-specification = [matches...] [target] match = -m matchname [per-match-options] target = -j targetname [per-target-options]
匹配條件:
基本匹配: -s\--src\--source IP|Netaddr :匹配源地址 !-s\--src\--source IP|Netaddr :匹配此源地址之外的其他源地址 -d\--dst\--destionation IP|Netaddr:匹配目標地址 !-d\--dst\--destionation IP|Netaddr:匹配此目標地址之外的其他目標地址 -p\--protocol {tcp|udp|icmp}:檢查報文中的協議,即IP首部中的protocol所標識的協議 -i\--in-interface interface_name :數據報文的流入接口,僅能用于PREROUTING\INPUT\FORWARD鏈上 -o\--out-interface interface_name:數據報文的流出接口,僅能用于FORWARD\OUTPUT\POSTROUTING鏈上
擴展匹配
隱形擴展匹配:使用-p 指定的協議進行擴展,可省略-m選項
-p tcp|udp 注意:UDP只有--dport與--sport --dport PORT[-PORT]:目標端口,可以是單個端口或連續多個端口 --sport PORT[-PORT]:源端口,可以是單個或連續多個端口 tcp-flags(TCP的標識位)LIST1 LIST2 檢查LIST1所指明的所有標記位,且這其中,LIST2所表示出的所有標記位必須為1,而余下的必須為0;沒有在LIST1中指明的,不作檢查{SYN,ACK,FIN,RST,PSH,URG共6種標記位} --tcp-flag SYN,ACK,FIN,RST SYN 匹配TCP三次握手中的SYN,ACK,FIN,RST標記位,其中SYN必須為1,其他為0.即匹配三次握手的第一次握手 --syn:檢查是否為新建TCP連接的第一次請求
-p igmp --igmp-type:(可用數字代表其類型)常見的0和8 0:echo-reply回送應答 8:echo-request請求答應
匹配條件的顯示擴展
顯式擴展 :必須使用-m選項指定使用擴展,可擴展模塊可通過rpm –ql iptables | grep "\.so$" 查看
-m的選項(常見) multiport擴展(多端口匹配) 以離散方式定義多端口,最多可定義15個端口 [!] --source-ports,--sports port[,port|,port:port]... 若加[!],則是非的意思 源端口 [!] --destination-ports,--dports port[,port|,port:port]... 目標端口 [!] --destination-ports,--dports port[,port|,port:port]... 放開22、80、8080端口 iptables -I INPUT -s 192.168.0.0/24 -d 192.168.1.11 -p tcp -m multiport --dports 22,80,8080 -j ACCEPT iptables -I OUTPUT -s 192.168.1.11 -d 192.168.0.0/24 -p tcp -m multiport --sports 22,80,8080 -j ACCEPT iprange擴展(IP范圍) 指明連續的IP范圍時使用(但一般是不能擴展為整個網絡) [!] --src-range from[-to] 源IP地址范圍 [!] --dst-range from[-to] 目標IP地址范圍 放開192.168.1.1-200訪問22,80,8080端口 iptables -I INPUT -d 192.168.1.11 -p tcp -m multiport --dport 80,22,8080 -m iprange --src 192.168.1.1-192.168.1.200 -j ACCEPT iptables -I OUPUT -s 192.168.1.11 -p tcp -m multiport --sport 80,22,8080 -m iprange --dst 192.168.1.1-192.168.1.200 -j ACCEPT string擴展(字符串擴展) 檢查報文中出現的字符串 --algo {bm|kmp} 指定匹配字符串的算法 [!] --string pattern 基于匹配模式進行檢查 [!] --hex-string pattern 基于十六進制檢查 iptables -I OUTPUT -m string -algo bm --string 'mover' -j REJECT \\拒絕訪問‘mover’字符串的內容 time擴展(基于時間做檢查) --datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]] 起始日期 --datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]] 結束日期 --timestart hh:mm[:ss] 起始時間,以每天為計 --timestop hh:mm[:ss] 結束時間,以每天為計 [!] --monthdays day[,day...] 每月的周期, [!] --weekdays day[,day...] 每周的周期 iptables -I INPUT -d 192.168.1.11 -p tcp --dport 80 -m time --timestart 8:00 --timstop 17:00 -j REJECT \\拒絕8點到17點之間訪問80端口 connlimit擴展(連接限制) 根據每個客戶IP做并發連接數匹配 --connlimit-above n:連接的數量大于n --connlimit-upto n:連接的數量小于等于n iptables -I INPUT -D TCP --dport 22 -m connlimit --connlimit-above 3 -j REJECT;\\設置22端口的并發連接數量不能大于3個,大于的拒絕 limit擴展 基于收發報文包的速率做檢查 令牌桶過濾器 --limit-burst number 一開始只是最多允許的報文數量 --limit rate[/second|/minute|/hour|/day] 當達到--limit-bust 指定的數量后,設置的速率,有秒、分鐘、小時、天 iptables -A INPUT -d 192.168.1.11 -p icmp --icmp-type 8 -m limit --limit-burst 5 --limit 30/minute -j ACCEPT \\設置開始時最多5個ping,然后每分鐘30個 iptables -A INPUT -s 192.168.1.11 -p icmp --icmp-type 0 -j ACCEPT state擴展(狀態擴展) 根據連接追蹤機制,檢查連接間的關系,記錄的時長為半小時 調整連接追蹤功能所能夠容納的最大連接數量: /proc/sys/net/nf_conntrack_max 已經追蹤到并記錄下的連接: /proc/net/nf_conntrack 不同協議或連接類型追的時長: /proc/sys/net/netfilter/ 可追蹤的連接狀態: NEW:新發出的請求;連接追蹤模板中不存此連接相關的信息條目,因此,將其識別為第一次發出的請求; ESTABLISHED:NEW狀態之后,連接追蹤模板中為其建立的條目失效之前期間內所進行的通信的狀態; RELATED:相關的連接;如ftp協議的命令連接與數據連接之間的關系; INVALIED:無法識別的連接; --state STATE1,STATE2,... 對22、80端口進行連接追蹤 iptables -I INPUT -d 192.168.1.11 -p tcp -m multiport --dport 22,80 state --state NEW,ESTABLISHED -j ACCEPT iptables -I OUTPUT -s 192.168.1.11 -p tcp -m multiport --sport 22,80 state --state ESTABLEISHED -j ACCEPT iptables -I OUTPUT -m state --state ESTABLEISHED -j ACCEPT \\對IN方向允許進來的所有連接追蹤,都在out方向放開
處理動作:
-j TARGET:jump,跳轉到指定的TARGET,TARGET如下: ACCEPT:接受 DROP:丟棄 REJECT:拒絕 RETURN:返回調用鏈 REDIRECT:端口重定向 LOG:記錄日志 MARK:做防火墻標記 DNAT:目標地址轉換 SNAT:源地址轉換 MASQUERADE:地址偽裝
練習依照以下圖片
說明: SER1配置默認路由指向192.168.90.128 練習內容: 1、通過iptables拒絕SER1訪問SER2的ssh\web, 2、SER3配置192.168.90.0路,讓SER1連通SER3 3、在SER2上啟用NAT,SER3不配置192.168.90.0路由,讓SER1連通SER3 4、把SER2的192.168.10.128的22端口映射給SER1 192.168.90.129的22端口
SER1的配置
[root@SER1 ~]# ifconfig eth2 Link encap:Ethernet HWaddr 00:0C:29:1D:8D:A8 inet addr:192.168.90.129 Bcast:192.168.90.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe1d:8da8/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:86 errors:0 dropped:0 overruns:0 frame:0 TX packets:84 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:9529 (9.3 KiB) TX bytes:10126 (9.8 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:108 errors:0 dropped:0 overruns:0 frame:0 TX packets:108 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:7256 (7.0 KiB) TX bytes:7256 (7.0 KiB) [root@SER1 ~]# route add default gw 192.168.90.128 [root@SER1 ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.90.0 * 255.255.255.0 U 1 0 0 eth2 default 192.168.90.128 0.0.0.0 UG 0 0 0 eth2
SER2的配置
[root@SER2 ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:76:31:7B inet addr:192.168.10.128 Bcast:192.168.10.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe76:317b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:613 errors:0 dropped:0 overruns:0 frame:0 TX packets:157 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:57414 (56.0 KiB) TX bytes:13174 (12.8 KiB) eth1 Link encap:Ethernet HWaddr 00:0C:29:76:31:85 inet addr:192.168.90.128 Bcast:192.168.90.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe76:3185/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:672 errors:0 dropped:0 overruns:0 frame:0 TX packets:149 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:62895 (61.4 KiB) TX bytes:15431 (15.0 KiB)
測試SER1與SER2的連通性,以下測試的結果說明SER1與SER2的連通性沒有問題
[root@SER1 ~]# ping 192.168.90.128 PING 192.168.90.128 (192.168.90.128) 56(84) bytes of data. 64 bytes from 192.168.90.128: icmp_seq=1 ttl=64 time=0.691 ms 64 bytes from 192.168.90.128: icmp_seq=2 ttl=64 time=1.15 ms ^C --- 192.168.90.128 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1262ms rtt min/avg/max/mdev = 0.691/0.924/1.158/0.235 ms [root@SER1 ~]# curl 192.168.90.128 test [root@SER1 ~]# [root@SER1 ~]# ssh 192.168.90.128 root@192.168.90.128's password: Last login: Sat Jul 2 02:24:28 2016 from 192.168.90.129 [root@SER2 ~]# [root@SER2 html]# iptables -L \\SER2的iptables表 Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination [root@SER2 html]#
一、通過iptables拒絕SER1訪問SER2的ssh\web
1、把SER2的filter表的規則清空
[root@SER2 ~]# iptables -F [root@SER2 ~]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination [root@SER2 ~]#
2、拒絕SER1 ping SER2 和SER1 訪問SER2 的SSH
[root@SER2 ~]# iptables -A INPUT -s 192.168.90.129 -p tcp --dport 80 -j DROP [root@SER2 ~]# iptables -A INPUT -s 192.168.90.129 -p tcp --dport 22 -j DROP [root@SER2 ~]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination DROP tcp -- 192.168.90.129 anywhere tcp dpt:http DROP tcp -- 192.168.90.129 anywhere tcp dpt:ssh Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination [root@SER2 ~]# [root@SER1 ~]# curl 192.168.90.128 curl: (7) couldn't connect to host [root@SER1 ~]# ssh 192.168.90.128 ssh: connect to host 192.168.90.128 port 22: Connection timed out [root@SER1 ~]#
3、拒絕SER1訪問SER2的192.168.90.128的80端口及ping,但可以訪問SER2的192.168.10.128的80端口與ping
先清空SER2的現在的filter的規則表 [root@SER2 ~]# iptables -F [root@SER2 ~]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination [root@SER2 ~]#
把 filter表的INPUT和OUTPUT默認策略修改為DROP
修改INPUT及OUPUT的規則
[root@SER1 ~]# curl 192.168.90.128 curl: (7) couldn't connect to host [root@SER1 ~]# curl 192.168.10.128 test [root@SER1 ~]# ping 192.168.90.128 PING 192.168.90.128 (192.168.90.128) 56(84) bytes of data. ^C --- 192.168.90.128 ping statistics --- 7 packets transmitted, 0 received, 100% packet loss, time 6907ms [root@SER1 ~]# ping 192.168.10.128 PING 192.168.10.128 (192.168.10.128) 56(84) bytes of data. 64 bytes from 192.168.10.128: icmp_seq=1 ttl=64 time=1.48 ms 64 bytes from 192.168.10.128: icmp_seq=2 ttl=64 time=0.438 ms ^C --- 192.168.10.128 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1495ms rtt min/avg/max/mdev = 0.438/0.961/1.485/0.524 ms [root@SER1 ~]#
二、SER3配置192.168.90.0路,讓SER1連通SER3
1、打開SER2的核心轉發功能
臨時開啟:把 /proc/sys/net/ipv4/ip_forward 修改為 1 永久開啟,需要修改:/etc/sysct.conf ---> net.ipv4.ip_forward = 1
[root@SER2 ~]# cat /proc/sys/net/ipv4/ip_forward 0 [root@SER2 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward [root@SER2 ~]# cat /proc/sys/net/ipv4/ip_forward 1 [root@SER2 ~]#
2、在SER3上添加192.168.90.0的路由,指向192.168.10.128
[root@SER3 ~]# route add -net 192.168.90.0/24 gw 192.168.10.128 [root@SER3 ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.10.0 * 255.255.255.0 U 1 0 0 eth2 192.168.90.0 192.168.10.128 255.255.255.0 UG 0 0 0 eth2 default 192.168.10.2 0.0.0.0 UG 0 0 0 eth2 [root@SER3 ~]#
3、在SER1上測試與SER3的連通性
[root@SER1 ~]# ping 192.168.10.130 PING 192.168.10.130 (192.168.10.130) 56(84) bytes of data. 64 bytes from 192.168.10.130: icmp_seq=1 ttl=63 time=3.59 ms 64 bytes from 192.168.10.130: icmp_seq=2 ttl=63 time=0.877 ms ^C --- 192.168.10.130 ping statistics --- 6 packets transmitted, 6 received, 0% packet loss, time 5467ms rtt min/avg/max/mdev = 0.685/1.675/3.592/1.034 ms [root@SER1 ~]#
三、在SER2上啟用NAT,SER3不配置192.168.90.0路由,讓SER1連通SER3
1、SER3刪除192.168.90.0的路由
[root@SER3 ~]# route del -net 192.168.90.0/24 [root@SER3 ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.10.0 * 255.255.255.0 U 1 0 0 eth2 default 192.168.10.2 0.0.0.0 UG 0 0 0 eth2 [root@SER3 ~]#
修改SER2之前SER1的連通性是不通的
[root@SER1 ~]# ping 192.168.10.130 PING 192.168.10.130 (192.168.10.130) 56(84) bytes of data. ^C --- 192.168.10.130 ping statistics --- 25 packets transmitted, 0 received, 100% packet loss, time 24014ms
修改SER2的NAT
[root@SER2 ~]# iptables -t nat -A POSTROUTING -s 192.168.90.0/24 -d 192.168.10.0/24 -j SNAT --to-source 192.168.10.128 [root@SER2 ~]# iptables -t nat -L Chain PREROUTING (policy ACCEPT) target prot opt source destination Chain POSTROUTING (policy ACCEPT) target prot opt source destination SNAT all -- 192.168.90.0/24 192.168.10.0/24 to:192.168.10.128 Chain OUTPUT (policy ACCEPT) target prot opt source destination [root@SER2 ~]#
再測試SER1與SER3的連通性
[root@SER1 ~]# ping 192.168.10.130 PING 192.168.10.130 (192.168.10.130) 56(84) bytes of data. 64 bytes from 192.168.10.130: icmp_seq=1 ttl=63 time=2.25 ms 64 bytes from 192.168.10.130: icmp_seq=2 ttl=63 time=0.947 ms ^C --- 192.168.10.130 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1479ms rtt min/avg/max/mdev = 0.947/1.600/2.254/0.654 ms [root@SER1 ~]# curl 192.168.10.130 1234567890 [root@SER1 ~]#
三、把SER2的192.168.10.128的22端口映射給SER1 192.168.90.129的22端口
1、修改SER2的22端口映射到192.168.90.129
[root@SER2 ~]# iptables -t nat -A PREROUTING -d 192.168.10.128 -p tcp --dport 22 -j DNAT --to-destination 192.168.90.12 9[root@SER2 ~]# iptables -t nat -L Chain PREROUTING (policy ACCEPT) target prot opt source destination DNAT tcp -- anywhere 192.168.10.128 tcp dpt:ssh to:192.168.90.129 Chain POSTROUTING (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination [root@SER2 ~]#
2、在SER3上使用SSH登陸到SER1
[root@SER3 html]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.10.0 * 255.255.255.0 U 1 0 0 eth2 default 192.168.10.2 0.0.0.0 UG 0 0 0 eth2 [root@SER3 html]# ssh 192.168.10.128 The authenticity of host '192.168.10.128 (192.168.10.128)' can't be established. RSA key fingerprint is cc:d2:aa:95:51:57:67:df:df:0a:3c:02:bb:0b:a2:bf. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.10.128' (RSA) to the list of known hosts. root@192.168.10.128's password: Last login: Sat Jul 2 02:20:32 2016 from 192.168.90.1
3、在SER1上查看SER3登陸的情況
[root@SER1 ~]# who root tty1 2016-07-02 02:20 root pts/0 2016-07-02 02:20 (192.168.90.1) root pts/1 2016-07-02 04:07 (192.168.10.130) \\SER3已成功登陸 [root@SER1 ~]#
原創文章,作者:Net20-deamon,如若轉載,請注明出處:http://www.www58058.com/21420