已阅读5页,还剩12页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
上海电力学院上海电力学院 计算机网络安全 计算机网络安全 1 课程实验报告课程实验报告 实验名称 winpcap 编程实验 基于 Winpcap 编程实现抓包实验 一 本设计要达到的目标 基于 winpcap 编程实现对网络数据的捕获 并分析数据类型 对于 IP ICMP ARP UDP 等 能够自动识别其协议类型并分析帧的构成 二 实现步骤 1 需要通过资料来了解 winpcap 抓包的工作原理 熟悉其运行过程 Winpcap 的内部结构 Wincap 有三部分组成 一个数据包监听设备驱动程序 一个低级的动态连接库和一个高级 的静态连接库 底层动态链接库运行在用户层 它将应用程序和数据包监听设备驱动程序 隔离开来 使得应用程序可以不加修改地在不同的 WINDOWS 系统上运行 高级的静态链 接库和应用程序编译在一起 它使用低级动态链接库提供的服务 向应用程序提供完善的 监听接口 抓包是 WinPcap 的基本功能 也是 NPF 最重要的操作 在抓包的时候 驱动 例如 NIC Driver 使用一个网络接口监视着数据包 并将这些数据包完整无缺地投递给用 户级应用程序 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 三 系统流程图 主函数 以太网协议分析函数 分析 ARP 协议函 数 分析 Ip 协议函数 分析 ICMP 协议函 数 判断下层函 数 分析 TCP 协议函 数 分析 UDP 协议函 数 16 进制数据 四 关键代码及其分析 主函数 void main pcap t pcap handle Winpcap 句柄 char error content PCAP ERRBUF SIZE 存储错误信息 char net interface 网络接口 bpf program bpf filter BPF 过滤规则 char bpf filter string 过滤规则字符串 bpf u int32 net mask 掩码 bpf u int32 net ip 网路地址 net interface pcap lookupdev error content 获得可用的网络接口 pcap lookupnet net interface 获 得网络地址和掩码地址 pcap handle pcap open live net interface BUFSIZ 1 1 error content 打开网路接口 pcap compile pcap handle 编译 BPF 过滤规则 pcap setfilter pcap handle 设置过滤 规则 对 IP 协议的定义 class ip header public if defined WORDS BIGENDIAN u int8 t ip version 4 版本 ip header length 4 首部长度 else u int8 t ip header length 4 ip version 4 endif u int8 t ip tos 服务质量 u int16 t ip length 长度 u int16 t ip id 标识 u int16 t ip off 偏移 u int8 t ip ttl 生存时间 u int8 t ip protocol 协议类型 u int16 t ip checksum 校验和 in addr ip souce address 源 IP 地址 in addr ip destination address 目的 IP 地址 pcap loop pcap handle n ethernet protocol packet callback NULL 注 册回调函数 循环捕获网络数据包 利用回调函数来处理每个数据包 分析 UDP 协议的函数代码 void udp protocol packet callback u char argument const pcap pkthdr packet header const u char packet content class udp header udp protocol UDP 协议变量 u short source port 源端口 u short destination port 目的端口号 u short length 长度 udp protocol class udp 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 协议 endl cout 源端口号 dec source port endl cout 目的端口号 dec destination port endl switch destination port case 138 cout 上层协议为 NETBIOS 数据报服务 endl break case 137 cout 上层协议为 NETBIOS 名字服务 endl break case 139 cout 上层协议为 NETBIOS 会话服务 endl break case 53 cout 上层协议为域名服务 endl break default break cout 长度 length endl cout 校验和 setw 4 setfill 0 hex udp checksum endl 五 参考文献 1 Winpcap 中文文档 2 网络资料 完整源程序 include pcap h include include include using namespace std 以下是以太网协议格式的定义 class ether header public u int8 t ether dhost 6 目的以太网地址 u int8 t ether shost 6 源以太网地址 u int16 t ether type 以太网类型 下面是 ARP 协议格式的定义 class arp header public u int16 t arp hardware type 硬件类型 u int16 t arp protocol type 协议类型 u int8 t arp hardware length 硬件地址长度 u int8 t arp protocol length 协议地址长度 u int16 t arp operation code 操作码 u int8 t arp source ethernet address 6 源以太网地址 u int8 t arp source ip address 4 源 IP 地址 u int8 t arp destination ethernet address 6 目的以太网地址 u int8 t arp destination ip address 4 目的 IP 地址 下面是 IP 协议格式的定义 class ip header public if defined WORDS BIGENDIAN u int8 t ip version 4 版本 ip header length 4 首部长度 else u int8 t ip header length 4 ip version 4 endif u int8 t ip tos 服务质量 u int16 t ip length 长度 u int16 t ip id 标识 u int16 t ip off 偏移 u int8 t ip ttl 生存时间 u int8 t ip protocol 协议类型 u int16 t ip checksum 校验和 in addr ip souce address 源 IP 地址 in addr ip destination address 目的 IP 地址 下面是 UDP 协议格式定义 class udp header public u int16 t udp source port 源端口号 u int16 t udp destination port 目的端口号 u int16 t udp length 长度 u int16 t udp checksum 校验和 下面是 TCP 协议格式的定义 class tcp header public u int16 t tcp source port 源端口号 u int16 t tcp destination port 目的端口号 u int32 t tcp sequence lliiuuwweennttaaoo 序列号 u int32 t tcp acknowledgement 确认序列号 ifdef WORDS BIGENDIAN u int8 t tcp offset 4 偏移 tcp reserved 4 未用 else u int8 t tcp reserved 4 未用 tcp offset 4 偏移 endif u int8 t tcp flags 标记 u int16 t tcp windows 窗口大小 u int16 t tcp checksum 校验和 u int16 t tcp urgent pointer 紧急指针 下面是 ICMP 协议格式的定义 class icmp header public u int8 t icmp type ICMP 类型 u int8 t icmp code ICMP 代码 u int16 t icmp checksum 校验和 u int16 t icmp id 标识符 u int16 t icmp sequence 序列码 下面是分析 TCP 协议的函数 其定义方式与回调函数相同 void tcp protocol packet callback u char argument const pcap pkthdr packet header const u char packet content class tcp header tcp protocol TCP 协议变量 u char flags 标记 int header length 长度 u short source port 源端口 u short destination port 目的端口 u short windows 窗口大小 u short urgent pointer 紧急指针 u int sequence 序列号 u int acknowledgement 确认号 u int16 t checksum 校验和 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 协议 endl cout 源端口号 dec source port endl cout 目的端口号 dec destination port endl switch destination port case 80 cout 上层协议为 HTTP 协议 endl break case 21 cout 上层协议为 FTP 协议 endl break case 23 cout 上层协议为 TELNET 协议 endl break case 25 cout 上层协议为 SMTP 协议 endl break case 110 cout 上层协议为 POP3 协议 endl break default break cout 序列码 sequence endl cout 确认号 acknowledgement endl cout 首部长度 dec header length endl cout 保留 tcp reserved endl cout 标记 if flags if flags if flags if flags if flags if flags cout endl cout 窗口大小 windows endl cout 校验和 setw 4 setfill 0 hex checksum endl cout 紧急指针 urgent pointer udp source port 获得源端口 destination port ntohs udp protocol udp destination port 获得目 的端口 length ntohs udp protocol udp length 获得长度 cout UDP 协议 endl cout 源端口号 dec source port endl cout 目的端口号 dec destination port endl switch destination port case 138 cout 上层协议为 NETBIOS 数据报服务 endl break case 137 cout 上层协议为 NETBIOS 名字服务 endl break case 139 cout 上层协议为 NETBIOS 会话服务 endl break case 53 cout 上层协议为域名服务 endl break default break cout 长度 length endl cout 校验和 setw 4 setfill 0 hex udp checksum endl 下面是实现分析 ICMP 协议的函数 函数类型与回调函数相同 void icmp protocol packet callback u char argument const pcap pkthdr packet header const u char packet content class icmp header icmp protocol ICMP 协议变量 icmp protocol icmp header packet content 14 20 获得 ICMP 协 议内容 cout ICMP 协议 endl cout ICMP 类型 icmp type icmp type case 8 cout ICMP 回显请求协议 endl cout ICMP 代码 icmp code endl cout 标识符 icmp id endl cout 序列码 icmp sequence endl break case 0 cout ICMP 回显应答协议 endl cout ICMP 代码 icmp code endl cout 标识符 setw 4 setfill 0 hex icmp id endl cout 序列码 icmp sequence endl break default break cout ICMP 校验和 setw 4 setfill 0 hex icmp checksum endl 获得 ICMP 校验和 return 下面是实现 ARP 协议分析的函数 函数类型与回调函数相同 void arp protocol packet callback u char argument const pcap pkthdr packet header const u char packet content arp header arp protocol u short protocol type u short hardware type u short operation code u char mac string in addr source ip address in addr destination ip address u char hardware length u char protocol length cout ARP 协议 arp hardware type protocol type ntohs arp protocol arp protocol type operation code ntohs arp protocol arp operation code hardware length arp protocol arp hardware length protocol length arp protocol arp protocol length cout 硬件类型 arp hardware type endl cout 协议类型 setw 4 setfill 0 arp protocol type endl cout 硬件地址长度 arp hardware length endl cout 协议地址长度 arp protocol length endl cout ARP 操作 arp operation code endl switch operation code case 1 cout ARP 请求协议 endl break case 2 cout ARP 应答协议 endl break case 3 cout RARP 请求协议 endl break case 4 cout RARP 应答协议 endl break default break cout 源以太网地址 arp source ethernet address cout setw 2 setfill 0 hex int mac string setw 2 setfill 0 hex int mac string 1 setw 2 setfill 0 hex int mac string 2 setw 2 setfill 0 hex int mac string 3 setw 2 setfill 0 hex int mac string 4 setw 2 setfill 0 hex int mac string 5 arp source ip address sizeof in addr cout 源 IP 地址 inet ntoa source ip address endl cout 目的以太网地址 arp destination ethernet address cout setw 2 setfill 0 hex int mac string setw 2 setfill 0 hex int mac string 1 setw 2 setfill 0 hex int mac string 2 setw 2 setfill 0 hex int mac string 3 setw 2 setfill 0 hex int mac string 4 setw 2 setfill 0 hex int mac string 5 arp destination ip address sizeof in addr cout 目的 IP 地址 inet ntoa destination ip address ip checksum 获得校验和 header length ip protocol ip header length 4 获得长度 tos ip protocol ip tos 获得服务质量 offset ntohs ip protocol ip off 获得偏移 cout IP 协议 endl cout 版本号 ip version endl cout 首部长度 header length endl cout 服务质量 ip tos endl cout 总长度 ip length endl cout 标识 setw 4 setfill 0 hex ip id endl cout 偏移 offset cout 生存时间 ip ttl endl cout 协议类型 ip protocol ip protocol case 6 cout 上层协议为 TCP 协议 endl break case 17 cout 上层协议为 UDP 协议 endl break case 1 cout 上层协议为 ICMP 协议 ICMP endl break default break cout 校验和 checksum endl cout 源 IP 地址 ip souce address endl 获得源 IP 地址 cout 目的 IP 地址 ip destination address ip protocol 根据 IP 协议判断上层协议 case 6 tcp protocol packet callback argument packet header packet content 上层协议是 TCP 协议 调用分析 TCP 协议的函数 注意参数的传递 break case 17 udp protocol packet callback argument packet header packet content 上层协议是 UDP 协议 调用分析 UDP 协议的函数 注意参数的传递 break case 1 icmp protocol packet callback argument packet header packet content 上层协议是 ICMP 协议 调用分析 ICMP 协议的函数 注意参数的传 递 break default break 下面是分析以太网协议的函数 也是回调函数 void ethernet protocol packet callback u char argument const pcap pkthdr packet header const u char packet content u short ethernet type 以太网类型 ether header ethernet protocol 以太网协议变量 u char mac string 以太网地址 static int packet number 1 数据包个数 静态变量 cout endl cout 捕获第 packet number 个网络数据包 endl cout 捕获时间 endl cout ts tv sec endl 获得捕 获数据包的时间 cout 数据包长度 endl cout len endl cout 以太网协议 ether type 获得以太网 类型 cout 以太网类型 setw 4 setfill 0 hex ethernet type endl switch ethernet type 根据以太网类型判断 case 0 x0800 cout 上层协议为 IP 协议 endl break case 0 x0806 cout 上层协议为 ARP 协议 endl break case 0 x8035 cout 上层协议为 RARP 协议 ether shost cout mac string 帧源地址 setw 2 setfill 0 hex int mac string setw 2 setfill 0 hex int mac string 1 setw 2 setfill 0 hex int mac string 2 setw 2 setfill 0 hex int mac string 3 setw 2 setfill 0 hex int mac string 4 setw 2 setfill 0 hex int mac string 5 ether dhost co
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 生物骨干教师面试题目及答案
- 2025智慧法庭系统行业研究及司法应用与技术创新管理分析
- 2025智慧水利监测设备市场供需分析及投资前景规划
- 社区护士考试试题及答案
- 2025年三门峡小学英语面试题库及答案
- 吴江校招面试实战专业解答与技巧分享
- 商务谈判技巧与沟通策略详解
- 未来五年片式压电陶瓷器件企业ESG实践与创新战略分析研究报告
- 未来五年密闭糊企业制定与实施新质生产力战略分析研究报告
- 南方航空公司面试技巧大全
- 银屑病皮肤护理指导
- (2025年)新疆阿拉尔市辅警招聘《公安基础知识》真题及答案解析
- 2025年教育信息化数字化校园管理系统建设实施方案
- 挂篮施工安全管理课件
- 《中国饮食文化》 课件 第四章 中国茶文化
- 含煤废水处理系统
- DB63T-国家公园天空地一体化监测系统技术规范
- 吉林开放大学2025年《中华民族共同体概论》形成性考核1-3综合考核答案
- 2025年公需科目(心理健康与职业发展)-试题练习答案
- 四川大学内部高分子物理课件
- 2024-2025学年广东省广州市黄埔广附教育集团八年级上学期期中数学试卷(含答案)
评论
0/150
提交评论