The Official Radare2 Book — страница 4 из 64

A general format for radare2 commands is as follows:

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

People who use Vim daily and are familiar with its commands will find themselves at home. You will see this format used throughout the book. Commands are identified by a single case-sensitive character [a-zA-Z].

To repeatedly execute a command, prefix the command with a number:

px # run px

3px # run px 3 times

The ! prefix is used to execute a command in shell context. If you want to use the cmd callback from the I/O plugin you must prefix with =!.

Note that a single exclamation mark will run the command and print the output through the RCons API. This means that the execution will be blocking and not interactive. Use double exclamation marks -- !! -- to run a standard system call.

All the socket, filesystem and execution APIs can be restricted with the cfg.sandbox configuration variable.

A few examples:

ds ; call the debugger's 'step' command

px 200 @ esp ; show 200 hex bytes at esp

pc > file.c ; dump buffer as a C byte array to file.c

wx 90 @@ sym.* ; write a nop on every symbol

pd 2000 | grep eax ; grep opcodes that use the 'eax' register

px 20 ; pd 3 ; px 40 ; multiple commands in a single line

The standard UNIX pipe | is also available in the radare2 shell. You can use it to filter the output of an r2 command with any shell program that reads from stdin, such as grep, less, wc. If you do not want to spawn anything, or you can't, or the target system does not have the basic UNIX tools you need (Windows or embedded users), you can also use the built-in grep (~).

See ~? for help.

The ~ character enables internal grep-like function used to filter output of any command:

pd 20~call ; disassemble 20 instructions and grep output for 'call'

Additionally, you can grep either for columns or for rows:

pd 20~call:0 ; get first row

pd 20~call:1 ; get second row

pd 20~call[0] ; get first column

pd 20~call[1] ; get second column

Or even combine them:

pd 20~call:0[0] ; grep the first column of the first row matching 'call'

This internal grep function is a key feature for scripting radare2, because it can be used to iterate over a list of offsets or data generated by disassembler, ranges, or any other command. Refer to the loops section (iterators) for more information.

The @ character is used to specify a temporary offset at which the command to its left will be executed. The original seek position in a file is then restored.

For example, pd 5 @ 0x100000fce to disassemble 5 instructions at address 0x100000fce.

Most of the commands offer autocompletion support using key, for example seek or flags commands. It offers autocompletion using all possible values, taking flag names in this case. Note that it is possible to see the history of the commands using the !~... command - it offers a visual mode to scroll through the radare2 command history.

To extend the autocompletion support to handle more commands or enable autocompletion to your own commands defined in core, I/O plugins you must use the !!! command.

Expressions

Expressions are mathematical representations of 64-bit numerical values. They can be displayed in different formats, be compared or used with all commands accepting numeric arguments. Expressions can use traditional arithmetic operations, as well as binary and boolean ones. To evaluate mathematical expressions prepend them with command ?:

[0xb7f9d810]> ?vi 0x8048000

134512640

[0xv7f9d810]> ?vi 0x8048000+34

134512674

[0xb7f9d810]> ?vi 0x8048000+0x34

134512692

[0xb7f9d810]> ? 1+2+3-4*3

hex 0xfffffffffffffffa

octal 01777777777777777777772

unit 17179869184.0G

segment fffff000:0ffa

int64 -6

string "\xfa\xff\xff\xff\xff\xff\xff\xff"

binary 0b1111111111111111111111111111111111111111111111111111111111111010

fvalue: -6.0

float: nanf

double: nan

trits 0t11112220022122120101211020120210210211201

Supported arithmetic operations are:

   • + : addition

   • - : subtraction

   • * : multiplication

   • / : division

   • % : modulus

   • > : shift right

   • < : shift left

[0x00000000]> ?vi 1+2+3

6

To use of logical OR should quote the whole command to avoid executing the | pipe:

[0x00000000]> "? 1 | 2"

hex 0x3

octal 03

unit 3

segment 0000:0003

int32 3

string "\x03"

binary 0b00000011

fvalue: 2.0

float: 0.000000f

double: 0.000000

trits 0t10

Numbers can be displayed in several formats:

0x033 : hexadecimal can be displayed

3334 : decimal

sym.fo : resolve flag offset

10K : KBytes 10*1024

10M : MBytes 10*1024*1024

You can also use variables and seek positions to build complex expressions.

Use the ?$? command to list all the available commands or read the refcard chapter of this book.

$$ here (the current virtual seek)

$l opcode length

$s file size

$j jump address (e.g. jmp 0x10, jz 0x10 => 0x10)

$f jump fail address (e.g. jz 0x10 => next instruction)

$m opcode memory reference (e.g. mov eax,[0x10] => 0x10)

$b block size

Some more examples:

[0x4A13B8C0]> ? $m + $l

140293837812900 0x7f98b45df4a4 03771426427372244 130658.0G 8b45d000:04a4 140293837812900 10100100 140293837812900.0 -0.000000

[0x4A13B8C0]> pd 1 @ +$l

0x4A13B8C2 call 0x4a13c000

Basic Debugger Session

To debug a program, start radare with the -d option. Note that you can attach to a running process by specifying its PID, or you can start a new program by specifying its name and parameters:

$ pidof mc

32220

$ r2 -d 32220

$ r2 -d /bin/ls

$ r2 -a arm -b 16 -d gdb://192.168.1.43:9090

...

In the second case, the debugger will fork and load the debugee ls program in memory.

It will pause its execution early in ld.so dynamic linker. As a result, you will not yet see the entrypoint or any shared libraries at this point.

You can override this behavior by setting another name for an entry breakpoint. To do this, add a radare command e dbg.bep=entry or e dbg.bep=main to your startup script, usually it is ~/.config/radare2/radare2rc.

Another way to continue until a specific address is by using the dcu command. Which means: "debug continue until" taking the address of the place to stop at. For example:

dcu main

Be warned that certain malware or other tricky programs can actually execute code before main() and thus you'll be unable to control them. (Like the program constructor or the tls initializers)

Below is a list of most common commands used with debugger:

> d? ; get help on debugger commands

> ds 3 ; step 3 times

> db 0x8048920 ; setup a breakpoint

> db -0x8048920 ; remove a breakpoint

> dc ; continue process execution

> dcs ; continue until syscall

> dd ; manipulate file descriptors

> dm ; show process maps

> dmp A S rwx ; change permissions of page at A and size S

> dr eax=33 ; set register value. eax = 33

There is another option for debugging in radare, which may be easier: using visual mode.

That way you will neither need to remember many commands nor to keep program state in your mind.

To enter visual debugger mode use Vpp:

[0xb7f0c8c0]> Vpp

The initial view after entering visual mode is a hexdump view of the current target program counter (e.g., EIP for x86). Pressing p will allow you to cycle through the rest of visual mode views. You can press p and P to rotate through the most commonly used print modes. Use F7 or s to step into and F8 or S to step over current instruction. With the c key you can toggle the cursor mode to mark a byte range selection (for example, to later overwrite them with nop). You can set breakpoints with F2 key.

In visual mode you can enter regular radare commands by prepending them with :. For example, to dump a one block of memory contents at ESI:

x @ esi

To get help on visual mode, press ?. To scroll the help screen, use arrows. To exit the help view, press q.

A frequently used command is dr, which is used to read or write values of the target's general purpose registers. For a more compact register value representation you might use dr= command. You can also manipulate the hardware and the extended/floating point registers.

Contributing

Radare2 Book

If you want to contribute to the Radare2 book, you can do it at the Github repository. Suggested contributions include:

   • Crackme writeups

   • CTF writeups

   • Documentation on how to use Radare2

   • Documentation on developing for Radare2

   • Conference presentations/workshops using Radare2

   • Missing content from the Radare1 book updated to Radare2

Please get permission to port any content you do not own/did not create before you put it in the Radare2 book.

See https://github.com/radareorg/radare2/blob/master/DEVELOPERS.md for general help on contributing to radare2.

Configuration