如何在 Ubuntu 中安装 nftables

在本文中,我们将学习如何在 Ubuntu 上将 Linux 防火墙从 IPtables 切换到 nftables。 IPtables 基于 Linux 内核 Netfilter 模块,目前是许多 Linux 发行版的默认防火墙。 它可以防止多个威胁向量,并允许您的服务器根据特定规则集阻止不需要的流量。

nftables 是 Linux 内核的一个新子系统,它取代了 Netfilter 框架(IPtables 所基于)的几个部分,从而改进了功能。 这些更改在内核版本 3.13 中实现。 该模块增强了 Netfilter 的类似防火墙的功能,用于过滤网络流量。 nftables 的高级框架以 Berkeley Packet Filter (BPF) 系统为模型,该系统使用一组基本表达式来构建分组的复杂过滤规则。 值得指出的是,这不是对 IPtables 的更新,而是替代品。 IPtables 很快将在大多数系统上被 nftables 取代,成为一个统一的平台,在内核虚拟机之上提供防火墙配置。

nftables 与 IPtables 有何不同?

在 IPtables 中,默认加载了几个链和表。
在 nftables 中,有 默认链或表。

在 IPtables 中,每个规则只有一个目标。
在 nftables 中,您可以在单个规则中执行多个操作。

在 nftables 中有一个工具叫做 ipset。 使用 ipset 允许列出可以在单个规则中匹配的多个网络或地址。

在 iptables 结构中,每个家族有四个工具:

  • iptables
  • ip6tables
  • arptables
  • ebtables

nftables 包含一个包含所有这些工具的兼容层,它允许使用旧的 iptables 规则语法。

nftables 的主要优点是:

  • 内置于内核中的架构
  • 将 IPtables 工具整合到单个命令行工具中的语法
  • 允许使用 IPtables 规则语法的兼容层。
  • 一种新的、易于学习的语法。
  • 添加防火墙规则的简化过程。
  • 改进的错误报告。
  • 减少代码复制。
  • 更好的整体性能、保留和规则过滤的渐变。

规则比较

. 所有防火墙信息都可以使用一个名为 nft 的命令行工具查看。 nft 对 IPv4 和 IPv6 地址使用单个规则,而不是对每个任务使用多个规则。 它不承担对防火墙后端的完全控制,也不会删除其他工具或用户安装的防火墙规则。 nft 还通过在同一规则中添加用于记录和拒绝功能的选项来改进防火墙规则集。 最后,新的 nft 后端与现有的防火墙配置几乎 100% 兼容。

这是 nftables 中 IP 的简单丢弃规则:

nft add rule ip filter output ip daddr 10.10.10.10 drop

在 IPtables 中,规则是:

iptables -A OUTPUT -d 10.10.10.10 -j DROP

下面的这些示例创建了一个允许 IPv6 流量到各种端口服务的防火墙规则集。

root@host [~]# nft add rule ip6 filter input tcp dport {ftp, http, telnet, https} accept

root@host [~]# nft add rule ip6 filter input icmpv6 type { nd-echo-request,  nd-router-advert, neighbor-solicit, nd-neighbor-advert } accept

以下是一些其他示例:

#review current configuration:
root@host [~]# nft list ruleset

#Add a new table, with family "inet" and table "filter":
root@host [~]# nft add table inet filter

#Add a new chain, to accept all inbound traffic:
root@host [~]# nft add chain inet filter input { type filter hook input priority 0 ; policy accept }

#Add a new rule, to accept several TCP ports:
root@host [~]# nft add rule inet filter input tcp dport { ssh, telnet, https, http } accept

#To show rule handles:
root@host [~]# nft --handle --numeric list chain family table chain

#To delete a rule:
root@host [~]# nft delete rule inet filter input handle 3

#To save the current configuration:
root@host [~]# nft list ruleset > /etc/nftables.conf

这是所使用的插入式替换的快速概述:

Debian/Ubuntu 安装

网络攻击

. 在 Debian/Ubuntu 服务器上安装 nftables 的方法非常简单。 在下面的部分中,我们将当前的 iptables 规则集保存到了一个 .txt 文件中,审查了该文件,将其转换为 nft 可读格式,然后将其导入到新的 nft 规则集中。

root@host:~# iptables-save > fwrules.txt
root@host:~# cat fwrules.txt
root@host:~# iptables-restore-translate -f fwrules.txt
root@host:~# iptables-restore-translate -f fwrules.txt > ruleset.nft
nftables 翻译器

在该过程的第二部分,我们安装 nftables 和 iptables-nftables-compat 工具(将规则加载到 nf_tables 内核子系统中),最后,我们启用该服务。

root@host:~# apt install nftables
root@host:~# apt install iptables-nftables-compat
root@host:~# systemctl enable nftables.service

在最后一节中,我们从 ruleset.nft 文件中提取了之前的规则集。 然后,我们使用“列表”标志查看规则集。

root@host:~# nft -f ruleset.nft
root@host:~# nft list ruleset
table ip nat {
	chain PREROUTING {
		type nat hook prerouting priority 0; policy accept;
	}

	chain INPUT {
		type nat hook input priority 0; policy accept;
	}

	chain OUTPUT {
		type nat hook output priority 0; policy accept;
	}

	chain POSTROUTING {
		type nat hook postrouting priority 0; policy accept;
	}
}
table ip mangle {
	chain PREROUTING {
		type filter hook prerouting priority 0; policy accept;
	}

	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;
	}

	chain POSTROUTING {
		type filter hook postrouting priority 0; policy accept;
	}
}
table ip raw {
	chain PREROUTING {
		type filter hook prerouting priority 0; policy accept;
	}

	chain OUTPUT {
		type filter hook output priority 0; policy accept;
	}
}
table ip 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;
	}
}
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;
	}
}
root@host:~# 

nft 命令概要

#nft command options and syntax
root@host [~]# nft [ -nNscaeSupyj ] [ -I directory ] [ -f filename | -i | cmd …]

root@host [~]#  nft -h
Usage: nft [ options ] [ cmds... ]

Options:
  -h, --help			Show this help
  -v, --version			Show version information

  -c, --check			Check commands validity without actually applying the changes.
  -f, --file <filename>		Read input from <filename>
  -i, --interactive		Read input from interactive CLI

  -n, --numeric			When specified once, show network addresses numerically (default behaviour).
  				Specify twice to also show Internet services (port numbers) numerically.
				Specify three times to also show protocols, user IDs, and group IDs numerically.
  -s, --stateless		Omit stateful information of ruleset.
  -N				Translate IP addresses to names.
  -a, --handle			Output rule handle.
  -e, --echo			Echo what has been added, inserted or replaced.
  -I, --includepath <directory>	Add <directory> to the paths searched for include files. Default is: /etc
  --debug <level [,level...]>	Specify debugging level (scanner, parser, eval, netlink, mnl, proto-ctx, segtree, all)

root@host [~]#  nft -v
nftables v0.8.2 (Joe Btfsplk)

防火墙和 nftables

防火墙呢? 值得庆幸的是,firewalld 通过 nft 命令本身与 nftables 轻松交互。 在下面的 firewalld 图像中,我们看到了 iptables 和 firewalld 当前是如何相互交互的。