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
>>> import r2pipe
>>> r2 = r2pipe.open('/bin/ls')
>>> print(r2.cmd('?e Hello World'))
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
r2 = r2pipe.open()
> . test.py
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/
>>> import r2pipe
>>> r2 = r2pipe.open('http://localhost:9090/cmd')
Use r2pipe from your favourite programming language:
$ 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
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
$ r2pm -i lang-python
$ cat core-plugin.py
if s[0:4] == "test":
print("Testing Python core plugin")
"desc": "r2 core plugin in python",
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")