memory introspection and reverse engineering hypervisor powered by leveraging Hyper-V
memory introspection and reverse engineering hypervisor powered by leveraging Hyper-V
hyper-reV is memory introspection and reverse engineering hypervisor powered by leveraging Hyper-V. There is also a usermode component The project provides the following abilities for the guest - meaning the Windows operating system virtualized by Hyper-V - to: read and write to guest virtual memory and physical memory, translate guest virtual addresses to their corresponding guest physical addresses, SLAT code hooks (also known as EPT/NPT hooks), and hiding entire pages of physical memory from the guest. The fact that it leverages Hyper-V means that it will also work under systems protected by HVCI.
There is also a usermode component which serves as a kernel debugger.
The 'uefi-boot' module is hyper-reV's UEFI driver. A copy of bootmgfw.efi is made. The contents of bootmgfw.efi are then replaced with the 'uefi-boot' module so that it will be executed at the next boot. The hyperv-attachment (the module inserted into Hyper-V) is also saved on disk in the same directory as bootmgfw.efi.
Once the 'uefi-boot' module is started, the original bootmgfw.efi file and any timestamp related metadata are restored to hide that the file has been tampered with.
A heap is also allocated using UEFI boot_services!AllocatePages (so it is 4kB aligned, rather than using boot_services!AllocatePool which does not guarantee that alignment).
This heap is used for allocating:
All of this memory is later hidden from the guest by pointing each page in guest SLAT mappings to a dummy page.
The original bootmgfw.efi image is then loaded through UEFI boot_services!LoadImage. A hook on bootmgfw.efi!ImgpLoadPEImage (a routine which loads a portable executable image) is applied. bootmgfw.efi is started using UEFI boot_services!StartImage.
Once the winload.efi image is being loaded by bootmgfw.efi, it is intercepted by the bootmgfw.efi!ImgpLoadPEImage hook. The bootmgfw.efi hooks are then fully removed before applying a hook to winload.efi!ImgpLoadPEImage. This winload.efi!ImgpLoadPEImage hook allows the loading of hvloader.dll to be intercepted once winload.efi loads it.
Once the loading of hvloader.dll is intercepted via the winload.efi!ImgpLoadPEImage hook, a hook is applied deep in the Hyper-V launch routine within hvloader.dll after removing the hooks on winload.efi. Below is a stripped down decompilation of the Hyper-V launch routine that is hooked (it is called within hvloader.dll!HvlLaunchHypervisor):
void __fastcall hv_launch(std::uint64_t hyperv_cr3, std::uint8_t* hyperv_entry_point, std::uint8_t* entry_point_gadget, std::uint64_t guest_kernel_cr3)
{
__writecr3(guest_kernel_cr3);
__asm { jmp entry_point_gadget }
}This hooked Hyper-V launch routine will jump to a gadget which will end up in Hyper-V's entry point. The parameters are as follows:
Continuing with the hook, a PML4e is inserted into the CR3 held in rcx. This PML4e contains an identity map of host physical memory. The physical memory backing the hyperv-attachment is also mapped in by this identity map, so there is a valid virtual mapping of the hyperv-attachment in Hyper-V's address space. The loaded hyperv-attachment image is relocated by (virtual base of the PML4e/identity map + physical base address of hyperv-attachment image). This relocation means that the hyperv-attachment will be able to execute under Hyper-V's address space once it is launched.
As @Iraq1337 described in his post, there are a few copies of Hyper-V in physical memory which are not SLAT protected (the guest can read them). Only the final Hyper-V image buffer is hidden from the guest. In the hypervisor launch hook, rdx holds a virtual address backing the final image buffer, which WILL be SLAT protected, so patches can be applied to it without them being disclosed to the guest. In the launch routine hook, a hook is placed on Hyper-V's VM exit handler, pointing it to a code cave where a far/long jump instruction will detour to the hyperv-attachment's VM exit handler. All of this is done after the Hyper-V launch routine hook is removed.
The hyperv-attachment supports both Intel and AMD. This is possible through some abstraction of architecture-specific code and a few #ifdefs, meaning both architectures are supported in the same codebase. The steps to compile the hyperv-attachment for Intel or AMD are described in '7. How to compile / use'.
The hyperv-attachment's entry point is called in the Hyper-V launch detour and does the following:
Host (the hypervisor running on the hardware which controls the guest) physical memory is accessed by the hyperv-attachment through the identity map that was set up before.
In the first VM exit, the hyperv-attachment does the following:
Some processes cannot be done in the first VM exit (especially ones relating to SLAT), as Hyper-V has not fully finished initializing yet. After a certain amount of VM exits have taken place, the hyperv-attachment hides the heap (the one that was set up originally in the uefi-boot image) via SLAT. The hidden uefi-boot heap memory includes hyperv-attachment's internal heap, image allocation and identity map page table structures that were allocated. This is achieved by setting the page frame numbers of all the page table entries to that of a free dummy page in the heap.
At the first VM exit, basic information of APIC is fetched through the 'APIC base' located at the MSR 0x1B. If APIC is already enabled, then it is checked whether xAPIC or x2APIC is used. If APIC is not already enabled, then the highest possible version of APIC is enabled. For xAPIC, the ICR (interrupt command register) can be accessed through its host physical address at the Local APIC. For x2APIC, the ICR is accessed through the MSRs representing the Local APIC.
Through the ICR, commands can be sent to the Local APIC. This is later used to send Non Maskable Interrupts to all processors but the currently executing one by formulating an ICR request. Later on in the post, it is described how those NMIs are used for synchronizing SLAT code hooks (EPT/NPT hooks). The APIC library which was internally developed for this project and released separately can be found here.
A SLAT CR3 is a CR3 which describes SLAT/translations of guest physical memory to host physical memory. On AMD this is called a nested CR3. On Intel this is called an EPT pointer.
The Hyper-V SLAT CR3 is the global SLAT CR3 created by Hyper-V. This is shared by all logical processors in Hyper-V in both Intel and AMD.
The hook SLAT CR3 is explained 4.5.3.3. It is the SLAT CR3 that the hyperv-attachment creates by making a deep copy of the Hyper-V SLAT CR3.
The SLAT code hooks were implemented as follows:
There is only 1 pointer (Intel's SLAT CR3), shared by all logical processors in Hyper-V.
The current core's EPT pointer changes between the hook SLAT CR3 and the Hyper-V SLAT CR3 in the EPT violation handler as needed. This is because both these SLAT CR3s hold mappings for the target page, but with different permissions.
When a hook is added, the following happens to the target page:
No open issues yet, or sync has not completed.