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

headers for libhyperdbg More...

Go to the source code of this file.

Functions

BOOLEAN HyperDbgIsAnyModuleLoaded ()
 check if any module is loaded (KD, VMM, HyperTrace, etc.)
INT HyperDbgUnloadAllModules ()
 unload all modules (KD, VMM, HyperTrace, etc.)
INT HyperDbgInitHyperTraceModule ()
 Initialize VMM module.
INT HyperDbgUnloadVmm ()
 Unload VMM module.
INT HyperDbgUnloadHyperTrace ()
 Unload HyperTrace module.
INT HyperDbgUnloadKd ()
 Unload KD driver.
INT HyperDbgInstallKdDriver ()
 Install KD (Kernel Debugger) driver.
INT HyperDbgUninstallKdDriver ()
 Remove the KD (Kernel Debugger) driver.
INT HyperDbgLoadKdModule ()
 load kd module
INT HyperDbgLoadVmmModule ()
 load vmm module
INT HyperDbgLoadHyperTraceModule ()
 load hypertrace module
INT HyperDbgLoadAllModules ()
 load all modules (KD, VMM, HyperTrace, etc.)
INT HyperDbgStartKdDriver ()
 Start KD driver.
INT HyperDbgStopKdDriver ()
 Stop KD driver.
INT HyperDbgInterpreter (CHAR *Command)
 Interpret commands.
INT HyperDbgScriptReadFileAndExecuteCommandline (INT argc, CHAR *argv[])
 Parsing the command line options for scripts.
GENERIC_PROCESSOR_VENDOR HyperDbgGetProcessorVendor ()
 Get the vendor of the current processor.
BOOLEAN HyperDbgTestCommandParser (CHAR *Command, UINT32 NumberOfTokens, CHAR **TokensList, UINT32 *FailedTokenNum, UINT32 *FailedTokenPosition)
 Parse the command (used for testing purposes).
VOID HyperDbgTestCommandParserShowTokens (CHAR *Command)
 Parse the command and show tokens (used for testing purposes).
VOID HyperDbgShowSignature ()
 Show signature of HyperDbg.

Detailed Description

headers for libhyperdbg

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.10
Date
2024-06-24

Function Documentation

◆ 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
unsigned long DWORD
Definition BasicTypes.h:38
#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
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
BOOLEAN g_IsKdModuleLoaded
shows whether the kernel debugger (KD) module is loaded or not
Definition globals.h:22
UINT32 KernelStatus
Definition RequestStructures.h:40

◆ 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

◆ HyperDbgInterpreter()

INT HyperDbgInterpreter ( CHAR * Command)

Interpret commands.

Parameters
CommandThe text of command
Returns
INT returns return zero if it was successful or non-zero if there was error
801{
802 BOOLEAN HelpCommand = FALSE;
803 UINT64 CommandAttributes = NULL;
804 CommandType::iterator Iterator;
805 CommandParser Parser;
806
807 //
808 // Check if it's the first command and whether the mapping of command is
809 // initialized or not
810 //
812 {
813 //
814 // Initialize the debugger
815 //
817
819 }
820
821 //
822 // Save the command into log open file
823 //
825 {
826 LogopenSaveToFile(Command);
827 LogopenSaveToFile("\n");
828 }
829
830 //
831 // Convert to string
832 //
833 string CommandString(Command);
834
835 //
836 // Tokenize the command string
837 //
838 auto Tokens = Parser.Parse(CommandString);
839
840 //
841 // Print the tokens
842 //
843 // Parser.PrintTokens(Tokens);
844
845 //
846 // Check if user entered an empty input
847 //
848 if (Tokens.empty())
849 {
850 ShowMessages("\n");
851 return 0;
852 }
853
854 //
855 // Get the first command (lower case)
856 //
857 string FirstCommand = GetLowerStringFromCommandToken(Tokens.front());
858
859 //
860 // Read the command's attributes
861 //
862 CommandAttributes = GetCommandAttributes(FirstCommand);
863
864 //
865 // Check if the command needs to be continued by pressing enter
866 //
867 if (CommandAttributes & DEBUGGER_COMMAND_ATTRIBUTE_REPEAT_ON_ENTER)
868 {
870 }
871 else
872 {
874 }
875
876 //
877 // Check and send remote command and also we check whether this
878 // is a command that should be handled in this command or we can
879 // send it to the remote computer, it is because in a remote connection
880 // still some of the commands should be handled in the local HyperDbg
881 //
884 {
885 //
886 // Check it here because generally, we use this variable in host
887 // for showing the correct signature but we won't try to block
888 // other commands, the only thing is events which is blocked
889 // by the remote computer itself
890 //
892 {
894 }
895
896 //
897 // It's a connection over network (VMI-Mode)
898 //
899 RemoteConnectionSendCommand(Command, (UINT32)strlen(Command) + 1);
900
901 ShowMessages("\n");
902
903 //
904 // Indicate that we sent the command to the target system
905 //
906 return 2;
907 }
909 !(CommandAttributes &
911 {
912 //
913 // It's a connection over serial (Debugger-Mode)
914 //
915
917 {
918 KdSendUserInputPacketToDebuggee(Command, (UINT32)strlen(Command) + 1, TRUE);
919
920 //
921 // Set the debuggee to show that it's running
922 //
924 }
925 else
926 {
927 //
928 // Disable the breakpoints and events while executing the command in the remote computer
929 //
931 KdSendUserInputPacketToDebuggee(Command, (UINT32)strlen(Command) + 1, FALSE);
933 }
934
935 //
936 // Indicate that we sent the command to the target system
937 //
938 return 2;
939 }
940
941 //
942 // Detect whether it's a .help command or not
943 //
944 if (!FirstCommand.compare(".help") || !FirstCommand.compare("help") ||
945 !FirstCommand.compare(".hh") || !FirstCommand.compare("!help"))
946 {
947 if (Tokens.size() == 2)
948 {
949 //
950 // Show that it's a help command
951 //
952 HelpCommand = TRUE;
953 FirstCommand = GetLowerStringFromCommandToken(Tokens.at(1));
954 }
955 else
956 {
957 ShowMessages("incorrect use of the '%s'\n\n",
960 return 0;
961 }
962 }
963
964 //
965 // Start parsing commands
966 //
967 string CaseSensitiveCommandString(Command);
968 Iterator = g_CommandsList.find(FirstCommand);
969
970 if (Iterator == g_CommandsList.end())
971 {
972 //
973 // Command doesn't exist
974 //
975 if (!HelpCommand)
976 {
977 ShowMessages("err, couldn't resolve command at '%s'\n",
979 }
980 else
981 {
982 ShowMessages("err, couldn't find the help for the command at '%s'\n",
984 }
985 }
986 else
987 {
988 if (HelpCommand)
989 {
990 Iterator->second.CommandHelpFunction();
991 }
992 else
993 {
994 //
995 // Call the parser with tokens
996 //
997 Iterator->second.CommandFunctionNewParser(Tokens, CaseSensitiveCommandString);
998 }
999 }
1000
1001 //
1002 // Save the command into log open file
1003 //
1005 {
1006 LogopenSaveToFile("\n");
1007 }
1008
1009 return 0;
1010}
BOOLEAN g_IsSerialConnectedToRemoteDebuggee
Shows if the debugger was connected to remote debuggee over (A remote guest).
Definition globals.h:253
UCHAR BOOLEAN
Definition BasicTypes.h:35
#define FALSE
Definition BasicTypes.h:113
unsigned int UINT32
Definition BasicTypes.h:54
@ TEST_BREAKPOINT_TURN_ON_BPS_AND_EVENTS_FOR_COMMANDS_IN_REMOTE_COMPUTER
Definition RequestStructures.h:343
@ TEST_BREAKPOINT_TURN_OFF_BPS_AND_EVENTS_FOR_COMMANDS_IN_REMOTE_COMPUTER
Definition RequestStructures.h:342
Definition interpreter.cpp:36
std::vector< CommandToken > Parse(const std::string &ConstInput)
Parse the input string (commands).
Definition interpreter.cpp:44
#define DEBUGGER_COMMAND_ATTRIBUTE_WONT_STOP_DEBUGGER_AGAIN
Definition commands.h:217
#define DEBUGGER_COMMAND_ATTRIBUTE_LOCAL_COMMAND_IN_DEBUGGER_MODE
Different attributes of commands.
Definition commands.h:213
#define DEBUGGER_COMMAND_ATTRIBUTE_REPEAT_ON_ENTER
Definition commands.h:216
#define DEBUGGER_COMMAND_ATTRIBUTE_LOCAL_COMMAND_IN_REMOTE_CONNECTION
Definition commands.h:215
std::string GetCaseSensitiveStringFromCommandToken(CommandToken TargetToken)
Get case sensitive string from command token.
Definition common.cpp:467
std::string GetLowerStringFromCommandToken(CommandToken TargetToken)
Get lower case string from command token.
Definition common.cpp:484
BOOLEAN g_IsConnectedToRemoteDebuggee
Shows whether the current debugger is the host and connected to a remote debuggee (guest).
Definition globals.h:96
VOID CommandHelpHelp()
help of the help command :)
Definition help.cpp:22
BOOLEAN g_ShouldPreviousCommandBeContinued
Shows whether the previous command should be continued or not.
Definition globals.h:342
BOOLEAN g_IsCommandListInitialized
Is list of command initialized.
Definition globals.h:366
UINT64 GetCommandAttributes(const string &FirstCommand)
Get Command Attributes.
Definition interpreter.cpp:1255
VOID InitializeDebugger()
Initialize the debugger and adjust commands for the first run.
Definition interpreter.cpp:1288
CommandType g_CommandsList
List of command and attributes.
Definition globals.h:348
BOOLEAN KdSendUserInputPacketToDebuggee(const CHAR *Sendbuf, INT Len, BOOLEAN IgnoreBreakingAgain)
Sends user input packet to the debuggee.
Definition kd.cpp:1295
BOOLEAN KdSendTestQueryPacketToDebuggee(DEBUGGER_TEST_QUERY_STATE Type)
Send a test query request to the debuggee.
Definition kd.cpp:425
VOID KdSetStatusAndWaitForPause()
Set the status of the debuggee to wait for the pause.
Definition kd.cpp:2072
VOID LogopenSaveToFile(const CHAR *Text)
Append text to the file object.
Definition logopen.cpp:110
BOOLEAN g_LogOpened
Shows whether the '.logopen' command is executed and the log file is open or not.
Definition globals.h:488
list Tokens
Definition generator.py:217
NULL()
Definition test-case-generator.py:530
BOOLEAN g_BreakPrintingOutput
Shows whether the pause command or CTRL+C or CTRL+Break is executed or not.
Definition globals.h:509
INT RemoteConnectionSendCommand(const CHAR *sendbuf, INT len)
send the command as a client (debugger, host) to the server (debuggee, guest)
Definition remote-connection.cpp:445
BOOLEAN g_ExecutingScript
Shows whether the target is executing a script form '.script' command or executing script by an argum...
Definition globals.h:502

◆ 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

◆ HyperDbgScriptReadFileAndExecuteCommandline()

INT HyperDbgScriptReadFileAndExecuteCommandline ( INT argc,
CHAR * argv[] )

Parsing the command line options for scripts.

Parameters
argc
argv
Returns
INT
226{
227 vector<string> Args;
228
229 //
230 // Convert it to the array
231 //
232 for (SIZE_T i = 2; i < argc; i++)
233 {
234 std::string TempStr(argv[i]);
235 Args.push_back(TempStr);
236 }
237
238 //
239 // Check if the target path and args for script is not empty
240 //
241 if (!Args.empty())
242 {
244 printf("\n");
245 }
246 else
247 {
248 printf("err, invalid command line options passed to the HyperDbg!\n");
249 return FALSE;
250 }
251
252 return TRUE;
253}
printf("ho")
VOID HyperDbgScriptReadFileAndExecuteCommand(std::vector< std::string > &PathAndArgs)
Read file and run the script.
Definition script.cpp:108
char ** argv
Definition symbol-parser.h:47

◆ HyperDbgShowSignature()

VOID HyperDbgShowSignature ( )

Show signature of HyperDbg.

Returns
VOID
1087{
1089 {
1090 //
1091 // Remote debugging over tcp (vmi-mode)
1092 //
1093 ShowMessages("[%s:%s] HyperDbg> ", g_ServerIp.c_str(), g_ServerPort.c_str());
1094 }
1095 else if (g_ActiveProcessDebuggingState.IsActive)
1096 {
1097 //
1098 // Debugging a special process
1099 //
1100 ShowMessages("%x:%x %s u%sHyperDbg> ",
1103 g_ActiveProcessDebuggingState.IsPaused ? "(paused)" : "(running)",
1104 g_ActiveProcessDebuggingState.Is32Bit ? "86" : "64");
1105 }
1107 {
1108 //
1109 // Remote debugging over serial (debugger-mode)
1110 //
1111 ShowMessages("%x: kHyperDbg> ", g_CurrentRemoteCore);
1112 }
1113 else
1114 {
1115 //
1116 // Anything other than above scenarios including local debugging
1117 // in vmi-mode
1118 //
1119 ShowMessages("HyperDbg> ");
1120 }
1121}
ACTIVE_DEBUGGING_PROCESS g_ActiveProcessDebuggingState
State of active debugging thread.
Definition globals.h:372
string g_ServerPort
In debugger (not debuggee), we save the port of server debuggee in this variable to use it later e....
Definition globals.h:132
string g_ServerIp
In debugger (not debuggee), we save the port of server debuggee in this variable to use it later e....
Definition globals.h:139
ULONG g_CurrentRemoteCore
Current core that the debuggee is debugging.
Definition globals.h:285

◆ 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}

◆ 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

◆ HyperDbgTestCommandParser()

BOOLEAN HyperDbgTestCommandParser ( CHAR * Command,
UINT32 NumberOfTokens,
CHAR ** TokensList,
UINT32 * FailedTokenNum,
UINT32 * FailedTokenPosition )

Parse the command (used for testing purposes).

Parameters
CommandThe text of command
NumberOfTokensThe number of tokens
TokensListThe tokens list
FailedTokenNumThe failed token number (if any)
FailedTokenPositionThe failed token position (if any)
Returns
BOOLEAN returns TRUE if the command was parsed successfully and FALSE if there was an error
717{
718 CommandParser Parser;
719
720 //
721 // Convert to string
722 //
723 string CommandString(Command);
724
725 //
726 // Tokenize the command string
727 //
728 std::vector<CommandToken> Tokens = Parser.Parse(CommandString);
729
730 //
731 // Check if the number of tokens is correct
732 //
733 if (Tokens.size() != NumberOfTokens)
734 {
735 ShowMessages("err, the number of tokens is not correct\n");
736 return FALSE;
737 }
738
739 //
740 // Check if the tokens are correct
741 //
742 for (UINT32 i = 0; i < Tokens.size(); i++)
743 {
744 auto Token = Tokens.at(i);
745
746 auto CaseSensitiveText = std::get<1>(Token);
747
748 if (strcmp(CaseSensitiveText.c_str(), TokensList[i]) != 0)
749 {
750 //
751 // Set the failed token number
752 //
753 *FailedTokenNum = i;
754 *FailedTokenPosition = FindDifferencePosition(CaseSensitiveText.c_str(), TokensList[i]);
755
756 ShowMessages("err, the token is not correct\n");
757 return FALSE;
758 }
759 }
760
761 //
762 // Everything is correct
763 //
764 return TRUE;
765}
INT FindDifferencePosition(const CHAR *prsTok, const CHAR *fileTok)
Find the position of the first difference between two strings.
Definition interpreter.cpp:670

◆ HyperDbgTestCommandParserShowTokens()

VOID HyperDbgTestCommandParserShowTokens ( CHAR * Command)

Parse the command and show tokens (used for testing purposes).

Parameters
CommandThe text of command
Returns
VOID
776{
777 CommandParser Parser;
778
779 //
780 // Convert to string
781 //
782 string CommandString(Command);
783
784 //
785 // Tokenize the command string
786 //
787 std::vector<CommandToken> Tokens = Parser.Parse(CommandString);
788
789 Parser.PrintTokens(Tokens);
790}
VOID PrintTokens(const std::vector< CommandToken > &Tokens)
Function to print the elements of a vector of Tokens.
Definition interpreter.cpp:496

◆ 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 g_IsMessageLoggingWindowClosed
Shows whether the message logging window is closed or not.
Definition globals.h:472
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