【问题标题】:How to inject a dll to a another process? [closed]如何将dll注入另一个进程? [关闭]
【发布时间】:2016-03-25 13:07:10
【问题描述】:

最近几天我一直在尝试创建一个 DLL 注入器。
我发现的 DLL 注入最简单的方法是使用CreateRemoteThread

这是我到目前为止写的,这段代码不起作用,无法找出原因。

我很确定我的问题出在我用来调用 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);

    }
.
.
.

【问题讨论】:

    标签: c++ winapi dll dll-injection


    【解决方案1】:

    至少 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)(如果使用std::wstring,则使用wchar_t 而不是char)。

    2) 使用 MEM_RESERVE | MEM_COMMIT 而不仅仅是 MEM_COMMIT :您希望同时保留和提交保留的内存。

    还要确保两个程序使用相同的 ISA(x86 / x86 ;x64 / x64,但不是不匹配的 ISA)。

    【讨论】:

    • 成功了!非常感谢!你能解释一下你为什么用dllPath.size() + sizeof(char)而不是dllPath.size()(是因为空终止符吗?)
    • @HackingGuy 没错,string::size() 将返回字符数,不包括终止的 NULL。想一想,字符串大小 以字节为单位 应计算为:(string.size() * sizeof(TCHAR)) + sizeof(TCHAR),因此如果您使用带有 char 的 std::string 和带有 wchar_t 的 std::wstring 和 _UNICODE ,则此方法有效已定义。
    • @Neitsa 你不能使用sizeof(TCHAR),除非你也使用std::basic_string<TCHAR> 来匹配。 std::string 使用 char,std::wstring 使用 wchar_t。相应地使用适当的 Ansi/Unicode API (LoadLibraryA/LoadLibraryW)。在这种情况下,正在使用std::string,因此请使用(string.size()+1) * sizeof(char)。
    猜你喜欢
    • 2012-01-01
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 2014-12-03
    相关资源
    最近更新 更多