我用Winpcap+MFC+VC做一个小程序,现在可以捕获了流经网卡的数据包(比如满足TCP协议或UDP协议的),包头部分我可以分析了,能得到诸如源IP、目的IP、使用协议等等信息,但是数据包的数据部分我不会分析。我现在只希望可以分析http应用的各种信息(比如URL等),请问我该怎么做呢?
    请说的详细点,谢谢!

解决方案 »

  1.   

    //获得以太网头 ip数据包头 tcp头位置
    Ip_Header* ipHeader;
    Tcp_Header* tcpHeader;
    unsigned int ipHeaderLen;
    unsigned short sport, dport;

    //Ethernet frame 
    Ether_Header* eh = (Ether_Header*)pkt_data;
    //获得IP数据包头部的位置
    ipHeader = (Ip_Header*)(pkt_data + 14);   //length of ethernet header 
    CString sDSTIP = _T("");
    sDSTIP.Format("%d.%d.%d.%d",ipHeader->ipSourceByte.byte1,ipHeader->ipSourceByte.byte2
    ,ipHeader->ipSourceByte.byte3,ipHeader->ipSourceByte.byte4);//不是本机发送的包,不予处理,直接返回
    if(sSelectAdapterIP != sDSTIP)
    return;//获得TCP首部的位置
    ipHeaderLen = (ipHeader->iphVerLen & 0xf) * 4; //length of ip header
    tcpHeader = (Tcp_Header*)(pkt_data + 14 + ipHeaderLen);// 将端口信息从网络型转变为主机顺序
    sport = ntohs(tcpHeader->sourcePort);
    dport = ntohs(tcpHeader->destinationPort);UCHAR Flags = tcpHeader->flags; //得到Tcp数据部分的位置
    unsigned int TcpHeadLen = ((tcpHeader->headerLen_unUsed & 0xf0) >> 4) * 4; // 高4位表示数据偏移
    unsigned char* HttpData = (unsigned char*)tcpHeader+TcpHeadLen;unsigned int IpLength = ntohs(ipHeader->ipLength);
    unsigned int HttpDataLen = IpLength - ipHeaderLen - TcpHeadLen;sHttpData = HttpData;
      

  2.   

    主要就是参考TCP/IP协议,以及HTTP包格式来取对应的数据部分
      

  3.   

    主要还是TCP协议的组包
    IP层得到的包不一定是顺序的
    所以要根据那些序列号把包组合成一串TCP包