PCAP库功能(编写新的PCAP文件)

时间:2012-06-22 04:36:37

标签: c# pcap pcap.net

我被困住了。

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

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

这里可以看到一个例子: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文件的能力。功能是否未在库中公开?

建议非常感谢。

谢谢,

克里斯

P.S。这是对我在此处提出的原始问题的修订:.NET writing PCAP files

3 个答案:

答案 0 :(得分:0)

  

库中没有公开功能吗?

哪个库?

它暴露在libpcap / WinPcap中,但打开输出文件有点尴尬 - 你需要pcap_t用于捕获设备,pcap_t用于捕获文件,或< / em>一个虚拟pcap_t,用于表示您正在编写的数据包不是来自实时捕获或现有捕获文件的文件的链接层标题类型和快照长度。

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

答案 1 :(得分:0)

您可以创建一个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文件创建头,例如:

    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

答案 2 :(得分:0)

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