嗅探器实验报告.doc_第1页
嗅探器实验报告.doc_第2页
嗅探器实验报告.doc_第3页
嗅探器实验报告.doc_第4页
嗅探器实验报告.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

嗅探器实验报告 学院:通信工程 班级:011252 学号:01125118 姓名:寇天聪 嗅探器设计原理嗅探器作为一种网络通讯程序,也是通过对网卡的编程来实现网络通讯的,对网卡的编程也是使用通常的套接字(socket)方式来进行。通常的套接字程序只能响应与自己硬件地址相匹配的或是以广播形式发出的数据帧,对于其他形式的数据帧比如已到达网络接口但却不是发给此地址的数据帧,网络接口在验证投递地址并非自身地址之后将不引起响应,也就是说应用程序无法收取到达的数据包。而网络嗅探器的目的恰恰在于从网卡接收所有经过它的数据包,这些数据包即可以是发给它的也可以是发往别处的。显然,要达到此目的就不能再让网卡按通常的正常模式工作,而必须将其设置为混杂模式。 网络嗅探器无论是在网络安全还是在黑客攻击方面均扮演了很重要的角色。通过使用网络嗅探器可以把网卡设置于混杂模式,并可实现对网络上传输的数据包的捕获与分析。此分析结果可供网络安全分析之用,但如为黑客所利用也可以为其发动进一步的攻击提供有价值的信息。可见,嗅探器实际是一把双刃剑。虽然网络嗅探器技术被黑客利用后会对网络安全构成一定的威胁,但嗅探器本身的危害并不是很大,主要是用来为其他黑客软件提供网络情报,真正的攻击主要是由其他黑软来完成的。而在网络安全方面,网络嗅探手段可以有效地探测在网络上传输的数据包信息,通过对这些信息的分析利用是有助于网络安全维护的。本程序实现的基本功能:指定局域网内的任一ip地址,能分析包的类型,结构,流量的大小。 嗅探器工作原理根据前面的设计思路,不难写出网络嗅探器的实现代码,下面就结合注释对程序的具体是实现进行讲解,同时为程序流程的清晰起见,去掉了错误检查等保护性代码。源程序:#include /*windows socket的头文件,系统定义的*/#include #include #include #include #pragma comment(lib,ws2_32.lib)/*链接API相关连的Ws2_32.lib静态库*/#define MAX_HOSTNAME_LAN 255#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)#define MAX_ADDR_LEN 16struct ipheader unsigned char ip_hl:4;/*header length(报头长度)*/unsigned char ip_v:4;/*version(版本)*/unsigned char ip_tos;/*type os service服务类型*/unsigned short int ip_len;/*total length (总长度)*/unsigned short int ip_id;/*identification (标识符)*/unsigned short int ip_off;/*fragment offset field(段移位域)*/unsigned char ip_ttl;/*time to live (生存时间)*/unsigned char ip_p;/*protocol(协议)*/unsigned short int ip_sum;/*checksum(校验和)*/unsigned int ip_src;/*source address(源地址)*/unsigned int ip_dst;/*destination address(目的地址)*/;/* total ip header length: 20 bytes (=160 bits) */typedef struct tcpheader unsigned short int sport;/*source port (源端口号)*/unsigned short int dport;/*destination port(目的端口号)*/unsigned int th_seq;/*sequence number(包的序列号)*/unsigned int th_ack;/*acknowledgement number(确认应答号)*/unsigned char th_x:4;/*unused(未使用)*/unsigned char th_off:4;/*data offset(数据偏移量)*/unsigned char Flags;/*标志全*/unsigned short int th_win;/*windows(窗口)*/unsigned short int th_sum;/*checksum(校验和)*/unsigned short int th_urp;/*urgent pointer(紧急指针)*/TCP_HDR;typedef struct udphdr unsigned short sport;/*source port(源端口号)*/unsigned short dport;/*destination port(目的端口号)*/unsigned short len;/*udp length(udp长度)*/unsigned short cksum;/*udp checksum(udp校验和)*/UDP_HDR;void main()SOCKET sock;WSADATA wsd;DWORD dwBytesRet;unsigned int optval = 1;unsigned char *dataudp,*datatcp;int i,pCount=0,lentcp, lenudp;SOCKADDR_IN sa,saSource, saDest;struct hostent FAR * pHostent;char FAR nameMAX_HOSTNAME_LAN;char szSourceIPMAX_ADDR_LEN, szDestIPMAX_ADDR_LEN,RecvBuf65535 = 0;struct udphdr *pUdpheader;struct ipheader *pIpheader;struct tcpheader *pTcpheader;WSAStartup(MAKEWORD(2,1),&wsd);if (sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP)=SOCKET_ERROR)exit(1);gethostname(name, MAX_HOSTNAME_LAN);pHostent = gethostbyname(name);sa.sin_family = AF_INET;sa.sin_port = htons(6000);memcpy(&sa.sin_addr.S_un.S_addr, pHostent-h_addr_list0, pHostent-h_length);bind(sock, (SOCKADDR *)&sa, sizeof(sa);/*bind()设定自己主机的IP地址和端口号*/if (WSAGetLastError()=10013) exit(1);WSAIoctl(sock, SIO_RCVALL, &optval, sizeof(optval), NULL, 0, &dwBytesRet, NULL, NULL);pIpheader = (struct ipheader *)RecvBuf;pTcpheader = (struct tcpheader *)(RecvBuf+ sizeof(struct ipheader );pUdpheader = (struct udphdr *) (RecvBuf+ sizeof(struct ipheader );while (1)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);saDest.sin_addr.s_addr = pIpheader-ip_dst;strncpy(szDestIP, inet_ntoa(saDest.sin_addr), MAX_ADDR_LEN);lentcp =(ntohs(pIpheader-ip_len)-(sizeof(struct ipheader)+sizeof(struct tcpheader); lenudp =(ntohs(pIpheader-ip_len)-(sizeof(struct ipheader)+sizeof(struct udphdr); if(pIpheader-ip_p)=IPPROTO_TCP&lentcp!=0)printf(*n);pCount+; datatcp=(unsigned char *) RecvBuf+sizeof(struct ipheader)+sizeof(struct tcpheader);printf(-TCP-n);printf(n目的IP地址:%sn,szDestIP);printf(n目的端口:%in,ntohs(pTcpheader-dport);printf(datatcp address-%xn,datatcp);printf(size of ipheader-%in,sizeof(struct ipheader);printf(size of tcpheader-%in,sizeof(struct tcpheader);printf(size of the hole packet-%in,ntohs(pIpheader-ip_len);printf(nchar Packet%i %i=,pCount,lentcp-1);for (i=0;ilentcp;i+)printf(x%.2x,*(datatcp+i);if (i%10=0) printf(n);printf(;nnn);for (i=0;ilentcp;i+)if( *(datatcp+i)=20)printf(%c,*(datatcp+i);elseprintf(.);printf(nn*n); if(pIpheader-ip_p)=IPPROTO_UDP&lentcp!=0)pCount+;dataudp=(unsigned char *) RecvBuf+sizeof(struct ipheader)+sizeof(struct udphdr);printf(-UDP-n);printf(n目的IP地址:%sn,szDestIP);printf(n目的端口:%dn,ntohs(pTcpheader-dport);printf(UDP数据地址:%xn,dataudp);printf(IP头部长度:%in,sizeof(struct ipheader);printf(UDP头部长度:%in,sizeof(struct udphdr);printf(包的大小:%in,ntohs(pIpheader-ip_len);printf(nchar Packet%i %i=,pCount,lenudp-1);for (i=0;ilenudp;i+)printf(x%.2x,*(dataudp+i);if (i%10=0)printf(n);printf(;nnn);for (i=0;ile

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论