0

does anyone have an idea about how can i convert the following C inline ASM to a valid C# format? Thanks in advance.

DWORD WINAPI RemoteExecPayloadStub(LPVOID lpParameter) {
__asm {
    mov eax, [lpParameter]
    call eax
    push 0
    call ExitThread
}
return 0;

}

EDIT

Tried to convert the ASM to bytecodes like so (below) and use it from a byte array but doesn't seem to work as expected. Thanks.

    0:  a1 00 00 00 00          mov    eax,ds:0x0
5:  ff d0                   call   eax
7:  6a 00                   push   0x0
9:  e8 fc ff ff ff          call   a <_main+0xa>
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
SlothGR
  • 135
  • 1
  • 2
  • 9

1 Answers1

2

Since people seem totally confused, I'll answer this here even though it is a duplicate.

The given code does nothing else but invoke a function pointer, and then exit the thread. As such the equivalent C# code could be:

delegate void FuncPtr();
static void RemoteExecPayloadStub(IntPtr lpParameter)
{
    FuncPtr ptr = Marshal.GetDelegateForFunctionPointer<FuncPtr>(lpParameter);
    ptr();
}

As for the ExitThread you can use Thread.CurrentThread.Abort() or pinvoke the winapi function (not recommended).

Executing arbitrary machine code isn't much more difficult either. Put your stuff into a byte array, allocate executable memory using appropriate pinvoke calls, copy your code there and then use the above method to execute it.

Jester
  • 56,577
  • 4
  • 81
  • 125