我正在尝试创建一个DLL注入器,该注入器将直接在目标进程中的DLL中执行功能。我试图获取注入的DLL的入口点,以便获得函数的偏移量。我阅读了要使用的Microsoft文档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 ++控制台项目。我的问题:为什么链接失败,如何使链接成功?
更改
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(library) -与库链接。Psapi
指定库的名称。(.lib
在Windows中省略后缀名称,在Linux中省略前缀名称lib
和后缀名称.a
)
使用g ++编译文档
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句