Printing data is essential for understanding the contents of registers and memory. This section will cover the various ways to print data in gdb.
We can use various summary commands to see a lot of data simultaneously. GEF's preview pane shadows a few of these commands, but they are still useful.
info registersThis command shows all the available registers and their current value.
info registerseax 0x804923c 0x804923cecx 0x22d4b89d 0x22d4b89dedx 0xffffd6c0 0xffffd6c0ebx 0xf7e2a000 0xf7e2a000esp 0xffffd698 0xffffd698ebp 0xffffd698 0xffffd698esi 0xffffd754 0xffffd754edi 0xf7ffcb80 0xf7ffcb80eip 0x804923f 0x804923f <main+3>eflags 0x246 [ PF ZF IF ]cs 0x23 0x23ss 0x2b 0x2bds 0x2b 0x2bes 0x2b 0x2bfs 0x0 0x0gs 0x63 0x63k0 0x0 0x0k1 0x0 0x0k2 0x0 0x0k3 0x0 0x0k4 0x0 0x0k5 0x0 0x0k6 0x0 0x0k7 0x0 0x0GEF provides a similar command, registers, outputs the registers that it shows in the GEF output. Its output is a bit more verbose and only shows the most important registers.
registers$eax : 0x0804923c → <main+0> push ebp$ebx : 0xf7e2a000 → 0x00229dac$ecx : 0x22d4b89d$edx : 0xffffd6c0 → 0xf7e2a000 → 0x00229dac$esp : 0xffffd698 → 0xf7ffd020 → 0xf7ffda40 → 0x00000000$ebp : 0xffffd698 → 0xf7ffd020 → 0xf7ffda40 → 0x00000000$esi : 0xffffd754 → 0xffffd90e → "./args"$edi : 0xf7ffcb80 → 0x00000000$eip : 0x0804923f → <main+3> and esp, 0xfffffff0$eflags: [ZERO carry PARITY adjust sign trap INTERRUPT direction overflow resume virtualx86 identification]$cs: 0x23 $ss: 0x2b $ds: 0x2b $es: 0x2b $fs: 0x00 $gs: 0x63registers takes an optional argument to show only a subset of registers. Remember that GDB uses $ syntax for registers.
registers $eip $esp $edi$esp : 0xffffd698 → 0xf7ffd020 → 0xf7ffda40 → 0x00000000$edi : 0xf7ffcb80 → 0x00000000$eip : 0x0804923f → <main+3> and esp, 0xfffffff0info frameThis gives us extra details on the stack frame. I do not commonly use this command because GEF provides a lot of this information in the GEF output.
info frameStack level 0, frame at 0xffffd6a0: eip = 0x804923f in main; saved eip = 0xf7c21519 Arglist at 0xffffd698, args: Locals at 0xffffd698, Previous frame's sp is 0xffffd6a0 Saved registers: ebp at 0xffffd698, eip at 0xffffd69cinfo proc mappings, vmmap, and elf-infoThis is one of the most useful commands in this section. It shows the memory mappings for the process, which is useful for understanding the memory sections, where data is allocated, and the permissions of each section.
GEF's vmmap performs the same function as info proc mappings, but provides a slightly cleaner output. vmmap also takes an address argument and resolves the address to a section.
GEF's elf-info provides more detailed information on the binary's segments. This includes the got and plt locations and the .text, .data, and .bss sections.
vmmap[ Legend: Code | Heap | Stack ]Start End Offset Perm Path0x08048000 0x08049000 0x00000000 r-- ./args0x08049000 0x0804a000 0x00001000 r-x ./args0x0804a000 0x0804b000 0x00002000 r-- ./args0x0804b000 0x0804c000 0x00002000 r-- ./args0x0804c000 0x0804d000 0x00003000 rw- ./args0xf7c00000 0xf7c20000 0x00000000 r-- /usr/lib/i386-linux-gnu/libc.so.60xf7c20000 0xf7da2000 0x00020000 r-x /usr/lib/i386-linux-gnu/libc.so.60xf7da2000 0xf7e27000 0x001a2000 r-- /usr/lib/i386-linux-gnu/libc.so.60xf7e27000 0xf7e28000 0x00227000 --- /usr/lib/i386-linux-gnu/libc.so.60xf7e28000 0xf7e2a000 0x00227000 r-- /usr/lib/i386-linux-gnu/libc.so.60xf7e2a000 0xf7e2b000 0x00229000 rw- /usr/lib/i386-linux-gnu/libc.so.60xf7e2b000 0xf7e35000 0x00000000 rw-0xf7fbe000 0xf7fc0000 0x00000000 rw-0xf7fc0000 0xf7fc4000 0x00000000 r-- [vvar]0xf7fc4000 0xf7fc6000 0x00000000 r-x [vdso]0xf7fc6000 0xf7fc7000 0x00000000 r-- /usr/lib/i386-linux-gnu/ld-linux.so.20xf7fc7000 0xf7fec000 0x00001000 r-x /usr/lib/i386-linux-gnu/ld-linux.so.20xf7fec000 0xf7ffb000 0x00026000 r-- /usr/lib/i386-linux-gnu/ld-linux.so.20xf7ffb000 0xf7ffd000 0x00034000 r-- /usr/lib/i386-linux-gnu/ld-linux.so.20xf7ffd000 0xf7ffe000 0x00036000 rw- /usr/lib/i386-linux-gnu/ld-linux.so.20xfffdd000 0xffffe000 0x00000000 rwx [stack]info variablesThis command shows us the global variables in the program. This is useful for understanding the program's layout and where data is stored. There is a lot of bloat in this output because the binary automatically includes a lot of variables for the C runtime.
info variablesAll defined variables:Non-debugging symbols:0x080481cc __abi_tag0x0804a000 _fp_hw0x0804a004 _IO_stdin_used0x0804a038 __GNU_EH_FRAME_HDR0x0804a15c __FRAME_END__0x0804bf08 __frame_dummy_init_array_entry0x0804bf0c __do_global_dtors_aux_fini_array_entry0x0804bf10 _DYNAMIC0x0804c000 _GLOBAL_OFFSET_TABLE_0x0804c020 __data_start0x0804c020 data_start0x0804c024 __dso_handle0x0804c028 __TMC_END__0x0804c028 __bss_start0x0804c028 _edata0x0804c028 completed...info functionsWe use this command whenever we open a binary to see the available functions.
info functionsAll defined functions:Non-debugging symbols:0x08049000 _init0x08049040 __libc_start_main@plt0x08049050 fflush@plt0x08049060 gets@plt0x08049070 puts@plt0x08049080 system@plt0x08049090 _start0x080490d0 _dl_relocate_static_pie0x080490e0 __x86.get_pc_thunk.bx0x080490f0 deregister_tm_clones0x08049130 register_tm_clones0x08049170 __do_global_dtors_aux0x080491a0 frame_dummy0x080491a6 win0x080491ef read_in0x0804923c main0x08049254 __x86.get_pc_thunk.ax0x08049258 _finilibc.The print command (p for short) is the driving force behind examining data. It allows us to print the value of an expression.
print 0x10-0x8$1 = 0x8We can use the x command to examine memory. It takes an address or a register as its argument:
x $esp0xffffd640: 0xffffd658x 0xffffd6400xffffd640: 0xffffd658There are three formatting parameters formatted like so: x/NFU <ADDRESS>. The three format parameters are:
N: The repeat count. The repeat count is the number of times to repeat the format. This is used to print arrays (or large amounts of data from the stack or heap).F: The display format. The display format is how to display the data. The default is hexadecimal (x). The types available are x (hex), d (decimal), u (unsigned), o (octal), t (binary), f (float), a (address), c (char), s (string), and i (instruction).U: The unit size. The unit size is the size of each block. The four types are b (byte), h (halfword), w (word), and g (giant, 8 bytes). The default is word size (w). 32-bit binaries used w-sized data and 64-bit uses g-sized data.Here is this in action in two common use cases:
in 32-bit (understanding passed parameters)x/20wx $esp0xffffd640: 0xffffd658 0x00000000 0x01000000 0x080491fb0xffffd650: 0xf7fc4540 0x00000000 0xf7c184be 0xf7e2a0540xffffd660: 0xf7fbe4a0 0xf7fd6f90 0xf7c184be 0xf7fbe4a00xffffd670: 0xffffd6b0 0xf7fbe66c 0xf7fbeb10 0x000000010xffffd680: 0x00000001 0xf7e2a000 0xffffd698 0x08049251x/i $eip=> 0x804922e <read_in+63>: call 0x8049060 <gets@plt>in 64-bit (identifying a return pointer)x/20gx $rsp0x7fffffffe3e0: 0x0000000000000000 0x00000000000000000x7fffffffe3f0: 0x0000000000000000 0x00000000000000000x7fffffffe400: 0x0000000000000000 0x00000000000000000x7fffffffe410: 0x00007fffffffe420 0x00000000004012050x7fffffffe420: 0x0000000000000001 0x00007ffff7c29d900x7fffffffe430: 0x0000000000000000 0x00000000004011f3gdb, so get some practice examining data.There are two major commands for finding data in memory: find and search-pattern.
findfind is built into GDB directly and is used for finding expressions within memory. The format of the find command is find [/UN] start, +len|end, expr1 [, expr2, …].
/UN: Unit size and number flags. These are the same flags you would use with x.start: Start address. Where to start searching.+len|end: End of search. You can specify a number of bytes to search or an end address. Addresses are inclusive by default.expr1: Expression. This is the expression to search for.In action:
find 0x08049000, +0x1000, "/bin/sh"Pattern not found.find 0x08049000, +0x1000, 0xc30x8049023 <_init+35>0x804926f <_fini+23>2 patterns found.search-patternsearch-pattern is a GEF command used for finding strings. It takes a string argument and searches across the binary and loaded libraries for all instances of the string.
search-pattern "/bin/cat flag.txt"[+] Searching '/bin/cat flag.txt' in memory[+] In './split'(0x601000-0x602000), permission=rw- 0x601060 - 0x601071 → "/bin/cat flag.txt"search-pattern /bin/sh[+] Searching '/bin/sh' in memory[+] In '/usr/lib/i386-linux-gnu/libc.so.6'(0xf7da2000-0xf7e27000), permission=r-- 0xf7dbd0f5 - 0xf7dbd0fc → "/bin/sh"search-pattern can also search based on endianness and can restrict search in only a certain part of memory.
search-pattern /bin/sh little 0x0-0x80500000[+] Searching '/bin/sh' in 0x0-0x80500000