概要
- nftables は従来の用途ごとに分かれていたフィルタリングツール(iptables, ip6tables, arptables, ebtables)を統合して管理しやすくしたもの。
- nftables のコマンドが nft になる。
- 今後はnftが標準になっていくと思われるので新規で覚えるならnftを覚えればいいと思う。
文法
iptables [-t テーブル] -A チェイン ルール
iptables [-t テーブル] -D チェイン ルール
iptables [-t テーブル] -P チェイン ターゲット
iptables [-t テーブル] -L [チェイン]
iptables [-t テーブル] -F [チェイン]
iptables [-t テーブル] -X [チェイン]
iptables [-t テーブル] -N [チェイン]
iptables -Z
テーブル
テーブル |
利用可能なチェイン |
説明 |
raw |
PREROUTING, OUTPUT |
接続追跡の対象から外れるようにパケットを設定する用 |
filter |
INPUT, OUTPUT, FORWARD |
パケットフィルタリング用(デフォルト) |
nat |
PREROUTING, OUTPUT, POSTROUTING |
ネットワークアドレス変換(NAT)用 |
mangle |
all |
IPヘッダ書き換え用 |
security |
INPUT, OUTPUT, FORWARD |
強制アクセス変換用(SELinux, MAC関連) |
- テーブル指定はどれを使ってもFirewall自体の処理に違いは無い
つまり、チェインとしてOUTPUTを使いたい場合に、上記のどれを使っても違いがない
- 基本的にはfilter以外はあまり使わないみたい
チェイン
チェイン |
説明 |
PREROUTING |
受信時(ルーティングの前)に実行, 宛先IP/portを変換 |
INPUT |
受信時(ローカルプロセスに入る前)に実行, パケットの許可/不許可 |
FORWARD |
転送するパケットの許可/不許可 |
OUTPUT |
送信時(ローカルプロセスから出た後)に実行, パケットの許可/不許可 |
POSTROUTING |
送信時(ルーティングの後)に実行, 送信元IP/portを変換 |
ターゲット
ターゲット |
説明 |
ACCEPT |
許可 |
DROP |
破棄(相手への通知無し) |
REJECT |
拒否(相手への通知あり) |
MASQUERADE |
IPマスカレード, アドレス変換を動的に行う |
SNAT |
("S"tatic) 送信元アドレスを変換する, 固定IPで利用 |
DNAT |
("D"ynamic) 送信先アドレスを変換する。動的IPで利用 |
REDIRECT |
送信先アドレスを変換する |
LOW |
マッチしたパケットをログに記録 |
コマンドオプション
Option |
説明 |
-s/--source |
送信元のアドレスを指定 |
-d/--destination |
送信先のアドレスを指定 |
--sport |
(TCP, UDP拡張) 送信元のport番号の指定 |
--dport |
(TCP, UDP拡張) 送信先のport番号の指定 |
-p/--protocol |
PROTの指定 |
-i/--in-interface |
受信するインターフェースの指定 |
-o/--out-interface |
送信するインターフェースの指定 |
-j/--jump |
適用するターゲットの指定 |
-L/--list |
ルールの一覧表示 |
-I/--insert [RULE番号] |
チェインにルールを挿入 |
実行例
sudo iptables -L -t filter
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
sudo iptables -t filter -A INPUT -p tcp --dport 22 -j REJECT
sudo iptables -t filter -A INPUT -p tcp --dport 25 -j DROP
sudo iptables -t filter -A INPUT -p tcp --dport 80
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable
DROP tcp -- anywhere anywhere tcp dpt:smtp
tcp -- anywhere anywhere tcp dpt:http
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
sudo iptables -t filter -D INPUT -p tcp --dport 22 -j REJECT
sudo iptables -t filter -D INPUT -p tcp --dport 25 -j DROP
sudo iptables -t filter -D INPUT -p tcp --dport 80
sudo iptables -t filter -F
使い方(nft)
はじめに
- nftはiptablesより自由度が高くなっているので、以下の順序でルールを追加することになる
- テーブルの追加
- チェインの追加
- ルールの追加
文法
nft list ruleset
nft add table [ファミリー] テーブル
nft add chain [ファミリー] テーブル チェイン { ベースチェイン }
nft add rule [ファミリー] テーブル チェイン 条件
nft delete table テーブル
nft delete chain テーブル チェイン
nft flush table [ファミリー] テーブル
nft --handle --numeric list chain テーブル チェイン
nft delete rule テーブル チェイン handle 番号
nft flush chain テーブル チェイン
テーブル/ファミリー
- iptablesと違い、nftには基本的にデフォルトでテーブルは作成されていない
- テーブルの作成時に、ファミリー名を指定可能である
- ファミリー名は省略可能で、省略した場合は"ip"(= iptables)が使われる
- ファミリー名はiptables系ユーティリティーの区分みたいなもので以下の対応が有る
チェイン
- こちらもiptablesと違い、デフォルトのチェインは無いので自分で作成する必要が有る。
ベースチェインとレギュラーチェインがある
- ベースチェインはNetfilter(nft本体)が呼び出す
- レギュラーチェインは他のチェインから呼び出される
ベースチェインの書式、及びパラメータ
nft add chain ファミリー テーブル チェイン { type タイプ hook フック [device デバイス] priority 優先値; [policy ポリシー;] }
条件 (statements)
その他
- Sets, Maps, Elements ... といろいろ有るが難しいので飛ばす
実行例
sudo nft list rulesets
table inet filter {
chain input {
type filter hook input priority 0; policy accept;
}
chain forward {
type filter hook forward priority 0; policy accept;
}
chain output {
type filter hook output priority 0; policy accept;
}
}
sudo nft add table sample
table ip sample {
}
sudo nft add chain sample c1
table ip sample {
chain c1 {
}
}
sudo nft add chain sample c2 { type filter hook input priority 0 \; policy accept \; }
table ip sample {
chain c1 {
}
chain c2 {
type filter hook input priority 0; policy accept;
}
}
sudo nft add rule sample c1 tcp dport 22 reject
sudo nft add chain sample c3 { type filter hook input priority 0 \; policy accept \; }
sudo nft add rule sample c3 tcp dport 22 reject
table ip sample {
chain c1 {
tcp dport ssh reject
}
chain c2 {
type filter hook input priority 0; policy accept;
}
chain c3 {
type filter hook input priority 0; policy accept;
tcp dport ssh reject
}
}
sudo nft --handle --numeric list chain sample c3
table ip sample {
chain c3 {
type filter hook input priority 0; policy accept;
tcp dport ssh reject
}
}
sudo nft delete rule sample c3 handle 11
sudo nft flush chain sample c3
table ip sample {
chain c1 {
tcp dport ssh reject
}
chain c2 {
type filter hook input priority 0; policy accept;
}
chain c3 {
type filter hook input priority 0; policy accept;
}
}
sudo nft delete chain sample c3
table ip sample {
chain c1 {
tcp dport ssh reject
}
chain c2 {
type filter hook input priority 0; policy accept;
}
}
sudo nft delete table sample
参考サイト