cleaned the code, designing swap chain a little more....

2.0
_xeroxz 3 years ago
parent 0e8d76b564
commit 062fdd49bd

3
.gitmodules vendored

@ -10,3 +10,6 @@
[submodule "dependencies/cli-parser"] [submodule "dependencies/cli-parser"]
path = dependencies/cli-parser path = dependencies/cli-parser
url = https://githacks.org/_xeroxz/cli-parser.git url = https://githacks.org/_xeroxz/cli-parser.git
[submodule "dependencies/mbuild"]
path = dependencies/mbuild
url = https://github.com/intelxed/mbuild.git

@ -0,0 +1 @@
Subproject commit 09b6654be0c52bf1df44e88c88b411a67b624cbd

@ -43,6 +43,7 @@
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<AdditionalOptions>-Xclang -fno-jump-tables %(AdditionalOptions)</AdditionalOptions>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>

@ -3,7 +3,21 @@
inline double g_version = 1.0; 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() );
}
} }

@ -1,54 +0,0 @@
#include <coff/archive.hpp>
#include <coff/image.hpp>
#include <theo/obf_pass.hpp>
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
{
/// <summary>
/// swapchain_t is the only one who needs to call iff_t::flush...
/// so its a friend class... flush is also private...
/// </summary>
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:
/// <summary>
/// flush changes from "sections" back to img...
/// </summary>
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

@ -0,0 +1,22 @@
#pragma once
#include <coff/archive.hpp>
#include <coff/image.hpp>
#include <theo/engine/iff.hpp>
#include <theo/engine/swapchain.hpp>
#include <theo/obf_pass/obf_pass.hpp>
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

@ -0,0 +1,33 @@
#pragma once
#include <coff/image.hpp>
#include <vector>
#include <xtils.hpp>
namespace theo
{
class iff_t
{
/// <summary>
/// swapchain_t is the only one who needs to call iff_t::flush...
/// so its a friend class... flush is also private...
/// </summary>
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:
/// <summary>
/// flush changes from "sections" back to img...
/// </summary>
void flush();
coff::image_t *img;
};
} // namespace theo

@ -0,0 +1,35 @@
#pragma once
#include <coff/archive.hpp>
#include <coff/image.hpp>
#include <theo/engine/iff.hpp>
#include <xtils.hpp>
namespace theo
{
class swapchain_t
{
/// <summary>
/// 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...
/// </summary>
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 );
/// <summary>
/// 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...
/// </summary>
/// <param name="iffs"></param>
void swap( std::vector< iff_t > &iffs );
};
} // namespace theo

@ -1,23 +0,0 @@
#include <xtils.hpp>
#include <theo/symbol.hpp>
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

@ -0,0 +1,35 @@
#pragma once
#include <theo/symbol.hpp>
#include <xtils.hpp>
namespace theo
{
enum class lvl_t
{
/// <summary>
/// callback gets passed entire IFF structures...
/// </summary>
l_iff,
/// <summary>
/// callback gets passed entire IFF section structures...
/// </summary>
l_section,
/// <summary>
/// callback gets passed entire IFF symbols...
/// </summary>
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

@ -0,0 +1,46 @@
#pragma once
#include <theo/engine/engine.hpp>
#include <theo/obf_pass/obf_pass.hpp>
#include <theo/symbol.hpp>
namespace theo
{
/// <summary>
/// obfuscation pass at the IFF level...
/// </summary>
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 )
{
}
};
/// <summary>
/// obfuscation pass at the IFF section level...
/// </summary>
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 )
{
}
};
/// <summary>
/// obfuscation pass at the IFF symbol level..
/// </summary>
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

@ -1,10 +1,27 @@
#pragma once
#include <coff/image.hpp>
#include <xtils.hpp> #include <xtils.hpp>
namespace llo #define XED_DECODER
extern "C"
{ {
class symbol_t #include <xed-decode.h>
}
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 );
}; };
} } // namespace theo

@ -0,0 +1,4 @@
#pragma once
#include <theo/engine/engine.hpp>
#include <theo/obf_pass/obf_pass_wrapper.hpp>
#include <theo/symbol.hpp>

@ -1,8 +1,5 @@
#define _CRT_SECURE_NO_WARNINGS
#include <cli-parser.hpp> #include <cli-parser.hpp>
#include <coff/archive.hpp> #include <theo/theo.hpp>
#include <coff/image.hpp>
#include <theo/engine.hpp>
#include <xtils.hpp> #include <xtils.hpp>
int __cdecl main( int argc, const char *argv[] ) 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; std::vector< std::uint8_t > lib;
umtils->open_binary_file( cli_parser.get< std::string >( "i" ), lib ); umtils->open_binary_file( cli_parser.get< std::string >( "i" ), lib );
theo::engine_t theo( lib ); theo::engine_t theo( lib );
//theo.add_pass( { theo::obf_pass_t::lvl_t::l_function } );
} }

@ -0,0 +1,33 @@
#include <theo/engine/engine.hpp>
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

@ -1,8 +1,8 @@
#include <theo/engine.hpp> #include <theo/engine/iff.hpp>
namespace theo 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... // add sections to iff...
std::for_each( img->get_sections(), img->get_sections() + img->file_header.num_sections, 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... // 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 ) { 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 } // namespace theo

@ -0,0 +1,35 @@
#include <theo/engine/swapchain.hpp>
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

@ -12,7 +12,11 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="src\main.cpp" /> <ClCompile Include="src\main.cpp" />
<ClCompile Include="src\theo\engine.cpp" /> <ClCompile Include="src\theo\engine\engine.cpp" />
<ClCompile Include="src\theo\engine\iff.cpp" />
<ClCompile Include="src\theo\engine\swapchain.cpp" />
<ClCompile Include="src\theo\obf_pass\obf_pass_wrapper.cpp" />
<ClCompile Include="src\theo\symbol.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include=".clang-format" /> <None Include=".clang-format" />
@ -87,9 +91,13 @@
<ClInclude Include="dependencies\xed\include\public\xed\xed-util.h" /> <ClInclude Include="dependencies\xed\include\public\xed\xed-util.h" />
<ClInclude Include="dependencies\xed\include\public\xed\xed-version.h" /> <ClInclude Include="dependencies\xed\include\public\xed\xed-version.h" />
<ClInclude Include="dependencies\xtils\xtils.hpp" /> <ClInclude Include="dependencies\xtils\xtils.hpp" />
<ClInclude Include="include\theo\engine.hpp" /> <ClInclude Include="include\theo\engine\engine.hpp" />
<ClInclude Include="include\theo\obf_pass.hpp" /> <ClInclude Include="include\theo\engine\iff.hpp" />
<ClInclude Include="include\theo\engine\swapchain.hpp" />
<ClInclude Include="include\theo\obf_pass\obf_pass.hpp" />
<ClInclude Include="include\theo\obf_pass\obf_pass_wrapper.hpp" />
<ClInclude Include="include\theo\symbol.hpp" /> <ClInclude Include="include\theo\symbol.hpp" />
<ClInclude Include="include\theo\theo.hpp" />
</ItemGroup> </ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion> <VCProjectVersion>16.0</VCProjectVersion>
@ -100,7 +108,7 @@
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset> <PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
@ -126,11 +134,11 @@
<PropertyGroup Label="UserMacros" /> <PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>
<IncludePath>$(ProjectDir)dependencies\xtils;$(ProjectDir)dependencies\linux-pe\includes;$(ProjectDir)dependencies\xed\include\public\xed;$(ProjectDir)dependencies\cli-parser\;$(ProjectDir)include\;$(IncludePath);$(ProjectDir)include\</IncludePath> <IncludePath>$(ProjectDir)dependencies\xtils;$(ProjectDir)dependencies\linux-pe\includes;$(ProjectDir)dependencies\xed\obj\wkit\include\xed\;$(ProjectDir)dependencies\cli-parser\;$(ProjectDir)include\;$(IncludePath);$(ProjectDir)include\</IncludePath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental> <LinkIncremental>false</LinkIncremental>
<IncludePath>$(ProjectDir)dependencies\xtils;$(ProjectDir)dependencies\linux-pe\includes;$(ProjectDir)dependencies\xed\include\public\xed;$(ProjectDir)dependencies\cli-parser\;$(IncludePath);$(ProjectDir)include\</IncludePath> <IncludePath>$(ProjectDir)dependencies\xtils;$(ProjectDir)dependencies\linux-pe\includes;$(ProjectDir)dependencies\xed\obj\wkit\include\xed\;$(ProjectDir)dependencies\cli-parser\;$(IncludePath);$(ProjectDir)include\</IncludePath>
</PropertyGroup> </PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile> <ClCompile>
@ -143,6 +151,7 @@
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>$(ProjectDir)dependencies\xed\obj\wkit\lib\xed.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
<ProjectReference> <ProjectReference>
<LinkLibraryDependencies>true</LinkLibraryDependencies> <LinkLibraryDependencies>true</LinkLibraryDependencies>
@ -167,6 +176,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>$(ProjectDir)dependencies\xed\obj\wkit\lib\xed.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
<ProjectReference> <ProjectReference>
<LinkLibraryDependencies>true</LinkLibraryDependencies> <LinkLibraryDependencies>true</LinkLibraryDependencies>

@ -37,12 +37,36 @@
<Filter Include="Source Files\theo"> <Filter Include="Source Files\theo">
<UniqueIdentifier>{348d8bd4-31b6-445d-a487-bc39267daf6b}</UniqueIdentifier> <UniqueIdentifier>{348d8bd4-31b6-445d-a487-bc39267daf6b}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="Header Files\theo\engine">
<UniqueIdentifier>{95451c54-49b0-4f77-ab9d-114bc75a44f3}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\theo\obf_pass">
<UniqueIdentifier>{56c94e09-c085-4b88-b732-cdf5f0663af1}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\theo\engine">
<UniqueIdentifier>{582e72f3-82c8-412f-b686-8e767eb5c229}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\theo\obf_pass">
<UniqueIdentifier>{e42f4fe1-e21d-4e37-9b35-320770c78e5f}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="src\main.cpp"> <ClCompile Include="src\main.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\theo\engine.cpp"> <ClCompile Include="src\theo\engine\engine.cpp">
<Filter>Source Files\theo\engine</Filter>
</ClCompile>
<ClCompile Include="src\theo\engine\iff.cpp">
<Filter>Source Files\theo\engine</Filter>
</ClCompile>
<ClCompile Include="src\theo\engine\swapchain.cpp">
<Filter>Source Files\theo\engine</Filter>
</ClCompile>
<ClCompile Include="src\theo\obf_pass\obf_pass_wrapper.cpp">
<Filter>Source Files\theo\obf_pass</Filter>
</ClCompile>
<ClCompile Include="src\theo\symbol.cpp">
<Filter>Source Files\theo</Filter> <Filter>Source Files\theo</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
@ -259,13 +283,25 @@
<ClInclude Include="dependencies\xed\include\public\xed\xed-version.h"> <ClInclude Include="dependencies\xed\include\public\xed\xed-version.h">
<Filter>Header Files\xed</Filter> <Filter>Header Files\xed</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="include\theo\engine.hpp"> <ClInclude Include="include\theo\symbol.hpp">
<Filter>Header Files\theo</Filter> <Filter>Header Files\theo</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="include\theo\obf_pass.hpp"> <ClInclude Include="include\theo\engine\engine.hpp">
<Filter>Header Files\theo</Filter> <Filter>Header Files\theo\engine</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="include\theo\symbol.hpp"> <ClInclude Include="include\theo\engine\iff.hpp">
<Filter>Header Files\theo\engine</Filter>
</ClInclude>
<ClInclude Include="include\theo\engine\swapchain.hpp">
<Filter>Header Files\theo\engine</Filter>
</ClInclude>
<ClInclude Include="include\theo\obf_pass\obf_pass.hpp">
<Filter>Header Files\theo\obf_pass</Filter>
</ClInclude>
<ClInclude Include="include\theo\obf_pass\obf_pass_wrapper.hpp">
<Filter>Header Files\theo\obf_pass</Filter>
</ClInclude>
<ClInclude Include="include\theo\theo.hpp">
<Filter>Header Files\theo</Filter> <Filter>Header Files\theo</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>

Loading…
Cancel
Save