<del id="7msxa"><table id="7msxa"><strong id="7msxa"></strong></table></del><legend id="7msxa"></legend>

<tbody id="7msxa"></tbody>
  • <ol id="7msxa"></ol><th id="7msxa"><track id="7msxa"><rt id="7msxa"></rt></track></th>
    1. <dd id="7msxa"></dd>
    2. 首頁 > 編程 > Java > 正文

      網絡上的數據報偵聽

      2019-09-06 23:33:16
      字體:
      來源:轉載
      供稿:網友

                          #include <math.h>
      #include <stdio.h>
      #include <string.h>
      #include <Winsock2.h>
      #include <mstcpip.h>

      #define STATUS_FAILED 0xFFFF //定義異常出錯代碼
      #define MAX_PACK_LEN 65535 //接收的最大IP報文
      #define MAX_ADDR_LEN 16 //點分十進制地址的最大長度
      #define MAX_PROTO_TEXT_LEN 16 //子協議名稱(如"TCP")最大長度
      #define MAX_PROTO_NUM 12 //子協議數量
      #define MAX_HOSTNAME_LAN 255 //最大主機名長度
      #define CMD_PARAM_HELP true

      typedef struct _iphdr
      {
      unsigned char h_lenver; //4位首部長度+4位IP版本號
      unsigned char tos; //8位服務類型TOS
      unsigned short total_len; //16位總長度(字節)
      unsigned short ident; //16位標識
      unsigned short frag_and_flags; //3位標志位
      unsigned char ttl; //8位生存時間 TTL
      unsigned char proto; //8位協議 (TCP, UDP 或其他)
      unsigned short checksum; //16位IP首部校驗和
      unsigned int sourceIP; //32位源IP地址
      unsigned int destIP; //32位目的IP地址
      }IP_HEADER;

      typedef struct _tcphdr //定義TCP首部
      {
      USHORT th_sport; //16位源端口
      USHORT th_dport; //16位目的端口
      unsigned int th_seq; //32位序列號
      unsigned int th_ack; //32位確認號
      unsigned char th_lenres; //4位首部長度/6位保留字
      unsigned char th_flag; //6位標志位
      USHORT th_win; //16位窗口大小
      USHORT th_sum; //16位校驗和
      USHORT th_urp; //16位緊急數據偏移量
      }TCP_HEADER;

      typedef struct _udphdr //定義UDP首部
      {
      unsigned short uh_sport; //16位源端口
      unsigned short uh_dport; //16位目的端口
      unsigned short uh_len; //16位長度
      unsigned short uh_sum; //16位校驗和
      } UDP_HEADER;

      typedef struct _icmphdr //定義ICMP首部
      {
      BYTE i_type; //8位類型
      BYTE i_code; //8位代碼
      USHORT i_cksum; //16位校驗和
      USHORT i_id; //識別號(一般用進程號作為識別號)
      USHORT i_seq; //報文序列號
      ULONG timestamp; //時間戳
      }ICMP_HEADER;

      typedef struct _protomap //定義子協議映射表
      {
      int ProtoNum;
      char ProtoText[MAX_PROTO_TEXT_LEN];
      }PROTOMAP;

      PROTOMAP ProtoMap[MAX_PROTO_NUM]={ //為子協議映射表賦值
      { IPPROTO_IP , "IP " },
      { IPPROTO_ICMP , "ICMP" },
      { IPPROTO_IGMP , "IGMP" },
      { IPPROTO_GGP , "GGP " },
      { IPPROTO_TCP , "TCP " },
      { IPPROTO_PUP , "PUP " },
      { IPPROTO_UDP , "UDP " },
      { IPPROTO_IDP , "IDP " },
      { IPPROTO_ND , "NP " },
      { IPPROTO_RAW , "RAW " },
      { IPPROTO_MAX , "MAX " },
      { NULL , "" } };

      SOCKET SockRaw;
      char TcpFlag[6]={'F','S','R','P','A','U'}; //定義TCP標志位
      bool ParamTcp =false; // -t關注TCP 報文
      bool ParamUdp =false; // -u關注UDP 報文
      bool ParamIcmp =false; // -i關注ICMP報文
      bool ParamDecode=false; // -d對協議進行解碼
      char *strFromIpFilter=NULL; // 源IP地址過濾
      char *strDestIpFilter=NULL; // 目的地址過濾
      char *strSensitive=NULL; // 敏感字符串
      int iPortFilter=0; // 端口過濾
      int iProtocol, iTTL;
      char szProtocol[MAX_PROTO_TEXT_LEN];
      char szSourceIP[MAX_ADDR_LEN], szDestIP[MAX_ADDR_LEN];

      int DecodeIpPack(char *, int); //IP解包函數
      int DecodeTcpPack(char *, int); //TCP解包函數
      int DecodeUdpPack(char *, int); //UDP解包函數
      int DecodeIcmpPack(char *, int); //ICMP解包函數
      void CheckSockError(int, char*); //出錯處理函數
      char * CheckProtocol(int); //協議檢查
      void usage(void); //使用說明
      bool GetCmdLine(int, char **); //命令行參數處理



      void main(int argc, char ** argv)
      {
      int iErrorCode;
      char RecvBuf[MAX_PACK_LEN] = {0};
      usage();
      if(GetCmdLine(argc, argv)==CMD_PARAM_HELP) exit(0);
      //初始化SOCKET
      WSADATA wsaData;
      iErrorCode = WSAStartup(MAKEWORD(2,1),&wsaData);
      CheckSockError(iErrorCode, "WSAStartup");
      SockRaw = socket(AF_INET , SOCK_RAW , IPPROTO_IP);
      CheckSockError(SockRaw, "socket");
      //獲取本機IP地址
      char FAR name[MAX_HOSTNAME_LAN];
      iErrorCode = gethostname(name, MAX_HOSTNAME_LAN);
      CheckSockError(iErrorCode, "gethostname");
      struct hostent FAR * pHostent;
      pHostent = (struct hostent * )malloc(sizeof(struct hostent));
      pHostent = gethostbyname(name);
      SOCKADDR_IN sa;
      sa.sin_family = AF_INET;
      sa.sin_port = htons(6000);
      memcpy(&sa.sin_addr.S_un.S_addr, pHostent->h_addr_list[0], pHostent->h_length);
      free(pHostent);
      iErrorCode = bind(SockRaw, (PSOCKADDR)&sa, sizeof(sa));
      CheckSockError(iErrorCode, "bind");
      //設置SOCK_RAW為SIO_RCVALL,以便接收所有的IP包
      DWORD dwBufferLen[10] ;
      DWORD dwBufferInLen = 1 ;
      DWORD dwBytesReturned = 0 ;
      iErrorCode=WSAIoctl(SockRaw, SIO_RCVALL,&dwBufferInLen, sizeof(dwBufferInLen),
      &dwBufferLen, sizeof(dwBufferLen),&dwBytesReturned , NULL , NULL );
      CheckSockError(iErrorCode, "Ioctl");
      //偵聽IP報文
      while(1)
      {
      memset(RecvBuf, 0, sizeof(RecvBuf));
      iErrorCode = recv(SockRaw, RecvBuf, sizeof(RecvBuf), 0);
      CheckSockError(iErrorCode, "recv");
      iErrorCode = DecodeIpPack(RecvBuf, iErrorCode);
      CheckSockError(iErrorCode, "Decode");
      }
      }

      //IP解包程序
      int DecodeIpPack(char *buf, int iBufSize)
      {
      IP_HEADER *pIpheader;
      SOCKADDR_IN saSource, saDest;
      pIpheader = (IP_HEADER *)buf;
      //協議甄別
      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;
      //源地址
      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;
      //目的地址
      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;
      //計算IP首部的長度
      int iIphLen = sizeof(unsigned long) * (pIpheader->h_lenver & 0xf);
      //根據協議類型分別調用相應的函數
      switch(iProtocol)
      {
      case IPPROTO_TCP :DecodeTcpPack(buf+iIphLen, iBufSize);break;
      case IPPROTO_UDP :DecodeUdpPack(buf+iIphLen, iBufSize);break;
      case IPPROTO_ICMP :DecodeIcmpPack(buf+iIphLen, iBufSize);break;
      default :break;
      }
      //printf("");
      return true;
      }

      //協議識別程序
      char * CheckProtocol(int iProtocol)
      {
      for(int i=0; i<MAX_PROTO_NUM; i++)
      if(ProtoMap.ProtoNum==iProtocol)
      return ProtoMap.ProtoText;
      return "";
      }

      //TCP解包程序
      int DecodeTcpPack(char * TcpBuf, int iBufSize)
      {
      TCP_HEADER * pTcpHeader;
      int i;
      int iSourcePort,iDestPort;

      pTcpHeader = (TCP_HEADER * )TcpBuf;
      //計算TCP首部長度
      int TcpHeaderLen = pTcpHeader->th_lenres>>4;
      TcpHeaderLen *= sizeof(unsigned long);
      char * TcpData=TcpBuf+TcpHeaderLen;
      //如果過濾敏感字符串則判斷是否包含
      if (strSensitive)
      if ((strstr(TcpData, strSensitive))==NULL) return true;
      //對端口進行過濾
      iSourcePort = ntohs(pTcpHeader->th_sport);
      iDestPort = ntohs(pTcpHeader->th_dport);
      if ((iPortFilter) && (iSourcePort!=iPortFilter) && (iDestPort!=iPortFilter))
      return true;
      //輸出
      printf("%s ", szProtocol);
      printf("%15s:%5d ->%15s:%5d ", szSourceIP, iSourcePort, szDestIP, iDestPort);
      printf("TTL=%3d ", iTTL);
      //判斷TCP標志位
      unsigned char FlagMask = 1;
      for( i=0; i<6; i++ )
      {
      if((pTcpHeader->th_flag) & FlagMask) printf("%c",TcpFlag);
      else printf("-");
      FlagMask=FlagMask<<1;
      }
      printf(" bytes=%4d", iBufSize);
      printf("");
      //對于長度大于40字節的包進行數據分析(IP_HEADER+TCP_HEADER=40)
      if ((ParamDecode) && (iBufSize>40))
      {
      //分析TCP數據段
      if ((!strSensitive) || (strstr(TcpData,strSensitive)))
      {
      printf(" [DATA]");
      printf("%s",TcpData);
      printf(" [DATA END]");
      }
      }
      return true;
      }


      //UDP解包程序
      int DecodeUdpPack(char * UdpBuf, int iBufSize)
      {
      UDP_HEADER *pUdpHeader;
      pUdpHeader = (UDP_HEADER * )UdpBuf;
      int iSourcePort = ntohs(pUdpHeader->uh_sport);
      int iDestPort = ntohs(pUdpHeader->uh_dport);
      //對端口進行過濾
      if(iPortFilter)
      if ((iSourcePort!=iPortFilter) && (iDestPort!=iPortFilter))
      return true;
      printf("%s ", szProtocol);
      printf("%15s:%5d ->%15s:%5d ", szSourceIP, iSourcePort, szDestIP, iDestPort);
      printf("TTL=%3d ", iTTL);
      printf("Len=%4d ", ntohs(pUdpHeader->uh_len));
      printf("bytes=%4d", iBufSize);
      printf("");
      //對于長度大于28字節的包進行數據分析(IP_HEADER+UDP_HEADER>28)
      if ((ParamDecode) && (iBufSize>28))
      {
      printf(" [DATA]");
      //UDP首部長度為8
      char * UdpData=UdpBuf+8;
      //分析UDP數據段
      for(unsigned int i=0;i<(iBufSize-sizeof(UDP_HEADER));i++)
      {
      if (!(i%8)) printf("");
      if ( (UdpData>33) && (UdpData<122) )
      printf("%3c [%3x]", UdpData, UdpData);
      else printf(" [%3x]", abs(UdpData));
      }
      printf(" [DATA END]");
      }
      return true;
      }

      //ICMP解包程序
      int DecodeIcmpPack(char * IcmpBuf, int iBufSize)
      {
      ICMP_HEADER * pIcmpHeader;
      pIcmpHeader = (ICMP_HEADER * )IcmpBuf;
      int iIcmpType = pIcmpHeader->i_type;
      int iIcmpCode = pIcmpHeader->i_code;
      //對類型進行過濾
      if ((iPortFilter) && (iIcmpType!=iPortFilter)) return true;
      printf("%s ", szProtocol);
      //printf("%15s Type%d ->%15s Code%d ", szSourceIP, iIcmpType, szDestIP, iIcmpCode);
      printf("%15s ->%15s ", szSourceIP, szDestIP);
      printf("TTL=%3d ", iTTL);
      printf("Type%2d,%d ",iIcmpType,iIcmpCode);
      printf("bytes=%4d", iBufSize);
      printf("");
      //對于包含數據段的包進行數據分析
      if ((ParamDecode) && (iBufSize>28))
      {
      char * IcmpData=IcmpBuf+4;
      //分析ICMP數據段
      printf(" [DATA]");
      for(unsigned int i=0;i<(iBufSize-sizeof(ICMP_HEADER));i++)
      {
      if (!(i%8)) printf("");
      if ( (IcmpData>33) && (IcmpData<122) )
      printf("%3c [%3x]", IcmpData, IcmpData);
      else printf(" [%3x]", abs(IcmpData));
      }
      printf(" [DATA END]");
      }
      return true;
      }

      //命令行參數處理
      bool GetCmdLine(int argc, char ** argv)
      {
      if (argc<2) return CMD_PARAM_HELP;
      for(int i=1;i<argc;i++)
      {
      if(argv[0]!='/') return CMD_PARAM_HELP;
      else switch (argv[1])
      {
      case 't':
      case 'T': ParamTcp=true; break;
      case 'u':
      case 'U': ParamUdp=true; break;
      case 'i':
      case 'I': ParamIcmp=true; break;
      case 'p':
      case 'P': ParamDecode=true; break;
      case 'f':
      case 'F':
      {
      strFromIpFilter=(char*)malloc(16*sizeof(char));
      memset(strFromIpFilter,0,16*sizeof(char));
      strcpy(strFromIpFilter,argv+3);
      break;
      }
      case 'd':
      case 'D':
      {
      strDestIpFilter=(char*)malloc(16*sizeof(char));
      memset(strDestIpFilter,0,16*sizeof(char));
      strcpy(strDestIpFilter,argv+3);
      break;
      }
      case 's':
      case 'S':
      {
      strSensitive=(char*)malloc(255*sizeof(char));
      memset(strSensitive,0,255*sizeof(char));
      strcpy(strSensitive,argv+3);
      break;
      }
      case 'o':
      case 'O':
      {
      iPortFilter=atoi(argv+3);
      break;
      }
      }
      }
      printf("Will Sniffer");
      if(ParamTcp) printf(" TCP");
      if(ParamUdp) printf(" UDP");
      if(ParamIcmp) printf(" ICMP");
      if(strFromIpFilter) printf(" FromIp:%s",strFromIpFilter);
      if(strDestIpFilter) printf(" DestIp:%s",strDestIpFilter);
      if(ParamDecode) printf(" DECODE ON");
      if(strSensitive) printf(" Sensitive String:'%s'",strSensitive);
      printf("CTRL+C to quitStart:");
      return (!CMD_PARAM_HELP);
      }

      //使用說明
      void usage(void)
      {
      printf("IPSpy");
      printf("USAGE:");
      printf("/t Output TCP Packets");
      printf("/u Output UDP Packets");
      printf("/i Output ICMP Packets");
      printf("/p Decode Packets (default OFF)");
      printf("/f: fromIP Output Packets FromIp=fromIP (default ALL)");
      printf("/d: destIP Output Packets DestIp=destIP (default ALL)");
      printf("/s: string Output Packets Include sensitive String(TCP only)");
      printf("/o: port Output Packets from or to the port(ICMP is TYPE)");
      printf("Example:");
      printf("GUNiffer.exe /d>GUNiffer.log");
      printf("GUNiffer.exe /t /u /f:192.168.15.231");
      printf("GUNiffer.exe /t /p /s:PASS");
      }

      //SOCK錯誤處理程序
      void CheckSockError(int iErrorCode, char *pErrorMsg)
      {
      if(iErrorCode==SOCKET_ERROR)
      {
      printf("%s Error:%d", pErrorMsg, GetLastError());
      closesocket(SockRaw);
      exit(0);
      }

      }
      發表評論 共有條評論
      用戶名: 密碼:
      驗證碼: 匿名發表
      japan日本人妻熟老太
      <del id="7msxa"><table id="7msxa"><strong id="7msxa"></strong></table></del><legend id="7msxa"></legend>

      <tbody id="7msxa"></tbody>
    3. <ol id="7msxa"></ol><th id="7msxa"><track id="7msxa"><rt id="7msxa"></rt></track></th>
      1. <dd id="7msxa"></dd>