返回文章列表

Nftables 防火牆設定與管理

本文介紹 nftables 的安裝、設定和管理方法,包含設定迴環流量、輸出與已建立的連線、預設拒絕防火牆政策,以及啟用 nftables 服務,確保系統安全。同時也提供 iptables 的設定方法和規則持久化組態,以及如何檢查輸入、轉發和輸出基礎鏈的設定。

系統管理 資安

現代 Linux 系統中,nftables 已成為管理防火牆的標準工具,取代了 iptables。本文說明如何設定 nftables,包含迴環流量、輸出和已建立連線的規則,以及設定預設拒絕策略和啟用 nftables 服務,確保系統安全。同時也提供 iptables 的設定方法和規則持久化組態,以及如何檢查輸入、轉發和輸出基礎鏈的設定,讓讀者能根據系統環境選擇合適的防火牆管理工具。

nftables 設定與管理

概述

nftables 是 Linux 核心中的一個封包過濾框架,提供了一個新的使用者空間命令列工具 nft,用於取代傳統的 iptables、ip6tables、ebtables 和 arptables。本文將介紹 nftables 的安裝、設定和管理方法。

3.6.2.1 安裝 nftables

適用性

  • Level 1 - Server
  • Level 1 - Workstation

描述

nftables 提供了一個新的核心內封包分類別框架,根據網路特定的虛擬機器(VM)和新的 nft 使用者空間命令列工具。它重用了現有的 Netfilter 子系統,如現有的 hook 基礎設施、連線追蹤系統、NAT、使用者空間佇列和日誌子系統。

原理

nftables 是 Linux 核心的一個子系統,可以保護主機免受來自企業網路內部的威脅,包括惡意的行動程式碼和組態不良的軟體。

稽核

執行以下命令以驗證 nftables 是否已安裝:

# dpkg-query -s nftables | grep 'Status: install ok installed'
Status: install ok installed

修正

執行以下命令以安裝 nftables:

# apt install nftables

3.6.2.2 停用 Uncomplicated Firewall (UFW)

適用性

  • Level 1 - Server
  • Level 1 - Workstation

描述

Uncomplicated Firewall (UFW) 是一個用於管理 netfilter 防火牆的程式,設計為易於使用。

原理

同時執行 nftables 和 ufw 可能會導致衝突和意外的結果。

稽核

執行以下命令以驗證 ufw 是否未安裝或未啟用:

# dpkg-query -s ufw
package 'ufw' is not installed and no information is available

# ufw status
Status: inactive

修正

執行以下命令以移除或停用 ufw:

# apt purge ufw
# ufw disable

3.6.2.3 清除 iptables 規則

適用性

  • Level 1 - Server
  • Level 1 - Workstation

描述

nftables 是 iptables、ip6tables、ebtables 和 arptables 的替代品。

原理

混合使用 iptables 和 nftables 可能會增加複雜性並引入錯誤。為了簡化操作,應清除所有 iptables 規則,並確保其未被載入。

稽核

執行以下命令以確保沒有 iptables 規則存在:

# iptables -L
# ip6tables -L

修正

執行以下命令以清除 iptables 規則:

# iptables -F
# ip6tables -F

3.6.2.4 建立 nftables 表

適用性

  • Level 1 - Server
  • Level 1 - Workstation

描述

表是鏈的容器,每個表只有一個位址族,並且只適用於該族的封包。

原理

nftables 預設沒有任何表。如果沒有建立表,nftables 將不會過濾網路流量。

稽核

執行以下命令以驗證是否存在 nftables 表:

# nft list tables
table inet filter

修正

執行以下命令以建立一個 nftables 表:

# nft create table inet filter

3.6.2.5 建立基本鏈

適用性

  • Level 1 - Server
  • Level 1 - Workstation

描述

鏈是規則的容器,分為基本鏈和常規鏈。基本鏈是封包從網路堆疊進入的入口點。

原理

如果沒有基本鏈存在,封包將不會被 nftables 處理。

稽核

執行以下命令以驗證是否存在基本鏈:

# nft list ruleset | grep 'hook input'
type filter hook input priority 0;

# nft list ruleset | grep 'hook forward'
type filter hook forward priority 0;

# nft list ruleset | grep 'hook output'
type filter hook output priority 0;

修正

執行以下命令以建立基本鏈:

# nft create chain inet filter input { type filter hook input priority 0 \; }
# nft create chain inet filter forward { type filter hook forward priority 0 \; }
# nft create chain inet filter output { type filter hook output priority 0 \; }

加強 nftables 設定以提升系統安全性

在現代 Linux 系統中,nftables 已成為預設的防火牆管理工具,取代了傳統的 iptables。正確組態 nftables 是確保系統安全性的關鍵步驟。本文將探討如何設定 nftables 以增強系統安全性,包括設定迴環流量、輸出與已建立的連線、預設拒絕防火牆政策,以及啟用 nftables 服務。

設定迴環流量

迴環流量(loopback traffic)是指在本機上不同行程之間產生的網路流量,通常對於系統的正常運作至關重要。正確設定迴環流量的規則,可以防止外部來源偽造迴環流量,進而增強系統安全性。

檢查迴環流量設定

首先,我們需要檢查目前的 nftables 設定中,是否正確組態了迴環介面的規則。可以使用以下命令:

# nft list ruleset | awk '/hook input/,/}/' | grep 'iif "lo" accept'
iif "lo" accept
# nft list ruleset | awk '/hook input/,/}/' | grep 'ip saddr'
ip saddr 127.0.0.0/8 counter packets 0 bytes 0 drop

若系統啟用了 IPv6,則還需檢查 IPv6 的迴環介面設定:

# nft list ruleset | awk '/hook input/,/}/' | grep 'ip6 saddr'
ip6 saddr ::1 counter packets 0 bytes 0 drop

設定迴環流量規則

若檢查結果不符合預期,可以使用以下命令來設定迴環流量的規則:

# nft add rule inet filter input iif lo accept
# nft add rule inet filter input ip saddr 127.0.0.0/8 counter drop

對於啟用了 IPv6 的系統,還需執行:

# nft add rule inet filter input ip6 saddr ::1 counter drop

內容解密:

  1. nft add rule inet filter input iif lo accept:此命令在 nftablesinput 鏈中新增一條規則,允許所有來自迴環介面(lo)的流量透過。
  2. nft add rule inet filter input ip saddr 127.0.0.0/8 counter drop:此命令新增一條規則,拒絕所有來源 IP 位址在 127.0.0.0/8 網段的流量,並進行計數統計。
  3. nft add rule inet filter input ip6 saddr ::1 counter drop(IPv6 系統):此命令拒絕所有來源 IP 位址為 ::1 的 IPv6 流量,同樣進行計數統計。

設定輸出與已建立的連線

正確設定輸出與已建立的連線,可以確保系統只允許必要的網路流量透過,從而降低安全風險。

檢查輸出與已建立的連線設定

檢查目前的 nftables 設定中,對於輸出與已建立連線的規則,可以使用以下命令:

# nft list ruleset | awk '/hook input/,/}/' | grep -E 'ip protocol (tcp|udp|icmp) ct state'
# nft list ruleset | awk '/hook output/,/}/' | grep -E 'ip protocol (tcp|udp|icmp) ct state'

設定輸出與已建立的連線規則

若檢查結果不符合預期,可以使用以下命令來設定:

# nft add rule inet filter input ip protocol tcp ct state established accept
# nft add rule inet filter input ip protocol udp ct state established accept
# nft add rule inet filter input ip protocol icmp ct state established accept
# nft add rule inet filter output ip protocol tcp ct state new,related,established accept
# nft add rule inet filter output ip protocol udp ct state new,related,established accept
# nft add rule inet filter output ip protocol icmp ct state new,related,established accept

內容解密:

  1. nft add rule inet filter input ip protocol tcp ct state established accept:允許所有已經建立的 TCP 連線的輸入流量。
  2. nft add rule inet filter output ip protocol tcp ct state new,related,established accept:允許所有新的、相關的或已建立的 TCP 連線的輸出流量。
  3. 對 UDP 和 ICMP 協定的處理邏輯相同,分別允許已建立的連線輸入流量和新的、相關的或已建立的連線輸出流量。

設定預設拒絕防火牆政策

預設拒絕所有流量,只允許必要的服務和連線,是增強系統安全性的有效手段。

檢查預設政策

檢查目前的 nftables 設定中,對於輸入、轉發和輸出的預設政策,可以使用以下命令:

# nft list ruleset | grep 'hook input'
# nft list ruleset | grep 'hook forward'
# nft list ruleset | grep 'hook output'

設定預設拒絕政策

若檢查結果不符合預期,可以使用以下命令來設定預設拒絕政策:

# nft chain inet filter input { policy drop \; }
# nft chain inet filter forward { policy drop \; }
# nft chain inet filter output { policy drop \; }

內容解密:

  1. nft chain inet filter input { policy drop \; }:將 input 鏈的預設政策設定為丟棄所有不符合規則的輸入流量。
  2. 同理,對 forwardoutput 鏈也設定預設丟棄政策。

啟用 nftables 服務

為了確保 nftables 的規則在系統重啟後依然生效,需要啟用 nftables 服務。

檢查 nftables 服務狀態

檢查 nftables 服務是否已啟用,可以使用以下命令:

# systemctl is-enabled nftables
enabled

啟用 nftables 服務

若服務未啟用,可以使用以下命令來啟用它:

# systemctl enable nftables

內容解密:

  1. systemctl is-enabled nftables:檢查 nftables 服務是否已設定為開機自動啟動。
  2. systemctl enable nftables:將 nftables 服務設定為開機自動啟動,以確保防火牆規則在系統重啟後依然生效。

設定與管理主機防火牆

在現代網路安全中,正確組態主機防火牆是保護系統免受未授權存取的關鍵步驟。Linux 系統提供了多種工具來實作這一目標,包括 nftablesiptables。本篇文章將探討如何設定和管理這些工具,以確保系統安全。

使用 nftables 設定防火牆

nftables 是 Linux 核心中的一個子系統,用於過濾和分類別網路封包。它提供了比 iptables 更靈活和更高效的規則設定方式。

確保 nftables 規則持久化

為了使 nftables 規則在系統重啟後仍然有效,需要確保規則被正確地載入。/etc/nftables.conf 檔案是用於定義 nftables 規則的主要設定檔。

設定 nftables 規則

編輯 /etc/nftables.conf 檔案,並加入或取消註解以下行,以包含所需的 nftables 規則檔案:

include "/etc/nftables.rules"

這個設定確保了在系統啟動時,/etc/nftables.rules 中的規則被載入。

檢查輸入、轉發和輸出基礎鏈

使用以下命令檢查輸入基礎鏈的設定:

awk '/hook input/,/}/' $(awk '$1 ~ /^\s*include/ { gsub("\"","",$2);print $2 }' /etc/nftables.conf)

預期的輸出應該類別似於:

type filter hook input priority 0; policy drop;
# 確保回環流量被正確組態
iif "lo" accept
ip saddr 127.0.0.0/8 counter packets 0 bytes 0 drop
ip6 saddr ::1 counter packets 0 bytes 0 drop
# 確保已建立的連線被允許
ip protocol tcp ct state established accept
ip protocol udp ct state established accept
ip protocol icmp ct state established accept
# 允許來自任意位置的 SSH(埠22)流量
tcp dport ssh accept
# 允許 ICMP 和 IGMP 流量
icmpv6 type { destination-unreachable, packet-too-big, time-exceeded, parameter-problem, mld-listener-query, mld-listener-report, mld-listener-done, nd-router-solicit, nd-router-advert, nd-neighbor-solicit, nd-neighbor-advert, ind-neighbor-solicit, ind-neighbor-advert, mld2-listener-report } accept

同樣地,可以使用類別似的命令檢查轉發和輸出基礎鏈的設定。

程式碼解析

awk '/hook input/,/}/' $(awk '$1 ~ /^\s*include/ { gsub("\"","",$2);print $2 }' /etc/nftables.conf)

內容解密:

  1. 內部 awk 命令awk '$1 ~ /^\s*include/ { gsub("\"","",$2);print $2 }' /etc/nftables.conf

    • 作用:解析 /etc/nftables.conf 檔案,找出所有以 include 開頭的行,並提取被包含的檔案路徑。
    • 邏輯:使用正規表示式匹配 include,去除引號後輸出檔案路徑。
  2. 外部 awk 命令awk '/hook input/,/}/'

    • 作用:讀取內部命令輸出的檔案內容,提取從 hook input 到第一個 } 之間的內容。
    • 邏輯:用於檢查輸入鏈的設定。
  3. 組合邏輯:整個命令用於檢查 /etc/nftables.conf 中包含的規則檔案中的輸入鏈設定。

使用 iptables 設定防火牆

如果您的環境中已經使用了 nftables 或其他防火牆工具(如 UFW),則可以跳過本文關於 iptables 的設定。

安裝 iptables 相關套件

執行以下命令安裝 iptablesiptables-persistent

apt install iptables iptables-persistent

這兩個套件分別提供了 iptables 命令列工具和使 iptables 規則持久化的功能。

移除 nftables(如果已安裝)

如果系統中同時安裝了 nftablesiptables,可能會導致衝突。因此,建議移除 nftables

apt purge nftables

程式碼解析

type filter hook input priority 0; policy drop;
iif "lo" accept
ip saddr 127.0.0.8 counter packets 0 bytes 0 drop

內容解密:

  1. type filter hook input priority 0; policy drop;

    • 作用:定義輸入鏈的基本策略為丟棄所有封包。
    • 邏輯:這是預設的安全策略,只有明確允許的流量才會被接受。
  2. iif "lo" accept

    • 作用:允許回環介面(localhost)的所有流量。
    • 邏輯:回環介面用於本地通訊,允許其流量是必要的。
  3. ip saddr 127.0.0.8 counter packets 0 bytes 0 drop

    • 作用:丟棄來源 IP 為 127.0.0.8 的封包。
    • 邏輯:這個規則可能用於特定的安全需求,但通常不應出現在標準組態中。