iptables(1)


Firewall:隔離工具;Packets Filter Firewall;工做於主機或網絡的邊緣,對經由的報文根據預先定義的規則(匹配條件)進行檢測,對於可以被規則匹配到的報文實行某預約義的處理機制的一套組件;網絡

    硬件防火牆:在硬件級別實現部分功能的防火牆;另外一個部分功能基於軟件實現; app

    軟件防火牆:應用軟件處理邏輯運行於通用硬件平臺之上的防火牆;框架


    主機防火牆:服務範圍爲當前主機;tcp

    網絡防火牆:服務範圍爲防火牆被的局域網;ide

iptables/netfilter工具

    netfilter:防火牆框架,framework;位於內核空間;post

    iptables:命令行工具程序,位於用戶空間;規則管理工具;spa


    netfilter命令行

        hooks functionserver

            prerouting

            input 

            forward 

            output 

            postrouting

    iptables

        CHAINS:

            PREROUTING

            INPUT

            FORWARD

            OUTPUT

            POSTROUTING

wKiom1gLYCGAoqumAACNsHTqDiY145.png-wh_50

    報文流向:

        到本機某進程的報文:PREROUTING --> INPUT 

        由本機轉發的報文:PREROUTING --> FORWARD --> POSTROUTING

        由本機的某進程發出報文:OUTPUT --> POSTROUTING

    tables:

        filter:過濾,防火牆;

        nat:network address translation,網絡地址轉換;

        mangle:拆解報文,作出修改,並從新封裝;

        raw:關閉nat表上啓用的鏈接追蹤機制;

    優先級次序(由高而低):

        raw --> mangle --> nat --> filter 

    功能<-->鉤子:

        raw僅適用於:PREROUTING,OUTPUT

        mangle五個鏈均適用:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING

        nat上僅有四個鏈:PREROUTING,INPUT,OUTPUT,POSTROUTING

        filter僅有三個鏈:INPUT,FORWARD,OUTPUT

wKiom1gLYEXSsxlpAANV5lTh6lw520.png

    iptables規則的組成部分:

         匹配條件:

            網絡層首部:Source IP, Destination IP

            傳輸層首部:Source Port, Destination Port

            擴展檢查機制:

         處理動做:target

            ACCEPT,DROP,REJECT

防火牆安裝:

    netfilter:要確認內核編譯的時候編譯了netfilter,位於內核中的tcp/ip協議棧報文處理框           架;

    iptables:

        CentOS 5/6:iptables命令編寫規則;

            # iptables -t filter -F

            # service iptables save

        CentOS 7:firewalld,firewall-cmd, firewall-config

            # systemctl disable firewalld

        補充:若是想向6同樣使用iptables,能夠安裝iptables-server工具

            yum install -y iptables-server

            cp /usr/libexec/iptables/iptables.init /etc/init.d/iptables

            以後就可使用service命令控制iptables了

        程序包:iptables, iptstate

iptables命令:

    規則:根據指定的匹配條件來嘗試匹配每一個流經此處的報文,一旦匹配成功,

        則由規則後面指定的處理動做進行處理;

    匹配條件:

        基本匹配條件:源地址,目標地址,傳輸層協議

        擴展匹配條件:須要藉助於擴展模塊進行指定的匹配條件

        隱式擴展:已經在基本匹配條件中指明的協議相關的擴展;

        顯式擴展:隱式擴展以外的其它擴展匹配條件;

    處理動做:

        基本動做:ACCEPT,DROP,...

        擴展動做:須要藉助於擴展模塊進行,但無須顯式指定,僅需指明動做;

             在擴展模塊中,大寫字母顯示的模塊就是爲擴展動做的

    添加規則時須要考量的問題:

        (1) 報文流經的位置:用於判斷將規則添加至哪一個鏈;

        (2) 實現的功能:用於判斷將規則添加至哪一個表;

        (3) 報文的方向:用於判斷哪一個爲「源」,哪一個爲「目標」;

        (4) 匹配條件:用於編寫可以正確匹配目標報文的規則;

iptabls命令的使用格式:

    iptables [-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]

    規則管理格式:iptables [-t able] COMMAND chain cretieria [ -m matchname [per-match-options]]  [-j targetname [per-target-options]]

        -t table:指明要管理的表; 默認爲filter;

COMMANDS

   鏈管理:

    -P:iptables [-t table] -P chain target,定義鏈的默認策略;其target通常可以使用ACCEPT或DROP;

    iptables -P INPUT ACCEPT/DROP 內網的時候可使用REJECT,外網儘可能不要用REJECT

    -N:iptables [-t table] -N chain,自定義規則鏈;僅在默認鏈經過某規則進行調用方可生效;所以,每一個自定義鏈都有其引用記數;

    -X:iptables [-t table] -X [chain],刪除自定義的空的引用計數爲0的鏈;

    -F:iptables [-t table] -F [chain [rulenum]] [options...],清空指定的鏈,或刪除指定鏈上的規則 ;

    -E:iptables [-t table] -E old-chain-name new-chain-name,重命名自定義的引用計數爲0的鏈;

    -Z:iptables [-t table] -Z  [chain [rulenum]] [options...]

規則:

    -A:append, iptables [-t table] -A chain rule-specification,追加規則到指定的鏈尾部;

    -I:insert, iptables [-t table] -I chain [rulenum] rule-specification,插入規則到指定的鏈中的指定位置,默認爲鏈首;

    -D:delete,iptables [-t table] -D chain rule-specification或iptables [-t table] -D chain rulenum,刪除指定的鏈上的指定規則;

    -R:replace,iptables [-t table] -R chain rulenum rule-specification,將指定的鏈上的指定規則替換爲新的規則;

查看:

    -L:list, iptables [-t table] -L [chain [rulenum]] [options...]

    -n:數字格式;

    -v:verbose,詳細格式信息;

    -vv, -vvv 

    --line-numbers:顯示鏈上的規則的編號;

    -x:exactly,顯示計數器的精確值; 

計數器:

    每條規則以及鏈的默認策略分別有各自的兩個計數器:

    (1) 匹配到的報文的個數:pkts

    (2) 匹配到的全部報文的大小之積:bytes

回顧:

iptables/netfilter

四表:raw, mangle, nat, filter

五鏈:PREROUTING, INPUT, FORWARD, OUTPUT, POSTROUTING

iptables命令:

iptables [-t able] COMMAND chain cretieria [-m -m matchname [per-match-options]]  [-j targetname [per-target-options]]

COMMANDS:

鏈管理:-P,-N,-X,-E,-F,-Z

規則管理:-A,-I,-D,-R

查看:-L,-n, -x, -v, --line-numbers

相關文章
相關標籤/搜索