我想在另一个进程中调用一个函数,并通过createremotethread发送超过1个参数。
现在,我可以通过发送内联asm来做到这一点,但我不知道足够的汇编以便这样做。此外,我无权访问远程进程源代码。
我在考虑使用:
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct RemoteThreadParams
{
[MarshalAs(UnmanagedType.I4)]
public int Param1;
[MarshalAs(UnmanagedType.I4)]
public int Param2;
}
但据我所知,远程进程必须知道如何处理它。
是否有一种简单的方法可以向远程进程发送超过1个参数但不包含任何程序集?
编辑:
这就是我现在试图解决的问题,但是我的内存异常,我真的不明白我做错了什么。
我们在远程进程中将函数ptr设置为0x64D480,这是从IDA pro获取的程序集。
// FUNCTION PTR IS 0x64D480
.text:0064D480 sub_64D480 proc near ; CODE XREF: sub_4DA7F0+3Ap
.text:0064D480 ; sub_64D550+Bp ...
.text:0064D480
.text:0064D480 var_C = dword ptr -0Ch // arg1
.text:0064D480 arg_0 = dword ptr 4 // arg2
.text:0064D480
.text:0064D480 push esi
.text:0064D481 push edi
.text:0064D482 mov edi, [esp+8+arg_0]
.text:0064D486 push edi
.text:0064D487 mov esi, ecx
.text:0064D489 call sub_64D330
.text:0064D48E test al, al
.text:0064D490 jnz short loc_64D497
.text:0064D492 pop edi
.text:0064D493 pop esi
.text:0064D494 retn 4
不应该以这种方式调用函数:
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct RemoteThreadParams
{
[MarshalAs(UnmanagedType.I4)]
public int Param1;
[MarshalAs(UnmanagedType.I4)]
public int Param2;
}
void CallFunction(IntPtr _functionPtr, RemoteThreadParams _parameters)
{
// Allocate some native heap memory in your process big enough to store the parameter data
IntPtr iptrtoparams = Marshal.AllocHGlobal(Marshal.SizeOf(_parameters));
// Copies the data in your structure into the native heap memory just allocated
Marshal.StructureToPtr(_parameters, iptrtoparams, false);
// Use to alloc "committed" memory that is addressable by other process
IntPtr iptrremoteallocatedmemory = VirtualAllocEx(this.handle, IntPtr.Zero, (uint)Marshal.SizeOf(_parameters), AllocationType.Commit, MemoryProtection.ExecuteReadWrite);
UIntPtr bytesWritten = UIntPtr.Zero;
// Copy from local process memory to the memory of the remote process
WriteProcessMemory(this.handle, iptrremoteallocatedmemory, iptrtoparams, (uint)Marshal.SizeOf(_parameters), out bytesWritten);
//Free up memory
Marshal.FreeHGlobal(iptrtoparams);
//thread id and return value in case we need it for later
uint iThreadId;
uint returnValue = 0;
IntPtr hThread = CreateRemoteThread(this.handle, IntPtr.Zero, 0, _functionPtr, iptrtoparams, 0, out iThreadId);
WaitForSingleObject(hThread, 0xFFFFFFFF);
GetExitCodeThread(hThread, out returnValue);
CloseHandle(hThread);
CloseHandle(this.handle);
}
答案 0 :(得分:1)
我不认为这会有所帮助,但是:
您实际上可以使用例如
来调用本机方法(您可能已经知道)public static extern Foo (Bar argument);
public void Main()
{
Foo(new Bar());
}
此外,您实际上可以读取和操作应用程序存储在RAM中的数据 这就是大多数“游戏培训师”所做的事情,这可能会对您有所帮助:http://forum.cheatengine.org/viewtopic.php?t=530207
答案 1 :(得分:1)
没有简单的方法。最简单的方法是使用LoadLibrary
和非托管代码执行此操作。您需要两个版本:32位dll和64位dll,您可以根据目标进程“bitness”选择要注入的版本。
CreateRemoteThread(....., Address of LoadLibrary, "location of your unmanaged dll");
当你google C#CreateRemoteThread LoadLibrary 时,你会在互联网上找到引用。
之后,您需要自定义dll执行函数调用:
#include "stdafx.h"
typedef void (__stdcall *MyCustomFunc)(int param1, int param2);
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
// this code is executed arbitrarily.
// this is just a test,
// you're better off writing a separate function in a DLL
// and export it, so you can later call it from C#, once DLL is injected.
MyCustomFunc test = 0x64D480;
test(2, 4);
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
ps,如果需要,您还需要FreeLibrary
。让调用约定正确也很重要。我在这里走出困境,说它看起来像__stdcall,但它可能是__cdecl,或__fastcall,或者其他什么。有关详细信息,请参阅:http://www.codeproject.com/Articles/1388/Calling-Conventions-Demystified。