Theodosius  v3.0
Jit linker, mapper, obfuscator, and mutator
theo.cpp
Go to the documentation of this file.
1 // Copyright (c) 2022, _xeroxz
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // 1. Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 
31 #include <theo.hpp>
32 
33 namespace theo {
34 theo_t::theo_t(std::vector<std::uint8_t>& lib,
35  lnk_fns_t lnkr_fns,
36  const std::string&& entry_sym)
37  : m_dcmp(lib, &m_sym_tbl),
38  m_recmp(&m_dcmp, {}, {}, {}),
39  m_entry_sym(entry_sym) {
40  m_recmp.allocator(std::get<0>(lnkr_fns));
41  m_recmp.copier(std::get<1>(lnkr_fns));
42  m_recmp.resolver(std::get<2>(lnkr_fns));
43 }
44 
45 std::optional<std::uint32_t> theo_t::decompose() {
46  auto res = m_dcmp.decompose(m_entry_sym);
47  if (!res.has_value()) {
48  spdlog::error("failed to decompose...\n");
49  return {};
50  }
51 
52  spdlog::info("decompose successful... {} symbols", res.value()->size());
53  return res.value()->size();
54 }
55 
56 std::uintptr_t theo_t::compose() {
57  // run obfuscation engine on all symbols...
58  //
59  auto engine = obf::engine_t::get();
60  m_sym_tbl.for_each([&](decomp::symbol_t& sym) { engine->run(&sym); });
61 
62  m_recmp.allocate();
63  m_recmp.resolve();
64  m_recmp.copy_syms();
65  return m_recmp.resolve(m_entry_sym.data());
66 }
67 
68 std::uintptr_t theo_t::resolve(const std::string&& sym) {
69  auto val = m_sym_tbl.sym_from_hash(decomp::symbol_t::hash(sym));
70  if (!val.has_value())
71  return {};
72 
73  return val.value()->allocated_at();
74 }
75 } // namespace theo
std::optional< recomp::symbol_table_t * > decompose(std::string &entry_sym)
decomposes (extracts) the symbols used. this function determines all used symbols given the entry poi...
Definition: decomp.cpp:37
symbol_t is an abstraction upon the coff symbol. this allows for easier manipulation of the symbol....
Definition: symbol.hpp:53
std::size_t hash()
gets the hash of the symbol name.
Definition: symbol.cpp:88
static engine_t * get()
get the singleton object of this class.
Definition: engine.cpp:34
void copy_syms()
when called, this function copies symbols into allocations.
Definition: recomp.cpp:155
void allocator(allocator_t alloc)
setter for the allocater lambda function.
Definition: recomp.cpp:163
void resolver(resolver_t resolve)
setter for the resolve lambda function.
Definition: recomp.cpp:171
void resolve()
when called, this function resolves all relocations in every symbol.
Definition: recomp.cpp:92
void allocate()
when called, this function allocates space for every symbol.
Definition: recomp.cpp:40
void copier(copier_t copy)
setter for the copier lambda function.
Definition: recomp.cpp:167
std::optional< decomp::symbol_t * > sym_from_hash(std::size_t hash)
returns an optional pointer to a symbol from the symbol table given the symbols hash (hash of its nam...
void for_each(std::function< void(decomp::symbol_t &sym)> fn)
this function is a wrapper function that allows you to get at each entry in the symbol table by refer...
theo_t(std::vector< std::uint8_t > &lib, lnk_fns_t lnkr_fns, const std::string &&entry_sym)
explicit constructor for theo class.
Definition: theo.cpp:34
std::uintptr_t compose()
compose the decomposed module. This will run obfuscation passes, the map and resolve symbols to each ...
Definition: theo.cpp:56
std::optional< std::uint32_t > decompose()
decomposes the lib file and return the number of symbols that are used.
Definition: theo.cpp:45
std::uintptr_t resolve(const std::string &&sym)
given the name of a symbol, it returns the address of where its mapped.
Definition: theo.cpp:68
The outer most encompassing namespace of this project.
Definition: decomp.hpp:49
std::tuple< recomp::allocator_t, recomp::copier_t, recomp::resolver_t > lnk_fns_t
tuple of functions used by theo to allocate, copy, and resolve symbols.
Definition: theo.hpp:61