Uncomplicated Firewall

出自 Arch Linux 中文维基

本文或本節需要翻譯。要貢獻翻譯,請訪問簡體中文翻譯團隊

附註: 請完成翻譯。(在 Talk:Uncomplicated Firewall# 中討論)

來自項目主頁:

Ufw(即Uncomplicated Firewall的縮寫),是一個管理網絡防火牆的程序。它提供了一個命令行界面,並旨在簡單(uncomplicated)易用。
注意: 注意UFW可以使用iptablesnftables作為防火牆後端。此外由於nft接受iptables語法(例如在/etc/ufw/before.rules中),因此使用UFW管理規則的用戶無需學習怎麼對iptables或者nftables進行底層調用。

安裝[編輯 | 編輯原始碼]

安裝軟體包ufw

啟動啟用ufw.service使其開機自啟。注意,如果iptables.service(或者其ipv6的部分)也被啟用了,ufw.service將不會生效。

基本配置[編輯 | 編輯原始碼]

以下是一個非常簡單的配置示例,它將會默認拒絕所有連接,允許來自192.168.0.1-192.168.0.255區域網內的所有連接,允許所有目標為Deluge(一個BitTorrent客戶端)的連接,並啟用了來自任何地方的SSH連接數量限制:

# ufw default deny
# ufw allow from 192.168.0.0/24
# ufw allow Deluge
# ufw limit ssh

僅當第一次使用UFW時需要:

# ufw enable
注意: 請確保ufw.service已經被啟用

最後,使用以下命令行確認更改已被應用:

# ufw status
Status: active
To                         Action      From
--                         ------      ----
Anywhere                   ALLOW       192.168.0.0/24
Deluge                     ALLOW       Anywhere
SSH                        LIMIT       Anywhere
注意: 狀態報告僅限於用戶添加的規則。在大多數情況下不會有什麼問題,但最好還是了解一下內置規則是否存在。這些規則包括允許 UPNP AVAHI 和 DHCP 回復的過濾器。

使用以下命令來查看額外信息(包括默認策略):

# ufw status verbose

但這依然限制於用戶指定的規則。為了查看所有設置的規則,可以使用:

# ufw show raw 

或者手冊中列出的其他報告。由於這些報告也總結了流量情況,其輸出有些難以閱讀,為了方便閱讀,你可以使用另一種檢查接受流量的方法:

# iptables -S | grep ACCEPT

請注意,只要你還在使用ufw來管理iptables,就不要將後者啟用

注意: 如果你在/etc/sysctl.d/*中設置了特殊的網絡環境變量,請相應更新/etc/ufw/sysctl.conf中的內容(因為此配置會覆蓋默認配置)。

轉發策略[編輯 | 編輯原始碼]

如果你需要使用 VPN (例如OpenVPNWireGuard),將在/etc/default/ufw中的DEFAULT_FORWARD_POLICY變量從"DROP"調整到"ACCEPT"以便無論用戶如何設置如何都能轉發所有數據包。要針對特定接口(例如 wg0)進行轉發,用戶可以在*filter塊中添加以下行:

/etc/ufw/before.rules
# End required lines 

-A ufw-before-forward -i wg0 -j ACCEPT
-A ufw-before-forward -o wg0 -j ACCEPT

你可能還需要取消以下行的注釋:

/etc/ufw/sysctl.conf
net/ipv4/ip_forward=1
net/ipv6/conf/default/forwarding=1
net/ipv6/conf/all/forwarding=1

Adding other applications[編輯 | 編輯原始碼]

The PKG comes with some defaults based on the default ports of many common daemons and programs. Inspect the options by looking in the /etc/ufw/applications.d directory or by listing them in the program itself:

# ufw app list

If users are running any of the applications on a non-standard port, it is recommended to simply make /etc/ufw/applications.d/custom containing the needed data using the defaults as a guide.

警告: If users modify any of the PKG provided rule sets, these will be overwritten the first time the ufw package is updated. This is why custom app definitions need to reside in a non-PKG file as recommended above!

Example, deluge with custom tcp ports that range from 20202-20205:

[Deluge-my]
title=Deluge
description=Deluge BitTorrent client
ports=20202:20205/tcp

Should you require to define both tcp and udp ports for the same application, simply separate them with a pipe as shown: this app opens tcp ports 10000-10002 and udp port 10003:

ports=10000:10002/tcp|10003/udp

One can also use a comma to define ports if a range is not desired. This example opens tcp ports 10000-10002 (inclusive) and udp ports 10003 and 10009

ports=10000:10002/tcp|10003,10009/udp

Deleting applications[編輯 | 編輯原始碼]

Drawing on the Deluge/Deluge-my example above, the following will remove the standard Deluge rules and replace them with the Deluge-my rules from the above example:

# ufw delete allow Deluge
# ufw allow Deluge-my

Query the result via the status command:

# ufw status
Status: active
To                         Action      From
--                         ------      ----
Anywhere                   ALLOW       192.168.0.0/24
SSH                        ALLOW       Anywhere
Deluge-my                  ALLOW       Anywhere

Black listing IP addresses[編輯 | 編輯原始碼]

It might be desirable to add ip addresses to a blacklist which is easily achieved simply by editing /etc/ufw/before.rules and inserting an iptables DROP line at the bottom of the file right above the "COMMIT" word.

/etc/ufw/before.rules
...
## blacklist section
# block just 199.115.117.99
-A ufw-before-input -s 199.115.117.99 -j DROP
# block 184.105.*.*
-A ufw-before-input -s 184.105.0.0/16 -j DROP

# don't delete the 'COMMIT' line or these rules won't be processed
COMMIT

Rate limiting with ufw[編輯 | 編輯原始碼]

ufw has the ability to deny connections from an IP address that has attempted to initiate 6 or more connections in the last 30 seconds. Users should consider using this option for services such as SSH.

Using the above basic configuration, to enable rate limiting we would simply replace the allow parameter with the limit parameter. The new rule will then replace the previous.

# ufw limit SSH
Rule updated
# ufw status
Status: active
To                         Action      From
--                         ------      ----
Anywhere                   ALLOW       192.168.0.0/24
SSH                        LIMIT       Anywhere
Deluge-my                  ALLOW       Anywhere

用戶規則[編輯 | 編輯原始碼]

所有的用戶規則都儲存在 etc/ufw/user.rulesetc/ufw/user6.rules 中,分別用於 IPv4 與 IPv6。

技巧和竅門[編輯 | 編輯原始碼]

禁用遠程ping[編輯 | 編輯原始碼]

在以下文件中將 ACCEPT 改為 DROP :

/etc/ufw/before.rules
# ok icmp codes
...
-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT

如果你正在使用 IPv6, 請一同更改 /etc/ufw/before6.rules 中的規則。

關閉UFW日誌[編輯 | 編輯原始碼]

關閉UFW日誌可以讓UFW停止填充內核(dmesg)和消息日誌:

# ufw logging off

UFW 與 Docker[編輯 | 編輯原始碼]

Docke在默認狀態下會編寫iptables規則並忽視ufw規則,這可能會造成一些安全隱患。一種解決方案是使用ufw-docker腳本

提示:你可以從AUR中安裝ufw-dockerAUR來使用。運行ufw-docker install來自動修復iptables規則。這個腳本也能幫你管理Docker相關的ufw規則,詳細參見ufw-docker help

GUI前端[編輯 | 編輯原始碼]

如果你使用KDE,你可以直接在系統設置 > 防火牆 對ufw進行管理。

Gufw[編輯 | 編輯原始碼]

gufw 是一個為 Ufw 提供的 GTK 前端,旨在使 Linux 防火牆的管理儘可能簡單易用。它具有常見埠和 p2p 應用程式的預設設置。其依賴於軟體包python, ufw,以及GTK支持。

See also[編輯 | 編輯原始碼]