-
Notifications
You must be signed in to change notification settings - Fork 6
/
executable.cpp
429 lines (354 loc) · 13.1 KB
/
executable.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
* @file
*
* @brief Class encapsulating the executable being dissassembled.
*
* @copyright Assemblize is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version
* 3 of the License, or (at your option) any later version.
* A full copy of the GNU General Public License can be found in
* LICENSE
*/
#include "executable.h"
#include "function.h"
#include <LIEF/LIEF.hpp>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include <strings.h>
const char unassemblize::Executable::s_symbolSection[] = "symbols";
const char unassemblize::Executable::s_sectionsSection[] = "sections";
const char unassemblize::Executable::s_configSection[] = "config";
const char unassemblize::Executable::s_objectSection[] = "objects";
unassemblize::Executable::Executable(const char *file_name, OutputFormats format, bool verbose) :
m_binary(LIEF::Parser::parse(file_name)),
m_endAddress(0),
m_outputFormat(format),
m_codeAlignment(sizeof(uint32_t)),
m_dataAlignment(sizeof(uint32_t)),
m_codePad(0x90), // NOP
m_dataPad(0x00),
m_verbose(verbose),
m_addBase(false)
{
if (m_verbose) {
printf("Loading section info...\n");
}
bool checked_image_base = false;
for (auto it = m_binary->sections().begin(); it != m_binary->sections().end(); ++it) {
if (!it->name().empty() && it->size() != 0) {
SectionInfo §ion = m_sections[it->name()];
section.data = it->content().data();
// Check on first section incase binary is huge and later sections start higher than imagebase.
if (!checked_image_base && it->virtual_address() <= m_binary->imagebase()) {
m_addBase = true;
}
// For PE format virtual_address appears to be an offset, in ELF/Mach-O it appears to be absolute.
if (m_addBase) {
section.address = m_binary->imagebase() + it->virtual_address();
} else {
section.address = it->virtual_address();
}
section.size = it->size();
if (section.address + section.size > m_endAddress) {
m_endAddress = section.address + section.size;
}
// Naive split on whether section contains data or code... have entrypoint? Code, else data.
// Needs to be refined by providing a config file with section types specified.
if (section.address <= m_binary->entrypoint() && section.address + section.size >= m_binary->entrypoint()) {
section.type = SECTION_CODE;
} else {
section.type = SECTION_DATA;
}
}
}
if (m_verbose) {
printf("Indexing embedded symbols...\n");
}
auto exe_syms = m_binary->symbols();
for (auto it = exe_syms.begin(); it != exe_syms.end(); ++it) {
if (it->value() != 0 && !it->name().empty() && m_symbolMap.find(it->value()) == m_symbolMap.end()) {
uint64_t value = it->value() > m_binary->imagebase() ? it->value() : it->value() + m_binary->imagebase();
m_symbolMap.insert({it->value(), Symbol(it->name(), value, it->size())});
}
}
auto exe_imports = m_binary->imported_functions();
for (auto it = exe_imports.begin(); it != exe_imports.end(); ++it) {
if (it->value() != 0 && !it->name().empty() && m_symbolMap.find(it->value()) == m_symbolMap.end()) {
uint64_t value = it->value() > m_binary->imagebase() ? it->value() : it->value() + m_binary->imagebase();
m_loadedSymbols.push_back(it->name());
m_symbolMap.insert({it->value(), Symbol(m_loadedSymbols.back(), value, it->size())});
}
}
}
const uint8_t *unassemblize::Executable::section_data(const char *name) const
{
auto it = m_sections.find(name);
return it != m_sections.end() ? it->second.data : nullptr;
}
uint64_t unassemblize::Executable::section_address(const char *name) const
{
auto it = m_sections.find(name);
return it != m_sections.end() ? it->second.address : UINT64_MAX;
}
uint64_t unassemblize::Executable::section_size(const char *name) const
{
auto it = m_sections.find(name);
return it != m_sections.end() ? it->second.size : 0;
}
uint64_t unassemblize::Executable::base_address() const
{
return m_binary->imagebase();
}
const unassemblize::Executable::Symbol &unassemblize::Executable::get_symbol(uint64_t addr) const
{
static std::string empty;
static Symbol def(empty, 0, 0);
auto it = m_symbolMap.find(addr);
if (it != m_symbolMap.end()) {
return it->second;
}
return def;
}
const unassemblize::Executable::Symbol &unassemblize::Executable::get_nearest_symbol(uint64_t addr) const
{
static std::string empty;
static Symbol def(empty, 0, 0);
auto it = m_symbolMap.lower_bound(addr);
if (it != m_symbolMap.end()) {
if (it->second.value == addr) {
return it->second;
} else {
return std::prev(it)->second;
}
}
return def;
}
void unassemblize::Executable::add_symbol(const char *sym, uint64_t addr)
{
if (m_symbolMap.find(addr) == m_symbolMap.end()) {
m_loadedSymbols.push_back(sym);
m_symbolMap.insert({addr, Symbol(m_loadedSymbols.back(), addr, 0)});
}
}
void unassemblize::Executable::load_config(const char *file_name)
{
if (m_verbose) {
printf("Loading config file '%s'...\n", file_name);
}
std::ifstream fs(file_name);
if (!fs.good()) {
return;
}
nlohmann::json j = nlohmann::json::parse(fs);
if (j.find(s_configSection) != j.end()) {
nlohmann::json &conf = j.at(s_configSection);
conf.at("codealign").get_to(m_codeAlignment);
conf.at("dataalign").get_to(m_dataAlignment);
conf.at("codepadding").get_to(m_codePad);
conf.at("datapadding").get_to(m_dataPad);
}
if (j.find(s_symbolSection) != j.end()) {
load_symbols(j.at(s_symbolSection));
}
if (j.find(s_sectionsSection) != j.end()) {
load_sections(j.at(s_sectionsSection));
}
if (j.find(s_objectSection) != j.end()) {
load_objects(j.at(s_objectSection));
}
}
void unassemblize::Executable::save_config(const char *file_name)
{
if (m_verbose) {
printf("Saving config file '%s'...\n", file_name);
}
nlohmann::json j;
// Parse the config file if it already exists and update it.
{
std::ifstream fs(file_name);
if (fs.good()) {
j = nlohmann::json::parse(fs);
}
}
if (j.find(s_configSection) == j.end()) {
j[s_configSection] = nlohmann::json();
}
nlohmann::json &conf = j.at(s_configSection);
conf["codealign"] = m_codeAlignment;
conf["dataalign"] = m_dataAlignment;
conf["codepadding"] = m_codePad;
conf["datapadding"] = m_dataPad;
// Don't dump if we already have a sections for these.
if (j.find(s_symbolSection) == j.end()) {
j[s_symbolSection] = nlohmann::json();
dump_symbols(j.at(s_symbolSection));
}
if (j.find(s_sectionsSection) == j.end()) {
j[s_sectionsSection] = nlohmann::json();
dump_sections(j.at(s_sectionsSection));
}
if (j.find(s_objectSection) == j.end()) {
j[s_objectSection] = nlohmann::json();
dump_objects(j.at(s_objectSection));
}
std::ofstream fs(file_name);
fs << std::setw(4) << j << std::endl;
}
void unassemblize::Executable::load_symbols(nlohmann::json &js)
{
if (m_verbose) {
printf("Loading external symbols...\n");
}
for (auto it = js.begin(); it != js.end(); ++it) {
std::string name;
it->at("name").get_to(name);
// Don't try and load an empty symbol.
if (!name.empty()) {
uint64_t size = 0;
uint64_t addr = 0;
it->at("address").get_to(addr);
if (addr == 0) {
continue;
}
it->at("size").get_to(size);
// Only load symbols for addresses we don't have any symbol for yet.
if (m_symbolMap.find(addr) == m_symbolMap.end()) {
m_loadedSymbols.push_back(name);
m_symbolMap.insert({addr, {m_loadedSymbols.back(), addr, size}});
}
}
}
}
void unassemblize::Executable::dump_symbols(nlohmann::json &js)
{
if (m_verbose) {
printf("Saving symbols...\n");
}
for (auto it = m_symbolMap.begin(); it != m_symbolMap.end(); ++it) {
js.push_back({{"name", it->second.name}, {"address", it->second.value}, {"size", it->second.size}});
}
}
void unassemblize::Executable::load_sections(nlohmann::json &js)
{
if (m_verbose) {
printf("Loading section info...\n");
}
for (auto it = js.begin(); it != js.end(); ++it) {
std::string name;
it->at("name").get_to(name);
// Don't try and load an empty symbol.
if (!name.empty()) {
auto section = m_sections.find(name);
if (section == m_sections.end() && m_verbose) {
printf("Tried to load section info for section not present in this binary!\n");
printf("Section '%s' info was ignored.\n", name.c_str());
}
std::string type;
it->at("type").get_to(type);
if (strcasecmp(type.c_str(), "code") == 0) {
section->second.type = SECTION_CODE;
} else if (strcasecmp(type.c_str(), "data") == 0) {
section->second.type = SECTION_DATA;
} else if (m_verbose) {
printf("Incorrect type specified for section '%s'.\n", name.c_str());
}
}
}
}
void unassemblize::Executable::dump_sections(nlohmann::json &js)
{
if (m_verbose) {
printf("Saving section info...\n");
}
for (auto it = m_sections.begin(); it != m_sections.end(); ++it) {
js.push_back({{"name", it->first}, {"type", it->second.type == SECTION_CODE ? "code" : "data"}});
}
}
void unassemblize::Executable::load_objects(nlohmann::json &js)
{
if (m_verbose) {
printf("Loading objects...\n");
}
for (auto it = js.begin(); it != js.end(); ++it) {
std::string obj_name;
it->at("name").get_to(obj_name);
if (obj_name.empty()) {
continue;
}
m_targetObjects.push_back({obj_name, std::list<ObjectSection>()});
auto &obj = m_targetObjects.back();
auto §ions = js.back().at("sections");
for (auto sec = sections.begin(); sec != sections.end(); ++sec) {
std::string name;
uint64_t start;
uint64_t size;
sec->at("name").get_to(name);
sec->at("start").get_to(start);
sec->at("size").get_to(size);
obj.sections.push_back({name, start, size});
}
}
}
void unassemblize::Executable::dump_objects(nlohmann::json &js)
{
if (m_verbose) {
printf("Saving objects...\n");
}
if (m_targetObjects.empty()) {
m_targetObjects.push_back(
{m_binary->name().substr(m_binary->name().find_last_of("/\\") + 1), std::list<ObjectSection>()});
auto &obj = m_targetObjects.back();
for (auto it = m_binary->sections().begin(); it != m_binary->sections().end(); ++it) {
if (it->name().empty() || it->size() == 0) {
continue;
}
obj.sections.push_back({it->name(), 0, it->size()});
}
}
for (auto it = m_targetObjects.begin(); it != m_targetObjects.end(); ++it) {
js.push_back({{"name", it->name}, {"sections", nlohmann::json()}});
auto §ions = js.back().at("sections");
for (auto it2 = it->sections.begin(); it2 != it->sections.end(); ++it2) {
sections.push_back({{"name", it2->name}, {"start", it2->start}, {"size", it2->size}});
}
}
}
void unassemblize::Executable::dissassemble_function(
FILE *output, const char *section_name, uint64_t start, uint64_t end)
{
// Abort if we can't output anywhere.
if (output == nullptr) {
return;
}
if (m_outputFormat != OUTPUT_MASM) {
dissassemble_gas_func(output, section_name, start, end);
}
}
void unassemblize::Executable::dissassemble_gas_func(
FILE *output, const char *section_name, uint64_t start, uint64_t end)
{
if (start != 0 && end != 0) {
unassemblize::Function func(*this, section_name, start, end);
if (m_outputFormat == OUTPUT_IGAS) {
func.disassemble(Function::FORMAT_IGAS);
} else {
func.disassemble(Function::FORMAT_AGAS);
}
const std::string &sym = get_symbol(start).name;
if (!sym.empty()) {
fprintf(output,
".globl %s\n%s:\n%s",
sym.c_str(),
sym.c_str(),
func.dissassembly().c_str());
} else {
fprintf(output,
".globl sub_%" PRIx64 "\nsub_%" PRIx64 ":\n%s",
start,
start,
func.dissassembly().c_str());
}
}
}