我无法让 iptables 显示跟踪日志

red*_*888 6 debian iptables

uname -a
Linux test 5.10.0-17-cloud-amd64 #1 SMP Debian 5.10.136-1 (2022-08-13) x86_64 GNU/Linux

# enable logging
sudo modprobe ipt_LOG
sudo modprobe nf_log_ipv4
sudo sysctl net.netfilter.nf_log.2=nf_log_ipv4

# Try to log traffic
sudo iptables -t raw -I PREROUTING -s 1.2.3.4 -j LOG
sudo iptables -t raw -I PREROUTING -s 1.2.3.5 -j LOG
# I see logs for these IPs
tail /var/log/kern.log

# Try to trace traffic
sudo iptables -t raw -I PREROUTING -s 1.2.3.4 -j TRACE
sudo iptables -t raw -I PREROUTING -s 1.2.3.5 -j TRACE
# See no trace logs for these IPs
tail /var/log/kern.log
Run Code Online (Sandbox Code Playgroud)

我在日志文件中看到日志,但看到零跟踪日志。我无法在任何地方找到有关原因的信息。

A.B*_*A.B 8

Debian在 Debian 10 中切换iptables -over- nftables,因此这包括 Debian 11。

$ update-alternatives --list iptables
/usr/sbin/iptables-legacy
/usr/sbin/iptables-nft
$ update-alternatives --display iptables
iptables - auto mode
  link best version is /usr/sbin/iptables-nft
  link currently points to /usr/sbin/iptables-nft
  link iptables is /usr/sbin/iptables
[...]
Run Code Online (Sandbox Code Playgroud)

目标框架与此变体特别不同,以受益于nftablesTRACE API的优势。iptables目标的手册中对此进行了描述:TRACExtables-monitor

使用 iptables-nft,目标被转换为 nftables 的 meta nftrace表达式。因此,内核通过 netlink 将跟踪事件发送到用户空间,在用户空间中可以使用xtables-monitor --trace 命令显示这些事件。有关详细信息,请参阅xtables-monitor(8)

xtables-monitor用于监视规则集的更改或显示使用 TRACE 目标标记的数据包的规则评估事件xtables-monitor将运行直到用户中止执行(通常使用 CTRL-C)。

这意味着大量文档和博客正在变得过时,并且仅显示遗留方法。

现在有了iptables-nft,我们可以运行它来显示跟踪:

xtables-monitor -t
Run Code Online (Sandbox Code Playgroud)

新 API 至少有一个优点:

较旧的 API(使用命令时仍然存在iptables-legacy)将 TRACE 结果发送到内核环形缓冲区(例如:使用dmesg)。此缓冲区不支持网络命名空间,因此可以选择仅记录初始网络命名空间活动(包括 TRACE 结果),或通过切换记录所有网络命名空间活动。TRACE 非常冗长,这很快就会成为一个问题。

新的 API 使用类似于netlink多播的套接字消息来向可能的多个侦听器提供信息。它也是网络命名空间感知的。它不会污染日志。实施者选择打破仅在调试时使用的特定目标的兼容性,以利用nftables的 API,而不是保留原始行为。

这意味着当运行多个网络命名空间时,可以单独跟踪它们(只要它们具有带有 TRACE 目标的规则),例如如下所示:

ip netns exec othernamespace xtables-monitor -t
Run Code Online (Sandbox Code Playgroud)

与所有其他网络命名空间完全隔离。

nftables命令大多数情况nft monitor trace下也可以工作(可能不显示本机 xtables 匹配和目标),而不是xtables-monitor -t监视这些iptables规则,因为它是相同的 API。


实际上,使用NFLOG目标而不是LOG具有类似的行为(并且使用旧版或 nft API 是相同的),并使用相同类型的工具发送日志,用于专门的日志记录工具:通常ulogd但甚至tcpdump能够在 Linux 上捕获它。例子:

1号航站楼:

$ ping 127.0.0.1
[...]
Run Code Online (Sandbox Code Playgroud)

终端 2(作为 root):

# iptables -I INPUT -i lo -p icmp -j NFLOG --nflog-group 123
# tcpdump -ttttt -n -l -i nflog:123
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on nflog:123, link-type NFLOG (Linux netfilter log messages), snapshot length 262144 bytes
 00:00:00.000000 IP 127.0.0.1 > 127.0.0.1: ICMP echo request, id 47883, seq 36, length 64
 00:00:00.000399 IP 127.0.0.1 > 127.0.0.1: ICMP echo reply, id 47883, seq 36, length 64
 00:00:01.023906 IP 127.0.0.1 > 127.0.0.1: ICMP echo request, id 47883, seq 37, length 64
 00:00:01.023976 IP 127.0.0.1 > 127.0.0.1: ICMP echo reply, id 47883, seq 37, length 64
^C
4 packets captured
4 packets received by filter
0 packets dropped by kernel
Run Code Online (Sandbox Code Playgroud)

附加信息:NFLOG 与wiresharkNftables 和Netfilter 日志框架