HyperDbg Debugger
Loading...
Searching...
No Matches
libhyperdbg.cpp File Reference

Main interface to connect applications to driver. More...

#include "pch.h"

Functions

INT HyperDbgStartDriver ()
 Install (start) VMM driver.
INT HyperDbgStopDriver (LPCTSTR DriverName)
 Stop the driver.
INT HyperDbgInstallKdDriver ()
 Install KD (Kernel Debugger) driver.
INT HyperDbgStartKdDriver ()
 Start KD driver.
INT HyperDbgStopKdDriver ()
 Stop KD driver.
INT HyperDbgUninstallDriver (LPCTSTR DriverName)
 Remove the driver.
INT HyperDbgUninstallKdDriver ()
 Remove the KD (Kernel Debugger) driver.
INT HyperDbgInitHyperTraceModule ()
 Initialize VMM module.
INT HyperDbgInitVmmModule ()
 Initialize VMM module.
INT HyperDbgCreateHandleFromKdModule ()
 Create handle from KD (HyperKD) module.
INT HyperDbgUnloadVmm ()
 Unload VMM module.
INT HyperDbgUnloadHyperTrace ()
 Unload HyperTrace module.
INT HyperDbgUnloadKd ()
 Unload KD driver.
BOOLEAN HyperDbgIsAnyModuleLoaded ()
 check if any module is loaded (KD, VMM, HyperTrace, etc.)
INT HyperDbgUnloadAllModules ()
 unload all modules (KD, VMM, HyperTrace, etc.)
INT HyperDbgLoadKdModule ()
 load kd module
GENERIC_PROCESSOR_VENDOR HyperDbgGetProcessorVendor ()
 Get the vendor of the current processor.
INT HyperDbgLoadVmmModule ()
 load vmm module
INT HyperDbgLoadHyperTraceModule ()
 load hypertrace module
INT HyperDbgLoadAllModules ()
 load all modules (KD, VMM, HyperTrace, etc.)

Variables

HANDLE g_DeviceHandle
 Holds the global handle of device which is used to send the request to the kernel by IOCTL, this handle is not used for IRP Pending of message tracing this handle is used in KD VMM.
HANDLE g_IsDriverLoadedSuccessfully
 Handle to show that if the debugger is loaded successfully.
BOOLEAN g_IsMessageLoggingWindowClosed
 Shows whether the message logging window is closed or not.
TCHAR g_DriverLocation [MAX_PATH]
 Holds the location driver to install it.
TCHAR g_DriverName [MAX_PATH]
 Holds the name of the driver to install it.
BOOLEAN g_UseCustomDriverLocation
 Whether the user wants to use a custom driver location or not.
LIST_ENTRY g_EventTrace
 Holds a list of events in kernel and the state of events and the commands to show the state of each command (disabled/enabled).
BOOLEAN g_IsKdModuleLoaded
 shows whether the kernel debugger (KD) module is loaded or not
BOOLEAN g_IsVmmModuleLoaded
 shows whether the VMM module is loaded or not
BOOLEAN g_IsHyperTraceModuleLoaded
 shows whether the HyperTrace module is loaded or not

Detailed Description

Main interface to connect applications to driver.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.1
Date
2020-04-11

Function Documentation

◆ HyperDbgCreateHandleFromKdModule()

INT HyperDbgCreateHandleFromKdModule ( )

Create handle from KD (HyperKD) module.

Returns
INT return zero if it was successful or non-zero if there was error

lpSecurityAttirbutes

lpTemplateFile

312{
313 DWORD ErrorNum;
314 DWORD ThreadId;
315
316 if (g_DeviceHandle)
317 {
318 ShowMessages("handle of the driver found, if you use the 'load' command before, please "
319 "unload it using the 'unload' command\n");
320 return 1;
321 }
322
323 //
324 // Make sure that this variable is false, because it might be set to
325 // true as the result of a previous load
326 //
328
329 //
330 // Init entering vmx
331 //
332 g_DeviceHandle = CreateFileA(
333 "\\\\.\\HyperDbgDebuggerDevice",
334 GENERIC_READ | GENERIC_WRITE,
335 FILE_SHARE_READ | FILE_SHARE_WRITE,
336 NULL,
337 OPEN_EXISTING,
338 FILE_ATTRIBUTE_NORMAL,
339 NULL);
340
341 if (g_DeviceHandle == INVALID_HANDLE_VALUE)
342 {
343 ErrorNum = GetLastError();
344 if (ErrorNum == ERROR_ACCESS_DENIED)
345 {
346 ShowMessages("err, access denied\nare you sure you have administrator "
347 "rights?\n");
348 }
349 else if (ErrorNum == ERROR_GEN_FAILURE)
350 {
351 ShowMessages("err, a device attached to the system is not functioning\n"
352 "vmx feature might be disabled from BIOS or VBS/HVCI is active\n");
353 }
354 else
355 {
356 ShowMessages("err, CreateFile failed (%x)\n", ErrorNum);
357 }
358
360 return 1;
361 }
362
363 //
364 // Initialize the list of events
365 //
366 InitializeListHead(&g_EventTrace);
367
368#if !UseDbgPrintInsteadOfUsermodeMessageTracking
369 HANDLE Thread = CreateThread(NULL, 0, IrpBasedBufferThread, NULL, 0, &ThreadId);
370
371 // if (Thread)
372 // {
373 // ShowMessages("thread Created successfully\n");
374 // }
375
376#endif
377
378 return 0;
379}
#define FALSE
Definition BasicTypes.h:113
unsigned long DWORD
Definition BasicTypes.h:38
HANDLE g_DeviceHandle
Holds the global handle of device which is used to send the request to the kernel by IOCTL,...
Definition globals.h:481
LIST_ENTRY g_EventTrace
Holds a list of events in kernel and the state of events and the commands to show the state of each c...
Definition globals.h:410
BOOLEAN g_IsMessageLoggingWindowClosed
Shows whether the message logging window is closed or not.
Definition globals.h:472
NULL()
Definition test-case-generator.py:530
DWORD WINAPI IrpBasedBufferThread(PVOID Data)
Create a thread for pending buffers.
Definition packets.cpp:337

◆ HyperDbgGetProcessorVendor()

GENERIC_PROCESSOR_VENDOR HyperDbgGetProcessorVendor ( )

Get the vendor of the current processor.

Returns
GENERIC_PROCESSOR_VENDOR the vendor of the processor
719{
720 CHAR CpuId[13] = {0};
721
722 //
723 // Read the vendor string
724 //
725 CpuReadVendorString(CpuId);
726
727 // ShowMessages("current processor vendor is : %s\n", CpuId);
728
729 if (strcmp(CpuId, "GenuineIntel") == 0)
730 {
732 }
733 else if (strcmp(CpuId, "AuthenticAMD") == 0)
734 {
736 }
737 else
738 {
740 }
741}
char CHAR
Definition BasicTypes.h:33
@ GENERIC_PROCESSOR_VENDOR_INTEL
Definition DataTypes.h:41
@ GENERIC_PROCESSOR_VENDOR_AMD
Definition DataTypes.h:42
@ GENERIC_PROCESSOR_VENDOR_OTHERS
Definition DataTypes.h:43
VOID CpuReadVendorString(CHAR *Result)
Reads the CPU vendor string.
Definition cpu.cpp:252

◆ HyperDbgInitHyperTraceModule()

INT HyperDbgInitHyperTraceModule ( )

Initialize VMM module.

Returns
INT return zero if it was successful or non-zero if there was error
191{
192 BOOL Status;
193 DWORD BytesReturned;
194 DEBUGGER_INIT_HYPERTRACE_PACKET InitHyperTracePacket = {0};
195
197
198 //
199 // Send IOCTL to initialize HyperTrace module
200 //
201 Status = DeviceIoControl(g_DeviceHandle, // Handle to device
202 IOCTL_INIT_HYPERTRACE, // IO Control Code (IOCTL)
203 &InitHyperTracePacket, // Input Buffer to driver.
204 SIZEOF_DEBUGGER_INIT_HYPERTRACE_PACKET, // Length of input buffer in bytes.
205 &InitHyperTracePacket, // Output Buffer from driver.
206 SIZEOF_DEBUGGER_INIT_HYPERTRACE_PACKET, // Length of output buffer in bytes.
207 &BytesReturned, // Bytes placed in buffer.
208 NULL // synchronous call
209 );
210
211 //
212 // Check if the IOCTL was successful, if not show the error message and return
213 //
214 if (!Status)
215 {
216 ShowMessages("ioctl failed with code 0x%x\n", GetLastError());
217 return 1;
218 }
219
220 //
221 // Check the kernel status
222 //
223 if (InitHyperTracePacket.KernelStatus != DEBUGGER_OPERATION_WAS_SUCCESSFUL)
224 {
225 ShowErrorMessage(InitHyperTracePacket.KernelStatus);
226 return 1;
227 }
228
229 return 0;
230}
int BOOL
Definition BasicTypes.h:25
#define DEBUGGER_OPERATION_WAS_SUCCESSFUL
General value to indicate that the operation or request was successful.
Definition ErrorCodes.h:23
#define IOCTL_INIT_HYPERTRACE
ioctl, initialize the HyperTrace module
Definition Ioctls.h:107
struct _DEBUGGER_INIT_HYPERTRACE_PACKET DEBUGGER_INIT_HYPERTRACE_PACKET
request for initializing HyperTrace
#define SIZEOF_DEBUGGER_INIT_HYPERTRACE_PACKET
Definition RequestStructures.h:31
BOOLEAN ShowErrorMessage(UINT32 Error)
shows the error message
Definition debugger.cpp:40
#define ASSERT_MESSAGE_KD_NOT_LOADED
Definition common.h:29
#define AssertShowMessageReturnStmt(expr1, expr2, message1, message2, rc)
Definition common.h:59
#define AssertReturnOne
Definition common.h:23
#define ASSERT_MESSAGE_DRIVER_NOT_LOADED
Definition common.h:27
BOOLEAN g_IsKdModuleLoaded
shows whether the kernel debugger (KD) module is loaded or not
Definition globals.h:22
UINT32 KernelStatus
Definition RequestStructures.h:40

◆ HyperDbgInitVmmModule()

INT HyperDbgInitVmmModule ( )

Initialize VMM module.

Returns
INT return zero if it was successful or non-zero if there was error
240{
241 BOOL Status;
242 DWORD BytesReturned;
243 DEBUGGER_INIT_VMM_PACKET InitVmmPacket = {0};
244
246
247 //
248 // Create event to show if the kd module is loaded or not
249 //
250 g_IsDriverLoadedSuccessfully = CreateEvent(NULL, FALSE, FALSE, NULL);
251
252 //
253 // Send IOCTL to initialize VMM module
254 //
255 Status = DeviceIoControl(g_DeviceHandle, // Handle to device
256 IOCTL_INIT_VMM, // IO Control Code (IOCTL)
257 &InitVmmPacket, // Input Buffer to driver.
258 SIZEOF_DEBUGGER_INIT_VMM_PACKET, // Length of input buffer in bytes.
259 &InitVmmPacket, // Output Buffer from driver.
260 SIZEOF_DEBUGGER_INIT_VMM_PACKET, // Length of output buffer in bytes.
261 &BytesReturned, // Bytes placed in buffer.
262 NULL // synchronous call
263 );
264
265 //
266 // check if the IOCTL was successful, if not show the error message and return
267 //
268 if (!Status)
269 {
270 ShowMessages("ioctl failed with code 0x%x\n", GetLastError());
271 CloseHandle(g_IsDriverLoadedSuccessfully);
272 return 1;
273 }
274
275 //
276 // Check the kernel status for early errors (e.g., HyperTrace already loaded)
277 //
278 if (InitVmmPacket.KernelStatus != DEBUGGER_OPERATION_WAS_SUCCESSFUL &&
279 InitVmmPacket.KernelStatus != 0)
280 {
281 ShowErrorMessage(InitVmmPacket.KernelStatus);
282 CloseHandle(g_IsDriverLoadedSuccessfully);
283 return 1;
284 }
285
286 //
287 // We wait for the first message from the kernel debugger to continue
288 //
289 WaitForSingleObject(
291 INFINITE);
292
293 //
294 // No need to handle anymore
295 //
296 CloseHandle(g_IsDriverLoadedSuccessfully);
297
298 //
299 // VMM module is initialized at this point
300 //
301 return 0;
302}
#define IOCTL_INIT_VMM
ioctl, initialize the VMM module
Definition Ioctls.h:100
#define SIZEOF_DEBUGGER_INIT_VMM_PACKET
Definition RequestStructures.h:16
struct _DEBUGGER_INIT_VMM_PACKET DEBUGGER_INIT_VMM_PACKET
request for initializing VMM
HANDLE g_IsDriverLoadedSuccessfully
Handle to show that if the debugger is loaded successfully.
Definition globals.h:152
UINT32 KernelStatus
Definition RequestStructures.h:25

◆ HyperDbgInstallKdDriver()

INT HyperDbgInstallKdDriver ( )

Install KD (Kernel Debugger) driver.

Returns
INT return zero if it was successful or non-zero if there was error
82{
83 //
84 // Check if the driver is already loaded, if that's the case, we shouldn't try to load it again
85 //
87 {
88 //
89 // The driver is already loaded, so we shouldn't try to load it again
90 // but we can consider it as success and return zero
91 //
92 return 0;
93 }
94
95 //
96 // The driver is not started yet so let us the install driver
97 // First setup full path to driver name
98 //
99
100 //
101 // If the user has not specified a custom driver location, then we
102 // need to find the driver in the same directory as the executable
103 //
105 {
107 {
108 return 1;
109 }
110
111 //
112 // Use default driver name
113 //
115 }
116
117 if (HyperDbgStartDriver() != 0)
118 {
119 ShowMessages("unable to install KD driver\n");
120
121 return 1;
122 }
123
124 return 0;
125}
#define KERNEL_DEBUGGER_DRIVER_NAME
name of HyperDbg's debugger driver
Definition Definition.h:40
#define KERNEL_DEBUGGER_DRIVER_NAME_AND_EXTENSION
name of HyperDbg's debugger driver + extension
Definition Definition.h:46
#define TRUE
Definition BasicTypes.h:114
BOOLEAN SetupPathForFileName(const CHAR *FileName, _Inout_updates_bytes_all_(BufferLength) PCHAR FileLocation, ULONG BufferLength, BOOLEAN CheckFileExists)
Setup file name.
Definition install.cpp:472
INT HyperDbgStartDriver()
Install (start) VMM driver.
Definition libhyperdbg.cpp:37
TCHAR g_DriverLocation[MAX_PATH]
Holds the location driver to install it.
Definition globals.h:433
TCHAR g_DriverName[MAX_PATH]
Holds the name of the driver to install it.
Definition globals.h:439
BOOLEAN g_UseCustomDriverLocation
Whether the user wants to use a custom driver location or not.
Definition globals.h:445

◆ HyperDbgIsAnyModuleLoaded()

BOOLEAN HyperDbgIsAnyModuleLoaded ( )

check if any module is loaded (KD, VMM, HyperTrace, etc.)

Returns
BOOLEAN return TRUE if any module is loaded, otherwise return FALSE
605{
606 INT RetVal = 0;
607
608 //
609 // Check if any module is loaded (KD, VMM, HyperTrace, etc.)
610 //
612 {
613 return TRUE;
614 }
615 else
616 {
617 return FALSE;
618 }
619}
int INT
Definition BasicTypes.h:43
BOOLEAN g_IsHyperTraceModuleLoaded
shows whether the HyperTrace module is loaded or not
Definition lbrdump.cpp:19
BOOLEAN g_IsVmmModuleLoaded
shows whether the VMM module is loaded or not
Definition globals.h:28

◆ HyperDbgLoadAllModules()

INT HyperDbgLoadAllModules ( )

load all modules (KD, VMM, HyperTrace, etc.)

Returns
INT return zero if it was successful or non-zero if there was error
909{
910 INT RetVal = 0;
911
912 //
913 // Load KD module if not loaded
914 //
916 {
917 return 1;
918 }
919
920 //
921 // Load VMM module if not loaded
922 //
924 {
925 return 1;
926 }
927
928 //
929 // Load HyperTrace module if not loaded
930 //
932 {
933 return 1;
934 }
935
936 return 0;
937}
INT HyperDbgLoadHyperTraceModule()
load hypertrace module
Definition libhyperdbg.cpp:838
INT HyperDbgLoadVmmModule()
load vmm module
Definition libhyperdbg.cpp:749
INT HyperDbgLoadKdModule()
load kd module
Definition libhyperdbg.cpp:665

◆ HyperDbgLoadHyperTraceModule()

INT HyperDbgLoadHyperTraceModule ( )

load hypertrace module

Returns
int return zero if it was successful or non-zero if there was error
839{
840 //
841 // Check if the module is already loaded, if that's the case, we don't
842 // need to handle anymore
843 //
845 {
846 //
847 // Return zero to indicate that the module is loaded successfully,
848 // and we no need to re-load it anymore
849 //
850 return 0;
851 }
852
853 //
854 // Enable Debug privilege to the current token
855 //
857 {
858 ShowMessages("err, couldn't set debug privilege\n");
859 return 1;
860 }
861
862 //
863 // Check if the processor is a genuine Intel processor (required for HyperTrace)
864 //
866 {
867 ShowMessages("err, this program is not designed to run in a non-Intel "
868 "environment as it needs Intel PT (Processor Trace) and Intel LBR "
869 "(Last Branch Record). It needs an Intel processor.\n");
870 return 1;
871 }
872
873 //
874 // Load the KD module and create handle to it
875 //
876 if (HyperDbgLoadKdModule() == 1)
877 {
878 return 1;
879 }
880
881 //
882 // Initialize HyperTrace module
883 //
885 {
886 ShowMessages("err, initializing hypertrace module\n");
887
888 return 1;
889 }
890
891 //
892 // If we reach here so the module are loaded
893 //
895
896 ShowMessages("hypertrace (trace) module is running...\n");
897
898 return 0;
899}
GENERIC_PROCESSOR_VENDOR HyperDbgGetProcessorVendor()
Get the vendor of the current processor.
Definition libhyperdbg.cpp:718
INT HyperDbgInitHyperTraceModule()
Initialize VMM module.
Definition libhyperdbg.cpp:190
BOOLEAN WindowsSetDebugPrivilege()
Adjust kernel debug privilege.
Definition windows-privilege.c:25

◆ HyperDbgLoadKdModule()

INT HyperDbgLoadKdModule ( )

load kd module

Returns
int return zero if it was successful or non-zero if there
666{
667 //
668 // Check if the module is already loaded, if that's the case, we don't
669 // need to handle anymore
670 //
672 {
673 //
674 // Return zero to indicate that the module is loaded successfully,
675 // and we no need to re-load it anymore
676 //
677 return 0;
678 }
679
680 //
681 // Enable Debug privilege to the current token
682 //
684 {
685 ShowMessages("err, couldn't set debug privilege\n");
686 return 1;
687 }
688
689 //
690 // Create handle from the KD module
691 //
693 {
694 //
695 // No need to handle anymore
696 //
697 return 1;
698 }
699
700 //
701 // KD module is loaded at this point
702 //
703
704 //
705 // If we reach here so the module are loaded
706 //
708
709 return 0;
710}
INT HyperDbgCreateHandleFromKdModule()
Create handle from KD (HyperKD) module.
Definition libhyperdbg.cpp:311

◆ HyperDbgLoadVmmModule()

INT HyperDbgLoadVmmModule ( )

load vmm module

Returns
int return zero if it was successful or non-zero if there
750{
751 //
752 // Check if the module is already loaded, if that's the case, we don't
753 // need to handle anymore
754 //
756 {
757 //
758 // Return zero to indicate that the module is loaded successfully,
759 // and we no need to re-load it anymore
760 //
761 return 0;
762 }
763
764 //
765 // Check if the HyperTrace module is loaded or not
766 //
768 {
769 ShowMessages(
770 "err, the trace module is currently loaded and should be unloaded before loading the vmm module\n"
771 "Note that HyperTrace (trace) uses the hypervisor (vmm) features when it is loaded after the vmm module, "
772 "however, HyperTrace can also operate without the vmm module, although hypervisor-specific features will not be available\n"
773 "to solve this problem, first unload the trace module using the 'unload trace' command, next, load "
774 "the vmm module and then load the trace module again. This way, the trace module is reloaded with hypervisor APIs\n");
775 return 1;
776 }
777
778 //
779 // Check if the processor is a genuine Intel processor (required for VT-x)
780 //
782 {
783 ShowMessages("err, this program is not designed to run in a non-VT-x "
784 "environment. It needs an Intel processor.\n");
785 return 1;
786 }
787
788 ShowMessages("virtualization technology is vt-x\n");
789
791 {
792 ShowMessages("vmx operation is supported by your processor\n");
793 }
794 else
795 {
796#ifdef HYPERDBG_ENV_WINDOWS
797 ShowMessages("vmx operation is not supported by your processor "
798 "(if you are using an Intel processor, it might be because VBS is not disabled!)\n");
799#endif
800 return 1;
801 }
802
803 //
804 // Load the KD module and create handle to it
805 //
806 if (HyperDbgLoadKdModule() == 1)
807 {
808 return 1;
809 }
810
811 //
812 // Initialize VMM module
813 //
814 if (HyperDbgInitVmmModule() == 1)
815 {
816 ShowMessages("err, initializing VMM module\n");
817
818 return 1;
819 }
820
821 //
822 // If we reach here so the module are loaded
823 //
825
826 ShowMessages("vmm module is running...\n");
827
828 return 0;
829}
BOOLEAN VmxSupportDetection()
Detect whether the VMX is supported or not.
Definition common.cpp:626
INT HyperDbgInitVmmModule()
Initialize VMM module.
Definition libhyperdbg.cpp:239

◆ HyperDbgStartDriver()

INT HyperDbgStartDriver ( )

Install (start) VMM driver.

Returns
INT return zero if it was successful or non-zero if there was error
38{
40 {
41 //
42 // Error - remove driver
43 //
45
46 return 1;
47 }
48
49 return 0;
50}
BOOLEAN ManageDriver(LPCTSTR DriverName, LPCTSTR ServiceName, UINT16 Function)
Manage Driver.
Definition install.cpp:150
#define DRIVER_FUNC_REMOVE
Definition install.h:36
#define DRIVER_FUNC_INSTALL
Definition install.h:34

◆ HyperDbgStartKdDriver()

INT HyperDbgStartKdDriver ( )

Start KD driver.

Returns
INT return zero if it was successful or non-zero if there was error
134{
135 return HyperDbgStartDriver();
136}

◆ HyperDbgStopDriver()

INT HyperDbgStopDriver ( LPCTSTR DriverName)

Stop the driver.

Returns
INT return zero if it was successful or non-zero if there was error
60{
61 //
62 // Unload the driver if loaded
63 //
64 if (g_DriverLocation[0] != (TCHAR)0 && ManageDriver(DriverName, g_DriverLocation, DRIVER_FUNC_STOP))
65 {
66 return 0;
67 }
68 else
69 {
70 return 1;
71 }
72}
#define DRIVER_FUNC_STOP
Definition install.h:35

◆ HyperDbgStopKdDriver()

INT HyperDbgStopKdDriver ( )

Stop KD driver.

Returns
INT return zero if it was successful or non-zero if there was error
145{
147}
INT HyperDbgStopDriver(LPCTSTR DriverName)
Stop the driver.
Definition libhyperdbg.cpp:59

◆ HyperDbgUninstallDriver()

INT HyperDbgUninstallDriver ( LPCTSTR DriverName)

Remove the driver.

Returns
INT return zero if it was successful or non-zero if there was error
157{
158 //
159 // Unload the driver if loaded. Ignore any errors
160 //
161 if (g_DriverLocation[0] != (TCHAR)0 && ManageDriver(DriverName, g_DriverLocation, DRIVER_FUNC_REMOVE))
162 {
163 return 0;
164 }
165 else
166 {
167 return 1;
168 }
169}

◆ HyperDbgUninstallKdDriver()

INT HyperDbgUninstallKdDriver ( )

Remove the KD (Kernel Debugger) driver.

Returns
INT return zero if it was successful or non-zero if there was error
179{
181}
INT HyperDbgUninstallDriver(LPCTSTR DriverName)
Remove the driver.
Definition libhyperdbg.cpp:156

◆ HyperDbgUnloadAllModules()

INT HyperDbgUnloadAllModules ( )

unload all modules (KD, VMM, HyperTrace, etc.)

Returns
INT return zero if it was successful or non-zero if there was error
629{
630 INT RetVal = 0;
631
632 //
633 // Unload HyperTrace module if loaded
634 //
636 {
637 return 1;
638 }
639
640 //
641 // Unload VMM module if loaded
642 //
644 {
645 return 1;
646 }
647
648 //
649 // Unload KD module if loaded
650 //
652 {
653 return 1;
654 }
655
656 return 0;
657}
INT HyperDbgUnloadVmm()
Unload VMM module.
Definition libhyperdbg.cpp:388
INT HyperDbgUnloadHyperTrace()
Unload HyperTrace module.
Definition libhyperdbg.cpp:457
INT HyperDbgUnloadKd()
Unload KD driver.
Definition libhyperdbg.cpp:505

◆ HyperDbgUnloadHyperTrace()

INT HyperDbgUnloadHyperTrace ( )

Unload HyperTrace module.

Returns
INT return zero if it was successful or non-zero if there was error
458{
459 BOOL Status;
460 DWORD BytesReturned;
461
463
464 ShowMessages("start terminating trace module...\n");
465
466 //
467 // Send IOCTL to unload HyperTrace module
468 //
469 Status = DeviceIoControl(g_DeviceHandle, // Handle to device
470 IOCTL_PERFORM_HYPERTRACE_UNLOAD, // IO Control Code (IOCTL)
471 NULL, // Input Buffer to driver.
472 0, // Length of input buffer in bytes. (x 2 is bcuz
473 // as the driver is x64 and has 64 bit values)
474 NULL, // Output Buffer from driver.
475 0, // Length of output buffer in bytes.
476 &BytesReturned, // Bytes placed in buffer.
477 NULL // synchronous call
478 );
479
480 //
481 // wait to make sure we don't use an invalid handle in another Ioctl
482 //
483 if (!Status)
484 {
485 ShowMessages("ioctl failed with code 0x%x\n", GetLastError());
486 return 1;
487 }
488
489 //
490 // HyperTrace module is not loaded anymore
491 //
493
494 ShowMessages("the trace module is unloaded!\n");
495
496 return 0;
497}
#define IOCTL_PERFORM_HYPERTRACE_UNLOAD
ioctl, to unload HyperTrace module
Definition Ioctls.h:400
#define ASSERT_MESSAGE_HYPERTRACE_NOT_LOADED
Definition common.h:33

◆ HyperDbgUnloadKd()

INT HyperDbgUnloadKd ( )

Unload KD driver.

Returns
INT return zero if it was successful or non-zero if there was error
506{
507 BOOL Status;
508 DWORD BytesReturned;
509
511
512 //
513 // Check if HyperTrace module is loaded, if so we need to unload it before unloading KD because KD is used by HyperTrace
514 //
516 {
517 ShowMessages("err, unable to unload the kd module because the trace module is currently loaded "
518 "and uses the kd module, if you want to unload the kd module, please first unload "
519 "the trace module using the 'unload trace' command\n");
520 return 1;
521 }
522
523 //
524 // Check if VMM module is loaded, if so we need to unload it before unloading KD because KD is used by VMM
525 //
527 {
528 ShowMessages("err, unable to unload the kd module because the vmm module is currently loaded "
529 "and uses the kd module, if you want to unload the kd module, please first unload "
530 "the vmm module using the 'unload vmm' command\n");
531 return 1;
532 }
533
534 //
535 // Indicate that the message logging window is closed
536 //
538
539 //
540 // Send IOCTL to mark complete all IRP Pending
541 //
542 Status = DeviceIoControl(
543 g_DeviceHandle, // Handle to device
545 NULL, // Input Buffer to driver.
546 0, // Length of input buffer in bytes. (x 2 is bcuz as the
547 // driver is x64 and has 64 bit values)
548 NULL, // Output Buffer from driver.
549 0, // Length of output buffer in bytes.
550 &BytesReturned, // Bytes placed in buffer.
551 NULL // synchronous call
552 );
553
554 //
555 // wait to make sure we don't use an invalid handle in another Ioctl
556 //
557 if (!Status)
558 {
559 ShowMessages("ioctl failed with code 0x%x\n", GetLastError());
560 return 1;
561 }
562
563 //
564 // Wait for a while to make sure that all IRP pending are completed and the driver is ready to be unloaded
565 //
566 Sleep(1000);
567
568 //
569 // Send IRP_MJ_CLOSE to driver to terminate Vmxs
570 //
571 if (!CloseHandle(g_DeviceHandle))
572 {
573 ShowMessages("err, closing handle 0x%x\n", GetLastError());
574 return 1;
575 };
576
577 //
578 // Null the handle to indicate that the driver's device is not ready
579 // to use
580 //
582
583 //
584 // Debugger module is not loaded anymore
585 //
587
588 //
589 // Check if we found an already built symbol table
590 //
592
593 ShowMessages("the debugger module is unloaded!\n");
594
595 return 0;
596}
#define IOCTL_RETURN_IRP_PENDING_PACKETS_AND_DISALLOW_IOCTL
ioctl, irp pending mechanism for reading from message tracing buffers
Definition Ioctls.h:121
BOOLEAN SymbolDeleteSymTable()
Delete and free structures and variables related to the symbols.
Definition symbol.cpp:717

◆ HyperDbgUnloadVmm()

INT HyperDbgUnloadVmm ( )

Unload VMM module.

Returns
INT return zero if it was successful or non-zero if there was error
389{
390 BOOL Status;
391 DWORD BytesReturned;
392
394
395 ShowMessages("start terminating vmm...\n");
396
397 //
398 // Check if HyperTrace module is loaded, if so we need to unload it before unloading VMM
399 //
401 {
402 ShowMessages(
403 "the trace module is currently loaded and will be unloaded before the vmm module\nnote that hypertrace (trace) "
404 "use the hypervisor (vmm) features when it is loaded after vmm, however, hypertrace can also operate without the vmm "
405 "module, although hypervisor-specific features will not be available\n"
406 "the 'trace' module will now be unloaded automatically. You can reload it later "
407 "using the command 'load trace', which will load the trace module again without enabling hypervisor-dependent features\n");
408
410 }
411
412 //
413 // Uninitialize the user debugger if it's initialized
414 //
416
417 //
418 // Send IOCTL terminate VMX
419 //
420 Status = DeviceIoControl(g_DeviceHandle, // Handle to device
421 IOCTL_TERMINATE_VMX, // IO Control Code (IOCTL)
422 NULL, // Input Buffer to driver.
423 0, // Length of input buffer in bytes. (x 2 is bcuz
424 // as the driver is x64 and has 64 bit values)
425 NULL, // Output Buffer from driver.
426 0, // Length of output buffer in bytes.
427 &BytesReturned, // Bytes placed in buffer.
428 NULL // synchronous call
429 );
430
431 //
432 // wait to make sure we don't use an invalid handle in another Ioctl
433 //
434 if (!Status)
435 {
436 ShowMessages("ioctl failed with code 0x%x\n", GetLastError());
437 return 1;
438 }
439
440 //
441 // Hypervisor (VMM) module is not loaded anymore
442 //
444
445 ShowMessages("you're not on HyperDbg's hypervisor anymore!\n");
446
447 return 0;
448}
VOID UdUninitializeUserDebugger()
uninitialize user debugger
Definition Ud.c:90
#define IOCTL_TERMINATE_VMX
ioctl, to terminate vmx and exit form debugger
Definition Ioctls.h:136
#define ASSERT_MESSAGE_VMM_NOT_LOADED
Definition common.h:31

Variable Documentation

◆ g_DeviceHandle

HANDLE g_DeviceHandle
extern

Holds the global handle of device which is used to send the request to the kernel by IOCTL, this handle is not used for IRP Pending of message tracing this handle is used in KD VMM.

◆ g_DriverLocation

TCHAR g_DriverLocation[MAX_PATH]
extern

Holds the location driver to install it.

433{0};

◆ g_DriverName

TCHAR g_DriverName[MAX_PATH]
extern

Holds the name of the driver to install it.

439{0};

◆ g_EventTrace

LIST_ENTRY g_EventTrace
extern

Holds a list of events in kernel and the state of events and the commands to show the state of each command (disabled/enabled).

this list is not have any relation with the things that HyperDbg holds for each event in the kernel

410{0};

◆ g_IsDriverLoadedSuccessfully

HANDLE g_IsDriverLoadedSuccessfully
extern

Handle to show that if the debugger is loaded successfully.

◆ g_IsHyperTraceModuleLoaded

BOOLEAN g_IsHyperTraceModuleLoaded
extern

shows whether the HyperTrace module is loaded or not

◆ g_IsKdModuleLoaded

BOOLEAN g_IsKdModuleLoaded
extern

shows whether the kernel debugger (KD) module is loaded or not

◆ g_IsMessageLoggingWindowClosed

BOOLEAN g_IsMessageLoggingWindowClosed
extern

Shows whether the message logging window is closed or not.

◆ g_IsVmmModuleLoaded

BOOLEAN g_IsVmmModuleLoaded
extern

shows whether the VMM module is loaded or not

◆ g_UseCustomDriverLocation

BOOLEAN g_UseCustomDriverLocation
extern

Whether the user wants to use a custom driver location or not.