|
|
|
@ -74,8 +74,10 @@ BOOL NcCreateLabels(PNATIVE_CODE_BLOCK Block)
|
|
|
|
|
ULONG CurrentLabelId = 0;
|
|
|
|
|
for (PNATIVE_CODE_LINK T = Block->Start; T; T = T->Next)
|
|
|
|
|
{
|
|
|
|
|
XED_CATEGORY_ENUM Category = XedDecodedInstGetCategory(&T->XedInst);
|
|
|
|
|
if (!(T->Flags & CODE_FLAG_IS_INST))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
XED_CATEGORY_ENUM Category = XedDecodedInstGetCategory(&T->XedInst);
|
|
|
|
|
if (Category != XED_CATEGORY_COND_BR && Category != XED_CATEGORY_UNCOND_BR)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
@ -86,23 +88,20 @@ BOOL NcCreateLabels(PNATIVE_CODE_BLOCK Block)
|
|
|
|
|
CONST XED_INST* Inst = XedDecodedInstInst(&T->XedInst);
|
|
|
|
|
if (!Inst)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CONST XED_OPERAND* Operand = XedInstOperand(Inst, 0);
|
|
|
|
|
if (!Operand)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
XED_OPERAND_TYPE_ENUM OperandType = XedOperandType(Operand);
|
|
|
|
|
if (OperandType != XED_OPERAND_TYPE_IMM && OperandType != XED_OPERAND_TYPE_IMM_CONST)
|
|
|
|
|
{
|
|
|
|
|
printf("Found jump to non immediate value. Category: %s\n", XedCategoryEnumToString(Category));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
INT32 BranchDisplacement = XedDecodedInstGetBranchDisplacement(&T->XedInst);
|
|
|
|
|
PNATIVE_CODE_LINK JmpPos = NcValidateJmp(T, BranchDisplacement);
|
|
|
|
|
if (!JmpPos)
|
|
|
|
|
{
|
|
|
|
|
printf("Failed to validate jump. Type: %s, Displacement: %d", XedCategoryEnumToString(Category), BranchDisplacement);
|
|
|
|
|
printf("Failed to validate jump. Type: %s, Displacement: %d\n", XedCategoryEnumToString(Category), BranchDisplacement);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -118,6 +117,7 @@ BOOL NcCreateLabels(PNATIVE_CODE_BLOCK Block)
|
|
|
|
|
}
|
|
|
|
|
T->Flags |= CODE_FLAG_IS_REL_JMP;
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PNATIVE_CODE_LINK NcValidateJmp(PNATIVE_CODE_LINK Jmp, INT32 Delta)
|
|
|
|
@ -149,7 +149,7 @@ PNATIVE_CODE_LINK NcValidateJmp(PNATIVE_CODE_LINK Jmp, INT32 Delta)
|
|
|
|
|
Delta += XedDecodedInstGetLength(&T->XedInst);
|
|
|
|
|
if (Delta >= 0)
|
|
|
|
|
break;
|
|
|
|
|
T = T->Next;
|
|
|
|
|
T = T->Prev;
|
|
|
|
|
}
|
|
|
|
|
if (Delta != 0 || !T)
|
|
|
|
|
return NULL;
|
|
|
|
@ -157,7 +157,6 @@ PNATIVE_CODE_LINK NcValidateJmp(PNATIVE_CODE_LINK Jmp, INT32 Delta)
|
|
|
|
|
T = T->Next;
|
|
|
|
|
return T;
|
|
|
|
|
}
|
|
|
|
|
//return the jmp if that delta is zero
|
|
|
|
|
return Jmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -172,8 +171,8 @@ BOOL NcFromBuffer(PNATIVE_CODE_BLOCK Block, PVOID Buffer, ULONG BufferSize)
|
|
|
|
|
while (Offset < BufferSize)
|
|
|
|
|
{
|
|
|
|
|
PNATIVE_CODE_LINK Link = new NATIVE_CODE_LINK;
|
|
|
|
|
Link->Flags = CODE_FLAG_IS_INST;
|
|
|
|
|
ULONG PossibleSize = min(15, BufferSize - Offset);
|
|
|
|
|
|
|
|
|
|
XED_ERROR_ENUM DecodeError = XedDecode(&Link->XedInst, (Buf + Offset), PossibleSize);
|
|
|
|
|
if (DecodeError != XED_ERROR_NONE)
|
|
|
|
|
{
|
|
|
|
@ -186,6 +185,8 @@ BOOL NcFromBuffer(PNATIVE_CODE_BLOCK Block, PVOID Buffer, ULONG BufferSize)
|
|
|
|
|
Link->Prev = Block->End;
|
|
|
|
|
Block->End->Next = Link;
|
|
|
|
|
Block->End = Link;
|
|
|
|
|
|
|
|
|
|
Offset += XedDecodedInstGetLength(&Link->XedInst);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PNATIVE_CODE_LINK StartLink = Block->Start;
|
|
|
|
@ -206,3 +207,33 @@ VOID NcDelete(PNATIVE_CODE_BLOCK Block)
|
|
|
|
|
T = Next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VOID NcDebugPrint(PNATIVE_CODE_BLOCK Block)
|
|
|
|
|
{
|
|
|
|
|
HANDLE ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
|
if (!ConsoleHandle)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
for (PNATIVE_CODE_LINK T = Block->Start; T; T = T->Next)
|
|
|
|
|
{
|
|
|
|
|
if (T->Flags & CODE_FLAG_IS_LABEL)
|
|
|
|
|
{
|
|
|
|
|
SetConsoleTextAttribute(ConsoleHandle, FOREGROUND_GREEN | FOREGROUND_RED);
|
|
|
|
|
printf("Label: %u\n", T->Label);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
XED_ICLASS_ENUM IClass = XedDecodedInstGetIClass(&T->XedInst);
|
|
|
|
|
if (T->Flags & CODE_FLAG_IS_REL_JMP)
|
|
|
|
|
{
|
|
|
|
|
SetConsoleTextAttribute(ConsoleHandle, FOREGROUND_GREEN | FOREGROUND_RED);
|
|
|
|
|
printf("%s: %u\n", XedIClassEnumToString(IClass), T->Label);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SetConsoleTextAttribute(ConsoleHandle, FOREGROUND_GREEN | FOREGROUND_BLUE);
|
|
|
|
|
printf("%s\n", XedIClassEnumToString(IClass));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|