fucking burnt out on this code, needs work, dont have the time

2.0
_xeroxz 3 years ago
parent 062fdd49bd
commit 41bd01e155

@ -3,20 +3,62 @@
#include <coff/image.hpp>
#include <theo/engine/iff.hpp>
#include <theo/engine/swapchain.hpp>
#include <theo/obf_pass/obf_pass.hpp>
namespace theo
{
class obf_pass_t
{
public:
obf_pass_t()
{
}
virtual void obfuscate( theo::iff_t & ) = 0;
};
class obf_pass_section_t : public obf_pass_t
{
public:
obf_pass_section_t()
{
}
virtual void obfuscate( theo::iff_t::section_t &iff_section ) = 0;
void obfuscate( theo::iff_t &iff ) override
{
std::for_each( iff.sections.begin(), iff.sections.end(),
[ & ]( theo::iff_t::section_t &iff_section ) { obfuscate( iff_section ); } );
}
};
class obf_pass_deadstore_t : public obf_pass_section_t
{
using callback_t = std::function< void( theo::iff_t::section_t &iff_section ) >;
const callback_t callback;
public:
obf_pass_deadstore_t( const callback_t &callback ) : callback( callback )
{
}
void obfuscate( theo::iff_t::section_t &iff_section ) override
{
// TODO impliment deadstore here...
callback( iff_section );
}
};
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 );
void push( std::shared_ptr< obf_pass_t > obf_pass );
void pop();
void run();
private:
std::vector< std::shared_ptr< obf_pass_t > > obf_passes;
std::shared_ptr< theo::swapchain_t > swapchain;
std::vector< theo::obf_pass_t > obf_passes;
};
} // namespace theo

@ -3,14 +3,18 @@
#include <vector>
#include <xtils.hpp>
#define XED_ENCODER
extern "C"
{
#include <xed-decode.h>
#include <xed-encode.h>
#include <xed-interface.h>
}
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:
@ -18,15 +22,18 @@ namespace theo
{
coff::section_header_t header;
std::vector< std::pair< std::uint32_t, coff::symbol_t > > symbols;
std::vector< xed_decoded_inst_t > instrs;
};
explicit iff_t( coff::image_t *img );
std::vector< section_t > sections;
static std::shared_ptr< theo::iff_t > make( coff::image_t *img )
{
return std::make_shared< theo::iff_t >( img );
}
private:
/// <summary>
/// flush changes from "sections" back to img...
/// </summary>
void flush();
coff::image_t *img;
};

@ -8,28 +8,18 @@ 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
public:
struct buffers_t
{
std::vector< std::uint8_t > front, back;
std::shared_ptr< iff_t > iff;
};
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 );
void swap( buffers_t &buffers );
/// <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 );
std::vector< buffers_t > objs;
const std::vector< std::uint8_t > archive;
};
} // namespace theo

@ -1,35 +0,0 @@
#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

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

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

@ -26,4 +26,13 @@ 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 );
auto deadstore_pass = std::make_shared< theo::obf_pass_deadstore_t >( []( theo::iff_t::section_t &iff_section ) {
std::printf( "> section name = %s\n", iff_section.header.name.to_string().data() );
std::printf( "> number of instructions = %d\n", iff_section.instrs.size() );
} );
theo.push( deadstore_pass );
theo.run();
std::getchar();
}

@ -6,27 +6,26 @@ namespace theo
{
}
engine_t &theo::engine_t::add_pass( const obf_pass_t &pass )
void engine_t::push( std::shared_ptr< obf_pass_t > obf_pass )
{
obf_passes.push_back( pass );
return *this;
obf_passes.insert( obf_passes.begin(), obf_pass );
}
engine_t &theo::engine_t::add_passes( const std::vector< obf_pass_t > &passes )
void engine_t::pop()
{
obf_passes.insert( obf_passes.end(), passes.begin(), passes.end() );
return *this;
obf_passes.erase( obf_passes.begin() );
}
void theo::engine_t::run( std::vector< std::uint8_t > &result )
void engine_t::run()
{
std::for_each( obf_passes.begin(), obf_passes.end(), [ & ]( theo::obf_pass_t &obf_pass ) {
std::vector< theo::iff_t > iffs;
swapchain->swap( iffs );
// for each obj, run obfuscation pass on it...
std::for_each( swapchain->objs.begin(), swapchain->objs.end(), [ & ]( swapchain_t::buffers_t &buffer ) {
std::for_each( obf_passes.begin(), obf_passes.end(), [ & ]( std::shared_ptr< theo::obf_pass_t > pass ) {
pass->obfuscate( *buffer.iff );
std::for_each( iffs.begin(), iffs.end(), [ & ]( const theo::iff_t &iff ) {
obf_pass.obfuscate( iff );
swapchain->swap( iffs );
// apply changes made by obfuscation pass to the back buffer...
// then swap the back buffer to the front...
swapchain->swap( buffer );
} );
} );
}

@ -4,27 +4,39 @@ namespace theo
{
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,
[ & ]( const coff::section_header_t &section_header ) {
sections.emplace_back( section_header );
const auto img_ptr = reinterpret_cast< std::uint8_t * >( img );
const auto num_sections = img->file_header.num_sections;
// add symbols to section...
const auto num_symbols = img->file_header.num_symbols;
for ( auto idx = 0u; idx < num_symbols; ++idx )
// important to note that we are making a COPY of this symbol...
sections.back().symbols.push_back( { idx, *img->get_symbol( idx ) } );
} );
for ( auto section_num = 0u; section_num != num_sections; ++section_num )
{
const auto section = img->get_section( section_num );
iff_t::section_t iff_section{ *section };
if ( section->characteristics.mem_execute && section->size_raw_data )
{
xed_error_enum_t err;
xed_decoded_inst_t instr;
std::uint32_t offset = 0u;
const xed_state_t istate{ XED_MACHINE_MODE_LONG_64, XED_ADDRESS_WIDTH_64b };
xed_decoded_inst_zero_set_mode( &instr, &istate );
// decodes instructions for this given section...
while ( ( err = xed_decode( &instr, img_ptr + section->ptr_raw_data + offset,
section->size_raw_data - offset ) ) == XED_ERROR_NONE )
{
offset += xed_decoded_inst_get_length( &instr );
iff_section.instrs.push_back( instr );
}
}
// add all symbols for this section...
for ( auto idx = 0u; idx < img->file_header.num_symbols; ++idx )
if ( img->get_symbol( idx )->section_index - 1 == section_num )
iff_section.symbols.push_back( { idx, *img->get_symbol( idx ) } );
}
}
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 ) {
std::for_each( iff_section.symbols.begin(), iff_section.symbols.end(), [ & ]( const auto &symbol_data ) {
const auto &[ symbol_idx, symbol ] = symbol_data;
img->get_symbol( symbol_idx )->value = symbol.value;
} );
} );
}
} // namespace theo

@ -8,7 +8,9 @@ namespace theo
// 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() } } );
buffers_t buffers{ { coff_img.begin(), coff_img.end() }, { coff_img.begin(), coff_img.end() } };
buffers.iff = theo::iff_t::make( reinterpret_cast< coff::image_t * >( buffers.front.data() ) );
objs.push_back( buffers );
} );
}
@ -17,19 +19,12 @@ namespace theo
return std::make_shared< theo::swapchain_t >( img );
}
void theo::swapchain_t::swap( std::vector< iff_t > &iffs )
void theo::swapchain_t::swap( buffers_t &buffers )
{
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...
}
// flush changes to the back buffer...
buffers.iff->flush();
// then swap buffers so that the front now sees the changes...
buffers.front = buffers.back;
}
} // namespace theo

@ -15,8 +15,6 @@
<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>
<None Include=".clang-format" />
@ -95,8 +93,6 @@
<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\theo.hpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
@ -134,11 +130,11 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<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>
<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\;$(ProjectDir)dependencies\xed\include\public\xed\</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(ProjectDir)dependencies\xtils;$(ProjectDir)dependencies\linux-pe\includes;$(ProjectDir)dependencies\xed\obj\wkit\include\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\;$(ProjectDir)dependencies\xed\include\public\xed\</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
@ -170,6 +166,7 @@
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpplatest</LanguageStandard>
<EnableModules>true</EnableModules>
<Optimization>Disabled</Optimization>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>

@ -63,12 +63,6 @@
<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>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include=".clang-format">
@ -283,9 +277,6 @@
<ClInclude Include="dependencies\xed\include\public\xed\xed-version.h">
<Filter>Header Files\xed</Filter>
</ClInclude>
<ClInclude Include="include\theo\symbol.hpp">
<Filter>Header Files\theo</Filter>
</ClInclude>
<ClInclude Include="include\theo\engine\engine.hpp">
<Filter>Header Files\theo\engine</Filter>
</ClInclude>
@ -295,14 +286,11 @@
<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>
</ClInclude>
<ClInclude Include="include\theo\obf_pass\obf_pass.hpp">
<Filter>Header Files\theo\obf_pass</Filter>
</ClInclude>
</ItemGroup>
</Project>
Loading…
Cancel
Save