The Debugging (d) Module is used to debug the binary. This module sets breakpoints, views registers, and runs the binary. It also drives our dynamic analysis, which involves actively running the binary and observing its behavior. This is done by watching the registers, the stack, and the instructions as they are executed.
In radare2, dynamic analysis is done in debug mode. We can enter debug mode by opening a binary and using the -d flag.
d?Usage: d # Debug commands| d:[?] [cmd] run custom debug plugin command| db[?] breakpoints commands| dbt[?] display backtrace based on dbg.btdepth and dbg.btalgo| dc[?] continue execution| dd[?][*+-tsdfrw] manage file descriptors for child process| de[-sc] [perm] [rm] [e] debug with ESIL (see de?)| dg <file> generate a core-file (WIP)| dh [plugin-name] select a new debug handler plugin (see dbh)| dH [handler] transplant process to a new handler| di[?] show debugger backend information (See dh)| dk[?] list, send, get, set, signal handlers of child| dL[?] list or set debugger handler| dm[?] show memory maps| do[?] open process (reload, alias for 'oo')| doo[args] reopen in debug mode with args (alias for 'ood')| doof[file] reopen in debug mode from file (alias for 'oodf')| doc close debug session| dp[?] list, attach to process or thread id| dr[?] cpu registers| ds[?] step, over, source line| dt[?] display instruction traces| dw <pid> block prompt until pid dies| dx[?][aers] execute code in the child process| date [-b] use -b for beat timeBreakpoints and watchpoints pause execution at a specific point in the program. This is useful for debugging and reverse engineering. Radare2 has several commands for managing breakpoints and watchpoints.
The db submodule is responsible for breakpoints. This module allows you to create, delete, and manage breakpoints. We can use db? to see the available commands.
You set breakpoints using db plus the address or symbol. The argument can also be a symbol plus an offset, which radare2 will resolve to its proper address. Here are some examples of setting breakpoints:
db maindb sym.read_in+8db 0x080491e2Use db to view the breakpoint list. You can use dbi to list the breakpoints by index; however, this only shows their address, not their name.
0x0804923c - 0x0804923d 1 --x sw break enabled valid cmd="" cond="" name="main" module="./args"0x080491f7 - 0x080491f8 1 --x sw break enabled valid cmd="" cond="" name="sym.read_in+8" module="./args"0x080491e2 - 0x080491e3 1 --x sw break enabled valid cmd="" cond="" name="0x080491e2" module="./args"To remove a breakpoint, use db -<name> or db -<address>. You can use dbi -<index> to remove a breakpoint by its index.
db -maindbi -2Use the dbe and dbd commands to enable and disable breakpoints, respectively. The dbie and dbid commands work the same way, using indices for their arguments.
dbe maindbd maindbie 1dbid 1Watchpoints are breakpoints that are triggered when a specific memory address is accessed. This is useful for detecting when a variable is changed. We can use dbw to set a watchpoint. This takes two arguments: the address to watch and the watch flags (read, write, or both).
dbw 0x0804a000 rwThe list of watchpoints is stored in db with the breakpoints.
This section will cover how to view registers, memory segments, and the heap.
Use the dr submodule to get more information about the registers.
dr? to view the help pages for this submodule.dreax = 0x0804923cebx = 0xf7e2a000ecx = 0x8e819449edx = 0xffe4a930esi = 0xffe4a9c4edi = 0xf7f47b80esp = 0xffe4a90cebp = 0xf7f48020eip = 0x0804923ceflags = 0x00000246oeax = 0xffffffffUse the dm submodule to get more information about the memory segments.
dm0x08048000 - 0x08049000 - usr 4K s r-- ./args ./args ; segment.ehdr0x08049000 - 0x0804a000 * usr 4K s r-x ./args ./args ; map._home_user_args.r_x0x0804a000 - 0x0804b000 - usr 4K s r-- ./args ./args ; map._home_user_args.r__0x0804b000 - 0x0804c000 - usr 4K s r-- ./args ./args ; map._home_user_args.rw_0x0804c000 - 0x0804d000 - usr 4K s rw- ./args ./args ; obj._GLOBAL_OFFSET_TABLE_0xf7c00000 - 0xf7c20000 - usr 128K s r-- /usr/lib/i386-linux-gnu/libc.so.6 /usr/lib/i386-linux-gnu/libc.so.60xf7c20000 - 0xf7da2000 - usr 1.5M s r-x /usr/lib/i386-linux-gnu/libc.so.6 /usr/lib/i386-linux-gnu/libc.so.60xf7da2000 - 0xf7e27000 - usr 532K s r-- /usr/lib/i386-linux-gnu/libc.so.6 /usr/lib/i386-linux-gnu/libc.so.60xf7e27000 - 0xf7e28000 - usr 4K s --- /usr/lib/i386-linux-gnu/libc.so.6 /usr/lib/i386-linux-gnu/libc.so.60xf7e28000 - 0xf7e2a000 - usr 8K s r-- /usr/lib/i386-linux-gnu/libc.so.6 /usr/lib/i386-linux-gnu/libc.so.60xf7e2a000 - 0xf7e2b000 - usr 4K s rw- /usr/lib/i386-linux-gnu/libc.so.6 /usr/lib/i386-linux-gnu/libc.so.6 ; ebx0xf7e2b000 - 0xf7e35000 - usr 40K s rw- unk0 unk00xf7f09000 - 0xf7f0b000 - usr 8K s rw- unk1 unk10xf7f0b000 - 0xf7f0f000 - usr 16K s r-- [vvar] [vvar] ; map._vvar_.r__0xf7f0f000 - 0xf7f11000 - usr 8K s r-x [vdso] [vdso] ; map._vdso_.r_x0xf7f11000 - 0xf7f12000 - usr 4K s r-- /usr/lib/i386-linux-gnu/ld-linux.so.2 /usr/lib/i386-linux-gnu/ld-linux.so.20xf7f12000 - 0xf7f37000 - usr 148K s r-x /usr/lib/i386-linux-gnu/ld-linux.so.2 /usr/lib/i386-linux-gnu/ld-linux.so.2 ; map._usr_lib_i386_linux_gnu_ld_linux.so.2.r_x0xf7f37000 - 0xf7f46000 - usr 60K s r-- /usr/lib/i386-linux-gnu/ld-linux.so.2 /usr/lib/i386-linux-gnu/ld-linux.so.2 ; map._usr_lib_i386_linux_gnu_ld_linux.so.2.r__We can use the dm. command to find the memory segment of a specific address. It defaults to the seek address if no address is provided.
dm.0x08049000 - 0x0804a000 * usr 4K s r-x ./args ./args ; map._home_user_args.r_xdm. @ 0xffe4a90c0xffe2c000 - 0xffe4d000 * usr 132K s rwx [stack] [stack] ; map._stack_.rwxThis submodule provides several commands to allocate, deallocate, and map virtual memory. I don't have any writeups using the write flag, but I might add this in the future.
The dmh submodule is responsible for displaying information about the heap. Use dmh to show a map of the heap:
dmh Malloc chunk @ 0x55a7ecbce250 [size: 0x411][allocated] Top chunk @ 0x55a7ecbce660 - [brk_start: 0x55a7ecbce000, brk_end: 0x55a7ecbef000]Alternatively, use dmhg to get a graph of the heap:
dmhgHeap Layout .------------------------------------. | Malloc chunk @ 0x55a7ecbce000 | | size: 0x251 | | fd: 0x0, bk: 0x0 | `------------------------------------' | .---' | | .---------------------------------------------. | Malloc chunk @ 0x55a7ecbce250 | | size: 0x411 | | fd: 0x57202c6f6c6c6548, bk: 0xa21646c726f | `---------------------------------------------' | .---' | |.----------------------------------------------------.| Top chunk @ 0x55a7ecbce660 || [brk_start:0x55a7ecbce000, brk_end:0x55a7ecbef000] |`----------------------------------------------------'dmhbdmhb command, you must set dbg.glibc.demangle to be true.Upon opening radare2, the binary is already running. Radare2 defaults to the binary's entry point (listed in rabin2's output) and puts a temporary breakpoint at that location. This is often inconvenient for us since we don't care much about this compiler-generated code. We can set our own breakpoint at the main function and then continue execution there.
Use dc to continue execution to the next breakpoint.
db maindcINFO: hit breakpoint at: 0x804923cAs we are navigating the output, scanning through instructions and the stack, we might lose the place of the instruction pointer. An important note in radare2 is that the seek address is not the same as the instruction pointer. This causes confusion with new users as they attempt to understand where they are in the binary.
The best way to return to the instruction pointer is to use s eip. If you are in visual mode, the . command also returns the display to the instruction pointer.
There are two kinds of steps when using a debugger: stepping in and stepping over. Stepping commands are outlined in the ds submodule.
To step in, use the ds instruction. To step over, use the dso instruction. You can use ds <num> or dso <num> to step <num> instructions.
s is for stepping in and S is for stepping over.You can use the dc command to continue execution until the next breakpoint or watchpoint is hit.
dcYou can use the dcu command to continue execution until a specified address is reached.
dcu 0x08049210INFO: Continue until 0x08049210 using 1 bpsizeGood luck winning here!INFO: hit breakpoint at: 0x8049210You can use the dcr command to continue performing step-over instructions until the next ret instruction is reached.
dcrfinish command in gdb. This command goes until the next ret instruction rather than the current function's ret.The best way to handle this is to set a breakpoint at the ret instruction and then continue execution, or use the dcu command.
A stack trace is a list of all the functions called up to this point. It is useful for debugging and understanding the program's flow.
To view the stack trace, use the dbt command.
dbt0 0x8049210 sp: 0x0 0 [sym.read_in] eip sym.read_in+33As far as I know, radare2 does not provide support for moving up and down the stack trace. If this is not the case, please let me know, and I'll update this section!
You can run programs in reverse order to better understand how to reach a certain location in a binary. Radare2 has a reverse debugger that can seek the program counter backward.
To do this, you must save the program state. This is handled via the dts submodule (the Trace Sessions submodule). Use dts+ to start a trace session.
dts+INFO: Reading 4096 byte(s) from 0x0804c000INFO: Reading 4096 byte(s) from 0xf7e2a000INFO: Reading 40960 byte(s) from 0xf7e2b000INFO: Reading 8192 byte(s) from 0xf7faf000INFO: Reading 4096 byte(s) from 0xf7fee000INFO: Reading 135168 byte(s) from 0xffa57000Use dsb to restore the previously recorded state by reverse-executing the instructions.
dsb[0x080491fb]>Use dcb to continue reverse-executing instructions until the next breakpoint.
dcb[0x080491fb]>When a binary forks, it creates a new process. This is useful for creating child processes that can run in parallel. However, this can be a pain for debugging because you have to debug each process individually.
This can be done in radare2 by deciding at the time of the fork. Use dcf to continue execution until the next fork. Once here, consult the dp submodule to handle processing commands.
The dp submodule is used to list and manage processes. Use dp to list the current processes. Use dp <num> to switch to a process. Use dp- to kill the current process.
o ModuleSooner or later, the program crashes or exits, and we need to start again. Rather than quitting radare2 and losing our flags, comments, and breakpoints, we can reopen the file. This is handled by the o module.
Use o to list the open files. While debugging, the binary is opened through ptrace, followed by the process ID:
o 3 * rwx 0xffffffffffffffff ptrace://1669284Use ood to restart the program in debug mode. This kills the current process and starts a fresh one, back at the entry point:
oodINFO: File dbg:///home/user/chall reopened in read-write modeo 4 * rwx 0xffffffffffffffff ptrace://1669309The new process has a new process ID, but our breakpoints, flags, and analysis carry over. This makes ood the radare2 equivalent of run in gdb.
ood may print a few errors like native-singlestep: No such process while it kills the old process. These are harmless.If the program takes command-line arguments, pass them to ood:
ood AAAA BBBBFor anything more complicated, like feeding a file to standard input, use a rarun2 profile. Write the profile, then reopen with oodr:
#!/usr/bin/rarun2stdin=input.txtoodr profile.rr2You can also pass the profile when opening radare2 with r2 -r profile.rr2 -d <binary>.
Outside the debugger, oo reopens the file as it was, and oo+ reopens it in read-write mode. We need write mode to patch the binary on disk, such as replacing a check with nops:
o 3 * r-x 0x00003aac chall 4 - rw- 0x00000004 null://4oo+o 3 * rwx 0x00003aac chall 4 - rw- 0x00000004 null://4oo+ changes the binary itself, not a copy. Make a backup before patching.oo?Usage: oo [arg] Map opened files| oo reopen current file| oo+ reopen in read-write| oob [baddr] reopen loading rbin info (change base address?)| ooi reopen bin info without reloading the file| ooc reopen core with current file| ood[?] reopen in debug mode| oom[?] reopen in malloc://| oon reopen without loading rbin info| oon+ reopen in read-write mode without loading rbin info| oonn reopen without loading rbin info, but with header flags| oonn+ reopen in read-write mode without loading rbin info, but with