iptables入門到進階

netfilter/iptables:


       netfilter是存在于內核中的一個防火墻框架,用來管理網絡數據包,netfilterIP數據包處理流程中的5個關鍵位置放置了5個鉤子(hook)函數,當數據包流經相應的位置時,相應的鉤子函數會被調用,每個對應的鉤子上有對應的表,表中有對應的規則鏈,鏈中存放著對應的規則,最終形成了對數據包處理規則的實現;

       iptables是運行于用戶空間中的進程,通過控制netfilter來實現對數據包的管理、維護與檢查;

net filter功能:

       filter包過濾;

       NAT網絡地址轉換(Network Address Translation);

       mangle拆解報文,做出修改,而后重新封裝;

       raw關閉nat表上啟用的連接追蹤機制;

鉤子函數(hook function):

1、 prerouting

2、 input

3、 forward

4、 output

5、 postrouting

 

每張表可以放置對應的鉤子:

       filterinput 
forward  output

       natprerouting 
output  postrouting

       mangleprerouting 
input  forward  output 
postrouting

       rawprerouting  output

iptables:內置的鏈

1、 PREROUTING

2、 INPUT

3、 FORWARD

4、 OUTPUT

5、 POSTROUTING

注:允許用戶自定義規則鏈,它們需要手動關聯至指定的“鉤子”;
各表的優先級:raw à mangle à nat à filter

iptables規則的組成:

匹配條件:

網絡層首部:源地址、目標地址

傳輸層首部:源端口、目標端口、TCP Flags…

       以及一些擴展模塊引入的檢查機制;

跳轉目標:-j  target

targetDROP  ACCEPT  REJECT  SNAT 
DNAT  MASQUERADE LOG ….

 

iptables命令的基本格式:

       iptables [-t table] {-A|-C|-D} chain
rule-specification

      
ip6tables [-t table] {-A|-C|-D} chain rule-specification

      
iptables [-t table] -I chain [rulenum] rule-specification   

      
iptables [-t table] -R chain rulenum rule-specification

      
iptables [-t table] -D chain rulenum

      
iptables [-t table] -S [chain [rulenum]]

      
iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options…]

      
iptables [-t table] -N chain

      
iptables [-t table] -X [chain]

      
iptables [-t table] -P chain 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]

COMMANDs

鏈管理:

       -N,
–new-chain chain
:新建一個自定義的規則鏈;

       -X,
–delete-chain [chain]
:刪除用戶自定義的引用計數為0的空鏈;

       -F,
–flush [chain]
:清空指定的規則鏈上的規則;

       -E,
–rename-chain old-chain new-chain
:重命名鏈;

       -Z, –zero
[chain [rulenum]]
:置零計數器;

              注意:每個規則都有兩個計數器

                     packets:被本規則所匹配到的所有報文的個數;

                     bytes:被本規則所匹配到的所有報文的大小之和;

       -P,
–policy chain target
:定義指定鏈的默認target

規則管理:

              -A,–append
chain rule-specification
:向指定鏈的尾部追加規則;

              -D,–delete
chain rulenum
:刪除指定鏈中的某條規則;

              -I,–insert
chain rulenum
:向指定鏈中的指定位置插入規則,默認為首部;

              -R –replace chain rulenum:替換指定鏈中的某條規則;

              -L –list [chain]:列出指定鏈中的所有規則;

                            –line-numbers:顯示鏈中規則的序號;

              -S –list-rules [chain]:列出指定鏈中的所有規則;

              -F:清除指定鏈的所有規則;

PARAMETERS

       通用匹配:

              [!] -s ,–source address[/mask][,..]:檢查報文的源地址是否符合或等于此處的地址;

              [!] -d ,–destination
address[/mask][,..]
:檢查報文的目標地址是否符合或等于此處的地址;

              [!] -p ,–rotocol protocol:匹配報文中的協議,可用值tcp, udp,  udplite, icmp,  icmpv6,esp, 
ah, sctp, mh
或者 “all”, 亦可以數字格式指明協議

              [!] -i ,–in-interface name :限定報文僅能夠從指定的接口流入;only for packets entering the INPUT, FORWARD  and 
PREROUTING  chains;

           [!] -o, –out-interface name:限定報文僅能夠從指定的接口流出;for packets entering the FORWARD, OUTPUT and POSTROUTING chains.

              -m –match match:調用指定的擴展模塊,來擴展匹配條件;

       擴展匹配:

              -m tcp

                     –sport   –dport

 

基礎使用:
       1
、創建一條新鏈,添加規則并關聯至指定的鉤子上:

              iptables -N Test_Chain

              iptables -A Test_Chain -d
172.18.54.3  -j REJECT

              iptables -A INPUT -j Test_Chain

       刪除自定義鏈:

              iptables -D INPUT 1 (此處的1為自定義鏈在引用鏈中所處的規則條目,如果引用多次,需要逐個刪除;)

              iptables -Z Test_Chain

              iptables -X Test_Chain

       重命名鏈:

              iptables -E Test_Chain  New_Chain_Name

       清空鏈上的規則:

              iptables
-F
(不指明鏈名,默認清空filter表上的所有鏈上的規則)

       刪除規則:

              iptables -D INPUT Number

       插入規則:

              iptables -I INPUT -j REJECT (默認插入首部)

       改變規則鏈默認策略:

              iptables -P INPUT DROP

rule-specification
= [matches…] [target]

              matches:匹配條件

              target:跳轉目標

 

1、開放本機的所有tcp服務給所有主機;

                            # iptables -I INPUT  -p
tcp -j ACCEPT

                            # iptables -I OUTPUT
-p tcp -j ACCEPT

2、開放本機的所有udp服務給172.16.0.0/16網絡中的主機,但不包含172.16.0.200;

                            # iptables -I INPUT
2 -s 172.18.0.200 -p udp -j REJECT

                            # iptables -I INPUT
3  -s 172.18.0.0/16 -p udp -j ACCEPT

                            #
iptables -I OUTPUT 2 -s 172.18.0.67 -d 172.18.0.0/16 -p udp -j ACCEPT

3、默認策略為REJECT;

                            # iptables -A INPUT
-j REJECT

擴展:

1、 僅開放本機的ssh服務給172.16.0.0/16中的主機,而且不包含172.16.0.200;

# iptables -s
172.16.0.200  -p tcp –dport 22 -j  REJECT

# iptables -s
172.16.0.0/16 -p tcp –dport 22 -j ACCEPT

 

擴展條件匹配:

    隱式擴展:

       -p tcp:可直接使用tcp擴展模塊的專用選項;

            [!] –source-port,–sport
port[:port] 
匹配報文源端口;可以給出多個端口,但只能是連續的端口范圍
;

            [!]
–destination-port,–dport port[:port]  
匹配報文目標端口;可以給出多個端口,但只能是連續的端口范圍

            [!] –tcp-flags mask
comp 
匹配報文中的tcp協議的標志位;Flags are: SYN ACK FIN RST URG PSH ALL NONE;

                 mask:要檢查的FLAGS list,以逗號分隔;

                 comp:在mask給定的諸多的FLAGS中,其值必須為1FLAGS列表,余下的其值必須為0;

                     –tcp-flags
SYN,ACK,FIN,RST  SYN

                     –tcp-flags
ALL ALL

                      –tcp-flags
ALL NONE

            [!] –syn    –tcp-flags
SYN,ACK,FIN,RST  SYN

                                  

              -p udp:可直接使用udp協議擴展模塊的專用選項:

                     [!] –source-port,–sport
port[:port]

                     [!]
–destination-port,–dport port[:port]

                                  

                     [!] –icmp-type
{type[/code]|typename}

                            0/0 echo reply

                            8/0echo request

                                  

   顯式擴展:必須使用-m選項指明要調用的擴展模塊的擴展機制;

       1、multiport

            以離散或連續的 方式定義多端口匹配條件,最多15個;

                  [!] –source-ports,–sports
port[,port|,port:port]…
:指定多個源端口;

                  [!] –destination-ports,–dports
port[,port|,port:port]…
:指定多個目標端口;

                  # iptables -I INPUT 
-d 172.16.0.7 -p tcp -m multiport –dports 22,80,139,445,3306 -j ACCEPT
                      

       2、iprange

                以連續地址塊的方式來指明多IP地址匹配條件;

                [!] –src-range from[-to]

                [!] –dst-range from[-to]                   

               # iptables -I INPUT -d
172.16.0.7 -p tcp -m multiport –dports 22,80,139,445,3306 -m iprange
–src-range 172.16.0.61-172.16.0.70 -j REJECT
                          

       3、time

               –timestart hh:mm[:ss]

               –timestop hh:mm[:ss]

              [!] –weekdays
day[,day…]

      [!] –monthdays day[,day…]

                       –datestart
YYYY[-MM[-DD[Thh[:mm[:ss]]]]]

                       –datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]    –kerneltz:使用內核配置的時區而非默認的UTC;

      4、string

              –algo {bm|kmp}

               [!] –string pattern

               [!] –hex-string
pattern

                        –from offset

                        –to offset

                 # iptables -I OUTPUT
-m string –algo bm –string “gay” -j REJECT

      5、connlimit

                 –connlimit-upto n

                 –connlimit-above n

                  # iptables -I INPUT
-d 172.16.0.7 -p tcp –syn –dport 22 -m connlimit –connlimit-above 2 -j
REJECT

     6、limit                            

                  –limit rate[/second|/minute|/hour|/day]

                  –limit-burst number

                   # iptables -I OUTPUT
-s 172.16.0.7 -p icmp –icmp-type 0 -j ACCEPT
                        

      7、state

                   [!] –state state

                       INVALID,
ESTABLISHED, NEW, RELATED or UNTRACKED.

                           NEW: 新連接請求;

                           ESTABLISHED:已建立的連接;

                           INVALID:無法識別的連接;

                           RELATED:相關聯的連接,當前連接是一個新請求,但附屬于某個已存在的連接;

                           UNTRACKED:未追蹤的連接;

                           state擴展:

                                 內核模塊裝載:

                                        nf_conntrack

                                        nf_conntrack_ipv4

                                  手動裝載:

                                        modprobe  nf_conntrack_ftp        

                     追蹤到的連接:

                            /proc/net/nf_conntrack       

                     調整可記錄的連接數量最大值:

                            /proc/sys/net/nf_conntrack_max

                     超時時長:

                            /proc/sys/net/netfilter/*timeout*

處理動作(跳轉目標):

       -j targetname [per-target-options]

              簡單target

                     ACCEPT DROP                 

              擴展target

                     REJECT

                            –reject-with type

                                   type   icmp-net-unreachable,
icmp-host-unreachable

icmp-port-unreachable,icmp-proto-unreachable

icmp-net-prohibited

icmp-host-prohib‐ited, or
icmp-admin-prohibite
                                                                     

                            LOG

                                   –log-level

                                          levelemerg, alert, crit, error, warning, notice, info or debug.

                                   –log-prefix:定義日志前綴;

保存和載入規則:

       保存:iptables-save
> /PATH/TO/SOME_RULE_FILE

       重載:iptabls-restore
< /PATH/FROM/SOME_RULE_FILE

              -n, –noflush:不清除原有規則

              -t, –test:僅分析生成規則集,但不提交

CentOS
6

       保存規則:

              service iptables save

              保存規則于/etc/sysconfig/iptables文件,覆蓋保存;

       重載規則:

              service iptables restart

              默認重載/etc/sysconfig/iptables文件中的規則

       配置文件:/etc/sysconfig/iptables-config

CentOS 7

              (1) 自定義Unit
File
,進行iptables-restore;

              (2) firewalld服務;

              (3) 自定義腳本;

       規則優化的思路:

              使用自定義鏈管理特定應用的相關規則,模塊化管理規則;

              (1) 優先放行雙方向狀態為ESTABLISHED的報文;

            (2) 服務于不同類別的功能的規則,匹配到報文可能性更大的放前面;

              (3) 服務于同一類別的功能的規則,匹配條件較嚴格的放在前面;

              (4) 設置默認策略:白名單機制

                     (a) iptables -P,不建議;

                     (b) 建議在規則的最后定義規則做為默認策略;

iptables/netfilter網絡防火墻:

              (1) 網關;

              (2) filter表的FORWARD鏈;

              要注意的問題:

                     (1) 請求響應報文均會經由FORWARD鏈,要注意規則的方向性;

                     (2) 如果要啟用conntrack機制,建議將雙方向的狀態為ESTABLISHED的報文直接放行;        

              NAT: Network Address Translation

                     請求報文:由管理員定義;

                     響應報文:由NATconntrack機制自動實現;

                     請求報文:

                            改源地址:SNAT

                            改目標地址:DNAT

                           

               iptables/netfilter

                     NAT定義在nat表;

                            PREROUTINGINPUT,OUTPUT,POSTROUTING


                            SNATPOSTROUTING

                            DNATPREROUTING                     

              target

                     SNAT

                            –to-source
[ipaddr[-ipaddr]]

                     #
iptables -t nat -I POSTROUTING -j SNAT –to-source 172.18.54.3

                           

                     DNAT

                            –to-destination
[ipaddr[-ipaddr]][:port[-port]]

                     # iptables -t nat -I
PREROUTING -j DNAT –to-destination 172.18.54.2

                           

                      MASQUERADE

                            SNAT場景中應用于POSTROUTING鏈上的規則實現源地址轉換,但外網地址不固定時,使用此target;

       

原創文章,作者:M22-Zero,如若轉載,請注明出處:http://www.www58058.com/74677

(0)
M22-ZeroM22-Zero
上一篇 2017-05-02 21:00
下一篇 2017-05-02 21:49

相關推薦

  • Linux上的文件管理類命令

    N27-第二周作業

    2017-10-08
  • bash功能特性五 重定向和管道

    一、管道     使用“|”符號來實現管道功能;管道可以實現在一行中處理使用管道符號連接的多個命令,后面的命令使用前面命令的輸出結果做為處理對象。 二、數據流重定向     數據輸入和輸出的類型:         標準輸入(std…

    Linux干貨 2015-04-21
  • 重定向以及管道

    描述I/O設備的重定向

    Linux干貨 2017-11-19
  • 運維自動化之系統安裝

    自動化安裝系統,cobbler的安裝使用

    Linux干貨 2018-01-15
  • linux文件系統目錄結構

    FHS標準      文件和目錄被組織成一個單根的倒置樹結構 文件系統從根目錄開始用“/”表示 根文件系統:root filesystem 目錄名稱vfat格式不敏感大小寫 以.開頭的為隱藏文件 路徑分割的/ /boot:引導文件存放目錄,內核文件,引導加載器都存放此目錄 /bin:供所有用戶使用的基本命令;不能關聯到…

    Linux干貨 2016-07-29
  • WORD文檔格式要求與linux學習規劃

    WORD文檔格式要求 一、封面               1、活動名稱(隸書,小初號字,加粗)               2、主辦單位、策劃日期(宋體、3號字) 二、目錄    &nbs…

    Linux干貨 2016-12-12
欧美性久久久久