diff --git a/hwdbg/src/test/bram/script_buffer.hex.txt b/hwdbg/src/test/bram/script_buffer.hex.txt new file mode 100644 index 00000000..73e93432 --- /dev/null +++ b/hwdbg/src/test/bram/script_buffer.hex.txt @@ -0,0 +1,256 @@ +000000b9 ; +0x0 | Checksum +00000000 ; +0x4 | Checksum +52444247 ; +0x8 | Indicator +48595045 ; +0xc | Indicator +00000004 ; +0x10 | TypeOfThePacket - DEBUGGER_TO_DEBUGGEE_HARDWARE_LEVEL (0x4) +00000002 ; +0x14 | RequestedActionOfThePacket - Value (0x2) +00000007 ; +0x18 | Start of Optional Data +00000006 ; +0x1c +0000000a ; +0x20 +00000003 ; +0x24 +00000000 ; +0x28 +0000000f ; +0x2c +00000000 ; +0x30 +0000000f ; +0x34 +00000000 ; +0x38 +00000006 ; +0x3c +00000018 ; +0x40 +00000003 ; +0x44 +00000001 ; +0x48 +00000000 ; +0x4c +00000000 ; +0x50 +00000004 ; +0x54 +00000000 ; +0x58 +00000000 ; +0x5c +00000000 ; +0x60 +00000000 ; +0x64 +00000000 ; +0x68 +00000000 ; +0x6c +00000000 ; +0x70 +00000000 ; +0x74 +00000000 ; +0x78 +00000000 ; +0x7c +00000000 ; +0x80 +00000000 ; +0x84 +00000000 ; +0x88 +00000000 ; +0x8c +00000000 ; +0x90 +00000000 ; +0x94 +00000000 ; +0x98 +00000000 ; +0x9c +00000000 ; +0xa0 +00000000 ; +0xa4 +00000000 ; +0xa8 +00000000 ; +0xac +00000000 ; +0xb0 +00000000 ; +0xb4 +00000000 ; +0xb8 +00000000 ; +0xbc +00000000 ; +0xc0 +00000000 ; +0xc4 +00000000 ; +0xc8 +00000000 ; +0xcc +00000000 ; +0xd0 +00000000 ; +0xd4 +00000000 ; +0xd8 +00000000 ; +0xdc +00000000 ; +0xe0 +00000000 ; +0xe4 +00000000 ; +0xe8 +00000000 ; +0xec +00000000 ; +0xf0 +00000000 ; +0xf4 +00000000 ; +0xf8 +00000000 ; +0xfc +00000000 ; +0x100 +00000000 ; +0x104 +00000000 ; +0x108 +00000000 ; +0x10c +00000000 ; +0x110 +00000000 ; +0x114 +00000000 ; +0x118 +00000000 ; +0x11c +00000000 ; +0x120 +00000000 ; +0x124 +00000000 ; +0x128 +00000000 ; +0x12c +00000000 ; +0x130 +00000000 ; +0x134 +00000000 ; +0x138 +00000000 ; +0x13c +00000000 ; +0x140 +00000000 ; +0x144 +00000000 ; +0x148 +00000000 ; +0x14c +00000000 ; +0x150 +00000000 ; +0x154 +00000000 ; +0x158 +00000000 ; +0x15c +00000000 ; +0x160 +00000000 ; +0x164 +00000000 ; +0x168 +00000000 ; +0x16c +00000000 ; +0x170 +00000000 ; +0x174 +00000000 ; +0x178 +00000000 ; +0x17c +00000000 ; +0x180 +00000000 ; +0x184 +00000000 ; +0x188 +00000000 ; +0x18c +00000000 ; +0x190 +00000000 ; +0x194 +00000000 ; +0x198 +00000000 ; +0x19c +00000000 ; +0x1a0 +00000000 ; +0x1a4 +00000000 ; +0x1a8 +00000000 ; +0x1ac +00000000 ; +0x1b0 +00000000 ; +0x1b4 +00000000 ; +0x1b8 +00000000 ; +0x1bc +00000000 ; +0x1c0 +00000000 ; +0x1c4 +00000000 ; +0x1c8 +00000000 ; +0x1cc +00000000 ; +0x1d0 +00000000 ; +0x1d4 +00000000 ; +0x1d8 +00000000 ; +0x1dc +00000000 ; +0x1e0 +00000000 ; +0x1e4 +00000000 ; +0x1e8 +00000000 ; +0x1ec +00000000 ; +0x1f0 +00000000 ; +0x1f4 +00000000 ; +0x1f8 +00000000 ; +0x1fc +00000000 ; +0x200 +00000000 ; +0x204 +00000000 ; +0x208 +00000000 ; +0x20c +00000000 ; +0x210 +00000000 ; +0x214 +00000000 ; +0x218 +00000000 ; +0x21c +00000000 ; +0x220 +00000000 ; +0x224 +00000000 ; +0x228 +00000000 ; +0x22c +00000000 ; +0x230 +00000000 ; +0x234 +00000000 ; +0x238 +00000000 ; +0x23c +00000000 ; +0x240 +00000000 ; +0x244 +00000000 ; +0x248 +00000000 ; +0x24c +00000000 ; +0x250 +00000000 ; +0x254 +00000000 ; +0x258 +00000000 ; +0x25c +00000000 ; +0x260 +00000000 ; +0x264 +00000000 ; +0x268 +00000000 ; +0x26c +00000000 ; +0x270 +00000000 ; +0x274 +00000000 ; +0x278 +00000000 ; +0x27c +00000000 ; +0x280 +00000000 ; +0x284 +00000000 ; +0x288 +00000000 ; +0x28c +00000000 ; +0x290 +00000000 ; +0x294 +00000000 ; +0x298 +00000000 ; +0x29c +00000000 ; +0x2a0 +00000000 ; +0x2a4 +00000000 ; +0x2a8 +00000000 ; +0x2ac +00000000 ; +0x2b0 +00000000 ; +0x2b4 +00000000 ; +0x2b8 +00000000 ; +0x2bc +00000000 ; +0x2c0 +00000000 ; +0x2c4 +00000000 ; +0x2c8 +00000000 ; +0x2cc +00000000 ; +0x2d0 +00000000 ; +0x2d4 +00000000 ; +0x2d8 +00000000 ; +0x2dc +00000000 ; +0x2e0 +00000000 ; +0x2e4 +00000000 ; +0x2e8 +00000000 ; +0x2ec +00000000 ; +0x2f0 +00000000 ; +0x2f4 +00000000 ; +0x2f8 +00000000 ; +0x2fc +00000000 ; +0x300 +00000000 ; +0x304 +00000000 ; +0x308 +00000000 ; +0x30c +00000000 ; +0x310 +00000000 ; +0x314 +00000000 ; +0x318 +00000000 ; +0x31c +00000000 ; +0x320 +00000000 ; +0x324 +00000000 ; +0x328 +00000000 ; +0x32c +00000000 ; +0x330 +00000000 ; +0x334 +00000000 ; +0x338 +00000000 ; +0x33c +00000000 ; +0x340 +00000000 ; +0x344 +00000000 ; +0x348 +00000000 ; +0x34c +00000000 ; +0x350 +00000000 ; +0x354 +00000000 ; +0x358 +00000000 ; +0x35c +00000000 ; +0x360 +00000000 ; +0x364 +00000000 ; +0x368 +00000000 ; +0x36c +00000000 ; +0x370 +00000000 ; +0x374 +00000000 ; +0x378 +00000000 ; +0x37c +00000000 ; +0x380 +00000000 ; +0x384 +00000000 ; +0x388 +00000000 ; +0x38c +00000000 ; +0x390 +00000000 ; +0x394 +00000000 ; +0x398 +00000000 ; +0x39c +00000000 ; +0x3a0 +00000000 ; +0x3a4 +00000000 ; +0x3a8 +00000000 ; +0x3ac +00000000 ; +0x3b0 +00000000 ; +0x3b4 +00000000 ; +0x3b8 +00000000 ; +0x3bc +00000000 ; +0x3c0 +00000000 ; +0x3c4 +00000000 ; +0x3c8 +00000000 ; +0x3cc +00000000 ; +0x3d0 +00000000 ; +0x3d4 +00000000 ; +0x3d8 +00000000 ; +0x3dc +00000000 ; +0x3e0 +00000000 ; +0x3e4 +00000000 ; +0x3e8 +00000000 ; +0x3ec +00000000 ; +0x3f0 +00000000 ; +0x3f4 +00000000 ; +0x3f8 +00000000 ; +0x3fc \ No newline at end of file diff --git a/hyperdbg/hyperdbg-test/code/hardware/hwdbg-tests.cpp b/hyperdbg/hyperdbg-test/code/hardware/hwdbg-tests.cpp index fb251ab0..46647903 100644 --- a/hyperdbg/hyperdbg-test/code/hardware/hwdbg-tests.cpp +++ b/hyperdbg/hyperdbg-test/code/hardware/hwdbg-tests.cpp @@ -134,7 +134,12 @@ ReadDirectoryAndCreateHwdbgTestCases(const char * HwdbgScriptTestCasesPath) // // Concatenate the new content (prepend the original content) // - std::string newContent = "; The raw script file is available at: " HWDBG_SCRIPT_TEST_CASE_COMPILED_SCRIPTS_DIRECTORY "\\" + entry.path().filename().string() + "\n;\n; !hw script " + commentedContent + "\n" + compiledContent; + std::string newContent = "; The raw script file is available at: " HWDBG_SCRIPT_TEST_CASE_COMPILED_SCRIPTS_DIRECTORY "\\" + + entry.path().filename().string() + + "\n;\n; !hw script " + + commentedContent + + "\n" + + compiledContent; // // Write the new content back to the file (overwriting it) diff --git a/hyperdbg/include/Definition.h b/hyperdbg/include/Definition.h index 6720dc09..f9089a6d 100644 --- a/hyperdbg/include/Definition.h +++ b/hyperdbg/include/Definition.h @@ -59,6 +59,11 @@ */ #define TEST_CASE_MAXIMUM_BUFFERS_TO_COMMUNICATE 0x1000 +/** + * @brief Maximum hwdbg testing pins count (for emulating the script behavior) + */ +#define MAX_HWDBG_TESTING_PIN_COUNT 500 + /** * @brief Test cases file name for command parser */ diff --git a/hyperdbg/include/SDK/imports/user/HyperDbgLibImports.h b/hyperdbg/include/SDK/imports/user/HyperDbgLibImports.h index 31c0e460..d5206724 100644 --- a/hyperdbg/include/SDK/imports/user/HyperDbgLibImports.h +++ b/hyperdbg/include/SDK/imports/user/HyperDbgLibImports.h @@ -252,6 +252,9 @@ hwdbg_script_run_script(const CHAR * script, const CHAR * hardware_script_file_path_to_save, UINT32 initial_bram_buffer_size); +VOID +hwdbg_script_engine_wrapper_test_parser(const CHAR * Expr); + #ifdef __cplusplus } #endif diff --git a/hyperdbg/libhyperdbg/code/debugger/commands/hwdbg-commands/hw.cpp b/hyperdbg/libhyperdbg/code/debugger/commands/hwdbg-commands/hw.cpp index b97545e6..7d06c0d9 100644 --- a/hyperdbg/libhyperdbg/code/debugger/commands/hwdbg-commands/hw.cpp +++ b/hyperdbg/libhyperdbg/code/debugger/commands/hwdbg-commands/hw.cpp @@ -1,10 +1,10 @@ /** - * @file hw_clk.cpp + * @file hw.cpp * @author Sina Karvandi (sina@hyperdbg.org) - * @brief !hw_clk command + * @brief !hw command * @details - * @version 0.9 - * @date 2024-05-29 + * @version 0.11 + * @date 2024-09-29 * * @copyright This project is released under the GNU Public License v3. * @@ -15,6 +15,7 @@ // Global Variables // extern HWDBG_INSTANCE_INFORMATION g_HwdbgInstanceInfo; +extern BOOLEAN g_HwdbgInstanceInfoIsValid; /** * @brief help of the !hw command @@ -27,9 +28,11 @@ CommandHwHelp() ShowMessages("!hw : runs a hardware script in the target device.\n\n"); ShowMessages("syntax : \t!hw script [script { Script (string) }]\n"); + ShowMessages("syntax : \t!hw script [unload]\n"); ShowMessages("\n"); ShowMessages("\t\te.g : !hw script { @hw_pin1 = 0; }\n"); + ShowMessages("\t\te.g : !hw unload\n"); } /** @@ -53,6 +56,21 @@ CommandHw(vector CommandTokens, string Command) HWDBG_TEST_WRITE_SCRIPT_BUFFER_PATH, DEFAULT_INITIAL_BRAM_BUFFER_SIZE); } + else if (CommandTokens.size() >= 2 && + (CompareLowerCaseStrings(CommandTokens.at(1), "eval") || CompareLowerCaseStrings(CommandTokens.at(1), "evaluation"))) + { + // + // Perform test evaluation + // + ScriptEngineWrapperTestParserForHwdbg(GetCaseSensitiveStringFromCommandToken(CommandTokens.at(2))); + } + else if (CommandTokens.size() == 2 && CompareLowerCaseStrings(CommandTokens.at(1), "unload")) + { + // + // Unload the script + // + g_HwdbgInstanceInfoIsValid = FALSE; + } else { ShowMessages("incorrect use of the '%s'\n\n", diff --git a/hyperdbg/libhyperdbg/code/debugger/script-engine/script-engine-wrapper.cpp b/hyperdbg/libhyperdbg/code/debugger/script-engine/script-engine-wrapper.cpp index 937721f5..fb80d854 100644 --- a/hyperdbg/libhyperdbg/code/debugger/script-engine/script-engine-wrapper.cpp +++ b/hyperdbg/libhyperdbg/code/debugger/script-engine/script-engine-wrapper.cpp @@ -22,6 +22,8 @@ extern UINT64 * g_ScriptGlobalVariables; extern UINT64 * g_ScriptStackBuffer; extern UINT64 g_CurrentExprEvalResult; extern BOOLEAN g_CurrentExprEvalResultHasError; +extern UINT64 * g_HwdbgPinsStatus; +extern BOOLEAN g_HwdbgInstanceInfoIsValid; // // Temporary structures used only for testing @@ -711,6 +713,32 @@ ScriptEngineWrapperTestParser(const string & Expr) free(AllocationsForCastings.Buff6); } +/** + * @brief test parser for hwdbg + * @param Expr + * + * @return VOID + */ +VOID +ScriptEngineWrapperTestParserForHwdbg(const string & Expr) +{ + if (!g_HwdbgPinsStatus) + { + g_HwdbgPinsStatus = (UINT64 *)malloc(MAX_HWDBG_TESTING_PIN_COUNT * sizeof(UINT64)); + + if (g_HwdbgPinsStatus == NULL) + { + ShowMessages("err, could not allocate memory for hwdbg pins status"); + + return; + } + + RtlZeroMemory(g_HwdbgPinsStatus, MAX_HWDBG_TESTING_PIN_COUNT * sizeof(UINT64)); + } + + ScriptEngineEvalWrapper((PGUEST_REGS)g_HwdbgPinsStatus, Expr); +} + /** * @brief In the local debugging (VMI mode) environment, this function computes the expressions * @details for example, if the user u ExAllocatePoolWithTag+0x10 this will evaluate the expr diff --git a/hyperdbg/libhyperdbg/code/export/export.cpp b/hyperdbg/libhyperdbg/code/export/export.cpp index b0f40c11..9c8590bb 100644 --- a/hyperdbg/libhyperdbg/code/export/export.cpp +++ b/hyperdbg/libhyperdbg/code/export/export.cpp @@ -695,13 +695,27 @@ hyperdbg_u_stepping_step_over_for_gu(BOOLEAN last_instruction) * @return BOOLEAN */ BOOLEAN -hwdbg_script_run_script(const CHAR * script, +hwdbg_script_run_script(const CHAR * script, const CHAR * instance_filepath_to_read, const CHAR * hardware_script_file_path_to_save, - UINT32 initial_bram_buffer_size) + UINT32 initial_bram_buffer_size) { return HwdbgScriptRunScript(script, instance_filepath_to_read, hardware_script_file_path_to_save, initial_bram_buffer_size); } + +/** + * @brief Run (test evaluation) hwdbg script + * + * @param Expr + * + * @return VOID + */ +VOID +hwdbg_script_engine_wrapper_test_parser(const char * Expr) +{ + std::string StrExpr = Expr; // Convert const char* to std::string + ScriptEngineWrapperTestParserForHwdbg(StrExpr); +} diff --git a/hyperdbg/libhyperdbg/header/globals.h b/hyperdbg/libhyperdbg/header/globals.h index 9694fbe9..02370094 100644 --- a/hyperdbg/libhyperdbg/header/globals.h +++ b/hyperdbg/libhyperdbg/header/globals.h @@ -685,3 +685,9 @@ BOOLEAN g_HwdbgInstanceInfoIsValid; * */ std::vector g_HwdbgPortConfiguration; + +/** + * @brief Holder of pins (and ports) status of hwdbg + * + */ +UINT64 * g_HwdbgPinsStatus; diff --git a/hyperdbg/libhyperdbg/header/script-engine.h b/hyperdbg/libhyperdbg/header/script-engine.h index 5f416e48..2c0ce3f7 100644 --- a/hyperdbg/libhyperdbg/header/script-engine.h +++ b/hyperdbg/libhyperdbg/header/script-engine.h @@ -75,6 +75,9 @@ ScriptEngineSymbolAbortLoadingWrapper(); VOID ScriptEngineWrapperTestParser(const string & Expr); +VOID +ScriptEngineWrapperTestParserForHwdbg(const string & Expr); + BOOLEAN ScriptAutomaticStatementsTestWrapper(const string & Expr, UINT64 ExpectationValue, BOOLEAN ExceptError); diff --git a/hyperdbg/script-eval/code/Regs.c b/hyperdbg/script-eval/code/Regs.c index 8e008cf2..7734e2cb 100644 --- a/hyperdbg/script-eval/code/Regs.c +++ b/hyperdbg/script-eval/code/Regs.c @@ -12,6 +12,23 @@ */ #include "pch.h" +#if defined(SCRIPT_ENGINE_USER_MODE) && defined(HYPERDBG_LIBHYPERDBG) +extern BOOLEAN g_HwdbgInstanceInfoIsValid; +#endif // defined(SCRIPT_ENGINE_USER_MODE) && defined(HYPERDBG_LIBHYPERDBG) + +/** + * @brief Get the register value for hardware debugging + * + * @param Regs + * @param RegId + * @return UINT64 + */ +UINT64 +GetRegValueHwdbg(UINT64 * Regs, UINT32 RegId) +{ + return Regs[RegId]; +} + /** * @brief Get the register value * @@ -22,6 +39,13 @@ UINT64 GetRegValue(PGUEST_REGS GuestRegs, REGS_ENUM RegId) { +#if defined(SCRIPT_ENGINE_USER_MODE) && defined(HYPERDBG_LIBHYPERDBG) + if (g_HwdbgInstanceInfoIsValid) + { + return GetRegValueHwdbg((UINT64 *)GuestRegs, RegId); + } +#endif // defined(SCRIPT_ENGINE_USER_MODE) && defined(HYPERDBG_LIBHYPERDBG) + switch (RegId) { case REGISTER_RAX: @@ -950,6 +974,22 @@ GetRegValue(PGUEST_REGS GuestRegs, REGS_ENUM RegId) } } +/** + * @brief Set the register value for hardware debugging + * + * @param Regs + * @param RegisterId + * @param Value + * + * @return BOOLEAN + */ +BOOLEAN +SetRegValueHwdbg(UINT64 * Regs, UINT32 RegisterId, UINT64 Value) +{ + Regs[RegisterId] = Value; + return TRUE; +} + /** * @brief Set the register value * @@ -1987,10 +2027,19 @@ SetRegValue(PGUEST_REGS GuestRegs, UINT32 RegisterId, UINT64 Value) * @param GuestRegs * @param Symbol * @param Value + * @param ActionBuffer + * * @return BOOLEAN */ BOOLEAN SetRegValueUsingSymbol(PGUEST_REGS GuestRegs, PSYMBOL Symbol, UINT64 Value) { +#if defined(SCRIPT_ENGINE_USER_MODE) && defined(HYPERDBG_LIBHYPERDBG) + if (g_HwdbgInstanceInfoIsValid == FALSE) + return SetRegValue(GuestRegs, (UINT32)Symbol->Value, Value); + else + return SetRegValueHwdbg((UINT64 *)GuestRegs, (UINT32)Symbol->Value, Value); +#else return SetRegValue(GuestRegs, (UINT32)Symbol->Value, Value); +#endif // defined(SCRIPT_ENGINE_USER_MODE) && defined(HYPERDBG_LIBHYPERDBG) } diff --git a/hyperdbg/script-eval/header/ScriptEngineHeader.h b/hyperdbg/script-eval/header/ScriptEngineHeader.h index 04a1b60f..f5bcf7e6 100644 --- a/hyperdbg/script-eval/header/ScriptEngineHeader.h +++ b/hyperdbg/script-eval/header/ScriptEngineHeader.h @@ -21,6 +21,12 @@ SetRegValue(PGUEST_REGS GuestRegs, UINT32 RegisterId, UINT64 Value); UINT64 GetRegValue(PGUEST_REGS GuestRegs, REGS_ENUM RegId); +BOOLEAN +SetRegValueHwdbg(UINT64 * Regs, UINT32 RegisterId, UINT64 Value); + +UINT64 +GetRegValueHwdbg(UINT64 * Regs, UINT32 RegId); + ////////////////////////////////////////////////// // Functions // //////////////////////////////////////////////////