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
|
#include <Security/RuntimeSecurity.hpp>
#include <Security/BretHart.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.
///////////////////////////////////////////////////////////
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();
[&](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)
{
// 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();
[&](decltype(&ExitProcess) A)
{
// Again, let's meme anyone who tries to reverse this.
uintptr_t NullPointer = *(uintptr_t *)0x00000000;
A(NullPointer);
}(oExitProcess);
}
// Call original function
oExitProcess(ExitCode);
}
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);
if(ReturnModule != LoaderModule)
// Let's meme anyone who tries to reverse this.
return []() { Protection->SecurityCallback(); 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);
if(ReturnModule != LoaderModule)
// Let's meme anyone who tries to reverse this.
return []() { Protection->SecurityCallback(); return -1; }();
// Call original function
return oWSASend(Socket, Buffer, Length, Flags);
}
// 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();
bool RuntimeSecurity::ApplyApiHooks()
{
// 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;
}
void RuntimeSecurity::PatchDebugFunctions()
{
WRAP_IF_RELEASE(VM_DOLPHIN_WHITE_START);
WRAP_IF_RELEASE(STR_ENCRYPT_START);
HMODULE Module = GetModuleHandleA("ntdll.dll");
if(!Module)
ERROR_ASSERT("[000F:00001A00] Failed to initialize. Please contact an administrator.");
// Grab exports from ntdll.dll
uintptr_t Export_DbgUiRemoteBreakin = (uintptr_t)GetProcAddress(Module, "DbgUiRemoteBreakin");
uintptr_t Export_DbgBreakPoint = (uintptr_t)GetProcAddress(Module, "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, "NtContinue");
// Our small shellcode that calls oExitProcess.
uint8_t Shellcode[] = {
0xB8, 0x00, 0x00, 0x00, 0x00, // mov eax, dword ptr[0]
0xFF, 0xE0 // jmp eax
};
std::memcpy(&Shellcode[1], oExitProcess, sizeof uint32_t);
// Ensure that the program gets closed if a debugger is attached.
uintptr_t Exports[] = {
Export_DbgUiRemoteBreakin,
Export_DbgBreakPoint,
Export_NtContinue
};
for(auto &It : Exports)
{
DWORD OldProtection;
if(!VirtualProtect((void *)It, sizeof Shellcode, PAGE_EXECUTE_READWRITE, &OldProtection))
ERROR_ASSERT("[000F:00001A00] Failed to initialize. Please contact an administrator.");
// Patch to __asm { jmp oExitProcess; };
*(uint8_t *)It = 0xE9;
*(uint32_t *)(It + 1) = (uintptr_t)oExitProcess;
VirtualProtect((void *)It, sizeof Shellcode, OldProtection, &OldProtection);
}
WRAP_IF_RELEASE(STR_ENCRYPT_END);
WRAP_IF_RELEASE(VM_DOLPHIN_WHITE_END);
}
void RuntimeSecurity::DispatchSecurityThreads()
{
WRAP_IF_RELEASE(MUTATE_START);
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::CheckForThreads, this); TamperThread.detach();
WRAP_IF_RELEASE(MUTATE_END);
}
// The following functions are only called internally.
// The reason these are seperate is in order to maintain
// code readability.
///////////////////////////////////////////////////////////
void RuntimeSecurity::CheckForVirtualMachine()
{
}
void RuntimeSecurity::CheckForDebugger()
{
// 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();
}
void RuntimeSecurity::CheckForDrivers()
{
}
void RuntimeSecurity::CheckForThreads()
{
}
// 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
PatchDebugFunctions();
return true;
}
HardwareIdentifier RuntimeSecurity::GetHardwareId()
{
return HardwareIdentifier{};
}
MEMORY_BASIC_INFORMATION RuntimeSecurity::QueryMemory(void *Address)
{
MEMORY_BASIC_INFORMATION Result;
VirtualQuery(Address, &Result, sizeof MEMORY_BASIC_INFORMATION);
return Result;
}
__forceinline void RuntimeSecurity::SecurityCallback()
{
// TODO: Implement something that bans the user or notifies
// you if someone tampers with the loader.
// For convenience sake, I'll just put a small .WAV file of
// Bret Hart screaming in here for now :^)
PlaySoundA((char *)Resource::BretWAV, NULL, SND_MEMORY);
oExitProcess(0);
}
}
|