天天看點

Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包

概述

libpcap 是一個網絡資料包捕獲函數庫,功能非常強大,Linux 下著名的 tcpdump 就是以它為基礎的。

libpcap主要的作用

1)捕獲各種資料包,列如:網絡流量統計。

2)過濾網絡資料包,列如:過濾掉本地上的一些資料,類似防火牆。

3)分析網絡資料包,列如:分析網絡協定,資料的采集。

4)存儲網絡資料包,列如:儲存捕獲的資料以為将來進行分析。

libpcap 的安裝

Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包

libpcap 的抓包架構

pcap_lookupdev():函數用于查找網絡裝置,傳回可被 pcap_open_live() 函數調用的網絡裝置名指針。

pcap_lookupnet():函數獲得指定網絡裝置的網絡号和掩碼。

pcap_open_live(): 函數用于打開網絡裝置,并且傳回用于捕獲網絡資料包的資料包捕獲描述字。對于此網絡裝置的操作都要基于此網絡裝置描述字。

pcap_compile(): 函數用于将使用者制定的過濾政策編譯到過濾程式中。

pcap_setfilter():函數用于設定過濾器。

pcap_loop():函數 pcap_dispatch() 函數用于捕獲資料包,捕獲後還可以進行處理,此外 pcap_next() 和 pcap_next_ex() 兩個函數也可以用來捕獲資料包。

pcap_close():函數用于關閉網絡裝置,釋放資源。

利用 libpcap 函數庫開發應用程式的基本步驟:

1、打開網絡裝置

2、設定過濾規則

3、捕獲資料

4、關閉網絡裝置

抓包詳細步驟

首先要使用 libpcap,我們必須包含 pcap.h 頭檔案,可以在 /usr/local/include/pcap/pcap.h 找到,其中包含了每個類型定義的詳細說明。

Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包

1、擷取網絡接口裝置名

char *pcap_lookupdev(char *errbuf)

功能:

得到可用的網絡裝置名指針

參數:

errbuf 存放出錯資訊字元串,裡面有個宏定義:PCAP_ERRBUF_SIZE,為錯誤緩沖區大小

傳回值:

成功傳回裝置名指針(第一個合适的網絡接口的字元串指針);

失敗傳回 NULL,同時,errbuf 存放出錯資訊字元串。

執行個體如下:

[objc] view plaincopy

Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
  1. char error_content[PCAP_ERRBUF_SIZE] = {0}; // 出錯資訊  
  2. charchar *dev = pcap_lookupdev(error_content);  
  3. if(NULL == dev)  
  4. {  
  5.     printf(error_content);  
  6.     exit(-1);  
  7. }  

2、擷取網絡号和掩碼

int pcap_lookupnet(    char *device,                     

bpf_u_int32 *netp, 
bpf_u_int32 *maskp,     
char *errbuf )

功能:

擷取指定網卡的 ip 位址,子網路遮罩

參數:

device:網絡裝置名,為第一步擷取的網絡接口字元串(pcap_lookupdev() 的傳回值 ),也可人為指定,如“eth0”。

netp:存放 ip 位址的指針,bpf_u_int32 為 32 位無符号整型

maskp:存放子網路遮罩的指針,bpf_u_int32 為 32 位無符号整型

errbuf:存放出錯資訊

傳回值:

成功傳回 0,失敗傳回 -1

執行個體如下:

[objc] view plaincopy

Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
  1. char error_content[PCAP_ERRBUF_SIZE] = {0}; // 出錯資訊  
  2. charchar *dev = pcap_lookupdev(error_content);  
  3. if(NULL == dev)  
  4. {  
  5.     printf(error_content);  
  6.     exit(-1);  
  7. }  
  8. bpf_u_int32 netp = 0, maskp = 0;  
  9. pcap_t * pcap_handle = NULL;  
  10. int ret = 0;  
  11. //獲得網絡号和掩碼  
  12. ret = pcap_lookupnet(dev, &netp, &maskp, error_content);  
  13. if(ret == -1)  
  14. {  
  15.     printf(error_content);  
  16.     exit(-1);  
  17. }  

3、打開網絡接口

pcap_t *pcap_open_live(  const char *device,

int snaplen,
int promisc,
int to_ms,
char *ebuf )

功能:

打開一個用于捕獲資料的網絡接口

參數:

device:網絡接口的名字,為第一步擷取的網絡接口字元串(pcap_lookupdev() 的傳回值 ),也可人為指定,如“eth0”。

snaplen:捕獲資料包的長度,長度不能大于 65535 個位元組。

promise:“1” 代表混雜模式,其它非混雜模式。什麼為混雜模式,請看《原始套接字程式設計》。

to_ms:指定需要等待的毫秒數,超過這個數值後,擷取資料包的函數就會立即傳回(這個函數不會阻塞,後面的抓包函數才會阻塞)。0 表示一直等待直到有資料包到來。

ebuf:存儲錯誤資訊。

傳回值:

傳回 pcap_t 類型指針,後面的所有操作都要使用這個指針。

執行個體如下:

[objc] view plaincopy

Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
  1. char error_content[PCAP_ERRBUF_SIZE] = {0}; // 出錯資訊  
  2. charchar *dev = pcap_lookupdev(error_content);  // 擷取網絡接口  
  3. if(NULL == dev)  
  4. {  
  5.     printf(error_content);  
  6.     exit(-1);  
  7. }  
  8. // 打開網絡接口  
  9. pcap_t * pcap_handle = pcap_open_live(dev, 1024, 1, 0, error_content);  
  10. if(NULL == pcap_handle)  
  11. {  
  12.     printf(error_content);  
  13.     exit(-1);  
  14. }  

4、擷取資料包

a)

const u_char *pcap_next(pcap_t *p, struct pcap_pkthdr *h)

功能:

捕獲一個網絡資料包,收到一個資料包立即傳回

參數:

p:pcap_open_live()傳回的 pcap_t 類型的指針

h:資料標頭

pcap_pkthdr 類型的定義如下:

[objc] view plaincopy

Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
  1. struct pcap_pkthdr  
  2. {  
  3.     struct timeval ts; // 抓到包的時間  
  4.     bpf_u_int32 caplen; // 表示抓到的資料長度  
  5.     bpf_u_int32 len; // 表示資料包的實際長度  
  6. }  

len 和 caplen的差別:

因為在某些情況下你不能保證捕獲的包是完整的,例如一個包長 1480,但是你捕獲到 1000 的時候,可能因為某些原因就中止捕獲了,是以 caplen 是記錄實際捕獲的包長,也就是 1000,而 len 就是 1480。 

傳回值:

成功傳回捕獲資料包的位址,失敗傳回 NULL

執行個體如下:

[objc] view plaincopy

Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
  1. const unsigned charchar *p_packet_content = NULL; // 儲存接收到的資料包的起始位址  
  2. pcap_t *pcap_handle = NULL;  
  3. struct pcap_pkthdr protocol_header;  
  4. pcap_handle = pcap_open_live("eth0", 1024, 1, 0,NULL);  
  5. p_packet_content = pcap_next(pcap_handle, &protocol_header);   
  6. //p_packet_content  所捕獲資料包的位址  
  7. printf("Capture Time is :%s",ctime((const time_ttime_t *)&protocol_header.ts.tv_sec)); // 時間  
  8. printf("Packet Lenght is :%d\n",protocol_header.len);   // 資料包的實際長度  
  9. // 分析以太網中的 源mac、目的mac  
  10. struct ether_header *ethernet_protocol = NULL;  
  11. unsigned charchar *p_mac_string = NULL;         // 儲存mac的位址,臨時變量  
  12. ethernet_protocol = (struct ether_header *)p_packet_content;  //struct ether_header 以太網幀頭部  
  13. p_mac_string = (unsigned charchar *)ethernet_protocol->ether_shost;//擷取源mac  
  14. printf("Mac Source Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(p_mac_string+0),*(p_mac_string+1),*(p_mac_string+2),*(p_mac_string+3),*(p_mac_string+4),*(p_mac_string+5));  
  15. p_mac_string = (unsigned charchar *)ethernet_protocol->ether_dhost;//擷取目的mac  
  16. printf("Mac Destination Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(p_mac_string+0),*(p_mac_string+1),*(p_mac_string+2),*(p_mac_string+3),*(p_mac_string+4),*(p_mac_string+5));  

b)

int pcap_loop( pcap_t *p,

int cnt,
pcap_handler callback,
u_char *user )

功能:

循環捕獲網絡資料包,直到遇到錯誤或者滿足退出條件。每次捕獲一個資料包就會調用 callback 指定的回調函數,是以,可以在回調函數中進行資料包的處理操作。

參數:

p:pcap_open_live()傳回的 pcap_t 類型的指針。

cnt:指定捕獲資料包的個數,一旦抓到了 cnt 個資料包,pcap_loop 立即傳回。如果是 -1,就會永無休止的捕獲,直到出現錯誤。

callback:回調函數,名字任意,根據需要自行起名。

user:向回調函數中傳遞的參數。

callback 回調函數的定義:

void callback( u_char *userarg, 

const struct pcap_pkthdr * pkthdr, 
const u_char * packet )

userarg:pcap_loop() 的最後一個參數,當收到足夠數量的包後 pcap_loop 會調用callback 回調函數,同時将pcap_loop()的user參數傳遞給它

pkthdr:是收到資料包的 pcap_pkthdr 類型的指針,和 pcap_next() 第二個參數是一樣的。

packet :收到的資料包資料

傳回值:

成功傳回0,失敗傳回負數

執行個體如下:

[objc] view plaincopy

Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
  1. if( pcap_loop(pcap_handle, -1, ethernet_protocol_callback, NULL) < 0 )  
  2. {  
  3.     perror("pcap_loop");  
  4. }  
  5. void ethernet_protocol_callback(unsigned charchar *argument,const struct pcap_pkthdr *packet_heaher,const unsigned charchar *packet_content)  
  6. {  
  7.     unsigned charchar *mac_string;              //  
  8.     struct ether_header *ethernet_protocol;  
  9.     unsigned short ethernet_type;           //以太網類型  
  10.     printf("----------------------------------------------------\n");  
  11.     printf("%s\n", ctime((time_ttime_t *)&(packet_heaher->ts.tv_sec))); //轉換時間  
  12.     ethernet_protocol = (struct ether_header *)packet_content;  
  13.     mac_string = (unsigned charchar *)ethernet_protocol->ether_shost;//擷取源mac位址  
  14.     printf("Mac Source Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(mac_string+0),*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));  
  15.     mac_string = (unsigned charchar *)ethernet_protocol->ether_dhost;//擷取目的mac  
  16.     printf("Mac Destination Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(mac_string+0),*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));  
  17.     ethernet_type = ntohs(ethernet_protocol->ether_type);//獲得以太網的類型  
  18.     printf("Ethernet type is :%04x\n",ethernet_type);  
  19.     switch(ethernet_type)  
  20.     {  
  21.         case 0x0800:printf("The network layer is IP protocol\n");break;//ip  
  22.         case 0x0806:printf("The network layer is ARP protocol\n");break;//arp  
  23.         case 0x0835:printf("The network layer is RARP protocol\n");break;//rarp  
  24.         default:break;  
  25.     }  
  26.     usleep(800*1000);  
  27. }  

c)

int pcap_dispatch(pcap_t * p, int cnt, pcap_handler callback, u_char * user)

這個函數和 pcap_loop() 非常類似,隻是在超過 to_ms 毫秒後就會傳回( to_ms 是pcap_open_live() 的第4個參數 )

5、釋放網絡接口

void pcap_close(pcap_t *p)

功能:

關閉 pcap_open_live() 打開的網絡接口(即其傳回值,pcap_t 類型指針),并釋放相關資源。注意,操作完網絡接口,應該釋放其資源。

參數:

需要關閉的網絡接口,pcap_open_live() 的傳回值(pcap_t 類型指針)

傳回值:

執行個體如下:

[objc] view plaincopy

Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
  1. // 打開網絡接口  
  2. pcap_t * pcap_handle = pcap_open_live("eth0", 1024, 1, 0, error_content);  
  3. if(NULL == pcap_handle)  
  4. {  
  5.     printf(error_content);  
  6.     exit(-1);  
  7. }  
  8.  ……  
  9.  ……  
  10. pcap_close(pcap_handle); //釋放網絡接口  

例子1(接收一個資料包):

[objc] view plaincopy

Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
  1. #include <stdio.h>  
  2. #include <pcap.h>  
  3. #include <arpa/inet.h>  
  4. #include <time.h>  
  5. #include <stdlib.h>  
  6. struct ether_header  
  7. {  
  8.     unsigned char ether_dhost[6];   //目的mac  
  9.     unsigned char ether_shost[6];   //源mac  
  10.     unsigned short ether_type;      //以太網類型  
  11. };  
  12. #define BUFSIZE 1514  
  13. int main(int argc,charchar *argv[])  
  14. {  
  15.     pcap_t * pcap_handle = NULL;  
  16.     char error_content[100] = "";   // 出錯資訊  
  17.     const unsigned charchar *p_packet_content = NULL;       // 儲存接收到的資料包的起始位址  
  18.     unsigned charchar *p_mac_string = NULL;         // 儲存mac的位址,臨時變量  
  19.     unsigned short ethernet_type = 0;           // 以太網類型  
  20.     charchar *p_net_interface_name = NULL;      // 接口名字  
  21.     struct pcap_pkthdr protocol_header;  
  22.     struct ether_header *ethernet_protocol;  
  23.     //獲得接口名  
  24.     p_net_interface_name = pcap_lookupdev(error_content);  
  25.     if(NULL == p_net_interface_name)  
  26.     {  
  27.         perror("pcap_lookupdev");  
  28.         exit(-1);  
  29.     }  
  30.     //打開網絡接口  
  31.     pcap_handle = pcap_open_live(p_net_interface_name,BUFSIZE,1,0,error_content);  
  32.     p_packet_content = pcap_next(pcap_handle,&protocol_header);  
  33.     printf("------------------------------------------------------------------------\n");  
  34.     printf("capture a Packet from p_net_interface_name :%s\n",p_net_interface_name);  
  35.     printf("Capture Time is :%s",ctime((const time_ttime_t *)&protocol_header.ts.tv_sec));  
  36.     printf("Packet Lenght is :%d\n",protocol_header.len);  
  37.     ethernet_protocol = (struct ether_header *)p_packet_content;  
  38.     p_mac_string = (unsigned charchar *)ethernet_protocol->ether_shost;//擷取源mac  
  39.     printf("Mac Source Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(p_mac_string+0),*(p_mac_string+1),*(p_mac_string+2),*(p_mac_string+3),*(p_mac_string+4),*(p_mac_string+5));  
  40.     p_mac_string = (unsigned charchar *)ethernet_protocol->ether_dhost;//擷取目的mac  
  41.     printf("Mac Destination Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(p_mac_string+0),*(p_mac_string+1),*(p_mac_string+2),*(p_mac_string+3),*(p_mac_string+4),*(p_mac_string+5));  
  42.     ethernet_type = ntohs(ethernet_protocol->ether_type);  
  43.     printf("Ethernet type is :%04x\t",ethernet_type);  
  44.     switch(ethernet_type)  
  45.     {  
  46.         case 0x0800:printf("The network layer is IP protocol\n");break;//ip  
  47.         case 0x0806:printf("The network layer is ARP protocol\n");break;//arp  
  48.         case 0x0835:printf("The network layer is RARP protocol\n");break;//rarp  
  49.         default:printf("The network layer unknow!\n");break;  
  50.     }  
  51.     pcap_close(pcap_handle);  
  52.     return 0;  
  53. }  

注意:gcc 編譯時需要加上 -lpcap,運作時需要使用超級權限

Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包

例子2(接收多個資料包):

[objc] view plaincopy

Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
  1. #include <stdio.h>  
  2. #include <pcap.h>  
  3. #include <arpa/inet.h>  
  4. #include <time.h>  
  5. #include <stdlib.h>  
  6. #define BUFSIZE 1514  
  7. struct ether_header  
  8. {  
  9.     unsigned char ether_dhost[6];   //目的mac  
  10.     unsigned char ether_shost[6];   //源mac  
  11.     unsigned short ether_type;      //以太網類型  
  12. };  
  13. void ethernet_protocol_callback(unsigned charchar *argument,const struct pcap_pkthdr *packet_heaher,const unsigned charchar *packet_content)  
  14. {  
  15.     unsigned charchar *mac_string;              //  
  16.     struct ether_header *ethernet_protocol;  
  17.     unsigned short ethernet_type;           //以太網類型  
  18.     printf("----------------------------------------------------\n");  
  19.     printf("%s\n", ctime((time_ttime_t *)&(packet_heaher->ts.tv_sec))); //轉換時間  
  20.     ethernet_protocol = (struct ether_header *)packet_content;  
  21.     mac_string = (unsigned charchar *)ethernet_protocol->ether_shost;//擷取源mac位址  
  22.     printf("Mac Source Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(mac_string+0),*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));  
  23.     mac_string = (unsigned charchar *)ethernet_protocol->ether_dhost;//擷取目的mac  
  24.     printf("Mac Destination Address is %02x:%02x:%02x:%02x:%02x:%02x\n",*(mac_string+0),*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));  
  25.     ethernet_type = ntohs(ethernet_protocol->ether_type);//獲得以太網的類型  
  26.     printf("Ethernet type is :%04x\n",ethernet_type);  
  27.     switch(ethernet_type)  
  28.     {  
  29.         case 0x0800:printf("The network layer is IP protocol\n");break;//ip  
  30.         case 0x0806:printf("The network layer is ARP protocol\n");break;//arp  
  31.         case 0x0835:printf("The network layer is RARP protocol\n");break;//rarp  
  32.         default:break;  
  33.     }  
  34.     usleep(800*1000);  
  35. }  
  36. int main(int argc, charchar *argv[])  
  37. {  
  38.     char error_content[100];    //出錯資訊  
  39.     pcap_t * pcap_handle;  
  40.     unsigned charchar *mac_string;                
  41.     unsigned short ethernet_type;           //以太網類型  
  42.     charchar *net_interface = NULL;                 //接口名字  
  43.     struct pcap_pkthdr protocol_header;  
  44.     struct ether_header *ethernet_protocol;  
  45.     //擷取網絡接口  
  46.     net_interface = pcap_lookupdev(error_content);  
  47.     if(NULL == net_interface)  
  48.     {  
  49.         perror("pcap_lookupdev");  
  50.         exit(-1);  
  51.     }  
  52.     pcap_handle = pcap_open_live(net_interface,BUFSIZE,1,0,error_content);//打開網絡接口  
  53.     if(pcap_loop(pcap_handle,-1,ethernet_protocol_callback,NULL) < 0)  
  54.     {  
  55.         perror("pcap_loop");  
  56.     }  
  57.     pcap_close(pcap_handle);  
  58.     return 0;  
  59. }  

運作情況如下:

Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包

過濾資料包

我們抓到的資料包往往很多,如何過濾掉我們不感興趣的資料包呢?

幾乎所有的作業系統( BSD, AIX, Mac OS, Linux 等)都會在核心中提供過濾資料包的方法,主要都是基于 BSD Packet Filter( BPF ) 結構的。libpcap 利用 BPF 來過濾資料包。

1)設定過濾條件

BPF 使用一種類似于彙編語言的文法書寫過濾表達式,不過 libpcap 和 tcpdump 都把它封裝成更進階且更容易的文法了,具體可以通過 man tcpdump檢視:

Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包

以下是一些例子:

src host 192.168.1.177

隻接收源 ip 位址是 192.168.1.177 的資料包

dst port 80

隻接收 tcp/udp 的目的端口是 80 的資料包

not tcp

隻接收不使用 tcp 協定的資料包

tcp[13] == 0x02 and (dst port 22 or dst port 23)

隻接收 SYN 标志位置位且目标端口是 22 或 23 的資料包( tcp 首部開始的第 13 個位元組)

icmp[icmptype] == icmp-echoreply or icmp[icmptype] == icmp-echo

隻接收 icmp 的 ping 請求和 ping 響應的資料包

ehter dst 00:e0:09:c1:0e:82

隻接收以太網 mac 位址是 00:e0:09:c1:0e:82 的資料包

ip[8] == 5

隻接收 ip 的 ttl=5 的資料包(ip首部開始的第8個位元組)

2)編譯 BPF 過濾規則

int pcap_compile(  pcap_t *p,

struct bpf_program *fp,

char *buf,

int optimize,

bpf_u_int32 mask )

功能:

編譯 BPF 過濾規則

參數:

p:pcap_open_live() 傳回的 pcap_t 類型的指針

fp:存放編譯後的 bpf,應用過濾規則時需要用到這個指針

buf:過濾條件

optimize:是否需要優化過濾表達式

mask:指定本地網絡的網絡掩碼,不需要時可寫 0

傳回值:

成功傳回 0,失敗傳回 -1

3)應用 BPF 過濾規則

int pcap_setfilter( pcap_t * p,  struct bpf_program * fp )

功能:

應用 BPF 過濾規則,簡單了解為讓過濾生效

參數:

p:pcap_open_live() 傳回的 pcap_t 類型的指針

fp:pcap_compile() 的第二個參數

傳回值:

成功傳回 0,失敗傳回 -1

這個編譯應用過程,有點類似于,我們寫一個 C 程式,先編譯,後運作的過程。

應用完過濾表達式之後我們便可以使用 pcap_loop() 或 pcap_next() 等抓包函數來抓包了。

下面的程式示範了如何過濾資料包,我們隻接收目的端口是 80 的資料包:

[objc] view plaincopy

Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
Linux 網絡程式設計—— libpcap 詳解概述libpcap 的抓包架構抓包詳細步驟過濾資料包
  1. #include <pcap.h>  
  2. #include <time.h>  
  3. #include <stdlib.h>  
  4. #include <stdio.h>  
  5. void getPacket(u_char * arg, const struct pcap_pkthdr * pkthdr, const u_char * packet)  
  6. {  
  7.   intint * id = (intint *)arg;  
  8.   printf("id: %d\n", ++(*id));  
  9.   printf("Packet length: %d\n", pkthdr->len);  
  10.   printf("Number of bytes: %d\n", pkthdr->caplen);  
  11.   printf("Recieved time: %s", ctime((const time_ttime_t *)&pkthdr->ts.tv_sec));   
  12.   int i;  
  13.   for(i=0; i<pkthdr->len; ++i)  
  14.   {  
  15.     printf(" %02x", packet[i]);  
  16.     if( (i + 1) % 16 == 0 )  
  17.     {  
  18.       printf("\n");  
  19.     }  
  20.   }  
  21.   printf("\n\n");  
  22. }  
  23. int main()  
  24. {  
  25.   char errBuf[PCAP_ERRBUF_SIZE], * devStr;  
  26.   devStr = pcap_lookupdev(errBuf);  
  27.   if(devStr)  
  28.   {  
  29.     printf("success: device: %s\n", devStr);  
  30.   }  
  31.   else  
  32.   {  
  33.     printf("error: %s\n", errBuf);  
  34.     exit(1);  
  35.   }  
  36.   pcap_t * device = pcap_open_live(devStr, 65535, 1, 0, errBuf);  
  37.   if(!device)  
  38.   {  
  39.     printf("error: pcap_open_live(): %s\n", errBuf);  
  40.     exit(1);  
  41.   }  
  42.   struct bpf_program filter;  
  43.   pcap_compile(device, &filter, "dst port 80", 1, 0);  
  44.   pcap_setfilter(device, &filter);  
  45.   int id = 0;  
  46.   pcap_loop(device, -1, getPacket, (u_char*)&id);  
  47.   pcap_close(device);  
  48.   return 0;  
  49. }  

源代碼下載下傳請點此處。

繼續閱讀