From 9b4fbaeb61511ce0473673748771a6be68df2861 Mon Sep 17 00:00:00 2001 From: SinaKarvandi Date: Mon, 24 Jun 2024 19:07:06 +0900 Subject: [PATCH] change libhyperdbg SDK functions --- hyperdbg/hyperdbg-cli/hyperdbg-cli.cpp | 41 +++++---- .../hyperdbg_app/code/hyperdbg-app.cpp | 4 +- .../include/SDK/Imports/HyperDbgLibImports.h | 66 ++++++++------ hyperdbg/libhyperdbg/code/app/libhyperdbg.cpp | 26 +++--- hyperdbg/libhyperdbg/code/common/common.cpp | 4 +- .../commands/debugging-commands/cpu.cpp | 2 +- .../commands/meta-commands/script.cpp | 8 +- .../code/debugger/core/interpreter.cpp | 28 +++--- hyperdbg/libhyperdbg/code/export/export.cpp | 90 +++++++++++++++++++ hyperdbg/libhyperdbg/header/commands.h | 9 ++ hyperdbg/libhyperdbg/header/common.h | 3 + hyperdbg/libhyperdbg/header/export.h | 20 +++++ hyperdbg/libhyperdbg/header/libhyperdbg.h | 43 +++++++++ hyperdbg/libhyperdbg/libhyperdbg.vcxproj | 3 + .../libhyperdbg/libhyperdbg.vcxproj.filters | 12 +++ hyperdbg/libhyperdbg/pch.h | 3 +- 16 files changed, 276 insertions(+), 86 deletions(-) create mode 100644 hyperdbg/libhyperdbg/code/export/export.cpp create mode 100644 hyperdbg/libhyperdbg/header/export.h create mode 100644 hyperdbg/libhyperdbg/header/libhyperdbg.h diff --git a/hyperdbg/hyperdbg-cli/hyperdbg-cli.cpp b/hyperdbg/hyperdbg-cli/hyperdbg-cli.cpp index b0de284c..42b6264f 100644 --- a/hyperdbg/hyperdbg-cli/hyperdbg-cli.cpp +++ b/hyperdbg/hyperdbg-cli/hyperdbg-cli.cpp @@ -36,9 +36,9 @@ using namespace std; int main(int argc, char * argv[]) { - bool ExitFromDebugger = false; - string PreviousCommand; - bool Reset = false; + BOOLEAN exit_from_debugger = FALSE; + string previous_command; + BOOLEAN reset = FALSE; // // Set console output code page to UTF-8 @@ -59,7 +59,7 @@ main(int argc, char * argv[]) // // Handle the script // - HyperDbgScriptReadFileAndExecuteCommandline(argc, argv); + hyperdbg_u_script_read_file_and_execute_commandline(argc, argv); } else { @@ -68,22 +68,22 @@ main(int argc, char * argv[]) } } - while (!ExitFromDebugger) + while (!exit_from_debugger) { - HyperDbgShowSignature(); + hyperdbg_u_show_signature(); - string CurrentCommand = ""; + string current_command = ""; // // Clear multiline // - Reset = true; + reset = TRUE; GetMultiLinecCommand: - string TempCommand = ""; + string temp_command = ""; - getline(cin, TempCommand); + getline(cin, temp_command); if (cin.fail() || cin.eof()) { @@ -100,17 +100,17 @@ main(int argc, char * argv[]) // // Check for multi-line commands // - if (HyperDbgCheckMultilineCommand((char *)TempCommand.c_str(), Reset) == true) + if (hyperdbg_u_check_multiline_command((CHAR *)temp_command.c_str(), reset) == TRUE) { // // It's a multi-line command // - Reset = false; + reset = FALSE; // // Save the command with a space separator // - CurrentCommand += TempCommand + "\n"; + current_command += temp_command + "\n"; // // Show a small signature @@ -127,32 +127,31 @@ main(int argc, char * argv[]) // // Reset for future commands // - Reset = true; + reset = TRUE; // // Either the multi-line is finished or it's a // single line command // - CurrentCommand += TempCommand; + current_command += temp_command; } - if (!CurrentCommand.compare("") && - HyperDbgContinuePreviousCommand()) + if (!current_command.compare("") && hyperdbg_u_continue_previous_command()) { // // Retry the previous command // - CurrentCommand = PreviousCommand; + current_command = previous_command; } else { // // Save previous command // - PreviousCommand = CurrentCommand; + previous_command = current_command; } - int CommandExecutionResult = HyperDbgInterpreter((char *)CurrentCommand.c_str()); + INT CommandExecutionResult = hyperdbg_u_interpreter((CHAR *)current_command.c_str()); // // if the debugger encounters an exit state then the return will be 1 @@ -162,7 +161,7 @@ main(int argc, char * argv[]) // // Exit from the debugger // - ExitFromDebugger = true; + exit_from_debugger = true; } if (CommandExecutionResult != 2) { diff --git a/hyperdbg/include/SDK/Examples/hyperdbg_app/code/hyperdbg-app.cpp b/hyperdbg/include/SDK/Examples/hyperdbg_app/code/hyperdbg-app.cpp index e4ea8d59..66feb232 100644 --- a/hyperdbg/include/SDK/Examples/hyperdbg_app/code/hyperdbg-app.cpp +++ b/hyperdbg/include/SDK/Examples/hyperdbg_app/code/hyperdbg-app.cpp @@ -412,7 +412,7 @@ HyperDbgLoadReversingMachine() // // Read the vendor string // - HyperDbgReadVendorString(CpuId); + CpuReadVendorString(CpuId); ShowMessages("current processor vendor is : %s\n", CpuId); @@ -427,7 +427,7 @@ HyperDbgLoadReversingMachine() return 1; } - if (HyperDbgVmxSupportDetection()) + if (VmxSupportDetection()) { ShowMessages("vmx operation is supported by your processor\n"); } diff --git a/hyperdbg/include/SDK/Imports/HyperDbgLibImports.h b/hyperdbg/include/SDK/Imports/HyperDbgLibImports.h index 092a859d..a131ea0e 100644 --- a/hyperdbg/include/SDK/Imports/HyperDbgLibImports.h +++ b/hyperdbg/include/SDK/Imports/HyperDbgLibImports.h @@ -11,9 +11,9 @@ #pragma once #ifdef HYPERDBG_LIBHYPERDBG -# define IMPORT_EXPORT_CTRL __declspec(dllexport) +# define IMPORT_EXPORT_LIBHYPERDBG __declspec(dllexport) #else -# define IMPORT_EXPORT_CTRL __declspec(dllimport) +# define IMPORT_EXPORT_LIBHYPERDBG __declspec(dllimport) #endif // @@ -27,40 +27,50 @@ extern "C" { // // Support Detection // -IMPORT_EXPORT_CTRL bool -HyperDbgVmxSupportDetection(); -IMPORT_EXPORT_CTRL void -HyperDbgReadVendorString(char *); +IMPORT_EXPORT_LIBHYPERDBG BOOLEAN +hyperdbg_u_detect_vmx_support(); + +IMPORT_EXPORT_LIBHYPERDBG VOID +hyperdbg_u_read_vendor_string(CHAR *); // // VMM Module // -IMPORT_EXPORT_CTRL int -HyperDbgLoadVmm(); -IMPORT_EXPORT_CTRL int -HyperDbgUnloadVmm(); -IMPORT_EXPORT_CTRL int -HyperDbgInstallVmmDriver(); -IMPORT_EXPORT_CTRL int -HyperDbgUninstallVmmDriver(); -IMPORT_EXPORT_CTRL int -HyperDbgStopVmmDriver(); +IMPORT_EXPORT_LIBHYPERDBG INT +hyperdbg_u_load_vmm(); + +IMPORT_EXPORT_LIBHYPERDBG INT +hyperdbg_u_unload_vmm(); + +IMPORT_EXPORT_LIBHYPERDBG INT +hyperdbg_u_install_vmm_driver(); + +IMPORT_EXPORT_LIBHYPERDBG INT +hyperdbg_u_uninstall_vmm_driver(); + +IMPORT_EXPORT_LIBHYPERDBG INT +hyperdbg_u_stop_vmm_driver(); // // General imports // -IMPORT_EXPORT_CTRL int -HyperDbgInterpreter(char * Command); -IMPORT_EXPORT_CTRL void -HyperDbgShowSignature(); -IMPORT_EXPORT_CTRL void -HyperDbgSetTextMessageCallback(Callback handler); -IMPORT_EXPORT_CTRL int -HyperDbgScriptReadFileAndExecuteCommandline(int argc, char * argv[]); -IMPORT_EXPORT_CTRL bool -HyperDbgContinuePreviousCommand(); -IMPORT_EXPORT_CTRL bool -HyperDbgCheckMultilineCommand(char * CurrentCommand, bool Reset); +IMPORT_EXPORT_LIBHYPERDBG INT +hyperdbg_u_interpreter(CHAR * command); + +IMPORT_EXPORT_LIBHYPERDBG VOID +hyperdbg_u_show_signature(); + +IMPORT_EXPORT_LIBHYPERDBG VOID +hyperdbg_u_set_text_message_callback(Callback handler); + +IMPORT_EXPORT_LIBHYPERDBG INT +hyperdbg_u_script_read_file_and_execute_commandline(INT argc, CHAR * argv[]); + +IMPORT_EXPORT_LIBHYPERDBG BOOLEAN +hyperdbg_u_continue_previous_command(); + +IMPORT_EXPORT_LIBHYPERDBG BOOLEAN +hyperdbg_u_check_multiline_command(CHAR * current_command, BOOLEAN reset); #ifdef __cplusplus } diff --git a/hyperdbg/libhyperdbg/code/app/libhyperdbg.cpp b/hyperdbg/libhyperdbg/code/app/libhyperdbg.cpp index c60e10b3..9a123ad5 100644 --- a/hyperdbg/libhyperdbg/code/app/libhyperdbg.cpp +++ b/hyperdbg/libhyperdbg/code/app/libhyperdbg.cpp @@ -38,7 +38,7 @@ extern LIST_ENTRY g_OutputSources; * @param handler Function that handles the messages */ VOID -HyperDbgSetTextMessageCallback(Callback handler) +SetTextMessageCallback(Callback handler) { g_MessageHandler = handler; } @@ -437,10 +437,10 @@ IrpBasedBufferThread(void * data) /** * @brief Install VMM driver * - * @return int return zero if it was successful or non-zero if there + * @return INT return zero if it was successful or non-zero if there * was error */ -int +INT HyperDbgInstallVmmDriver() { // @@ -493,10 +493,10 @@ HyperDbgStopDriver(LPCTSTR DriverName) /** * @brief Stop VMM driver * - * @return int return zero if it was successful or non-zero if there + * @return INT return zero if it was successful or non-zero if there * was error */ -int +INT HyperDbgStopVmmDriver() { return HyperDbgStopDriver(KERNEL_DEBUGGER_DRIVER_NAME); @@ -527,10 +527,10 @@ HyperDbgUninstallDriver(LPCTSTR DriverName) /** * @brief Remove the VMM driver * - * @return int return zero if it was successful or non-zero if there + * @return INT return zero if it was successful or non-zero if there * was error */ -int +INT HyperDbgUninstallVmmDriver() { return HyperDbgUninstallDriver(KERNEL_DEBUGGER_DRIVER_NAME); @@ -539,10 +539,10 @@ HyperDbgUninstallVmmDriver() /** * @brief Load the VMM driver * - * @return int return zero if it was successful or non-zero if there + * @return INT return zero if it was successful or non-zero if there * was error */ -int +INT HyperDbgLoadVmm() { char CpuId[13] = {0}; @@ -559,7 +559,7 @@ HyperDbgLoadVmm() // // Read the vendor string // - HyperDbgReadVendorString(CpuId); + CpuReadVendorString(CpuId); ShowMessages("current processor vendor is : %s\n", CpuId); @@ -574,7 +574,7 @@ HyperDbgLoadVmm() return 1; } - if (HyperDbgVmxSupportDetection()) + if (VmxSupportDetection()) { ShowMessages("vmx operation is supported by your processor\n"); } @@ -645,10 +645,10 @@ HyperDbgLoadVmm() /** * @brief Unload VMM driver * - * @return int return zero if it was successful or non-zero if there + * @return INT return zero if it was successful or non-zero if there * was error */ -int +INT HyperDbgUnloadVmm() { BOOL Status; diff --git a/hyperdbg/libhyperdbg/code/common/common.cpp b/hyperdbg/libhyperdbg/code/common/common.cpp index 99bcc440..1b98ca98 100644 --- a/hyperdbg/libhyperdbg/code/common/common.cpp +++ b/hyperdbg/libhyperdbg/code/common/common.cpp @@ -501,8 +501,8 @@ ValidateIP(const string & ip) * @return true if vmx is supported * @return false if vmx is not supported */ -bool -HyperDbgVmxSupportDetection() +BOOLEAN +VmxSupportDetection() { // // Call assembly function diff --git a/hyperdbg/libhyperdbg/code/debugger/commands/debugging-commands/cpu.cpp b/hyperdbg/libhyperdbg/code/debugger/commands/debugging-commands/cpu.cpp index 4e459582..dbd688e6 100644 --- a/hyperdbg/libhyperdbg/code/debugger/commands/debugging-commands/cpu.cpp +++ b/hyperdbg/libhyperdbg/code/debugger/commands/debugging-commands/cpu.cpp @@ -246,7 +246,7 @@ const InstructionSet::InstructionSet_Internal InstructionSet::CPU_Rep; * @return char * */ VOID -HyperDbgReadVendorString(char * Result) +CpuReadVendorString(CHAR * Result) { std::string VendorString = InstructionSet::Vendor(); strcpy(Result, VendorString.c_str()); diff --git a/hyperdbg/libhyperdbg/code/debugger/commands/meta-commands/script.cpp b/hyperdbg/libhyperdbg/code/debugger/commands/meta-commands/script.cpp index 159336f4..b6a39fa6 100644 --- a/hyperdbg/libhyperdbg/code/debugger/commands/meta-commands/script.cpp +++ b/hyperdbg/libhyperdbg/code/debugger/commands/meta-commands/script.cpp @@ -146,7 +146,7 @@ HyperDbgScriptReadFileAndExecuteCommand(std::vector & PathAndArgs) // // Check for multiline commands // - if (HyperDbgCheckMultilineCommand((char *)Line.c_str(), Reset)) + if (CheckMultilineCommand((char *)Line.c_str(), Reset)) { // // if the reset is true, we should make the saving buffer empty @@ -222,10 +222,10 @@ HyperDbgScriptReadFileAndExecuteCommand(std::vector & PathAndArgs) /** * @brief Parsing the command line options for scripts * - * @return int + * @return INT */ -int -HyperDbgScriptReadFileAndExecuteCommandline(int argc, char * argv[]) +INT +ScriptReadFileAndExecuteCommandline(INT argc, CHAR * argv[]) { vector Args; diff --git a/hyperdbg/libhyperdbg/code/debugger/core/interpreter.cpp b/hyperdbg/libhyperdbg/code/debugger/core/interpreter.cpp index f806dc2b..a710f9bd 100644 --- a/hyperdbg/libhyperdbg/code/debugger/core/interpreter.cpp +++ b/hyperdbg/libhyperdbg/code/debugger/core/interpreter.cpp @@ -43,11 +43,11 @@ extern string g_ServerIp; * @brief Interpret commands * * @param Command The text of command - * @return int returns return zero if it was successful or non-zero if there was + * @return INT returns return zero if it was successful or non-zero if there was * error */ -int -HyperDbgInterpreter(char * Command) +INT +HyperDbgInterpreter(CHAR * Command) { BOOLEAN HelpCommand = FALSE; UINT64 CommandAttributes = NULL; @@ -370,11 +370,11 @@ HyperDbgShowSignature() * * @param CurrentCommand * @param Reset - * @return bool return true if the command needs extra input, otherwise - * return false + * @return BOOLEAN return TRUE if the command needs extra input, otherwise + * return FALSE */ -bool -HyperDbgCheckMultilineCommand(char * CurrentCommand, bool Reset) +BOOLEAN +CheckMultilineCommand(CHAR * CurrentCommand, BOOLEAN Reset) { UINT32 CurrentCommandLen = 0; std::string CurrentCommandStr(CurrentCommand); @@ -451,14 +451,14 @@ HyperDbgCheckMultilineCommand(char * CurrentCommand, bool Reset) // either the command is finished or it's a single // line command // - return false; + return FALSE; } else { // // There still other lines, this command is incomplete // - return true; + return TRUE; } } @@ -467,11 +467,11 @@ HyperDbgCheckMultilineCommand(char * CurrentCommand, bool Reset) * need to be repeated when the user press enter, this function shows * whether we should continue the previous command or not * - * @return true means the command should be continued, false means command + * @return TRUE means the command should be continued, FALSE means command * should be ignored */ -bool -HyperDbgContinuePreviousCommand() +BOOLEAN +ContinuePreviousCommand() { BOOLEAN Result = g_ShouldPreviousCommandBeContinued; @@ -482,11 +482,11 @@ HyperDbgContinuePreviousCommand() if (Result) { - return true; + return TRUE; } else { - return false; + return FALSE; } } diff --git a/hyperdbg/libhyperdbg/code/export/export.cpp b/hyperdbg/libhyperdbg/code/export/export.cpp new file mode 100644 index 00000000..5f849bf1 --- /dev/null +++ b/hyperdbg/libhyperdbg/code/export/export.cpp @@ -0,0 +1,90 @@ +/** + * @file export.cpp + * @author Sina Karvandi (sina@hyperdbg.org) + * @brief Exported functions from libhyperdbg interface + * @details + * @version 1.0 + * @date 2024-06-24 + * + * @copyright This project is released under the GNU Public License v3. + * + */ +#include "pch.h" + +BOOLEAN +hyperdbg_u_detect_vmx_support() +{ + return VmxSupportDetection(); +} + +VOID +hyperdbg_u_read_vendor_string(CHAR * vendor_string) +{ + CpuReadVendorString(vendor_string); +} + +INT +hyperdbg_u_load_vmm() +{ + return HyperDbgLoadVmm(); +} + +INT +hyperdbg_u_unload_vmm() +{ + return HyperDbgUnloadVmm(); +} + +INT +hyperdbg_u_install_vmm_driver() +{ + return HyperDbgInstallVmmDriver(); +} + +INT +hyperdbg_u_uninstall_vmm_driver() +{ + return HyperDbgUninstallVmmDriver(); +} + +INT +hyperdbg_u_stop_vmm_driver() +{ + return HyperDbgStopVmmDriver(); +} + +INT +hyperdbg_u_interpreter(CHAR * command) +{ + return HyperDbgInterpreter(command); +} + +VOID +hyperdbg_u_show_signature() +{ + HyperDbgShowSignature(); +} + +VOID +hyperdbg_u_set_text_message_callback(Callback handler) +{ + SetTextMessageCallback(handler); +} + +INT +hyperdbg_u_script_read_file_and_execute_commandline(INT argc, CHAR * argv[]) +{ + return ScriptReadFileAndExecuteCommandline(argc, argv); +} + +BOOLEAN +hyperdbg_u_continue_previous_command() +{ + return ContinuePreviousCommand(); +} + +BOOLEAN +hyperdbg_u_check_multiline_command(CHAR * current_command, BOOLEAN reset) +{ + return CheckMultilineCommand(current_command, reset); +} diff --git a/hyperdbg/libhyperdbg/header/commands.h b/hyperdbg/libhyperdbg/header/commands.h index 714cdae8..1418e798 100644 --- a/hyperdbg/libhyperdbg/header/commands.h +++ b/hyperdbg/libhyperdbg/header/commands.h @@ -37,6 +37,9 @@ CommandSettingsGetValueFromConfigFile(std::string OptionName, std::string & Opti // Functions // ////////////////////////////////////////////////// +VOID +CpuReadVendorString(CHAR * Result); + int ReadCpuDetails(); @@ -125,6 +128,12 @@ InitializeCommandsDictionary(); VOID InitializeDebugger(); +BOOLEAN +CheckMultilineCommand(CHAR * CurrentCommand, BOOLEAN Reset); + +BOOLEAN +ContinuePreviousCommand(); + VOID CommandDumpSaveIntoFile(PVOID Buffer, UINT32 Length); diff --git a/hyperdbg/libhyperdbg/header/common.h b/hyperdbg/libhyperdbg/header/common.h index f807cd59..9670af13 100644 --- a/hyperdbg/libhyperdbg/header/common.h +++ b/hyperdbg/libhyperdbg/header/common.h @@ -201,3 +201,6 @@ Getx86VirtualAddressWidth(); BOOLEAN CheckAccessValidityAndSafety(UINT64 TargetAddress, UINT32 Size); + +BOOLEAN +VmxSupportDetection(); diff --git a/hyperdbg/libhyperdbg/header/export.h b/hyperdbg/libhyperdbg/header/export.h new file mode 100644 index 00000000..f677ae28 --- /dev/null +++ b/hyperdbg/libhyperdbg/header/export.h @@ -0,0 +1,20 @@ +/** + * @file export.h + * @author Sina Karvandi (sina@hyperdbg.org) + * @brief headers for controller of the reversing machine's module + * @details + * @version 1.0 + * @date 2024-06-24 + * + * @copyright This project is released under the GNU Public License v3. + * + */ +#pragma once + +////////////////////////////////////////////////// +// Functions // +////////////////////////////////////////////////// + +// +// Some functions are exported to HyperDbgLibImports.h +// diff --git a/hyperdbg/libhyperdbg/header/libhyperdbg.h b/hyperdbg/libhyperdbg/header/libhyperdbg.h new file mode 100644 index 00000000..db963264 --- /dev/null +++ b/hyperdbg/libhyperdbg/header/libhyperdbg.h @@ -0,0 +1,43 @@ +/** + * @file libhyperdbg.h + * @author Sina Karvandi (sina@hyperdbg.org) + * @brief headers for libhyperdbg + * @details + * @version 1.0 + * @date 2024-06-24 + * + * @copyright This project is released under the GNU Public License v3. + * + */ +#pragma once + +////////////////////////////////////////////////// +// Functions // +////////////////////////////////////////////////// + +INT +HyperDbgLoadVmm(); + +INT +HyperDbgUnloadVmm(); + +INT +HyperDbgInstallVmmDriver(); + +INT +HyperDbgUninstallVmmDriver(); + +INT +HyperDbgStopVmmDriver(); + +INT +HyperDbgInterpreter(CHAR * Command); + +INT +ScriptReadFileAndExecuteCommandline(INT argc, CHAR * argv[]); + +VOID +HyperDbgShowSignature(); + +VOID +SetTextMessageCallback(Callback handler); diff --git a/hyperdbg/libhyperdbg/libhyperdbg.vcxproj b/hyperdbg/libhyperdbg/libhyperdbg.vcxproj index 1c1b72f4..7ea5020f 100644 --- a/hyperdbg/libhyperdbg/libhyperdbg.vcxproj +++ b/hyperdbg/libhyperdbg/libhyperdbg.vcxproj @@ -130,6 +130,7 @@ + @@ -137,6 +138,7 @@ + @@ -189,6 +191,7 @@ + diff --git a/hyperdbg/libhyperdbg/libhyperdbg.vcxproj.filters b/hyperdbg/libhyperdbg/libhyperdbg.vcxproj.filters index 6cbbc61f..b821ad68 100644 --- a/hyperdbg/libhyperdbg/libhyperdbg.vcxproj.filters +++ b/hyperdbg/libhyperdbg/libhyperdbg.vcxproj.filters @@ -81,6 +81,9 @@ {a27149ec-f7ce-4356-998f-79bd2a7e7961} + + {cfacdcfe-8503-4a00-b7e2-75b0e906f75e} + @@ -155,6 +158,12 @@ header\platform + + header + + + header + @@ -529,6 +538,9 @@ code\hwdbg + + code\export + diff --git a/hyperdbg/libhyperdbg/pch.h b/hyperdbg/libhyperdbg/pch.h index 2ca4af07..2f9017b5 100644 --- a/hyperdbg/libhyperdbg/pch.h +++ b/hyperdbg/libhyperdbg/pch.h @@ -123,7 +123,6 @@ typedef const wchar_t *LPCWCHAR, *PCWCHAR; #include "Configuration.h" #include "Definition.h" #include "SDK/HyperDbgSdk.h" -#include "SDK/Imports/HyperDbgLibImports.h" // // Script-engine @@ -140,6 +139,8 @@ typedef const wchar_t *LPCWCHAR, *PCWCHAR; // // General // +#include "header/libhyperdbg.h" +#include "header/export.h" #include "header/inipp.h" #include "header/commands.h" #include "header/common.h"