diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..8063c5e --- /dev/null +++ b/.clang-format @@ -0,0 +1,18 @@ +--- +BasedOnStyle: Microsoft +AlignAfterOpenBracket: Align +AllowAllArgumentsOnNextLine: 'true' +AllowAllParametersOfDeclarationOnNextLine: 'true' +AllowShortIfStatementsOnASingleLine: Never +BreakBeforeBraces: Allman +IndentWidth: '4' +Language: Cpp +NamespaceIndentation: All +SpacesInAngles: 'true' +SpacesInCStyleCastParentheses: 'true' +SpacesInContainerLiterals: 'true' +SpacesInParentheses: 'true' +SpacesInSquareBrackets: 'true' +UseTab: Never + +... diff --git a/.gitmodules b/.gitmodules index 5947c6a..0d92ff0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "dependencies/cli-parser"] path = dependencies/cli-parser url = https://githacks.org/_xeroxz/cli-parser.git +[submodule "dependencies/xtils"] + path = dependencies/xtils + url = https://githacks.org/_xeroxz/xtils.git diff --git a/dependencies/vmprofiler b/dependencies/vmprofiler index 05ba2cc..4832530 160000 --- a/dependencies/vmprofiler +++ b/dependencies/vmprofiler @@ -1 +1 @@ -Subproject commit 05ba2cc84ba94e1d526168ad686fc2e167ad2eab +Subproject commit 4832530dfb2ef7522ed960d3393d44906c42eb74 diff --git a/dependencies/xtils b/dependencies/xtils new file mode 160000 index 0000000..7c32517 --- /dev/null +++ b/dependencies/xtils @@ -0,0 +1 @@ +Subproject commit 7c32517322c29a866cbb1e67fb9051efa2e05553 diff --git a/src/main.cpp b/src/main.cpp index 8b5ff2e..f4d740b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,150 +1,136 @@ -#include #include -#include #include -#include -#include -#include +#include +#include + #include +#include +#include +#include +#include -int __cdecl main(int argc, const char* argv[]) +int __cdecl main( int argc, const char *argv[] ) { - argparse::argument_parser_t parser( - "vmprofiler-cli", "virtual instruction pseudo code generator"); - - parser.add_argument() - .names({ "--bin", "--vmpbin" }) - .description("unpacked binary protected with VMProtect 2") - .required(true); - - parser.add_argument() - .names({ "--vmentry", "--entry" }) - .description("rva to push prior to a vm_entry") - .required(true); - - parser.add_argument() - .names({ "--imagebase", "--base" }) - .description("image base from OptionalHeader::ImageBase") - .required(true); - - parser.add_argument() - .names({ "--vmtrace", "--trace" }) - .description("a vmp2 file generated by a vmtracer"); - - parser.enable_help(); - auto err = parser.parse(argc, argv); - - if (err) - { - std::cout << err << std::endl; - return -1; - } - - if (parser.exists("help")) - { - parser.print_help(); - return 0; - } - - const auto module_base = - reinterpret_cast( - LoadLibraryExA(parser.get("bin").c_str(), - NULL, DONT_RESOLVE_DLL_REFERENCES)); - - const auto vm_entry_ptr = - module_base + std::strtoull( - parser.get("vmentry").c_str(), nullptr, 16); - - const auto image_base = std::strtoull( - parser.get("imagebase").c_str(), nullptr, 16); - - zydis_routine_t vm_entry; - std::printf("> vm entry start = 0x%p\n", vm_entry_ptr); - - if (!vm::util::flatten(vm_entry, vm_entry_ptr)) - { - std::printf("> failed to flatten vm entry...\n"); - return -1; - } - - vm::util::deobfuscate(vm_entry); - std::printf("> flattened vm entry...\n"); - std::printf("> deobfuscated vm entry...\n"); - vm::util::print(vm_entry); - - const auto vm_handler_table = vm::handler::table::get(vm_entry); - - if (!vm_handler_table) - { - std::printf("> failed to locate vm handler table...\n"); - return -1; - } - - std::printf("> located vm handler table... at = 0x%p, rva = 0x%p\n", vm_handler_table, - (reinterpret_cast(vm_handler_table) - module_base) + image_base); - - std::vector vm_handlers; - if (!vm::handler::get_all(module_base, image_base, vm_entry, vm_handler_table, vm_handlers)) - { - std::printf("> failed to get all vm handler meta data...\n"); - return -1; - } - - for (auto idx = 0u; idx < vm_handlers.size(); ++idx) - { - auto vm_handler = vm_handlers[idx]; - std::printf("==========[vm handler %s, idx = %d, imm size = %d]========\n", - vm_handler.profile ? vm_handler.profile->name : "UNKNOWN", idx, - vm_handler.imm_size); - - std::printf("================[vm handler instructions]==============\n"); - vm::util::print(vm_handler.instrs); - - if (vm_handler.imm_size) - { - std::printf("=================[vm handler transforms]===============\n"); - for (const auto& [transform_type, transform] : vm_handler.transforms) - vm::util::print(transform); - } - - std::printf("=======================================================\n\n"); - } - - if (parser.exists("vmtrace")) - { - std::ifstream vmp2_file( - parser.get("vmtrace").c_str(), std::ios::binary); - - const auto file_size = - std::filesystem::file_size( - parser.get("vmtrace").c_str()); - - const auto file_header = - reinterpret_cast(malloc(file_size)); - - vmp2_file.read((char*)file_header, file_size); - - const auto entry_list = - reinterpret_cast( - reinterpret_cast( - file_header) + file_header->entry_offset); - - std::printf("> module base = 0x%p\n", file_header->module_base); - std::getchar(); - - vm::vmctx_t vmctx( - file_header, - entry_list, - vm_handlers, - module_base, - image_base - ); - - for (auto [virt_instr, log_entry] = vmctx.step(); log_entry && !virt_instr.empty(); - std::tie(virt_instr, log_entry) = vmctx.step()) - std::printf("> %s\n", virt_instr.c_str()); - } - - std::printf("> finished...\n"); - std::getchar(); + argparse::argument_parser_t parser( "vmprofiler-cli", "virtual instruction pseudo code generator" ); + + parser.add_argument() + .names( { "--bin", "--vmpbin" } ) + .description( "unpacked binary protected with VMProtect 2" ) + .required( true ); + + parser.add_argument() + .names( { "--vmentry", "--entry" } ) + .description( "rva to push prior to a vm_entry" ) + .required( true ); + + parser.add_argument().name( "--showhandlers" ).description( "show all vm handlers..." ); + + parser.enable_help(); + auto err = parser.parse( argc, argv ); + + if ( err ) + { + std::cout << err << std::endl; + return -1; + } + + if ( parser.exists( "help" ) ) + { + parser.print_help(); + return 0; + } + + const auto module_base = reinterpret_cast< std::uintptr_t >( + LoadLibraryExA( parser.get< std::string >( "bin" ).c_str(), NULL, DONT_RESOLVE_DLL_REFERENCES ) ); + + const auto vm_entry_ptr = + module_base + std::strtoull( parser.get< std::string >( "vmentry" ).c_str(), nullptr, 16 ); + + const auto image_base = xtils::um_t::get_instance()->image_base( parser.get< std::string >( "bin" ).c_str() ); + + zydis_routine_t vm_entry; + std::printf( "> vm entry start = 0x%p\n", vm_entry_ptr ); + + if ( !vm::util::flatten( vm_entry, vm_entry_ptr ) ) + { + std::printf( "> failed to flatten vm entry...\n" ); + return -1; + } + + vm::util::deobfuscate( vm_entry ); + std::printf( "> flattened vm entry...\n" ); + std::printf( "> deobfuscated vm entry...\n" ); + std::printf( "==================================================================================\n" ); + vm::util::print( vm_entry ); + + const auto vm_handler_table = vm::handler::table::get( vm_entry ); + + if ( !vm_handler_table ) + { + std::printf( "> failed to locate vm handler table...\n" ); + return -1; + } + + std::printf( "==================================================================================\n" ); + std::printf( "> located vm handler table... at = 0x%p, rva = 0x%p\n", vm_handler_table, + ( reinterpret_cast< std::uintptr_t >( vm_handler_table ) - module_base ) + image_base ); + + zydis_decoded_instr_t vm_handler_transform; + if ( !vm::handler::table::get_transform( vm_entry, &vm_handler_transform ) ) + { + std::printf( "[!] failed to locate vm handler table entry transformation...\n" ); + return -1; + } + + std::printf( "> vm handler table entries decrypted with = " ); + vm::util::print( vm_handler_transform ); + + vm_handler_transform.mnemonic = vm::transform::inverse[ vm_handler_transform.mnemonic ]; + std::printf( "> vm handler table entries encrypted with = " ); + vm::util::print( vm_handler_transform ); + + std::printf( "==================================================================================\n" ); + std::vector< zydis_decoded_instr_t > vinstr_rva_decrypt_instrs; + if ( !vm::instrs::get_rva_decrypt( vm_entry, vinstr_rva_decrypt_instrs ) ) + { + std::printf( "[!] failed to get virtual instruction rva decrypt transformations...\n" ); + return -1; + } + + std::printf( "> virtual instruction rva decryption instructions:\n" ); + for ( auto &transform : vinstr_rva_decrypt_instrs ) + { + std::printf( "\t" ); + vm::util::print( transform ); + } + + std::vector< vm::handler::handler_t > vm_handlers; + if ( !vm::handler::get_all( module_base, image_base, vm_entry, vm_handler_table, vm_handlers ) ) + { + std::printf( "> failed to get all vm handler meta data...\n" ); + return -1; + } + + if ( parser.exists( "showhandlers" ) ) + { + for ( auto idx = 0u; idx < vm_handlers.size(); ++idx ) + { + auto vm_handler = vm_handlers[ idx ]; + std::printf( "==========[vm handler %s, idx = %d, imm size = %d]========\n", + vm_handler.profile ? vm_handler.profile->name : "UNKNOWN", idx, vm_handler.imm_size ); + + std::printf( "================[vm handler instructions]==============\n" ); + vm::util::print( vm_handler.instrs ); + + if ( vm_handler.imm_size ) + { + std::printf( "=================[vm handler transforms]===============\n" ); + for ( const auto &[ transform_type, transform ] : vm_handler.transforms ) + vm::util::print( transform ); + } + + std::printf( "=======================================================\n\n" ); + } + } + std::printf( "> finished...\n" ); } \ No newline at end of file diff --git a/src/vmprofiler-cli.vcxproj b/src/vmprofiler-cli.vcxproj index 3dbff7b..7a9e323 100644 --- a/src/vmprofiler-cli.vcxproj +++ b/src/vmprofiler-cli.vcxproj @@ -33,7 +33,7 @@ false - $(ProjectDir)..\dependencies\vmprofiler\include\;$(ProjectDir)..\dependencies\vmprofiler\dependencies\zydis\include;$(ProjectDir)..\dependencies\vmprofiler\dependencies\zydis\msvc;$(ProjectDir)..\dependencies\vmprofiler\dependencies\zydis\dependencies\zycore\include;$(ProjectDir)..\dependencies\cli-parser\;$(IncludePath) + $(ProjectDir)..\dependencies\vmprofiler\include\;$(ProjectDir)..\dependencies\vmprofiler\dependencies\zydis\include;$(ProjectDir)..\dependencies\vmprofiler\dependencies\zydis\msvc;$(ProjectDir)..\dependencies\vmprofiler\dependencies\zydis\dependencies\zycore\include;$(ProjectDir)..\dependencies\cli-parser\;$(ProjectDir)..\dependencies\xtils\;$(IncludePath) @@ -41,7 +41,7 @@ true true true - NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;ZYDIS_STATIC_DEFINE;%(PreprocessorDefinitions) true stdcpplatest Disabled @@ -64,56 +64,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -123,6 +121,9 @@ {d0b6092a-9944-4f24-9486-4b7dae372619} + + + diff --git a/src/vmprofiler-cli.vcxproj.filters b/src/vmprofiler-cli.vcxproj.filters index 4e22b05..a866b8a 100644 --- a/src/vmprofiler-cli.vcxproj.filters +++ b/src/vmprofiler-cli.vcxproj.filters @@ -13,19 +13,19 @@ {faf4cf50-0bc3-4674-80ff-fec015d82127} - {4e807ab2-7dfc-48a3-a092-039f09dd57f4} - - - {a10f8ff1-7349-4a48-9061-facf5a836ee4} + {ab5cf1fc-f056-4d4c-b00f-d111109aa610} - {82afe7a2-4ff1-4e12-806d-297e714bad9c} + {9bd82e24-4e3f-4444-b5df-98d02537beea} + + + {a9886b41-1872-4765-ad66-e61c999d61fc} - {a8d11bd2-d945-406f-aeb4-c28feb79a31d} + {ef756627-8757-4365-9367-e9f880b6af45} - {94c3a376-8f65-450f-9b11-740f95bef4d6} + {b21444ff-896e-4fad-931e-306b139b5b47} @@ -44,155 +44,154 @@ - - Header Files\Zycore - - - Header Files\Zycore - - - Header Files\Zycore - - - Header Files\Zycore + + Header Files - - Header Files\Zycore + + Header Files - - Header Files\Zycore + + Header Files - - Header Files\Zycore + + Header Files - - Header Files\Zycore + + Header Files - - Header Files\Zycore + + Header Files\Zydis\Internal - - Header Files\Zycore + + Header Files\Zydis\Internal - - Header Files\Zycore + + Header Files\Zydis\Internal - - Header Files\Zycore + + Header Files\Zydis\Internal - - Header Files\Zycore + + Header Files\Zydis\Internal - - Header Files\Zycore + + Header Files\Zydis\Internal - - Header Files\Zycore\API + + Header Files\Zydis\Generated - - Header Files\Zycore\API + + Header Files\Zydis\Generated - - Header Files\Zycore\API + + Header Files\Zydis\Generated - - Header Files\Zycore\API + + Header Files\Zydis\Generated - - Header Files\Zycore\API + + Header Files\Zydis\Generated - + Header Files\Zydis - + Header Files\Zydis - + Header Files\Zydis - + Header Files\Zydis - + Header Files\Zydis - + Header Files\Zydis - + Header Files\Zydis - + Header Files\Zydis - + Header Files\Zydis - + Header Files\Zydis - + Header Files\Zydis - + Header Files\Zydis - - Header Files\Zydis\Generated + + Header Files - - Header Files\Zydis\Generated + + Header Files - - Header Files\Zydis\Generated + + Header Files\Zycore - - Header Files\Zydis\Generated + + Header Files\Zycore - - Header Files\Zydis\Generated + + Header Files\Zycore - - Header Files\Zydis\Internal + + Header Files\Zycore - - Header Files\Zydis\Internal + + Header Files\Zycore - - Header Files\Zydis\Internal + + Header Files\Zycore - - Header Files\Zydis\Internal + + Header Files\Zycore - - Header Files\Zydis\Internal + + Header Files\Zycore - - Header Files\Zydis\Internal + + Header Files\Zycore - - Header Files + + Header Files\Zycore - - Header Files + + Header Files\Zycore - - Header Files + + Header Files\Zycore - - Header Files + + Header Files\Zycore - - Header Files + + Header Files\Zycore - - Header Files + + Header Files\Zycore\API - - Header Files + + Header Files\Zycore\API - - Header Files + + Header Files\Zycore\API + + Header Files\Zycore\API + + + + + Resource Files + \ No newline at end of file