-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionmatcher.cpp
More file actions
383 lines (330 loc) · 10.1 KB
/
functionmatcher.cpp
File metadata and controls
383 lines (330 loc) · 10.1 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <limits>
#include <queue>
#include "binaryninjaapi.h"
#define SQLITE_ENABLE_RTREE 1
#include "sqlite/sqlite3.h"
#include "sqlite/database.hpp"
using std::make_pair;
using std::map;
using std::pair;
using std::priority_queue;
using std::string;
using std::stringstream;
using std::vector;
namespace BN = BinaryNinja;
struct BasicBlockGraphProperties;
using BasicBlockGraph = boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::bidirectionalS,
BN::Ref<BN::BasicBlock>,
BN::BasicBlockEdge,
BasicBlockGraphProperties
>;
using BasicBlockVertexID = boost::graph_traits<BasicBlockGraph>::vertex_descriptor;
using BasicBlockEdgeID = boost::graph_traits<BasicBlockGraph>::edge_descriptor;
using BasicBlockVertexSize = boost::graph_traits<BasicBlockGraph>::vertices_size_type;
using BasicBlockDistanceMap = map<BasicBlockVertexID, BasicBlockVertexSize>;
struct BasicBlockGraphProperties {
map<uint64_t, BasicBlockVertexID> addressMap;
};
sqlite::database initDB(string filename);
void saveFunction(BN::BinaryView* bv, BN::Ref<BN::Function> func, sqlite::database& db);
vector<string> split(const string& s, char delim);
////////////////////////////////////////////////////////////////////////////////
static int64_t PRIMES[] = {
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97,
101,
103,
107,
109,
113,
127,
131,
137,
139,
149,
151,
157,
163,
173,
179,
181,
191,
193,
197,
199,
211,
223,
227,
229,
233,
239,
241,
251,
257,
263,
269,
271,
277,
281,
283,
293,
307,
311,
313,
317,
331,
337,
347,
};
////////////////////////////////////////////////////////////////////////////////
void saveDatabase(BN::BinaryView* bv)
{
BN::Ref<BN::Architecture> arch = bv->GetDefaultArchitecture();
string archName = arch->GetName();
string defaultName = split(bv->GetFile()->GetFilename(), '.').front() + ".bnsm";
string filename;
BinaryNinja::GetSaveFileNameInput(filename, "Save function database", "*.bnsm", defaultName);
sqlite::database db = initDB(filename);
for (auto&& func : bv->GetAnalysisFunctionList()) {
saveFunction(bv, func, db);
}
}
sqlite::database initDB(string filename) {
sqlite::database db(filename, sqlite::read_write_create);
db.execute("CREATE TABLE symbols("
"id INTEGER PRIMARY KEY ASC,"
"address BIGINT,"
"full_name TEXT,"
"short_name TEXT,"
"raw_name TEXT"
")");
db.execute("CREATE TABLE functions("
"id INTEGER PRIMARY KEY ASC,"
"address BIGINT,"
"length INTEGER,"
"spp BIGINT,"
"indegree INTEGER,"
"outdegree INTEGER,"
"is_recursive TINYINT,"
"basic_block_count INTEGER,"
"edge_count INTEGER"
")");
db.execute("CREATE TABLE basic_blocks("
"id INTEGER PRIMARY KEY ASC,"
"function_id INTEGER,"
"address BIGINT,"
"length INTEGER,"
"spp BIGINT,"
"indegree INTEGER,"
"outdegree INTEGER,"
"is_recursive TINYINT,"
"distance_to_entry INTEGER,"
"distance_to_exit INTEGER,"
"call_count INTEGER"
")");
db.execute("CREATE TABLE function_string_refs("
"function_id INTEGER,"
"string_hash INTEGER"
")");
db.execute("CREATE TABLE basic_block_string_refs("
"basic_block_id INTEGER,"
"string_hash INTEGER"
")");
return db;
}
//template<class It>
//boost::iterator_range<It> pair_range(std::pair<It, It> const& p) {
// return boost::make_iterator_range(p.first, p.second);
//}
void buildBasicBlockGraph(const BN::Ref<BN::Function> func, BasicBlockGraph& g) {
const auto blocks = func->GetBasicBlocks();
auto&& addressMap = g[boost::graph_bundle].addressMap;
// Create vertices from all of the basic blocks in this function
for (auto&& block : blocks) {
BasicBlockVertexID id = boost::add_vertex(g);
g[id] = block;
addressMap[block->GetStart()] = id;
}
// Create edges between the basic blocks
for (auto&& block : blocks) {
const auto edges = block->GetOutgoingEdges();
const BasicBlockVertexID a = addressMap.at(block->GetStart());
for (auto&& edge : edges) {
const auto bI = addressMap.find(edge.target);
if (bI != addressMap.end()) {
const BasicBlockVertexID b = bI->second;
const BasicBlockEdgeID id = boost::add_edge(a, b, g).first;
g[id] = edge;
}
}
}
}
void buildDistanceMap(const BasicBlockGraph& g, BasicBlockVertexID start, BasicBlockDistanceMap& distances) {
using namespace boost;
using ColorMap = map<BasicBlockVertexID, default_color_type>;
ColorMap colorMap;
boost::queue<BasicBlockVertexID> queue;
breadth_first_visit(g, start, queue,
make_bfs_visitor(
record_distances(
associative_property_map<BasicBlockDistanceMap>(distances), on_tree_edge()
)
),
associative_property_map<ColorMap>(colorMap)
);
}
void saveFunction(BN::BinaryView* bv, BN::Ref<BN::Function> func, sqlite::database& db) {
BN::Ref<BN::Architecture> arch = bv->GetDefaultArchitecture();
vector<BN::Ref<BN::BasicBlock>> basicBlocks = func->GetBasicBlocks();
BN::Ref<BN::LowLevelILFunction> llil = func->GetLowLevelIL();
size_t count = llil->GetInstructionCount();
BasicBlockGraph g;
buildBasicBlockGraph(func, g);
auto&& addressMap = g[boost::graph_bundle].addressMap;
int64_t address = static_cast<int64_t>(func->GetStart());
int32_t length = 0;
int64_t spp = 1;
size_t indegree = bv->GetCodeReferences(address).size();
size_t outdegree = 0;
bool isRecursive = false;
size_t basicBlockCount = basicBlocks.size();
size_t edgeCount = boost::num_edges(g);
for (auto&& block : basicBlocks) {
edgeCount += block->GetOutgoingEdges().size();
length += static_cast<int32_t>(block->GetLength());
}
for (size_t i = 0; i < count; i++) {
BNLowLevelILInstruction il = llil->operator[](i);
spp *= PRIMES[il.operation];
switch (il.operation) {
case LLIL_CALL:
++outdegree;
if (il.sourceOperand == address) { isRecursive = true; }
break;
case LLIL_SYSCALL:
++outdegree;
break;
default:
break;
}
}
sqlite::statement stmt(db.prepare_statement(
"INSERT INTO functions "
"(address, length, spp, indegree, outdegree, is_recursive, basic_block_count, edge_count) "
"VALUES (:address, :length, :spp, :indegree, :outdegree, :is_recursive, :basic_block_count, :edge_count)"
));
stmt.bind(":address", address);
stmt.bind(":length", length);
stmt.bind(":spp", spp);
stmt.bind(":indegree", indegree);
stmt.bind(":outdegree", outdegree);
stmt.bind(":is_recursive", isRecursive);
stmt.bind(":basic_block_count", basicBlockCount);
stmt.bind(":edge_count", edgeCount);
sqlite::result results(db.execute(stmt));
int64_t functionID = sqlite3_last_insert_rowid(db.getDB());
BasicBlockVertexID entryID = addressMap.at(func->GetStart());
BasicBlockDistanceMap entryDistances;
entryDistances[entryID] = 0;
buildDistanceMap(g, entryID, entryDistances);
for (auto&& block : basicBlocks) {
BasicBlockVertexID v = addressMap.at(block->GetStart());
address = static_cast<int64_t>(block->GetStart());
length = static_cast<int32_t>(block->GetLength());
outdegree = boost::out_degree(v, g);
indegree = boost::in_degree(v, g);
spp = 1;
isRecursive = false;
int32_t distanceToEntry = -1;
int32_t distanceToExit = -1; // TODO:
int callCount = 0;
auto distToEntry = entryDistances.find(v);
if (distToEntry != entryDistances.end()) {
distanceToEntry = static_cast<int32_t>(distToEntry->second);
}
for (size_t addr = block->GetStart(); addr < block->GetEnd(); addr++) {
size_t i = func->GetLowLevelILForInstruction(arch, addr);
size_t index = llil->GetIndexForInstruction(i);
if (index == std::numeric_limits<uint64_t>::max())
continue;
BNLowLevelILInstruction il = llil->operator[](index);
spp *= PRIMES[il.operation];
switch (il.operation) {
case LLIL_CALL:
case LLIL_SYSCALL:
++callCount;
break;
case LLIL_JUMP:
if (il.sourceOperand >= block->GetStart() && il.sourceOperand <= block->GetEnd())
isRecursive = true;
break;
default:
break;
}
}
sqlite::statement stmt(db.prepare_statement(
"INSERT INTO basic_blocks "
"(function_id, address, length, spp, indegree, outdegree, is_recursive, distance_to_entry, distance_to_exit, call_count) "
"VALUES (:function_id, :address, :length, :spp, :indegree, :outdegree, :is_recursive, :distance_to_entry, :distance_to_exit, :call_count)"
));
stmt.bind(":function_id", functionID);
stmt.bind(":address", address);
stmt.bind(":length", length);
stmt.bind(":spp", spp);
stmt.bind(":indegree", indegree);
stmt.bind(":outdegree", outdegree);
stmt.bind(":is_recursive", isRecursive);
stmt.bind(":distance_to_entry", distanceToEntry);
stmt.bind(":distance_to_exit", distanceToExit);
stmt.bind(":call_count", callCount);
sqlite::result results(db.execute(stmt));
}
}
vector<string> split(const string& s, char delim) {
vector<string> elems;
stringstream ss;
ss.str(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
extern "C"
{
BINARYNINJAPLUGIN bool CorePluginInit()
{
// Register the plugin with Binary Ninja
BN::PluginCommand::Register("Save Function Database",
"Create a database holding function metadata and symbols.",
&saveDatabase);
return true;
}
}