2010-05-24 23 views
1

我不知道如何解決我的問題。Lipipq(iptables)。如何使用iptables隊列將捕獲的數據包重定向到另一個地址?

是否有可能使用ipq_set_verdict()重定向捕獲的數據包?

我想將未經授權的用戶重定向到登錄頁面。

請看看我的代碼:

的數據包被接受,我的瀏覽器中打開請求的頁面(不改變目的地地址)

無效的主要(){ 結構ipq_handle *小時;

if (!(h = ipq_create_handle(0, PF_INET))) { 
    //error 
    return; 
}; 

if ((ipq_set_mode(h, IPQ_COPY_PACKET, BUFSIZE)) < 0) { 
    // error 
    return; 
}; 


struct iphdr* ip_pack; 
ipq_packet_msg_t* eth_pack; 

__u32 ip_auth_server = inet_addr("192.168.177.1"); 
unsigned char buf[68000]; 

if ((ipq_read(h, buf, BUFSIZE, 0)) < 0) { 

    //error 
    return; 

} 


switch (ipq_message_type(buf)) { 
    case NLMSG_ERROR: 
     //error 
     break; 

    case IPQM_PACKET: 
    { 

     eth_pack = ipq_get_packet(buf); 
     ip_pack = (struct iphdr*) eth_pack->payload; 
     redirect_server_auth(ip_auth_server, ip_pack, eth_pack->packet_id, h, eth_pack->payload, eth_pack->data_len); 

    } 


} 

u_int16_t ip_checksum(u_int32_t init, const u_int8_t* buf, size_t len) { 
    u_int32_t sum = init; 
    u_int16_t* shorts = (u_int16_t*) buf; 

    while (len > 1) { 
     sum += *shorts++; 
     len -= 2; 
    } 

    if (len == 1) 
     sum += *((u_int8_t*) shorts); 

    while (sum >> 16) 
     sum = (sum >> 16) + (sum & 0xFFFF); 

    return ~sum; 
} 

u_int16_t tcp_checksum(const struct iphdr* iph, const struct tcphdr* tcph, 
     size_t len) { 
    u_int32_t cksum = 0; 

    cksum += (iph->saddr >> 16) & 0x0000ffff; 
    cksum += iph->saddr & 0x0000ffff; 
    cksum += (iph->daddr >> 16) & 0x0000ffff; 
    cksum += iph->daddr & 0x0000ffff; 
    cksum += htons(iph->protocol & 0x00ff); 
    cksum += htons(len); 
    return ip_checksum(cksum, (unsigned char*) tcph, len); 
} 

void handle_packet(unsigned char* pkt, size_t len, __u32 dest_addr, int port_dest) { 
    struct iphdr* iph = (struct iphdr*) pkt; 
    struct tcphdr* tcph = (struct tcphdr*) (pkt + iph->ihl * 4); 


    iph->daddr = dest_addr; 
    //  syslog(LOG_DEBUG, "PORT"); 
    //syslog(LOG_DEBUG, tcph->dest); //tcph->dest = htons(80); 

    iph->check = 0; 
    iph->check = ip_checksum(0, pkt, iph->ihl * 4); 
    tcph->check = 0; 
    tcph->check = tcp_checksum(iph, tcph, len - (iph->ihl * 4)); 

} 

void redirect_server_auth(__u32* ip_srv, struct iphdr* ip_pack, 
     unsigned long packet_id, ipq_handle* handle, 
     unsigned char* buf, int size_buf) { 

    syslog(LOG_DEBUG, "RENAME IP AND SEND PACK ACCEPT %s", inet_ntoa(
      *((struct in_addr*) & ip_pack->daddr))); 


    handle_packet(buf, size_buf, *ip_srv, 80); 


    syslog(LOG_DEBUG, "RENAME IP AND SEND PACK ACCEPT %s", inet_ntoa(

      *((struct in_addr*) & ip_pack->daddr))); 

    int v = ipq_set_verdict(handle, packet_id, NF_ACCEPT, size_buf, buf); 

    if (v < 0) { 
     syslog(LOG_DEBUG, "problems"); 
     syslog(LOG_DEBUG, ipq_errstr()); 
    } 

} 

我期待着您的幫助。謝謝你的時間。

回答

0

首先,libipq被認爲已被棄用,改爲使用libnefilter_queue。

然後它是不可能重定向與set_verdict的數據包,從我得到你只能使用DROP ACCEPT或REJECT判決。但是您可以使用nfq_set_verdict2()修改數據有效負載/標頭。這似乎是你已經在做的事情。

我不是100%確定要做什麼:將瀏覽器重定向到另一個(登錄)網頁,如果它未經過身份驗證?

如果是這樣,您應該發出一個包含認證位置的301 HTTP答案,而不是修改數據包目的地。在這種情況下,你應該在應用層做到這一點。

但我還是不知道你actualy想要做什麼做什麼,你就可以更準確的瞭解您的系統(誰和如何驗證?什麼時候?你怎麼檢查,如果客戶端已經通過身份驗證?)

相關問題