Dll 注入 - LoadLibraryA 失败

Dll injection - LoadLibraryA fails

我正在尝试将 dll 注入进程。 dll 除了 return TRUE.

什么都不做

我将调试器附加到我要注入的进程并确认 LoadLibraryA 被正确调用但 return 为 NULL。 现在我认为这可能与我的 dll 的依赖关系有关。所以我检查了他们,发现它需要 vcruntime140.dll。 我想注入我的 dll 的进程没有加载那个 dll。

#include "pch.h"

extern "C" int __stdcall APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    return TRUE;
}
#include "Source.h"

const char* DllName = "InjectMe.dll";

int main()
{
    DWORD processID = 0;
    printf("Process ID: ");
    scanf_s("%i", &processID);

    HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processID);
    if (handle == nullptr) {
        printf("Process could not be opened.");
        return -1;
    }
    LPVOID memDllName = VirtualAllocEx(handle, nullptr, strlen(DllName) + 1, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
    assert(memDllName != nullptr);
    assert(WriteProcessMemory(handle, memDllName, DllName, strlen(DllName) + 1, nullptr));

    LPVOID loadLibraryAddr = GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA");
    assert(loadLibraryAddr != nullptr);

    HANDLE thread = CreateRemoteThreadEx(handle, nullptr, 0, (LPTHREAD_START_ROUTINE)loadLibraryAddr, memDllName, CREATE_SUSPENDED, nullptr, nullptr);
    assert(thread != nullptr);
    ResumeThread(thread);
    DWORD returnCode = WaitForSingleObject(thread, 5000);
    CloseHandle(thread);
    if (returnCode == WAIT_TIMEOUT) {
        printf("DLL was not loaded. Thread timed out.");
        return -1;
    }
    else if (returnCode == WAIT_OBJECT_0) {
        printf("DLL was successfully injected into the process.");
    }
    CloseHandle(handle);
    std::cin.get();
    return 0;
}

调用 LoadLibrary() 时必须使用完整文件路径,而不是相对文件路径

const char* DllName = "InjectMe.dll";

需要改成这样

const char* DllName = "c:\Users\User\Desktop\InjectMe.dll";

如果 OpenProcess 失败或有时您还需要使用 SeDebugPrivelage

,请确保您 运行 是管理员

为了测试它是否是路径问题,请尝试以下操作。保持

const char* DllName = "InjectMe.dll";

然后将 InjectMe.dll 和您的 .exe 放在同一目录中并尝试 运行 您的 exe。如果dll加载成功,那就是路径问题

要解决这个问题,您可以像 GuidedHacking 所说的那样指定完整路径,或者您可以将 InjectMe.dll 放在与 .vcxproj 和 .cpp 文件相同的目录中(而不是 .sln 文件所在的目录)是)