基于Winpcap编程实现抓包实验.docx

上传人:b****6 文档编号:4451761 上传时间:2022-12-01 格式:DOCX 页数:21 大小:144.44KB
下载 相关 举报
基于Winpcap编程实现抓包实验.docx_第1页
第1页 / 共21页
基于Winpcap编程实现抓包实验.docx_第2页
第2页 / 共21页
基于Winpcap编程实现抓包实验.docx_第3页
第3页 / 共21页
基于Winpcap编程实现抓包实验.docx_第4页
第4页 / 共21页
基于Winpcap编程实现抓包实验.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

基于Winpcap编程实现抓包实验.docx

《基于Winpcap编程实现抓包实验.docx》由会员分享,可在线阅读,更多相关《基于Winpcap编程实现抓包实验.docx(21页珍藏版)》请在冰豆网上搜索。

基于Winpcap编程实现抓包实验.docx

基于Winpcap编程实现抓包实验

上海电力学院

计算机网络安全

(1)

课程实验报告

 

 

实验名称:

winpcap编程实验

 

基于Winpcap编程实现抓包实验

一.本设计要达到的目标

基于winpcap编程实现对网络数据的捕获,并分析数据类型,对于IP,ICMP,ARP,UDP等,能够自动识别其协议类型并分析帧的构成。

二.实现步骤

(1)需要通过资料来了解winpcap抓包的工作原理,熟悉其运行过程

Winpcap的内部结构

Wincap有三部分组成:

一个数据包监听设备驱动程序,一个低级的动态连接库和一个高级的静态连接库。

底层动态链接库运行在用户层,它将应用程序和数据包监听设备驱动程序隔离开来,使得应用程序可以不加修改地在不同的WINDOWS系统上运行。

高级的静态链接库和应用程序编译在一起,它使用低级动态链接库提供的服务,向应用程序提供完善的监听接口。

抓包是WinPcap的基本功能,也是NPF最重要的操作。

在抓包的时候,驱动(例如NICDriver)使用一个网络接口监视着数据包,并将这些数据包完整无缺地投递给用户级应用程序。

(2)进一步了解winpcap编程所需要的编译环境,下载WpdPack,了解编译环境所需要的库文件.在编译时需要把wpdpack中的include与lib添加进vc的库文件里。

(3)明确整个编程的步骤与具体函数。

刚开始要定义,在主函数中获取设备接口信息,获得网络地址与掩码地址,打开网络接口,还要设置过滤规则。

使用loop函数来回调循环捕获数据包,以便一层一层解析。

(4)还要定义几个以太网,ARP,IP,UDP,TCP,ICMP协议的格式。

需要注意在存储空间中,在存储空间中才能更好的逐层分析,不然很容易出错

(5)定义分析协议的函数,定义方式与回调函数相同.常用的函数有:

用于获取本机设备列表的pcap_findalldevs_ex函数

用于打开设备的pcap_open函数,可以指定为混杂模式打开

用于编译数据包过滤器的pcap_compile函数

用于设置数据包过滤器的pcap_setfilter函数

用于从设备读取数据包的pcap_netx_ex函数

用于关闭设备的pcap_close函数(参数为pcap_open返回值)

用于释放设备列表的pcap_freealldevs函数(对应pcap_findalldevs_ex)

三.系统流程图

四.关键代码及其分析

 

 

主函数

voidmain()

{

pcap_t*pcap_handle;/*Winpcap句柄*/

charerror_content[PCAP_ERRBUF_SIZE];/*存储错误信息*/

char*net_interface;/*网络接口*/

bpf_programbpf_filter;/*BPF过滤规则*/

charbpf_filter_string[]="";/*过滤规则字符串*/

bpf_u_int32net_mask;/*掩码*/

bpf_u_int32net_ip;/*网路地址*/

net_interface=pcap_lookupdev(error_content);/*获得可用的网络接口*/

pcap_lookupnet(net_interface,&net_ip,&net_mask,error_content);/*获得网络地址和掩码地址*/

pcap_handle=pcap_open_live(net_interface,BUFSIZ,1,1,error_content);/*打开网路接口*/

pcap_compile(pcap_handle,&bpf_filter,bpf_filter_string,0,net_ip);/*编译BPF过滤规则*/

pcap_setfilter(pcap_handle,&bpf_filter);/*设置过滤规则*/

对IP协议的定义

classip_header

{public:

#ifdefined(WORDS_BIGENDIAN)

u_int8_tip_version:

4,/*版本*/

ip_header_length:

4;/*首部长度*/

#else

u_int8_tip_header_length:

4,ip_version:

4;

#endif

u_int8_tip_tos;/*服务质量*/

u_int16_tip_length;/*长度*/

u_int16_tip_id;/*标识*/

u_int16_tip_off;/*偏移*/

u_int8_tip_ttl;/*生存时间*/

u_int8_tip_protocol;/*协议类型*/

u_int16_tip_checksum;/*校验和*/

in_addrip_souce_address;/*源IP地址*/

in_addrip_destination_address;/*目的IP地址*/

pcap_loop(pcap_handle,n,ethernet_protocol_packet_callback,NULL);/*注册回调函数,循环捕获网络数据包,利用回调函数来处理每个数据包*/

分析UDP协议的函数代码

voidudp_protocol_packet_callback(u_char*argument,constpcap_pkthdr*packet_header,constu_char*packet_content)

{

classudp_header*udp_protocol;/*UDP协议变量*/

u_shortsource_port;/*源端口*/

u_shortdestination_port;/*目的端口号*/

u_shortlength;//长度

udp_protocol=(classudp_header*)(packet_content+14+20);/*获得UDP协议内容*/

source_port=ntohs(udp_protocol->udp_source_port);/*获得源端口*/

destination_port=ntohs(udp_protocol->udp_destination_port);/*获得目的端口*/

length=ntohs(udp_protocol->udp_length);/*获得长度*/

cout<<"----------UDP协议----------"<

cout<<"源端口号:

"<

cout<<"目的端口号:

"<

switch(destination_port)

{

case138:

cout<<"上层协议为NETBIOS数据报服务"<

break;

case137:

cout<<"上层协议为NETBIOS名字服务"<

break;

case139:

cout<<"上层协议为NETBIOS会话服务"<

break;

case53:

cout<<"上层协议为域名服务"<

break;

default:

break;

}

cout<<"长度:

"<

cout<<"校验和:

"<udp_checksum)<

}

 

五.参考文献

(1)Winpcap中文文档

(2)网络资料

 

完整源程序

#include"pcap.h"

#include

#include

#include

usingnamespacestd;

/*以下是以太网协议格式的定义*/

classether_header

{

public:

u_int8_tether_dhost[6];/*目的以太网地址*/

u_int8_tether_shost[6];/*源以太网地址*/

u_int16_tether_type;/*以太网类型*/

};

/*下面是ARP协议格式的定义*/

classarp_header

{

public:

u_int16_tarp_hardware_type;/*硬件类型*/

u_int16_tarp_protocol_type;/*协议类型*/

u_int8_tarp_hardware_length;/*硬件地址长度*/

u_int8_tarp_protocol_length;/*协议地址长度*/

u_int16_tarp_operation_code;/*操作码*/

u_int8_tarp_source_ethernet_address[6];/*源以太网地址*/

u_int8_tarp_source_ip_address[4];/*源IP地址*/

u_int8_tarp_destination_ethernet_address[6];/*目的以太网地址*/

u_int8_tarp_destination_ip_address[4];/*目的IP地址*/

};

/*下面是IP协议格式的定义*/

classip_header

{public:

#ifdefined(WORDS_BIGENDIAN)

u_int8_tip_version:

4,/*版本*/

ip_header_length:

4;/*首部长度*/

#else

u_int8_tip_header_length:

4,ip_version:

4;

#endif

u_int8_tip_tos;/*服务质量*/

u_int16_tip_length;/*长度*/

u_int16_tip_id;/*标识*/

u_int16_tip_off;/*偏移*/

u_int8_tip_ttl;/*生存时间*/

u_int8_tip_protocol;/*协议类型*/

u_int16_tip_checksum;/*校验和*/

in_addrip_souce_address;/*源IP地址*/

in_addrip_destination_address;/*目的IP地址*/

};

/*下面是UDP协议格式定义*/

classudp_header

{public:

u_int16_tudp_source_port;/*源端口号*/

u_int16_tudp_destination_port;/*目的端口号*/

u_int16_tudp_length;/*长度*/

u_int16_tudp_checksum;/*校验和*/

};

/*下面是TCP协议格式的定义*/

classtcp_header

{public:

u_int16_ttcp_source_port;/*源端口号*/

u_int16_ttcp_destination_port;/*目的端口号*/

u_int32_ttcp_sequence_lliiuuwweennttaaoo;/*序列号*/

u_int32_ttcp_acknowledgement;/*确认序列号*/

#ifdefWORDS_BIGENDIAN

u_int8_ttcp_offset:

4,/*偏移*/

tcp_reserved:

4;/*未用*/

#else

u_int8_ttcp_reserved:

4,/*未用*/

tcp_offset:

4;/*偏移*/

#endif

u_int8_ttcp_flags;/*标记*/

u_int16_ttcp_windows;/*窗口大小*/

u_int16_ttcp_checksum;/*校验和*/

u_int16_ttcp_urgent_pointer;/*紧急指针*/

};

/*下面是ICMP协议格式的定义*/

classicmp_header

{public:

u_int8_ticmp_type;/*ICMP类型*/

u_int8_ticmp_code;/*ICMP代码*/

u_int16_ticmp_checksum;/*校验和*/

u_int16_ticmp_id;/*标识符*/

u_int16_ticmp_sequence;/*序列码*/

};

/*下面是分析TCP协议的函数,其定义方式与回调函数相同*/

voidtcp_protocol_packet_callback(u_char*argument,constpcap_pkthdr*packet_header,constu_char*packet_content)

{

classtcp_header*tcp_protocol;/*TCP协议变量*/

u_charflags;/*标记*/

intheader_length;/*长度*/

u_shortsource_port;/*源端口*/

u_shortdestination_port;/*目的端口*/

u_shortwindows;/*窗口大小*/

u_shorturgent_pointer;/*紧急指针*/

u_intsequence;/*序列号*/

u_intacknowledgement;/*确认号*/

u_int16_tchecksum;/*校验和*/

tcp_protocol=(tcp_header*)(packet_content+14+20);/*获得TCP协议内容*/

source_port=ntohs(tcp_protocol->tcp_source_port);/*获得源端口*/

destination_port=ntohs(tcp_protocol->tcp_destination_port);/*获得目的端口*/

header_length=tcp_protocol->tcp_offset*4;/*长度*/

sequence=ntohl(tcp_protocol->tcp_sequence_lliiuuwweennttaaoo);/*序列码*/

acknowledgement=ntohl(tcp_protocol->tcp_acknowledgement);/*确认序列码*/

windows=ntohs(tcp_protocol->tcp_windows);/*窗口大小*/

urgent_pointer=ntohs(tcp_protocol->tcp_urgent_pointer);/*紧急指针*/

flags=tcp_protocol->tcp_flags;/*标识*/

checksum=ntohs(tcp_protocol->tcp_checksum);/*校验和*/

cout<<"TCP协议"<

cout<<"源端口号:

"<

cout<<"目的端口号:

"<

switch(destination_port)

{

case80:

cout<<"上层协议为HTTP协议:

"<

break;

case21:

cout<<"上层协议为FTP协议"<

break;

case23:

cout<<"上层协议为TELNET协议"<

break;

case25:

cout<<"上层协议为SMTP协议"<

break;

case110:

cout<<"上层协议为POP3协议"<

break;

default:

break;

}

cout<<"序列码"<

cout<<"确认号:

"<

cout<<"首部长度:

"<

cout<<"保留:

"<tcp_reserved)<

cout<<"标记:

";

if(flags&0x08)

cout<<"PSH"<

if(flags&0x10)

cout<<"ACK"<

if(flags&0x02)

cout<<"SYN"<

if(flags&0x20)

cout<<"URG"<

if(flags&0x01)

cout<<"FIN"<

if(flags&0x04)

cout<<"RST"<

cout<

cout<<"窗口大小:

"<

cout<<"校验和:

"<

cout<<"紧急指针:

"<

}

/*下面是实现UDP协议分析的函数,函数类型与回调函数相同*/

voidudp_protocol_packet_callback(u_char*argument,constpcap_pkthdr*packet_header,constu_char*packet_content)

{

classudp_header*udp_protocol;/*UDP协议变量*/

u_shortsource_port;/*源端口*/

u_shortdestination_port;/*目的端口号*/

u_shortlength;//长度

udp_protocol=(classudp_header*)(packet_content+14+20);/*获得UDP协议内容*/

source_port=ntohs(udp_protocol->udp_source_port);/*获得源端口*/

destination_port=ntohs(udp_protocol->udp_destination_port);/*获得目的端口*/

length=ntohs(udp_protocol->udp_length);/*获得长度*/

cout<<"UDP协议"<

cout<<"源端口号:

"<

cout<<"目的端口号:

"<

switch(destination_port)

{

case138:

cout<<"上层协议为NETBIOS数据报服务"<

break;

case137:

cout<<"上层协议为NETBIOS名字服务"<

break;

case139:

cout<<"上层协议为NETBIOS会话服务"<

break;

case53:

cout<<"上层协议为域名服务"<

break;

default:

break;

}

cout<<"长度:

"<

cout<<"校验和:

"<udp_checksum)<

}

/*下面是实现分析ICMP协议的函数,函数类型与回调函数相同*/

voidicmp_protocol_packet_callback(u_char*argument,constpcap_pkthdr*packet_header,constu_char*packet_content)

{

classicmp_header*icmp_protocol;/*ICMP协议变量*/

icmp_protocol=(icmp_header*)(packet_content+14+20);/*获得ICMP协议内容*/

cout<<"ICMP协议"<

cout<<"ICMP类型:

"<icmp_type<

switch(icmp_protocol->icmp_type)

{

case8:

cout<<"ICMP回显请求协议"<

cout<<"ICMP代码:

"<icmp_code<

cout<<"标识符:

"<icmp_id<

cout<<"序列码:

"<icmp_sequence<

break;

case0:

cout<<"ICMP回显应答协议"<

cout<<"ICMP代码:

"<icmp_code<

cout<<"标识符:

"<

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

当前位置:首页 > 高中教育 > 英语

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

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