2


首先我想说的是,我并不是要破解游戏。我实际上受雇于我试图注入其流程的公司。:)
我想知道如何从已经注入的 DLL 中调用函数。
因此,我已经使用 CreateRemoteThread() 在目标中成功注入并加载了我的 DLL。您可以在下面看到注入的片段:

private static bool Inject(Process pToBeInjected, string sDllPath,out string sError, out IntPtr hwnd, out IntPtr hLibModule)
    { 
        IntPtr zeroPtr = (IntPtr)0;
        hLibModule = zeroPtr;

        IntPtr hProcess = NativeUtils.OpenProcess(
            (0x2 | 0x8 | 0x10 | 0x20 | 0x400), //create thread, query info, operation ,write, and read 
            1,
            (uint)pToBeInjected.Id);

        hwnd = hProcess;
        IntPtr loadLibH = NativeUtils.GetProcAddress( NativeUtils.GetModuleHandle("kernel32.dll"),"LoadLibraryA");

        IntPtr dllAddress = NativeUtils.VirtualAllocEx(
            hProcess,
            (IntPtr)null,
            (IntPtr)sDllPath.Length, //520 bytes should be enough 
            (uint)NativeUtils.AllocationType.Commit |
            (uint)NativeUtils.AllocationType.Reserve,
            (uint)NativeUtils.MemoryProtection.ExecuteReadWrite);

    byte[] bytes = CalcBytes(sDllPath);
        IntPtr ipTmp = IntPtr.Zero;

        NativeUtils.WriteProcessMemory(
            hProcess,
            dllAddress,
            bytes,
            (uint)bytes.Length,
            out ipTmp);


        IntPtr hThread = NativeUtils.CreateRemoteThread(
            hProcess,
            (IntPtr)null,
            (IntPtr)0,
            loadLibH, //handle to LoabLibrary function
            dllAddress,//Address of the dll in remote process
            0,
            (IntPtr)null);

        uint retV= NativeUtils.WaitForSingleObject(hThread, NativeUtils.INFINITE_WAIT);
        bool exitR = NativeUtils.GetExitCodeThread(hThread, out hLibModule);
        return true;
    }

注意:为简洁起见,错误检查和释放资源已被删除,但请放心,我会检查所有指针并释放我的资源。
在上面的函数退出后,我有一个由 LoadLibrary 通过返回的 DLL 的非零模块句柄hLibModule,这意味着 DLL 已正确加载。
我的 DLL 是一个 C# 类库,旨在显示一个消息框(用于测试)。我已尝试测试该功能并弹出消息框。它看起来像这样:

 public class Class1
    {
        public static void ThreadFunc(IntPtr param ) 
        {       
        IntPtr libPtr = LoadLibrary("user32.dll");

            MessageBox(IntPtr.Zero, "I'm ALIVE!!!!", "InjectedDll", 0);

        }
        [DllImport("kernel32", SetLastError = true)]
        public static extern IntPtr LoadLibrary(string lpFileName);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int MessageBox(IntPtr hWnd, String text, String caption, int options);

} 

我从 Visual Studio 编译它,DLL 出现在 Debug 文件夹中。然后我将我的 DLL 的完整路径传递给注入器。
注入目标进程后,我不知道如何ThreadFunc从注入的DLL中调用我的,所以它永远不会执行。
我无法使用GetProcAddress(hLibModule,"ThreadFunc"),因为我不在进程中,所以答案必须在于以某种方式调用 CreateRemoteThread()。另外,我已经读到 .NET DLL 不再允许使用 DllMain,因此我也无法通过这种方式获得任何免费执行。
有谁知道如何从注入的 DLL 调用函数?
先感谢您。

4

1 回答 1

2

好吧,您已经在该进程中运行了一个线程。你让它做一些无聊的事情,它只加载一个DLL。这完全是偶然的,LoadLibrary 恰好必须更正函数签名。

它可以做得更多。但是最好是非托管代码,就像 LoadLibrary() 一样,您不能指望任何正常运行的托管代码。这需要更多的工作,您必须加载和初始化 CLR 并告诉它加载和执行您要运行的程序集。不,您不能在 DllMain() 中加载 CLR。

要查找的关键字是 CorBindToRuntimeEx() 和 ICLRRuntimeHost::ExecuteInAppDomain()。这是要开始的坚韧不拔的事情,但我已经看到它完成了。需要 COM 和 C++ 技能以及慷慨的运气帮助。

于 2010-11-02T20:39:04.977 回答