Scripting

Radare2 comes with a powerful API: we used to have classic bindings, but it required people to learn how to use radare2 twice: once for the command line, and a second time for the bindings. So we deprecated the bindings and created r2pipe.

It’s a magical pipe where you throw radare2 commands at, and it’ll answer you their results. We wrote a tutorial about how to use them from Node.js and JavaScript. But don’t worry, r2pipe is usable from a large number of languages: Python, Go, PHP, OCaml, Ruby, awk, …

Examples

Python

import r2pipe

r2 = r2pipe.open("/bin/ls")
r2.cmd('aa')
print(r2.cmd("afl"))
print(r2.cmdj("aflj"))  # evaluates JSONs and returns an object
r2.quit()

Haskell

import R2pipe
import qualified Data.ByteString.Lazy as L

showMainFunction ctx = do
    cmd ctx "s main"
    L.putStr =<< cmd ctx "pD `fl $$`"

main = do
    -- Run r2 locally
    open "/bin/ls" >>= showMainFunction
    -- Connect to r2 via HTTP (e.g. if "r2 -qc=h /bin/ls" is running)
    open "http://127.0.0.1:9090" >>= showMainFunction

Node.js

var r2pipe = require('r2pipe');

function doStuff (err, r2p) {
  if (!err) {
    console.log (r2p.cmdj('ij'));
  } else  {
    console.log(`Error: ${err.message}`)
  }
  r2p.quit();
}

/* rlang r2pipe script ( r2 -qi foo.js /bin/ls ) */
r2pipe.open (doStuff);

r2pipe.open ('/bin/ls', doStuff);

r2pipe.open ('http://cloud.radare.org/cmd/', doStuff);