2012-04-29 71 views
0

任何人都可以告诉我如何通过使用Netfilter钩子的linux模块修改数据包数据?用于修改数据包的Linux模块(Netfilter)

谢谢!

+0

你有什么试过吗? (http://mattgemmell.com/2008/12/08/what-have-you-tried/) – 2012-04-29 13:47:28

+0

我什么也没试,因为我找不到东西 – user1262425 2012-04-29 15:18:58

回答

2

没有必要编写自己的netfilter模块。你可以从用户空间使用来自iptables的QUEUE目标,并编写一个处理队列的守护程序。

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

1

试试下面的程序

写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; 
}