0

我编写了一些示例程序和 DLL 来学习 DLL 注入的概念。

我将 DLL 注入示例程序的注入代码如下(省略错误处理):

std::wstring dll(L"D:\\Path\\to\\my\\DLL.dll");
LPTHREAD_START_ROUTINE pLoadLibraryW = 
    (LPTHREAD_START_ROUTINE)GetProcAddress(hKernel32, "LoadLibraryW");
int bytesNeeded = WideCharToMultiByte(CP_UTF8, 0, dll.c_str(), dll.length(), 
    NULL, 0, NULL, NULL);
std::vector<byte> dllName(bytesNeeded);
WideCharToMultiByte(CP_UTF8, 0, dll.c_str(), dll.length(), 
    (LPSTR)&dllName[0], bytesNeeded, NULL, NULL);
// Memory is a class written by me to simplify memory processes. 
// Constructor takes desired permissions.
Memory mem (pid, false, true, false, true, false, false, false, 
    false, false, true, true, true, false);
// Ensures deletion of the allocated range.
// true / true / false = read and write access, no execute permissions
std::tr1::shared_ptr<void> allocated = 
    mem.AllocateBytes(dllName.size(), true, true, false);
mem.WriteBytes((unsigned int)allocated.get(), dllName);
mem.CreateThread(pLoadLibraryW, allocated.get());

Memory::CreateThread 如下:

void Memory::CreateThread(LPTHREAD_START_ROUTINE address, LPVOID parameter) const {
    std::tr1::shared_ptr<void> hThread(CreateRemoteThread(m_hProcess.get(), 
        NULL, 0, address, parameter, 0, NULL), CloseHandle);
    if (hThread.get() == NULL) {
        throw std::runtime_error("Memory::CreateThread: CreateRemoteThread failed");
    }
    DWORD returned = WaitForSingleObject(hThread.get(), INFINITE);
    if (returned != WAIT_OBJECT_0) {
        throw std::runtime_error("Memory::CreateThread: The remote thread did not complete properly");
    }
}

问题是,该模块未加载。但是,当我将第二行更改为

LPTHREAD_START_ROUTINE pLoadLibraryW =
    (LPTHREAD_START_ROUTINE)GetProcAddress(hKernel32, "LoadLibraryA");

它可以工作(因为测试 dll 的名称中没有 unicode 字符)。

如何让它工作LoadLibraryW

4

2 回答 2

2
HMODULE WINAPI LoadLibrary(
  __in  LPCTSTR lpFileName
);

它需要一个 TCHAR——因此 LoadLibraryW 的参数必须是一个宽字符串;上面的代码传递了参数的多字节形式,这是 LoadLibraryA 想要的形式。

于 2009-10-18T10:27:16.817 回答
1

我不确定你为什么要创建一个线程并将LoadLibraryW函数的地址传递给它。直接打电话不是更容易更安全LoadLibraryW吗?

无论哪种方式,您当然不需要拨打任何WideCharToMultiByte电话。LoadLibraryW需要一个宽字符模块名称。

你有什么理由不能这样做吗?

HMODULE hLibHandle = LoadLibraryW( L"D:\\Path\\to\\my\\DLL.dll" );
于 2009-10-18T10:26:42.577 回答