This repository was archived by the owner on Jul 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathInfoHandler.cpp
More file actions
260 lines (205 loc) · 9.4 KB
/
InfoHandler.cpp
File metadata and controls
260 lines (205 loc) · 9.4 KB
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
/*
* Copyright (c) 2022 Jon Palmisciano. All rights reserved.
*
* Use of this source code is governed by the BSD 3-Clause license; the full
* terms of the license can be found in the LICENSE.txt file.
*/
#include "InfoHandler.h"
#include "CustomTypes.h"
#include <algorithm>
#include <regex>
using namespace BinaryNinja;
std::string InfoHandler::sanitizeText(const std::string& text)
{
std::string result;
std::string input = text.substr(0, 24);
std::regex re("[a-zA-Z0-9]+");
std::smatch sm;
while (std::regex_search(input, sm, re)) {
std::string part = sm[0];
part[0] = static_cast<char>(std::toupper(part[0]));
result += part;
input = sm.suffix();
}
return result;
}
std::string InfoHandler::sanitizeSelector(const std::string& text)
{
auto result = text;
std::replace(result.begin(), result.end(), ':', '_');
return result;
}
TypeRef InfoHandler::namedType(BinaryViewRef bv, const std::string& name)
{
return Type::NamedType(bv, name);
}
TypeRef InfoHandler::stringType(size_t size)
{
return Type::ArrayType(Type::IntegerType(1, true), size + 1);
}
void InfoHandler::defineVariable(BinaryViewRef bv, uint64_t address, TypeRef type)
{
bv->DefineUserDataVariable(address, type);
}
void InfoHandler::defineSymbol(BinaryViewRef bv, uint64_t address, const std::string& name,
const std::string& prefix, BNSymbolType symbolType)
{
bv->DefineUserSymbol(new Symbol(symbolType, prefix + name, address));
}
void InfoHandler::defineReference(BinaryViewRef bv, uint64_t from, uint64_t to)
{
bv->AddUserDataReference(from, to);
}
void InfoHandler::applyMethodType(BinaryViewRef bv, const ObjectiveNinja::ClassInfo& ci,
const ObjectiveNinja::MethodInfo& mi)
{
auto selectorTokens = mi.selectorTokens();
auto typeTokens = mi.decodedTypeTokens();
// Shorthand for formatting an individual "part" of the type signature.
auto partForIndex = [selectorTokens, typeTokens](size_t i) {
std::string argName;
if (i == 0)
argName = "";
else if (i == 1)
argName = "self";
else if (i == 2)
argName = "sel";
else if (i - 3 < selectorTokens.size())
argName = selectorTokens[i - 3];
return typeTokens[i] + " " + argName;
};
// Build the type string for the method.
std::string typeString;
for (size_t i = 0; i < typeTokens.size(); ++i) {
auto part = partForIndex(i);
std::string suffix;
if (i == 0)
suffix = " (";
else if (i == typeTokens.size() - 1)
suffix = ")";
else
suffix = ", ";
typeString += part + suffix;
}
// Attempt to parse the type string that was just built.
QualifiedNameAndType functionNat;
std::string errors;
if (!bv->ParseTypeString(typeString, functionNat, errors))
return;
// Search for the method's implementation function; apply the type if found.
if (auto f = bv->GetAnalysisFunction(bv->GetDefaultPlatform(), mi.implAddress))
f->SetUserType(functionNat.type);
auto name = "[" + ci.name + " " + mi.selector + "]";
defineSymbol(bv, mi.implAddress, name, "", FunctionSymbol);
}
void InfoHandler::applyInfoToView(SharedAnalysisInfo info, BinaryViewRef bv)
{
bv->BeginUndoActions();
BinaryReader reader(bv);
auto taggedPointerType = namedType(bv, CustomTypes::TaggedPointer);
auto categoryType = namedType(bv, CustomTypes::Category);
auto cfStringType = namedType(bv, CustomTypes::CFString);
auto classType = namedType(bv, CustomTypes::Class);
auto classDataType = namedType(bv, CustomTypes::ClassRO);
auto methodListType = namedType(bv, CustomTypes::MethodList);
// Create data variables and symbols for all CFString instances.
for (const auto& csi : info->cfStrings) {
reader.Seek(csi.dataAddress);
auto text = reader.ReadString(csi.size + 1);
auto sanitizedText = sanitizeText(text);
defineVariable(bv, csi.address, cfStringType);
defineVariable(bv, csi.dataAddress, stringType(csi.size));
defineSymbol(bv, csi.address, sanitizedText, "cf_");
defineSymbol(bv, csi.dataAddress, sanitizedText, "as_");
defineReference(bv, csi.address, csi.dataAddress);
}
// Create data variables and symbols for selectors and selector references.
for (const auto& sr : info->selectorRefs) {
auto sanitizedSelector = sanitizeSelector(sr->name);
defineVariable(bv, sr->address, taggedPointerType);
defineVariable(bv, sr->nameAddress, stringType(sr->name.size()));
defineSymbol(bv, sr->address, sanitizedSelector, "sr_");
defineSymbol(bv, sr->nameAddress, sanitizedSelector, "sl_");
defineReference(bv, sr->address, sr->nameAddress);
}
// Create data variables and symbols for the analyzed classes.
for (const auto& ci : info->classes) {
defineVariable(bv, ci.listPointer, taggedPointerType);
defineVariable(bv, ci.address, classType);
defineVariable(bv, ci.dataAddress, classDataType);
defineVariable(bv, ci.nameAddress, stringType(ci.name.size()));
defineSymbol(bv, ci.listPointer, ci.name, "cp_");
defineSymbol(bv, ci.address, ci.name, "cl_");
defineSymbol(bv, ci.dataAddress, ci.name, "ro_");
defineSymbol(bv, ci.nameAddress, ci.name, "nm_");
defineReference(bv, ci.listPointer, ci.address);
defineReference(bv, ci.address, ci.dataAddress);
defineReference(bv, ci.dataAddress, ci.nameAddress);
defineReference(bv, ci.dataAddress, ci.methodListAddress);
if (ci.methodList.address == 0 || ci.methodList.methods.empty())
continue;
auto methodType = ci.methodList.hasRelativeOffsets()
? bv->GetTypeByName(CustomTypes::MethodListEntry)
: bv->GetTypeByName(CustomTypes::Method);
// Create data variables for each method in the method list.
for (const auto& mi : ci.methodList.methods) {
defineVariable(bv, mi.address, methodType);
defineSymbol(bv, mi.address, sanitizeSelector(mi.selector), "mt_");
defineVariable(bv, mi.typeAddress, stringType(mi.type.size()));
defineReference(bv, ci.methodList.address, mi.address);
defineReference(bv, mi.address, mi.nameAddress);
defineReference(bv, mi.address, mi.typeAddress);
defineReference(bv, mi.address, mi.implAddress);
applyMethodType(bv, ci, mi);
}
// Create a data variable and symbol for the method list header.
defineVariable(bv, ci.methodListAddress, methodListType);
defineSymbol(bv, ci.methodListAddress, ci.name, "ml_");
}
// Create data variables and symbols for the analyzed Categories.
for (const auto& ci : info->categories) {
defineVariable(bv, ci.listPointer, taggedPointerType);
defineVariable(bv, ci.address, categoryType);
defineSymbol(bv, ci.listPointer, ci.name, "catp_");
defineSymbol(bv, ci.address, ci.name, "cat_");
defineReference(bv, ci.listPointer, ci.address);
if (ci.instanceMethods.address && !ci.instanceMethods.methods.empty()) {
auto methodType = ci.instanceMethods.hasRelativeOffsets()
? bv->GetTypeByName(CustomTypes::MethodListEntry)
: bv->GetTypeByName(CustomTypes::Method);
// Create data variables for each method in the method list.
for (const auto& mi : ci.instanceMethods.methods) {
defineVariable(bv, mi.address, methodType);
defineSymbol(bv, mi.address, sanitizeSelector(mi.selector), "mt_");
defineVariable(bv, mi.typeAddress, stringType(mi.type.size()));
defineReference(bv, ci.instanceMethods.address, mi.address);
defineReference(bv, mi.address, mi.nameAddress);
defineReference(bv, mi.address, mi.typeAddress);
defineReference(bv, mi.address, mi.implAddress);
}
// Create a data variable and symbol for the method list header.
defineVariable(bv, ci.instanceMethodListAddress, methodListType);
defineSymbol(bv, ci.instanceMethodListAddress, ci.name, "mli_");
}
if (ci.classMethods.address && !ci.classMethods.methods.empty()) {
auto methodType = ci.classMethods.hasRelativeOffsets()
? bv->GetTypeByName(CustomTypes::MethodListEntry)
: bv->GetTypeByName(CustomTypes::Method);
// Create data variables for each method in the method list.
for (const auto& mi : ci.classMethods.methods) {
defineVariable(bv, mi.address, methodType);
defineSymbol(bv, mi.address, sanitizeSelector(mi.selector), "mt_");
defineVariable(bv, mi.typeAddress, stringType(mi.type.size()));
defineReference(bv, ci.classMethods.address, mi.address);
defineReference(bv, mi.address, mi.nameAddress);
defineReference(bv, mi.address, mi.typeAddress);
defineReference(bv, mi.address, mi.implAddress);
}
// Create a data variable and symbol for the method list header.
defineVariable(bv, ci.classMethodListAddress, methodListType);
defineSymbol(bv, ci.classMethodListAddress, ci.name, "mlc_");
}
}
bv->CommitUndoActions();
bv->UpdateAnalysis();
}