所有,
我花了一天的时间来查看各种PCAP库,在我开始编写PCAP编写器之前,我想描述一下我的场景并征求意见。
我有一个客户端要求我提供一个读取pcap文件并将数据包写入所选数据库的服务。然后,客户端可以查询数据库(日期时间范围),结果最终应该是包含与该范围标准匹配的数据包的pcap文件。
到目前为止,我在库中发现的是,与特定捕获设备相关联时,pcap的“转储”似乎只是可用的。对于我的场景,情况并非如此。
我正在使用PCAP.NET来读取原始的pcap文件并提取数据包。我将数据包存储到数据库中,然后我可以从数据库中读取数据并重新创建数据包,但我没有找到将查询结果写入pcap文件的方法。
最简单的情况,考虑一个Packet类型的List的数据结构(所以新的实际写入堆栈溢出,我不知道如何编写T的List,并且尖括号没有被过滤) - 做任何可用的库支持将该结构写入pcap?
鉴于这似乎不是一个常见的情况,我想知道整个方案的有效性。我还应该指出,我总共花了两天时间处理PCAP数据,这应该是一个概念验证应用程序,因此我完全有可能错过了使这一点变得微不足道的知识。
如果我在Google上的尝试以及Stack Overflow搜索的更多时间忽略了显而易见的事情,请提前感谢您宝贵的时间,考虑和道歉。
克里斯
我相信Pcap.Net的静态方法PacketDumpFile.Dump()可以为您提供所需的内容。
这是C#中的简单工具,我将ETL转换为PCAP文件,这是一个如何编写PCAP文件的例子。这将使用链路层标头类型的以太网写入文件。有关其他类型,请参阅http://www.tcpdump.org/linktypes.html。
Visual Studio解决方案在这里https://github.com/chentiangemalc/EtlToCap
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace chentiangemalc
{
public static class NetworkRoutines
{
public static long ConvertEtlToPcap(string source, string destination, UInt32 maxPacketSize)
{
int result = 0;
using (BinaryWriter writer = new BinaryWriter(File.Open(destination, FileMode.Create)))
{
UInt32 magic_number = 0xa1b2c3d4;
UInt16 version_major = 2;
UInt16 version_minor = 4;
Int32 thiszone = 0;
UInt32 sigfigs = 0;
UInt32 snaplen = maxPacketSize;
UInt32 network = 1; // LINKTYPE_ETHERNET
writer.Write(magic_number);
writer.Write(version_major);
writer.Write(version_minor);
writer.Write(thiszone);
writer.Write(sigfigs);
writer.Write(snaplen);
writer.Write(network);
long c = 0;
long t = 0;
using (var reader = new EventLogReader(source, PathType.FilePath))
{
EventRecord record;
while ((record = reader.ReadEvent()) != null)
{
c++;
t++;
if (c == 100000)
{
Console.WriteLine(String.Format("Processed {0} events",t));
c = 0;
}
using (record)
{
if (record.Id == 1001 && record.ProviderName == "Microsoft-Windows-NDIS-PacketCapture")
{
result++;
DateTime timeCreated = (DateTime)record.TimeCreated;
UInt32 ts_sec = (UInt32)((timeCreated.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
UInt32 ts_usec = (UInt32)(((timeCreated.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds) - ((UInt32)((timeCreated.Subtract(new DateTime(1970, 1, 1))).TotalSeconds * 1000))) * 1000;
UInt32 incl_len = (UInt32)record.Properties[2].Value;
if (incl_len > maxPacketSize)
{
Console.WriteLine(String.Format("Packet size of {0} exceeded max packet size {1}, packet ignored",incl_len,maxPacketSize));
}
UInt32 orig_len = incl_len;
writer.Write(ts_sec);
writer.Write(ts_usec);
writer.Write(incl_len);
writer.Write(orig_len);
writer.Write((byte[])record.Properties[3].Value);
}
}
}
}
}
return result;
}
}
}