linux服務之iptables

關於防火牆的相關設置在咱們的學習中也扮演了很是重要的角色,下面就讓咱們一塊兒來認識一下防火牆的具體工做方式。html

(1) 對於iptables命令而言有四表五鏈之言:
tables:
            filter:過濾,防火牆;默認表
            nat:network address translation;用於修改報文的源地址或目標地址,甚至是端口;
            mangle:拆解報文,作出修改,並從新封裝起來;
            raw:關閉nat表上啓用的鏈接追蹤機制;
shell

優先級次序(由高而低):
                raw --> mangle --> nat –> filter
centos

chain:
                 PREROUTING
                 INPUT
                 FORWARD
                 OUTPUT
                 POSTROUTING
功能<-->鉤子之間對應關係以下:
             raw:PREROUTING,OUTPUT
             mangle:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
             nat:PREROUTING,INPUT,OUTPUT,POSTRUTING
             filter:INPUT,FORWARD,OUTPUT
併發

(2) 規則的編寫格式app

iptables [-t table] COMMAND chain [-m matchname [per-match-options]] [-j targetname [per-target-options]]
                 -t table:
                默認爲filter;其它可用的有raw, mangle, nat;
               COMMAND:
                 鏈:
                    -P:policy,策略,定義默認策略; 通常有兩種選擇,ACCEPT和DROP;
                    -N:new,新建一條自定義的規則鏈;被內建鏈上的規則調用才能生效;[-j  chain_name];
                    -X:drop,刪除自定義的引用計數爲0的空鏈;
                    -F:flush,清空指定的鏈;
                    -E:重命名自定義的引用計數和爲0的鏈;
                規則:
                    -A:append,追加,在指定鏈的尾部追加一條規則;
                    -I:insert,插入,在指定的位置(省略位置時表示鏈首)插入一條規則;
                    -D:delelte,刪除,刪除指定的規則;
                    -R:replace,替換,將指定的規則替換爲新規則;不能僅修改規則中的部分,而是整條規則徹底替換;
                查看:
                    -L:list,列出表中的鏈上的規則;
                        -n:numeric,以數值格式顯示;
                        -v:verbose,顯示詳細格式信息;
                            -vv, -vvv
                         -x:exactly,計數器的精確結果;
                        --line-numbers:顯示鏈中的規則編號;
dom

(3) iptables命令:
規則:根據指定的匹配條件來嘗試匹配每一個流經此處的報文,一旦匹配成功,就由規則後面指明的處理動做進行處理;
            匹配條件:
                 基本匹配條件:簡單檢查IP、TCP、UDP等報文的某屬性進行匹配的機制;                   
                 擴展匹配條件:須要藉助於擴展模塊進行的匹配條件指定即爲擴展匹配;
            處理動做:
                 基本動做:ACCEPT,DROP, ...
                 擴展動做:須要藉助擴展模塊進行的動做;
添加規則之時須要考量的問題:
             (1) 報文的流經路徑,判斷添加規則至哪一個鏈上;
             (2) 肯定要實現的功能,判斷添加規則至哪一個表上;
             (3) 要指定的匹配條件,以用於匹配目標報文;
下面咱們再來認識一下iptables命令的具體使用操做吧!        
ssh

iptables命令的使用格式:
         iptables [-t table] -I chain [rulenum] rule-specification   列出全部的防火牆策略
         經常使用的命令是
curl

  • 列出有的防火牆設置
[root@firewall ~]# 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@firewall ~]#iptables -P INPUT DROP #也能夠設置爲ACCEPT,不能爲REJECT
 [root@firewall ~]# iptables -L
Chain INPUT (policy DROP)設置的默認策略爲drop target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination 
  • 清空防火牆策略除默認

 

[root@firewall ~]# iptables -F 清空全部策略,但不會影響鏈上的默認策略
  • 追加防火牆策略
[root@firewall ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
[root@firewall ~]# iptables -L
Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- anywhere anywhere tcp dpt:http Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination 
  • 追加防火牆策略
[root@firewall ~]# iptables -I INPUT 1 -p tcp -j ACCEPT
  • 刪除防火牆策略
[root@firewall ~]# iptables -D INPUT 2 指定刪除那條策略
[root@firewall ~]# iptables -L
Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- anywhere anywhere Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination 
  • 替換防火牆策略

 

[root@firewall ~]# iptables -R INPUT 1 -p tcp --dport 80 -j ACCEPT 表明80端口是拒絕的

(4)匹配條件:
      多重條件:邏輯關係爲「與」;
      基本匹配條件:
                     [!] -s, --source address[/mask][,...]:檢查報文中的源IP地址是否符合此處指定的地址或範圍;
                     [!] -d, --destination address[/mask][,...]:檢查報文中的目標IP地址是否符合此處指定的地址或範圍;
                     [!] -p, --protocol protocol:
                         protocol:{tcp|udp|icmp}
                     [!] -i, --in-interface name:數據報文的流入接口;INPUT, FORWARD  and  PREROUTING
                     [!] -o, --out-interface name:數據報文的流出接口; FORWARD, OUTPUT and POSTROUTING
        擴展匹配條件:
         隱式擴展:不用-m選項指出matchname便可使用此match的專用選項進行匹配;
                         -p tcp:隱含了-m tcp;
                             [!] --source-port,--sport port[:port]:匹配報文中傳輸層的源端口;
                             [!] --destination-port,--dport port[:port]:匹配報文中傳輸層的目標端口;
                             [!] --tcp-flags mask comp
                                 SYN,ACK,FIN,RST,URG,PSH;   
                                 mask:要檢查的標誌位列表,以逗號分隔;
                                 comp:必須爲1的標誌位列表,餘下的出如今mask列表中的標誌位則必須爲0;
                                 --tcp-flags  SYN,ACK,FIN,RST  SYN
                             [!] --syn:
                                 至關於--tcp-flags  SYN,ACK,FIN,RST  SYN
                         -p udp:隱含了-m udp:
                             [!] --source-port,--sport port[:port]:匹配報文中傳輸層的源端口;
                             [!] --destination-port,--dport port[:port]:匹配報文中傳輸層的目標端口;
                         -p icmp:隱含了-m icmp:
                              [!] --icmp-type {type[/code]|typename}
                                 8:echo-request
                                 0:echo-reply
tcp

        顯式擴展:必須使用-m選項指出matchname,有的match可能存在專用的選項   
         獲取幫助:
                             CentOS 7:man iptables-extensions
                             CentOS 6:man iptables
          下面詳細的介紹一下擴展模塊                  
                         一、multiport擴展          以離散或連續的方式定義多端口匹配
ide

 

[root@firewall ~]# iptables -R INPUT 1 -p tcp -m multiport --dport 21:23,80,53 -j ACCEPT dport指定目的端口,sport指定源端口

                         二、iprange擴展              以連續的ip地址範圍指明連續的多地址匹配條件;
                             [!] --src-range from[-to]:源IP地址;
                             [!] --dst-range from[-to]:目標IP地址;

[root@firewall ~]# iptables -I INPUT 1 -p tcp --dport 22 -m iprange --src-range 192.168.111.101-192.168.111.103 -j ACCEPT 101到103之間的容許鏈接xshell.
[root@firewall ~]# iptables -L
Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp --  anywhere             anywhere             tcp dpt:ssh source IP range 192.168.111.101-192.168.111.103 ACCEPT tcp -- anywhere anywhere multiport dports ftp:telnet,http,domain Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination 

                        三、set擴展                   依賴於ipset命令行工具;對不連續的ip進行訪問設置

[root@firewall ~]# iptables -I INPUT 1 -p tcp --dport 80 -m set --match-set httplist src -j ACCEPT
[root@firewall ~]# iptables -I OUTPUT 1 -p tcp --sport 80 -m set --match-set httplist dst -j ACCEPT
 [root@firewall ~]# iptables -L
Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp --  anywhere             anywhere             tcp dpt:http match-set httplist src ACCEPT tcp -- anywhere anywhere multiport dports ftp:telnet,http,domain Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp --  anywhere             anywhere             tcp spt:http match-set httplist dst 作測試的話,httplist中的主機名是容許訪問的,在策略默認是關着的時候。                     

                         四、string擴展
                         對報文中的應用層數據作字符串匹配檢測;

[root@outside html]# iptables -I INPUT 1 -m string --algo bm --string "google" -j REJECT
 [root@inside ~]# curl 172.18.254.72/baidu.com
baidu [root@inside ~]# curl 172.18.254.72/google.com
^C

                        五、time擴展
                             根據報文到達的時間與指定的時間範圍進行匹配度檢測;    --kerneltz:使用內核中配置的時區
                            --timestart hh:mm[:ss]
                            --timestop  hh:mm[:ss]

[root@outside html]# iptables -R INPUT 1 -m time --timestart 11:00 --timestop 19:00 --weekdays Mon,Thu -m string --algo bm --string "baidu" -j REJECT
[root@inside ~]# curl 172.18.254.72/baidu.com
^C [root@inside ~]# curl 172.18.254.72/google.com
goo000le   

                         六、connlimit擴展
                             根據每客戶端IP作併發鏈接數匹配;
                             --connlimit-upto n:鏈接數數量小於等於n,此時應該容許;
                             --connlimit-above n:鏈接數數量大於n,此時應該拒絕

[root@outside html]#iptables -A INPUT -d 172.18.254.72 -p tcp --dport 22 -m connlimit --connlimit-upto 2 -j ACCEPT
[root@outside html]# iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 1 -j REJECT

                        七、limit擴展
                             基於收發報文的速率進行匹配;
                          --limit rate[/second|/minute|/hour|/day]:平均速率
                          --limit-burst number:峯值速率

[root@outside html]# iptables -A INPUT -p icmp --icmp-type 8 -m limit --limit 10/minute --limit-burst 6 -j ACCEPT
[root@outside html]# iptables -A INPUT -p icmp -j REJECT
 [root@inside ~]# ping 172.18.254.72
PING 172.18.254.72 (172.18.254.72) 56(84) bytes of data. 64 bytes from 172.18.254.72: icmp_seq=1 ttl=64 time=0.621 ms 64 bytes from 172.18.254.72: icmp_seq=2 ttl=64 time=2.09 ms 64 bytes from 172.18.254.72: icmp_seq=3 ttl=64 time=0.859 ms 64 bytes from 172.18.254.72: icmp_seq=4 ttl=64 time=0.707 ms 64 bytes from 172.18.254.72: icmp_seq=5 ttl=64 time=0.842 ms 64 bytes from 172.18.254.72: icmp_seq=6 ttl=64 time=0.799 ms 64 bytes from 172.18.254.72: icmp_seq=7 ttl=64 time=0.801 ms From 172.18.254.72 icmp_seq=8 Destination Port Unreachable From 172.18.254.72 icmp_seq=9 Destination Port Unreachable From 172.18.254.72 icmp_seq=10 Destination Port Unreachable From 172.18.254.72 icmp_seq=11 Destination Port Unreachable From 172.18.254.72 icmp_seq=12 Destination Port Unreachable 64 bytes from 172.18.254.72: icmp_seq=13 ttl=64 time=0.813 ms

                         八、state擴展
                             狀態檢測;鏈接追蹤機制(conntrack);
                              INVALID:沒法識別的狀態;
                             ESTABLISHED:已創建的鏈接;
                             NEW:新鏈接;
                             RELATED:相關聯的鏈接;
                             UNTRACKED:未追蹤的鏈接;
如何開放被模式的ftp服務:

(1) 裝載追蹤ftp協議的模塊; [root@outside html]# modprobe nf_conntrack_ftp (2) 放行命令鏈接 [root@outside ~]# iptables -I INPUT -p tcp --dport 21 -m state --state NEW -j ACCEPT
(3) 放行數據鏈接 [root@outside ~]# iptables -I INPUT 2 -m state --state ESTABLISHED,RELATED -j ACCEPT 

                    九、保存和重載規則:

         centos7:
         [root@outside ~]# iptables-save > /root/source
         [root@outside ~]# iptables-restore < /root/source
         CentOS 6:
             保存規則:
                 service iptables save
                 自動保存規則至/etc/sysconfig/iptables文件中;
             重載規則:
                 server iptables restore
                 從/etc/sysconfig/iptables文件中重載規則;

 iptables的基礎命令已經介紹完了,相關的命令大體就像上邊所寫的同樣,後續內容之後再作更新。

相關文章
相關標籤/搜索