首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >.NET编写PCAP文件

.NET编写PCAP文件
EN

Stack Overflow用户
提问于 2012-06-21 17:25:20
回答 2查看 2.2K关注 0票数 3

全,

我花了相当多的时间研究各种PCAP库,在我致力于编写PCAP编写器之前,我想描述一下我的场景并征求意见。

我有一个客户要求我提供一个读取pcap文件并将数据包写入他们选择的数据库的服务。然后,客户端可以查询数据库(datetime range),结果最终应该是一个包含匹配该范围标准的包的pcap文件。

到目前为止,我在这些库中发现,pcap的“转储”,即编写,似乎只有在与特定的捕获设备关联时才可用。对于我的场景,情况并非如此。

我正在使用PCAP.NET读取原始的pcap文件并提取数据包。我将数据包存储到数据库中,然后可以从数据库中读取数据并重新创建数据包,但我没有找到将查询结果写入pcap文件的方法。

最简单的情况是,考虑数据包类型列表的数据结构(对于实际写入堆栈溢出来说,我不知道如何写带有尖括号的T列表,而不是过滤)--是否有可用的库支持将该结构写入pcap?

鉴于这似乎不是一个常见的场景,我想知道整个场景的有效性。我还应该指出的是,我总共使用了两天的PCAP数据,这应该是一个概念应用程序的证明,因此我完全有可能遗漏了一段使这件事微不足道的知识。

感谢您的宝贵时间和考虑,并提前道歉,如果我尝试使用Google,甚至更多的时间使用Stack Overflow搜索忽略了明显的事实。

克里斯

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-22 14:33:09

我相信Pcap.Net的静态方法PacketDumpFile.Dump()恰好满足了您的需求。

票数 2
EN

Stack Overflow用户

发布于 2018-10-09 07:07:54

这是我用C#编写的一个简单工具,用于将ETL语言转换为PCAP文件,这是一个如何编写PCAP文件的示例。这将写入链路层报头类型为Ethernet的文件。其他类型请参考http://www.tcpdump.org/linktypes.html

此处为https://github.com/chentiangemalc/EtlToCap的Visual Studio解决方案

代码语言:javascript
复制
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;
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11135215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档