|
| 1 | +#define _CRT_SECURE_NO_WARNINGS |
| 2 | +#include <cinttypes> |
| 3 | +#include <cstdio> |
| 4 | +#include <cstring> |
| 5 | +#include <map> |
| 6 | +#include <functional> |
| 7 | +#include <vector> |
| 8 | +#include "binaryninjaapi.h" |
| 9 | +#include "highlevelilinstruction.h" |
| 10 | + |
| 11 | +using namespace BinaryNinja; |
| 12 | +using namespace std; |
| 13 | + |
| 14 | + |
| 15 | +static Ref<CustomStringType> g_encodedStringType; |
| 16 | + |
| 17 | + |
| 18 | +class EncodedStringRecognizer : public StringRecognizer |
| 19 | +{ |
| 20 | + typedef function<uint8_t(uint8_t, uint8_t)> Decoder; |
| 21 | + map<string, Decoder> m_decoders; |
| 22 | + |
| 23 | +public: |
| 24 | + EncodedStringRecognizer() : StringRecognizer("encoded_strings") |
| 25 | + { |
| 26 | + // Initialize decoders |
| 27 | + m_decoders["xor_encoded"] = [](uint8_t encoded, uint8_t key) -> uint8_t { |
| 28 | + return encoded ^ key; |
| 29 | + }; |
| 30 | + m_decoders["sub_encoded"] = [](uint8_t encoded, uint8_t key) -> uint8_t { |
| 31 | + return encoded - key; |
| 32 | + }; |
| 33 | + m_decoders["add_encoded"] = [](uint8_t encoded, uint8_t key) -> uint8_t { |
| 34 | + return encoded + key; |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + bool IsValidForType(HighLevelILFunction*, Type* type) override |
| 39 | + { |
| 40 | + if (!type || type->GetClass() != PointerTypeClass) |
| 41 | + return false; |
| 42 | + |
| 43 | + auto target = type->GetChildType(); |
| 44 | + if (!target.GetValue()) |
| 45 | + return false; |
| 46 | + |
| 47 | + // Check if any decoder attribute exists |
| 48 | + for (const auto& decoder : m_decoders) |
| 49 | + { |
| 50 | + if (target->GetAttribute(decoder.first).has_value()) |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + optional<DerivedString> RecognizeConstantPointer( |
| 58 | + const HighLevelILInstruction& instr, Type* type, int64_t val) override |
| 59 | + { |
| 60 | + if (!type || type->GetClass() != PointerTypeClass) |
| 61 | + return std::nullopt; |
| 62 | + |
| 63 | + auto target = type->GetChildType(); |
| 64 | + if (!target) |
| 65 | + return std::nullopt; |
| 66 | + |
| 67 | + // Find the decoder and values |
| 68 | + vector<uint8_t> values; |
| 69 | + Decoder chosenDecoder; |
| 70 | + |
| 71 | + for (const auto& [name, decoder] : m_decoders) |
| 72 | + { |
| 73 | + if (auto attr = target->GetAttribute(name); attr.has_value()) |
| 74 | + { |
| 75 | + // Parse hex string |
| 76 | + if (attr->length() % 2 != 0) |
| 77 | + return std::nullopt; |
| 78 | + |
| 79 | + for (size_t i = 0; i < attr->length(); i += 2) |
| 80 | + { |
| 81 | + string byteStr = attr->substr(i, 2); |
| 82 | + try |
| 83 | + { |
| 84 | + values.push_back((uint8_t)stoul(byteStr, nullptr, 16)); |
| 85 | + } |
| 86 | + catch (...) |
| 87 | + { |
| 88 | + return std::nullopt; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + chosenDecoder = decoder; |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (values.empty() || !chosenDecoder) |
| 98 | + return std::nullopt; |
| 99 | + |
| 100 | + bool encodedNull = target->GetAttribute("encoded_null").has_value(); |
| 101 | + |
| 102 | + // Decode the string |
| 103 | + vector<uint8_t> resultBytes; |
| 104 | + size_t i = 0; |
| 105 | + Ref<BinaryView> view = instr.function->GetFunction()->GetView(); |
| 106 | + |
| 107 | + while (true) |
| 108 | + { |
| 109 | + uint8_t byte; |
| 110 | + if (view->Read(&byte, val + i, 1) != 1) |
| 111 | + return std::nullopt; |
| 112 | + |
| 113 | + // Check for unencoded null terminator |
| 114 | + if (!encodedNull && byte == 0) |
| 115 | + break; |
| 116 | + |
| 117 | + // Decode the byte |
| 118 | + byte = chosenDecoder(byte, values[i % values.size()]); |
| 119 | + |
| 120 | + // Check for encoded null terminator |
| 121 | + if (byte == 0) |
| 122 | + break; |
| 123 | + |
| 124 | + resultBytes.push_back(byte); |
| 125 | + i++; |
| 126 | + } |
| 127 | + |
| 128 | + // Create the derived string |
| 129 | + DerivedStringLocation loc(DataBackedStringLocation, val, i); |
| 130 | + return DerivedString(string((char*)resultBytes.data(), resultBytes.size()), loc, g_encodedStringType); |
| 131 | + } |
| 132 | +}; |
| 133 | + |
| 134 | + |
| 135 | +extern "C" |
| 136 | +{ |
| 137 | + BN_DECLARE_CORE_ABI_VERSION |
| 138 | + |
| 139 | + BINARYNINJAPLUGIN void CorePluginDependencies() |
| 140 | + { |
| 141 | + } |
| 142 | + |
| 143 | + BINARYNINJAPLUGIN bool CorePluginInit() |
| 144 | + { |
| 145 | + g_encodedStringType = CustomStringType::Register("Encoded", "", "_enc"); |
| 146 | + StringRecognizer::Register(new EncodedStringRecognizer()); |
| 147 | + return true; |
| 148 | + } |
| 149 | +} |
0 commit comments