r2pipe
The simplest and most effective way to script radare2, which consists in 1 method API that takes a string representing the r2 command to run and returns the output as a string.
Checkout more examples in the r2pipe repository.
$ pip3 install r2pipe
$ python3
>>> import r2pipe
>>> r2 = r2pipe.open('/bin/ls')
>>> print(r2.cmd('?e Hello World'))
Hello World
>>> r2.quit()
JSON
Many r2 commands print JSON as output when the 'j' suffix is used, many r2pipe implementations provide the `.cmdj()` API to automatically parse the json and return a native object for the language.
$ r2 /bin/ls
> cat test.py
import r2pipe
r2 = r2pipe.open()
print(r2.cmdj('ij')['core']['type'])
> . test.py
Executable file
Open
The argument passed to r2pipe.open() selects the way to talk to r2, this is by spawning a new process, sending commands to the r2 http webserver, using dlopen+dlsym, etc
$ r2 -c=H /bin/ls
Starting http server at http://localhost:9090/
$ python
>>> import r2pipe
>>> r2 = r2pipe.open('http://localhost:9090/cmd')
Languages
Use r2pipe from your favourite programming language:
$ pwd
~/prg/radare2-r2pipe
$ echo $(ls -F | grep /|sed -e 's./.,.')
c, clojure, cxx-qt, dlang, dotnet, erlang, go, haskell, java,
lisp, newlisp, nim, nodejs, ocaml, perl, php, prolog, python,
r2core-js, ruby, rust, shell, swift, v, vala
Plugins
Radare2 plugins can written via r2pipe using the rlang plugins.
RLang plugins run an instance of the language VM inside r2:
- io, asm, anal, bin, core plugins in !C
- faster execution of scripts
- keep global state
- repl
$ r2pm -i lang-python
$ cat core-plugin.py
import r2lang
def pycore(a):
def _call(s):
if s[0:4] == "test":
print("Testing Python core plugin")
return 1
return 0
return {
"name": "PyCore",
"license": "MIT",
"desc": "r2 core plugin in python",
"call": _call,
}
r2lang.plugin('core', pycore)
Bindings
The low level C APIs are also available for other programmming language thanks to Valabind and Swig, they are not very well maintained, but they will at some point, contribs are welcome.
$ r2pm -i r2api-python
$ cat test-core-api.py
from r_core import RCore
core = RCore()
core.file_open("/bin/ls", False, 0)
core.bin_load("/bin/ls", 0, 0, 0, 0, 0)
entry0 = core.cmd_str("?v entry0")
print(entry0)