| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710 |
- #include "mifare_nested_worker_i.h"
- #include "lib/nested/nested.h"
- #include "lib/parity/parity.h"
- #include "lib/nfclegacy/protocols/nfc_util.h"
- #include <storage/storage.h>
- #include <stream/stream.h>
- #include <stream/file_stream.h>
- #include "string.h"
- #define TAG "MifareNestedWorker"
- // possible sum property values
- static uint16_t sums[] =
- {0, 32, 56, 64, 80, 96, 104, 112, 120, 128, 136, 144, 152, 160, 176, 192, 200, 224, 256};
- void mifare_nested_worker_change_state(
- MifareNestedWorker* mifare_nested_worker,
- MifareNestedWorkerState state) {
- furi_assert(mifare_nested_worker);
- mifare_nested_worker->state = state;
- }
- MifareNestedWorker* mifare_nested_worker_alloc() {
- MifareNestedWorker* mifare_nested_worker = malloc(sizeof(MifareNestedWorker));
- // Worker thread attributes
- mifare_nested_worker->thread = furi_thread_alloc_ex(
- "MifareNestedWorker", 8192, mifare_nested_worker_task, mifare_nested_worker);
- mifare_nested_worker->callback = NULL;
- mifare_nested_worker->context = NULL;
- mifare_nested_worker_change_state(mifare_nested_worker, MifareNestedWorkerStateReady);
- return mifare_nested_worker;
- }
- void mifare_nested_worker_free(MifareNestedWorker* mifare_nested_worker) {
- furi_assert(mifare_nested_worker);
- furi_thread_free(mifare_nested_worker->thread);
- free(mifare_nested_worker);
- }
- void mifare_nested_worker_stop(MifareNestedWorker* mifare_nested_worker) {
- furi_assert(mifare_nested_worker);
- mifare_nested_worker_change_state(mifare_nested_worker, MifareNestedWorkerStateStop);
- furi_thread_join(mifare_nested_worker->thread);
- }
- void mifare_nested_worker_start(
- MifareNestedWorker* mifare_nested_worker,
- MifareNestedWorkerState state,
- NfcDeviceData* dev_data,
- MifareNestedWorkerCallback callback,
- void* context) {
- furi_assert(mifare_nested_worker);
- furi_assert(dev_data);
- mifare_nested_worker->callback = callback;
- mifare_nested_worker->context = context;
- mifare_nested_worker->dev_data = dev_data;
- mifare_nested_worker_change_state(mifare_nested_worker, state);
- furi_thread_start(mifare_nested_worker->thread);
- }
- int32_t mifare_nested_worker_task(void* context) {
- MifareNestedWorker* mifare_nested_worker = context;
- if(mifare_nested_worker->state == MifareNestedWorkerStateCheck) {
- mifare_nested_worker_check(mifare_nested_worker);
- } else if(mifare_nested_worker->state == MifareNestedWorkerStateCollectingStatic) {
- mifare_nested_worker_collect_nonces_static(mifare_nested_worker);
- } else if(mifare_nested_worker->state == MifareNestedWorkerStateCollecting) {
- mifare_nested_worker_collect_nonces(mifare_nested_worker);
- } else if(mifare_nested_worker->state == MifareNestedWorkerStateCollectingHard) {
- mifare_nested_worker_collect_nonces_hard(mifare_nested_worker);
- } else if(mifare_nested_worker->state == MifareNestedWorkerStateValidating) {
- mifare_nested_worker_check_keys(mifare_nested_worker);
- }
- mifare_nested_worker_change_state(mifare_nested_worker, MifareNestedWorkerStateReady);
- return 0;
- }
- void mifare_nested_worker_write_uid_string(FurryHalNfcDevData* data, FuriString* string) {
- uint8_t* uid = data->uid;
- uint8_t uid_len = data->uid_len;
- for(size_t i = 0; i < uid_len; i++) {
- uint8_t uid_part = uid[i];
- furi_string_cat_printf(string, "%02X", uid_part);
- }
- }
- void mifare_nested_worker_get_key_cache_file_path(FurryHalNfcDevData* data, FuriString* file_path) {
- furi_string_set(file_path, EXT_PATH("nfc/.cache") "/");
- mifare_nested_worker_write_uid_string(data, file_path);
- furi_string_cat_printf(file_path, ".keys");
- }
- void mifare_nested_worker_get_nonces_file_path(FurryHalNfcDevData* data, FuriString* file_path) {
- furi_string_set(file_path, NESTED_FOLDER "/");
- mifare_nested_worker_write_uid_string(data, file_path);
- furi_string_cat_printf(file_path, ".nonces");
- }
- void mifare_nested_worker_get_found_keys_file_path(FurryHalNfcDevData* data, FuriString* file_path) {
- furi_string_set(file_path, NESTED_FOLDER "/");
- mifare_nested_worker_write_uid_string(data, file_path);
- furi_string_cat_printf(file_path, ".keys");
- }
- void mifare_nested_worker_get_hardnested_folder_path(
- FurryHalNfcDevData* data,
- FuriString* file_path) {
- furi_string_set(file_path, NESTED_FOLDER "/");
- mifare_nested_worker_write_uid_string(data, file_path);
- }
- void mifare_nested_worker_get_hardnested_file_path(
- FurryHalNfcDevData* data,
- FuriString* file_path,
- uint8_t sector,
- uint8_t key_type) {
- mifare_nested_worker_get_hardnested_folder_path(data, file_path);
- furi_string_cat_printf(file_path, "/%u_%u.nonces", sector, key_type);
- }
- uint8_t mifare_nested_worker_get_block_by_sector(uint8_t sector) {
- furi_assert(sector < 40);
- if(sector < 32) {
- return (sector * 4) + 3;
- } else {
- return 32 * 4 + (sector - 32) * 16 + 15;
- }
- }
- static MfClassicSectorTrailer*
- mifare_nested_worker_get_sector_trailer_by_sector(MfClassicData* data, uint8_t sector) {
- return (MfClassicSectorTrailer*)data->block[mifare_nested_worker_get_block_by_sector(sector)]
- .value;
- }
- bool mifare_nested_worker_read_key_cache(FurryHalNfcDevData* data, MfClassicData* mf_data) {
- Storage* storage = furi_record_open(RECORD_STORAGE);
- FuriString* temp_str = furi_string_alloc();
- mifare_nested_worker_get_key_cache_file_path(data, temp_str);
- FlipperFormat* file = flipper_format_file_alloc(storage);
- bool load_success = false;
- uint32_t sector_count = 0;
- do {
- if(storage_common_stat(storage, furi_string_get_cstr(temp_str), NULL) != FSE_OK) break;
- if(!flipper_format_file_open_existing(file, furi_string_get_cstr(temp_str))) break;
- uint32_t version = 0;
- if(!flipper_format_read_header(file, temp_str, &version)) break;
- if(furi_string_cmp_str(temp_str, "Flipper NFC keys")) break;
- if(version != 1) break;
- if(!flipper_format_read_string(file, "Mifare Classic type", temp_str)) break;
- if(!furi_string_cmp(temp_str, "1K")) {
- mf_data->type = MfClassicType1k;
- sector_count = 16;
- } else if(!furi_string_cmp(temp_str, "4K")) {
- mf_data->type = MfClassicType4k;
- sector_count = 40;
- } else if(!furi_string_cmp(temp_str, "MINI")) {
- mf_data->type = MfClassicTypeMini;
- sector_count = 5;
- } else {
- break;
- }
- if(!flipper_format_read_hex_uint64(file, "Key A map", &mf_data->key_a_mask, 1)) break;
- if(!flipper_format_read_hex_uint64(file, "Key B map", &mf_data->key_b_mask, 1)) break;
- bool key_read_success = true;
- for(size_t i = 0; (i < sector_count) && (key_read_success); i++) {
- MfClassicSectorTrailer* sec_tr =
- mifare_nested_worker_get_sector_trailer_by_sector(mf_data, i);
- if(FURI_BIT(mf_data->key_a_mask, i)) {
- furi_string_printf(temp_str, "Key A sector %d", i);
- key_read_success = flipper_format_read_hex(
- file, furi_string_get_cstr(temp_str), sec_tr->key_a, 6);
- }
- if(!key_read_success) break;
- if(FURI_BIT(mf_data->key_b_mask, i)) {
- furi_string_printf(temp_str, "Key B sector %d", i);
- key_read_success = flipper_format_read_hex(
- file, furi_string_get_cstr(temp_str), sec_tr->key_b, 6);
- }
- }
- load_success = key_read_success;
- } while(false);
- furi_string_free(temp_str);
- flipper_format_free(file);
- return load_success;
- }
- bool hex_char_to_hex_nibble(char c, uint8_t* nibble) {
- if((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) {
- if(c <= '9') {
- *nibble = c - '0';
- } else if(c <= 'F') {
- *nibble = c - 'A' + 10;
- } else {
- *nibble = c - 'a' + 10;
- }
- return true;
- } else {
- return false;
- }
- }
- bool hex_char_to_uint8(char hi, char low, uint8_t* value) {
- uint8_t hi_nibble_value, low_nibble_value;
- if(hex_char_to_hex_nibble(hi, &hi_nibble_value) &&
- hex_char_to_hex_nibble(low, &low_nibble_value)) {
- *value = (hi_nibble_value << 4) | low_nibble_value;
- return true;
- } else {
- return false;
- }
- }
- void free_nonces(NonceList_t* nonces, uint8_t sector_count, uint8_t tries_count) {
- for(uint8_t sector = 0; sector < sector_count; sector++) {
- for(uint8_t key_type = 0; key_type < 2; key_type++) {
- for(uint8_t tries = 0; tries < tries_count; tries++) {
- free(nonces->nonces[sector][key_type][tries]);
- }
- }
- }
- }
- MfClassicType mifare_nested_worker_get_tag_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
- UNUSED(ATQA1);
- if((ATQA0 == 0x44 || ATQA0 == 0x04)) {
- if((SAK == 0x08 || SAK == 0x88)) {
- return MfClassicType1k;
- } else if(SAK == 0x09) {
- return MfClassicTypeMini;
- }
- } else if((ATQA0 == 0x01) && (ATQA1 == 0x0F) && (SAK == 0x01)) {
- //skylanders support
- return MfClassicType1k;
- } else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
- return MfClassicType4k;
- }
- return MfClassicType1k;
- }
- uint32_t mifare_nested_worker_predict_delay(
- FurryHalNfcTxRxContext* tx_rx,
- uint8_t blockNo,
- uint8_t keyType,
- uint64_t ui64Key,
- uint32_t tries,
- MifareNestedWorker* mifare_nested_worker) {
- uint32_t cuid = 0;
- Crypto1* crypto = malloc(sizeof(Crypto1));
- uint32_t nt1, nt2, i = 0, previous = 0, prng_delay = 0, zero_prng_value = 65565, repeat = 0;
- if(tries > 25) {
- free(crypto);
- return 2; // Too many tries, fallback to hardnested
- }
- // This part of attack is my attempt to implement it on Flipper.
- // Check README.md for more info
- // First, we find RPNG rounds per 1000 us
- for(uint32_t rtr = 0; rtr < 25; rtr++) {
- if(mifare_nested_worker->state != MifareNestedWorkerStateCollecting) {
- free(crypto);
- return 1;
- }
- nfc_activate();
- if(!furry_hal_nfc_activate_nfca(200, &cuid)) break;
- mifare_classic_authex(crypto, tx_rx, cuid, blockNo, keyType, ui64Key, false, &nt1);
- furi_delay_us(rtr * 1000);
- mifare_classic_authex(crypto, tx_rx, cuid, blockNo, keyType, ui64Key, true, &nt2);
- // Searching for delay, where PRNG will be near 800
- uint32_t nttmp = prng_successor(nt1, 100);
- for(i = 101; i < 65565; i++) {
- nttmp = prng_successor(nttmp, 1);
- if(nttmp == nt2) break;
- }
- if(!rtr) {
- zero_prng_value = i;
- }
- if(previous && i > previous && i != 65565) {
- if(!prng_delay) {
- prng_delay = i - previous;
- } else if(prng_delay - 100 > i - previous && prng_delay + 100 < i - previous) {
- prng_delay += i - previous;
- prng_delay /= 2;
- }
- }
- previous = i;
- FURI_LOG_D(TAG, "Calibrating: ntdist=%lu, delay=%lu", i, rtr * 1000);
- // Let's hope...
- if(i > 810 && i < 840) {
- free(crypto);
- return rtr * 1000;
- }
- }
- FURI_LOG_D(TAG, "PRNG timing: growth ratio per 1000 us = %lu", prng_delay);
- // Next, we try to calculate time until PRNG near 800 with more perfect timing
- // Mifare Classic (weak) RPNG repeats every 65565 PRNG cycles
- if(zero_prng_value == 65565) {
- free(crypto);
- // PRNG isn't pretictable
- return 1;
- }
- uint32_t cycles_to_reset = (65565 - zero_prng_value) / prng_delay;
- uint32_t limit = 7;
- for(uint32_t rtr = cycles_to_reset - 1; rtr < cycles_to_reset + limit; rtr++) {
- for(uint32_t rtz = 0; rtz < 100; rtz++) {
- if(mifare_nested_worker->state != MifareNestedWorkerStateCollecting) {
- free(crypto);
- return 1;
- }
- nfc_activate();
- if(!furry_hal_nfc_activate_nfca(200, &cuid)) break;
- uint32_t delay = rtr * 1000 + rtz * 10;
- mifare_classic_authex(crypto, tx_rx, cuid, blockNo, keyType, ui64Key, false, &nt1);
- furi_delay_us(delay);
- mifare_classic_authex(crypto, tx_rx, cuid, blockNo, keyType, ui64Key, true, &nt2);
- // Searching for delay, where PRNG will be near 800
- uint32_t nttmp = prng_successor(nt1, 0);
- for(i = 1; i < 65565; i++) {
- nttmp = prng_successor(nttmp, 1);
- if(nttmp == nt2) break;
- }
- if(!(i > previous - 50 && i < previous + 50) && rtz) {
- repeat++;
- if(repeat < 5) {
- FURI_LOG_D(TAG, "Invalid RPNG value: ntdist=%lu", i);
- continue;
- }
- }
- if(i > 2000 && i < 65500) {
- uint32_t catch_cycles = (65565 - i) / prng_delay;
- if(catch_cycles > 2) {
- catch_cycles++;
- FURI_LOG_D(
- TAG,
- "Trying a more accurate value: skipping additional %lu us",
- catch_cycles * 1000);
- limit += catch_cycles + 2;
- rtr += catch_cycles;
- }
- }
- FURI_LOG_D(
- TAG,
- "Calibrating: ntdist=%lu, delay=%lu, max=%lu",
- i,
- delay,
- (cycles_to_reset + limit) * 1000);
- repeat = 0;
- previous = i;
- if(i > 810 && i < 840) {
- free(crypto);
- FURI_LOG_I(TAG, "Found delay: %lu us", delay);
- return delay;
- } else if(i > 840 && i < 40000) {
- FURI_LOG_D(TAG, "Trying again: timing lost");
- tries++;
- free(crypto);
- return mifare_nested_worker_predict_delay(
- tx_rx, blockNo, keyType, ui64Key, tries, mifare_nested_worker);
- }
- }
- }
- if(i > 1000 && i < 65000) {
- FURI_LOG_D(TAG, "Trying again: wrong predicted timing");
- tries++;
- free(crypto);
- return mifare_nested_worker_predict_delay(
- tx_rx, blockNo, keyType, ui64Key, tries, mifare_nested_worker);
- }
- free(crypto);
- return 1;
- }
- SaveNoncesResult_t* mifare_nested_worker_write_nonces(
- FurryHalNfcDevData* data,
- Storage* storage,
- NonceList_t* nonces,
- uint8_t tries_count,
- uint8_t free_tries_count,
- uint8_t sector_count,
- uint32_t delay,
- uint32_t distance) {
- FuriString* path = furi_string_alloc();
- Stream* file_stream = file_stream_alloc(storage);
- SaveNoncesResult_t* result = malloc(sizeof(SaveNoncesResult_t));
- result->saved = 0;
- result->invalid = 0;
- result->skipped = 0;
- mifare_nested_worker_get_nonces_file_path(data, path);
- file_stream_open(file_stream, furi_string_get_cstr(path), FSAM_READ_WRITE, FSOM_CREATE_ALWAYS);
- FuriString* header = furi_string_alloc_printf(
- "Filetype: Flipper Nested Nonce Manifest File\nVersion: %s\nNote: you will need desktop app to recover keys: %s\n",
- NESTED_NONCE_FORMAT_VERSION,
- NESTED_RECOVER_KEYS_GITHUB_LINK);
- stream_write_string(file_stream, header);
- for(uint8_t tries = 0; tries < tries_count; tries++) {
- for(uint8_t sector = 0; sector < sector_count; sector++) {
- for(uint8_t key_type = 0; key_type < 2; key_type++) {
- if(nonces->nonces[sector][key_type][tries]->invalid) {
- if(tries == 0) {
- result->invalid++;
- }
- } else if(nonces->nonces[sector][key_type][tries]->skipped) {
- if(tries == 0) {
- result->skipped++;
- }
- } else if(nonces->nonces[sector][key_type][tries]->collected) {
- if(nonces->nonces[sector][key_type][tries]->hardnested) {
- FuriString* hardnested_path = furi_string_alloc();
- mifare_nested_worker_get_hardnested_file_path(
- data, hardnested_path, sector, key_type);
- FuriString* str = furi_string_alloc_printf(
- "HardNested: Key %c cuid 0x%08lx file %s sec %u\n",
- !key_type ? 'A' : 'B',
- nonces->cuid,
- furi_string_get_cstr(hardnested_path),
- sector);
- stream_write_string(file_stream, str);
- furi_string_free(hardnested_path);
- furi_string_free(str);
- } else {
- FuriString* str = furi_string_alloc_printf(
- "Nested: Key %c cuid 0x%08lx", !key_type ? 'A' : 'B', nonces->cuid);
- for(uint8_t type = 0; type < 2; type++) {
- furi_string_cat_printf(
- str,
- " nt%u 0x%08lx ks%u 0x%08lx par%u ",
- type,
- nonces->nonces[sector][key_type][tries]->target_nt[type],
- type,
- nonces->nonces[sector][key_type][tries]->target_ks[type],
- type);
- uint8_t* par = nonces->nonces[sector][key_type][tries]->parity[type];
- for(uint8_t i = 0; i < 4; i++) {
- furi_string_cat_printf(str, "%u", par[i]);
- }
- }
- furi_string_cat_printf(str, " sec %u\n", sector);
- stream_write_string(file_stream, str);
- furi_string_free(str);
- }
- result->saved++;
- }
- }
- }
- }
- if(delay) {
- FuriString* str =
- furi_string_alloc_printf("Nested: Delay %lu, distance %lu", delay, distance);
- stream_write_string(file_stream, str);
- furi_string_free(str);
- }
- free_nonces(nonces, sector_count, free_tries_count);
- file_stream_close(file_stream);
- stream_free(file_stream);
- if(!result->saved) {
- FURI_LOG_E(TAG, "No nonces collected, removing file...");
- if(!storage_simply_remove(storage, furi_string_get_cstr(path))) {
- FURI_LOG_E(TAG, "Failed to remove .nonces file");
- }
- }
- furi_string_free(path);
- furi_record_close(RECORD_STORAGE);
- return result;
- }
- bool mifare_nested_worker_check_initial_keys(
- NonceList_t* nonces,
- MfClassicData* mf_data,
- uint8_t tries_count,
- uint8_t sector_count,
- uint64_t* key,
- uint32_t* key_block,
- uint32_t* found_key_type) {
- bool has_a_key, has_b_key;
- FurryHalNfcTxRxContext tx_rx = {};
- for(uint8_t sector = 0; sector < sector_count; sector++) {
- for(uint8_t key_type = 0; key_type < 2; key_type++) {
- for(uint8_t tries = 0; tries < tries_count; tries++) {
- Nonces* info = malloc(sizeof(Nonces));
- info->key_type = key_type;
- info->block = mifare_nested_worker_get_block_by_sector(sector);
- info->collected = false;
- info->skipped = true;
- info->from_start = false;
- nonces->nonces[sector][key_type][tries] = info;
- }
- }
- }
- for(uint8_t sector = 0; sector < sector_count; sector++) {
- MfClassicSectorTrailer* trailer =
- mifare_nested_worker_get_sector_trailer_by_sector(mf_data, sector);
- has_a_key = FURI_BIT(mf_data->key_a_mask, sector);
- has_b_key = FURI_BIT(mf_data->key_b_mask, sector);
- if(has_a_key) {
- for(uint8_t tries = 0; tries < tries_count; tries++) {
- Nonces* info = nonces->nonces[sector][0][tries];
- info->collected = true;
- info->skipped = true;
- info->from_start = true;
- nonces->nonces[sector][0][tries] = info;
- }
- if(*key_block == 0) {
- uint64_t key_check = nfc_util_bytes2num(trailer->key_a, 6);
- if(nested_check_key(
- &tx_rx, mifare_nested_worker_get_block_by_sector(sector), 0, key_check) ==
- NestedCheckKeyValid) {
- *key = key_check;
- *key_block = mifare_nested_worker_get_block_by_sector(sector);
- *found_key_type = 0;
- }
- }
- }
- if(has_b_key) {
- for(uint8_t tries = 0; tries < tries_count; tries++) {
- Nonces* info = nonces->nonces[sector][1][tries];
- info->collected = true;
- info->skipped = true;
- info->from_start = true;
- nonces->nonces[sector][1][tries] = info;
- }
- if(*key_block == 0) {
- uint64_t key_check = nfc_util_bytes2num(trailer->key_b, 6);
- if(nested_check_key(
- &tx_rx, mifare_nested_worker_get_block_by_sector(sector), 1, key_check) ==
- NestedCheckKeyValid) {
- *key = key_check;
- *key_block = mifare_nested_worker_get_block_by_sector(sector);
- *found_key_type = 1;
- }
- }
- }
- }
- nonces->cuid = 0;
- nonces->hardnested_states = 0;
- nonces->sector_count = sector_count;
- nonces->tries = tries_count;
- return *key_block;
- }
- void mifare_nested_worker_check(MifareNestedWorker* mifare_nested_worker) {
- while(mifare_nested_worker->state == MifareNestedWorkerStateCheck) {
- FurryHalNfcTxRxContext tx_rx = {};
- NfcDevice* dev = mifare_nested_worker->context->nfc_dev;
- MfClassicData* mf_data = &dev->dev_data.mf_classic_data;
- FurryHalNfcDevData data = {};
- MifareNestedNonceType type = MifareNestedNonceNoTag;
- nested_get_data(&data);
- if(mifare_nested_worker_read_key_cache(&data, mf_data)) {
- for(uint8_t sector = 0; sector < 40; sector++) {
- if(FURI_BIT(mf_data->key_a_mask, sector) ||
- FURI_BIT(mf_data->key_b_mask, sector)) {
- type = nested_check_nonce_type(
- &tx_rx, mifare_nested_worker_get_block_by_sector(sector));
- break;
- }
- }
- if(type == MifareNestedNonceNoTag) {
- type = nested_check_nonce_type(&tx_rx, 0);
- }
- } else {
- type = nested_check_nonce_type(&tx_rx, 0);
- }
- if(type == MifareNestedNonceStatic) {
- mifare_nested_worker->context->collecting_type =
- MifareNestedWorkerStateCollectingStatic;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventCollecting, mifare_nested_worker->context);
- break;
- } else if(type == MifareNestedNonceWeak) {
- mifare_nested_worker->context->collecting_type = MifareNestedWorkerStateCollecting;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventCollecting, mifare_nested_worker->context);
- break;
- } else if(type == MifareNestedNonceHard) {
- mifare_nested_worker->context->collecting_type = MifareNestedWorkerStateCollectingHard;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventCollecting, mifare_nested_worker->context);
- break;
- }
- furi_delay_ms(250);
- }
- nfc_deactivate();
- }
- void mifare_nested_worker_collect_nonces_static(MifareNestedWorker* mifare_nested_worker) {
- NonceList_t nonces;
- Storage* storage = furi_record_open(RECORD_STORAGE);
- NfcDevice* dev = mifare_nested_worker->context->nfc_dev;
- MfClassicData* mf_data = &dev->dev_data.mf_classic_data;
- FuriString* folder_path = furi_string_alloc();
- FurryHalNfcDevData data = {};
- nested_get_data(&data);
- MfClassicType type = mifare_nested_worker_get_tag_type(data.atqa[0], data.atqa[1], data.sak);
- uint64_t key = 0; // Found key for attack
- uint32_t found_key_type = 0;
- uint32_t key_block = 0;
- uint32_t sector_count = 0;
- FURI_LOG_I(TAG, "Running Static Nested attack");
- FuriString* tag_info = furi_string_alloc_printf("Tag UID: ");
- mifare_nested_worker_write_uid_string(&data, tag_info);
- FURI_LOG_I(TAG, "%s", furi_string_get_cstr(tag_info));
- furi_string_free(tag_info);
- if(type == MfClassicType4k) {
- sector_count = 40;
- FURI_LOG_I(TAG, "Found Mifare Classic 4K tag");
- } else if(type == MfClassicType1k) {
- sector_count = 16;
- FURI_LOG_I(TAG, "Found Mifare Classic 1K tag");
- } else { // if(type == MfClassicTypeMini)
- sector_count = 5;
- FURI_LOG_I(TAG, "Found Mifare Classic Mini tag");
- }
- furi_string_set(folder_path, NESTED_FOLDER);
- storage_common_mkdir(storage, furi_string_get_cstr(folder_path));
- furi_string_free(folder_path);
- bool ok = mifare_nested_worker_read_key_cache(&data, mf_data);
- if(ok) {
- ok = mifare_nested_worker_check_initial_keys(
- &nonces, mf_data, 1, sector_count, &key, &key_block, &found_key_type);
- if(!ok) {
- free_nonces(&nonces, sector_count, 1);
- }
- }
- if(!ok) {
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNeedKey, mifare_nested_worker->context);
- nfc_deactivate();
- return;
- }
- FURI_LOG_I(
- TAG, "Using %c key for block %lu: %012llX", !found_key_type ? 'A' : 'B', key_block, key);
- while(mifare_nested_worker->state == MifareNestedWorkerStateCollectingStatic) {
- FurryHalNfcTxRxContext tx_rx = {};
- for(uint8_t sector = 0; sector < sector_count; sector++) {
- for(uint8_t key_type = 0; key_type < 2; key_type++) {
- Nonces* info = nonces.nonces[sector][key_type][0];
- if(info->collected) {
- FURI_LOG_I(
- TAG,
- "Skipping sector %u, block %u, key_type: %u as we already have a key",
- sector,
- mifare_nested_worker_get_block_by_sector(sector),
- key_type);
- info->skipped = true;
- nonces.nonces[sector][key_type][0] = info;
- mifare_nested_worker->context->nonces = &nonces;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
- continue;
- }
- if(!nested_check_block(
- &tx_rx, mifare_nested_worker_get_block_by_sector(sector), key_type)) {
- FURI_LOG_E(
- TAG,
- "Skipping sector %u, block %u, key_type: %u as we can't auth on it",
- sector,
- mifare_nested_worker_get_block_by_sector(sector),
- key_type);
- info->invalid = true;
- nonces.nonces[sector][key_type][0] = info;
- mifare_nested_worker->context->nonces = &nonces;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
- continue;
- }
- while(!info->collected) {
- if(mifare_nested_worker->state != MifareNestedWorkerStateCollectingStatic) {
- break;
- }
- struct nonce_info_static result = nested_static_nonce_attack(
- &tx_rx,
- key_block,
- found_key_type,
- mifare_nested_worker_get_block_by_sector(sector),
- key_type,
- key);
- if(result.full) {
- FURI_LOG_I(
- TAG,
- "Accured nonces for sector %u, block %u, key_type: %u",
- sector,
- mifare_nested_worker_get_block_by_sector(sector),
- key_type);
- info = nonces.nonces[sector][key_type][0];
- info->collected = true;
- info->skipped = false;
- memcpy(&info->target_nt, result.target_nt, sizeof(result.target_nt));
- memcpy(&info->target_ks, result.target_ks, sizeof(result.target_ks));
- nonces.nonces[sector][key_type][0] = info;
- nonces.cuid = result.cuid;
- nonces.sector_count = sector_count;
- mifare_nested_worker->context->nonces = &nonces;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
- break;
- } else {
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNoTagDetected, mifare_nested_worker->context);
- }
- }
- }
- }
- break;
- }
- SaveNoncesResult_t* result =
- mifare_nested_worker_write_nonces(&data, storage, &nonces, 1, 1, sector_count, 0, 0);
- if(result->saved) {
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNoncesCollected, mifare_nested_worker->context);
- } else {
- mifare_nested_worker->context->save_state = result;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNoNoncesCollected, mifare_nested_worker->context);
- }
- nfc_deactivate();
- }
- void mifare_nested_worker_collect_nonces_hard(MifareNestedWorker* mifare_nested_worker) {
- NonceList_t nonces;
- Storage* storage = furi_record_open(RECORD_STORAGE);
- NfcDevice* dev = mifare_nested_worker->context->nfc_dev;
- MfClassicData* mf_data = &dev->dev_data.mf_classic_data;
- FuriString* folder_path = furi_string_alloc();
- FurryHalNfcDevData data = {};
- nested_get_data(&data);
- MfClassicType type = mifare_nested_worker_get_tag_type(data.atqa[0], data.atqa[1], data.sak);
- uint64_t key = 0; // Found key for attack
- uint32_t found_key_type = 0;
- uint32_t key_block = 0;
- uint32_t sector_count = 0;
- uint32_t cuid = 0;
- furry_hal_nfc_activate_nfca(200, &cuid);
- FURI_LOG_I(TAG, "Running Hard Nested attack");
- FuriString* tag_info = furi_string_alloc_printf("Tag UID: ");
- mifare_nested_worker_write_uid_string(&data, tag_info);
- FURI_LOG_I(TAG, "%s", furi_string_get_cstr(tag_info));
- furi_string_free(tag_info);
- if(type == MfClassicType4k) {
- sector_count = 40;
- FURI_LOG_I(TAG, "Found Mifare Classic 4K tag");
- } else if(type == MfClassicType1k) {
- sector_count = 16;
- FURI_LOG_I(TAG, "Found Mifare Classic 1K tag");
- } else { // if(type == MfClassicTypeMini)
- sector_count = 5;
- FURI_LOG_I(TAG, "Found Mifare Classic Mini tag");
- }
- furi_string_set(folder_path, NESTED_FOLDER);
- storage_common_mkdir(storage, furi_string_get_cstr(folder_path));
- mifare_nested_worker_get_hardnested_folder_path(&data, folder_path);
- storage_common_mkdir(storage, furi_string_get_cstr(folder_path));
- furi_string_free(folder_path);
- bool ok = mifare_nested_worker_read_key_cache(&data, mf_data);
- if(ok) {
- ok = mifare_nested_worker_check_initial_keys(
- &nonces, mf_data, 1, sector_count, &key, &key_block, &found_key_type);
- if(!ok) {
- free_nonces(&nonces, sector_count, 1);
- }
- }
- if(!ok) {
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNeedKey, mifare_nested_worker->context);
- nfc_deactivate();
- return;
- }
- FURI_LOG_I(
- TAG, "Using %c key for block %lu: %012llX", !found_key_type ? 'A' : 'B', key_block, key);
- FurryHalNfcTxRxContext tx_rx = {};
- nonces.tries = 1;
- nonces.hardnested_states = 0;
- nonces.sector_count = sector_count;
- mifare_nested_worker->context->nonces = &nonces;
- mifare_nested_worker->callback(MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
- mifare_nested_worker->callback(
- MifareNestedWorkerEventHardnestedStatesFound, mifare_nested_worker->context);
- for(uint8_t sector = 0; sector < sector_count &&
- mifare_nested_worker->state == MifareNestedWorkerStateCollectingHard;
- sector++) {
- for(uint8_t key_type = 0;
- key_type < 2 && mifare_nested_worker->state == MifareNestedWorkerStateCollectingHard;
- key_type++) {
- Nonces* info = nonces.nonces[sector][key_type][0];
- if(info->collected) {
- FURI_LOG_I(
- TAG,
- "Skipping sector %u, block %u, key_type: %u as we already have a key",
- sector,
- mifare_nested_worker_get_block_by_sector(sector),
- key_type);
- info->skipped = true;
- nonces.nonces[sector][key_type][0] = info;
- mifare_nested_worker->context->nonces = &nonces;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
- continue;
- }
- if(!nested_check_block(
- &tx_rx, mifare_nested_worker_get_block_by_sector(sector), key_type)) {
- FURI_LOG_E(
- TAG,
- "Skipping sector %u, block %u, key_type: %u as we can't auth on it",
- sector,
- mifare_nested_worker_get_block_by_sector(sector),
- key_type);
- info->invalid = true;
- nonces.nonces[sector][key_type][0] = info;
- mifare_nested_worker->context->nonces = &nonces;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
- continue;
- }
- while(!info->collected &&
- mifare_nested_worker->state == MifareNestedWorkerStateCollectingHard) {
- Stream* file_stream = file_stream_alloc(storage);
- FuriString* hardnested_file = furi_string_alloc();
- mifare_nested_worker_get_hardnested_file_path(
- &data, hardnested_file, sector, key_type);
- file_stream_open(
- file_stream,
- furi_string_get_cstr(hardnested_file),
- FSAM_READ_WRITE,
- FSOM_CREATE_ALWAYS);
- FuriString* header = furi_string_alloc_printf(
- "Filetype: Flipper Nested Nonces File\nVersion: %s\nNote: you will need desktop app to recover keys: %s\nKey %c cuid 0x%08lx sec %u\n",
- NESTED_NONCE_FORMAT_VERSION,
- NESTED_RECOVER_KEYS_GITHUB_LINK,
- !key_type ? 'A' : 'B',
- cuid,
- sector);
- stream_write_string(file_stream, header);
- furi_string_free(header);
- uint32_t first_byte_sum = 0;
- uint32_t* found = malloc(sizeof(uint32_t) * 256);
- for(uint32_t i = 0; i < 256; i++) {
- found[i] = 0;
- }
- while(mifare_nested_worker->state == MifareNestedWorkerStateCollectingHard) {
- struct nonce_info_hard result = nested_hard_nonce_attack(
- &tx_rx,
- key_block,
- found_key_type,
- mifare_nested_worker_get_block_by_sector(sector),
- key_type,
- key,
- found,
- &first_byte_sum,
- file_stream);
- if(result.static_encrypted) {
- file_stream_close(file_stream);
- storage_simply_remove(storage, furi_string_get_cstr(hardnested_file));
- furi_string_free(hardnested_file);
- free(found);
- nfc_deactivate();
- mifare_nested_worker->callback(
- MifareNestedWorkerEventStaticEncryptedNonce,
- mifare_nested_worker->context);
- return;
- }
- if(result.full) {
- uint32_t states = 0;
- for(uint32_t i = 0; i < 256; i++) {
- states += found[i];
- }
- nonces.hardnested_states = states;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventHardnestedStatesFound,
- mifare_nested_worker->context);
- FURI_LOG_D(TAG, "Found states: %lu", states);
- if(states == 256) {
- FURI_LOG_D(
- TAG, "All states collected, first_byte_sum: %lu", first_byte_sum);
- bool valid = false;
- for(uint8_t i = 0; i < sizeof(sums); i++) {
- if(sums[i] == first_byte_sum) {
- valid = true;
- break;
- }
- }
- if(!valid) {
- FURI_LOG_E(TAG, "Invalid first_byte_sum!");
- break;
- }
- info->collected = true;
- info->hardnested = true;
- info->skipped = false;
- nonces.cuid = result.cuid;
- nonces.nonces[sector][key_type][0] = info;
- mifare_nested_worker->context->nonces = &nonces;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
- break;
- }
- } else {
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNoTagDetected, mifare_nested_worker->context);
- }
- }
- free(found);
- furi_string_free(hardnested_file);
- file_stream_close(file_stream);
- }
- }
- }
- SaveNoncesResult_t* result =
- mifare_nested_worker_write_nonces(&data, storage, &nonces, 1, 1, sector_count, 0, 0);
- if(result->saved) {
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNoncesCollected, mifare_nested_worker->context);
- } else {
- mifare_nested_worker->context->save_state = result;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNoNoncesCollected, mifare_nested_worker->context);
- }
- nfc_deactivate();
- }
- void mifare_nested_worker_collect_nonces(MifareNestedWorker* mifare_nested_worker) {
- NonceList_t nonces;
- Storage* storage = furi_record_open(RECORD_STORAGE);
- NfcDevice* dev = mifare_nested_worker->context->nfc_dev;
- MfClassicData* mf_data = &dev->dev_data.mf_classic_data;
- FuriString* folder_path = furi_string_alloc();
- FurryHalNfcDevData data = {};
- nested_get_data(&data);
- MfClassicType type = mifare_nested_worker_get_tag_type(data.atqa[0], data.atqa[1], data.sak);
- uint64_t key = 0; // Found key for attack
- uint32_t found_key_type = 0;
- uint32_t key_block = 0;
- uint32_t sector_count = 0;
- uint32_t delay = 0;
- uint32_t distance = 0;
- uint32_t tries_count = 1;
- FURI_LOG_I(TAG, "Running Nested attack");
- FuriString* tag_info = furi_string_alloc_printf("Tag UID: ");
- mifare_nested_worker_write_uid_string(&data, tag_info);
- FURI_LOG_I(TAG, "%s", furi_string_get_cstr(tag_info));
- furi_string_free(tag_info);
- if(type == MfClassicType4k) {
- sector_count = 40;
- FURI_LOG_I(TAG, "Found Mifare Classic 4K tag");
- } else if(type == MfClassicType1k) {
- sector_count = 16;
- FURI_LOG_I(TAG, "Found Mifare Classic 1K tag");
- } else { // if(type == MfClassicTypeMini)
- sector_count = 5;
- FURI_LOG_I(TAG, "Found Mifare Classic Mini tag");
- }
- furi_string_set(folder_path, NESTED_FOLDER);
- storage_common_mkdir(storage, furi_string_get_cstr(folder_path));
- furi_string_free(folder_path);
- bool ok = mifare_nested_worker_read_key_cache(&data, mf_data);
- if(ok) {
- ok = mifare_nested_worker_check_initial_keys(
- &nonces, mf_data, 3, sector_count, &key, &key_block, &found_key_type);
- if(!ok) {
- free_nonces(&nonces, sector_count, 3);
- }
- }
- if(!ok) {
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNeedKey, mifare_nested_worker->context);
- nfc_deactivate();
- return;
- }
- FURI_LOG_I(
- TAG, "Using %c key for block %lu: %012llX", !found_key_type ? 'A' : 'B', key_block, key);
- while(mifare_nested_worker->state == MifareNestedWorkerStateCollecting) {
- FurryHalNfcTxRxContext tx_rx = {};
- uint32_t first_distance = 0;
- uint32_t second_distance = 0;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventCalibrating, mifare_nested_worker->context);
- distance = nested_calibrate_distance(&tx_rx, key_block, found_key_type, key, delay, false);
- if(mifare_nested_worker->state == MifareNestedWorkerStateCollecting) {
- first_distance =
- nested_calibrate_distance(&tx_rx, key_block, found_key_type, key, delay, true);
- }
- if(mifare_nested_worker->state == MifareNestedWorkerStateCollecting) {
- second_distance =
- nested_calibrate_distance(&tx_rx, key_block, found_key_type, key, 10000, true);
- }
- if(first_distance == 0 && second_distance == 0) {
- nfc_deactivate();
- free_nonces(&nonces, sector_count, 3);
- mifare_nested_worker_change_state(
- mifare_nested_worker, MifareNestedWorkerStateCollectingHard);
- mifare_nested_worker_collect_nonces_hard(mifare_nested_worker);
- return;
- }
- if(first_distance < second_distance - 100 && second_distance > 100) {
- FURI_LOG_E(
- TAG,
- "Discovered tag with PRNG that depends on time. PRNG values: %lu, %lu",
- first_distance,
- second_distance);
- struct distance_info info =
- nested_calibrate_distance_info(&tx_rx, key_block, found_key_type, key);
- if(info.max_prng - info.min_prng > 150) {
- FURI_LOG_W(
- TAG,
- "PRNG is too unpredictable (min/max values more than 150: %lu - %lu = %lu), fallback to delay method",
- info.max_prng,
- info.min_prng,
- info.max_prng - info.min_prng);
- delay = 1;
- } else {
- FURI_LOG_I(
- TAG,
- "PRNG is stable, using method without delay! (May be false positive, still will collect x3 times)");
- distance =
- nested_calibrate_distance(&tx_rx, key_block, found_key_type, key, delay, true);
- delay = 2;
- tries_count = 3;
- }
- }
- if(distance == 0 || delay == 1) {
- bool failed = false;
- // Tag need delay or unpredictable PRNG
- FURI_LOG_W(TAG, "Can't determine distance, trying to find timing...");
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNeedPrediction, mifare_nested_worker->context);
- delay = mifare_nested_worker_predict_delay(
- &tx_rx, key_block, found_key_type, key, 0, mifare_nested_worker);
- if(delay == 1) {
- FURI_LOG_E(TAG, "Can't determine delay");
- // Check that we didn't lost tag
- FurryHalNfcDevData lost_tag_data = {};
- nested_get_data(&lost_tag_data);
- if(lost_tag_data.uid_len == 0) {
- // We lost it.
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNoTagDetected, mifare_nested_worker->context);
- while(mifare_nested_worker->state == MifareNestedWorkerStateCollecting &&
- lost_tag_data.cuid != data.cuid) {
- furi_delay_ms(250);
- nested_get_data(&lost_tag_data);
- }
- mifare_nested_worker->callback(
- MifareNestedWorkerEventCalibrating, mifare_nested_worker->context);
- continue;
- }
- failed = true;
- }
- if(delay == 2) {
- FURI_LOG_E(TAG, "Can't determine delay in 25 tries, fallback to hardnested");
- nfc_deactivate();
- free_nonces(&nonces, sector_count, 3);
- mifare_nested_worker_change_state(
- mifare_nested_worker, MifareNestedWorkerStateCollectingHard);
- mifare_nested_worker_collect_nonces_hard(mifare_nested_worker);
- return;
- }
- if(mifare_nested_worker->state == MifareNestedWorkerStateCollecting && !failed) {
- distance = nested_calibrate_distance(
- &tx_rx, key_block, found_key_type, key, delay, false);
- }
- if(distance == 0 && !failed) {
- FURI_LOG_E(TAG, "Found delay, but can't find distance");
- failed = true;
- }
- if(failed) {
- nfc_deactivate();
- mifare_nested_worker->callback(
- MifareNestedWorkerEventAttackFailed, mifare_nested_worker->context);
- free_nonces(&nonces, sector_count, 3);
- return;
- }
- tries_count = 3;
- }
- mifare_nested_worker->context->nonces = &nonces;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
- for(uint8_t tries = 0; tries < tries_count; tries++) {
- for(uint8_t sector = 0; sector < sector_count; sector++) {
- for(uint8_t key_type = 0; key_type < 2; key_type++) {
- Nonces* info = nonces.nonces[sector][key_type][tries];
- if(info->collected) {
- FURI_LOG_I(
- TAG,
- "Skipping sector %u, block %u, key_type: %u as we already have a key",
- sector,
- mifare_nested_worker_get_block_by_sector(sector),
- key_type);
- info->skipped = true;
- nonces.nonces[sector][key_type][tries] = info;
- mifare_nested_worker->context->nonces = &nonces;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
- continue;
- }
- if(!nested_check_block(
- &tx_rx, mifare_nested_worker_get_block_by_sector(sector), key_type)) {
- FURI_LOG_E(
- TAG,
- "Skipping sector %u, block %u, key_type: %u as we can't auth on it",
- sector,
- mifare_nested_worker_get_block_by_sector(sector),
- key_type);
- info->skipped = true;
- nonces.nonces[sector][key_type][0] = info;
- mifare_nested_worker->context->nonces = &nonces;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
- continue;
- }
- while(!info->collected) {
- if(mifare_nested_worker->state != MifareNestedWorkerStateCollecting) {
- break;
- }
- struct nonce_info result = nested_attack(
- &tx_rx,
- key_block,
- found_key_type,
- mifare_nested_worker_get_block_by_sector(sector),
- key_type,
- key,
- distance,
- delay);
- if(result.full) {
- FURI_LOG_I(
- TAG,
- "Accured nonces for sector %u, block %u, key_type: %u",
- sector,
- mifare_nested_worker_get_block_by_sector(sector),
- key_type);
- info = nonces.nonces[sector][key_type][tries];
- info->collected = true;
- info->skipped = false;
- memcpy(&info->target_nt, result.target_nt, sizeof(result.target_nt));
- memcpy(&info->target_ks, result.target_ks, sizeof(result.target_ks));
- memcpy(&info->parity, result.parity, sizeof(result.parity));
- nonces.nonces[sector][key_type][tries] = info;
- nonces.cuid = result.cuid;
- nonces.sector_count = sector_count;
- nonces.tries = tries_count;
- mifare_nested_worker->context->nonces = &nonces;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
- break;
- } else {
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNoTagDetected,
- mifare_nested_worker->context);
- }
- }
- }
- }
- }
- break;
- }
- SaveNoncesResult_t* result = mifare_nested_worker_write_nonces(
- &data, storage, &nonces, tries_count, 3, sector_count, delay, distance);
- if(result->saved) {
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNoncesCollected, mifare_nested_worker->context);
- } else {
- mifare_nested_worker->context->save_state = result;
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNoNoncesCollected, mifare_nested_worker->context);
- }
- nfc_deactivate();
- }
- bool* mifare_nested_worker_check_keys_exists(
- Storage* storage,
- char* path,
- uint64_t* keys,
- uint32_t key_count,
- MifareNestedWorker* mifare_nested_worker) {
- bool* old_keys = malloc(sizeof(bool) * key_count);
- Stream* file_stream = file_stream_alloc(storage);
- file_stream_open(file_stream, path, FSAM_READ, FSOM_OPEN_ALWAYS);
- FuriString* key_strings[key_count];
- for(uint32_t i = 0; i < key_count; i++) {
- old_keys[i] = false;
- key_strings[i] = furi_string_alloc_printf("%012llX\n", keys[i]);
- }
- while(mifare_nested_worker->state == MifareNestedWorkerStateValidating) {
- FuriString* next_line = furi_string_alloc();
- if(!stream_read_line(file_stream, next_line)) {
- break;
- }
- for(uint32_t i = 0; i < key_count; i++) {
- if(keys[i] == (uint64_t)-1) continue;
- if(furi_string_cmp(next_line, key_strings[i]) == 0) {
- old_keys[i] = true;
- }
- }
- furi_string_free(next_line);
- }
- for(uint32_t i = 0; i < key_count; i++) {
- furi_string_free(key_strings[i]);
- }
- file_stream_close(file_stream);
- stream_free(file_stream);
- return old_keys;
- }
- void mifare_nested_worker_write_key(Storage* storage, FuriString* key) {
- Stream* file_stream = file_stream_alloc(storage);
- file_stream_open(
- file_stream,
- EXT_PATH("nfc/assets/mf_classic_dict_user.nfc"),
- FSAM_READ_WRITE,
- FSOM_OPEN_APPEND);
- stream_write_string(file_stream, key);
- file_stream_close(file_stream);
- }
- void mifare_nested_worker_check_keys(MifareNestedWorker* mifare_nested_worker) {
- KeyInfo_t* key_info = mifare_nested_worker->context->keys;
- Storage* storage = furi_record_open(RECORD_STORAGE);
- Stream* file_stream = file_stream_alloc(storage);
- FuriString* next_line = furi_string_alloc();
- FuriString* path = furi_string_alloc();
- FurryHalNfcDevData data = {};
- nested_get_data(&data);
- MfClassicType type = mifare_nested_worker_get_tag_type(data.atqa[0], data.atqa[1], data.sak);
- NestedCheckKeyResult result = NestedCheckKeyNoTag;
- FurryHalNfcTxRxContext tx_rx = {};
- uint32_t key_count = 0;
- uint32_t sector_key_count = 0;
- uint64_t keys[80];
- bool found_keys[2][40];
- bool unique_keys[2][40];
- uint32_t sector_count = 0;
- if(type == MfClassicType4k) {
- sector_count = 40;
- FURI_LOG_I(TAG, "Found Mifare Classic 4K tag");
- } else if(type == MfClassicType1k) {
- sector_count = 16;
- FURI_LOG_I(TAG, "Found Mifare Classic 1K tag");
- } else { // if(type == MfClassicTypeMini)
- sector_count = 5;
- FURI_LOG_I(TAG, "Found Mifare Classic Mini tag");
- }
- uint32_t keys_count = sector_count * 2;
- for(uint8_t key = 0; key < 2; key++) {
- for(uint8_t i = 0; i < sector_count; i++) {
- found_keys[key][i] = false;
- unique_keys[key][i] = false;
- }
- }
- for(uint8_t i = 0; i < keys_count; i++) {
- keys[i] = -1;
- }
- mifare_nested_worker_get_found_keys_file_path(&data, path);
- if(!file_stream_open(file_stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
- FURI_LOG_E(TAG, "Can't open %s", furi_string_get_cstr(path));
- file_stream_close(file_stream);
- mifare_nested_worker_get_nonces_file_path(&data, path);
- if(!file_stream_open(
- file_stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNeedCollection, mifare_nested_worker->context);
- } else {
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNeedKeyRecovery, mifare_nested_worker->context);
- }
- file_stream_close(file_stream);
- stream_free(file_stream);
- furi_string_free(path);
- furi_string_free(next_line);
- furi_record_close(RECORD_STORAGE);
- return;
- };
- while(true) {
- if(!stream_read_line(file_stream, next_line)) {
- break;
- }
- if(furi_string_start_with_str(next_line, "Key")) {
- uint8_t key_type = furi_string_get_char(next_line, 4) == 'B';
- uint8_t sector = atoi((char[]){furi_string_get_char(next_line, 13)}) * 10 +
- atoi((char[]){furi_string_get_char(next_line, 14)});
- if(!unique_keys[key_type][sector]) {
- unique_keys[key_type][sector] = true;
- sector_key_count++;
- }
- }
- key_count++;
- }
- stream_rewind(file_stream);
- key_info->total_keys = key_count;
- key_info->sector_keys = sector_key_count;
- while(mifare_nested_worker->state == MifareNestedWorkerStateValidating) {
- if(!stream_read_line(file_stream, next_line)) {
- break;
- }
- if(furi_string_start_with_str(next_line, "Key")) {
- // Key X sector XX: XX XX XX XX XX XX
- // 0000000000111111111122222222223333
- // 0123456789012345678901234567890123
- uint8_t keyChar[6];
- uint8_t count = 0;
- uint8_t key_type = furi_string_get_char(next_line, 4) == 'B';
- uint8_t sector = atoi((char[]){furi_string_get_char(next_line, 13)}) * 10 +
- atoi((char[]){furi_string_get_char(next_line, 14)});
- for(uint8_t i = 17; i < 33; i += 3) {
- hex_char_to_uint8(
- furi_string_get_char(next_line, i),
- furi_string_get_char(next_line, i + 1),
- &keyChar[count]);
- count++;
- }
- uint64_t key = nfc_util_bytes2num(keyChar, 6);
- key_info->checked_keys++;
- if(found_keys[key_type][sector]) {
- mifare_nested_worker->callback(
- MifareNestedWorkerEventKeyChecked, mifare_nested_worker->context);
- continue;
- }
- while(mifare_nested_worker->state == MifareNestedWorkerStateValidating) {
- result = nested_check_key(
- &tx_rx, mifare_nested_worker_get_block_by_sector(sector), key_type, key);
- if(result == NestedCheckKeyNoTag) {
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNoTagDetected, mifare_nested_worker->context);
- furi_delay_ms(250);
- } else {
- break;
- }
- }
- if(result == NestedCheckKeyValid) {
- FURI_LOG_I(
- TAG, "Found valid %c key for sector %u: %012llX", key_type, sector, key);
- bool exists = false;
- for(uint8_t i = 0; i < keys_count; i++) {
- if(keys[i] == key) {
- exists = true;
- }
- }
- if(!exists) {
- keys[key_info->found_keys] = key;
- }
- key_info->found_keys++;
- found_keys[key_type][sector] = true;
- }
- mifare_nested_worker->callback(
- MifareNestedWorkerEventKeyChecked, mifare_nested_worker->context);
- }
- }
- furi_string_free(next_line);
- file_stream_close(file_stream);
- stream_free(file_stream);
- mifare_nested_worker->callback(
- MifareNestedWorkerEventProcessingKeys, mifare_nested_worker->context);
- bool* old_keys = mifare_nested_worker_check_keys_exists(
- storage,
- EXT_PATH("nfc/assets/mf_classic_dict_user.nfc"),
- keys,
- keys_count,
- mifare_nested_worker);
- for(uint8_t i = 0; i < keys_count; i++) {
- if(old_keys[i]) {
- keys[i] = -1;
- }
- }
- old_keys = mifare_nested_worker_check_keys_exists(
- storage,
- EXT_PATH("nfc/assets/mf_classic_dict.nfc"),
- keys,
- keys_count,
- mifare_nested_worker);
- for(uint8_t i = 0; i < keys_count; i++) {
- if(old_keys[i]) {
- keys[i] = -1;
- }
- }
- for(uint8_t i = 0; i < keys_count; i++) {
- if(keys[i] == (uint64_t)-1) continue;
- FuriString* key_string = furi_string_alloc_printf("%012llX\n", keys[i]);
- mifare_nested_worker_write_key(storage, key_string);
- FURI_LOG_I(TAG, "Added new key: %s", furi_string_get_cstr(key_string));
- key_info->added_keys++;
- furi_string_free(key_string);
- }
- if(!storage_simply_remove(storage, furi_string_get_cstr(path))) {
- FURI_LOG_E(TAG, "Failed to remove .keys file");
- }
- furi_record_close(RECORD_STORAGE);
- furi_string_free(path);
- mifare_nested_worker->callback(
- MifareNestedWorkerEventKeysFound, mifare_nested_worker->context);
- return;
- }
|