-
Notifications
You must be signed in to change notification settings - Fork 213
/
firmwareninja.cpp
292 lines (235 loc) · 8.28 KB
/
firmwareninja.cpp
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
// Copyright (c) 2015-2024 Vector 35 Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#include "binaryninjaapi.h"
#include "binaryninjacore.h"
using namespace BinaryNinja;
static BNFirmwareNinjaFunctionMemoryAccesses** MemoryInfoVectorToArray(
const std::vector<FirmwareNinjaFunctionMemoryAccesses>& fma)
{
BNFirmwareNinjaFunctionMemoryAccesses** result = new BNFirmwareNinjaFunctionMemoryAccesses*[fma.size()];
for (size_t i = 0; i < fma.size(); i++)
{
result[i] = new BNFirmwareNinjaFunctionMemoryAccesses;
result[i]->start = fma[i].start;
result[i]->count = fma[i].count;
result[i]->accesses = new BNFirmwareNinjaMemoryAccess*[fma[i].count];
for (size_t j = 0; j < fma[i].count; j++)
{
result[i]->accesses[j] = new BNFirmwareNinjaMemoryAccess;
std::memcpy(result[i]->accesses[j], &fma[i].accesses[j], sizeof(BNFirmwareNinjaMemoryAccess));
}
}
return result;
}
static void FreeMemoryInfoArray(BNFirmwareNinjaFunctionMemoryAccesses** fma, size_t count)
{
for (size_t i = 0; i < count; i++)
{
for (size_t j = 0; j < fma[i]->count; j++)
delete fma[i]->accesses[j];
delete[] fma[i]->accesses;
delete fma[i];
}
}
FirmwareNinja::FirmwareNinja(Ref<BinaryView> view)
{
m_view = view;
m_object = BNCreateFirmwareNinja(view->GetObject());
}
FirmwareNinja::~FirmwareNinja()
{
BNFreeFirmwareNinja(m_object);
}
bool FirmwareNinja::StoreCustomDevice(FirmwareNinjaDevice& device)
{
return BNFirmwareNinjaStoreCustomDevice(m_object, device.name.c_str(),
device.start, device.end, device.info.c_str());
}
bool FirmwareNinja::RemoveCustomDevice(const std::string& name)
{
return BNFirmwareNinjaRemoveCustomDevice(m_object, name.c_str());
}
std::vector<FirmwareNinjaDevice> FirmwareNinja::QueryCustomDevices()
{
std::vector<FirmwareNinjaDevice> result;
BNFirmwareNinjaDevice* devices;
int count = BNFirmwareNinjaQueryCustomDevices(m_object, &devices);
if (count <= 0)
return result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
result.push_back({
devices[i].name,
devices[i].start,
devices[i].end,
devices[i].info
});
BNFirmwareNinjaFreeDevices(devices, count);
return result;
}
std::vector<std::string> FirmwareNinja::QueryBoardNames()
{
std::vector<std::string> result;
char** boards;
auto platform = m_view->GetDefaultPlatform();
if (!platform)
return result;
auto arch = platform->GetArchitecture();
if (!arch)
return result;
int count = BNFirmwareNinjaQueryBoardNamesForArchitecture(m_object, arch->GetObject(), &boards);
if (count <= 0)
return result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
result.push_back(boards[i]);
BNFirmwareNinjaFreeBoardNames(boards, count);
sort(result.begin(), result.end());
return result;
}
std::vector<FirmwareNinjaDevice> FirmwareNinja::QueryDevicesForBoard(const std::string& board)
{
std::vector<FirmwareNinjaDevice> result;
BNFirmwareNinjaDevice* devices;
auto platform = m_view->GetDefaultPlatform();
if (!platform)
return result;
auto arch = platform->GetArchitecture();
if (!arch)
return result;
int count = BNFirmwareNinjaQueryBoardDevices(m_object, arch->GetObject(), board.c_str(), &devices);
if (count <= 0)
return result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
result.push_back({
devices[i].name,
devices[i].start,
devices[i].end,
devices[i].info
});
BNFirmwareNinjaFreeDevices(devices, count);
return result;
}
std::vector<BNFirmwareNinjaSection> FirmwareNinja::FindSections(float highCodeEntropyThreshold,
float lowCodeEntropyThreshold, size_t blockSize, BNFirmwareNinjaSectionAnalysisMode mode)
{
std::vector<BNFirmwareNinjaSection> result;
BNFirmwareNinjaSection* sections;
int count = BNFirmwareNinjaFindSectionsWithEntropy(m_object, §ions, highCodeEntropyThreshold,
lowCodeEntropyThreshold, blockSize, mode);
if (count <= 0)
return result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
result.push_back(sections[i]);
BNFirmwareNinjaFreeSections(sections, count);
return result;
}
std::vector<FirmwareNinjaFunctionMemoryAccesses> FirmwareNinja::GetFunctionMemoryAccesses(BNProgressFunction progress,
void* progressContext)
{
std::vector<FirmwareNinjaFunctionMemoryAccesses> result;
BNFirmwareNinjaFunctionMemoryAccesses** fma;
int count = BNFirmwareNinjaGetFunctionMemoryAccesses(m_object, &fma, progress, progressContext);
if (count <= 0)
return result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
{
FirmwareNinjaFunctionMemoryAccesses info;
info.start = fma[i]->start;
info.count = fma[i]->count;
for (size_t j = 0; j < info.count; j++)
{
BNFirmwareNinjaMemoryAccess access;
std::memcpy(&access, fma[i]->accesses[j], sizeof(BNFirmwareNinjaMemoryAccess));
info.accesses.push_back(access);
}
result.push_back(info);
}
BNFirmwareNinjaFreeFunctionMemoryAccesses(fma, count);
std::sort(result.begin(), result.end(), [](const FirmwareNinjaFunctionMemoryAccesses& a,
const FirmwareNinjaFunctionMemoryAccesses& b) {
return a.count > b.count;
});
return result;
}
void FirmwareNinja::StoreFunctionMemoryAccesses(const std::vector<FirmwareNinjaFunctionMemoryAccesses>& fma)
{
BNFirmwareNinjaFunctionMemoryAccesses** fmaArray = MemoryInfoVectorToArray(fma);
BNFirmwareNinjaStoreFunctionMemoryAccessesToMetadata(m_object, fmaArray, fma.size());
FreeMemoryInfoArray(fmaArray, fma.size());
}
std::vector<FirmwareNinjaFunctionMemoryAccesses> FirmwareNinja::QueryFunctionMemoryAccesses()
{
std::vector<FirmwareNinjaFunctionMemoryAccesses> result;
BNFirmwareNinjaFunctionMemoryAccesses** fma;
int count = BNFirmwareNinjaQueryFunctionMemoryAccessesFromMetadata(m_object, &fma);
if (count <= 0)
return result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
{
FirmwareNinjaFunctionMemoryAccesses info;
info.start = fma[i]->start;
info.count = fma[i]->count;
for (size_t j = 0; j < info.count; j++)
{
BNFirmwareNinjaMemoryAccess access;
std::memcpy(&access, fma[i]->accesses[j], sizeof(BNFirmwareNinjaMemoryAccess));
info.accesses.push_back(access);
}
result.push_back(info);
}
BNFirmwareNinjaFreeFunctionMemoryAccesses(fma, count);
std::sort(result.begin(), result.end(), [](const FirmwareNinjaFunctionMemoryAccesses& a,
const FirmwareNinjaFunctionMemoryAccesses& b) {
return a.count > b.count;
});
return result;
}
std::vector<FirmwareNinjaDeviceAccesses> FirmwareNinja::GetBoardDeviceAccesses(
const std::vector<FirmwareNinjaFunctionMemoryAccesses>& fma)
{
std::vector<FirmwareNinjaDeviceAccesses> result;
auto platform = m_view->GetDefaultPlatform();
if (!platform)
return result;
auto arch = platform->GetArchitecture();
if (!arch)
return result;
BNFirmwareNinjaFunctionMemoryAccesses** fmaArray = MemoryInfoVectorToArray(fma);
BNFirmwareNinjaDeviceAccesses* accesses;
int count = BNFirmwareNinjaGetBoardDeviceAccesses(m_object, fmaArray, fma.size(), &accesses, arch->GetObject());
if (count <= 0)
{
FreeMemoryInfoArray(fmaArray, fma.size());
return result;
}
result.reserve(count);
for (size_t i = 0; i < count; i++)
result.push_back({accesses[i].name, accesses[i].total, accesses[i].unique});
BNFirmwareNinjaFreeBoardDeviceAccesses(accesses, count);
sort(result.begin(), result.end(), [](const FirmwareNinjaDeviceAccesses& a, const FirmwareNinjaDeviceAccesses& b) {
return a.total > b.total;
});
return result;
}