-
Notifications
You must be signed in to change notification settings - Fork 10
/
NdisCapPacket.cpp
423 lines (365 loc) · 9.73 KB
/
NdisCapPacket.cpp
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
The BSD 3-Clause License
Copyright (c) 2016 Egtra All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of {{ project }} nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "stdafx.h"
#include <iphlpapi.h>
#include <evntrace.h>
#include <evntcons.h>
#include "EtwCommon.h"
#pragma comment(lib, "iphlpapi.lib")
using namespace std::literals;
class ComInitializer
{
public:
ComInitializer()
: m_hr(CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE))
{
assert(IsInitialized());
}
bool IsInitialized() const
{
return SUCCEEDED(m_hr) || m_hr == RPC_E_CHANGED_MODE;
}
~ComInitializer()
{
if (m_hr == S_OK)
{
CoUninitialize();
}
}
private:
const HRESULT m_hr;
ComInitializer(const ComInitializer&) = delete;
ComInitializer& operator=(const ComInitializer&) = delete;
};
PCHAR PacketGetVersion()
{
static char version[] = "NdisCapPacket 0.1";
return version;
}
PCHAR PacketGetDriverVersion()
{
static char driverVersion[] = "NdisCap";
return driverVersion;
}
BOOLEAN PacketSetMinToCopy(LPADAPTER AdapterObject, int nbytes)
{
//assert(false);
return TRUE;
}
BOOLEAN PacketSetNumWrites(LPADAPTER AdapterObject, int nwrites)
{
assert(false);
return FALSE;
}
BOOLEAN PacketSetMode(LPADAPTER AdapterObject, int mode)
{
assert(false);
return TRUE;
}
BOOLEAN PacketSetReadTimeout(LPADAPTER AdapterObject, int timeout)
{
//assert(false);
return TRUE;
}
BOOLEAN PacketSetBpf(LPADAPTER AdapterObject, bpf_program *fp)
{
//assert(false);
return TRUE;
}
BOOLEAN PacketSetLoopbackBehavior(LPADAPTER AdapterObject, UINT LoopbackBehavior)
{
assert(false);
return TRUE;
}
INT PacketSetSnapLen(LPADAPTER AdapterObject, int snaplen)
{
assert(false);
return 0;
}
BOOLEAN PacketGetStats(_In_ LPADAPTER AdapterObject, _Out_ bpf_stat* s)
{
if (s == nullptr)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
*s = {};
return TRUE;
}
BOOLEAN PacketGetStatsEx(LPADAPTER AdapterObject, bpf_stat *s)
{
assert(false);
return FALSE;
}
BOOLEAN PacketSetBuff(LPADAPTER AdapterObject, int dim)
{
//assert(false);
return TRUE;
}
BOOLEAN PacketGetNetType(_In_ LPADAPTER AdapterObject, _Out_ NetType* type)
{
type->LinkType = 0; // NdisMedium802_3
type->LinkSpeed = 100 * 1000 * 1000;
return TRUE;
}
LPADAPTER PacketOpenAdapter(_In_ PCHAR AdapterName)
{
if (AdapterName == nullptr)
{
SetLastError(ERROR_INVALID_PARAMETER);
return nullptr;
}
if (std::strlen(AdapterName) + 1 > sizeof EventTraceData::Name)
{
SetLastError(ERROR_INVALID_PARAMETER);
return nullptr;
}
try
{
auto data = std::make_unique<EventTraceData>();
strcpy_s(data->Name, AdapterName);
if (!data->ConsumerThread.joinable())
{
ComInitializer comInit;
auto resultStartCapture = StartCapture();
if (std::get<0>(resultStartCapture) != ERROR_SUCCESS)
{
SetLastError(std::get<0>(resultStartCapture));
return nullptr;
}
data->TraceHandleController = std::get<1>(resultStartCapture);
EVENT_TRACE_LOGFILE etl{};
#if 1
etl.LoggerName = const_cast<PWSTR>(SessionName);
etl.ProcessTraceMode = PROCESS_TRACE_MODE_EVENT_RECORD | PROCESS_TRACE_MODE_REAL_TIME;
#else
etl.LogFileName = LR"(T:\a.etl)";
etl.ProcessTraceMode = PROCESS_TRACE_MODE_EVENT_RECORD;
#endif
etl.EventRecordCallback = EventRecordCallback;
etl.Context = data.get();
auto hTrace = OpenTrace(&etl);
// See “Return Value” section: https://msdn.microsoft.com/en-us/library/windows/desktop/aa364089(v=vs.85).aspx
#ifdef _WIN64
ATLENSURE_RETURN_VAL(hTrace != 0xFFFFFFFFFFFFFFFF, nullptr);
#else
ATLENSURE_RETURN_VAL(hTrace != 0x00000000FFFFFFFF, nullptr);
#endif
data->TraceHandleConsumer = hTrace;
data->ConsumerThread = std::thread([hTrace]() mutable
{
ProcessTrace(&hTrace, 1, nullptr, nullptr);
});
}
return data.release();
}
catch (const std::system_error& e)
{
if (e.code().category() == std::system_category())
{
SetLastError(static_cast<DWORD>(e.code().value()));
}
else
{
SetLastError(E_FAIL);
}
}
catch (const std::bad_alloc&)
{
SetLastError(ERROR_OUTOFMEMORY);
}
catch (...)
{
SetLastError(E_FAIL);
}
return nullptr;
}
BOOLEAN PacketSendPacket(LPADAPTER AdapterObject, LPPACKET pPacket, BOOLEAN Sync)
{
assert(false);
return FALSE;
}
INT PacketSendPackets(LPADAPTER AdapterObject, PVOID PacketBuff, ULONG Size, BOOLEAN Sync)
{
assert(false);
return FALSE;
}
LPPACKET PacketAllocatePacket()
{
return new(std::nothrow) PACKET{};
}
VOID PacketInitPacket(_Inout_ LPPACKET lpPacket, _In_ PVOID Buffer, _In_ UINT Length)
{
lpPacket->Buffer = Buffer;
lpPacket->Length = Length;
lpPacket->ulBytesReceived = 0;
lpPacket->bIoComplete = FALSE;
}
VOID PacketFreePacket(_In_opt_ LPPACKET lpPacket)
{
delete lpPacket;
}
BOOLEAN PacketReceivePacket(_In_ LPADAPTER AdapterObject, _In_ LPPACKET lpPacket, _In_ BOOLEAN Sync)
{
if (AdapterObject == nullptr || lpPacket == nullptr)
{
return FALSE;
}
auto data = static_cast<EventTraceData*>(AdapterObject);
std::vector<std::uint8_t> packet;
while (!data->Packet.try_pop(packet))
{
lpPacket->ulBytesReceived = 0;
return TRUE;
}
auto p = static_cast<std::uint8_t*>(lpPacket->Buffer);
auto header = reinterpret_cast<bpf_hdr*>(p);
header->bh_tstamp.tv_sec = time(nullptr);
header->bh_tstamp.tv_usec = 0;
header->bh_caplen = packet.size();
header->bh_datalen = packet.size();
header->bh_hdrlen = sizeof(bpf_hdr);
memcpy(p + sizeof(bpf_hdr), packet.data(), packet.size());
lpPacket->ulBytesReceived = packet.size();
return TRUE;
}
BOOLEAN PacketSetHwFilter(LPADAPTER AdapterObject, ULONG Filter)
{
//assert(false);
return TRUE;
}
BOOLEAN PacketGetAdapterNames(_Out_opt_ PSTR pStr, _Inout_ PULONG BufferSize)
{
if (BufferSize == nullptr)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
static constexpr char data[] = "NdisCapPacket\0\0NdisCapPacket\0";
if (pStr == nullptr || *BufferSize < sizeof data)
{
SetLastError(ERROR_INSUFFICIENT_BUFFER);
*BufferSize = sizeof data;
return FALSE;
}
memcpy(pStr, data, sizeof data);
*BufferSize = sizeof data;
return TRUE;
}
BOOLEAN PacketGetNetInfoEx(_In_ PCHAR AdapterName, _Out_ npf_if_addr* buffer, _Inout_ PLONG NEntries)
{
if (AdapterName == nullptr || buffer == nullptr || NEntries == nullptr)
{
return FALSE;
}
constexpr DWORD Flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME;
ULONG addressesBufferSize;
auto resultSize = GetAdaptersAddresses(AF_UNSPEC, Flags, nullptr, nullptr, &addressesBufferSize);
if (resultSize != ERROR_BUFFER_OVERFLOW)
{
return FALSE;
}
std::unique_ptr<std::uint8_t[]> addressesBuffer(new(std::nothrow) std::uint8_t[addressesBufferSize]);
if (addressesBuffer == nullptr)
{
return FALSE;
}
auto addresses = reinterpret_cast<IP_ADAPTER_ADDRESSES*>(addressesBuffer.get());
auto resultAddress = GetAdaptersAddresses(AF_UNSPEC, Flags, nullptr, addresses, &addressesBufferSize);
if (resultAddress != ERROR_SUCCESS)
{
return FALSE;
}
ULONG n = *NEntries;
ULONG i = 0;
for (; i < n && addresses != nullptr; ++i, addresses = addresses->Next)
{
auto a = addresses->FirstUnicastAddress;
memcpy(&buffer[i].IPAddress, a->Address.lpSockaddr, a->Address.iSockaddrLength);
buffer[i].SubnetMask = {};
buffer[i].Broadcast = {};
}
*NEntries = i;
return TRUE;
}
BOOLEAN PacketRequest(LPADAPTER AdapterObject, BOOLEAN Set, PPACKET_OID_DATA OidData)
{
assert(false);
return FALSE;
}
HANDLE PacketGetReadEvent(LPADAPTER AdapterObject)
{
assert(false);
return nullptr;
}
BOOLEAN PacketSetDumpName(LPADAPTER AdapterObject, void *name, int len)
{
assert(false);
return FALSE;
}
BOOLEAN PacketSetDumpLimits(LPADAPTER AdapterObject, UINT maxfilesize, UINT maxnpacks)
{
assert(false);
return FALSE;
}
BOOLEAN PacketIsDumpEnded(LPADAPTER AdapterObject, BOOLEAN sync)
{
assert(false);
return FALSE;
}
BOOL PacketStopDriver()
{
assert(false);
return TRUE;
}
void PacketCloseAdapter(_In_ LPADAPTER lpAdapter)
{
if (lpAdapter == nullptr)
{
return;
}
auto data = static_cast<EventTraceData*>(lpAdapter);
ComInitializer comInit;
StopCapture(data->TraceHandleController);
data->ConsumerThread.join();
CloseTrace(data->TraceHandleConsumer);
delete data;
}
BOOLEAN PacketStartOem(PCHAR errorString, UINT errorStringLength)
{
assert(false);
return FALSE;
}
BOOLEAN PacketStartOemEx(PCHAR errorString, UINT errorStringLength, ULONG flags)
{
assert(false);
return FALSE;
}
PAirpcapHandle PacketGetAirPcapHandle(LPADAPTER AdapterObject)
{
assert(false);
return nullptr;
}