2020-10-07 10:36:04 +00:00
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-12-26 19:56:06 +00:00
|
|
|
// Copyright (c) 2020-2021, The Monero Project.
|
2020-10-07 10:36:04 +00:00
|
|
|
|
|
|
|
#include "cli.h"
|
|
|
|
|
|
|
|
// libwalletqt
|
|
|
|
#include "libwalletqt/TransactionHistory.h"
|
2021-02-24 10:57:35 +00:00
|
|
|
#include "libwalletqt/WalletManager.h"
|
2020-10-07 10:36:04 +00:00
|
|
|
#include "model/AddressBookModel.h"
|
|
|
|
#include "model/TransactionHistoryModel.h"
|
2021-02-24 10:57:35 +00:00
|
|
|
#include "utils/brute.h"
|
2020-10-07 10:36:04 +00:00
|
|
|
|
|
|
|
CLI::CLI(AppContext *ctx, QObject *parent) :
|
|
|
|
QObject(parent),
|
|
|
|
ctx(ctx) {
|
|
|
|
connect(this->ctx, &AppContext::walletOpened, this, &CLI::onWalletOpened);
|
|
|
|
connect(this->ctx, &AppContext::walletOpenedError, this, &CLI::onWalletOpenedError);
|
|
|
|
connect(this->ctx, &AppContext::walletOpenPasswordNeeded, this, &CLI::onWalletOpenPasswordRequired);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CLI::run() {
|
2021-02-24 10:57:35 +00:00
|
|
|
if (mode == CLIMode::ExportContacts || mode == CLIMode::ExportTxHistory)
|
|
|
|
{
|
|
|
|
if(!ctx->cmdargs->isSet("wallet-file"))
|
|
|
|
return this->finishedError("--wallet-file argument missing");
|
|
|
|
if(!ctx->cmdargs->isSet("password"))
|
|
|
|
return this->finishedError("--password argument missing");
|
2020-10-07 10:36:04 +00:00
|
|
|
ctx->onOpenWallet(ctx->cmdargs->value("wallet-file"), ctx->cmdargs->value("password"));
|
|
|
|
}
|
2021-02-24 10:57:35 +00:00
|
|
|
else if (mode == CLIMode::BruteforcePassword)
|
|
|
|
{
|
|
|
|
QString keys_file = ctx->cmdargs->value("bruteforce-password");
|
|
|
|
if (!keys_file.endsWith(".keys")) {
|
|
|
|
return this->finishedError("Wallet file does not end with .keys");
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList words;
|
|
|
|
if (ctx->cmdargs->isSet("bruteforce-dict")) {
|
|
|
|
QString data = Utils::barrayToString(Utils::fileOpen(ctx->cmdargs->value("bruteforce-dict")));
|
|
|
|
words = data.split("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ctx->cmdargs->isSet("bruteforce-chars")) {
|
|
|
|
return this->finishedError("--bruteforce-chars argument missing");
|
|
|
|
}
|
|
|
|
QString chars = ctx->cmdargs->value("bruteforce-chars");
|
|
|
|
|
|
|
|
brute b(chars.toStdString());
|
|
|
|
if (words.isEmpty()) {
|
|
|
|
qDebug() << "No dictionairy specified, bruteforcing all chars";
|
|
|
|
while (true) {
|
|
|
|
QString pass = QString::fromStdString(b.next());
|
|
|
|
if (ctx->walletManager->verifyWalletPassword(keys_file, pass, false)) {
|
|
|
|
this->finished(QString("Found password: %1").arg(pass));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
qDebug() << pass;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bruteword bb(chars.toStdString());
|
|
|
|
bool foundPass = false;
|
|
|
|
for (auto word: words) {
|
|
|
|
if (word.isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bb.setWord(word.toStdString());
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
QString pass = QString::fromStdString(bb.next());
|
|
|
|
if (pass == "") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ctx->walletManager->verifyWalletPassword(keys_file, pass, false)) {
|
|
|
|
this->finished(QString("Found password: %1").arg(pass));
|
|
|
|
foundPass = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
qDebug() << pass;
|
|
|
|
}
|
|
|
|
if (foundPass) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundPass) {
|
|
|
|
this->finished("Search space exhausted");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-07 10:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CLI::onWalletOpened() {
|
2021-02-24 10:57:35 +00:00
|
|
|
if(mode == CLIMode::ExportContacts){
|
2020-10-07 10:36:04 +00:00
|
|
|
auto *model = ctx->currentWallet->addressBookModel();
|
|
|
|
auto fn = ctx->cmdargs->value("export-contacts");
|
|
|
|
if(model->writeCSV(fn))
|
|
|
|
this->finished(QString("Address book exported to %1").arg(fn));
|
|
|
|
else
|
|
|
|
this->finishedError("Address book export failure");
|
2021-02-24 10:57:35 +00:00
|
|
|
} else if(mode == ExportTxHistory) {
|
2020-10-07 10:36:04 +00:00
|
|
|
ctx->currentWallet->history()->refresh(ctx->currentWallet->currentSubaddressAccount());
|
|
|
|
auto *model = ctx->currentWallet->history();
|
|
|
|
auto fn = ctx->cmdargs->value("export-txhistory");
|
|
|
|
if(model->writeCSV(fn))
|
|
|
|
this->finished(QString("Transaction history exported to %1").arg(fn));
|
|
|
|
else
|
|
|
|
this->finishedError("Transaction history export failure");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CLI::onWalletOpenedError(const QString &err) {
|
2021-02-24 10:57:35 +00:00
|
|
|
if(mode == CLIMode::ExportContacts ||
|
|
|
|
mode == CLIMode::ExportTxHistory)
|
2020-10-07 10:36:04 +00:00
|
|
|
return this->finishedError(err);
|
|
|
|
}
|
|
|
|
|
2020-10-21 06:45:25 +00:00
|
|
|
void CLI::onWalletOpenPasswordRequired(bool invalidPassword, const QString &path) {
|
2021-02-24 10:57:35 +00:00
|
|
|
if(mode == CLIMode::ExportContacts ||
|
|
|
|
mode == CLIMode::ExportTxHistory)
|
2020-10-07 10:36:04 +00:00
|
|
|
return this->finishedError("invalid password");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CLI::finished(const QString &msg){
|
|
|
|
qInfo() << msg;
|
|
|
|
emit closeApplication();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CLI::finishedError(const QString &err) {
|
|
|
|
qCritical() << err;
|
|
|
|
emit closeApplication();
|
|
|
|
}
|
|
|
|
|
|
|
|
CLI::~CLI() {
|
|
|
|
ctx->disconnect();
|
|
|
|
delete ctx;
|
|
|
|
}
|