-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgameDetection.cpp
More file actions
313 lines (245 loc) · 7.17 KB
/
Copy pathgameDetection.cpp
File metadata and controls
313 lines (245 loc) · 7.17 KB
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
#include "gameDetection.h"
#include "sourceGameLounge.h"
#include <chrono>
#include <thread>
#include <comdef.h>
namespace sgl {
EventSink::EventSink()
{
m_lRef = 0;
}
ULONG EventSink::AddRef()
{
return InterlockedIncrement(&m_lRef);
}
ULONG EventSink::Release()
{
LONG lRef = InterlockedDecrement(&m_lRef);
if (lRef == 0)
delete this;
return lRef;
}
HRESULT EventSink::QueryInterface(REFIID riid, void** ppv)
{
if (riid == IID_IUnknown || riid == IID_IWbemObjectSink)
{
*ppv = (IWbemObjectSink *) this;
AddRef();
return WBEM_S_NO_ERROR;
}
else
return E_NOINTERFACE;
}
HRESULT EventSink::Indicate(long lObjectCount, IWbemClassObject **apObjArray)
{
if (!GameDetection::instance().destructing)
{
EnterCriticalSection(&GameDetection::instance().criticalSection);
HRESULT hr = S_OK;
_variant_t vtProp;
for (int i = 0; i < lObjectCount; i++)
{
if (GameDetection::instance().bProcessDetected || GameDetection::instance().destructing)
{
LeaveCriticalSection(&GameDetection::instance().criticalSection);
return WBEM_S_NO_ERROR;
}
hr = apObjArray[i]->Get(_bstr_t(L"TargetInstance"), 0, &vtProp, 0, 0);
if (!FAILED(hr))
{
IUnknown* str = vtProp;
hr = str->QueryInterface(IID_IWbemClassObject, reinterpret_cast<void**>(&apObjArray[i]));
if (SUCCEEDED(hr))
{
_variant_t cn;
hr = apObjArray[i]->Get(L"Caption", 0, &cn, NULL, NULL);
if (SUCCEEDED(hr))
{
if ((cn.vt == VT_NULL) || (cn.vt == VT_EMPTY) || (cn.vt == VT_ARRAY))
lstrcpyW(GameDetection::instance().processName, ((cn.vt == VT_NULL) ? L"NULL" : L"EMPTY"));
else
lstrcpyW(GameDetection::instance().processName, cn.bstrVal);
WCHAR *exes[] = {L"csgo.exe", L"hl2.exe", NULL}; // temporary
int n = 1; // temporary
while ((exes[n - 1] != NULL) && wcscmp(GameDetection::instance().processName, exes[n - 1])) // temporary
++n; // temporary
if (n < 3) // temporary
GameDetection::instance().bProcessDetected = true;
}
VariantClear(&cn);
/* Use process path to make sure it's the desired game
hr = apObjArray[i]->Get(L"CommandLine", 0, &cn, NULL, NULL);
if (SUCCEEDED(hr))
{
if ((cn.vt == VT_NULL) || (cn.vt == VT_EMPTY) || (cn.vt == VT_ARRAY))
lstrcpyW(gameDetection::instance().processPath, ((cn.vt == VT_NULL) ? L"NULL" : L"EMPTY"));
else
lstrcpyW(gameDetection::instance().processPath, cn.bstrVal);
}
VariantClear(&cn);*/
if (GameDetection::instance().bProcessDetected)
{
hr = apObjArray[i]->Get(L"Handle", 0, &cn, NULL, NULL);
if (SUCCEEDED(hr))
{
if ((cn.vt == VT_NULL) || (cn.vt == VT_EMPTY) || (cn.vt == VT_ARRAY))
{
lstrcpyW(GameDetection::instance().processPID, ((cn.vt == VT_NULL) ? L"NULL" : L"EMPTY"));
GameDetection::instance().bProcessDetected = false;
}
else
{
lstrcpyW(GameDetection::instance().processPID, cn.bstrVal);
GameDetection::instance().processDetected();
}
}
else
GameDetection::instance().bProcessDetected = false;
VariantClear(&cn);
}
}
}
VariantClear(&vtProp);
}
LeaveCriticalSection(&GameDetection::instance().criticalSection);
}
return WBEM_S_NO_ERROR;
}
HRESULT EventSink::SetStatus(LONG lFlags, HRESULT hResult, BSTR strParam, IWbemClassObject __RPC_FAR *pObjParam)
{
return WBEM_S_NO_ERROR;
}
GameDetection& GameDetection::instance()
{
static GameDetection gameDetection;
return gameDetection;
}
bool GameDetection::wmiInitialize(bool initialize)
{
HRESULT hres;
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
displayError(L"COM library initialization failed.");
return false;
}
if (!bCIS)
{
hres = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
if (FAILED(hres))
{
displayError(L"COM security initialization failed.");
CoUninitialize();
return false;
}
bCIS = true;
}
pLoc = NULL;
hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *)&pLoc);
if (FAILED(hres))
{
displayError(L"IWbemLocator object creation failed.");
CoUninitialize();
return false;
}
pSvc = NULL;
hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc);
if (FAILED(hres))
{
displayError(L"Connect to WMI namespace failed.");
pLoc->Release();
CoUninitialize();
return false;
}
hres = CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
if (FAILED(hres))
{
displayError(L"Setting WMI proxy blanket failed.");
pSvc->Release();
pLoc->Release();
CoUninitialize();
return false;
}
pUnsecApp = NULL;
hres = CoCreateInstance(CLSID_UnsecuredApartment, NULL, CLSCTX_LOCAL_SERVER, IID_IUnsecuredApartment, (void**)&pUnsecApp);
pSink = new EventSink();
pSink->AddRef();
pStubUnk = NULL;
pUnsecApp->CreateObjectStub(pSink, &pStubUnk);
pStubSink = NULL;
pStubUnk->QueryInterface(IID_IWbemObjectSink, (void **)&pStubSink);
hres = pSvc->ExecNotificationQueryAsync(_bstr_t("WQL"), _bstr_t("SELECT * " "FROM __InstanceCreationEvent WITHIN 1 " "WHERE TargetInstance ISA 'Win32_Process'"), WBEM_FLAG_SEND_STATUS, NULL, pStubSink);
if (FAILED(hres))
{
displayError(L"Call to ExecNotificationQueryAsync failed.");
pSvc->Release();
pLoc->Release();
pUnsecApp->Release();
pStubUnk->Release();
pSink->Release();
pStubSink->Release();
CoUninitialize();
return false;
}
if (initialize)
InitializeCriticalSection(&instance().criticalSection);
return bWMI = true;
}
void GameDetection::processDetected()
{
hProcHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)_wtoi(processPID));
HANDLE hNewHandle;
if (!RegisterWaitForSingleObject(&hNewHandle, hProcHandle, waitOrTimerCallback, NULL, INFINITE, WT_EXECUTEONLYONCE))
{
displayError(L"Call to RegisterWaitForSingleObject failed.");
CloseHandle(hProcHandle);
}
wmiCleanup();
}
GameDetection::GameDetection()
{
bWMI = false;
bProcessDetected = false;
bCIS = false;
}
GameDetection::~GameDetection()
{
destructing = true;
if (bWMI)
wmiCleanup();
while (!TryEnterCriticalSection(&instance().criticalSection))
std::this_thread::sleep_for(std::chrono::milliseconds(100));
LeaveCriticalSection(&instance().criticalSection);
DeleteCriticalSection(&instance().criticalSection);
}
VOID CALLBACK GameDetection::waitOrTimerCallback(PVOID lpParameter, BOOLEAN timerOrWaitFired)
{
displayError(instance().processName); // remove after testing
while (!instance().destructing)
{
if (!TryEnterCriticalSection(&instance().criticalSection))
std::this_thread::sleep_for(std::chrono::milliseconds(100));
else
break;
}
if (!instance().destructing)
{
instance().bProcessDetected = false;
LeaveCriticalSection(&instance().criticalSection);
instance().wmiInitialize();
}
CloseHandle(instance().hProcHandle);
}
void GameDetection::wmiCleanup()
{
pSvc->CancelAsyncCall(pStubSink);
pSvc->Release();
pLoc->Release();
pUnsecApp->Release();
pStubUnk->Release();
pSink->Release();
pStubSink->Release();
CoUninitialize();
bWMI = false;
}
} // namespace sgl