cutter/src/dialogs/InitialOptionsDialog.cpp

420 lines
12 KiB
C++
Raw Normal View History

2018-10-17 07:55:53 +00:00
#include "common/AsyncTask.h"
#include "InitialOptionsDialog.h"
#include "ui_InitialOptionsDialog.h"
2019-03-14 09:28:42 +00:00
#include "core/MainWindow.h"
2017-10-02 09:41:28 +00:00
#include "dialogs/NewFileDialog.h"
2018-05-26 18:49:57 +00:00
#include "dialogs/AsyncTaskDialog.h"
2018-10-17 07:55:53 +00:00
#include "common/Helpers.h"
#include <QSettings>
#include <QFileInfo>
#include <QFileDialog>
2019-03-14 09:28:42 +00:00
#include <QCloseEvent>
#include "core/Cutter.h"
#include "common/AnalTask.h"
InitialOptionsDialog::InitialOptionsDialog(MainWindow *main):
QDialog(nullptr), // parent must not be main
ui(new Ui::InitialOptionsDialog),
2017-10-09 18:08:35 +00:00
main(main),
2018-08-18 18:45:49 +00:00
core(Core())
{
ui->setupUi(this);
setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
2018-02-12 12:22:53 +00:00
ui->logoSvgWidget->load(Config()->getLogoFile());
// Fill the plugins combo
2017-10-09 18:08:35 +00:00
asm_plugins = core->getAsmPluginNames();
for (const auto &plugin : asm_plugins) {
ui->archComboBox->addItem(plugin, plugin);
}
2017-10-09 18:08:35 +00:00
ui->archComboBox->setToolTip(core->cmd("e? asm.arch").trimmed());
// cpu combo box
ui->cpuComboBox->lineEdit()->setPlaceholderText(tr("Auto"));
2017-10-09 18:08:35 +00:00
ui->cpuComboBox->setToolTip(core->cmd("e? asm.cpu").trimmed());
updateCPUComboBox();
// os combo box
for (const auto &plugin : core->cmdList("e asm.os=?")) {
ui->kernelComboBox->addItem(plugin, plugin);
}
2017-10-09 18:08:35 +00:00
ui->kernelComboBox->setToolTip(core->cmd("e? asm.os").trimmed());
2017-10-09 18:08:35 +00:00
ui->bitsComboBox->setToolTip(core->cmd("e? asm.bits").trimmed());
for (const auto &plugin : core->getRBinPluginDescriptions("bin")) {
ui->formatComboBox->addItem(plugin.name, QVariant::fromValue(plugin));
}
analysisCommands = {
{ { "aa", tr("Analyze all symbols") }, new QCheckBox(), true },
{ { "aar", tr("Analyze instructions for references") }, new QCheckBox(), true },
{ { "aac", tr("Analyze function calls") }, new QCheckBox(), true },
{ { "aab", tr("Analyze all basic blocks") }, new QCheckBox(), false },
{ { "aao", tr("Analyze all objc references") }, new QCheckBox(), false },
{ { "avrr", tr("Recover class information from RTTI") }, new QCheckBox(), false },
{ { "aan", tr("Autoname functions based on context") }, new QCheckBox(), false },
{ { "aae", tr("Emulate code to find computed references") }, new QCheckBox(), false },
{ { "aat", tr("Analyze all consecutive functions") }, new QCheckBox(), false },
{ { "aaft", tr("Type and Argument matching analysis") }, new QCheckBox(), false },
{ { "aaT", tr("Analyze code after trap-sleds") }, new QCheckBox(), false },
{ { "aap", tr("Analyze function preludes") }, new QCheckBox(), false },
{ { "e! anal.jmp.tbl", tr("Analyze jump tables in switch statements") }, new QCheckBox(), false },
{ { "e! anal.pushret", tr("Analyze PUSH+RET as JMP") }, new QCheckBox(), false },
{ { "e! anal.hasnext", tr("Continue analysis after each function") }, new QCheckBox(), false }};
// Per each checkbox, set a tooltip desccribing it
AnalysisCommands item;
foreach (item, analysisCommands){
item.checkbox->setText(item.commandDesc.description);
item.checkbox->setToolTip(item.commandDesc.command);
item.checkbox->setChecked(item.checked);
ui->verticalLayout_7->addWidget(item.checkbox);
}
ui->hideFrame->setVisible(false);
ui->analoptionsFrame->setVisible(false);
ui->advancedAnlysisLine->setVisible(false);
updatePDBLayout();
connect(ui->pdbCheckBox, SIGNAL(stateChanged(int)), this, SLOT(updatePDBLayout()));
updateScriptLayout();
connect(ui->scriptCheckBox, SIGNAL(stateChanged(int)), this, SLOT(updateScriptLayout()));
2017-12-06 12:32:35 +00:00
connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
2017-04-09 19:55:06 +00:00
ui->programLineEdit->setText(main->getFilename());
}
InitialOptionsDialog::~InitialOptionsDialog() {}
void InitialOptionsDialog::updateCPUComboBox()
{
QString currentText = ui->cpuComboBox->lineEdit()->text();
ui->cpuComboBox->clear();
QString cmd = "e asm.cpu=?";
QString arch = getSelectedArch();
if (!arch.isNull()) {
cmd += " @a:" + arch;
}
ui->cpuComboBox->addItem("");
2017-10-09 18:08:35 +00:00
ui->cpuComboBox->addItems(core->cmdList(cmd));
ui->cpuComboBox->lineEdit()->setText(currentText);
}
QList<QString> InitialOptionsDialog::getAnalysisCommands(const InitialOptions &options) {
QList<QString> commands;
for (auto& commandDesc: options.analCmd) {
commands << commandDesc.command;
}
return commands;
}
void InitialOptionsDialog::loadOptions(const InitialOptions &options)
2018-07-24 16:49:52 +00:00
{
if (options.analCmd.isEmpty()) {
2018-08-18 18:45:49 +00:00
analLevel = 0;
} else if (options.analCmd.first().command == "aaa" ) {
2018-08-18 18:45:49 +00:00
analLevel = 1;
} else if (options.analCmd.first().command == "aaaa" ) {
2018-08-18 18:45:49 +00:00
analLevel = 2;
} else {
analLevel = 3;
AnalysisCommands item;
QList<QString> commands = getAnalysisCommands(options);
foreach (item, analysisCommands){
qInfo() << item.commandDesc.command;
item.checkbox->setChecked(commands.contains(item.commandDesc.command));
}
2018-07-24 16:49:52 +00:00
}
if (!options.script.isEmpty()) {
ui->scriptCheckBox->setChecked(true);
ui->scriptLineEdit->setText(options.script);
2018-08-18 18:45:49 +00:00
analLevel = 0;
2018-08-18 16:09:49 +00:00
} else {
ui->scriptCheckBox->setChecked(false);
ui->scriptLineEdit->setText("");
}
2018-08-18 18:45:49 +00:00
ui->analSlider->setValue(analLevel);
shellcode = options.shellcode;
// TODO: all other options should also be applied to the ui
}
QString InitialOptionsDialog::getSelectedArch() const
{
QVariant archValue = ui->archComboBox->currentData();
return archValue.isValid() ? archValue.toString() : nullptr;
}
QString InitialOptionsDialog::getSelectedCPU() const
{
QString cpu = ui->cpuComboBox->currentText();
if (cpu.isNull() || cpu.isEmpty()) {
return nullptr;
}
return cpu;
}
int InitialOptionsDialog::getSelectedBits() const
{
QString sel_bits = ui->bitsComboBox->currentText();
2018-03-21 20:32:32 +00:00
if (sel_bits != "Auto") {
return sel_bits.toInt();
}
return 0;
}
InitialOptions::Endianness InitialOptionsDialog::getSelectedEndianness() const
2018-01-20 10:35:31 +00:00
{
2018-03-21 20:32:32 +00:00
switch (ui->endiannessComboBox->currentIndex()) {
2018-01-20 10:35:31 +00:00
case 1:
return InitialOptions::Endianness::Little;
2018-01-20 10:35:31 +00:00
case 2:
return InitialOptions::Endianness::Big;
2018-01-20 10:35:31 +00:00
default:
return InitialOptions::Endianness::Auto;
2018-01-20 10:35:31 +00:00
}
}
QString InitialOptionsDialog::getSelectedOS() const
{
QVariant os = ui->kernelComboBox->currentData();
return os.isValid() ? os.toString() : nullptr;
}
QList<CommandDescription> InitialOptionsDialog::getSelectedAdvancedAnalCmds() const
2018-05-27 19:38:19 +00:00
{
QList<CommandDescription> advanced = QList<CommandDescription>();
2018-05-27 19:38:19 +00:00
if (ui->analSlider->value() == 3) {
AnalysisCommands item;
foreach (item, analysisCommands){
if(item.checkbox->isChecked()) {
advanced << item.commandDesc;
}
2018-05-27 19:38:19 +00:00
}
}
return advanced;
}
void InitialOptionsDialog::setupAndStartAnalysis(/*int level, QList<QString> advanced*/)
{
main->initUI();
2018-09-30 20:00:53 +00:00
InitialOptions options;
2018-05-26 18:49:57 +00:00
options.filename = main->getFilename();
2018-09-27 11:16:07 +00:00
if (!options.filename.isEmpty()) {
main->setWindowTitle("Cutter " + options.filename);
}
options.shellcode = this->shellcode;
// Where the bin header is located in the file (-B)
if (ui->entry_loadOffset->text().length() > 0) {
options.binLoadAddr = Core()->math(ui->entry_loadOffset->text());
}
2018-09-30 20:00:53 +00:00
options.mapAddr = Core()->math(
ui->entry_mapOffset->text()); // Where to map the file once loaded (-m)
options.arch = getSelectedArch();
options.cpu = getSelectedCPU();
options.bits = getSelectedBits();
options.os = getSelectedOS();
options.writeEnabled = ui->writeCheckBox->isChecked();
options.loadBinInfo = !ui->binCheckBox->isChecked();
QVariant forceBinPluginData = ui->formatComboBox->currentData();
if (!forceBinPluginData.isNull()) {
RBinPluginDescription pluginDesc = forceBinPluginData.value<RBinPluginDescription>();
options.forceBinPlugin = pluginDesc.name;
}
options.demangle = ui->demangleCheckBox->isChecked();
if (ui->pdbCheckBox->isChecked()) {
options.pdbFile = ui->pdbLineEdit->text();
}
if (ui->scriptCheckBox->isChecked()) {
options.script = ui->scriptLineEdit->text();
}
2019-01-31 16:45:58 +00:00
options.endian = getSelectedEndianness();
int level = ui->analSlider->value();
2018-09-30 20:00:53 +00:00
switch (level) {
case 1:
options.analCmd = { {"aaa", "Auto analysis"} };
2018-09-30 20:00:53 +00:00
break;
case 2:
options.analCmd = { {"aaaa", "Auto analysis (experimental}"} };
2018-09-30 20:00:53 +00:00
break;
case 3:
options.analCmd = getSelectedAdvancedAnalCmds();
break;
default:
options.analCmd = {};
break;
2018-05-27 19:38:19 +00:00
}
2018-05-28 14:19:04 +00:00
AnalTask *analTask = new AnalTask();
analTask->setOptions(options);
MainWindow *main = this->main;
connect(analTask, &AnalTask::openFileFailed, main, &MainWindow::openNewFileFailed);
connect(analTask, &AsyncTask::finished, main, [analTask, main]() {
if (analTask->getOpenFileFailed()) {
return;
}
main->finalizeOpen();
});
2018-05-28 14:19:04 +00:00
AsyncTask::Ptr analTaskPtr(analTask);
AsyncTaskDialog *taskDialog = new AsyncTaskDialog(analTaskPtr);
taskDialog->setInterruptOnClose(true);
taskDialog->setAttribute(Qt::WA_DeleteOnClose);
2018-05-26 18:49:57 +00:00
taskDialog->show();
Core()->getAsyncTaskManager()->start(analTaskPtr);
done(0);
}
void InitialOptionsDialog::on_okButton_clicked()
{
ui->okButton->setEnabled(false);
setupAndStartAnalysis();
}
void InitialOptionsDialog::closeEvent(QCloseEvent *event)
{
event->accept();
}
QString InitialOptionsDialog::analysisDescription(int level)
{
//TODO: replace this with meaningful descriptions
2018-03-21 20:32:32 +00:00
switch (level) {
case 0:
return tr("No analysis");
case 1:
return tr("Auto-Analysis (aaa)");
case 2:
return tr("Auto-Analysis Experimental (aaaa)");
case 3:
return tr("Advanced");
default:
return tr("Unknown");
}
}
void InitialOptionsDialog::on_analSlider_valueChanged(int value)
{
2017-04-12 15:02:35 +00:00
ui->analDescription->setText(tr("Level") + QString(": %1").arg(analysisDescription(value)));
2018-03-21 20:32:32 +00:00
if (value == 0) {
ui->analCheckBox->setChecked(false);
ui->analCheckBox->setText(tr("Analysis: Disabled"));
2018-03-21 20:32:32 +00:00
} else {
ui->analCheckBox->setChecked(true);
ui->analCheckBox->setText(tr("Analysis: Enabled"));
2018-03-21 20:32:32 +00:00
if (value == 3) {
ui->analoptionsFrame->setVisible(true);
ui->advancedAnlysisLine->setVisible(true);
2018-03-21 20:32:32 +00:00
} else {
ui->analoptionsFrame->setVisible(false);
ui->advancedAnlysisLine->setVisible(false);
}
}
}
void InitialOptionsDialog::on_AdvOptButton_clicked()
{
2018-03-21 20:32:32 +00:00
if (ui->AdvOptButton->isChecked()) {
ui->hideFrame->setVisible(true);
ui->AdvOptButton->setArrowType(Qt::DownArrow);
2018-03-21 20:32:32 +00:00
} else {
ui->hideFrame->setVisible(false);
ui->AdvOptButton->setArrowType(Qt::RightArrow);
}
}
void InitialOptionsDialog::on_analCheckBox_clicked(bool checked)
{
2018-08-18 18:45:49 +00:00
if (!checked) {
analLevel = ui->analSlider->value();
}
ui->analSlider->setValue(checked ? analLevel : 0);
}
void InitialOptionsDialog::on_archComboBox_currentIndexChanged(int)
{
updateCPUComboBox();
}
void InitialOptionsDialog::updatePDBLayout()
{
ui->pdbWidget->setEnabled(ui->pdbCheckBox->isChecked());
}
void InitialOptionsDialog::on_pdbSelectButton_clicked()
{
QFileDialog dialog(this);
dialog.setWindowTitle(tr("Select PDB file"));
dialog.setNameFilters({ tr("PDB file (*.pdb)"), tr("All files (*)") });
2018-03-21 20:32:32 +00:00
if (!dialog.exec()) {
return;
}
const QString &fileName = QDir::toNativeSeparators(dialog.selectedFiles().first());
2018-03-21 20:32:32 +00:00
if (!fileName.isEmpty()) {
ui->pdbLineEdit->setText(fileName);
}
}
2017-12-06 12:32:35 +00:00
void InitialOptionsDialog::updateScriptLayout()
{
ui->scriptWidget->setEnabled(ui->scriptCheckBox->isChecked());
}
void InitialOptionsDialog::on_scriptSelectButton_clicked()
{
QFileDialog dialog(this);
dialog.setWindowTitle(tr("Select radare2 script file"));
dialog.setNameFilters({ tr("Script file (*.r2)"), tr("All files (*)") });
if (!dialog.exec()) {
return;
}
const QString &fileName = QDir::toNativeSeparators(dialog.selectedFiles().first());
if (!fileName.isEmpty()) {
ui->scriptLineEdit->setText(fileName);
}
}
void InitialOptionsDialog::reject()
2017-12-06 12:32:35 +00:00
{
done(0);
main->displayNewFileDialog();
2017-12-06 12:32:35 +00:00
}