使用WinPcap抓包分析网络协议

时间:2021-06-15 05:05:09
使用WinPcap抓包分析网络协议 http://www.cnblogs.com/luxiaoxun/archive/2012/08/05/2623641.html

创建一个使用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抓包分析网络协议

使用WinPcap抓包分析网络协议
//捕获网络数据包的C++程序   
//可以获得数据包长度、通过以太网类型确定上层协议、源以太网地址和目的以太网地址!
#include "pcap.h"
#include
<winsock2.h>

#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_addr ip_souce_address;
struct in_addr ip_destination_address;
};

void ip_protool_packet_callback(u_char *argument,const struct pcap_pkthdr* packet_header,const u_char* packet_content)
{
struct ip_header *ip_protocol;
u_int header_length;
u_int offset;
u_char tos;
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)
{
case 1: printf("上层协议是ICMP协议\n");break;
case 2: printf("上层协议是IGMP协议\n");break;
case 6: printf("上层协议是TCP协议\n");break;
case 17: 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,const struct pcap_pkthdr* packet_header,const u_char* packet_content)
{
u_short ethernet_type;
struct ether_header *ethernet_protocol;
u_char
*mac_string;
static int packet_number = 1;
printf(
"----------------------------------------------\n");
printf(
"捕获第%d个网络数据包\n",packet_number);
printf(
"捕获时间:\n");
printf(
"%s",ctime((const time_t*)&packet_header->ts.tv_sec));
printf(
"数据包长度:\n");
printf(
"%d\n",packet_header->len);
printf(
"---------以太网协议---------\n");
ethernet_protocol
=(struct ether_header*)packet_content;//获得数据包内容
printf("以太网类型:\n");
ethernet_type
=ntohs(ethernet_protocol->ether_type);//获得以太网类型
printf("%04x\n",ethernet_type);
switch (ethernet_type)
{
case 0x0800: printf("上层协议是IP协议\n");break;
case 0x0806: printf("上层协议是ARP协议\n");break;
case 0x8035: printf("上层协议是RARP协议\n");break;
default:break;
}
printf(
"MAC帧源地址:\n");
mac_string
=ethernet_protocol->ether_shost;
printf(
"%02x:%02x:%02x:%02x:%02x:%02x\n",*mac_string,*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));
printf(
"MAC帧目的地址:\n");
mac_string
=ethernet_protocol->ether_dhost;
printf(
"%02x:%02x:%02x:%02x:%02x:%02x\n",*mac_string,*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));
if(ethernet_type==0x0800)//继续分析IP协议
{
ip_protool_packet_callback (argument,packet_header,packet_content);
}
printf(
"----------------------------------------------\n");
packet_number
++;
}

int main()
{
pcap_t
* pcap_handle; //winpcap句柄
char error_content[PCAP_ERRBUF_SIZE]; //存储错误信息
bpf_u_int32 net_mask; //掩码地址
bpf_u_int32 net_ip; //网络地址
char *net_interface; //网络接口
struct bpf_program bpf_filter; //BPF过滤规则
char bpf_filter_string[]="ip"; //过滤规则字符串,只分析IPv4的数据包
net_interface=pcap_lookupdev(error_content); //获得网络接口
pcap_lookupnet(net_interface,&net_ip,&net_mask,error_content); //获得网络地址和掩码地址
pcap_handle=pcap_open_live(net_interface,BUFSIZ,1,0,error_content); //打开网络接口
pcap_compile(pcap_handle,&bpf_filter,bpf_filter_string,0,net_ip); //编译过滤规则
pcap_setfilter(pcap_handle,&bpf_filter);//设置过滤规则
if (pcap_datalink(pcap_handle)!=DLT_EN10MB) //DLT_EN10MB表示以太网
return 0;
pcap_loop(pcap_handle,
10,ethernet_protocol_packet_callback,NULL); //捕获10个数据包进行分析
pcap_close(pcap_handle);
return 0;
}
使用WinPcap抓包分析网络协议

 

WinPcap 中文技术文档:
http://www.ferrisxu.com/WinPcap/html/index.html

WinPcap下载地址:

http://www.winpcap.org/install/default.htm

 

作者:阿凡卢出处:http://www.cnblogs.com/luxiaoxun/本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。分类: Network