用于修改数据包的Linux模块(Netfilter)

用于修改数据包的Linux模块(Netfilter),linux,module,packet,Linux,Module,Packet,有人能告诉我如何使用Netfilter挂钩通过linux模块修改数据包数据吗 谢谢 无需编写自己的netfilter模块。您可以使用iptables中的队列目标从用户空间访问它,并编写一个处理队列的守护进程 这方面的例子相对较少,但确实存在一些。它通常用于过滤,但您也可以(我相信)重新注入修改过的数据包(至少在iptables的mangle表中)。无需编写自己的netfilter模块。您可以使用iptables中的队列目标从用户空间访问它,并编写一个处理队列的守护进程 这方面的例子相对较少,但确

有人能告诉我如何使用Netfilter挂钩通过linux模块修改数据包数据吗


谢谢

无需编写自己的netfilter模块。您可以使用iptables中的队列目标从用户空间访问它,并编写一个处理队列的守护进程


这方面的例子相对较少,但确实存在一些。它通常用于过滤,但您也可以(我相信)重新注入修改过的数据包(至少在iptables的mangle表中)。

无需编写自己的netfilter模块。您可以使用iptables中的队列目标从用户空间访问它,并编写一个处理队列的守护进程

这方面的例子相对较少,但确实存在一些。它通常用于过滤,但您也可以(我相信)重新注入修改过的数据包(至少在iptables的mangle表中)。

试试下面的程序

写入IPTABLES规则以将数据包传递给用户空间数据包

# iptables -A INPUT -p TCP -j QUEUE
编译和执行为

$ gcc test.c -lipq
$ sudo ./a.out
源代码

#include <netinet/in.h> 
#include <linux/netfilter.h> 
#include <libipq.h> 
#include <stdio.h> 
#include <stdlib.h>


#define BUFSIZE 2048
static void die(struct ipq_handle *h)
{
    ipq_perror("passer");
    ipq_destroy_handle(h);
    exit(1);
}
int main(int argc, char **argv)
{
    int status, i=0;
    unsigned char buf[BUFSIZE];
    struct ipq_handle *h;
    h = ipq_create_handle(0, NFPROTO_IPV4);

    if (!h)     die(h);

    status = ipq_set_mode(h, IPQ_COPY_PACKET, BUFSIZE);

    if (status < 0) die(h);

    do{
        i++;
        status = ipq_read(h, buf, BUFSIZE, 0);

        if (status < 0) die(h);

        switch (ipq_message_type(buf)) {
            case NLMSG_ERROR:
                fprintf(stderr, "Received error message %d\n",
                ipq_get_msgerr(buf));
                break;
            case IPQM_PACKET:
            {
                ipq_packet_msg_t *m = ipq_get_packet(buf);
                printf("\nReceived Packet");
                /****YOUR CODE TO MODIFY PACKET GOES HERE****/
                status = ipq_set_verdict(h, m->packet_id, NF_ACCEPT, 0, NULL);
                if (status < 0)  die(h);
                break;
            }
            default:
                fprintf(stderr, "Unknown message type!\n");
                break;
        }
    } while (1);
    ipq_destroy_handle(h);
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义bufsize2048
静态空心模具(结构ipq_手柄*h)
{
ipq_perror(“通行证”);
ipq_销毁_手柄(h);
出口(1);
}
int main(int argc,字符**argv)
{
int状态,i=0;
无符号字符buf[BUFSIZE];
结构ipq_句柄*h;
h=ipq_创建_句柄(0,NFPROTO_IPV4);
如果(!h)死亡(h);
状态=ipq_设置_模式(h,ipq_复制_数据包,BUFSIZE);
如果(状态<0)死亡(h);
做{
i++;
状态=ipq_读取(h、buf、BUFSIZE、0);
如果(状态<0)死亡(h);
开关(ipq_消息_类型(buf)){
案例NLMSG_错误:
fprintf(标准,“收到错误消息%d\n”,
ipq_get_msgerr(buf));
打破
案例IPQM_数据包:
{
ipq\u数据包\u msg\u t*m=ipq\u get\u数据包(buf);
printf(“\n收到的数据包”);
/****修改数据包的代码在这里****/
状态=ipq\u集合\u判定(h,m->packet\u id,NF\u ACCEPT,0,NULL);
如果(状态<0)死亡(h);
打破
}
违约:
fprintf(stderr,“未知消息类型!\n”);
打破
}
}而(1),;
ipq_销毁_手柄(h);
返回0;
}
尝试下面的程序

写入IPTABLES规则以将数据包传递给用户空间数据包

# iptables -A INPUT -p TCP -j QUEUE
编译和执行为

$ gcc test.c -lipq
$ sudo ./a.out
源代码

#include <netinet/in.h> 
#include <linux/netfilter.h> 
#include <libipq.h> 
#include <stdio.h> 
#include <stdlib.h>


#define BUFSIZE 2048
static void die(struct ipq_handle *h)
{
    ipq_perror("passer");
    ipq_destroy_handle(h);
    exit(1);
}
int main(int argc, char **argv)
{
    int status, i=0;
    unsigned char buf[BUFSIZE];
    struct ipq_handle *h;
    h = ipq_create_handle(0, NFPROTO_IPV4);

    if (!h)     die(h);

    status = ipq_set_mode(h, IPQ_COPY_PACKET, BUFSIZE);

    if (status < 0) die(h);

    do{
        i++;
        status = ipq_read(h, buf, BUFSIZE, 0);

        if (status < 0) die(h);

        switch (ipq_message_type(buf)) {
            case NLMSG_ERROR:
                fprintf(stderr, "Received error message %d\n",
                ipq_get_msgerr(buf));
                break;
            case IPQM_PACKET:
            {
                ipq_packet_msg_t *m = ipq_get_packet(buf);
                printf("\nReceived Packet");
                /****YOUR CODE TO MODIFY PACKET GOES HERE****/
                status = ipq_set_verdict(h, m->packet_id, NF_ACCEPT, 0, NULL);
                if (status < 0)  die(h);
                break;
            }
            default:
                fprintf(stderr, "Unknown message type!\n");
                break;
        }
    } while (1);
    ipq_destroy_handle(h);
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义bufsize2048
静态空心模具(结构ipq_手柄*h)
{
ipq_perror(“通行证”);
ipq_销毁_手柄(h);
出口(1);
}
int main(int argc,字符**argv)
{
int状态,i=0;
无符号字符buf[BUFSIZE];
结构ipq_句柄*h;
h=ipq_创建_句柄(0,NFPROTO_IPV4);
如果(!h)死亡(h);
状态=ipq_设置_模式(h,ipq_复制_数据包,BUFSIZE);
如果(状态<0)死亡(h);
做{
i++;
状态=ipq_读取(h、buf、BUFSIZE、0);
如果(状态<0)死亡(h);
开关(ipq_消息_类型(buf)){
案例NLMSG_错误:
fprintf(标准,“收到错误消息%d\n”,
ipq_get_msgerr(buf));
打破
案例IPQM_数据包:
{
ipq\u数据包\u msg\u t*m=ipq\u get\u数据包(buf);
printf(“\n收到的数据包”);
/****修改数据包的代码在这里****/
状态=ipq\u集合\u判定(h,m->packet\u id,NF\u ACCEPT,0,NULL);
如果(状态<0)死亡(h);
打破
}
违约:
fprintf(stderr,“未知消息类型!\n”);
打破
}
}而(1),;
ipq_销毁_手柄(h);
返回0;
}

您尝试过什么?我什么也没试过因为我找不到你试过什么?()我什么也没试过,因为我找不到任何东西。有人可以共享任何显示如何使用nf_reinject函数和netfilter挂钩的链接吗?有人可以共享任何显示如何使用nf_reinject函数和netfilter挂钩的链接吗?