[SOLVED] DLL injection works, except when I compile it with Qt Creator
-
Basically, I wrote a very simple program that injects a dll I made myself into a target program. it works fine when I compile it in Visual Studio, but it fails when I compile the same exact code in Qt Creator, although the compilation works.
Here is the relevant part of my code:
@bool Injector::InjectDll(DWORD processId, std::string dllPath)
{
HANDLE hThread, hProcess;
void* pLibRemote = 0; // the address (in the remote process) where szLibPath will be copied to;HMODULE hKernel32 = GetModuleHandleA("Kernel32");
char DllFullPathName[_MAX_PATH];
GetFullPathNameA(dllPath.c_str(), _MAX_PATH, DllFullPathName, NULL);
printf("Loading dll: %s\n", DllFullPathName);// Get process handle
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId);// copy file path in szLibPath
char szLibPath[_MAX_PATH];
strcpy_s(szLibPath, DllFullPathName);// 1. Allocate memory in the remote process for szLibPath
pLibRemote = VirtualAllocEx(hProcess, NULL, sizeof(szLibPath), MEM_COMMIT, PAGE_READWRITE);if (pLibRemote == NULL)
{
printf("Couldn't allocate memory, please restart with administrator privileges\n");
return false;
}// 2. Write szLibPath to the allocated memory
WriteProcessMemory(hProcess, pLibRemote, (void*)szLibPath, sizeof(szLibPath), NULL);// 3. Force remote process to load dll
hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetProcAddress(hKernel32, "LoadLibraryA"), pLibRemote, 0, NULL);if (hThread == NULL)
{
printf("Couldn't load DLL");
return false;
}WaitForSingleObject(hThread, INFINITE);
// Get handle of the loaded module
DWORD hLibModule;
GetExitCodeThread(hThread, &hLibModule);
if (hLibModule == 0)
{
printf("error code: %d\n", GetLastError());
}printf("Dll successfully loaded\n");
return true;
}
@In qt, GetExitCodeThread returns 0, indicating an error. Yet, GetLastError returns ERROR_SUCCESS, indicating success. In visual studio, I don't get any error.
More information here: http://stackoverflow.com/questions/28055355/dll-injection-works-except-when-i-compile-it-in-qt-creator
Thanks for your help!
-
Could be the culprit is the pLibRemote pointer you get from the VirtualAllocEx call, in the 64-bit injector program that pointer is 64-bit, and I think it confuses WriteProcessMemory, i.e. writing into a 32-bit code segment using a 64-bit pointer = no happiness.