分享

pcap文件解析

 mediatv 2014-03-15

前段时间接到一个公司关于解析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
  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
  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

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多