1. iptables 規則詳解


1.什么是防火牆,防火牆的種類有哪些

防火牆: 古人用來隔絕失火后的高牆,阻擋外來的火勢,起到了隔離的手段。
在互聯網上,我們會采用類似於防火牆的方法,來保護我們的網絡不受外來攻擊,為此我們需要設定一些防火牆的規則,確定哪些數據允許通過,哪些不能通過,具有這種功能的設備或軟件,我們將其稱為防火牆

防火牆的種類:
  邏輯層面:
    主機防火牆:針對單個主機進行防護,例如Windows防火牆   一般是軟件
    網絡防火牆:處於網路入口的邊緣,針對網絡入口進行防護  一般是硬件,也可以用軟件
  物理層面:
    硬件防火牆:一個實體的硬件,嵌入軟件,實現防火牆的功能,性能高,成本高
    軟件防火牆:通過軟件的方式,模擬防火牆的功能,運行在操作系統上面,性能低,成本低

2.iptables介紹

1.什么是iptables?
  iptables可以理解為是一個代理程序,用戶通過代理程序,將安全規則添加到安全框架中 net filter(內核空間)
  netfilter/iptables 是Linux中的包過濾型防火牆,是免費的,可以代替昂貴的商業防火牆解決方案,可以實現數據包的過濾,實現數據包的轉換。

2.什么是包過濾防火牆?
  包過濾型防火牆在我們網絡層攔截網絡數據包的包頭(header),可以針對數據包的包頭,與我們事先准備好的防火牆規則進行比對,與規則相匹配的包放行,不匹配的包丟棄或者執行一些復雜的動作,包過濾型防火牆一般作用在我們的網絡層,故也稱為網絡型防火牆。 
  通常header中帶有 客戶端來源IP,源端口,目標IP,目標端口,協議類型:tcp udp icmp,根據這些狀態信息來判斷,是否符合我們的IP tables的過濾規則,符合通過,不然拒絕。

3.包過濾型防火牆是怎么實現的?
  是基於net filter安全框架實現的,是內核的一部分,如果我們想要讓防火牆達到防火的目的,需要我們在內核中,設定一些關卡,所有進出的報文,根據設定的這些關卡進行檢查,將符合條件的放行,不符合的阻止,
  在net filter中,這些關卡就是鏈

3.iptables鏈的概念

1.什么是鏈?
  防火牆的作用就是在於將經過的報文與設定的規則進行比對,然后執行響應的動作,報文經過鏈時,必須匹配鏈的規則,在鏈上不止一條規則,存在着很多規則,當我們將這些規則穿在一起的時候就形成了鏈 
  鏈的匹配規則,如下圖所示,當有報文經過時,會以此往下匹配規則,如果匹配成功,則執行相對於的動作,如果匹配不成功,就以此往下匹配,直到最后一條規則還是沒有匹配成功,會執行鏈的默認規則。

2. iptables 有哪些鏈?
    我們從A點到達B點時,途中會經過很多的高速路,不同的高速路有不通的關卡設定,比如說,過路費,查酒駕,查超載,查超速等等,這些都可以理解為一些關卡
    在iptables中,數據流入本機,經過防火牆,會經過五個關卡,也就是五個鏈
    PREROUTING: 
    input:
    output:
    forward:
    postrouting:

    數據流入本機:當數據經過prerouting,會判斷是否是請求本機,請求本機,送往input鏈,經過input 發送到我們用戶空間的process進程
    數據流出本機:process進程發送給output,由我們的output發送給postrouting,由postrouting選擇最優路線,回傳給用戶
    數據經過本機:當用戶請求防火牆時,數據報文要傳給后端的服務器,這時數據報文是經過本機,而不是到達本機,首先會經過prerouting鏈,由於不是請求本機,所以不會送往input,會送往forward,通過postrouting選擇接口,轉給后端主機。

4.什么是表?

  對每個鏈上面會放很多規則比如:
    A:IP地址或端口過濾
    B:修改數據報文
  我們把具有相同功能規則的集合叫做表,不通功能的規則,可以放置在不同的表中進行管理
iptables中的表:
  filter:   負責做過濾功能         input、output、forward
  nat:      網絡地址轉換           PREROUTING、input、output、postrouting
  mangle:   負責修改數據包的內容    PREROUTING: input、output、forward、postrouting
  raw:  關閉nat表中的追蹤機制(用的較少)

操作不同表中的不同的鏈實現的功能不一樣,

4.1.數據包的優先級

1.當用戶A請求的是我本機的用戶進程,首先數據包進來會經過prerouting鏈,由於raw,mangle,nat表都可以管理prerouting鏈,他會依次看raw,mangle,nat 表中的鏈有什么規則,依次執行,執行完之后在進行路由選擇,
2.由於是訪問我們本機的應用進程,會送往input鏈,會先執行mangle表中的數據規則,看有沒有需要改寫的,在進入filter表中的數據規則,看有沒有需要過濾的,在送往我們的用戶空間進程

數據包的優先級
  raw--->mangle--->nat--->filter
問題1:來自於10.0.0.1的地址,訪問本機的web服務請求都不允許,應該在那個表的那個鏈上設定規則?
  在filter表中的input鏈
  prerouting鏈是由raw,mangle,nat表管理的,他們沒有數據包過濾的功能
  input 是由 filter nat mangle 管理的,filter 支持過濾,所以是由filter表中的input鏈

問題2:所有本機發往10.0.0.0/24網段的tcp服務都不允許
  在filter表中的output鏈
  output屬於filter表,有數據改寫的功能,postrouting屬於nat,mangle沒有數據包過濾的功能

問題3:所有來自本機內部網絡的主機,像互聯網發送web請求,都不允許
  防火牆在內部主機之前,內部主機要上網的化需要經過防火牆,當內部主機需要經過防火牆時,第一個經過的時prerouting,不是請求本機,只是從本機經過,所有不會轉給input,會轉給forward,forward轉給postrouting選擇接口,轉給web服務器。
  prerouting ---> froward ---> postrouting 
  prerouting 和postrouting都不屬於 filter表,所以沒有過濾功能,要在forward的上面做

PS: 要明確的了解需要做的是 過濾、地址轉換、數據包的改寫,這樣我們才知道是操作的那個表,然后我們需要知道數據包流經的路徑,這樣我們才知道應該添加到那個鏈上
  實現的功能是什么:  操作那個表
  報文經過的路徑:  判斷規則添加到那個鏈上

5.iptables 的規則

1.什么是規則?規則是如何組成的
  我們的數據包的過濾,就是基於規則,而規則,是基於條件+動作完成的,想實現防火牆達到防火的目的,無外乎是為其添加相應的規則,即,我們匹配的條件和執行的動作

2.規則的增刪查改
  iptables -t 表名 選項(增刪查改)鏈名稱 條件 動作
       -t                指定操作的表名
       -A, --append      追加一條規則到鏈中
       -D, --delete      刪除鏈中的規則
       -I, --insert      插入一條規則,插入到頂部
       -R, --replace     替換,修改
       -L, --list        列出當前的規則
       -S, --list-rules  列出所有的規則
       -F, --flush       清空  
       -Z, --zero        清空計數器(每條規則都有匹配的包數量、包大小)
       -N, --new-chain   創建一個自定義鏈
       -X, --delete-chain 刪除一個自定義鏈
       -P, --policy      指定鏈的默認規則
  

iptables -t 表名 選項(增刪查改)鏈名稱 條件 動作

hdss7-11 bin]# iptables -t filter -L -n -v #查看規則

hdss7-11 bin]# iptables -t filter -I INPUT -p icmp -j REJECT #添加一條禁止icmp的規則(禁ping)

hdss7-11 bin]# iptables -t filter -R INPUT 1 -p icmp -j DROP #修改第一條規則,改為DROP(丟棄)

[root@hdss7-11 bin]# iptables -t filter -Z #清除計數器

hdss7-11 bin]# iptables -t filter -D INPUT 1 #刪除指定編號規則

6.iptables 基本的條件匹配

1. -p      #指定協議
2. -s -d    #源地址,目標地址
3. --sport --dport  #源端口 目標端口
4. -i -o     #進來的接口,出去的接口
5. -m -j     # 

實例1:僅允許10.0.0.1訪問10.0.0.5的80端口,其他拒絕
先允許:
  1.過濾,添加到filter 表,10.0.0.1請求本機的報文,會經過prerouting,會到達input,到達用戶進程,prerouting不支持過濾,所以要在input鏈中做
  2.指定來源的ip地址    10.0.0.1
  3.指定目標的IP地址    10.0.0.5
  4.指定具體的協議    tcp
  5.指定具體的端口    80
  6.指定匹配后的動作  accept
后拒絕:
  1.添加到filter表
  2.明確指定拒絕所有

具體的配置:
  iptables -t filter -I INPUT -s 10.0.0.1 -d 10.0.0.5 -p tcp --dport 80 -j ACCEPT    #允許來源IP為10.0.0.1目的IP為10.0.0.5 協議為tcp 端口為80 的進行訪問
  iptables -t filter -A INPUT -p tcp -j DROP        #拒絕所有的tcp鏈接,注意ssh屬於tcp連接,-A 追加
  iptables -t filter -F         #清空所有的配置
實例2:凡是由本機發出的TCP協議報文,都允許出去,其他的協議不允許

先允許:
    1.過濾,filter表,要從本機出去,所以是output鏈
  
再拒絕:
    拒絕所有的

具體配置:
  iptables -t filter -I OUTPUT -p tcp -j ACCEPT      #在頂部添加一條允許tcp出去的條件
  iptables -t filter -A OUTPUT -j DROP               #追加一條條件為任何協議都不允許出去
  iptables -t filter -F OUTPUT                        #清空OUTPUT 鏈上的所有規則

實例3:禁止其他主機 從eth0 發來的ping請求
  1.過濾,filter表,請求的是本機,在input鏈上
  2.指定從哪個網絡接口過來的數據包  ech0  -i  指定接口
  3.指定具體的協議 icmp
  4.指定具體的動作 DROP

  iptables -t filter -I INPUT -p icmp -i eth0 -j DROP

       [!] -i, --in-interface name
              Name of an interface via which a packet was received (only for packets entering the INPUT, FORWARD and PREROUT‐
              ING chains).  When the "!" argument is used before the interface name, the sense is inverted.  If the interface
              name  ends in a "+", then any interface which begins with this name will match.  If this option is omitted, any
              interface name will match.

  1. iptables 擴展的條件匹配
1. multiport模塊      #可以添加多個不連續的端口,最多可以添加十五個
  實例:僅允許10.0.0.1訪問本機:80、443、20、21、22
    使用--dport 20:22  會添加20 21 22 這幾個端口,但是,是連續的
  iptables -t filter -I INPUT -s 10.0.0.1 -d 10.0.0.5 -p tcp -m multiport --dport 20,21,22,80,443 -j ACCEPT    #允許tcp 20,21,22,80,443 ,且來源IP是10.0.0.1,目的IP是10.0.0.5 的請求通過
  iptables -t filter -A INPUT -p tcp -j DROP    #拒絕所有的tcp請求通過

2.iprange模塊
  指定一段連續的ip地址范圍:用於匹配報文的源地址或者目的地址
  
  [!] --src-range from[-to]    源地址范圍  感嘆號表示非
  [!] --dst-range from[-to]    目標地址范圍

實例1 : 10.0.0.1-10.0.0.7 都不允許ping10.0.0.5這台主機

  1.過濾,filter ,流入本機需要input鏈
  2.協議 icmp
  3.執行的動作,丟棄
    iptables -t filter -I INPUT -p icmp -m iprange --src-range 10.0.0.1-10.0.0.7 -j DROP

   iprange
       This matches on a given arbitrary range of IP addresses.

       [!] --src-range from[-to]
              Match source IP in the specified range.

       [!] --dst-range from[-to]
              Match destination IP in the specified range.
3.string 模塊      
  匹配指定的字符串,數據報文中包含匹配的字符串,則滿足要求
       --algo {bm|kmp}          #匹配的查詢算法
              Select the pattern matching strategy. (bm = Boyer-Moore, kmp = Knuth-Pratt-Morris)
 
       [!] --string pattern      #指定需要匹配的字符串
              Matches the given pattern.

實例:當應用返回的數據報文包含hello,則丟棄,其他正常通過
echo "iptables-tt" > /var/www/html/index.html
echo "iptables-hello" > /var/www/html/test.html

iptables -t filter -I OUTPUT -p tcp -m string --string "hello" --algo bm -j DROP        #使用場景,防火牆,當作路由器,屏蔽一些出去的網站,比如淘寶之類的

curl 10.0.0.5/index.html
iptables-tt
curl 10.0.0.5/test.html
返回值為空

   string
       This modules matches a given string by using some pattern matching strategy. It requires a linux kernel >= 2.6.14.

       --algo {bm|kmp}
              Select the pattern matching strategy. (bm = Boyer-Moore, kmp = Knuth-Pratt-Morris)

       --from offset
              Set the offset from which it starts looking for any matching. If not passed, default is 0.

       --to offset
              Set  the offset up to which should be scanned. That is, byte offset-1 (counting from 0) is the last one that is
              scanned.  If not passed, default is the packet size.

       [!] --string pattern
              Matches the given pattern.

       [!] --hex-string pattern
              Matches the given pattern in hex notation.

       Examples:

              # The string pattern can be used for simple text characters.
              iptables -A INPUT -p tcp --dport 80 -m string --algo bm --string 'GET /index.html' -j LOG

              # The hex string pattern can be used for non-printable characters, like |0D 0A| or |0D0A|.
              iptables -p udp --dport 53 -m string --algo bm --from 40 --to 57 --hex-string '|03|www|09|netfilter|03|org|00|'

  
4.time 模塊


      --timestart hh:mm[:ss]      #開始時間
  
       --timestop hh:mm[:ss]      #結束時間
              Only match during the given daytime. The possible time range  is  00:00:00  to  23:59:59.  Leading  zeroes  are
              allowed (e.g. "06:03") and correctly interpreted as base-10.

       [!] --monthdays day[,day...]    #指定每個月的某天
              Only  match on the given days of the month. Possible values are 1 to 31. Note that specifying 31 will of course
              not match on months which do not have a 31st day; the same goes for 28- or 29-day February.

       [!] --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. You may also use two-character variants (Mo, Tu, etc.).


      --kerneltz       #使用內核時區,而不是UTC時間


實例:拒絕每天8:30 -- 18:00 任何主機發送icmp協議
 iptables -t filter -I INPUT -p icmp -m time --timeout 00:30 --timestop 10:30 -j DROP
5.icmp 模塊
  當我們iptables -t filter -I INPUT -p icmp -j DROP 時,我們無法和對端主機發送ping 請求,對端也無法ping同我
  可以使用--icmp-type 模塊,使對方無法ping我,而我可以ping同對端
  iptables -t filter -I INPUT -p icmp --icmp-type "echo-request" -j DROP  

echo-request (8)(狀態碼) 請求
echo-reply (0)  回應

   icmp (IPv4-specific)
       This extension can be used if `--protocol icmp' is specified. It provides the following option:

       [!] --icmp-type {type[/code]|typename}
              This allows specification of the ICMP type, which can be a numeric ICMP type, type/code pair,  or  one  of  the
              ICMP type names shown by the command
               iptables -p icmp -h
6.connlimit 模塊
限制每個客戶端ip地址,到服務器的並發連接數

iptables -t filter -I INPUT -p tcp --dport 23 -m connlimit --connlimit-above 2 -j REJECT        #一個ip超過兩個連接拒絕


   connlimit
       Allows you to restrict the number of parallel connections to a server per client IP address (or client address block).

       --connlimit-upto n  如果現有連接數小於或等於多少時,則進行匹配
              Match if the number of existing connections is below or equal n.

       --connlimit-above n  如果現有連接數大於,則進行匹配
              Match if the number of existing connections is above n.

       --connlimit-mask prefix_length
              Group hosts using the prefix length. For IPv4, this must be a number between (including) 0 and  32.  For  IPv6,
              between 0 and 128. If not specified, the maximum prefix length for the applicable protocol is used.

       --connlimit-saddr
              Apply the limit onto the source group. This is the default if --connlimit-daddr is not specified.

       --connlimit-daddr
              Apply the limit onto the destination group.
7.limit 
  針對報文速率進行限制 秒 分 時 天


   limit
       This module matches at a limited rate using a token bucket filter.  A rule using this extension will match until  this
       limit is reached.  It can be used in combination with the LOG target to give limited logging, for example.

       xt_limit  has no negation support - you will have to use -m hashlimit !  --hashlimit rate in this case whilst omitting
       --hashlimit-mode.

       --limit rate[/second|/minute|/hour|/day]
              Maximum average matching rate: specified as a number, with an optional `/second', `/minute', `/hour', or `/day'
              suffix; the default is 3/hour.

       --limit-burst number   當我們限制一秒鍾接受一個報文,默認是5個報文,也就是第一秒是5個報文,然后一秒一個
              Maximum  initial  number  of packets to match: this number gets recharged by one every time the limit specified
              above is not reached, up to this number; the default is 5.

實例1:限制每分鍾接收10個報文
  iptables -t filter -I INPUT -p icmp -m limit --limit 10/minute -j ACCEPT      每分鍾允許通過十個報文
  iptables -t filter -A INPUT -p icmp -j DROP 拒絕通過icmp報文

實例2:允許10個數據報文快速通過,超過的數據報文 1/m
  iptables -t filter -I INPUT -p icmp -m limit --limit 1/m --limit-burst 10 -j ACCEPT  默認十個
  iptables -t filter -A INPUT -p icmp -j DROP

實例3:限制傳輸的帶寬不可以超過500k
    500k * 1024 = 500000 b /1500 = 300 左右
    iptables -t filter -I OUTPUT -p tcp -m limit --limit 300/s -j ACCEPT
    iptables -t filter -I OUTPUT -p tcp -j DROP

如若轉載,請注明出處:https://www.cnblogs.com/yangxiaoni


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM