Linux   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Linux NFLOG – 来自C的文档,配置大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

几个不同的地方(例如 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 connma
几个不同的地方(例如 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

我还没有找到任何关于它如何工作的文档(特别是,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”才能运行?
>有没有办法告诉内核为我选择@L_616_7@未分配的组号并告诉我它是什么?
>有没有办法告诉内核,当进程X终止时,应该自动删除这些过滤规则? (进程X不会作为uid 1000运行.)
>据推测,iptables命令可以进行一些特殊的ioctl调用或配置防火墙.是否有@L_616_7@C库可用于在程序中执行相同的操作(即Q4中的“进程X”)?

解决方法

netfilter网站上有一些例子可以帮助解释这些功能.这是我在自己的代码中编写的@L_616_7@函数,用于设置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,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,sz);
  }
}

这是不必要的.

不 – 实际上我不在这个应用程序中使用它.

不是我知道的.在任何情况下,如果您为http设置了NFLOG目标,@L_616_7@用于记录丢弃的FTP数据包,另@L_616_7@用于扫描SMTP字符串,则无用.
在这种情况下,您无法确定哪个规则绑定到哪个组,从而应该监听哪个组.

不,但内核只填充最大大小的缓冲区然后将丢弃数据.在使用过多内存而没有收听规则方面,它不会对性能产生影响.

我所知道的netfilter库没有帮助你操纵规则.但是有@L_616_7@内部驱动的库可供使用.

IPtables继承了一种与用户空间对话的相当陈旧的方法 – 你打开@L_616_7@SOCK_RAW IP套接字与它进行通信.这完全将被删除(因为它没有意义)与nftables将通过netlink说话做同样的事情.

@H_696_56@

大佬总结

以上是大佬教程为你收集整理的Linux NFLOG – 来自C的文档,配置全部内容,希望文章能够帮你解决Linux NFLOG – 来自C的文档,配置所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。