packet packetcapture改包发包

winpcap库既可以用来抓包,也可以用来发包。
Although the name WinPcap indicates clearly that the purpose of the library is packet capture, other useful features for raw networking are provided. Among them, the user can find a complete set of functions to send packets.
Note that the original libpcap library at the moment doesn’t provide any way to send packets, therefore all the functions shown here are WinPcap extensions and will not work under Unix.
Sending a single packet with pcap_sendpacket()
The simplest way to send a packet is shown in the following code snippet. After opening an adapter, pcap_sendpacket() is called to send a hand-crafted packet. pcap_sendpacket() takes as arguments a buffer containing the data to send, the length of the buffer and the adapter that will send it. Notice that the buffer is sent to the net as is, without any manipulation. This means that the application has to create the correct protocol headers in order to send something meaningful.
#include &stdlib.h&
#include &stdio.h&
#include &pcap.h&
void main(int argc, char **argv)
char errbuf[PCAP_ERRBUF_SIZE];
u_char packet[100];
if (argc != 2)
printf("usage: %s interface (e.g. 'rpcap://eth0')", argv[0]);
if ( (fp= pcap_open(argv[1],
PCAP_OPENFLAG_PROMISCUOUS,
) ) == NULL)
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", argv[1]);
packet[0]=1;
packet[1]=1;
packet[2]=1;
packet[3]=1;
packet[4]=1;
packet[5]=1;
packet[6]=2;
packet[7]=2;
packet[8]=2;
packet[9]=2;
packet[10]=2;
packet[11]=2;
for(i=12;i&100;i++)
packet[i]=i%256;
if (pcap_sendpacket(fp, packet, 100 ) != 0)
fprintf(stderr,"\nError sending the packet: \n", pcap_geterr(fp));
Send queues
While pcap_sendpacket() offers a simple and immediate way to send a single packet, send queues provides an advanced, powerful and optimized mechanism to send a collection of packets. A send queue is a container for a variable number of packets that will be sent to the network. It has a size, that represents the maximum amount of bytes it can store.
A send queue is created calling the pcap_sendqueue_alloc() function, specifying the size of the new send queue.
Once the send queue is created, pcap_sendqueue_queue() can be used to add a packet to the send queue. This function takes a pcap_pkthdr with the timestamp and the length and a buffer with the data of the packet. These parameters are the same as those received by pcap_next_ex() and pcap_handler(), therefore queuing a packet that was just captured or read from a file is a matter of passing these parameters to pcap_sendqueue_queue().
To transmit a send queue, WinPcap provides the pcap_sendqueue_transmit() function. Note the third parameter: if nonzero, the send will be synchronized, i.e. the relative timestamps of the packets will be respected. This operation requires a remarkable amount of CPU, because the synchronization takes place in the kernel driver using “busy wait” loops. Although this operation is quite CPU intensive, it often results in very high precision packet transmissions (often around few microseconds or less).
Note that transmitting a send queue with pcap_sendqueue_transmit() is much more efficient than performing a series of pcap_sendpacket(), because the send queue is buffered at kernel level drastically decreasing the number of context switches.
When a queue is no longer needed, it can be deleted with pcap_sendqueue_destroy() that frees all the buffers associated with the send queue.
The next program shows how to use send queues. It opens a capture file with pcap_open_offline(), then it moves the packets from the file to a properly allocated send queue. At his point it transmits the queue, synchronizing it if requested by the user.
Note that the link-layer of the dumpfile is compared with the one of the interface that will send the packets using pcap_datalink(), and a warning is printed if they are different – it is important that the capture-file link-layer be the same as the adapter’s link layer for otherwise the tranmission is pointless.
#include &stdlib.h&
#include &stdio.h&
#include &pcap.h&
#include &remote-ext.h&
void usage();
void main(int argc, char **argv)
pcap_t *indesc,*
char errbuf[PCAP_ERRBUF_SIZE];
char source[PCAP_BUF_SIZE];
int caplen,
pcap_send_queue *
struct pcap_pkthdr *
float cpu_
u_int npacks = 0;
if (argc &= 2 || argc &= 5)
capfile=fopen(argv[1],"rb");
if(!capfile){
printf("Capture file not found!\n");
fseek(capfile , 0, SEEK_END);
caplen= ftell(capfile)- sizeof(struct pcap_file_header);
fclose(capfile);
if(argc == 4 && argv[3][0] == 's')
sync = TRUE;
sync = FALSE;
if ( pcap_createsrcstr( source,
PCAP_SRC_FILE,
fprintf(stderr,"\nError creating a source string\n");
if ( (indesc= pcap_open(source, 65536, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL)
fprintf(stderr,"\nUnable to open the file %s.\n", source);
if ( (outdesc= pcap_open(argv[2], 100, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL)
fprintf(stderr,"\nUnable to open adapter %s.\n", source);
if (pcap_datalink(indesc) != pcap_datalink(outdesc))
printf("Warning: the datalink of the capture differs from the one of the selected interface.\n");
printf("Press a key to continue, or CTRL+C to stop.\n");
getchar();
squeue = pcap_sendqueue_alloc(caplen);
while ((res = pcap_next_ex( indesc, &pktheader, &pktdata)) == 1)
if (pcap_sendqueue_queue(squeue, pktheader, pktdata) == -1)
printf("Warning: packet buffer too small, not all the packets will be sent.\n");
if (res == -1)
printf("Corrupted input file.\n");
pcap_sendqueue_destroy(squeue);
cpu_time = (float)clock ();
if ((res = pcap_sendqueue_transmit(outdesc, squeue, sync)) & squeue-&len)
printf("An error occurred sending the packets: %s. Only %d bytes were sent\n", pcap_geterr(outdesc), res);
cpu_time = (clock() - cpu_time)/CLK_TCK;
printf ("\n\nElapsed time: %5.3f\n", cpu_time);
printf ("\nTotal packets generated = %d", npacks);
printf ("\nAverage packets per second = %d", (int)((double)npacks/cpu_time));
printf ("\n");
pcap_sendqueue_destroy(squeue);
pcap_close(indesc);
pcap_close(outdesc);
void usage()
printf("\nSendcap, sends a libpcap/tcpdump capture file to the net. Copyright (C) 2002 Loris Degioanni.\n");
printf("\nUsage:\n");
printf("\t sendcap file_name adapter [s]\n");
printf("\nParameters:\n");
printf("\nfile_name: the name of the dump file that will be sent to the network\n");
printf("\nadapter: the device to use. Use \"WinDump -D\" for a list of valid devices\n");
printf("\ns: if present, forces the packets to be sent synchronously, i.e. respecting the timestamps in the dump file. This option will work only under Windows NTx.\n\n");
WinPcap基础知识(第八课:发送数据包)
尽管WinPcap这个名字已经很清晰的表明这个库是用来捕获数据包的,但是另外一个非常有用的功能是提供了原始网络操作。这里,用户可以找到一系列函数来发送数据包,这节课我们就会向大家一一展示。注意到原始的...
winpcap 快速入门 实现抓包&&发包
winpcap 快速入门 抓包&&发包
关键的winpcap api
实现抓包/发包
系统: windows 8.1
编译器: visaul stiduo 2015
pcap 执行版本: 4.1.3
WinPcap介绍
WinPcap是一个基于Win32平台的,用于捕获网络数据包并进行分析的开源库。
大多数网络应用程序通过被广泛使用的操作系统元件来访问网络,比如sockets。这是一种简单的实现方式,因...
使用WinPCAP接口编程抓取数据包
使用本文档,需要有一些C基础,除非你只是想了解基本的原理而不实现。有些地方需要有一些编程经验,我尽量详细的描述相关概念。此外,一些网络相关的知识可以帮助你理解此教程。教程中实现的嗅探器在FreeBSD...
个别数据结构
pcap_if_t是一个interface数据结构,表明网络接口的信息。网络接口就是interface,就是我们用来上网的设备,一般为网卡,还有一些虚拟网卡也算作...
*投诉人联系方式:
*版权证明: 只允许上传png/jpeg/jpg/gif格式的图片,且小于3M
*详细原因:
交 &em&WinPcap&/em& 3积分 立即下载 ...
使用WinPcap抓取各种包
使用WinPcap抓各种包
#defineHAVE_REMOTE
#include&pcap.h&
#includ...
&em&winpcap&/em&4.0.1中文安装,开发网络抓包,流量监测工具... &em&winpcap&/em&4.0.1中文安装,开发网络抓包,流量监测工具 综合评分:5 收藏(2)评论(26)举报 所需: 3积分/C币 开通...
Winpcap的安装使用方法
1.到http://www.winpcap.org/install/default.htm下载winpcap的安装包,然后到http://www.winpcap....
Winpcap网络编程八之Winpcap学习教程,发包,发包!
时间问题,
使用 pcap_sendpacket() 发送单个数据包
下面的代码展示了发送一个数据包的最简单的方式。打开适配器以后,调用 pcap_sendpacket() 来发送手工制...
没有更多推荐了,豆丁微信公众号
君,已阅读到文档的结尾了呢~~
一个简易网络嗅探器的实现,网络嗅探器设计与实现,网络嗅探器,网络嗅探器破解版,网络嗅探器怎么用,地瓜网络嗅探器,网络嗅探器5.5,网络嗅探器 影音神探,网络资源嗅探器,简单的网络嗅探器
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
一个简易网络嗅探器的实现
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='http://www.docin.com/DocinViewer--144.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口21733 条评论分享收藏感谢收起h5.darkal.cn/har/demo.apk下载地址:赞同 12150 条评论分享收藏感谢收起0、说在前面的话
1)本文将以一个初学者的角度,一步一步几乎是从0开始讲述如何完成一个基于winpcap+MFC的sniffer(嗅探器)当然我指的“0”并不是指连编程都不会,如果C/C++不知为何物那么还是别看这篇文章了,本文只有那么几点小小要求:
a.懂基本的编程语法,只会java的没关系,语言这东西语法差别不大,看着看着就懂了
b.懂一点点网络知识,起码知道OSI七层网络模型或者TCP/IP四层(也有说五层的)网络模型
c.想学怎么做一个sniffer,而不是想来这里down代码,这种代码CSDN下载里多得是,不用来这里了
2)本文里的程序不是最优秀的,不是最牛的,甚至不知道能不能算作优秀,所以牛人可以略过此文,此文是给那些想开发但又不会开发,懂那么一点点又其实不太懂的人看的(写这个程序之前,我在MFC方面也算是这一类人:)
3)本文不讨论专业、学术内容,什么架构之类的都不在讨论范围之类,一切以实用为主,一切以开发出一个sniffer为核心
4)本文主要内容:
a.工欲善其事,必先配环境——配置winpcap开发环境
b.掀起你的盖头来,让我来画你的脸——用MFC开发GUI
c.安得广厦千万间,先画蓝图再砌砖——搭建wipcap抓包框架
d.要想从此过,留下协议头——各层网络协议头的实现
e.莫道无人能识君,其实我懂你的心——解析数据包
f.千呼万唤始出来,不抱琵琶也露面——将解析数据写到GUI上
5)来看看我们要做的最终成果:
列出监测主机的所有网卡,选择一个网卡,在混杂模式下进行监听。
捕获所有流经网卡的数据包,并利用WinPcap函数库设置过滤规则。
可以本地文件形式保存和读取已捕获的数据包信息。
分析捕获到的数据包的包头和数据,按照各种协议的格式进行格式化显示。
支持协议包括:、、、、、、
一、工欲善其事,必先配环境——配置winpcap开发环境
WinPcap(Windows Packet Capture)是Windows平台下的链路层网络访问工具,其目的在于为Windows应用程序提供访问网络底层的能力。需要注意的是WinPcap只能把数据包从网络中抓过来(其实是复制过来,并没有真实的抓过来,感觉像是拍了张照),并不能修改数据。
“那么在哪里可以买得到呢?”,其实不用买,要得到很容易,更何况咱中国人没有为软件付费的习惯:),参看下一节,“不可或缺的东西”:
1.1、不可或缺的东西
WinPcap V4.1.2:驱动程序、Dll文件
WinPcap V4.1.2 Developer’s
Pack:库文件、头文件、简单的示例程序代码和帮助文件
当然还有VisualStudio2008或者VC++6.0,本文用的是VS2008
(这玩意的下载网址就不用我介绍了吧)
1.2、配置过程
l添加头文件目录
工具-&属性-&项目和解决方案-&
VC++目录-&包含文件-&添加WinPcap开发包中的Include目录,如下图:
Tools-&Options-&Derectories-&Include files-&添加WinPcap开发包中的Include目录,如下图:
l添加库文件目录
与上一个步骤一样,只有一小个变化,选择“库文件”,然后添加WinPcap开发包中的Lib目录,如下图:
Tools-&Options-&Derectories-&Library files-&添加WinPcap开发包中的Lib目录,如下图:
l增加与WinPcap有关的预处理定义
项目-&项目属性-&配置属性-&预处理定义-&添加WPCAP和HAVE_REMOTE,如下图:
Project-&Setting-&C/C++-&Preprocessor-&添加WPCAP和HAVE_REMOTE,如下图:
l添加wpcap.lib库文件
项目-&项目属性-&配置属性-&连接器-&命令行-&附加选项框中加入wpcap.lib,如下图:
Project-&setting-&link-&加入wpcap.lib,如下图:
l添加pcap.h头文件
在使用WinPcap API的所有源文件中添加#include
1.3、运行测试代码
所有配置工作全部搞定,现在来运行测试代码,如果能顺利执行,那么就配置成功了。
这里的测试代码其实就是WinPcap中自带的实例,还记得“不可或缺的文件”中下载的两个文件没?从WpdPack_4_1_2.zip解压出来的文件中有Examples-pcap和Examples-remote两个文件夹,进入Examples-pcap中可以看到,里面有很多实例,任意打开一个文件夹,打开其中的.dsw文件,执行一下就知道了。
笔者这里选择的是UDPdump这个实例,执行成功的画面如下:
第一章至此结束,欲知后事如何,请看下一章“掀起你的盖头来,让我来画你的脸——用MFC开发GUI”分解!
为了方便学习,完整的代码下载在这里:
使用WinPCAP接口编程抓取数据包
使用本文档,需要有一些C基础,除非你只是想了解基本的原理而不实现。有些地方需要有一些编程经验,我尽量详细的描述相关概念。此外,一些网络相关的知识可以帮助你理解此教程。教程中实现的嗅探器在FreeBSD...
WinPcap编程【1】--编程环境的设置
vs2008.1、安装winpcap官网下载地址http://www.winpcap.org/install/bin/WinPcap_4_1_1.exe 安装so easy,不多说了。2、下载WinP...
winpcap安装使用教程
1、安装winpcap
地址:http://www.winpcap.org/install/default.htm =》安装软件
地址:http://www.winpcap.org/archive =...
Winpcap网络编程九之Winpcap实战,ARP协议获得MAC表及主机通信
利用Winpcap
完成两台主机之间的数据通信(数据链路层)
仿真ARP协议获得网段内主机的MAC表
使用帧完成两台主机的通信(Hello! I’m …)...
利用WinpCap 编写抓包程序
利用WinpCap 编写抓包程序
网友:yeahilly 发布于:
11:12 (共有条评论) 查看评论 | 我要评论
&em&winpcap编程&/em& 截获数据帧 发送自定义帧!!黑客学习,这篇教程将会指引读者逐步了解&em&WinPcap编程&/em&, 从简单的基础函数(获取网络接口列表, 捕捉数据包)到更高级的内容(处理...
获取完适配器信息之后,我们就需要利用适配器来
WinPcap编程
WinPcap是一个开源的、运行于Win32平台下的体系结构,它的主要功能是进行数据包捕获和网络分析。它允许应用程序通过协议栈捕获和传输网络数据包,也包括内核级别的数据包过滤、网...
第五天 处理离线的存储文件( offline dump file )
Winpcap 提供了一些函数把网络通信保存到文件并且可以读取这些文件的内容。 dump 文件的格式跟 libpcap 是...
主要分为以下几个步骤:
一:vs对于Winpcap的相关配置:
所需文件:http://pan.baidu.com/s/1i4DAvcx
配置步骤...
没有更多推荐了,}

我要回帖

更多关于 packetcapture抓包 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信