[2022]hypervisor: 检测与预防 (二) huoji hypervisor 2022-06-19 525 次浏览 1 次点赞 前情回顾: [[2022] hypervisor: 检测与预防 (一)](https://key08.com/index.php/2022/05/12/1477.html "[2022] hypervisor: 检测与预防 (一)") 让我们来看看新的问题: ### CR3 很多开源虚拟机,CR3是guest和host共享的  导致可以通过写垃圾CR3后提交vmexit让host崩溃 POC代码: ```cpp dpc level: _writecr3(自己的CR3); memcpy(系统pml4,0x0,512*8) vmexit //<-此时你的垃圾玩具机就崩溃了 ``` poc: https://github.com/huoji120/mash_hypervisor 修复: host和guest要独立一份CR3 ### TSS TSS在windows上负责异常的时候拷贝栈. 当异常发生并且执行被重定向到中断处理程序时,RSP中的地址不能被信任.因为有可能不正确.或者在权限转换的时候发生提权操作 修复: 拷贝TSS,修改host的gdt的tss为自己的.然后把几个比如NMI、de这些带stack的东西设置自己的堆栈就行 ### xsetbv xsetbv 光靠try catch还是不太行.可能会发生"主机设置了后导致了异常,再try的时候来不及"的情况.所以要跟XEN/KVM一样在try前面再次检查字段有没有问题: 大概代码: ```cpp bool valid_xcr0(uintptr_t xcr0) { /* FP must be unconditionally set. */ if (!(xcr0 & X86_XCR0_FP)) return false; /* YMM depends on SSE. */ if ((xcr0 & X86_XCR0_YMM) && !(xcr0 & X86_XCR0_SSE)) return false; if (xcr0 & (X86_XCR0_OPMASK | X86_XCR0_ZMM | X86_XCR0_HI_ZMM)) { /* OPMASK, ZMM, and HI_ZMM require YMM. */ if (!(xcr0 & X86_XCR0_YMM)) return false; /* OPMASK, ZMM, and HI_ZMM must be the same. */ if (~xcr0 & (X86_XCR0_OPMASK | X86_XCR0_ZMM | X86_XCR0_HI_ZMM)) return false; } /* BNDREGS and BNDCSR must be the same. */ if (!(xcr0 & X86_XCR0_BNDREGS) != !(xcr0 & X86_XCR0_BNDCSR)) return false; return true; } .... UINT32 xcr = (UINT32)guest_status->guest_context->Rcx; ULARGE_INTEGER xcr_value; if (xcr != 0) { //不为0 注入GP异常 inject_event(guest_status, ia32_invalid_opcode, ia32_hardware_exception, false, NULL); return; } xcr_value.u.LowPart = (UINT32)guest_status->guest_context->Rax; xcr_value.u.HighPart = (UINT32)guest_status->guest_context->Rdx; __cpuid_params_t cpuid_reg; _huoji_cpuidex((int*)&cpuid_reg, 7, 0); if ((xcr_value.LowPart & cpuid_reg.rax) != cpuid_reg.rax || (xcr_value.HighPart & cpuid_reg.rdx) != cpuid_reg.rdx) { //cpu不支持这个指令 inject_event(guest_status, ia32_invalid_opcode, ia32_hardware_exception, false, NULL); return; } if (!valid_xcr0(xcr_value.QuadPart)) { //给了个无效值 inject_event(guest_status, ia32_invalid_opcode, ia32_hardware_exception, false, NULL); return; } ..... ``` 本文由 huoji 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。 点赞 1
还不快抢沙发