欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

WinCap数据包显示

发布时间:2023/12/18 编程问答 31 豆豆
生活随笔 收集整理的这篇文章主要介绍了 WinCap数据包显示 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

BackGround: 计算机网络实验到了用WinpCap进行编程的课时

本实验是通过WinpCap编程来做一个简单的数据包获取显示的软件

---------------------------------------------------------------

一.WinpCap

libpcap是一个UNIX下的C函数库,它提供的API,能获取和过滤从任意一块网卡在数据链路层上的帧。不同的UNIX系统有不同的架构去处理数据链路层上的数据帧,所以程序员如果想要写一个能运行在UNIX上的、直接读取或者操作数据链路层上的帧的应用程序,他就不得不专门为这个特点版本的UNIX写一个访问帧的函数。libpcap的目的就是提供一个抽象层,这样程序员就能编写可运行在所有版本的UNIX上的包获取和分析工具了。
WinPcap是一个专为Windows系统设计的、基于libpcap开发的库.

winpcap有两种 一种是需要安装的驱动程序,一种是直接解压使用的开发包,这里我们要使用的是开发包,但是开发包是基于驱动程序上面使用的,所有首先应该安装Winpcap驱动程序,之后再使用编程软件去使用开发包进行开发
学校使用校园网软件的同学电脑上,基本已经安装好winpcap驱动软件了。

1、下载winpcap驱动软件。
如果是安装了wireshark或者其他数据包拦截软件的话,它就自动帮我们装了winpcap驱动。(wireshark是基于winpcap开发的)
http://www.winpcap.org/install/default.htm 这个是winpcap官网下载页面,我下载的是4.1.3版本的。
下载到本地后,直接双击安装就可以了。


2、下载winpcap开发包
http://www.winpcap.org/devel.htm 这个是winpcap官网的开发包下载,主要它的版本要和驱动版本一致。
下载到本地后,解压缩就可以了。里面有winpcap的库、头文件,以及例子和使用文档

---------------------------------------------------------------------------------------

二.配置VS2015支持进行基于WinPcap开发

1.设置环境目录

右键点击项目-属性-VC++目录



2.设置  包含目录  和  库目录

①首先设置 包含目录,设置为解压的WinpCap开发包的include目录


---------------------------------


-----------------------------------


-----------------------------------

②同样操作设置 库目录,库目录为 Lib目录

------------------------------


以上两个设置好之后就会是这样的


----------------------------------------------------------

3.设置编译预处理

在预处理器里面添加“预处理定义” WPCAP 和HAVE_REMOTE



--------------------------------------------

4.设置链接器
在输入 中添加附加依赖项ws2_32.lib   wpcap.lib Packet.lib 三个依赖包


自此VS2015配置WinpCap开发环境完毕,vs2013配置方法相同

------------------------------------------

#include "pcap.h" #include <stdio.h> #include<Windows.h> typedef struct ip_address {u_char byte1; u_char byte2;u_char byte3;u_char byte4; } ip_address;//ipv4地址typedef struct ip_header {u_char versionAndHeader; //版本 + 首部长度u_char serviceType; //服务类型 u_short totalLength; //总长 u_short identification; //标识u_short flagsAndOffset; //标志位+段偏移 u_char timeToLive; //存活时间u_char protocal; //协议名u_short headerChecksum; //校验和ip_address sourceAddress; //源地址ip_address destinationAddress;//目的地址u_int option;//附加 } ip_header;//TCP数据包头部typedef struct udp_header {u_short sourcePort; //源端口 u_short destinationPort; //目的端口 u_short len; //udp数据包长度u_short Checksum; //校验和 } udp_header;//UDP数据包头部void packHandleFunc(u_char *param, const struct pcap_pkthdr *header, const u_char *data) {struct tm *localTime; //本地时间结构char timeString[16]; //转换正常的显示时间time_t local_second;//时间戳ip_header *ipHeader; //ip数据包头部udp_header *udpHeader;//udp数据包头部u_int ip_len; //ip头部长度u_short sourceport, destinationport; //源和目的端口local_second = header->ts.tv_sec;//数据包到达时间戳localTime = localtime(&local_second); //时间戳转本地时间格式strftime(timeString, sizeof(timeString), "%H:%M:%S", localTime);//本地时间格式转正常21:11:30时间格式printf("时间:%s, 包长度:%d ", timeString, header->len);ipHeader = (ip_header *)(data + 14);//跳过以太网头部14字节长度获得ip数据包头部位置ip_len = (ipHeader->versionAndHeader & 0xf) * 4; //只取头部长度udpHeader = (udp_header *)((u_char *)ipHeader + ip_len);//跳到udp头部地址sourceport = ntohs(udpHeader->sourcePort); // ntohs 网络字节转主机字节端口destinationport = ntohs(udpHeader->destinationPort);printf("%d.%d.%d.%d:%d -> %d.%d.%d.%d:%d\n",ipHeader->sourceAddress.byte1,ipHeader->sourceAddress.byte2,ipHeader->sourceAddress.byte3,ipHeader->sourceAddress.byte4,sourceport,ipHeader->destinationAddress.byte1,ipHeader->destinationAddress.byte2,ipHeader->destinationAddress.byte3,ipHeader->destinationAddress.byte4,destinationport); } int main(int argc, char* argv[]) {pcap_if_t *Devs;pcap_if_t *d;int selectNum;int i = 0;pcap_t *handle;char errBuff[PCAP_ERRBUF_SIZE];if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &Devs, errBuff) == -1){fprintf(stderr, "获取适配器列表失败:%s\n", errBuff);system("pause");exit(1);}for (d = Devs; d; d = d->next){if (d->description)printf("%d. %s\n",++i, d->description);}if (i == 0){printf("\n没有发现适配器,请确认是否已经安装WinCap\n");system("pause");return -1;}printf("请输入要捕获数据包的适配器编号 (1-%d):", i);scanf("%d", &selectNum);if (selectNum < 1 || selectNum > i){printf("\n输入有误噢\n");pcap_freealldevs(Devs);system("pause");return -1;}for (d = Devs, i = 0; i< selectNum - 1; d = d->next, i++);if ((handle = pcap_open(d->name, 65535, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errBuff)) == NULL){fprintf(stderr, "\n不能够打开所选择适配器 %s 不支持WinCap\n", d->name);pcap_freealldevs(Devs);return -1;}printf("接下来开始监听数据包%s...\n", d->description);pcap_freealldevs(Devs);pcap_loop(handle, 0, packHandleFunc, NULL);system("pause");return 0; }
实验截图:


总结

以上是生活随笔为你收集整理的WinCap数据包显示的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。