Are there any libraries, callable from .NET, where I can pass in binary data and have it disassembled to x86 assembly code?
4 Answers
If you don't mind binding to an unmanaged dll using P/Invoke, have a look at beaengine, its the best disassembler library your likely to find.
The libdisasm library provides basic disassembly of Intel x86 instructions from a binary stream. The intent is to provide an easy to use disassembler which can be called from any application; the disassembly can be produced in AT&T syntax and Intel syntax, as well as in an intermediate format which includes detailed instruction and operand type information.
The official project provide it as a *nix library. However, folks at Phenoelit wrote a Windows debugger based on this library and ported it to Windows. You can download the source code of the entire application at the bottom of the page (yes, their libdisasm port for Windows is included).
The link provided by cyanic doesn't appear to be available any longer.
If you are after a 100% C# .NET implementation rather than using interop, SharpDisasm provides an x86/x86-64 disassembler to both Intel and AT&T syntax. It decodes each instruction to an object that provides access to low-level information about the instruction (e.g. instruction size, operand number and types etc).
SharpDisasm is a full C# port of the libudis86 C-library disassembler.
The disassembler is exposed through the SharpDisam.Disassembler
class.
Output from the provided example console app:
C:\>echo a1 c9 fd ff ff a1 37 02 00 00 b8 37 02 00 00 b4 09 8a
25 09 00 00 00 8b 04 6d 85 ff ff ff 89 45 f0| disasmcli 32
00000000 a1 c9 fd ff ff mov eax, [0xfffffdc9]
00000005 a1 37 02 00 00 mov eax, [0x237]
0000000a b8 37 02 00 00 mov eax, 0x237
0000000f b4 09 mov ah, 0x9
00000011 8a 25 09 00 00 00 mov ah, [0x9]
00000017 8b 04 6d 85 ff ff ff mov eax, [ebp*2-0x7b]
0000001e 89 45 f0 mov [ebp-0x10], eax
C:\>echo 488b05f7ffffff67668b40f06766035e1048030425ffff
000067660344bef04c0384980000008048a10000000000800000 | disasmcli 64
0000000000000000 48 8b 05 f7 ff ff ff mov rax, [rip-0x9]
0000000000000007 67 66 8b 40 f0 mov ax, [eax-0x10]
000000000000000c 67 66 03 5e 10 add bx, [esi+0x10]
0000000000000011 48 03 04 25 ff ff 00 00 add rax, [0xffff]
0000000000000019 67 66 03 44 be f0 add ax, [esi+edi*4-0x10]
000000000000001f 4c 03 84 98 00 00 00 80 add r8, [rax+rbx*4-0x80000000]
0000000000000027 48 a1 00 00 00 00 00 80 00 00 mov rax, [0x800000000000]
I've tried out the .NET wrappers for BeaEngine and diStorm, but they either crash or do not disassemble at all. I did find a managed port of LibDasm as part of BlackStorms reverse engineering framework, and so far it seems to work. It also contains PE manipulation code, if you need that.