Theodosius v3.0
Jit linker, symbol mapper, and obfuscator
gen.hpp
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#pragma once
33#include <recomp/reloc.hpp>
34
35namespace theo::obf::transform {
36/// <summary>
37/// generate a sequence of transformations given an instruction that has a
38/// relocation in it.
39/// </summary>
40/// <param name="inst">instruction that has a relocation in it.</param>
41/// <param name="reloc">meta data relocation object for the instruction.</param>
42/// <param name="low">lowest number of transformations to generate.</param>
43/// <param name="high">highest number of transformations to generate.</param>
44/// <returns></returns>
45inline std::vector<std::uint8_t> generate(xed_decoded_inst_t* inst,
46 recomp::reloc_t* reloc,
47 std::uint8_t low,
48 std::uint8_t high) {
49 auto num_transforms = transform::operation_t::random(low, high);
50 auto num_ops = transform::operations.size();
51 std::vector<std::uint8_t> new_inst_bytes;
52
53 std::uint32_t inst_len = {};
54 std::uint8_t inst_buff[XED_MAX_INSTRUCTION_BYTES];
55 xed_encoder_request_t req;
56
57 xed_state_t istate{XED_MACHINE_MODE_LONG_64, XED_ADDRESS_WIDTH_64b};
58 xed_encoder_request_zero_set_mode(&req, &istate);
59 xed_encoder_request_set_effective_operand_width(&req, 64);
60 xed_encoder_request_set_iclass(&req, XED_ICLASS_PUSHFQ);
61 xed_encode(&req, inst_buff, sizeof(inst_buff), &inst_len);
62 new_inst_bytes.insert(new_inst_bytes.end(), inst_buff, inst_buff + inst_len);
63
64 for (auto cnt = 0u; cnt < num_transforms; ++cnt) {
65 std::uint32_t imm = transform::operation_t::random(
66 0, std::numeric_limits<std::int32_t>::max());
67
68 auto itr = transform::operations.begin();
69 std::advance(itr, transform::operation_t::random(0, num_ops - 1));
70 auto transform_bytes = itr->second->native(inst, imm);
71 new_inst_bytes.insert(new_inst_bytes.end(), transform_bytes.begin(),
72 transform_bytes.end());
73
74 reloc->add_transform(
75 {transform::operations[itr->second->inverse()]->get_transform(), imm});
76 }
77
78 xed_encoder_request_zero_set_mode(&req, &istate);
79 xed_encoder_request_set_effective_operand_width(&req, 64);
80 xed_encoder_request_set_iclass(&req, XED_ICLASS_POPFQ);
81 xed_encode(&req, inst_buff, sizeof(inst_buff), &inst_len);
82 new_inst_bytes.insert(new_inst_bytes.end(), inst_buff, inst_buff + inst_len);
83
84 // inverse the order in which the transformations are executed...
85 //
86 std::reverse(reloc->get_transforms().begin(), reloc->get_transforms().end());
87 return new_inst_bytes;
88}
89} // namespace theo::obf::transform