簡體   English   中英

Dll 注入 - LoadLibraryA 失敗

[英]Dll injection - LoadLibraryA fails

我正在嘗試將一個 dll 注入到一個進程中。 dll 除了返回 TRUE 什么都不做。

我將調試器附加到要注入的進程並確認LoadLibraryA被正確調用但返回 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 失敗,請確保您以管理員身份運行,或者有時您還需要使用 SeDebugPrivalage

為了測試它是否是路徑問題,請嘗試以下操作。 保持

const char* DllName = "InjectMe.dll";

然后將 InjectMe.dll 和您的 .exe 放在同一目錄中並嘗試運行您的 exe。 如果 dll 加載成功,則是路徑問題。

要解決這個問題,您可以指定像 GuidedHacking 所說的完整路徑,或者您可以將 InjectMe.dll 放在與 .vcxproj 和 .cpp 文件相同的目錄中(而不是 .sln 文件所在的位置)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM