几个不同的地方(例如http://wiki.wireshark.org/CaptureSetup/NFLOG )推荐使用Linux的“NFLOG”防火墙模块来捕获由特定UID生成的数据包,如下所示:
# iptables -A OUTPUT -m owner --uid-owner 1000 -j CONNMARK --set-mark 1 # iptables -A INPUT -m connmark --mark 1 -j NFLOG --nflog-group 30 # iptables -A OUTPUT -m connmark --mark 1 -j NFLOG --nflog-group 30 # dumpcap -i nflog:30 -w uid-1000.pcap
我一直没有find任何关于这个工作原理的文档(特别是netfilter.org有很多写得很差的库API文档,据我所知,没有任何关于实际内核的语义级防火墙规则),所以我有几个问题:
有没有任何该死的文件,它隐藏在哪里?
CONNMARK事情真的有必要吗? 也就是说,这个工作也一样吗?
# iptables -A INPUT -m owner --uid-owner 1000 -j NFLOG --nflog-group 30 # iptables -A OUTPUT -m owner --uid-owner 1000 -j NFLOG --nflog-group 30
是否有必要运行“ulogd”来运行?
有没有办法告诉内核为我select一个未分配的组号码,告诉我它是什么?
有没有办法告诉内核,当进程X终止时,这些过滤规则应该被自动删除? (进程X 不会像uid 1000那样运行。)
据推测iptables命令做了一些特殊的ioctl调用或configuration防火墙的东西。 是否有一个C库可以用来在程序中做同样的事情(即Q4中的“process X”)?
有没有任何该死的文件,它隐藏在哪里?
netfilter网站上有一些例子可以帮助解释function。 这是我在自己的代码中写的一个函数,它设置了netfilter NFLOG。
以下是他们提供的示例: http : //www.netfilter.org/projects/libnetfilter_log/doxygen/files.html
void setup_netlogger_loop( int groupnum, queue_t queue) { int sz; int fd = -1; char buf[BUFSZ]; /* Setup handle */ struct nflog_handle *handle = NULL; struct nflog_g_handle *group = NULL; memset(buf, 0, sizeof(buf)); /* This opens the relevent netlink socket of the relevent type */ if ((handle = nflog_open()) == NULL){ sd_journal_perror("Could not get netlink handle"); exit(EX_OSERR); } /* We tell the kernel that we want ipv4 tables not ipv6 */ if (nflog_bind_pf(handle, AF_INET) < 0) { sd_journal_perror("Could not bind netlink handle"); exit(EX_OSERR); } /* Setup groups, this binds to the group specified */ if ((group = nflog_bind_group(handle, groupnum)) == NULL) { sd_journal_perror("Could not bind to group"); exit(EX_OSERR); } if (nflog_set_mode(group, NFULNL_COPY_PACKET, 0xffff) < 0) { sd_journal_perror("Could not set group mode"); exit(EX_OSERR); } if (nflog_set_nlbufsiz(group, BUFSZ) < 0) { sd_journal_perror("Could not set group buffer size"); exit(EX_OSERR); } if (nflog_set_timeout(group, 1500) < 0) { sd_journal_perror("Could not set the group timeout"); } /* Register the callback */ nflog_callback_register(group, &queue_push, (void *)queue); /* Get the actual FD for the netlogger entry */ fd = nflog_fd(handle); /* We continually read from the loop and push the contents into nflog_handle_packet (which seperates one entry from the other), which will eventually invoke our callback (queue_push) */ for (;;) { sz = recv(fd, buf, BUFSZ, 0); if (sz < 0 && errno == EINTR) continue; else if (sz < 0) break; nflog_handle_packet(handle, buf, sz); } }
CONNMARK事情真的有必要吗? 也就是说,这个工作也一样吗?
这是没有必要的。
是否有必要运行“ulogd”来运行?
没有 – 事实上,我不使用它在这个应用程序。
有没有办法告诉内核为我select一个未分配的组号码,告诉我它是什么?
不是我所知道的。 在任何情况下,如果您为HTTP设置NFLOG目标,那么这将是无用的,一个用于logging丢弃的数据包是FTP和一个正在扫描SMTPstring。 在这种情况下,您不能确定哪个规则绑定到哪个组,哪个组应该被监听。
有没有办法告诉内核,当进程X终止时,这些过滤规则应该被自动删除? (进程X不会像uid 1000那样运行。)
不,但内核填充缓冲区只能达到最大值,然后会丢弃数据。 它没有带来性能上的影响,因为使用了太多内存而没有监听规则。
据推测iptables命令做了一些特殊的ioctl调用或configuration防火墙的东西。 是否有一个C库可以用来在程序中做同样的事情(即Q4中的“process X”)?
我知道没有netfilter库可以帮助你操纵规则。 有一个内部驱动的库,虽然使用。
IPtablesinheritance了对用户空间的一种相当古老的方法 – 你打开一个SOCK_RAW IP套接字来与它通信。 这是完全将被删除(因为它没有任何意义)与nftables将通过netlink发言做同样的事情。