-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathIaitoApplication.cpp
548 lines (481 loc) · 17.9 KB
/
IaitoApplication.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include "IaitoApplication.h"
#include "IaitoConfig.h"
#include "R2AnotesDecompiler.h"
#include "R2DecaiDecompiler.h"
#include "R2GhidraCmdDecompiler.h"
#include "R2pdcCmdDecompiler.h"
#include "R2retdecDecompiler.h"
#include "common/CrashHandler.h"
#include "common/Decompiler.h"
#include "common/PythonManager.h"
#include "common/ResourcePaths.h"
#include "plugins/PluginManager.h"
#include <QApplication>
#include <QCommandLineParser>
#include <QEvent>
#include <QFileOpenEvent>
#include <QMenu>
#include <QMessageBox>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QTextCodec>
#endif
#include <QDir>
#include <QFontDatabase>
#include <QLibraryInfo>
#include <QPluginLoader>
#include <QProcess>
#include <QStringList>
#include <QTranslator>
#ifdef Q_OS_WIN
#include <QtNetwork/QtNetwork>
#endif // Q_OS_WIN
#include <cstdlib>
#if IAITO_R2GHIDRA_STATIC
#include <R2GhidraDecompiler.h>
#endif
static bool versionCheck()
{
// Check r2 version
QString a = r_core_version(); // runtime library version
QString b = "" R2_GITTAP; // compiled version
QStringList la = a.split(".");
QStringList lb = b.split(".");
if (la.size() < 2 && lb.size() < 2) {
R_LOG_WARN("Invalid version string somwhere");
return false;
}
if (la.at(0) != lb.at(0)) {
R_LOG_WARN("Major version differs");
return false;
}
if (la.at(1) != lb.at(1)) {
R_LOG_WARN("Minor version differs");
return false;
}
return true;
}
IaitoApplication::IaitoApplication(int &argc, char **argv)
: QApplication(argc, argv)
{
// Setup application information
setApplicationVersion(IAITO_VERSION_FULL);
#ifndef Q_OS_MACX
setWindowIcon(QIcon(":/img/iaito.svg"));
#endif
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
setLayoutDirection(Qt::LeftToRight);
// WARN!!! Put initialization code below this line. Code above this line is
// mandatory to be run First
#ifdef Q_OS_WIN
// Hack to force Iaito load internet connection related DLL's
QSslSocket s;
s.sslConfiguration();
#endif // Q_OS_WIN
// Load translations
if (!loadTranslations()) {
qWarning() << "Cannot load translations";
}
// Load fonts
int ret = QFontDatabase::addApplicationFont(":/fonts/AgaveRegular.ttf");
if (ret == -1) {
qWarning() << "Cannot load Agave Regular font.";
}
ret = QFontDatabase::addApplicationFont(":/fonts/AnonymousPro.ttf");
if (ret == -1) {
qWarning() << "Cannot load Anonymous Pro font.";
}
ret = QFontDatabase::addApplicationFont(":/fonts/InconsolataRegular.ttf");
if (ret == -1) {
qWarning() << "Cannot load Incosolata-Regular font.";
}
// Set QString codec to UTF-8
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
#endif
#endif
if (!parseCommandLineOptions()) {
QCoreApplication::exit();
// std::exit(1);
}
if (!versionCheck()) {
QMessageBox msg;
msg.setIcon(QMessageBox::Critical);
msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msg.setWindowTitle(QObject::tr("Version mismatch!"));
QString localVersion = r_core_version();
QString r2version = R2_GITTAP;
msg.setText(QString(QObject::tr("The version used to compile Iaito (%1) does not match the "
"binary version of radare2 (%2). This could result in "
"unexpected behaviour. Are you sure you want to continue?"))
.arg(localVersion, r2version));
if (msg.exec() == QMessageBox::No) {
QCoreApplication::exit();
// std::exit(1);
}
}
#ifdef IAITO_ENABLE_PYTHON
// Init python
if (!clOptions.pythonHome.isEmpty()) {
Python()->setPythonHome(clOptions.pythonHome);
}
Python()->initialize();
#endif
#ifdef Q_OS_WIN
// Redefine r_sys_prefix() behaviour
qputenv("R_ALT_SRC_DIR", "1");
#endif
#ifdef MACOS_R2_BUNDLED
{
auto appdir = QDir(QCoreApplication::applicationDirPath()); // Contents/MacOS
appdir.cdUp(); // Contents
auto r2prefix = appdir; // Contents
r2prefix.cd("Resources/radare2"); // Contents/Resources/radare2
qputenv("R2_PREFIX", r2prefix.absolutePath().toLocal8Bit());
auto r2bin = appdir; // Contents
r2bin.cd("Helpers"); // Contents/Helpers
auto paths = QStringList(QString::fromLocal8Bit(qgetenv("PATH")));
paths.prepend(r2bin.absolutePath());
qputenv("PATH", paths.join(QLatin1Char(':')).toLocal8Bit());
// auto sleighHome = appdir; // Contents
// sleighHome.cd("PlugIns/radare2/r2ghidra_sleigh"); // Contents/PlugIns/radare2/r2ghidra_sleigh
// qputenv("SLEIGHHOME", sleighHome.absolutePath().toLocal8Bit());
}
#endif
Core()->initialize(clOptions.enableR2Plugins);
Core()->setSettings();
Config()->loadInitial();
Core()->loadIaitoRC(0);
Config()->setOutputRedirectionEnabled(clOptions.outputRedirectionEnabled);
if (R2pdcCmdDecompiler::isAvailable()) {
Core()->registerDecompiler(new R2pdcCmdDecompiler(Core()));
}
if (R2retdecDecompiler::isAvailable()) {
Core()->registerDecompiler(new R2retdecDecompiler(Core()));
}
if (R2DecDecompiler::isAvailable()) {
Core()->registerDecompiler(new R2DecDecompiler(Core()));
}
if (R2DecaiDecompiler::isAvailable()) {
Core()->registerDecompiler(new R2DecaiDecompiler(Core()));
}
if (R2GhidraCmdDecompiler::isAvailable()) {
Core()->registerDecompiler(new R2GhidraCmdDecompiler(Core()));
}
if (R2AnotesDecompiler::isAvailable()) {
Core()->registerDecompiler(new R2AnotesDecompiler(Core()));
}
#if IAITO_R2GHIDRA_STATIC
Core()->registerDecompiler(new R2GhidraDecompiler(Core()));
#endif
Plugins()->loadPlugins(clOptions.enableIaitoPlugins);
for (auto &plugin : Plugins()->getPlugins()) {
plugin->registerDecompilers();
}
mainWindow = new MainWindow();
installEventFilter(mainWindow);
// set up context menu shortcut display fix
#if QT_VERSION_CHECK(5, 10, 0) < QT_VERSION
setStyle(new IaitoProxyStyle());
#endif // QT_VERSION_CHECK(5, 10, 0) < QT_VERSION
RCore *kore = iaitoPluginCore();
if (kore) {
mainWindow->openCurrentCore(clOptions.fileOpenOptions, false);
} else if (clOptions.args.empty()) {
// check if this is the first execution of Iaito in this computer
// Note: the execution after the preferences been reset, will be
// considered as first-execution
if (Config()->isFirstExecution()) {
// TODO: add cmdline flag to show the welcome dialog
mainWindow->displayWelcomeDialog();
}
mainWindow->displayNewFileDialog();
} else { // filename specified as positional argument
bool askOptions = clOptions.analLevel != AutomaticAnalysisLevel::Ask;
mainWindow->openNewFile(clOptions.fileOpenOptions, askOptions);
}
#if 0
#ifdef APPIMAGE
{
auto appdir = QDir(QCoreApplication::applicationDirPath()); // appdir/bin
appdir.cdUp(); // appdir
auto sleighHome = appdir;
sleighHome.cd("share/radare2/plugins/r2ghidra_sleigh"); // appdir/share/radare2/plugins/r2ghidra_sleigh
Core()->setConfig("r2ghidra.sleighhome", sleighHome.absolutePath());
auto r2decHome = appdir;
r2decHome.cd("share/radare2/plugins/r2dec-js"); // appdir/share/radare2/plugins/r2dec-js
qputenv("R2DEC_HOME", r2decHome.absolutePath().toLocal8Bit());
}
#endif
#ifdef IAITO_APPVEYOR_R2DEC
qputenv("R2DEC_HOME", "lib\\plugins\\r2dec-js");
#endif
#ifdef Q_OS_WIN
{
auto sleighHome = QDir(QCoreApplication::applicationDirPath());
sleighHome.cd("lib/plugins/r2ghidra_sleigh");
Core()->setConfig("r2ghidra.sleighhome", sleighHome.absolutePath());
}
#endif
#endif
}
IaitoApplication::~IaitoApplication()
{
Plugins()->destroyPlugins();
delete mainWindow;
#ifdef IAITO_ENABLE_PYTHON
Python()->shutdown();
#endif
}
void IaitoApplication::launchNewInstance(const QStringList &args)
{
QProcess process(this);
process.setEnvironment(QProcess::systemEnvironment());
QStringList allArgs;
if (!clOptions.enableIaitoPlugins) {
allArgs.push_back("--no-iaito-plugins");
}
if (!clOptions.enableR2Plugins) {
allArgs.push_back("--no-r2-plugins");
}
allArgs.append(args);
process.startDetached(qApp->applicationFilePath(), allArgs);
}
bool IaitoApplication::event(QEvent *e)
{
if (e->type() == QEvent::FileOpen) {
RCore *kore = iaitoPluginCore();
if (kore) {
return false;
}
QFileOpenEvent *openEvent = static_cast<QFileOpenEvent *>(e);
if (openEvent) {
if (m_FileAlreadyDropped) {
// We already dropped a file in macOS, let's spawn another
// instance (Like the File -> Open)
QString fileName = openEvent->file();
launchNewInstance({fileName});
} else {
QString fileName = openEvent->file();
// eprintf ("FILE %s\n", fileName.toStdString().c_str());
if (fileName == "") {
return false;
}
m_FileAlreadyDropped = true;
mainWindow->closeNewFileDialog();
InitialOptions options;
options.filename = fileName;
mainWindow->openNewFile(options);
}
}
}
return QApplication::event(e);
}
bool IaitoApplication::loadTranslations()
{
const QString &language = Config()->getCurrLocale().bcp47Name();
if (language == QStringLiteral("en") || language.startsWith(QStringLiteral("en-"))) {
return true;
}
const auto &allLocales
= QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry);
bool iaitoTrLoaded = false;
for (const QLocale &it : allLocales) {
const QString &langPrefix = it.bcp47Name();
if (langPrefix == language) {
QApplication::setLayoutDirection(it.textDirection());
QLocale::setDefault(it);
QTranslator *trIaito = new QTranslator;
QTranslator *trQtBase = new QTranslator;
QTranslator *trQt = new QTranslator;
const QStringList &cutterTrPaths = Iaito::getTranslationsDirectories();
for (const auto &trPath : cutterTrPaths) {
if (trIaito
&& trIaito->load(it, QLatin1String("iaito"), QLatin1String("_"), trPath)) {
installTranslator(trIaito);
iaitoTrLoaded = true;
trIaito = nullptr;
}
if (trQt && trQt->load(it, "qt", "_", trPath)) {
installTranslator(trQt);
trQt = nullptr;
}
if (trQtBase && trQtBase->load(it, "qtbase", "_", trPath)) {
installTranslator(trQtBase);
trQtBase = nullptr;
}
}
if (trIaito) {
delete trIaito;
}
if (trQt) {
delete trQt;
}
if (trQtBase) {
delete trQtBase;
}
return true;
}
}
if (!iaitoTrLoaded) {
qWarning() << "Cannot load Iaito's translation for " << language;
}
return false;
}
bool IaitoApplication::parseCommandLineOptions()
{
// Keep this function in sync with documentation
QCommandLineParser cmd_parser;
cmd_parser.setApplicationDescription(
QObject::tr("A Qt and C++ GUI for radare2 reverse engineering framework"));
cmd_parser.addHelpOption();
cmd_parser.addVersionOption();
cmd_parser.addPositionalArgument("filename", QObject::tr("Filename to open."));
QCommandLineOption analOption(
{"A", "analysis"},
QObject::tr("Automatically open file and optionally start analysis. "
"Needs filename to be specified. May be a value between 0 and 3:"
" 0 = no analysis, 1 = aa, 2 = aaa, 3 = aaaa (slow)"),
QObject::tr("level"));
cmd_parser.addOption(analOption);
QCommandLineOption formatOption(
{"F", "format"},
QObject::tr("Force using a specific file format (bin plugin)"),
QObject::tr("name"));
cmd_parser.addOption(formatOption);
QCommandLineOption baddrOption(
{"B", "base"},
QObject::tr("Load binary at a specific base address"),
QObject::tr("base address"));
cmd_parser.addOption(baddrOption);
QCommandLineOption scriptOption("i", QObject::tr("Run script file"), QObject::tr("file"));
cmd_parser.addOption(scriptOption);
QCommandLineOption writeModeOption({"w", "writemode"}, QObject::tr("Open file in write mode"));
cmd_parser.addOption(writeModeOption);
QCommandLineOption
binCacheOption({"c", "bincache"}, QObject::tr("Patch relocs (enable bin.cache)"));
cmd_parser.addOption(binCacheOption);
QCommandLineOption pythonHomeOption(
"pythonhome",
QObject::tr("PYTHONHOME to use for embedded python interpreter"),
"PYTHONHOME");
cmd_parser.addOption(pythonHomeOption);
QCommandLineOption disableRedirectOption(
"no-output-redirect",
QObject::tr("Disable output redirection."
" Some of the output in console widget will not be visible."
" Use this option when debuging a crash or freeze and output "
" redirection is causing some messages to be lost."));
cmd_parser.addOption(disableRedirectOption);
QCommandLineOption disablePlugins("no-plugins", QObject::tr("Do not load plugins"));
cmd_parser.addOption(disablePlugins);
QCommandLineOption
disableIaitoPlugins("no-iaito-plugins", QObject::tr("Do not load Iaito plugins"));
cmd_parser.addOption(disableIaitoPlugins);
QCommandLineOption disableR2Plugins("no-r2-plugins", QObject::tr("Do not load radare2 plugins"));
cmd_parser.addOption(disableR2Plugins);
cmd_parser.process(*this);
IaitoCommandLineOptions opts;
opts.args = cmd_parser.positionalArguments();
if (cmd_parser.isSet(analOption)) {
bool analLevelSpecified = false;
int analLevel = cmd_parser.value(analOption).toInt(&analLevelSpecified);
if (!analLevelSpecified || analLevel < 0 || analLevel > 3) {
fprintf(
stderr,
"%s\n",
QObject::tr("Invalid Analysis Level. May be a value between 0 and 3.")
.toLocal8Bit()
.constData());
return false;
}
switch (analLevel) {
case 0:
opts.analLevel = AutomaticAnalysisLevel::None;
break;
case 1:
opts.analLevel = AutomaticAnalysisLevel::AAA;
break;
case 2:
opts.analLevel = AutomaticAnalysisLevel::AAAA;
break;
case 3:
opts.analLevel = AutomaticAnalysisLevel::AAAAA;
break;
}
}
if (opts.args.empty() && opts.analLevel != AutomaticAnalysisLevel::Ask) {
fprintf(
stderr,
"%s\n",
QObject::tr("Filename must be specified to start analysis automatically.")
.toLocal8Bit()
.constData());
return false;
}
InitialOptions options;
if (!opts.args.isEmpty()) {
opts.fileOpenOptions.filename = opts.args[0];
opts.fileOpenOptions.forceBinPlugin = cmd_parser.value(formatOption);
if (cmd_parser.isSet(baddrOption)) {
bool ok;
RVA baddr = cmd_parser.value(baddrOption).toULongLong(&ok, 0);
if (ok) {
options.binLoadAddr = baddr;
}
}
switch (opts.analLevel) {
case AutomaticAnalysisLevel::Ask:
break;
case AutomaticAnalysisLevel::None:
opts.fileOpenOptions.analCmd = {};
break;
case AutomaticAnalysisLevel::AAA:
opts.fileOpenOptions.analCmd = {{"aaa", "Auto analysis"}};
break;
case AutomaticAnalysisLevel::AAAA:
opts.fileOpenOptions.analCmd = {{"aaaa", "Advanced analysis"}};
break;
case AutomaticAnalysisLevel::AAAAA:
opts.fileOpenOptions.analCmd = {{"aaaaa", "Auto analysis (experimental)"}};
break;
}
opts.fileOpenOptions.script = cmd_parser.value(scriptOption);
opts.fileOpenOptions.writeEnabled = cmd_parser.isSet(writeModeOption);
}
if (cmd_parser.isSet(pythonHomeOption)) {
opts.pythonHome = cmd_parser.value(pythonHomeOption);
}
opts.outputRedirectionEnabled = !cmd_parser.isSet(disableRedirectOption);
if (cmd_parser.isSet(disablePlugins)) {
opts.enableIaitoPlugins = false;
opts.enableR2Plugins = false;
}
if (cmd_parser.isSet(disableIaitoPlugins)) {
opts.enableIaitoPlugins = false;
}
if (cmd_parser.isSet(disableR2Plugins)) {
opts.enableR2Plugins = false;
}
this->clOptions = opts;
return true;
}
void IaitoProxyStyle::polish(QWidget *widget)
{
QProxyStyle::polish(widget);
#if QT_VERSION_CHECK(5, 10, 0) < QT_VERSION
// HACK: This is the only way I've found to force Qt (5.10 and newer) to
// display shortcuts in context menus on all platforms. It's ugly,
// but it gets the job done.
if (auto menu = qobject_cast<QMenu *>(widget)) {
const auto &actions = menu->actions();
for (auto action : actions) {
action->setShortcutVisibleInContextMenu(true);
}
}
#endif // QT_VERSION_CHECK(5, 10, 0) < QT_VERSION
}