Linux NFLOG - 文档,来自 C 的配置

zwo*_*wol 6 linux firewall packet-capture

几个不同的地方(例如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
Run Code Online (Sandbox Code Playgroud)

我一直无法找到任何关于它是如何工作的文档(特别是,netfilter.org有很多写得很糟糕的库 API 文档,据我所知,在实际内核级防火墙的语义上没有任何内容规则),所以我有几个问题:

  1. 是否有任何该死的文档,它藏在哪里?

  2. 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
    
    Run Code Online (Sandbox Code Playgroud)
  3. 是否有必要运行“ulogd”才能使其工作?

  4. 有没有办法告诉内核为我选择一个未分配的组号并告诉我它是什么?

  5. 有没有办法告诉内核在进程 X 终止时应该自动删除这些过滤规则?(进程 X不会以 uid 1000 运行。)

  6. 据推测,该iptables命令会进行一些特殊ioctl调用或配置防火墙。是否有一个 C 库可用于在程序中执行相同的操作(即 Q4 中的“进程 X”)?

Mat*_*Ife 6

是否有任何该死的文档,它藏在哪里?

netfilter 站点上有一些示例可以帮助解释该功能。这是我在自己的代码中编写的一个函数,用于设置 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);
  }
}
Run Code Online (Sandbox Code Playgroud)

CONNMARK的东西真的有必要吗?也就是说,这也能正常工作吗?

这是不必要的。

是否有必要运行“ulogd”才能使其工作?

不——事实上我没有在这个应用程序中使用它。

有没有办法告诉内核为我选择一个未分配的组号并告诉我它是什么?

不是我所知道的。在任何情况下,如果您为 HTTP 设置了 NFLOG 目标,一个用于记录丢弃的 FTP 数据包,另一个用于扫描 SMTP 字符串,那么这将是无用的。在这种情况下,您无法确定哪个规则绑定到哪个组,以及应该监听哪个组。

有没有办法告诉内核在进程 X 终止时应该自动删除这些过滤规则?(进程 X 不会以 uid 1000 运行。)

不,但内核仅将缓冲区填充到最大大小,然后将丢弃数据。它不会因使用过多内存而未监听规则而对性能造成影响。

据推测,iptables 命令会进行一些特殊的 ioctl 调用或其他东西来配置防火墙。是否有一个 C 库可用于在程序中执行相同的操作(即 Q4 中的“进程 X”)?

我知道没有 netfilter 库可以帮助您操纵规则。但是有一个内部驱动的库被用来代替。

IPtables 继承了一种与用户空间对话的相当古老的方法——您打开一个 SOCK_RAW IP 套接字与它进行通信。这将被 nftables 完全删除(因为它没有意义),它会通过 netlink 说话来做同样的事情。