1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
#include "binaryninjaapi.h"
#include "ffi.h"
#include "highlevelilinstruction.h"
using namespace BinaryNinja;
using namespace std;
BNDerivedString DerivedString::ToAPIObject(bool owned) const
{
BNDerivedString result;
result.value = owned ? BNDuplicateStringRef(value.GetObject()) : value.GetObject();
result.locationValid = location.has_value();
if (location.has_value())
{
result.location.locationType = location->locationType;
result.location.addr = location->addr;
result.location.len = location->len;
}
else
{
result.location.locationType = DataBackedStringLocation;
result.location.addr = 0;
result.location.len = 0;
}
result.customType = customType ? customType->GetObject() : nullptr;
return result;
}
DerivedString DerivedString::FromAPIObject(BNDerivedString* str, bool owned)
{
DerivedString result;
result.value = StringRef(owned ? str->value : BNDuplicateStringRef(str->value));
if (str->locationValid)
result.location = {str->location.locationType, str->location.addr, str->location.len};
result.customType = str->customType ? new CustomStringType(str->customType) : nullptr;
return result;
}
CustomStringType::CustomStringType(BNCustomStringType* type)
{
m_object = type;
}
string CustomStringType::GetName() const
{
char* name = BNGetCustomStringTypeName(m_object);
string result = name;
BNFreeString(name);
return result;
}
string CustomStringType::GetStringPrefix() const
{
char* prefix = BNGetCustomStringTypePrefix(m_object);
string result = prefix;
BNFreeString(prefix);
return result;
}
string CustomStringType::GetStringPostfix() const
{
char* postfix = BNGetCustomStringTypePostfix(m_object);
string result = postfix;
BNFreeString(postfix);
return result;
}
Ref<CustomStringType> CustomStringType::Register(
const std::string& name, const std::string& stringPrefix, const std::string& stringPostfix)
{
BNCustomStringTypeInfo info;
info.name = (char*)name.c_str();
info.stringPrefix = (char*)stringPrefix.c_str();
info.stringPostfix = (char*)stringPostfix.c_str();
BNCustomStringType* result = BNRegisterCustomStringType(&info);
return new CustomStringType(result);
}
StringRecognizer::StringRecognizer(const std::string& name): m_nameForRegister(name)
{
m_object = nullptr;
}
StringRecognizer::StringRecognizer(BNStringRecognizer* recognizer)
{
m_object = recognizer;
}
string StringRecognizer::GetName() const
{
char* name = BNGetStringRecognizerName(m_object);
string result = name;
BNFreeString(name);
return result;
}
bool StringRecognizer::IsValidForType(HighLevelILFunction*, Type*)
{
return true;
}
std::optional<DerivedString> StringRecognizer::RecognizeConstant(const HighLevelILInstruction&, Type*, int64_t)
{
return std::nullopt;
}
std::optional<DerivedString> StringRecognizer::RecognizeConstantPointer(const HighLevelILInstruction&, Type*, int64_t)
{
return std::nullopt;
}
std::optional<DerivedString> StringRecognizer::RecognizeExternPointer(
const HighLevelILInstruction&, Type*, int64_t, uint64_t)
{
return std::nullopt;
}
std::optional<DerivedString> StringRecognizer::RecognizeImport(const HighLevelILInstruction&, Type*, int64_t)
{
return std::nullopt;
}
std::optional<DerivedString> StringRecognizer::RecognizeConstantData(const HighLevelILInstruction&)
{
return std::nullopt;
}
void StringRecognizer::Register(StringRecognizer* recognizer)
{
BNCustomStringRecognizer callbacks;
callbacks.context = recognizer;
callbacks.isValidForType = IsValidForTypeCallback;
callbacks.recognizeConstant = RecognizeConstantCallback;
callbacks.recognizeConstantPointer = RecognizeConstantPointerCallback;
callbacks.recognizeExternPointer = RecognizeExternPointerCallback;
callbacks.recognizeImport = RecognizeImportCallback;
callbacks.recognizeConstantData = RecognizeConstantDataCallback;
recognizer->AddRefForRegistration();
recognizer->m_object = BNRegisterStringRecognizer(recognizer->m_nameForRegister.c_str(), &callbacks);
}
bool StringRecognizer::IsValidForTypeCallback(void* ctxt, BNHighLevelILFunction* hlil, BNType* type)
{
StringRecognizer* recognizer = (StringRecognizer*)ctxt;
Ref<HighLevelILFunction> hlilObj = new HighLevelILFunction(BNNewHighLevelILFunctionReference(hlil));
Ref<Type> typeObj = new Type(BNNewTypeReference(type));
return recognizer->IsValidForType(hlilObj, typeObj);
}
bool StringRecognizer::RecognizeConstantCallback(
void* ctxt, BNHighLevelILFunction* hlil, size_t expr, BNType* type, int64_t val, BNDerivedString* result)
{
StringRecognizer* recognizer = (StringRecognizer*)ctxt;
Ref<HighLevelILFunction> hlilObj = new HighLevelILFunction(BNNewHighLevelILFunctionReference(hlil));
HighLevelILInstruction instr = hlilObj->GetExpr(expr);
Ref<Type> typeObj = new Type(BNNewTypeReference(type));
auto str = recognizer->RecognizeConstant(instr, typeObj, val);
if (!str.has_value())
return false;
*result = str->ToAPIObject(true);
return true;
}
bool StringRecognizer::RecognizeConstantPointerCallback(
void* ctxt, BNHighLevelILFunction* hlil, size_t expr, BNType* type, int64_t val, BNDerivedString* result)
{
StringRecognizer* recognizer = (StringRecognizer*)ctxt;
Ref<HighLevelILFunction> hlilObj = new HighLevelILFunction(BNNewHighLevelILFunctionReference(hlil));
HighLevelILInstruction instr = hlilObj->GetExpr(expr);
Ref<Type> typeObj = new Type(BNNewTypeReference(type));
auto str = recognizer->RecognizeConstantPointer(instr, typeObj, val);
if (!str.has_value())
return false;
*result = str->ToAPIObject(true);
return true;
}
bool StringRecognizer::RecognizeExternPointerCallback(void* ctxt, BNHighLevelILFunction* hlil, size_t expr,
BNType* type, int64_t val, uint64_t offset, BNDerivedString* result)
{
StringRecognizer* recognizer = (StringRecognizer*)ctxt;
Ref<HighLevelILFunction> hlilObj = new HighLevelILFunction(BNNewHighLevelILFunctionReference(hlil));
HighLevelILInstruction instr = hlilObj->GetExpr(expr);
Ref<Type> typeObj = new Type(BNNewTypeReference(type));
auto str = recognizer->RecognizeExternPointer(instr, typeObj, val, offset);
if (!str.has_value())
return false;
*result = str->ToAPIObject(true);
return true;
}
bool StringRecognizer::RecognizeImportCallback(
void* ctxt, BNHighLevelILFunction* hlil, size_t expr, BNType* type, int64_t val, BNDerivedString* result)
{
StringRecognizer* recognizer = (StringRecognizer*)ctxt;
Ref<HighLevelILFunction> hlilObj = new HighLevelILFunction(BNNewHighLevelILFunctionReference(hlil));
HighLevelILInstruction instr = hlilObj->GetExpr(expr);
Ref<Type> typeObj = new Type(BNNewTypeReference(type));
auto str = recognizer->RecognizeImport(instr, typeObj, val);
if (!str.has_value())
return false;
*result = str->ToAPIObject(true);
return true;
}
bool StringRecognizer::RecognizeConstantDataCallback(
void* ctxt, BNHighLevelILFunction* hlil, size_t expr, BNDerivedString* result)
{
StringRecognizer* recognizer = (StringRecognizer*)ctxt;
Ref<HighLevelILFunction> hlilObj = new HighLevelILFunction(BNNewHighLevelILFunctionReference(hlil));
HighLevelILInstruction instr = hlilObj->GetExpr(expr);
auto str = recognizer->RecognizeConstantData(instr);
if (!str.has_value())
return false;
*result = str->ToAPIObject(true);
return true;
}
Ref<StringRecognizer> StringRecognizer::GetByName(const std::string& name)
{
BNStringRecognizer* recognizer = BNGetStringRecognizerByName(name.c_str());
if (!recognizer)
return nullptr;
return new CoreStringRecognizer(recognizer);
}
vector<Ref<StringRecognizer>> StringRecognizer::GetRecognizers()
{
size_t count = 0;
BNStringRecognizer** recognizers = BNGetStringRecognizerList(&count);
vector<Ref<StringRecognizer>> result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
result.push_back(new CoreStringRecognizer(recognizers[i]));
BNFreeStringRecognizerList(recognizers);
return result;
}
CoreStringRecognizer::CoreStringRecognizer(BNStringRecognizer* recognizer):
StringRecognizer(recognizer)
{
}
bool CoreStringRecognizer::IsValidForType(HighLevelILFunction* func, Type* type)
{
return BNIsStringRecognizerValidForType(m_object, func->GetObject(), type->GetObject());
}
std::optional<DerivedString> CoreStringRecognizer::RecognizeConstant(
const HighLevelILInstruction& instr, Type* type, int64_t val)
{
BNDerivedString str;
if (!BNStringRecognizerRecognizeConstant(m_object, instr.function->GetObject(), instr.exprIndex,
type->GetObject(), val, &str))
return std::nullopt;
return DerivedString::FromAPIObject(&str, true);
}
std::optional<DerivedString> CoreStringRecognizer::RecognizeConstantPointer(
const HighLevelILInstruction& instr, Type* type, int64_t val)
{
BNDerivedString str;
if (!BNStringRecognizerRecognizeConstantPointer(m_object, instr.function->GetObject(), instr.exprIndex,
type->GetObject(), val, &str))
return std::nullopt;
return DerivedString::FromAPIObject(&str, true);
}
std::optional<DerivedString> CoreStringRecognizer::RecognizeExternPointer(
const HighLevelILInstruction& instr, Type* type, int64_t val, uint64_t offset)
{
BNDerivedString str;
if (!BNStringRecognizerRecognizeExternPointer(m_object, instr.function->GetObject(), instr.exprIndex,
type->GetObject(), val, offset, &str))
return std::nullopt;
return DerivedString::FromAPIObject(&str, true);
}
std::optional<DerivedString> CoreStringRecognizer::RecognizeImport(
const HighLevelILInstruction& instr, Type* type, int64_t val)
{
BNDerivedString str;
if (!BNStringRecognizerRecognizeImport(m_object, instr.function->GetObject(), instr.exprIndex,
type->GetObject(), val, &str))
return std::nullopt;
return DerivedString::FromAPIObject(&str, true);
}
std::optional<DerivedString> CoreStringRecognizer::RecognizeConstantData(
const HighLevelILInstruction& instr)
{
BNDerivedString str;
if (!BNStringRecognizerRecognizeConstantData(m_object, instr.function->GetObject(), instr.exprIndex, &str))
return std::nullopt;
return DerivedString::FromAPIObject(&str, true);
}
|