I want to call a function inside another process and send more then 1 argument through createremotethread.
Now, I could do that by sending inline asm but I don't know enough assembly in order to do it that way. Also I don't have any access to the remote process source-code.
I was thinking about using:
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct RemoteThreadParams
{
[MarshalAs(UnmanagedType.I4)]
public int Param1;
[MarshalAs(UnmanagedType.I4)]
public int Param2;
}
but from what I understand, the remote process must know how to handle that.
Is there a easy way to send more then 1 argument to the remote process which doesn't include any assembly?
EDIT:
This is how I'm trying to solve it at the moment but i get out of memory exception and I really don't understand what I'm doing wrong.
We have our function ptr at 0x64D480 in the remote process, this is the assembly taken from 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
Shouldn't it be possible to call the function this way:
[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);
}