繁体   English   中英

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

[英]How to fix “LPVOID: unknown size” error while using the CreateRemoteThread API?

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

由于在调用CreateRemoteThread()时不需要传递给lpStartAddress任何参数,因此将lpParameter初始化为NULL。


LPVOID lpParameter = NULL;

...
...
thread_handle = CreateRemoteThread(process_handle, NULL, 0, (LPTHREAD_START_ROUTINE)(base_address + offset), lpParameter, 0, NULL);

编译代码时出现错误:

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

有没有一种方法可以将lpParameter的值传递为NULL?

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. 在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...!");
}

希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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