HyperDbg Debugger
|
Implementation of different EPT hidden hooks functions. More...
#include "pch.h"
Functions | |
_Must_inspect_result_ | _Success_ (return==TRUE) |
Check whether the desired PhysicalAddress is already in the g_EptState->HookedPagesList hooks or not. | |
VOID | EptHookReservePreallocatedPoolsForEptHooks (UINT32 Count) |
Reserve pre-allocated pools for EPT hooks. | |
VOID | EptHookAllocateExtraHookingPagesForMemoryMonitorsAndExecEptHooks (UINT32 Count) |
Allocate (reserve) extra pages for storing details of page hooks for memory monitor and regular hidden breakpoit exec EPT hooks. | |
PVOID | ExAllocatePoolWithTagHook (POOL_TYPE PoolType, SIZE_T NumberOfBytes, ULONG Tag) |
Hook function that HooksExAllocatePoolWithTag. | |
BOOLEAN | EptHookPerformPageHook (VIRTUAL_MACHINE_STATE *VCpu, PVOID TargetAddress, CR3_TYPE ProcessCr3) |
The main function that performs EPT page hook with hidden breakpoint. | |
BOOLEAN | EptHookPerformHook (PVOID TargetAddress, UINT32 ProcessId, BOOLEAN ApplyDirectlyFromVmxRoot) |
This function invokes a VMCALL to set the hook and broadcast the exiting for the breakpoints on exception bitmap. | |
BOOLEAN | EptHook (PVOID TargetAddress, UINT32 ProcessId) |
This function invokes a VMCALL to set the hook and broadcast the exiting for the breakpoints on exception bitmap. | |
BOOLEAN | EptHookFromVmxRoot (PVOID TargetAddress) |
This function invokes a direct VMCALL to setup the hook. | |
BOOLEAN | EptHookRestoreSingleHookToOriginalEntry (VIRTUAL_MACHINE_STATE *VCpu, SIZE_T PhysicalAddress, UINT64 OriginalEntry) |
Remove and Invalidate Hook in TLB (Hidden Detours and if counter of hidden breakpoint is zero) | |
VOID | EptHookRestoreAllHooksToOriginalEntry (VIRTUAL_MACHINE_STATE *VCpu) |
Remove and Invalidate Hook in TLB. | |
VOID | EptHookWriteAbsoluteJump (PCHAR TargetBuffer, SIZE_T TargetAddress) |
Write an absolute x64 jump to an arbitrary address to a buffer. | |
VOID | EptHookWriteAbsoluteJump2 (PCHAR TargetBuffer, SIZE_T TargetAddress) |
Write an absolute x64 jump to an arbitrary address to a buffer. | |
BOOLEAN | EptHookInstructionMemory (PEPT_HOOKED_PAGE_DETAIL Hook, CR3_TYPE ProcessCr3, PVOID TargetFunction, PVOID TargetFunctionInSafeMemory, PVOID HookFunction) |
Hook instructions. | |
BOOLEAN | EptHookPerformPageHookMonitorAndInlineHook (VIRTUAL_MACHINE_STATE *VCpu, PVOID HookingDetails, CR3_TYPE ProcessCr3, UINT32 PageHookMask) |
The main function that performs EPT page hook with hidden detours and monitor. | |
BOOLEAN | EptHookPerformMemoryOrInlineHook (VIRTUAL_MACHINE_STATE *VCpu, EPT_HOOKS_ADDRESS_DETAILS_FOR_EPTHOOK2 *EptHook2AddressDetails, EPT_HOOKS_ADDRESS_DETAILS_FOR_MEMORY_MONITOR *MemoryAddressDetails, UINT32 ProcessId, BOOLEAN EptHiddenHook2, BOOLEAN ApplyDirectlyFromVmxRoot) |
This function allocates a buffer in VMX Non Root Mode and then invokes a VMCALL to set the hook. | |
BOOLEAN | EptHookInlineHook (VIRTUAL_MACHINE_STATE *VCpu, PVOID TargetAddress, PVOID HookFunction, UINT32 ProcessId) |
This function applies EPT hook 2 (inline) to the target EPT table. | |
BOOLEAN | EptHookMonitorHook (VIRTUAL_MACHINE_STATE *VCpu, EPT_HOOKS_ADDRESS_DETAILS_FOR_MEMORY_MONITOR *HookingDetails, UINT32 ProcessId) |
This function applies monitor hooks to the target EPT table. | |
BOOLEAN | EptHookInlineHookFromVmxRoot (VIRTUAL_MACHINE_STATE *VCpu, PVOID TargetAddress, PVOID HookFunction) |
This function applies EPT inline to the target EPT table. | |
BOOLEAN | EptHookMonitorFromVmxRoot (VIRTUAL_MACHINE_STATE *VCpu, EPT_HOOKS_ADDRESS_DETAILS_FOR_MEMORY_MONITOR *MemoryAddressDetails) |
This function applies EPT monitor hooks to the target EPT table. | |
BOOLEAN | EptHookHandleHookedPage (VIRTUAL_MACHINE_STATE *VCpu, EPT_HOOKED_PAGE_DETAIL *HookedEntryDetails, VMX_EXIT_QUALIFICATION_EPT_VIOLATION ViolationQualification, SIZE_T PhysicalAddress, EPT_HOOKS_CONTEXT *LastContext, BOOLEAN *IgnoreReadOrWriteOrExec, BOOLEAN *IsExecViolation) |
Handles page hooks (trigger events) | |
BOOLEAN | EptHookRemoveEntryAndFreePoolFromEptHook2sDetourList (UINT64 Address) |
Remove the entry from g_EptHook2sDetourListHead in the case of !epthook2 details. | |
UINT32 | EptHookGetCountOfEpthooks (BOOLEAN IsEptHook2) |
get the length of active EPT hooks (!epthook and !epthook2) | |
BOOLEAN | EptHookUnHookSingleAddressDetoursAndMonitor (PEPT_HOOKED_PAGE_DETAIL HookedEntry, BOOLEAN ApplyDirectlyFromVmxRoot, EPT_SINGLE_HOOK_UNHOOKING_DETAILS *TargetUnhookingDetails) |
Remove single hook of detours type. | |
VOID | EptHookHandleMonitorTrapFlag (VIRTUAL_MACHINE_STATE *VCpu) |
Handle vm-exits for Monitor Trap Flag to restore previous state. | |
BOOLEAN | EptHookUnHookSingleAddressHiddenBreakpoint (PEPT_HOOKED_PAGE_DETAIL HookedEntry, UINT64 VirtualAddress, BOOLEAN ApplyDirectlyFromVmxRoot, EPT_SINGLE_HOOK_UNHOOKING_DETAILS *TargetUnhookingDetails) |
Remove single hook of hidden breakpoint type. | |
BOOLEAN | EptHookPerformUnHookSingleAddress (UINT64 VirtualAddress, UINT64 PhysAddress, UINT64 HookingTag, UINT32 ProcessId, BOOLEAN ApplyDirectlyFromVmxRoot, EPT_SINGLE_HOOK_UNHOOKING_DETAILS *TargetUnhookingDetails) |
Remove single hook from the hooked pages list and invalidate TLB. | |
BOOLEAN | EptHookUnHookAllByHookingTag (UINT64 HookingTag) |
Remove all hooks from the hooked pages by the given hooking tag. | |
BOOLEAN | EptHookUnHookSingleHookByHookingTagFromVmxRoot (UINT64 HookingTag, EPT_SINGLE_HOOK_UNHOOKING_DETAILS *TargetUnhookingDetails) |
Remove single hook from the hooked pages by the given hooking tag. | |
BOOLEAN | EptHookUnHookSingleAddress (UINT64 VirtualAddress, UINT64 PhysAddress, UINT32 ProcessId) |
Remove single hook from the hooked pages list and invalidate TLB. | |
BOOLEAN | EptHookUnHookSingleAddressFromVmxRoot (UINT64 VirtualAddress, UINT64 PhysAddress, EPT_SINGLE_HOOK_UNHOOKING_DETAILS *TargetUnhookingDetails) |
Remove single hook from the hooked pages list and invalidate TLB. | |
VOID | EptHookUnHookAll () |
Remove all hooks from the hooked pages list and invalidate TLB @detailsShould be called from Vmx Non-root. | |
PVOID | EptHook2GeneralDetourEventHandler (PGUEST_REGS Regs, PVOID CalledFrom) |
routines to generally handle breakpoint hit for detour | |
BOOLEAN | EptHookModifyInstructionFetchState (VIRTUAL_MACHINE_STATE *VCpu, PVOID PhysicalAddress, BOOLEAN IsUnset) |
Change PML EPT state for execution (execute) @detail should be called from VMX-root. | |
BOOLEAN | EptHookModifyPageReadState (VIRTUAL_MACHINE_STATE *VCpu, PVOID PhysicalAddress, BOOLEAN IsUnset) |
Change PML EPT state for read @detail should be called from VMX-root. | |
BOOLEAN | EptHookModifyPageWriteState (VIRTUAL_MACHINE_STATE *VCpu, PVOID PhysicalAddress, BOOLEAN IsUnset) |
Change PML EPT state for write @detail should be called from VMX-root. | |
Implementation of different EPT hidden hooks functions.
All the R/W hooks, Execute hooks and hardware register simulators are implemented here
_Must_inspect_result_ _Success_ | ( | return | = = TRUE | ) |
Check whether the desired PhysicalAddress is already in the g_EptState->HookedPagesList hooks or not.
PhysicalBaseAddress |
This function invokes a VMCALL to set the hook and broadcast the exiting for the breakpoints on exception bitmap.
Hook in VMX non-root Mode (hidden breakpoint)
this command uses hidden breakpoints (0xcc) to hook, THIS FUNCTION SHOULD BE CALLED WHEN THE VMLAUNCH ALREADY EXECUTED, it is because, broadcasting to enable exception bitmap for breakpoint is not clear here, if we want to broadcast to enable exception bitmaps on all cores when vmlaunch is not executed then that's ok but a user might call this function when we didn't configure the vmcs, it's a problem! we can solve it by giving a hint to vmcs configure function to make it ok for future configuration but that sounds stupid, I think it's better to not support this feature. Btw, debugger won't use this function in the above mentioned method, so we won't have any problem with this This function should be called from VMX non-root mode
TargetAddress | The address of function or memory address to be hooked |
ProcessId | The process id to translate based on that process's cr3 |
PVOID EptHook2GeneralDetourEventHandler | ( | PGUEST_REGS | Regs, |
PVOID | CalledFrom ) |
routines to generally handle breakpoint hit for detour
Regs | |
CalledFrom |
Allocate (reserve) extra pages for storing details of page hooks for memory monitor and regular hidden breakpoit exec EPT hooks.
Count |
BOOLEAN EptHookFromVmxRoot | ( | PVOID | TargetAddress | ) |
This function invokes a direct VMCALL to setup the hook.
the caller of this function should make sure to 1) broadcast to all cores to intercept breakpoints (#BPs) and after calling this function 2) the caller should broadcast to all cores to invalidate their EPTPs This function should be called from VMX root-mode
TargetAddress | The address of function or memory address to be hooked |
get the length of active EPT hooks (!epthook and !epthook2)
IsEptHook2 | Whether the length should be for !epthook or !epthook2 |
BOOLEAN EptHookHandleHookedPage | ( | VIRTUAL_MACHINE_STATE * | VCpu, |
EPT_HOOKED_PAGE_DETAIL * | HookedEntryDetails, | ||
VMX_EXIT_QUALIFICATION_EPT_VIOLATION | ViolationQualification, | ||
SIZE_T | PhysicalAddress, | ||
EPT_HOOKS_CONTEXT * | LastContext, | ||
BOOLEAN * | IgnoreReadOrWriteOrExec, | ||
BOOLEAN * | IsExecViolation ) |
Handles page hooks (trigger events)
Handle hooked pages in Vmx-root mode.
VCpu | The virtual processor's state |
HookedEntryDetails | The entry that describes the hooked page |
ViolationQualification | The exit qualification of vm-exit |
PhysicalAddress | The physical address that cause this vm-exit |
LastContext | The last (current) context of the execution |
IgnoreReadOrWriteOrExec | Whether to ignore the event effects or not |
IsExecViolation | Whether it's execution violation or not executing the post triggering of the event or not |
VOID EptHookHandleMonitorTrapFlag | ( | VIRTUAL_MACHINE_STATE * | VCpu | ) |
Handle vm-exits for Monitor Trap Flag to restore previous state.
VCpu | The virtual processor's state |
BOOLEAN EptHookInlineHook | ( | VIRTUAL_MACHINE_STATE * | VCpu, |
PVOID | TargetAddress, | ||
PVOID | HookFunction, | ||
UINT32 | ProcessId ) |
This function applies EPT hook 2 (inline) to the target EPT table.
Hook in VMX non-root mode (hidden detours)
this function should be called from VMX non-root mode
VCpu | The virtual processor's state |
TargetAddress | The address of function or memory address to be hooked |
HookFunction | The function that will be called when hook triggered |
ProcessId | The process id to translate based on that process's cr3 |
BOOLEAN EptHookInlineHookFromVmxRoot | ( | VIRTUAL_MACHINE_STATE * | VCpu, |
PVOID | TargetAddress, | ||
PVOID | HookFunction ) |
This function applies EPT inline to the target EPT table.
Hook in VMX root-mode (hidden detours)
this function should be called from VMX root-mode
VCpu | The virtual processor's state |
TargetAddress | The address of function or memory address to be hooked |
HookFunction | The function that will be called when hook triggered |
BOOLEAN EptHookInstructionMemory | ( | PEPT_HOOKED_PAGE_DETAIL | Hook, |
CR3_TYPE | ProcessCr3, | ||
PVOID | TargetFunction, | ||
PVOID | TargetFunctionInSafeMemory, | ||
PVOID | HookFunction ) |
Hook instructions.
Hook | The details of hooked pages |
ProcessCr3 | The target Process CR3 |
TargetFunction | Target function that needs to be hooked |
TargetFunctionInSafeMemory | Target content in the safe memory (used in Length Disassembler Engine) |
HookFunction | The function that will be called when hook triggered |
BOOLEAN EptHookModifyInstructionFetchState | ( | VIRTUAL_MACHINE_STATE * | VCpu, |
PVOID | PhysicalAddress, | ||
BOOLEAN | IsUnset ) |
Change PML EPT state for execution (execute) @detail should be called from VMX-root.
VCpu | The virtual processor's state |
PhysicalAddress | Target physical address |
IsUnset | Is unsetting bit or setting bit |
BOOLEAN EptHookModifyPageReadState | ( | VIRTUAL_MACHINE_STATE * | VCpu, |
PVOID | PhysicalAddress, | ||
BOOLEAN | IsUnset ) |
Change PML EPT state for read @detail should be called from VMX-root.
VCpu | The virtual processor's state |
PhysicalAddress | Target physical address |
IsUnset | Is unsetting bit or setting bit |
BOOLEAN EptHookModifyPageWriteState | ( | VIRTUAL_MACHINE_STATE * | VCpu, |
PVOID | PhysicalAddress, | ||
BOOLEAN | IsUnset ) |
Change PML EPT state for write @detail should be called from VMX-root.
VCpu | The virtual processor's state |
PhysicalAddress | Target physical address |
IsUnset | Is unsetting bit or setting bit |
BOOLEAN EptHookMonitorFromVmxRoot | ( | VIRTUAL_MACHINE_STATE * | VCpu, |
EPT_HOOKS_ADDRESS_DETAILS_FOR_MEMORY_MONITOR * | MemoryAddressDetails ) |
This function applies EPT monitor hooks to the target EPT table.
this function should be called from VMX root-mode
VCpu | The virtual processor's state |
MemoryAddressDetails | details of the target memory |
BOOLEAN EptHookMonitorHook | ( | VIRTUAL_MACHINE_STATE * | VCpu, |
EPT_HOOKS_ADDRESS_DETAILS_FOR_MEMORY_MONITOR * | HookingDetails, | ||
UINT32 | ProcessId ) |
This function applies monitor hooks to the target EPT table.
this function should be called from VMX non-root mode
VCpu | The virtual processor's state |
HookingDetails | Monitor hooking details |
ProcessId | The process id to translate based on that process's cr3 |
BOOLEAN EptHookPerformHook | ( | PVOID | TargetAddress, |
UINT32 | ProcessId, | ||
BOOLEAN | ApplyDirectlyFromVmxRoot ) |
This function invokes a VMCALL to set the hook and broadcast the exiting for the breakpoints on exception bitmap.
TargetAddress | The address of function or memory address to be hooked |
ProcessId | The process id to translate based on that process's cr3 |
ApplyDirectlyFromVmxRoot | should it be directly applied from VMX-root mode or not |
BOOLEAN EptHookPerformMemoryOrInlineHook | ( | VIRTUAL_MACHINE_STATE * | VCpu, |
EPT_HOOKS_ADDRESS_DETAILS_FOR_EPTHOOK2 * | EptHook2AddressDetails, | ||
EPT_HOOKS_ADDRESS_DETAILS_FOR_MEMORY_MONITOR * | MemoryAddressDetails, | ||
UINT32 | ProcessId, | ||
BOOLEAN | EptHiddenHook2, | ||
BOOLEAN | ApplyDirectlyFromVmxRoot ) |
This function allocates a buffer in VMX Non Root Mode and then invokes a VMCALL to set the hook.
this command uses hidden detours, if it calls from VMX root-mode directly, it should also invalidate EPT caches (by the caller)
VCpu | The virtual processor's state |
EptHook2AddressDetails | The address details for inline EPT hooks |
MemoryAddressDetails | The address details for monitor EPT hooks |
ProcessId | The process id to translate based on that process's cr3 |
EptHiddenHook2 | epthook2 style hook |
ApplyDirectlyFromVmxRoot | should it be directly applied from VMX-root mode or not |
BOOLEAN EptHookPerformPageHook | ( | VIRTUAL_MACHINE_STATE * | VCpu, |
PVOID | TargetAddress, | ||
CR3_TYPE | ProcessCr3 ) |
The main function that performs EPT page hook with hidden breakpoint.
Hook in VMX Root Mode with hidden breakpoints (A pre-allocated buffer should be available)
This function returns false in VMX Non-Root Mode if the VM is already initialized This function have to be called through a VMCALL in VMX Root Mode
VCpu | The virtual processor's state |
TargetAddress | The address of function or memory address to be hooked |
ProcessCr3 | The process cr3 to translate based on that process's cr3 |
BOOLEAN EptHookPerformPageHookMonitorAndInlineHook | ( | VIRTUAL_MACHINE_STATE * | VCpu, |
PVOID | HookingDetails, | ||
CR3_TYPE | ProcessCr3, | ||
UINT32 | PageHookMask ) |
The main function that performs EPT page hook with hidden detours and monitor.
Hook in VMX Root Mode with hidden detours and monitor (A pre-allocated buffer should be available)
This function returns false in VMX Non-Root Mode if the VM is already initialized This function have to be called through a VMCALL in VMX Root Mode
VCpu | The virtual processor's state |
HookingDetails | The address of function or memory address to be hooked |
ProcessCr3 | The process cr3 to translate based on that process's cr3 |
PageHookMask | Mask hook of the page |
BOOLEAN EptHookPerformUnHookSingleAddress | ( | UINT64 | VirtualAddress, |
UINT64 | PhysAddress, | ||
UINT64 | HookingTag, | ||
UINT32 | ProcessId, | ||
BOOLEAN | ApplyDirectlyFromVmxRoot, | ||
EPT_SINGLE_HOOK_UNHOOKING_DETAILS * | TargetUnhookingDetails ) |
Remove single hook from the hooked pages list and invalidate TLB.
VirtualAddress | Virtual address to unhook |
PhysAddress | Physical address to unhook (optional) |
HookingTag | Hooking tag (optional) |
ProcessId | The process id of target process (in unhooking for some hooks only physical address is availables) |
ApplyDirectlyFromVmxRoot | should it be directly applied from VMX-root mode or not |
TargetUnhookingDetails | Target data for the caller to restore EPT entry and invalidate EPT caches. Only when applied in VMX-root mode directly |
Remove the entry from g_EptHook2sDetourListHead in the case of !epthook2 details.
Remove an entry from g_EptHook2sDetourListHead.
Address | Address to remove |
Reserve pre-allocated pools for EPT hooks.
Allocate pre-allocated pools for EPT hooks.
Count | number of hooks |
VOID EptHookRestoreAllHooksToOriginalEntry | ( | VIRTUAL_MACHINE_STATE * | VCpu | ) |
Remove and Invalidate Hook in TLB.
Remove all hooks from the hooked pages lists (Should be called in vmx-root)
VCpu | The virtual processor's state |
BOOLEAN EptHookRestoreSingleHookToOriginalEntry | ( | VIRTUAL_MACHINE_STATE * | VCpu, |
SIZE_T | PhysicalAddress, | ||
UINT64 | OriginalEntry ) |
Remove and Invalidate Hook in TLB (Hidden Detours and if counter of hidden breakpoint is zero)
Remove a special hook from the hooked pages lists.
VCpu | The virtual processor's state |
PhysicalAddress | |
OriginalEntry |
VOID EptHookUnHookAll | ( | ) |
Remove all hooks from the hooked pages list and invalidate TLB @detailsShould be called from Vmx Non-root.
Remove all hooks from the hooked pages lists.
Remove all hooks from the hooked pages by the given hooking tag.
Should be called from Vmx Non-root
HookingTag | The hooking tag to unhook |
Remove single hook from the hooked pages list and invalidate TLB.
Remove single hook from the hooked pages list and invalidate TLB From VMX non-root mode.
Should be called from VMX non-root
VirtualAddress | Virtual address to unhook |
PhysAddress | Physical address to unhook (optional) |
ProcessId | The process id of target process |
in unhooking for some hooks only physical address is availables
BOOLEAN EptHookUnHookSingleAddressDetoursAndMonitor | ( | PEPT_HOOKED_PAGE_DETAIL | HookedEntry, |
BOOLEAN | ApplyDirectlyFromVmxRoot, | ||
EPT_SINGLE_HOOK_UNHOOKING_DETAILS * | TargetUnhookingDetails ) |
Remove single hook of detours type.
HookedEntry | entry detail of hooked address |
ApplyDirectlyFromVmxRoot | should it be directly applied from VMX-root mode or not |
TargetUnhookingDetails | Target data for the caller to restore EPT entry and invalidate EPT caches. Only when applied in VMX-root mode directly |
BOOLEAN EptHookUnHookSingleAddressFromVmxRoot | ( | UINT64 | VirtualAddress, |
UINT64 | PhysAddress, | ||
EPT_SINGLE_HOOK_UNHOOKING_DETAILS * | TargetUnhookingDetails ) |
Remove single hook from the hooked pages list and invalidate TLB.
Remove single hook from the hooked pages list and invalidate TLB From VMX root-mode.
Should be called from VMX root-mode
VirtualAddress | Virtual address to unhook |
PhysAddress | Physical address to unhook (optional) |
TargetUnhookingDetails | Target data for the caller to restore EPT entry and invalidate EPT caches. Only when applied in VMX-root mode directly |
BOOLEAN EptHookUnHookSingleAddressHiddenBreakpoint | ( | PEPT_HOOKED_PAGE_DETAIL | HookedEntry, |
UINT64 | VirtualAddress, | ||
BOOLEAN | ApplyDirectlyFromVmxRoot, | ||
EPT_SINGLE_HOOK_UNHOOKING_DETAILS * | TargetUnhookingDetails ) |
Remove single hook of hidden breakpoint type.
HookedEntry | entry detail of hooked address |
VirtualAddress | virtual address to unhook |
ApplyDirectlyFromVmxRoot | should it be directly applied from VMX-root mode or not |
TargetUnhookingDetails | Target data for the caller to restore EPT entry and invalidate EPT caches. Only when applied in VMX-root mode directly |
BOOLEAN EptHookUnHookSingleHookByHookingTagFromVmxRoot | ( | UINT64 | HookingTag, |
EPT_SINGLE_HOOK_UNHOOKING_DETAILS * | TargetUnhookingDetails ) |
Remove single hook from the hooked pages by the given hooking tag.
Should be called from Vmx root-mode
HookingTag | The hooking tag to unhook |
VOID EptHookWriteAbsoluteJump | ( | PCHAR | TargetBuffer, |
SIZE_T | TargetAddress ) |
Write an absolute x64 jump to an arbitrary address to a buffer.
TargetBuffer | |
TargetAddress |
VOID EptHookWriteAbsoluteJump2 | ( | PCHAR | TargetBuffer, |
SIZE_T | TargetAddress ) |
Write an absolute x64 jump to an arbitrary address to a buffer.
TargetBuffer | |
TargetAddress |
PVOID ExAllocatePoolWithTagHook | ( | POOL_TYPE | PoolType, |
SIZE_T | NumberOfBytes, | ||
ULONG | Tag ) |
Hook function that HooksExAllocatePoolWithTag.
PoolType | |
NumberOfBytes | |
Tag |