zoukankan      html  css  js  c++  java
  • (原创)WinpCap的详解(三)

      接着WinpCap详解(二),这篇博客主要来讨论一下,堆文件的处理以及简单发送数据包。

    1、处理脱机堆文件

      我们将学习如何处理捕获到文件中的数据包。 WinPcap提供了很多函数来将网络数据流保存到文件并读取它们 -- 本讲将教你如何使用这些函数。我们还将看到如何使用WinPcap内核堆特性来获取一个高性能的堆。

      堆文件的格式是libpcap的一种。这种格式中,包含了被捕捉到的包的二进制数据,并且,这种格式是许多网络工具所使用的一种标准,这些工具包括WinDump,Etheral和Snort。

     保存数据包到堆文件

      首先,让我们看一下如何将一个数据包写成libpcap的格式。

      接下来的例子讲从一个选定的接口捕获数据包,并且将它们保存到用户指定的文件中。 

      这里pcap_dump_t和pcap_dump一样的东西,表示的是libpcap存储文件的描述符,对用户来说也是不透明的。

      这里的pcap_findalldevs_ex()函数是pcap_findalldevs()的扩展。这里我列出他的英文解释吧,翻译起来还不是很好,看英文比较容易了。

      This function is a superset of the old 'pcap_findalldevs()', which is obsolete, and which allows listing only the devices present on the local machine. Vice versa, pcap_findalldevs_ex() allows listing the devices present on a remote machine as well. Additionally, it can list all the pcap files available into a given folder. Moreover, pcap_findalldevs_ex() is platform independent, since it relies on the standard pcap_findalldevs() to get addresses on the local machine.

      pcap_dump_open(pcap_t* p,const char* name)打开一个堆文件,第二个参数是文件的名字,第一个参数是已经打开的适配器描述符。

    #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_ex(PCAP_SRC_IF_STRING, NULL, &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");
            /* 释放列表 */
            pcap_freealldevs(alldevs);
            return -1;
        }
            
        /* 跳转到选中的适配器 */
        for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
        
        
        /* 打开适配器 */
        if ( (adhandle= pcap_open(d->name,          // 设备名
                                  65536,            // 要捕捉的数据包的部分 
                                            		   // 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
                                  PCAP_OPENFLAG_PROMISCUOUS,    // 混杂模式
                                  1000,             // 读取超时时间
                                  NULL,             // 远程机器验证
                                  errbuf            // 错误缓冲池
                                  ) ) == NULL)
        {
            fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
            /* 释放设备列表 */
            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... Press Ctrl+C to stop...\n", d->description);
        
        /* 释放设备列表 */
        pcap_freealldevs(alldevs);
        
        /* 开始捕获 */
        pcap_loop(adhandle, 0, packet_handler, (unsigned char *)dumpfile);
    
        return 0;
    }
    
    /* 回调函数,用来处理数据包 */
    void packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data)
    {
        /* 保存数据包到堆文件 */
        pcap_dump(dumpfile, header, pkt_data);
    }
    
    
    

     从堆文件中读取数据包

      既然我们有了可用的堆文件,那我们就能读取它的内容了。 以下代码将打开一个WinPcap/libpcap的堆文件,并显示文件中每一个包的信息。文件通过 pcap_open_offline() 打开,然后,我们通常使用 pcap_loop() 来有序获取数据包。你可以看到,从脱机文件中读取数据包和从物理接口中接收它们是很相似的。

      这个例子还会介绍另一个函数:pcap_createsrcsrc()。这个函数用于创建一个源字符串,这个源字符串以一个标志开头,这个标志会告诉WinPcap这个源的类型。比如,使用"rpcap://"标志来打开一个适配器,使用"file://"来打开一个文件。如果 pcap_findalldevs_ex() 已经被使用,那么这部是不需要的,因为其返回值已经包含了这些字符串。然而,在这个例子中,我们需要它。因为文件的名字来自于用户的输入。

      

    2、发送数据包

      尽管从 WinPcap 的名字上看,这个库的目标应该是数据捕捉(Packet Capture),然而,它也提供了针对很多其它有用的特性。在其中,我们可以找到一组很完整的用于发送数据包的函数。

      请注意:原始的libpcap库是不支持发送数据包的,因此,这里展示的函数都属于是WinPcap的扩展,并且它们不能运行于Unix平台下。

      使用 pcap_sendpacket() 发送单个数据包

      下面的代码展示了发送一个数据包的最简单的方式。打开适配器以后,调用 pcap_sendpacket() 来发送手工制作的数据包。 pcap_sendpacket() 的参数有一个要包涵发送数据的缓冲区,缓冲的长度,以及用来发送数据的适配器。注意,缓冲数据将直接发送到网络,而不会进行任何加工和处理。这就意味着应用程序需要创建一个正确的协议首部,来使这个数据包更有意义。

    #include <stdlib.h>
    #include <stdio.h>
    
    #include <pcap.h>
    
    
    void main(int argc, char **argv)
    {
    pcap_t *fp;
    char errbuf[PCAP_ERRBUF_SIZE];
    u_char packet[100];
    int i;
    
        /* 检查命令行参数的合法性 */
        if (argc != 2)
        {
            printf("usage: %s interface (e.g. 'rpcap://eth0')", argv[0]);
            return;
        }
        
        /* 打开输出设备 */
        if ( (fp= pcap_open(argv[1],            // 设备名
                            100,                // 要捕获的部分 (只捕获前100个字节)
                            PCAP_OPENFLAG_PROMISCUOUS,  // 混杂模式
                            1000,               // 读超时时间
                            NULL,               // 远程机器验证
                            errbuf              // 错误缓冲
                            ) ) == NULL)
        {
            fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", argv[1]);
            return;
        }
    
        /* 假设在以太网上,设置MAC的目的地址为 1:1:1:1:1:1 */
        packet[0]=1;
        packet[1]=1;
        packet[2]=1;
        packet[3]=1;
        packet[4]=1;
        packet[5]=1;
        
        /* 设置MAC源地址为 2:2:2:2:2:2 */
        packet[6]=2;
        packet[7]=2;
        packet[8]=2;
        packet[9]=2;
        packet[10]=2;
        packet[11]=2;
        
        /* 填充剩下的内容 */
        for(i=12;i<100;i++)
        {
            packet[i]=i%256;
        }
    
        /* 发送数据包 */
        if (pcap_sendpacket(fp, packet, 100 /* size */) != 0)
        {
            fprintf(stderr,"\nError sending the packet: \n", pcap_geterr(fp));
            return;
        }
    
        return;
    }
    
    
    

      这里面只是简单的数据发送,还有更复杂的队列发送,由于现在不需要用到这一块,所以就不看这一部分了,因为我只需要发送简单的命令,不需要大量的数据发送,这里就不再讲下去啦,如果想更深入的了解WinpCap的功能,推荐一个好的网站,http://www.ferrisxu.com/WinPcap/html/modules.html,这个网站里面有很多关于WinpCap的东西,可能里面有很多更新的东西,还值得大家以后共同学习,本人就学习这么多啦!

  • 相关阅读:
    Android学习之天气预报简单版
    Android学习之Json数据的获取与解析
    getPath()返回路径包含的“%20”(空格)的处理
    自学php 之数据库连接编程
    bnuoj 1071 拼图++
    hdu 2489 Minimal Ratio Tree
    hdu 4720 Naive and Silly Muggles
    hdu 4725 The Shortest Path in Nya Graph
    经典算法学习:排序之希尔排序(壳排序)
    经典算法学习:排序之插入排序
  • 原文地址:https://www.cnblogs.com/yingfang18/p/1890164.html
Copyright © 2011-2022 走看看