Linux-防火牆配置-iptables命令

1、防火牆簡介

  • 防火牆功能:限制訪問(限制mac、IP、port),限制訪問數量,給數據包打標籤,控制網速,統計流量等
    即過濾數據包

  • 防火牆分類:硬件/軟件防火牆, Linux中的防火牆偏硬件(因爲是調用系統內核使用的)

  • Linux(CentOS)中的防火牆:iptables、firewall、SELinux

    • Linux是通過iptables(firewall也是基於iptables的)向內核寫入規則
    • 也就是說Linux下的防火牆分爲內核態和用戶態
      • 內核態:netfilter
      • 用戶態:iptables | firewall,爲指令集

2、iptables

(1)基本使用

  • 安裝
yum install iptables.x86_64 iptables-devel.x86_64 iptables-services.x86_64 -y
  • 啓動iptables的方法
systemctl start iptables
  • 使用
    1. 查看防火牆規則:iptables -L
    2. 清除所有防火牆規則:iptables -F

(2)防火牆的基本原理(Linux下)

  • 防火牆就是對指定條件的數據包作出指定動作,包括不限於過濾
  • Linux下,通過iptables命令將規則寫入內核態,當有數據包進入或出去時,就會和我們所寫的規則一條條去匹配,若匹配到特定條件的規則,則按該規則所指定的動作執行;若未匹配到特定條件,則按默認規則處理
  • iptables定義的規則
    • 表:定義的規則放到表裏,有4張表
      • raw 數據包跟蹤
      • mangle 標記數據包(只標記,不做其它動作)
      • nat 網絡地址轉換,用於修改源地址和目的地址
      • filter 數據包過濾
    • 鏈:鏈裏放的是具體的規則,一張最多有5種鏈
      • PREROUTING 路由之前(即針對的是路由之前的數據包)
      • INPUT 數據包流入網卡
      • FORWARD 數據包流經網卡
      • OUTPUT 數據包流出網卡
      • POSTROUTING 路由之後
    • 匹配條件時按順序匹配(raw–>mangle–>nat–>filter,鏈也是從上到下按順序匹配)

(3)iptables的語法規則

  • 規則分爲增刪改查四種類型
  • 語法模版:iptables [-t table_name] <動作> <鏈名> <匹配條件> <目標動作>

<1>查看規則

  • 查看防火牆規則

    • iptables -L 查看4張表的全部規則

    • iptables -L -t table_name 查看指定表的規則

    • iptables -nL 把所有主機名用IP地址表示

    • iptables -S [-t table_name] 直接查看創建規則時的命令

      [root@192 ~]# iptables -L -t filter 
      Chain INPUT (policy ACCEPT)
      target     prot opt source               destination         
      
      Chain FORWARD (policy ACCEPT)
      target     prot opt source               destination   
      ...
      
      [root@192 ~]# iptables -S -t filter 
      -P INPUT ACCEPT
      -P FORWARD ACCEPT
      ...
      

<2>添加規則

  • 增加規則:-A(add)或-I(insert)

    • 在指定表的最後面增加一條規則(若沒有-t table_name,默認爲filter表)

      iptables -t filter -A INPUT -p icmp -s IP -j REJECT
      
      • -A 表示動作爲添加規則
      • INPUT 表示添加的規則是針對INPUT鏈的(即進入網卡的數據包)
      • -p icmp -s IP 匹配的條件
        • -p icmp 表示使用的協議是ICMP協議的(協議是protocol)
        • -s IP 表示指定源IP地址(源地址是source IP)
      • -j REJECT 表示拒絕匹配到的數據包(jump有快速行動的意思)
      • 該命令的效果是拒絕指定IP地址的主機ping本機
    • 在一張表中指定鏈的指定位置插入規則

      iptables [-t table_name] -I [n] <鏈名> <匹配條件> <目標動作>
      
      • -I [n] 不加n時,是在指定表的指定鏈的最前面加入規則,使用n可以指定添加到第n條
      • 同理,刪除時也可以指定刪除,如iptables -D INPUT 3表示刪除filter表的INPUT鏈的第3條規則
    • 自定義鏈:非自定義鏈(即那5個鏈)是即寫即生效的,而自定義鏈寫好後要被調用才生效

      • 創建自定義鏈

        iptables -N <chain_name>
        
        • 若要更改自定義鏈的名字,使用命令iptables -E <old_name> <new_name>
        • 創建後就可以往自定義鏈裏寫規則了
      • 調用自定義鏈,即將自定義鏈添加到5條鏈中去

        iptables -t filter -A INPUT -j test
        
        • 該命令是將自定義鏈test添加到filter表的INPUT鏈中去
      • 刪除自定義鏈:注意,被調用的自定義鏈和寫了規則的自定義鏈不能刪除

        iptables -X <chain_name>
        

<3>更改規則

  • 不常用,比較麻煩,一般先刪除再添加即可

    iptables [-t table_name] -R n <chain_name> <匹配條件> <目標動作>`
    
    • R是replace的意思
    • -R n 表示修改指定表中的指定鏈的第n條規則

<4>刪除規則

  • 刪除所有規則:iptables -F
  • 刪除指定鏈的規則:iptables -F <chain_name>
  • 刪除指定鏈的指定行:iptables -D INPUT 3表示刪除filter(默認)的INPUT鏈的第3條規則

<5>關於默認規則

  • 所有數據包都會經過防火牆,當數據包匹配到了特點規則時,就按指定的動作執行;當未匹配到特點規則時,就按默認動作(規則)執行。默認規則只有ACCEPTDROP

  • 查看一下默認規則

    [root@192 ~]# iptables -L
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination 
    ...
    
    • 可以看到這條INPUT鏈的默認規則時ACCEPT
  • 修改默認規則

    iptables -P INPUT DROP
    
    • 該命令是將INPUT鏈的默認規則都改爲DROP(丟棄)

<6>補充

  • iptables還可以查看流量

    iptables -nL -v
    # 查看流量
    iptables -Z [chain_name n]
    # 清空流量,可以指定鏈和鏈中第n條規則的流量
    

(4)iptables的匹配條件

  • iptables可以匹配的條件非常多,可以在需要的時候查看手冊,如查看設定一個範圍的IP地址的用法

    [dream@192 ~]$ iptables -m iprange --help
    [dream@192 ~]$ iptables -m iprange --help
    iprange: Could not determine whether revision 1 is supported, assuming it is.
    iptables v1.4.21
    ...
    iprange match options:      # 這就是iprange的用法
    [!] --src-range ip[-ip]    Match source IP in the specified range
    [!] --dst-range ip[-ip]    Match destination IP in the specified range
    
    • -m range m表示match,即擴展匹配
    • 另外可以使用命令man iptables -extensions查看所有用法
  • iptables有三個基本匹配條件:IP地址,協議protocol,端口port

    • IP地址匹配

      -s IP_address[/mask] [, IP_address[/mask], ...]  # 指定源IP地址,可以同時指定多個
      -d IP_address[/mask] [, IP_address[/mask], ...]  # 指定目的IP地址,可以同時指定多個
      
      • -s --source -d --destination
      • mask 即子網掩碼,可寫可不寫,可以寫出數字,也可以寫成掩碼,如24 = 255.255.255.0
      • 也可以指定一個IP範圍,如-m iprange --src-range 192.168.1.1-192.168.1.10表示匹配條件爲源IP地址在指定範圍內
    • 端口匹配

      -sport port_num     # 匹配源端口(source port)
      -dport port_num			# 匹配目的端口(destination port)
      
      • 也可以多端口匹配

        [dream@192 ~]$ iptables -m multiport --help
        multiport: Could not determine whether revision 1 is supported, assuming it is.
        iptables v1.4.21
        ...
        multiport match options:
        [!] --source-ports port[,port:port,port...]
         --sports ...
        				match source port(s)
        [!] --destination-ports port[,port:port,port...]
         --dports ...
        				match destination port(s)
        [!] --ports port[,port:port,port]
        				match both source and destination port(s)
        
      • 例子:

      [dream@192 ~]$ sudo iptables -A INPUT -p tcp -m multiport --dports 22,21,80 -j REJECT
      [sudo] dream 的密碼:
      [dream@192 ~]$ sudo iptables -L
      Chain INPUT (policy ACCEPT)
      target   prot   opt   source     destination         
      REJECT   tcp    --    anywhere   anywhere       multiport dports ssh,ftp,http reject-with icmp-port-unreachable
      

(5)iptables的目標動作

  • 當匹配到了條件後,就要執行特定的動作

  • 常用的動作如下

    • DROP 丟棄

    • REJECT 拒絕

    • ACCEPT 接收

    • SNAT 修改源IP地址,必須按如下格式書寫

      iptables -t nat -A POSTROUTING -s <old_IP> -j SNAT  <old_IP> to <new_IP> 
      
      • 理解:計算機只能修改已經路由後,準備發出去的數據包的源IP(如果修改目標IP,就找不到了)

        ​ 機理和網絡地址轉換協議NAT是一樣的

    • DNAT 修改源目標P地址,必須按如下格式書寫

      iptables -t nat -A PREROUTING -d <old_IP> -j DNAT  <old_IP> to <new_IP> 
      
      • 理解:計算機只能修改路由前,剛收到的數據包的目標IP,機理同NAT協議
  • 參考教程
    B站視頻:千峯

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章