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

If radare2 opens an executable file, by default it will open the file in Virtual Addressing (VA) mode and the sections will be mapped to their virtual addresses. In VA mode, seeking is based on the virtual address and the starting position is set to the entry point of the executable. Using -n option you can suppress this default behavior and ask radare2 to open the file in non-VA mode for you. In non-VA mode, seeking is based on the offset from the beginning of the file.

The 'print' command is abbreviated as p and has a number of submodes — the second letter specifying a desired print mode. Frequent variants include px to print in hexadecimal, and pd for disassembling.

To be allowed to write files, specify the -w option to radare2 when opening a file. The w command can be used to write strings, hexpairs (x subcommand), or even assembly opcodes (a subcommand). Examples:

> w hello world ; string

> wx 90 90 90 90 ; hexpairs

> wa jmp 0x8048140 ; assemble

> wf inline.bin ; write contents of file

Appending a ? to a command will show its help message, for example, p?. Appending ?* will show commands starting with the given string, e.g. p?*.

To enter visual mode, press V<enter>. Use q to quit visual mode and return to the prompt.

In visual mode you can use HJKL keys to navigate (left, down, up, and right, respectively). You can use these keys in cursor mode toggled by c key. To select a byte range in cursor mode, hold down SHIFT key, and press navigation keys HJKL to mark your selection.

While in visual mode, you can also overwrite bytes by pressing i. You can press TAB to switch between the hex (middle) and string (right) columns. Pressing q inside the hex panel returns you to visual mode. By pressing p or P you can scroll different visual mode representations. There is a second most important visual mode - curses-like panels interface, accessible with V! command.

The radare core accepts many flags from the command line.

This is an excerpt from the usage help message:

$ radare2 -h

Usage: r2 [-ACdfLMnNqStuvwzX] [-P patch] [-p prj] [-a arch] [-b bits] [-i file]

[-s addr] [-B baddr] [-m maddr] [-c cmd] [-e k=v] file|pid|-|--|=

-- run radare2 without opening any file

- same as 'r2 malloc://512'

= read file from stdin (use -i and -c to run cmds)

-= perform !=! command to run all commands remotely

-0 print \x00 after init and every command

-2 close stderr file descriptor (silent warning messages)

-a [arch] set asm.arch

-A run 'aaa' command to analyze all referenced code

-b [bits] set asm.bits

-B [baddr] set base address for PIE binaries

-c 'cmd..' execute radare command

-C file is host:port (alias for -c+=http://%s/cmd/)

-d debug the executable 'file' or running process 'pid'

-D [backend] enable debug mode (e cfg.debug=true)

-e k=v evaluate config var

-f block size = file size

-F [binplug] force to use that rbin plugin

-h, -hh show help message, -hh for long

-H ([var]) display variable

-i [file] run script file

-I [file] run script file before the file is opened

-k [OS/kern] set asm.os (linux, macos, w32, netbsd, ...)

-l [lib] load plugin file

-L list supported IO plugins

-m [addr] map file at given address (loadaddr)

-M do not demangle symbol names

-n, -nn do not load RBin info (-nn only load bin structures)

-N do not load user settings and scripts

-q quiet mode (no prompt) and quit after -i

-Q quiet mode (no prompt) and quit faster (quickLeak=true)

-p [prj] use project, list if no arg, load if no file

-P [file] apply rapatch file and quit

-r [rarun2] specify rarun2 profile to load (same as -e dbg.profile=X)

-R [rr2rule] specify custom rarun2 directive

-s [addr] initial seek

-S start r2 in sandbox mode

-t load rabin2 info in thread

-u set bin.filter=false to get raw sym/sec/cls names

-v, -V show radare2 version (-V show lib versions)

-w open file in write mode

-x open without exec-flag (asm.emu will not work), See io.exec

-X same as -e bin.usextr=false (useful for dyldcache)

-z, -zz do not load strings or load them even in raw

Open a file in write mode without parsing the file format headers.

$ r2 -nw file

Quickly get into an r2 shell without opening any file.

$ r2 -

Specify which sub-binary you want to select when opening a fatbin file:

$ r2 -a ppc -b 32 ls.fat

Run a script before showing interactive command-line prompt:

$ r2 -i patch.r2 target.bin

Execute a command and quit without entering the interactive mode:

$ r2 -qc ij hi.bin > imports.json

Set the configuration variable:

$ r2 -e scr.color=0 blah.bin

Debug a program:

$ r2 -d ls

Use an existing project file:

$ r2 -p test

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~calclass="underline" 0 ; get first row

pd 20~calclass="underline" 1 ; get second row

pd 20~call[0] ; get first column

pd 20~call[1] ; get second column

Or even combine them:

pd 20~calclass="underline" 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.