Domanda

Ho scritto qualche programma di esempio e DLL per imparare il concetto di iniezione DLL.

Il mio codice di iniezione per iniettare la DLL per il programma di esempio è il seguente (errore manipolazioni omesso):

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 è la seguente:

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

Il problema è, che il modulo non viene caricato. Tuttavia, quando cambio la seconda linea a

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

funziona (dal momento che la dll di prova non ha caratteri unicode nel suo nome).

Come per farlo funzionare con LoadLibraryW?

È stato utile?

Soluzione

HMODULE WINAPI LoadLibrary(
  __in  LPCTSTR lpFileName
);

Ci vuole un TCHAR - così l'argomento per LoadLibraryW deve essere una stringa di larghezza; il codice sopra passa forma multi-byte dell'argomento, che è la forma che LoadLibraryA vuole.

Altri suggerimenti

Non sono sicuro che il motivo per cui si sta creando un filo e passando l'indirizzo della funzione LoadLibraryW. Non sarebbe più semplice e sicuro per chiamare direttamente LoadLibraryW?

In entrambi i casi, certamente non è necessario effettuare alcuna chiamata WideCharToMultiByte. LoadLibraryW aspetta un'ampia nome del modulo carattere.

C'è qualche motivo per cui non si può semplicemente fare questo?

HMODULE hLibHandle = LoadLibraryW( L"D:\\Path\\to\\my\\DLL.dll" );
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top