All Projects → mytbk → r2dumpbin

mytbk / r2dumpbin

Licence: MIT license
A radare2 Python script to dump a raw IA32 binary to an NASM source file

Programming Languages

assembly
5116 projects
python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to r2dumpbin

R2m2
radare2 + miasm2 = ♥
Stars: ✭ 86 (+290.91%)
Mutual labels:  radare2
Jni helper
Find JNI function signatures in APK and apply to reverse tools.
Stars: ✭ 154 (+600%)
Mutual labels:  radare2
Radare2
UNIX-like reverse engineering framework and command-line toolset
Stars: ✭ 15,412 (+69954.55%)
Mutual labels:  radare2
Radare2 Bindings
Bindings of the r2 api for Valabind and friends
Stars: ✭ 100 (+354.55%)
Mutual labels:  radare2
R2con2019
r2con2019 - slides and materials
Stars: ✭ 128 (+481.82%)
Mutual labels:  radare2
Radeco
radare2-based decompiler and symbol executor
Stars: ✭ 313 (+1322.73%)
Mutual labels:  radare2
R2frida
Radare2 and Frida better together.
Stars: ✭ 610 (+2672.73%)
Mutual labels:  radare2
Guanciale
🥓 Grab info needed by Carbonara from executables and disassemblers databases
Stars: ✭ 14 (-36.36%)
Mutual labels:  radare2
R2vmi
Hypervisor-Level Debugger based on Radare2 / LibVMI, using VMI IO and debug plugins
Stars: ✭ 130 (+490.91%)
Mutual labels:  radare2
Awesome Reverse Engineering
Reverse Engineering Resources About All Platforms(Windows/Linux/macOS/Android/iOS/IoT) And Every Aspect! (More than 3500 open source tools and 2300 posts&videos)
Stars: ✭ 2,954 (+13327.27%)
Mutual labels:  radare2
Reversing List
Reversing list
Stars: ✭ 106 (+381.82%)
Mutual labels:  radare2
Iaito
This project has been moved to:
Stars: ✭ 1,516 (+6790.91%)
Mutual labels:  radare2
Radare2 Extras
Source graveyard and random candy for radare2
Stars: ✭ 202 (+818.18%)
Mutual labels:  radare2
Radare2 Pm
Package Manager for Radare2
Stars: ✭ 100 (+354.55%)
Mutual labels:  radare2
re-scripts
IDA, Ghidra and Radare2 scripts. Also Android scripts to make your life easier.
Stars: ✭ 47 (+113.64%)
Mutual labels:  radare2
Arcore Patch
Attempt to get ARCore Preview 2 running on unsupported devices
Stars: ✭ 74 (+236.36%)
Mutual labels:  radare2
R2frida Wiki
This repo aims at providing practical examples on how to use r2frida
Stars: ✭ 168 (+663.64%)
Mutual labels:  radare2
ghidra-r2web
Ghidra plugin to start an r2 webserver to let r2 interact with it
Stars: ✭ 38 (+72.73%)
Mutual labels:  radare2
flashre
Tools to reverse the Toshiba FlashAir SD cards
Stars: ✭ 23 (+4.55%)
Mutual labels:  radare2
Radare2 R2pipe
Access radare2 via pipe from any programming language!
Stars: ✭ 212 (+863.64%)
Mutual labels:  radare2

r2dumpbin - an IA32 binary recovery tool

r2dumpbin is a radare2 script to dump a binary to assembly which can be assembled with NASM.

I wrote this script for working with the coreboot project to convert mrc.bin to assembly, so that I can link it with coreboot romstage, and convert part of assembly code in it to C code, and at last, fully reverse all the code to readable C code.

While recovering the assembly to readable C code is hard work even with Ghidra decompiler released in 2019, I found it easy to do some binary modding or instrumentation after getting an assembly dump. Now I try to use this tool to recover more kinds of binaries besides mrc.bin.

The script is tested in radare2 5.2.1 and python-r2pipe 5.1.0 from Arch Linux.

What does this script do?

The entry point of an mrc.bin is at the beginning of the file, and the binary is loaded at a fixed address (0xfffa0000). Structured binary objects such as PE files have their memory mappings and an entry point.

The script analyzes the code from the entry point, and does the following:

  • Find out all the calls, branches and unconditional jumps to find all the executable code in the binary. This can be done by both the script and radare2 aa series commands.
  • In the non-code areas of the binary, find out all the pointers that points to other places in the binary, so that the result assembly code can be relocatable and linkable.

Usage

Install radare2 and Python r2pipe:

# for Arch use pacman
pacman -S python-r2pipe
# if using other distro, you'd better install radare2 from git
# and install r2pipe with ``pip install --user r2pipe``

r2dumpbin can be used both as a script and a library, the following shows how to use it as a script to dump mrc.bin and Win32 PE files.

Dump mrc.bin

Originally I using this tool to dump Haswell mrc.bin to assembly code. The assembly code can be assembled to an object file linkable with coreboot romstage. You can check it out at https://github.com/mytbk/coreboot/tree/haswell-dumpbin (not maintained now).

Load the binary with with radare2, and flag the load virtual address:

$ r2 mrc.bin
[0x00000000]> f va @ 0xfffa0000

At last, use the script to dump mrc.bin to mrc.asm:

[0x00000000]> . dumpbin.py > mrc.asm

After you get mrc.asm, you can assemble it with nasm, and find the resulting binary identical to the origin mrc.bin:

nasm mrc.asm # the generated binary is `mrc`
sha1sum mrc mrc.bin

If you want to link it with other code, you need to:

  • remove the org 0xfffa0000 line
  • rename the entry point label, and make it global
  • use -f elf to generate an ELF object when assembling the code.

Dump Win32 PE files

This tool has initial support for Win32 PE files. It can now dump notepad.exe from wine, and the assembly code can be built to a working notepad. Currently the script only support running under Arch Linux with mingw-w64-crt package installed for Win32 symbol resolution.

An example is located at test/pe:

$ r2 test/pe/notepad.exe
[0x00406a20]> . ./dumpbin_pe.py > notepad.asm

And you can assemble notepad.asm and link it to notepad.exe:

$ nasm -f win32 notepad.asm
# see the link flag comment in notepad.asm for flags and libs
$ i686-w64-mingw32-ld -o notepad_new.exe notepad.obj   -e fcn_00406a20 -lkernel32 -ladvapi32 -lcomdlg32 -lgdi32 -lshell32 -lshlwapi -lucrtbase -luser32

Debug

To debug this script, you can modify dumpbin.py to change the debug level. Then run r2 like:

r2 -qc 'f va @ 0xfffa0000; . dumpbin.py > haswell-mrc.asm' haswell-mrc.bin 2>debug.log

A more flexible way is to use r2dumpbin as a library, write your workflow in your script, and debug it.

Bug

It is very hard to disassemble an object to correct code. There may be bugs including:

  • pointers wrongly recognized or ignored when analyzing non-relocatable objects
  • assembly doesn't built to identical code, which needs fix up
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].