我正在尝试读取在 FreeBSD 9.2 上使用 TShark 1.10.6 和 libpcap 版本 1.4.0 创建的 pcap 文件,并在 Windows 7 上使用 Visual Studio、SharpPcap 4.2、PacketDotNet 0.13 和 WinPcap 中的 c# 执行此操作4.1.3.
引发的异常是无法打开脱机适配器:转储文件格式错误,它发生在例程 CaptureFileReaderDevice 的行中:
IntPtr adapterHandle = LibPcapSafeNativeMethods.pcap_open_offline( captureFilename, errbuf);
有趣的是,同一系统上的 Wireshark 版本 1.10.3 - 并且也使用相同版本的 WinPcap,能够打开该文件!
什么会导致 SharpPcap 无法打开此文件?
编辑#1:
我更仔细地查看了 WireShark 的版本信息,显然它使用的是不支持 AirPcap 的 WinPcap 版本。我相信 SharpPcap 包含 AirPcap,所以我想知道这是否会有所不同。
Running on 64-bit Windows 7 Service Pack 1, build 7601, with WinPcap version 4.1.3 (packet.dll version 4.1.0.2980), based on libpcap version 1.0 branch 1_0_rel0b (20091008), GnuTLS 2.12.18, Gcrypt 1.4.6, without AirPcap.
此外,我正在编译 32 位输出。
编辑#2:
使用 WireShark 的 pcap 统计/摘要,它会显示有关捕获的以下内容:
- 操作系统:FreeBSD 9.2-RELEASE-p10
- 捕获应用程序:Dumpcap 1.10.6(Git Rev Unknown,来自未知)
最佳答案
简短的回答是,原始服务器正在生成pcap-ng捕获文件,而WinPcap当前不支持该格式,因此SharpPcap无法打开这些文件。然而,当前版本的 Wireshark 确实支持 pcap-ng 捕获文件。
导致我得出这个答案的血淋淋的细节:
我找到了Libpcap File Format在 Wireshark 文档中,根据全局 header 部分,我得到了一个包含魔数(Magic Number) 0x4d3c2b1a 的转储文件(如我的 Windows 7 系统上所示)。
医生说:
For nanosecond-resolution files, the writing application writes 0xa1b23c4d, with the two nibbles of the two lower-order bytes swapped, and the reading application will read either 0xa1b23c4d (identical) or 0x4d3cb2a1 (swapped).
这些都不匹配这个神奇的数字,所以我在原始 FreeBSD 系统上为 pcap 文件运行了 file
命令,它报告:
em0.pcap: pcap-ng capture file - version 1.0
所以我查看了该服务器上的 /usr/share/misc/magic 并找到了 pcap-ng 条目:
#
# "pcap-ng" capture files.
# http://www.winpcap.org/ntar/draft/PCAP-DumpFileFormat.html
# Pcap-ng files can contain multiple sections. Printing the endianness,
# snaplen, or other information from the first SHB may be misleading.
#
0 ubelong 0x0a0d0d0a
>8 ubelong 0x1a2b3c4d pcap-ng capture file
>>12 beshort x - version %d
>>14 beshort x \b.%d
0 ulelong 0x0a0d0d0a
>8 ulelong 0x1a2b3c4d pcap-ng capture file
>>12 leshort x - version %d
>>14 leshort x \b.%d
这让我看到了PCAP Dump File Format (请参阅节标题 block 描述)。
pcap 文件的 hexdump 显示:
0000000 0d0a 0a0d 0070 0000 3c4d 1a2b 0001 0000
0000010 ffff ffff ffff ffff 0003 0017 7246 6565
0000020 5342 2044 2e39 2d32 4552 454c 5341 2d45
这证实了:
- 这确实是下一代 pcap 文件 - 因为 block 头以 Block Type = 0x0A0D0D0A 开头(显示为 0d0a 0a0d),并且
- header 包含pcap-ng魔数(Magic Number)0x1a2b3c4d(显示为3c4d 1a2b)。
关于c# - SharpPcap 4.2 与 WinPcap 4.1.3 给出 "Unable to open offline adapter: bad dump file format",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26477301/