r2pipe
The best programming interface to radare2 called r2pipe.
To quote from the project's Github page:
The r2pipe APIs are based on a single r2 primitive found behind
r_core_cmd_str()
which is a function that accepts a string parameter
describing the r2 command to run and returns a string with the result.
r2pipe supports many programming languages including Python, NodeJS, Rust, C and others.
Writing the program
To use r2pipe you should first make sure you have radare2 installed. It is always recommended to use the latest version from git.
Install or update radare2 with the following steps:
$ git clone https://github.com/radare/radare2.git # clone radare2 if you didn't do it yet for some reason.
$ cd radare2
$ ./sys/install.sh
Now after radare2 is installed and updated let's move to write the C program to list all the functions of the binary. As you've mentioned in your question, it should be the equivalent for opening the binary with radare2, executing aaa
to analyze the binary (which is actually r2 -A binary_name
) and then execute afl
to list all the functions.
First we should define a function that accepts r2 instance and command. This function will execute the command, print the results and free the resulting string.
#include <r_socket.h>
static void r2cmd(R2Pipe *r2, const char *cmd) {
char *msg = r2p_cmd (r2, cmd);
if (msg) {
printf ("%s\n", msg);
free (msg);
}
}
After that, writing the main()
function should be intuitive:
int main() {
R2Pipe *r2 = r2p_open ("r2 -q0 /bin/echo");
if (r2) {
r2cmd (r2, "aaa");
r2cmd (r2, "afl");
r2p_close (r2);
return 0;
}
return 1;
}
We define an instance of r2 with the program we want to examine, and then use the function we just created to execute commands through radare2's interface. First we analyze the program using aaa
and then we print the functions using afl
.
Compiling
To compile the program using gcc
we should execute the following command:
gcc list_functions.c -o list_functions $(pkg-config --cflags r_socket --libs r_socket)
References