Radare2 IO plugin tutorial
15 Apr 2018I recently had to write my own IO plugin for Radare2, and as i couldn’t find a good tutorial on the Internet to, i hope this one will help you get going.
You can take a look at the official documentation first: Radare2 plugins documentation
We are going to create an IO plugin called foo
.
You can download the skeleton from this URL Or just
git clone https://github.com/Wenzel/radare2-extras -b skel
List of the files in skel/
total 24K
-rw-r--r--. 1 wenzel wenzel 53 9 avril 22:27 foo.mk
-rw-r--r--. 1 wenzel wenzel 2,5K 9 avril 22:34 io_foo.c
-rw-r--r--. 1 wenzel wenzel 90 9 avril 22:29 io_foo.h
-rw-r--r--. 1 wenzel wenzel 249 9 avril 22:27 Makefile
-rw-r--r--. 1 wenzel wenzel 158 9 avril 22:16 r2.mk
-rw-r--r--. 1 wenzel wenzel 242 15 avril 20:50 README.md
Makefile
foo.mk
will define the TARGETS
that we want to build (io_foo.so
), and
r2.mk
will define where the plugin should be installed as well as the CFLAGS
to link with radare2
.
Plugin declaration
Let’s have a look at io_foo.c
and go to the end of the file:
This structure defines our plugin name
, description
(r2 -L
), and allows radare2
call our
own implementation of read
, write
, etc…
This one declares our plugin to radare2
, and set the type to IO plugin
with
R_LIB_TYPE_IO
.
plugin_open
Now the first function you want to implement is __plugin_open
:
As you can see, it’s already implemented. Quite trivial actually. radare2
will
call this function with a given pathname
to check if the prefix
matches when
a URL is opened.
our prefix is:
You might want to change that.
open
Next function too look at is __open
:
This one is called ` after __plugin_open
, when radare2
wants to initialize
our plugin.
you are given the pathname
: (foo://something
), the flags
(read, write)
, and the
mode
In this function you need to initialize your own data structure, (i called it
RIOFoo
), because it’s the only way to maintain some data accross the function
calls (see later).
And when you are done, return a r_io_desc_new()
to create the RIODesc *
.
close
Let’s implement __close
now.
Simply destroy your own RIOFoo
and free the
memory !
You can see that we use the RIODesc->data
to get our RIOFoo *
structure.
lseek
This function is called when radare2
needs to move to another position in the
file, typically with the s
command.
I stole this implementation from another plugin. That’s what you want in most of the cases. To get the details, ask on the Telegram channel.
read
This one should be easy too, radare2
wants to read a buffer buf, at a certain
position, from your
file/whatever`:
write
Same as above, but writing a buffer:
system
You can implement specific commands in your IO plugin.
When you type !=command
from the radare2
shell, this function will be called.
I didn’t had to use this, so you will have to implement it by yourself !
And we should have most of the main functions now.
To compile, run make
, and install with make install
you should see your plugin with r2 -L
rwd windbg Attach to a KD debugger (windbg://socket) (LGPL3)
rwd winedbg Wine-dbg io and debug.io plugin for r2 (MIT)
rw_ zip Open zip files [apk|ipa|zip|zipall]://[file//path] (BSD)
rwd foo IO Foo plugin (LGPL)
And run it with r2 foo://something
!