如何在使用CreateRemoteThread API时解决“ LPVOID:未知大小”错误?

时间:2019-07-05 06:48:04

标签: c++ winapi dll dll-injection createremotethread

我正在尝试创建一个用于执行DLL-Injection的工具,方法是使用VirtualAclloc() API将DLL写入正在运行的进程的内存中,然后找到入口点的偏移量并将其传递给{{ 1}} API,方法是将入口点偏移量添加到CreateRemoteThread()函数的基地址中。

由于在调用VirtualAlloc时没有传递给lpStartAddress的任何参数,因此将CreateRemoteThread()初始化为NULL。

lpParameter

在编译代码时出现错误:

  

LPVOID:未知大小”和消息“表达式必须是指向完整对象类型的指针。

有没有一种方法可以将 LPVOID lpParameter = NULL; ... ... thread_handle = CreateRemoteThread(process_handle, NULL, 0, (LPTHREAD_START_ROUTINE)(base_address + offset), lpParameter, 0, NULL); 的值传递为NULL?

2 个答案:

答案 0 :(得分:4)

base_address + offsetoffset*sizeof *base_address字节添加到指针base_address。但是,如果base_address的类型为LPVOID,则*base_address没有大小,因此这是一个错误。看看C ++书籍中有关指针算术的部分。

从上下文来看,我猜您应该将base_address更改为char*而不是LPVOID。或者,您可以像这样(LPTHREAD_START_ROUTINE)((char*)base_address + offset)添加演员表。

答案 1 :(得分:0)

在这种情况下,您需要执行以下过程:

  1. 在kernel32.dll中获取LoadLibraryA函数的句柄
  2. 使用VirtualAllocEx在目标进程的地址空间中分配和初始化内存
  3. 通过使用WriteProcessMemory在目标进程地址空间中写入要注入的dll的路径
  4. 使用CreateRemoteThread注入dll,并将LoadLibraryA的地址作为lpStartAddress传递

下面是示例代码:

char* dllPath = "C:\\testdll.dll";

int procID = 16092;
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, procID);
if (!hProcess) {
    printf("Error: Process not found.\n");
}

LPVOID lpvLoadLib = (LPVOID)GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryA");       /*address of LoadLibraryA*/
if (!lpvLoadLib) {
    printf("Error: LoadLibraryA not found.\n");
}

LPVOID lpBaseAddress = (LPVOID)VirtualAllocEx(hProcess, NULL, strlen(dllPath)+1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);     /*Initialize and Allocate memory to zero in target process address space*/
if (!lpBaseAddress) {
    printf("Error: Memory was not allocated.\n");
}
SIZE_T byteswritten;
int result = WriteProcessMemory(hProcess, lpBaseAddress, (LPCVOID)dllPath, strlen(dllPath)+1, &byteswritten);   /*Write the path of dll to an area of memory in a specified process*/
if (result == 0) {
    printf("Error: Could not write to process address space.\n");
}

HANDLE threadID = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)lpvLoadLib, lpBaseAddress, NULL, NULL); /*lpStartAddress = lpvLoadLib address of LoadLibraryA function*/
if (!threadID) {
    printf("Error: Not able to create remote thread.\n");
}
else {
    printf("Remote process created...!");
}

希望这会有所帮助