Linux-nftables
https://netfilter.org/
https://netfilter.org/projects/iptables/index.html
https://netfilter.org/projects/nftables/index.html
https://www.netfilter.org/projects/nftables/manpage.html
https://wiki.nftables.org/wiki-nftables/index.php/Main_Page
https://wiki.nftables.org/wiki-nftables/index.php/Quick_reference-nftables_in_10_minutes
https://wiki.archlinux.org/index.php/Nftables
https://wiki.gentoo.org/wiki/Nftables
https://wiki.gentoo.org/wiki/Nftables/Examples
https://kernelnewbies.org/nftables_examples
1. Linux防火牆簡介
2. nftables介紹
3. 為什么要使用nftables
4. Netfilter日志框架
5. nft命令介紹
6. nft基礎操作
7. 更多規則選項
1. Linux防火牆簡介
Linux系統較早使用的防火牆是(約1996年的Linux 2.0) ipfwadm 和(約1999年的Linux 2.2) ipchains。
基於以上版本經過重新設計和大幅改進后出現了(約2001年的Linux 2.4)的netfilter。一直應用到現在。
- 包過濾框架:netfilter (約2001年Linux 2.4) 該框架內的軟件支持包過濾,網絡地址[和端口]轉換(NA[P]T)和其他數據包重整。是Linux內核中的一組鈎子,允許內核模塊向網絡堆棧注冊回調函數。然后,對於遍歷網絡堆棧內的相應掛鈎的每個數據包,回調注冊的回調函數。
- 用戶工具集:iptables, nftables等
- iptables (約2001年Linux 2.4) 是用戶空間命令行程序,用於配置netfilter包過濾規則集。
- nftables (約2013年Linux 3.13) 旨在取代現有iptables的{ip,ip6,arp,eb}表框架。(iptables,ip6tables,arptables,etables用於以太網橋過濾的功能。)
補充:firewalld也是Linux的防火牆,同時支持iptables和nftables,最新版本默認使用nftables。簡單的說firewalld是基於nftfilter防火牆的用戶界面工具。而iptables和nftables是命令行工具。firewalld引入區域的概念,可以動態配置,讓防火牆配置及使用變得簡便。詳見firewalld.org
netfilter, iptables, nftables 都是netfilter項目。nftables 用於替代 iptables。
netfilter的功能很強大,遠遠超越防火牆的范圍,詳見項目主頁。
2. nftables介紹
本文主要記錄較新的nftables的特性及nft工具的使用。
nftables 提供了一個新的包過濾框架,該框架基於特定於網絡的虛擬機(VM),一個新的用戶空間實用程序(nft)和一個用於{ip,ip6}表的兼容層。
它使用現有的鈎子,連接跟蹤系統,用戶空間排隊組件和netfilter的日志子系統。
它由三個主要組件組成:內核實現,libnl netlink通信和nftables用戶空間前端。
內核提供了netlink配置接口,以及運行時規則集評估,
libnl包含用於與內核通信的低級函數,
而nftables前端是用戶通過nft進行交互的內容。
從Linux內核3.13版本開始(2013),nftables已經成為Linux內核主線的一部分。正在逐漸替換iptables。
3. 為什么要使用nftables
https://developers.redhat.com/blog/2016/10/28/what-comes-after-iptables-its-successor-of-course-nftables/
nftables對舊iptables提供的最顯着的功能有:
支持查找表 - 無需進行線性規則評估
不再強制執行隱式規則計數器和地址/接口匹配的開銷
擁有使用額外腳本的能力。
nftables不包含任何內置表。
可用性:
事務規則更新 - 所有規則都以原子方式應用
應用程序可以訂閱nfnetlink通知,以便在添加或刪除新規則時接收規則更新
nft命令行工具可以顯示正在匹配的規則的實時日志,以便於規則集調試
nftables重用了netfilter框架的許多部分,例如連接跟蹤和NAT設施。它還保留了命名法和基本iptables設計的幾個部分,例如表格,鏈條和規則。
就像使用iptables一樣,表充當鏈的容器,鏈包含單獨的規則,可以執行諸如丟棄數據包,移動到下一個規則或跳轉到新鏈的操作。
什么被替換?
從用戶的角度來看,nftables添加了一個名為nft的新工具,它取代了iptables,arptables和ebtables中的所有其他工具。從架構的角度來看,它還取代了處理包過濾規則集的運行時評估的內核部分。
允許監控規則更新。nft工具可用於監視規則集中的更改,例如“nft monitor”顯示從內核添加和刪除的每個規則。
它還提供“跟蹤模式”,系統管理員可以在此處執行“添加規則修改” prerouting ip saddr 10.2.3.4 meta nftrace set 1'。
然后,“nft monitor trace”將顯示與來自IP地址10.2.3.4的數據包匹配的每個規則。
4. Netfilter日志框架
Nftables 7 : Nftables Logging
http://computer-outlines.over-blog.com/article-nftables-7-nftables-logging-123303629.html
/proc/net/netfilter/nf_log
從Linux 2.6.14開始,可以通過內核數據包過濾器記錄的用戶空間數據包進行傳遞。
有兩個工具:
QUEUE et NFQUEUE:外部應用程序用於決策(SNORT IDS / IPS,NuFW,ulogd,...)
LOG et NFLOG:用於外部日志記錄(Wireshark,Syslog,...)
NFlog是一個新的iptables和Nftables的目標是通過虛擬設備記錄數據包。
NFLOG目標會復制數據包並將其發送到指定的netlink套接字。
$ sudo cat /proc/net/netfilter/nf_log
0 NONE ()
1 NONE ()
2 NONE ()// IPv4
3 NONE ()
4 NONE ()// Novell IPX
5 NONE ()
6 NONE ()
7 NONE ()
8 NONE ()
9 NONE ()
10 NONE ()// IPv6
11 NONE ()
12 NONE ()
// 行號是協議。
比如啟用IPv6 nfnetlink_log:
echo "nfnetlink_log" > /proc/sys/net/netfilter/nf_log/10
$ ls /proc/net/netfilter/nf_log
/proc/net/netfilter/nf_log
$ ls /proc/sys/net/netfilter/nf_log
0 1 10 11 12 2 3 4 5 6 7 8 9
$ sudo cat /proc/net/netfilter/nf_log
...
10 nfnetlink_log(nfnetlink_log)
5. nft命令介紹
不同與iptables,nftables不包含任何內置表。還擁有使用額外腳本的能力, 擁有一些高級的類似編程語言的能力,例如定義變量和包含外部文件。
與iptables類似的是,表層次結構:表包含鏈,鏈里是具體的過濾規則。處理規則則由 地址,接口,端口或包含當前處理數據包中的其他數據等表達式以及諸如drop, queue, continue等聲明組成。
可以用於多種地址族的過濾和處理:
- ip: IPv4 地址
- ip6: IPv6 地址
- inet: IPv4 和 IPv6 地址
- arp: 地址解析協議(ARP)地址
- bridge: 處理橋接數據包
注:當沒有指定地址族時,默認為IP。
特定的地址族包含鈎子,對ip,ipv6,和inet地址族來說,可以應用如下鈎子
- prerouting: 剛到達並且未被處理過的數據包
- input: 已經被接收並且已經經過prerouting鈎子的傳入數據包
- forward: 如果數據包將被發送到另一個設備,他將會通過forward鈎子
- output: 從本地系統傳出的數據包
- postrouting: 僅僅在離開系統之前
ARP地址族只能使用input和output鈎子。
$ nft -h
-h, --help 顯示幫書
-v, --version 顯示版本信息
-c, --check 檢查命令的有效性,而不實際應用更改。
-f, --file <filename> 包含文件內容<filename>
-i, --interactive 從命令行讀取輸入
-j, --json 以JSON格式化輸出
-n, --numeric 指定一次后,以數字方式顯示網絡地址(默認行為)。
指定兩次以數字方式顯示Internet服務(端口號)。
指定三次以數字方式顯示協議,用戶ID和組ID。
-s, --stateless 省略規則集的有狀態信息。
-N 將IP地址轉換為名稱。
-a, --handle 顯示規則句柄handle.
-e, --echo Echo what has been added, inserted or replaced.
-I, --includepath <directory> 添加<directory>目錄到包含文件的搜索路徑中。默認為: /usr/share
--debug <level [,level...]> 添加調試,在level處(scanner, parser, eval, netlink, mnl, proto-ctx, segtree, all)
# nft 操作符 操作目標 操作內容
操作符: 增,刪,改,查,清除,插入,創建
操作目標: 表,鏈(),規則()
表操作(add,delete,list,flush;
鏈操作(add,delete,rename,list,flush,create);鏈類型(type:filter,route,nat);鏈鈎子(hook...)
規則(add,delete,insert;
操作內容:...
查詢所有表名
nft list tables
查詢某個表的內容
nft list table 表名
\Subcmd→ cmd↓\ |
表語法 table |
鏈語法 chain |
規則語法 rule |
備注 | |
增 | add | ◉ | ◉ | ◉ | 添加到末尾 |
create | ◌ | ◉ | ◌ | 除非已存在同名鏈 | |
insert | ◌ | ◌ | ◉ | 插入到指定位置 | |
刪 | delete | ◉ | ◉ | ◉ | |
flush | ◉ | ◉ | ◌ | ||
改 | rename | ◌ | ◉ | ◌ | 鏈名稱 |
查 | list | ◉ | ◉ | ◌ |
6. nft基礎操作
https://yq.aliyun.com/articles/531207
1、增
增加表:nft add table fillter
增加鏈:nft add chain filter input { type filter hook input priority 0 \; } # 要和hook(鈎子)相關連
增加規則:nft add rule filter input tcp dport 22 accept
2、刪
只需要把上面的 add 改為 delete 即可
3、改
更改鏈名用rename
更改規則用replace
4、查
nft list ruleset # 列出所有規則
nft list tables # 列出所有表
nft list table filter # 列出filter表
nft chain filter input # 列出filter表input鏈
以上命令后面也可以加 -nn 用於不解析ip地址和端口
加 -a 用於顯示 handles
7. 更多規則選項
聲明
1 | accept | 接受 | 接受 包 | 停止處理 |
2 | drop | 丟棄 | 丟棄 包 | 停止處理 |
3 | reject | 拒絕 | 駁回 包 | 停止處理 |
4 | queue | 隊列 | 發送包 到用戶空間程序 | 停止處理 |
5 | continue | 繼續 | 繼續處理包 | |
6 | return | 返回 | 發送到調用的規則鏈進行處理 | |
7 | jump <chain> |
跳躍 | 發送到指定的規則鏈進行處理 | 當完成時或執行了返回的聲明,返回到調用的規則鏈 |
8 | goto <chain> |
轉到 | 發送到指定的規則鏈進行處理 | 不返回到調用的規則鏈 |
9 | limit | limit | 達到接收包的匹配限制, | 則根據規則處理包 |
10 | log | log | 日志記錄 包 | 繼續處理 |
meta 元表達式
66 | meta | rtclassid | Routing realm | 路由領域 | |||||
67 | length | Length of the packet in bytes: meta length > 1000 | 以字節為單位的數據包長度:元長度> 1000 | ||||||
68 | protocol | ethertype protocol: meta protocol vlan | ethertype協議:元協議vlan | ||||||
69 | priority | TC packet priority | TC包優先級 | ||||||
70 | mark | Packet mark | 包標記 | ||||||
71 | iif | Input interface index | 輸入接口索引 | 76 | meta | oif | Output interface index | 輸出接口索引 | |
72 | iifname | Input interface name | 輸入接口名稱 | 77 | oifname | Output interface name | 輸出接口名稱 | ||
73 | iiftype | Input interface type | 輸入接口類型 | 78 | oiftype | Output interface hardware type | 輸出接口硬件類型 | ||
74 | skuid | UID associated with originating socket | 與原始套接字關聯的UID | 79 | skgid | GID associated with originating socket | 與原始套接字關聯的GID | ||
75 | iifgroup | Input interface group | 輸入接口組 | 80 | oifgroup | Output interface group | 輸出接口組 |
ct 連接跟蹤
54 | ct | direction | Direction of the packet relative to the connection | 數據包相對於連接的方向 | |||||
55 | mark | Connection mark | 連接標記 | ||||||
56 | expiration | Connection expiration time | 連接到期時間 | ||||||
57 | helper | Helper associated with the connection | 輔助與連接相關聯 | ||||||
58 | l3proto | Layer 3 protocol of the connection | 連接的第3層協議 | ||||||
59 | protocol | Layer 4 protocol of the connection for the given direction | 給定方向的連接的第4層協議 | ||||||
60 | state | State of the connection | 連接狀態 | 63 | ct | status | Status of the connection | 連接狀態 | |
61 | saddr | Source address of the connection for the given direction | 給定方向的連接的源地址 | 64 | daddr | Destination address of the connection for the given direction | 給定方向的連接的目標地址 | ||
62 | proto-src | Layer 4 protocol source for the given direction | 給定方向的第4層協議源 | 65 | proto-dst | Layer 4 protocol destination for the given direction | 給定方向的第4層協議目的地 |
IPv4 IPv6 報頭表達式
1 | ip | hdrlength | IP header length | IP標頭長度 | |||||
2 | tos | Type of Service | 服務類型 | ||||||
3 | id | IP ID | IP ID | ||||||
4 | frag-off | Fragmentation offset | 碎片偏移 | ||||||
5 | ttl | Time to live | 生存的時間 | ||||||
6 | protocol | Upper layer protocol | 上層協議 | ||||||
7 | checksum | IP header checksum | IP頭校驗和 | ||||||
8 | version | Ip Header version | Ip Header版本 | 12 | ip6 | version | IP header version | IP標頭版本 | |
9 | saddr | Source address | 來源地址 | 13 | saddr | Source Address | 來源地址 | ||
10 | daddr | Destination address | 目的地址 | 14 | daddr | Destination Address | 目的地址 | ||
11 | length | Total packet length | 總包長度 | 15 | length | Payload length | 有效載荷長度 | ||
16 | priority | 優先 | |||||||
17 | nexthdr | Next header type (Upper layer protocol number) | 下一個標題類型 (上層協議號) |
||||||
18 | flowlabel | Flow label | 流標簽 | ||||||
19 | hoplimit | Hop limit | 跳限制 |
TCP UDP 報頭表達式
20 | tcp | sequence | Sequence number | 序列號 | |||||
21 | ackseq | Acknowledgement number | 致謝號碼 | ||||||
22 | doff | Data offset | 數據偏移 | ||||||
23 | flags | TCP flags | TCP標志 | ||||||
24 | window | Window | 窗口 | ||||||
25 | urgptr | Urgent pointer | 緊急指針 | ||||||
26 | sport | Source port | 源端口 | 29 | udp | sport | Source port | 源端口 | |
27 | dport | Destination port | 目的端口 | 30 | dport | destination port | 目的端口 | ||
28 | checksum | Checksum | 校驗 | 31 | checksum | Checksum | 校驗 | ||
32 | length | Total packet length | 總包長度 |
其他匹配項目
33 | udplite | sport | Source port | 源端口 |
34 | dport | destination port | 目的端口 | |
35 | cscov | Checksum coverage | 校驗和覆蓋范圍 | |
36 | checksum | Checksum | 校驗 | |
37 | sctp | sport | Source port | 源端口 |
38 | dport | destination port | 目的端口 | |
39 | vtag | Verification tag | 驗證標簽 | |
40 | checksum | Checksum | 校驗 | |
41 | dccp | sport | Source port | 源端口 |
42 | dport | destination port | 目的端口 | |
43 | ah | nexthdr | Next header protocol (Upper layer protocol) | 下一個標頭協議(上層協議) |
44 | hdrlength | AH header length | AH標頭長度 | |
45 | spi | Security Parameter Index | 安全參數索引 | |
46 | sequence | Sequence Number | 序列號 | |
47 | esp | spi | Security Parameter Index | 安全參數索引 |
48 | sequence | Sequence Number | 序列號 | |
49 | ipcomp | nexthdr | Next header protocol (Upper layer protocol) | 下一個標頭協議(上層協議) |
50 | flags | Flags | 旗 | |
51 | cfi | Compression Parameter Index | 壓縮參數索引 | |
52 | icmp | type | icmp packet type | icmp數據包類型 |
53 | icmpv6 | type | icmpv6 packet type | icmpv6數據包類型 |