0
我一直在嘗試過去幾天創建一個DLL注入器。
我發現DLL注入最簡單的方法是使用CreateRemoteThread
如何注入一個DLL到另一個進程?
這是我迄今爲止寫的,這段代碼不起作用,不能找出原因。
我很確定我的問題是在我用來調用WinAPI函數的變量類型中,但我找不到在哪裏。
bool Injector::Inject(HANDLE hProcess)
{
//hProcess is a process with writing and reading access
HANDLE hThread;
void* pLibRemote = 0;
string dllPath = "Some dll path";
HMODULE hKernel32 = GetModuleHandle(__TEXT("Kernel32"));
pLibRemote = VirtualAllocEx(hProcess, NULL, sizeof(dllPath.c_str()), MEM_COMMIT, PAGE_READWRITE);
WriteProcessMemory(hProcess, pLibRemote, dllPath.c_str(), sizeof(dllPath.c_str()), NULL);
hThread = CreateRemoteThread(hProcess, NULL, 0,
(LPTHREAD_START_ROUTINE) ::GetProcAddress(hKernel32,"LoadLibraryA"),
pLibRemote, 0, NULL);
.
.
.
CloseHandle(hThread);
}
.
.
.
工作正常!非常感謝你!你能向我解釋爲什麼你使用'dllPath.size()+ sizeof(char)'而不是'dllPath.size()'(是因爲空終止符?) – HackinGuy
@HackinGuy確切地說,[字符串: :size()](http://en.cppreference.com/w/cpp/string/basic_string/size)將返回字符數,不包括終止NULL。考慮到這一點,以字節**爲單位的字符串大小**應該計算爲:'(string.size()* sizeof(TCHAR))+ sizeof(TCHAR)'',所以如果你使用std ::字符串與char和std :: wstring與wchar_t和_UNICODE被定義。 – Neitsa
@Neitsa你不能使用'sizeof(TCHAR)',除非你也使用'std :: basic_string'來匹配。 'std :: string'使用'char','std :: wstring'使用'wchar_t'。相應地使用適當的Ansi/Unicode API('LoadLibraryA' /'LoadLibraryW')。在這種情況下,正在使用'std :: string',所以使用'(string.size()+ 1)* sizeof(char)'。 –