HyperDbg Debugger
Loading...
Searching...
No Matches
script-engine-wrapper.cpp File Reference

Interpret general fields. More...

#include "pch.h"

Classes

struct  _ALLOCATED_MEMORY_FOR_SCRIPT_ENGINE_CASTING
 

Typedefs

typedef struct _ALLOCATED_MEMORY_FOR_SCRIPT_ENGINE_CASTING ALLOCATED_MEMORY_FOR_SCRIPT_ENGINE_CASTING
 
typedef struct _ALLOCATED_MEMORY_FOR_SCRIPT_ENGINE_CASTINGPALLOCATED_MEMORY_FOR_SCRIPT_ENGINE_CASTING
 

Functions

UINT64 ScriptEngineConvertNameToAddressWrapper (const char *FunctionOrVariableName, PBOOLEAN WasFound)
 ScriptEngineConvertNameToAddress wrapper.
 
UINT32 ScriptEngineLoadFileSymbolWrapper (UINT64 BaseAddress, const char *PdbFileName, const char *CustomModuleName)
 ScriptEngineLoadFileSymbol wrapper.
 
VOID ScriptEngineSetTextMessageCallbackWrapper (PVOID Handler)
 ScriptEngineSetTextMessageCallback wrapper.
 
UINT32 ScriptEngineUnloadAllSymbolsWrapper ()
 ScriptEngineUnloadAllSymbols wrapper.
 
UINT32 ScriptEngineUnloadModuleSymbolWrapper (char *ModuleName)
 ScriptEngineUnloadModuleSymbol wrapper.
 
UINT32 ScriptEngineSearchSymbolForMaskWrapper (const char *SearchMask)
 ScriptEngineSearchSymbolForMask wrapper.
 
BOOLEAN ScriptEngineGetFieldOffsetWrapper (CHAR *TypeName, CHAR *FieldName, UINT32 *FieldOffset)
 ScriptEngineGetFieldOffset wrapper.
 
BOOLEAN ScriptEngineGetDataTypeSizeWrapper (CHAR *TypeName, UINT64 *TypeSize)
 ScriptEngineGetDataTypeSize wrapper.
 
BOOLEAN ScriptEngineCreateSymbolTableForDisassemblerWrapper (void *CallbackFunction)
 ScriptEngineCreateSymbolTableForDisassembler wrapper.
 
BOOLEAN ScriptEngineConvertFileToPdbPathWrapper (const char *LocalFilePath, char *ResultPath)
 ScriptEngineConvertFileToPdbPath wrapper.
 
BOOLEAN ScriptEngineSymbolInitLoadWrapper (PMODULE_SYMBOL_DETAIL BufferToStoreDetails, UINT32 StoredLength, BOOLEAN DownloadIfAvailable, const char *SymbolPath, BOOLEAN IsSilentLoad)
 ScriptEngineSymbolInitLoad wrapper.
 
BOOLEAN ScriptEngineShowDataBasedOnSymbolTypesWrapper (const char *TypeName, UINT64 Address, BOOLEAN IsStruct, PVOID BufferAddress, const char *AdditionalParameters)
 ScriptEngineShowDataBasedOnSymbolTypes wrapper.
 
VOID ScriptEngineSymbolAbortLoadingWrapper ()
 SymbolAbortLoading wrapper.
 
BOOLEAN ScriptEngineConvertFileToPdbFileAndGuidAndAgeDetailsWrapper (const char *LocalFilePath, char *PdbFilePath, char *GuidAndAgeDetails, BOOLEAN Is32BitModule)
 ScriptEngineConvertFileToPdbFileAndGuidAndAgeDetails wrapper.
 
PVOID ScriptEngineParseWrapper (char *Expr, BOOLEAN ShowErrorMessageIfAny)
 ScriptEngineParse wrapper.
 
VOID PrintSymbolBufferWrapper (PVOID SymbolBuffer)
 PrintSymbolBuffer wrapper.
 
VOID ScriptEngineEvalWrapper (PGUEST_REGS GuestRegs, string Expr)
 Script engine evaluation wrapper.
 
BOOLEAN ScriptAutomaticStatementsTestWrapper (const string &Expr, UINT64 ExpectationValue, BOOLEAN ExceptError)
 massive tests for script engine statements
 
PVOID AllocateStructForCasting (PALLOCATED_MEMORY_FOR_SCRIPT_ENGINE_CASTING AllocationsForCastings)
 allocate memory and build structure for casting
 
VOID ScriptEngineWrapperTestParser (const string &Expr)
 test parser
 
UINT64 ScriptEngineEvalUInt64StyleExpressionWrapper (const string &Expr, PBOOLEAN HasError)
 In the local debugging (VMI mode) environment, this function computes the expressions.
 
UINT64 ScriptEngineWrapperGetHead (PVOID SymbolBuffer)
 wrapper for getting head
 
UINT32 ScriptEngineWrapperGetSize (PVOID SymbolBuffer)
 wrapper for getting size
 
UINT32 ScriptEngineWrapperGetPointer (PVOID SymbolBuffer)
 wrapper for getting pointer
 
VOID ScriptEngineWrapperRemoveSymbolBuffer (PVOID SymbolBuffer)
 wrapper for removing symbol buffer
 
BOOLEAN ScriptEngineFuncNumberOfOperands (UINT64 FuncType, UINT32 *NumberOfGetOperands, UINT32 *NumberOfSetOperands)
 wrapper for getting operand count
 

Variables

UINT64g_ScriptGlobalVariables
 Holder of script engines global variables.
 
UINT64g_ScriptLocalVariables
 Holder of local variables for script engine.
 
UINT64g_ScriptTempVariables
 Holder of temp variables for script engine.
 
UINT64 g_CurrentExprEvalResult
 global variable to save the result of script-engine statement tests
 
BOOLEAN g_CurrentExprEvalResultHasError
 global variable to detect if there was an error in the result of script-engine statement tests
 

Detailed Description

Interpret general fields.

Author
M.H. Gholamrezaei (mh@hy.nosp@m.perd.nosp@m.bg.or.nosp@m.g)
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.1
Date
2020-10-25

Typedef Documentation

◆ ALLOCATED_MEMORY_FOR_SCRIPT_ENGINE_CASTING

◆ PALLOCATED_MEMORY_FOR_SCRIPT_ENGINE_CASTING

Function Documentation

◆ AllocateStructForCasting()

PVOID AllocateStructForCasting ( PALLOCATED_MEMORY_FOR_SCRIPT_ENGINE_CASTING AllocationsForCastings)

allocate memory and build structure for casting

Parameters
AllocationsForCastingsMemory details for future deallocations
Returns
PVOID
577{
578 typedef struct _UNICODE_STRING
579 {
580 UINT16 Length; // +0x000
581 UINT16 MaximumLength; // +0x002
582 PWSTR Buffer; // +0x004
584
585 typedef struct _STUPID_STRUCT1
586 {
587 UINT32 Flag32; // +0x000
588 UINT64 Flag64; // +0x004
589 PVOID Context; // +0x00c
592
593 typedef struct _STUPID_STRUCT2
594 {
595 UINT32 Sina32; // +0x000
596 UINT64 Sina64; // +0x004
597 PVOID AghaaSina; // +0x00c
598 PUNICODE_STRING UnicodeStr; // +0x014
600
602
603 //
604 // Allocate UNICODE_STRING 1
605 //
606 WCHAR MyString1[40] = L"Hi come from stupid struct 1!";
607 UINT32 SizeOfMyString1 = (UINT32)wcslen(MyString1) * sizeof(WCHAR) + 2;
608 PUNICODE_STRING UnicodeStr1 = (PUNICODE_STRING)malloc(sizeof(UNICODE_STRING));
609
610 if (UnicodeStr1 == NULL)
611 {
612 return NULL;
613 }
614
615 AllocationsForCastings->Buff1 = (CHAR *)UnicodeStr1;
616 WCHAR * Buff1 = (WCHAR *)malloc(SizeOfMyString1);
617
618 if (Buff1 == NULL)
619 {
620 free(UnicodeStr1);
621 return NULL;
622 }
623
624 AllocationsForCastings->Buff2 = (CHAR *)Buff1;
625 RtlZeroMemory(Buff1, SizeOfMyString1);
626 UnicodeStr1->Buffer = Buff1;
627 UnicodeStr1->Length = UnicodeStr1->MaximumLength = SizeOfMyString1;
628 memcpy(UnicodeStr1->Buffer, MyString1, SizeOfMyString1);
629
630 //
631 // Allocate UNICODE_STRING 2
632 //
633 WCHAR MyString2[40] = L"Goodbye I'm at stupid struct 2!";
634 UINT32 SizeOfMyString2 = (UINT32)wcslen(MyString2) * sizeof(WCHAR) + 2;
635 PUNICODE_STRING UnicodeStr2 = (PUNICODE_STRING)malloc(sizeof(UNICODE_STRING));
636
637 if (UnicodeStr2 == NULL)
638 {
639 free(UnicodeStr1);
640 free(Buff1);
641
642 return NULL;
643 }
644
645 AllocationsForCastings->Buff3 = (CHAR *)UnicodeStr2;
646 WCHAR * Buff2 = (WCHAR *)malloc(SizeOfMyString2);
647
648 if (Buff2 == NULL)
649 {
650 free(UnicodeStr1);
651 free(Buff1);
652 free(UnicodeStr2);
653
654 return NULL;
655 }
656
657 AllocationsForCastings->Buff4 = (CHAR *)Buff2;
658 RtlZeroMemory(Buff2, SizeOfMyString2);
659 UnicodeStr2->Buffer = Buff2;
660 UnicodeStr2->Length = UnicodeStr2->MaximumLength = SizeOfMyString2;
661 memcpy(UnicodeStr2->Buffer, MyString2, SizeOfMyString2);
662
663 //
664 // Allocate STUPID_STRUCT1
665 //
666 PSTUPID_STRUCT1 StupidStruct1 = (PSTUPID_STRUCT1)malloc(sizeof(STUPID_STRUCT1));
667
668 if (StupidStruct1 == NULL)
669 {
670 free(UnicodeStr1);
671 free(Buff1);
672 free(UnicodeStr2);
673 free(Buff2);
674
675 return NULL;
676 }
677
678 AllocationsForCastings->Buff5 = (CHAR *)StupidStruct1;
679 StupidStruct1->Flag32 = 0x3232;
680 StupidStruct1->Flag64 = 0x6464;
681 StupidStruct1->Context = (PVOID)0x85;
682 StupidStruct1->StringValue = UnicodeStr1;
683
684 //
685 // Allocate STUPID_STRUCT2
686 //
687 PSTUPID_STRUCT2 StupidStruct2 = (PSTUPID_STRUCT2)malloc(sizeof(STUPID_STRUCT2));
688
689 if (StupidStruct2 == NULL)
690 {
691 free(UnicodeStr1);
692 free(Buff1);
693 free(UnicodeStr2);
694 free(Buff2);
695 free(StupidStruct1);
696
697 return NULL;
698 }
699
700 AllocationsForCastings->Buff6 = (CHAR *)StupidStruct2;
701
702 StupidStruct2->Sina32 = 0x32;
703 StupidStruct2->Sina64 = 0x64;
704 StupidStruct2->AghaaSina = (PVOID)0x55;
705 StupidStruct2->UnicodeStr = UnicodeStr2;
706 StupidStruct2->StupidStruct1 = StupidStruct1;
707
708 //_CrtDbgBreak();
709 return StupidStruct2;
710}
unsigned short UINT16
Definition BasicTypes.h:47
wchar_t WCHAR
Definition BasicTypes.h:32
unsigned __int64 UINT64
Definition BasicTypes.h:21
unsigned int UINT32
Definition BasicTypes.h:48
char CHAR
Definition BasicTypes.h:31
struct _STUPID_STRUCT1 STUPID_STRUCT1
struct _STUPID_STRUCT2 * PSTUPID_STRUCT2
struct _STUPID_STRUCT1 * PSTUPID_STRUCT1
struct _UNICODE_STRING * PUNICODE_STRING
struct _UNICODE_STRING UNICODE_STRING
struct _STUPID_STRUCT2 STUPID_STRUCT2
NULL()
Definition test-case-generator.py:530
CHAR * Buff1
Definition script-engine-wrapper.cpp:31
CHAR * Buff5
Definition script-engine-wrapper.cpp:35
CHAR * Buff6
Definition script-engine-wrapper.cpp:36
CHAR * Buff4
Definition script-engine-wrapper.cpp:34
CHAR * Buff2
Definition script-engine-wrapper.cpp:32
CHAR * Buff3
Definition script-engine-wrapper.cpp:33
Definition casting.cpp:32
UINT32 Flag32
Definition casting.cpp:33
PVOID Context
Definition casting.cpp:35
UINT64 Flag64
Definition casting.cpp:34
PUNICODE_STRING StringValue
Definition casting.cpp:36
Definition casting.cpp:40
PSTUPID_STRUCT1 StupidStruct1
Definition casting.cpp:45
PUNICODE_STRING UnicodeStr
Definition casting.cpp:44
UINT64 Sina64
Definition casting.cpp:42
UINT32 Sina32
Definition casting.cpp:41
PVOID AghaaSina
Definition casting.cpp:43
Definition casting.cpp:25
USHORT Length
Definition casting.cpp:26
USHORT MaximumLength
Definition casting.cpp:27
PWSTR Buffer
Definition casting.cpp:28

◆ PrintSymbolBufferWrapper()

VOID PrintSymbolBufferWrapper ( PVOID SymbolBuffer)

PrintSymbolBuffer wrapper.

Print symbol buffer wrapper

Parameters
SymbolBuffer
Returns
PVOID
302{
303 PrintSymbolBuffer(SymbolBuffer);
304}
void PrintSymbolBuffer(const PVOID SymbolBuffer)
Prints a symbol buffer.
Definition script-engine.c:2859

◆ ScriptAutomaticStatementsTestWrapper()

BOOLEAN ScriptAutomaticStatementsTestWrapper ( const string & Expr,
UINT64 ExpectationValue,
BOOLEAN ExceptError )

massive tests for script engine statements

Parameters
ExprThe expression to test
ExpectationValueWhat value this statements expects (not used if ExceptError is TRUE)
ExceptErrorTrue if the statement expects an error
Returns
BOOLEAN whether the test was successful or not
543{
544 //
545 // Set the global variable indicator of test_statement to 0
546 //
548
549 //
550 // Call the test parser
551 //
553
554 //
555 // Check the global variable to see the results
556 //
557 if (g_CurrentExprEvalResultHasError && ExceptError)
558 {
559 return TRUE;
560 }
561 else if (ExpectationValue == g_CurrentExprEvalResult)
562 {
563 return TRUE;
564 }
565
566 return FALSE;
567}
#define TRUE
Definition BasicTypes.h:55
#define FALSE
Definition BasicTypes.h:54
VOID ScriptEngineWrapperTestParser(const string &Expr)
test parser
Definition script-engine-wrapper.cpp:719
BOOLEAN g_CurrentExprEvalResultHasError
global variable to detect if there was an error in the result of script-engine statement tests
Definition globals.h:671
UINT64 g_CurrentExprEvalResult
global variable to save the result of script-engine statement tests
Definition globals.h:664

◆ ScriptEngineConvertFileToPdbFileAndGuidAndAgeDetailsWrapper()

BOOLEAN ScriptEngineConvertFileToPdbFileAndGuidAndAgeDetailsWrapper ( const char * LocalFilePath,
char * PdbFilePath,
char * GuidAndAgeDetails,
BOOLEAN Is32BitModule )

ScriptEngineConvertFileToPdbFileAndGuidAndAgeDetails wrapper.

Parameters
LocalFilePath
PdbFilePath
GuidAndAgeDetails
Is32BitModule
Returns
BOOLEAN
250{
252}
char * PdbFilePath
Definition HyperDbgScriptImports.h:63
char char BOOLEAN Is32BitModule
Definition HyperDbgScriptImports.h:63
char char * GuidAndAgeDetails
Definition HyperDbgScriptImports.h:63
BOOLEAN ScriptEngineConvertFileToPdbFileAndGuidAndAgeDetails(const char *LocalFilePath, char *PdbFilePath, char *GuidAndAgeDetails, BOOLEAN Is32BitModule)
Convert file to pdb attributes for symbols.
Definition script-engine.c:252

◆ ScriptEngineConvertFileToPdbPathWrapper()

BOOLEAN ScriptEngineConvertFileToPdbPathWrapper ( const char * LocalFilePath,
char * ResultPath )

ScriptEngineConvertFileToPdbPath wrapper.

Parameters
LocalFilePath
ResultPath
Returns
BOOLEAN
175{
176 return ScriptEngineConvertFileToPdbPath(LocalFilePath, ResultPath);
177}
char * ResultPath
Definition HyperDbgScriptImports.h:61
BOOLEAN ScriptEngineConvertFileToPdbPath(const char *LocalFilePath, char *ResultPath)
Convert local file to pdb path.
Definition script-engine.c:173

◆ ScriptEngineConvertNameToAddressWrapper()

UINT64 ScriptEngineConvertNameToAddressWrapper ( const char * FunctionOrVariableName,
PBOOLEAN WasFound )

ScriptEngineConvertNameToAddress wrapper.

Parameters
FunctionName
WasFound
Returns
UINT64
54{
55 return ScriptEngineConvertNameToAddress(FunctionOrVariableName, WasFound);
56}
PBOOLEAN WasFound
Definition HyperDbgScriptImports.h:45
UINT64 ScriptEngineConvertNameToAddress(const char *FunctionOrVariableName, PBOOLEAN WasFound)
Converts name to address.
Definition script-engine.c:33

◆ ScriptEngineCreateSymbolTableForDisassemblerWrapper()

BOOLEAN ScriptEngineCreateSymbolTableForDisassemblerWrapper ( void * CallbackFunction)

ScriptEngineCreateSymbolTableForDisassembler wrapper.

Parameters
CallbackFunction
Returns
BOOLEAN
160{
161 return ScriptEngineCreateSymbolTableForDisassembler(CallbackFunction);
162}
BOOLEAN ScriptEngineCreateSymbolTableForDisassembler(void *CallbackFunction)
Create symbol table for disassembler.
Definition script-engine.c:157

◆ ScriptEngineEvalUInt64StyleExpressionWrapper()

UINT64 ScriptEngineEvalUInt64StyleExpressionWrapper ( const string & Expr,
PBOOLEAN HasError )

In the local debugging (VMI mode) environment, this function computes the expressions.

for example, if the user u ExAllocatePoolWithTag+0x10 this will evaluate the expr

Parameters
Expr
HasError
Returns
UINT64
800{
801 //
802 // In VMI-mode we'll form all registers as zero
803 //
804 GUEST_REGS GuestRegs = {0};
805
806 ScriptEngineEvalWrapper(&GuestRegs, Expr);
807
808 //
809 // Set the results and return the value
810 //
813}
VOID ScriptEngineEvalWrapper(PGUEST_REGS GuestRegs, string Expr)
Script engine evaluation wrapper.
Definition script-engine-wrapper.cpp:314
Definition BasicTypes.h:70

◆ ScriptEngineEvalWrapper()

VOID ScriptEngineEvalWrapper ( PGUEST_REGS GuestRegs,
string Expr )

Script engine evaluation wrapper.

Parameters
GuestRegs
Expr
Returns
VOID
316{
317 SCRIPT_ENGINE_VARIABLES_LIST VariablesList = {0};
318
319 //
320 // Allocate global variables holder
321 //
323 {
324 g_ScriptGlobalVariables = (UINT64 *)malloc(MAX_VAR_COUNT * sizeof(UINT64));
325
326 if (g_ScriptGlobalVariables == NULL)
327 {
328 ShowMessages("err, could not allocate memory for user-mode global variables");
329
330 return;
331 }
332
333 RtlZeroMemory(g_ScriptGlobalVariables, MAX_VAR_COUNT * sizeof(UINT64));
334 }
335
336 //
337 // Allocate local variables holder, actually in reality each core should
338 // have its own set of local variables but as we never run multi-core scripts
339 // in user-mode, thus, it's okay to just have one buffer for local variables
340 //
342 {
343 g_ScriptLocalVariables = (UINT64 *)malloc(MAX_VAR_COUNT * sizeof(UINT64));
344
345 if (g_ScriptLocalVariables == NULL)
346 {
348
349 ShowMessages("err, could not allocate memory for user-mode local variables");
350
351 return;
352 }
353
354 RtlZeroMemory(g_ScriptLocalVariables, MAX_VAR_COUNT * sizeof(UINT64));
355 }
356
357 //
358 // Allocate temp variables holder, actually in reality each core should
359 // have its own set of temp variables but as we never run multi-core scripts
360 // in user-mode, thus, it's okay to just have one buffer for temp variables
361 //
363 {
364 g_ScriptTempVariables = (UINT64 *)malloc(MAX_TEMP_COUNT * sizeof(UINT64));
365
366 if (g_ScriptTempVariables == NULL)
367 {
370
371 ShowMessages("err, could not allocate memory for user-mode temp variables");
372
373 return;
374 }
375
376 RtlZeroMemory(g_ScriptTempVariables, MAX_TEMP_COUNT * sizeof(UINT64));
377 }
378
379 //
380 // Run Parser
381 //
382 PSYMBOL_BUFFER CodeBuffer = (PSYMBOL_BUFFER)ScriptEngineParse((char *)Expr.c_str());
383
384#ifdef _SCRIPT_ENGINE_CODEEXEC_DBG_EN
385 //
386 // Print symbol buffer
387 //
388 PrintSymbolBuffer((PVOID)CodeBuffer);
389#endif
390
391 ACTION_BUFFER ActionBuffer = {0};
392 SYMBOL ErrorSymbol = {0};
393
394 //
395 // Making symbol buffer
396 //
397
398 PSYMBOL_BUFFER StackBuffer = (PSYMBOL_BUFFER)malloc(sizeof(SYMBOL_BUFFER));
399 if (StackBuffer == NULL)
400 {
403
404 ShowMessages("err, could not allocate memory for user-mode stack buffer");
405
406 return;
407 }
408 StackBuffer->Pointer = 0;
409 StackBuffer->Size = 0;
410 StackBuffer->Message = NULL;
411 StackBuffer->Head = (PSYMBOL)malloc(MAX_STACK_BUFFER_COUNT * sizeof(SYMBOL));
412 if (StackBuffer->Head == NULL)
413 {
416 free(StackBuffer);
417 ShowMessages("err, could not allocate memory for user-mode stack buffer");
418
419 return;
420 }
421 RtlZeroMemory(StackBuffer->Head, MAX_STACK_BUFFER_COUNT * sizeof(SYMBOL));
422
423 UINT64 StackIndx = 0;
424 UINT64 StackBaseIndx = 0;
425 UINT64 EXECUTENUMBER = 0;
426 UINT64 ReturnValue = 0;
427 RtlZeroMemory(g_ScriptTempVariables, MAX_TEMP_COUNT * sizeof(UINT64));
428 RtlZeroMemory(g_ScriptLocalVariables, MAX_VAR_COUNT * sizeof(UINT64));
429
430 if (CodeBuffer->Message == NULL)
431 {
432#ifdef _SCRIPT_ENGINE_CODEEXEC_DBG_EN
433 printf("\nScriptEngineExecute:\n");
434#endif
435 UINT64 i = 0;
436 for (; i < CodeBuffer->Pointer;)
437 {
438 //
439 // Fill the action buffer but as we're in user-mode here
440 // then there is nothing to fill
441 //
442 ActionBuffer.Context = NULL;
443 ActionBuffer.CurrentAction = NULL;
444 ActionBuffer.ImmediatelySendTheResults = FALSE;
445 ActionBuffer.Tag = NULL;
446
447 //
448 // Fill the variables list for this run
449 //
450 VariablesList.TempList = g_ScriptTempVariables;
453
454#ifdef _SCRIPT_ENGINE_CODEEXEC_DBG_EN
455 printf("Address = %lld, StackIndx = %lld, StackBaseIndx = %lld\n", i, StackIndx, StackBaseIndx);
456 PSYMBOL Operator = (PSYMBOL)((unsigned long long)CodeBuffer->Head +
457 (unsigned long long)(i * sizeof(SYMBOL)));
458 printf("Function = %s\n", FunctionNames[Operator->Value]);
459 printf("Stack Buffer:\n");
460 for (UINT64 j = 0; j < StackIndx; j++)
461 {
462 PSYMBOL StackSymbol = (PSYMBOL)((unsigned long long)StackBuffer->Head +
463 (unsigned long long)(j * sizeof(SYMBOL)));
464
465 printf("StackIndx = %lld, Value = %lld", j, StackSymbol->Value);
466
467 if (StackSymbol->Type == SYMBOL_RETURN_ADDRESS_TYPE)
468 {
469 printf(", Type = SYMBOL_RETURN_ADDRESS_TYPE");
470 }
471
472 if (j == StackBaseIndx)
473 {
474 printf(" <===== StackBaseIndx");
475 }
476 printf("\n");
477 }
478 printf("\n");
479#endif
480
481 //
482 // If has error, show error message and abort
483 //
484 if (ScriptEngineExecute(GuestRegs,
485 &ActionBuffer,
486 &VariablesList,
487 CodeBuffer,
488 &i,
489 StackBuffer,
490 &StackIndx,
491 &StackBaseIndx,
492 &ErrorSymbol,
493 &ReturnValue) == TRUE)
494 {
495 ShowMessages("err, ScriptEngineExecute, function = %s\n",
496 FunctionNames[ErrorSymbol.Value]);
499 break;
500 }
501 else if (StackIndx >= MAX_STACK_BUFFER_COUNT)
502 {
503 ShowMessages("err, stack buffer overflow\n");
506 break;
507 }
508 else if (EXECUTENUMBER >= MAX_EXECUTION_COUNT)
509 {
510 ShowMessages("err, exceeding the max execution count\n");
513 break;
514 }
515
516 EXECUTENUMBER++;
517 }
518#ifdef _SCRIPT_ENGINE_CODEEXEC_DBG_EN
519 printf("Address = %lld, StackIndx = %lld, StackBaseIndx = %lld\n", i, StackIndx, StackBaseIndx);
520#endif
521 }
522 else
523 {
524 ShowMessages("%s\n", CodeBuffer->Message);
525 }
526
527 RemoveSymbolBuffer(CodeBuffer);
528
529 return;
530}
#define MAX_VAR_COUNT
Definition Constants.h:574
#define MAX_EXECUTION_COUNT
Definition Constants.h:570
#define MAX_TEMP_COUNT
Definition Constants.h:566
#define MAX_STACK_BUFFER_COUNT
Definition Constants.h:568
struct SYMBOL * PSYMBOL
struct SYMBOL_BUFFER * PSYMBOL_BUFFER
#define SYMBOL_RETURN_ADDRESS_TYPE
Definition ScriptEngineCommonDefinitions.h:66
BOOL ScriptEngineExecute(PGUEST_REGS GuestRegs, ACTION_BUFFER *ActionDetail, SCRIPT_ENGINE_VARIABLES_LIST *VariablesList, SYMBOL_BUFFER *CodeBuffer, UINT64 *Indx, SYMBOL_BUFFER *StackBuffer, UINT64 *StackIndx, UINT64 *StackBaseIndx, SYMBOL *ErrorOperator, UINT64 *ReturnValue)
Execute the script buffer.
Definition ScriptEngineEval.c:288
VOID ShowMessages(const char *Fmt,...)
Show messages.
Definition libhyperdbg.cpp:96
UINT64 * g_ScriptGlobalVariables
Holder of script engines global variables.
Definition Global.h:23
UINT64 * g_ScriptLocalVariables
Holder of local variables for script engine.
Definition globals.h:336
UINT64 * g_ScriptTempVariables
Holder of temp variables for script engine.
Definition globals.h:342
PVOID ScriptEngineParse(char *str)
The entry point of script engine.
Definition script-engine.c:267
void RemoveSymbolBuffer(PVOID SymbolBuffer)
Frees the memory allocated by SymbolBuffer.
Definition script-engine.c:2725
List of different variables.
Definition BasicTypes.h:118
UINT64 * GlobalVariablesList
Definition BasicTypes.h:120
UINT64 * LocalVariablesList
Definition BasicTypes.h:121
UINT64 * TempList
Definition BasicTypes.h:119
Definition ScriptEngineCommonDefinitions.h:34
long long unsigned Context
Definition ScriptEngineCommonDefinitions.h:38
long long unsigned Tag
Definition ScriptEngineCommonDefinitions.h:35
long long unsigned CurrentAction
Definition ScriptEngineCommonDefinitions.h:36
char ImmediatelySendTheResults
Definition ScriptEngineCommonDefinitions.h:37
Definition ScriptEngineCommonDefinitions.h:21
char * Message
Definition ScriptEngineCommonDefinitions.h:25
unsigned int Pointer
Definition ScriptEngineCommonDefinitions.h:23
PSYMBOL Head
Definition ScriptEngineCommonDefinitions.h:22
unsigned int Size
Definition ScriptEngineCommonDefinitions.h:24
Definition ScriptEngineCommonDefinitions.h:6
long long unsigned Value
Definition ScriptEngineCommonDefinitions.h:10
long long unsigned Type
Definition ScriptEngineCommonDefinitions.h:7

◆ ScriptEngineFuncNumberOfOperands()

BOOLEAN ScriptEngineFuncNumberOfOperands ( UINT64 FuncType,
UINT32 * NumberOfGetOperands,
UINT32 * NumberOfSetOperands )

wrapper for getting operand count

Parameters
FuncType
NumberOfGetOperands
NumberOfSetOperands
Returns
BOOLEAN
875{
877}
UINT32 * NumberOfGetOperands
Definition HyperDbgScriptImports.h:33
UINT32 UINT32 * NumberOfSetOperands
Definition HyperDbgScriptImports.h:33
BOOLEAN FuncGetNumberOfOperands(UINT64 FuncType, UINT32 *NumberOfGetOperands, UINT32 *NumberOfSetOperands)
Script Engine get number of operands.
Definition script-engine.c:3356

◆ ScriptEngineGetDataTypeSizeWrapper()

BOOLEAN ScriptEngineGetDataTypeSizeWrapper ( CHAR * TypeName,
UINT64 * TypeSize )

ScriptEngineGetDataTypeSize wrapper.

Parameters
TypeName
TypeSize
Returns
BOOLEAN
147{
148 return ScriptEngineGetDataTypeSize(TypeName, TypeSize);
149}
UINT64 * TypeSize
Definition HyperDbgScriptImports.h:57
BOOLEAN ScriptEngineGetDataTypeSize(CHAR *TypeName, UINT64 *TypeSize)
Get size of a data type (structure)
Definition script-engine.c:142

◆ ScriptEngineGetFieldOffsetWrapper()

BOOLEAN ScriptEngineGetFieldOffsetWrapper ( CHAR * TypeName,
CHAR * FieldName,
UINT32 * FieldOffset )

ScriptEngineGetFieldOffset wrapper.

Parameters
TypeName
FieldName
FieldOffset
Returns
BOOLEAN
133{
135}
CHAR * FieldName
Definition HyperDbgScriptImports.h:55
CHAR UINT32 * FieldOffset
Definition HyperDbgScriptImports.h:55
BOOLEAN ScriptEngineGetFieldOffset(CHAR *TypeName, CHAR *FieldName, UINT32 *FieldOffset)
Get offset of a field from the structure.
Definition script-engine.c:126

◆ ScriptEngineLoadFileSymbolWrapper()

UINT32 ScriptEngineLoadFileSymbolWrapper ( UINT64 BaseAddress,
const char * PdbFileName,
const char * CustomModuleName )

ScriptEngineLoadFileSymbol wrapper.

Parameters
BaseAddress
FileName
Returns
UINT32
68{
70}
const char const char * CustomModuleName
Definition HyperDbgScriptImports.h:47
const char * PdbFileName
Definition HyperDbgScriptImports.h:47
UINT32 ScriptEngineLoadFileSymbol(UINT64 BaseAddress, const char *PdbFileName, const char *CustomModuleName)
Definition script-engine.c:53

◆ ScriptEngineParseWrapper()

PVOID ScriptEngineParseWrapper ( char * Expr,
BOOLEAN ShowErrorMessageIfAny )

ScriptEngineParse wrapper.

Parameters
Expr
ShowErrorMessageIfAny
Returns
PVOID
268{
269 PSYMBOL_BUFFER SymbolBuffer;
270 SymbolBuffer = (PSYMBOL_BUFFER)ScriptEngineParse(Expr);
271
272 //
273 // Check if there is an error or not
274 //
275 if (SymbolBuffer->Message == NULL)
276 {
277 return SymbolBuffer;
278 }
279 else
280 {
281 //
282 // Show error message and free the buffer
283 //
284 if (ShowErrorMessageIfAny)
285 {
286 ShowMessages("%s\n", SymbolBuffer->Message);
287 }
289 return NULL;
290 }
291}
VOID ScriptEngineWrapperRemoveSymbolBuffer(PVOID SymbolBuffer)
wrapper for removing symbol buffer
Definition script-engine-wrapper.cpp:860

◆ ScriptEngineSearchSymbolForMaskWrapper()

UINT32 ScriptEngineSearchSymbolForMaskWrapper ( const char * SearchMask)

ScriptEngineSearchSymbolForMask wrapper.

Parameters
SearchMask
Returns
UINT32
117{
118 return ScriptEngineSearchSymbolForMask(SearchMask);
119}
UINT32 ScriptEngineSearchSymbolForMask(const char *SearchMask)
Search for a special mask.
Definition script-engine.c:109

◆ ScriptEngineSetTextMessageCallbackWrapper()

VOID ScriptEngineSetTextMessageCallbackWrapper ( PVOID Handler)

ScriptEngineSetTextMessageCallback wrapper.

Parameters
Handler
Returns
VOID
81{
83}
VOID ScriptEngineSetTextMessageCallback(PVOID Handler)
Set the message handler as an alternative to printf.
Definition script-engine.c:68

◆ ScriptEngineShowDataBasedOnSymbolTypesWrapper()

BOOLEAN ScriptEngineShowDataBasedOnSymbolTypesWrapper ( const char * TypeName,
UINT64 Address,
BOOLEAN IsStruct,
PVOID BufferAddress,
const char * AdditionalParameters )

ScriptEngineShowDataBasedOnSymbolTypes wrapper.

Parameters
TypeName
Address
IsStruct
BufferAddress
AdditionalParameters
Returns
BOOLEAN
218{
220}
UINT64 BOOLEAN IsStruct
Definition HyperDbgScriptImports.h:67
UINT64 Address
Definition HyperDbgScriptImports.h:67
UINT64 BOOLEAN PVOID const char * AdditionalParameters
Definition HyperDbgScriptImports.h:67
UINT64 BOOLEAN PVOID BufferAddress
Definition HyperDbgScriptImports.h:67
BOOLEAN ScriptEngineShowDataBasedOnSymbolTypes(const char *TypeName, UINT64 Address, BOOLEAN IsStruct, PVOID BufferAddress, const char *AdditionalParameters)
Show data based on symbol types.
Definition script-engine.c:215

◆ ScriptEngineSymbolAbortLoadingWrapper()

VOID ScriptEngineSymbolAbortLoadingWrapper ( )

SymbolAbortLoading wrapper.

Returns
VOID
230{
232}
VOID ScriptEngineSymbolAbortLoading()
Cancel loading.
Definition script-engine.c:233

◆ ScriptEngineSymbolInitLoadWrapper()

BOOLEAN ScriptEngineSymbolInitLoadWrapper ( PMODULE_SYMBOL_DETAIL BufferToStoreDetails,
UINT32 StoredLength,
BOOLEAN DownloadIfAvailable,
const char * SymbolPath,
BOOLEAN IsSilentLoad )

ScriptEngineSymbolInitLoad wrapper.

Parameters
ScriptEngineSymbolInitLoad
StoredLength
DownloadIfAvailable
SymbolPath
IsSilentLoad
Returns
BOOLEAN
196{
198}
UINT32 BOOLEAN const char * SymbolPath
Definition HyperDbgScriptImports.h:65
UINT32 BOOLEAN const char BOOLEAN IsSilentLoad
Definition HyperDbgScriptImports.h:65
UINT32 StoredLength
Definition HyperDbgScriptImports.h:65
UINT32 BOOLEAN DownloadIfAvailable
Definition HyperDbgScriptImports.h:65
BOOLEAN ScriptEngineSymbolInitLoad(PVOID BufferToStoreDetails, UINT32 StoredLength, BOOLEAN DownloadIfAvailable, const char *SymbolPath, BOOLEAN IsSilentLoad)
Initial load of the symbols.
Definition script-engine.c:192

◆ ScriptEngineUnloadAllSymbolsWrapper()

UINT32 ScriptEngineUnloadAllSymbolsWrapper ( )

ScriptEngineUnloadAllSymbols wrapper.

Returns
UINT32
92{
94}
UINT32 ScriptEngineUnloadAllSymbols()
Unload all the previously loaded symbols.
Definition script-engine.c:79

◆ ScriptEngineUnloadModuleSymbolWrapper()

UINT32 ScriptEngineUnloadModuleSymbolWrapper ( char * ModuleName)

ScriptEngineUnloadModuleSymbol wrapper.

Parameters
ModuleName
Returns
UINT32
104{
105 return ScriptEngineUnloadModuleSymbol(ModuleName);
106}
UINT32 ScriptEngineUnloadModuleSymbol(char *ModuleName)
Unload a special pdb.
Definition script-engine.c:94

◆ ScriptEngineWrapperGetHead()

UINT64 ScriptEngineWrapperGetHead ( PVOID SymbolBuffer)

wrapper for getting head

Parameters
SymbolBuffer
Returns
UINT64
823{
824 return (UINT64)((PSYMBOL_BUFFER)SymbolBuffer)->Head;
825}

◆ ScriptEngineWrapperGetPointer()

UINT32 ScriptEngineWrapperGetPointer ( PVOID SymbolBuffer)

wrapper for getting pointer

Parameters
SymbolBuffer
Returns
UINT32
849{
850 return (UINT32)((PSYMBOL_BUFFER)SymbolBuffer)->Pointer;
851}

◆ ScriptEngineWrapperGetSize()

UINT32 ScriptEngineWrapperGetSize ( PVOID SymbolBuffer)

wrapper for getting size

Parameters
SymbolBuffer
Returns
UINT32
835{
836 UINT32 Size =
837 (UINT32)((PSYMBOL_BUFFER)SymbolBuffer)->Pointer * sizeof(SYMBOL);
838 return Size;
839}

◆ ScriptEngineWrapperRemoveSymbolBuffer()

VOID ScriptEngineWrapperRemoveSymbolBuffer ( PVOID SymbolBuffer)

wrapper for removing symbol buffer

Parameters
SymbolBuffer
Returns
UINT32
861{
862 RemoveSymbolBuffer((PSYMBOL_BUFFER)SymbolBuffer);
863}

◆ ScriptEngineWrapperTestParser()

VOID ScriptEngineWrapperTestParser ( const string & Expr)

test parser

Parameters
Expr
Returns
VOID
720{
721 ALLOCATED_MEMORY_FOR_SCRIPT_ENGINE_CASTING AllocationsForCastings = {0};
722
723 typedef struct _TEST_STRUCT
724 {
725 UINT64 Var1;
726 UINT64 Var2;
727 UINT64 Var3;
728 UINT64 Var4;
729 } TEST_STRUCT, *PTEST_STRUCT;
730
731 PTEST_STRUCT TestStruct = (PTEST_STRUCT)malloc(sizeof(TEST_STRUCT));
732
733 if (TestStruct == NULL)
734 {
735 return;
736 }
737
738 RtlZeroMemory(TestStruct, sizeof(TEST_STRUCT));
739
740 TestStruct->Var1 = 0x41414141;
741 TestStruct->Var3 = 0x4242424242424242;
742
743 GUEST_REGS GuestRegs = {0};
744
745 char test[] = "Hello world !";
746 wchar_t testw[] =
747 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 "
748 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";
749
750 char * RspReg = (char *)malloc(0x100);
751
752 if (RspReg == NULL)
753 {
754 ShowMessages("err, unable to allocate stack for script engine tests");
755 free(TestStruct);
756 return;
757 }
758
759 memcpy(RspReg, testw, sizeof(testw));
760
761 GuestRegs.rax = 0x1;
762 GuestRegs.rcx = (UINT64)AllocateStructForCasting(&AllocationsForCastings); // TestStruct
763 GuestRegs.rdx = 0x3;
764 GuestRegs.rbx = 0x4;
765 GuestRegs.rsp = (UINT64)RspReg + 0x50;
766 GuestRegs.rbp = 0x6;
767 GuestRegs.rsi = 0x7;
768 GuestRegs.rdi = 0x8;
769 GuestRegs.r8 = 0x9;
770 GuestRegs.r9 = 0xa;
771 GuestRegs.r10 = 0xb;
772 GuestRegs.r11 = 0xc;
773 GuestRegs.r12 = 0xd;
774 GuestRegs.r13 = 0xe;
775 GuestRegs.r14 = (UINT64)testw;
776 GuestRegs.r15 = (UINT64)test;
777
778 ScriptEngineEvalWrapper(&GuestRegs, Expr);
779
780 free(RspReg);
781 free(TestStruct);
782 free(AllocationsForCastings.Buff1);
783 free(AllocationsForCastings.Buff2);
784 free(AllocationsForCastings.Buff3);
785 free(AllocationsForCastings.Buff4);
786 free(AllocationsForCastings.Buff5);
787 free(AllocationsForCastings.Buff6);
788}
PVOID AllocateStructForCasting(PALLOCATED_MEMORY_FOR_SCRIPT_ENGINE_CASTING AllocationsForCastings)
allocate memory and build structure for casting
Definition script-engine-wrapper.cpp:576
Definition script-engine-wrapper.cpp:30
UINT64 rsp
Definition BasicTypes.h:79
UINT64 r14
Definition BasicTypes.h:89
UINT64 r15
Definition BasicTypes.h:90
UINT64 rdi
Definition BasicTypes.h:82
UINT64 rax
Definition BasicTypes.h:75
UINT64 r12
Definition BasicTypes.h:87
UINT64 r13
Definition BasicTypes.h:88
UINT64 r9
Definition BasicTypes.h:84
UINT64 r8
Definition BasicTypes.h:83
UINT64 rbp
Definition BasicTypes.h:80
UINT64 rbx
Definition BasicTypes.h:78
UINT64 r10
Definition BasicTypes.h:85
UINT64 rcx
Definition BasicTypes.h:76
UINT64 rsi
Definition BasicTypes.h:81
UINT64 r11
Definition BasicTypes.h:86
UINT64 rdx
Definition BasicTypes.h:77

Variable Documentation

◆ g_CurrentExprEvalResult

UINT64 g_CurrentExprEvalResult
extern

global variable to save the result of script-engine statement tests

◆ g_CurrentExprEvalResultHasError

BOOLEAN g_CurrentExprEvalResultHasError
extern

global variable to detect if there was an error in the result of script-engine statement tests

◆ g_ScriptGlobalVariables

UINT64* g_ScriptGlobalVariables
extern

Holder of script engines global variables.

Holder of script engines global variables.

◆ g_ScriptLocalVariables

UINT64* g_ScriptLocalVariables
extern

Holder of local variables for script engine.

◆ g_ScriptTempVariables

UINT64* g_ScriptTempVariables
extern

Holder of temp variables for script engine.