2014-04-16 37 views
2

我在想,如果有使用iptables/netfilter的複製數據包,將其更改並交付到應用程序的方式。基本上,我想從一個流中捕獲一個數據包並將其重定向到某個隊列,然後我想複製它,爲它發出判決(我知道如何在C中執行此部分),然後我需要改變複製版本中的內容,並對該「修改」的數據包發出判決。iptables的netfilter的複製數據包

基本上我想要應用程序接收這兩個未修改和修改後的版本。

這可能嗎? 在此先感謝您的幫助。

回答

1

您的任務可以通過libipq庫實現。下面的教程就像專注於修改&修改用戶空間中的數據包一樣。

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.205.2605&rep=rep1&type=pdf

您需要了解C進行這項工作。或者,可以使用「Scapy」 - 一種基於python的數據包捕捉工具。

#include <linux/netfilter.h> 
#include <libipq.h> 

/* 
* Used to open packet ; Insert a iptables rule to get packet here 
* iptables -I 1 [INPUT|OUTPUT|FORWARD] <packet header match> -j QUEUE 
*/ 

#include <linux/netfilter.h> 
#include <libipq.h> 
#include <stdio.h> 
#define BUFSIZE 2048 
static void die(struct ipq_handle *h) 
{  
    ipq_destroy_handle(h); 
    exit(1); 
} 
int main(int argc, char **argv) 
{ 
    int status; 
    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{ 
     status = ipq_read(h, buf, BUFSIZE, 0); 
     if (status < 0) 
      die(h); 
     if (ipq_message_type(buf) == IPQM_PACKET){ 
      ipq_packet_msg_t *m = ipq_get_packet(buf); 
      status = ipq_set_verdict(h, m->packet_id, NF_ACCEPT, 0, NULL);   

     }      

    } while (1); 
     ipq_destroy_handle(h); 
    return 0; 
} 
相關問題