简体   繁体   中英

Dll injection - LoadLibraryA fails

I'm trying to inject a dll into a process. The dll does nothing except return TRUE.

I attached a debugger to the process that I want to inject into and confirmed that LoadLibraryA is called correctly but returns NULL. Now I think that this might have something to do with my dll's dependencies. So I checked them and found out that it requires vcruntime140.dll . The process that I want to inject my dll into does not load that 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;
}

You must use a full file path not a relative file path when calling LoadLibrary()

const char* DllName = "InjectMe.dll";

needs to be changed to something like this

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

Also make sure you run as administrator if OpenProcess fails or sometimes you also need to use SeDebugPrivelage

In order to test if it is a pathing issue, try the following. Keep the

const char* DllName = "InjectMe.dll";

Then put the InjectMe.dll and your .exe in the same directory and try to run your exe. If the dll is loaded successfully, then it is a pathing issue.

To work around that, you can either specify the full path like GuidedHacking said, OR you can put your InjectMe.dll in the same directory as the .vcxproj and .cpp files (not where the .sln file is)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM