-
Notifications
You must be signed in to change notification settings - Fork 33
/
Detections.cpp
755 lines (631 loc) · 29 KB
/
Detections.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
//By AlSch092 @github
#include "Detections.hpp"
/*
Detections::StartMonitor - use class member MonitorThread to start our main detections loop
*/
BOOL Detections::StartMonitor()
{
if (this->MonitorThread != nullptr) //prevent accidental double calls to this function/double thread creation
return FALSE;
this->MonitorThread = new Thread((LPTHREAD_START_ROUTINE)&Monitor, (LPVOID)this, true);
Logger::logf("UltimateAnticheat.log", Info, "Created monitoring thread with ID %d", this->MonitorThread->GetId());
if (this->MonitorThread->GetHandle() == NULL || this->MonitorThread->GetHandle() == INVALID_HANDLE_VALUE)
{
Logger::logf("UltimateAnticheat.log", Err, " Failed to create monitor thread @ Detections::StartMonitor");
return FALSE;
}
return TRUE;
}
/*
LdrpDllNotification - This function is called whenever a new module is loaded into the process space, called before TLS callbacks
*/
VOID CALLBACK Detections::OnDllNotification(ULONG NotificationReason, const PLDR_DLL_NOTIFICATION_DATA NotificationData, PVOID Context)
{
Detections* Monitor = reinterpret_cast<Detections*>(Context);
if (NotificationReason == LDR_DLL_NOTIFICATION_REASON_LOADED)
{
LPCWSTR FullDllName = NotificationData->Loaded.FullDllName->pBuffer;
Logger::logfw("UltimateAnticheat.log", Info, L"[LdrpDllNotification Callback] dll loaded: %s, verifying signature...\n", FullDllName);
if (!Authenticode::HasSignature(FullDllName))
{
Logger::logfw("UltimateAnticheat.log", Detection, L"Failed to verify signature of %s\n", FullDllName);
Monitor->Flag(DetectionFlags::INJECTED_ILLEGAL_PROGRAM);
}
}
}
/*
Detections::Monitor(LPVOID thisPtr)
Routine which monitors aspects of the process for fragments of cheating, loops continuously until the thread is signalled to shut down
*/
void Detections::Monitor(LPVOID thisPtr)
{
if (thisPtr == NULL)
{
Logger::logf("UltimateAnticheat.log", Err, "thisPtr was NULL @ Detections::Monitor. Aborting execution!");
return;
}
Logger::logf("UltimateAnticheat.log", Info, "Starting Detections::Monitor");
Detections* Monitor = reinterpret_cast<Detections*>(thisPtr);
if (Monitor == nullptr)
{
Logger::logf("UltimateAnticheat.log", Err, "Monitor Ptr was NULL @ Detections::Monitor. Aborting execution!");
return;
}
UINT64 CachedSectionAddress = 0;
DWORD CachedSectionSize = 0;
if (Monitor->GetSettings()->bCheckIntegrity) //integrity check setup if option is enabled
{
list<ProcessData::Section*>* sections = Monitor->SetSectionHash("UltimateAnticheat.exe", ".text"); //set our memory hashes of .text
if (sections == nullptr)
{
Logger::logf("UltimateAnticheat.log", Err, "Sections was NULLPTR @ Detections::Monitor. Aborting execution!");
return;
}
if (sections->size() == 0)
{
Logger::logf("UltimateAnticheat.log", Err, "Sections size was 0 @ Detections::Monitor. Aborting execution!");
return;
}
UINT64 ModuleAddr = (UINT64)GetModuleHandleA(NULL);
if (ModuleAddr == 0)
{
Logger::logf("UltimateAnticheat.log", Err, "Module couldn't be retrieved @ Detections::Monitor. Aborting execution! (%d)\n", GetLastError());
return;
}
std::list<ProcessData::Section*>::iterator it;
for (it = sections->begin(); it != sections->end(); ++it)
{
ProcessData::Section* s = it._Ptr->_Myval;
if (s == nullptr)
continue;
if (strcmp(s->name, ".text") == 0) //cache our .text sections address and memory size, since an attacker could possibly spoof the section name or # of sections in ntheaders to prevent section traversing
{
CachedSectionAddress = s->address + ModuleAddr; //any strings such as ".text" can be encrypted at compile time and decrypted at runtime to make reversing a bit more difficult
CachedSectionSize = s->size;
break;
}
}
}
//Main Monitor Loop, continuous detections go in here. we need access to CachedSectionAddress variables so this loop doesnt get its own function.
bool Monitoring = true;
const int MonitorLoopMilliseconds = 5000;
while (Monitoring) //&& !Monitor->IsUserCheater()) //uncomment if you'd like monitoring to stop once a cheater has been detected
{
if (Monitor->GetMonitorThread()->IsShutdownSignalled())
{
Logger::logf("UltimateAnticheat.log", Info, "STOPPING Detections::Monitor , ending detections thread");
//Monitor->GetMonitorThread()->CurrentlyRunning = false;
return;
}
if (Monitor->GetSettings()->bCheckIntegrity)
{
if (Monitor->GetIntegrityChecker()->IsTLSCallbackStructureModified()) //check various aspects of the TLS callback structure for modifications
{
Logger::logf("UltimateAnticheat.log", Detection, "Found modified TLS callback structure section (atleast one aspect of the TLS data directory structure was modified)");
Monitor->Flag(DetectionFlags::CODE_INTEGRITY);
}
if (Monitor->CheckSectionHash(CachedSectionAddress, CachedSectionSize)) //compare hashes of .text for modifications
{
Logger::logf("UltimateAnticheat.log", Detection, "Found modified .text section (or you're debugging with software breakpoints)!\n");
Monitor->Flag(DetectionFlags::CODE_INTEGRITY);
}
}
if (Monitor->GetIntegrityChecker()->IsModuleModified(L"WINTRUST.dll")) //check hashes of wintrust.dll for signing-related hooks
{
Logger::logf("UltimateAnticheat.log", Detection, "Found modified .text section in WINTRUST.dll!");
Monitor->Flag(DetectionFlags::CODE_INTEGRITY);
}
if (Monitor->IsBlacklistedProcessRunning()) //external applications running on machine
{
Logger::logf("UltimateAnticheat.log", Detection, "Found blacklisted process!");
Monitor->Flag(DetectionFlags::EXTERNAL_ILLEGAL_PROGRAM);
}
//make sure ws2_32.dll is actually loaded if this gives an error, on my build the dll is not loaded but we'll pretend it is
if (Monitor->DoesFunctionAppearHooked("ws2_32.dll", "send") || Monitor->DoesFunctionAppearHooked("ws2_32.dll", "recv")) //ensure you use this routine on functions that don't have jumps or calls as their first byte
{
Logger::logf("UltimateAnticheat.log", Detection, "Networking WINAPI (send | recv) was hooked!\n"); //WINAPI hooks doesn't always determine someone is cheating since AV and other software can write the hooks
Monitor->Flag(DetectionFlags::DLL_TAMPERING);
}
if (Monitor->GetIntegrityChecker()->IsUnknownModulePresent()) //authenticode call and check against whitelisted module list
{
Logger::logf("UltimateAnticheat.log", Detection, "Found at least one unsigned dll loaded : We ideally only want verified, signed dlls in our application!");
Monitor->Flag(DetectionFlags::INJECTED_ILLEGAL_PROGRAM);
}
if (Services::IsTestsigningEnabled() || Services::IsDebugModeEnabled()) //test signing enabled, self-signed drivers
{
Logger::logf("UltimateAnticheat.log", Detection, "Testsigning or debugging mode is enabled! In most cases we don't allow the game/process to continue if testsigning is enabled.");
Monitor->Flag(DetectionFlags::UNSIGNED_DRIVERS);
}
if (Detections::DoesIATContainHooked()) //iat hook check
{
Logger::logf("UltimateAnticheat.log", Detection, "IAT was hooked! One or more functions lead to addresses outside their respective modules!\n");
Monitor->Flag(DetectionFlags::BAD_IAT);
}
if (Detections::IsTextSectionWritable()) //page protections check, can be made more granular or loop over all mem pages
{
Logger::logf("UltimateAnticheat.log", Detection, ".text section was writable, which means someone re-re-mapped our memory regions! (or you ran this in DEBUG build)");
#ifndef _DEBUG //in debug build we are not remapping, and software breakpoints in VS may cause page protections to be writable
Monitor->Flag(DetectionFlags::PAGE_PROTECTIONS);
#endif
}
if (Detections::CheckOpenHandles()) //open handles to our process check
{
Logger::logf("UltimateAnticheat.log", Detection, "Found open process handles to our process from other processes");
Monitor->Flag(DetectionFlags::OPEN_PROCESS_HANDLES);
}
if (Monitor->IsBlacklistedWindowPresent())
{
Logger::logf("UltimateAnticheat.log", Detection, "Found blacklisted window text!");
Monitor->Flag(DetectionFlags::EXTERNAL_ILLEGAL_PROGRAM);
}
Sleep(MonitorLoopMilliseconds);
}
}
/*
SetSectionHash sets the member variable `_MemorySectionHashes` via SetMemoryHashList() call after finding the `sectionName` named section (.text in our case)
Returns a list<Section*> which we can use in later hashing calls to compare sets of these hashes and detect memory tampering within the section
*/
list<ProcessData::Section*>* Detections::SetSectionHash(const char* moduleName, const char* sectionName)
{
if (moduleName == NULL || sectionName == NULL)
{
return nullptr;
}
list<ProcessData::Section*>* sections = Process::GetSections(moduleName);
UINT64 ModuleAddr = (UINT64)GetModuleHandleA(moduleName);
if (ModuleAddr == 0)
{
Logger::logf("UltimateAnticheat.log", Err, "ModuleAddr was 0 @ SetSectionHash\n", sectionName);
return nullptr;
}
if (sections->size() == 0)
{
Logger::logf("UltimateAnticheat.log", Err, "sections.size() of section %s was 0 @ SetSectionHash\n", sectionName);
return nullptr;
}
std::list<ProcessData::Section*>::iterator it;
for (it = sections->begin(); it != sections->end(); ++it)
{
ProcessData::Section* s = it._Ptr->_Myval;
if (s == nullptr)
continue;
if (strcmp(s->name, sectionName) == 0)
{
vector<uint64_t> hashes = GetIntegrityChecker()->GetMemoryHash((uint64_t)s->address + ModuleAddr, s->size); //check most of section, a few short to stop edge read cases
if (hashes.size() > 0)
{
GetIntegrityChecker()->SetMemoryHashList(hashes);
break;
}
else
{
Logger::logf("UltimateAnticheat.log", Err, "hashes.size() was 0 @ SetSectionHash\n", sectionName);
return nullptr;
}
}
}
return sections;
}
/*
CheckSectionHash compares our collected hash list from ::SetSectionHash() , we use cached address + size to prevent spoofing (sections can be renamed at runtime by an attacker)
Returns true if the two sets of hashes do not match, implying memory was modified
*/
BOOL Detections::CheckSectionHash(UINT64 cachedAddress, DWORD cachedSize)
{
if (cachedAddress == 0 || cachedSize == 0)
{
Logger::logf("UltimateAnticheat.log", Err, "Parameters were 0 @ Detections::CheckSectionHash");
return FALSE;
}
Logger::logf("UltimateAnticheat.log", Info, "Checking hashes of address: %llx (%d bytes) for memory integrity\n", cachedAddress, cachedSize);
if (GetIntegrityChecker()->Check((uint64_t)cachedAddress, cachedSize, GetIntegrityChecker()->GetMemoryHashList())) //compares hash to one gathered previously
{
Logger::logf("UltimateAnticheat.log", Info, "Hashes match: Program's .text section appears genuine.\n");
return FALSE;
}
else
{
Logger::logf("UltimateAnticheat.log", Detection, " .text section of program is modified!\n");
return TRUE;
}
}
/*
IsBlacklistedProcessRunning
returns TRUE if a blacklisted program is running in the background, blacklisted processes can be found in the class constructor
*/
BOOL __forceinline Detections::IsBlacklistedProcessRunning() const
{
BOOL foundBlacklistedProcess = FALSE;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE)
{
Logger::logf("UltimateAnticheat.log", Err, "Failed to create snapshot of processes. Error code: %d @ Detections::IsBlacklistedProcessRunning\n", GetLastError());
return FALSE;
}
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot, &pe32))
{
Logger::logf("UltimateAnticheat.log", Err, "Failed to get first process. Error code: %d @ Detections::IsBlacklistedProcessRunning\n", GetLastError());
CloseHandle(hSnapshot);
return FALSE;
}
do
{
for (wstring blacklisted : BlacklistedProcesses)
{
if (Utility::wcscmp_insensitive(blacklisted.c_str(), pe32.szExeFile))
{
foundBlacklistedProcess = true;
break;
}
}
} while (Process32Next(hSnapshot, &pe32));
CloseHandle(hSnapshot);
return foundBlacklistedProcess;
}
/*
* DoesFunctionAppearHooked - Checks if first bytes of a routine are a jump or call. Please make sure the function you use with this doesnt normally start with a jump or call.
Returns TRUE if the looked up function contains a jump or call as its first instruction
*/
BOOL __forceinline Detections::DoesFunctionAppearHooked(const char* moduleName, const char* functionName)
{
if (moduleName == nullptr || functionName == nullptr)
{
Logger::logf("UltimateAnticheat.log", Err, "moduleName or functionName was NULL @ Detections::DoesFunctionAppearHooked");
return FALSE;
}
BOOL FunctionPreambleHooked = FALSE;
HMODULE hMod = GetModuleHandleA(moduleName);
if (hMod == NULL)
{
Logger::logf("UltimateAnticheat.log", Err, " Couldn't fetch module @ Detections::DoesFunctionAppearHooked: %s\n", moduleName);
return FALSE;
}
UINT64 AddressFunction = (UINT64)GetProcAddress(hMod, functionName);
if (AddressFunction == NULL)
{
Logger::logf("UltimateAnticheat.log", Err, " Couldn't fetch address of function @ Detections::DoesFunctionAppearHooked: %s\n", functionName);
return FALSE;
}
__try
{
if (*(BYTE*)AddressFunction == 0xE8 || *(BYTE*)AddressFunction == 0xE9 || *(BYTE*)AddressFunction == 0xEA || *(BYTE*)AddressFunction == 0xEB) //0xEB = short jump, 0xE8 = call X, 0xE9 = long jump, 0xEA = "jmp oper2:oper1"
FunctionPreambleHooked = TRUE;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
Logger::logf("UltimateAnticheat.log", Warning, " Couldn't read bytes @ Detections::DoesFunctionAppearHooked: %s\n", functionName);
return FALSE; //couldn't read memory at function
}
return FunctionPreambleHooked;
}
/*
DoesIATContainHooked - Returns TRUE if any routines in the IAT lead to addresses outside their respective modules
if the attacker writes their hooks in the dll's address space then they can get around this detection
*/
BOOL __forceinline Detections::DoesIATContainHooked()
{
list<ProcessData::ImportFunction*> IATFunctions = Process::GetIATEntries();
auto modules = Process::GetLoadedModules();
if (modules == nullptr)
{
Logger::logf("UltimateAnticheat.log", Err, " Couldn't fetch module list @ Detections::DoesIATContainHooked");
return FALSE;
}
for (ProcessData::ImportFunction* IATEntry : IATFunctions)
{
DWORD moduleSize = Process::GetModuleSize(IATEntry->Module);
if (moduleSize != 0)
{ //some IAT functions in k32 can point to ntdll (forwarding), thus we have to compare IAT to each other whitelisted DLL range
for (std::vector<ProcessData::MODULE_DATA>::iterator it = modules->begin(); it != modules->end(); ++it)
{
UINT64 LowAddr = (UINT64)it->dllInfo.lpBaseOfDll;
UINT64 HighAddr = (UINT64)it->dllInfo.lpBaseOfDll + it->dllInfo.SizeOfImage;
if (IATEntry->AddressOfData > LowAddr && IATEntry->AddressOfData < HighAddr)
{
delete modules; modules = nullptr;
return FALSE; //IAT function was found to be inside address range of loaded DLL, thus its not hooked
}
}
}
else //error, we shouldnt get here!
{
Logger::logf("UltimateAnticheat.log", Err, " Couldn't fetch module size @ Detections::DoesIATContainHooked");
delete modules; modules = nullptr;
return FALSE;
}
}
delete modules; modules = nullptr;
return TRUE;
}
/*
Detections::IsTextSectionWritable() - Simple memory protections check on page in the .text section
returns address where the page was writable, which imples someone re-re-mapped our process memory and wants to write patches.
*/
UINT64 Detections::IsTextSectionWritable()
{
UINT64 textAddr = Process::GetSectionAddress(NULL, ".text");
MEMORY_BASIC_INFORMATION mbi = { 0 };
SIZE_T result;
UINT64 address = textAddr;
const int pageSize = 0x1000;
if (textAddr == NULL)
{
Logger::logf("UltimateAnticheat.log", Err, "textAddr was NULL @ Detections::IsTextSectionWritable");
return 0;
}
UINT64 max_addr = textAddr + Process::GetTextSectionSize(GetModuleHandle(NULL));
while ((result = VirtualQuery((LPCVOID)address, &mbi, sizeof(mbi))) != 0) //Loop through all pages in .text
{
if (address >= max_addr)
break;
if (mbi.Protect != PAGE_EXECUTE_READ) //check if its not RX protections
{
Logger::logfw("UltimateAnticheat.log", Detection, L"Memory region at address %p is not PAGE_EXECUTE_READ - attacker likely re-re-mapped\n", address);
return address;
}
address += pageSize;
}
return 0;
}
/*
CheckOpenHandles - Checks if any processes have open handles to our process, excluding whitelisted processes such as conhost.exe
returns true if some other process has an open process handle to the current process
*/
BOOL Detections::CheckOpenHandles()
{
BOOL foundHandle = FALSE;
std::vector<Handles::_SYSTEM_HANDLE> handles = Handles::DetectOpenHandlesToProcess();
for (auto& handle : handles)
{
if (Handles::DoesProcessHaveOpenHandleTous(handle.ProcessId, handles))
{
wstring procName = Process::GetProcessName(handle.ProcessId);
int size = sizeof(Handles::Whitelisted) / sizeof(UINT64);
for (int i = 0; i < size; i++)
{
if (wcscmp(Handles::Whitelisted[i], procName.c_str()) == 0) //whitelisted program has open handle
{
goto inner_break;
}
}
Logger::logfw("UltimateAnticheat.log", Detection, L"Process %s has open process handle to our process.", procName.c_str());
foundHandle = TRUE;
inner_break:
continue;
}
}
return foundHandle;
}
/*
AddDetectedFlags - adds DetectionFlags `flag` to the list of detected flags. Does not add if the flag is already in the list.
*/
bool Detections::AddDetectedFlag(DetectionFlags flag)
{
bool isDuplicate = false;
for (DetectionFlags f : this->DetectedFlags)
{
if (f == flag)
{
isDuplicate = true;
}
}
if (!isDuplicate)
this->DetectedFlags.push_back(flag);
return isDuplicate;
}
/*
Flag - function adds flag to the detected list and sends a message to the server informing of the detection
*/
bool Detections::Flag(DetectionFlags flag)
{
bool wasDuplicate = AddDetectedFlag(flag);
this->SetCheater(true);
if (wasDuplicate) //prevent duplicate server comms
return true;
NetClient* client = this->GetNetClient(); //report back to server that someone's cheating
if (client != nullptr)
{
if (client->FlagCheater(flag) != Error::OK) //cheat engine attachment can be detected this way
{
Logger::logf("UltimateAnticheat.log", Err, "Failed to notify server of cheating status.");
return false;
}
}
else
{
Logger::logf("UltimateAnticheat.log", Err, "NetClient was NULL @ Detections::Flag");
return false;
}
return true;
}
/*
IsBlacklistedWindowPresent - Checks if windows with specific title or class names are present
*/
BOOL Detections::IsBlacklistedWindowPresent()
{
typedef BOOL(WINAPI* ENUMWINDOWS)(WNDENUMPROC, LPARAM);
HMODULE hUser32 = GetModuleHandleA("USER32.dll");
if (hUser32 != NULL)
{
auto WindowCallback = [](HWND hwnd, LPARAM lParam) -> BOOL
{
char windowTitle[256]{ 0 };
char className[256]{ 0 };
const int xorKey1 = 0x44;
const int xorKey2 = 0x47;
Detections* Monitor = reinterpret_cast<Detections*>(lParam); //optionally, make blacklisted xor'd strings into a list in Detections class
unsigned char CheatEngine[] = //"Cheat Engine"
{
'C' ^ xorKey1, 'h' ^ xorKey1, 'e' ^ xorKey1, 'a' ^ xorKey1, 't' ^ xorKey1, ' ' ^ xorKey1,
'E' ^ xorKey1, 'n' ^ xorKey1, 'g' ^ xorKey1, 'i' ^ xorKey1, 'n' ^ xorKey1, 'e' ^ xorKey1
};
unsigned char LuaScript[] = // //"Lua script:"
{
'L' ^ xorKey2, 'u' ^ xorKey2, 'a' ^ xorKey2, ' ' ^ xorKey2,
's' ^ xorKey2, 'c' ^ xorKey2, 'r' ^ xorKey2, 'i' ^ xorKey2,
'p' ^ xorKey2, 't' ^ xorKey2, ':' ^ xorKey2
};
char original_CheatEngine[13]{ 0 };
char original_LUAScript[12]{ 0 };
for (int i = 0; i < 13 - 1; i++) //13 - 1 to stop last 00 from being xor'd
{
original_CheatEngine[i] = (char)(CheatEngine[i] ^ xorKey1);
}
for (int i = 0; i < 12; i++)
{
original_LUAScript[i] = (char)(CheatEngine[i] ^ xorKey2);
}
if (GetWindowTextA(hwnd, windowTitle, sizeof(windowTitle)))
{
if (GetClassNameA(hwnd, className, sizeof(className)))
{
if (strstr(windowTitle, (const char*)original_CheatEngine) || strstr(windowTitle, (const char*)original_CheatEngine) != NULL)
{
Monitor->SetCheater(true);
Monitor->Flag(DetectionFlags::EXTERNAL_ILLEGAL_PROGRAM);
Logger::logf("UltimateAnticheat.log", Detection, "Detected cheat engine window");
return FALSE;
}
else if (strstr(windowTitle, (const char*)original_LUAScript))
{
Monitor->SetCheater(true);
Monitor->Flag(DetectionFlags::EXTERNAL_ILLEGAL_PROGRAM);
Logger::logf("UltimateAnticheat.log", Detection, "Detected cheat engine's lua script window");
return FALSE;
}
}
}
return TRUE;
};
ENUMWINDOWS pEnumWindows = (ENUMWINDOWS)GetProcAddress(hUser32, "EnumWindows");
if (pEnumWindows != NULL)
{
EnumWindows(WindowCallback, (LPARAM)this);
}
else
{
Logger::logf("UltimateAnticheat.log", Err, "GetProcAddress failed @ Detections::IsBlacklistedWindowPresent: %d", GetLastError());
return FALSE;
}
}
else
{
Logger::logf("UltimateAnticheat.log", Err, "GetModuleHandle failed @ Detections::IsBlacklistedWindowPresent: %d", GetLastError());
return FALSE;
}
return FALSE;
}
/*
Detections::MonitorNewProcesses - Monitors process creation events via WMI
Intended thread function, has no return value as it logs in real-time
*/
void Detections::MonitorProcessCreation(LPVOID thisPtr)
{
if (thisPtr == nullptr)
{
Logger::logf("UltimateAnticheat.log", Err, "Monitor Ptr was NULL @ MonitorNewProcesses");
return;
}
Detections* monitor = reinterpret_cast<Detections*>(thisPtr);
monitor->MonitoringProcessCreation = true;
HRESULT hres;
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
Logger::logf("UltimateAnticheat.log", Err, "Failed to initialize COM library @ MonitorNewProcesses");
return;
}
hres = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
if (FAILED(hres))
{
Logger::logf("UltimateAnticheat.log", Err, "Failed to initialize security @ MonitorNewProcesses");
CoUninitialize();
return;
}
IWbemLocator* pLoc = NULL;
hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLoc);
if (FAILED(hres))
{
Logger::logf("UltimateAnticheat.log", Err, "Failed to create IWbemLocator object");
CoUninitialize();
return;
}
IWbemServices* pSvc = NULL;
hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc);
if (FAILED(hres))
{
Logger::logf("UltimateAnticheat.log", Err, "Could not connect to WMI namespace");
pLoc->Release();
CoUninitialize();
return;
}
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))
{
Logger::logf("UltimateAnticheat.log", Err, "Could not set proxy blanket");
pSvc->Release();
pLoc->Release();
CoUninitialize();
return;
}
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecNotificationQuery((wchar_t*)L"WQL", (wchar_t*)L"SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'", WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
if (FAILED(hres))
{
Logger::logf("UltimateAnticheat.log", Err, "Query for process creation events failed");
pSvc->Release();
pLoc->Release();
CoUninitialize();
return;
}
IWbemClassObject* pclsObj = NULL;
ULONG uReturn = 0;
while (pEnumerator && monitor->MonitoringProcessCreation)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (0 == uReturn) break;
VARIANT vtProp;
hr = pclsObj->Get(L"TargetInstance", 0, &vtProp, 0, 0);
if (SUCCEEDED(hr) && (vtProp.vt == VT_UNKNOWN))
{
IUnknown* str = vtProp.punkVal;
IWbemClassObject* pClassObj = NULL;
str->QueryInterface(IID_IWbemClassObject, (void**)&pClassObj);
if (pClassObj)
{
VARIANT vtName;
pClassObj->Get(L"Name", 0, &vtName, 0, 0);
for (wstring blacklistedProcess : monitor->BlacklistedProcesses)
{
if (Utility::wcscmp_insensitive(blacklistedProcess.c_str(), vtName.bstrVal))
{
Logger::logfw("UltimateAnticheat.log", Detection, L"Blacklisted process was spawned: %s", vtName.bstrVal);
break;
}
}
VariantClear(&vtName);
pClassObj->Release();
}
}
VariantClear(&vtProp);
pclsObj->Release();
}
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();
}
/*
InitializeBlacklistedProcessesList - add static list of blacklisted process names to our Detections object
*/
void Detections::InitializeBlacklistedProcessesList()
{
this->BlacklistedProcesses.push_back(L"Cheat Engine.exe"); //todo: hide these strings
this->BlacklistedProcesses.push_back(L"CheatEngine.exe"); //...we can also scan for window class names, possible exported functions, specific text inside windows, etc.
this->BlacklistedProcesses.push_back(L"cheatengine-x86_64-SSE4-AVX2.exe");
this->BlacklistedProcesses.push_back(L"x64dbg.exe");
this->BlacklistedProcesses.push_back(L"windbg.exe");
this->BlacklistedProcesses.push_back(L"DSEFix.exe");
}