Continuing my reverse engineering education I've often wanted to be able to copy portions of x86 assembly code and call it from a high level language of my choice for testing.
Does anyone know of a method of calling a sequence of x86 instructions from within a C# method? I know that this can be done using C++ but I'm curious if it can be done in C#?
Note: I'm not talking about executing MSIL instructions. I'm talking about executing a series of raw x86 assembly instructions.
Just to counter Brian's claim, rewritten code from leppie's answer link:
I believe, you can add a managed C++ project to your solution and expose method with usage of asm instructions. You can reference that project from any .Net project (not just C#), so you can call that method from there.
Yes, see my detailed answer here
The main part is: (Without any P/Invoke or external reference)
Which can be used as follows:
Yes.
Just use P/Invoke on winapi functions.
WriteProcessMemory or find the pointer to your buffer. Enable the execute bit on page (don't remember the function for this).
CreateThread on the pointer. WaitForObject (if you want it to be single threaded).
No, but you can write assembly in C++ and call it from C#. See this example.