summaryrefslogtreecommitdiff
path: root/csgo-loader/csgo-client/Security/RuntimeSecurity.cpp
blob: 7f528e3585eaeee22fa6a1a16854a58bf3ed9b95 (plain)
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
#include <Security/RuntimeSecurity.hpp>
#include <Security/SyscallManager.hpp>
#include <Security/FnvHash.hpp>

#include <UserExperience/UserInterface.hpp>
#include <Networking/TCPClient.hpp>

// Global accessor to security instance.
Security::RuntimeSecurityPtr Protection = std::make_unique<Security::RuntimeSecurity>();


namespace Security
{
	// Hooked functions.
	///////////////////////////////////////////////////////////

#pragma optimize("", off)

	decltype(&OpenProcess) oOpenProcess;
	HANDLE __stdcall Hooked_OpenProcess(DWORD AccessLevel, bool Inherit, DWORD ProcessId)
	{
		// Determine where the return address of the function actually points.
		void *Address                  = _ReturnAddress();
		MEMORY_BASIC_INFORMATION Query = Protection->QueryMemory(Address);

		// If the return address points outside of the loader module,
		// fail the function.
		HMODULE ReturnModule = (HMODULE)Query.AllocationBase;
		HMODULE LoaderModule = GetModuleHandleA(NULL);

		if(ReturnModule != LoaderModule)
		{
			Protection->SecurityCallback(STR("Malicious activity [Tampering]."));

			[&](decltype(&OpenProcess) A)
			{
				// Again, let's meme anyone who tries to reverse this.
				uintptr_t NullPointer = *(uintptr_t *)0x00000000;
				A(NullPointer, NullPointer, NullPointer);
			}(OpenProcess);
		}

		// Call original function
		return oOpenProcess(AccessLevel, Inherit, ProcessId);
	}

	decltype(&ExitProcess) oExitProcess;
	void __stdcall Hooked_ExitProcess(DWORD ExitCode)
	{
		WRAP_IF_DEBUG(oExitProcess(ExitCode));

		WRAP_IF_RELEASE(
			[&](decltype(&ExitProcess) A)
			{
				// Again, let's meme anyone who tries to reverse this.
				uintptr_t NullPointer = *(uintptr_t *)0x00000000;
				A(NullPointer);
			}(oExitProcess);
		);
	}

	decltype(&recv) oWSARecv;
	int __stdcall Hooked_WSARecv(SOCKET Socket, char *Buffer, int Length, int Flags)
	{

		// Determine where the return address of the function actually points.
		void *Address = _ReturnAddress();
		MEMORY_BASIC_INFORMATION Query = Protection->QueryMemory(Address);

		// If the return address points outside of the loader module,
		// fail the function.
		HMODULE ReturnModule = (HMODULE)Query.AllocationBase;
		HMODULE LoaderModule = GetModuleHandleA(NULL);
		
		// Let's meme anyone who tries to reverse this.
		if(ReturnModule != LoaderModule)
		{		
			return []() { Protection->SecurityCallback(STR("Malicious activity [Tampering].")); return -1; }();
		}

		// Call original function
		return oWSARecv(Socket, Buffer, Length, Flags);

	}

	decltype(&send) oWSASend;
	int __stdcall Hooked_WSASend(SOCKET Socket, char *Buffer, int Length, int Flags)
	{

		// Determine where the return address of the function actually points.
		void *Address                  = _ReturnAddress();
		MEMORY_BASIC_INFORMATION Query = Protection->QueryMemory(Address);

		// If the return address points outside of the loader module,
		// fail the function.
		HMODULE ReturnModule = (HMODULE)Query.AllocationBase;
		HMODULE LoaderModule = GetModuleHandleA(NULL);

		// Let's meme anyone who tries to reverse this.
		if(ReturnModule != LoaderModule)
		{
			return []() { Protection->SecurityCallback(STR("Malicious activity [Tampering].")); return -1; }();
		}

		// Call original function
		return oWSASend(Socket, Buffer, Length, Flags);
	}

#pragma optimize("", on)

	// The following functions are only called internally.
	///////////////////////////////////////////////////////////

	// Sick macros, retard.
#define CreateMinHook()		MH_STATUS Status; Status = MH_Initialize();
#define CheckStatus()			if(Status != MH_OK) { return false; }
#define SafeCallTo(Function)	Status = Function; CheckStatus();

#pragma optimize("", off)
	
	bool RuntimeSecurity::ApplyApiHooks()
	{
		VMProtectBeginMutation("ApplyHooks");

		// Make sure that MinHook is initialized properly.
		CreateMinHook();
		CheckStatus();

		// Apply any hooks.
		SafeCallTo(MH_CreateHook(&OpenProcess, Hooked_OpenProcess, (void **)&oOpenProcess));
		SafeCallTo(MH_EnableHook(&OpenProcess));
		
		SafeCallTo(MH_CreateHook(&ExitProcess, Hooked_ExitProcess, (void **)&oExitProcess));
		SafeCallTo(MH_EnableHook(&ExitProcess));

		SafeCallTo(MH_CreateHook(&recv, Hooked_WSARecv, (void **)&oWSARecv));
		SafeCallTo(MH_EnableHook(&recv));

		SafeCallTo(MH_CreateHook(&send, Hooked_WSASend, (void **)&oWSASend));
		SafeCallTo(MH_EnableHook(&send));

		return true;

		VMProtectEnd();
	}

#pragma optimize("", on)

	void RuntimeSecurity::PatchDebugFunctions()
	{
		HMODULE Module = GetModuleHandleA("ntdll.dll");

		if(!Module)
			ERROR_ASSERT(STR("[000F:00001A00] Failed to initialize. Please contact an administrator."));

		// Grab exports from ntdll.dll
		uintptr_t Export_DbgUiRemoteBreakin = (uintptr_t)GetProcAddress(Module, STR("DbgUiRemoteBreakin"));
		uintptr_t Export_DbgBreakPoint      = (uintptr_t)GetProcAddress(Module, STR("DbgBreakPoint"));
		
		// Most plugins for OllyDBG / IDA only fix DbgUiRemoteBreakin/DbgBreakPoint,
		// however, NtContinue is never touched although it is used.
		// This should prevent any such plugins from effectively attaching the debugger.
		// NOTE: This does not work on x64dbg for whatever reason..
		uintptr_t Export_NtContinue         = (uintptr_t)GetProcAddress(Module, STR("NtContinue"));
		
		// Ensure that the program gets closed if a debugger is attached.
		uintptr_t Exports[] = {
			Export_DbgUiRemoteBreakin,
			Export_DbgBreakPoint,
			Export_NtContinue // This causes a lot of crashes ATM while debugging, leave this out till release.
		};

		for(auto &It : Exports)
		{
			DWORD OldProtection;
			if(!VirtualProtect((void *)It, sizeof uintptr_t + 1, PAGE_EXECUTE_READWRITE, &OldProtection))
				ERROR_ASSERT(STR("[000F:00001A00] Failed to initialize. Please contact an administrator."));

			// Patch to __asm { jmp oExitProcess; };
			*(uint8_t *)It         = 0xE9;
			*(uintptr_t *)(It + 1) = (uintptr_t)oExitProcess;

			VirtualProtect((void *)It, sizeof uintptr_t + 1, OldProtection, &OldProtection);
		}
	}

	void RuntimeSecurity::DispatchSecurityThreads()
	{
		VMProtectBeginMutation("DispatchThreads");

		std::thread DebugThread	(&RuntimeSecurity::CheckForDebugger,		this); DebugThread.detach();
		std::thread VMThread	(&RuntimeSecurity::CheckForVirtualMachine,	this); VMThread.detach();
		std::thread DriverThread(&RuntimeSecurity::CheckForDrivers,			this); DriverThread.detach();
		std::thread TamperThread(&RuntimeSecurity::CheckForTampering,		this); TamperThread.detach();
	
		VMProtectEnd();
	}

	// The following functions are only called internally.
	// The reason these are seperate is in order to maintain
	// code readability.
	///////////////////////////////////////////////////////////

#pragma optimize("", off)

	void RuntimeSecurity::CheckForVirtualMachine()
	{
		VMProtectBeginVirtualization("VMThread");

		for(;;)
		{
			if(VMProtectIsVirtualMachinePresent())
				SecurityCallback(STR("Malicious activity [Virtualized environment]."));

			// Don't put too much stress on the CPU.
			Sleep(1);
		}

		VMProtectEnd();
	}

	void RuntimeSecurity::CheckForDebugger()
	{
		VMProtectBeginVirtualization("DebuggerThread");

		for(;;)
		{
			// Read the PEB from the TIB.
			// Offset for x86 is 0x30 ; mov ..., dword ptr fs:[0x30]
			// Offset for x64 is 0x60 ; mov ..., qword ptr gs:[0x60]
			PEB *ProcessEnvBlock = (PEB *)__readgsqword(0x60);

			if(ProcessEnvBlock->BeingDebugged)
				SecurityCallback(STR("Malicious activity [Debugging attempt]."));

			// TODO: Check for x64dbg window?
			using WindowParams = std::pair<const char *, const char *>;
			static std::vector<WindowParams> BlackListedWindows = {
				{STR("ID"),				STR("Immunity")}, // Immunity Debugger
				{STR("Qt5QWindowIcon"),	STR("x64dbg")},	// x64dbg
				{STR("Qt5QWindowIcon"),	STR("x32dbg")},	// x32dbg
				{STR("Qt5QWindowIcon"),	STR("The Wireshark Network Analyzer")}, // x32dbg
				{STR("OLLYDBG"),		STR("OllyDbg")}, // OllyDbg
				{nullptr, STR("Progress Telerik Fiddler Web Debugger")}, // Telerik Fiddler
			};

			for(auto &It : BlackListedWindows)
			{
				// size_t Index = std::distance(...);

				if(FindWindowA(It.first, It.second))
					SecurityCallback(STR("Malicious activity [Debugging attempt]."));
			}

			// Don't put too much stress on the CPU.
			Sleep(1);
		}

		VMProtectEnd();
	}

	void RuntimeSecurity::CheckForDrivers()
	{
		// TODO: Check for disallowed drivers
		for(;;)
		{
			static const char *BlackListedDrivers[] = {
				STR("Sbie"),	// Sandboxie
				STR("NPF"),		// WireShark / WinPCAP
				STR("acker"),	// Process Hacker
				STR("CEDRI"),	// Cheat Engine
				//STR("VBox")	// VirtualBox
			};

			static const char *BlackListReasons[] = {
				STR("Please uninstall Sandboxie."),
				STR("Please uninstall WireShark."),
				STR("Please close Process Hacker."),
				STR("Please close Cheat Engine."),
				STR("Please uninstall VirtualBox.")
			};

			uint16_t Length = sizeof BlackListedDrivers / sizeof(BlackListedDrivers[0]);

			void *DriverList[1024];
			DWORD Needed;
			
			if(K32EnumDeviceDrivers(DriverList, sizeof DriverList, &Needed))
			{
				if(Needed > sizeof DriverList)
				{
					ERROR_ASSERT(
						STR("[00DF:00001CFF] A security thread has failed. Contact an administrator.")
					);
				}

				char	 DriverName[1024];
				uint32_t DriverCount = Needed / sizeof DriverList[0];

				for(size_t n{}; n < DriverCount; ++n)
				{
					if(K32GetDeviceDriverBaseNameA(DriverList[n], DriverName, sizeof DriverName / sizeof DriverList[0]))
					{
						for(size_t j{}; j < Length; ++j)
						{
							if(strstr(DriverName, BlackListedDrivers[j]))
								SecurityCallback(BlackListReasons[j]);
						}
					}
				}
			}

			// Don't put too much stress on the CPU.
			Sleep(1);
		}
	}

	void RuntimeSecurity::CheckForTampering()
	{
		for(;;)
		{
			if(!VMProtectIsProtected())
				SecurityCallback(STR("Malicious activity [Tampering]."));

			if(!VMProtectIsValidImageCRC())
				SecurityCallback(STR("Malicious activity [Tampering]."));

			// Don't put too much stress on the CPU.
			Sleep(1);
		}
	}

#pragma optimize("", on)

	// The following functions are exposed publicly.
	///////////////////////////////////////////////////////////

	bool RuntimeSecurity::Start()
	{
		// If hooking API functions fails, exit the program.
		if(!ApplyApiHooks())
			return false;

		// Dispatch threads before patching NtContinue & co.
		DispatchSecurityThreads();

		// Patch DbgUiRemoteBreakin, DbgBreakPoint, NtContinue
		// This also fucks up detours for some reason... only extra protection :-)
		PatchDebugFunctions();

		return true;
	}

	constexpr uintptr_t KUSER_SHARED_DATA = 0x7FFE0000;

	__forceinline uint64_t get_hdd_hash() {
		STORAGE_PROPERTY_QUERY		query{ };
		STORAGE_DESCRIPTOR_HEADER	desc_header{ };
		STORAGE_DEVICE_DESCRIPTOR*	device_descriptor{ };
		HANDLE						device;
		DWORD						bytes_returned;
		uint8_t*					out_buffer;

		const wchar_t* device_path = L"\\??\\PhysicalDrive0";
		device = CreateFileA("\\\\.\\PhysicalDrive0", 0, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
		if(!device) return uint64_t{ };

		query.PropertyId = StorageDeviceProperty;
		query.QueryType = PropertyStandardQuery;

		if(!DeviceIoControl(device, IOCTL_STORAGE_QUERY_PROPERTY,
			&query, sizeof(STORAGE_PROPERTY_QUERY),
			&desc_header, sizeof(STORAGE_DESCRIPTOR_HEADER),
			&bytes_returned, 0)) {
			return uint64_t{ };
		}

		out_buffer = new uint8_t[desc_header.Size];
		memset(out_buffer, 0, desc_header.Size);

		if(!DeviceIoControl(device, IOCTL_STORAGE_QUERY_PROPERTY,
			&query, sizeof(STORAGE_PROPERTY_QUERY),
			out_buffer, desc_header.Size,
			&bytes_returned, 0)) {
			delete[] out_buffer;
			return uint64_t{ };
		}

		device_descriptor = (STORAGE_DEVICE_DESCRIPTOR*)out_buffer;
		if(device_descriptor->SerialNumberOffset) {
			std::string serial_num = reinterpret_cast<const char*>(
				out_buffer + device_descriptor->SerialNumberOffset);

			delete[] out_buffer;
			CloseHandle(device);
			return fnv::hash_runtime(serial_num.c_str());
		}

		return 0;
	}

	HardwareIdentifier RuntimeSecurity::GetHardwareId()
	{
		VMProtectBeginMutation("HardwareIdentifier");

		HardwareIdentifier Identifier{};

		// CPU information
		Identifier.m_CpuCount        = *(uint32_t *)(KUSER_SHARED_DATA + 0x3C0);
		Identifier.m_CpuArchitecture = *(uint16_t *)(KUSER_SHARED_DATA + 0x26A);

		// HDD serial number
		Identifier.m_HardDiskSerialHash = get_hdd_hash();

		// Safe-mode
		Identifier.m_SpecialMode[0] = *(uint8_t *)(KUSER_SHARED_DATA + 0x2EC);

		// Test-signing mode
		static auto ZwQuerySystemInformation = Syscalls->Find<long(__stdcall *)(uint32_t, void *, uint32_t, uint32_t *)>(FNV("ZwQuerySystemInformation"));

		// 0x02 CODEINTEGRITY_OPTION_TESTSIGN
		// 0x20	CODEINTEGRITY_OPTION_TEST_BUILD
		// 0x80	CODEINTEGRITY_OPTION_DEBUGMODE_ENABLED

		CodeIntegrityInformation Info{ sizeof CodeIntegrityInformation };
		NTSTATUS Status = ZwQuerySystemInformation(0x67, &Info, sizeof Info, nullptr);

		if(NT_ERROR(Status))
			ERROR_ASSERT(STR("[00CF:%08x] Critical execution error."), Status);

		if(Info.m_Options & 0x02)
			Identifier.m_SpecialMode[1] = true;

		if(Info.m_Options & 0x20)
			Identifier.m_SpecialMode[2] = true;

		if(Info.m_Options & 0x40)
			Identifier.m_SpecialMode[3] = true;

		VMProtectEnd();

		return Identifier;
	}

#pragma optimize("", off)

	__declspec(noinline) MEMORY_BASIC_INFORMATION RuntimeSecurity::QueryMemory(void *Address)
	{
		MEMORY_BASIC_INFORMATION Result{};
	
		// VirtualQuery is also referenced in MinHook lib, will be a pain to find anyway
		// especially if I have VMP encrypt all this shit.
		bool Success = VirtualQuery(Address, &Result, sizeof Result);

		if(!Success)
		{
			char		ReasonParameter[64];
			uint32_t	Status = GetLastError();

			sprintf_s(ReasonParameter, STR("[00DF:%08x] There was an error with accessing a process."), Status);
			ERROR_ASSERT(ReasonParameter);
		}

		return Result;
	}

	void RuntimeSecurity::SecurityCallback(const char *Reason)
	{
		static bool TriggeredCallback = false;

		if(!TriggeredCallback) 
		{
			// You can use the reason parameters to debug the security in case
			// something weird starts going on with it.
			std::ofstream File("loader.err");
			File.write(Reason, strlen(Reason));
			File.close();

			// The process will straight up die on Release mode.
			// Compile with FuckMSVC to debug this.
			WRAP_IF_RELEASE(
				ExitProcess(rand() % RAND_MAX);
			);

			TriggeredCallback = true;
		}
	}

#pragma optimize("", on)
}