-
Notifications
You must be signed in to change notification settings - Fork 10
/
EtwController.cpp
266 lines (223 loc) · 8.3 KB
/
EtwController.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
/*
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 <initguid.h>
#include <winevt.h>
#include <netcfgx.h>
#include <evntrace.h>
#include <wrl/client.h>
#include "EtwCommon.h"
#pragma comment(lib, "wevtapi.lib")
using namespace std::literals;
struct ServiceHandleDeleter
{
typedef SC_HANDLE pointer;
void operator()(_In_opt_ SC_HANDLE hsc) const noexcept
{
if (hsc != nullptr)
{
CloseServiceHandle(hsc);
}
}
};
using service_handle = std::unique_ptr<SC_HANDLE, ServiceHandleDeleter>;
struct HKeyDeleter
{
typedef HKEY pointer;
void operator()(_In_opt_ HKEY hk) const noexcept
{
if (hk != nullptr)
{
RegCloseKey(hk);
}
}
};
using hkey = std::unique_ptr<HKEY, HKeyDeleter>;
union EventTracePropertiesWithBuffer
{
// buffer size: EVENT_TRACE_PROPERTIES + logFileName + sessionName
char buffer[sizeof(EVENT_TRACE_PROPERTIES) + (1024 + 1024) * sizeof(WCHAR)];
EVENT_TRACE_PROPERTIES etp;
};
// {2ED6006E-4729-4609-B423-3EE7BCD678EF}
static constexpr GUID GUID_Microsoft_Windows_NDIS_PacketCapture = { 0x2ED6006E, 0x4729, 0x4609,{ 0xB4, 0x23, 0x3E, 0xE7, 0xBC, 0xD6, 0x78, 0xEF } };
static DWORD StartNdisCapService()
{
service_handle hscm(OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT));
ATLENSURE_RETURN_VAL(hscm != nullptr, GetLastError());
service_handle hsc(OpenService(hscm.get(), L"ndiscap", SERVICE_START));
ATLENSURE_RETURN_VAL(hsc != nullptr, GetLastError());
return StartService(hsc.get(), 0, nullptr);
}
static DWORD SetNdisCapParameters(bool enable)
{
ATL::CRegKey key;
auto lstatusCreate = key.Create(
HKEY_LOCAL_MACHINE,
LR"(System\CurrentControlSet\Services\NdisCap\Parameters)");
ATLENSURE_RETURN_VAL(lstatusCreate == ERROR_SUCCESS, lstatusCreate);
DWORD refCount;
auto lstatusQuery = key.QueryDWORDValue(L"RefCount", refCount);
ATLENSURE_RETURN_VAL(lstatusQuery == ERROR_SUCCESS, lstatusQuery);
if (enable)
{
++refCount;
auto lstatusSetRefCount = key.SetDWORDValue(L"RefCount", refCount);
ATLENSURE_RETURN_VAL(lstatusSetRefCount == ERROR_SUCCESS, lstatusSetRefCount);
}
else
{
if (refCount > 0)
{
--refCount;
auto lstatusSetRefCount = key.SetDWORDValue(L"RefCount", refCount);
ATLENSURE_RETURN_VAL(lstatusSetRefCount == ERROR_SUCCESS, lstatusSetRefCount);
}
}
if (enable)
{
auto lstatusSetCaptureMode = key.SetDWORDValue(L"CaptureMode", 0);
ATLENSURE_RETURN_VAL(lstatusSetCaptureMode == ERROR_SUCCESS, lstatusSetCaptureMode);
}
return ERROR_SUCCESS;
}
static DWORD EnableCapture(bool enable)
{
Microsoft::WRL::ComPtr<INetCfg> nc;
auto hrCreate = CoCreateInstance(CLSID_CNetCfg, nullptr, CLSCTX_SERVER, IID_PPV_ARGS(&nc));
ATLENSURE_RETURN_HR(SUCCEEDED(hrCreate), hrCreate);
Microsoft::WRL::ComPtr<INetCfgLock> lock;
auto hrQueryL = nc.As(&lock);
ATLENSURE_RETURN_HR(SUCCEEDED(hrQueryL), hrQueryL);
auto hrLock = lock->AcquireWriteLock(5000, L"NdisCapPacket", nullptr);
ATLENSURE_RETURN_HR(SUCCEEDED(hrLock), hrLock);
auto hrInit = nc->Initialize(nullptr);
ATLENSURE_RETURN_HR(SUCCEEDED(hrInit), hrInit);
Microsoft::WRL::ComPtr<INetCfgComponent> ndisCap;
auto hrFind = nc->FindComponent(L"ms_ndiscap", &ndisCap);
ATLENSURE_RETURN_HR(SUCCEEDED(hrFind), hrFind);
Microsoft::WRL::ComPtr<INetCfgComponentBindings> bindings;
auto hrQueryB = ndisCap.As(&bindings);
ATLENSURE_RETURN_HR(SUCCEEDED(hrQueryB), hrQueryB);
Microsoft::WRL::ComPtr<IEnumNetCfgBindingPath> enumBindingPath;
auto hrEnum = bindings->EnumBindingPaths(EBP_BELOW, &enumBindingPath);
ATLENSURE_RETURN_HR(SUCCEEDED(hrEnum), hrEnum);
for (;;)
{
ATL::CComPtr<INetCfgBindingPath> bindingPath;
ULONG fetched;
auto hrNext = enumBindingPath->Next(1, &bindingPath, &fetched);
ATLENSURE_RETURN_HR(SUCCEEDED(hrNext), hrNext);
if (hrNext != S_OK)
{
break;
}
auto hrEnable = bindingPath->Enable(static_cast<BOOL>(enable));
ATLENSURE_RETURN_HR(SUCCEEDED(hrEnable), hrEnable);
}
auto hrApply = nc->Apply();
ATLENSURE_RETURN_HR(SUCCEEDED(hrApply), hrApply);
ATL::CRegKey session;
auto lstatusCreate = session.Create(
HKEY_CURRENT_USER,
LR"(System\CurrentControlSet\Control\NetTrace\Session)",
nullptr,
REG_OPTION_NON_VOLATILE,
KEY_SET_VALUE);
ATLENSURE_RETURN_VAL(lstatusCreate == ERROR_SUCCESS, lstatusCreate);
auto lstatusSet = session.SetDWORDValue(L"CaptureEnabled", 1u);
ATLENSURE_RETURN_VAL(lstatusSet == ERROR_SUCCESS, lstatusSet);
auto hrReleaseLock = lock->ReleaseWriteLock();
ATLENSURE_RETURN_HR(SUCCEEDED(hrReleaseLock), hrReleaseLock);
auto hrUninit = nc->Uninitialize();
ATLENSURE_RETURN_HR(SUCCEEDED(hrUninit), hrUninit);
return S_OK;
}
static std::pair<DWORD, TRACEHANDLE> StartTrace()
{
TRACEHANDLE th;
EventTracePropertiesWithBuffer buffer{};
buffer.etp.Wnode.BufferSize = sizeof buffer;
buffer.etp.Wnode.ClientContext = 2; // Query perfomance counter
buffer.etp.Wnode.Flags = WNODE_FLAG_TRACED_GUID;
buffer.etp.BufferSize = 128;
buffer.etp.MaximumBuffers = 128;
buffer.etp.MaximumFileSize = 250;
buffer.etp.LogFileMode = EVENT_TRACE_REAL_TIME_MODE;
buffer.etp.LoggerNameOffset = sizeof buffer.etp;
auto resultStartTrace = StartTrace(&th, SessionName, &buffer.etp);
if (resultStartTrace == ERROR_ALREADY_EXISTS)
{
auto resultControlTrace = ControlTrace(0, SessionName, &buffer.etp, EVENT_TRACE_CONTROL_STOP);
ATLENSURE_RETURN_VAL(resultControlTrace == ERROR_SUCCESS, std::make_pair(resultControlTrace, TRACEHANDLE()));
resultStartTrace = StartTrace(&th, SessionName, &buffer.etp);
}
ATLENSURE_RETURN_VAL(resultStartTrace == ERROR_SUCCESS, std::make_pair(resultStartTrace, TRACEHANDLE()));
ENABLE_TRACE_PARAMETERS params{};
params.Version = ENABLE_TRACE_PARAMETERS_VERSION;
auto resultEnableTrace = EnableTraceEx2(th,
&GUID_Microsoft_Windows_NDIS_PacketCapture,
EVENT_CONTROL_CODE_ENABLE_PROVIDER,
TRACE_LEVEL_INFORMATION,
0,
0,
0,
¶ms);
ATLENSURE_RETURN_VAL(resultEnableTrace == ERROR_SUCCESS, std::make_pair(resultEnableTrace, TRACEHANDLE()));
return{ ERROR_SUCCESS, th };
}
std::pair<DWORD, TRACEHANDLE> StartCapture()
{
for (;;)
{
auto resultService = StartNdisCapService();
if (resultService == ERROR_SUCCESS)
{
break;
}
else if (resultService == ERROR_INVALID_FUNCTION)
{
Sleep(50);
continue;
}
return{ resultService, TRACEHANDLE() };
}
auto resultParameters = SetNdisCapParameters(true);
ATLENSURE_RETURN_VAL(resultParameters == ERROR_SUCCESS, std::make_pair(resultParameters, TRACEHANDLE()));
auto resultCapture = EnableCapture(true);
ATLENSURE_RETURN_VAL(resultCapture == ERROR_SUCCESS, std::make_pair(resultCapture, TRACEHANDLE()));
return StartTrace();
}
void StopCapture(_In_ TRACEHANDLE th)
{
EventTracePropertiesWithBuffer buffer{};
buffer.etp.Wnode.BufferSize = sizeof buffer;
buffer.etp.LoggerNameOffset = sizeof buffer.etp;
buffer.etp.LogFileNameOffset = sizeof buffer.etp + 1024 * sizeof (WCHAR);
ControlTrace(th, nullptr, &buffer.etp, EVENT_TRACE_CONTROL_STOP);
EnableCapture(false);
SetNdisCapParameters(false);
}