2016-03-25 23 views
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); 

    } 
. 
. 
. 

回答

2

至少有2個問題:

pLibRemote = VirtualAllocEx(hProcess, NULL, sizeof(dllPath.c_str()), MEM_COMMIT, PAGE_READWRITE); 

1)sizeof(dllPath.c_str()):你在一個指針(c_str() returns a pointer)做的sizeof,因此你可能會得到4或8個結果。使用dllPath.size() + sizeof(char)(或wchar_t而不是char,如果使用std::wstring)。

2)使用MEM_RESERVE | MEM_COMMIT,而不是僅僅MEM_COMMIT:要保留在同一時間提交的保留內存。

同時確保這兩個程序使用相同的ISA(x86/x86; x64/x64,但不是不匹配的ISA)。

+0

工作正常!非常感謝你!你能向我解釋爲什麼你使用'dllPath.size()+ sizeof(char)'而不是'dllPath.size()'(是因爲空終止符?) – HackinGuy

+0

@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

+0

@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)'。 –

相關問題