Theodosius v3.0
Jit linker, symbol mapper, and obfuscator
main.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 <Windows.h>
32#include <psapi.h>
33
34#include <filesystem>
35#include <fstream>
36#include <iostream>
37
38#include <spdlog/spdlog.h>
39#include <theo.hpp>
40
41#include <obf/engine.hpp>
45
46#include "hello_world_pass.hpp"
47
48namespace fs = std::filesystem;
49
50/// <summary>
51/// example usage of how to interface with theo. please refer to the source code
52/// of this function for details.
53/// </summary>
54/// <param name="argc"></param>
55/// <param name="argv"></param>
56/// <returns></returns>
57int main(int argc, char* argv[]) {
58 if (argc < 2)
59 return -1;
60
61 // read in lib file...
62 std::ifstream f(argv[1], std::ios::binary);
63 auto fsize = fs::file_size(fs::path(argv[1]));
64 std::vector<std::uint8_t> fdata;
65 fdata.resize(fsize);
66 f.read((char*)fdata.data(), fsize);
67
68 LoadLibraryA("user32.dll");
69 LoadLibraryA("win32u.dll");
70
71 // declare your allocator, resolver, and copier lambda functions.
72 //
73
75 [&](std::uint32_t size,
76 coff::section_characteristics_t section_type) -> std::uintptr_t {
77 return reinterpret_cast<std::uintptr_t>(VirtualAlloc(
78 NULL, size, MEM_COMMIT | MEM_RESERVE,
79 section_type.mem_execute ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE));
80 };
81
82 theo::recomp::copier_t copier = [&](std::uintptr_t ptr, void* buff,
83 std::uint32_t size) {
84 std::memcpy((void*)ptr, buff, size);
85 };
86
87 theo::recomp::resolver_t resolver = [&](std::string sym) -> std::uintptr_t {
88 auto loaded_modules = std::make_unique<HMODULE[]>(64);
89 std::uintptr_t result = 0u, loaded_module_sz = 0u;
90 if (!EnumProcessModules(GetCurrentProcess(), loaded_modules.get(), 512,
91 (PDWORD)&loaded_module_sz))
92 return {};
93
94 for (auto i = 0u; i < loaded_module_sz / 8u; i++) {
95 wchar_t file_name[MAX_PATH] = L"";
96 if (!GetModuleFileNameExW(GetCurrentProcess(), loaded_modules.get()[i],
97 file_name, _countof(file_name)))
98 continue;
99
100 if ((result = reinterpret_cast<std::uintptr_t>(
101 GetProcAddress(LoadLibraryW(file_name), sym.c_str()))))
102 break;
103 }
104 return result;
105 };
106
107 // init enc/dec tables only once... important that this is done before adding
108 // obfuscation passes to the engine...
109 //
110 xed_tables_init();
111
112 // order matters, the order in which the pass is added is the order they
113 // will be executed!
114 //
115 auto engine = theo::obf::engine_t::get();
116
117 // add in our hello world pass here
118 //
119 engine->add_pass(theo::obf::hello_world_pass_t::get());
120
121 // add the rest of the passes in this order. this order is important.
122 //
123 engine->add_pass(theo::obf::reloc_transform_pass_t::get());
124 engine->add_pass(theo::obf::next_inst_pass_t::get());
125 engine->add_pass(theo::obf::jcc_rewrite_pass_t::get());
126
127 std::string entry_name;
128 std::cout << "enter the name of the entry point: ";
129 std::cin >> entry_name;
130
131 // create a theo object and pass in the lib, your allocator, copier, and
132 // resolver functions, as well as the entry point symbol name.
133 //
134 theo::theo_t t(fdata, {allocator, copier, resolver}, entry_name.data());
135
136 // call the decompose method to decompose the lib into coff files and extract
137 // the symbols that are used. the result of this call will be an optional
138 // value containing the number of symbols extracted.
139 //
140 auto res = t.decompose();
141
142 if (!res.has_value()) {
143 spdlog::error("decomposition failed...\n");
144 return -1;
145 }
146
147 spdlog::info("decomposed {} symbols...", res.value());
148 auto entry_pnt = t.compose();
149 spdlog::info("entry point address: {:X}", entry_pnt);
150 reinterpret_cast<void (*)()>(entry_pnt)();
151}