网络嗅探器的设计与实现

发布时间 : 星期日 文章网络嗅探器的设计与实现更新完毕开始阅读

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

4.3 循环抓包

4.3.1 TCP包

if((pIpheader->ip_p)==IPPROTO_TCP&&lentcp!=0){ printf(\ pCount++; datatcp=(unsigned char *) RecvBuf+sizeof(struct ipheader)+sizeof(struct tcpheader); printf(\ printf(\目的IP地址:%s\\n\ printf(\目的端口:%i\\n\cpheader->dport)); printf(\ printf(\ printf(\ printf(\ printf(\ for (i=0;i

for (i=0;i=20) printf(\ else printf(\ } printf(\ }

运行后的结果:

7

网络嗅探器的设计与实现

图4.3.1 TCP包运行结果

4.3.2 UDP包

if((pIpheader->ip_p)==IPPROTO_UDP&&lentcp!=0){ pCount++; dataudp=(unsigned char *) RecvBuf+sizeof(struct ipheader)+sizeof(struct udphdr);

printf(\ printf(\目的IP地址:%s\\n\ printf(\目的端口:%d\\n\ printf(\数据地址:%x\\n\ printf(\头部长度:%i\\n\ printf(\头部长度:%i\\n\ printf(\包的大小:%i\\n\ printf(\ for (i=0;i=20) printf(\ else printf(\ }

8

printf(\ }

运行后的结果:

图4.3.2 UDP包运行结果

5 总结

通过这次课程设计,使我更加扎实的掌握了有关嗅探器方面的知识,在设计过程中虽然遇到了一些问题,但经过一次又一次的思考,一遍又一遍的检查终于找出了原因所在,也暴露出了前期我在这方面的知识欠缺和经验不足。这次课程设计不仅培养了我独立思考、动手操作的能力,在各种其它能力上也都有了提高。

从理论到实践,在这段日子里,不仅可以巩固了以前所学过的知识,而且学到了很多在书本上所没有学到过的知识。使我懂得了理论与实际相结合是很重要的,只有理论知识是远远不够的,只有把所学的理论知识与实践相结合起来,从理论中得出结论,才能真正为社会服务,从而提高自己的实际动手能力和独立思考的能力。

此次设计也让我明白了思路即出路,有不懂或不明白的地方要及时请教或上网查询,只要认真钻研,动脑思考,动手实践,就没有弄不懂的知识,收获颇丰。

参考文献:

[1]吴功宜,董大凡王珺等.计算机网络高级软件编程技术【M】.北京:清华大学出版社,2008 [2]谢小特,王勇军.基于winPcap的捕包程序设计【J】.软件特刊,2007(11):71-72

[3]胡晓元,史浩山.winpcap包截获系统的分析及其应用【J】.计算机工程,2005(1):96-97 [4]赵辉,叶子青.VisualC++系统开发实例精粹【M】,北京:人民邮电出版社,2005

9

网络嗅探器的设计与实现

附件:

#include /*windows socket的头文件,系统定义的*/ #include #include #include #include

#pragma comment(lib,\ /*链接API相关连的Ws2_32.lib静态库*/ #define MAX_HOSTNAME_LAN 255 #define SIO_RCVALL _WSAIOW(IOC_VENDOR,1) #define MAX_ADDR_LEN 16 struct ipheader {

unsigned char ip_hl:4; /*header unsigned char ip_v:4; /*version(版unsigned char ip_tos; /*type os unsigned short int ip_len; /*total unsigned short int ip_id;

unsigned short int ip_off; /*fragment unsigned char ip_ttl; /*time to live (生unsigned char ip_p; /*protocol(协unsigned short int ip_sum; unsigned int ip_src; /*source unsigned int ip_dst; /*destination length(报头长度)*/ 本)*/

service服务类型*/ length (总长度)*/ /*identification (标识符)*/ offset field(段移位域)*/ 存时间)*/ 议)*/

/*checksum(校验和)*/ address(源地址)*/ address(目的地址)*/ }; typedef struct tcpheader {

unsigned short int sport; /*source port unsigned short int dport; /*destination

10

(源端口号)*/ port(目的端口号)*/

unsigned int th_seq; /*sequence unsigned int th_ack;

unsigned char th_x:4; /*unused(未unsigned char th_off:4; /*data unsigned char Flags; /*标志全*/ unsigned short int th_win; unsigned short int th_sum; unsigned short int th_urp; /*urgent number(包的序列号)*/

/*acknowledgement number(确认应答号)*/ 使用)*/

offset(数据偏移量)*/

/*windows(窗口)*/ /*checksum(校验和)*/ pointer(紧急指针)*/ }TCP_HDR;

typedef struct udphdr {

unsigned short sport; /*source unsigned short dport; /*destination unsigned short len; /*udp unsigned short cksum; /*udp port(源端口号)*/ port(目的端口号)*/ length(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

char szSourceIP[MAX_ADDR_LEN],

/* total ip header length: 20 bytes (=160 bits) */ name[MAX_HOSTNAME_LAN];

szDestIP[MAX_ADDR_LEN],RecvBuf[65535] = {0};

struct udphdr *pUdpheader; struct ipheader *pIpheader;

联系合同范文客服:xxxxx#qq.com(#替换为@)