Skip to content

Commit

Permalink
Merge pull request #320 from xmaple555/dev
Browse files Browse the repository at this point in the history
Add hexadecimal escape sequence as string parameter for string function in script engine
  • Loading branch information
SinaKarvandi committed Nov 23, 2023
2 parents c8b06cb + 60fbec6 commit 6619529
Show file tree
Hide file tree
Showing 9 changed files with 565 additions and 531 deletions.
6 changes: 3 additions & 3 deletions hyperdbg/script-engine/code/common.c
Expand Up @@ -179,7 +179,7 @@ PrintToken(PTOKEN Token)
* @param char
*/
void
Append(PTOKEN Token, char c)
AppendByte(PTOKEN Token, char c)
{
//
// Check overflow of the string
Expand All @@ -195,15 +195,15 @@ Append(PTOKEN Token, char c)
//
// Free Old buffer and update the pointer
//
strncpy(NewValue, Token->Value, Token->Len);
memcpy(NewValue, Token->Value, Token->Len);
free(Token->Value);
Token->Value = NewValue;
}

//
// Append the new charcter to the string
//
strncat(Token->Value, &c, 1);
Token->Value[Token->Len] = c;
Token->Len++;
}

Expand Down
980 changes: 490 additions & 490 deletions hyperdbg/script-engine/code/parse-table.c

Large diffs are not rendered by default.

65 changes: 47 additions & 18 deletions hyperdbg/script-engine/code/scanner.c
Expand Up @@ -35,22 +35,50 @@ GetToken(char * c, char * str)
*c = sgetc(str);
if (*c == 'n')
{
Append(Token, '\n');
AppendByte(Token, '\n');
continue;
}
if (*c == '\\')
{
Append(Token, '\\');
AppendByte(Token, '\\');
continue;
}
else if (*c == 't')
{
Append(Token, '\t');
AppendByte(Token, '\t');
continue;
}
else if (*c == 'x')
{
*c = sgetc(str);
if (('0' <= *c && *c <= '9') || ('a' <= *c && *c <= 'f') || ('A' <= *c && *c <= 'F'))
{
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);
}
else
{
Token->Type = UNKNOWN;
*c = sgetc(str);
return Token;
}
}
else if (*c == '"')
{
Append(Token, '"');
AppendByte(Token, '"');
continue;
}
else
Expand All @@ -66,10 +94,11 @@ GetToken(char * c, char * str)
}
else
{
Append(Token, *c);
AppendByte(Token, *c);
}
} while (1);

Token->Len++;
Token->Type = STRING;
*c = sgetc(str);
return Token;
Expand Down Expand Up @@ -349,7 +378,7 @@ GetToken(char * c, char * str)
{
while (IsLetter(*c) || IsDecimal(*c))
{
Append(Token, *c);
AppendByte(Token, *c);
*c = sgetc(str);
}
if (RegisterToInt(Token->Value) != INVALID)
Expand All @@ -372,7 +401,7 @@ GetToken(char * c, char * str)
//
while (IsLetter(*c) || IsDecimal(*c) || *c == '_')
{
Append(Token, *c);
AppendByte(Token, *c);
*c = sgetc(str);
}
if (PseudoRegToInt(Token->Value) != INVALID)
Expand All @@ -388,13 +417,13 @@ GetToken(char * c, char * str)
}

case '.':
Append(Token, *c);
AppendByte(Token, *c);
*c = sgetc(str);
if (IsLetter(*c) || IsHex(*c) || (*c == '_') || (*c == '!'))
{
do
{
Append(Token, *c);
AppendByte(Token, *c);
*c = sgetc(str);
} while (IsLetter(*c) || IsHex(*c) || (*c == '_') || (*c == '!'));

Expand Down Expand Up @@ -461,7 +490,7 @@ GetToken(char * c, char * str)
while (IsHex(*c) || *c == '`')
{
if (*c != '`')
Append(Token, *c);
AppendByte(Token, *c);
*c = sgetc(str);
}
Token->Type = HEX;
Expand All @@ -473,7 +502,7 @@ GetToken(char * c, char * str)
while (IsOctal(*c) || *c == '`')
{
if (*c != '`')
Append(Token, *c);
AppendByte(Token, *c);
*c = sgetc(str);
}
Token->Type = OCTAL;
Expand All @@ -485,7 +514,7 @@ GetToken(char * c, char * str)
while (IsDecimal(*c) || *c == '`')
{
if (*c != '`')
Append(Token, *c);
AppendByte(Token, *c);
*c = sgetc(str);
}
Token->Type = DECIMAL;
Expand All @@ -497,7 +526,7 @@ GetToken(char * c, char * str)
while (IsBinary(*c) || *c == '`')
{
if (*c != '`')
Append(Token, *c);
AppendByte(Token, *c);
*c = sgetc(str);
}
Token->Type = BINARY;
Expand All @@ -509,7 +538,7 @@ GetToken(char * c, char * str)
do
{
if (*c != '`')
Append(Token, *c);
AppendByte(Token, *c);
*c = sgetc(str);
} while (IsHex(*c) || *c == '`');
Token->Type = HEX;
Expand Down Expand Up @@ -582,7 +611,7 @@ GetToken(char * c, char * str)
do
{
if (*c != '`')
Append(Token, *c);
AppendByte(Token, *c);
*c = sgetc(str);
} while (IsHex(*c) || *c == '`');
Token->Type = HEX;
Expand All @@ -594,7 +623,7 @@ GetToken(char * c, char * str)
do
{
if (*c != '`')
Append(Token, *c);
AppendByte(Token, *c);

*c = sgetc(str);
if (IsHex(*c) || *c == '`')
Expand All @@ -616,7 +645,7 @@ GetToken(char * c, char * str)
do
{
if (*c != '`')
Append(Token, *c);
AppendByte(Token, *c);
*c = sgetc(str);
} while (IsLetter(*c) || IsHex(*c) || (*c == '_') || (*c == '!'));
if (IsKeyword(Token->Value))
Expand Down Expand Up @@ -727,7 +756,7 @@ GetToken(char * c, char * str)
do
{
if (*c != '`')
Append(Token, *c);
AppendByte(Token, *c);
*c = sgetc(str);
} while (IsLetter(*c) || IsHex(*c) || (*c == '_') || (*c == '!'));
if (IsKeyword(Token->Value))
Expand Down
21 changes: 12 additions & 9 deletions hyperdbg/script-engine/code/script-engine.c
Expand Up @@ -1900,6 +1900,7 @@ NewSymbol(void)
PSYMBOL Symbol;
Symbol = (PSYMBOL)malloc(sizeof(SYMBOL));
Symbol->Value = 0;
Symbol->Len = 0;
Symbol->Type = 0;
return Symbol;
}
Expand All @@ -1911,13 +1912,14 @@ NewSymbol(void)
* @return PSYMBOL
*/
PSYMBOL
NewStringSymbol(char * value)
NewStringSymbol(PTOKEN Token)
{
PSYMBOL Symbol;
int BufferSize = (sizeof(unsigned long long) + (strlen(value))) / sizeof(SYMBOL) + 1;
Symbol = (unsigned long long)malloc(BufferSize * sizeof(SYMBOL));
strcpy(&Symbol->Value, value);
int BufferSize = (2 * sizeof(unsigned long long) + Token->Len) / sizeof(SYMBOL) + 1;
Symbol = (unsigned long long)calloc(sizeof(SYMBOL), BufferSize);
memcpy(&Symbol->Value, Token->Value, Token->Len);
SetType(&Symbol->Type, SYMBOL_STRING_TYPE);
Symbol->Len = Token->Len;
return Symbol;
}

Expand All @@ -1931,7 +1933,7 @@ PSYMBOL
NewWstringSymbol(wchar_t * value)
{
PSYMBOL Symbol;
int BufferSize = (sizeof(unsigned long long) + 2 * (wcslen(value))) / sizeof(SYMBOL) + 1;
int BufferSize = (2 * sizeof(unsigned long long) + 2 * (wcslen(value))) / sizeof(SYMBOL) + 1;
Symbol = (unsigned long long)malloc(BufferSize * sizeof(SYMBOL));
wcscpy(&Symbol->Value, value);
SetType(&Symbol->Type, SYMBOL_WSTRING_TYPE);
Expand All @@ -1947,7 +1949,7 @@ NewWstringSymbol(wchar_t * value)
unsigned int
GetStringSymbolSize(PSYMBOL Symbol)
{
int Temp = (sizeof(unsigned long long) + (strlen(&Symbol->Value))) / sizeof(SYMBOL) + 1;
int Temp = (2 * sizeof(unsigned long long) + Symbol->Len) / sizeof(SYMBOL) + 1;
return Temp;
}

Expand All @@ -1960,7 +1962,7 @@ GetStringSymbolSize(PSYMBOL Symbol)
unsigned int
GetWstringSymbolSize(PSYMBOL Symbol)
{
int Temp = (sizeof(unsigned long long) + 2 * (wcslen(&Symbol->Value))) / sizeof(SYMBOL) + 1;
int Temp = (2 * sizeof(unsigned long long) + 2 * (wcslen(&Symbol->Value))) / sizeof(SYMBOL) + 1;
return Temp;
}

Expand Down Expand Up @@ -2054,7 +2056,7 @@ ToSymbol(PTOKEN Token, PSCRIPT_ENGINE_ERROR_TYPE Error)

case STRING:
RemoveSymbol(&Symbol);
return NewStringSymbol(Token->Value);
return NewStringSymbol(Token);

case WSTRING:
RemoveSymbol(&Symbol);
Expand Down Expand Up @@ -2159,7 +2161,8 @@ PushSymbol(PSYMBOL_BUFFER SymbolBuffer, const PSYMBOL Symbol)
}
WriteAddr = (PSYMBOL)((uintptr_t)SymbolBuffer->Head + (uintptr_t)Pointer * (uintptr_t)sizeof(SYMBOL));
WriteAddr->Type = Symbol->Type;
strcpy((char *)&WriteAddr->Value, (char *)&Symbol->Value);
WriteAddr->Len = Symbol->Len;
memcpy((char *)&WriteAddr->Value, (char *)&Symbol->Value, Symbol->Len);
}
else if (Symbol->Type == SYMBOL_WSTRING_TYPE)
{
Expand Down
2 changes: 1 addition & 1 deletion hyperdbg/script-engine/header/common.h
Expand Up @@ -97,7 +97,7 @@ void
PrintToken(PTOKEN Token);

void
Append(PTOKEN Token, char c);
AppendByte(PTOKEN Token, char c);

void
AppendWchar(PTOKEN Token, wchar_t c);
Expand Down
2 changes: 1 addition & 1 deletion hyperdbg/script-engine/header/script-engine.h
Expand Up @@ -65,7 +65,7 @@ PSYMBOL
NewSymbol(void);

PSYMBOL
NewStringSymbol(char * value);
NewStringSymbol(PTOKEN Token);

PSYMBOL
NewWstringSymbol(wchar_t * value);
Expand Down
1 change: 1 addition & 0 deletions hyperdbg/script-engine/python/generator.py
Expand Up @@ -40,6 +40,7 @@ def WriteCommonHeader(self):
#define SCRIPT_ENGINE_COMMON_DEFINITIONS_H
typedef struct SYMBOL {
long long unsigned Type;
long long unsigned Len;
long long unsigned Value;
} SYMBOL, * PSYMBOL;
typedef struct SYMBOL_BUFFER {
Expand Down
18 changes: 9 additions & 9 deletions hyperdbg/script-eval/code/ScriptEngineEval.c
Expand Up @@ -1275,7 +1275,7 @@ ScriptEngineExecute(PGUEST_REGS GuestRegs,
if (Src0->Type == SYMBOL_STRING_TYPE)
{
*Indx =
*Indx + ((sizeof(unsigned long long) + strlen((char *)&Src0->Value)) /
*Indx + ((2 * sizeof(unsigned long long) + Src0->Len) /
sizeof(SYMBOL));
SrcVal0 = (UINT64)&Src0->Value;
}
Expand Down Expand Up @@ -1343,7 +1343,7 @@ ScriptEngineExecute(PGUEST_REGS GuestRegs,
if (Src0->Type == SYMBOL_WSTRING_TYPE)
{
*Indx =
*Indx + ((sizeof(unsigned long long) + 2 * wcslen((wchar_t *)&Src0->Value)) /
*Indx + ((2 * sizeof(unsigned long long) + 2 * wcslen((wchar_t *)&Src0->Value)) /
sizeof(SYMBOL));
SrcVal0 = (UINT64)&Src0->Value;
}
Expand Down Expand Up @@ -1660,7 +1660,7 @@ ScriptEngineExecute(PGUEST_REGS GuestRegs,
if (Src0->Type == SYMBOL_STRING_TYPE)
{
*Indx =
*Indx + ((sizeof(unsigned long long) + strlen((char *)&Src0->Value)) /
*Indx + ((2 * sizeof(unsigned long long) + Src0->Len) /
sizeof(SYMBOL));
SrcVal0 = (UINT64)&Src0->Value;
}
Expand All @@ -1678,7 +1678,7 @@ ScriptEngineExecute(PGUEST_REGS GuestRegs,
if (Src1->Type == SYMBOL_STRING_TYPE)
{
*Indx =
*Indx + ((sizeof(unsigned long long) + strlen((char *)&Src1->Value)) /
*Indx + ((2 * sizeof(unsigned long long) + Src1->Len) /
sizeof(SYMBOL));
SrcVal1 = (UINT64)&Src1->Value;
}
Expand Down Expand Up @@ -1709,7 +1709,7 @@ ScriptEngineExecute(PGUEST_REGS GuestRegs,
if (Src0->Type == SYMBOL_WSTRING_TYPE)
{
*Indx =
*Indx + ((sizeof(unsigned long long) + 2 * wcslen((wchar_t *)&Src0->Value)) /
*Indx + ((2 * sizeof(unsigned long long) + 2 * wcslen((wchar_t *)&Src0->Value)) /
sizeof(SYMBOL));
SrcVal0 = (UINT64)&Src0->Value;
}
Expand All @@ -1727,7 +1727,7 @@ ScriptEngineExecute(PGUEST_REGS GuestRegs,
if (Src1->Type == SYMBOL_WSTRING_TYPE)
{
*Indx =
*Indx + ((sizeof(unsigned long long) + 2 * wcslen((wchar_t *)&Src1->Value)) /
*Indx + ((2 * sizeof(unsigned long long) + 2 * wcslen((wchar_t *)&Src1->Value)) /
sizeof(SYMBOL));
SrcVal1 = (UINT64)&Src1->Value;
}
Expand Down Expand Up @@ -1766,7 +1766,7 @@ ScriptEngineExecute(PGUEST_REGS GuestRegs,
if (Src1->Type == SYMBOL_STRING_TYPE)
{
*Indx =
*Indx + ((sizeof(unsigned long long) + strlen((char *)&Src1->Value)) /
*Indx + ((2 * sizeof(unsigned long long) + Src1->Len) /
sizeof(SYMBOL));
SrcVal1 = (UINT64)&Src1->Value;
}
Expand All @@ -1784,7 +1784,7 @@ ScriptEngineExecute(PGUEST_REGS GuestRegs,
if (Src2->Type == SYMBOL_STRING_TYPE)
{
*Indx =
*Indx + ((sizeof(unsigned long long) + strlen((char *)&Src2->Value)) /
*Indx + ((2 * sizeof(unsigned long long) + Src2->Len) /
sizeof(SYMBOL));
SrcVal2 = (UINT64)&Src2->Value;
}
Expand Down Expand Up @@ -1816,7 +1816,7 @@ ScriptEngineExecute(PGUEST_REGS GuestRegs,
//

*Indx =
*Indx + ((sizeof(unsigned long long) + strlen((char *)&Src0->Value)) /
*Indx + ((2 * sizeof(unsigned long long) + Src0->Len) /
sizeof(SYMBOL));

Src1 = (PSYMBOL)((unsigned long long)CodeBuffer->Head +
Expand Down
Expand Up @@ -3,6 +3,7 @@
#define SCRIPT_ENGINE_COMMON_DEFINITIONS_H
typedef struct SYMBOL {
long long unsigned Type;
long long unsigned Len;
long long unsigned Value;
} SYMBOL, * PSYMBOL;
typedef struct SYMBOL_BUFFER {
Expand Down

0 comments on commit 6619529

Please sign in to comment.