[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!



  • Hi just a guess but it seems your VS2013-flavored injection program is 32-bit and the Qt-flavored one is 64-bit. If the target also is a 32-bit program that could explain the error. Maybe try injecting into a 64-bit program.



  • You were right, that was exactly the problem! Although, I'm kind of surprsied, how is it that a 32 bit dll can't get injected in a 32 bit target, just because the injector is 64 bit?



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.