簡體   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