简体   繁体   English

如何从 pcap-ng 文件中的数据包数据中提取链路层数据?

[英]How can I extract Link-Layer data from Packet Data in pcap-ng file?

I have a pcap-ng file, and I'd like to extract the source/destination IPs.我有一个 pcap-ng 文件,我想提取源/目标 IP。

According to the winpcap dump file format , the data I'm looking for is in the Packet Data section of the enhanced packet block.根据winpcap 转储文件格式,我要查找的数据位于增强数据包块的数据包数据部分。

I've been using this library in C# to parse through the pcap-ng file.我一直在 C# 中使用这个库来解析 pcap-ng 文件。 And while I've been able to successfully get out the Enhanced Packet Block, I'm really not sure how to get into it.虽然我已经能够成功地退出增强型数据包块,但我真的不确定如何进入它。

The current Enhanced Packet Block Packet Data comes out as a byte array, using the following method.目前的Enhanced Packet Block Packet Data是字节数组出来的,使用方法如下。

private static void extractEnhancedPacketBlock()
{
    var myFile = "\\path\\to\\my.pcapng"

    using (StreamWriter file = new StreamWriter(myFile))
    {
        foreach (var enhancedPacketBlock in reader.EnhancedPacketBlocks)
        {
            byte[] packetData = enhancedPacketBlock.Data;

            Console.WriteLine(BitConverter.ToString(packetData));
        }
    }
}

Which outputs what you would expect, similar to the following:哪个输出你所期望的,类似于以下内容:

79-2C-C8-80-A8-65-00-00-BC-C4-2F-65-09-00-42-00-01-5E...etc

A good answer to this could be a few different things like, guidance on where to look to learn more about what I need to do next.对此的一个很好的回答可能是一些不同的事情,比如关于去哪里寻找更多关于我下一步需要做什么的指导。 A library that already does that that I could use (I've tried a lot of libraries, and none of them seem to go this deep).一个已经做到了我可以使用的图书馆(我已经尝试了很多图书馆,但似乎没有一个能深入到这个层面)。 Or if you already have some code that does this, that would be awesome.或者,如果您已经有一些代码可以执行此操作,那就太棒了。 I'm also open to moving to Python if necessary.如有必要,我也愿意转向 Python。


Additional info.附加信息。

I know that I can parse the source IP and destination IP out of the Enhanced Packet Blocks, and I know that it will require a hexadecimal to IP conversion, but I do not know where the IP Hex exists in the Enhanced Packet Blocks.我知道我可以从增强型数据包块中解析源 IP 和目标 IP,并且我知道它需要十六进制到 IP 的转换,但我不知道增强型数据包块中 IP Hex 的位置。 I know it's not in the same place every time, but I need to know how to calculate this.我知道它每次都不在同一个地方,但我需要知道如何计算它。

Use https://github.com/chmorgan/packetnet for parsing the packet data使用https://github.com/chmorgan/packetnet解析包数据

Example:例子:

var packet = Packet.ParsePacket(LinkLayers.Ethernet, enhancedPacketBlock.Data);
var ip = packet.Extract<IPPacket>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM