如何在 nftables 中记录策略丢弃的数据包?

mau*_*cev 6 linux-networking nftables

我有一个在输入链中丢弃数据包的策略

        chain input {
                type filter hook input priority 0; policy drop;
Run Code Online (Sandbox Code Playgroud)

我怎样才能只记录这些丢弃的数据包?

Joh*_*ene 5

好吧,我猜您还想知道 nftables 链末尾正在记录什么类型的数据包。

\n

让我们假设您已经有一个日志捕获守护进程(例如syslogdrsyslog-ngulogd2),该守护进程已正确配置、守护、运行并读取/dev/log由 kernel\xe2\x80\x99s 提供的所有内核日志记录(来自 )ksyslog()和将这些日志消息保存到文件(例如/var/log/message)。

\n

您的默认策略是接受

\n

如果您的链策略是accept,则将关键字附加log到您的 nftable 规则中:

\n
\ntable filter {\n  ...\n  chain input {\n    type filter hook input priority 0; policy accept;\n    ...\n    # All my rules go here\n    # Pick one that suits your needs best\n\n    add rule inet filter input tcp dport 22 drop log\n    add rule inet filter input tcp dport 21 counter drop log prefix my_input_ftp\n\n  }\n  ...\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这会将 SSH 或 FTP 的任何尝试记录到 SSH/FTP 服务器,然后丢弃数据包。

\n

您的默认策略是 DROP

\n

我会在该链的最后添加一行,filter input链的示例如下:

\n
table filter {\n  ...\n  chain input {\n    type filter hook input priority 0; policy drop;\n    ...\n    # All my rules go here\n    \n    ...\n    # Pick one that suits your needs best\n    counter comment "total unfiltered input packets"\n    log            # simple detail goes into the log\n    log flags all  # extra details go into the log\n    log flags all prefix "GOTCHA!: " # parseable keyword\n    log flags all counter  # redundant but example\n    # drop; # this is redundant policy is drop already\n  }\n  ...\n}\n
Run Code Online (Sandbox Code Playgroud)\n

  • 这些日志命令记录所有内容,而不仅仅是帖子询问的丢弃/拒绝的数据包。 (2认同)

mau*_*cev 0

规则似乎是按顺序执行的。政策是下降;那么你就有了你需要的任何接受规则。现在只要有

log
Run Code Online (Sandbox Code Playgroud)

最后单独在一行上就可以了。