ELF 是什么

我们平时编译出来的 Linux 可执行文件,比如

1
gcc pwn.c -o pwn

生成的 pwn,通常就是一个 ELF 文件。
ELF 全称

1
Executable and Linkable Format

可以简单理解成:

Linux 下可执行文件、目标文件、动态库的一种标准格式。

一个 ELF 文件里面不是只有“代码”,而是分成很多区域。
我们现在最需要认识这几个

1
2
3
4
5
6
7
ELF
├── .text
├── .data
├── .bss
├── .rodata
├── .plt
└── .got

我们要先记住用途

区域 作用
.text 程序代码
.data 已初始化的全局变量
.bss 未初始化的全局变量
.rodata 只读数据,比如字符串
.plt 用来“跳转调用动态库函数”
.got 保存动态库函数的真实地址

这里 Pwn 最重要的是

1
2
.plt
.got

为什么需要 PLT 和 GOT

假设有这样一个程序

1
2
3
4
5
6
#include <stdio.h>

int main() {
puts("hello");
return 0;
}

你写了

1
puts("hello");

但是有个问题。
puts 并不是你这个程序自己实现的。
它是在libc.so里面。
也就是说

1
2
3
4
5
你的程序
↓
想调用 puts
↓
puts 实际在 libc

于是问题来了

程序运行之前,它怎么知道 libc 里的 puts 到底在哪个地址?

尤其 ASLR 开启后

1
libc 每次加载地址可能都不一样

所以编译阶段不能简单写死

1
call 0x7ffff7xxxxxx

这时候就需要

1
PLT + GOT

先理解 GOT

GOT 全称:

1
Global Offset Table

你先把它理解成一张:

地址表

比如:

1
2
3
4
5
6
7
8
GOT
┌───────────────┬──────────────────┐
│ 函数 │ 实际地址 │
├───────────────┼──────────────────┤
│ puts │ 0x7ffff7e12340 │
│ printf │ 0x7ffff7df1020 │
│ read │ 0x7ffff7ebxxxx │
└───────────────┴──────────────────┘

于是

1
puts@got

可以理解为

GOT 表里专门存放 puts 地址的那个位置。

注意这里非常重要

1
puts@got

不是 puts 函数本身。
而是

一个内存位置,这个位置里面存了 puts 的真实地址。

比如

1
puts@got = 0x404018

然后

1
[0x404018] = 0x7ffff7e12340

含义是0x404018这个位置里保存着:puts 真正的 libc 地址

PLT 是什么

PLT 全称

1
Procedure Linkage Table

可以理解成

动态库函数的“跳板”。

例如

1
puts@plt

它是一小段代码。
程序不会直接call libc里的puts
而是call puts@plt
然后

1
2
3
4
5
puts@plt
↓
查 puts@got
↓
跳转到真正的 puts

所以整体关系

1
2
3
4
5
6
7
main
↓
puts@plt
↓
puts@got
↓
libc 中真正的 puts

这是最重要的一张图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
程序代码
│
│ call puts@plt
▼
┌─────────────┐
│ puts@plt │
└──────┬──────┘
│
│ 查 GOT
▼
┌─────────────┐
│ puts@got │
│ 0x404018 │
└──────┬──────┘
│
│ 里面存着
▼
0x7ffff7xxxxxx
│
▼
┌─────────────┐
│ libc puts() │
└─────────────┘

为什么 Pwn 特别喜欢 GOT

这里就和我们后面要学的 ret2libc 接上了。
假设

1
puts@got = 0x404018

而程序运行后

1
[0x404018] = 0x7ffff7e12340

这个

1
0x7ffff7e12340

就是 libc 里的真实 puts 地址。
如果我们能让程序执行

1
puts(puts@got);

会发生什么?
注意puts(x),会把 x 当成一个地址。
于是我们通过 ROP

1
2
3
pop rdi ; ret
puts@got
puts@plt

实际上就相当于

1
puts(puts@got);

这样就能把puts@got 里面的数据泄露出来。
也就是puts 的 libc 地址
然后

1
puts真实地址 - puts在libc中的偏移

就能得到

1
libc base

这正是 ret2libc 的核心

puts@plt 和 puts@got 千万别混

我们以后经常会看到

1
2
elf.plt["puts"]
elf.got["puts"]

它们完全不是一回事
例如

1
2
3
4
from pwn import *
elf = ELF("./pwn")
print(hex(elf.plt["puts"]))
print(hex(elf.got["puts"]))

可能输出

1
2
0x401030
0x404018

含义分别是

1
0x401030 → puts@plt

这个是一段调用 puts 的跳板代码。

1
0x404018 → puts@got

是保存 puts 真实地址的内存位置。

我们可以这么记

1
2
PLT = 怎么调用
GOT = 调到哪里

这句话非常好用。

用真实汇编理解

我们以后反汇编时可能看到

1
call   0x401030 <puts@plt>

也就是说

1
程序不是直接 call puts

而是

1
call puts@plt

再看 PLT

1
2
puts@plt:
jmp QWORD PTR [rip+0x2fe2]

本质就是跳到 puts@got 里保存的地址
所以可以简化理解

1
call puts@plt

约等于

1
jmp [puts@got]

第一次调用为什么更复杂

这里再进阶一点
Linux 通常会使用Lazy Binding
中文一般叫延迟绑定

意思是程序启动时,不马上把所有动态函数地址解析出来。

比如

1
2
3
4
puts
printf
read
write

程序可能只用其中几个
所以没有必要一启动就全部解析
第一次调用

1
puts@plt

时,可能流程是

1
2
3
4
5
6
7
8
9
10
11
12
13
puts@plt
↓
puts@got
↓
发现还没有真实地址
↓
动态链接器
↓
寻找 libc 里的 puts
↓
把地址写进 puts@got
↓
调用 puts

第二次

1
2
3
4
5
6
7
puts@plt
↓
puts@got
↓
里面已经有 puts 地址
↓
直接跳过去

所以

1
GOT

一开始可能不是最终真实地址
第一次调用之后才会被更新
这叫lazy binding

一个生活化理解

你可以把

1
PLT

理解成公司前台。

1
GOT

理解成公司通讯录。
你说

1
我要找 puts

先去

1
puts@plt

前台看通讯录

1
puts@got

里面记录

1
puts 电话:0x7ffff7xxxxxx

然后把你转接过去
于是

1
2
3
PLT = 前台
GOT = 通讯录
libc函数 = 真正的人

如果第一次通讯录还没有号码:

1
前台 → 人事部门/动态链接器 → 找号码 → 写进通讯录

以后就直接打

为什么 ret2libc 需要 PLT + GOT

现在把它串起来。
假设程序

1
2
3
4
void vuln() {
char buf[64];
gets(buf);
}

没有

1
win();

但是程序导入了

1
puts();

那我们可以构造:

1
2
3
4
5
padding
pop rdi ; ret
puts@got
puts@plt
main

解释

1
padding

覆盖返回地址。
然后

1
pop rdi ; ret

因为 64 位 Linux

1
第一个参数 → RDI

于是

1
RDI = puts@got

接下来

1
puts@plt

等价于

1
puts(puts@got);

于是 libc 地址被泄露
最后

1
main

回到 main,再给你一次输入机会。
这就是

1
ret2libc 第一阶段

完整逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ROP
↓
puts(puts@got)
↓
泄露 puts 真实地址
↓
libc_base = leak - puts_offset
↓
system = libc_base + system_offset
↓
binsh = libc_base + "/bin/sh"_offset
↓
第二次 ROP
↓
system("/bin/sh")

现在先牢牢记住这 4 句话

以后看到 PLT/GOT,脑子里直接反应

1
2
3
4
ELF = Linux 可执行文件格式
PLT = 动态函数调用跳板
GOT = 保存动态函数真实地址的表
puts@plt → puts@got → libc puts

以及 Pwn 最重要的

1
2
3
4
5
泄露 puts@got
↓
得到 puts libc 地址
↓
计算 libc base

到这里,我们其实已经把 ret2libc 的地基打好了。