记录在 iptables 中丢弃的数据包的内容

One*_*unk 3 iptables tcpdump packet

我试图找到一种方法来记录根据 iptables 中的规则丢弃的数据包(可能使用 tcpdump)的全部内容。

目前,我有一个规则来记录这些数据包(带有日志前缀),然后按照这个规则来删除它们。

有没有办法记录这些数据包的内容以供以后查看?

所以,我正在寻找这个:

  1. 记录匹配数据包的规则
  2. 将数据包传递到记录其内容的新目标(可能是 QUEUE 目标?)
  3. 丢弃数据包的规则

2 & 3 甚至可以组合。

我的理解是 tcpdump 可能无法做到这一点,因为它iptables之前检查数据包,因此不会只记录丢弃的数据包。

谢谢。

Lek*_*eyn 7

NFLOG 目标可用于此目的。这是一个非常基本的例子:

# Drop traffic by default
iptables -P INPUT DROP

# add your whitelists here
# iptables -A INPUT ...

# Pass the packets to NFLOG (just like LOG, but instead of syslog,
# it uses netlink). You can add extra filters such as '-p tcp' as usual
iptables -A INPUT -j NFLOG
# packets that get here will now be dropped per INPUT policy

# Finally you can use tcpdump to capture from this interface (there
# can only be one active user of nflog AFAIK)
tcpdump -i nflog ...
Run Code Online (Sandbox Code Playgroud)

iptables-extensions有关NFLOG目标的说明,请参阅手册页。