20. while(1) 22. { 23. fromlen = sizeof from; 24. bytes_recieved = recvfrom(sock, buffer, sizeofbuffer, 0, (struct sockaddr *)&from, &fromlen); 25. printf("\nBytes received :::%5d\n",bytes_recieved); 26. printf("Source address :::%s\n",inet_ntoa(from.sin_addr)); 27. ip = (struct ip *)buffer; /*See if this is a TCP packet*/ 28. if(ip->ip_protocol == 6) { 29. printf("IP header length :::%d\n",ip->ip_length); 30. printf("Protocol :::%d\n",ip->ip_protocol); 31. tcp = (struct tcp *)(buffer +(4*ip->ip_length)); 32. printf("Source port :::%d\n",ntohs(tcp->tcp_source_port)); 33. printf("Dest port :::%d\n",ntohs(tcp->tcp_dest_port)); 34. }
35. } 36.} 37.int Open_Raw_Socket() { 38. int sock; 39. if((sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0){ /*Then the socket was not created properly and must die*/ 40. perror("The raw socket was not created"); 41. exit(0); 42. }; 43. return(sock); 44. }
45.int Set_Promisc(char *interface, int sock ) { 46. struct ifreq ifr; 47. strncpy(ifr.ifr_name, interface,strnlen(interface)+1); 48. if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)) { /*Could not retrieve flags for the interface*/ 49. perror("Could not retrive flags for the interface"); 50. exit(0); 51. } 52. printf("The interface is ::: %s\n", interface); 53. perror("Retrieved flags from interface successfully"); 54. ifr.ifr_flags |= IFF_PROMISC; 55. if (ioctl (sock, SIOCSIFFLAGS, &ifr) == -1 ) { /*Could not set the flags on the interface */ 56. perror("Could not set the PROMISC flag:"); 57. exit(0); 58. } 59. printf("Setting interface ::: %s ::: to promisc",interface); 60. return(0); 61. }
/***********************EOF**********************************/ 上面这段程序中有很详细的注解,不过我想还是有必要说一说,首先第10行--intOpen_Raw_Socket(void); 是我们的自定义函数,具体内容如下: 37.int Open_Raw_Socket() { 38. int sock; 39. if((sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0){ /*Then the socket was not created properly and must die*/ 40. perror("The raw socket was not created"); 41. exit(0); 42. }; 43. return(sock); 44. }
到了第三步,分析数据包。27行,ip = (struct ip*)buffer,使我们在头文件中的IP结构对应于所接收到的数据,接下来判断在网络层中是否使用的是TCP协议,if(ip->ip_protocol== 6) ,如果答案是,tcp信息包从整个IP/TCP包 buffer +(4*ip->ip_length) 地址处开始,所以31行 tcp = (struct tcp*)(buffer +(4*ip->ip_length)),然后对应结构把你所需要的信息输出。 /*************************headers.h**************************/ /*structure of an ip header*/ struct ip { unsigned int ip_length:4; /*little-endian*/ unsigned int ip_version:4; unsigned char ip_tos; unsigned short ip_total_length; unsigned short ip_id; unsigned short ip_flags; unsigned char ip_ttl; unsigned char ip_protocol; unsigned short ip_cksum; unsigned int ip_source; unsigned int ip_dest; };
/* Structure of a TCP header */ struct tcp { unsigned short tcp_source_port; unsigned short tcp_dest_port; unsigned int tcp_seqno; unsigned int tcp_ackno; unsigned int tcp_res1:4, /*little-endian*/ tcp_hlen:4, tcp_fin:1, tcp_syn:1, tcp_rst:1, tcp_psh:1, tcp_ack:1, tcp_urg:1, tcp_res2:2; unsigned short tcp_winsize; unsigned short tcp_cksum; unsigned short tcp_urgent; }; /*********************EOF***********************************/ 从上面的分析我们可以清楚的认识到,认识一个SNIFF需要对TCP/IP协议有着详细的了解,否则你根本无法找到你需要的信息。有了上面的基础,你可以自己来做一个你需要的SNIFF了。 五 常用的SNIFF 很少有原因会让你自己亲自动手来做一个自己的SNIFF,除非你是想了解他的原理,或者是其他一些特别的原因,比如你要在某个特殊的环境拦截一些特殊的数据包。下面我们就来看看一些在网络上经常使用的SNIFF。 (1)windows环境下 windows环境下当然是大名鼎鼎的netxray以及snifferpro了,实际上很多人都是用他在windows环境下抓包来分析,不过我想很少有人笨到去在别人的机器上安装一个图形界面的SNIFF,除非他和管理员很熟悉........netxray的使用就不多说了,反正windows下的东西就是click,click,click,非常的方便用户。