This repository has been archived by the owner on May 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDumpManager.cs
213 lines (187 loc) · 7.42 KB
/
DumpManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using Advanced_Combat_Tracker;
using PacketAnalyzer.Network;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PacketAnalyzer
{
public static class DumpManager
{
const int maxPacketSize = 65535;
static string dumpDir = Path.Combine();
static Thread pcapLogger = null;
// Global Header | Packet Header | Packet Data | Packet Header | Packet Data | Packet Header | Packet Data | ...
/**
* @url: https://wiki.wireshark.org/Development/LibpcapFileFormat
*/
[StructLayout(LayoutKind.Sequential)]
struct PcapGlobalHeader
{
public PcapGlobalHeader(uint network)
{
// 147 is valid for private use, see http://www.tcpdump.org/linktypes.html
magic_number = 0xff14ff14;
version_major = 2;
version_minor = 4;
thiszone = 0;
sigfigs = 0;
snaplen = maxPacketSize;
this.network = network;
}
/* magic number */
public uint magic_number;
/* major version number */
public ushort version_major;
/* minor version number */
public ushort version_minor;
/* GMT to local correction */
public int thiszone;
/* accuracy of timestamps */
public uint sigfigs;
/* max length of captured packets, in octets */
public uint snaplen;
/* data link type */
public uint network;
}
[StructLayout(LayoutKind.Sequential)]
struct PcapPacketHeader
{
public PcapPacketHeader(uint incl_len, uint orig_len)
{
var now = DateTime.Now;
ts_sec = (uint)(now.EpochMillis() / 1000);
ts_usec = (uint)((now.Ticks / 10) % 1e6);
this.incl_len = incl_len;
this.orig_len = orig_len;
}
/* timestamp seconds */
public uint ts_sec;
/* timestamp microseconds */
public uint ts_usec;
/* number of octets of packet saved in file */
public uint incl_len;
/* actual length of packet */
public uint orig_len;
}
static volatile BlockingCollection<byte[]> pcapQueue = new BlockingCollection<byte[]>();
static void PcapLogger()
{
string dumpPath = Path.Combine(ActGlobals.oFormActMain.AppDataFolder.FullName, "PacketDump", DateTime.UtcNow.EpochMillis() + ".pcap");
int flushCount = 0;
try
{
using (FileStream fs = File.OpenWrite(dumpPath))
{
var globalHeader = Network.Util.StructureToByteArray(new PcapGlobalHeader(147));
fs.Write(globalHeader, 0, globalHeader.Length);
foreach (byte[] buffer in pcapQueue.GetConsumingEnumerable())
{
try
{
int dumpLength = buffer.Length > maxPacketSize ? maxPacketSize : buffer.Length;
var packetHeader = Network.Util.StructureToByteArray(new PcapPacketHeader((uint)dumpLength, (uint)buffer.Length));
fs.Write(packetHeader, 0, packetHeader.Length);
fs.Write(buffer, 0, dumpLength);
if (++flushCount > 15)
{
flushCount = 0;
fs.Flush(true);
}
}
catch (Exception e)
{
MessageBox.Show(string.Format("[E][PcapLogger: {0}]{1}\r\n{2}", e.GetType(), e.Message, e.StackTrace));
}
}
}
}
catch (Exception e)
{
MessageBox.Show(string.Format("[E][Failed starting PcapLogger: {0}]{1}\r\n{2}", e.GetType(), e.Message, e.StackTrace));
}
pcapLogger = null;
}
public static void FlushPcap()
{
pcapQueue.CompleteAdding();
pcapQueue = new BlockingCollection<byte[]>();
}
public static void Pcap(byte[] buffer)
{
if (pcapLogger == null)
{
pcapLogger = new Thread(PcapLogger);
pcapLogger.Start();
}
pcapQueue.Add(buffer);
}
public async static Task LoadPcap(string path, Action<byte[], DateTime> callback, IProgress<float> progress = null)
{
byte[] headerBuffer = new byte[16];
int bytesRead;
using (FileStream fs = File.OpenRead(path))
{
// Skip global header
fs.Seek(24, SeekOrigin.Begin);
while ((bytesRead = await fs.ReadAsync(headerBuffer, 0, 16)) == 16)
{
PacketParser.ParsePacket(headerBuffer, 0, out PcapPacketHeader header);
if (header.incl_len > maxPacketSize) return;
byte[] message = new byte[header.incl_len];
bytesRead = await fs.ReadAsync(message, 0, (int)header.incl_len);
if (bytesRead != header.incl_len) return;
callback(message, DateTimeOffset.FromUnixTimeSeconds(header.ts_sec).DateTime.AddTicks(header.ts_usec * 10));
if (progress != null)
{
progress.Report((float)fs.Position * 100 / fs.Length);
}
}
}
}
public static string Dump(Dictionary<string, string> param, byte[] buffer, int offset)
{
StringBuilder sb = new StringBuilder();
foreach (var item in param)
{
sb.Append(item.Key);
sb.Append(": ");
sb.Append(item.Value);
sb.AppendLine();
}
if (offset >= 0)
{
string hexDump = Util.BuildHexDump(buffer, offset);
sb.Append(hexDump);
}
return sb.ToString();
}
static async Task WriteTextAsync(string filePath, string text)
{
byte[] encodedText = Encoding.UTF8.GetBytes(text);
using (FileStream sourceStream = new FileStream(filePath,
FileMode.Append, FileAccess.Write, FileShare.None,
bufferSize: 4096, useAsync: true))
{
await sourceStream.WriteAsync(encodedText, 0, encodedText.Length);
};
}
public static Task Write(Dictionary<string, string> param, string content)
{
string dumpPath = Path.Combine(ActGlobals.oFormActMain.AppDataFolder.FullName, "PacketDump", param["ID"] + ".txt");
return WriteTextAsync(dumpPath, content);
}
public static string Write(Dictionary<string, string> param, byte[] buffer, int offset)
{
string dump = Dump(param, buffer, offset);
Write(param, dump);
return dump;
}
}
}