From 062fdd49bdd4c40a83b440c9aaca15c3c965ca8a Mon Sep 17 00:00:00 2001 From: _xeroxz Date: Sun, 27 Jun 2021 17:01:21 -0700 Subject: [PATCH] cleaned the code, designing swap chain a little more.... --- .gitmodules | 3 ++ dependencies/mbuild | 1 + examples/demo/demo.vcxproj | 1 + examples/demo/main.cpp | 18 +++++++- include/theo/engine.hpp | 54 ---------------------- include/theo/engine/engine.hpp | 22 +++++++++ include/theo/engine/iff.hpp | 33 +++++++++++++ include/theo/engine/swapchain.hpp | 35 ++++++++++++++ include/theo/obf_pass.hpp | 23 --------- include/theo/obf_pass/obf_pass.hpp | 35 ++++++++++++++ include/theo/obf_pass/obf_pass_wrapper.hpp | 46 ++++++++++++++++++ include/theo/symbol.hpp | 25 ++++++++-- include/theo/theo.hpp | 4 ++ src/main.cpp | 7 +-- src/theo/engine/engine.cpp | 33 +++++++++++++ src/theo/{engine.cpp => engine/iff.cpp} | 25 ++-------- src/theo/engine/swapchain.cpp | 35 ++++++++++++++ src/theo/obf_pass/obf_pass_wrapper.cpp | 0 src/theo/symbol.cpp | 0 theodosius.vcxproj | 22 ++++++--- theodosius.vcxproj.filters | 46 ++++++++++++++++-- 21 files changed, 346 insertions(+), 122 deletions(-) create mode 160000 dependencies/mbuild delete mode 100644 include/theo/engine.hpp create mode 100644 include/theo/engine/engine.hpp create mode 100644 include/theo/engine/iff.hpp create mode 100644 include/theo/engine/swapchain.hpp delete mode 100644 include/theo/obf_pass.hpp create mode 100644 include/theo/obf_pass/obf_pass.hpp create mode 100644 include/theo/obf_pass/obf_pass_wrapper.hpp create mode 100644 include/theo/theo.hpp create mode 100644 src/theo/engine/engine.cpp rename src/theo/{engine.cpp => engine/iff.cpp} (57%) create mode 100644 src/theo/engine/swapchain.cpp create mode 100644 src/theo/obf_pass/obf_pass_wrapper.cpp create mode 100644 src/theo/symbol.cpp diff --git a/.gitmodules b/.gitmodules index 54bcaee..6b996fa 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "dependencies/cli-parser"] path = dependencies/cli-parser url = https://githacks.org/_xeroxz/cli-parser.git +[submodule "dependencies/mbuild"] + path = dependencies/mbuild + url = https://github.com/intelxed/mbuild.git diff --git a/dependencies/mbuild b/dependencies/mbuild new file mode 160000 index 0000000..09b6654 --- /dev/null +++ b/dependencies/mbuild @@ -0,0 +1 @@ +Subproject commit 09b6654be0c52bf1df44e88c88b411a67b624cbd diff --git a/examples/demo/demo.vcxproj b/examples/demo/demo.vcxproj index f448972..996a550 100644 --- a/examples/demo/demo.vcxproj +++ b/examples/demo/demo.vcxproj @@ -43,6 +43,7 @@ true stdcpp17 Disabled + -Xclang -fno-jump-tables %(AdditionalOptions) Console diff --git a/examples/demo/main.cpp b/examples/demo/main.cpp index 070d9df..341f878 100644 --- a/examples/demo/main.cpp +++ b/examples/demo/main.cpp @@ -3,7 +3,21 @@ inline double g_version = 1.0; -int main() +int main( int argc, char **argv ) { - std::printf( "> g_version = %f, get_version = %f\n", g_version, get_version() ); + switch ( std::atoi( argv[ 1 ] ) ) + { + case 0: + return 10; + case 1: + return 12; + case 2: + return 342; + case 3: + return 43; + case 4: + return 342; + default: + std::printf( "> g_version = %f, get_version = %f\n", g_version, get_version() ); + } } \ No newline at end of file diff --git a/include/theo/engine.hpp b/include/theo/engine.hpp deleted file mode 100644 index 559564e..0000000 --- a/include/theo/engine.hpp +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include - -namespace theo -{ - class engine_t - { - class swapchain_t - { - std::vector< std::uint8_t > front, back; - std::vector< std::vector< std::uint8_t > > objs; - - public: - class iff_t - { - /// - /// swapchain_t is the only one who needs to call iff_t::flush... - /// so its a friend class... flush is also private... - /// - friend class swapchain_t; - - public: - struct section_t - { - coff::section_header_t header; - std::vector< std::pair< std::uint32_t, coff::symbol_t > > symbols; - }; - - explicit iff_t( coff::image_t *img ); - std::vector< section_t > sections; - - private: - /// - /// flush changes from "sections" back to img... - /// - void flush(); - coff::image_t *img; - }; - explicit swapchain_t( const std::vector< std::uint8_t > &img ); - std::shared_ptr< swapchain_t > make( const std::vector< std::uint8_t > &img ); - void swap( std::vector< iff_t > &iffs ); - }; - - public: - explicit engine_t( const std::vector< std::uint8_t > &lib_img ); - void add_pass( const obf_pass_t &pass ); - void run( std::vector< std::uint8_t > &result ); - - private: - swapchain_t swap; - std::vector< theo::obf_pass_t > passes; - }; -} // namespace theo \ No newline at end of file diff --git a/include/theo/engine/engine.hpp b/include/theo/engine/engine.hpp new file mode 100644 index 0000000..54f0feb --- /dev/null +++ b/include/theo/engine/engine.hpp @@ -0,0 +1,22 @@ +#pragma once +#include +#include +#include +#include +#include + +namespace theo +{ + class engine_t + { + public: + explicit engine_t( const std::vector< std::uint8_t > &lib_img ); + engine_t &add_pass( const obf_pass_t &pass ); + engine_t &add_passes( const std::vector< obf_pass_t > &passes ); + void run( std::vector< std::uint8_t > &result ); + + private: + std::shared_ptr< theo::swapchain_t > swapchain; + std::vector< theo::obf_pass_t > obf_passes; + }; +} // namespace theo \ No newline at end of file diff --git a/include/theo/engine/iff.hpp b/include/theo/engine/iff.hpp new file mode 100644 index 0000000..fb8aa85 --- /dev/null +++ b/include/theo/engine/iff.hpp @@ -0,0 +1,33 @@ +#pragma once +#include +#include +#include + +namespace theo +{ + class iff_t + { + /// + /// swapchain_t is the only one who needs to call iff_t::flush... + /// so its a friend class... flush is also private... + /// + friend class swapchain_t; + + public: + struct section_t + { + coff::section_header_t header; + std::vector< std::pair< std::uint32_t, coff::symbol_t > > symbols; + }; + + explicit iff_t( coff::image_t *img ); + std::vector< section_t > sections; + + private: + /// + /// flush changes from "sections" back to img... + /// + void flush(); + coff::image_t *img; + }; +} // namespace theo \ No newline at end of file diff --git a/include/theo/engine/swapchain.hpp b/include/theo/engine/swapchain.hpp new file mode 100644 index 0000000..2181af4 --- /dev/null +++ b/include/theo/engine/swapchain.hpp @@ -0,0 +1,35 @@ +#pragma once +#include +#include +#include +#include + +namespace theo +{ + class swapchain_t + { + /// + /// pair of front and back buffers... + /// changes are flushed to the back buffer, then the front + /// buffer gets overwritten by the back buffer when swapped... + /// + struct pair_t + { + std::vector< std::uint8_t > front, back; + }; + + std::vector< pair_t > objs; + std::vector< std::uint8_t > archive; + + public: + explicit swapchain_t( const std::vector< std::uint8_t > &img ); + static std::shared_ptr< swapchain_t > make( const std::vector< std::uint8_t > &img ); + + /// + /// if theo::swapchain::swap takes in an empty vector then it will simply fill it up + /// instead of flushing the iff data to the back buffer then swapping... + /// + /// + void swap( std::vector< iff_t > &iffs ); + }; +} // namespace theo \ No newline at end of file diff --git a/include/theo/obf_pass.hpp b/include/theo/obf_pass.hpp deleted file mode 100644 index 2a176c2..0000000 --- a/include/theo/obf_pass.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - -namespace theo -{ - class obf_pass_t - { - friend class engine_t; - - public: - enum class lvl_t - { - l_section, - l_function, - l_instr - }; - obf_pass_t( const lvl_t &pass_lvl ); - - private: - virtual void callback() = 0; - lvl_t lvl; - }; -} // namespace theo \ No newline at end of file diff --git a/include/theo/obf_pass/obf_pass.hpp b/include/theo/obf_pass/obf_pass.hpp new file mode 100644 index 0000000..1897922 --- /dev/null +++ b/include/theo/obf_pass/obf_pass.hpp @@ -0,0 +1,35 @@ +#pragma once +#include +#include + +namespace theo +{ + enum class lvl_t + { + /// + /// callback gets passed entire IFF structures... + /// + l_iff, + + /// + /// callback gets passed entire IFF section structures... + /// + l_section, + + /// + /// callback gets passed entire IFF symbols... + /// + l_symbol + }; + + class obf_pass_t + { + friend class engine_t; + virtual void obfuscate( const theo::iff_t & ) = 0; + lvl_t lvl; + + public: + explicit obf_pass_t( const lvl_t &pass_lvl ); + lvl_t get_lvl() const; + }; +} // namespace theo \ No newline at end of file diff --git a/include/theo/obf_pass/obf_pass_wrapper.hpp b/include/theo/obf_pass/obf_pass_wrapper.hpp new file mode 100644 index 0000000..d8ec054 --- /dev/null +++ b/include/theo/obf_pass/obf_pass_wrapper.hpp @@ -0,0 +1,46 @@ +#pragma once +#include +#include +#include + +namespace theo +{ + /// + /// obfuscation pass at the IFF level... + /// + class obf_pass_iff_t : public obf_pass_t + { + void obfuscate( const theo::iff_t & ) override; + + public: + obf_pass_iff_t() : obf_pass_t( lvl_t::l_iff ) + { + } + }; + + /// + /// obfuscation pass at the IFF section level... + /// + class obf_pass_section_t : public obf_pass_t + { + void obfuscate( const theo::iff_t & ) override; + + public: + obf_pass_section_t() : obf_pass_t( lvl_t::l_section ) + { + } + }; + + /// + /// obfuscation pass at the IFF symbol level.. + /// + class obf_pass_symbol_t : public obf_pass_t + { + void obfuscate( const theo::iff_t & ) override; + + public: + obf_pass_symbol_t() : obf_pass_t( lvl_t::l_symbol ) + { + } + }; +} // namespace theo \ No newline at end of file diff --git a/include/theo/symbol.hpp b/include/theo/symbol.hpp index bec3c2f..15a9de1 100644 --- a/include/theo/symbol.hpp +++ b/include/theo/symbol.hpp @@ -1,10 +1,27 @@ +#pragma once +#include #include -namespace llo +#define XED_DECODER +extern "C" { - class symbol_t +#include +} + +namespace theo +{ + class symbol_t { - public: + std::uint32_t symbol_table_idx; + coff::symbol_t coff_symbol; + std::vector< symbol_t * > deps; + std::vector< xed_decoded_inst_t > instrs; + + void on_update(); + + public: + symbol_t(); + void add_dep( const symbol_t &dep ); }; -} \ No newline at end of file +} // namespace theo \ No newline at end of file diff --git a/include/theo/theo.hpp b/include/theo/theo.hpp new file mode 100644 index 0000000..41ecceb --- /dev/null +++ b/include/theo/theo.hpp @@ -0,0 +1,4 @@ +#pragma once +#include +#include +#include \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 757d7e6..4baed47 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,5 @@ -#define _CRT_SECURE_NO_WARNINGS #include -#include -#include -#include +#include #include int __cdecl main( int argc, const char *argv[] ) @@ -28,7 +25,5 @@ int __cdecl main( int argc, const char *argv[] ) std::vector< std::uint8_t > lib; umtils->open_binary_file( cli_parser.get< std::string >( "i" ), lib ); - theo::engine_t theo( lib ); - //theo.add_pass( { theo::obf_pass_t::lvl_t::l_function } ); } \ No newline at end of file diff --git a/src/theo/engine/engine.cpp b/src/theo/engine/engine.cpp new file mode 100644 index 0000000..1781718 --- /dev/null +++ b/src/theo/engine/engine.cpp @@ -0,0 +1,33 @@ +#include + +namespace theo +{ + engine_t::engine_t( const std::vector< std::uint8_t > &lib_img ) : swapchain( theo::swapchain_t::make( lib_img ) ) + { + } + + engine_t &theo::engine_t::add_pass( const obf_pass_t &pass ) + { + obf_passes.push_back( pass ); + return *this; + } + + engine_t &theo::engine_t::add_passes( const std::vector< obf_pass_t > &passes ) + { + obf_passes.insert( obf_passes.end(), passes.begin(), passes.end() ); + return *this; + } + + void theo::engine_t::run( std::vector< std::uint8_t > &result ) + { + std::for_each( obf_passes.begin(), obf_passes.end(), [ & ]( theo::obf_pass_t &obf_pass ) { + std::vector< theo::iff_t > iffs; + swapchain->swap( iffs ); + + std::for_each( iffs.begin(), iffs.end(), [ & ]( const theo::iff_t &iff ) { + obf_pass.obfuscate( iff ); + swapchain->swap( iffs ); + } ); + } ); + } +} // namespace theo \ No newline at end of file diff --git a/src/theo/engine.cpp b/src/theo/engine/iff.cpp similarity index 57% rename from src/theo/engine.cpp rename to src/theo/engine/iff.cpp index eb1a8b2..6094ecd 100644 --- a/src/theo/engine.cpp +++ b/src/theo/engine/iff.cpp @@ -1,8 +1,8 @@ -#include +#include namespace theo { - engine_t::swapchain_t::iff_t::iff_t( coff::image_t *img ) + iff_t::iff_t( coff::image_t *img ) { // add sections to iff... std::for_each( img->get_sections(), img->get_sections() + img->file_header.num_sections, @@ -17,7 +17,7 @@ namespace theo } ); } - void engine_t::swapchain_t::iff_t::flush() + void iff_t::flush() { // for each section, loop over symbols to see if they have changed... std::for_each( sections.begin(), sections.end(), [ & ]( const iff_t::section_t &iff_section ) { @@ -27,23 +27,4 @@ namespace theo } ); } ); } - - engine_t::swapchain_t::swapchain_t( const std::vector< std::uint8_t > &img ) : front( img ), back( img ) - { - ar::view lib( front.data(), front.size() ); - // extract obj files from lib archive... - std::for_each( lib.begin(), lib.end(), [ & ]( const auto &coff_data ) { - const auto &[ coff_name, coff_img ] = coff_data; - objs.push_back( { coff_img.begin(), coff_img.end() } ); - } ); - } - - std::shared_ptr< engine_t::swapchain_t > engine_t::swapchain_t::make( const std::vector< std::uint8_t > &img ) - { - return std::make_shared< engine_t::swapchain_t >( img ); - } - - engine_t::engine_t( const std::vector< std::uint8_t > &lib_img ) : swap{ lib_img } - { - } } // namespace theo \ No newline at end of file diff --git a/src/theo/engine/swapchain.cpp b/src/theo/engine/swapchain.cpp new file mode 100644 index 0000000..db30637 --- /dev/null +++ b/src/theo/engine/swapchain.cpp @@ -0,0 +1,35 @@ +#include + +namespace theo +{ + swapchain_t::swapchain_t( const std::vector< std::uint8_t > &img ) : archive( img ) + { + ar::view lib( archive.data(), archive.size() ); + // extract obj files from lib archive... + std::for_each( lib.begin(), lib.end(), [ & ]( const auto &coff_data ) { + const auto &[ coff_name, coff_img ] = coff_data; + objs.push_back( { { coff_img.begin(), coff_img.end() }, { coff_img.begin(), coff_img.end() } } ); + } ); + } + + std::shared_ptr< theo::swapchain_t > swapchain_t::make( const std::vector< std::uint8_t > &img ) + { + return std::make_shared< theo::swapchain_t >( img ); + } + + void theo::swapchain_t::swap( std::vector< iff_t > &iffs ) + { + if ( iffs.empty() ) + { + for ( auto &[ front, back ] : objs ) + { + theo::iff_t iff( reinterpret_cast< coff::image_t * >( front.data() ) ); + iffs.push_back( iff ); + } + } + else + { + // TODO flush results to the back buffer and swap front and back... + } + } +} // namespace theo \ No newline at end of file diff --git a/src/theo/obf_pass/obf_pass_wrapper.cpp b/src/theo/obf_pass/obf_pass_wrapper.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/theo/symbol.cpp b/src/theo/symbol.cpp new file mode 100644 index 0000000..e69de29 diff --git a/theodosius.vcxproj b/theodosius.vcxproj index 009a93f..8776af0 100644 --- a/theodosius.vcxproj +++ b/theodosius.vcxproj @@ -12,7 +12,11 @@ - + + + + + @@ -87,9 +91,13 @@ - - + + + + + + 16.0 @@ -100,7 +108,7 @@ - StaticLibrary + Application true v142 Unicode @@ -126,11 +134,11 @@ true - $(ProjectDir)dependencies\xtils;$(ProjectDir)dependencies\linux-pe\includes;$(ProjectDir)dependencies\xed\include\public\xed;$(ProjectDir)dependencies\cli-parser\;$(ProjectDir)include\;$(IncludePath);$(ProjectDir)include\ + $(ProjectDir)dependencies\xtils;$(ProjectDir)dependencies\linux-pe\includes;$(ProjectDir)dependencies\xed\obj\wkit\include\xed\;$(ProjectDir)dependencies\cli-parser\;$(ProjectDir)include\;$(IncludePath);$(ProjectDir)include\ false - $(ProjectDir)dependencies\xtils;$(ProjectDir)dependencies\linux-pe\includes;$(ProjectDir)dependencies\xed\include\public\xed;$(ProjectDir)dependencies\cli-parser\;$(IncludePath);$(ProjectDir)include\ + $(ProjectDir)dependencies\xtils;$(ProjectDir)dependencies\linux-pe\includes;$(ProjectDir)dependencies\xed\obj\wkit\include\xed\;$(ProjectDir)dependencies\cli-parser\;$(IncludePath);$(ProjectDir)include\ @@ -143,6 +151,7 @@ Console true + $(ProjectDir)dependencies\xed\obj\wkit\lib\xed.lib;%(AdditionalDependencies) true @@ -167,6 +176,7 @@ true true true + $(ProjectDir)dependencies\xed\obj\wkit\lib\xed.lib;%(AdditionalDependencies) true diff --git a/theodosius.vcxproj.filters b/theodosius.vcxproj.filters index 846fbd5..717bcd0 100644 --- a/theodosius.vcxproj.filters +++ b/theodosius.vcxproj.filters @@ -37,12 +37,36 @@ {348d8bd4-31b6-445d-a487-bc39267daf6b} + + {95451c54-49b0-4f77-ab9d-114bc75a44f3} + + + {56c94e09-c085-4b88-b732-cdf5f0663af1} + + + {582e72f3-82c8-412f-b686-8e767eb5c229} + + + {e42f4fe1-e21d-4e37-9b35-320770c78e5f} + Source Files - + + Source Files\theo\engine + + + Source Files\theo\engine + + + Source Files\theo\engine + + + Source Files\theo\obf_pass + + Source Files\theo @@ -259,13 +283,25 @@ Header Files\xed - + Header Files\theo - - Header Files\theo + + Header Files\theo\engine - + + Header Files\theo\engine + + + Header Files\theo\engine + + + Header Files\theo\obf_pass + + + Header Files\theo\obf_pass + + Header Files\theo