Windows 10中的链接上的GetModuleInformation失败

问题描述 投票:0回答:1

我正在尝试创建一个DLL Injector,它将在目标进程中直接执行DLL中的函数。我试图得到我注入的DLL的入口点,所以我可以得到函数的偏移量。我在Microsoft Docs上阅读使用GetModuleInfo()。我使用标头psapi.h并发现编译工作,但它在链接上失败,提供以下错误:

g ++ dll_injector.cpp -o dll_injector.exe

C:\Users\DELKAR~1\AppData\Local\Temp\ccOeKdhW.o:dll_injector.cpp:(.text+0x187): undefined reference to `GetModuleInformation@16'
C:\Users\DELKAR~1\AppData\Local\Temp\ccOeKdhW.o:dll_injector.cpp:(.text+0x1ae): undefined reference to `GetModuleInformation@16'
collect2.exe: error: ld returned 1 exit status

我已经尝试将Psapi.lib放在与dll_injector.cpp相同的目录中,然后进行编译

g ++ dll_injector.cpp -o dll_injector.exe -Xlinker -L Psapi.lib

但它仍然抛出同样的错误。

这是dll_injector.cpp代码:

#include <windows.h>
#include <iostream>
#include <psapi.h>
using namespace std;

int main() {
    DWORD pid;
    const char* dll_path = "C:\\Users\\Delkarix\\Desktop\\target_dll.dll";
    HWND hwnd = FindWindow(NULL, "C:\\Users\\Delkarix\\Desktop\\target_process.exe");
    GetWindowThreadProcessId(hwnd, &pid);
    HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);

    LPVOID addr = VirtualAllocEx(hProcess, NULL, strlen(dll_path) + 1, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    WriteProcessMemory(hProcess, addr, dll_path, strlen(dll_path) + 1, NULL);
    CreateRemoteThread(hProcess, NULL, 0, LPTHREAD_START_ROUTINE(GetProcAddress(LoadLibrary("kernel32"), "LoadLibraryA")), addr, 0, NULL);

    HMODULE lib = LoadLibrary("target_dll.dll");
    FARPROC proc_addr = GetProcAddress(lib, "test_function");

    MODULEINFO info;
    MODULEINFO info_current;
    BOOL success1 = GetModuleInformation(GetCurrentProcess(), lib, &info_current, sizeof(info_current));
    BOOL success2 = GetModuleInformation(hProcess, lib, &info, sizeof(info));
    cout << success1 << endl; // Test if it works
    cout << success2 << endl; // Test if it works
}

这是target_dll.cpp代码:

#include <iostream>
using namespace std;

extern "C" __declspec(dllexport) void test_function() {
    cout << "Hello World" << endl;
}

我希望dll_injector.exe打印出值,表明功能是否成功。我稍后会更改代码,这样我就可以获得入口点值。目前,我只想让函数成功而无需创建Visual Studio C ++控制台项目。我的问题:为什么联系失败,我怎样才能使联系成功?

c++ windows winapi g++ psapi
1个回答
1
投票

更改

g ++ dll_injector.cpp -o dll_injector.exe -Xlinker -L Psapi.lib

g ++ dll_injector.cpp -o dll_injector.exe -L。 -lPsapi

-L(库路径) - 指定lib目录。 .指定当前路径。

-l(库) - 与库链接。 Psapi指定库的名称。(在windows中省略后缀名称.lib或在linux中添加前缀名称lib和后缀名称.a

来自Compiling with g++的文件

© www.soinside.com 2019 - 2024. All rights reserved.