防火墙子系统/包过滤引擎-Nftables
2014-04-20 22:03:49 阿炯

本站赞助商链接,请多关照。 Nftables 提供了一个更简单的kernel ABI,减少重复代码,改进错误报告,更有效的支持过滤规则。采用C语言开发并在GPL协议下授权。


nftables is the project that aims to replace the existing {ip,ip6,arp,eb}tables framework. Basically, this project provides a new packet filtering framework, a new userspace utility and also a compatibility layer for {ip,ip6}tables. nftables is built upon the building blocks of the Netfilter infrastructure such as the existing hooks, the connection tracking system, the userspace queueing component and the logging subsystem.

Features

Pseudo-state machine in kernel-space: the userspace utility nftables interprets the rule-set provided by the user (using a new syntax), it compiles it into the pseudo-state machine bytecode and then it transfers it to the kernel via the nftables Netlink's API. Roughly, the idea behind nftables is similar to the Berkeley Packet Filters (BPF).

Fast lookups through performance data structures: The new syntax allows you to arrange you rule-set in a very performance way contrary to purely linear-list based filtering policies. Nftables allows you to use set-based action mappings, ie. for a matching element in the set, issue the action specified by the user.

Reduce the amount of code in kernel-space. You can express the packet selectors for all existing protocols using the instruction-set provided by the nftables pseudo-state machine. That means that we do not need a specific extension in kernel-space for each protocol that you want to support. As a side effect, you are likely not need to upgrade your kernel to obtain new features as it has been designed to keep most of the logic in user-space.

Unified interface to replace iptables/ip6tables/arptables/ebtables utilities. Thus, we will be able to fully get rid of all the existing code replication in kernel and user-space.

对于Linux主机而言,防火墙是首要的安全防线;其按照预设规则筛查每一个进出数据包,拦截潜在的恶意流量。在 Linux 下防火墙技术经历了从 iptables 到 nftables 的演进。在此将宏观了解 Linux 防火墙生态,并深入剖析这两代核心技术的设计差异与使用方式。

Linux 防火墙生态概览:认识三大主流工具

在配置防火墙时通常会遇到三个名字:UFW、Firewalld、iptables,理解它们的区别至关重要。

• iptables:这是最底层的工具,直接与 Linux 内核的 netfilter 模块交互。它功能极其强大,控制粒度极细,但语法也相对复杂,学习曲线较陡。它是许多其他防火墙工具的基础。

• UFW (Uncomplicated Firewall):正如其名,UFW 是一个"不复杂的防火墙"。它是为简化 iptables 操作而设计的用户友好型前端,在 Ubuntu 等 Debian 系发行版中非常流行。它的命令简单直观,非常适合快速配置。

Firewalld:这是 Red Hat 系发行版(如 CentOS, Fedora)中的默认防火墙管理工具。其引入了"区域 (zones)"的概念,允许根据网络环境(如公共、家庭、工作)快速切换不同的安全策略集,在底层可以使用 iptables 或现代的 nftables 作为后端。

一句话:iptables 是引擎,而 UFW 和 Firewalld 则是更易于操作的前端手柄。

技术核心的演变:iptables vs nftables

现在来深入引擎室,看看驱动这一切的底层技术是如何演变的。

1. 经典王者:iptables

iptables 服务了 Linux 世界二十多年,其设计理念是围绕"表 (Tables)"和"链 (Chains)"构建的。
• 表 (Tables): 如 filter 表用于过滤,nat 表用于网络地址转换。
• 链 (Chains): 如 INPUT (入站)、OUTPUT (出站)、FORWARD (转发)。

虽然功能强大,但 iptables 的一些天生缺陷也逐渐显现:
• 代码冗余:IPv4 (iptables) 和 IPv6 (ip6tables) 有两套独立的命令和规则,管理起来有些繁琐。
• 性能瓶颈:规则是线性检查的,当规则数量庞大时,性能会下降。
• 语法复杂:命令和参数晦涩,难以阅读和维护。

示例:使用 iptables 允许 Web 流量

# 需要为 HTTP 和 HTTPS 分别添加规则
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

2. 下一代继任者:nftables

为了解决 iptables 的痛点,nftables 应运而生。它不是 iptables 的简单升级,而是一次彻底的重构。在新的主流发行版中,nftables 已成为默认后端。

nftables 的核心优势:
• 统一与简洁:一个 nft 命令就整合了 iptables, ip6tables, arptables 的所有功能。语法也更加直观,可读性极高。
• 卓越的性能:引入了新的数据结构,如"集合 (sets)"和"字典 (maps)",允许对一组 IP 或端口应用单条规则,极大地提升了大规模规则集的处理效率。
• 原子性更新:规则集的更新是"原子操作",要么全部成功,要么全部失败,避免了在更新过程中出现防火墙配置不一致的中间状态,大大增强了可靠性。
• 更好的内核集成:nftables 更像一个简单的虚拟机,大部分逻辑从内核空间移到了用户空间,使得更新和调试更为灵活。

示例:使用 nftables 允许 Web 流量

# 使用"集合"将多个端口合并到一条规则中
nft add rule inet filter input tcp dport { 80, 443 } accept

更多 nftables 实用示例:
• 创建表与链(一次性设置防火墙结构)
# 创建 inet 家族的 filter 表
nft add table inet filter

# 在 filter 表中添加 input 链,默认策略为 drop
nft add chain inet filter input { type filter hook input priority 0 \; policy drop \; }

• 允许本地回环接口流量
nft add rule inet filter input iif "lo" accept

• 允许已建立和相关连接的数据包
nft add rule inet filter input ct state established,related accept

• 允许特定端口(如 SSH)
nft add rule inet filter input tcp dport 22 accept

• 拒绝所有其他流量
nft add rule inet filter input counter drop

这样就建立了一个既安全又精简的 nftables 防火墙策略集,涵盖了典型的 Web 和 SSH 场景。可以看到,nftables 的语法不仅更简洁,逻辑也更清晰。

用户该如何选择?

• 对于新项目或新服务器:毫无疑问,直接学习和使用 nftables。它是 Linux 防火墙的未来,更高效、更强大、更易于维护。
• 对于管理工具的选择:如果你偏爱简单,UFW 是个不错的选择。如果你需要根据不同网络环境切换策略,Firewalld 的"区域"概念会非常有用。
• 对于老旧系统:如果你的系统仍在使用 iptables 并且运行良好,没有必要立即进行迁移。但了解 nftables 的优势,将有助于你为未来的架构演进做好准备。

理解 Linux 防火墙的生态和其核心技术的演进,有助于做出更明智的技术选型,从而更高效、更可靠地保护数字资产的安全。在 Linux 防火墙技术的演进中,nftables 作为 iptables 的现代替代品,正逐渐成为系统管理员和网络工程师的首选工具。自 2014 年在 Linux 内核 3.13 版本中引入以来,nftables 以其简洁的语法、强大的功能和优越的性能,重新定义了 Linux 防火墙的配置方式。

nftables 是一个用户空间工具,用于配置 Linux 内核的 netfilter 框架,负责处理网络数据包的过滤、地址转换和修改。与 iptables 类似,nftables 依赖 netfilter 提供底层支持,但它引入了全新的设计,克服了 iptables 的诸多局限性。其主要目标包括:
统一 IPv4 和 IPv6:使用单一工具处理 IPv4 和 IPv6 流量,取代 iptables 和 ip6tables 的分离。
简化语法:提供更直观、类似脚本的语法,便于管理复杂规则集。
提高性能:通过增量规则处理和集合机制,优化大规模规则集的效率。
增强灵活性:支持用户定义的表和链,以及高级功能如集合(sets)和映射(maps)。

nftables 自 2014 年发布以来,已在现代 Linux 发行版(如 Ubuntu 20.04、CentOS 8)中成为默认防火墙工具,逐渐取代 iptables。

设计理念

其设计旨在解决 iptables 的以下问题:
复杂语法:iptables 的每条规则都需要单独命令,管理大型规则集时容易出错。
性能瓶颈:iptables 使用线性规则处理,规则越多,性能越低。
协议分离:iptables 需要单独的工具(iptables、ip6tables、arptables 等)处理不同协议。
有限的扩展性:iptables 的表和链结构固定,难以适应现代网络需求。

核心改进包括:
统一框架:一个工具支持 IPv4、IPv6、ARP 和桥接流量。
增量处理:规则以字节码形式存储,内核处理效率更高。
灵活结构:用户可以自由定义表和链,组织规则更加模块化。
高级功能:支持集合、映射和判决映射(verdict maps),便于动态规则管理。

例如,允许 SSH 流量的 nftables 规则如下:
nft add rule ip filter input tcp dport 22 accept

相比 iptables 的类似规则(iptables -A INPUT -p tcp --dport 22 -j ACCEPT),nftables 的语法更简洁,且支持批量操作。

架构

与 iptables 基于固定表的架构不同,nftables 允许用户完全自定义表和链,提供了更高的灵活性。其核心组件包括:
表(Tables):规则的逻辑容器,可任意命名,支持 IPv4、IPv6 或混合协议(inet)。
链(Chains):规则的有序列表,绑定到 netfilter 钩子,决定数据包的处理阶段。
规则(Rules):由匹配条件和动作组成,定义数据包的处理逻辑。

表(Tables)
表是 nftables 中规则的顶级容器,用户可以根据需要创建任意数量的表。例如:
ip 表:处理 IPv4 流量。
ip6 表:处理 IPv6 流量。
inet 表:同时处理 IPv4 和 IPv6 流量。
bridge 表:处理桥接流量。

创建表的命令:
nft add table inet my_table

链(Chains)
链定义了规则的处理阶段,分为内置链和自定义链:
内置链:绑定到 netfilter 钩子(如 input、output、forward),需指定类型(如 filter、nat)、钩子和优先级。
自定义链:用户定义的链,用于组织复杂规则。

创建内置链的示例:
nft add chain inet my_table input { type filter hook input priority 0 \; policy drop \; }

此命令创建一个 filter 类型的输入链,绑定到 input 钩子,优先级为 0,默认策略为 drop。

规则(Rules)

规则由匹配条件和动作组成:
匹配条件:包括协议(tcp、udp)、端口、IP 地址、连接状态等。
动作:包括 accept、drop、reject、log 或跳转到其他链。

示例:允许 HTTP 流量:
nft add rule inet my_table input tcp dport 80 accept

数据包处理流程

nftables 与 iptables 共享相同的 netfilter 钩子:
prerouting:数据包进入系统后,路由决定之前。
input:发往本地进程的数据包。
forward:通过系统的转发数据包。
output:本地生成的数据包。
postrouting:数据包离开系统前,路由决定之后。

nftables 的表和链可以绑定到这些钩子。例如一个典型的 filter 表可能包含 input 和 output 链,用于处理本地流量。

核心优势

1. 统一协议处理
nftables 使用单一工具处理 IPv4、IPv6、ARP 和桥接流量。例如,使用 inet 表可以同时定义 IPv4 和 IPv6 规则:
nft add rule inet my_table input ip protocol tcp tcp dport 80 accept
nft add rule inet my_table input ip6 nexthdr tcp tcp dport 80 accept

2. 简洁的语法
nftables 的命令支持批量操作,可以通过脚本或单行命令定义复杂规则集。例如:
nft add rule inet my_table input tcp dport { 80, 443 } accept

此规则同时允许 HTTP(80)和 HTTPS(443)流量。

3. 高性能
nftables 使用字节码表示规则,内核处理效率高于 iptables 的线性规则检查。此外,集合(sets)允许快速查找:
nft add set inet my_table allowed_ports { type inet_service \; elements = { 80, 443 } \;}
nft add rule inet my_table input tcp dport @allowed_ports accept

4. 灵活性和扩展性
用户可以自由定义表和链,支持复杂的逻辑。例如,创建多个表分别处理不同类型的流量:
nft add table inet web_traffic
nft add table inet admin_traffic

基本命令

以下是 nftables 的常用命令:

列出规则:
nft list ruleset

添加表:
nft add table inet my_table

添加链:
nft add chain inet my_table input { type filter hook input priority 0 \; policy drop \; }

添加规则:
nft add rule inet my_table input tcp dport 22 accept

删除规则:
nft delete rule inet my_table input handle 123
(需通过 nft list ruleset 获取规则的 handle 编号)

清空规则:
nft flush table inet my_table
    
实践示例:配置简单的防火墙

假设我们要为一个 Web 服务器配置 nftables,需求如下:
允许 HTTP(80 端口)和 SSH(22 端口)入站流量。
允许所有出站流量。
丢弃其他入站流量。
记录所有 ICMP(ping)请求。

配置步骤

创建表和链:
nft add table inet my_table
nft add chain inet my_table input { type filter hook input priority 0 \; policy drop \; }
nft add chain inet my_table output { type filter hook output priority 0 \; policy accept \; }

允许 HTTP 和 SSH 流量:
nft add rule inet my_table input tcp dport 80 accept
nft add rule inet my_table input tcp dport 22 accept

允许已建立的连接:
nft add rule inet my_table input ct state established,related accept

记录并丢弃 ICMP 流量:
nft add rule inet my_table input ip protocol icmp log prefix \"ICMP_LOG: \"
nft add rule inet my_table input ip protocol icmp drop

测试配置

使用以下命令验证规则:

nft list ruleset
输出将显示所有表、链和规则的详细信息。

测试防火墙效果:
使用 nmap 检查开放端口:
nmap 192.168.1.100

检查日志(假设使用 syslog):
cat /var/log/syslog | grep ICMP_LOG

测试出站连接:
curl http://www.freeoa.net

保存和恢复规则

nftables 规则默认存储在内存中,重启后会丢失。保存和恢复规则的方法如下:

保存规则:
nft list ruleset > /etc/nftables.conf

恢复规则:
nft -f /etc/nftables.conf

在 Ubuntu 上可以使用 nftables 服务自动加载规则:
sudo systemctl enable nftables
sudo systemctl start nftables


nftables 与 iptables 的对比
特性iptablesnftables
协议支持IPv4 和 IPv6 需单独工具(iptables、ip6tables)统一支持 IPv4、IPv6、ARP、桥接
语法逐条命令,复杂规则集管理困难脚本化语法,支持批量操作
性能线性规则处理,大规则集效率低增量处理,支持集合,效率高
表结构固定表(filter、nat、mangle、raw)用户自定义表和链,灵活性高
高级功能有限(如状态匹配、限速)支持集合、映射、判决映射等


注意事项

规则顺序:nftables 按顺序处理规则,优先级高的规则应放在前面。
默认策略:设置 drop 策略时,确保关键服务(如 SSH)已明确允许。
兼容性:nftables 支持 iptables-nft 后端,允许运行旧 iptables 规则。
调试:使用 nft list ruleset 和日志功能排查规则问题。

nftables 作为 iptables 的现代替代品,以其统一框架、简洁语法和高性能,正在成为 Linux 防火墙的主流工具。


最新版本:0.2
此版本更新内容如下:
支持 Linux kernel 3.14 的新特性;
bug 修复 和文档更新;
混合 IPv4/IPv6 表,队列负载均衡
支持修改 packet 和 conntrack 元数据
修复了 big endian 平台上所有已知的问题

项目主页:http://netfilter.org/projects/nftables/