-
Notifications
You must be signed in to change notification settings - Fork 0
/
dllmain.cpp
345 lines (305 loc) · 11.4 KB
/
dllmain.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
#include "minhook\include\MinHook.h"
#if defined _M_X64
#pragma comment(lib, "libMinHook.x64.lib")
#elif defined _M_IX86
#pragma comment(lib, "libMinHook.x86.lib")
#endif
#include "framework.h"
/* Hooking and extending CreateThread's capabilities to all logical processors on Windows system using SetThreadAffinity function */
typedef HANDLE (WINAPI *HCreateThread)(
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_ SIZE_T dwStackSize,
_In_ LPTHREAD_START_ROUTINE lpStartAddress,
_In_opt_ __drv_aliasesMem LPVOID lpParameter,
_In_ DWORD dwCreationFlags,
_Out_opt_ LPDWORD lpThreadId
);
HCreateThread fp_CreateThread = NULL;
typedef HANDLE(WINAPI* HCreateRemoteThread)(
_In_ HANDLE hProcess,
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_ SIZE_T dwStackSize,
_In_ LPTHREAD_START_ROUTINE lpStartAddress,
_In_opt_ LPVOID lpParameter,
_In_ DWORD dwCreationFlags,
_Out_opt_ LPDWORD lpThreadId
);
HCreateRemoteThread fp_CreateRemoteThread = NULL;
typedef HANDLE (WINAPI* HCreateRemoteThreadEx)(
_In_ HANDLE hProcess,
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_ SIZE_T dwStackSize,
_In_ LPTHREAD_START_ROUTINE lpStartAddress,
_In_opt_ LPVOID lpParameter,
_In_ DWORD dwCreationFlags,
_In_opt_ LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList,
_Out_opt_ LPDWORD lpThreadId
);
HCreateRemoteThreadEx fp_CreateRemoteThreadEx = NULL;
/* Hooking and extending GetSystemInfo */
typedef VOID (WINAPI* HGetSystemInfo)(
_Out_ LPSYSTEM_INFO lpSystemInfo
);
HGetSystemInfo fp_GetSystemInfo = NULL;
typedef DWORD (WINAPI* HGetActiveProcessorCount)(
_In_ WORD GroupNumber
);
HGetActiveProcessorCount fp_GetActiveProcessorCount = NULL;
typedef DWORD (WINAPI* HGetEnvironmentVariableA)(
_In_opt_ LPCSTR lpName,
_Out_writes_to_opt_(nSize, return + 1) LPSTR lpBuffer,
_In_ DWORD nSize
);
HGetEnvironmentVariableA fp_GetEnvironmentVariableA = NULL;
typedef DWORD (WINAPI* HGetEnvironmentVariableW)(
_In_opt_ LPCWSTR lpName,
_Out_writes_to_opt_(nSize, return + 1) LPWSTR lpBuffer,
_In_ DWORD nSize
);
HGetEnvironmentVariableW fp_GetEnvironmentVariableW = NULL;
typedef LPVOID (WINAPI* HVirtualAllocEx)(
_In_ HANDLE hProcess,
_In_opt_ LPVOID lpAddress,
_In_ SIZE_T dwSize,
_In_ DWORD flAllocationType,
_In_ DWORD flProtect
);
HVirtualAllocEx fp_VirtualAllocEx = NULL;
typedef BOOL (WINAPI* HVirtualFreeEx)(
_In_ HANDLE hProcess,
_Pre_notnull_ _When_(dwFreeType == MEM_DECOMMIT, _Post_invalid_) _When_(dwFreeType == MEM_RELEASE, _Post_ptr_invalid_) LPVOID lpAddress,
_In_ SIZE_T dwSize,
_In_ DWORD dwFreeType
);
HVirtualFreeEx fp_VirtualFreeEx = NULL;
typedef unsigned int (*Hhardware_concurrency)();
Hhardware_concurrency fp_hardware_concurrency = NULL;
/* Detours functions */
HANDLE WINAPI DetourCreateThread(
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_ SIZE_T dwStackSize,
_In_ LPTHREAD_START_ROUTINE lpStartAddress,
_In_opt_ __drv_aliasesMem LPVOID lpParameter,
_In_ DWORD dwCreationFlags,
_Out_opt_ LPDWORD lpThreadId
)
{
auto thread = fp_CreateThread(lpThreadAttributes, dwStackSize, lpStartAddress, lpParameter, dwCreationFlags /* | INHERIT_PARENT_AFFINITY */, lpThreadId);
setThreadAffinityAllGroupCores(thread);
setThreadParallelAllNUMAGroups(thread);
return thread;
}
HANDLE WINAPI DetourCreateRemoteThread(
_In_ HANDLE hProcess,
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_ SIZE_T dwStackSize,
_In_ LPTHREAD_START_ROUTINE lpStartAddress,
_In_opt_ LPVOID lpParameter,
_In_ DWORD dwCreationFlags,
_Out_opt_ LPDWORD lpThreadId
)
{
auto thread = fp_CreateRemoteThread(hProcess, lpThreadAttributes, dwStackSize, lpStartAddress, lpParameter, dwCreationFlags /* | INHERIT_PARENT_AFFINITY */, lpThreadId);
setThreadAffinityAllGroupCores(thread);
setThreadParallelAllNUMAGroups(thread);
return thread;
}
HANDLE WINAPI DetourCreateRemoteThreadEx(
_In_ HANDLE hProcess,
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_ SIZE_T dwStackSize,
_In_ LPTHREAD_START_ROUTINE lpStartAddress,
_In_opt_ LPVOID lpParameter,
_In_ DWORD dwCreationFlags,
_In_opt_ LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList,
_Out_opt_ LPDWORD lpThreadId
)
{
//auto* pAttribs = lpAttributeList;
//DWORD node = __g_ProcSelectedForThread;
//UpdateProcThreadAttribute(pAttribs, 0, PROC_THREAD_ATTRIBUTE_PREFERRED_NODE, &node, sizeof(DWORD), NULL, NULL);
auto thread = fp_CreateRemoteThreadEx(hProcess, lpThreadAttributes, dwStackSize, lpStartAddress, lpParameter, dwCreationFlags /* | INHERIT_PARENT_AFFINITY */, lpAttributeList, lpThreadId);
setThreadAffinityAllGroupCores(thread);
setThreadParallelAllNUMAGroups(thread);
return thread;
}
VOID WINAPI DetourGetSystemInfo(
_Out_ LPSYSTEM_INFO lpSystemInfo
)
{
// MessageBoxA(0, "DetourGetSystemInfo", "Some Title", MB_ICONERROR | MB_OK);
lpSystemInfo->dwNumberOfProcessors = __g_ProcLogicalThreadCount;
return fp_GetSystemInfo(lpSystemInfo);
}
DWORD WINAPI DetourGetActiveProcessorCount(
_In_ WORD GroupNumber
)
{
GroupNumber = ALL_PROCESSOR_GROUPS;
return fp_GetActiveProcessorCount(GroupNumber);
}
DWORD WINAPI DetourGetEnvironmentVariableA(
_In_opt_ LPCSTR lpName,
_Out_writes_to_opt_(nSize, return +1) LPSTR lpBuffer,
_In_ DWORD nSize
)
{
if (lpName == "NUMBER_OF_PROCESSORS")
_itoa(__g_ProcLogicalThreadCount, lpBuffer, 10);
return fp_GetEnvironmentVariableA(lpName, lpBuffer, nSize);
}
DWORD WINAPI DetourGetEnvironmentVariableW(
_In_opt_ LPCWSTR lpName,
_Out_writes_to_opt_(nSize, return +1) LPWSTR lpBuffer,
_In_ DWORD nSize
)
{
if (lpName == L"NUMBER_OF_PROCESSORS")
_itow(__g_ProcLogicalThreadCount, lpBuffer, 10);
return fp_GetEnvironmentVariableW(lpName, lpBuffer, nSize);
}
LPVOID WINAPI DetourVirtualAllocEx(
_In_ HANDLE hProcess,
_In_opt_ LPVOID lpAddress,
_In_ SIZE_T dwSize,
_In_ DWORD flAllocationType,
_In_ DWORD flProtect
)
{
return virtualAllocNUMA(hProcess, lpAddress, dwSize);
}
BOOL WINAPI DetourVirtualFreeEx(
_In_ HANDLE hProcess,
_Pre_notnull_ _When_(dwFreeType == MEM_DECOMMIT, _Post_invalid_) _When_(dwFreeType == MEM_RELEASE, _Post_ptr_invalid_) LPVOID lpAddress,
_In_ SIZE_T dwSize,
_In_ DWORD dwFreeType
)
{
return virtualFreeNUMA();
}
unsigned int DetourHardware_concurrency()
{
// MessageBoxA(0, "DetourHardware_concurrency", "Some Title", MB_ICONERROR | MB_OK);
//return fp_hardware_concurrency();
return __g_ProcLogicalThreadCount;
}
BOOL InitCreateEnableHooks()
{
// WINAPI hook
if (MH_CreateHookApiEx(L"kernel32", "CreateThread", &DetourCreateThread, reinterpret_cast<LPVOID*>(&fp_CreateThread), NULL) != MH_OK)
{
MessageBoxW(NULL, L"Failed to create CreateThread hook", L"NUMAYei", MB_OK);
return TRUE;
}
if (MH_CreateHookApiEx(L"kernel32", "CreateRemoteThread", &DetourCreateRemoteThread, reinterpret_cast<LPVOID*>(&fp_CreateRemoteThread), NULL) != MH_OK)
{
MessageBoxW(NULL, L"Failed to create CreateRemoteThread hook", L"NUMAYei", MB_OK);
return TRUE;
}
/* dont uncomment - this broken ideal sheduler
if (MH_CreateHookApiEx(L"kernel32", "CreateRemoteThreadEx", &DetourCreateRemoteThreadEx, reinterpret_cast<LPVOID*>(&fp_CreateRemoteThreadEx), NULL) != MH_OK)
{
MessageBoxW(NULL, L"Failed to create CreateRemoteThreadEx hook", L"NUMAYei", MB_OK);
return TRUE;
}
*/
if (MH_CreateHookApiEx(L"kernel32", "GetSystemInfo", &DetourGetSystemInfo, reinterpret_cast<LPVOID*>(&fp_GetSystemInfo), NULL) != MH_OK)
{
MessageBoxW(NULL, L"Failed to create GetSystemInfo hook", L"NUMAYei", MB_OK);
return TRUE;
}
if (MH_CreateHookApiEx(L"kernel32", "GetActiveProcessorCount", &DetourGetActiveProcessorCount, reinterpret_cast<LPVOID*>(&fp_GetActiveProcessorCount), NULL) != MH_OK)
{
MessageBoxW(NULL, L"Failed to create GetActiveProcessorCount hook", L"NUMAYei", MB_OK);
return TRUE;
}
if (MH_CreateHookApiEx(L"kernel32", "GetEnvironmentVariableA", &DetourGetEnvironmentVariableA, reinterpret_cast<LPVOID*>(&fp_GetEnvironmentVariableA), NULL) != MH_OK)
{
MessageBoxW(NULL, L"Failed to create GetEnvironmentVariableA hook", L"NUMAYei", MB_OK);
return TRUE;
}
if (MH_CreateHookApiEx(L"kernel32", "GetEnvironmentVariableW", &DetourGetEnvironmentVariableW, reinterpret_cast<LPVOID*>(&fp_GetEnvironmentVariableW), NULL) != MH_OK)
{
MessageBoxW(NULL, L"Failed to create GetEnvironmentVariableW hook", L"NUMAYei", MB_OK);
return TRUE;
}
if (MH_CreateHookApiEx(L"kernel32", "VirtualAllocEx", &DetourVirtualAllocEx, reinterpret_cast<LPVOID*>(&fp_VirtualAllocEx), NULL) != MH_OK)
{
MessageBoxW(NULL, L"Failed to create VirtualAllocEx hook", L"NUMAYei", MB_OK);
return TRUE;
}
if (MH_CreateHookApiEx(L"kernel32", "VirtualFreeEx", &DetourVirtualFreeEx, reinterpret_cast<LPVOID*>(&fp_VirtualFreeEx), NULL) != MH_OK)
{
MessageBoxW(NULL, L"Failed to create VirtualFreeEx hook", L"NUMAYei", MB_OK);
return TRUE;
}
// hook which overrides std::thread::hardware::concurrency
if (MH_CreateHook(&std::thread::hardware_concurrency, &DetourHardware_concurrency,
reinterpret_cast<LPVOID*>(&fp_hardware_concurrency)) != MH_OK)
{
MessageBoxW(NULL, L"Failed to create std::thread::hardware_concurrency hook", L"NUMAYei", MB_OK);
return TRUE;
}
if (MH_EnableHook(&CreateThread) != MH_OK)
{
MessageBoxW(NULL, L"Failed to enable hook CreateThread", L"NUMAYei", MB_OK);
return TRUE;
}
if (MH_EnableHook(&CreateRemoteThread) != MH_OK)
{
MessageBoxW(NULL, L"Failed to enable hook CreateRemoteThread", L"NUMAYei", MB_OK);
return TRUE;
}
/*
if (MH_EnableHook(&CreateRemoteThreadEx) != MH_OK)
{
MessageBoxW(NULL, L"Failed to enable hook CreateRemoteThreadEx", L"NUMAYei", MB_OK);
return TRUE;
}
*/
if (MH_EnableHook(&GetSystemInfo) != MH_OK)
{
MessageBoxW(NULL, L"Failed to enable hook GetSystemInfo", L"NUMAYei", MB_OK);
return TRUE;
}
if (MH_EnableHook(&GetActiveProcessorCount) != MH_OK)
{
MessageBoxW(NULL, L"Failed to enable hook GetActiveProcessorCount", L"NUMAYei", MB_OK);
return TRUE;
}
if (MH_EnableHook(&std::thread::hardware_concurrency) != MH_OK)
{
MessageBoxW(NULL, L"Failed to enable hook std::thread::hardware_concurrency", L"NUMAYei", MB_OK);
return TRUE;
}
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
if (!IsNUMA())
{
MessageBoxW(NULL, L"Failed to DLL injection: your system configuration is not multiprocessor NUMA (Hint: maybe you forgot turn on NUMA in BIOS/UEFI)", L"NUMAYei", MB_OK);
return FALSE;
}
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
if (MH_Initialize() != MH_OK) {
MessageBoxW(NULL, L"Failed to initialize MinHook", L"HideTS", MB_OK);
return TRUE;
}
__g_ProcGroupCount = calcLogicalGroups() + 1;
__g_ProcLogicalThreadCount = GetLogicalThreadCount();
InitCreateEnableHooks();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
if (MH_Uninitialize() != MH_OK)
{
return TRUE;
}
break;
}
return TRUE;
}