added clang format

merge-requests/2/head
_xeroxz 4 years ago
parent 47a46d1c58
commit 33e13a1597

@ -0,0 +1,10 @@
---
BreakBeforeBraces: GNU
CompactNamespaces: 'false'
FixNamespaceComments: 'true'
NamespaceIndentation: All
ObjCBinPackProtocolList: Always
Standard: Cpp11
UseTab: Always
...

@ -2,32 +2,34 @@
namespace vm namespace vm
{ {
namespace calc_jmp namespace calc_jmp
{ {
bool get(const zydis_routine_t& vm_entry, zydis_routine_t& calc_jmp) bool get(const zydis_routine_t &vm_entry, zydis_routine_t &calc_jmp)
{ {
auto result = std::find_if(vm_entry.begin(), vm_entry.end(), auto result = std::find_if(
[](const zydis_instr_t& instr_data) -> bool vm_entry.begin(), vm_entry.end(),
{ [](const zydis_instr_t &instr_data) -> bool {
// mov/movsx/movzx rax/eax/ax/al, [rsi] // mov/movsx/movzx rax/eax/ax/al, [rsi]
if (instr_data.instr.operand_count > 1 && if (instr_data.instr.operand_count > 1 &&
(instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOV || (instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOV ||
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVSX || instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVSX ||
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVZX) && instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVZX) &&
instr_data.instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER && instr_data.instr.operands[0].type ==
util::reg::to64(instr_data.instr.operands[0].reg.value) == ZYDIS_REGISTER_RAX && ZYDIS_OPERAND_TYPE_REGISTER &&
instr_data.instr.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY && util::reg::to64(instr_data.instr.operands[0].reg.value) ==
instr_data.instr.operands[1].mem.base == ZYDIS_REGISTER_RSI) ZYDIS_REGISTER_RAX &&
return true; instr_data.instr.operands[1].type ==
return false; ZYDIS_OPERAND_TYPE_MEMORY &&
} instr_data.instr.operands[1].mem.base == ZYDIS_REGISTER_RSI)
); return true;
return false;
});
if (result == vm_entry.end()) if (result == vm_entry.end())
return false; return false;
calc_jmp.insert(calc_jmp.end(), result, vm_entry.end()); calc_jmp.insert(calc_jmp.end(), result, vm_entry.end());
return true; return true;
} }
} } // namespace calc_jmp
} } // namespace vm

@ -2,398 +2,407 @@
namespace vm namespace vm
{ {
namespace handler namespace handler
{
bool get(zydis_routine_t &calc_jmp, zydis_routine_t &vm_handler,
std::uintptr_t handler_addr)
{
if (!vm::util::flatten(vm_handler, handler_addr))
return false;
vm::util::deobfuscate(vm_handler);
static const auto calc_jmp_check = [&](std::uintptr_t addr) -> bool {
for (const auto &[instr, instr_raw, instr_addr] : calc_jmp)
if (instr_addr == addr)
return true;
return false;
};
auto result = std::find_if(
vm_handler.begin(), vm_handler.end(),
[](const zydis_instr_t &instr) -> bool {
if (instr.instr.mnemonic == ZYDIS_MNEMONIC_LEA &&
instr.instr.operands[0].reg.value == ZYDIS_REGISTER_RAX &&
instr.instr.operands[1].mem.base == ZYDIS_REGISTER_RDI &&
instr.instr.operands[1].mem.disp.value == 0xE0)
return true;
return calc_jmp_check(instr.addr);
});
// remove calc_jmp from the vm handler vector...
if (result != vm_handler.end())
vm_handler.erase(result, vm_handler.end());
else // locate the last mov al, [rsi],
// then remove all instructions after that...
{ {
bool get(zydis_routine_t& calc_jmp, zydis_routine_t& vm_handler, std::uintptr_t handler_addr) zydis_routine_t::iterator last = vm_handler.end();
{ result = vm_handler.begin();
if (!vm::util::flatten(vm_handler, handler_addr))
return false; while (result != vm_handler.end())
{
vm::util::deobfuscate(vm_handler); result = std::find_if(
++result, vm_handler.end(),
static const auto calc_jmp_check = [](const zydis_instr_t &instr_data) -> bool {
[&](std::uintptr_t addr) -> bool // mov/movsx/movzx rax/eax/ax/al, [rsi]
{ if (instr_data.instr.operand_count > 1 &&
for (const auto& [instr, instr_raw, instr_addr] : calc_jmp) (instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOV ||
if (instr_addr == addr) instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVSX ||
return true; instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVZX) &&
instr_data.instr.operands[0].type ==
return false; ZYDIS_OPERAND_TYPE_REGISTER &&
}; util::reg::to64(
instr_data.instr.operands[0].reg.value) ==
auto result = std::find_if( ZYDIS_REGISTER_RAX &&
vm_handler.begin(), vm_handler.end(), instr_data.instr.operands[1].type ==
[](const zydis_instr_t& instr) -> bool ZYDIS_OPERAND_TYPE_MEMORY &&
{ instr_data.instr.operands[1].mem.base ==
if (instr.instr.mnemonic == ZYDIS_MNEMONIC_LEA && ZYDIS_REGISTER_RSI)
instr.instr.operands[0].reg.value == ZYDIS_REGISTER_RAX && return true;
instr.instr.operands[1].mem.base == ZYDIS_REGISTER_RDI && return false;
instr.instr.operands[1].mem.disp.value == 0xE0) });
return true;
if (result != vm_handler.end())
return calc_jmp_check(instr.addr); last = result;
} }
);
if (last != vm_handler.end())
// remove calc_jmp from the vm handler vector... vm_handler.erase(last, vm_handler.end());
if (result != vm_handler.end()) }
vm_handler.erase(result, vm_handler.end()); return true;
else // locate the last mov al, [rsi], }
// then remove all instructions after that...
{ bool get_all(std::uintptr_t module_base, std::uintptr_t image_base,
zydis_routine_t::iterator last = vm_handler.end(); zydis_routine_t &vm_entry, std::uintptr_t *vm_handler_table,
result = vm_handler.begin(); std::vector<vm::handler::handler_t> &vm_handlers)
{
while (result != vm_handler.end()) zydis_decoded_instr_t instr;
{ if (!vm::handler::table::get_transform(vm_entry, &instr))
result = std::find_if( return false;
++result, vm_handler.end(),
[](const zydis_instr_t& instr_data) -> bool zydis_routine_t calc_jmp;
{ if (!vm::calc_jmp::get(vm_entry, calc_jmp))
// mov/movsx/movzx rax/eax/ax/al, [rsi] return false;
if (instr_data.instr.operand_count > 1 &&
(instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOV || for (auto idx = 0u; idx < 256; ++idx)
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVSX || {
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVZX) && const auto decrypt_val =
instr_data.instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER && vm::handler::table::decrypt(instr, vm_handler_table[idx]);
util::reg::to64(instr_data.instr.operands[0].reg.value) == ZYDIS_REGISTER_RAX &&
instr_data.instr.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY &&
instr_data.instr.operands[1].mem.base == ZYDIS_REGISTER_RSI)
return true;
return false;
}
);
if (result != vm_handler.end())
last = result;
}
if (last != vm_handler.end())
vm_handler.erase(last, vm_handler.end());
}
return true;
}
bool get_all(std::uintptr_t module_base, std::uintptr_t image_base,
zydis_routine_t& vm_entry, std::uintptr_t* vm_handler_table, std::vector<vm::handler::handler_t>& vm_handlers)
{
zydis_decoded_instr_t instr;
if (!vm::handler::table::get_transform(vm_entry, &instr))
return false;
zydis_routine_t calc_jmp; handler_t vm_handler;
if (!vm::calc_jmp::get(vm_entry, calc_jmp)) vm::transform::map_t transforms;
return false; zydis_routine_t vm_handler_instrs;
for (auto idx = 0u; idx < 256; ++idx) if (!vm::handler::get(calc_jmp, vm_handler_instrs,
{ (decrypt_val - image_base) + module_base))
const auto decrypt_val = return false;
vm::handler::table::decrypt(
instr, vm_handler_table[idx]);
handler_t vm_handler; const auto has_imm = vm::handler::has_imm(vm_handler_instrs);
vm::transform::map_t transforms;
zydis_routine_t vm_handler_instrs;
if (!vm::handler::get(calc_jmp, vm_handler_instrs, (decrypt_val - image_base) + module_base)) const auto imm_size = vm::handler::imm_size(vm_handler_instrs);
return false;
const auto has_imm = if (has_imm && !vm::handler::get_operand_transforms(vm_handler_instrs,
vm::handler::has_imm(vm_handler_instrs); transforms))
return false;
const auto imm_size = vm_handler.address = (decrypt_val - image_base) + module_base;
vm::handler::imm_size(vm_handler_instrs); vm_handler.instrs = vm_handler_instrs;
vm_handler.imm_size = imm_size;
vm_handler.transforms = transforms;
vm_handler.profile = vm::handler::get_profile(vm_handler);
vm_handlers.push_back(vm_handler);
}
if (has_imm && !vm::handler::get_operand_transforms(vm_handler_instrs, transforms)) return true;
return false; }
bool has_imm(const zydis_routine_t &vm_handler)
{
const auto result = std::find_if(
vm_handler.begin(), vm_handler.end(),
[](const zydis_instr_t &instr_data) -> bool {
// mov/movsx/movzx rax/eax/ax/al, [rsi]
if (instr_data.instr.operand_count > 1 &&
(instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOV ||
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVSX ||
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVZX) &&
instr_data.instr.operands[0].type ==
ZYDIS_OPERAND_TYPE_REGISTER &&
util::reg::to64(instr_data.instr.operands[0].reg.value) ==
ZYDIS_REGISTER_RAX &&
instr_data.instr.operands[1].type ==
ZYDIS_OPERAND_TYPE_MEMORY &&
instr_data.instr.operands[1].mem.base == ZYDIS_REGISTER_RSI)
return true;
return false;
});
return result != vm_handler.end();
}
std::uint8_t imm_size(const zydis_routine_t &vm_handler)
{
const auto result = std::find_if(
vm_handler.begin(), vm_handler.end(),
[](const zydis_instr_t &instr_data) -> bool {
// mov/movsx/movzx rax/eax/ax/al, [rsi]
if (instr_data.instr.operand_count > 1 &&
(instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOV ||
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVSX ||
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVZX) &&
instr_data.instr.operands[0].type ==
ZYDIS_OPERAND_TYPE_REGISTER &&
util::reg::to64(instr_data.instr.operands[0].reg.value) ==
ZYDIS_REGISTER_RAX &&
instr_data.instr.operands[1].type ==
ZYDIS_OPERAND_TYPE_MEMORY &&
instr_data.instr.operands[1].mem.base == ZYDIS_REGISTER_RSI)
return true;
return false;
});
if (result == vm_handler.end())
return 0u;
return result->instr.operands[1].size;
}
bool get_operand_transforms(const zydis_routine_t &vm_handler,
transform::map_t &transforms)
{
auto imm_fetch = std::find_if(
vm_handler.begin(), vm_handler.end(),
[](const zydis_instr_t &instr_data) -> bool {
// mov/movsx/movzx rax/eax/ax/al, [rsi]
if (instr_data.instr.operand_count > 1 &&
(instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOV ||
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVSX ||
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVZX) &&
instr_data.instr.operands[0].type ==
ZYDIS_OPERAND_TYPE_REGISTER &&
util::reg::to64(instr_data.instr.operands[0].reg.value) ==
ZYDIS_REGISTER_RAX &&
instr_data.instr.operands[1].type ==
ZYDIS_OPERAND_TYPE_MEMORY &&
instr_data.instr.operands[1].mem.base == ZYDIS_REGISTER_RSI)
return true;
return false;
});
if (imm_fetch == vm_handler.end())
return false;
// this finds the first transformation which looks like:
// transform rax, rbx <--- note these registers can be smaller so we to64
// them...
auto key_transform = std::find_if(
imm_fetch, vm_handler.end(),
[](const zydis_instr_t &instr_data) -> bool {
if (util::reg::compare(instr_data.instr.operands[0].reg.value,
ZYDIS_REGISTER_RAX) &&
util::reg::compare(instr_data.instr.operands[1].reg.value,
ZYDIS_REGISTER_RBX))
return true;
return false;
});
if (key_transform == vm_handler.end())
return false;
// look for a primer/instruction that alters RAX prior to the 5
// transformations...
auto generic0 = std::find_if(
imm_fetch + 1, key_transform,
[](const zydis_instr_t &instr_data) -> bool {
return util::reg::compare(instr_data.instr.operands[0].reg.value,
ZYDIS_REGISTER_RAX) &&
!util::reg::compare(instr_data.instr.operands[1].reg.value,
ZYDIS_REGISTER_RBX);
});
zydis_decoded_instr_t nogeneric0;
nogeneric0.mnemonic = ZYDIS_MNEMONIC_INVALID;
transforms[transform::type::generic0] =
generic0 != key_transform ? generic0->instr : nogeneric0;
// last transformation is the same as the first except src and dest are
// swwapped...
transforms[transform::type::rolling_key] = key_transform->instr;
auto instr_copy = key_transform->instr;
instr_copy.operands[0].reg.value =
key_transform->instr.operands[1].reg.value;
instr_copy.operands[1].reg.value =
key_transform->instr.operands[0].reg.value;
transforms[transform::type::update_key] = instr_copy;
// three generic transformations...
auto generic_transform = key_transform;
for (auto idx = 2u; idx < 5; ++idx)
{
generic_transform = std::find_if(
++generic_transform, vm_handler.end(),
[](const zydis_instr_t &instr_data) -> bool {
if (util::reg::compare(instr_data.instr.operands[0].reg.value,
ZYDIS_REGISTER_RAX))
return true;
vm_handler.address = (decrypt_val - image_base) + module_base; return false;
vm_handler.instrs = vm_handler_instrs; });
vm_handler.imm_size = imm_size;
vm_handler.transforms = transforms;
vm_handler.profile = vm::handler::get_profile(vm_handler);
vm_handlers.push_back(vm_handler);
}
return true; if (generic_transform == vm_handler.end())
} return false;
bool has_imm(const zydis_routine_t& vm_handler) transforms[(transform::type)(idx)] = generic_transform->instr;
{ }
const auto result = std::find_if(
vm_handler.begin(), vm_handler.end(),
[](const zydis_instr_t& instr_data) -> bool
{
// mov/movsx/movzx rax/eax/ax/al, [rsi]
if (instr_data.instr.operand_count > 1 &&
(instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOV ||
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVSX ||
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVZX) &&
instr_data.instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER &&
util::reg::to64(instr_data.instr.operands[0].reg.value) == ZYDIS_REGISTER_RAX &&
instr_data.instr.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY &&
instr_data.instr.operands[1].mem.base == ZYDIS_REGISTER_RSI)
return true;
return false;
}
);
return result != vm_handler.end();
}
std::uint8_t imm_size(const zydis_routine_t& vm_handler)
{
const auto result = std::find_if(
vm_handler.begin(), vm_handler.end(),
[](const zydis_instr_t& instr_data) -> bool
{
// mov/movsx/movzx rax/eax/ax/al, [rsi]
if (instr_data.instr.operand_count > 1 &&
(instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOV ||
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVSX ||
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVZX) &&
instr_data.instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER &&
util::reg::to64(instr_data.instr.operands[0].reg.value) == ZYDIS_REGISTER_RAX &&
instr_data.instr.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY &&
instr_data.instr.operands[1].mem.base == ZYDIS_REGISTER_RSI)
return true;
return false;
}
);
if (result == vm_handler.end())
return 0u;
return result->instr.operands[1].size;
}
bool get_operand_transforms(const zydis_routine_t& vm_handler, transform::map_t& transforms)
{
auto imm_fetch = std::find_if(
vm_handler.begin(), vm_handler.end(),
[](const zydis_instr_t& instr_data) -> bool
{
// mov/movsx/movzx rax/eax/ax/al, [rsi]
if (instr_data.instr.operand_count > 1 &&
(instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOV ||
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVSX ||
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOVZX) &&
instr_data.instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER &&
util::reg::to64(instr_data.instr.operands[0].reg.value) == ZYDIS_REGISTER_RAX &&
instr_data.instr.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY &&
instr_data.instr.operands[1].mem.base == ZYDIS_REGISTER_RSI)
return true;
return false;
}
);
if (imm_fetch == vm_handler.end())
return false;
// this finds the first transformation which looks like:
// transform rax, rbx <--- note these registers can be smaller so we to64 them...
auto key_transform = std::find_if(imm_fetch, vm_handler.end(),
[](const zydis_instr_t& instr_data) -> bool
{
if (util::reg::compare(instr_data.instr.operands[0].reg.value, ZYDIS_REGISTER_RAX) &&
util::reg::compare(instr_data.instr.operands[1].reg.value, ZYDIS_REGISTER_RBX))
return true;
return false;
}
);
if (key_transform == vm_handler.end())
return false;
// look for a primer/instruction that alters RAX prior to the 5 transformations...
auto generic0 = std::find_if(imm_fetch + 1, key_transform,
[](const zydis_instr_t& instr_data) -> bool
{
return util::reg::compare(
instr_data.instr.operands[0].reg.value, ZYDIS_REGISTER_RAX) &&
!util::reg::compare(instr_data.instr.operands[1].reg.value, ZYDIS_REGISTER_RBX);
}
);
zydis_decoded_instr_t nogeneric0;
nogeneric0.mnemonic = ZYDIS_MNEMONIC_INVALID;
transforms[transform::type::generic0] =
generic0 != key_transform ? generic0->instr : nogeneric0;
// last transformation is the same as the first except src and dest are swwapped...
transforms[transform::type::rolling_key] = key_transform->instr;
auto instr_copy = key_transform->instr;
instr_copy.operands[0].reg.value = key_transform->instr.operands[1].reg.value;
instr_copy.operands[1].reg.value = key_transform->instr.operands[0].reg.value;
transforms[transform::type::update_key] = instr_copy;
// three generic transformations...
auto generic_transform = key_transform;
for (auto idx = 2u; idx < 5; ++idx)
{
generic_transform = std::find_if(++generic_transform, vm_handler.end(),
[](const zydis_instr_t& instr_data) -> bool
{
if (util::reg::compare(instr_data.instr.operands[0].reg.value, ZYDIS_REGISTER_RAX))
return true;
return false;
}
);
if (generic_transform == vm_handler.end())
return false;
transforms[(transform::type)(idx)] = generic_transform->instr;
}
return true;
}
vm::handler::profile_t* get_profile(handler_t& vm_handler) return true;
}
vm::handler::profile_t *get_profile(handler_t &vm_handler)
{
static const auto vcontains = [](vm::handler::profile_t *vprofile,
handler_t *vm_handler) -> bool {
if (vprofile->imm_size != vm_handler->imm_size)
return false;
for (auto &instr : vprofile->signature)
{
const auto contains = std::find_if(
vm_handler->instrs.begin(), vm_handler->instrs.end(),
[&](zydis_instr_t &instr_data) -> bool {
return instr(instr_data.instr);
});
if (contains == vm_handler->instrs.end())
return false;
}
return true;
};
for (auto profile : vm::handler::profile::all)
if (vcontains(profile, &vm_handler))
return profile;
return nullptr;
}
namespace table
{
std::uintptr_t *get(const zydis_routine_t &vm_entry)
{
const auto result = std::find_if(
vm_entry.begin(), vm_entry.end(),
[](const zydis_instr_t &instr_data) -> bool {
const auto instr = &instr_data.instr;
// lea r12, vm_handlers... (always r12)...
if (instr->mnemonic == ZYDIS_MNEMONIC_LEA &&
instr->operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER &&
instr->operands[0].reg.value == ZYDIS_REGISTER_R12 &&
!instr->raw.sib.base) // no register used for the sib base...
return true;
return false;
});
if (result == vm_entry.end())
return nullptr;
std::uintptr_t ptr = 0u;
ZydisCalcAbsoluteAddress(&result->instr, &result->instr.operands[1],
result->addr, &ptr);
return reinterpret_cast<std::uintptr_t *>(ptr);
}
bool get_transform(const zydis_routine_t &vm_entry,
zydis_decoded_instr_t *transform_instr)
{
zydis_register_t rcx_or_rdx = ZYDIS_REGISTER_NONE;
auto handler_fetch = std::find_if(
vm_entry.begin(), vm_entry.end(),
[&](const zydis_instr_t &instr_data) -> bool {
const auto instr = &instr_data.instr;
if (instr->mnemonic == ZYDIS_MNEMONIC_MOV &&
instr->operand_count == 2 &&
instr->operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY &&
instr->operands[1].mem.base == ZYDIS_REGISTER_R12 &&
instr->operands[1].mem.index == ZYDIS_REGISTER_RAX &&
instr->operands[1].mem.scale == 8 &&
instr->operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER &&
(instr->operands[0].reg.value == ZYDIS_REGISTER_RDX ||
instr->operands[0].reg.value == ZYDIS_REGISTER_RCX))
{ {
static const auto vcontains = rcx_or_rdx = instr->operands[0].reg.value;
[](vm::handler::profile_t* vprofile, handler_t* vm_handler) -> bool return true;
{
if (vprofile->imm_size != vm_handler->imm_size)
return false;
for (auto& instr : vprofile->signature)
{
const auto contains = std::find_if
(
vm_handler->instrs.begin(),
vm_handler->instrs.end(),
[&](zydis_instr_t& instr_data) -> bool
{ return instr(instr_data.instr); }
);
if (contains == vm_handler->instrs.end())
return false;
}
return true;
};
for (auto profile : vm::handler::profile::all)
if (vcontains(profile, &vm_handler))
return profile;
return nullptr;
} }
namespace table return false;
{ });
std::uintptr_t* get(const zydis_routine_t& vm_entry)
{ // check to see if we found the fetch instruction and if the next
const auto result = std::find_if( // instruction is not the end of the vector...
vm_entry.begin(), vm_entry.end(), if (handler_fetch == vm_entry.end() ||
[](const zydis_instr_t& instr_data) -> bool ++handler_fetch == vm_entry.end() ||
{ // must be RCX or RDX... else something went wrong...
const auto instr = &instr_data.instr; (rcx_or_rdx != ZYDIS_REGISTER_RCX &&
// lea r12, vm_handlers... (always r12)... rcx_or_rdx != ZYDIS_REGISTER_RDX))
if (instr->mnemonic == ZYDIS_MNEMONIC_LEA && return false;
instr->operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER &&
instr->operands[0].reg.value == ZYDIS_REGISTER_R12 && // find the next instruction that writes to RCX or RDX...
!instr->raw.sib.base) // no register used for the sib base... // the register is determined by the vm handler fetch above...
return true; auto handler_transform = std::find_if(
handler_fetch, vm_entry.end(),
return false; [&](const zydis_instr_t &instr_data) -> bool {
} if (instr_data.instr.operands[0].reg.value == rcx_or_rdx &&
); instr_data.instr.operands[0].actions &
ZYDIS_OPERAND_ACTION_WRITE)
if (result == vm_entry.end()) return true;
return nullptr; return false;
});
std::uintptr_t ptr = 0u;
ZydisCalcAbsoluteAddress(&result->instr, if (handler_transform == vm_entry.end())
&result->instr.operands[1], result->addr, &ptr); return false;
return reinterpret_cast<std::uintptr_t*>(ptr); *transform_instr = handler_transform->instr;
} return true;
}
bool get_transform(const zydis_routine_t& vm_entry, zydis_decoded_instr_t* transform_instr)
{ std::uint64_t encrypt(zydis_decoded_instr_t &transform_instr,
zydis_register_t rcx_or_rdx = ZYDIS_REGISTER_NONE; std::uint64_t val)
{
auto handler_fetch = std::find_if( assert(transform_instr.operands[0].size == 64,
vm_entry.begin(), vm_entry.end(), "invalid transformation for vm handler table entries...");
[&](const zydis_instr_t& instr_data) -> bool
{ const auto operation = vm::transform::inverse[transform_instr.mnemonic];
const auto instr = &instr_data.instr; const auto bitsize = transform_instr.operands[0].size;
if (instr->mnemonic == ZYDIS_MNEMONIC_MOV && const auto imm = vm::transform::has_imm(&transform_instr)
instr->operand_count == 2 && ? transform_instr.operands[1].imm.value.u
instr->operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY && : 0u;
instr->operands[1].mem.base == ZYDIS_REGISTER_R12 &&
instr->operands[1].mem.index == ZYDIS_REGISTER_RAX && return vm::transform::apply(bitsize, operation, val, imm);
instr->operands[1].mem.scale == 8 && }
instr->operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER &&
(instr->operands[0].reg.value == ZYDIS_REGISTER_RDX || std::uint64_t decrypt(zydis_decoded_instr_t &transform_instr,
instr->operands[0].reg.value == ZYDIS_REGISTER_RCX)) std::uint64_t val)
{ {
rcx_or_rdx = instr->operands[0].reg.value; assert(transform_instr.operands[0].size == 64,
return true; "invalid transformation for vm handler table entries...");
}
const auto operation = transform_instr.mnemonic;
return false; const auto bitsize = transform_instr.operands[0].size;
} const auto imm = vm::transform::has_imm(&transform_instr)
); ? transform_instr.operands[1].imm.value.u
: 0u;
// check to see if we found the fetch instruction and if the next instruction
// is not the end of the vector... return vm::transform::apply(bitsize, operation, val, imm);
if (handler_fetch == vm_entry.end() || ++handler_fetch == vm_entry.end() || }
// must be RCX or RDX... else something went wrong... } // namespace table
(rcx_or_rdx != ZYDIS_REGISTER_RCX && rcx_or_rdx != ZYDIS_REGISTER_RDX)) } // namespace handler
return false; } // namespace vm
// find the next instruction that writes to RCX or RDX...
// the register is determined by the vm handler fetch above...
auto handler_transform = std::find_if(
handler_fetch, vm_entry.end(),
[&](const zydis_instr_t& instr_data) -> bool
{
if (instr_data.instr.operands[0].reg.value == rcx_or_rdx &&
instr_data.instr.operands[0].actions & ZYDIS_OPERAND_ACTION_WRITE)
return true;
return false;
}
);
if (handler_transform == vm_entry.end())
return false;
*transform_instr = handler_transform->instr;
return true;
}
std::uint64_t encrypt(zydis_decoded_instr_t& transform_instr, std::uint64_t val)
{
assert(transform_instr.operands[0].size == 64,
"invalid transformation for vm handler table entries...");
const auto operation = vm::transform::inverse[transform_instr.mnemonic];
const auto bitsize = transform_instr.operands[0].size;
const auto imm = vm::transform::has_imm(&transform_instr) ?
transform_instr.operands[1].imm.value.u : 0u;
return vm::transform::apply(bitsize, operation, val, imm);
}
std::uint64_t decrypt(zydis_decoded_instr_t& transform_instr, std::uint64_t val)
{
assert(transform_instr.operands[0].size == 64,
"invalid transformation for vm handler table entries...");
const auto operation = transform_instr.mnemonic;
const auto bitsize = transform_instr.operands[0].size;
const auto imm = vm::transform::has_imm(&transform_instr) ?
transform_instr.operands[1].imm.value.u : 0u;
return vm::transform::apply(bitsize, operation, val, imm);
}
}
}
}

@ -2,190 +2,191 @@
namespace vm namespace vm
{ {
namespace instrs namespace instrs
{
std::pair<std::uint64_t, std::uint64_t>
decrypt_operand(transform::map_t &transforms, std::uint64_t operand,
std::uint64_t rolling_key)
{
const auto generic_decrypt_0 = &transforms[transform::type::generic0];
const auto key_decrypt = &transforms[transform::type::rolling_key];
const auto generic_decrypt_1 = &transforms[transform::type::generic1];
const auto generic_decrypt_2 = &transforms[transform::type::generic2];
const auto generic_decrypt_3 = &transforms[transform::type::generic3];
const auto update_key = &transforms[transform::type::update_key];
if (generic_decrypt_0->mnemonic != ZYDIS_MNEMONIC_INVALID)
{ {
std::pair<std::uint64_t, std::uint64_t> decrypt_operand(transform::map_t& transforms, operand =
std::uint64_t operand, std::uint64_t rolling_key) transform::apply(generic_decrypt_0->operands[0].size,
{ generic_decrypt_0->mnemonic, operand,
const auto generic_decrypt_0 = &transforms[transform::type::generic0]; // check to see if this instruction has an IMM...
const auto key_decrypt = &transforms[transform::type::rolling_key]; transform::has_imm(generic_decrypt_0)
const auto generic_decrypt_1 = &transforms[transform::type::generic1]; ? generic_decrypt_0->operands[1].imm.value.u
const auto generic_decrypt_2 = &transforms[transform::type::generic2]; : 0);
const auto generic_decrypt_3 = &transforms[transform::type::generic3];
const auto update_key = &transforms[transform::type::update_key];
if (generic_decrypt_0->mnemonic != ZYDIS_MNEMONIC_INVALID)
{
operand = transform::apply(
generic_decrypt_0->operands[0].size,
generic_decrypt_0->mnemonic, operand,
// check to see if this instruction has an IMM...
transform::has_imm(generic_decrypt_0) ?
generic_decrypt_0->operands[1].imm.value.u : 0);
}
// apply transformation with rolling decrypt key...
operand = transform::apply(key_decrypt->operands[0].size,
key_decrypt->mnemonic, operand, rolling_key);
// apply three generic transformations...
{
operand = transform::apply(
generic_decrypt_1->operands[0].size,
generic_decrypt_1->mnemonic, operand,
// check to see if this instruction has an IMM...
transform::has_imm(generic_decrypt_1) ?
generic_decrypt_1->operands[1].imm.value.u : 0);
operand = transform::apply(
generic_decrypt_2->operands[0].size,
generic_decrypt_2->mnemonic, operand,
// check to see if this instruction has an IMM...
transform::has_imm(generic_decrypt_2) ?
generic_decrypt_2->operands[1].imm.value.u : 0);
operand = transform::apply(
generic_decrypt_3->operands[0].size,
generic_decrypt_3->mnemonic, operand,
// check to see if this instruction has an IMM...
transform::has_imm(generic_decrypt_3) ?
generic_decrypt_3->operands[1].imm.value.u : 0);
}
// update rolling key...
auto result = transform::apply(update_key->operands[0].size,
update_key->mnemonic, rolling_key, operand);
// update decryption key correctly...
switch (update_key->operands[0].size)
{
case 8:
rolling_key = (rolling_key & ~0xFFull) + result;
break;
case 16:
rolling_key = (rolling_key & ~0xFFFFull) + result;
break;
default:
rolling_key = result;
break;
}
return { operand, rolling_key };
}
std::pair<std::uint64_t, std::uint64_t> encrypt_operand(transform::map_t& transforms,
std::uint64_t operand, std::uint64_t rolling_key)
{
transform::map_t inverse;
inverse_transforms(transforms, inverse);
const auto generic_decrypt_0 = &inverse[transform::type::generic0];
const auto key_decrypt = &inverse[transform::type::rolling_key];
const auto generic_decrypt_1 = &inverse[transform::type::generic1];
const auto generic_decrypt_2 = &inverse[transform::type::generic2];
const auto generic_decrypt_3 = &inverse[transform::type::generic3];
const auto update_key = &inverse[transform::type::update_key];
auto result = transform::apply(update_key->operands[0].size,
update_key->mnemonic, rolling_key, operand);
// make sure we update the rolling decryption key correctly...
switch (update_key->operands[0].size)
{
case 8:
rolling_key = (rolling_key & ~0xFFull) + result;
break;
case 16:
rolling_key = (rolling_key & ~0xFFFFull) + result;
break;
default:
rolling_key = result;
break;
}
{
operand = transform::apply(
generic_decrypt_3->operands[0].size,
generic_decrypt_3->mnemonic, operand,
// check to see if this instruction has an IMM...
transform::has_imm(generic_decrypt_3) ?
generic_decrypt_3->operands[1].imm.value.u : 0);
operand = transform::apply(
generic_decrypt_2->operands[0].size,
generic_decrypt_2->mnemonic, operand,
// check to see if this instruction has an IMM...
transform::has_imm(generic_decrypt_2) ?
generic_decrypt_2->operands[1].imm.value.u : 0);
operand = transform::apply(
generic_decrypt_1->operands[0].size,
generic_decrypt_1->mnemonic, operand,
// check to see if this instruction has an IMM...
transform::has_imm(generic_decrypt_1) ?
generic_decrypt_1->operands[1].imm.value.u : 0);
}
operand = transform::apply(key_decrypt->operands[0].size,
key_decrypt->mnemonic, operand, rolling_key);
if (generic_decrypt_0->mnemonic != ZYDIS_MNEMONIC_INVALID)
{
operand = transform::apply(
generic_decrypt_0->operands[0].size,
generic_decrypt_0->mnemonic, operand,
// check to see if this instruction has an IMM...
transform::has_imm(generic_decrypt_0) ?
generic_decrypt_0->operands[1].imm.value.u : 0);
}
return { operand, rolling_key };
}
bool get_rva_decrypt(
const zydis_routine_t& vm_entry, std::vector<zydis_decoded_instr_t>& transform_instrs)
{
//
// find mov esi, [rsp+0xA0]
//
auto result = std::find_if(vm_entry.begin(), vm_entry.end(),
[](const zydis_instr_t& instr_data) -> bool
{
if (instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOV &&
instr_data.instr.operand_count == 2 &&
instr_data.instr.operands[0].reg.value == ZYDIS_REGISTER_ESI &&
instr_data.instr.operands[1].mem.base == ZYDIS_REGISTER_RSP &&
instr_data.instr.operands[1].mem.disp.has_displacement &&
instr_data.instr.operands[1].mem.disp.value == 0xA0)
return true;
return false;
}
);
if (result == vm_entry.end())
return false;
//
// find the next three instruction with ESI as the dest...
//
for (auto idx = 0u; idx < 3; ++idx)
{
result = std::find_if(++result, vm_entry.end(),
[](const zydis_instr_t& instr_data) -> bool
{
return instr_data.instr.operands[0].reg.value == ZYDIS_REGISTER_ESI;
}
);
if (result == vm_entry.end())
return false;
transform_instrs.push_back(result->instr);
}
return true;
}
} }
}
// apply transformation with rolling decrypt key...
operand = transform::apply(key_decrypt->operands[0].size,
key_decrypt->mnemonic, operand, rolling_key);
// apply three generic transformations...
operand =
transform::apply(generic_decrypt_1->operands[0].size,
generic_decrypt_1->mnemonic, operand,
// check to see if this instruction has an IMM...
transform::has_imm(generic_decrypt_1)
? generic_decrypt_1->operands[1].imm.value.u
: 0);
operand =
transform::apply(generic_decrypt_2->operands[0].size,
generic_decrypt_2->mnemonic, operand,
// check to see if this instruction has an IMM...
transform::has_imm(generic_decrypt_2)
? generic_decrypt_2->operands[1].imm.value.u
: 0);
operand =
transform::apply(generic_decrypt_3->operands[0].size,
generic_decrypt_3->mnemonic, operand,
// check to see if this instruction has an IMM...
transform::has_imm(generic_decrypt_3)
? generic_decrypt_3->operands[1].imm.value.u
: 0);
// update rolling key...
auto result =
transform::apply(update_key->operands[0].size, update_key->mnemonic,
rolling_key, operand);
// update decryption key correctly...
switch (update_key->operands[0].size)
{
case 8:
rolling_key = (rolling_key & ~0xFFull) + result;
break;
case 16:
rolling_key = (rolling_key & ~0xFFFFull) + result;
break;
default:
rolling_key = result;
break;
}
return {operand, rolling_key};
}
std::pair<std::uint64_t, std::uint64_t>
encrypt_operand(transform::map_t &transforms, std::uint64_t operand,
std::uint64_t rolling_key)
{
transform::map_t inverse;
inverse_transforms(transforms, inverse);
const auto generic_decrypt_0 = &inverse[transform::type::generic0];
const auto key_decrypt = &inverse[transform::type::rolling_key];
const auto generic_decrypt_1 = &inverse[transform::type::generic1];
const auto generic_decrypt_2 = &inverse[transform::type::generic2];
const auto generic_decrypt_3 = &inverse[transform::type::generic3];
const auto update_key = &inverse[transform::type::update_key];
auto result =
transform::apply(update_key->operands[0].size, update_key->mnemonic,
rolling_key, operand);
// make sure we update the rolling decryption key correctly...
switch (update_key->operands[0].size)
{
case 8:
rolling_key = (rolling_key & ~0xFFull) + result;
break;
case 16:
rolling_key = (rolling_key & ~0xFFFFull) + result;
break;
default:
rolling_key = result;
break;
}
operand =
transform::apply(generic_decrypt_3->operands[0].size,
generic_decrypt_3->mnemonic, operand,
// check to see if this instruction has an IMM...
transform::has_imm(generic_decrypt_3)
? generic_decrypt_3->operands[1].imm.value.u
: 0);
operand =
transform::apply(generic_decrypt_2->operands[0].size,
generic_decrypt_2->mnemonic, operand,
// check to see if this instruction has an IMM...
transform::has_imm(generic_decrypt_2)
? generic_decrypt_2->operands[1].imm.value.u
: 0);
operand =
transform::apply(generic_decrypt_1->operands[0].size,
generic_decrypt_1->mnemonic, operand,
// check to see if this instruction has an IMM...
transform::has_imm(generic_decrypt_1)
? generic_decrypt_1->operands[1].imm.value.u
: 0);
operand = transform::apply(key_decrypt->operands[0].size,
key_decrypt->mnemonic, operand, rolling_key);
if (generic_decrypt_0->mnemonic != ZYDIS_MNEMONIC_INVALID)
{
operand =
transform::apply(generic_decrypt_0->operands[0].size,
generic_decrypt_0->mnemonic, operand,
// check to see if this instruction has an IMM...
transform::has_imm(generic_decrypt_0)
? generic_decrypt_0->operands[1].imm.value.u
: 0);
}
return {operand, rolling_key};
}
bool get_rva_decrypt(const zydis_routine_t &vm_entry,
std::vector<zydis_decoded_instr_t> &transform_instrs)
{
// find mov esi, [rsp+0xA0]
auto result = std::find_if(
vm_entry.begin(), vm_entry.end(),
[](const zydis_instr_t &instr_data) -> bool {
if (instr_data.instr.mnemonic == ZYDIS_MNEMONIC_MOV &&
instr_data.instr.operand_count == 2 &&
instr_data.instr.operands[0].reg.value == ZYDIS_REGISTER_ESI &&
instr_data.instr.operands[1].mem.base == ZYDIS_REGISTER_RSP &&
instr_data.instr.operands[1].mem.disp.has_displacement &&
instr_data.instr.operands[1].mem.disp.value == 0xA0)
return true;
return false;
});
if (result == vm_entry.end())
return false;
// find the next three instruction with ESI as the dest...
for (auto idx = 0u; idx < 3; ++idx)
{
result =
std::find_if(++result, vm_entry.end(),
[](const zydis_instr_t &instr_data) -> bool {
return instr_data.instr.operands[0].reg.value ==
ZYDIS_REGISTER_ESI;
});
if (result == vm_entry.end())
return false;
transform_instrs.push_back(result->instr);
}
return true;
}
} // namespace instrs
} // namespace vm

@ -1,227 +1,206 @@
<?xml version="1.0" encoding="utf-8"?> < ? xml version = "1.0" encoding =
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> "utf-8" ? > <Project ToolsVersion = "4.0" xmlns =
<ItemGroup> "http://schemas.microsoft.com/developer/msbuild/2003">
<Filter Include="Source Files"> <ItemGroup><Filter Include = "Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> <UniqueIdentifier>{4FC737F1 - C7A5 - 4376 - A066 -
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions> 2A32D752A2FF}</ UniqueIdentifier>
</Filter> <Extensions> cpp;
<Filter Include="Header Files"> c;
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> cc;
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions> cxx;
</Filter> c++;
<Filter Include="Header Files\Zydis"> cppm;
<UniqueIdentifier>{b85373f1-1f33-4b4f-aadd-04432b6d62f0}</UniqueIdentifier> ixx;
</Filter> def;
<Filter Include="Header Files\Zycore"> odl;
<UniqueIdentifier>{f57dabfd-2fe1-46a9-96d5-990cd620eda3}</UniqueIdentifier> idl;
</Filter> hpj;
<Filter Include="Header Files\Zydis\Generated"> bat;
<UniqueIdentifier>{40b5c3d5-2a68-4f45-b655-b621ef669204}</UniqueIdentifier> asm;
</Filter> asmx</ Extensions></ Filter><Filter Include = "Header Files"><UniqueIdentifier>{
<Filter Include="Header Files\Zydis\Internal"> 93995380 - 89BD - 4b04 - 88EB - 625FBE52EBFB}</ UniqueIdentifier>
<UniqueIdentifier>{4dc3025a-a1f4-460d-b992-1ed53e44f2c0}</UniqueIdentifier> <Extensions> h;
</Filter> hh;
<Filter Include="Header Files\Zycore\API"> hpp;
<UniqueIdentifier>{a4d9e340-8f8c-4606-bce8-58b86119c829}</UniqueIdentifier> hxx;
</Filter> h++;
<Filter Include="Source Files\vmprofiles"> hm;
<UniqueIdentifier>{388154c1-cb08-493f-88fb-7e16cfffa010}</UniqueIdentifier> inl;
</Filter> inc;
</ItemGroup> ipp;
<ItemGroup> xsd</ Extensions></ Filter><Filter Include = "Header Files\Zydis">
<ClCompile Include="vmutils.cpp"> <UniqueIdentifier>{b85373f1 - 1f33 - 4b4f - aadd -
<Filter>Source Files</Filter> 04432b6d62f0}</ UniqueIdentifier></ Filter>
</ClCompile> <Filter Include = "Header Files\Zycore"><UniqueIdentifier>{
<ClCompile Include="vmprofiles\add.cpp"> f57dabfd - 2fe1 - 46a9 - 96d5 - 990cd620eda3}</ UniqueIdentifier>
<Filter>Source Files\vmprofiles</Filter> </ Filter><Filter Include = "Header Files\Zydis\Generated">
</ClCompile> <UniqueIdentifier>{40b5c3d5 - 2a68 - 4f45 - b655 -
<ClCompile Include="vmprofiles\div.cpp"> b621ef669204}</ UniqueIdentifier></ Filter>
<Filter>Source Files\vmprofiles</Filter> <Filter Include = "Header Files\Zydis\Internal"><UniqueIdentifier>{
</ClCompile> 4dc3025a - a1f4 - 460d - b992 - 1ed53e44f2c0}</ UniqueIdentifier>
<ClCompile Include="vmprofiles\jmp.cpp"> </ Filter><Filter Include = "Header Files\Zycore\API"><UniqueIdentifier>{
<Filter>Source Files\vmprofiles</Filter> a4d9e340 - 8f8c - 4606 - bce8 - 58b86119c829}</ UniqueIdentifier>
</ClCompile> </ Filter><Filter Include = "Source Files\vmprofiles"><UniqueIdentifier>{
<ClCompile Include="vmprofiles\lconst.cpp"> 388154c1 - cb08 - 493f - 88fb - 7e16cfffa010}</ UniqueIdentifier>
<Filter>Source Files\vmprofiles</Filter> </ Filter></ ItemGroup><ItemGroup><ClCompile Include = "vmutils.cpp">
</ClCompile> <Filter> Source Files</ Filter></ ClCompile>
<ClCompile Include="vmprofiles\lreg.cpp"> <ClCompile Include = "vmprofiles\add.cpp">
<Filter>Source Files\vmprofiles</Filter> <Filter> Source Files\vmprofiles</ Filter></ ClCompile>
</ClCompile> <ClCompile Include = "vmprofiles\div.cpp">
<ClCompile Include="vmprofiles\mul.cpp"> <Filter> Source Files\vmprofiles</ Filter></ ClCompile>
<Filter>Source Files\vmprofiles</Filter> <ClCompile Include = "vmprofiles\jmp.cpp">
</ClCompile> <Filter> Source Files\vmprofiles</ Filter></ ClCompile>
<ClCompile Include="vmprofiles\nand.cpp"> <ClCompile Include = "vmprofiles\lconst.cpp">
<Filter>Source Files\vmprofiles</Filter> <Filter> Source Files\vmprofiles</ Filter></ ClCompile>
</ClCompile> <ClCompile Include = "vmprofiles\lreg.cpp">
<ClCompile Include="vmprofiles\pushvsp.cpp"> <Filter> Source Files\vmprofiles</ Filter></ ClCompile>
<Filter>Source Files\vmprofiles</Filter> <ClCompile Include = "vmprofiles\mul.cpp">
</ClCompile> <Filter> Source Files\vmprofiles</ Filter></ ClCompile>
<ClCompile Include="vmprofiles\read.cpp"> <ClCompile Include = "vmprofiles\nand.cpp">
<Filter>Source Files\vmprofiles</Filter> <Filter> Source Files\vmprofiles</ Filter></ ClCompile>
</ClCompile> <ClCompile Include = "vmprofiles\pushvsp.cpp">
<ClCompile Include="vmprofiles\sreg.cpp"> <Filter> Source Files\vmprofiles</ Filter></ ClCompile>
<Filter>Source Files\vmprofiles</Filter> <ClCompile Include = "vmprofiles\read.cpp">
</ClCompile> <Filter> Source Files\vmprofiles</ Filter></ ClCompile>
<ClCompile Include="vmprofiles\vmexit.cpp"> <ClCompile Include = "vmprofiles\sreg.cpp">
<Filter>Source Files\vmprofiles</Filter> <Filter> Source Files\vmprofiles</ Filter></ ClCompile>
</ClCompile> <ClCompile Include = "vmprofiles\vmexit.cpp">
<ClCompile Include="vmprofiles\write.cpp"> <Filter> Source Files\vmprofiles</ Filter></ ClCompile>
<Filter>Source Files\vmprofiles</Filter> <ClCompile Include = "vmprofiles\write.cpp">
</ClCompile> <Filter> Source Files\vmprofiles</ Filter></ ClCompile>
<ClCompile Include="vmprofiles\shl.cpp"> <ClCompile Include = "vmprofiles\shl.cpp">
<Filter>Source Files\vmprofiles</Filter> <Filter> Source Files\vmprofiles</ Filter></ ClCompile>
</ClCompile> <ClCompile Include = "vmprofiles\shr.cpp">
<ClCompile Include="vmprofiles\shr.cpp"> <Filter> Source Files\vmprofiles</ Filter></ ClCompile>
<Filter>Source Files\vmprofiles</Filter> <ClCompile Include = "calc_jmp.cpp"><Filter> Source Files</ Filter>
</ClCompile> </ ClCompile><ClCompile Include = "vmhandler.cpp">
<ClCompile Include="calc_jmp.cpp"> <Filter> Source Files</ Filter></ ClCompile>
<Filter>Source Files</Filter> <ClCompile Include = "vminstrs.cpp"><Filter> Source Files</ Filter>
</ClCompile> </ ClCompile></ ItemGroup><ItemGroup>
<ClCompile Include="vmhandler.cpp"> <ClInclude Include = "..\include\transform.hpp">
<Filter>Source Files</Filter> <Filter> Header Files</ Filter></ ClInclude>
</ClCompile> <ClInclude Include = "..\include\vmprofiler.hpp">
<ClCompile Include="vminstrs.cpp"> <Filter> Header Files</ Filter></ ClInclude>
<Filter>Source Files</Filter> <ClInclude Include = "..\include\vmutils.h"><Filter> Header Files</ Filter>
</ClCompile> </ ClInclude><ClInclude Include = ".."
</ItemGroup> "\dependencies\zydis\include\Zydis\Genera"
<ItemGroup> "ted\EnumInstructionCategory.h">
<ClInclude Include="..\include\transform.hpp"> <Filter> Header Files\Zydis\Generated</ Filter></ ClInclude>
<Filter>Header Files</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\include\Zydis\Generated\EnumISAExt.h">
<ClInclude Include="..\include\vmprofiler.hpp"> <Filter> Header Files\Zydis\Generated</ Filter></ ClInclude>
<Filter>Header Files</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\include\Zydis\Generated\EnumISASet.h">
<ClInclude Include="..\include\vmutils.h"> <Filter> Header Files\Zydis\Generated</ Filter></ ClInclude>
<Filter>Header Files</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\include\Zydis\Generated\EnumMnemonic.h">
<ClInclude Include="..\dependencies\zydis\include\Zydis\Generated\EnumInstructionCategory.h"> <Filter> Header Files\Zydis\Generated</ Filter></ ClInclude>
<Filter>Header Files\Zydis\Generated</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\include\Zydis\Generated\EnumRegister.h">
<ClInclude Include="..\dependencies\zydis\include\Zydis\Generated\EnumISAExt.h"> <Filter> Header Files\Zydis\Generated</ Filter></ ClInclude>
<Filter>Header Files\Zydis\Generated</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\include\Zydis\Internal\DecoderData.h">
<ClInclude Include="..\dependencies\zydis\include\Zydis\Generated\EnumISASet.h"> <Filter> Header Files\Zydis\Internal</ Filter></ ClInclude>
<Filter>Header Files\Zydis\Generated</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\include\Zydis\Internal\FormatterATT.h">
<ClInclude Include="..\dependencies\zydis\include\Zydis\Generated\EnumMnemonic.h"> <Filter> Header Files\Zydis\Internal</ Filter></ ClInclude>
<Filter>Header Files\Zydis\Generated</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\include\Zydis\Internal\FormatterBase.h">
<ClInclude Include="..\dependencies\zydis\include\Zydis\Generated\EnumRegister.h"> <Filter> Header Files\Zydis\Internal</ Filter></ ClInclude>
<Filter>Header Files\Zydis\Generated</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\include\Zydis\Internal\FormatterIntel.h">
<ClInclude Include="..\dependencies\zydis\include\Zydis\Internal\DecoderData.h"> <Filter> Header Files\Zydis\Internal</ Filter></ ClInclude>
<Filter>Header Files\Zydis\Internal</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\include\Zydis\Internal\SharedData.h">
<ClInclude Include="..\dependencies\zydis\include\Zydis\Internal\FormatterATT.h"> <Filter> Header Files\Zydis\Internal</ Filter></ ClInclude>
<Filter>Header Files\Zydis\Internal</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\include\Zydis\Internal\String.h">
<ClInclude Include="..\dependencies\zydis\include\Zydis\Internal\FormatterBase.h"> <Filter> Header Files\Zydis\Internal</ Filter></ ClInclude>
<Filter>Header Files\Zydis\Internal</Filter> <ClInclude Include = "..\dependencies\zydis\include\Zydis\Decoder.h">
</ClInclude> <Filter> Header Files\Zydis</ Filter></ ClInclude>
<ClInclude Include="..\dependencies\zydis\include\Zydis\Internal\FormatterIntel.h"> <ClInclude Include = "..\dependencies\zydis\include\Zydis\DecoderTypes.h">
<Filter>Header Files\Zydis\Internal</Filter> <Filter> Header Files\Zydis</ Filter></ ClInclude>
</ClInclude> <ClInclude Include = "..\dependencies\zydis\include\Zydis\Formatter.h">
<ClInclude Include="..\dependencies\zydis\include\Zydis\Internal\SharedData.h"> <Filter> Header Files\Zydis</ Filter></ ClInclude>
<Filter>Header Files\Zydis\Internal</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\include\Zydis\FormatterBuffer.h">
<ClInclude Include="..\dependencies\zydis\include\Zydis\Internal\String.h"> <Filter> Header Files\Zydis</ Filter></ ClInclude>
<Filter>Header Files\Zydis\Internal</Filter> <ClInclude Include = "..\dependencies\zydis\include\Zydis\MetaInfo.h">
</ClInclude> <Filter> Header Files\Zydis</ Filter></ ClInclude>
<ClInclude Include="..\dependencies\zydis\include\Zydis\Decoder.h"> <ClInclude Include = "..\dependencies\zydis\include\Zydis\Mnemonic.h">
<Filter>Header Files\Zydis</Filter> <Filter> Header Files\Zydis</ Filter></ ClInclude>
</ClInclude> <ClInclude Include = "..\dependencies\zydis\include\Zydis\Register.h">
<ClInclude Include="..\dependencies\zydis\include\Zydis\DecoderTypes.h"> <Filter> Header Files\Zydis</ Filter></ ClInclude>
<Filter>Header Files\Zydis</Filter> <ClInclude Include = "..\dependencies\zydis\include\Zydis\SharedTypes.h">
</ClInclude> <Filter> Header Files\Zydis</ Filter></ ClInclude>
<ClInclude Include="..\dependencies\zydis\include\Zydis\Formatter.h"> <ClInclude Include = "..\dependencies\zydis\include\Zydis\ShortString.h">
<Filter>Header Files\Zydis</Filter> <Filter> Header Files\Zydis</ Filter></ ClInclude>
</ClInclude> <ClInclude Include = "..\dependencies\zydis\include\Zydis\Status.h">
<ClInclude Include="..\dependencies\zydis\include\Zydis\FormatterBuffer.h"> <Filter> Header Files\Zydis</ Filter></ ClInclude>
<Filter>Header Files\Zydis</Filter> <ClInclude Include = "..\dependencies\zydis\include\Zydis\Utils.h">
</ClInclude> <Filter> Header Files\Zydis</ Filter></ ClInclude>
<ClInclude Include="..\dependencies\zydis\include\Zydis\MetaInfo.h"> <ClInclude Include = "..\dependencies\zydis\include\Zydis\Zydis.h">
<Filter>Header Files\Zydis</Filter> <Filter> Header Files\Zydis</ Filter></ ClInclude>
</ClInclude> <ClInclude Include =
<ClInclude Include="..\dependencies\zydis\include\Zydis\Mnemonic.h"> "..\dependencies\zydis\dependencies\zycore\include\Zycore\API\Memory."
<Filter>Header Files\Zydis</Filter> "h">
</ClInclude> <Filter> Header Files\Zycore\API</ Filter></ ClInclude>
<ClInclude Include="..\dependencies\zydis\include\Zydis\Register.h"> <ClInclude Include = ".."
<Filter>Header Files\Zydis</Filter> "\dependencies\zydis\dependencies\zycore\include\Zycor"
</ClInclude> "e\API\Synchronization.h">
<ClInclude Include="..\dependencies\zydis\include\Zydis\SharedTypes.h"> <Filter> Header Files\Zycore\API</ Filter></ ClInclude>
<Filter>Header Files\Zydis</Filter> <ClInclude Include = ".."
</ClInclude> "\dependencies\zydis\dependencies\zycore\include\Zycor"
<ClInclude Include="..\dependencies\zydis\include\Zydis\ShortString.h"> "e\API\Terminal.h">
<Filter>Header Files\Zydis</Filter> <Filter> Header Files\Zycore\API</ Filter></ ClInclude>
</ClInclude> <ClInclude Include =
<ClInclude Include="..\dependencies\zydis\include\Zydis\Status.h"> "..\dependencies\zydis\dependencies\zycore\include\Zycore\API\Thread."
<Filter>Header Files\Zydis</Filter> "h">
</ClInclude> <Filter> Header Files\Zycore\API</ Filter></ ClInclude>
<ClInclude Include="..\dependencies\zydis\include\Zydis\Utils.h"> <ClInclude Include =
<Filter>Header Files\Zydis</Filter> "..\dependencies\zydis\dependencies\zycore\include\Zycore\Allocator.h">
</ClInclude> <Filter> Header Files\Zycore</ Filter></ ClInclude>
<ClInclude Include="..\dependencies\zydis\include\Zydis\Zydis.h"> <ClInclude Include =
<Filter>Header Files\Zydis</Filter> "..\dependencies\zydis\dependencies\zycore\include\Zycore\ArgParse.h">
</ClInclude> <Filter> Header Files\Zycore</ Filter></ ClInclude>
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\API\Memory.h"> <ClInclude Include =
<Filter>Header Files\Zycore\API</Filter> "..\dependencies\zydis\dependencies\zycore\include\Zycore\Bitset.h">
</ClInclude> <Filter> Header Files\Zycore</ Filter></ ClInclude>
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\API\Synchronization.h"> <ClInclude Include =
<Filter>Header Files\Zycore\API</Filter> "..\dependencies\zydis\dependencies\zycore\include\Zycore\Comparison."
</ClInclude> "h">
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\API\Terminal.h"> <Filter> Header Files\Zycore</ Filter></ ClInclude>
<Filter>Header Files\Zycore\API</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\dependencies\zycore\include\Zycore\Defines.h">
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\API\Thread.h"> <Filter> Header Files\Zycore</ Filter></ ClInclude>
<Filter>Header Files\Zycore\API</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\dependencies\zycore\include\Zycore\Format.h">
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\Allocator.h"> <Filter> Header Files\Zycore</ Filter></ ClInclude>
<Filter>Header Files\Zycore</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\dependencies\zycore\include\Zycore\LibC.h">
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\ArgParse.h"> <Filter> Header Files\Zycore</ Filter></ ClInclude>
<Filter>Header Files\Zycore</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\dependencies\zycore\include\Zycore\List.h">
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\Bitset.h"> <Filter> Header Files\Zycore</ Filter></ ClInclude>
<Filter>Header Files\Zycore</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\dependencies\zycore\include\Zycore\Object.h">
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\Comparison.h"> <Filter> Header Files\Zycore</ Filter></ ClInclude>
<Filter>Header Files\Zycore</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\dependencies\zycore\include\Zycore\Status.h">
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\Defines.h"> <Filter> Header Files\Zycore</ Filter></ ClInclude>
<Filter>Header Files\Zycore</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\dependencies\zycore\include\Zycore\String.h">
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\Format.h"> <Filter> Header Files\Zycore</ Filter></ ClInclude>
<Filter>Header Files\Zycore</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\dependencies\zycore\include\Zycore\Types.h">
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\LibC.h"> <Filter> Header Files\Zycore</ Filter></ ClInclude>
<Filter>Header Files\Zycore</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\dependencies\zycore\include\Zycore\Vector.h">
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\List.h"> <Filter> Header Files\Zycore</ Filter></ ClInclude>
<Filter>Header Files\Zycore</Filter> <ClInclude Include =
</ClInclude> "..\dependencies\zydis\dependencies\zycore\include\Zycore\Zycore.h">
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\Object.h"> <Filter> Header Files\Zycore</ Filter></ ClInclude>
<Filter>Header Files\Zycore</Filter> <ClInclude Include = "..\dependencies\zydis\msvc\ZycoreExportConfig.h">
</ClInclude> <Filter> Header Files</ Filter></ ClInclude>
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\Status.h"> <ClInclude Include = "..\dependencies\zydis\msvc\ZydisExportConfig.h">
<Filter>Header Files\Zycore</Filter> <Filter> Header Files</ Filter></ ClInclude></ ItemGroup></ Project>
</ClInclude>
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\String.h">
<Filter>Header Files\Zycore</Filter>
</ClInclude>
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\Types.h">
<Filter>Header Files\Zycore</Filter>
</ClInclude>
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\Vector.h">
<Filter>Header Files\Zycore</Filter>
</ClInclude>
<ClInclude Include="..\dependencies\zydis\dependencies\zycore\include\Zycore\Zycore.h">
<Filter>Header Files\Zycore</Filter>
</ClInclude>
<ClInclude Include="..\dependencies\zydis\msvc\ZycoreExportConfig.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\dependencies\zydis\msvc\ZydisExportConfig.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

@ -3,226 +3,229 @@
namespace vm namespace vm
{ {
namespace util namespace util
{
namespace reg
{ {
namespace reg zydis_register_t to64(zydis_register_t reg)
{ {
zydis_register_t to64(zydis_register_t reg) return ZydisRegisterGetLargestEnclosing(ZYDIS_MACHINE_MODE_LONG_64,
{ reg);
return ZydisRegisterGetLargestEnclosing( }
ZYDIS_MACHINE_MODE_LONG_64, reg);
} bool compare(zydis_register_t a, zydis_register_t b)
{
bool compare(zydis_register_t a, zydis_register_t b) return to64(a) == to64(b);
{ }
return to64(a) == to64(b); } // namespace reg
}
} void print(const zydis_decoded_instr_t &instr)
{
void print(const zydis_decoded_instr_t& instr) char buffer[256];
{ ZydisFormatter formatter;
char buffer[256]; ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL);
ZydisFormatter formatter; ZydisFormatterFormatInstruction(&formatter, &instr, buffer,
ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL); sizeof(buffer), 0u);
ZydisFormatterFormatInstruction(&formatter, &instr,
buffer, sizeof(buffer), 0u); puts(buffer);
}
puts(buffer);
} void print(zydis_routine_t &routine)
{
void print(zydis_routine_t& routine) char buffer[256];
{ ZydisFormatter formatter;
char buffer[256]; ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL);
ZydisFormatter formatter;
ZydisFormatterInit(&formatter, ZYDIS_FORMATTER_STYLE_INTEL); for (auto [instr, raw, addr] : routine)
{
for (auto [instr, raw, addr] : routine) std::printf("> 0x%p ", addr);
{ ZydisFormatterFormatInstruction(&formatter, &instr, buffer,
std::printf("> 0x%p ", addr); sizeof(buffer), addr);
ZydisFormatterFormatInstruction(&formatter, &instr,
buffer, sizeof(buffer), addr); puts(buffer);
}
puts(buffer); }
}
} bool is_jmp(const zydis_decoded_instr_t &instr)
{
bool is_jmp(const zydis_decoded_instr_t& instr) switch (instr.mnemonic)
{ {
switch (instr.mnemonic) case ZYDIS_MNEMONIC_JB:
{ case ZYDIS_MNEMONIC_JBE:
case ZYDIS_MNEMONIC_JB: case ZYDIS_MNEMONIC_JCXZ:
case ZYDIS_MNEMONIC_JBE: case ZYDIS_MNEMONIC_JECXZ:
case ZYDIS_MNEMONIC_JCXZ: case ZYDIS_MNEMONIC_JKNZD:
case ZYDIS_MNEMONIC_JECXZ: case ZYDIS_MNEMONIC_JKZD:
case ZYDIS_MNEMONIC_JKNZD: case ZYDIS_MNEMONIC_JL:
case ZYDIS_MNEMONIC_JKZD: case ZYDIS_MNEMONIC_JLE:
case ZYDIS_MNEMONIC_JL: case ZYDIS_MNEMONIC_JMP:
case ZYDIS_MNEMONIC_JLE: case ZYDIS_MNEMONIC_JNB:
case ZYDIS_MNEMONIC_JMP: case ZYDIS_MNEMONIC_JNBE:
case ZYDIS_MNEMONIC_JNB: case ZYDIS_MNEMONIC_JNL:
case ZYDIS_MNEMONIC_JNBE: case ZYDIS_MNEMONIC_JNLE:
case ZYDIS_MNEMONIC_JNL: case ZYDIS_MNEMONIC_JNO:
case ZYDIS_MNEMONIC_JNLE: case ZYDIS_MNEMONIC_JNP:
case ZYDIS_MNEMONIC_JNO: case ZYDIS_MNEMONIC_JNS:
case ZYDIS_MNEMONIC_JNP: case ZYDIS_MNEMONIC_JNZ:
case ZYDIS_MNEMONIC_JNS: case ZYDIS_MNEMONIC_JO:
case ZYDIS_MNEMONIC_JNZ: case ZYDIS_MNEMONIC_JP:
case ZYDIS_MNEMONIC_JO: case ZYDIS_MNEMONIC_JRCXZ:
case ZYDIS_MNEMONIC_JP: case ZYDIS_MNEMONIC_JS:
case ZYDIS_MNEMONIC_JRCXZ: case ZYDIS_MNEMONIC_JZ:
case ZYDIS_MNEMONIC_JS: return true;
case ZYDIS_MNEMONIC_JZ: default:
return true; break;
default: }
break; return false;
} }
return false;
} bool flatten(zydis_routine_t &routine, std::uintptr_t routine_addr,
bool keep_jmps)
bool flatten(zydis_routine_t& routine, std::uintptr_t routine_addr, bool keep_jmps) {
{ ZydisDecoder decoder;
ZydisDecoder decoder; zydis_decoded_instr_t instr;
zydis_decoded_instr_t instr; ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64,
ZydisDecoderInit(&decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_ADDRESS_WIDTH_64); ZYDIS_ADDRESS_WIDTH_64);
while (ZYAN_SUCCESS(ZydisDecoderDecodeBuffer(&decoder, reinterpret_cast<void*>( while (ZYAN_SUCCESS(ZydisDecoderDecodeBuffer(
routine_addr), 0x1000, &instr))) &decoder, reinterpret_cast<void *>(routine_addr), 0x1000, &instr)))
{ {
std::vector<u8> raw_instr; std::vector<u8> raw_instr;
raw_instr.insert(raw_instr.begin(), raw_instr.insert(raw_instr.begin(), (u8 *)routine_addr,
(u8*)routine_addr, (u8 *)routine_addr + instr.length);
(u8*)routine_addr + instr.length);
if (is_jmp(instr))
if (is_jmp(instr)) {
{ if (instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER)
if (instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER) {
{ routine.push_back({instr, raw_instr, routine_addr});
routine.push_back({ instr, raw_instr, routine_addr }); return true;
return true; }
}
if (keep_jmps)
if (keep_jmps) routine.push_back({instr, raw_instr, routine_addr});
routine.push_back({ instr, raw_instr, routine_addr });
ZydisCalcAbsoluteAddress(&instr, &instr.operands[0], routine_addr,
ZydisCalcAbsoluteAddress(&instr, &instr.operands[0], routine_addr, &routine_addr); &routine_addr);
} }
else if (instr.mnemonic == ZYDIS_MNEMONIC_RET) else if (instr.mnemonic == ZYDIS_MNEMONIC_RET)
{ {
routine.push_back({ instr, raw_instr, routine_addr }); routine.push_back({instr, raw_instr, routine_addr});
return true; return true;
} }
else else
{ {
routine.push_back({ instr, raw_instr, routine_addr }); routine.push_back({instr, raw_instr, routine_addr});
routine_addr += instr.length; routine_addr += instr.length;
} }
} }
return false; return false;
} }
void deobfuscate(zydis_routine_t& routine) void deobfuscate(zydis_routine_t &routine)
{ {
static const auto _uses = static const auto _uses = [](ZydisDecodedOperand &op,
[](ZydisDecodedOperand& op, zydis_register_t reg) -> bool zydis_register_t reg) -> bool {
{ switch (op.type)
switch (op.type) {
{ case ZYDIS_OPERAND_TYPE_MEMORY:
case ZYDIS_OPERAND_TYPE_MEMORY: {
{ return reg::compare(op.mem.base, reg) ||
return reg::compare(op.mem.base, reg) || reg::compare(op.mem.index, reg); reg::compare(op.mem.index, reg);
} }
case ZYDIS_OPERAND_TYPE_REGISTER: case ZYDIS_OPERAND_TYPE_REGISTER:
{ {
return reg::compare(op.reg.value, reg); return reg::compare(op.reg.value, reg);
} }
} }
return false; return false;
}; };
static const auto _writes = static const auto _writes = [](zydis_decoded_instr_t &inst) -> bool {
[](zydis_decoded_instr_t& inst) -> bool for (auto idx = 0; idx < inst.operand_count; ++idx)
{ if (inst.operands[idx].actions & ZYDIS_OPERAND_ACTION_MASK_WRITE)
for (auto idx = 0; idx < inst.operand_count; ++idx) return true;
if (inst.operands[idx].actions & ZYDIS_OPERAND_ACTION_MASK_WRITE)
return true; return false;
};
return false;
}; static const auto _remove =
[](zydis_routine_t &routine, zydis_routine_t::iterator itr,
static const auto _remove = zydis_register_t reg, u32 opcode_size) -> void {
[](zydis_routine_t& routine, zydis_routine_t::iterator itr, for (; itr >= routine.begin(); --itr)
zydis_register_t reg, u32 opcode_size) -> void {
{ const auto instruction = &itr->instr;
for (; itr >= routine.begin(); --itr) bool stop = false;
{
const auto instruction = &itr->instr; if (instruction->mnemonic == ZYDIS_MNEMONIC_JMP)
bool stop = false; continue;
if (instruction->mnemonic == ZYDIS_MNEMONIC_JMP) for (auto op_idx = 0u; op_idx < instruction->operand_count;
continue; ++op_idx)
{
for (auto op_idx = 0u; op_idx < instruction->operand_count; ++op_idx) const auto op = &instruction->operands[op_idx];
{
const auto op = &instruction->operands[op_idx]; if (!_uses(*op, reg))
continue;
if (!_uses(*op, reg))
continue; if (op->type == ZYDIS_OPERAND_TYPE_MEMORY)
{
if (op->type == ZYDIS_OPERAND_TYPE_MEMORY) stop = true;
{ continue;
stop = true; }
continue;
} if (opcode_size < 32 && op->size > opcode_size)
continue;
if (opcode_size < 32 && op->size > opcode_size)
continue; if (op->actions & ZYDIS_OPERAND_ACTION_MASK_WRITE)
op->actions &= ~ZYDIS_OPERAND_ACTION_MASK_WRITE;
if (op->actions & ZYDIS_OPERAND_ACTION_MASK_WRITE) else
op->actions &= ~ZYDIS_OPERAND_ACTION_MASK_WRITE; stop = true;
else stop = true; }
}
if (!_writes(*instruction))
if (!_writes(*instruction)) routine.erase(itr);
routine.erase(itr);
else if (stop)
else if (stop) break; break;
} }
}; };
for (const auto& instr_data : routine) for (const auto &instr_data : routine)
{ {
if (routine.empty() || routine.size() == 1 || if (routine.empty() || routine.size() == 1 ||
instr_data.instr.mnemonic == ZYDIS_MNEMONIC_JMP) instr_data.instr.mnemonic == ZYDIS_MNEMONIC_JMP)
continue; continue;
for (auto itr = routine.begin() + 1; itr != routine.end(); itr++) for (auto itr = routine.begin() + 1; itr != routine.end(); itr++)
{ {
if (itr->instr.mnemonic == ZYDIS_MNEMONIC_JMP || if (itr->instr.mnemonic == ZYDIS_MNEMONIC_JMP ||
itr->instr.mnemonic == ZYDIS_MNEMONIC_RET) itr->instr.mnemonic == ZYDIS_MNEMONIC_RET)
break; break;
// find the write operations that happen... // find the write operations that happen...
for (auto idx = 0u; idx < itr->instr.operand_count; ++idx) for (auto idx = 0u; idx < itr->instr.operand_count; ++idx)
{ {
const auto op = &itr->instr.operands[idx]; const auto op = &itr->instr.operands[idx];
// if its a read, continue to next opcode... // if its a read, continue to next opcode...
if (op->actions & ZYDIS_OPERAND_ACTION_MASK_READ) if (op->actions & ZYDIS_OPERAND_ACTION_MASK_READ)
continue; continue;
// if its not a write then continue to next opcode... // if its not a write then continue to next opcode...
if (!(op->actions & ZYDIS_OPERAND_ACTION_MASK_WRITE)) if (!(op->actions & ZYDIS_OPERAND_ACTION_MASK_WRITE))
continue; continue;
// if this operand is not a register then we continue... // if this operand is not a register then we continue...
if (op->type != ZYDIS_OPERAND_TYPE_REGISTER) if (op->type != ZYDIS_OPERAND_TYPE_REGISTER)
continue; continue;
// else we see if we can remove dead writes to this register... // else we see if we can remove dead writes to this
_remove(routine, itr - 1, op->reg.value, op->size); // register...
} _remove(routine, itr - 1, op->reg.value, op->size);
} }
} }
} }
} }
} } // namespace util
} // namespace vm
Loading…
Cancel
Save