libpcap的简单使用--抓取特定类型和端口的网络数据
生活随笔
收集整理的這篇文章主要介紹了
libpcap的简单使用--抓取特定类型和端口的网络数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
[cpp]?view plaincopy
#include??? #include??? #include??? #include??? #include??? #include??? #include??? #include??? #include??? #include??? #include??? ?? using?std::cout;?? using?std::endl;?? using?std::thread;?? using?std::vector;?? using?std::string;?? ?? ?? //解析數據包?? void?getPacket(u_char?*?arg,const?struct?pcap_pkthdr?*pkthdr,const?u_char?*?packet){?? ????unsigned?char?src_mac[18]?=?"";?? ????unsigned?char?dst_mac[18]?=?"";?? ????unsigned?char?src_addr[20]?=?"";?????? ????unsigned?char?dst_addr[20]?=?"";?? ?????? ????unsigned?char?head_str[50]?=?"";?? ????unsigned?char?body_str[512]?=?"";?? ?? ????vector?split_vector;?? ????char?*p?=?NULL;?? ????const?char?*split?=?"|";?? ?? ????int?*id?=?(int?*)arg;?? ????cout?<<?"id:?"?<<?++(*id)?<<?endl;?? ????cout?<<?"Packet?length:?"?<<?pkthdr->len?<<?endl;?? ????cout?<<?"Number?of?bytes:?"?<<?pkthdr->caplen?<<?endl;?? ????cout?<<?"Recieved?time:?"?<<?ctime((const?time_t?*)&pkthdr->ts.tv_sec);?? ?? ????if?(pkthdr->len?!=?94)?? ????{?? ????????cout?<<?"wifi?TanZhen?message?length?error."?<<?endl;?? ????????exit(1);?? ????}?? ?????? ????memcpy(head_str,?(char?*)packet,?42);?? ????memcpy(body_str,?(char?*)packet?+?42,?52);?? ????sprintf((char?*)dst_mac,?"%02x:%02x:%02x:%02x:%02x:%02x",?head_str[0],?head_str[1],?head_str[2],?head_str[3],?head_str[4],?head_str[5]);?????? ????sprintf((char?*)src_mac,?"%02x:%02x:%02x:%02x:%02x:%02x",?head_str[6],?head_str[7],?head_str[8],?head_str[9],?head_str[10],?head_str[11]);???? ?? ????//消息頭?? ????if?(head_str[12]?==?0x08?&&?head_str[13]?==?0x00)?? ????{?? ????????printf("____________________IP?Protocol____________________\n");?? ????????printf("MAC:%s?>>?%s\n",?src_mac,?dst_mac);?? ????????sprintf((char?*)src_addr,?"%02d.%02d.%02d.%02d",?head_str[26],?head_str[27],?head_str[28],?head_str[29]);????? ????????sprintf((char?*)dst_addr,?"%02d.%02d.%02d.%02d",?head_str[30],?head_str[31],?head_str[32],?head_str[33]);?? ????????printf("IP:%s?>>?%s\n",?src_addr,?dst_addr);?? ?? ????????if?(head_str[23]?==?0x01)?? ????????{?? ????????????printf("Type:ICMP\n");?? ????????}?? ????????else?if?(head_str[23]?==?0x02)?? ????????{?? ????????????printf("Type:IGMP\n");?? ????????}?? ????????else?if?(head_str[23]?==?0x06)?? ????????{?? ????????????printf("Type:TCP\n");?? ????????}????????? ????????else?if?(head_str[23]?==?0x11)?? ????????{?? ????????????printf("Type:UDP\n");?? ????????}?? ?? ????????printf("Port:?%d?>>?%d\n",?ntohs(*(unsigned?short?*)(head_str?+?34)),?ntohs(*(unsigned?short?*)(head_str?+?36)));?? ????}?? ?? ????//消息體?? ????for?(unsigned?int?i=42;?ilen;?++i)?? ????{?? ????????printf("%c",?*(packet?+?i));?? ????}?? ????cout?<<?endl;?? ?? ????//拆分消息體?? ????p?=?strtok((char?*)body_str,?split);?? ????while(p?!=?NULL){?? ????????split_vector.push_back(p);?? ????????p?=?strtok(NULL,?split);?? ????}?? ?? ????cout?<<?"split?vector?size:"?<<?split_vector.size()?<<?endl;?? ????for?(auto?itr?=?split_vector.cbegin();?itr?!=?split_vector.cend();?itr++){?? ????????cout?<<?*itr?<<?endl;?? ????}?? ?????? ????cout?<<?"-------------------------------------------------------"?<<?endl;?? }?? ?? ?? int?main(int?argc,?char?*argv[]){?? ????char?errBuf[PCAP_ERRBUF_SIZE]?=?{0};?? ????char?*device?=?nullptr;?? ?? ????//獲取網絡接口?? ????device?=?pcap_lookupdev(errBuf);?? ?? ????if?(device){?? ????????cout?<<?"succeed?get?device:?"?<<?device?<<?endl;?? ????}?? ????else{?? ????????cout?<<?"error:?"?<<?errBuf?<<?endl;?? ????????exit(1);?? ????}?? ?? ????//打開網絡接口?? ????pcap_t?*live_device?=?pcap_open_live(device,?65535,?1,?0,?errBuf);//任何一個協議的一個數據包長度必然小于65535,1表示混雜模式,0表示一直等待數據包到來?? ?? ????if?(!live_device){?? ????????cout?<<?"error:?pcap_open_live():?"?<<?errBuf?<<?endl;?? ????????exit(1);?? ????}?? ?????? ????//構造一個過濾器?? ????struct?bpf_program?filter;?? ????//編譯過濾器?? ????pcap_compile(live_device,?&filter,?"udp?dst?port?9900",?1,?0);//在wifi探針平臺設置接收消息的服務器和端口?? ????//設置過濾器?? ????pcap_setfilter(live_device,?&filter);?? ?????? ????//循環獲取數據?? ????int?id?=?0;?? ????pcap_loop(live_device,?-1,?getPacket,?(u_char?*)&id);//-1表示循環抓包??? ?? ????//關閉網絡接口?? ????pcap_close(live_device);?? ?????? ????return?0;?? } ?
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的libpcap的简单使用--抓取特定类型和端口的网络数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux下c语言抓包库libpcap
- 下一篇: 解决VS2017运行时控制台一闪即逝问题