【问题标题】:PCAP Library Capabilities (writing new PCAP files)PCAP 库功能(编写新的 PCAP 文件)
【发布时间】:2012-06-24 09:37:37
【问题描述】:

我被卡住了。

我使用 PCAP.NET 读取 .PCAP 文件并将数据包写入数据库。现在我可以查询数据库并取回与约束匹配的数据包。我需要一种将结果写入新 .PCAP 文件的方法。

问题在于,据我所知,生成新 PCAP 文件的唯一方法是通过 DumpFile,它只能通过本身绑定到 PacketDevice 的 PacketCommunicator 进行初始化。

可以在这里看到一个例子:http://pcapdotnet.codeplex.com/wikipage?title=Pcap.Net%20Tutorial%20-%20Handling%20offline%20dump%20files&referringTitle=Pcap.Net%20User%20Guide

很好,但在这种情况下我没有设备。

我是否应该为此推出自己的 PCAP 编写器?

我是否遗漏了一些明显的东西?

如何将这些数据包放入新的 PCAP 文件中?

我确信我忽略了一些简单的事情……PCAP 对我来说是一个新领域,我感觉很不舒服。工作中的 Unix 人员表示,winpcap 和 pcap.net 所基于的 libpcap 提供了直接写入 pcap 文件的能力。库中没有公开功能吗?

非常感谢您的建议。

谢谢,

克里斯

附:这是对我在这里提出的原始问题的修订:.NET writing PCAP files

【问题讨论】:

    标签: c# pcap pcap.net


    【解决方案1】:

    the Pcap.Net User Guide 中,"Handling offline dump files" page 有一个使用PacketDumpFile 类写出转储文件的示例。我没有看到任何 Pcap.Net 参考手册;回答this question,用户阅读Pcap.Net源码。

    【讨论】:

      【解决方案2】:

      您可以创建 pcap 标头和格式,规范很简单,您不需要 pcap.h 之外的外部库。

      struct pcap_file_header {
          bpf_u_int32 magic;
          u_short version_major;
          u_short version_minor;
          bpf_int32 thiszone;     /* gmt to local correction */
          bpf_u_int32 sigfigs;    /* accuracy of timestamps */
          bpf_u_int32 snaplen;    /* max length saved portion of each pkt */
          bpf_u_int32 linktype;   /* data link type (LINKTYPE_*) */
      };
      

      所以你的文件先为pcap文件创建一个header,例如:

          struct pcap_file_header pheader;
      
          pheader.magic = 0xA1B2C3D4; // MAGIC NUMBER FOR TCPDUMP
          pheader.version_major = PCAP_VERSION_MAJOR;
          pheader.version_minor = PCAP_VERSION_MINOR;
          pheader.thiszone = 0;
          pheader.sigfigs = 0;
          pheader.snaplen = 1500;
          pheader.linktype = 1;
      

      对于编写每个数据包,您需要一个如下结构:

      typedef struct {
          int32_t t0;
          int32_t t1;
          int32_t len;
          int32_t caplen;
      } pcap_header_writeable;
      

      所以你会在文件上写:

          pcap_header_writeable header;
      
          header.t0 = 0;
          header.t1 = 0;
          header.len = length;
          header.caplen = length;
      
          // write the header on the file 
          // write the packet on the file after the header
      

      【讨论】:

        【解决方案3】:

        库中没有公开功能吗?

        哪个库?

        它在 libpcap/WinPcap 中公开,但打开文件进行输出有点尴尬 - 您需要 pcap_t 用于捕获设备,pcap_t 用于捕获文件,如果您正在编写的数据包不是来自实时捕获或现有捕获文件,则为您正在编写的文件的链路层标头类型和快照长度的虚拟pcap_t

        我找不到 Pcap.NET 的任何参考文档,只有教程文档,但似乎没有任何东西可以让您打开虚拟句柄 - 您可以打开捕获设备或离线捕获文件,但是我没有看到任何关于创建一个您无法从中读取数据包但您可以在打开捕获文件进行写入时使用的虚拟句柄的任何内容 - 所以 libpcap/WinPcap 中可用的所有功能都是 NOT据我所知,暴露在 Pcap.NET 中。

        【讨论】:

          猜你喜欢
          • 2012-06-23
          • 2011-07-11
          • 2013-04-15
          • 2013-08-02
          • 2015-01-14
          • 1970-01-01
          • 2020-08-27
          • 2012-06-29
          • 2021-10-10
          相关资源
          最近更新 更多