从USBPcap库读取原始数据

Read raw data from USBPcap library

本文关键字:原始数据 读取 USBPcap      更新时间:2023-10-16

我使用USBPcap来捕获USB设备驱动器和操作系统之间传输的数据,但它会将数据保存在.pcap文件中,该文件只能由Wireshark自己读取。我进入USBPcap源代码,但因为我不知道任何关于Windows API的信息,我无法理解源代码,只是在调试时发现了这个文件:

https://github.com/desowin/usbpcap/blob/master/USBPcapCMD/thread.c#L177

我需要原始数据,而不是.pcap编码的文件。我怎么能得到它?

.pcap文件格式很简单,最简单的解决方案是自己解析或使用现有库:https://wiki.wireshark.org/Development/LibpcapFileFormat

USBPcap使用上述格式:https://github.com/desowin/usbpcap/blob/37a8e3cf12234df96a7e101eec336085dbb3e4c7/USBPcapDriver/include/USBPcap.h#L63

文件头为:

typedef struct pcap_hdr_s {
UINT32 magic_number;   /* magic number */
UINT16 version_major;  /* major version number */
UINT16 version_minor;  /* minor version number */
INT32  thiszone;       /* GMT to local correction */
UINT32 sigfigs;        /* accuracy of timestamps */
UINT32 snaplen;        /* max length of captured packets, in octets */
UINT32 network;        /* data link type */
} pcap_hdr_t;

每个数据包的格式为:

typedef struct pcaprec_hdr_s {
UINT32 ts_sec;         /* timestamp seconds */
UINT32 ts_usec;        /* timestamp microseconds */
UINT32 incl_len;       /* number of octets of packet saved in file */
UINT32 orig_len;       /* actual length of packet */
} pcaprec_hdr_t;