基于WinPcap的Wireshark/tcp包的抓取实现

本文介绍了如何使用WinPcap库在Wireshark中实现TCP包的抓取。首先通过pcap_findalldevs_ex获取设备列表并选择适配器,接着利用pcap_datalink过滤非以太网数据包,再设置过滤器只捕获IP和TCP协议的包。由于数据包捕获由驱动控制,因此未使用回调函数,而是采用pcap_next_ex进行逐包处理。
摘要由CSDN通过智能技术生成

获得设备列表 pcap_findalldevs_ex,然后从设备列表中跳转到某个设备,并带开适配器。

调用pcap_datalink是为了减少数据包的数量:过滤掉不属于以太网的包。之后再设置过滤器,这里只是为了抓取 ip 和 tcp 的包

char packet_filter[] = "ip and tcp";

由于使用回调函数是由数据包捕获驱动直接控制,所以用户程序是无法直接控制的。这里我没有采用回调函数,而是使用的pcap_next_ex

#include <stdio.h>
#include <iostream>
#define HAVE_REMOTE
#include "pcap.h"
#include "remote-ext.h"

#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "wpcap.lib")

using namespace std;

// 用户保存4字节的IP地址
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 ver_ihl;
	u_char tos;
	u_short tlen;
	u_short identification;
	u_short flags_fo;
	u_char ttl;
	u_char proto;
	u_short crc;
	ip_address saddr;
	ip_address daddr;
	u_int op_pad;
}ip_header;

// 保存UDP首部
type
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值