4

过去一周我一直在努力让我的简单注入应用程序成功地将 dll 注入其他进程。然而,到目前为止,它只有在我将 dll 注入注入器本身时才起作用。当我尝试注入不同的应用程序时,我的函数报告成功(线程已成功创建,内存分配并写入目标)但我的 dllMain 似乎没有被调用。此外,错误代码 0 (ERROR_SUCCESS) 由GetLastError().

指定targetProcessIdasGetCurrentProcess()时,将显示对话框并成功执行 RemoteThread。

但是,当我尝试将 targetProcessId 设置为 calc.exe 的运行实例时,没有任何反应。我在其他地方读到,从 DllMain 调用 MessageBox 是一个坏主意,因为它的模块可能尚未加载,所以我还尝试在我的 DllMain 中创建一个无限循环并检查 calc.exe 中的线程数是否增加了 1。它保持不变。

这是我在 StackOverflow 上的第一个问题,我试图在发布之前尽可能多地研究这个主题,但经过几个小时后我没有运气。我显然是 dll 注入的新手(我最近读完了 Ritcher 的书,Windows via C/C++)。任何帮助是极大的赞赏。

我的所有代码都是从 Visual Studio 2010 为 x64 平台编译的。

下面是我的 Injector 应用程序中的相关代码:

BOOL WINAPI Inject(DWORD processID, PCWSTR sourceDLL)
{
    BOOL success = false;
    HANDLE targetProcess = NULL, createdThread = NULL;
    PWSTR pszLibFileRemote = NULL;
    __try
    {
        std::cout << "Process ID: "<< processID << std::endl;
        targetProcess = OpenProcess(
            PROCESS_QUERY_INFORMATION | PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION  | PROCESS_VM_WRITE,
            FALSE, processID);
        if (targetProcess == NULL)
        {
            std::cout << "ERROR: " << GetLastError();
            MessageBox(NULL, L"Unable to open process.", L"Error", MB_OK);
            __leave;
        }

        int cch = 1 + lstrlenW(sourceDLL); //Calculate the number of bytes required for the DLL's path
        int cb = cch * sizeof(wchar_t);

        pszLibFileRemote = (PWSTR)VirtualAllocEx(targetProcess, NULL, cb, MEM_COMMIT, PAGE_READWRITE);
        if (pszLibFileRemote == NULL)
        {
            MessageBox(NULL, L"Could not allocate dll pathname in target process.", L"Error", MB_OK);
            __leave;
        }

        if (!WriteProcessMemory(targetProcess, pszLibFileRemote, (PVOID) sourceDLL, cb, NULL))
        {
            MessageBox(NULL, L"Could not write dll pathname in target process.", L"Error", MB_OK);
            __leave;
        }

        PTHREAD_START_ROUTINE pfnThreadRtn = (PTHREAD_START_ROUTINE) GetProcAddress(GetModuleHandle(_T("Kernel32")), "LoadLibraryW");

        if (pfnThreadRtn == NULL)
        {
            MessageBox(NULL, L"Error finding LoadLibraryW address.", L"Error", MB_OK);
            __leave;
        }

        createdThread = CreateRemoteThread(targetProcess, NULL, 0, pfnThreadRtn, pszLibFileRemote, 0, NULL);
        if (createdThread == NULL)
        {
            __leave;
        }

        WaitForSingleObject(createdThread, INFINITE);
        success = true;

    }
    __finally { // Now, we can clean everything up
        // Free the remote memory that contained the DLL's pathname
        if (pszLibFileRemote != NULL)
            VirtualFreeEx(targetProcess, pszLibFileRemote, 0, MEM_RELEASE);
        if (createdThread != NULL)
            CloseHandle(createdThread);
        if (targetProcess != NULL)
            CloseHandle(targetProcess);
    }

    return success;
}
int _tmain(int argc, _TCHAR* argv[])
{
    PCWSTR srcDll = L"test.dll"; //dll in the same directory as the injector.exe, its code is specified below.
    DWORD processID = "768"; //Hard coded process ID of a running calc.exe. When I change this line to GetCurrentProcessId() the messagebox from my dll shows.      
    if (Inject(processID, srcDll))
    {
        std::cout << "Injection successful" << std::endl;
        Eject(processID, srcDll); //This detaches the dll by calling freelibrary from a remote thread. This function was omitted from this response to keep things relavent.
    }
    system("PAUSE");
    return 0;
}

这是我简单的 helloworld test.dll 的代码:

#include "stdafx.h"
#include <Windows.h>
#include <tchar.h>
BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        MessageBox(NULL, L"HULLO.", L"DLL", MB_OK);
        break;
    case DLL_THREAD_ATTACH:
        MessageBox(NULL, L"HULLO.", L"DLL", MB_OK);
        break;
    case DLL_THREAD_DETACH:
        MessageBox(NULL, L"HULLO.", L"DLL", MB_OK);
        break;
    case DLL_PROCESS_DETACH:
        MessageBox(NULL, L"HULLO.", L"DLL", MB_OK);
        break;
    }
    return TRUE;
}

已解决:注入的 dll 的目录必须相对于目标进程或完整路径指定,以便目标进程可以找到它。(我把它放在我的 Injector 目录中,这导致了加载问题—— calc 不知道它在哪里。)

4

1 回答 1

4
PCWSTR srcDll = L"test.dll"; //dll in the same directory as the injector.exe

由于您LoadLibraryW()在目标程序中执行,而不是在注入器中执行,因此它不知道“与注入器相同的目录”是什么 - 它只是在 MSDN 上记录的标准目录中搜索它。

您需要传递完整路径而不是相对路径。

于 2012-11-18T11:53:47.880 回答