如何用winpcap抓包分析网络协议

  • 格式:docx
  • 大小:23.35 KB
  • 文档页数:5

下载文档原格式

  / 5
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

创建一个使用wpcap.dll的应用程序

用 Microsoft Visual C++ 创建一个使用wpcap.dll的应用程序,需要按一下步骤:

∙在每一个使用了库的源程序中,将pcap.h头文件包含(include)进来。

∙如果你在程序中使用了WinPcap中提供给Win32平台的特有的函数,记得在预处理中加入WPCAP的定义。(工程->设置->c/c++->预处理程序定义中添加WPCAP)∙如果你的程序使用了WinPcap的远程捕获功能,那么在预处理定义中加入HAVE_REMOTE。不要直接把remote-ext.h直接加入到你的源文件中去。(工程->设置->c/c++->预处理程序定义中添加HAVE_REMOTE)

∙设置VC++的链接器(Linker),把wpcap.lib库文件包含进来。wpcap.lib可以在WinPcap 中找到。

∙设置VC++的链接器(Linker),把ws2_32.lib库文件包含进来。这个文件分布于C的编译器,并且包含了Windows的一些socket函数。本教程中的一些范例程序,会需要它。

记住以下几点:

∙要添加一个预处理定义,你需要打开Project菜单,选择Settings,然后选择C/C++选项卡,在General类下,你必须在Preprocessor Definitions下的文本框中添加定义。

∙要在一个VC++6.0工程中,添加一,个新的库,你必须打开Project菜单,选择Settings,然后选择Link选项卡,然后把新库的名字添加到Object/Library modules下的文本框中

∙要向VC++6.0中添加一个新的库所在的路径,你必须打开Tool菜单,选择Options,然后选择Directories选项卡,在Show directories下拉框中选择Library files,并且将

新的路径添加到Directories中去

∙要向VC++6.0中添加一个新的包含文件所在的路径,你必须打开Tool菜单,选择Options,然后选择Directories选项卡,在Show directories下拉框中选择Include files,并且将新的路径添加到Directories中去

范例程序

我们一共了一些范例程序来显示WinPcap API的用法。这些程序的源代码,以及编译运行这些代码所需的所有文件,都可以在Developer's Pack找到。作为教程,在这里,我们提供了浏览器式的代码:这样,在每个函数和变量之间的跳转会比较方便。更多完整的范例程序,请参阅WinPcap 教程.

// NOTE: remember to include WPCAP and HAVE_REMOTE among your preprocessor definitions.

(工程->设置->c/c++->预处理程序定义中添加WPCAP和

HAVE_REMOTE)

如果连接有问题,把lib复制到工程目录下用下面方法:

#pragma comment(lib,"wpcap.lib")

#pragma comment(lib,"packet.lib")

使用WinPcap抓包分析网络协议

//捕获网络数据包的C++程序

//可以获得数据包长度、通过以太网类型确定上层协议、源以太网地址和目的以太网地址!

#include "pcap.h"

#include

#pragma comment(lib,"wpcap.lib")

#pragma comment(lib,"packet.lib")

#pragma comment(lib,"ws2_32.lib")

/*以下是以太网协议格式*/

struct ether_header

{

u_int8_t ether_dhost[6]; //目的Mac地址

u_int8_t ether_shost[6]; //源Mac地址

u_int16_t ether_type; //协议类型

};

struct ip_header

{

#if defined(WORDS_BIENDIAN)

u_int8_t ip_version:4,

ip_header_length:4;

#else

u_int8_t ip_header_length:4,

ip_version:4;

#endif

u_int8_t ip_tos;

u_int16_t ip_length;

u_int16_t ip_id;

u_int16_t ip_off;

u_int8_t ip_ttl;

u_int8_t ip_protocol;

u_int16_t ip_checksum;

struct in_addrip_souce_address;

struct in_addrip_destination_address;

};

void ip_protool_packet_callback(u_char

*argument,conststruct pcap_pkthdr*

packet_header,const u_char*packet_content)

{

struct ip_header *ip_protocol;

u_intheader_length;

u_int offset;

u_chartos;

u_int16_t checksum;

//MAC首部是14位的,加上14位得到IP协议首部

ip_protocol = (struct ip_header *) (packet_content+14);

checksum =ntohs(ip_protocol->ip_checksum);

tos= ip_protocol->ip_tos;

offset = ntohs(ip_protocol->ip_off);

printf("---------IP协议---------\n");

printf("版本号:%d\n", ip_protocol->ip_version);

printf("首部长度:%d\n",header_length);

printf("服务质量:%d\n",tos);

printf("总长度:%d\n",ntohs(ip_protocol->ip_length));

printf("标识:%d\n",ntohs(ip_protocol->ip_id));

printf("偏移:%d\n",(offset &0x1fff) * 8);

printf("生存时间:%d\n",ip_protocol->ip_ttl);

printf("协议类型:%d\n",ip_protocol->ip_protocol);

switch (ip_protocol->ip_protocol)

{

case1: printf("上层协议是ICMP协议\n");break;

case2: printf("上层协议是IGMP协议\n");break;

case6: printf("上层协议是TCP协议\n");break;

case17: printf("上层协议是UDP协议\n");break;

default:break;

}

printf("检验和:%d\n",checksum);

printf("源IP地址:%s\n", inet_ntoa(ip_protocol->ip_souce_address)); printf("目的地址:%s\n",

inet_ntoa(ip_protocol->ip_destination_address));

}

void ethernet_protocol_packet_callback(u_char

*argument,conststruct pcap_pkthdr*

packet_header,const u_char*packet_content)

{

u_shortethernet_type;

struct ether_header *ethernet_protocol;