아래코드는 리눅스에서 pcap hex값을 변환시켜주는 프로그램이있길래 변환된hex를 분석해서  간편하게 만든것이다.

초간단 심플코드이며  다른건 건드리지말고 hex값만넣으면됀다..


#include <Windows>

 

//.cpp


Hex2PcapIncode::Hex2PcapIncode()
{
 ZeroMemory(&m_wsf,sizeof(WSFormat));

 m_wsf.Magic           = 0xA1B2C3D4;
 m_wsf.MajorVer[0]     = 0x02; 
 m_wsf.MinerVer[0]     = 0x04;
 m_wsf.unknown         = 0x00018000;
 m_wsf.capsulationtype = 01;
}

Hex2PcapIncode::Hex2PcapIncode(WSFormat Data)
{
 ZeroMemory(&m_wsf,sizeof(WSFormat));

 m_wsf.Magic           = Data.Magic;
 m_wsf.zero[0]         = Data.zero[0];
 m_wsf.zero[1]         = Data.zero[1];
 m_wsf.MajorVer[0]     = Data.MajorVer[0];
 m_wsf.MajorVer[1]     = Data.MajorVer[1];
 m_wsf.MinerVer[0]     = Data.MinerVer[0];
 m_wsf.MinerVer[1]     = Data.MinerVer[1];
 m_wsf.unknown         = Data.unknown;
 m_wsf.capsulationtype = Data.capsulationtype;
}

void Hex2PcapIncode::CreatePcapFile(char * filepath)
{
 m_fp = fopen(filepath,"wb");
 fwrite(&m_wsf,1,sizeof(WSFormat),m_fp);
}

void Hex2PcapIncode::ClosePcapFile()
{
 fclose(m_fp);
}

void Hex2PcapIncode::AddPacketitem(unsigned long hightime,unsigned long lowtime ,unsigned char * pdata,int datasize)
{
 //Sleep(1000);
 ZeroMemory(&m_Frame,sizeof(PacketFrame));
 m_Frame.CapturedLen    = datasize;
 m_Frame.PacketLen      = datasize;
 m_Frame.TimeStampHigh  = hightime; //32bit unsigned
 m_Frame.TimeStampLow   = lowtime;  //32bit unsigned

 fwrite(&m_Frame,1,sizeof(PacketFrame),m_fp);
 fwrite(pdata,1,datasize,m_fp);
}

 

 

 

 

 

 

 

 

 

//.h

 

 

#ifndef HEX2PCAP_FORMAT_H
#define HEX2PCAP_FORMAT_H

struct WSFormat
{
 unsigned int  Magic ; //1AB2C3D4
    unsigned char MajorVer[2] ; //0x02 0x00
 unsigned char MinerVer[2] ; //0x04 0x00
 unsigned int  zero[2];      //0x00000000 00000000
 unsigned int  unknown;//....?
 unsigned int  capsulationtype; //1
};


/*
   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

0 |                        Timestamp (High)                       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
4 |                        Timestamp (Low)                        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
8 |                         Captured Len                          |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12 |                          Packet Len                           |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16 /                                                               /
   /                          Packet Data                          /
   /             (variable length, aligned to 32 bits)             /
   /                                                               /
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */

struct PacketFrame
{
 unsigned int TimeStampHigh; 
 unsigned int TimeStampLow ;
 unsigned int CapturedLen;
 unsigned int PacketLen;
};


class Hex2PcapIncode
{
private :
 WSFormat m_wsf;
 PacketFrame m_Frame;
 FILE * m_fp;
 
public :
  Hex2PcapIncode ();
  Hex2PcapIncode (WSFormat /*WireSharkGlobalBlock*/ );
void CreatePcapFile(char * filepath = "F:\\TEST.pcap");
void ClosePcapFile();
void AddPacketitem(unsigned long hightime,unsigned long lowtime ,unsigned char * pdata,int datasize);

};

#endif /*HEX2PCAP_FORMAT_H*/

 

 

 

//사용법

 

1.클래스를 만든다

2. CreatePcapFile을 호출한다 파일경로명 넣어서(풀경로)

3. hex데이터와 시간값 그리고 데이타 크기를 넣어준다(시간값은 high는 time() 쓰면되고 low는 time을 잘쪼개서쓰면된다)

4. ClosePcapFile로 닫는다

 

기타. 나머지 옵션은 건드리지마라 기본디폴트로 설정되어있다. (지식이있다면 바꾸는것 무방 본인도 전부이해하지 않음) 그저 pdata에 올바른 hex값만 넣는것만 신경써라 data사이즈는 hex값크기이다(오래되서 기억이잘안남)

ps. hex값의 크기는 예를들어 char data[120] = {0x00,0x01 ....}; // 120이 hex의 크기가 되며 배열과 데이터의 크기는 정확하게 일치해야한다


//옛날에 만든거라 뭔가 말투가 


+ Recent posts