views:

1053

answers:

7

Is there such a thing as an x86 assembler that I can call through C#? I want to be able to pass x86 instructions as a string and get a byte array back. If one doesn't exist, how can I make my own?

To be clear - I don't want to call assembly code from C# - I just want to be able to assemble code from instructions and get the machine code in a byte array. I'll be injecting this code (which will be generated on the fly) to inject into another process altogether.

A: 

Take a look at this: CodeProject: Using unmanaged code and assembly in C#.

J D OConal
I should have been clearer - I'll edit my question for more clarity and you'll see why this isn't what I'm looking for. =) Thank you though.
Erik Forbes
Ah, I understand now. Why don't you just compile the code using an assembly compiler?
J D OConal
I have some requirements regarding the baggage I'm allowed to deploy - specifically I need to keep everything in a single executable.
Erik Forbes
+1  A: 

Not directly from C# you can't. However, you could potentially write your own wrapper class that uses an external assembler to compile code. So, you would potentially write the assembly out to a file, use the .NET Framework to spin up a new process that executes the assembler program, and then use System.IO to open up the generated file by the assembler to pull out the byte stream.

However, even if you do all that, I would be highly surprised if you don't then run into security issues. Injecting executable code into a completely different process is becoming less and less possible with each new OS. With Vista, I believe you would definitely get denied. And even in XP, I think you would get an access denied exception when trying to write into memory of another process.

Of course, that raises the question of why you are needing to do this. Surely there's got to be a better way :).

jolson
Perhaps he's doing it to see if he can.
J D OConal
No, not viruses. =P And in both Vista and XP I can inject memory into processes using WriteProcessMemory API. It's relatively simple, provided you're running as Administrator.
Erik Forbes
As for why I need it - I'm really not at liberty to say, but I can promise that it's not for any nefarious purpose. =)
Erik Forbes
Yeah, using the official API and being Administrator would help, I'd imagine :).
jolson
Indeed it does. =P
Erik Forbes
+5  A: 

As part of some early prototyping I did on a personal project, I wrote quite a bit of code to do something like this. It doesn't take strings -- x86 opcodes are methods on an X86Writer class. Its not documented at all, and has nowhere near complete coverage, but if it would be of interest, I would be willing to open-source it under the New BSD license.

UPDATE: Ok, I've created that project -- Managed.X86

Alex Lyman
I would definitely be interested in this - I'd be happy to contribute as well. If you decide to do this, drop me a line - erikforbes at gmail dot com - and we'll collaborate. =)
Erik Forbes
This is *exactly* what I've been looking for - thank you!
Erik Forbes
A: 

I think you would be best off writing a native Win32 dll. You can then write a function in assembler that is exported from the dll. You can then use C# to dynamically link to the dll.

This is not quite the same as passing in a string and returning a byte array. To do this you would need an x86 assembler component, or a wrapper around masm.exe.

Mike Thompson
Right, I need an x86 assembler component.
Erik Forbes
A: 

i don't know if this is how it works but you could just shellexecute an external compiler then loading the object generated in your byte array.

CiNN
+1  A: 

Cosmos also has some interesting support for generating x86 code:

http://www.gocosmos.org/blog/20080428.en.aspx

Luke Quinane
This is certainly interesting, but not quite what I'm looking for - they translate from IL -> x86. Thanks for the link though! =)
Erik Forbes
+1  A: 

Take a look at Phoenix from Microsoft Research.

TraumaPony
Interesting stuff - might be overkill for my purposes, however. Thanks though - +1 =)
Erik Forbes