2020-10-25 10:09:52 -07:00
/**
2021-05-30 20:25:07 +04:30
* @ file script - engine - wrapper . cpp
2022-01-18 22:38:56 +03:30
* @ author M . H . Gholamrezaei ( mh @ hyperdbg . org )
* @ author Sina Karvandi ( sina @ hyperdbg . org )
2020-10-25 10:09:52 -07:00
* @ brief Interpret general fields
* @ details
* @ version 0.1
* @ date 2020 - 10 - 25
*
* @ copyright This project is released under the GNU Public License v3 .
*
*/
2022-05-04 16:38:21 -07:00
# include "pch.h"
2020-10-25 10:09:52 -07:00
2024-07-24 21:25:55 +08:00
// #define _SCRIPT_ENGINE_IR_PRINT_EN
2024-07-19 19:17:46 +08:00
// #define _SCRIPT_ENGINE_CODEEXEC_DBG_EN
2020-10-25 10:09:52 -07:00
//
2021-05-07 16:30:26 +04:30
// Global Variables
2020-10-25 10:09:52 -07:00
//
2021-05-07 16:30:26 +04:30
extern UINT64 * g_ScriptGlobalVariables ;
2024-07-24 21:25:55 +08:00
extern UINT64 * g_ScriptStackBuffer ;
2022-06-28 09:52:24 -07:00
extern UINT64 g_CurrentExprEvalResult ;
extern BOOLEAN g_CurrentExprEvalResultHasError ;
2024-10-02 18:00:34 +02:00
extern UINT64 * g_HwdbgPinsStatus ;
extern BOOLEAN g_HwdbgInstanceInfoIsValid ;
2020-10-25 10:09:52 -07:00
2022-05-08 20:24:31 +04:30
//
// Temporary structures used only for testing
//
typedef struct _ALLOCATED_MEMORY_FOR_SCRIPT_ENGINE_CASTING
{
CHAR * Buff1 ;
CHAR * Buff2 ;
CHAR * Buff3 ;
CHAR * Buff4 ;
CHAR * Buff5 ;
CHAR * Buff6 ;
} ALLOCATED_MEMORY_FOR_SCRIPT_ENGINE_CASTING , * PALLOCATED_MEMORY_FOR_SCRIPT_ENGINE_CASTING ;
2021-05-29 18:52:50 +04:30
//
2021-05-30 20:25:07 +04:30
// *********************** Pdb parse wrapper ***********************
2021-05-29 18:52:50 +04:30
//
2021-05-30 20:25:07 +04:30
/**
* @ brief ScriptEngineConvertNameToAddress wrapper
*
* @ param FunctionName
* @ param WasFound
2022-06-28 07:29:07 -07:00
*
2021-05-30 20:25:07 +04:30
* @ return UINT64
*/
2021-05-29 18:52:50 +04:30
UINT64
2021-06-03 16:43:00 +04:30
ScriptEngineConvertNameToAddressWrapper ( const char * FunctionOrVariableName , PBOOLEAN WasFound )
2021-05-29 18:52:50 +04:30
{
2021-06-03 16:43:00 +04:30
return ScriptEngineConvertNameToAddress ( FunctionOrVariableName , WasFound ) ;
2021-05-30 20:25:07 +04:30
}
/**
* @ brief ScriptEngineLoadFileSymbol wrapper
*
* @ param BaseAddress
* @ param FileName
2022-06-28 07:29:07 -07:00
*
2021-05-31 20:13:18 +04:30
* @ return UINT32
2021-05-30 20:25:07 +04:30
*/
2021-05-31 20:13:18 +04:30
UINT32
2023-07-17 01:13:53 +09:00
ScriptEngineLoadFileSymbolWrapper ( UINT64 BaseAddress , const char * PdbFileName , const char * CustomModuleName )
2021-05-31 20:13:18 +04:30
{
2023-07-17 01:13:53 +09:00
return ScriptEngineLoadFileSymbol ( BaseAddress , PdbFileName , CustomModuleName ) ;
2021-05-31 20:13:18 +04:30
}
2021-09-28 18:17:14 +03:30
/**
* @ brief ScriptEngineSetTextMessageCallback wrapper
*
* @ param Handler
2022-06-28 07:29:07 -07:00
*
2021-09-28 18:17:14 +03:30
* @ return VOID
*/
VOID
ScriptEngineSetTextMessageCallbackWrapper ( PVOID Handler )
{
return ScriptEngineSetTextMessageCallback ( Handler ) ;
}
2021-05-31 20:13:18 +04:30
/**
* @ brief ScriptEngineUnloadAllSymbols wrapper
2022-06-28 07:29:07 -07:00
*
2021-05-31 20:13:18 +04:30
* @ return UINT32
*/
UINT32
ScriptEngineUnloadAllSymbolsWrapper ( )
{
return ScriptEngineUnloadAllSymbols ( ) ;
}
2021-06-21 01:18:42 +04:30
/**
* @ brief ScriptEngineUnloadModuleSymbol wrapper
* @ param ModuleName
2022-06-28 07:29:07 -07:00
*
2021-06-21 01:18:42 +04:30
* @ return UINT32
*/
UINT32
ScriptEngineUnloadModuleSymbolWrapper ( char * ModuleName )
{
return ScriptEngineUnloadModuleSymbol ( ModuleName ) ;
}
2021-05-31 20:13:18 +04:30
/**
* @ brief ScriptEngineSearchSymbolForMask wrapper
*
* @ param SearchMask
2022-06-28 07:29:07 -07:00
*
2021-05-31 20:13:18 +04:30
* @ return UINT32
*/
UINT32
2021-10-19 17:13:44 +03:30
ScriptEngineSearchSymbolForMaskWrapper ( const char * SearchMask )
2021-05-30 20:25:07 +04:30
{
2021-10-19 17:13:44 +03:30
return ScriptEngineSearchSymbolForMask ( SearchMask ) ;
}
2021-11-11 00:47:55 +03:30
/**
* @ brief ScriptEngineGetFieldOffset wrapper
*
* @ param TypeName
* @ param FieldName
* @ param FieldOffset
2022-06-28 07:29:07 -07:00
*
2021-11-11 00:47:55 +03:30
* @ return BOOLEAN
*/
BOOLEAN
2022-04-13 12:45:39 +04:30
ScriptEngineGetFieldOffsetWrapper ( CHAR * TypeName , CHAR * FieldName , UINT32 * FieldOffset )
2021-11-11 00:47:55 +03:30
{
return ScriptEngineGetFieldOffset ( TypeName , FieldName , FieldOffset ) ;
}
2022-04-16 01:03:28 +04:30
/**
* @ brief ScriptEngineGetDataTypeSize wrapper
*
* @ param TypeName
* @ param TypeSize
2022-06-28 07:29:07 -07:00
*
2022-04-16 01:03:28 +04:30
* @ return BOOLEAN
*/
BOOLEAN
ScriptEngineGetDataTypeSizeWrapper ( CHAR * TypeName , UINT64 * TypeSize )
{
return ScriptEngineGetDataTypeSize ( TypeName , TypeSize ) ;
}
2021-10-19 17:13:44 +03:30
/**
* @ brief ScriptEngineCreateSymbolTableForDisassembler wrapper
*
* @ param CallbackFunction
2022-06-28 07:29:07 -07:00
*
2021-10-19 17:13:44 +03:30
* @ return BOOLEAN
*/
BOOLEAN
ScriptEngineCreateSymbolTableForDisassemblerWrapper ( void * CallbackFunction )
{
return ScriptEngineCreateSymbolTableForDisassembler ( CallbackFunction ) ;
2021-05-29 18:52:50 +04:30
}
2021-06-07 21:01:41 +04:30
/**
* @ brief ScriptEngineConvertFileToPdbPath wrapper
*
* @ param LocalFilePath
* @ param ResultPath
2022-06-28 07:29:07 -07:00
*
2021-06-07 21:01:41 +04:30
* @ return BOOLEAN
*/
BOOLEAN
2024-12-25 21:35:17 +03:30
ScriptEngineConvertFileToPdbPathWrapper ( const char * LocalFilePath , char * ResultPath , size_t ResultPathSize )
2021-06-07 21:01:41 +04:30
{
2024-12-25 21:35:17 +03:30
return ScriptEngineConvertFileToPdbPath ( LocalFilePath , ResultPath , ResultPathSize ) ;
2021-06-07 21:01:41 +04:30
}
2021-06-10 17:33:05 +04:30
/**
* @ brief ScriptEngineSymbolInitLoad wrapper
*
* @ param ScriptEngineSymbolInitLoad
* @ param StoredLength
2021-06-18 01:37:29 +04:30
* @ param DownloadIfAvailable
2021-06-10 17:33:05 +04:30
* @ param SymbolPath
2021-06-21 15:15:28 +04:30
* @ param IsSilentLoad
2022-06-28 07:29:07 -07:00
*
2021-06-10 08:10:18 -07:00
* @ return BOOLEAN
2021-06-10 17:33:05 +04:30
*/
2021-06-10 08:10:18 -07:00
BOOLEAN
2021-06-21 15:15:28 +04:30
ScriptEngineSymbolInitLoadWrapper ( PMODULE_SYMBOL_DETAIL BufferToStoreDetails ,
UINT32 StoredLength ,
BOOLEAN DownloadIfAvailable ,
const char * SymbolPath ,
BOOLEAN IsSilentLoad )
2021-06-10 17:33:05 +04:30
{
2021-06-21 15:15:28 +04:30
return ScriptEngineSymbolInitLoad ( BufferToStoreDetails , StoredLength , DownloadIfAvailable , SymbolPath , IsSilentLoad ) ;
2021-06-10 17:33:05 +04:30
}
2022-04-07 12:45:48 +04:30
/**
* @ brief ScriptEngineShowDataBasedOnSymbolTypes wrapper
*
* @ param TypeName
* @ param Address
2022-04-15 23:18:06 +04:30
* @ param IsStruct
2022-04-07 12:45:48 +04:30
* @ param BufferAddress
* @ param AdditionalParameters
2022-06-28 07:29:07 -07:00
*
2022-04-07 12:45:48 +04:30
* @ return BOOLEAN
*/
BOOLEAN
ScriptEngineShowDataBasedOnSymbolTypesWrapper (
const char * TypeName ,
UINT64 Address ,
2022-04-15 23:18:06 +04:30
BOOLEAN IsStruct ,
2022-04-07 12:45:48 +04:30
PVOID BufferAddress ,
2022-04-08 01:21:03 +04:30
const char * AdditionalParameters )
2022-04-07 12:45:48 +04:30
{
2022-04-15 23:18:06 +04:30
return ScriptEngineShowDataBasedOnSymbolTypes ( TypeName , Address , IsStruct , BufferAddress , AdditionalParameters ) ;
2022-04-07 12:45:48 +04:30
}
2021-09-01 16:01:45 +04:30
/**
* @ brief SymbolAbortLoading wrapper
*
* @ return VOID
*/
VOID
ScriptEngineSymbolAbortLoadingWrapper ( )
{
return ScriptEngineSymbolAbortLoading ( ) ;
}
2021-06-10 02:54:32 +04:30
/**
* @ brief ScriptEngineConvertFileToPdbFileAndGuidAndAgeDetails wrapper
*
* @ param LocalFilePath
* @ param PdbFilePath
* @ param GuidAndAgeDetails
2023-07-16 04:00:09 +09:00
* @ param Is32BitModule
2022-06-28 07:29:07 -07:00
*
2021-06-10 02:54:32 +04:30
* @ return BOOLEAN
*/
BOOLEAN
ScriptEngineConvertFileToPdbFileAndGuidAndAgeDetailsWrapper ( const char * LocalFilePath ,
char * PdbFilePath ,
2023-07-16 04:00:09 +09:00
char * GuidAndAgeDetails ,
BOOLEAN Is32BitModule )
2021-06-10 02:54:32 +04:30
{
2023-07-16 04:00:09 +09:00
return ScriptEngineConvertFileToPdbFileAndGuidAndAgeDetails ( LocalFilePath , PdbFilePath , GuidAndAgeDetails , Is32BitModule ) ;
2021-06-10 02:54:32 +04:30
}
2021-05-07 16:30:26 +04:30
//
2021-05-30 20:25:07 +04:30
// *********************** Function links (wrapper) ***********************
2021-05-07 16:30:26 +04:30
//
2021-05-30 20:25:07 +04:30
/**
* @ brief ScriptEngineParse wrapper
*
2021-09-24 14:03:48 +03:30
* @ param Expr
* @ param ShowErrorMessageIfAny
2022-06-28 07:29:07 -07:00
*
2021-05-30 20:25:07 +04:30
* @ return PVOID
*/
2021-03-22 18:19:39 +04:30
PVOID
2021-09-24 14:03:48 +03:30
ScriptEngineParseWrapper ( char * Expr , BOOLEAN ShowErrorMessageIfAny )
2021-03-22 18:19:39 +04:30
{
PSYMBOL_BUFFER SymbolBuffer ;
2024-06-24 20:41:43 +09:00
SymbolBuffer = ( PSYMBOL_BUFFER ) ScriptEngineParse ( Expr ) ;
2021-03-22 18:19:39 +04:30
2021-02-04 15:08:10 -08:00
//
2021-03-22 18:19:39 +04:30
// Check if there is an error or not
2021-02-04 15:08:10 -08:00
//
2021-03-22 18:19:39 +04:30
if ( SymbolBuffer - > Message = = NULL )
{
return SymbolBuffer ;
}
else
{
//
// Show error message and free the buffer
//
2021-09-24 14:03:48 +03:30
if ( ShowErrorMessageIfAny )
{
ShowMessages ( " %s \n " , SymbolBuffer - > Message ) ;
}
2021-03-22 18:19:39 +04:30
ScriptEngineWrapperRemoveSymbolBuffer ( SymbolBuffer ) ;
return NULL ;
}
2020-10-29 07:05:20 -07:00
}
2020-10-25 10:09:52 -07:00
2021-05-30 20:25:07 +04:30
/**
* @ brief PrintSymbolBuffer wrapper
* @ details Print symbol buffer wrapper
* @ param SymbolBuffer
2022-06-28 07:29:07 -07:00
*
2021-05-30 20:25:07 +04:30
* @ return PVOID
*/
VOID
2021-03-22 18:19:39 +04:30
PrintSymbolBufferWrapper ( PVOID SymbolBuffer )
{
2024-06-24 20:41:43 +09:00
PrintSymbolBuffer ( SymbolBuffer ) ;
2020-10-25 10:09:52 -07:00
}
2021-05-30 20:25:07 +04:30
/**
2024-06-11 22:24:05 +09:00
* @ brief Script engine evaluation wrapper
2021-05-30 20:25:07 +04:30
* @ param GuestRegs
* @ param Expr
2022-06-28 07:29:07 -07:00
*
2021-05-30 20:25:07 +04:30
* @ return VOID
*/
2021-03-22 18:19:39 +04:30
VOID
2021-09-23 23:29:29 +03:30
ScriptEngineEvalWrapper ( PGUEST_REGS GuestRegs ,
string Expr )
2021-03-22 18:19:39 +04:30
{
2024-07-24 21:25:55 +08:00
SCRIPT_ENGINE_GENERAL_REGISTERS ScriptGeneralRegisters = { 0 } ;
2021-10-26 22:28:32 +03:30
2021-03-22 18:19:39 +04:30
//
2021-09-23 23:29:29 +03:30
// Allocate global variables holder
//
if ( ! g_ScriptGlobalVariables )
{
g_ScriptGlobalVariables = ( UINT64 * ) malloc ( MAX_VAR_COUNT * sizeof ( UINT64 ) ) ;
2024-03-18 12:31:11 +09:00
if ( g_ScriptGlobalVariables = = NULL )
{
ShowMessages ( " err, could not allocate memory for user-mode global variables " ) ;
return ;
}
2021-09-23 23:29:29 +03:30
RtlZeroMemory ( g_ScriptGlobalVariables , MAX_VAR_COUNT * sizeof ( UINT64 ) ) ;
}
2021-10-26 22:28:32 +03:30
//
2024-07-24 21:25:55 +08:00
// Allocate stack buffer holder, actually in reality each core should
// have its own set of stack buffer but as we never run multi-core scripts
// in user-mode, thus, it's okay to just have one buffer for stack buffer
2021-10-26 22:28:32 +03:30
//
2024-07-24 21:25:55 +08:00
if ( ! g_ScriptStackBuffer )
2021-10-26 22:28:32 +03:30
{
2024-08-23 22:33:36 +08:00
g_ScriptStackBuffer = ( UINT64 * ) malloc ( MAX_STACK_BUFFER_COUNT * sizeof ( UINT64 ) ) ;
2024-03-18 12:31:11 +09:00
2024-07-24 21:25:55 +08:00
if ( g_ScriptStackBuffer = = NULL )
2024-03-18 12:31:11 +09:00
{
free ( g_ScriptGlobalVariables ) ;
2024-07-24 21:25:55 +08:00
ShowMessages ( " err, could not allocate memory for user-mode stack buffer " ) ;
2024-03-18 12:31:11 +09:00
return ;
}
2022-01-27 23:49:26 +03:30
}
2021-09-23 23:29:29 +03:30
//
// Run Parser
2021-03-22 18:19:39 +04:30
//
2024-06-24 20:41:43 +09:00
PSYMBOL_BUFFER CodeBuffer = ( PSYMBOL_BUFFER ) ScriptEngineParse ( ( char * ) Expr . c_str ( ) ) ;
2021-05-07 16:30:26 +04:30
2024-07-24 21:25:55 +08:00
# ifdef _SCRIPT_ENGINE_IR_PRINT_EN
2021-08-17 15:54:09 +04:30
//
// Print symbol buffer
//
2024-07-19 19:17:46 +08:00
PrintSymbolBuffer ( ( PVOID ) CodeBuffer ) ;
# endif
2021-08-17 15:54:09 +04:30
2022-01-27 23:49:26 +03:30
ACTION_BUFFER ActionBuffer = { 0 } ;
SYMBOL ErrorSymbol = { 0 } ;
2021-03-22 18:19:39 +04:30
2024-07-20 11:58:17 +08:00
UINT64 EXECUTENUMBER = 0 ;
2024-07-24 21:25:55 +08:00
ScriptGeneralRegisters . StackBuffer = g_ScriptStackBuffer ;
ScriptGeneralRegisters . GlobalVariablesList = g_ScriptGlobalVariables ;
2024-08-23 22:33:36 +08:00
RtlZeroMemory ( g_ScriptStackBuffer , MAX_STACK_BUFFER_COUNT * sizeof ( UINT64 ) ) ;
2024-03-02 01:59:33 +08:00
2021-03-22 18:19:39 +04:30
if ( CodeBuffer - > Message = = NULL )
{
2024-07-19 19:17:46 +08:00
# ifdef _SCRIPT_ENGINE_CODEEXEC_DBG_EN
printf ( " \n ScriptEngineExecute: \n " ) ;
# endif
UINT64 i = 0 ;
for ( ; i < CodeBuffer - > Pointer ; )
2021-03-22 18:19:39 +04:30
{
//
// Fill the action buffer but as we're in user-mode here
// then there is nothing to fill
//
ActionBuffer . Context = NULL ;
ActionBuffer . CurrentAction = NULL ;
ActionBuffer . ImmediatelySendTheResults = FALSE ;
ActionBuffer . Tag = NULL ;
2024-07-19 19:17:46 +08:00
# ifdef _SCRIPT_ENGINE_CODEEXEC_DBG_EN
2024-07-24 21:25:55 +08:00
printf ( " Address = %lld, StackIndx = %lld, StackBaseIndx = %lld \n " , i , ScriptGeneralRegisters . StackIndx , ScriptGeneralRegisters . StackBaseIndx ) ;
2024-07-19 19:17:46 +08:00
PSYMBOL Operator = ( PSYMBOL ) ( ( unsigned long long ) CodeBuffer - > Head +
( unsigned long long ) ( i * sizeof ( SYMBOL ) ) ) ;
printf ( " Function = %s \n " , FunctionNames [ Operator - > Value ] ) ;
printf ( " Stack Buffer: \n " ) ;
2024-07-24 21:25:55 +08:00
for ( UINT64 j = 0 ; j < ScriptGeneralRegisters . StackIndx ; j + + )
2024-07-19 19:17:46 +08:00
{
2024-08-23 22:33:36 +08:00
printf ( " StackIndx = %lld, Value = %lld " , j , ScriptGeneralRegisters . StackBuffer [ j ] ) ;
2024-07-19 19:17:46 +08:00
2024-07-24 21:25:55 +08:00
if ( j = = ScriptGeneralRegisters . StackBaseIndx )
2024-07-19 19:17:46 +08:00
{
printf ( " <===== StackBaseIndx " ) ;
}
printf ( " \n " ) ;
}
printf ( " \n " ) ;
# endif
2021-03-22 18:19:39 +04:30
//
2022-03-12 15:09:17 +03:30
// If has error, show error message and abort
2021-03-22 18:19:39 +04:30
//
2022-03-12 15:09:17 +03:30
if ( ScriptEngineExecute ( GuestRegs ,
& ActionBuffer ,
2024-07-24 21:25:55 +08:00
& ScriptGeneralRegisters ,
2022-03-12 15:09:17 +03:30
CodeBuffer ,
& i ,
2024-07-24 21:25:55 +08:00
& ErrorSymbol ) = = TRUE )
2021-03-22 18:19:39 +04:30
{
2024-07-20 11:58:17 +08:00
ShowMessages ( " err, ScriptEngineExecute, function = %s \n " ,
2024-07-19 19:17:46 +08:00
FunctionNames [ ErrorSymbol . Value ] ) ;
g_CurrentExprEvalResultHasError = TRUE ;
g_CurrentExprEvalResult = NULL ;
break ;
}
2024-07-24 21:25:55 +08:00
else if ( ScriptGeneralRegisters . StackIndx > = MAX_STACK_BUFFER_COUNT )
2024-07-19 19:17:46 +08:00
{
2025-01-25 18:37:54 +01:00
ShowMessages ( " err, stack buffer overflow (more information: https://docs.hyperdbg.org/tips-and-tricks/misc/customize-build/change-script-engine-limitations) \n " ) ;
2021-09-24 00:40:21 +03:30
g_CurrentExprEvalResultHasError = TRUE ;
g_CurrentExprEvalResult = NULL ;
2021-03-22 18:19:39 +04:30
break ;
}
2024-07-20 11:58:17 +08:00
else if ( EXECUTENUMBER > = MAX_EXECUTION_COUNT )
{
2025-01-25 18:37:54 +01:00
ShowMessages ( " err, exceeding the max execution count (more information: https://docs.hyperdbg.org/tips-and-tricks/misc/customize-build/change-script-engine-limitations) \n " ) ;
2024-07-20 11:58:17 +08:00
g_CurrentExprEvalResultHasError = TRUE ;
g_CurrentExprEvalResult = NULL ;
break ;
}
EXECUTENUMBER + + ;
2021-03-22 18:19:39 +04:30
}
}
else
{
ShowMessages ( " %s \n " , CodeBuffer - > Message ) ;
2020-10-25 10:09:52 -07:00
}
2021-03-02 02:48:41 +03:30
2022-01-28 02:11:15 +09:00
RemoveSymbolBuffer ( CodeBuffer ) ;
2021-02-04 15:08:10 -08:00
2021-03-22 18:19:39 +04:30
return ;
2020-10-25 10:09:52 -07:00
}
2021-08-20 17:56:54 +04:30
/**
* @ brief massive tests for script engine statements
* @ param Expr The expression to test
2022-06-28 07:29:07 -07:00
* @ param ExpectationValue What value this statements expects ( not
2021-08-20 17:56:54 +04:30
* used if ExceptError is TRUE )
* @ param ExceptError True if the statement expects an error
2022-06-28 07:29:07 -07:00
*
2021-08-20 17:56:54 +04:30
* @ return BOOLEAN whether the test was successful or not
*/
BOOLEAN
2022-02-03 19:17:34 +03:00
ScriptAutomaticStatementsTestWrapper ( const string & Expr , UINT64 ExpectationValue , BOOLEAN ExceptError )
2021-08-20 17:56:54 +04:30
{
2022-01-30 21:00:44 +03:30
//
// Set the global variable indicator of test_statement to 0
//
g_CurrentExprEvalResult = 0 ;
2021-08-20 18:42:12 +04:30
//
// Call the test parser
//
ScriptEngineWrapperTestParser ( Expr ) ;
//
// Check the global variable to see the results
//
2021-09-24 00:40:21 +03:30
if ( g_CurrentExprEvalResultHasError & & ExceptError )
2021-08-20 18:42:12 +04:30
{
return TRUE ;
}
2021-09-24 00:40:21 +03:30
else if ( ExpectationValue = = g_CurrentExprEvalResult )
2021-08-20 18:42:12 +04:30
{
return TRUE ;
}
return FALSE ;
2021-08-20 17:56:54 +04:30
}
2022-05-08 20:24:31 +04:30
/**
* @ brief allocate memory and build structure for casting
* @ param AllocationsForCastings Memory details for future deallocations
2022-06-28 07:29:07 -07:00
*
* @ return PVOID
2022-05-08 20:24:31 +04:30
*/
2022-02-04 22:40:24 +03:30
PVOID
2022-05-08 20:24:31 +04:30
AllocateStructForCasting ( PALLOCATED_MEMORY_FOR_SCRIPT_ENGINE_CASTING AllocationsForCastings )
2022-02-04 22:40:24 +03:30
{
typedef struct _UNICODE_STRING
{
2022-04-13 12:45:39 +04:30
UINT16 Length ; // +0x000
UINT16 MaximumLength ; // +0x002
2022-02-04 22:40:24 +03:30
PWSTR Buffer ; // +0x004
} UNICODE_STRING , * PUNICODE_STRING ;
typedef struct _STUPID_STRUCT1
{
UINT32 Flag32 ; // +0x000
UINT64 Flag64 ; // +0x004
PVOID Context ; // +0x00c
PUNICODE_STRING StringValue ; // +0x014
} STUPID_STRUCT1 , * PSTUPID_STRUCT1 ;
typedef struct _STUPID_STRUCT2
{
UINT32 Sina32 ; // +0x000
UINT64 Sina64 ; // +0x004
PVOID AghaaSina ; // +0x00c
PUNICODE_STRING UnicodeStr ; // +0x014
PSTUPID_STRUCT1 StupidStruct1 ; // +0x01c
} STUPID_STRUCT2 , * PSTUPID_STRUCT2 ;
//
// Allocate UNICODE_STRING 1
//
WCHAR MyString1 [ 40 ] = L " Hi come from stupid struct 1! " ;
2024-03-15 13:49:02 +09:00
UINT32 SizeOfMyString1 = ( UINT32 ) wcslen ( MyString1 ) * sizeof ( WCHAR ) + 2 ;
2022-02-04 22:40:24 +03:30
PUNICODE_STRING UnicodeStr1 = ( PUNICODE_STRING ) malloc ( sizeof ( UNICODE_STRING ) ) ;
2024-03-18 12:31:11 +09:00
if ( UnicodeStr1 = = NULL )
{
return NULL ;
}
AllocationsForCastings - > Buff1 = ( CHAR * ) UnicodeStr1 ;
WCHAR * Buff1 = ( WCHAR * ) malloc ( SizeOfMyString1 ) ;
if ( Buff1 = = NULL )
{
free ( UnicodeStr1 ) ;
return NULL ;
}
AllocationsForCastings - > Buff2 = ( CHAR * ) Buff1 ;
2022-02-04 22:40:24 +03:30
RtlZeroMemory ( Buff1 , SizeOfMyString1 ) ;
UnicodeStr1 - > Buffer = Buff1 ;
UnicodeStr1 - > Length = UnicodeStr1 - > MaximumLength = SizeOfMyString1 ;
memcpy ( UnicodeStr1 - > Buffer , MyString1 , SizeOfMyString1 ) ;
//
// Allocate UNICODE_STRING 2
//
WCHAR MyString2 [ 40 ] = L " Goodbye I'm at stupid struct 2! " ;
2024-03-15 13:49:02 +09:00
UINT32 SizeOfMyString2 = ( UINT32 ) wcslen ( MyString2 ) * sizeof ( WCHAR ) + 2 ;
2022-02-04 22:40:24 +03:30
PUNICODE_STRING UnicodeStr2 = ( PUNICODE_STRING ) malloc ( sizeof ( UNICODE_STRING ) ) ;
2024-03-18 12:31:11 +09:00
if ( UnicodeStr2 = = NULL )
{
free ( UnicodeStr1 ) ;
free ( Buff1 ) ;
return NULL ;
}
AllocationsForCastings - > Buff3 = ( CHAR * ) UnicodeStr2 ;
WCHAR * Buff2 = ( WCHAR * ) malloc ( SizeOfMyString2 ) ;
if ( Buff2 = = NULL )
{
free ( UnicodeStr1 ) ;
free ( Buff1 ) ;
free ( UnicodeStr2 ) ;
return NULL ;
}
AllocationsForCastings - > Buff4 = ( CHAR * ) Buff2 ;
2022-02-04 22:40:24 +03:30
RtlZeroMemory ( Buff2 , SizeOfMyString2 ) ;
UnicodeStr2 - > Buffer = Buff2 ;
UnicodeStr2 - > Length = UnicodeStr2 - > MaximumLength = SizeOfMyString2 ;
memcpy ( UnicodeStr2 - > Buffer , MyString2 , SizeOfMyString2 ) ;
//
// Allocate STUPID_STRUCT1
//
PSTUPID_STRUCT1 StupidStruct1 = ( PSTUPID_STRUCT1 ) malloc ( sizeof ( STUPID_STRUCT1 ) ) ;
2024-03-18 12:31:11 +09:00
if ( StupidStruct1 = = NULL )
{
free ( UnicodeStr1 ) ;
free ( Buff1 ) ;
free ( UnicodeStr2 ) ;
free ( Buff2 ) ;
return NULL ;
}
2022-05-08 20:24:31 +04:30
AllocationsForCastings - > Buff5 = ( CHAR * ) StupidStruct1 ;
2022-02-04 22:40:24 +03:30
StupidStruct1 - > Flag32 = 0x3232 ;
StupidStruct1 - > Flag64 = 0x6464 ;
StupidStruct1 - > Context = ( PVOID ) 0x85 ;
StupidStruct1 - > StringValue = UnicodeStr1 ;
//
// Allocate STUPID_STRUCT2
//
PSTUPID_STRUCT2 StupidStruct2 = ( PSTUPID_STRUCT2 ) malloc ( sizeof ( STUPID_STRUCT2 ) ) ;
2024-03-18 12:31:11 +09:00
if ( StupidStruct2 = = NULL )
{
free ( UnicodeStr1 ) ;
free ( Buff1 ) ;
free ( UnicodeStr2 ) ;
free ( Buff2 ) ;
free ( StupidStruct1 ) ;
return NULL ;
}
2022-05-08 20:24:31 +04:30
AllocationsForCastings - > Buff6 = ( CHAR * ) StupidStruct2 ;
2022-02-04 22:40:24 +03:30
StupidStruct2 - > Sina32 = 0x32 ;
StupidStruct2 - > Sina64 = 0x64 ;
StupidStruct2 - > AghaaSina = ( PVOID ) 0x55 ;
StupidStruct2 - > UnicodeStr = UnicodeStr2 ;
StupidStruct2 - > StupidStruct1 = StupidStruct1 ;
2022-02-13 03:38:24 +03:30
//_CrtDbgBreak();
2022-02-04 22:40:24 +03:30
return StupidStruct2 ;
}
2021-05-30 20:25:07 +04:30
/**
* @ brief test parser
* @ param Expr
2022-06-28 07:29:07 -07:00
*
2021-05-30 20:25:07 +04:30
* @ return VOID
*/
2021-03-22 18:19:39 +04:30
VOID
2022-02-03 19:17:34 +03:00
ScriptEngineWrapperTestParser ( const string & Expr )
2021-03-22 18:19:39 +04:30
{
2022-05-08 20:24:31 +04:30
ALLOCATED_MEMORY_FOR_SCRIPT_ENGINE_CASTING AllocationsForCastings = { 0 } ;
2021-03-22 18:19:39 +04:30
typedef struct _TEST_STRUCT
{
UINT64 Var1 ;
UINT64 Var2 ;
UINT64 Var3 ;
UINT64 Var4 ;
} TEST_STRUCT , * PTEST_STRUCT ;
PTEST_STRUCT TestStruct = ( PTEST_STRUCT ) malloc ( sizeof ( TEST_STRUCT ) ) ;
2024-03-18 12:31:11 +09:00
if ( TestStruct = = NULL )
{
return ;
}
2021-03-22 18:19:39 +04:30
RtlZeroMemory ( TestStruct , sizeof ( TEST_STRUCT ) ) ;
TestStruct - > Var1 = 0x41414141 ;
TestStruct - > Var3 = 0x4242424242424242 ;
GUEST_REGS GuestRegs = { 0 } ;
char test [ ] = " Hello world ! " ;
wchar_t testw [ ] =
L " A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 "
L " 9 a b c d e f g h i j k l m n o p q r s t u v w x y z " ;
2024-03-18 12:31:11 +09:00
2022-05-12 03:00:09 +04:30
char * RspReg = ( char * ) malloc ( 0x100 ) ;
2024-03-18 12:31:11 +09:00
if ( RspReg = = NULL )
{
ShowMessages ( " err, unable to allocate stack for script engine tests " ) ;
2024-04-05 16:13:13 +03:00
free ( TestStruct ) ;
2024-03-18 12:31:11 +09:00
return ;
}
memcpy ( RspReg , testw , sizeof ( testw ) ) ;
2021-03-22 18:19:39 +04:30
GuestRegs . rax = 0x1 ;
2022-05-08 20:24:31 +04:30
GuestRegs . rcx = ( UINT64 ) AllocateStructForCasting ( & AllocationsForCastings ) ; // TestStruct
2021-03-22 18:19:39 +04:30
GuestRegs . rdx = 0x3 ;
GuestRegs . rbx = 0x4 ;
2022-05-12 03:00:09 +04:30
GuestRegs . rsp = ( UINT64 ) RspReg + 0x50 ;
2021-03-22 18:19:39 +04:30
GuestRegs . rbp = 0x6 ;
GuestRegs . rsi = 0x7 ;
GuestRegs . rdi = 0x8 ;
GuestRegs . r8 = 0x9 ;
GuestRegs . r9 = 0xa ;
GuestRegs . r10 = 0xb ;
GuestRegs . r11 = 0xc ;
GuestRegs . r12 = 0xd ;
GuestRegs . r13 = 0xe ;
2022-04-13 12:45:39 +04:30
GuestRegs . r14 = ( UINT64 ) testw ;
GuestRegs . r15 = ( UINT64 ) test ;
2021-03-22 18:19:39 +04:30
2021-09-23 23:29:29 +03:30
ScriptEngineEvalWrapper ( & GuestRegs , Expr ) ;
2022-05-08 20:24:31 +04:30
2022-05-12 03:00:09 +04:30
free ( RspReg ) ;
2022-01-27 22:07:59 +09:00
free ( TestStruct ) ;
2022-05-08 20:24:31 +04:30
free ( AllocationsForCastings . Buff1 ) ;
free ( AllocationsForCastings . Buff2 ) ;
free ( AllocationsForCastings . Buff3 ) ;
free ( AllocationsForCastings . Buff4 ) ;
free ( AllocationsForCastings . Buff5 ) ;
free ( AllocationsForCastings . Buff6 ) ;
2021-09-23 23:29:29 +03:30
}
2024-10-02 18:00:34 +02:00
/**
* @ 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 ) ;
}
2021-09-23 23:29:29 +03:30
/**
* @ 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
* @ param Expr
2021-09-24 00:40:21 +03:30
* @ param HasError
2022-06-28 07:29:07 -07:00
*
2021-09-24 00:40:21 +03:30
* @ return UINT64
2021-09-23 23:29:29 +03:30
*/
UINT64
2022-02-03 19:17:34 +03:00
ScriptEngineEvalUInt64StyleExpressionWrapper ( const string & Expr , PBOOLEAN HasError )
2021-09-23 23:29:29 +03:30
{
2021-05-07 16:30:26 +04:30
//
2021-09-23 23:29:29 +03:30
// In VMI-mode we'll form all registers as zero
2021-05-07 16:30:26 +04:30
//
2021-09-23 23:29:29 +03:30
GUEST_REGS GuestRegs = { 0 } ;
2021-05-07 16:30:26 +04:30
2021-09-23 23:29:29 +03:30
ScriptEngineEvalWrapper ( & GuestRegs , Expr ) ;
2021-09-24 00:40:21 +03:30
//
// Set the results and return the value
//
* HasError = g_CurrentExprEvalResultHasError ;
return g_CurrentExprEvalResult ;
2020-10-25 10:09:52 -07:00
}
2021-05-30 20:25:07 +04:30
/**
2022-06-28 07:29:07 -07:00
* @ brief wrapper for getting head
2021-05-30 20:25:07 +04:30
* @ param SymbolBuffer
2022-06-28 07:29:07 -07:00
*
2021-05-30 20:25:07 +04:30
* @ return UINT64
*/
2021-03-22 18:19:39 +04:30
UINT64
ScriptEngineWrapperGetHead ( PVOID SymbolBuffer )
{
return ( UINT64 ) ( ( PSYMBOL_BUFFER ) SymbolBuffer ) - > Head ;
2020-10-25 10:09:52 -07:00
}
2021-05-30 20:25:07 +04:30
/**
2022-06-28 07:29:07 -07:00
* @ brief wrapper for getting size
2021-05-30 20:25:07 +04:30
* @ param SymbolBuffer
2022-06-28 07:29:07 -07:00
*
2021-05-30 20:25:07 +04:30
* @ return UINT32
*/
2021-03-22 18:19:39 +04:30
UINT32
ScriptEngineWrapperGetSize ( PVOID SymbolBuffer )
{
UINT32 Size =
( UINT32 ) ( ( PSYMBOL_BUFFER ) SymbolBuffer ) - > Pointer * sizeof ( SYMBOL ) ;
return Size ;
2020-10-25 10:09:52 -07:00
}
2020-10-27 02:06:10 -07:00
2021-05-30 20:25:07 +04:30
/**
2022-06-28 07:29:07 -07:00
* @ brief wrapper for getting pointer
2021-05-30 20:25:07 +04:30
* @ param SymbolBuffer
2022-06-28 07:29:07 -07:00
*
2021-05-30 20:25:07 +04:30
* @ return UINT32
*/
2021-03-22 18:19:39 +04:30
UINT32
ScriptEngineWrapperGetPointer ( PVOID SymbolBuffer )
{
return ( UINT32 ) ( ( PSYMBOL_BUFFER ) SymbolBuffer ) - > Pointer ;
2020-10-27 02:06:10 -07:00
}
2021-02-04 13:36:57 -08:00
2021-05-30 20:25:07 +04:30
/**
2022-06-28 07:29:07 -07:00
* @ brief wrapper for removing symbol buffer
2021-05-30 20:25:07 +04:30
* @ param SymbolBuffer
2022-06-28 07:29:07 -07:00
*
2021-05-30 20:25:07 +04:30
* @ return UINT32
*/
2021-03-22 18:19:39 +04:30
VOID
ScriptEngineWrapperRemoveSymbolBuffer ( PVOID SymbolBuffer )
{
RemoveSymbolBuffer ( ( PSYMBOL_BUFFER ) SymbolBuffer ) ;
2021-02-04 13:36:57 -08:00
}