从 x64 注入器注入带有 x86 dll 的 x86 目标

Injecting a x86 target with a x86 dll from a x64 injector

我在做标题所说的事情时遇到了一些麻烦...我制作了一个适用于 x86 到 x86 和 x64 到 x64 的注入器,但是从 x64(带有 x86 dll)注入 x86 并没有' 使用该代码:

#include <Windows.h>
#include <string>

bool 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 = GetModuleHandle("Kernel32");

    char DllFullPathName[_MAX_PATH];
    GetFullPathName(dllPath.c_str(), _MAX_PATH, DllFullPathName, NULL);

    // 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)
        return false;

    // 2. Write szLibPath to the allocated memory
    WriteProcessMemory(hProcess, pLibRemote, (void*)szLibPath,
                       sizeof(szLibPath), NULL);

    // 3. Force remote process to load dll
    LPTHREAD_START_ROUTINE thread;
    thread = (LPTHREAD_START_ROUTINE)GetProcAddress(hKernel32,"LoadLibraryA");

    hThread = CreateRemoteThread(hProcess, NULL, 0, thread,  pLibRemote,
                                 0, NULL);

    if (hThread == NULL)
        return false;

    return true;
}

函数returns在所有情况下都是正确的(即使是从 64 位注入器注入 32 位进程),但它无法实际注入 dll。

顺便说一句,在我的研究过程中,我发现了这些问题:

x86 Code Injection into an x86 Process from a x64 Process

C++: Injecting 32 bit targets from 64 bit process

但是虽然答案解释了如何操作,但我并没有真正做到...所以也许我需要的只是一个代码片段以正确的方式发送给我?

更改此行:

thread = (LPTHREAD_START_ROUTINE)GetProcAddress(hKernel32,"LoadLibraryA");

对于这一行:

thread = (LPTHREAD_START_ROUTINE)system("loadLibrary_x86_address.exe");

其中 "loadLibrary_x86_address.exe" 是 32 位应用程序,定义为:

#include <Windows.h>

int main()
{
    return (int)LoadLibraryA;
}

有效!这有点骇人听闻,但确实有效。