我试图编写一个用户空间应用程序,该应用程序绑定到NFQUEUE套接字,内核模块将将传入的数据包(通过标记为NF_QUEUE)转发到该套接字。这是通过使用iptables规则来实现的,如下所示:
iptables -A INPUT -j NFQUEUE --queue-num 0
iptables接受链如下所示:
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
发布于 2015-03-03 05:58:12
我想通了。毕竟,我甚至不需要内核模块来完成这个任务,只需要iptables规则。问题是,这条规则并不是第一条。先把它移开
sudo iptables -I INPUT -j NFQUEUE --queue-num 0
解决了。
发布于 2018-12-18 13:53:30
您可能希望将NFQUEUE调整为只针对特定端口,否则可能会意外地将自己锁在系统之外。
例如,NFQUEUE的一个典型用法是suricata。如果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
https://stackoverflow.com/questions/28560940
复制相似问题