Theodosius v3.0
Jit linker, symbol mapper, and obfuscator
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
33namespace theo {
34theo_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
45std::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
56std::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
68std::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