pcap文件解析(二)--初识IP包

来源:互联网 发布:卡巴斯基mac版激活码 编辑:程序博客网 时间:2024/05/16 16:14

转载自:http://blog.csdn.net/yhangleo/article/details/8486927


在上一篇我们简单认识pcap文件,现在我们来看看IP包的大致结构。

IP包

在开始之前给大家推荐一个非常好用的工具RFCView,通过这个工具我们只需要输入RFC(Request For Comments,基本的因特网通讯协定都有在RFC文件内详细说明)号码就能查看各种RFC文档了。
在RFC791中详细定义了IP包的数据结构,这里做大致介绍:
 0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| IHL |Type of Service| Total Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identification |Flags| Fragment Offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Time to Live | Protocol | Header Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Version:版本号 4位
IHL:包头长度,IHL并不直接表示包头所占用的长度,而是表示包头占用了多少个32位 4位
Type of Service: 服务类型,为在数据传输中选择具体网络类型的摘要描述。 8位
Total Length: 总长度 16位
Identification: 用于组装数据包的标识符 16位
Flags:控制表示 3位
  Bit 0: reserved, must be zero
Bit 1: (DF) 0 = May Fragment, 1 = Don't Fragment.
Bit 2: (MF) 0 = Last Fragment, 1 = More Fragments.

Fragment Offset:数据偏移 13位
 Time to Live:生命周期 8位
Protocol:协议类型,具体定义见:RFC790文档 8位
Header Checksum:头校验码 16位
Source Address:源地址 32位
Destination Address:目的地址 32位

IP数据在PCAP文件中的位置

pcap头数据包头目的MAC、源MAC、TYPE(14字节)IP数据……数据包头目的MAC、源MAC、TYPE(14字节)IP数据……
根据上表……在每个IP数据包中,IP数据的起始位置=数据包起始位置+包头长度+14。

解析IP数据

下面是一个重pacp文件中导出SCTP协议数据的程序。

pcap_header.h
[cpp] view plaincopy
  1. #pragma pack( push, 1)  
  2. // 为了保证在windows和linux下都能正常编译,放弃使用INT64或者_int_64  
  3. typedef unsigned short _Int16;  
  4. typedef unsigned long  _Int32;  
  5. typedef unsigned char Byte;  
  6. typedef int  bool;  
  7. #define false 1  
  8. #define true  0  
  9.   
  10. // Pcap文件头  
  11. struct __file_header  
  12. {  
  13.     _Int32  iMagic;  
  14.     _Int16  iMaVersion;  
  15.     _Int16  iMiVersion;  
  16.     _Int32  iTimezone;  
  17.     _Int32  iSigFlags;  
  18.     _Int32  iSnapLen;  
  19.     _Int32  iLinkType;  
  20. };  
  21. typedef struct __file_header __pcap_header;  
  22.   
  23. // 数据包头  
  24. struct __pkthdr  
  25. {  
  26.     _Int32      iTimeSecond;  
  27.     _Int32      iTimeSS;  
  28.     _Int32      iPLength;  
  29.     _Int32      iLength;  
  30. };  
  31.   
  32. typedef struct __pkthdr __pk_header;  
  33.   
  34. struct __iphdr  
  35. {  
  36.    Byte    byteVersions:4;  
  37.    Byte    byteHdLength:4;  
  38.     Byte    byteSerType;  
  39.     _Int16  iTotalLength;  
  40.     _Int16  iIdentification;  
  41.     union  
  42.     {  
  43.         _Int16  iFlags;  
  44.         _Int16  iFragmentOffset;  
  45.     };  
  46.     Byte    byteTimeToLive;  
  47.     Byte    byteProtocol;  
  48.     _Int16  iHeaderChecksum;  
  49.     _Int32  iSourceAddress;  
  50.     _Int32  iDestinationAddress;  
  51. };  
  52.   
  53. // Flags  
  54. #define IP_DF 0x4000  
  55. #define IP_MF 0x2000  
  56. // FragmentOffset mask  
  57. #define IP_OFFMASK 0x1fff  
  58.   
  59. typedef struct __iphdr __ip_header;  
  60.   
  61. #pragma pack( pop)  

pcap_adapter.h
[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include "pcap_header.h"  
  3.   
  4. // 打开Pcap文件  
  5. bool OpenPcapFile( char* strPath);  
  6.   
  7. // 获得Pcap文件头  
  8. void GetPcapHeader( __pcap_header* pHeader);  
  9.   
  10. // 获得当前包内容 返回值为buffer长度,buffer不含头信息, 获得信息后会自动跳转到下个包头  
  11. int GetPacketAndMoveNext( __pk_header* pPacket, Byte** pBuffer);  
  12.   
  13. // 移动到第一个包的位置 读取包数据前需要优先调用这个函数  
  14. bool MoveFirst();  
  15.   
  16. // 是否已经到达文件尾  
  17. bool IeEof();  
  18.   
  19. // 获取ip信息  
  20. void GetIpData( /*out*/ __ip_header* pIpData , /*IN*/ Byte* pDataBuffer);  

pcap_adapter.c
[cpp] view plaincopy
  1. #include "pcap_adapter.h"  
  2. #include "value_checker.h"  
  3.   
  4.   
  5. #define NULL 0  
  6.   
  7. #define CHECKFILE  if( m_gpPcapFile == NULL)         \  
  8.                     {                               \  
  9.                         printf( "未打开文件");       \  
  10.                         return ;                     \  
  11.                     }  
  12.   
  13. #define CHECKFILEEX(x) if( m_gpPcapFile == NULL)         \  
  14.                     {                               \  
  15.                         printf( "未打开文件");       \  
  16.                         return x;                     \  
  17.                     }  
  18.   
  19. #define NTETOHOST_SHORT(x) x = t_ntohs(x)  
  20. #define NTETOHOST_LONG(x)  x = t_ntohl(x)  
  21.   
  22. FILE* m_gpPcapFile = NULL;  
  23. int   m_giFileLength = 0;  
  24.   
  25. bool OpenPcapFile( char* strPath)  
  26. {  
  27.     m_gpPcapFile = fopen( strPath, "rb");  
  28.     CHECKFILEEX(false);  
  29.   
  30.     fseek( m_gpPcapFile, 0, SEEK_END);  
  31.     m_giFileLength = ftell( m_gpPcapFile);  
  32.     fseek( m_gpPcapFile, 0, SEEK_SET);  
  33.   
  34.     return true;  
  35. }  
  36.   
  37. void GetPcapHeader( __pcap_header* pHeader)  
  38. {  
  39.     CHECKFILE;  
  40.     // 保存当前游标位置  
  41.     int iNowPos = ftell( m_gpPcapFile);  
  42.     fseek( m_gpPcapFile, 0, SEEK_SET);  
  43.     fread( (void*)pHeader, sizeof( __pcap_header), 1,m_gpPcapFile);  
  44.     fseek( m_gpPcapFile, iNowPos, SEEK_SET);  
  45. }  
  46.   
  47. bool MoveFirst()  
  48. {  
  49.     CHECKFILEEX(false);  
  50.     return fseek( m_gpPcapFile, sizeof( __pcap_header), SEEK_SET) == 0;  
  51. }  
  52.   
  53. int GetPacketAndMoveNext( __pk_header* pPacket, Byte** pBuffer)  
  54. {  
  55.     CHECKFILEEX(0);  
  56.     fread( (void*)pPacket, sizeof( __pk_header), 1,m_gpPcapFile);  
  57.     *pBuffer = (Byte*)malloc( pPacket->iLength);  
  58.     fread( (void*)*pBuffer, pPacket->iLength, 1,m_gpPcapFile);  
  59.   
  60.     return pPacket->iLength;  
  61. }  
  62.   
  63. bool IeEof()  
  64. {  
  65.     CHECKFILEEX(false);  
  66.     int iNowPos = ftell( m_gpPcapFile);  
  67.   
  68.     return iNowPos >= m_giFileLength;  
  69. }  
  70.   
  71. void GetIpData( /*out*/ __ip_header* pIpData , /*IN*/ Byte* pDataBuffer)  
  72. {  
  73.     memcpy( (void*)pIpData, pDataBuffer + 14, sizeof( __ip_header));  
  74.   
  75.     NTETOHOST_SHORT(pIpData->iTotalLength);  
  76.     NTETOHOST_SHORT( pIpData->iIdentification);  
  77.     NTETOHOST_SHORT( pIpData->iFragmentOffset);  
  78.     NTETOHOST_SHORT( pIpData->iHeaderChecksum);  
  79.     NTETOHOST_LONG( pIpData->iSourceAddress);  
  80.     NTETOHOST_LONG( pIpData->iDestinationAddress);  
  81. }  

main.c
[cpp] view plaincopy
  1. #include"pcap_adapter.h"  
  2.   
  3. int main()  
  4. {  
  5.     if( OpenPcapFile( "ZSRNC3_IUPS_inerface3.pcap"))  
  6.     {  
  7.         printf( "打开pcap文件失败");  
  8.         return 0;  
  9.     }  
  10.     __pcap_header header;  
  11.     GetPcapHeader( &header);  
  12.   
  13.     int iNo = 1;  
  14.     MoveFirst();  
  15.     FILE* pwFile = fopen( "export-file.pcap""wb");  
  16.     fwrite((void*)&header, sizeof( __pcap_header), 1, pwFile);  
  17.     while( !IeEof())  
  18.     {  
  19.         __pk_header data;  
  20.         __ip_header ipData;  
  21.         Byte* pBuffer;  
  22.         GetPacketAndMoveNext( &data, &pBuffer);  
  23.         GetIpData( &ipData, pBuffer);  
  24.   
  25.         // SCTP == 132  
  26.         if( ipData.byteProtocol == 132)  
  27.         {  
  28.             fwrite( (void*)(&data), sizeof(struct __pkthdr), 1, pwFile);  
  29.             fwrite( (void*)pBuffer, data.iLength, 1, pwFile);  
  30.         }  
  31.   
  32.         free( pBuffer);  
  33.     }  
  34.   
  35.     fclose( pwFile);  
  36.     printf( "Export over");  
  37.     return 1;  
  38. }  


0 0
原创粉丝点击