您好,欢迎访问三七文档
当前位置:首页 > 机械/制造/汽车 > 制造加工工艺 > Winpcap学习资料
网络资源Winpcap学习第一天说明:本系列文章是我阅读winpcap手册后整理的一个学习笔记。文章中出现的所有代码是我根据winpcap手册中的示例代码进行了学习,并调试通过,其中对部分代码作了修改,关于代码的版权我尊重winpcap手册中的版权说明,如果你使用了本系列文章中的代码而引起任何的版权或造成安全威胁等问题,我将不负任何责任。下载好了WpdPack_3_2_alpha1.zip,解压后除了有文档,例子外还有Include和lib,推荐用VC++6.0。第一个试验是:#includepcap.h#includeremote-ext.hintmain(){pcap_if_t*alldevs;pcap_if_t*d;inti=0;charerrbuf[PCAP_ERRBUF_SIZE];/*Retrievethedevicelistfromthelocalmachine*/if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL/*authisnotneeded*/,&alldevs,errbuf)==-1){printf(Errorinpcap_findalldevs_ex:%s\n,errbuf);exit(1);}/*Printthelist*/for(d=alldevs;d!=NULL;d=d-next){/*Printthedevice'sname*/printf(%d.%s,++i,d-name);/*Printthedevice'sdscription*/if(d-description){printf((%s)\n,d-description);}else{printf((Nodescriptionavailable)\n);}}if(i==0){printf(\nNointerfacesfound!MakesureWinPcapisinstalled.\n);return0;}/*Wedon'tneedanymorethedevicelist.Freeit*/pcap_freealldevs(alldevs);return1;}编译的时候又遇到问题——“无法打开pcap.h”。又查看开发手册才找到解决方法:1.安装Winpcap驱动。下载地址:。2.将Winpcap的Include,Lib目录添加进VC6.0的环境变量中;3.针对每一个项目,先用VC打开项目,然后在Project-Settings,标签栏出选择C/C++,在Preprocessordefinitions的输入框里添加WPCAP,再选择Link,在Object/librarymodules的输入框里添加wpcap.libPacket.lib。(可以在project-setting-Link-object/librarymodules里添加wsock32.lib,也可在stdafx.cpp里添加#pragmacomment(lib,wsock32.lib))再编译时终于OK了。之后,阅读代码并查看开发手册学到了下面的东西:pcap_if是一个结构体,具体点它是一个链表的结点,他的定义如下:structpcap_if{structpcap_if*next;char*name;char*description;structpcap_addr*addresses;u_intflags;}另外,在pcap.h中有一句“typedefstructpcap_ifpcap_if_t;”,所以也可以用pcap_if_t代替pcap_if。intpcap_findalldevs_ex(char*source,structpcap_rmtauth*auth,pcap_if_t**alldevs,char*errbuf)这个函数是’pcap_findalldevs()’的一个超集。’pcap_findalldevs()’比较老,他只允许列出本地机器上的设备。然而,’pcap_findalldevs_ex()’除了可以列出本地及其上的设备,还可以列出远程机器上的设备。此外,它还能列出所有可用的pcap文件到指定的文件夹。’pcap_findalldevs_ex()’是平台无关的,然而它以来于标准的’pcap_findalldevs()’来获得本地机器的地址。Winpcap学习第二天今天在阅读WinpcapManual的时候发现一句话:“Thismeansthatonsharedmedia(likenon-switchedEthernet),WinPcapwillbeabletocapturethepacketsofotherhosts.”我理解为:如果在通过没有交换功能的集线器连接的网络上,只要把网卡设置为混杂(promiscuous)模式,winpcap能够捕获到其他主机通信的数据包。如果是具有交换功能的集线器连接的网络winpcap还能管用吗?这个在后边的实习中将会进行试验。试验程序2:/**截获数据包的试验。先打印出所有网络适配器的列表,然后选择*想在哪个适配器上截获数据包。然后通过pcap_loop()函数将截获*的数据包传给回调函数packet_handler()处理。*通过该程序初步了解了使用winpcap截获数据包的步骤以及一些在*截获数据包时非常重要的函数和结构体。*2006-1-26*/#includepcap.h#includeremote-ext.h/*Prototypeofthepackethandler*/voidpacket_handler(u_char*param,conststructpcap_pkthdr*header,constu_char*pkt_data);intmain(){pcap_if_t*alldevs;pcap_if_t*d;intinum;inti=0;pcap_t*adhandle;charerrbuf[PCAP_ERRBUF_SIZE];/*Retrievethedeviceslistonthelocalmachine*/if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&alldevs,errbuf)==-1){fprintf(stderr,Errorinpcap_findalldevs:%s\n,errbuf);exit(1);}/*Printthelist*/for(d=alldevs;d;d=d-next){/*Printname*/printf(%d.%s,++i,d-name);/*Printdescription*/if(d-description){printf((%s)\n,d-description);}else{printf((Nodescriptionavailable)\n);}}if(i==0){printf(\nNointerfacesfound!MakesureWinpcapisinstalled.\n);return-1;}/*Selectanadapter*/printf(Entertheinterfacenumber(1-%d):,i);scanf(%d,&inum);if(inum1||inumi){printf(\nInterfacenumberoutofrange.\n);/*Freethedevicelist*/pcap_freealldevs(alldevs);return-1;}/*Jumptotheselectedadapter*/for(d=alldevs,i=0;iinum-1;d=d-next,++i);/*Openthedevice*/if((adhandle=pcap_open(d-name,/*nameofthedevice*/65536,/*portionofthepackettocapture*//*65535guaranteesthatthewholepacketwillbecapturedonallthelinklayers*/PCAP_OPENFLAG_PROMISCUOUS,/*promiscuousmode*/1000,/*readtimeout*/NULL,/*authenticationontheremotemachine*/errbuf/*errorbuffer*/))==NULL){fprintf(stderr,\nnabletoopentheadapter.%sisnotsupportedbyWinpcap\n,d-name);/*Freethedeviceslist*/pcap_freealldevs(alldevs);return-1;}printf(\nlisteningon%s...\n,d-description);/*Atthispoint,wedon'tneedanymorethedevicelist.Freeit*/pcap_freealldevs(alldevs);/*startthecapture*/pcap_loop(adhandle,0,packet_handler,NULL);return1;}/*Callbackfunctioninvokedbylibpcapforeveryincomingpacket*/voidpacket_handler(u_char*param,conststructpcap_pkthdr*header,constu_char*pkt_data){structtm*ltime;chartimestr[16];/*convertthetimestamptoreadableformat*/ltime=localtime(&header-ts.tv_sec);strftime(timestr,sizeof(timestr),%H:%M:%S,ltime);printf(%s,%.6dlen:%d\n,timestr,header-ts.tv_usec,header-len);}函数1:pcap_t*pcap_open(constchar*source,intsnaplen,intflags,intread_timeout,structpcap_rmtauth*auth,char*errbuf)为捕获/发送数据打开一个普通的源。pcap_open()能够替代所有的pcap_open_xxx()函数,它隐藏了不同的pcap_open_xxx()之间的差异,所以程序员不必使用不同的open函数。source:的是包含要打开的源名称的以’\0’结尾的字符串。源名称得包含新的源规范语法(SourceSpecificationSyntax),并且它不能为NULL。为了方便的使用源语法,请记住:(1)pcap_findalldevs_ex()返回的适配器(网卡)可以直接被pcap_open()使用;(2)万一用户想传递他自己的源字符串给pcap_open(),pcap_createsrcstr()可以创建正确的源标识。snaplen:需要保留的数据包的长度。对每一个过滤器接收到的数据包,第一个‘snaplen’字节的内容将被保存到缓冲区,并且传递给用户程序。例如,snaplen等于100,那么仅仅每一个数据包的第一个100字节的内容被保存。简言之就是从每一个包的开头到snaplen的那段内容将被保存。flags:保存一些由于抓包需要的标志。Winpcap定义了三种标志:lPCAP_OPENFLAG_PROMISCUOUS:1,它定义了适配器(网卡)是否进入混杂模式(promiscuousmode)。lPCAP_OPENFLAG_DATATX_UDP:2,它定义了数据传输(假如是远程抓包)是否用UDP协议来处理。lPCAP_OPENFLAG
本文标题:Winpcap学习资料
链接地址:https://www.777doc.com/doc-4769277 .html