Skip to content

Commit

Permalink
Add hexadecimal escape sequence as wstring parameter for wstring func…
Browse files Browse the repository at this point in the history
…tion in script engine
  • Loading branch information
xmaple555 committed Nov 23, 2023
1 parent 6619529 commit e6dbc3f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 99 deletions.
24 changes: 21 additions & 3 deletions hyperdbg/script-engine/code/common.c
Expand Up @@ -230,15 +230,15 @@ AppendWchar(PTOKEN Token, wchar_t c)
//
// Free Old buffer and update the pointer
//
wcsncpy(NewValue, Token->Value, Token->Len / 2);
memcpy(NewValue, Token->Value, Token->Len);
free(Token->Value);
Token->Value = NewValue;
}

//
// Append the new charcter to the string
// Append the new charcter to the wstring
//
wcsncat(Token->Value, &c, 1);
*((wchar_t *)(Token->Value) + Token->Len/2) = c;
Token->Len += 2;
}

Expand Down Expand Up @@ -1290,3 +1290,21 @@ BinaryToInt(char * str)
}
return Acc;
}

/**
* @brief Rotate a character array to the left by one time
*
* @param str
*/
void
RotateLeftStringOnce(char * str)
{
int length = strlen(str);
char temp = str[0];
for (int i = 0; i < (length - 1); i++)
{
str[i] = str[i + 1];
}
str[length - 1] = temp;

}
65 changes: 47 additions & 18 deletions hyperdbg/script-engine/code/scanner.c
Expand Up @@ -50,31 +50,31 @@ GetToken(char * c, char * str)
}
else if (*c == 'x')
{
*c = sgetc(str);
if (('0' <= *c && *c <= '9') || ('a' <= *c && *c <= 'f') || ('A' <= *c && *c <= 'F'))
char ByteString[] = "000";
int len = strlen(ByteString);
int i = 0;
for (; i < len; i++)
{
char byte[3] = {NULL};
char NextCharacter = *(str + InputIdx);
if (('0' <= NextCharacter && NextCharacter <= '9') || ('a' <= NextCharacter && NextCharacter <= 'f') || ('A' <= NextCharacter && NextCharacter <= 'F'))
{
InputIdx++;
byte[0] = *c;
byte[1] = NextCharacter;
}
else
{
byte[0] = '0';
byte[1] = *c;
}
char num = strtol(byte, NULL, 16);
AppendByte(Token, num);
*c = sgetc(str);
if (!IsHex(*c))
break;

RotateLeftStringOnce(ByteString);
ByteString[len-1] = *c;
}
else

if (i == 0 || i == 3)
{
Token->Type = UNKNOWN;
*c = sgetc(str);
return Token;
}
else
{
InputIdx--;
char num = strtol(ByteString, NULL, 16);
AppendByte(Token, num);
}
}
else if (*c == '"')
{
Expand Down Expand Up @@ -577,6 +577,34 @@ GetToken(char * c, char * str)
AppendWchar(Token, L'\t');
continue;
}
else if (*c == 'x')
{
char ByteString[] = "00000";
int len = strlen(ByteString);
int i = 0;
for (; i < len; i++)
{
*c = sgetc(str);
if (!IsHex(*c))
break;

RotateLeftStringOnce(ByteString);
ByteString[len - 1] = *c;
}

if (i == 0 || i == 5)
{
Token->Type = UNKNOWN;
*c = sgetc(str);
return Token;
}
else
{
InputIdx--;
wchar_t num = strtol(ByteString, NULL, 16);
AppendWchar(Token, num);
}
}
else if (*c == '"')
{
AppendWchar(Token, L'"');
Expand All @@ -599,6 +627,7 @@ GetToken(char * c, char * str)
}
} while (1);

Token->Len += 2;
Token->Type = WSTRING;
*c = sgetc(str);
return Token;
Expand Down
80 changes: 11 additions & 69 deletions hyperdbg/script-engine/code/script-engine.c
Expand Up @@ -1930,42 +1930,30 @@ NewStringSymbol(PTOKEN Token)
* @return PSYMBOL
*/
PSYMBOL
NewWstringSymbol(wchar_t * value)
NewWstringSymbol(PTOKEN Token)
{
PSYMBOL Symbol;
int BufferSize = (2 * sizeof(unsigned long long) + 2 * (wcslen(value))) / sizeof(SYMBOL) + 1;
int BufferSize = (2 * sizeof(unsigned long long) + Token->Len) / sizeof(SYMBOL) + 1;
Symbol = (unsigned long long)malloc(BufferSize * sizeof(SYMBOL));
wcscpy(&Symbol->Value, value);
memcpy(&Symbol->Value, Token->Value, Token->Len);
SetType(&Symbol->Type, SYMBOL_WSTRING_TYPE);
Symbol->Len = Token->Len;
return Symbol;
}

/**
* @brief Returns the number of SYMBOL objects (16 bytes) allocated by string sybmol
* @brief Returns the number of SYMBOL objects (24 bytes) allocated by string or wstring sybmol
*
* @param Symbol
* @return unsigned int
*/
unsigned int
GetStringSymbolSize(PSYMBOL Symbol)
GetSymbolHeapSize(PSYMBOL Symbol)
{
int Temp = (2 * sizeof(unsigned long long) + Symbol->Len) / sizeof(SYMBOL) + 1;
return Temp;
}

/**
* @brief Returns the number of SYMBOL objects (16 bytes) allocated by wstring sybmol
*
* @param Symbol
* @return unsigned int
*/
unsigned int
GetWstringSymbolSize(PSYMBOL Symbol)
{
int Temp = (2 * sizeof(unsigned long long) + 2 * (wcslen(&Symbol->Value))) / sizeof(SYMBOL) + 1;
return Temp;
}

/**
* @brief Frees the memory allocate by this Symbol
*
Expand Down Expand Up @@ -2060,7 +2048,7 @@ ToSymbol(PTOKEN Token, PSCRIPT_ENGINE_ERROR_TYPE Error)

case WSTRING:
RemoveSymbol(&Symbol);
return NewWstringSymbol(Token->Value);
return NewWstringSymbol(Token);

default:
*Error = SCRIPT_ENGINE_ERROR_UNRESOLVED_VARIABLE;
Expand Down Expand Up @@ -2117,12 +2105,12 @@ PushSymbol(PSYMBOL_BUFFER SymbolBuffer, const PSYMBOL Symbol)
uintptr_t Pointer = (uintptr_t)SymbolBuffer->Pointer;
PSYMBOL WriteAddr = (PSYMBOL)(Head + Pointer * sizeof(SYMBOL));

if (Symbol->Type == SYMBOL_STRING_TYPE)
if (Symbol->Type == SYMBOL_STRING_TYPE || Symbol->Type == SYMBOL_WSTRING_TYPE)
{
//
// Update Pointer
//
SymbolBuffer->Pointer += GetStringSymbolSize(Symbol);
SymbolBuffer->Pointer += GetSymbolHeapSize(Symbol);

//
// Handle Overflow
Expand Down Expand Up @@ -2164,52 +2152,6 @@ PushSymbol(PSYMBOL_BUFFER SymbolBuffer, const PSYMBOL Symbol)
WriteAddr->Len = Symbol->Len;
memcpy((char *)&WriteAddr->Value, (char *)&Symbol->Value, Symbol->Len);
}
else if (Symbol->Type == SYMBOL_WSTRING_TYPE)
{
//
// Update Pointer
//
SymbolBuffer->Pointer += GetWstringSymbolSize(Symbol);

//
// Handle Overflow
//
if (SymbolBuffer->Pointer >= SymbolBuffer->Size - 1)
{
//
// Calculate new size for the symbol B
//
int NewSize = SymbolBuffer->Size;
do
{
NewSize *= 2;
} while (NewSize <= SymbolBuffer->Pointer);

//
// Allocate a new buffer for string list with doubled length
//
PSYMBOL NewHead = (PSYMBOL)malloc(NewSize * sizeof(SYMBOL));

//
// Copy old buffer to new buffer
//
memcpy(NewHead, SymbolBuffer->Head, SymbolBuffer->Size * sizeof(SYMBOL));

//
// Free old buffer
//
free(SymbolBuffer->Head);

//
// Upadate Head and size of SymbolBuffer
//
SymbolBuffer->Size = NewSize;
SymbolBuffer->Head = NewHead;
}
WriteAddr = (PSYMBOL)((uintptr_t)SymbolBuffer->Head + (uintptr_t)Pointer * (uintptr_t)sizeof(SYMBOL));
WriteAddr->Type = Symbol->Type;
wcscpy((wchar_t *)&WriteAddr->Value, (wchar_t *)&Symbol->Value);
}
else
{
//
Expand Down Expand Up @@ -2268,9 +2210,9 @@ PrintSymbolBuffer(const PSYMBOL_BUFFER SymbolBuffer)

printf("%8x:", i);
PrintSymbol(Symbol);
if (Symbol->Type == SYMBOL_STRING_TYPE)
if (Symbol->Type == SYMBOL_STRING_TYPE || Symbol->Type == SYMBOL_WSTRING_TYPE)
{
int temp = GetStringSymbolSize(Symbol);
int temp = GetSymbolHeapSize(Symbol);
i += temp;
}
else
Expand Down
5 changes: 4 additions & 1 deletion hyperdbg/script-engine/header/common.h
Expand Up @@ -191,7 +191,10 @@ OctalToInt(char * str);
unsigned long long int
BinaryToInt(char * str);

////////////////////////////////////////////////////
void
RotateLeftStringOnce(char * str);

////////////////////////////////////////////////////
// Semantic Rule Related Functions //
////////////////////////////////////////////////////

Expand Down
7 changes: 2 additions & 5 deletions hyperdbg/script-engine/header/script-engine.h
Expand Up @@ -68,13 +68,10 @@ PSYMBOL
NewStringSymbol(PTOKEN Token);

PSYMBOL
NewWstringSymbol(wchar_t * value);
NewWstringSymbol(PTOKEN Token);

unsigned int
GetStringSymbolSize(PSYMBOL Symbol);

unsigned int
GetWstringSymbolSize(PSYMBOL Symbol);
GetSymbolHeapSize(PSYMBOL Symbol);

void
RemoveSymbol(PSYMBOL * Symbol);
Expand Down
6 changes: 3 additions & 3 deletions hyperdbg/script-eval/code/ScriptEngineEval.c
Expand Up @@ -1343,7 +1343,7 @@ ScriptEngineExecute(PGUEST_REGS GuestRegs,
if (Src0->Type == SYMBOL_WSTRING_TYPE)
{
*Indx =
*Indx + ((2 * sizeof(unsigned long long) + 2 * wcslen((wchar_t *)&Src0->Value)) /
*Indx + ((2 * sizeof(unsigned long long) + Src0->Len) /
sizeof(SYMBOL));
SrcVal0 = (UINT64)&Src0->Value;
}
Expand Down Expand Up @@ -1709,7 +1709,7 @@ ScriptEngineExecute(PGUEST_REGS GuestRegs,
if (Src0->Type == SYMBOL_WSTRING_TYPE)
{
*Indx =
*Indx + ((2 * sizeof(unsigned long long) + 2 * wcslen((wchar_t *)&Src0->Value)) /
*Indx + ((2 * sizeof(unsigned long long) + Src0->Len) /
sizeof(SYMBOL));
SrcVal0 = (UINT64)&Src0->Value;
}
Expand All @@ -1727,7 +1727,7 @@ ScriptEngineExecute(PGUEST_REGS GuestRegs,
if (Src1->Type == SYMBOL_WSTRING_TYPE)
{
*Indx =
*Indx + ((2 * sizeof(unsigned long long) + 2 * wcslen((wchar_t *)&Src1->Value)) /
*Indx + ((2 * sizeof(unsigned long long) + Src1->Len) /
sizeof(SYMBOL));
SrcVal1 = (UINT64)&Src1->Value;
}
Expand Down

0 comments on commit e6dbc3f

Please sign in to comment.