• 学习使用WINPCAP(一)


    一、一些需要知道的细节描述(前言):

    这一部分展示了如何使用WINPCAP-API的不同的功能,它作为一个使用指南被划分为一系列的课时来带领读者循序渐进的体会PCAP的程序设计的

    魅力:从简单的基本功能(如获取网卡的列表,数据包的捕获等)到统计和收集网络流量等高级功能。

    在这里将提供一些简单但完整的代码作为参考:所有的这些原代码都有和它相关的详细信息的连接以便单击这些功能和数据结构时能够即使跳转到相关的文献。

    这些例子是用C语言写的,所以在学习之前首先要有一定的C语言的基础,当然PCAP作为一个网络底层的驱动,要想学好它也必须具备一定的网络方面的知识。



    (一)得到网络驱动列表

    用PCAP写应用程序的第一件事往往就是要获得本地的网卡列表。PCAP提供了pcap_findalldevs()这个函数来实现此功能,这个API返回一个pcap_if结构的连表,连表的每项内容都含有全面的网卡信息:尤其是字段名字和含有名字的描述以及有关驱动器的易读信息。

    得到网络驱动列表的程序如下:

    #include "pcap.h"

    main()
    {
        pcap_if_t *alldevs;
        pcap_if_t *d;
        int i=0;
        char errbuf[PCAP_ERRBUF_SIZE];

        /* 这个API用来获得网卡 的列表 */
    if (pcap_findalldevs(&alldevs, errbuf) == -1)
        {
            fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
            exit(1);
        }

        /* 显示列表的响应字段的内容 */
    for(d=alldevs;d;d=d->next)
        {
            printf("%d. %s", ++i, d->name);
            if (d->description)
                printf(" (%s)\n", d->description);
            else            printf(" (No description available)\n");
        }

        if(i==0)
        {
            printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
            return;
        }

        /* We don't need any more the device list. Free it */
        pcap_freealldevs(alldevs);
    }



    有关这段程序的一些说明:
    首先pcap_findalldevs()同其他的libpca函数一样有一个errbuf参数,当有异常情况发生时,这个参数会被PCAP填充为某个特定的错误字串。

    再次,UNIX也同样提供pcap_findalldevs()这个函数,但是请注意并非所有的系统都支持libpcap提供的网络程序接口。所以我门要想写出合适

    的程序就必须考虑到这些情况(系统不能够返回一些字段的描述信息),在这种情况下我门应该给出类似"No description available"这样的

    提示。

    最后结束时别忘了用pcap_freealldevs()释放掉内存资源。




    二、获得已安装网络驱动器的高级信息

    在第一章中演示了如何获得已存在适配器的静态信息。实际上WinPcap同样也提供其他的高级信息,特别是 pcap_findalldevs()这个函数返回的每个 pcap_if结构体都同样包含一个pcap_addr结构的列表,他包含:
    一个地址列表,一个掩码列表,一个广播地址列表和一个目的地址列表。
    下面的例子通过一个ifprint()函数打印出了pcap_if结构的的所有字段信息,该程序对每一个pcap_findalldevs()所返回的pcap_if结构循环调用ifprint()来显示详细的字段信息。


    #include "pcap.h"
    #ifndef WIN32
    #include <sys/socket.h>
    #include <netinet/in.h>
    #else
    #include <winsock.h>
    #endif

    void ifprint(pcap_if_t *d);
    char *iptos(u_long in);

    int main()
    {
      pcap_if_t *alldevs;
      pcap_if_t *d;
      char errbuf[PCAP_ERRBUF_SIZE+1];

      /* 获得网卡的列表 */
    if (pcap_findalldevs(&alldevs, errbuf) == -1)
      {
        fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
        exit(1);
      }

      /* 循环调用ifprint() 来显示pcap_if结构的信息*/
    for(d=alldevs;d;d=d->next)
      {
        ifprint(d);
      }

      return 1;
    }

    /* Print all the available information on the given interface */
    void ifprint(pcap_if_t *d)
    {
      pcap_addr_t *a;

      /* Name */
      printf("%s\n",d->name);

      /* Description */
      if (d->description)
        printf("\tDescription: %s\n",d->description);

      /* Loopback Address*/
      printf("\tLoopback: %s\n",(d->flags & PCAP_IF_LOOPBACK)?"yes":"no");

      /* IP addresses */
      for(a=d->addresses;a;a=a->next) {
        printf("\tAddress Family: #%d\n",a->addr->sa_family);

      /*关于 sockaddr_in 结构请参考其他的网络编程书*/
    switch(a->addr->sa_family)
        {
          case AF_INET:
            printf("\tAddress Family Name: AF_INET\n");//打印网络地址类型
    if (a->addr)//打印IP地址
    printf("\tAddress: %s\n",iptos(((struct sockaddr_in *)a->addr)->sin_addr.s_addr));
            if (a->netmask)//打印掩码
    printf("\tNetmask: %s\n",iptos(((struct sockaddr_in *)a->netmask)->sin_addr.s_addr));
            if (a->broadaddr)//打印广播地址
    printf("\tBroadcast Address: %s\n",iptos(((struct sockaddr_in *)a->broadaddr)->sin_addr.s_addr));
            if (a->dstaddr)//目的地址
    printf("\tDestination Address: %s\n",iptos(((struct sockaddr_in *)a->dstaddr)->sin_addr.s_addr));
            break;
          default:
            printf("\tAddress Family Name: Unknown\n");
            break;
        }
      }
      printf("\n");
    }

    /*  将一个unsigned long 型的IP转换为字符串类型的IP */
    #define IPTOSBUFFERS    12
    char *iptos(u_long in)
    {
        static char output[IPTOSBUFFERS][3*4+3+1];
        static short which;
        u_char *p;

        p = (u_char *)&in;
        which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
        sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
        return output[which];
    }


    三、打开网卡捕获数据包

    现在我门已经知道了如何去获得网卡的信息现在就让我们开始真正的工作:打开网卡并捕获数据流。在这一节

    里我们将写一个打印流经网络的每个数据包信息的程序。打开网卡的功能是通过pcap_open_live()来实现的它

    有三个参数snaplen  promisc  to_ms。

    snaplen用于指定所捕获包的特定部分,在一些系统上(象xBSD and Win32等)驱动只给出所捕获数据包

    的一部分而不是全部,这样就减少了拷贝数据的数量从而提高了包捕获的效率。

    promisc指明网卡处于混杂模式,在正常情况下网卡只接受去往它的包而去往其他主机的数据包则被忽略

    。相反当网卡处于混杂 模式时他将接收所有的流经它的数据包:这就意味着在共享介质的情况下我门可以捕获

    到其它主机的数据包。大部分的包捕获程序都将混杂模式设为默认,所有我们在下面的例子里也将网卡设为混杂模式。

    to_ms 参数指定读数据的超时控制,超时以毫秒计算。当在超时时间内网卡上没有数据到来时对网卡的读

    操作将返回(如pcap_dispatch() or pcap_next_ex()等函数)。还有,如果网卡处于统计模式下(请查看“统计和收集网络数据流一节”)to_ms还定义了统计的时间间隔。如果该参数为0那么意味着没有超时控制,对网卡的读操作在没有数据到来是将永远堵塞。如果为-1那么对网卡的读操作将立即返回不管有没有数据可读。



    #include "pcap.h"

    /* prototype of the packet handler */
    void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);

    main()
    {
        pcap_if_t *alldevs;
        pcap_if_t *d;
        int inum;
        int i=0;
        pcap_t *adhandle;
        char errbuf[PCAP_ERRBUF_SIZE];

        /* 获得网卡的列表 */
    if (pcap_findalldevs(&alldevs, errbuf) == -1)
        {
            fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
            exit(1);
        }

        /* 打印网卡信息 */
    for(d=alldevs; d; d=d->next)
        {
            printf("%d. %s", ++i, d->name);
            if (d->description)
                printf(" (%s)\n", d->description);
            else
                printf(" (No description available)\n");
        }

        if(i==0)
        {
            printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
            return -1;
        }

        printf("Enter the interface number (1-%d):",i);
        scanf("%d", &inum);                      //输入要选择打开的网卡号

    if(inum < 1 || inum > i)  //判断号的合法性
    {
    printf("\nInterface number out of range.\n");
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return -1;
        }

        /* 找到要选择的网卡结构 */
    for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);

        /* 打开选择的网卡 */
    if ( (adhandle= pcap_open_live(d->name, // 设备名称
    65536,     // portion of the packet to capture.
                                 // 65536 grants that the whole packet will be captured on all the

    MACs.
                                 1,         // 混杂模式
    1000,      // 读超时为1秒
    errbuf     // error buffer
                                 ) ) == NULL)
        {
            fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return -1;
        }

        printf("\nlistening on %s...\n", d->description);

        /* At this point, we don't need any more the device list. Free it */
        pcap_freealldevs(alldevs);

        /* 开始捕获包 */
    pcap_loop(adhandle, 0, packet_handler, NULL);

        return 0;
    }


    /* 对每一个到来的数据包调用该函数 */
    void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
    {
        struct tm *ltime;
        char timestr[16];

        /* 将时间戳转变为易读的标准格式*/
    ltime=localtime(&header->ts.tv_sec);
        strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);

        printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);

    }

    一旦网卡被打开,旧可以调用pcap_dispatch() 或pcap_loop()进行数据的捕获,这两个函数的功能十分相似不

    同的是pcap_ dispatch()可以不被阻塞,而pcap_loop()在没有数据流到达时将阻塞。在这个简单的例子里用

    pcap_loop()就足够了,而在一些复杂的程序里往往用pcap_dispatch()。
    Once the adapter is opened, the capture can be started with pcap_dispatch() or pcap_loop(). These

    two functions are very similar, the difference is that pcap_ dispatch() is granted to return when

    the expires while pcap_loop() doesn't return until cnt packets have been captured, so it can

    block for an arbitrary period on a few utilized network. pcap_loop() is enough for the purpose of

    this sample, while pcap_dispatch() is normally used in more complex program.
    这两个函数都有返回的参数,一个指向某个函数(该函数用来接受数据如该程序中的packet_handler)的指针
    ,libpcap调用该函数对每个从网上到来的数据包进行处理和接收数据包。另一个参数是带有时间戳和包长等信

    息的头部,最后一个是含有所有协议头部数据报的实际数据。注意MAC的冗余校验码一般不出现,因为当一个桢

    到达并被确认后网卡就把它删除了,同样需要注意的是大多数网卡会丢掉冗余码出错的数据包,所以WinPcap一

    般不能够捕获这些出错的数据报。

    刚才的例子里从pcap_pkthdr中提取出了每个数据报的时间戳和长度并在显示器上打印出了他们。



    四、不用loopback捕获数据报
    这节的例子很象先前的一章(获得网卡的高级信息)但是这一节中是用pcap_next_ex()来代替pcap_loop()来捕

    获数据包。基于回调包捕获机制的pcap_loop()在某些情况下是不错的选择。但是在一些情况下处理回调并不特

    别好:这会使程序变的复杂并且在象多线程或C++类这些情况下它看起来到象一块绊脚石。

    在这些情况下pcap_next_ex()允许直接调用来接收包,它的参数和pcap_loop()相同:有一个网卡描述副,和两

    个指针,这两个指针会被初始化并返回给用户,一个是pcap_pkthdr结构,另一个是接收数据的缓冲区。

    下面的程序我门将循环调用前一节的例子中的回掉部分,只是把它移到了main里面了。



    #include "pcap.h"


    main()
    {
        pcap_if_t *alldevs;
        pcap_if_t *d;
        int inum;
        int i=0;
        pcap_t *adhandle;
        int res;
        char errbuf[PCAP_ERRBUF_SIZE];
        struct tm *ltime;
        char timestr[16];
        struct pcap_pkthdr *header;
        u_char *pkt_data;


        /* Retrieve the device list */
        if (pcap_findalldevs(&alldevs, errbuf) == -1)
        {
            fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
            exit(1);
        }

        /* Print the list */
        for(d=alldevs; d; d=d->next)
        {
            printf("%d. %s", ++i, d->name);
            if (d->description)
                printf(" (%s)\n", d->description);
            else
                printf(" (No description available)\n");
        }

        if(i==0)
        {
            printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
            return -1;
        }

        printf("Enter the interface number (1-%d):",i);
        scanf("%d", &inum);

        if(inum < 1 || inum > i)
        {
            printf("\nInterface number out of range.\n");
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return -1;
        }

        /* Jump to the selected adapter */
        for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);

        /* Open the adapter */
        if ( (adhandle= pcap_open_live(d->name, // name of the device
            65536,     // portion of the packet to capture.
            // 65536 grants that the whole packet will be captured on all the MACs.
            1,         // promiscuous mode
            1000,      // read timeout
            errbuf     // error buffer
            ) ) == NULL)
        {
            fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return -1;
        }

        printf("\nlistening on %s...\n", d->description);

        /* At this point, we don't need any more the device list. Free it */
        pcap_freealldevs(alldevs);

        /* 此处循环调用 pcap_next_ex来接受数据报*/
    while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0){

            if(res == 0)
                /* Timeout elapsed */
                continue;

            /* convert the timestamp to readable format */
            ltime=localtime(&header->ts.tv_sec);
            strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);

            printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);
        }

        if(res == -1){
            printf("Error reading the packets: %s\n", pcap_geterr(adhandle));
            return -1;
        }

        return 0;
    }


    注:pcap_next_ex()只在Win32环境下才行因为它不是原始libpcap API中的一部分,这就意味着依赖于这个函

    数的代码不会在UNIX下工作。那么为什么我们用pcap_next_ex()而不用pcap_next()?因为pcap_next()有许多

    限制在很多情况下并不鼓励用它。首先它的效率很低因为它隐藏了回掉方法并且还依赖于pcap_dispatch()这个

    函数。再次它不能够识别文件结束标志EOF所以对来自文件的数据流它几乎无能为力。
    注意当pcap_next_ex()在成功,超时,出错和文件结束的情况下会返回不同的值。


    五、数据流的过滤

    WinPcap或libpca最强大的特点之一就是数据流的过滤引擎。它提供一种高效的方法来只捕获网络数据流的某些数据而且常常和系统的捕获机制相集成。过滤数据的函数是pcap_compile() 和 pcap_setfilter()来实现的。


    pcap_compile()来编译一个过滤设备,它通过一个高层的boolean型变量和字串产生一系列的能够被底层驱动所解释的二进制编码。boolean表示语法能够在这个文件的过滤表示语法中找到。

    pcap_setfilter() 用来联系一个在内核驱动上过滤的过滤器,这时所有网络数据包都将流经过滤器,并拷贝到应用程序中。

    下面的代码展示了如何编译并社定一个过滤设备。注意我们必须从pcap_if结构中获得掩码,因为一些过滤器的创建需要这个参数。

    下面的代码段中的pcap_compile()的"ip and tcp"参数说明只有IPV4和TCP数据才会被内核保存并被传递到应用程序。



    if(d->addresses != NULL)
            /* 获得第一个接口地址的掩码 */
    netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
        else
            /* 如果这个接口没有地址那么我们假设他为C类地址 */
    netmask=0xffffff;


        //compile the filter
        if(pcap_compile(adhandle, &fcode, "ip and tcp", 1, netmask) <0 ){
            fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return -1;
        }

        //set the filter
        if(pcap_setfilter(adhandle, &fcode)<0){
            fprintf(stderr,"\nError setting the filter.\n");
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return -1;
        }

    如何你想进一步查看本节中用过滤器过滤数据流的例子可以查看下一节《数据的解包》。


    六、解析数据包现在经过上几节的学习能够进行数据报的捕获和过滤了,我们想用一个简单的"real world"程序将我们所学的

    知识应用于实际。
    这一节里我们将利用以前的代码并将其引申从而建立一个更实用的程序。该程序的主要目的是如何显示出所捕

    获的数据报的内容,尤其是对它的协议头的分析和说明。这个程序名叫UDPdump它将在屏幕上显示出我们网络上

    UDP数据的信息。
    在此我们选择解析UDP而不用TCP因为他比TCP简单更加的直观明了。下面让我们来看看原代码。




    /*
    * Copyright (c) 1999 - 2002
    *  Politecnico di Torino.  All rights reserved.
    *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that: (1) source code distributions
    * retain the above copyright notice and this paragraph in its entirety, (2)
    * distributions including binary code include the above copyright notice and
    * this paragraph in its entirety in the documentation or other materials
    * provided with the distribution, and (3) all advertising materials mentioning
    * features or use of this software display the following acknowledgement:
    * ``This product includes software developed by the Politecnico
    * di Torino, and its contributors.'' Neither the name of
    * the University nor the names of its contributors may be used to endorse
    * or promote products derived from this software without specific prior
    * written permission.
    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
    * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
    * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    */

    #include "pcap.h"

    /* 4 BIT的IP头定义 */
    typedef struct ip_address{
        u_char byte1;
        u_char byte2;
        u_char byte3;
        u_char byte4;
    }ip_address;

    /* IPv4 头的定义 */
    typedef struct ip_header{
        u_char  ver_ihl;        // 4 bit的版本信息 + 4 bits的头长
    u_char  tos;            // TOS类型
    u_short tlen;           // 总长度
    u_short identification; // Identification
        u_short flags_fo;       // Flags (3 bits) + Fragment offset (13 bits)
        u_char  ttl;            // 生存期
    u_char  proto;          // 后面的协议信息
    u_short crc;            // 校验和
    ip_address  saddr;      // 源IP
        ip_address  daddr;      // 目的IP
        u_int   op_pad;         // Option + Padding
    }ip_header;

    /* UDP header*/
    typedef struct udp_header{
        u_short sport;          // Source port
        u_short dport;          // Destination port
        u_short len;            // Datagram length
        u_short crc;            // Checksum
    }udp_header;

    /* 定义处理包的函数 */
    void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);


    main()
    {
        pcap_if_t *alldevs;
        pcap_if_t *d;
        int inum;
        int i=0;
        pcap_t *adhandle;
        char errbuf[PCAP_ERRBUF_SIZE];
        u_int netmask;
        char packet_filter[] = "ip and udp";
        struct bpf_program fcode;

        /* Retrieve the device list */
        if (pcap_findalldevs(&alldevs, errbuf) == -1)
        {
            fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
            exit(1);
        }

        /* Print the list */
        for(d=alldevs; d; d=d->next)
        {
            printf("%d. %s", ++i, d->name);
            if (d->description)
                printf(" (%s)\n", d->description);
            else
                printf(" (No description available)\n");
        }

        if(i==0)
        {
            printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
            return -1;
        }

        printf("Enter the interface number (1-%d):",i);
        scanf("%d", &inum);

        if(inum < 1 || inum > i)
        {
            printf("\nInterface number out of range.\n");
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return -1;
        }

        /* Jump to the selected adapter */
        for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);

        /* Open the adapter */
        if ( (adhandle= pcap_open_live(d->name, // name of the device
                                 65536,     // portion of the packet to capture.
                                            // 65536 grants that the whole packet will be captured on

    all the MACs.
                                 1,         // promiscuous mode
                                 1000,      // read timeout
                                 errbuf     // error buffer
                                 ) ) == NULL)
        {
            fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return -1;
        }

        /* Check the link layer. We support only Ethernet for simplicity. */
        if(pcap_datalink(adhandle) != DLT_EN10MB)
        {
            fprintf(stderr,"\nThis program works only on Ethernet networks.\n");
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return -1;
        }

        if(d->addresses != NULL)
            /* Retrieve the mask of the first address of the interface */
            netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
        else
            /* If the interface is without addresses we suppose to be in a C class network */
            netmask=0xffffff;


        //compile the filter
        if(pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) <0 ){
            fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return -1;
        }

        //set the filter
        if(pcap_setfilter(adhandle, &fcode)<0){
            fprintf(stderr,"\nError setting the filter.\n");
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return -1;
        }

        printf("\nlistening on %s...\n", d->description);

        /* At this point, we don't need any more the device list. Free it */
        pcap_freealldevs(alldevs);

        /* start the capture */
        pcap_loop(adhandle, 0, packet_handler, NULL);

        return 0;
    }

    /* Callback function invoked by libpcap for every incoming packet */
    void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
    {
        struct tm *ltime;
        char timestr[16];
        ip_header *ih;
        udp_header *uh;
        u_int ip_len;
        u_short sport,dport;

        /* convert the timestamp to readable format */
        ltime=localtime(&header->ts.tv_sec);
        strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);

        /* print timestamp and length of the packet */
        printf("%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);

        /* 找到IP头的位置 */
    ih = (ip_header *) (pkt_data +
            14); //14为以太头的长度

    /* 找到UDP的位置 */
    ip_len = (ih->ver_ihl & 0xf) * 4;
        uh = (udp_header *) ((u_char*)ih + ip_len);

        /* 将端口信息从网络型转变为主机顺序 */
    sport = ntohs( uh->sport );
        dport = ntohs( uh->dport );

        /* print ip addresses and udp ports */
        printf("%d.%d.%d.%d.%d -> %d.%d.%d.%d.%d\n",
            ih->saddr.byte1,
            ih->saddr.byte2,
            ih->saddr.byte3,
            ih->saddr.byte4,
            sport,
            ih->daddr.byte1,
            ih->daddr.byte2,
            ih->daddr.byte3,
            ih->daddr.byte4,
            dport);
    }

    首先我门设置UDP过滤,用这种方法我们确保packet_handler()只接受到基于IPV4的UDP数据。我们同样定义了

    两个数据结构来描述IP 和UDP的头部信息,packet_handler()用这两个结构来定位头部的各种字段。
    packet_handler()虽然只是限于处理一些UDP数据但却显示了复杂的嗅探器如tcpdump/WinDump的工作原理。
    首先我们对MAC地址的头部并不感兴趣所以我们跳过它。不过在开始捕获之前我们用pcap_datalink()来检查MAC

    层,所以以上的程序只能够工作在Ethernet networks上,再次我们确保MAC头为14 bytes。
    MAC头之后是IP头,我们从中提取出了目的地址。IP之后是UDP,在确定UDP的位置时有点复杂,因为IP的长度以

    为版本的不同而不同,所以我们用头长字段来定位UDP,一旦 我们确定了UDP的起始位置,我们就可以解析出原

    和目的端口。
    下面是我们打印出来的一些结果:

    1. {A7FD048A-5D4B-478E-B3C1-34401AC3B72F} (Xircom t 10/100 Adapter)
    Enter the interface number (1-2):1

    listening on Xircom CardBus Ethernet 10/100 Adapter...
    16:13:15.312784 len:87 130.192.31.67.2682 -> 130.192.3.21.53
    16:13:15.314796 len:137 130.192.3.21.53 -> 130.192.31.67.2682
    16:13:15.322101 len:78 130.192.31.67.2683 -> 130.192.3.21.53

    上面每一行都显示出不同的数据包的内容.



    七、处理脱机的堆文件

    通过以前的学习我门已经熟悉了从网卡上捕获数据包,现在我门将学习如何处理数据包。WINPCAP为我们提供了很多API来将流经网络的数据包保存到一个堆文件并读取堆的内容。这一节将讲述如何使用所有的这些API。
    这种文件的格式很简单,但包含了所捕获的数据报的二进制内容,这种文件格式也是很多网络工具的标准如WinDump, Ethereal 还有 Snort等.


    关于如何将数据包保存到文件:

    首先我们看看如何以LIBPCAP的格式写数据包。
    下面的例子演示了如何从指定的接口上捕获数据包并将它们存储到一个指定的文件。


    #include "pcap.h"

    /* 定义处理数据的函数原形 */
    void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);

    main(int argc, char **argv)
    {
        pcap_if_t *alldevs;
        pcap_if_t *d;
        int inum;
        int i=0;
        pcap_t *adhandle;//定义文件句柄
    char errbuf[PCAP_ERRBUF_SIZE];
        pcap_dumper_t *dumpfile;



        /* 检查命令行参数 是否带有文件名*/
    if(argc != 2){

            printf("usage: %s filename", argv[0]);
            return -1;

        }

        /* 获得驱动列表 */
    if (pcap_findalldevs(&alldevs, errbuf) == -1)
        {
            fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
            exit(1);
        }

        /* 打印 list */
        for(d=alldevs; d; d=d->next)
        {
            printf("%d. %s", ++i, d->name);
            if (d->description)
                printf(" (%s)\n", d->description);
            else
                printf(" (No description available)\n");
        }

        if(i==0)
        {
            printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
            return -1;
        }

        printf("Enter the interface number (1-%d):",i);
        scanf("%d", &inum);

        if(inum < 1 || inum > i)
        {
            printf("\nInterface number out of range.\n");
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return -1;
        }

        /* 跳转到指定的网卡 */
    for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);

        /* Open the adapter */
        if ( (adhandle = pcap_open_live(d->name, // name of the device
                                 65536,     // portion of the packet to capture.
                                            // 65536 grants that the whole packet will be captured on all the MACs.
                                 1,         // promiscuous mode
                                 1000,      // read timeout
                                 errbuf     // error buffer
                                 ) ) == NULL)
        {
            fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
            /* Free the device list */
            pcap_freealldevs(alldevs);
            return -1;
        }

        /* 打开文件 */
    dumpfile = pcap_dump_open(adhandle, argv[1]);
        if(dumpfile==NULL){
            fprintf(stderr,"\nError opening output file\n");
            return -1;
        }

        printf("\nlistening on %s...\n", d->description);

        /* At this point, we don't need any more the device list. Free it */
        pcap_freealldevs(alldevs);

        /* 循环捕获数据并调用packet_handler函数把数据存储到堆文件 */
    pcap_loop(adhandle, 0, packet_handler, (unsigned char *)dumpfile);

        return 0;
    }

    /* Callback function invoked by libpcap for every incoming packet */

    void packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data)
    {
        /* 此函数功能将数据报存储到堆文件 */
    pcap_dump(dumpfile, header, pkt_data);
    }

    正如你看到的那样该程序的结构非常类似与以前的例子,区别是:
    一旦打开网卡就调用pcap_dump_open()来打开一个文件,该调用将文件和某个网卡相关联。
    packet_handler()内部通过调用pcap_dump()来将捕获的数据报存储到文件。pcap_dump()的参数和 packet_handler()一样,所以用起来比较方便。

    从文件读数据包:

    下面我们来看如何从文件读取数据内容。下面的代码打开了 一个堆文件并打印了其中的每个包内容。
    pcap_open_offline()用来打开一个堆文件,之后用pcap_loop()来循环从文件中读取数据。你能发现读取脱机的数据几乎和实时的从网卡上读取一摸一样。



    #include <stdio.h>
    #include <pcap.h>

    #define LINE_LEN 16

    void dispatcher_handler(u_char *, const struct pcap_pkthdr *, const u_char *);

    main(int argc, char **argv) {

        pcap_t *fp;
        char errbuf[PCAP_ERRBUF_SIZE];


        if(argc != 2){

            printf("usage: %s filename", argv[0]);
            return -1;

        }

        /* 打开一个存储有数据的堆文件 */
    if ( (fp = pcap_open_offline(argv[1], errbuf) ) == NULL)
        {
            fprintf(stderr,"\nError opening dump file\n");
            return -1;
        }

        // 读取数据直到遇到 EOF标志。
    pcap_loop(fp, 0, dispatcher_handler, NULL);

        return 0;
    }



    void dispatcher_handler(u_char *temp1,
                            const struct pcap_pkthdr *header, const u_char *pkt_data)
    {
        u_int i=0;

        /* print pkt timestamp and pkt len */
        printf("%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec, header->len);         

        /* Print the packet */
        for (i=1; (i < header->caplen + 1 ) ; i++)
        {
            printf("%.2x ", pkt_data[i-1]);
            if ( (i % LINE_LEN) == 0) printf("\n");
        }

        printf("\n\n");    

    }


    下面的代码具有一样的作用,只不过是用pcap_next_ex()来代替pcap_loop()循环读取数据而已。




    #include <stdio.h>
    #include <pcap.h>

    #define LINE_LEN 16

    main(int argc, char **argv) {

        pcap_t *fp;
        char errbuf[PCAP_ERRBUF_SIZE];
        struct pcap_pkthdr *header;
        u_char *pkt_data;
        u_int i=0;
        int res;

        if(argc != 2){

            printf("usage: %s filename", argv[0]);
            return -1;

        }

        /* Open a capture file */
        if ( (fp = pcap_open_offline(argv[1], errbuf) ) == NULL)
        {
            fprintf(stderr,"\nError opening dump file\n");
            return -1;
        }

        /* Retrieve the packets from the file */
        while((res = pcap_next_ex( fp, &header, &pkt_data)) >= 0){
            /* print pkt timestamp and pkt len */
            printf("%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec, header->len);         

            /* Print the packet */
            for (i=1; (i < header->caplen + 1 ) ; i++)
            {
                printf("%.2x ", pkt_data[i-1]);
                if ( (i % LINE_LEN) == 0) printf("\n");
            }

            printf("\n\n");    
        }


        if(res == -1){
            printf("Error reading the packets: %s\n", pcap_geterr(fp));
        }

        return 0;
    }



    用pcap_live_dump将数据写到文件:
    WinPcap的最新版本提供了一个进一步的方法来将数据包存储到磁盘,就是使用pcap_live_dump()函数。他需要三个参数:一个文件名,和一个该文件允许的最大长度还有一个参数是该文件所允许的最大包的数量。对这些参数来说 0 意味着没有最大限制。注:我们可以在调用pcap_live_dump()前设置一个过滤器来定义哪些数据报需要存储。

    pcap_live_dump() 是非阻塞的,所以他会立刻返回:数据的存储过程将会异步的进行,直到文件到达了指定的最大长度或最大数据报的数目为止。
    应用程序能够用pcap_live_dump_ended()来等检查是否数据存储完毕,如果你指定的最大长度参数和数据报数量为0,那么该操作将永远阻塞。

    pcap_live_dump() 和 pcap_dump()的不同从设置的最大极限来说就是性能的问题。pcap_live_dump()采用WinPcap NPF驱动来从内核级的层次上向文件中写数据,从而使内存拷贝最小化。
    显然,这些特点当前在其他的操作系统下是不能够实现的,pcap_live_dump()是WinPcap所特有的,而且只能够应用于Win32环境.

    ---------------------------------------
    (续)
  • 相关阅读:
    Coursera机器学习week11 单元测试
    关于 TypeReference 的解释
    getModifiers 方法解释。
    instanceof isInstance isAssignableFrom 比较
    elasticsearch 基础 语法总结
    kibana 启动 关闭 和进程查找
    MD5 SHA1 SHA256 SHA512 SHA1WithRSA 的区别
    spring boot 项目 热启动
    java zip 压缩文件
    Packet for query is too large (1660 > 1024). You can change this value on the server by setting the max_allowed_packet' variable.
  • 原文地址:https://www.cnblogs.com/pulove/p/2454819.html
Copyright © 2020-2023  润新知