首先我想说,我不是要破解游戏。我实际上受雇于我试图注入(inject)其流程的公司。 :)
我想知道如何从已注入(inject)的 DLL 中调用函数。
所以,我已经使用 CreateRemoteThread() 成功地将我的 DLL 注入(inject)并加载到目标中。下面你可以看到注入(inject)的片段:
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 通过 hLibModule
返回我的 DLL 的非零模块句柄,这意味着 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 的完整路径传递给注入(inject)器。
注入(inject)目标进程后,我不知道如何从注入(inject)的DLL中调用我的ThreadFunc
,所以它永远不会执行。
我无法使用 GetProcAddress(hLibModule,"ThreadFunc")
,因为我不在进程中,所以答案必须在于以某种方式调用 CreateRemoteThread()。另外,我读到 DllMain 不再允许用于 .NET DLL,因此我也无法通过这种方式获得任何自由执行。
有谁知道如何从注入(inject)的 DLL 中调用函数?
先感谢您。
最佳答案
好吧,您已经在该进程中运行了一个线程。你让它做一些无聊的事情,它只加载一个 DLL。这完全是偶然的,LoadLibrary 恰好必须更正函数签名。
它可以做更多。然而,最好是非托管代码,就像 LoadLibrary() 一样,您不能指望任何托管代码都能正常运行。这需要大量的工作,您必须加载和初始化 CLR 并告诉它加载和执行您要运行的程序集。不,您不能在 DllMain() 中加载 CLR。
要查找的关键字是 CorBindToRuntimeEx() 和 ICLRRuntimeHost::ExecuteInAppDomain()。这是一项艰巨的任务,但我已经看到它完成了。 COM 和 C++ 技能以及运气的慷慨帮助。
关于c# - 从注入(inject)的 DLL 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4081788/