嗅探器实验报告Word格式文档下载.docx

上传人:b****4 文档编号:17957062 上传时间:2022-12-12 格式:DOCX 页数:8 大小:71.65KB
下载 相关 举报
嗅探器实验报告Word格式文档下载.docx_第1页
第1页 / 共8页
嗅探器实验报告Word格式文档下载.docx_第2页
第2页 / 共8页
嗅探器实验报告Word格式文档下载.docx_第3页
第3页 / 共8页
嗅探器实验报告Word格式文档下载.docx_第4页
第4页 / 共8页
嗅探器实验报告Word格式文档下载.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

嗅探器实验报告Word格式文档下载.docx

《嗅探器实验报告Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《嗅探器实验报告Word格式文档下载.docx(8页珍藏版)》请在冰豆网上搜索。

嗅探器实验报告Word格式文档下载.docx

本程序实现的基本功能:

指定局域网的任一ip地址,能分析包的类型,结构,流量的大小。

嗅探器工作原理

根据前面的设计思路,不难写出网络嗅探器的实现代码,下面就结合注释对程序的具体是实现进行讲解,同时为程序流程的清晰起见,去掉了错误检查等保护性代码。

源程序:

#include<

winsock2.h>

/*windowssocket的头文件,系统定义的*/

windows.h>

ws2tcpip.h>

stdio.h>

stdlib.h>

#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:

/*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:

/*unused(未使用)*/

unsignedcharth_off:

/*dataoffset(数据偏移量)*/

unsignedcharFlags;

/*标志全*/

unsignedshortintth_win;

/*windows(窗口)*/

unsignedshortintth_sum;

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);

-TCP-\n"

\n目的IP地址:

%s\n"

szDestIP);

\n目的端口:

%i\n"

ntohs(pTcpheader->

dport));

datatcpaddress->

%x\n"

datatcp);

sizeofipheader->

sizeof(structipheader));

sizeoftcpheader->

sizeof(structtcpheader));

sizeoftheholepacket->

ntohs(pIpheader->

ip_len));

\ncharPacket%i[%i]=\"

"

pCount,lentcp-1);

for(i=0;

i<

lentcp;

i++){

printf("

\\x%.2x"

*(datatcp+i));

if(i%10==0)printf("

\"

\n\"

}

;

\n\n\n"

if(*(datatcp+i)<

=127&

*(datatcp+i)>

=20)

printf("

%c"

else

."

\n\n*******************************************\n"

}

ip_p)==IPPROTO_UDP&

dataudp=(unsignedchar*)RecvBuf+sizeof(structipheader)+sizeof(structudphdr);

-UDP-\n"

%d\n"

UDP数据地址:

dataudp);

IP头部长度:

UDP头部长度:

sizeof(structudphdr));

包的大小:

pCount,lenudp-1);

lenudp;

*(dataudp+i));

if(i%10==0)

if(*(dataudp+i)<

*(dataudp+i)>

}

}

运行结果截图:

实验总结

1.实验中的遇到的问题:

刚开始老师布置作业的时候完全不知道怎么做,在网上找了一些资料和借鉴同学的实验过程才完成了这个实验,着实不易。

2.实验中的收获:

一定程度上加深了对TCP/IP协议的理解,对课程学习打下了一个比较好的基础;

用一些设计模式在一定程度能提高开发效能,降低开发的复杂度,并且深刻地认识到实践才能出真知这个硬道理。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 育儿理论经验

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1