嗅探器实验报告.docx
《嗅探器实验报告.docx》由会员分享,可在线阅读,更多相关《嗅探器实验报告.docx(8页珍藏版)》请在冰豆网上搜索。
嗅探器实验报告
嗅探器实验报告
学院:
通信工程
班级:
011252
学号:
01125118
:
寇天聪
嗅探器设计原理
嗅探器作为一种网络通讯程序,也是通过对网卡的编程来实现网络通讯的,对网卡的编程也是使用通常的套接字(socket)方式来进行。
通常的套接字程序只能响应与自己硬件地址相匹配的或是以广播形式发出的数据帧,对于其他形式的数据帧比如已到达网络接口但却不是发给此地址的数据帧,网络接口在验证投递地址并非自身地址之后将不引起响应,也就是说应用程序无法收取到达的数据包。
而网络嗅探器的目的恰恰在于从网卡接收所有经过它的数据包,这些数据包即可以是发给它的也可以是发往别处的。
显然,要达到此目的就不能再让网卡按通常的正常模式工作,而必须将其设置为混杂模式。
网络嗅探器无论是在网络安全还是在黑客攻击方面均扮演了很重要的角色。
通过使用网络嗅探器可以把网卡设置于混杂模式,并可实现对网络上传输的数据包的捕获与分析。
此分析结果可供网络安全分析之用,但如为黑客所利用也可以为其发动进一步的攻击提供有价值的信息。
可见,嗅探器实际是一把双刃剑。
虽然网络嗅探器技术被黑客利用后会对网络安全构成一定的威胁,但嗅探器本身的危害并不是很大,主要是用来为其他黑客软件提供网络情报,真正的攻击主要是由其他黑软来完成的。
而在网络安全方面,网络嗅探手段可以有效地探测在网络上传输的数据包信息,通过对这些信息的分析利用是有助于网络安全维护的。
本程序实现的基本功能:
指定局域网的任一ip地址,能分析包的类型,结构,流量的大小。
嗅探器工作原理
根据前面的设计思路,不难写出网络嗅探器的实现代码,下面就结合注释对程序的具体是实现进行讲解,同时为程序流程的清晰起见,去掉了错误检查等保护性代码。
源程序:
#include/*windowssocket的头文件,系统定义的*/
#include
#include
#include
#include
#pragmament(lib,"ws2_32.lib")/*API相关连的Ws2_32.lib静态库*/
#defineMAX_HOSTNAME_LAN255
#defineSIO_RCVALL_WSAIOW(IOC_VENDOR,1)
#defineMAX_ADDR_LEN16
structipheader{
unsignedcharip_hl:
4;/*headerlength(报头长度)*/
unsignedcharip_v:
4;/*version(版本)*/
unsignedcharip_tos;/*typeosservice服务类型*/
unsignedshortintip_len;/*totallength(总长度)*/
unsignedshortintip_id;/*identification(标识符)*/
unsignedshortintip_off;/*fragmentoffsetfield(段移位域)*/
unsignedcharip_ttl;/*timetolive(生存时间)*/
unsignedcharip_p;/*protocol(协议)*/
unsignedshortintip_sum;/*checksum(校验和)*/
unsignedintip_src;/*sourceaddress(源地址)*/
unsignedintip_dst;/*destinationaddress(目的地址)*/
};/*totalipheaderlength:
20bytes(=160bits)*/
typedefstructtcpheader{
unsignedshortintsport;/*sourceport(源端口号)*/
unsignedshortintdport;/*destinationport(目的端口号)*/
unsignedintth_seq;/*sequencenumber(包的序列号)*/
unsignedintth_ack;/*acknowledgementnumber(确认应答号)*/
unsignedcharth_x:
4;/*unused(未使用)*/
unsignedcharth_off:
4;/*dataoffset(数据偏移量)*/
unsignedcharFlags;/*标志全*/
unsignedshortintth_win;/*windows(窗口)*/
unsignedshortintth_sum;/*checksum(校验和)*/
unsignedshortintth_urp;/*urgentpointer(紧急指针)*/
}TCP_HDR;
typedefstructudphdr{
unsignedshortsport;/*sourceport(源端口号)*/
unsignedshortdport;/*destinationport(目的端口号)*/
unsignedshortlen;/*udplength(udp长度)*/
unsignedshortcksum;/*udpchecksum(udp校验和)*/
}UDP_HDR;
voidmain(){
SOCKETsock;
WSADATAwsd;
DWORDdwBytesRet;
unsignedintoptval=1;
unsignedchar*dataudp,*datatcp;
inti,pCount=0,lentcp,lenudp;
SOCKADDR_INsa,saSource,saDest;
structhostentFAR*pHostent;
charFARname[MAX_HOSTNAME_LAN];
charszSourceIP[MAX_ADDR_LEN],szDestIP[MAX_ADDR_LEN],RecvBuf[65535]={0};
structudphdr*pUdpheader;
structipheader*pIpheader;
structtcpheader*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_list[0],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=(structipheader*)RecvBuf;
pTcpheader=(structtcpheader*)(RecvBuf+sizeof(structipheader));
pUdpheader=(structudphdr*)(RecvBuf+sizeof(structipheader));
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(structipheader)+sizeof(structtcpheader)));
lenudp=(ntohs(pIpheader->ip_len)-(sizeof(structipheader)+sizeof(structudphdr)));
if((pIpheader->ip_p)==IPPROTO_TCP&&lentcp!
=0){
printf("*******************************************\n");
pCount++;
datatcp=(unsignedchar*)RecvBuf+sizeof(structipheader)+sizeof(structtcpheader);
printf("-TCP-\n");
printf("\n目的IP地址:
%s\n",szDestIP);
printf("\n目的端口:
%i\n",ntohs(pTcpheader->dport));
printf("datatcpaddress->%x\n",datatcp);
printf("sizeofipheader->%i\n",sizeof(structipheader));
printf("sizeoftcpheader->%i\n",sizeof(structtcpheader));
printf("sizeoftheholepacket->%i\n",ntohs(pIpheader->ip_len));
printf("\ncharPacket%i[%i]=\"",pCount,lentcp-1);
for(i=0;iprintf("\\x%.2x",*(datatcp+i));
if(i%10==0)printf("\"\n\"");
}
printf("\";\n\n\n");
for(i=0;iif(*(datatcp+i)<=127&&*(datatcp+i)>=20)
printf("%c",*(datatcp+i));
else
printf(".");
}
printf("\n\n*******************************************\n");
}
if((pIpheader->ip_p)==IPPROTO_UDP&&lentcp!
=0){
pCount++;
dataudp=(unsignedchar*)RecvBuf+sizeof(structipheader)+sizeof(structudphdr);
printf("-UDP-\n");
printf("\n目的IP地址:
%s\n",szDestIP);
printf("\n目的端口:
%d\n",ntohs(pTcpheader->dport));
printf("UDP数据地址:
%x\n",dataudp);
printf("IP头部长度:
%i\n",sizeof(structipheader));
printf("UDP头部长度:
%i\n",sizeof(structudphdr));
printf("包的大小:
%i\n",ntohs(pIpheader->ip_len));
printf("\ncharPacket%i[%i]=\"",pCount,lenudp-1);
for(i=0;iprintf("\\x%.2x",*(dataudp+i));
if(i%10==0)
printf("\"\n\"");
}
printf("\";\n\n\n");
for(i=0;iif(*(dataudp+i)<=127&&*(dataudp+i)>=20)
printf("%c",*(dataudp+i));
else
printf(".");
}
printf("\n\n*******************************************\n");
}
}
}
运行结果截图:
实验总结
1.实验中的遇到的问题:
刚开始老师布置作业的时候完全不知道怎么做,在网上找了一些资料和借鉴同学的实验过程才完成了这个实验,着实不易。
2.实验中的收获:
一定程度上加深了对TCP/IP协议的理解,对课程学习打下了一个比较好的基础;用一些设计模式在一定程度能提高开发效能,降低开发的复杂度,并且深刻地认识到实践才能出真知这个硬道理。