高手请进:怎么样解析固定端口上收发的数据包中的内容(在线等)

来源:互联网 发布:加工中心编程软件排行 编辑:程序博客网 时间:2024/05/21 18:31

怎么样解析固定端口上收发的数据包中的内容??

我是利用Sniffer的原理,监测所有收发的数据包,然后去掉相关的头部,得到包中的数据.将其转化为字符串类型.

我在机器上装了个邮件服务器,试着检测在25端口和110端口上的数据包.我认为可以把 user ,pass, mail from, rcpt to等命令相关的数据解析出来.可我试了好久,都没有结果.

当我从局域网中别的机子拷个 .txt文件 或者 向别的机子拷个 txt文件时,文件肯定是被打成几个包发送,但第一个包(文件最前面的一部分数据)是监测到了,而且大小都正确,但解析出的字符串却是空串,后面的几个包完全正确.

这个问题让我郁闷了好久,烦请大家帮我解决一下.

使用的代码如下:

 SOCKET        sock;
 WSADATA       wsd;//wsd为指向WSADATA结构的指针
 DWORD  dwBytesRet;
 int pCount=0;
 unsigned int  optval = 1;
 unsigned char *datatcp=NULL;
 unsigned char *dataudp=NULL;
 int lentcp=0, lenudp;

 WSAStartup(MAKEWORD(2,1),&wsd);//初始化WINSOCK
 if((sock=socket(AF_INET,SOCK_RAW,IPPROTO_IP))==SOCKET_ERROR)// 创建原始套接字
  exit(1);

 char FAR name[MAX_HOSTNAME_LAN];
 gethostname(name, MAX_HOSTNAME_LAN);//获取本机名

 struct hostent FAR * pHostent;
 pHostent = (struct hostent * )malloc(sizeof(struct hostent));
 pHostent = gethostbyname(name);//获取给定主机名的IP地址

 SOCKADDR_IN sa;
 sa.sin_family = AF_INET;//填充SOCKADDR_IN结构
 sa.sin_port = htons(6000);
 memcpy(&sa.sin_addr.S_un.S_addr, pHostent->h_addr_list[0], pHostent->h_length);

 bind(sock, (SOCKADDR *)&sa, sizeof(sa));//将原始套接字绑定到本地网卡上
 if ((WSAGetLastError())==10013)
  exit(1);

 WSAIoctl(sock, SIO_RCVALL, &optval, sizeof(optval), NULL, 0, &dwBytesRet, NULL, NULL);

 struct udphdr *pUdpheader;
 struct ipheader *pIpheader;
 struct tcpheader *pTcpheader;
 char szSourceIP[MAX_ADDR_LEN], szDestIP[MAX_ADDR_LEN];
 SOCKADDR_IN saSource, saDest;

 

 while(1)
 {
  char RecvBuf[65535] = {0};
  
  pIpheader=(struct ipheader *)RecvBuf;//pIpheader指向IP包头部
  pTcpheader=(struct tcpheader *)(RecvBuf+sizeof(struct ipheader));//pTcpheader指向TCP包头部
  pUdpheader=(struct udphdr *)(RecvBuf+sizeof(struct ipheader));//pUdpheader指向UDP包头部

  memset(RecvBuf, 0, sizeof(RecvBuf));

  recv(sock, RecvBuf, sizeof(RecvBuf), 0);//从套接字接收数据

  saSource.sin_addr.s_addr = pIpheader->ip_src;//信源端口
  strncpy(szSourceIP, inet_ntoa(saSource.sin_addr), MAX_ADDR_LEN);//szSourceIP中放的是数据的信源IP地址,用点分

十进制表示

  saDest.sin_addr.s_addr = pIpheader->ip_dst;//信宿端口
  strncpy(szDestIP, inet_ntoa(saDest.sin_addr), MAX_ADDR_LEN);//szDestIP中放的是数据的信宿IP地址,用点分十进制

表示

  lentcp =(ntohs(pIpheader->ip_len)-(sizeof(struct ipheader)+sizeof(struct tcpheader)));//TCP数据包长度=IP包总

长度-IP头长度-TCP头长度 
  lenudp =(ntohs(pIpheader->ip_len)-(sizeof(struct ipheader)+sizeof(struct udphdr)));//UCP数据包长度=IP包总长

度-IP头长度-UDP头长度 

  if((pIpheader->ip_p)==IPPROTO_TCP&&lentcp!=0)//若这个数据包采用的是TCP协议
  {
 
   pCount++; 
   datatcp=(unsigned char *)RecvBuf+sizeof(struct ipheader)+sizeof(struct tcpheader);

   CString temp="";
   CString sourceIp(szDestIP);
   CString destIP(szSourceIP);

   int destPort = ntohs(pTcpheader->dport);
   int sourcePort = ntohs(pTcpheader->sport);
   temp.Format("%d",destPort);

   CString s;
   s.Format("%d", sourcePort);
 
   MessageBox("协议:TCP /nIP源地址:"+sourceIp + "  源端口:"+s + "/n目的地址:" + destIP +"目的端口:"

+temp);


     temp.Format("%d",lentcp);
   MessageBox("tcp包的长度:"+temp);


   CString data="";
   for(int i=0;i<lentcp;i++)
   {
    temp = (CString)(*(char*)(datatcp+i));
    data.Append(temp);
   }
  
   CString ss(data);
   MessageBox(data);

  if( (pIpheader->ip_p)==IPPROTO_UDP&&lentcp!=0)
  {
   //pCount++; 
   //dataudp=(unsigned char *) RecvBuf+sizeof(struct ipheader)+sizeof(struct udphdr);
   //printf("-UDP-/n");
   //printf("/nDestination address->%s/n",szDestIP);
   //printf("/nDestination port->%d/n",ntohs(pTcpheader->dport));
   //printf("dataudp address->%x/n",dataudp);
   //printf("size of ipheader->%i/n",sizeof(struct ipheader));
   //printf("size of udpheader->%i/n",sizeof(struct udphdr));
   //printf("size of the hole packet->%i/n",ntohs(pIpheader->ip_len));
   //printf("/nchar Packet%i []=/"",pCount,lenudp);
   //for (int x=0;x<lenudp;x++)
   //{
   // printf("//x%.2x",*(dataudp+x));
   // if (x%10==0)
   // {      
   //  printf("/"");
   //  printf("/n/"");
   // }
   //}
   //printf("/";/n/n/n");
   //for (int x2=0;x2<lenudp;x2++)
   //{
   // if( *(dataudp+x2)<=127&&*(dataudp+x2)>=0)
   //  printf("%c",*(dataudp+x2));
   // else
   //  printf(".");
   //}
   //printf("/n/n");
   //printf("*******************************************/n");
  }
 }
  

原创粉丝点击