nflog 使用

  xx产品有个功能是对任何端口的访问都会被记录。它的实现原理是iptables的NFLOG

NFLOG是什么

它是一个target,就像ACCEPT、DROP等可以作为iptables -j后的参数值

iptables -A INPUT -p tcp -m tcp --dport 80 -j NFLOG --nflog-group 40 --nflog-prefix TCPDUMP-PCAP-IN
tcpdump -i nflog:40 -v -w nf40.pcap
// 此时tcpdump必须使用 -w 参数报文报文

 tcpdump  -i nflog:40 -v -w nf40.pcap
tcpdump: listening on nflog:40, link-type NFLOG (Linux netfilter log messages), capture size 262144 bytes

示例:

Can I log and drop packets with nflog in one iptables rule?

-N log_and_drop
-A log_and_drop -j NFLOG --nflog-prefix "shared prefix"
-A log_and_drop -j DROP
-A INPUT -p tcp --sport 1234 -g log_and_drop
-A INPUT -p udp --sport 4321 -g log_and_drop

Is there any way to put NFLOG (--nflog-prefix) inside the iptables rule so that there is only one line left?

NFLOG:https://ipset.netfilter.org/iptables-extensions.man.html#lbDG

 

内核代码为:

static struct xt_target ebt_nflog_tg_reg __read_mostly = {
	.name       = "nflog",
	.revision   = 0,
	.family     = NFPROTO_BRIDGE,
	.target     = ebt_nflog_tg,
	.checkentry = ebt_nflog_tg_check,
	.targetsize = sizeof(struct ebt_nflog_info),
	.me         = THIS_MODULE,
};
ebt_nflog_tg(struct sk_buff *skb, const struct xt_action_param *par)
{
	const struct ebt_nflog_info *info = par->targinfo;
	struct net *net = xt_net(par);
	struct nf_loginfo li;

	li.type = NF_LOG_TYPE_ULOG;
	li.u.ulog.copy_len = info->len;
	li.u.ulog.group = info->group;
	li.u.ulog.qthreshold = info->threshold;
	li.u.ulog.flags = 0;

	nf_log_packet(net, PF_BRIDGE, xt_hooknum(par), skb, xt_in(par),
		      xt_out(par), &li, "%s", info->prefix);
	return EBT_CONTINUE;
}

 

posted @   codestacklinuxer  阅读(607)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
历史上的今天:
2021-09-22 skiplist 记录
点击右上角即可分享
微信分享提示