【问题标题】:Linux NFQUEUE handling issueLinux NFQUEUE 处理问题
【发布时间】:2015-04-18 02:46:42
【问题描述】:

我正在尝试编写一个绑定到 NFQUEUE 套接字的用户空间应用程序,内核模块会将传入的数据包转发到该套接字(通过标记它们 NF_QUEUE)。这是通过使用 iptables 规则来实现的,例如:

iptables -­A INPUT -j NFQUEUE --­­queue­-num 0

iptables ACCEPT 链如下所示:

iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination        
ACCEPT     all  --  anywhere             anywhere            state    RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere           
ACCEPT     all  --  anywhere             anywhere           
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
NFQUEUE    all  --  anywhere             anywhere            NFQUEUE num 0

这是轮询 NFQUEUE 套接字的代码(QNUM 为 0):

printf("binding nfnetlink_queue as nf_queue handler for AF_INET\n");
if (nfq_bind_pf(h, AF_INET) < 0)
{   
    fprintf(stderr, "error during nfq_bind_pf()\n");
    return 1;
}   

/* Register a  callback function for our queue number */
qh = nfq_create_queue(h, QNUM, &cb_func, NULL);

if (!qh)
{   
    fprintf(stderr, "error during creating nf_queue\n");
    return 1;
}   

/* See if we have received a packet and send it to our cb func */
fd = nfq_fd(h);
if (fd < 0)
   return 1;

#if 1    
for (;;)
{
    if ((rv = recv(fd, buf, sizeof(buf), 0)) >= 0) {
        nfq_handle_packet(h, buf, sizeof(buf)); /* send packet to callback */
        continue;
    }
}
#endif

问题是 /proc/net/netfilter/nf_queue 列出队列编号 2 而不是编号 0 正在使用,因此我的用户空间程序没有检测到相关套接字中的任何传入数据包。这是为什么呢?

cat /proc/net/netfilter/nf_queue
0 NONE
1 NONE
2 nf_queue
3 NONE
4 NONE
5 NONE
6 NONE
7 NONE
8 NONE
9 NONE
10 NONE
11 NONE
12 NONE

【问题讨论】:

    标签: c linux sockets module userspace


    【解决方案1】:

    我想通了。毕竟,我什至不需要内核模块来执行此操作,只需 iptables 规则即可。问题是该规则不是链条中的第一个。先移动它,用

    sudo iptables -I INPUT -j NFQUEUE --queue-num 0
    

    解决了。

    【讨论】:

      【解决方案2】:

      您可能希望调整 NFQUEUE 以仅针对特定端口,否则您最终可能会意外地将自己锁定在系统之外。

      例如,NFQUEUE 的典型用途是 suricata IPS。如果 suricata 进入失败状态,ssh 访问将随之而来。

      您可以使用以下 IP 表规则定位特定端口:

      -A INPUT -p tcp --dport <port> -j NFQUEUE --queue-num 0
      -A OUTPUT -p tcp --sport <port> -j NFQUEUE --queue-num 0
      

      【讨论】:

        猜你喜欢
        • 2015-07-27
        • 2015-05-09
        • 2019-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-24
        • 2020-05-24
        相关资源
        最近更新 更多