[FL-1396] Mifare Classic read (#1034)

* rfal: add new data exchange function
* core: add FURI_BIT to common defines
* furi_hal_nfc: add data exchange with custom patiry bits
* lib: extend nfc common API
* assets: add mf classic dictionary
* lib: introduce mifare classic library
* nfc: add dictionary reader helper
* nfc worker: add worker events, add mifare classic read
* nfc: rework scenes with worker events
* nfc: add read mifare classic GUI
* nfc device: add mifare classic save
* nfc: add dictionary open fail scene
* nfc: mention resources
* stream: fix stream read line
* subghz: rework file read with fixed stream_read_line
* furi_hal_nfc: decrease communication timeout
* nfc: rework keys load from dictionary with file_stream
* nfc: add read mifare classic suggestion
* nfc: fix mifare classic read view
* nfc: fix index size
* nfc: add switch to no dictionary found scene
* nfc: add mifare classic load
* nfc: improve read mifare classic design
* mifare_classic: add proxmark3 mention
* nfc: format sources
* nfc: fix typos, add documentation
This commit is contained in:
gornekich
2022-03-24 01:14:34 +03:00
committed by GitHub
parent 46a894bc5c
commit eafeefb843
46 changed files with 3113 additions and 111 deletions

View File

@@ -8,4 +8,5 @@ enum NfcCustomEvent {
NfcCustomEventWorkerExit,
NfcCustomEventByteInputDone,
NfcCustomEventTextInputDone,
NfcCustomEventDictAttackDone,
};

View File

@@ -0,0 +1,53 @@
#include "nfc_mf_classic_dict.h"
#include <flipper_format/flipper_format.h>
#include <lib/toolbox/args.h>
#define NFC_MF_CLASSIC_DICT_PATH "/ext/nfc/assets/mf_classic_dict.nfc"
#define NFC_MF_CLASSIC_KEY_LEN (13)
bool nfc_mf_classic_dict_check_presence(Storage* storage) {
furi_assert(storage);
return storage_common_stat(storage, NFC_MF_CLASSIC_DICT_PATH, NULL) == FSE_OK;
}
bool nfc_mf_classic_dict_open_file(Stream* stream) {
furi_assert(stream);
return file_stream_open(stream, NFC_MF_CLASSIC_DICT_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
}
void nfc_mf_classic_dict_close_file(Stream* stream) {
furi_assert(stream);
file_stream_close(stream);
}
bool nfc_mf_classic_dict_get_next_key(Stream* stream, uint64_t* key) {
furi_assert(stream);
furi_assert(key);
uint8_t key_byte_tmp = 0;
string_t next_line;
string_init(next_line);
*key = 0;
bool next_key_read = false;
while(!next_key_read) {
if(stream_read_line(stream, next_line)) break;
if(string_get_char(next_line, 0) == '#') continue;
if(string_size(next_line) != NFC_MF_CLASSIC_KEY_LEN) continue;
for(uint8_t i = 0; i < 12; i += 2) {
args_char_to_hex(
string_get_char(next_line, i), string_get_char(next_line, i + 1), &key_byte_tmp);
*key |= (uint64_t)key_byte_tmp << 8 * (5 - i / 2);
}
next_key_read = true;
}
string_clear(next_line);
return next_key_read;
}
void nfc_mf_classic_dict_reset(Stream* stream) {
furi_assert(stream);
stream_rewind(stream);
}

View File

@@ -0,0 +1,15 @@
#pragma once
#include <stdbool.h>
#include <storage/storage.h>
#include <lib/toolbox/stream/file_stream.h>
bool nfc_mf_classic_dict_check_presence(Storage* storage);
bool nfc_mf_classic_dict_open_file(Stream* stream);
void nfc_mf_classic_dict_close_file(Stream* stream);
bool nfc_mf_classic_dict_get_next_key(Stream* stream, uint64_t* key);
void nfc_mf_classic_dict_reset(Stream* stream);

View File

@@ -79,6 +79,11 @@ Nfc* nfc_alloc() {
view_dispatcher_add_view(
nfc->view_dispatcher, NfcViewBankCard, bank_card_get_view(nfc->bank_card));
// Dict Attack
nfc->dict_attack = dict_attack_alloc();
view_dispatcher_add_view(
nfc->view_dispatcher, NfcViewDictAttack, dict_attack_get_view(nfc->dict_attack));
return nfc;
}
@@ -121,6 +126,10 @@ void nfc_free(Nfc* nfc) {
view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewBankCard);
bank_card_free(nfc->bank_card);
// Dict Attack
view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewDictAttack);
dict_attack_free(nfc->dict_attack);
// Worker
nfc_worker_stop(nfc->worker);
nfc_worker_free(nfc->worker);

View File

@@ -22,13 +22,15 @@ void nfc_device_free(NfcDevice* nfc_dev) {
free(nfc_dev);
}
void nfc_device_prepare_format_string(NfcDevice* dev, string_t format_string) {
static void nfc_device_prepare_format_string(NfcDevice* dev, string_t format_string) {
if(dev->format == NfcDeviceSaveFormatUid) {
string_set_str(format_string, "UID");
} else if(dev->format == NfcDeviceSaveFormatBankCard) {
string_set_str(format_string, "Bank card");
} else if(dev->format == NfcDeviceSaveFormatMifareUl) {
string_set_str(format_string, nfc_mf_ul_type(dev->dev_data.mf_ul_data.type, true));
} else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
string_set_str(format_string, "Mifare Classic");
} else if(dev->format == NfcDeviceSaveFormatMifareDesfire) {
string_set_str(format_string, "Mifare DESFire");
} else {
@@ -36,7 +38,7 @@ void nfc_device_prepare_format_string(NfcDevice* dev, string_t format_string) {
}
}
bool nfc_device_parse_format_string(NfcDevice* dev, string_t format_string) {
static bool nfc_device_parse_format_string(NfcDevice* dev, string_t format_string) {
if(string_start_with_str_p(format_string, "UID")) {
dev->format = NfcDeviceSaveFormatUid;
dev->dev_data.nfc_data.protocol = NfcDeviceProtocolUnknown;
@@ -56,6 +58,11 @@ bool nfc_device_parse_format_string(NfcDevice* dev, string_t format_string) {
return true;
}
}
if(string_start_with_str_p(format_string, "Mifare Classic")) {
dev->format = NfcDeviceSaveFormatMifareClassic;
dev->dev_data.nfc_data.protocol = NfcDeviceProtocolMifareClassic;
return true;
}
if(string_start_with_str_p(format_string, "Mifare DESFire")) {
dev->format = NfcDeviceSaveFormatMifareDesfire;
dev->dev_data.nfc_data.protocol = NfcDeviceProtocolMifareDesfire;
@@ -605,6 +612,79 @@ bool nfc_device_load_bank_card_data(FlipperFormat* file, NfcDevice* dev) {
return parsed;
}
static bool nfc_device_save_mifare_classic_data(FlipperFormat* file, NfcDevice* dev) {
bool saved = false;
MfClassicData* data = &dev->dev_data.mf_classic_data;
string_t temp_str;
string_init(temp_str);
uint16_t blocks = 0;
// Save Mifare Classic specific data
do {
if(!flipper_format_write_comment_cstr(file, "Mifare Classic specific data")) break;
if(data->type == MfClassicType1k) {
if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "1K")) break;
blocks = 64;
} else if(data->type == MfClassicType4k) {
if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "4K")) break;
blocks = 256;
}
if(!flipper_format_write_comment_cstr(file, "Mifare Classic blocks")) break;
bool block_saved = true;
for(size_t i = 0; i < blocks; i++) {
string_printf(temp_str, "Block %d", i);
if(!flipper_format_write_hex(
file, string_get_cstr(temp_str), data->block[i].value, 16)) {
block_saved = false;
break;
}
}
if(!block_saved) break;
saved = true;
} while(false);
string_clear(temp_str);
return saved;
}
static bool nfc_device_load_mifare_classic_data(FlipperFormat* file, NfcDevice* dev) {
bool parsed = false;
MfClassicData* data = &dev->dev_data.mf_classic_data;
string_t temp_str;
string_init(temp_str);
uint16_t data_blocks = 0;
do {
// Read Mifare Classic type
if(!flipper_format_read_string(file, "Mifare Classic type", temp_str)) break;
if(!string_cmp_str(temp_str, "1K")) {
data->type = MfClassicType1k;
data_blocks = 64;
} else if(!string_cmp_str(temp_str, "4K")) {
data->type = MfClassicType4k;
data_blocks = 256;
} else {
break;
}
// Read Mifare Classic blocks
bool block_read = true;
for(size_t i = 0; i < data_blocks; i++) {
string_printf(temp_str, "Block %d", i);
if(!flipper_format_read_hex(
file, string_get_cstr(temp_str), data->block[i].value, 16)) {
block_read = false;
break;
}
}
if(!block_read) break;
parsed = true;
} while(false);
string_clear(temp_str);
return parsed;
}
void nfc_device_set_name(NfcDevice* dev, const char* name) {
furi_assert(dev);
@@ -635,7 +715,7 @@ static bool nfc_device_save_file(
if(!flipper_format_write_header_cstr(file, nfc_file_header, nfc_file_version)) break;
// Write nfc device type
if(!flipper_format_write_comment_cstr(
file, "Nfc device type can be UID, Mifare Ultralight, Bank card"))
file, "Nfc device type can be UID, Mifare Ultralight, Mifare Classic, Bank card"))
break;
nfc_device_prepare_format_string(dev, temp_str);
if(!flipper_format_write_string(file, "Device type", temp_str)) break;
@@ -652,6 +732,8 @@ static bool nfc_device_save_file(
if(!nfc_device_save_mifare_df_data(file, dev)) break;
} else if(dev->format == NfcDeviceSaveFormatBankCard) {
if(!nfc_device_save_bank_card_data(file, dev)) break;
} else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
if(!nfc_device_save_mifare_classic_data(file, dev)) break;
}
saved = true;
} while(0);
@@ -714,6 +796,8 @@ static bool nfc_device_load_data(NfcDevice* dev, string_t path) {
// Parse other data
if(dev->format == NfcDeviceSaveFormatMifareUl) {
if(!nfc_device_load_mifare_ul_data(file, dev)) break;
} else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
if(!nfc_device_load_mifare_classic_data(file, dev)) break;
} else if(dev->format == NfcDeviceSaveFormatMifareDesfire) {
if(!nfc_device_load_mifare_df_data(file, dev)) break;
} else if(dev->format == NfcDeviceSaveFormatBankCard) {

View File

@@ -5,8 +5,9 @@
#include <storage/storage.h>
#include <dialogs/dialogs.h>
#include "mifare_ultralight.h"
#include "mifare_desfire.h"
#include <lib/nfc_protocols/mifare_ultralight.h>
#include <lib/nfc_protocols/mifare_classic.h>
#include <lib/nfc_protocols/mifare_desfire.h>
#define NFC_DEV_NAME_MAX_LEN 22
#define NFC_FILE_NAME_MAX_LEN 120
@@ -27,6 +28,7 @@ typedef enum {
NfcDeviceProtocolUnknown,
NfcDeviceProtocolEMV,
NfcDeviceProtocolMifareUl,
NfcDeviceProtocolMifareClassic,
NfcDeviceProtocolMifareDesfire,
} NfcProtocol;
@@ -34,6 +36,7 @@ typedef enum {
NfcDeviceSaveFormatUid,
NfcDeviceSaveFormatBankCard,
NfcDeviceSaveFormatMifareUl,
NfcDeviceSaveFormatMifareClassic,
NfcDeviceSaveFormatMifareDesfire,
} NfcDeviceSaveFormat;
@@ -69,6 +72,7 @@ typedef struct {
union {
NfcEmvData emv_data;
MifareUlData mf_ul_data;
MfClassicData mf_classic_data;
MifareDesfireData mf_df_data;
};
} NfcDeviceData;

View File

@@ -24,6 +24,7 @@
#include <gui/modules/widget.h>
#include "views/bank_card.h"
#include "views/dict_attack.h"
#include <nfc/scenes/nfc_scene.h>
#include <nfc/helpers/nfc_custom_event.h>
@@ -53,6 +54,7 @@ struct Nfc {
TextBox* text_box;
Widget* widget;
BankCard* bank_card;
DictAttack* dict_attack;
};
typedef enum {
@@ -64,6 +66,7 @@ typedef enum {
NfcViewTextBox,
NfcViewWidget,
NfcViewBankCard,
NfcViewDictAttack,
} NfcView;
Nfc* nfc_alloc();

View File

@@ -53,6 +53,8 @@ const char* nfc_guess_protocol(NfcProtocol protocol) {
return "EMV bank card";
} else if(protocol == NfcDeviceProtocolMifareUl) {
return "Mifare Ultral/NTAG";
} else if(protocol == NfcDeviceProtocolMifareClassic) {
return "Mifare Classic";
} else if(protocol == NfcDeviceProtocolMifareDesfire) {
return "Mifare DESFire";
} else {
@@ -75,3 +77,13 @@ const char* nfc_mf_ul_type(MfUltralightType type, bool full_name) {
return "Mifare Ultralight";
}
}
const char* nfc_mf_classic_type(MfClassicType type) {
if(type == MfClassicType1k) {
return "Mifare Classic 1K";
} else if(type == MfClassicType4k) {
return "Mifare Classic 4K";
} else {
return "Mifare Classic";
}
}

View File

@@ -15,3 +15,5 @@ const char* nfc_get_nfca_type(rfalNfcaListenDeviceType type);
const char* nfc_guess_protocol(NfcProtocol protocol);
const char* nfc_mf_ul_type(MfUltralightType type, bool full_name);
const char* nfc_mf_classic_type(MfClassicType type);

View File

@@ -1,8 +1,13 @@
#include "nfc_worker_i.h"
#include <furi_hal.h>
#include "nfc_protocols/emv_decoder.h"
#include "nfc_protocols/mifare_desfire.h"
#include "nfc_protocols/mifare_ultralight.h"
#include <lib/nfc_protocols/nfc_util.h>
#include <lib/nfc_protocols/emv_decoder.h>
#include <lib/nfc_protocols/mifare_ultralight.h>
#include <lib/nfc_protocols/mifare_classic.h>
#include <lib/nfc_protocols/mifare_desfire.h>
#include "helpers/nfc_mf_classic_dict.h"
#define TAG "NfcWorker"
@@ -20,6 +25,7 @@ NfcWorker* nfc_worker_alloc() {
nfc_worker->callback = NULL;
nfc_worker->context = NULL;
nfc_worker->storage = furi_record_open("storage");
// Initialize rfal
while(furi_hal_nfc_is_busy()) {
@@ -33,6 +39,7 @@ NfcWorker* nfc_worker_alloc() {
void nfc_worker_free(NfcWorker* nfc_worker) {
furi_assert(nfc_worker);
furi_thread_free(nfc_worker->thread);
furi_record_close("storage");
free(nfc_worker);
}
@@ -95,6 +102,8 @@ int32_t nfc_worker_task(void* context) {
nfc_worker_read_mifare_ul(nfc_worker);
} else if(nfc_worker->state == NfcWorkerStateEmulateMifareUl) {
nfc_worker_emulate_mifare_ul(nfc_worker);
} else if(nfc_worker->state == NfcWorkerStateReadMifareClassic) {
nfc_worker_mifare_classic_dict_attack(nfc_worker);
} else if(nfc_worker->state == NfcWorkerStateReadMifareDesfire) {
nfc_worker_read_mifare_desfire(nfc_worker);
} else if(nfc_worker->state == NfcWorkerStateField) {
@@ -130,6 +139,11 @@ void nfc_worker_detect(NfcWorker* nfc_worker) {
dev->dev.nfca.sensRes.platformInfo,
dev->dev.nfca.selRes.sak)) {
result->protocol = NfcDeviceProtocolMifareUl;
} else if(mf_classic_check_card_type(
dev->dev.nfca.sensRes.anticollisionInfo,
dev->dev.nfca.sensRes.platformInfo,
dev->dev.nfca.selRes.sak)) {
result->protocol = NfcDeviceProtocolMifareClassic;
} else if(mf_df_check_card_type(
dev->dev.nfca.sensRes.anticollisionInfo,
dev->dev.nfca.sensRes.platformInfo,
@@ -149,7 +163,7 @@ void nfc_worker_detect(NfcWorker* nfc_worker) {
}
// Notify caller and exit
if(nfc_worker->callback) {
nfc_worker->callback(nfc_worker->context);
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
}
break;
}
@@ -171,7 +185,7 @@ bool nfc_worker_emulate_uid_callback(
if(reader_data->size > 0) {
memcpy(reader_data->data, buff_rx, reader_data->size);
if(nfc_worker->callback) {
nfc_worker->callback(nfc_worker->context);
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
}
}
return true;
@@ -231,7 +245,7 @@ void nfc_worker_read_emv_app(NfcWorker* nfc_worker) {
result->emv_data.aid_len = emv_app.aid_len;
memcpy(result->emv_data.aid, emv_app.aid, emv_app.aid_len);
if(nfc_worker->callback) {
nfc_worker->callback(nfc_worker->context);
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
}
break;
} else {
@@ -329,7 +343,7 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) {
memcpy(result->emv_data.number, emv_app.card_number, emv_app.card_number_len);
// Notify caller and exit
if(nfc_worker->callback) {
nfc_worker->callback(nfc_worker->context);
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
}
break;
} else {
@@ -378,7 +392,7 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) {
}
// Notify caller and exit
if(nfc_worker->callback) {
nfc_worker->callback(nfc_worker->context);
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
}
break;
} else {
@@ -633,7 +647,7 @@ void nfc_worker_read_mifare_ul(NfcWorker* nfc_worker) {
// Notify caller and exit
if(nfc_worker->callback) {
nfc_worker->callback(nfc_worker->context);
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
}
break;
} else {
@@ -663,13 +677,166 @@ void nfc_worker_emulate_mifare_ul(NfcWorker* nfc_worker) {
if(mf_ul_emulate.data_changed) {
nfc_worker->dev_data->mf_ul_data = mf_ul_emulate.data;
if(nfc_worker->callback) {
nfc_worker->callback(nfc_worker->context);
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
}
mf_ul_emulate.data_changed = false;
}
}
}
void nfc_worker_mifare_classic_dict_attack(NfcWorker* nfc_worker) {
furi_assert(nfc_worker->callback);
rfalNfcDevice* dev_list;
rfalNfcDevice* dev;
NfcDeviceCommonData* nfc_common;
uint8_t dev_cnt = 0;
FuriHalNfcTxRxContext tx_rx_ctx = {};
MfClassicAuthContext auth_ctx = {};
MfClassicReader reader = {};
uint64_t curr_key = 0;
uint16_t curr_sector = 0;
uint8_t total_sectors = 0;
NfcWorkerEvent event;
// Open dictionary
nfc_worker->dict_stream = file_stream_alloc(nfc_worker->storage);
if(!nfc_mf_classic_dict_open_file(nfc_worker->dict_stream)) {
event = NfcWorkerEventNoDictFound;
nfc_worker->callback(event, nfc_worker->context);
nfc_mf_classic_dict_close_file(nfc_worker->dict_stream);
stream_free(nfc_worker->dict_stream);
return;
}
// Detect Mifare Classic card
while(nfc_worker->state == NfcWorkerStateReadMifareClassic) {
if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 300, false)) {
dev = &dev_list[0];
if(mf_classic_get_type(
dev->nfcid,
dev->nfcidLen,
dev->dev.nfca.sensRes.anticollisionInfo,
dev->dev.nfca.sensRes.platformInfo,
dev->dev.nfca.selRes.sak,
&reader)) {
total_sectors = mf_classic_get_total_sectors_num(&reader);
if(reader.type == MfClassicType1k) {
event = NfcWorkerEventDetectedClassic1k;
} else {
event = NfcWorkerEventDetectedClassic4k;
}
nfc_worker->callback(event, nfc_worker->context);
break;
}
} else {
event = NfcWorkerEventNoCardDetected;
nfc_worker->callback(event, nfc_worker->context);
}
}
if(nfc_worker->state == NfcWorkerStateReadMifareClassic) {
bool card_removed_notified = false;
bool card_found_notified = false;
// Seek for mifare classic keys
for(curr_sector = 0; curr_sector < total_sectors; curr_sector++) {
FURI_LOG_I(TAG, "Sector: %d ...", curr_sector);
event = NfcWorkerEventNewSector;
nfc_worker->callback(event, nfc_worker->context);
mf_classic_auth_init_context(&auth_ctx, reader.cuid, curr_sector);
bool sector_key_found = false;
while(nfc_mf_classic_dict_get_next_key(nfc_worker->dict_stream, &curr_key)) {
furi_hal_nfc_deactivate();
if(furi_hal_nfc_activate_nfca(300, &reader.cuid)) {
if(!card_found_notified) {
if(reader.type == MfClassicType1k) {
event = NfcWorkerEventDetectedClassic1k;
} else {
event = NfcWorkerEventDetectedClassic4k;
}
nfc_worker->callback(event, nfc_worker->context);
card_found_notified = true;
card_removed_notified = false;
}
FURI_LOG_D(
TAG,
"Try to auth to sector %d with key %04lx%08lx",
curr_sector,
(uint32_t)(curr_key >> 32),
(uint32_t)curr_key);
if(mf_classic_auth_attempt(&tx_rx_ctx, &auth_ctx, curr_key)) {
sector_key_found = true;
if((auth_ctx.key_a != MF_CLASSIC_NO_KEY) &&
(auth_ctx.key_b != MF_CLASSIC_NO_KEY))
break;
}
} else {
// Notify that no tag is availalble
FURI_LOG_D(TAG, "Can't find tags");
if(!card_removed_notified) {
event = NfcWorkerEventNoCardDetected;
nfc_worker->callback(event, nfc_worker->context);
card_removed_notified = true;
card_found_notified = false;
}
}
if(nfc_worker->state != NfcWorkerStateReadMifareClassic) break;
}
if(nfc_worker->state != NfcWorkerStateReadMifareClassic) break;
if(sector_key_found) {
// Notify that keys were found
if(auth_ctx.key_a != MF_CLASSIC_NO_KEY) {
FURI_LOG_I(
TAG,
"Sector %d key A: %04lx%08lx",
curr_sector,
(uint32_t)(auth_ctx.key_a >> 32),
(uint32_t)auth_ctx.key_a);
event = NfcWorkerEventFoundKeyA;
nfc_worker->callback(event, nfc_worker->context);
}
if(auth_ctx.key_b != MF_CLASSIC_NO_KEY) {
FURI_LOG_I(
TAG,
"Sector %d key B: %04lx%08lx",
curr_sector,
(uint32_t)(auth_ctx.key_b >> 32),
(uint32_t)auth_ctx.key_b);
event = NfcWorkerEventFoundKeyB;
nfc_worker->callback(event, nfc_worker->context);
}
// Add sectors to read sequence
mf_classic_reader_add_sector(&reader, curr_sector, auth_ctx.key_a, auth_ctx.key_b);
}
nfc_mf_classic_dict_reset(nfc_worker->dict_stream);
}
}
if(nfc_worker->state == NfcWorkerStateReadMifareClassic) {
FURI_LOG_I(TAG, "Found keys to %d sectors. Start reading sectors", reader.sectors_to_read);
uint8_t sectors_read =
mf_classic_read_card(&tx_rx_ctx, &reader, &nfc_worker->dev_data->mf_classic_data);
if(sectors_read) {
dev = &dev_list[0];
nfc_common = &nfc_worker->dev_data->nfc_data;
nfc_common->uid_len = dev->dev.nfca.nfcId1Len;
nfc_common->atqa[0] = dev->dev.nfca.sensRes.anticollisionInfo;
nfc_common->atqa[1] = dev->dev.nfca.sensRes.platformInfo;
nfc_common->sak = dev->dev.nfca.selRes.sak;
nfc_common->protocol = NfcDeviceProtocolMifareClassic;
memcpy(nfc_common->uid, dev->dev.nfca.nfcId1, nfc_common->uid_len);
event = NfcWorkerEventSuccess;
FURI_LOG_I(TAG, "Successfully read %d sectors", sectors_read);
} else {
event = NfcWorkerEventFail;
FURI_LOG_W(TAG, "Failed to read any sector");
}
nfc_worker->callback(event, nfc_worker->context);
}
nfc_mf_classic_dict_close_file(nfc_worker->dict_stream);
stream_free(nfc_worker->dict_stream);
}
ReturnCode nfc_exchange_full(
uint8_t* tx_buff,
uint16_t tx_len,
@@ -900,7 +1067,7 @@ void nfc_worker_read_mifare_desfire(NfcWorker* nfc_worker) {
// Notify caller and exit
if(nfc_worker->callback) {
nfc_worker->callback(nfc_worker->context);
nfc_worker->callback(NfcWorkerEventSuccess, nfc_worker->context);
}
break;
}

View File

@@ -18,12 +18,27 @@ typedef enum {
NfcWorkerStateField,
NfcWorkerStateReadMifareUl,
NfcWorkerStateEmulateMifareUl,
NfcWorkerStateReadMifareClassic,
NfcWorkerStateReadMifareDesfire,
// Transition
NfcWorkerStateStop,
} NfcWorkerState;
typedef void (*NfcWorkerCallback)(void* context);
typedef enum {
NfcWorkerEventSuccess,
NfcWorkerEventFail,
NfcWorkerEventNoCardDetected,
// Mifare Classic events
NfcWorkerEventNoDictFound,
NfcWorkerEventDetectedClassic1k,
NfcWorkerEventDetectedClassic4k,
NfcWorkerEventNewSector,
NfcWorkerEventFoundKeyA,
NfcWorkerEventFoundKeyB,
NfcWorkerEventStartReading,
} NfcWorkerEvent;
typedef void (*NfcWorkerCallback)(NfcWorkerEvent event, void* context);
NfcWorker* nfc_worker_alloc();

View File

@@ -5,6 +5,7 @@
#include <furi.h>
#include <stdbool.h>
#include <lib/toolbox/stream/file_stream.h>
#include <rfal_analogConfig.h>
#include <rfal_rf.h>
@@ -18,6 +19,8 @@
struct NfcWorker {
FuriThread* thread;
Storage* storage;
Stream* dict_stream;
NfcDeviceData* dev_data;
@@ -45,6 +48,10 @@ void nfc_worker_field(NfcWorker* nfc_worker);
void nfc_worker_read_mifare_ul(NfcWorker* nfc_worker);
void nfc_worker_mifare_classic_dict_attack(NfcWorker* nfc_worker);
void nfc_worker_read_mifare_desfire(NfcWorker* nfc_worker);
void nfc_worker_emulate_mifare_ul(NfcWorker* nfc_worker);
void nfc_worker_emulate_mifare_classic(NfcWorker* nfc_worker);

View File

@@ -8,13 +8,13 @@ enum SubmenuIndex {
};
void nfc_scene_card_menu_submenu_callback(void* context, uint32_t index) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, index);
}
void nfc_scene_card_menu_on_enter(void* context) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
Submenu* submenu = nfc->submenu;
if(nfc->dev->dev_data.nfc_data.protocol > NfcDeviceProtocolUnknown) {
@@ -42,7 +42,8 @@ void nfc_scene_card_menu_on_enter(void* context) {
}
bool nfc_scene_card_menu_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == SubmenuIndexRunApp) {
@@ -54,34 +55,36 @@ bool nfc_scene_card_menu_on_event(void* context, SceneManagerEvent event) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadMifareDesfire);
} else if(nfc->dev->dev_data.nfc_data.protocol == NfcDeviceProtocolEMV) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadEmvApp);
} else if(nfc->dev->dev_data.nfc_data.protocol == NfcDeviceProtocolMifareClassic) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadMifareClassic);
}
return true;
consumed = true;
} else if(event.event == SubmenuIndexChooseScript) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneCardMenu, SubmenuIndexChooseScript);
scene_manager_next_scene(nfc->scene_manager, NfcSceneScriptsMenu);
return true;
consumed = true;
} else if(event.event == SubmenuIndexEmulate) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneCardMenu, SubmenuIndexEmulate);
scene_manager_next_scene(nfc->scene_manager, NfcSceneEmulateUid);
return true;
consumed = true;
} else if(event.event == SubmenuIndexSave) {
scene_manager_set_scene_state(nfc->scene_manager, NfcSceneCardMenu, SubmenuIndexSave);
nfc->dev->format = NfcDeviceSaveFormatUid;
scene_manager_next_scene(nfc->scene_manager, NfcSceneSaveName);
return true;
consumed = true;
}
} else if(event.type == SceneManagerEventTypeBack) {
return scene_manager_search_and_switch_to_previous_scene(
nfc->scene_manager, NfcSceneStart);
consumed =
scene_manager_search_and_switch_to_previous_scene(nfc->scene_manager, NfcSceneStart);
}
return false;
return consumed;
}
void nfc_scene_card_menu_on_exit(void* context) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
submenu_reset(nfc->submenu);
}

View File

@@ -34,3 +34,5 @@ ADD_SCENE(nfc, emulate_apdu_sequence, EmulateApduSequence)
ADD_SCENE(nfc, restore_original, RestoreOriginal)
ADD_SCENE(nfc, debug, Debug)
ADD_SCENE(nfc, field, Field)
ADD_SCENE(nfc, read_mifare_classic, ReadMifareClassic)
ADD_SCENE(nfc, dict_not_found, DictNotFound)

View File

@@ -30,13 +30,19 @@ void nfc_scene_device_info_bank_card_callback(GuiButtonType result, InputType ty
void nfc_scene_device_info_on_enter(void* context) {
Nfc* nfc = context;
bool data_display_supported = (nfc->dev->format == NfcDeviceSaveFormatUid) ||
(nfc->dev->format == NfcDeviceSaveFormatMifareUl) ||
(nfc->dev->format == NfcDeviceSaveFormatMifareDesfire) ||
(nfc->dev->format == NfcDeviceSaveFormatBankCard);
// Setup Custom Widget view
widget_add_text_box_element(
nfc->widget, 0, 0, 128, 22, AlignCenter, AlignTop, nfc->dev->dev_name);
widget_add_button_element(
nfc->widget, GuiButtonTypeLeft, "Back", nfc_scene_device_info_widget_callback, nfc);
widget_add_button_element(
nfc->widget, GuiButtonTypeRight, "Data", nfc_scene_device_info_widget_callback, nfc);
if(data_display_supported) {
widget_add_button_element(
nfc->widget, GuiButtonTypeRight, "Data", nfc_scene_device_info_widget_callback, nfc);
}
char uid_str[32];
NfcDeviceCommonData* data = &nfc->dev->dev_data.nfc_data;
if(data->uid_len == 4) {
@@ -69,6 +75,8 @@ void nfc_scene_device_info_on_enter(void* context) {
protocol_name = nfc_guess_protocol(data->protocol);
} else if(data->protocol == NfcDeviceProtocolMifareUl) {
protocol_name = nfc_mf_ul_type(nfc->dev->dev_data.mf_ul_data.type, false);
} else if(data->protocol == NfcDeviceProtocolMifareClassic) {
protocol_name = nfc_mf_classic_type(nfc->dev->dev_data.mf_classic_data.type);
}
if(protocol_name) {
widget_add_string_element(

View File

@@ -0,0 +1,52 @@
#include "../nfc_i.h"
void nfc_scene_dict_not_found_popup_callback(void* context) {
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventViewExit);
}
void nfc_scene_dict_not_found_on_enter(void* context) {
Nfc* nfc = context;
// Setup view
Popup* popup = nfc->popup;
popup_set_text(
popup,
"Function requires\nan SD card with\nfresh databases.",
82,
24,
AlignCenter,
AlignCenter);
popup_set_icon(popup, 6, 10, &I_SDQuestion_35x43);
popup_set_timeout(popup, 2500);
popup_set_context(popup, nfc);
popup_set_callback(popup, nfc_scene_dict_not_found_popup_callback);
popup_enable_timeout(popup);
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewPopup);
}
bool nfc_scene_dict_not_found_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == NfcCustomEventViewExit) {
if(scene_manager_has_previous_scene(nfc->scene_manager, NfcSceneScriptsMenu)) {
consumed = scene_manager_search_and_switch_to_previous_scene(
nfc->scene_manager, NfcSceneScriptsMenu);
} else if(scene_manager_has_previous_scene(nfc->scene_manager, NfcSceneCardMenu)) {
consumed = scene_manager_search_and_switch_to_previous_scene(
nfc->scene_manager, NfcSceneCardMenu);
} else {
consumed = scene_manager_search_and_switch_to_previous_scene(
nfc->scene_manager, NfcSceneStart);
}
}
}
return consumed;
}
void nfc_scene_dict_not_found_on_exit(void* context) {
Nfc* nfc = context;
popup_reset(nfc->popup);
}

View File

@@ -4,7 +4,7 @@
#define NFC_MF_UL_DATA_NOT_CHANGED (0UL)
#define NFC_MF_UL_DATA_CHANGED (1UL)
void nfc_emulate_mifare_ul_worker_callback(void* context) {
void nfc_emulate_mifare_ul_worker_callback(NfcWorkerEvent event, void* context) {
Nfc* nfc = (Nfc*)context;
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneEmulateMifareUl, NFC_MF_UL_DATA_CHANGED);

View File

@@ -6,7 +6,7 @@ enum {
NfcSceneEmulateUidStateTextBox,
};
void nfc_emulate_uid_worker_callback(void* context) {
void nfc_emulate_uid_worker_callback(NfcWorkerEvent event, void* context) {
furi_assert(context);
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventWorkerExit);

View File

@@ -1,7 +1,7 @@
#include "../nfc_i.h"
#include <dolphin/dolphin.h>
void nfc_read_card_worker_callback(void* context) {
void nfc_read_card_worker_callback(NfcWorkerEvent event, void* context) {
Nfc* nfc = (Nfc*)context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventWorkerExit);
}

View File

@@ -1,7 +1,7 @@
#include "../nfc_i.h"
#include <dolphin/dolphin.h>
void nfc_read_emv_app_worker_callback(void* context) {
void nfc_read_emv_app_worker_callback(NfcWorkerEvent event, void* context) {
Nfc* nfc = (Nfc*)context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventWorkerExit);
}

View File

@@ -1,7 +1,7 @@
#include "../nfc_i.h"
#include <dolphin/dolphin.h>
void nfc_read_emv_data_worker_callback(void* context) {
void nfc_read_emv_data_worker_callback(NfcWorkerEvent event, void* context) {
Nfc* nfc = (Nfc*)context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventWorkerExit);
}

View File

@@ -0,0 +1,95 @@
#include "../nfc_i.h"
enum {
NfcSceneReadMifareClassicStateInProgress,
NfcSceneReadMifareClassicStateDone,
};
void nfc_read_mifare_classic_worker_callback(NfcWorkerEvent event, void* context) {
furi_assert(context);
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, event);
}
void nfc_read_mifare_classic_dict_attack_result_callback(void* context) {
furi_assert(context);
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventDictAttackDone);
}
void nfc_scene_read_mifare_classic_on_enter(void* context) {
Nfc* nfc = context;
// Setup and start worker
memset(&nfc->dev->dev_data.mf_classic_data, 0, sizeof(MfClassicData));
dict_attack_set_result_callback(
nfc->dict_attack, nfc_read_mifare_classic_dict_attack_result_callback, nfc);
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneReadMifareClassic, NfcSceneReadMifareClassicStateInProgress);
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewDictAttack);
nfc_worker_start(
nfc->worker,
NfcWorkerStateReadMifareClassic,
&nfc->dev->dev_data,
nfc_read_mifare_classic_worker_callback,
nfc);
}
bool nfc_scene_read_mifare_classic_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = context;
bool consumed = false;
uint32_t state = scene_manager_get_scene_state(nfc->scene_manager, NfcSceneReadMifareClassic);
if(event.type == SceneManagerEventTypeTick) {
if(state == NfcSceneReadMifareClassicStateInProgress) {
notification_message(nfc->notifications, &sequence_blink_blue_10);
}
consumed = true;
} else if(event.type == SceneManagerEventTypeCustom) {
if(event.event == NfcCustomEventDictAttackDone) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneSaveName);
consumed = true;
} else if(event.event == NfcWorkerEventDetectedClassic1k) {
dict_attack_card_detected(nfc->dict_attack, MfClassicType1k);
consumed = true;
} else if(event.event == NfcWorkerEventDetectedClassic4k) {
dict_attack_card_detected(nfc->dict_attack, MfClassicType4k);
consumed = true;
} else if(event.event == NfcWorkerEventNewSector) {
dict_attack_inc_curr_sector(nfc->dict_attack);
consumed = true;
} else if(event.event == NfcWorkerEventFoundKeyA) {
dict_attack_inc_found_key(nfc->dict_attack, MfClassicKeyA);
consumed = true;
} else if(event.event == NfcWorkerEventFoundKeyB) {
dict_attack_inc_found_key(nfc->dict_attack, MfClassicKeyB);
consumed = true;
} else if(event.event == NfcWorkerEventNoCardDetected) {
dict_attack_card_removed(nfc->dict_attack);
consumed = true;
} else if(event.event == NfcWorkerEventSuccess) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneReadMifareClassic, NfcSceneReadMifareClassicStateDone);
notification_message(nfc->notifications, &sequence_success);
nfc->dev->format = NfcDeviceSaveFormatMifareClassic;
dict_attack_set_result(nfc->dict_attack, true);
consumed = true;
} else if(event.event == NfcWorkerEventFail) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneReadMifareClassic, NfcSceneReadMifareClassicStateDone);
dict_attack_set_result(nfc->dict_attack, false);
consumed = true;
} else if(event.event == NfcWorkerEventNoDictFound) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneDictNotFound);
consumed = true;
}
}
return consumed;
}
void nfc_scene_read_mifare_classic_on_exit(void* context) {
Nfc* nfc = context;
// Stop worker
nfc_worker_stop(nfc->worker);
dict_attack_reset(nfc->dict_attack);
}

View File

@@ -1,7 +1,7 @@
#include "../nfc_i.h"
#include <dolphin/dolphin.h>
void nfc_read_mifare_desfire_worker_callback(void* context) {
void nfc_read_mifare_desfire_worker_callback(NfcWorkerEvent event, void* context) {
Nfc* nfc = (Nfc*)context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventWorkerExit);
}

View File

@@ -1,13 +1,13 @@
#include "../nfc_i.h"
#include <dolphin/dolphin.h>
void nfc_read_mifare_ul_worker_callback(void* context) {
Nfc* nfc = (Nfc*)context;
void nfc_read_mifare_ul_worker_callback(NfcWorkerEvent event, void* context) {
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventWorkerExit);
}
void nfc_scene_read_mifare_ul_on_enter(void* context) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
DOLPHIN_DEED(DolphinDeedNfcRead);
// Setup view
@@ -26,29 +26,25 @@ void nfc_scene_read_mifare_ul_on_enter(void* context) {
}
bool nfc_scene_read_mifare_ul_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == NfcCustomEventWorkerExit) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadMifareUlSuccess);
return true;
consumed = true;
}
} else if(event.type == SceneManagerEventTypeTick) {
notification_message(nfc->notifications, &sequence_blink_blue_10);
return true;
consumed = true;
}
return false;
return consumed;
}
void nfc_scene_read_mifare_ul_on_exit(void* context) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
// Stop worker
nfc_worker_stop(nfc->worker);
// Clear view
Popup* popup = nfc->popup;
popup_set_header(popup, NULL, 0, 0, AlignCenter, AlignBottom);
popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
popup_set_icon(popup, 0, 0, NULL);
popup_reset(nfc->popup);
}

View File

@@ -3,17 +3,17 @@
enum SubmenuIndex {
SubmenuIndexBankCard,
SubmenuIndexMifareUltralight,
SubmenuIdexReadMfClassic,
SubmenuIndexMifareDesfire,
};
void nfc_scene_scripts_menu_submenu_callback(void* context, uint32_t index) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, index);
}
void nfc_scene_scripts_menu_on_enter(void* context) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
Submenu* submenu = nfc->submenu;
submenu_add_item(
@@ -28,6 +28,12 @@ void nfc_scene_scripts_menu_on_enter(void* context) {
SubmenuIndexMifareUltralight,
nfc_scene_scripts_menu_submenu_callback,
nfc);
submenu_add_item(
submenu,
"Read Mifare Classic",
SubmenuIdexReadMfClassic,
nfc_scene_scripts_menu_submenu_callback,
nfc);
submenu_add_item(
submenu,
"Read Mifare DESFire",
@@ -40,32 +46,37 @@ void nfc_scene_scripts_menu_on_enter(void* context) {
}
bool nfc_scene_scripts_menu_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == SubmenuIndexBankCard) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneScriptsMenu, SubmenuIndexBankCard);
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadEmvApp);
return true;
consumed = true;
} else if(event.event == SubmenuIndexMifareUltralight) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneScriptsMenu, SubmenuIndexMifareUltralight);
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadMifareUl);
return true;
consumed = true;
} else if(event.event == SubmenuIdexReadMfClassic) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneScriptsMenu, SubmenuIdexReadMfClassic);
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadMifareClassic);
consumed = true;
} else if(event.event == SubmenuIndexMifareDesfire) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneScriptsMenu, SubmenuIndexMifareDesfire);
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadMifareDesfire);
return true;
consumed = true;
}
}
return false;
return consumed;
}
void nfc_scene_scripts_menu_on_exit(void* context) {
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
submenu_reset(nfc->submenu);
}

View File

@@ -49,21 +49,15 @@ bool nfc_scene_start_on_event(void* context, SceneManagerEvent event) {
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == SubmenuIndexRead) {
scene_manager_set_scene_state(nfc->scene_manager, NfcSceneStart, SubmenuIndexRead);
scene_manager_next_scene(nfc->scene_manager, NfcSceneReadCard);
consumed = true;
} else if(event.event == SubmenuIndexRunScript) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneStart, SubmenuIndexRunScript);
scene_manager_next_scene(nfc->scene_manager, NfcSceneScriptsMenu);
consumed = true;
} else if(event.event == SubmenuIndexSaved) {
scene_manager_set_scene_state(nfc->scene_manager, NfcSceneStart, SubmenuIndexSaved);
scene_manager_next_scene(nfc->scene_manager, NfcSceneFileSelect);
consumed = true;
} else if(event.event == SubmenuIndexAddManualy) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneStart, SubmenuIndexAddManualy);
scene_manager_next_scene(nfc->scene_manager, NfcSceneSetType);
consumed = true;
} else if(event.event == SubmenuIndexDebug) {
@@ -71,6 +65,7 @@ bool nfc_scene_start_on_event(void* context, SceneManagerEvent event) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneDebug);
consumed = true;
}
scene_manager_set_scene_state(nfc->scene_manager, NfcSceneStart, event.event);
}
return consumed;
}

View File

@@ -0,0 +1,194 @@
#include "dict_attack.h"
#include <m-string.h>
#include <gui/elements.h>
typedef enum {
DictAttackStateSearchCard,
DictAttackStateSearchKeys,
DictAttackStateCardRemoved,
DictAttackStateSuccess,
DictAttackStateFail,
} DictAttackState;
struct DictAttack {
View* view;
DictAttackResultCallback callback;
void* context;
};
typedef struct {
DictAttackState state;
MfClassicType type;
uint8_t current_sector;
uint8_t total_sectors;
uint8_t keys_a_found;
uint8_t keys_a_total;
uint8_t keys_b_found;
uint8_t keys_b_total;
} DictAttackViewModel;
static void dict_attack_draw_callback(Canvas* canvas, void* model) {
DictAttackViewModel* m = model;
if(m->state == DictAttackStateSearchCard) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(
canvas, 64, 32, AlignCenter, AlignCenter, "Detecting Mifare Classic");
} else if(m->state == DictAttackStateCardRemoved) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(
canvas, 64, 32, AlignCenter, AlignTop, "Place card back to flipper");
} else {
char draw_str[32];
if(m->state == DictAttackStateSearchKeys) {
snprintf(
draw_str, sizeof(draw_str), "Searching keys for sector %d", m->current_sector);
canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, draw_str);
} else if(m->state == DictAttackStateSuccess) {
canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, "Complete!");
elements_button_right(canvas, "Save");
} else if(m->state == DictAttackStateFail) {
canvas_draw_str_aligned(
canvas, 64, 2, AlignCenter, AlignTop, "Failed to read any sector");
}
uint16_t keys_found = m->keys_a_found + m->keys_b_found;
uint16_t keys_total = m->keys_a_total + m->keys_b_total;
float progress = (float)(m->current_sector) / (float)(m->total_sectors);
elements_progress_bar(canvas, 5, 12, 120, progress);
canvas_set_font(canvas, FontSecondary);
snprintf(draw_str, sizeof(draw_str), "Total keys found: %d/%d", keys_found, keys_total);
canvas_draw_str_aligned(canvas, 1, 23, AlignLeft, AlignTop, draw_str);
snprintf(
draw_str, sizeof(draw_str), "A keys found: %d/%d", m->keys_a_found, m->keys_a_total);
canvas_draw_str_aligned(canvas, 1, 34, AlignLeft, AlignTop, draw_str);
snprintf(
draw_str, sizeof(draw_str), "B keys found: %d/%d", m->keys_b_found, m->keys_b_total);
canvas_draw_str_aligned(canvas, 1, 45, AlignLeft, AlignTop, draw_str);
}
}
static bool dict_attack_input_callback(InputEvent* event, void* context) {
DictAttack* dict_attack = context;
bool consumed = false;
DictAttackState state;
with_view_model(
dict_attack->view, (DictAttackViewModel * model) {
state = model->state;
return false;
});
if(state == DictAttackStateSuccess && event->type == InputTypeShort &&
event->key == InputKeyRight) {
if(dict_attack->callback) {
dict_attack->callback(dict_attack->context);
}
consumed = true;
}
return consumed;
}
DictAttack* dict_attack_alloc() {
DictAttack* dict_attack = malloc(sizeof(DictAttack));
dict_attack->view = view_alloc();
view_allocate_model(dict_attack->view, ViewModelTypeLocking, sizeof(DictAttackViewModel));
view_set_draw_callback(dict_attack->view, dict_attack_draw_callback);
view_set_input_callback(dict_attack->view, dict_attack_input_callback);
view_set_context(dict_attack->view, dict_attack);
return dict_attack;
}
void dict_attack_free(DictAttack* dict_attack) {
furi_assert(dict_attack);
view_free(dict_attack->view);
free(dict_attack);
}
void dict_attack_reset(DictAttack* dict_attack) {
furi_assert(dict_attack);
with_view_model(
dict_attack->view, (DictAttackViewModel * model) {
memset(model, 0, sizeof(DictAttackViewModel));
return true;
});
}
View* dict_attack_get_view(DictAttack* dict_attack) {
furi_assert(dict_attack);
return dict_attack->view;
}
void dict_attack_set_result_callback(
DictAttack* dict_attack,
DictAttackResultCallback callback,
void* context) {
furi_assert(dict_attack);
furi_assert(callback);
dict_attack->callback = callback;
dict_attack->context = context;
}
void dict_attack_card_detected(DictAttack* dict_attack, MfClassicType type) {
furi_assert(dict_attack);
with_view_model(
dict_attack->view, (DictAttackViewModel * model) {
model->state = DictAttackStateSearchKeys;
if(type == MfClassicType1k) {
model->total_sectors = 16;
model->keys_a_total = 16;
model->keys_b_total = 16;
} else if(type == MfClassicType4k) {
model->total_sectors = 40;
model->keys_a_total = 40;
model->keys_b_total = 40;
}
return true;
});
}
void dict_attack_card_removed(DictAttack* dict_attack) {
furi_assert(dict_attack);
with_view_model(
dict_attack->view, (DictAttackViewModel * model) {
if(model->state == DictAttackStateSearchKeys) {
model->state = DictAttackStateCardRemoved;
} else {
model->state = DictAttackStateSearchCard;
}
return true;
});
}
void dict_attack_inc_curr_sector(DictAttack* dict_attack) {
furi_assert(dict_attack);
with_view_model(
dict_attack->view, (DictAttackViewModel * model) {
model->current_sector++;
return true;
});
}
void dict_attack_inc_found_key(DictAttack* dict_attack, MfClassicKey key) {
furi_assert(dict_attack);
with_view_model(
dict_attack->view, (DictAttackViewModel * model) {
model->state = DictAttackStateSearchKeys;
if(key == MfClassicKeyA) {
model->keys_a_found++;
} else if(key == MfClassicKeyB) {
model->keys_b_found++;
}
return true;
});
}
void dict_attack_set_result(DictAttack* dict_attack, bool success) {
furi_assert(dict_attack);
with_view_model(
dict_attack->view, (DictAttackViewModel * model) {
if(success) {
model->state = DictAttackStateSuccess;
} else {
model->state = DictAttackStateFail;
}
return true;
});
}

View File

@@ -0,0 +1,33 @@
#pragma once
#include <stdint.h>
#include <gui/view.h>
#include <gui/modules/widget.h>
#include <lib/nfc_protocols/mifare_classic.h>
typedef struct DictAttack DictAttack;
typedef void (*DictAttackResultCallback)(void* context);
DictAttack* dict_attack_alloc();
void dict_attack_free(DictAttack* dict_attack);
void dict_attack_reset(DictAttack* dict_attack);
View* dict_attack_get_view(DictAttack* dict_attack);
void dict_attack_set_result_callback(
DictAttack* dict_attack,
DictAttackResultCallback callback,
void* context);
void dict_attack_card_detected(DictAttack* dict_attack, MfClassicType type);
void dict_attack_card_removed(DictAttack* dict_attack);
void dict_attack_inc_curr_sector(DictAttack* dict_attack);
void dict_attack_inc_found_key(DictAttack* dict_attack, MfClassicKey key);
void dict_attack_set_result(DictAttack* dict_attack, bool success);