Support read/write data in emulator

pull/3/head
wh201906 5 years ago
parent 5f1df6782a
commit b44488fbed

@ -1,6 +1,7 @@
#include "mifare.h"
Mifare::Mifare(Ui::MainWindow *ui, Util *addr, QWidget *parent) : QObject(parent)
Mifare::Mifare(Ui::MainWindow *ui, Util *addr, QWidget *parent)
: QObject(parent)
{
this->parent = parent;
util = addr;
@ -16,14 +17,13 @@ Mifare::Mifare(Ui::MainWindow *ui, Util *addr, QWidget *parent) : QObject(parent
nestedKeyPattern = new QRegExp("\\|\\d{3}\\|.+\\|.+\\|.+\\|.+\\|");
}
QString Mifare::info(bool isRequiringOutput)
{
if (isRequiringOutput)
{
QString result = util->execCMDWithOutput("hf 14a info", 500);
qDebug() << result << result.indexOf(QRegExp(ui->MF_RW_dataEdit->text()), 0);
qDebug() << result
<< result.indexOf(QRegExp(ui->MF_RW_dataEdit->text()), 0);
result.replace("UID :", "|");
result.replace("ATQA :", "|");
result.replace("SAK :", "|");
@ -47,14 +47,17 @@ QString Mifare::info(bool isRequiringOutput)
void Mifare::chk()
{
QString result = util->execCMDWithOutput("hf mf chk *" + QString::number(cardType.type) + " ?", 1000 + cardType.type * 1000);
QString result = util->execCMDWithOutput(
"hf mf chk *" + QString::number(cardType.type) + " ?",
1000 + cardType.type * 1000);
qDebug() << result;
int offset = 0;
QString tmp, tmp2;
for (int i = 0; i < cardType.sectors; i++)
{
offset = result.indexOf(*chkKeyPattern, offset);
offset = chkKeyPattern->indexIn(result, offset);
// offset = result.indexOf(*chkKeyPattern, offset);
tmp = result.mid(offset, 39).toUpper();
offset += 39;
qDebug() << tmp << offset;
@ -70,13 +73,15 @@ void Mifare::chk()
void Mifare::nested()
{
QString result = util->execCMDWithOutput("hf mf nested " + QString::number(cardType.type) + " *");
QString result = util->execCMDWithOutput(
"hf mf nested " + QString::number(cardType.type) + " *");
int offset = 0;
QString tmp;
for (int i = 0; i < cardType.sectors; i++)
{
offset = result.indexOf(*nestedKeyPattern, offset);
offset = nestedKeyPattern->indexIn(result, offset);
// offset = result.indexOf(*nestedKeyPattern, offset);
tmp = result.mid(offset, 47).toUpper();
offset += 47;
if (tmp.at(23) == '1')
@ -111,29 +116,29 @@ void Mifare::read()
{
int waitTime = 300;
int currblk = ui->MF_RW_blockBox->currentText().toInt();
QString result = util->execCMDWithOutput("hf mf rdbl "
+ QString::number(currblk)
+ " "
+ ui->MF_RW_keyTypeBox->currentText()
+ " "
+ ui->MF_RW_keyEdit->text(), waitTime);
QString result = util->execCMDWithOutput(
"hf mf rdbl " + QString::number(currblk) + " " +
ui->MF_RW_keyTypeBox->currentText() + " " + ui->MF_RW_keyEdit->text(),
waitTime);
if (result.indexOf("isOk:01") != -1)
{
result = result.mid(result.indexOf(*dataPattern, 0), 47).toUpper();
if((currblk < 128 && ((currblk + 1) % 4 == 0)) || ((currblk + 1) % 8 == 0)) // process key block
result = result.mid(dataPattern->indexIn(result), 47).toUpper();
if ((currblk < 128 && ((currblk + 1) % 4 == 0)) ||
((currblk + 1) % 8 == 0)) // process key block
{
if (ui->MF_RW_keyTypeBox->currentText() == "A")
{
for (int i = 0; i < 6; i++)
{
result = result.replace(i * 3, 2, ui->MF_RW_keyEdit->text().mid(i * 2, 2));
result =
result.replace(i * 3, 2, ui->MF_RW_keyEdit->text().mid(i * 2, 2));
}
ui->MF_RW_dataEdit->setText(result);
QString tmpKey = result.right(18).replace(" ", "");
result = util->execCMDWithOutput("hf mf rdbl "
+ ui->MF_RW_keyTypeBox->currentText()
+ " B "
+ tmpKey, waitTime);
result = util->execCMDWithOutput(
"hf mf rdbl " + ui->MF_RW_keyTypeBox->currentText() + " B " +
tmpKey,
waitTime);
if (result.indexOf("isOk:01") == -1)
{
result = ui->MF_RW_dataEdit->text();
@ -145,7 +150,8 @@ void Mifare::read()
{
for (int i = 0; i < 6; i++)
{
result = result.replace(30 + i * 3, 2, ui->MF_RW_keyEdit->text().mid(i * 2, 2));
result = result.replace(30 + i * 3, 2,
ui->MF_RW_keyEdit->text().mid(i * 2, 2));
}
result = result.replace(0, 18, "?? ?? ?? ?? ?? ?? ");
ui->MF_RW_dataEdit->setText(result);
@ -176,10 +182,9 @@ void Mifare::readAll()
// check keys and read the first block of each sector
if (data_isKeyValid(keyAList->at(i)))
{
result = util->execCMDWithOutput("hf mf rdsc "
+ QString::number(i)
+ " A "
+ keyAList->at(i), waitTime);
result = util->execCMDWithOutput("hf mf rdsc " + QString::number(i) +
" A " + keyAList->at(i),
waitTime);
qDebug() << result;
offset = result.indexOf("isOk:01");
if (offset != -1)
@ -187,7 +192,8 @@ void Mifare::readAll()
isKeyAValid = true;
for (int j = 0; j < cardType.blk[i]; j++)
{
offset = result.indexOf(*dataPattern, offset);
offset = dataPattern->indexIn(result, offset);
// offset = result.indexOf(*dataPattern, offset);
tmp = result.mid(offset, 47).toUpper();
offset += 47;
qDebug() << tmp;
@ -199,10 +205,9 @@ void Mifare::readAll()
}
if (data_isKeyValid(keyBList->at(i)))
{
result = util->execCMDWithOutput("hf mf rdsc "
+ QString::number(i)
+ " B "
+ keyBList->at(i), waitTime);
result = util->execCMDWithOutput("hf mf rdsc " + QString::number(i) +
" B " + keyBList->at(i),
waitTime);
qDebug() << result;
offset = result.indexOf("isOk:01");
if (offset != -1)
@ -210,7 +215,8 @@ void Mifare::readAll()
isKeyBValid = true;
for (int j = 0; j < cardType.blk[i]; j++)
{
offset = result.indexOf(*dataPattern, offset);
offset = dataPattern->indexIn(result, offset);
// offset = result.indexOf(*dataPattern, offset);
tmp = result.mid(offset, 47).toUpper();
offset += 47;
qDebug() << tmp;
@ -227,7 +233,8 @@ void Mifare::readAll()
// fill the MF_dataWidget with the known valid key
//
// check whether the MF_dataWidget contains the valid key,
// and fill MF_keyWidget(when you only have KeyA but the ReadBlock output contains the KeyB)
// and fill MF_keyWidget(when you only have KeyA but the ReadBlock output
// contains the KeyB)
//
// the structure is not symmetric, since you cannot get KeyA from output
// this program will only process the provided KeyA(in MF_keyWidget)
@ -250,11 +257,13 @@ void Mifare::readAll()
}
else // now isKeyAValid == true, the output might contains the KeyB
{
QString tmpKey = dataList->at(cardType.blks[i] + cardType.blk[i] - 1).right(12);
result = util->execCMDWithOutput("hf mf rdbl "
+ QString::number(cardType.blks[i] + cardType.blk[i] - 1)
+ " B "
+ tmpKey, waitTime);
QString tmpKey =
dataList->at(cardType.blks[i] + cardType.blk[i] - 1).right(12);
result = util->execCMDWithOutput(
"hf mf rdbl " +
QString::number(cardType.blks[i] + cardType.blk[i] - 1) +
" B " + tmpKey,
waitTime);
if (result.indexOf("isOk:01") != -1)
{
keyBList->replace(i, tmpKey);
@ -265,7 +274,6 @@ void Mifare::readAll()
result = dataList->at(cardType.blks[i] + cardType.blk[i] - 1);
result = result.replace(20, 12, "????????????");
dataList->replace(cardType.blks[i] + cardType.blk[i] - 1, result);
}
}
data_syncWithDataWidget(false, cardType.blks[i] + cardType.blk[i] - 1);
@ -276,14 +284,12 @@ void Mifare::readAll()
void Mifare::write()
{
int waitTime = 300;
QString result = util->execCMDWithOutput("hf mf wrbl "
+ ui->MF_RW_blockBox->currentText()
+ " "
+ ui->MF_RW_keyTypeBox->currentText()
+ " "
+ ui->MF_RW_keyEdit->text()
+ " "
+ ui->MF_RW_dataEdit->text().replace(" ", ""), waitTime);
QString result = util->execCMDWithOutput(
"hf mf wrbl " + ui->MF_RW_blockBox->currentText() + " " +
ui->MF_RW_keyTypeBox->currentText() + " " +
ui->MF_RW_keyEdit->text() + " " +
ui->MF_RW_dataEdit->text().replace(" ", ""),
waitTime);
if (result.indexOf("isOk:01") != -1)
{
QMessageBox::information(parent, tr("Info"), tr("Success!"));
@ -302,27 +308,27 @@ void Mifare::writeAll()
{
for (int j = 0; j < cardType.blk[i]; j++)
{
result = ""; // if the KeyA is invalid and the result is not empty, the KeyB will not be tested.
if(data_isDataValid(dataList->at(cardType.blks[i] + j)) != DATA_NOSPACE || dataList->at(cardType.blks[i] + j).contains('?'))
result = ""; // if the KeyA is invalid and the result is not empty, the
// KeyB will not be tested.
if (data_isDataValid(dataList->at(cardType.blks[i] + j)) !=
DATA_NOSPACE ||
dataList->at(cardType.blks[i] + j).contains('?'))
continue;
if (data_isKeyValid(keyAList->at(i)))
{
result = util->execCMDWithOutput("hf mf wrbl "
+ QString::number(cardType.blks[i] + j)
+ " A "
+ keyAList->at(i)
+ " "
+ dataList->at(cardType.blks[i] + j), waitTime);
result = util->execCMDWithOutput(
"hf mf wrbl " + QString::number(cardType.blks[i] + j) + " A " +
keyAList->at(i) + " " + dataList->at(cardType.blks[i] + j),
waitTime);
}
qDebug() << i << j << result.indexOf("isOk:01") << data_isKeyValid(keyBList->at(i));
qDebug() << i << j << result.indexOf("isOk:01")
<< data_isKeyValid(keyBList->at(i));
if (result.indexOf("isOk:01") == -1 && data_isKeyValid(keyBList->at(i)))
{
result = util->execCMDWithOutput("hf mf wrbl "
+ QString::number(cardType.blks[i] + j)
+ " B "
+ keyBList->at(i)
+ " "
+ dataList->at(cardType.blks[i] + j), waitTime);
result = util->execCMDWithOutput(
"hf mf wrbl " + QString::number(cardType.blks[i] + j) + " B " +
keyBList->at(i) + " " + dataList->at(cardType.blks[i] + j),
waitTime);
}
}
}
@ -332,11 +338,11 @@ void Mifare::readC()
{
int waitTime = 300;
int currblk = ui->MF_RW_blockBox->currentText().toInt();
QString result = util->execCMDWithOutput("hf mf cgetblk "
+ QString::number(currblk), waitTime);
QString result = util->execCMDWithOutput(
"hf mf cgetblk " + QString::number(currblk), waitTime);
if (result.indexOf("No chinese") == -1)
{
result = result.mid(result.indexOf(*dataPattern, 0), 47).toUpper();
result = result.mid(dataPattern->indexIn(result), 47).toUpper();
ui->MF_RW_dataEdit->setText(result);
}
}
@ -350,15 +356,15 @@ void Mifare::readAllC()
int offset = 0;
for (int i = 0; i < cardType.sectors; i++)
{
result = util->execCMDWithOutput("hf mf cgetsc "
+ QString::number(i), waitTime);
result = util->execCMDWithOutput("hf mf cgetsc " + QString::number(i), waitTime);
qDebug() << result;
if (result.indexOf("No chinese") == -1)
{
offset = 0;
for (int j = 0; j < cardType.blk[i]; j++)
{
offset = result.indexOf(*dataPattern, offset);
offset = dataPattern->indexIn(result, offset);
// offset = result.indexOf(*dataPattern, offset);
tmp = result.mid(offset, 47).toUpper();
offset += 47;
qDebug() << tmp;
@ -366,8 +372,10 @@ void Mifare::readAllC()
dataList->replace(cardType.blks[i] + j, tmp);
data_syncWithDataWidget(false, cardType.blks[i] + j);
}
keyAList->replace(i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).left(12));
keyBList->replace(i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).right(12));
keyAList->replace(
i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).left(12));
keyBList->replace(
i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).right(12));
data_syncWithKeyWidget(false, i, true);
data_syncWithKeyWidget(false, i, false);
}
@ -377,10 +385,10 @@ void Mifare::readAllC()
void Mifare::writeC()
{
int waitTime = 150;
QString result = util->execCMDWithOutput("hf mf csetblk "
+ ui->MF_RW_blockBox->currentText()
+ " "
+ ui->MF_RW_dataEdit->text().replace(" ", ""), waitTime);
QString result = util->execCMDWithOutput(
"hf mf csetblk " + ui->MF_RW_blockBox->currentText() + " " +
ui->MF_RW_dataEdit->text().replace(" ", ""),
waitTime);
if (result.indexOf("No chinese") == -1)
{
QMessageBox::information(parent, tr("Info"), tr("Success!"));
@ -399,22 +407,21 @@ void Mifare::writeAllC()
{
for (int j = 0; j < cardType.blk[i]; j++)
{
result = ""; // if the KeyA is invalid and the result is not empty, the KeyB will not be tested.
if(data_isDataValid(dataList->at(cardType.blks[i] + j)) != DATA_NOSPACE || dataList->at(cardType.blks[i] + j).contains('?'))
result = "";
if (data_isDataValid(dataList->at(cardType.blks[i] + j)) != DATA_NOSPACE ||
dataList->at(cardType.blks[i] + j).contains('?'))
continue;
result = util->execCMDWithOutput("hf mf csetblk "
+ QString::number(cardType.blks[i] + j)
+ " "
+ dataList->at(cardType.blks[i] + j), waitTime);
result = util->execCMDWithOutput(
"hf mf csetblk " + QString::number(cardType.blks[i] + j) + " " +
dataList->at(cardType.blks[i] + j),
waitTime);
}
}
}
void Mifare::wipeC()
{
util->execCMD("hf mf cwipe "
+ QString::number(cardType.type)
+ " f");
util->execCMD("hf mf cwipe " + QString::number(cardType.type) + " f");
ui->funcTab->setCurrentIndex(1);
}
@ -429,7 +436,8 @@ void Mifare::setParameterC()
lis[0].replace(" ", "");
lis[1].replace(" ", "");
lis[2].replace(" ", "");
MF_UID_parameterDialog dialog(lis[0].toUpper(), lis[1].toUpper(), lis[2].mid(0, 2).toUpper());
MF_UID_parameterDialog dialog(lis[0].toUpper(), lis[1].toUpper(),
lis[2].mid(0, 2).toUpper());
connect(&dialog, &MF_UID_parameterDialog::sendCMD, util, &Util::execCMD);
if (dialog.exec() == QDialog::Accepted)
ui->funcTab->setCurrentIndex(1);
@ -442,10 +450,75 @@ void Mifare::lockC()
util->execCMD("hf 14a raw -pa 43");
util->execCMD("hf 14a raw -pa E0 00 39 F7");
util->execCMD("hf 14a raw -pa E1 00 E1 EE");
util->execCMD("hf 14a raw -pa 85 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 18 47");
util->execCMD("hf 14a raw -pa 85 00 00 00 00 00 00 00 00 00 00 00 "
" 00 00 00 08 18 47");
util->execCMD("hf 14a raw 52");
}
void Mifare::writeAllE()
{
const int waitTime = 200;
QString result;
for (int i = 0; i < cardType.sectors; i++)
{
for (int j = 0; j < cardType.blk[i]; j++)
{
result = "";
if (data_isDataValid(dataList->at(cardType.blks[i] + j)) != DATA_NOSPACE || dataList->at(cardType.blks[i] + j).contains('?'))
continue;
result = util->execCMDWithOutput(
"hf mf eset " + QString::number(cardType.blks[i] + j)
+ " "
+ dataList->at(cardType.blks[i] + j),
waitTime);
}
}
util->execCMDWithOutput("hf mf eget", waitTime); // to refresh output buffer;
}
void Mifare::readAllE()
{
QString result;
const int waitTime = 200;
QString tmp;
int offset = 0;
for (int i = 0; i < cardType.sectors; i++)
{
offset = 0;
for (int j = 0; j < cardType.blk[i]; j++)
{
qDebug() << "**********" ;
result = util->execCMDWithOutput("hf mf eget " + QString::number(cardType.blks[i] + j), waitTime);
qDebug() << result ;
offset = dataPattern->indexIn(result);
// offset = result.indexOf(*dataPattern, offset); // When I find the data position in this way, the Regex might fail to match.
tmp = result.mid(offset, 47).toUpper();
qDebug() << tmp << offset;
qDebug() << "**********" ;
if(offset == -1)
continue;
tmp.replace(" ", "");
dataList->replace(cardType.blks[i] + j, tmp);
data_syncWithDataWidget(false, cardType.blks[i] + j);
}
keyAList->replace(i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).left(12));
keyBList->replace(i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).right(12));
data_syncWithKeyWidget(false, i, true);
data_syncWithKeyWidget(false, i, false);
}
}
void Mifare::wipeE()
{
util->execCMD("hf mf eclr");
}
void Mifare::dump()
{
util->execCMD("hf mf dump");
@ -543,7 +616,8 @@ bool Mifare::data_isKeyValid(const QString& key)
return true;
}
Mifare::DataType Mifare::data_isDataValid(QString data) // "?" will not been processd there
Mifare::DataType
Mifare::data_isDataValid(QString data) // "?" will not been processd there
{
if (data.length() == 47)
{
@ -608,11 +682,7 @@ bool Mifare::data_loadDataFile(const QString& filename)
for (int i = 0; i < cardType.blocks * 16; i++) // Detect the file type
{
// qDebug() << (unsigned char)buff[i];
if(!((buff[i] >= 'A' && buff[i] <= 'F') ||
(buff[i] >= 'a' && buff[i] <= 'f') ||
(buff[i] >= '0' && buff[i] <= '9') ||
buff[i] == '\n' ||
buff[i] == '\r'))
if (!((buff[i] >= 'A' && buff[i] <= 'F') || (buff[i] >= 'a' && buff[i] <= 'f') || (buff[i] >= '0' && buff[i] <= '9') || buff[i] == '\n' || buff[i] == '\r'))
{
isBin = true;
break;
@ -689,7 +759,9 @@ QString Mifare::bin2text(const QByteArray& buff, int i, int length)
{
QString ret = "";
char LByte, RByte;
char map[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char map[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
for (int j = 0; j < length; j++)
{
LByte = map[(unsigned char)buff[i * length + j] >> 4];
@ -786,7 +858,6 @@ bool Mifare::data_saveKeyFile(const QString& filename, bool isBin)
}
else
{
}
bool ret = file.write(buff) != -1;
file.close();
@ -834,8 +905,10 @@ void Mifare::data_data2Key()
}
else
{
keyAList->replace(i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).left(12));
keyBList->replace(i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).right(12));
keyAList->replace(
i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).left(12));
keyBList->replace(
i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).right(12));
}
data_syncWithKeyWidget();
}

@ -105,6 +105,9 @@ public:
void data_setData(int block, const QString &data);
void data_setKey(int sector, bool isKeyA, const QString &key);
void lockC();
void writeAllE();
void readAllE();
void wipeE();
public slots:
signals:

@ -6,10 +6,11 @@ MainWindow::MainWindow(QWidget *parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->MF_simGroupBox->setVisible(false); // developing...
// ui->MF_simGroupBox->setVisible(false); // developing...
ui->MF_sniffGroupBox->setVisible(false); // developing...
myInfo = new QAction("wh201906", this);
connect(myInfo, &QAction::triggered, [ = ]() {
connect(myInfo, &QAction::triggered, [ = ]()
{
QDesktopServices::openUrl(QUrl("https://github.com/wh201906"));
});
this->addAction(myInfo);
@ -473,6 +474,25 @@ void MainWindow::on_MF_UID_lockButton_clicked()
mifare->lockC();
}
void MainWindow::on_MF_Sim_loadDataButton_clicked()
{
setState(false);
mifare->writeAllE();
setState(true);
}
void MainWindow::on_MF_Sim_writeAllButton_clicked()
{
setState(false);
mifare->readAllE();
setState(true);
}
void MainWindow::on_MF_Sim_clearButton_clicked()
{
mifare->wipeE();
}
void MainWindow::on_MF_Sniff_sniffButton_clicked()
{
setState(false);
@ -651,3 +671,5 @@ void MainWindow::setState(bool st)
}
// ***********************************************

@ -22,7 +22,8 @@
#include "common/util.h"
QT_BEGIN_NAMESPACE
namespace Ui {
namespace Ui
{
class MainWindow;
}
QT_END_NAMESPACE
@ -119,6 +120,12 @@ private slots:
void on_MF_UID_lockButton_clicked();
void on_MF_Sim_loadDataButton_clicked();
void on_MF_Sim_writeAllButton_clicked();
void on_MF_Sim_clearButton_clicked();
private:
Ui::MainWindow* ui;
QButtonGroup* typeBtnGroup;

@ -105,9 +105,6 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="mifareTab">
<attribute name="title">
<string>Mifare</string>

Loading…
Cancel
Save