包分析(原始通訊端七)

來源:互聯網
上載者:User

標籤:des   style   class   code   http   tar   

緊接上節,DecodeIpPack()函數完成包的解析:

//IP包解析
int DecodeIpPack(char *buf, int iBufSize)
{
 IP_HEADER *pIpheader;
 int iProtocol, iTTL;
 char szProtocol[MAX_PROTO_TEXT_LEN];
 char szSourceIP[MAX_ADDR_LEN], szDestIP[MAX_ADDR_LEN];
 SOCKADDR_IN saSource, saDest;
 pIpheader = (IP_HEADER*)buf;
 //Check Proto
 iProtocol = pIpheader->proto;
 strncpy(szProtocol, CheckProtocol(iProtocol), MAX_PROTO_TEXT_LEN);
 if ((iProtocol == IPPROTO_TCP) && (!ParamTcp))
  return true;
 if ((iProtocol == IPPROTO_UDP) && (!ParamUdp))
  return true;
 if ((iProtocol == IPPROTO_ICMP) && (!ParamIcmp))
  return true;
 //Check Source IP
 saSource.sin_addr.s_addr = pIpheader->sourceIP;
 strncpy(szSourceIP, inet_ntoa(saSource.sin_addr), MAX_ADDR_LEN);
 if (strFromIpFilter)
 if (strcmp(strFromIpFilter, szSourceIP))
  return true;
 //Check Dest IP
 saDest.sin_addr.s_addr = pIpheader->destIP;
 strncpy(szDestIP, inet_ntoa(saDest.sin_addr), MAX_ADDR_LEN);
 if (strDestIpFilter)
  if (strcmp(strDestIpFilter, szDestIP))
   return true;
  iTTL = pIpheader->ttl;
  //Output
  printf("%s ", szProtocol);
  printf("%s->%s ", szSourceIP, szDestIP);
  printf("bytes=%d TTL=%d ", iBufSize, iTTL);
  //Calculate IP Header Length
  int iIphLen = sizeof(unsigned long)*(pIpheader->h_lenver &0xf);
  //Decode Sub Protocol:TCP, UDP, ICMP, etc
 switch (iProtocol)
 {
  case IPPROTO_TCP:
   DecodeTcpPack(buf + iIphLen);
   break;
  case IPPROTO_UDP:
   DecodeUdpPack(buf + iIphLen);
   break;
  case IPPROTO_ICMP:
   DecodeIcmpPack(buf + iIphLen);
   break;
  default:
   break;
 }
 return true;
}


  上述程式解析IP包類型後又分別調用DecodeTcpPack()、DecodeUdpPack()、DecodeIcmpPack()解析相應的TCP報文、UDP報文和ICMP報文。

//TCP報文解析
int DecodeTcpPack(char *TcpBuf)
{
 TCP_HEADER *pTcpHeader;
 int i;
 pTcpHeader = (TCP_HEADER*)TcpBuf;
 printf("Port:%d->%d ", ntohs(pTcpHeader->th_sport), ntohs(pTcpHeader->th_dport));
 unsigned char FlagMask = 1;
 for (i = 0; i < 6; i++)
 {
  if ((pTcpHeader->th_flag) &FlagMask)
   printf("%c", TcpFlag[i]);
  else
   printf("-");
  FlagMask = FlagMask << 1;
 }
 printf("\n");
 return true;

//UDP報文解析
int DecodeUdpPack(char *UdpBuf)
{
 UDP_HEADER *pUdpHeader;
 pUdpHeader = (UDP_HEADER*)UdpBuf;
 printf("Port:%d->%d ", ntohs(pUdpHeader->uh_sport), ntohs(pUdpHeader->uh_dport));
 printf("Len=%d\n", ntohs(pUdpHeader->uh_len));
 return true;
}

//ICMP報文解析 
int DecodeIcmpPack(char *IcmpBuf)
{
 ICMP_HEADER *pIcmpHeader;
 pIcmpHeader = (ICMP_HEADER*)IcmpBuf;
 printf("Type:%d,%d ", pIcmpHeader->i_type, pIcmpHeader->i_code);
 printf("ID=%d SEQ=%d\n", pIcmpHeader->i_id, pIcmpHeader->i_seq);
 return true;
}


  上述程式分析了具體的TCP、UDP和ICMP前序,解析出源地址、目標地址、源連接埠、目標連接埠、ICMP控制資訊類型和代碼等。當然,我們也可以進一步分析報文的資料域,或進行應用程式層解析,從而可獲知任何資訊(如果資訊未採用任何加密手段),包括:

  1. 區域網路上的其他使用者在訪問什麼網站; 

  2. 區域網路上的其他使用者在QQ、MSN上發送和接收什麼內容;

  3. 區域網路上的使用者網路遊戲的遊戲資訊;

  4. 沒有加密的銀行卡賬戶、密碼等。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.