Pregunta

He escrito un programa de muestreo y DLL para aprender el concepto de inyección de DLL.

Mi código de inyección para inyectar la DLL para el programa de ejemplo es el siguiente (manejo de errores omite):

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());

Memoria :: CreateThread es el siguiente:

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

El problema es, que el módulo no está cargado. Sin embargo, cuando cambio la segunda línea a

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

funciona (ya que la DLL de prueba no contiene caracteres Unicode en su nombre).

¿Cómo hacer que funcione con LoadLibraryW?

¿Fue útil?

Solución

HMODULE WINAPI LoadLibrary(
  __in  LPCTSTR lpFileName
);

Se necesita un TCHAR - por lo que el argumento a favor de LoadLibraryW tiene que ser una cadena de ancho; el código anterior pasa a la forma multi-byte del argumento, que es la forma que quiere LoadLibraryA.

Otros consejos

No estoy seguro de por qué se va a crear un hilo y pasándole la dirección de la función LoadLibraryW. ¿No sería más fácil y más seguro para LoadLibraryW llamada directamente?

De cualquier manera, ciertamente no es necesario realizar ninguna llamada WideCharToMultiByte. LoadLibraryW espera una amplia nombre del módulo carácter.

¿Hay alguna razón por la que no se puede simplemente hacer esto?

HMODULE hLibHandle = LoadLibraryW( L"D:\\Path\\to\\my\\DLL.dll" );
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top