Ubuntu 22.04
Sponsored Link

UFW : 基本操作2022/10/04

 
UFW (Uncomplicated FireWall) の基本操作です。
[1] UFW は nftables/iptables を設定するためのフロントエンドツールです。
Ubuntu 22.04 では、UFW のデフォルトバックエンドは nftables となっています。
root@dlp:~#
update-alternatives --config iptables

There are 2 choices for the alternative iptables (providing /usr/sbin/iptables).

  Selection    Path                       Priority   Status
------------------------------------------------------------
* 0            /usr/sbin/iptables-nft      20        auto mode
  1            /usr/sbin/iptables-legacy   10        manual mode
  2            /usr/sbin/iptables-nft      20        manual mode

Press <enter> to keep the current choice[*], or type selection number:
[2] UFW を利用するには、サービスを起動しておく必要があります。(デフォルトで自動起動状態)
さらに、サービスは起動していても、デフォルトでは無効化されているため、機能を利用するには有効化する必要があります。
root@dlp:~#
systemctl status ufw

*  ufw.service - Uncomplicated firewall
     Loaded: loaded (/lib/systemd/system/ufw.service; enabled; vendor preset: e>
     Active: active (exited) since Fri 2022-09-30 04:33:04 UTC; 59min ago
       Docs: man:ufw(8)
    Process: 548 ExecStart=/lib/ufw/ufw-init start quiet (code=exited, status=0>
   Main PID: 548 (code=exited, status=0/SUCCESS)
        CPU: 925us

# 現在の状態

root@dlp:~#
ufw status

Status: inactive
# 有効化

root@dlp:~#
ufw enable

Firewall is active and enabled on system startup
root@dlp:~#
ufw status

Status: active
# 無効化する場合は以下

root@dlp:~#
ufw disable

Firewall stopped and disabled on system startup
[3] UFW で サービス/ポート を許可する場合の基本操作です。
# デフォルトはインカミング通信全て拒否

root@dlp:~#
ufw status verbose

Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

# 例として SSH を許可

root@dlp:~#
ufw allow ssh

Rule added
Rule added (v6)

# 例として HTTP を許可

root@dlp:~#
ufw allow http

Rule added
Rule added (v6)

# 例として 2049/tcp を許可

root@dlp:~#
ufw allow 2049/tcp

Rule added
Rule added (v6)

root@dlp:~#
ufw status verbose

Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW IN    Anywhere
80/tcp                     ALLOW IN    Anywhere
2049/tcp                   ALLOW IN    Anywhere
22/tcp (v6)                ALLOW IN    Anywhere (v6)
80/tcp (v6)                ALLOW IN    Anywhere (v6)
2049/tcp (v6)              ALLOW IN    Anywhere (v6)

# * [ufw allow (service name)] では [/etc/services] で定義されたポートが許可される
[4] 追加したルールを削除する場合の基本操作です。
root@dlp:~#
ufw status verbose

Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW IN    Anywhere
80/tcp                     ALLOW IN    Anywhere
2049/tcp                   ALLOW IN    Anywhere
22/tcp (v6)                ALLOW IN    Anywhere (v6)
80/tcp (v6)                ALLOW IN    Anywhere (v6)
2049/tcp (v6)              ALLOW IN    Anywhere (v6)

# 例として SSH を削除

root@dlp:~#
ufw delete allow ssh

Rule deleted
Rule deleted (v6)

# 例として 80/tcp を削除

root@dlp:~#
ufw delete allow 80/tcp

Rule deleted
Rule deleted (v6)

# ルール番号付きで状態を表示

root@dlp:~#
ufw status numbered

Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 2049/tcp                   ALLOW IN    Anywhere
[ 2] 2049/tcp (v6)              ALLOW IN    Anywhere (v6)

# ルール番号を指定してルールを削除

root@dlp:~#
ufw delete 2

Deleting:
 allow 2049/tcp
Proceed with operation (y|n)? y
Rule deleted (v6)

# ルールを全て削除して初期化し、UFW を無効化する場合は以下

root@dlp:~#
ufw reset

Resetting all rules to installed defaults. Proceed with operation (y|n)? y
Backing up 'user.rules' to '/etc/ufw/user.rules.20221004_011751'
Backing up 'before.rules' to '/etc/ufw/before.rules.20221004_011751'
Backing up 'after.rules' to '/etc/ufw/after.rules.20221004_011751'
Backing up 'user6.rules' to '/etc/ufw/user6.rules.20221004_011751'
Backing up 'before6.rules' to '/etc/ufw/before6.rules.20221004_011751'
Backing up 'after6.rules' to '/etc/ufw/after6.rules.20221004_011751'

root@dlp:~#
ufw status

Status: inactive
[5] 接続回数の制限をかけたり、送信元や送信先を指定してサービスの許可をする場合の基本操作です。
# 例として [10.0.0.215] からの SSH を許可

root@dlp:~#
ufw allow from 10.0.0.215 to any port ssh

Rule added
# 例として [10.0.0.215] から [10.0.0.30] への [80/tcp] を許可

root@dlp:~#
ufw allow from 10.0.0.215 to 10.0.0.30 port 80 proto tcp

Rule added
# 例として [10.0.0.220] からの SSH を制限
# * 30 秒間に 6 回以上の連続 SSH は拒否される

root@dlp:~#
ufw limit from 10.0.0.220 to any port ssh

Rule added
root@dlp:~#
ufw status verbose

Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW IN    10.0.0.215
10.0.0.30 80/tcp           ALLOW IN    10.0.0.215
22/tcp                     LIMIT IN    10.0.0.220

# limit を設定した場合は以下のようなルールセットが登録される

root@dlp:~#
nft list ruleset | grep 'dport 22'

meta l4proto tcp ip saddr 10.0.0.220 tcp dport 22 ct state new # recent: SET name: DEFAULT side: source mask: 255.255.255.255 counter packets 0 bytes 0
meta l4proto tcp ip saddr 10.0.0.220 tcp dport 22 ct state new # recent: UPDATE seconds: 30 hit_count: 6 name: DEFAULT side: source mask: 255.255.255.255 counter packets 0 bytes 0 jump ufw-user-limit
meta l4proto tcp ip saddr 10.0.0.220 tcp dport 22 counter packets 0 bytes 0 jump ufw-user-limit-accept
[6] ICMP の 許可/拒否 設定を調整したい場合は、設定ファイルを編集します。
UFW デフォルトはインカミング通信全て拒否ですが、ICMP 関連はデフォルトで許可されています。
root@dlp:~#
vi /etc/ufw/before.rules
# 以下の設定により ICMP 関連は許可されている
# 拒否したい場合は下行を全てコメントにすれば OK
# ok icmp codes for INPUT
# -A ufw-before-input -p icmp --icmp-type destination-unreachable -j ACCEPT
# -A ufw-before-input -p icmp --icmp-type time-exceeded -j ACCEPT
# -A ufw-before-input -p icmp --icmp-type parameter-problem -j ACCEPT
# -A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT

# ok icmp code for FORWARD
# -A ufw-before-forward -p icmp --icmp-type destination-unreachable -j ACCEPT
# -A ufw-before-forward -p icmp --icmp-type time-exceeded -j ACCEPT
# -A ufw-before-forward -p icmp --icmp-type parameter-problem -j ACCEPT
# -A ufw-before-forward -p icmp --icmp-type echo-request -j ACCEPT

# 設定内容をリロード

root@dlp:~#
ufw reload

Firewall reloaded
# 特定の IP アドレス/ネットワーク からの [echo-request] を許可する場合は以下
# * 許可ホストからの Ping に応答する

root@dlp:~#
vi /etc/ufw/before.rules
# 例として [10.0.0.0/24] からの [echo-request] は許可
# ok icmp codes for INPUT
# -A ufw-before-input -p icmp --icmp-type destination-unreachable -j ACCEPT
# -A ufw-before-input -p icmp --icmp-type time-exceeded -j ACCEPT
# -A ufw-before-input -p icmp --icmp-type parameter-problem -j ACCEPT
# -A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT
-A ufw-before-input -p icmp --icmp-type echo-request -s 10.0.0.0/24 -j ACCEPT

# ok icmp code for FORWARD
# -A ufw-before-forward -p icmp --icmp-type destination-unreachable -j ACCEPT
# -A ufw-before-forward -p icmp --icmp-type time-exceeded -j ACCEPT
# -A ufw-before-forward -p icmp --icmp-type parameter-problem -j ACCEPT
# -A ufw-before-forward -p icmp --icmp-type echo-request -j ACCEPT

root@dlp:~#
ufw reload

Firewall reloaded
関連コンテンツ