MCPcopy Create free account
hub / github.com/dobin/RedEdr / InitHooksThread

Function InitHooksThread

RedEdrDll/dllmain.cpp:1295–1386  ·  view source on GitHub ↗

This function initializes the hooks via the MinHook library

Source from the content-addressed store, hash-verified

1293
1294// This function initializes the hooks via the MinHook library
1295DWORD WINAPI InitHooksThread(LPVOID param) {
1296 LONG error;
1297
1298 if (DetourIsHelperProcess()) {
1299 return TRUE;
1300 }
1301 char start_str[STARTSTOP_LEN] = { 0 };
1302 char stop_str[STARTSTOP_LEN] = { 0 };
1303
1304 snprintf(start_str, STARTSTOP_LEN, "{\"type\":\"dll\",\"func\":\"hooking_start\",\"pid\":%lu,\"tid\":%lu}",
1305 (DWORD)GetCurrentProcessId(), (DWORD)GetCurrentThreadId());
1306 snprintf(stop_str, STARTSTOP_LEN, "{\"type\":\"dll\",\"func\":\"hooking_finished\",\"pid\":%lu,\"tid\":%lu}",
1307 (DWORD)GetCurrentProcessId(), (DWORD)GetCurrentThreadId());
1308
1309 LOG_A(LOG_INFO, "Injected DLL Detours Main thread started on pid %lu threadid %lu",
1310 GetCurrentProcessId(), GetCurrentThreadId());
1311 InitProcessQuery();
1312 InitDllPipe();
1313 SendDllPipe(start_str);
1314
1315 // All the original methods
1316
1317 // NOTE: Do be VERY CAREFUL enabling these
1318 // Just uncommenting the variable will break the callstack
1319 // (e.g. with a nonexisting function as parameter)
1320 //Real_LdrLoadDll = (pLdrLoadDll)DetourFindFunction("ntdll.dll", "LdrLoadDll");
1321 Real_LdrGetProcedureAddress = (pLdrGetProcedureAddress)DetourFindFunction("ntdll.dll", "LdrGetProcedureAddress");
1322 Real_NtQueueApcThread = (pNtQueueApcThread)DetourFindFunction("ntdll.dll", "NtQueueApcThread");
1323 Real_NtQueueApcThreadEx = (pNtQueueApcThreadEx)DetourFindFunction("ntdll.dll", "NtQueueApcThreadEx");
1324 Real_NtCreateProcess = (pNtCreateProcess)DetourFindFunction("ntdll.dll", "NtCreateProcess");
1325 Real_NtCreateThread = (pNtCreateThread)DetourFindFunction("ntdll.dll", "NtCreateThread");
1326 Real_NtCreateThreadEx = (pNtCreateThreadEx)DetourFindFunction("ntdll.dll", "NtCreateThreadEx");
1327 Real_NtOpenProcess = (pNtOpenProcess)DetourFindFunction("ntdll.dll", "NtOpenProcess");
1328 Real_NtLoadDriver = (pNtLoadDriver)DetourFindFunction("ntdll.dll", "NtLoadDriver");
1329 Real_NtCreateNamedPipeFile = (pNtCreateNamedPipeFile)DetourFindFunction("ntdll.dll", "NtCreateNamedPipeFile");
1330 Real_NtCreateSection = (pNtCreateSection)DetourFindFunction("ntdll.dll", "NtCreateSection");
1331 Real_NtCreateProcessEx = (pNtCreateProcessEx)DetourFindFunction("ntdll.dll", "NtCreateProcessEx");
1332 Real_NtCreateEvent = (pNtCreateEvent)DetourFindFunction("ntdll.dll", "NtCreateEvent");
1333 Real_NtCreateTimer = (pNtCreateTimer)DetourFindFunction("ntdll.dll", "NtCreateTimer");
1334 Real_NtCreateTimer2 = (pNtCreateTimer2)DetourFindFunction("ntdll.dll", "NtCreateTimer2");
1335 Real_NtReadVirtualMemory = (pNtReadVirtualMemory)DetourFindFunction("ntdll.dll", "NtReadVirtualMemory");
1336 Real_NtOpenThread = (pNtOpenThread)DetourFindFunction("ntdll.dll", "NtOpenThread");
1337 Real_NtWriteVirtualMemory = (t_NtWriteVirtualMemory)DetourFindFunction("ntdll.dll", "NtWriteVirtualMemory");
1338 Real_NtMapViewOfSection = (t_NtMapViewOfSection)DetourFindFunction("ntdll.dll", "NtMapViewOfSection");
1339 Real_NtAllocateVirtualMemory = (t_NtAllocateVirtualMemory)DetourFindFunction("ntdll.dll", "NtAllocateVirtualMemory");
1340 Real_NtProtectVirtualMemory = (t_NtProtectVirtualMemory)DetourFindFunction("ntdll.dll", "NtProtectVirtualMemory");
1341 Real_NtFreeVirtualMemory = (t_NtFreeVirtualMemory)DetourFindFunction("ntdll.dll", "NtFreeVirtualMemory");
1342 Real_NtResumeThread = (pNtResumeThread)DetourFindFunction("ntdll.dll", "NtResumeThread");
1343
1344 DetourRestoreAfterWith();
1345 DetourTransactionBegin();
1346 DetourUpdateThread(GetCurrentThread());
1347
1348 // All the hooks
1349 //DetourAttach(&(PVOID&)Real_NtSetContextThread, Catch_NtSetContextThread); // broken
1350 //DetourAttach(&(PVOID&)Real_LdrLoadDll, Catch_LdrLoadDll); // broken
1351 //DetourAttach(&(PVOID&)Real_NtCreateNamedPipeFile, Catch_NtCreateNamedPipeFile); // broken for cs410 stager
1352 DetourAttach(&(PVOID&)Real_LdrGetProcedureAddress, Catch_LdrGetProcedureAddress);

Callers 1

DllMainFunction · 0.85

Calls 5

InitProcessQueryFunction · 0.85
InitDllPipeFunction · 0.85
SendDllPipeFunction · 0.85
DetourAttachFunction · 0.85
LOG_AFunction · 0.70

Tested by

no test coverage detected