How to create a condition?
This document helps you to create a condition for events
HyperDbg supports the creation of conditional events. Each event has one condition and can have multiple actions.
An unconditional event is an event that all its actions will be executed without any condition. This document is a brief on how to create a conditional event.
Each command in HyperDbg that are tagged as an "event" in the document follows the same structure described here. At the time you execute a command, you can add a condition { xx xx xx xx }
where xx
is the assembly (hex) of what you want to be executed in the case of that event.
Starting from v0.10, HyperDbg supports direct assembly code in the condition section. You can add asm condition { asm1; asm2; asm3; asm4}
where you can add any assembly code to be executed in the case of that event.
For example, let's imagine we want to create a condition for a command like "!epthook".
When you execute the command like :
then it is unconditional, but when you execute a command like this:
or a command like this:
then it is a conditional command.
Note that you can use all of the events in the same way (instead of !epthook). For example, you can use !syscall, !sysret, !epthook2, !ioin, etc.
Parameters to Conditions
Conditions will be executed like the above function so that you can expect a pointer to target debuggee's general-purpose registers on rcx
and a Context in rdx
.
The structure for general-purpose registers is :
The Context
is a special variable that shows an essential parameter of an event. This value is different for each event. You should check the documentation of that command for more information about the Context
. For example, Context
for !syscall command is the syscall-number or for the !epthook2 command is the physical address of where the hidden hook triggered.
Example 1
Imagine we want to check for the name of the process, so only if the name contains the "svchost.exe" then triggers the event's action(s).
We all know that you can search for the name of the process in its _EPROCESS
.
For example, ImageFileName in _EPROCESS contains the 15 characters of the process name. It is not where Windows shows the name in Task Manager but checking this value is enough.
The following assembly code gets the current _KTHREAD
from _KPCR
. From there, we can find the address of _KPROCESS
, and this structure is located at the start address of _EPROCESS
.
As you can see from the above picture, ImageFileName is located at +0x450
after the _EPROCESS
.
So our final assembly code is like this :
The offsets of _EPROCESS
and other structures might change in the different versions of Windows.
Now we should assemble the above code into its hex representation in the assembly. For example, you can use an online assembler.
Keep in mind that if you return with rax=0
or null
then it means false, and if you return anything other than zero (for example rax=1
) then it means true.
If you return true, then all the actions of that event will be executed, and if you return false, then HyperDbg ignores the actions of that event.
The final result of the assembler is :
Now you can call the command with the following arguments :
or
We automatically add a 0xc3
or ret
the opcode to the end of the condition assembly, and in the case if you forget to return the control of the processor back to the HyperDbg, then there is no problem. Make sure to not jump to another address without returning back to the HyperDbg. Otherwise, it causes a crash on your system.
Example 2
Sometimes we need to read the registers and decide based on them. For example, let's imagine we want to hook ExAllocatePoolWithTag
and if the size of the requested buffer is xx
then perform the actions.
This function (ExAllocatePoolWithTag
) is defined like this :
It's obvious that based on the x64 fastcall calling convention in Windows, PoolType
is on rcx
, NumberOfBytes
is on rdx
and Tag
is on r8
. We need to check for rdx
.
Note that rdx
is not the same as the rdx
that you receive in the function, instead we pass a structure containing all the general-purpose register, you can read them or even modify them, and if you modify them, then the operating system will continue with new values in these registers.
For general-purpose registers, we pass a pointer to the following structure as the first argument on rcx
.
If you want to change or examine other registers like XMM registers, floating-point registers, or other registers, you can change and examine them directly.
In the following example, we want to check NumberOfBytes (rdx)
with 0x1000
and if the requested size is 0x1000, then the actions should be performed.
After using assembler to convert the above code to hex representation of assembly, the final command will be like this :
One important note is that if you want to create a condition for !syscall command, which is common, then you should know that the syscall calling convention is fastcall (rcx
, rdx
, r8
, r9
and stack), so if your target user-mode application is x64, then you can expect the exact arguments from user-mode to kernel-mode. Still, if your user-mode application is x86, then Windows might change some of the arguments that contain addresses to new addresses.
Example 3
Now, let's run the above code by using the assembly code directly without converting them to hexadecimal by using HyperDbg's assembler:
The above code uses the internal assembler of HyperDbg.
Accessing random memory in custom code and condition code in vmx root-mode is considered "unsafe". You have some limitations on accessing memory on some special events.
Last updated