使用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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM