|
|
@ -1,72 +1,75 @@
|
|
|
|
#include <vmlocate.hpp>
|
|
|
|
#include <vmlocate.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
namespace vm::locate
|
|
|
|
namespace vm::locate {
|
|
|
|
{
|
|
|
|
std::uintptr_t sigscan(void* base,
|
|
|
|
std::uintptr_t sigscan( void *base, std::uint32_t size, const char *pattern, const char *mask )
|
|
|
|
std::uint32_t size,
|
|
|
|
{
|
|
|
|
const char* pattern,
|
|
|
|
static const auto check_mask = [ & ]( const char *base, const char *pattern, const char *mask ) -> bool {
|
|
|
|
const char* mask) {
|
|
|
|
for ( ; *mask; ++base, ++pattern, ++mask )
|
|
|
|
static const auto check_mask = [&](const char* base, const char* pattern,
|
|
|
|
if ( *mask == 'x' && *base != *pattern )
|
|
|
|
const char* mask) -> bool {
|
|
|
|
|
|
|
|
for (; *mask; ++base, ++pattern, ++mask)
|
|
|
|
|
|
|
|
if (*mask == 'x' && *base != *pattern)
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
size -= std::strlen( mask );
|
|
|
|
size -= std::strlen(mask);
|
|
|
|
for ( auto i = 0; i <= size; ++i )
|
|
|
|
for (auto i = 0; i <= size; ++i) {
|
|
|
|
{
|
|
|
|
void* addr = (void*)&(((char*)base)[i]);
|
|
|
|
void *addr = ( void * )&( ( ( char * )base )[ i ] );
|
|
|
|
if (check_mask((char*)addr, pattern, mask))
|
|
|
|
if ( check_mask( ( char * )addr, pattern, mask ) )
|
|
|
|
return reinterpret_cast<std::uintptr_t>(addr);
|
|
|
|
return reinterpret_cast< std::uintptr_t >( addr );
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector< vm_enter_t > get_vm_entries( std::uintptr_t module_base, std::uint32_t module_size )
|
|
|
|
std::vector<vm_enter_t> get_vm_entries(std::uintptr_t module_base,
|
|
|
|
{
|
|
|
|
std::uint32_t module_size) {
|
|
|
|
std::uintptr_t result = module_base;
|
|
|
|
std::uintptr_t result = module_base;
|
|
|
|
std::vector< vm_enter_t > entries;
|
|
|
|
std::vector<vm_enter_t> entries;
|
|
|
|
|
|
|
|
|
|
|
|
static const auto push_regs = [ & ]( const zydis_routine_t &rtn ) -> bool {
|
|
|
|
static const auto push_regs = [&](const zydis_routine_t& rtn) -> bool {
|
|
|
|
for ( unsigned reg = ZYDIS_REGISTER_RAX; reg < ZYDIS_REGISTER_R15; ++reg )
|
|
|
|
for (unsigned reg = ZYDIS_REGISTER_RAX; reg < ZYDIS_REGISTER_R15; ++reg) {
|
|
|
|
{
|
|
|
|
auto res = std::find_if(
|
|
|
|
auto res = std::find_if( rtn.begin(), rtn.end(), [ & ]( const zydis_instr_t &instr ) -> bool {
|
|
|
|
rtn.begin(), rtn.end(), [&](const zydis_instr_t& instr) -> bool {
|
|
|
|
return instr.instr.mnemonic == ZYDIS_MNEMONIC_PUSH &&
|
|
|
|
return instr.instr.mnemonic == ZYDIS_MNEMONIC_PUSH &&
|
|
|
|
instr.instr.operands[ 0 ].type == ZYDIS_OPERAND_TYPE_REGISTER &&
|
|
|
|
instr.instr.operands[0].type ==
|
|
|
|
instr.instr.operands[ 0 ].reg.value == reg;
|
|
|
|
ZYDIS_OPERAND_TYPE_REGISTER &&
|
|
|
|
} );
|
|
|
|
instr.instr.operands[0].reg.value == reg;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// skip RSP push...
|
|
|
|
// skip RSP push...
|
|
|
|
if ( res == rtn.end() && reg != ZYDIS_REGISTER_RSP )
|
|
|
|
if (res == rtn.end() && reg != ZYDIS_REGISTER_RSP)
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
do {
|
|
|
|
{
|
|
|
|
result = sigscan((void*)++result, module_size - (result - module_base),
|
|
|
|
result = sigscan( ( void * )++result, module_size - ( result - module_base ), PUSH_4B_IMM, PUSH_4B_MASK );
|
|
|
|
PUSH_4B_IMM, PUSH_4B_MASK);
|
|
|
|
|
|
|
|
|
|
|
|
zydis_routine_t rtn;
|
|
|
|
zydis_routine_t rtn;
|
|
|
|
if ( !scn::executable( module_base, result ) )
|
|
|
|
if (!scn::executable(module_base, result))
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
if ( !vm::util::flatten( rtn, result, false, 500, module_base ) )
|
|
|
|
if (!vm::util::flatten(rtn, result, false, 500, module_base))
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// the last instruction in the stream should be a JMP to a register or a return instruction...
|
|
|
|
// the last instruction in the stream should be a JMP to a register or a
|
|
|
|
const auto &last_instr = rtn[ rtn.size() - 1 ];
|
|
|
|
// return instruction...
|
|
|
|
if ( !( ( last_instr.instr.mnemonic == ZYDIS_MNEMONIC_JMP &&
|
|
|
|
const auto& last_instr = rtn[rtn.size() - 1];
|
|
|
|
last_instr.instr.operands[ 0 ].type == ZYDIS_OPERAND_TYPE_REGISTER ) ||
|
|
|
|
if (!((last_instr.instr.mnemonic == ZYDIS_MNEMONIC_JMP &&
|
|
|
|
last_instr.instr.mnemonic == ZYDIS_MNEMONIC_RET ) )
|
|
|
|
last_instr.instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER) ||
|
|
|
|
|
|
|
|
last_instr.instr.mnemonic == ZYDIS_MNEMONIC_RET))
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
std::uint8_t num_pushs = 0u;
|
|
|
|
std::uint8_t num_pushs = 0u;
|
|
|
|
std::for_each( rtn.begin(), rtn.end(), [ & ]( const zydis_instr_t &instr ) {
|
|
|
|
std::for_each(rtn.begin(), rtn.end(), [&](const zydis_instr_t& instr) {
|
|
|
|
if ( instr.instr.mnemonic == ZYDIS_MNEMONIC_PUSH &&
|
|
|
|
if (instr.instr.mnemonic == ZYDIS_MNEMONIC_PUSH &&
|
|
|
|
instr.instr.operands[ 0 ].type == ZYDIS_OPERAND_TYPE_IMMEDIATE )
|
|
|
|
instr.instr.operands[0].type == ZYDIS_OPERAND_TYPE_IMMEDIATE)
|
|
|
|
++num_pushs;
|
|
|
|
++num_pushs;
|
|
|
|
} );
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
only one legit imm pushes for every vm entry...
|
|
|
|
only one legit imm pushes for every vm entry...
|
|
|
@ -90,14 +93,14 @@ namespace vm::locate
|
|
|
|
> 0x7a53 : push rbx
|
|
|
|
> 0x7a53 : push rbx
|
|
|
|
> 0x500d : push r15
|
|
|
|
> 0x500d : push r15
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
if ( num_pushs != 1 )
|
|
|
|
if (num_pushs != 1)
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// check for a pushfq...
|
|
|
|
// check for a pushfq...
|
|
|
|
// > 0x4926 : pushfq <---
|
|
|
|
// > 0x4926 : pushfq <---
|
|
|
|
if ( !vm::locate::find( rtn, [ & ]( const zydis_instr_t &instr ) -> bool {
|
|
|
|
if (!vm::locate::find(rtn, [&](const zydis_instr_t& instr) -> bool {
|
|
|
|
return instr.instr.mnemonic == ZYDIS_MNEMONIC_PUSHFQ;
|
|
|
|
return instr.instr.mnemonic == ZYDIS_MNEMONIC_PUSHFQ;
|
|
|
|
} ) )
|
|
|
|
}))
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
@ -120,51 +123,42 @@ namespace vm::locate
|
|
|
|
> 0x7a53 : push rbx
|
|
|
|
> 0x7a53 : push rbx
|
|
|
|
> 0x500d : push r15
|
|
|
|
> 0x500d : push r15
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
if ( !push_regs( rtn ) )
|
|
|
|
if (!push_regs(rtn))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// check for a mov r13, rax...
|
|
|
|
|
|
|
|
if ( !vm::locate::find( rtn, [ & ]( const zydis_instr_t &instr ) -> bool {
|
|
|
|
|
|
|
|
return instr.instr.mnemonic == ZYDIS_MNEMONIC_MOV &&
|
|
|
|
|
|
|
|
instr.instr.operands[ 0 ].type == ZYDIS_OPERAND_TYPE_REGISTER &&
|
|
|
|
|
|
|
|
instr.instr.operands[ 0 ].reg.value == ZYDIS_REGISTER_R13 &&
|
|
|
|
|
|
|
|
instr.instr.operands[ 1 ].type == ZYDIS_OPERAND_TYPE_REGISTER &&
|
|
|
|
|
|
|
|
instr.instr.operands[ 1 ].reg.value == ZYDIS_REGISTER_RAX;
|
|
|
|
|
|
|
|
} ) )
|
|
|
|
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// check for a mov reg, rsp
|
|
|
|
// check for a mov reg, rsp
|
|
|
|
if ( !vm::locate::find( rtn, [ & ]( const zydis_instr_t &instr ) -> bool {
|
|
|
|
if (!vm::locate::find(rtn, [&](const zydis_instr_t& instr) -> bool {
|
|
|
|
return instr.instr.mnemonic == ZYDIS_MNEMONIC_MOV &&
|
|
|
|
return instr.instr.mnemonic == ZYDIS_MNEMONIC_MOV &&
|
|
|
|
instr.instr.operands[ 0 ].type == ZYDIS_OPERAND_TYPE_REGISTER &&
|
|
|
|
instr.instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER &&
|
|
|
|
instr.instr.operands[ 1 ].type == ZYDIS_OPERAND_TYPE_REGISTER &&
|
|
|
|
instr.instr.operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER &&
|
|
|
|
instr.instr.operands[ 1 ].reg.value == ZYDIS_REGISTER_RSP;
|
|
|
|
instr.instr.operands[1].reg.value == ZYDIS_REGISTER_RSP;
|
|
|
|
} ) )
|
|
|
|
}))
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// check for a mov reg, [rsp+0x90]
|
|
|
|
// check for a mov reg, [rsp+0x90]
|
|
|
|
if ( !vm::locate::find( rtn, [ & ]( const zydis_instr_t &instr ) -> bool {
|
|
|
|
if (!vm::locate::find(rtn, [&](const zydis_instr_t& instr) -> bool {
|
|
|
|
return instr.instr.mnemonic == ZYDIS_MNEMONIC_MOV &&
|
|
|
|
return instr.instr.mnemonic == ZYDIS_MNEMONIC_MOV &&
|
|
|
|
instr.instr.operands[ 0 ].type == ZYDIS_OPERAND_TYPE_REGISTER &&
|
|
|
|
instr.instr.operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER &&
|
|
|
|
instr.instr.operands[ 1 ].type == ZYDIS_OPERAND_TYPE_MEMORY &&
|
|
|
|
instr.instr.operands[1].type == ZYDIS_OPERAND_TYPE_MEMORY &&
|
|
|
|
instr.instr.operands[ 1 ].mem.base == ZYDIS_REGISTER_RSP &&
|
|
|
|
instr.instr.operands[1].mem.base == ZYDIS_REGISTER_RSP &&
|
|
|
|
instr.instr.operands[ 1 ].mem.disp.value == 0x90;
|
|
|
|
instr.instr.operands[1].mem.disp.value == 0x90;
|
|
|
|
} ) )
|
|
|
|
}))
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// if code execution gets to here then we can assume this is a legit vm
|
|
|
|
// if code execution gets to here then we can assume this is a legit vm
|
|
|
|
// entry... its time to build a vm_enter_t... first we check to see if an
|
|
|
|
// entry... its time to build a vm_enter_t... first we check to see if an
|
|
|
|
// existing entry already exits...
|
|
|
|
// existing entry already exits...
|
|
|
|
|
|
|
|
|
|
|
|
auto push_val = ( std::uint32_t )rtn[ 0 ].instr.operands[ 0 ].imm.value.u;
|
|
|
|
auto push_val = (std::uint32_t)rtn[0].instr.operands[0].imm.value.u;
|
|
|
|
if ( std::find_if( entries.begin(), entries.end(), [ & ]( const vm_enter_t &vm_enter ) -> bool {
|
|
|
|
if (std::find_if(entries.begin(), entries.end(),
|
|
|
|
|
|
|
|
[&](const vm_enter_t& vm_enter) -> bool {
|
|
|
|
return vm_enter.encrypted_rva == push_val;
|
|
|
|
return vm_enter.encrypted_rva == push_val;
|
|
|
|
} ) != entries.end() )
|
|
|
|
}) != entries.end())
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
vm_enter_t entry{ ( std::uint32_t )( result - module_base ), push_val };
|
|
|
|
vm_enter_t entry{(std::uint32_t)(result - module_base), push_val};
|
|
|
|
entries.push_back( entry );
|
|
|
|
entries.push_back(entry);
|
|
|
|
} while ( result );
|
|
|
|
} while (result);
|
|
|
|
return entries;
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace vm::locate
|
|
|
|
} // namespace vm::locate
|