使用WinPcap抓包分析网络协议

简介:

创建一个使用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<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 中文技术文档:
http://www.ferrisxu.com/WinPcap/html/index.html

WinPcap下载地址:

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



    本文转自阿凡卢博客园博客,原文链接:http://www.cnblogs.com/luxiaoxun/archive/2012/08/05/2623641.html,如需转载请自行联系原作者

相关文章
|
2月前
|
机器学习/深度学习 编解码 计算机视觉
【APFN】从大佬论文中探索如何分析改进金字塔网络
【APFN】从大佬论文中探索如何分析改进金字塔网络
37 0
|
2月前
|
机器学习/深度学习 分布式计算 资源调度
【社交网络分析】课程考试复盘 + 相关资料补充
【社交网络分析】课程考试复盘 + 相关资料补充
52 0
|
28天前
|
监控 Shell Linux
【Shell 命令集合 网络通讯 】Linux 分析串口的状态 statserial命令 使用指南
【Shell 命令集合 网络通讯 】Linux 分析串口的状态 statserial命令 使用指南
32 0
|
2天前
|
Python 数据可视化 索引
PYTHON用GARCH、离散随机波动率模型DSV模拟估计股票收益时间序列与蒙特卡洛可视化
PYTHON用GARCH、离散随机波动率模型DSV模拟估计股票收益时间序列与蒙特卡洛可视化
14 0
PYTHON用GARCH、离散随机波动率模型DSV模拟估计股票收益时间序列与蒙特卡洛可视化
|
2天前
|
存储 算法 前端开发
R语言中贝叶斯网络(BN)、动态贝叶斯网络、线性模型分析错颌畸形数据
R语言中贝叶斯网络(BN)、动态贝叶斯网络、线性模型分析错颌畸形数据
24 0
|
2天前
|
数据可视化 网络可视化
R语言混合图形模型MGM的网络可预测性分析
R语言混合图形模型MGM的网络可预测性分析
|
3天前
|
算法 定位技术 Windows
R语言最大流最小割定理和最短路径算法分析交通网络流量拥堵问题
R语言最大流最小割定理和最短路径算法分析交通网络流量拥堵问题
10 4
|
10天前
|
安全 网络安全 网络虚拟化
虚拟网络设备与网络安全:深入分析与实践应用
在数字化时代📲,网络安全🔒成为了企业和个人防御体系中不可或缺的一部分。随着网络攻击的日益复杂和频繁🔥,传统的物理网络安全措施已经无法满足快速发展的需求。虚拟网络设备🖧,作为网络架构中的重要组成部分,通过提供灵活的配置和强大的隔离能力🛡️,为网络安全提供了新的保障。本文将从多个维度深入分析虚拟网络设备是如何保障网络安全的,以及它们的实际意义和应用场景。
|
21天前
|
缓存 网络协议 数据库连接
【底层服务/编程功底系列】「网络通信体系」深入探索和分析TCP协议的运输连接管理的核心原理和技术要点
【底层服务/编程功底系列】「网络通信体系」深入探索和分析TCP协议的运输连接管理的核心原理和技术要点
20 0
|
25天前
|
运维 负载均衡 监控
【软件设计师备考 专题 】网络性能分析
【软件设计师备考 专题 】网络性能分析
39 0