pcap文件解析--pcap文件头与包文件头(一)

前段时间接到一个公司关于解析pacp文件的培训(我是被培训的),在完成了一部分的功能后决定把一些关于pcap文件的了解记录到博客中。

初识Pcap文件

在开始读取pcap文件之前,先让我们来看看Pcap文件的大概结构。




如上图所示在一个Pcap文件中存在1个Pcap文件头和多个数据包,其中每个数据包都有自己的头和包内容。
下面我们先看看PCAP文件头每个字段是什么意思:
magic为文件识别头,pcap固定为:0xA1B2C3D4。(4个字节)
magor version为主版本号(2个字节)
minor version为次要版本号(2个字节)
timezone为 当地的标准时间(4个字节)
sigflags为 时间戳的精度(4个字节)
snaplen为 最大的存储长度(4个字节)
linktype为链路类型(4个字节)
常用类型:
0            BSD loopback devices, except for later OpenBSD
1            Ethernet, and Linux loopback devices
6            802.5 Token Ring
7            ARCnet
8            SLIP
9            PPP
10           FDDI
100         LLC/SNAP-encapsulated ATM
101         “raw IP”, with no link
102         BSD/OS SLIP
103         BSD/OS PPP
104         Cisco HDLC
105         802.11
108         later OpenBSD loopback devices (with the AF_value in network byte order)
113         special Linux “cooked” capture
114         LocalTalk

数据包头则依次为:时间戳(秒)、时间戳(微妙)、抓包长度和实际长度,依次各占4个字节。

读取各个数据包到单个pcap文件

c代码:
pcap_header.h
[cpp]  view plain copy
  1. #pragma pack( push, 1)  
  2. // 为了保证在windows和linux下都能正常编译,放弃使用INT64或者_int_64  
  3. typedef short _Int16;  
  4. typedef long  _Int32;  
  5. typedef char Byte;  
  6.   
  7. // Pcap文件头  
  8. struct __file_header  
  9. {  
  10.     _Int32  iMagic;  
  11.     _Int16  iMaVersion;  
  12.     _Int16  iMiVersion;  
  13.     _Int32  iTimezone;  
  14.     _Int32  iSigFlags;  
  15.     _Int32  iSnapLen;  
  16.     _Int32  iLinkType;  
  17. };  
  18.   
  19. // 数据包头  
  20. struct __pkthdr  
  21. {  
  22.     _Int32      iTimeSecond;  
  23.     _Int32      iTimeSS;  
  24.     _Int32      iPLength;  
  25.     _Int32      iLength;  
  26. };  
  27.   
  28. #pragma pack( pop)  

main.c
[cpp]  view plain copy
  1. #include<stdio.h>  
  2. #include"pcap_header.h"  
  3. #include<memory.h>  
  4. #include<stdlib.h>  
  5. #include<math.h>  
  6.   
  7. int main()  
  8. {  
  9.     struct __pkthdr data;  
  10.     struct __file_header    header;  
  11.     FILE* pFile = fopen( "iupsc.pcap""rb");  
  12.     if( pFile == 0)  
  13.     {  
  14.         printf( "打开pcap文件失败");  
  15.         return 0;  
  16.     }  
  17.   
  18.     fseek( pFile, 0, SEEK_END);  
  19.     long iFileLen = ftell( pFile);  
  20.     fseek( pFile, 0, SEEK_SET);  
  21.   
  22.     Byte* pBuffer = (Byte*)malloc( iFileLen);  
  23.     fread( (void*)pBuffer, 1, iFileLen, pFile);  
  24.     fclose( pFile);  
  25.   
  26.     memcpy( (void*)&header, (void*)(pBuffer)  
  27.             , sizeof(struct __file_header));  
  28.   
  29.     int iIndex = sizeof(struct __file_header);  
  30.     int iNo = 1;  
  31.     while(iIndex <= iFileLen)  
  32.     {  
  33.         memcpy( (void*)&data, (void*)(pBuffer + iIndex)  
  34.             , sizeof(struct __pkthdr));  
  35.   
  36.         char strPath[51];  
  37.         sprintf( strPath, "export/%d-%d.pcap", iNo++, (int)data.iTimeSecond);  
  38.         strPath[50] = '\0';  
  39.   
  40.         FILE* pwFile = fopen( strPath, "wb");  
  41.   
  42.         fwrite((void*)&header, 1, sizeof(struct __file_header), pwFile);  
  43.         fwrite( (void*)(pBuffer + iIndex), 1,  
  44.             sizeof(struct __pkthdr) + data.iPLength, pwFile);  
  45.         fclose( pwFile);  
  46.   
  47.   
  48.         iIndex += sizeof(struct __pkthdr) + data.iPLength;  
  49.     }  
  50.   
  51.     free( pBuffer);  
  52.     printf( "成功导出%d个文件", iNo - 1);  
  53.     return 1;  
  54. }  

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Wireshark是一个流行的网络协议分析工具,它允许用户捕获和分析网络数据PCAP文件是Wireshark使用的一种数据格式,用于存储捕获到的网络数据PCAP文件格式具体说明了文件的结构和存储方式,以便于其他工具或程序能够正确地解析和处理这些文件PCAP文件由全局文件(Global Header)和数据(Packet Header)组成。全局文件记录了文件的版本信息、网络接口类型等元数据。每个数据都由数据和数据负载组成。数据含了数据的时间戳、数据长度等信息。 要解析PCAP文件,可以借助Wireshark软件本身或者使用编程语言中的相关库,如libpcap或WinPcap。在Java程序中,可以使用WireShark库进行解析,并在后台查看PCAP的内容。 PCAP文件解析的过程括读取PCAP文件的全局文件,然后逐个读取数据和数据负载,以提取所需的信息。例如,可以通过解析数据中的时间戳和源/目的IP地址来分析网络流量的来源和目标。 总结起来,Wireshark可以使用PCAP文件解析网络数据PCAP文件的结构和格式可以通过参考了解。在Java程序中,可以使用WireShark库进行解析,并在后台查看PCAP的内容。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Wireshark文件pcap的格式详细解析有实例(Global Header、Packet Header)](https://blog.csdn.net/Hollake/article/details/90108950)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [借助WireShark解析PCAP](https://blog.csdn.net/q35222806/article/details/78817811)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值