Выбрать главу

radare2 will first try to load /usr/share/radare2/radare2rc

Each user in the system can have its own r2 scripts to run on startup to select the color scheme, and other custom options by having r2 commands in there.

   • ~/.radare2rc

   • ~/.config/radare2/radare2rc

   • ~/.config/radare2/radare2rc.d/

If you want to run a script everytime you open a file, just create a file with the same name of the file but appending .r2 to it.

Most command names in radare are derived from action names. They should be easy to remember, as they are short. Actually, all commands are single letters. Subcommands or related commands are specified using the second character of the command name. For example, / foo is a command to search plain string, while /x 90 90 is used to look for hexadecimal pairs.

The general format for a valid command (as explained in the Command Format chapter) looks like this:

[.][times][cmd][~grep][@[@iter]addr!size][|>pipe] ; ...

For example,

> 3s +1024 ; seeks three times 1024 from the current seek

If a command starts with =!, the rest of the string is passed to the currently loaded IO plugin (a debugger, for example). Most plugins provide help messages with =!? or =!help.

$ r2 -d /bin/ls

> =!help ; handled by the IO plugin

If a command starts with !, posix_system() is called to pass the command to your shell. Check !? for more options and usage examples.

> !ls ; run `ls` in the shell

The meaning of the arguments (iter, addr, size) depends on the specific command. As a rule of thumb, most commands take a number as an argument to specify the number of bytes to work with, instead of the currently defined block size. Some commands accept math expressions or strings.

> px 0x17 ; show 0x17 bytes in hexs at current seek

> s base+0x33 ; seeks to flag 'base' plus 0x33

> / lib ; search for 'lib' string.

The @ sign is used to specify a temporary offset location or a seek position at which the command is executed, instead of current seek position. This is quite useful as you don't have to seek around all the time.

> p8 10 @ 0x4010 ; show 10 bytes at offset 0x4010

> f patata @ 0x10 ; set 'patata' flag at offset 0x10

Using @@ you can execute a single command on a list of flags matching the glob. You can think of this as a foreach operation:

> s 0

> / lib ; search 'lib' string

> p8 20 @@ hit0_* ; show 20 hexpairs at each search hit

The > operation is used to redirect the output of a command into a file (overwriting it if it already exists).

> pr > dump.bin ; dump 'raw' bytes of current block to file named 'dump.bin'

> f > flags.txt ; dump flag list to 'flags.txt'

The | operation (pipe) is similar to what you are used to expect from it in a *NIX shelclass="underline" an output of one command as input to another.

[0x4A13B8C0]> f | grep section | grep text

0x0805f3b0 512 section._text

0x080d24b0 512 section._text_end

You can pass several commands in a single line by separating them with a semicolon ;:

> px ; dr

Using _, you can print the result that was obtained by the last command.

[0x00001060]> axt 0x00002004

main 0x1181 [DATA] lea rdi, str.argv__2d_:__s

[0x00001060]> _

main 0x1181 [DATA] lea rdi, str.argv__2d_:__s

To move around the file we are inspecting we will need to change the offset at which we are using the s command.

The argument is a math expression that can contain flag names, parenthesis, addition, substraction, multiplication of immediates of contents of memory using brackets.

Some example commands:

[0x00000000]> s 0x10

[0x00000010]> s+4

[0x00000014]> s-

[0x00000010]> s+

[0x00000014]>

Observe how the prompt offset changes. The first line moves the current offset to the address 0x10.

The second does a relative seek 4 bytes forward.

And finally, the last 2 commands are undoing, and redoing the last seek operations.

Instead of using just numbers, we can use complex expressions, or basic arithmetic operations to represent the address to seek.

To do this, check the ?$? Help message which describes the internal variables that can be used in the expressions. For example, this is the same as doing s+4 .

[0x00000000]> s $$+4

From the debugger (or when emulating) we can also use the register names as references. They are loaded as flags with the .dr* command, which happens under the hood.

[0x00000000]> s rsp+0x40

Here's the full help of the s command. We will explain in more detail below.

[0x00000000]> s?

Usage: s # Help for the seek commands. See ?$? to see all variables

| s Print current address

| s.hexoff Seek honoring a base from core->offset

| s:pad Print current address with N padded zeros (defaults to 8)

| s addr Seek to address

| s- Undo seek

| s-* Reset undo seek history

| s- n Seek n bytes backward

| s--[n] Seek blocksize bytes backward (/=n)

| s+ Redo seek

| s+ n Seek n bytes forward

| s++[n] Seek blocksize bytes forward (/=n)

| s[j*=!] List undo seek history (JSON, =list, *r2, !=names, s==)

| s/ DATA Search for next occurrence of 'DATA'

| s/x 9091 Search for next occurrence of \x90\x91

| sa [[+-]a] [asz] Seek asz (or bsize) aligned to addr

| sb Seek aligned to bb start

| sC[?] string Seek to comment matching given string

| sf Seek to next function (f->addr+f->size)

| sf function Seek to address of specified function

| sf. Seek to the beginning of current function

| sg/sG Seek begin (sg) or end (sG) of section or file

| sl[?] [+-]line Seek to line

| sn/sp ([nkey]) Seek to next/prev location, as specified by scr.nkey

| so [N] Seek to N next opcode(s)

| sr pc Seek to register

| ss Seek silently (without adding an entry to the seek history)

> 3s++ ; 3 times block-seeking

> s 10+0x80 ; seek at 0x80+10

If you want to inspect the result of a math expression, you can evaluate it using the ? command. Simply pass the expression as an argument. The result can be displayed in hexadecimal, decimal, octal or binary formats.

> ? 0x100+200

0x1C8 ; 456d ; 710o ; 1100 1000

There are also subcommands of ? that display the output in one specific format (base 10, base 16 ,...). See ?v and ?vi.

In the visual mode, you can press u (undo) or U (redo) inside the seek history to return back to previous or forward to the next location.

As a test file, let's use a simple hello_world.c compiled in Linux ELF format. After we compile it let's open it with radare2:

$ r2 hello_world

Now we have the command prompt:

[0x00400410]>

And it is time to go deeper.