| 10 | //#define DEBUG_COMMENTS |
| 11 | |
| 12 | HMODULE DllInjection::dllInjection(HANDLE hProcess, const WCHAR * filename) |
| 13 | { |
| 14 | LPVOID remoteMemory = 0; |
| 15 | SIZE_T memorySize = 0; |
| 16 | HANDLE hThread = 0; |
| 17 | HMODULE hModule = 0; |
| 18 | |
| 19 | memorySize = (wcslen(filename) + 1) * sizeof(WCHAR); |
| 20 | |
| 21 | if (memorySize < 7) |
| 22 | { |
| 23 | #ifdef DEBUG_COMMENTS |
| 24 | Scylla::debugLog.log(L"dllInjection :: memorySize invalid"); |
| 25 | #endif |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | remoteMemory = VirtualAllocEx(hProcess, NULL, memorySize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); |
| 30 | |
| 31 | if (remoteMemory == 0) |
| 32 | { |
| 33 | #ifdef DEBUG_COMMENTS |
| 34 | Scylla::debugLog.log(L"dllInjection :: VirtualAllocEx failed 0x%X", GetLastError()); |
| 35 | #endif |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | if (WriteProcessMemory(hProcess, remoteMemory, filename, memorySize, &memorySize)) |
| 40 | { |
| 41 | hThread = startRemoteThread(hProcess,LoadLibraryW,remoteMemory); |
| 42 | |
| 43 | if (hThread) |
| 44 | { |
| 45 | WaitForSingleObject(hThread, INFINITE); |
| 46 | |
| 47 | #ifdef _WIN64 |
| 48 | |
| 49 | hModule = getModuleHandleByFilename(hProcess, filename); |
| 50 | |
| 51 | #else |
| 52 | //returns only 32 bit values -> design bug by microsoft |
| 53 | if (!GetExitCodeThread(hThread, (LPDWORD) &hModule)) |
| 54 | { |
| 55 | #ifdef DEBUG_COMMENTS |
| 56 | Scylla::debugLog.log(L"dllInjection :: GetExitCodeThread failed 0x%X", GetLastError()); |
| 57 | #endif |
| 58 | hModule = 0; |
| 59 | } |
| 60 | #endif |
| 61 | |
| 62 | CloseHandle(hThread); |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | #ifdef DEBUG_COMMENTS |
| 67 | Scylla::debugLog.log(L"dllInjection :: CreateRemoteThread failed 0x%X", GetLastError()); |
| 68 | #endif |
| 69 | } |
no test coverage detected