テックブログ

nftables をいい感じに使ってみる

こんにちは amです。
昨今では OS 上のfirewall 機能を使わずとも、クラウド側の機能で簡単にアクセス制限が実現可能なことが多くなってきました。

とは言ってもまだまだ使うこともあり、RHEL9系のディストリビューションでは標準になると思われる nftables で実際に利用した方法を2パターンほど載せたいと思います。

1. コマンド直書き

そのまま設定ファイルにコマンド直書きしているような記載方法です。
数が多くないときは比較的見やすいです。iptablesの互換機能を用いて生成しているため移設が(個人的には)簡単
iptables からの変換については rhel のサイトを参照
https://docs.redhat.com/ja/documentation/red_hat_enterprise_linux/8/html/securing_networks/getting-started-with-nftables_securing-networks#when-to-use-firewalld-nftables-or-iptables_assembly_migrating-from-iptables-to-nftables

・/etc/sysconfig/nftables.conf
このファイルにinclude したいファイルのパスを記述する

# Uncomment the include statement here to load the default config sample
# in /etc/nftables for nftables service.

#include "/etc/nftables/main.nft"

# To customize, either edit the samples in /etc/nftables, append further
# commands to the end of this file or overwrite it after first service
# start by calling: 'nft list ruleset >/etc/sysconfig/nftables.conf'.
include "/etc/nftables/netassist.nft"


・/etc/nftables/netassist.nft のサンプル

add table ip filter
add chain ip filter INPUT { type filter hook input priority 0; policy drop; }
add chain ip filter FORWARD { type filter hook forward priority 0; policy accept; }
add chain ip filter OUTPUT { type filter hook output priority 0; policy accept; }
add chain ip filter Block
add chain ip filter AllowNetwork
add chain ip filter NetAssist
add chain ip filter Service
add chain ip filter Local
add rule ip filter INPUT ct state related,established  counter accept
add rule ip filter INPUT counter jump Block
add rule ip filter INPUT counter jump Local
add rule ip filter INPUT counter jump AllowNetwork
add rule ip filter INPUT counter jump NetAssist
add rule ip filter INPUT counter jump Service
#add rule ip filter INPUT ct state new limit rate 1/second burst 5 packets counter log prefix "[NFTABLES INPUT] : "
add rule ip filter INPUT ct state new counter log prefix "[NFTABLES INPUT] : "
add rule ip filter Local iifname "lo" counter accept
add rule ip filter OUTPUT ct state new counter log prefix "[NFTABLES OUTPUT] : "


# localnetwork
add rule ip filter Local ip saddr 192.168.0.0/24 counter accept

# Allow Network
add rule ip filter AllowNetwork ip saddr 123.123.123.123  counter accept

# NetAssist Network
add rule ip filter NetAssist ip saddr 11.11.11.11/32 counter accept


# AllowService
add rule ip filter Service ip protocol icmp counter accept
add rule ip filter Service ct state new  tcp dport 80 counter accept
add rule ip filter Service ct state new  tcp dport 443 counter accept

Chain ごとにリストファイルを作ってそれを読み込む

・/etc/nftables/netassist.nft のサンプル

#!/usr/sbin/nft -f

# drop any existing nftables ruleset
flush ruleset

include "/etc/nftables/allow_ip_list"
include "/etc/nftables/netassist_ip_list"
include "/etc/nftables/service_port_list"

table inet filter {
  set allow_ip_list {
           type ipv4_addr; flags interval;
           elements = {
                  $allow_ip_list
           }
        }
 set netassist_ip_list {
           type ipv4_addr; flags interval;
           elements = {
                  $netassist_ip_list
           }
        }
  set service_port_list {
           type inet_service; flags interval;
           elements = {
                  $service_port_list
           }
        }

chain INPUT {
      type filter hook input priority 0; policy drop;

      # allow established/related connections
      ct state { established, related } accept

      # early drop of invalid connections
      ct state invalid log prefix "[nftables-drop]: " counter drop

      counter jump Block
      counter jump Local
      counter jump AllowNetwork
      counter jump NetAssist
      counter jump Service

      }

      chain Block {
      }

      chain AllowNetwork {
      ip saddr @allow_ip_list counter accept
      }

      chain NetAssist {
      ip saddr @netassist_ip_list counter accept
      }

      chain Service {
      tcp dport @service_port_list accept
      }

      chain Local {
      iifname "lo" accept
     }

}

netassist_ip_list のサンプル

# cat /etc/nftables/netassist_ip_list
define netassist_ip_list = {
111.111.11.111/32,
222.222.22.222/32,
12.12.12.12/32,
}

このような形で縦につないでいけます。最初に作る設定ファイルの行数は増えるものの、後の管理を考えると用途ごとにリスト化したほうが楽かなと思っています。
今後もう少し楽な記述が思いついたり見つかったりするかもしれませんが、そのときまたご紹介できればと思います。

読んでいただきありがとうございました。

この記事をシェアする

  • facebook
  • twitter
  • hatena
  • line
URLとタイトルをコピーする

実績数30,000件!
サーバーやネットワークなど
ITインフラのことならネットアシストへ、
お気軽にご相談ください