天天看點

Wireshark的Pcap檔案格式分析及解析源碼【轉】

pcap檔案格式是常用的資料報存儲格式,包括wireshark在内的主流抓包軟體都可以生成這種格式的資料包 下面對這種格式的檔案簡單分析一下:    pcap檔案的格式為:

  檔案頭    24位元組

  資料報頭 + 資料報  資料標頭為16位元組,後面緊跟資料報

  資料報頭 + 資料報  ...... pcap.h裡定義了檔案頭的格式

struct pcap_file_header {

        bpf_u_int32 magic;

        u_short version_major;

        u_short version_minor;

        bpf_int32 thiszone;    

        bpf_u_int32 sigfigs;   

        bpf_u_int32 snaplen;   

        bpf_u_int32 linktype;  

};

Wireshark的Pcap檔案格式分析及解析源碼【轉】

Pcap檔案頭24B各字段說明:

Magic:4B:0×1A 2B 3C 4D:用來識别檔案自己和位元組順序。0xa1b2c3d4用來表示按照原來的順序讀取,0xd4c3b2a1表示下面的位元組都要交換順序讀取。一般,我們使用0xa1b2c3d4 Major:2B,0×02 00:目前檔案主要的版本号 Minor:2B,0×04 00目前檔案次要的版本号 ThisZone:4B 時區。GMT和本地時間的相差,用秒來表示。如果本地的時區是GMT,那麼這個值就設定為0.這個值一般也設定為0 SigFigs:4B時間戳的精度;全零 SnapLen:4B最大的存儲長度(該值設定所抓獲的資料包的最大長度,如果所有資料包都要抓獲,将該值設定為65535; 例如:想擷取資料包的前64位元組,可将該值設定為64) LinkType:4B鍊路類型 常用類型: 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

Wireshark的Pcap檔案格式分析及解析源碼【轉】

Packet 標頭和Packet資料組成 字段說明: Timestamp:時間戳高位,精确到seconds(值是自從January 1, 1970 00:00:00 GMT以來的秒數來記) Timestamp:時間戳低位,精确到microseconds (資料包被捕獲時候的微秒(microseconds)數,是自ts-sec的偏移量) Caplen:目前資料區的長度,即抓取到的資料幀長度,由此可以得到下一個資料幀的位置。 Len:離線資料長度:網絡中實際資料幀的長度,一般不大于caplen,多數情況下和Caplen數值相等。 (例如,實際上有一個包長度是1500 bytes(Len=1500),但是因為在Global Header的snaplen=1300有限制,是以隻能抓取這個包的前1300個位元組,這個時候,Caplen = 1300 ) Packet 資料:即 Packet(通常就是鍊路層的資料幀)具體内容,長度就是Caplen,這個長度的後面,就是目前PCAP檔案中存放的下一個Packet資料包,也就 是說:PCAP檔案裡面并沒有規定捕獲的Packet資料包之間有什麼間隔字元串,下一組資料在檔案中的起始位置。我們需要靠第一個Packet包确定。 最後,Packet資料部分的格式其實就是标準的網路協定格式了可以任何網絡教材上找得到。

Wireshark的Pcap檔案格式分析及解析源碼【轉】
Wireshark的Pcap檔案格式分析及解析源碼【轉】

以下是我的實作,針對自定義的UDP的抓封包件進行解析

typedef struct tagIpHead

{

    int version;//版本

    int headLength; //頭長度

    int  diffsever; 

    int  totallength; //總長度

    int  identification; 

    int  flag;

    int  fragment;

    int  ttl;

    int  protocoltype; //協定類型

    int  checksum;

    unsigned long  srcip;//源ip

    unsigned long  dstip;//目的ip

}IP_HEAD;

typedef struct tagUdpHead

    unsigned short  srcport; //源端口

    unsigned short  dstport; //目的端口

    int   length; //udp包長度

}UDP_HEAD;

unsigned long FileParse::Parse( const char* FileName,bool& bThreadRun)//,HWND hwnd )

{

    if (_wass_session)

    {

        delete _wass_session;

        _wass_session = NULL;

    }

    _wass_session = new WassSessions();

    //

    unsigned long lRes =0;

    FILE* pFile=NULL;

    int nReadSize = 0;

    char buff[FILE_READ_LEN];

    char acip[30];

    char portalip[30];

    char radiusip[30];

    unsigned long timestamp1;

    unsigned long timestamp2;

    CConfigure* config=new CConfigure();

    if (config)

    {

        //讀取ip位址,添加到iplist中

        unsigned long ipTmp=0; 

        unsigned short portTmp=0;

        config->getIPConfig(acip,portalip,radiusip);

        cut_ip(acip,ipTmp,portTmp);

        acport_list.push_back(portTmp);

        acip_list.push_back(ipTmp);

        cut_ip(portalip,ipTmp,portTmp);

        portalip_list.push_back(ipTmp);

        portalport_list.push_back(portTmp);

        delete config;

        config = NULL;

    }

    //

    memset(buff,0,FILE_READ_LEN);

    do 

    {

        pFile =fopen(FileName,"rb"); 

        //pFile =_open( FileName, _O_RDONLY | _O_BINARY ); 

        if (!pFile)

        {

            //failed for the file opened

            fprintf(stderr, "Open the file failed:%s ", strerror(errno));

            lRes = 2;

            break;

        }

        nReadSize = fread(buff,sizeof(char),24,pFile);

        if (nReadSize == 24)

        {

            while (!feof(pFile) && bThreadRun) 

            {

                memset(buff,0,FILE_READ_LEN);

                nReadSize = fread(buff,sizeof(char),16,pFile);

                unsigned long nPacketLen=0;

                memcpy(&timestamp1,buff,4);

                memcpy(&timestamp2,buff+4,4);

                memcpy(&nPacketLen,buff+8,4);

                //nPacketLen = ntohl(nPacketLen);

                char* buf = new char[nPacketLen];

                memset(buf,0,nPacketLen);

                int nReadCount=0;

                //讀取包

                while (nReadCount < nPacketLen)

                {

                    nReadSize = fread(buff,sizeof(char),nPacketLen-nReadCount,pFile);

                    memcpy(buf+nReadCount,buff,nReadSize);

                    nReadCount += nReadSize;

                }

                //在此處處理ip/udp包部分

                int nOffset=14;//資料偏移位置

                _ip->Parse(buf+nOffset);//ip解析

                if(_ip->wass_ip_head.protocoltype==17)//隻處理UDP

                {

                    nOffset += 20;

                    _udp->Parse(buf+nOffset);//udp解析

                    nOffset +=8;

                    std::list<unsigned long>::iterator acit= acip_list.begin();

                    std::list<unsigned long>::iterator portalit = portalip_list.begin();

                    bool bFoundIP = false;

                    //暫時不考慮算法,周遊ip位址

                    //while (acit++ != acip_list.end())

                    for (;acit != acip_list.end();acit++)

                    {

                        unsigned long aIP = *acit;

                        char aTmp[20];

                        IPULongToString(aIP,aTmp);

                        IPULongToString(_ip->wass_ip_head.dstip,aTmp);

                        if (_ip->wass_ip_head.dstip== *acit  || _ip->wass_ip_head.srcip == *acit)

                        {

                        for (;portalit !=portalip_list.end();portalit++)

                        {

                            if (_ip->wass_ip_head.dstip== *portalit  || _ip->wass_ip_head.srcip == *portalit)

                            {

                                bFoundIP = true;

                                break;

                            }

                        }

                        break;

                        }

                    }

                    if (bFoundIP)

                    {

                        //此處是表示可以進行資料的解析

                        _portalPacket = new CPortalPacket();

                        _portalPacket->parse(buf + nOffset,nPacketLen - nOffset);

                    //設定包的源IP和目的IP,源端口,目的端口

                        _portalPacket->setIpAndPort(_ip->wass_ip_head.srcip,

                            _ip->wass_ip_head.dstip,_udp->wass_udp_head.srcport,_udp->wass_udp_head.dstport);

                        _portalPacket->setPacketTime(timestamp1,timestamp2); 

                        _wass_session->AddPacket(_portalPacket,_sessions);

                    }

                    else

                    {

                    }

                }

                if (buf)

                {

                    delete [] buf;

                }

            }

        }

    } while (false);

    if (pFile)

    {

        fclose(pFile);

    }

    //

    //::PostMessage(_hwnd,WM_FINISHED,0,0);

    return lRes;

}