如何在linux上拦截IP数据包

时间:2013-11-09 23:16:22

标签: linux ip intercept packets

我正在尝试在Linux上实现这一目标(不确定它是否可行以及如果是这样): 我需要编写一个程序来拦截接口上的所有IP数据包,并将此数据包传递给一组特定的用户空间程序。通过拦截我的意思是,我的程序(可能是内核模块或特殊的用户空间程序)捕获IP数据包,然后该数据包不再通过IP堆栈。例如,假设操作系统正在运行许多进程(在内核空间或用户空间中),A,B,C,D ......等。如果在接口处收到IP数据包,请说eth2 ,我只想要A,B看到这个数据包,所有其他进程甚至都知道这个数据包的存在。 有人能让我走向正确的方向吗?非常感谢!

1 个答案:

答案 0 :(得分:2)

我建议您重新检查是否真的有必要以您描述的方式拦截数据包。听起来,如上所述,您还没有完全理解网络的运作方式。

首先,除非您的程序以某种方式神奇地设法读取原始网络数据包而不是使用标准套接字,否则它们无法接收相互之间的流量。每个套接字都有一个与之关联的端口,只有一个进程可以绑定到同一主机的同一个端口上(套接字实际上只是一对端口和主机地址)。

如果您实际上正在程序中读取原始网络数据包并且这是必要的,那么您很可能不应该在同一主机上运行它们。而是使用虚拟化来放置不允许在不同虚拟主机中看到彼此发往的数据包的程序,从而将它们完全分开,而不是使用相当复杂的程序化解决方案。

如果其他一切都失败了,您可能需要仔细查看libpcap,它允许您捕获网络数据包,甚至可以同时从多个程序中捕获。尽管你必须以root身份运行它们,但无论如何这对于能够将网络接口置于混杂模式等等都是必要的。这是一个从网络中读取一些东西的简单示例,您可以从libpcap主页和相关联的

中找到更多信息。
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pcap.h>

/* IP header (from tcpdump examples) */
struct sniff_ip {
    u_char ip_vhl;          /* version << 4 | header length >> 2 */
    u_char ip_tos;          /* type of service */
    u_short ip_len;         /* total length */
    u_short ip_id;          /* identification */
    u_short ip_off;         /* fragment offset field */
    u_char ip_ttl;          /* time to live */
    u_char ip_p;            /* protocol */
    u_short ip_sum;         /* checksum */
    struct in_addr ip_src, ip_dst;  /* source and dest address */
};

/* callback function for pcap_loop */
void cllbck(u_char * args,
        const struct pcap_pkthdr *hdr, const u_char * pkt)
{
    const struct sniff_ip *ip = (struct sniff_ip *) (pkt + 14);

    fprintf(stderr, "Sniffed a packet with length %d.\n", hdr->len);
    fprintf(stderr, "IP version %d.\n", ip->ip_vhl >> 4);
}

int main(int argc, char *argv[])
{
    char *dev;                  /* device name */
    char errbuf[PCAP_ERRBUF_SIZE];      /* buffer for libpcap errmsgs */
    pcap_t *cap;                /* libpcap capture session */
    char *filt = "host 127.0.0.1";      /* capture filter */
    struct bpf_program fp;      /* compiled filter */
    struct pcap_pkthdr hdr;     /* packet header from libpcap */
    const u_char *pkt;          /* packet from libpcap */

    dev = strdup(argv[1]);
    if (dev == NULL) {
        fprintf(stderr, "Invalid device.\n");
        return 2;
    }

    /* open the device for live capture */
    cap = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
    if (cap == NULL) {
        fprintf(stderr, "Opening device `%s´ failed: %s\n", dev, errbuf);
        return 2;
    }

    /* compile the capture filter */
    if (pcap_compile(cap, &fp, filt, 0, PCAP_NETMASK_UNKNOWN) < 0) {
        fprintf(stderr, "Failed to parse filter `%s´: %s\n",
                filt, pcap_geterr(cap));
        return 2;
    }
    /* set the filter active for this session */
    if (pcap_setfilter(cap, &fp) == -1) {
        fprintf(stderr, "Couldn't install filter %s: %s\n",
                filt, pcap_geterr(cap));
        return 2;
    }

    /* pcap close will loop until an error if 2nd arg is < 0 */
    pcap_loop(cap, -1, cllbck, NULL);

    /* end session, pcap_loop has exited ie. an error has occurred */
    pcap_close(cap);

    return 0;
}

/* end of file */