From 184a9ed5f2707933c847e930a196218febc36ab4 Mon Sep 17 00:00:00 2001 From: wh201906 Date: Tue, 27 Jun 2023 21:09:08 +0800 Subject: [PATCH] Fix saving/loading trace(sniff) files Fix file extension for RRG repo Fix overwriting existing file Add support for RRG v4.16717(path in prefs) --- config/config_rrgv4.16717.json | 4 ++- src/module/mifare.cpp | 15 +++++++++++ src/module/mifare.h | 1 + src/ui/mainwindow.cpp | 47 +++++++++++++++++++++++++++------- 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/config/config_rrgv4.16717.json b/config/config_rrgv4.16717.json index e355231..7fc7a3e 100644 --- a/config/config_rrgv4.16717.json +++ b/config/config_rrgv4.16717.json @@ -104,7 +104,9 @@ "cmd": "hf mf darkside" }, "save sniff": { - "cmd": "trace save -f " + "cmd": "trace save -f ", + "path cmd":"prefs show", + "path pattern":"trace save path\\.+\\s*(.+)$" }, "load sniff": { "cmd": "trace load -f ", diff --git a/src/module/mifare.cpp b/src/module/mifare.cpp index 46baf7c..ca6b9ff 100644 --- a/src/module/mifare.cpp +++ b/src/module/mifare.cpp @@ -1375,3 +1375,18 @@ quint16 Mifare::getTrailerBlockId(quint8 sectorId, qint8 cardTypeId) // other cardTypeId: use current cardtype(include default -1) return (cardType.blks[sectorId] + cardType.blk[sectorId] - 1); } + +QString Mifare::getTraceSavePath() +{ + QVariantMap config = configMap["save sniff"].toMap(); + QString pathCmd = config["path cmd"].toString(); + QString patternText = config["path pattern"].toString(); + QRegularExpression pattern = QRegularExpression(patternText, QRegularExpression::MultilineOption); + if(pathCmd.isEmpty() || patternText.isEmpty()) + return QString(); + QString result = util->execCMDWithOutput(pathCmd, 500); + QRegularExpressionMatch reMatch = pattern.match(result); + if(!reMatch.hasMatch()) + return QString(); + return reMatch.captured(1).trimmed(); +} diff --git a/src/module/mifare.h b/src/module/mifare.h index bc4d153..79663d9 100644 --- a/src/module/mifare.h +++ b/src/module/mifare.h @@ -115,6 +115,7 @@ class Mifare : public QObject QString data_getUID(); quint16 getTrailerBlockId(quint8 sectorId, qint8 cardTypeId = -1); // -1: use current cardtype void setConfigMap(const QVariantMap& configMap); + QString getTraceSavePath(); public slots: signals: diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index 2a183d7..3dd3e57 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -956,18 +956,31 @@ void MainWindow::on_MF_Sniff_loadButton_clicked() // use a tmp file to support c { QString title = ""; QString filename = ""; + QString defaultExtension; + QDir clientTracePath; + + if(Util::getClientType() == Util::CLIENTTYPE_OFFICIAL) + defaultExtension = ".trc"; + else if(Util::getClientType() == Util::CLIENTTYPE_ICEMAN) + defaultExtension = ".trace"; + + QString userTraceSavePath = mifare->getTraceSavePath(); + if(userTraceSavePath.isEmpty()) + clientTracePath = *clientWorkingDir; + else + clientTracePath = QDir(userTraceSavePath); // For v4.16717 and later title = tr("Plz select the trace file:"); - filename = QFileDialog::getOpenFileName(this, title, clientWorkingDir->absolutePath(), tr("Trace Files(*.trc)") + ";;" + tr("All Files(*.*)")); + filename = QFileDialog::getOpenFileName(this, title, clientTracePath.absolutePath(), tr("Trace Files") + "(*" + defaultExtension + ")" + ";;" + tr("All Files(*.*)")); qDebug() << filename; if(filename != "") { - QString tmpFile = "tmp" + QString::number(QDateTime::currentDateTime().toTime_t()) + ".trc"; - if(QFile::copy(filename, clientWorkingDir->absolutePath() + "/" + tmpFile)) + QString tmpFile = "tmp" + QString::number(QDateTime::currentDateTimeUtc().toTime_t()) + defaultExtension; + if(QFile::copy(filename, clientTracePath.absolutePath() + "/" + tmpFile)) { mifare->loadSniff(tmpFile); util->delay(3000); - QFile::remove(clientWorkingDir->absolutePath() + "/" + tmpFile); + QFile::remove(clientTracePath.absolutePath() + "/" + tmpFile); } else { @@ -980,25 +993,41 @@ void MainWindow::on_MF_Sniff_saveButton_clicked() { QString title = ""; QString filename = ""; + QString defaultExtension; + QDir clientTracePath; + + if(Util::getClientType() == Util::CLIENTTYPE_OFFICIAL) + defaultExtension = ".trc"; + else if(Util::getClientType() == Util::CLIENTTYPE_ICEMAN) + defaultExtension = ".trace"; + + QString userTraceSavePath = mifare->getTraceSavePath(); + if(userTraceSavePath.isEmpty()) + clientTracePath = *clientWorkingDir; + else + clientTracePath = QDir(userTraceSavePath); // For v4.16717 and later title = tr("Plz select the location to save trace file:"); - filename = QFileDialog::getSaveFileName(this, title, clientWorkingDir->absolutePath(), tr("Trace Files(*.trc)")); + filename = QFileDialog::getSaveFileName(this, title, clientTracePath.absolutePath(), tr("Trace Files") + "(*" + defaultExtension + ")"); qDebug() << filename; if(filename != "") { - QString tmpFile = "tmp" + QString::number(QDateTime::currentDateTime().toTime_t()) + ".trc"; + QString tmpFile = "tmp" + QString::number(QDateTime::currentDateTimeUtc().toTime_t()) + defaultExtension; mifare->saveSniff(tmpFile); for(int i = 0; i < 100; i++) { util->delay(100); - if(QFile::exists(clientWorkingDir->absolutePath() + "/" + tmpFile)) + if(QFile::exists(clientTracePath.absolutePath() + "/" + tmpFile)) break; } - if(!QFile::copy(clientWorkingDir->absolutePath() + "/" + tmpFile, filename)) + // filename is not empty -> the user has chosen to overwrite the existing file + if(QFile::exists(filename)) + QFile::remove(filename); + if(!QFile::copy(clientTracePath.absolutePath() + "/" + tmpFile, filename)) { QMessageBox::information(this, tr("Info"), tr("Failed to save to") + "\n" + filename); } - QFile::remove(clientWorkingDir->absolutePath() + "/" + tmpFile); + QFile::remove(clientTracePath.absolutePath() + "/" + tmpFile); } }