|
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
| 3 | +#include <vector> |
| 4 | +#include <string> |
| 5 | +#include <functional> |
| 6 | +#include <triton/context.hpp> |
| 7 | +#include <triton/x86Specifications.hpp> |
| 8 | +#include <LIEF/ELF.hpp> |
| 9 | +#include <fmt/format.h> |
| 10 | +#include <sstream> |
| 11 | +#include <iomanip> |
| 12 | + |
| 13 | +#define DEBUG 0 |
| 14 | + |
| 15 | +std::string getMemoryString(const triton::Context& ctx, triton::uint64 addr) { |
| 16 | + std::string s; |
| 17 | + size_t index = 0; |
| 18 | + |
| 19 | + while (auto value = ctx.getConcreteMemoryValue(addr + index)) { |
| 20 | + char c = static_cast<char>(value); |
| 21 | + if (std::isprint(c)) { |
| 22 | + s += c; |
| 23 | + } |
| 24 | + index++; |
| 25 | + } |
| 26 | + |
| 27 | + return s; |
| 28 | +} |
| 29 | + |
| 30 | +std::string getFormatString(const triton::Context& ctx, triton::uint64 addr) { |
| 31 | + std::string s = getMemoryString(ctx, addr); |
| 32 | + // Implement string replacements here if needed |
| 33 | + return s; |
| 34 | +} |
| 35 | + |
| 36 | +triton::uint64 strlenHandler(triton::Context& ctx) { |
| 37 | + if constexpr (DEBUG) { |
| 38 | + std::cout << "[+] Strlen hooked" << std::endl; |
| 39 | + } |
| 40 | + auto arg1 = getMemoryString(ctx, static_cast<triton::uint64>(ctx.getConcreteRegisterValue(ctx.registers.x86_rdi))); |
| 41 | + return arg1.length(); |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +triton::uint64 printfHandler(triton::Context& ctx) { |
| 47 | + if constexpr (DEBUG) { |
| 48 | + std::cout << "[+] printf hooked" << std::endl; |
| 49 | + } |
| 50 | + auto format_str = getFormatString(ctx, static_cast<triton::uint64>(ctx.getConcreteRegisterValue(ctx.registers.x86_rdi))); |
| 51 | + auto arg1 = ctx.getConcreteRegisterValue(ctx.registers.x86_rsi); |
| 52 | + |
| 53 | + std::stringstream ss; |
| 54 | + size_t pos = format_str.find("%ld"); |
| 55 | + if (pos != std::string::npos) { |
| 56 | + ss << format_str.substr(0, pos) << arg1 << format_str.substr(pos + 3); |
| 57 | + } else { |
| 58 | + ss << format_str; |
| 59 | + } |
| 60 | + |
| 61 | + std::string formatted_str = ss.str(); |
| 62 | + std::cout << formatted_str << std::endl; |
| 63 | + return formatted_str.length(); |
| 64 | +} |
| 65 | + |
| 66 | +triton::uint64 libcMainHandler(triton::Context& ctx) { |
| 67 | + if constexpr (DEBUG) { |
| 68 | + std::cout << "[+] __libc_start_main hooked" << std::endl; |
| 69 | + } |
| 70 | + |
| 71 | + auto main = ctx.getConcreteRegisterValue(ctx.registers.x86_rdi); |
| 72 | + |
| 73 | + ctx.setConcreteRegisterValue(ctx.registers.x86_rsp, |
| 74 | + ctx.getConcreteRegisterValue(ctx.registers.x86_rsp) - 8); |
| 75 | + |
| 76 | + auto ret2main = triton::arch::MemoryAccess(static_cast<triton::uint64>(ctx.getConcreteRegisterValue(ctx.registers.x86_rsp)), 8); |
| 77 | + ctx.setConcreteMemoryValue(ret2main, main); |
| 78 | + |
| 79 | + ctx.concretizeRegister(ctx.registers.x86_rdi); |
| 80 | + ctx.concretizeRegister(ctx.registers.x86_rsi); |
| 81 | + |
| 82 | + std::vector<std::string> argvs = {"sample_1", "my_first_arg"}; |
| 83 | + |
| 84 | + triton::uint64 base = 0x20000000; |
| 85 | + std::vector<triton::uint64> addrs; |
| 86 | + |
| 87 | + for (const auto& argv : argvs) { |
| 88 | + addrs.push_back(base); |
| 89 | + ctx.setConcreteMemoryAreaValue(base, reinterpret_cast<const triton::uint8*>(argv.c_str()), argv.length() + 1); |
| 90 | + base += argv.length() + 1; |
| 91 | + } |
| 92 | + |
| 93 | + triton::uint64 argc = argvs.size(); |
| 94 | + triton::uint64 argv = base; |
| 95 | + for (const auto& addr : addrs) { |
| 96 | + ctx.setConcreteMemoryValue(triton::arch::MemoryAccess(base, 8), addr); |
| 97 | + base += 8; |
| 98 | + } |
| 99 | + |
| 100 | + ctx.setConcreteRegisterValue(ctx.registers.x86_rdi, argc); |
| 101 | + ctx.setConcreteRegisterValue(ctx.registers.x86_rsi, argv); |
| 102 | + |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
| 106 | +struct CustomRelocation { |
| 107 | + std::string name; |
| 108 | + std::function<triton::uint64(triton::Context&)> handler; |
| 109 | + triton::uint64 address; |
| 110 | +}; |
| 111 | + |
| 112 | +std::vector<CustomRelocation> customRelocation = { |
| 113 | + {"strlen", strlenHandler, 0x10000000}, |
| 114 | + {"printf", printfHandler, 0x10000001}, |
| 115 | + {"__libc_start_main", libcMainHandler, 0x10000002}, |
| 116 | +}; |
| 117 | + |
| 118 | +void hookingHandler(triton::Context& ctx) { |
| 119 | + auto pc = ctx.getConcreteRegisterValue(ctx.registers.x86_rip); |
| 120 | + for (const auto& rel : customRelocation) { |
| 121 | + if (rel.address == pc) { |
| 122 | + auto ret_value = rel.handler(ctx); |
| 123 | + ctx.setConcreteRegisterValue(ctx.registers.x86_rax, ret_value); |
| 124 | + |
| 125 | + auto ret_addr = ctx.getConcreteMemoryValue(triton::arch::MemoryAccess( |
| 126 | + static_cast<triton::uint64>(ctx.getConcreteRegisterValue(ctx.registers.x86_rsp)), 8)); |
| 127 | + |
| 128 | + ctx.setConcreteRegisterValue(ctx.registers.x86_rip, ret_addr); |
| 129 | + ctx.setConcreteRegisterValue(ctx.registers.x86_rsp, |
| 130 | + ctx.getConcreteRegisterValue(ctx.registers.x86_rsp) + 8); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +void emulate(triton::Context& ctx, triton::uint64 pc) { |
| 136 | + if constexpr (DEBUG) { |
| 137 | + std::cout << "[+] Starting emulation." << std::endl; |
| 138 | + } |
| 139 | + |
| 140 | + while (pc) { |
| 141 | + auto opcode = ctx.getConcreteMemoryAreaValue(pc, 16); |
| 142 | + |
| 143 | + triton::arch::Instruction instruction; |
| 144 | + instruction.setOpcode(opcode.data(), opcode.size()); |
| 145 | + instruction.setAddress(pc); |
| 146 | + |
| 147 | + ctx.processing(instruction); |
| 148 | + if constexpr (DEBUG) { |
| 149 | + std::cout << instruction << std::endl; |
| 150 | + } |
| 151 | + |
| 152 | + if (instruction.getType() == triton::arch::x86::ID_INS_HLT || instruction.getType() == triton::arch::x86::ID_INS_RET) { // add ID_INS_RET too maybe? see issue https://github.com/JonathanSalwan/Triton/issues/912 |
| 153 | + break; |
| 154 | + } |
| 155 | + |
| 156 | + hookingHandler(ctx); |
| 157 | + |
| 158 | + pc = static_cast<triton::uint64>(ctx.getConcreteRegisterValue(ctx.registers.x86_rip)); |
| 159 | + } |
| 160 | + if constexpr (DEBUG) { |
| 161 | + std::cout << "[+] Emulation done." << std::endl; |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +std::unique_ptr<LIEF::ELF::Binary> loadBinary(triton::Context& ctx, const std::string& path) { |
| 166 | + std::unique_ptr<LIEF::ELF::Binary> binary = LIEF::ELF::Parser::parse(path); |
| 167 | + if (!binary) { |
| 168 | + throw std::runtime_error("Failed to parse ELF binary"); |
| 169 | + } |
| 170 | + |
| 171 | + for (const auto& segment : binary->segments()) { |
| 172 | + if (segment.type() != LIEF::ELF::Segment::TYPE::LOAD) { |
| 173 | + continue; |
| 174 | + } |
| 175 | + triton::uint64 vaddr = segment.virtual_address(); |
| 176 | + const auto& content = segment.content(); // This is now a span<const uint8_t> |
| 177 | + if constexpr (DEBUG) { |
| 178 | + std::cout << "[+] Loading 0x" << std::hex << vaddr << " - 0x" << (vaddr + content.size()) << std::endl; |
| 179 | + } |
| 180 | + |
| 181 | + // Use data() and size() methods of span |
| 182 | + ctx.setConcreteMemoryAreaValue(vaddr, content.data(), content.size()); |
| 183 | + } |
| 184 | + |
| 185 | + return binary; |
| 186 | +} |
| 187 | + |
| 188 | +void makeRelocation(triton::Context& ctx, const LIEF::ELF::Binary& binary) { |
| 189 | + for (const auto& pltgot_entry : binary.pltgot_relocations()) { |
| 190 | + const std::string& symbolName = pltgot_entry.symbol()->name(); |
| 191 | + triton::uint64 symbolRelo = pltgot_entry.address(); |
| 192 | + for (const auto& crel : customRelocation) { |
| 193 | + if (symbolName == crel.name) { |
| 194 | + if constexpr (DEBUG) { |
| 195 | + std::cout << "[+] Hooking " << symbolName << std::endl; |
| 196 | + } |
| 197 | + ctx.setConcreteMemoryValue(triton::arch::MemoryAccess(symbolRelo, 8), crel.address); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +int main(int argc, char* argv[]) { |
| 204 | + if (argc != 2) { |
| 205 | + std::cerr << "Usage: " << argv[0] << " <path_to_binary>" << std::endl; |
| 206 | + return 1; |
| 207 | + } |
| 208 | + |
| 209 | + triton::Context ctx; |
| 210 | + ctx.setArchitecture(triton::arch::ARCH_X86_64); |
| 211 | + |
| 212 | + std::unique_ptr<LIEF::ELF::Binary> binary; |
| 213 | + try { |
| 214 | + binary = loadBinary(ctx, argv[1]); |
| 215 | + } catch (const std::exception& e) { |
| 216 | + std::cerr << "Error loading binary: " << e.what() << std::endl; |
| 217 | + return 1; |
| 218 | + } |
| 219 | + |
| 220 | + makeRelocation(ctx, *binary); |
| 221 | + |
| 222 | + ctx.setConcreteRegisterValue(ctx.registers.x86_rbp, 0x7fffffff); |
| 223 | + ctx.setConcreteRegisterValue(ctx.registers.x86_rsp, 0x6fffffff); |
| 224 | + |
| 225 | + triton::uint64 entrypoint = binary->entrypoint(); |
| 226 | + emulate(ctx, entrypoint); |
| 227 | + |
| 228 | + return 0; |
| 229 | +} |
0 commit comments