windows pcapng文件抽出udp负载

本文介绍如何使用Wireshark处理pcapng格式的数据流,针对特定的UDP传输(192.168.178.1:10005),设置过滤器并解析IP及负载头部。涉及到NpcapSDK的依赖和负载定位技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • wireshark收下的流是pcapng格式的,需要另存为pcap格式
  • 依赖Npcap SDK
  • 不同的需求抽取负载的方法不同,主要是负载的起始位置不同

我的需求:UDP传输,端口为10005,绑定的IP为192.168.178.1
所以设置过滤器"host 192.168.178.1 and udp port 9000"

#define RTP_HEAD_LEN  (12)

#define ipFrameFrontLen  (16 * 2 + 8 + 2)  //UDP负载开始位置


/* 4 bytes IP address */
typedef struct ip_address
{
	u_char byte1;
	u_char byte2;
	u_char byte3;
	u_char byte4;
}ip_address;

/* IPv4 header */
typedef struct ip_header
{
	u_char	ver_ihl;		// Version (4 bits) + Internet header length (4 bits)
	u_char	tos;			// Type of service 
	u_short tlen;			// Total length 
	u_short identification; // Identification
	u_short flags_fo;		// Flags (3 bits) + Fragment offset (13 bits)
	u_char	ttl;			// Time to live
	u_char	proto;			// Protocol
	u_short crc;			// Header checksum
	ip_address	saddr;		// Source address
	ip_address	daddr;		// Destination address
	u_int	op_pad;			// Option + Padding
}ip_header;

int main()
{
	pcap_t* fp;
	char errbuf[
	PCAP_ERRBUF_SIZE];
	//打开Pcap包
	if ((fp = pcap_open_offline(1.pcap”, errbuf)) == NULL)
	{
		return 1;
	}
	std::string packet_filter = "host 192.168.178.1 and udp port 9000"; //过滤(源IP:172.16.21.174且端口8080)
	struct bpf_program fcode;	// used in pcap_compile()
	if (pcap_compile(fp, &fcode, packet_filter.c_str(), 1, 0) >= 0)
	{
		//设置过滤器
		if (pcap_setfilter(fp, &fcode) < 0)
		{
			fprintf(stderr, "\nError setting the filter.\n");
			return 1;
		}
	}
	else
	{
		fprintf(stderr, "\nError setting the filter.\n");
		return 1;
	}
	//读取文件直到结尾
	struct pcap_pkthdr* header;		//报文头
	const unsigned char* pkt_data;	//报文内容
	ip_header* ih;
	int read_count = 0;


	while ((pcap_next_ex(fp, &header, &pkt_data)) > 0)
	{
  		memcpy(buffer, &pkt_data[ipFrameFrontLen], header->caplen- ipFrameFrontLen);
  		//buff 就是RTP数据
		Sleep(1);
	}
	pcap_close(fp);
}
	
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值