如何将DLL注入挂起的x64进程?

时间:2017-11-05 20:19:56

标签: c++ winapi dependency-injection dll-injection trampolines

我试图将我的DLL注入我刚刚创建的64位进程中。我最初创建它暂停,以便我可以在该过程中应用WinAPI补丁蹦床(来自我注入的DLL。)但是如果我understand it correctly,我无法将我的DLL注入暂停的进程。

所以我按照guy's suggestions提出了以下代码,但它并没有走得太远。 VirtualProtectEx失败,我收到错误代码ERROR_INVALID_ADDRESS。我在下面的来源中标记了它。

知道我搞砸了哪里?

PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
si.cb = sizeof(si);

WCHAR buffer[MAX_PATH];
::StringCchCopy(buffer, _countof(buffer), L"injected-process.exe");

if(CreateProcessW(0, buffer, 0, 0, 0, CREATE_SUSPENDED, 0, 0, &si, &pi))
{
    inject_dll_into_suspended_x64_proc(pi.hProcess, pi.hThread, "injected-process.exe");

    //... continue on
}

然后准备注射过程的代码:

bool inject_dll_into_suspended_x64_proc(HANDLE hProc, HANDLE hMainThread, const char* pstrProcFileName)
{
    bool bRes = false;
    int nOSError = NO_ERROR;

    DWORD dwEntryOffset = 0;
    LOADED_IMAGE li = { 0 };
    if (::MapAndLoad(pstrProcFileName, NULL, &li, FALSE, TRUE))
    {
        dwEntryOffset = li.FileHeader->OptionalHeader.AddressOfEntryPoint;
        ::UnMapAndLoad(&li);
    }

    if(dwEntryOffset)
    {
        //  90               nop              
        //  EB FE            jmp         self
        static BYTE inject_asm_x64[] = {
            0x90,
            0xEB, 0xFE,
        };
        BYTE buffBackup[sizeof(inject_asm_x64)] = { 0 };

        //Get process base addr
        BYTE* pBaseAddr = (BYTE*)::VirtualAllocEx(hProc, NULL, sizeof(buffBackup), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
        if (pBaseAddr)
        {
            BYTE* pAddr = pBaseAddr + dwEntryOffset;

            DWORD dwOldProtect = 0;
            if (::VirtualProtectEx(hProc, pAddr, sizeof(buffBackup), PAGE_EXECUTE_READWRITE, &dwOldProtect))    //** FAILS: With error code: 487, or ERROR_INVALID_ADDRESS
            {
                __try
                {
                    //Backup what we have there now
                    size_t szcbRead = 0;
                    if (::ReadProcessMemory(hProc, pAddr, buffBackup, sizeof(buffBackup), &szcbRead) &&
                        szcbRead == sizeof(buffBackup))
                    {
                        //Now write our code into entry point
                        size_t dwcbSzWrtn = 0;
                        if (WriteProcessMemory(hProc, pAddr, inject_asm_x64, sizeof(inject_asm_x64), &dwcbSzWrtn) &&
                            dwcbSzWrtn == sizeof(inject_asm_x64))
                        {
                            bool bIntermediateSuccess = false;
                            bool bThreadIsSuspended = true;

                            //Resume thread
                            if (ResumeThread(hMainThread) == 1)
                            {
                                bThreadIsSuspended = false;

                                CONTEXT context;
                                bool bReached = false;

                                //Wait for it to reach our JMP self opcode
                                for(;; ::Sleep(1))
                                {
                                    if(!::GetThreadContext(hMainThread, &context))
                                    {
                                        //Failed
                                        nOSError = ::GetLastError();
                                        break;
                                    }

                                    if(context.Rip == (DWORD64)(pAddr + 1))     //First is nop, so skip it
                                    {
                                        //Got it
                                        bReached = true;
                                        break;
                                    }
                                }

                                if(bReached)
                                {
                                    //Do our DLL injection now
                                    if(inject_dll_here(hProc))
                                    {
                                        //Injected OK
                                        bIntermediateSuccess = true;
                                    }
                                    else
                                        nOSError = ::GetLastError();

                                    //Suspend main thread
                                    if(::SuspendThread(hMainThread) == 0)
                                    {
                                        //Thread is again suspended
                                        bThreadIsSuspended = true;
                                    }
                                    else
                                    {
                                        //Failed
                                        nOSError = ::GetLastError();
                                        bIntermediateSuccess = false;
                                    }
                                }
                            }
                            else
                                nOSError = ::GetLastError();

                            if(bThreadIsSuspended)
                            {
                                //Revert process memory back
                                if (WriteProcessMemory(hProc, pAddr, buffBackup, sizeof(buffBackup), &dwcbSzWrtn) &&
                                    dwcbSzWrtn == sizeof(buffBackup))
                                {
                                    //Now restore the main thread
                                    if (ResumeThread(hMainThread) == 1)
                                    {
                                        //Done
                                        bRes = bIntermediateSuccess;
                                    }
                                    else
                                        nOSError = ::GetLastError();
                                }
                                else
                                    nOSError = ::GetLastError();
                            }
                        }
                        else
                            nOSError = ::GetLastError();
                    }
                    else
                        nOSError = ::GetLastError();
                }
                __finally
                {
                    //Reset protection flags
                    ::VirtualProtectEx(hProc, pAddr, sizeof(buffBackup), dwOldProtect, NULL);
                }
            }
            else
                nOSError = ::GetLastError();

            //Free mem
            ::VirtualFreeEx(hProc, pBaseAddr, 0, MEM_RELEASE);
        }
        else
            nOSError = ::GetLastError();
    }
    else
        nOSError = ::GetLastError();

    return bRes;
}

0 个答案:

没有答案