diff --git a/module/mifare.cpp b/module/mifare.cpp index 8eeb023..6e1c7da 100644 --- a/module/mifare.cpp +++ b/module/mifare.cpp @@ -273,7 +273,7 @@ QString Mifare::_readblk(int blockId, KeyType keyType, const QString& key, int w { QString data; QString result; - bool isKeyBlock = (blockId < 128 && ((blockId + 1) % 4 == 0)) || ((blockId + 1) % 8 == 0); + bool isKeyBlock = (blockId < 128 && ((blockId + 1) % 4 == 0)) || ((blockId + 1) % 16 == 0); if(!data_isKeyValid(key)) { @@ -397,7 +397,7 @@ QStringList Mifare::_readsec(int sectorId, KeyType keyType, const QString& key, return data; } -void Mifare::read() +void Mifare::readOne() { int blockId = ui->MF_RW_blockBox->currentText().toInt(); Mifare::KeyType keyType = (Mifare::KeyType)(ui->MF_RW_keyTypeBox->currentData().toInt()); @@ -424,7 +424,7 @@ void Mifare::readSelected(const QList& selectedBlocks) } for(int item : selectedBlocks) { - selectedSectors[item / 4] = true; + selectedSectors[data_b2s(item)] = true; } for(int i = 0; i < cardType.sector_size; i++) @@ -514,7 +514,7 @@ bool Mifare::_writeblk(int blockId, KeyType keyType, const QString& key, const Q } } -void Mifare::write() +void Mifare::writeOne() { int blockId = ui->MF_RW_blockBox->currentText().toInt(); Mifare::KeyType keyType = (Mifare::KeyType)(ui->MF_RW_keyTypeBox->currentData().toInt()); @@ -529,22 +529,19 @@ void Mifare::write() } } -void Mifare::writeAll() +void Mifare::writeSelected(const QList& selectedBlocks) { - for(int i = 0; i < cardType.sector_size; i++) + for(int item : selectedBlocks) { - for(int j = 0; j < cardType.blk[i]; j++) + bool result = false; + result = _writeblk(item, KEY_A, keyAList->at(data_b2s(item)), dataList->at(item)); + if(!result) { - bool result = false; - result = _writeblk(cardType.blks[i] + j, KEY_A, keyAList->at(i), dataList->at(cardType.blks[i] + j)); - if(!result) - { - result = _writeblk(cardType.blks[i] + j, KEY_B, keyBList->at(i), dataList->at(cardType.blks[i] + j)); - } - if(!result) - { - result = _writeblk(cardType.blks[i] + j, KEY_A, "FFFFFFFFFFFF", dataList->at(cardType.blks[i] + j)); - } + result = _writeblk(item, KEY_B, keyBList->at(data_b2s(item)), dataList->at(item)); + } + if(!result) + { + result = _writeblk(item, KEY_A, "FFFFFFFFFFFF", dataList->at(item)); } } } @@ -1190,6 +1187,16 @@ void Mifare::data_fillKeys() data_syncWithKeyWidget(); } +int Mifare::data_b2s(int block) +{ + if(block >= 0 && block < 128) + return block / 4; + else if(block < 256) + return (block - 128) / 16 + 32; + else + return -1; +} + QList Mifare::data_getACBits(const QString& text) //return empty QList if the text is invalid { QString input = text; diff --git a/module/mifare.h b/module/mifare.h index dfa1db5..ecc5e2e 100644 --- a/module/mifare.h +++ b/module/mifare.h @@ -24,10 +24,10 @@ public: void sniff(); void snoop(); void list(); - void read(); + void readOne(); void readSelected(const QList& selectedBlocks); - void write(); - void writeAll(); + void writeOne(); + void writeSelected(const QList& selectedBlocks); void dump(); void restore(); @@ -106,6 +106,7 @@ public: void data_fillKeys(); static QList data_getACBits(const QString &text); + static int data_b2s(int block); public slots: signals: diff --git a/ui/mainwindow.cpp b/ui/mainwindow.cpp index b87a5ae..96390b5 100644 --- a/ui/mainwindow.cpp +++ b/ui/mainwindow.cpp @@ -263,7 +263,7 @@ void MainWindow::on_MF_dataWidget_itemChanged(QTableWidgetItem *item) if(item->column() == 0) { int selectedSectors = 0; - for(int i = 0; i < mifare->cardType.blk[item->row() / 4]; i++) + for(int i = 0; i < mifare->cardType.blk[Mifare::data_b2s(item->row())]; i++) { ui->MF_dataWidget->item(i + item->row(), 1)->setCheckState(item->checkState()); qDebug() << i << mifare->cardType.blk[item->row()] << i + item->row() << ui->MF_dataWidget->item(i + item->row(), 1)->text(); @@ -300,9 +300,9 @@ void MainWindow::on_MF_dataWidget_itemChanged(QTableWidgetItem *item) selectedBlocks++; } } - for(int i = 0; i < mifare->cardType.blk[item->row() / 4]; i++) + for(int i = 0; i < mifare->cardType.blk[Mifare::data_b2s(item->row())]; i++) { - if(ui->MF_dataWidget->item(i + mifare->cardType.blks[item->row() / 4], 1)->checkState() == Qt::Checked) + if(ui->MF_dataWidget->item(i + mifare->cardType.blks[Mifare::data_b2s(item->row())], 1)->checkState() == Qt::Checked) { selectedSubBlocks++; } @@ -321,15 +321,15 @@ void MainWindow::on_MF_dataWidget_itemChanged(QTableWidgetItem *item) } if(selectedSubBlocks == 0) { - ui->MF_dataWidget->item(mifare->cardType.blks[item->row() / 4], 0)->setCheckState(Qt::Unchecked); + ui->MF_dataWidget->item(mifare->cardType.blks[Mifare::data_b2s(item->row())], 0)->setCheckState(Qt::Unchecked); } - else if(selectedSubBlocks == mifare->cardType.blk[item->row() / 4]) + else if(selectedSubBlocks == mifare->cardType.blk[Mifare::data_b2s(item->row())]) { - ui->MF_dataWidget->item(mifare->cardType.blks[item->row() / 4], 0)->setCheckState(Qt::Checked); + ui->MF_dataWidget->item(mifare->cardType.blks[Mifare::data_b2s(item->row())], 0)->setCheckState(Qt::Checked); } else { - ui->MF_dataWidget->item(mifare->cardType.blks[item->row() / 4], 0)->setCheckState(Qt::PartiallyChecked); + ui->MF_dataWidget->item(mifare->cardType.blks[Mifare::data_b2s(item->row())], 0)->setCheckState(Qt::PartiallyChecked); } } else if(item->column() == 2) @@ -502,21 +502,27 @@ void MainWindow::on_MF_RW_readSelectedButton_clicked() void MainWindow::on_MF_RW_readBlockButton_clicked() { setState(false); - mifare->read(); + mifare->readOne(); setState(true); } void MainWindow::on_MF_RW_writeBlockButton_clicked() { setState(false); - mifare->write(); + mifare->writeOne(); setState(true); } -void MainWindow::on_MF_RW_writeAllButton_clicked() +void MainWindow::on_MF_RW_writeSelectedButton_clicked() { setState(false); - mifare->writeAll(); + QList selectedBlocks; + for(int i = 0; i < mifare->cardType.block_size; i++) + { + if(ui->MF_dataWidget->item(i, 1)->checkState() == Qt::Checked) + selectedBlocks.append(i); + } + mifare->writeSelected(selectedBlocks); setState(true); } diff --git a/ui/mainwindow.h b/ui/mainwindow.h index 9427cf2..2b4fec0 100644 --- a/ui/mainwindow.h +++ b/ui/mainwindow.h @@ -85,7 +85,7 @@ private slots: void on_MF_Attack_infoButton_clicked(); - void on_MF_RW_writeAllButton_clicked(); + void on_MF_RW_writeSelectedButton_clicked(); void on_MF_RW_dumpButton_clicked(); diff --git a/ui/mainwindow.ui b/ui/mainwindow.ui index 762b548..ba2bf94 100644 --- a/ui/mainwindow.ui +++ b/ui/mainwindow.ui @@ -638,7 +638,7 @@ - + Write Selected