forked from pulpocaminante/PPL-0day
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWin32Interface.hpp
executable file
·258 lines (218 loc) · 8.28 KB
/
Win32Interface.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
#pragma once
#include "WMIConnection.hpp"
class Win32Interface
{
public:
BOOL IReady;
BOOL bInitialized;
Win32Interface(COMWrapper& com_wrapper, WMIConnection& wmi_connection) :
COM(com_wrapper), WMI(wmi_connection),
IReady(FALSE), bInitialized(FALSE)
{
if (WMI.ConnectToNamespace("RVOM2OITC\\", 0) && WMI.IReady && COM.IReady)
this->IReady = TRUE;
}
UINT32 Enumerate(
)
{
}
UINT32 Create(
_In_ LPCWSTR CommandLine,
_In_opt_ LPCWSTR CurrentDirectory,
_In_opt_ LPVOID ProcessStartupInformation,
_Out_ UINT32* ProcessId,
_Out_ UINT32* ReturnValue)
{
if (!IReady || !WMI.IReady) return 0;
if (wcscmp(this->WMI.currentNamespace, L"RVOM2OITC\\") != 0)
WMI.ConnectToNamespace("RVOM2OITC\\", 0);
if (!WMI.bConnected)
{
ILog("Failed to connect to namespace\n");
return E_FAIL; // Something is very wrong
}
// Object declarations
IWbemClassObject* pClass = NULL; // Win32_Process
IWbemClassObject* pInParamsDefinition = NULL; // Win32_Process.Create
IWbemClassObject* pOutParamsDefinition = NULL; // Win32_Process.Create
IWbemClassObject* pInParams = NULL; // Win32_Process.Create
IWbemClassObject* pOutParams = NULL; // Win32_Process.Create
IWbemClassObject* pClassInstance = NULL; // Win32_Process
IWbemCallResult* pCallResult = NULL;
IWbemClassObject* pStartupObject = NULL; // Win32_ProcessStartup
IWbemClassObject* pStartupInstance = NULL; // Win32_ProcessStartup
IWbemClassObject* pParamsInstance = NULL; // Win32_ProcessStartup
// Method declarations
BOOL bInitialized = FALSE;
HRESULT hres;
WCHAR szSystemDir[MAX_PATH];
// If no working directory is passed, set it to system32
if (CurrentDirectory == NULL)
{
// Get system directory
GetSystemDirectory(szSystemDir, MAX_PATH);
CurrentDirectory = szSystemDir;
}
LPCWSTR lpwMethodName = L"Create";
LPCWSTR lpwClassName = L"Win32_Process";
// Initialize the COM
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
ILog("Failed to initialize COM library. Error code = 0x%lx\n", hres);
return hres; // Program has failed.
}
else bInitialized = TRUE;
//Setup the method call
BSTR MethodName = SysAllocString(lpwMethodName);
BSTR ClassName = SysAllocString(lpwClassName);
hres = WMI.pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);
if (FAILED(hres))
{
ILog("Failed to get class object. Error code = 0x%lx\n", hres);
return hres;
}
// Get the class object
hres = pClass->GetMethod(lpwMethodName, 0,
&pInParamsDefinition, NULL);
hres = pInParamsDefinition->SpawnInstance(0, &pClassInstance);
if (FAILED(hres))
{
ILog("Failed to get class. Error code = 0x%lx\n", hres);
return hres;
}
hres = WMI.pSvc->GetObject(_bstr_t(L"Win32_ProcessStartup"), 0, NULL, &pStartupObject, NULL);
if (FAILED(hres))
{
ILog("Failed to get Win32_ProcessStartup object. Error code = 0x%lx\n", hres);
return hres;
}
hres = pStartupObject->SpawnInstance(0, &pStartupInstance);
if (FAILED(hres))
{
ILog("Failed to spawn Win32_ProcessStartup instance. Error code = 0x%lx\n", hres);
return hres;
}
//Create the values for the in parameters
VARIANT varParams;
VariantInit(&varParams);
varParams.vt = VT_I2;
varParams.intVal = SW_SHOW;
hres = pStartupInstance->Put(_bstr_t(L"ShowWindow"), 0, &varParams, 0);
if (FAILED(hres))
{
ILog("Failed to set ShowWindow property. Error code = 0x%lx\n", hres);
return hres;
}
hres = pClass->GetMethod(_bstr_t(lpwMethodName), 0, &pInParamsDefinition, NULL);
if (FAILED(hres))
{
ILog("Failed to get method definition. Error code = 0x%lx\n", hres);
return hres;
}
hres = pInParamsDefinition->SpawnInstance(0, &pParamsInstance);
if (FAILED(hres))
{
ILog("Failed to spawn instance. Error code = 0x%lx\n", hres);
return hres;
}
// Construct the variant for CurrentDirectory
VARIANT varCurrentDirectory;
VariantInit(&varCurrentDirectory);
varCurrentDirectory.vt = VT_BSTR;
varCurrentDirectory.bstrVal = SysAllocString(CurrentDirectory);
hres = pParamsInstance->Put(_bstr_t(L"CurrentDirectory"), 0, &varCurrentDirectory, 0);
if (FAILED(hres))
{
ILog("Failed to set CurrentDirectory property. Error code = 0x%lx\n", hres);
return hres;
}
// Construct VARIANT for command line
VARIANT varCommand;
VariantInit(&varCommand);
varCommand.vt = VT_BSTR;
varCommand.bstrVal = SysAllocString(CommandLine);
hres = pParamsInstance->Put(_bstr_t(L"CommandLine"), 0, &varCommand, 0);
if (FAILED(hres))
{
ILog("Failed to set CommandLine property. Error code = 0x%lx\n", hres);
return hres;
}
// Pass Win32_ProcessStartup object to the method
// But it has to be a VARIANT
//
VARIANT varStartup;
VariantInit(&varStartup);
varStartup.vt = VT_UNKNOWN;
varStartup.punkVal = pStartupInstance;
hres = pParamsInstance->Put(_bstr_t(L"ProcessStartupInformation"), 0, &varStartup, 0);
if (FAILED(hres))
{
ILog("Failed to set ProcessStartupInformation property. Error code = 0x%lx\n", hres);
return hres;
}
WMI.InvokeMethod(ClassName, MethodName, 0, NULL, pParamsInstance, &pOutParams, &pCallResult);
// Get the return value
VARIANT vtProp;
VariantInit(&vtProp);
hres = pOutParams->Get(_bstr_t(L"ReturnValue"), 0, &vtProp, 0, 0);
if (FAILED(hres))
{
ILog("Failed to get return value. Error code = 0x%lx\n", hres);
return hres;
}
// Get the process ID
VARIANT vtProp2;
VariantInit(&vtProp2);
hres = pOutParams->Get(_bstr_t(L"ProcessId"), 0, &vtProp2, 0, 0);
if (FAILED(hres))
{
ILog("Failed to get process ID. Error code = 0x%lx\n", hres);
return hres;
}
// Print the return value and process id
ILog("Return Value = %d Process ID = %d \n", vtProp.intVal, vtProp2.intVal);
// Store the return values for process ID & return value
*ProcessId = vtProp2.intVal;
*ReturnValue = vtProp.intVal;
// Cleanup
if (pStartupInstance)
pStartupInstance->Release();
if (pParamsInstance)
pParamsInstance->Release();
if (pClassInstance)
pClassInstance->Release();
if (pInParamsDefinition)
pInParamsDefinition->Release();
if (pStartupObject)
pStartupObject->Release();
if (pClass)
pClass->Release();
if (pOutParams)
pOutParams->Release();
if (pInParams)
pInParams->Release();
varStartup.punkVal = NULL;
hres = VariantClear(&varStartup);
hres = VariantClear(&vtProp);
hres = VariantClear(&vtProp2);
hres = VariantClear(&varCommand);
hres = VariantClear(&varCurrentDirectory);
hres = VariantClear(&varParams);
SysFreeString(ClassName);
SysFreeString(MethodName);
if (bInitialized)
CoUninitialize();
}
UINT32 TERMINATE(_In_ UINT32 Reason)
{
return 0;
}
UINT32 SETPRIORITY(_In_ UINT32 Priority)
{
return 0;
}
private:
WMIConnection& WMI;
COMWrapper& COM;
};