Skip to content

Commit 4c125cc

Browse files
committed
Add C++ encoded strings example
1 parent 2af5f74 commit 4c125cc

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Personal+ edition (free edition doesn't get plugins due to no API access)
22
add_subdirectory(background_task)
33
add_subdirectory(breakpoint)
4+
add_subdirectory(encoded_strings)
45
add_subdirectory(x86_extension)
56
add_subdirectory(workflows/inliner)
67
add_subdirectory(workflows/tailcall)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
2+
3+
project(encoded_strings CXX C)
4+
5+
add_library(${PROJECT_NAME} SHARED
6+
src/encoded_strings.cpp)
7+
8+
if(NOT BN_API_BUILD_EXAMPLES AND NOT BN_INTERNAL_BUILD)
9+
# Out-of-tree build
10+
find_path(
11+
BN_API_PATH
12+
NAMES binaryninjaapi.h
13+
HINTS ../.. binaryninjaapi $ENV{BN_API_PATH}
14+
REQUIRED
15+
)
16+
add_subdirectory(${BN_API_PATH} api)
17+
endif()
18+
19+
target_link_libraries(${PROJECT_NAME}
20+
binaryninjaapi)
21+
22+
set_target_properties(${PROJECT_NAME} PROPERTIES
23+
CXX_STANDARD 20
24+
CXX_VISIBILITY_PRESET hidden
25+
CXX_STANDARD_REQUIRED ON
26+
VISIBILITY_INLINES_HIDDEN ON
27+
POSITION_INDEPENDENT_CODE ON
28+
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/out/bin)
29+
30+
bn_install_plugin(${PROJECT_NAME})
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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

Comments
 (0)