-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
880 lines (787 loc) · 31.8 KB
/
main.cpp
File metadata and controls
880 lines (787 loc) · 31.8 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
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
#include <Windows.h>
#include <iostream>
#include <string>
#include <TlHelp32.h>
#include <cwchar>
#include <fstream>
#include <filesystem>
#include <algorithm>
#include <stdexcept>
#include <sddl.h>
#include <thread>
#include <chrono>
#include <vector>
#include <psapi.h>
#include <memoryapi.h>
#define FOREGROUND_YELLOW (FOREGROUND_RED | FOREGROUND_GREEN)
#define FOREGROUND_WHITE (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
HANDLE hProcess = nullptr;
std::wstring targetProcessName;
const std::string INJECTOR_VERSION = "1.4";
const std::vector<std::wstring> SUPPORTED_GAMES = { L"cs2.exe",
L"csgo.exe",
L"RustClient.exe",
L"gmod.exe"
};
namespace Helper {
void SetConsoleColor(int color) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, color);
}
void PrintBanner() {
SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cout << "AnarchyInjector v" << INJECTOR_VERSION << std::endl << std::endl;
SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
std::cout << "ManualMap DLL injector for:" << std::endl;
for (const std::wstring& game : SUPPORTED_GAMES) {
std::wcout << L"- " << game << std::endl;
}
std::cout << "By: dest4590" << std::endl;
SetConsoleColor(FOREGROUND_WHITE);
}
bool IsElevated() {
BOOL fIsElevated = FALSE;
HANDLE hToken = NULL;
TOKEN_ELEVATION tokenElevation;
DWORD dwSize;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "OpenProcessToken failed: " << GetLastError() << std::endl;
SetConsoleColor(FOREGROUND_WHITE);
return FALSE;
}
if (GetTokenInformation(hToken, TokenElevation, &tokenElevation, sizeof(tokenElevation), &dwSize)) {
fIsElevated = tokenElevation.TokenIsElevated;
}
else {
SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "GetTokenInformation failed: " << GetLastError() << std::endl;
SetConsoleColor(FOREGROUND_WHITE);
}
if (hToken) {
CloseHandle(hToken);
}
return fIsElevated;
}
std::string GetFileNameFromPath(const std::string& path) {
return std::filesystem::path(path).filename().string();
}
bool IsDigits(const std::string& str) {
return std::all_of(str.begin(), str.end(), ::isdigit);
}
bool IsGameInSupportedList(const std::wstring& processName) {
return std::any_of(SUPPORTED_GAMES.begin(), SUPPORTED_GAMES.end(),
[&](const std::wstring& game) { return _wcsicmp(processName.c_str(), game.c_str()) == 0; });
}
}
namespace ProcessUtils {
HANDLE GetProcessByName(const std::wstring& processName) {
DWORD desiredAccess = PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ;
if (Helper::IsElevated()) {
desiredAccess = PROCESS_ALL_ACCESS;
}
PROCESSENTRY32W entry;
entry.dwSize = sizeof(PROCESSENTRY32W);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (snapshot == INVALID_HANDLE_VALUE) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "CreateToolhelp32Snapshot failed: " << GetLastError() << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return NULL;
}
if (Process32FirstW(snapshot, &entry)) {
do {
if (_wcsicmp(entry.szExeFile, processName.c_str()) == 0) {
DWORD processId = entry.th32ProcessID;
HANDLE hProc = OpenProcess(desiredAccess, FALSE, processId);
if (hProc != NULL) {
CloseHandle(snapshot);
targetProcessName = processName;
return hProc;
}
else {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::wcerr << L"OpenProcess failed for process '" << processName << L"' (PID: " << processId << L"): " << GetLastError() << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
}
}
} while (Process32NextW(snapshot, &entry));
}
CloseHandle(snapshot);
return NULL;
}
HANDLE GetProcessById(DWORD processId) {
DWORD desiredAccess = PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ;
if (Helper::IsElevated()) {
desiredAccess = PROCESS_ALL_ACCESS;
}
HANDLE hProc = OpenProcess(desiredAccess, FALSE, processId);
if (hProc == NULL) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "OpenProcess failed for PID " << processId << ": " << GetLastError() << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
}
else {
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (snapshot != INVALID_HANDLE_VALUE) {
PROCESSENTRY32W entry;
entry.dwSize = sizeof(PROCESSENTRY32W);
if (Process32FirstW(snapshot, &entry)) {
do {
if (entry.th32ProcessID == processId) {
targetProcessName = entry.szExeFile;
break;
}
} while (Process32NextW(snapshot, &entry));
}
CloseHandle(snapshot);
}
else
{
Helper::SetConsoleColor(FOREGROUND_YELLOW | FOREGROUND_INTENSITY);
std::cerr << "Warning: Could not retrieve process name for PID " << processId << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
targetProcessName = std::to_wstring(processId);
}
}
return hProc;
}
}
namespace ModuleUtils {
bool WaitForModules(HANDLE hProcess, const std::vector<std::wstring>& moduleNames, DWORD timeoutMs) {
auto startTime = std::chrono::steady_clock::now();
std::vector<HMODULE> hModules(1024);
DWORD cbNeeded;
while (true) {
if (!EnumProcessModules(hProcess, hModules.data(), static_cast<DWORD>(hModules.size() * sizeof(HMODULE)), &cbNeeded)) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "EnumProcessModules failed: " << GetLastError() << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return false;
}
if (cbNeeded > hModules.size() * sizeof(HMODULE)) {
hModules.resize(cbNeeded / sizeof(HMODULE));
continue;
}
int modulesFound = 0;
for (const auto& moduleName : moduleNames) {
for (DWORD i = 0; i < cbNeeded / sizeof(HMODULE); ++i) {
wchar_t szModuleName[MAX_PATH];
if (GetModuleFileNameExW(hProcess, hModules[i], szModuleName, MAX_PATH)) {
if (_wcsicmp(szModuleName, moduleName.c_str()) == 0 || std::wstring(szModuleName).find(moduleName) != std::wstring::npos) {
modulesFound++;
break;
}
}
}
}
if (modulesFound == moduleNames.size()) {
return true;
}
auto currentTime = std::chrono::steady_clock::now();
auto elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime).count();
if (elapsedTime > timeoutMs) {
Helper::SetConsoleColor(FOREGROUND_YELLOW | FOREGROUND_INTENSITY);
std::cerr << "Timeout waiting for modules." << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return false;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
}
namespace MemoryUtils {
LPVOID FindFreeMemoryRegion(HANDLE hProcess, SIZE_T size) {
MEMORY_BASIC_INFORMATION mbi;
LPVOID address = NULL;
while (VirtualQueryEx(hProcess, address, &mbi, sizeof(mbi))) {
if (mbi.State == MEM_FREE && mbi.RegionSize >= size) {
return mbi.BaseAddress;
}
address = (LPVOID)((DWORD_PTR)mbi.BaseAddress + mbi.RegionSize);
}
return NULL;
}
}
LPVOID ntOpenFile = GetProcAddress(LoadLibraryW(L"ntdll"), "NtOpenFile"); // https://github.com/v3ctra/load-lib-injector
namespace Injection {
void bypass(HANDLE hProcess) // https://github.com/v3ctra/load-lib-injector
{
// Restore original NtOpenFile from external process
//credits: Daniel Krupiñski(pozdro dla ciebie byczku <3)
if (ntOpenFile) {
char originalBytes[5];
memcpy(originalBytes, ntOpenFile, 5);
WriteProcessMemory(hProcess, ntOpenFile, originalBytes, 5, NULL);
}
}
void backup(HANDLE hProcess) // https://github.com/v3ctra/load-lib-injector
{
if (ntOpenFile) {
//So, when I patching first 5 bytes I need to backup them to 0? (I think)
char originalBytes[5];
memcpy(originalBytes, ntOpenFile, 5);
WriteProcessMemory(hProcess, ntOpenFile, originalBytes, 0, NULL);
}
}
bool InjectDll(const std::string& path, HANDLE hProcess) {
std::filesystem::path dllPath = std::filesystem::absolute(path);
std::string absoluteDllPath = dllPath.string();
std::string dllFileName = Helper::GetFileNameFromPath(absoluteDllPath);
Helper::SetConsoleColor(FOREGROUND_YELLOW | FOREGROUND_INTENSITY);
std::wcout << L"Attempting to inject DLL: " << std::wstring(dllFileName.begin(), dllFileName.end()) << L" into process: " << targetProcessName << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
std::ifstream file(absoluteDllPath);
if (!file.good()) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Error: DLL file not found: " << absoluteDllPath << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return false;
}
file.close();
std::cout << "[+] DLL file found." << std::endl;
if (dllFileName == "skeet.dll") {
Helper::SetConsoleColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
std::cout << "Performing skeet-specific injection..." << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
bypass(hProcess);
VirtualAllocEx(hProcess, (LPVOID)0x43310000, 0x2FC000u, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); // for skeet
VirtualAllocEx(hProcess, 0, 0x1000u, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); // for skeet
LPVOID lpPathAddress = VirtualAllocEx(hProcess, nullptr, absoluteDllPath.size() + 1, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (lpPathAddress == nullptr) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Error: VirtualAllocEx failed (skeet path alloc): " << GetLastError() << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return false;
}
Helper::SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
std::cout << "Memory allocated for path at address: " << lpPathAddress << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
if (!WriteProcessMemory(hProcess, lpPathAddress, absoluteDllPath.c_str(), absoluteDllPath.size() + 1, nullptr)) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Error: WriteProcessMemory failed (skeet path write): " << GetLastError() << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
VirtualFreeEx(hProcess, lpPathAddress, 0, MEM_RELEASE);
return false;
}
std::cout << "[+] DLL path written successfully." << std::endl;
HMODULE hKernel32 = GetModuleHandleA("kernel32.dll");
if (!hKernel32) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Error: GetModuleHandleA failed for kernel32.dll" << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
VirtualFreeEx(hProcess, lpPathAddress, 0, MEM_RELEASE);
return false;
}
FARPROC lpLoadLibraryA = GetProcAddress(hKernel32, "LoadLibraryA");
if (!lpLoadLibraryA) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Error: GetProcAddress failed for LoadLibraryA" << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
VirtualFreeEx(hProcess, lpPathAddress, 0, MEM_RELEASE);
return false;
}
std::cout << "[+] LoadLibraryA address found." << std::endl;
HANDLE hThread = CreateRemoteThread(hProcess, nullptr, 0, (LPTHREAD_START_ROUTINE)lpLoadLibraryA, lpPathAddress, 0, nullptr);
if (!hThread) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Error: CreateRemoteThread failed (skeet injection): " << GetLastError() << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
VirtualFreeEx(hProcess, lpPathAddress, 0, MEM_RELEASE);
return false;
}
Helper::SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
std::cout << "Remote thread created with handle: " << hThread << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
WaitForSingleObject(hThread, INFINITE);
DWORD exitCode;
GetExitCodeThread(hThread, &exitCode);
std::cout << "DLL ";
Helper::SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
std::wcout << std::wstring(dllFileName.begin(), dllFileName.end());
Helper::SetConsoleColor(FOREGROUND_WHITE);
std::cout << " injected successfully into ";
Helper::SetConsoleColor(FOREGROUND_YELLOW | FOREGROUND_INTENSITY);
std::wcout << targetProcessName;
Helper::SetConsoleColor(FOREGROUND_WHITE);
std::cout << ", Return code: ";
Helper::SetConsoleColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
std::cout << exitCode << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
CloseHandle(hThread);
backup(hProcess);
std::cout << "[+] Injection completed (skeet)." << std::endl;
return true;
}
else {
Helper::SetConsoleColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
std::cout << "Allocating memory in target process..." << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
LPVOID allocatedMem = VirtualAllocEx(hProcess, NULL, absoluteDllPath.size() + 1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (!allocatedMem) {
DWORD error = GetLastError();
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Error: VirtualAllocEx failed: " << error << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return false;
}
Helper::SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
std::cout << "Memory allocated at address: " << allocatedMem << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
Helper::SetConsoleColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
std::cout << "Writing DLL path to target process..." << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
SIZE_T bytesWritten;
if (!WriteProcessMemory(hProcess, allocatedMem, absoluteDllPath.c_str(), absoluteDllPath.size() + 1, &bytesWritten)) {
DWORD error = GetLastError();
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Error: WriteProcessMemory failed: " << error << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
VirtualFreeEx(hProcess, allocatedMem, 0, MEM_RELEASE);
return false;
}
Helper::SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
std::cout << "Successfully wrote " << bytesWritten << " bytes to target process." << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
Helper::SetConsoleColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
std::cout << "Creating remote thread in target process..." << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
HANDLE hThread = CreateRemoteThread(hProcess, 0, 0, (LPTHREAD_START_ROUTINE)LoadLibraryA, allocatedMem, 0, 0);
if (!hThread) {
DWORD error = GetLastError();
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Error: CreateRemoteThread failed: " << error << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
VirtualFreeEx(hProcess, allocatedMem, 0, MEM_RELEASE);
return false;
}
Helper::SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
std::cout << "Remote thread created with handle: " << hThread << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
WaitForSingleObject(hThread, INFINITE);
DWORD exitCode;
GetExitCodeThread(hThread, &exitCode);
std::cout << "DLL ";
Helper::SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
std::wcout << std::wstring(dllFileName.begin(), dllFileName.end());
Helper::SetConsoleColor(FOREGROUND_WHITE);
std::cout << " injected successfully into ";
Helper::SetConsoleColor(FOREGROUND_YELLOW | FOREGROUND_INTENSITY);
std::wcout << targetProcessName;
Helper::SetConsoleColor(FOREGROUND_WHITE);
std::cout << ", Return code: ";
Helper::SetConsoleColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
std::cout << exitCode << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
VirtualFreeEx(hProcess, allocatedMem, 0, MEM_RELEASE);
CloseHandle(hThread);
std::cout << "[+] Injection completed." << std::endl;
return true;
}
}
bool InjectAfterModulesLoaded(HANDLE hProcess, const std::string& dllPath, const std::vector<std::wstring>& moduleNames, DWORD timeoutMs) {
if (!ModuleUtils::WaitForModules(hProcess, moduleNames, timeoutMs)) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Failed to wait for necessary modules." << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return false;
}
Helper::SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
std::cout << "All required modules found. Waiting 20 seconds..." << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
std::this_thread::sleep_for(std::chrono::seconds(20));
return InjectDll(dllPath, hProcess);
}
}
namespace HookBypass {
void LoadLib() {
if (!GetModuleHandleW(L"kernel32")) LoadLibraryW(L"kernel32");
if (!GetModuleHandleW(L"ntdll")) LoadLibraryW(L"ntdll");
if (!GetModuleHandleW(L"KernelBase")) LoadLibraryW(L"KernelBase");
}
BOOL UnhookMethod(const char* methodName, const wchar_t* dllName, PBYTE save_origin_bytes) {
HMODULE hModule = GetModuleHandleW(dllName);
if (!hModule) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::wcerr << L"Error: GetModuleHandleW failed for " << dllName << L" (" << GetLastError() << L")" << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return FALSE;
}
LPVOID oriMethodAddr = GetProcAddress(hModule, methodName);
if (!oriMethodAddr) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Error: GetProcAddress failed for " << methodName << " in " << dllName << " (" << GetLastError() << ")" << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return FALSE;
}
PBYTE originalGameBytes[6];
if (!ReadProcessMemory(hProcess, oriMethodAddr, originalGameBytes, sizeof(char) * 6, NULL)) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Error: ReadProcessMemory failed for " << methodName << " in " << dllName << ": " << GetLastError() << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return FALSE;
}
if (save_origin_bytes != nullptr) {
memcpy(save_origin_bytes, originalGameBytes, sizeof(char) * 6);
}
PBYTE originalDllBytes[6];
memcpy(originalDllBytes, oriMethodAddr, sizeof(char) * 6);
if (!WriteProcessMemory(hProcess, oriMethodAddr, originalDllBytes, sizeof(char) * 6, NULL)) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Error: WriteProcessMemory failed for " << methodName << " in " << dllName << ": " << GetLastError() << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return FALSE;
}
return TRUE;
}
BOOL RestoreOriginalHook(const char* methodName, const wchar_t* dllName, PBYTE save_origin_bytes) {
HMODULE hModule = GetModuleHandleW(dllName);
if (!hModule) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::wcerr << L"Error: GetModuleHandleW failed for " << dllName << L" (" << GetLastError() << L")" << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return FALSE;
}
LPVOID oriMethodAddr = GetProcAddress(hModule, methodName);
if (!oriMethodAddr) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Error: GetProcAddress failed for " << methodName << " in " << dllName << " (" << GetLastError() << ")" << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return FALSE;
}
if (!WriteProcessMemory(hProcess, oriMethodAddr, save_origin_bytes, sizeof(char) * 6, NULL)) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Error: WriteProcessMemory failed for " << methodName << " in " << dllName << ": " << GetLastError() << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return FALSE;
}
return TRUE;
}
enum MethodNum {
LOADLIBEXW = 1,
VIRALLOC = 2,
FREELIB = 3,
LOADLIBEXA = 4,
LOADLIBW = 5,
LOADLIBA = 6,
VIRALLOCEX = 7,
LDRLOADDLL = 10,
NTOPENFILE = 11,
VIRPROT = 12,
CREATPROW = 13,
CREATPROA = 14,
VIRPROTEX = 15,
FREELIB_ = 16,
LOADLIBEXA_ = 17,
LOADLIBEXW_ = 18,
RESUMETHREAD = 19,
};
BYTE originalGameBytess[30][6];
BOOL BypassCSGO_hook(bool disableAll = false) {
BOOL res = TRUE;
if (!disableAll)
{
res &= UnhookMethod("LoadLibraryExW", L"kernel32", originalGameBytess[LOADLIBEXW]);
res &= UnhookMethod("VirtualAlloc", L"kernel32", originalGameBytess[VIRALLOC]);
res &= UnhookMethod("FreeLibrary", L"kernel32", originalGameBytess[FREELIB]);
res &= UnhookMethod("LoadLibraryExA", L"kernel32", originalGameBytess[LOADLIBEXA]);
res &= UnhookMethod("LoadLibraryW", L"kernel32", originalGameBytess[LOADLIBW]);
res &= UnhookMethod("LoadLibraryA", L"kernel32", originalGameBytess[LOADLIBA]);
res &= UnhookMethod("VirtualAllocEx", L"kernel32", originalGameBytess[VIRALLOCEX]);
res &= UnhookMethod("LdrLoadDll", L"ntdll", originalGameBytess[LDRLOADDLL]);
res &= UnhookMethod("NtOpenFile", L"ntdll", originalGameBytess[NTOPENFILE]);
res &= UnhookMethod("VirtualProtect", L"kernel32", originalGameBytess[VIRPROT]);
res &= UnhookMethod("CreateProcessW", L"kernel32", originalGameBytess[CREATPROW]);
res &= UnhookMethod("CreateProcessA", L"kernel32", originalGameBytess[CREATPROA]);
res &= UnhookMethod("VirtualProtectEx", L"kernel32", originalGameBytess[VIRPROTEX]);
res &= UnhookMethod("FreeLibrary", L"KernelBase", originalGameBytess[FREELIB_]);
res &= UnhookMethod("LoadLibraryExA", L"KernelBase", originalGameBytess[LOADLIBEXA_]);
res &= UnhookMethod("LoadLibraryExW", L"KernelBase", originalGameBytess[LOADLIBEXW_]);
res &= UnhookMethod("ResumeThread", L"KernelBase", originalGameBytess[RESUMETHREAD]);
}
else {
res &= UnhookMethod("LoadLibraryExW", L"kernel32", nullptr);
res &= UnhookMethod("VirtualAlloc", L"kernel32", nullptr);
res &= UnhookMethod("FreeLibrary", L"kernel32", nullptr);
res &= UnhookMethod("LoadLibraryExA", L"kernel32", nullptr);
res &= UnhookMethod("LoadLibraryW", L"kernel32", nullptr);
res &= UnhookMethod("LoadLibraryA", L"kernel32", nullptr);
res &= UnhookMethod("VirtualAllocEx", L"kernel32", nullptr);
res &= UnhookMethod("LdrLoadDll", L"ntdll", nullptr);
res &= UnhookMethod("NtOpenFile", L"ntdll", nullptr);
res &= UnhookMethod("VirtualProtect", L"kernel32", nullptr);
res &= UnhookMethod("CreateProcessW", L"kernel32", nullptr);
res &= UnhookMethod("CreateProcessA", L"kernel32", nullptr);
res &= UnhookMethod("VirtualProtectEx", L"kernel32", nullptr);
res &= UnhookMethod("FreeLibrary", L"KernelBase", nullptr);
res &= UnhookMethod("LoadLibraryExA", L"KernelBase", nullptr);
res &= UnhookMethod("LoadLibraryExW", L"KernelBase", nullptr);
res &= UnhookMethod("ResumeThread", L"KernelBase", nullptr);
}
return res;
}
BOOL RestoreCSGO_hook() {
BOOL res = TRUE;
res &= RestoreOriginalHook("LoadLibraryExW", L"kernel32", originalGameBytess[LOADLIBEXW]);
res &= RestoreOriginalHook("VirtualAlloc", L"kernel32", originalGameBytess[VIRALLOC]);
res &= RestoreOriginalHook("FreeLibrary", L"kernel32", originalGameBytess[FREELIB]);
res &= RestoreOriginalHook("LoadLibraryExA", L"kernel32", originalGameBytess[LOADLIBEXA]);
res &= RestoreOriginalHook("LoadLibraryW", L"kernel32", originalGameBytess[LOADLIBW]);
res &= RestoreOriginalHook("LoadLibraryA", L"kernel32", originalGameBytess[LOADLIBA]);
res &= RestoreOriginalHook("VirtualAllocEx", L"kernel32", originalGameBytess[VIRALLOCEX]);
res &= RestoreOriginalHook("LdrLoadDll", L"ntdll", originalGameBytess[LDRLOADDLL]);
res &= RestoreOriginalHook("NtOpenFile", L"ntdll", originalGameBytess[NTOPENFILE]);
res &= RestoreOriginalHook("VirtualProtect", L"kernel32", originalGameBytess[VIRPROT]);
res &= RestoreOriginalHook("CreateProcessW", L"kernel32", originalGameBytess[CREATPROW]);
res &= RestoreOriginalHook("CreateProcessA", L"kernel32", originalGameBytess[CREATPROA]);
res &= RestoreOriginalHook("VirtualProtectEx", L"kernel32", originalGameBytess[VIRPROTEX]);
res &= RestoreOriginalHook("FreeLibrary", L"KernelBase", originalGameBytess[FREELIB_]);
res &= RestoreOriginalHook("LoadLibraryExA", L"KernelBase", originalGameBytess[LOADLIBEXA_]);
res &= RestoreOriginalHook("LoadLibraryExW", L"KernelBase", originalGameBytess[LOADLIBEXW_]);
res &= RestoreOriginalHook("ResumeThread", L"KernelBase", originalGameBytess[RESUMETHREAD]);
return res;
}
}
namespace SteamInjection {
bool InjectSteamDll(const std::string& dllPath) {
std::string cheatName = Helper::GetFileNameFromPath(dllPath);
cheatName = cheatName.substr(0, cheatName.find_last_of("."));
std::string steamDllName = "steam_" + cheatName + ".dll";
std::filesystem::path injectorPath = std::filesystem::absolute(dllPath).parent_path();
std::filesystem::path steamDllPath = injectorPath / steamDllName;
std::string steamDllPathStr = steamDllPath.string();
if (std::filesystem::exists(steamDllPath)) {
Helper::SetConsoleColor(FOREGROUND_YELLOW | FOREGROUND_INTENSITY);
std::cout << "Found Steam DLL: " << steamDllName << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
HANDLE hSteamProcess = ProcessUtils::GetProcessByName(L"steam.exe");
if (hSteamProcess) {
targetProcessName = L"steam.exe";
if (!Injection::InjectDll(steamDllPathStr, hSteamProcess)) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Failed to inject into steam.exe" << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
CloseHandle(hSteamProcess);
return false;
}
else {
Helper::SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
std::cout << "Successfully injected " << steamDllName << " into steam.exe" << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
CloseHandle(hSteamProcess);
return true;
}
}
else {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Could not find steam.exe. Skipping Steam DLL injection." << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return false;
}
}
else {
return false;
}
}
}
namespace GameSpecific {
std::vector<std::wstring> GetModulesToWaitFor(const std::wstring& processName) {
if (processName == L"cs2.exe") {
return {
L"client.dll",
L"engine2.dll",
L"server.dll"
};
}
else if (processName == L"csgo.exe") {
return {
L"client.dll",
L"engine.dll",
L"server.dll"
};
}
else if (processName == L"RustClient.exe")
{
return {
L"GameAssembly.dll",
L"UnityPlayer.dll"
};
}
else if (processName == L"gmod.exe")
{
return {
L"client.dll",
L"engine.dll"
};
}
return {};
}
bool ApplyHookBypass(const std::wstring& processName, bool disableHooks) {
if (processName == L"cs2.exe" || processName == L"csgo.exe") {
if (!HookBypass::BypassCSGO_hook(disableHooks)) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Failed to bypass VAC hooks!" << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
if (!disableHooks) {
if (!HookBypass::RestoreCSGO_hook()) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Failed to restore VAC hooks. This is VERY dangerous." << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
}
}
return false;
}
if (!disableHooks)
{
std::cout << "[+] VAC hooks bypassed." << std::endl;
}
else
{
std::cout << "[+] VAC bypass not applied as the target process is steam." << std::endl;
}
}
else
{
return true;
}
return true;
}
bool RestoreHookBypass(const std::wstring& processName) {
if (processName == L"cs2.exe" || processName == L"csgo.exe") {
if (!HookBypass::RestoreCSGO_hook()) {
Helper::SetConsoleColor(FOREGROUND_YELLOW | FOREGROUND_INTENSITY);
std::cerr << "Warning: Failed to restore VAC hooks! This may result in a VAC ban." << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return false;
}
else {
std::cout << "[+] VAC hooks restored." << std::endl;
return true;
}
}
return true;
}
}
int main(int argc, char* argv[]) {
SetConsoleTitleA("AnarchyInjector");
if (argc == 1) {
Helper::PrintBanner();
}
if (Helper::IsElevated()) {
Helper::SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
std::cout << "[+] Injector is running with administrator privileges." << std::endl;
}
Helper::SetConsoleColor(FOREGROUND_WHITE);
std::cout << std::endl;
std::string dllPath;
std::string processNameOrId;
std::string exeName = std::filesystem::path(argv[0]).filename().string();
if (argc == 2) {
dllPath = argv[1];
}
else if (argc == 3) {
processNameOrId = argv[1];
dllPath = argv[2];
}
else {
Helper::SetConsoleColor(FOREGROUND_YELLOW | FOREGROUND_INTENSITY);
std::cerr << "Usage: " << exeName << " <dll_path>\nOR: " << exeName << " <process_name_or_PID> <dll_path>" << std::endl << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
system("pause");
return 1;
}
bool injectedIntoSteam = false;
bool isSupportedGame = false;
if (!processNameOrId.empty()) {
std::wstring wProcessName(processNameOrId.begin(), processNameOrId.end());
isSupportedGame = Helper::IsGameInSupportedList(wProcessName);
}
else {
isSupportedGame = true;
}
if (isSupportedGame) {
injectedIntoSteam = SteamInjection::InjectSteamDll(dllPath);
}
if (processNameOrId.empty()) {
Helper::SetConsoleColor(FOREGROUND_YELLOW | FOREGROUND_INTENSITY);
std::cout << "Please launch the target process..." << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
bool gameFound = false;
for (int i = 0; i < 60; ++i) {
for (const auto& game : SUPPORTED_GAMES) {
hProcess = ProcessUtils::GetProcessByName(game);
if (hProcess) {
targetProcessName = game;
gameFound = true;
break;
}
}
if (gameFound)
break;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
if (!gameFound) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Timeout: Target process not launched within the waiting period." << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return 1;
}
}
else {
if (Helper::IsDigits(processNameOrId)) {
try {
DWORD processId = std::stoi(processNameOrId);
hProcess = ProcessUtils::GetProcessById(processId);
}
catch (const std::invalid_argument&) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Invalid process ID: " << processNameOrId << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return 1;
}
catch (const std::out_of_range&) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Process ID out of range: " << processNameOrId << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return 1;
}
}
else {
std::wstring wProcessName(processNameOrId.begin(), processNameOrId.end());
hProcess = ProcessUtils::GetProcessByName(wProcessName);
}
if (!hProcess) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Can not find process: " << processNameOrId << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
return 1;
}
}
Helper::SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
std::wcout << L"Process found: " << targetProcessName << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
std::string dllFileName = Helper::GetFileNameFromPath(dllPath);
bool disableBypass = injectedIntoSteam;
if (isSupportedGame && dllFileName != "skeet.dll")
{
if (!GameSpecific::ApplyHookBypass(targetProcessName, disableBypass))
{
CloseHandle(hProcess);
return 1;
}
}
if (!Injection::InjectDll(dllPath, hProcess)) {
Helper::SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
std::cerr << "Failed to inject DLL directly." << std::endl;
Helper::SetConsoleColor(FOREGROUND_WHITE);
GameSpecific::RestoreHookBypass(targetProcessName);
CloseHandle(hProcess);
return 1;
}
if ((isSupportedGame && !disableBypass) && dllFileName != "skeet.dll")
{
GameSpecific::RestoreHookBypass(targetProcessName);
}
CloseHandle(hProcess);
return 0;
}