From b123113171a624639df771e9cf5c10eff3582e50 Mon Sep 17 00:00:00 2001 From: _xeroxz Date: Fri, 13 Aug 2021 21:40:46 -0700 Subject: [PATCH 1/4] added support for virtual jmp tables... TODO: make sure im serializing .vmp2 file format v4 correctly... --- dependencies/vmprofiler | 2 +- src/main.cpp | 60 +++++++++++++++++++++++++++---------- src/vmemu_t.cpp | 66 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 111 insertions(+), 17 deletions(-) diff --git a/dependencies/vmprofiler b/dependencies/vmprofiler index f06bf5f..fb59f8f 160000 --- a/dependencies/vmprofiler +++ b/dependencies/vmprofiler @@ -1 +1 @@ -Subproject commit f06bf5f6b72b1d52a6156d6828675c77ceaee7e2 +Subproject commit fb59f8f58f86a7abe461999315bfbd40bca9622c diff --git a/src/main.cpp b/src/main.cpp index e5d09f2..0252fb1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -92,50 +92,80 @@ int __cdecl main( int argc, const char *argv[] ) if ( code_block.jcc.has_jcc ) { - if ( code_block.jcc.type == vm::instrs::jcc_type::branching ) + switch ( code_block.jcc.type ) + { + case vm::instrs::jcc_type::branching: + { std::printf( "> branch 1 = %p, branch 2 = %p\n", code_block.jcc.block_addr[ 0 ], code_block.jcc.block_addr[ 1 ] ); - else + break; + } + case vm::instrs::jcc_type::absolute: + { std::printf( "> branch 1 = %p\n", code_block.jcc.block_addr[ 0 ] ); + break; + } + case vm::instrs::jcc_type::switch_case: + { + std::printf( "> switch case blocks:\n" ); + for ( auto idx = 0u; idx < code_block.jcc.block_addr.size(); ++idx ) + std::printf( " case block at = 0x%p\n", code_block.jcc.block_addr[ idx ] ); + break; + } + } } } std::printf( "> serializing results....\n" ); - vmp2::v3::file_header file_header; + vmp2::v4::file_header file_header; file_header.magic = VMP_MAGIC; file_header.epoch_time = std::time( nullptr ); - file_header.version = vmp2::version_t::v3; + file_header.version = vmp2::version_t::v4; file_header.module_base = module_base; file_header.image_base = image_base; file_header.vm_entry_rva = vm_entry_rva; file_header.module_offset = sizeof file_header; file_header.module_size = image_size; - file_header.code_block_offset = image_size + sizeof file_header; - file_header.code_block_count = code_blocks.size(); + file_header.rtn_count = 1; + file_header.rtn_offset = image_size + sizeof file_header; + vmp2::v4::rtn_t rtn{ code_blocks.size() }; std::ofstream output( parser.get< std::string >( "out" ), std::ios::binary ); output.write( reinterpret_cast< const char * >( &file_header ), sizeof file_header ); output.write( reinterpret_cast< const char * >( module_base ), image_size ); + output.write( reinterpret_cast< const char * >( &rtn ), sizeof vmp2::v4::rtn_t::code_block_count ); for ( const auto &code_block : code_blocks ) { - const auto _code_block_size = - ( code_block.vinstrs.size() * sizeof vm::instrs::virt_instr_t ) + sizeof vmp2::v3::code_block_t; + const auto _code_block_size = sizeof vmp2::v4::code_block_t + ( code_block.jcc.block_addr.size() * 8 ); - vmp2::v3::code_block_t *_code_block = - reinterpret_cast< vmp2::v3::code_block_t * >( malloc( _code_block_size ) ); + vmp2::v4::code_block_t *_code_block = + reinterpret_cast< vmp2::v4::code_block_t * >( malloc( _code_block_size ) ); + // serialize block meta data... _code_block->vip_begin = code_block.vip_begin; - _code_block->jcc = code_block.jcc; _code_block->next_block_offset = _code_block_size; - _code_block->vinstr_count = code_block.vinstrs.size(); + _code_block->jcc.num_block_addrs = code_block.jcc.block_addr.size(); + + // serialize jcc data... + for ( auto idx = 0u; idx < code_block.jcc.block_addr.size(); ++idx ) + _code_block->jcc.block_addr[ idx ] = code_block.jcc.block_addr[ idx ]; + + output.write( reinterpret_cast< const char * >( _code_block ), + sizeof vmp2::v4::code_block_t + ( code_block.jcc.block_addr.size() * 8 ) ); + + // serialize virtual instructions... + auto vinstr_count = ( std::uint32_t )code_block.vinstrs.size(); + output.write( reinterpret_cast< const char * >( &vinstr_count ), sizeof vinstr_count ); for ( auto idx = 0u; idx < code_block.vinstrs.size(); ++idx ) - _code_block->vinstr[ idx ] = code_block.vinstrs[ idx ]; + { + const auto& vinstr = code_block.vinstrs[ idx ]; + output.write( reinterpret_cast< const char * >( &vinstr ), sizeof vinstr ); + } - output.write( reinterpret_cast< const char * >( _code_block ), _code_block_size ); + free( _code_block ); } - output.close(); } else if ( parser.exists( "unpack" ) && parser.exists( "out" ) ) diff --git a/src/vmemu_t.cpp b/src/vmemu_t.cpp index 5b18a7b..f17c388 100644 --- a/src/vmemu_t.cpp +++ b/src/vmemu_t.cpp @@ -209,6 +209,64 @@ namespace vm code_blocks.push_back( branch_block ); break; } + case vm::instrs::jcc_type::switch_case: + { + for ( auto _idx = 0u; _idx < _code_block.code_block.jcc.block_addr.size(); ++_idx ) + { + if ( std::find( vip_begins.begin(), vip_begins.end(), + _code_block.code_block.jcc.block_addr[ _idx ] ) != vip_begins.end() ) + continue; + + std::uintptr_t rbp = 0ull; + std::uint32_t branch_rva = + ( _code_block.code_block.jcc.block_addr[ _idx ] - g_vm_ctx->module_base ) + + g_vm_ctx->image_base; + + // setup object globals so that the tracing will work... + code_block_data_t branch_block{ {}, nullptr, nullptr }; + cc_block = &branch_block; + g_vm_ctx = _code_block.g_vm_ctx.get(); + + // restore register values... + if ( ( err = uc_context_restore( uc_ctx, _code_block.cpu_ctx->context ) ) ) + { + std::printf( "> failed to restore emu context... reason = %d\n", err ); + return false; + } + + // restore stack values... + if ( ( err = uc_mem_write( uc_ctx, STACK_BASE, _code_block.cpu_ctx->stack, STACK_SIZE ) ) ) + { + std::printf( "> failed to restore stack... reason = %d\n", err ); + return false; + } + + // get the address in rbp (top of vsp)... then patch the branch rva... + if ( ( err = uc_reg_read( uc_ctx, UC_X86_REG_RBP, &rbp ) ) ) + { + std::printf( "> failed to read rbp... reason = %d\n", err ); + return false; + } + + // patch the branch rva... + if ( ( err = uc_mem_write( uc_ctx, rbp, &branch_rva, sizeof branch_rva ) ) ) + { + std::printf( "> failed to patch branch rva... reason = %d\n", err ); + return false; + } + + std::printf( "> beginning execution at = 0x%p\n", _code_block.cpu_ctx->rip ); + if ( ( err = uc_emu_start( uc_ctx, _code_block.cpu_ctx->rip, 0ull, 0ull, 0ull ) ) ) + { + std::printf( "> error starting emu... reason = %d\n", err ); + return false; + } + + // push back new block that has been traced... + code_blocks.push_back( branch_block ); + } + break; + } } } @@ -231,6 +289,13 @@ namespace vm ( code_block.jcc.block_addr[ 0 ] - g_vm_ctx->module_base ) + g_vm_ctx->image_base; break; } + case vm::instrs::jcc_type::switch_case: + { + for ( auto idx = 0u; idx < code_block.jcc.block_addr.size(); ++idx ) + code_block.jcc.block_addr[ idx ] = + ( code_block.jcc.block_addr[ idx ] - g_vm_ctx->module_base ) + g_vm_ctx->image_base; + break; + } } } entries.push_back( code_block ); @@ -415,7 +480,6 @@ namespace vm } obj->cc_block->code_block.vinstrs.push_back( vinstr.value() ); - std::printf( "> %s %p\n", vm_handler.profile ? vm_handler.profile->name : "UNK", vm_handler_addr ); if ( vm_handler.profile ) { From 07fd9cc99998a21bf1ccec74db8e1b56067223f7 Mon Sep 17 00:00:00 2001 From: _xeroxz Date: Sat, 14 Aug 2021 18:18:29 -0700 Subject: [PATCH 2/4] updated vmp2 file format structures and how i serialize them... --- dependencies/vmprofiler | 2 +- src/main.cpp | 142 +++++++++++++++++++++++++++++++++------- 2 files changed, 120 insertions(+), 24 deletions(-) diff --git a/dependencies/vmprofiler b/dependencies/vmprofiler index fb59f8f..b336e01 160000 --- a/dependencies/vmprofiler +++ b/dependencies/vmprofiler @@ -1 +1 @@ -Subproject commit fb59f8f58f86a7abe461999315bfbd40bca9622c +Subproject commit b336e0145d3655ebd7b43abd7338016f36640ab0 diff --git a/src/main.cpp b/src/main.cpp index 0252fb1..e5de3c7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,6 +13,10 @@ int __cdecl main( int argc, const char *argv[] ) parser.add_argument().name( "--bin" ).description( "path to unpacked virtualized binary..." ); parser.add_argument().name( "--out" ).description( "output file name..." ); parser.add_argument().name( "--unpack" ).description( "unpack a vmp2 binary..." ); + parser.add_argument() + .name( "--emuall" ) + .description( "scan for all vm enters and trace all of them... this may take a few minutes..." ); + parser.add_argument() .name( "--locateconst" ) .description( "scan all vm enters for a specific constant value...\n" ); @@ -137,7 +141,8 @@ int __cdecl main( int argc, const char *argv[] ) for ( const auto &code_block : code_blocks ) { - const auto _code_block_size = sizeof vmp2::v4::code_block_t + ( code_block.jcc.block_addr.size() * 8 ); + const auto _code_block_size = sizeof vmp2::v4::code_block_t + ( code_block.jcc.block_addr.size() * 8 ) + + code_block.vinstrs.size() * sizeof vm::instrs::virt_instr_t; vmp2::v4::code_block_t *_code_block = reinterpret_cast< vmp2::v4::code_block_t * >( malloc( _code_block_size ) ); @@ -145,25 +150,23 @@ int __cdecl main( int argc, const char *argv[] ) // serialize block meta data... _code_block->vip_begin = code_block.vip_begin; _code_block->next_block_offset = _code_block_size; - _code_block->jcc.num_block_addrs = code_block.jcc.block_addr.size(); + _code_block->vinstr_count = code_block.vinstrs.size(); + _code_block->has_jcc = code_block.jcc.has_jcc; + _code_block->jcc_type = code_block.jcc.type; + _code_block->num_block_addrs = code_block.jcc.block_addr.size(); - // serialize jcc data... + // serialize jcc branches... for ( auto idx = 0u; idx < code_block.jcc.block_addr.size(); ++idx ) - _code_block->jcc.block_addr[ idx ] = code_block.jcc.block_addr[ idx ]; - - output.write( reinterpret_cast< const char * >( _code_block ), - sizeof vmp2::v4::code_block_t + ( code_block.jcc.block_addr.size() * 8 ) ); + _code_block->branch_addr[ idx ] = code_block.jcc.block_addr[ idx ]; - // serialize virtual instructions... - auto vinstr_count = ( std::uint32_t )code_block.vinstrs.size(); - output.write( reinterpret_cast< const char * >( &vinstr_count ), sizeof vinstr_count ); + auto block_vinstrs = reinterpret_cast< vm::instrs::virt_instr_t * >( + reinterpret_cast< std::uintptr_t >( _code_block ) + sizeof vmp2::v4::code_block_t + + ( code_block.jcc.block_addr.size() * 8 ) ); for ( auto idx = 0u; idx < code_block.vinstrs.size(); ++idx ) - { - const auto& vinstr = code_block.vinstrs[ idx ]; - output.write( reinterpret_cast< const char * >( &vinstr ), sizeof vinstr ); - } + block_vinstrs[ idx ] = code_block.vinstrs[ idx ]; + output.write( reinterpret_cast< const char * >( _code_block ), _code_block_size ); free( _code_block ); } output.close(); @@ -196,28 +199,121 @@ int __cdecl main( int argc, const char *argv[] ) output.write( reinterpret_cast< char * >( unpacked_bin.data() ), unpacked_bin.size() ); output.close(); } - else if ( parser.exists( "bin" ) && parser.exists( "locateconst" ) ) + else if ( parser.exists( "bin" ) && parser.exists( "emuall" ) && parser.exists( "out" ) ) { const auto module_base = reinterpret_cast< std::uintptr_t >( LoadLibraryExA( parser.get< std::string >( "bin" ).c_str(), NULL, DONT_RESOLVE_DLL_REFERENCES ) ); - const auto const_val = std::strtoull( parser.get< std::string >( "locateconst" ).c_str(), nullptr, 16 ); const auto image_base = umtils->image_base( parser.get< std::string >( "bin" ).c_str() ); const auto image_size = NT_HEADER( module_base )->OptionalHeader.SizeOfImage; auto vm_handler_tables = vm::locate::all_handler_tables( module_base ); auto vm_enters = vm::locate::all_vm_enters( module_base, vm_handler_tables ); - std::printf( "> number of vm enters = %d\n", vm_enters.size() ); - if ( std::find_if( vm_enters.begin() + 1, vm_enters.end(), - [ & ]( const std::pair< std::uint32_t, std::uint32_t > &vm_enter_data ) -> bool { - return vm_enter_data.second == vm_enters[ 0 ].second; - } ) != vm_enters.end() ) + std::vector< std::vector< vm::instrs::code_block_t > > virt_rtns; + for ( const auto &[ vm_enter_offset, encrypted_rva ] : vm_enters ) { - std::printf( "> optimizations can be done.\n" ); - std::getchar(); + std::printf( "> emulating vm enter at rva = 0x%x\n", vm_enter_offset ); + vm::ctx_t vm_ctx( module_base, image_base, image_size, vm_enter_offset ); + + if ( !vm_ctx.init() ) + { + std::printf( "[!] failed to init vmctx... this can be for many reasons..." + " try validating your vm entry rva... make sure the binary is unpacked and is" + "protected with VMProtect 2...\n" ); + return -1; + } + + vm::emu_t emu( &vm_ctx ); + + if ( !emu.init() ) + { + std::printf( "[!] failed to init emulator...\n" ); + return -1; + } + + std::vector< vm::instrs::code_block_t > code_blocks; + + if ( !emu.get_trace( code_blocks ) ) + { + std::printf( "[!] something failed during tracing, review the console for more information...\n" ); + continue; + } + + std::printf( "> number of blocks = %d\n", code_blocks.size() ); + virt_rtns.push_back( code_blocks ); } + std::printf( "> traced %d virtual routines...\n", virt_rtns.size() ); + std::printf( "> serializing results....\n" ); + + vmp2::v4::file_header file_header; + file_header.magic = VMP_MAGIC; + file_header.epoch_time = std::time( nullptr ); + file_header.version = vmp2::version_t::v4; + file_header.module_base = module_base; + file_header.image_base = image_base; + file_header.vm_entry_rva = 0ull; + file_header.module_offset = sizeof file_header; + file_header.module_size = image_size; + file_header.rtn_count = virt_rtns.size(); + file_header.rtn_offset = image_size + sizeof file_header; + + std::ofstream output( parser.get< std::string >( "out" ), std::ios::binary ); + output.write( reinterpret_cast< const char * >( &file_header ), sizeof file_header ); + output.write( reinterpret_cast< const char * >( module_base ), image_size ); + + for ( auto virt_rtn : virt_rtns ) + { + vmp2::v4::rtn_t rtn{ virt_rtn.size() }; + output.write( reinterpret_cast< const char * >( &rtn ), sizeof vmp2::v4::rtn_t::code_block_count ); + + for ( const auto &code_block : virt_rtn ) + { + const auto _code_block_size = sizeof vmp2::v4::code_block_t + ( code_block.jcc.block_addr.size() * 8 ) + + code_block.vinstrs.size() * sizeof vm::instrs::virt_instr_t; + + vmp2::v4::code_block_t *_code_block = + reinterpret_cast< vmp2::v4::code_block_t * >( malloc( _code_block_size ) ); + + // serialize block meta data... + _code_block->vip_begin = code_block.vip_begin; + _code_block->next_block_offset = _code_block_size; + _code_block->vinstr_count = code_block.vinstrs.size(); + _code_block->has_jcc = code_block.jcc.has_jcc; + _code_block->jcc_type = code_block.jcc.type; + _code_block->num_block_addrs = code_block.jcc.block_addr.size(); + + // serialize jcc branches... + for ( auto idx = 0u; idx < code_block.jcc.block_addr.size(); ++idx ) + _code_block->branch_addr[ idx ] = code_block.jcc.block_addr[ idx ]; + + auto block_vinstrs = reinterpret_cast< vm::instrs::virt_instr_t * >( + reinterpret_cast< std::uintptr_t >( _code_block ) + sizeof vmp2::v4::code_block_t + + ( code_block.jcc.block_addr.size() * 8 ) ); + + for ( auto idx = 0u; idx < code_block.vinstrs.size(); ++idx ) + block_vinstrs[ idx ] = code_block.vinstrs[ idx ]; + + output.write( reinterpret_cast< const char * >( _code_block ), _code_block_size ); + free( _code_block ); + } + } + output.close(); + } + else if ( parser.exists( "bin" ) && parser.exists( "locateconst" ) ) + { + const auto module_base = reinterpret_cast< std::uintptr_t >( + LoadLibraryExA( parser.get< std::string >( "bin" ).c_str(), NULL, DONT_RESOLVE_DLL_REFERENCES ) ); + + const auto const_val = std::strtoull( parser.get< std::string >( "locateconst" ).c_str(), nullptr, 16 ); + const auto image_base = umtils->image_base( parser.get< std::string >( "bin" ).c_str() ); + const auto image_size = NT_HEADER( module_base )->OptionalHeader.SizeOfImage; + + auto vm_handler_tables = vm::locate::all_handler_tables( module_base ); + auto vm_enters = vm::locate::all_vm_enters( module_base, vm_handler_tables ); + + std::printf( "> number of vm enters = %d\n", vm_enters.size() ); for ( const auto &[ vm_enter_offset, encrypted_rva ] : vm_enters ) { std::printf( "> emulating vm enter at rva = 0x%x\n", vm_enter_offset ); From 4229939de0251df3c0a48d0f3c8263bb02923420 Mon Sep 17 00:00:00 2001 From: _xeroxz Date: Sun, 15 Aug 2021 05:03:16 -0700 Subject: [PATCH 3/4] impl v4 file format... --- dependencies/vmprofiler | 2 +- src/main.cpp | 44 +++++++++++++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/dependencies/vmprofiler b/dependencies/vmprofiler index b336e01..be14462 160000 --- a/dependencies/vmprofiler +++ b/dependencies/vmprofiler @@ -1 +1 @@ -Subproject commit b336e0145d3655ebd7b43abd7338016f36640ab0 +Subproject commit be144629075d10eceebbea30361d1b6c96512c01 diff --git a/src/main.cpp b/src/main.cpp index e5de3c7..062b00d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -137,8 +137,8 @@ int __cdecl main( int argc, const char *argv[] ) std::ofstream output( parser.get< std::string >( "out" ), std::ios::binary ); output.write( reinterpret_cast< const char * >( &file_header ), sizeof file_header ); output.write( reinterpret_cast< const char * >( module_base ), image_size ); - output.write( reinterpret_cast< const char * >( &rtn ), sizeof vmp2::v4::rtn_t::code_block_count ); + std::vector< vmp2::v4::code_block_t * > vmp2_blocks; for ( const auto &code_block : code_blocks ) { const auto _code_block_size = sizeof vmp2::v4::code_block_t + ( code_block.jcc.block_addr.size() * 8 ) + @@ -166,9 +166,24 @@ int __cdecl main( int argc, const char *argv[] ) for ( auto idx = 0u; idx < code_block.vinstrs.size(); ++idx ) block_vinstrs[ idx ] = code_block.vinstrs[ idx ]; - output.write( reinterpret_cast< const char * >( _code_block ), _code_block_size ); - free( _code_block ); + vmp2_blocks.push_back( _code_block ); } + + std::size_t code_blocks_size = sizeof( vmp2::v4::rtn_t::size ) + sizeof( vmp2::v4::rtn_t::code_block_count ); + std::for_each( vmp2_blocks.begin(), vmp2_blocks.end(), [ & ]( vmp2::v4::code_block_t *vmp2_block ) -> void { + code_blocks_size += vmp2_block->next_block_offset; + } ); + + rtn.size = code_blocks_size; + rtn.code_block_count = vmp2_blocks.size(); + + output.write( reinterpret_cast< const char * >( &rtn ), + sizeof( vmp2::v4::rtn_t::size ) + sizeof( vmp2::v4::rtn_t::code_block_count ) ); + + std::for_each( vmp2_blocks.begin(), vmp2_blocks.end(), [ & ]( vmp2::v4::code_block_t *vmp2_block ) -> void { + output.write( reinterpret_cast< const char * >( vmp2_block ), vmp2_block->next_block_offset ); + free( vmp2_block ); + } ); output.close(); } else if ( parser.exists( "unpack" ) && parser.exists( "out" ) ) @@ -266,7 +281,7 @@ int __cdecl main( int argc, const char *argv[] ) for ( auto virt_rtn : virt_rtns ) { vmp2::v4::rtn_t rtn{ virt_rtn.size() }; - output.write( reinterpret_cast< const char * >( &rtn ), sizeof vmp2::v4::rtn_t::code_block_count ); + std::vector< vmp2::v4::code_block_t * > vmp2_blocks; for ( const auto &code_block : virt_rtn ) { @@ -295,9 +310,26 @@ int __cdecl main( int argc, const char *argv[] ) for ( auto idx = 0u; idx < code_block.vinstrs.size(); ++idx ) block_vinstrs[ idx ] = code_block.vinstrs[ idx ]; - output.write( reinterpret_cast< const char * >( _code_block ), _code_block_size ); - free( _code_block ); + vmp2_blocks.push_back( _code_block ); } + + std::size_t code_blocks_size = + sizeof( vmp2::v4::rtn_t::size ) + sizeof( vmp2::v4::rtn_t::code_block_count ); + + std::for_each( vmp2_blocks.begin(), vmp2_blocks.end(), [ & ]( vmp2::v4::code_block_t *vmp2_block ) -> void { + code_blocks_size += vmp2_block->next_block_offset; + } ); + + rtn.size = code_blocks_size; + rtn.code_block_count = vmp2_blocks.size(); + + output.write( reinterpret_cast< const char * >( &rtn ), + sizeof( vmp2::v4::rtn_t::size ) + sizeof( vmp2::v4::rtn_t::code_block_count ) ); + + std::for_each( vmp2_blocks.begin(), vmp2_blocks.end(), [ & ]( vmp2::v4::code_block_t *vmp2_block ) -> void { + output.write( reinterpret_cast< const char * >( vmp2_block ), vmp2_block->next_block_offset ); + free( vmp2_block ); + } ); } output.close(); } From 7f6e88a77f129fa00040a698556c499c0aeb0c7c Mon Sep 17 00:00:00 2001 From: _xeroxz Date: Sun, 15 Aug 2021 17:38:37 -0700 Subject: [PATCH 4/4] updated vmprofiler, forgot to set a field in vmp2 file format... fixed... --- dependencies/vmprofiler | 2 +- src/main.cpp | 29 ++++++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/dependencies/vmprofiler b/dependencies/vmprofiler index be14462..05c98b1 160000 --- a/dependencies/vmprofiler +++ b/dependencies/vmprofiler @@ -1 +1 @@ -Subproject commit be144629075d10eceebbea30361d1b6c96512c01 +Subproject commit 05c98b1ef57c3375ffd455221a3be1be6110d4eb diff --git a/src/main.cpp b/src/main.cpp index 062b00d..6e2bb8c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -133,7 +133,7 @@ int __cdecl main( int argc, const char *argv[] ) file_header.rtn_count = 1; file_header.rtn_offset = image_size + sizeof file_header; - vmp2::v4::rtn_t rtn{ code_blocks.size() }; + vmp2::v4::rtn_t rtn; std::ofstream output( parser.get< std::string >( "out" ), std::ios::binary ); output.write( reinterpret_cast< const char * >( &file_header ), sizeof file_header ); output.write( reinterpret_cast< const char * >( module_base ), image_size ); @@ -169,16 +169,20 @@ int __cdecl main( int argc, const char *argv[] ) vmp2_blocks.push_back( _code_block ); } - std::size_t code_blocks_size = sizeof( vmp2::v4::rtn_t::size ) + sizeof( vmp2::v4::rtn_t::code_block_count ); + std::size_t code_blocks_size = sizeof( vmp2::v4::rtn_t::size ) + sizeof( vmp2::v4::rtn_t::code_block_count ) + + sizeof( vmp2::v4::rtn_t::vm_enter_offset ); + std::for_each( vmp2_blocks.begin(), vmp2_blocks.end(), [ & ]( vmp2::v4::code_block_t *vmp2_block ) -> void { code_blocks_size += vmp2_block->next_block_offset; } ); rtn.size = code_blocks_size; rtn.code_block_count = vmp2_blocks.size(); + rtn.vm_enter_offset = vm_entry_rva; - output.write( reinterpret_cast< const char * >( &rtn ), - sizeof( vmp2::v4::rtn_t::size ) + sizeof( vmp2::v4::rtn_t::code_block_count ) ); + output.write( reinterpret_cast< const char * >( &rtn ), sizeof( vmp2::v4::rtn_t::size ) + + sizeof( vmp2::v4::rtn_t::code_block_count ) + + sizeof( vmp2::v4::rtn_t::vm_enter_offset ) ); std::for_each( vmp2_blocks.begin(), vmp2_blocks.end(), [ & ]( vmp2::v4::code_block_t *vmp2_block ) -> void { output.write( reinterpret_cast< const char * >( vmp2_block ), vmp2_block->next_block_offset ); @@ -225,7 +229,7 @@ int __cdecl main( int argc, const char *argv[] ) auto vm_handler_tables = vm::locate::all_handler_tables( module_base ); auto vm_enters = vm::locate::all_vm_enters( module_base, vm_handler_tables ); - std::vector< std::vector< vm::instrs::code_block_t > > virt_rtns; + std::vector< std::pair< std::uintptr_t, std::vector< vm::instrs::code_block_t > > > virt_rtns; for ( const auto &[ vm_enter_offset, encrypted_rva ] : vm_enters ) { std::printf( "> emulating vm enter at rva = 0x%x\n", vm_enter_offset ); @@ -256,7 +260,7 @@ int __cdecl main( int argc, const char *argv[] ) } std::printf( "> number of blocks = %d\n", code_blocks.size() ); - virt_rtns.push_back( code_blocks ); + virt_rtns.push_back( { vm_enter_offset, code_blocks } ); } std::printf( "> traced %d virtual routines...\n", virt_rtns.size() ); @@ -278,7 +282,7 @@ int __cdecl main( int argc, const char *argv[] ) output.write( reinterpret_cast< const char * >( &file_header ), sizeof file_header ); output.write( reinterpret_cast< const char * >( module_base ), image_size ); - for ( auto virt_rtn : virt_rtns ) + for ( auto [ vm_enter_offset, virt_rtn ] : virt_rtns ) { vmp2::v4::rtn_t rtn{ virt_rtn.size() }; std::vector< vmp2::v4::code_block_t * > vmp2_blocks; @@ -313,8 +317,9 @@ int __cdecl main( int argc, const char *argv[] ) vmp2_blocks.push_back( _code_block ); } - std::size_t code_blocks_size = - sizeof( vmp2::v4::rtn_t::size ) + sizeof( vmp2::v4::rtn_t::code_block_count ); + std::size_t code_blocks_size = sizeof( vmp2::v4::rtn_t::size ) + + sizeof( vmp2::v4::rtn_t::vm_enter_offset ) + + sizeof( vmp2::v4::rtn_t::code_block_count ); std::for_each( vmp2_blocks.begin(), vmp2_blocks.end(), [ & ]( vmp2::v4::code_block_t *vmp2_block ) -> void { code_blocks_size += vmp2_block->next_block_offset; @@ -322,9 +327,11 @@ int __cdecl main( int argc, const char *argv[] ) rtn.size = code_blocks_size; rtn.code_block_count = vmp2_blocks.size(); + rtn.vm_enter_offset = vm_enter_offset; - output.write( reinterpret_cast< const char * >( &rtn ), - sizeof( vmp2::v4::rtn_t::size ) + sizeof( vmp2::v4::rtn_t::code_block_count ) ); + output.write( reinterpret_cast< const char * >( &rtn ), sizeof( vmp2::v4::rtn_t::size ) + + sizeof( vmp2::v4::rtn_t::code_block_count ) + + sizeof( vmp2::v4::rtn_t::vm_enter_offset ) ); std::for_each( vmp2_blocks.begin(), vmp2_blocks.end(), [ & ]( vmp2::v4::code_block_t *vmp2_block ) -> void { output.write( reinterpret_cast< const char * >( vmp2_block ), vmp2_block->next_block_offset );