mifare_nested_worker.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. #include "mifare_nested_worker_i.h"
  2. #include "lib/nested/nested.h"
  3. #include <lib/nfc/protocols/nfc_util.h>
  4. #include <storage/storage.h>
  5. #include <stream/stream.h>
  6. #include <stream/buffered_file_stream.h>
  7. #include "string.h"
  8. #include <furi.h>
  9. #include <furi_hal.h>
  10. #define TAG "MifareNestedWorker"
  11. void mifare_nested_worker_change_state(
  12. MifareNestedWorker* mifare_nested_worker,
  13. MifareNestedWorkerState state) {
  14. furi_assert(mifare_nested_worker);
  15. mifare_nested_worker->state = state;
  16. }
  17. MifareNestedWorker* mifare_nested_worker_alloc() {
  18. MifareNestedWorker* mifare_nested_worker = malloc(sizeof(MifareNestedWorker));
  19. // Worker thread attributes
  20. mifare_nested_worker->thread = furi_thread_alloc_ex(
  21. "MifareNestedWorker", 8192, mifare_nested_worker_task, mifare_nested_worker);
  22. mifare_nested_worker->callback = NULL;
  23. mifare_nested_worker->context = NULL;
  24. mifare_nested_worker_change_state(mifare_nested_worker, MifareNestedWorkerStateReady);
  25. return mifare_nested_worker;
  26. }
  27. void mifare_nested_worker_free(MifareNestedWorker* mifare_nested_worker) {
  28. furi_assert(mifare_nested_worker);
  29. furi_thread_free(mifare_nested_worker->thread);
  30. free(mifare_nested_worker);
  31. }
  32. void mifare_nested_worker_stop(MifareNestedWorker* mifare_nested_worker) {
  33. furi_assert(mifare_nested_worker);
  34. mifare_nested_worker_change_state(mifare_nested_worker, MifareNestedWorkerStateStop);
  35. furi_thread_join(mifare_nested_worker->thread);
  36. }
  37. void mifare_nested_worker_start(
  38. MifareNestedWorker* mifare_nested_worker,
  39. MifareNestedWorkerState state,
  40. NfcDeviceData* dev_data,
  41. MifareNestedWorkerCallback callback,
  42. void* context) {
  43. furi_assert(mifare_nested_worker);
  44. furi_assert(dev_data);
  45. mifare_nested_worker->callback = callback;
  46. mifare_nested_worker->context = context;
  47. mifare_nested_worker->dev_data = dev_data;
  48. mifare_nested_worker_change_state(mifare_nested_worker, state);
  49. furi_thread_start(mifare_nested_worker->thread);
  50. }
  51. int32_t mifare_nested_worker_task(void* context) {
  52. MifareNestedWorker* mifare_nested_worker = context;
  53. if(mifare_nested_worker->state == MifareNestedWorkerStateCheck) {
  54. mifare_nested_worker_check(mifare_nested_worker);
  55. } else if(mifare_nested_worker->state == MifareNestedWorkerStateCollectingStatic) {
  56. mifare_nested_worker_collect_nonces_static(mifare_nested_worker);
  57. } else if(mifare_nested_worker->state == MifareNestedWorkerStateCollecting) {
  58. mifare_nested_worker_collect_nonces(mifare_nested_worker);
  59. } else if(mifare_nested_worker->state == MifareNestedWorkerStateValidating) {
  60. mifare_nested_worker_check_keys(mifare_nested_worker);
  61. }
  62. mifare_nested_worker_change_state(mifare_nested_worker, MifareNestedWorkerStateReady);
  63. return 0;
  64. }
  65. void mifare_nested_worker_check(MifareNestedWorker* mifare_nested_worker) {
  66. bool tag_found_notified = false;
  67. while(mifare_nested_worker->state == MifareNestedWorkerStateCheck) {
  68. FuriHalNfcTxRxContext tx_rx = {};
  69. MifareNestedNonceType type = nested_check_nonce_type(&tx_rx);
  70. if(type == MifareNestedNonceStatic) {
  71. mifare_nested_worker->context->collecting_type =
  72. MifareNestedWorkerStateCollectingStatic;
  73. if(!tag_found_notified) {
  74. mifare_nested_worker->callback(
  75. MifareNestedWorkerEventCollecting, mifare_nested_worker->context);
  76. tag_found_notified = true;
  77. }
  78. mifare_nested_worker->callback(
  79. MifareNestedWorkerEventCollecting, mifare_nested_worker->context);
  80. break;
  81. } else if(type == MifareNestedNonce) {
  82. mifare_nested_worker->context->collecting_type = MifareNestedWorkerStateCollecting;
  83. if(!tag_found_notified) {
  84. mifare_nested_worker->callback(
  85. MifareNestedWorkerEventCollecting, mifare_nested_worker->context);
  86. tag_found_notified = true;
  87. }
  88. mifare_nested_worker->callback(
  89. MifareNestedWorkerEventCollecting, mifare_nested_worker->context);
  90. break;
  91. }
  92. furi_delay_ms(250);
  93. }
  94. nfc_deactivate();
  95. }
  96. void mifare_nested_worker_write_uid_string(FuriHalNfcDevData* data, FuriString* string) {
  97. uint8_t* uid = data->uid;
  98. uint8_t uid_len = data->uid_len;
  99. for(size_t i = 0; i < uid_len; i++) {
  100. uint8_t uid_part = uid[i];
  101. furi_string_cat_printf(string, "%02X", uid_part);
  102. }
  103. }
  104. void mifare_nested_worker_get_key_cache_file_path(FuriHalNfcDevData* data, FuriString* file_path) {
  105. furi_string_set(file_path, EXT_PATH("nfc/.cache") "/");
  106. mifare_nested_worker_write_uid_string(data, file_path);
  107. furi_string_cat_printf(file_path, ".keys");
  108. }
  109. void mifare_nested_worker_get_nonces_file_path(FuriHalNfcDevData* data, FuriString* file_path) {
  110. furi_string_set(file_path, NESTED_FOLDER "/");
  111. mifare_nested_worker_write_uid_string(data, file_path);
  112. furi_string_cat_printf(file_path, ".nonces");
  113. }
  114. void mifare_nested_worker_get_found_keys_file_path(FuriHalNfcDevData* data, FuriString* file_path) {
  115. furi_string_set(file_path, NESTED_FOLDER "/");
  116. mifare_nested_worker_write_uid_string(data, file_path);
  117. furi_string_cat_printf(file_path, ".keys");
  118. }
  119. uint8_t mifare_nested_worker_get_block_by_sector(uint8_t sector) {
  120. furi_assert(sector < 40);
  121. if(sector < 32) {
  122. return (sector * 4) + 3;
  123. } else {
  124. return 32 * 4 + (sector - 32) * 16 + 15;
  125. }
  126. }
  127. static MfClassicSectorTrailer*
  128. mifare_nested_worker_get_sector_trailer_by_sector(MfClassicData* data, uint8_t sector) {
  129. return (MfClassicSectorTrailer*)data->block[mifare_nested_worker_get_block_by_sector(sector)]
  130. .value;
  131. }
  132. bool mifare_nested_worker_read_key_cache(FuriHalNfcDevData* data, MfClassicData* mf_data) {
  133. Storage* storage = furi_record_open(RECORD_STORAGE);
  134. FuriString* temp_str = furi_string_alloc();
  135. mifare_nested_worker_get_key_cache_file_path(data, temp_str);
  136. FlipperFormat* file = flipper_format_file_alloc(storage);
  137. bool load_success = false;
  138. uint32_t sector_count = 0;
  139. do {
  140. if(storage_common_stat(storage, furi_string_get_cstr(temp_str), NULL) != FSE_OK) break;
  141. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(temp_str))) break;
  142. uint32_t version = 0;
  143. if(!flipper_format_read_header(file, temp_str, &version)) break;
  144. if(furi_string_cmp_str(temp_str, "Flipper NFC keys")) break;
  145. if(version != 1) break;
  146. if(!flipper_format_read_string(file, "Mifare Classic type", temp_str)) break;
  147. if(!furi_string_cmp(temp_str, "1K")) {
  148. mf_data->type = MfClassicType1k;
  149. sector_count = 16;
  150. } else if(!furi_string_cmp(temp_str, "4K")) {
  151. mf_data->type = MfClassicType4k;
  152. sector_count = 40;
  153. } else if(!furi_string_cmp(temp_str, "MINI")) {
  154. mf_data->type = MfClassicTypeMini;
  155. sector_count = 5;
  156. } else {
  157. break;
  158. };
  159. if(!flipper_format_read_hex_uint64(file, "Key A map", &mf_data->key_a_mask, 1)) break;
  160. if(!flipper_format_read_hex_uint64(file, "Key B map", &mf_data->key_b_mask, 1)) break;
  161. bool key_read_success = true;
  162. for(size_t i = 0; (i < sector_count) && (key_read_success); i++) {
  163. MfClassicSectorTrailer* sec_tr =
  164. mifare_nested_worker_get_sector_trailer_by_sector(mf_data, i);
  165. if(FURI_BIT(mf_data->key_a_mask, i)) {
  166. furi_string_printf(temp_str, "Key A sector %d", i);
  167. key_read_success = flipper_format_read_hex(
  168. file, furi_string_get_cstr(temp_str), sec_tr->key_a, 6);
  169. }
  170. if(!key_read_success) break;
  171. if(FURI_BIT(mf_data->key_b_mask, i)) {
  172. furi_string_printf(temp_str, "Key B sector %d", i);
  173. key_read_success = flipper_format_read_hex(
  174. file, furi_string_get_cstr(temp_str), sec_tr->key_b, 6);
  175. }
  176. }
  177. load_success = key_read_success;
  178. } while(false);
  179. furi_string_free(temp_str);
  180. flipper_format_free(file);
  181. return load_success;
  182. }
  183. bool hex_char_to_hex_nibble(char c, uint8_t* nibble) {
  184. if((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) {
  185. if((c >= '0' && c <= '9')) {
  186. *nibble = c - '0';
  187. } else if((c >= 'A' && c <= 'F')) {
  188. *nibble = c - 'A' + 10;
  189. } else {
  190. *nibble = c - 'a' + 10;
  191. }
  192. return true;
  193. } else {
  194. return false;
  195. }
  196. }
  197. bool hex_char_to_uint8(char hi, char low, uint8_t* value) {
  198. uint8_t hi_nibble_value, low_nibble_value;
  199. if(hex_char_to_hex_nibble(hi, &hi_nibble_value) &&
  200. hex_char_to_hex_nibble(low, &low_nibble_value)) {
  201. *value = (hi_nibble_value << 4) | low_nibble_value;
  202. return true;
  203. } else {
  204. return false;
  205. }
  206. }
  207. MfClassicType mifare_nested_worker_get_tag_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
  208. UNUSED(ATQA1);
  209. if((ATQA0 == 0x44 || ATQA0 == 0x04)) {
  210. if((SAK == 0x08 || SAK == 0x88)) {
  211. return MfClassicType1k;
  212. } else if(SAK == 0x09) {
  213. return MfClassicTypeMini;
  214. }
  215. } else if((ATQA0 == 0x01) && (ATQA1 == 0x0F) && (SAK == 0x01)) {
  216. //skylanders support
  217. return MfClassicType1k;
  218. } else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
  219. return MfClassicType4k;
  220. }
  221. return MfClassicType1k;
  222. }
  223. uint32_t mifare_nested_worker_predict_delay(
  224. FuriHalNfcTxRxContext* tx_rx,
  225. uint8_t blockNo,
  226. uint8_t keyType,
  227. uint64_t ui64Key,
  228. MifareNestedWorker* mifare_nested_worker) {
  229. uint32_t cuid = 0;
  230. Crypto1* crypto = malloc(sizeof(Crypto1));
  231. uint32_t nt1, nt2, i = 0, previous = 0, prng_delay = 0, zero_prng_value = 65565, repeat = 0;
  232. // This part of attack is my attempt to implement it on Flipper.
  233. // Proxmark can do this in 2 fucking steps, but idk how.
  234. // First, we find RPNG rounds per 1000 us
  235. for(uint32_t rtr = 0; rtr < 25; rtr++) {
  236. if(mifare_nested_worker->state != MifareNestedWorkerStateCollecting) {
  237. return 1;
  238. }
  239. nfc_activate();
  240. if(!furi_hal_nfc_activate_nfca(200, &cuid)) break;
  241. mifare_classic_authex(crypto, tx_rx, cuid, blockNo, keyType, ui64Key, false, &nt1);
  242. furi_delay_us(rtr * 1000);
  243. mifare_classic_authex(crypto, tx_rx, cuid, blockNo, keyType, ui64Key, true, &nt2);
  244. // Searching for delay, where PRNG will be near 800
  245. uint32_t nttmp = prng_successor(nt1, 100);
  246. for(i = 101; i < 65565; i++) {
  247. nttmp = prng_successor(nttmp, 1);
  248. if(nttmp == nt2) break;
  249. }
  250. if(!rtr) {
  251. zero_prng_value = i;
  252. }
  253. if(previous && i > previous && i != 65565) {
  254. if(!prng_delay) {
  255. prng_delay = i - previous;
  256. } else if(prng_delay - 100 > i - previous && prng_delay + 100 < i - previous) {
  257. prng_delay += i - previous;
  258. prng_delay /= 2;
  259. }
  260. }
  261. previous = i;
  262. FURI_LOG_D(TAG, "Calibrating: ntdist=%lu, delay=%lu", i, rtr * 1000);
  263. // Let's hope...
  264. if(i > 810 && i < 840) {
  265. return rtr * 1000;
  266. }
  267. }
  268. FURI_LOG_D(TAG, "PRNG timing: growth ratio per 1000 us = %lu", prng_delay);
  269. // Next, we try to calculate time until PRNG near 800 with more perfect timing
  270. // Mifare Classic (weak) RPNG repeats every 65565 PRNG cycles
  271. if(zero_prng_value == 65565) {
  272. // PRNG isn't pretictable
  273. return 1;
  274. }
  275. uint32_t cycles_to_reset = (65565 - zero_prng_value) / prng_delay;
  276. uint32_t limit = 7;
  277. for(uint32_t rtr = cycles_to_reset - 1; rtr < cycles_to_reset + limit; rtr++) {
  278. for(uint32_t rtz = 0; rtz < 100; rtz++) {
  279. if(mifare_nested_worker->state != MifareNestedWorkerStateCollecting) {
  280. return 1;
  281. }
  282. nfc_activate();
  283. if(!furi_hal_nfc_activate_nfca(200, &cuid)) break;
  284. uint32_t delay = rtr * 1000 + rtz * 10;
  285. mifare_classic_authex(crypto, tx_rx, cuid, blockNo, keyType, ui64Key, false, &nt1);
  286. furi_delay_us(delay);
  287. mifare_classic_authex(crypto, tx_rx, cuid, blockNo, keyType, ui64Key, true, &nt2);
  288. // Searching for delay, where PRNG will be near 800
  289. uint32_t nttmp = prng_successor(nt1, 0);
  290. for(i = 1; i < 65565; i++) {
  291. nttmp = prng_successor(nttmp, 1);
  292. if(nttmp == nt2) break;
  293. }
  294. if(!(i > previous - 50 && i < previous + 50) && rtz) {
  295. repeat++;
  296. if(repeat < 5) {
  297. FURI_LOG_D(TAG, "Invalid RPNG value: ntdist=%lu", i);
  298. continue;
  299. }
  300. }
  301. if(i > 2000 && i < 65500) {
  302. uint32_t catch_cycles = (65565 - i) / prng_delay;
  303. if(catch_cycles > 2) {
  304. catch_cycles++;
  305. FURI_LOG_D(
  306. TAG,
  307. "Trying a more accurate value: skipping additional %lu us",
  308. catch_cycles * 1000);
  309. limit += catch_cycles + 2;
  310. rtr += catch_cycles;
  311. }
  312. }
  313. FURI_LOG_D(
  314. TAG,
  315. "Calibrating: ntdist=%lu, delay=%lu, max=%lu",
  316. i,
  317. delay,
  318. (cycles_to_reset + limit) * 1000);
  319. repeat = 0;
  320. previous = i;
  321. if(i > 810 && i < 840) {
  322. FURI_LOG_I(TAG, "Found delay: %lu us", delay);
  323. return delay;
  324. } else if(i > 840 && i < 40000) {
  325. FURI_LOG_D(TAG, "Trying again: timing lost");
  326. return mifare_nested_worker_predict_delay(
  327. tx_rx, blockNo, keyType, ui64Key, mifare_nested_worker);
  328. }
  329. }
  330. }
  331. if(i > 1000 && i < 65000) {
  332. FURI_LOG_D(TAG, "Trying again: wrong predicted timing");
  333. return mifare_nested_worker_predict_delay(
  334. tx_rx, blockNo, keyType, ui64Key, mifare_nested_worker);
  335. }
  336. return 1;
  337. }
  338. void mifare_nested_worker_write_nonces(
  339. FuriHalNfcDevData* data,
  340. Storage* storage,
  341. NonceList_t* nonces,
  342. uint8_t tries_count,
  343. uint8_t sector_count,
  344. uint32_t delay,
  345. uint32_t distance) {
  346. FuriString* path = furi_string_alloc();
  347. Stream* file_stream = buffered_file_stream_alloc(storage);
  348. mifare_nested_worker_get_nonces_file_path(data, path);
  349. buffered_file_stream_open(
  350. file_stream, furi_string_get_cstr(path), FSAM_READ_WRITE, FSOM_CREATE_ALWAYS);
  351. FuriString* header = furi_string_alloc_printf(
  352. "Note: you will need desktop app to recover keys: %s\nVersion: %s\n",
  353. NESTED_RECOVER_KEYS_GITHUB_LINK,
  354. NESTED_NONCE_FORMAT_VERSION);
  355. stream_write_string(file_stream, header);
  356. for(uint8_t tries = 0; tries < tries_count; tries++) {
  357. for(uint8_t sector = 0; sector < sector_count; sector++) {
  358. for(uint8_t key_type = 0; key_type < 2; key_type++) {
  359. if(nonces->nonces[sector][key_type][tries]->collected &&
  360. !nonces->nonces[sector][key_type][tries]->skipped) {
  361. FuriString* str = furi_string_alloc_printf(
  362. "Nested: Key %c cuid 0x%08lx", !key_type ? 'A' : 'B', nonces->cuid);
  363. for(uint8_t type = 0; type < 2; type++) {
  364. furi_string_cat_printf(
  365. str,
  366. " nt%u 0x%08lx ks%u 0x%08lx par%u ",
  367. type,
  368. nonces->nonces[sector][key_type][tries]->target_nt[type],
  369. type,
  370. nonces->nonces[sector][key_type][tries]->target_ks[type],
  371. type);
  372. uint8_t* par = nonces->nonces[sector][key_type][tries]->parity[type];
  373. for(uint8_t i = 0; i < 4; i++) {
  374. furi_string_cat_printf(str, "%u", par[i]);
  375. }
  376. }
  377. furi_string_cat_printf(str, " sec %u\n", sector);
  378. stream_write_string(file_stream, str);
  379. furi_string_free(str);
  380. }
  381. }
  382. }
  383. }
  384. if(delay) {
  385. FuriString* str =
  386. furi_string_alloc_printf("Nested: Delay %lu, distance %lu", delay, distance);
  387. stream_write_string(file_stream, str);
  388. furi_string_free(str);
  389. }
  390. free(nonces);
  391. furi_string_free(path);
  392. buffered_file_stream_close(file_stream);
  393. free(file_stream);
  394. furi_record_close(RECORD_STORAGE);
  395. }
  396. bool mifare_nested_worker_check_initial_keys(
  397. NonceList_t* nonces,
  398. MfClassicData* mf_data,
  399. uint8_t tries_count,
  400. uint8_t sector_count,
  401. uint64_t* key,
  402. uint32_t* key_block,
  403. uint32_t* found_key_type) {
  404. bool has_a_key, has_b_key;
  405. FuriHalNfcTxRxContext tx_rx = {};
  406. for(uint8_t sector = 0; sector < sector_count; sector++) {
  407. for(uint8_t key_type = 0; key_type < 2; key_type++) {
  408. for(uint8_t tries = 0; tries < tries_count; tries++) {
  409. Nonces* info = malloc(sizeof(Nonces));
  410. info->key_type = key_type;
  411. info->block = mifare_nested_worker_get_block_by_sector(sector);
  412. info->collected = false;
  413. nonces->nonces[sector][key_type][tries] = info;
  414. }
  415. }
  416. }
  417. for(uint8_t sector = 0; sector < sector_count; sector++) {
  418. MfClassicSectorTrailer* trailer =
  419. mifare_nested_worker_get_sector_trailer_by_sector(mf_data, sector);
  420. has_a_key = FURI_BIT(mf_data->key_a_mask, sector);
  421. has_b_key = FURI_BIT(mf_data->key_b_mask, sector);
  422. if(has_a_key) {
  423. for(uint8_t tries = 0; tries < tries_count; tries++) {
  424. Nonces* info = nonces->nonces[sector][0][tries];
  425. info->collected = true;
  426. nonces->nonces[sector][0][tries] = info;
  427. }
  428. if(*key_block == 0) {
  429. uint64_t key_check = nfc_util_bytes2num(trailer->key_a, 6);
  430. if(nested_check_key(
  431. &tx_rx, mifare_nested_worker_get_block_by_sector(sector), 0, key_check) ==
  432. NestedCheckKeyValid) {
  433. *key = key_check;
  434. *key_block = mifare_nested_worker_get_block_by_sector(sector);
  435. *found_key_type = 0;
  436. }
  437. }
  438. }
  439. if(has_b_key) {
  440. for(uint8_t tries = 0; tries < tries_count; tries++) {
  441. Nonces* info = nonces->nonces[sector][1][tries];
  442. info->collected = true;
  443. nonces->nonces[sector][1][tries] = info;
  444. }
  445. if(*key_block == 0) {
  446. uint64_t key_check = nfc_util_bytes2num(trailer->key_b, 6);
  447. if(nested_check_key(
  448. &tx_rx, mifare_nested_worker_get_block_by_sector(sector), 1, key_check) ==
  449. NestedCheckKeyValid) {
  450. *key = key_check;
  451. *key_block = mifare_nested_worker_get_block_by_sector(sector);
  452. *found_key_type = 1;
  453. }
  454. }
  455. }
  456. if(sector == sector_count - 1 && key_block == 0) {
  457. return false;
  458. }
  459. }
  460. return true;
  461. }
  462. void mifare_nested_worker_collect_nonces_static(MifareNestedWorker* mifare_nested_worker) {
  463. NonceList_t* nonces = malloc(sizeof(NonceList_t));
  464. Storage* storage = furi_record_open(RECORD_STORAGE);
  465. NfcDevice* dev = mifare_nested_worker->context->nfc_dev;
  466. MfClassicData* mf_data = &dev->dev_data.mf_classic_data;
  467. FuriString* folder_path = furi_string_alloc();
  468. FuriHalNfcDevData data = {};
  469. nested_get_data(&data);
  470. MfClassicType type = mifare_nested_worker_get_tag_type(data.atqa[0], data.atqa[1], data.sak);
  471. uint64_t key = 0; // Found key for attack
  472. uint32_t found_key_type = 0;
  473. uint32_t key_block = 0;
  474. uint32_t sector_count = 0;
  475. FURI_LOG_I(TAG, "Running static nested attack");
  476. FuriString* tag_info = furi_string_alloc_printf("Tag UID: ");
  477. mifare_nested_worker_write_uid_string(&data, tag_info);
  478. FURI_LOG_I(TAG, "%s", furi_string_get_cstr(tag_info));
  479. furi_string_free(tag_info);
  480. if(type == MfClassicType4k) {
  481. sector_count = 40;
  482. FURI_LOG_I(TAG, "Found Mifare Classic 4K tag");
  483. } else if(type == MfClassicType1k) {
  484. sector_count = 16;
  485. FURI_LOG_I(TAG, "Found Mifare Classic 1K tag");
  486. } else if(type == MfClassicTypeMini) {
  487. sector_count = 5;
  488. FURI_LOG_I(TAG, "Found Mifare Classic Mini tag");
  489. }
  490. furi_string_set(folder_path, NESTED_FOLDER);
  491. storage_common_mkdir(storage, furi_string_get_cstr(folder_path));
  492. furi_string_free(folder_path);
  493. if(!mifare_nested_worker_read_key_cache(&data, mf_data)) {
  494. mifare_nested_worker->callback(
  495. MifareNestedWorkerEventNeedKey, mifare_nested_worker->context);
  496. nfc_deactivate();
  497. free(mf_data);
  498. free(nonces);
  499. return;
  500. }
  501. if(!mifare_nested_worker_check_initial_keys(
  502. nonces, mf_data, 1, sector_count, &key, &key_block, &found_key_type)) {
  503. mifare_nested_worker->callback(
  504. MifareNestedWorkerEventNeedKey, mifare_nested_worker->context);
  505. nfc_deactivate();
  506. free(mf_data);
  507. free(nonces);
  508. return;
  509. }
  510. FURI_LOG_I(
  511. TAG, "Using %c key for block %lu: %06llX", !found_key_type ? 'A' : 'B', key_block, key);
  512. nonces->tries = 1;
  513. while(mifare_nested_worker->state == MifareNestedWorkerStateCollectingStatic) {
  514. FuriHalNfcTxRxContext tx_rx = {};
  515. for(uint8_t sector = 0; sector < sector_count; sector++) {
  516. for(uint8_t key_type = 0; key_type < 2; key_type++) {
  517. Nonces* info = nonces->nonces[sector][key_type][0];
  518. if(info->collected) {
  519. FURI_LOG_I(
  520. TAG,
  521. "Skipping sector %u, block %u, key_type: %u as we already have a key",
  522. sector,
  523. mifare_nested_worker_get_block_by_sector(sector),
  524. key_type);
  525. info->skipped = true;
  526. nonces->nonces[sector][key_type][0] = info;
  527. mifare_nested_worker->context->nonces = nonces;
  528. mifare_nested_worker->callback(
  529. MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
  530. }
  531. while(!info->collected) {
  532. if(mifare_nested_worker->state != MifareNestedWorkerStateCollectingStatic) {
  533. break;
  534. }
  535. struct nonce_info_static result = nested_static_nonce_attack(
  536. &tx_rx,
  537. key_block,
  538. found_key_type,
  539. mifare_nested_worker_get_block_by_sector(sector),
  540. key_type,
  541. key);
  542. if(result.full) {
  543. FURI_LOG_I(
  544. TAG,
  545. "Accured nonces for sector %u, block %u, key_type: %u",
  546. sector,
  547. mifare_nested_worker_get_block_by_sector(sector),
  548. key_type);
  549. info = nonces->nonces[sector][key_type][0];
  550. info->collected = true;
  551. memcpy(&info->target_nt, result.target_nt, sizeof(result.target_nt));
  552. memcpy(&info->target_ks, result.target_ks, sizeof(result.target_ks));
  553. nonces->nonces[sector][key_type][0] = info;
  554. nonces->cuid = result.cuid;
  555. nonces->sector_count = sector_count;
  556. mifare_nested_worker->context->nonces = nonces;
  557. mifare_nested_worker->callback(
  558. MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
  559. break;
  560. } else {
  561. mifare_nested_worker->callback(
  562. MifareNestedWorkerEventNoTagDetected, mifare_nested_worker->context);
  563. }
  564. }
  565. }
  566. }
  567. break;
  568. }
  569. mifare_nested_worker_write_nonces(&data, storage, nonces, 1, sector_count, 0, 0);
  570. free(mf_data);
  571. mifare_nested_worker->callback(
  572. MifareNestedWorkerEventNoncesCollected, mifare_nested_worker->context);
  573. nfc_deactivate();
  574. }
  575. void mifare_nested_worker_collect_nonces(MifareNestedWorker* mifare_nested_worker) {
  576. NonceList_t* nonces = malloc(sizeof(NonceList_t));
  577. Storage* storage = furi_record_open(RECORD_STORAGE);
  578. NfcDevice* dev = mifare_nested_worker->context->nfc_dev;
  579. MfClassicData* mf_data = &dev->dev_data.mf_classic_data;
  580. FuriString* folder_path = furi_string_alloc();
  581. FuriHalNfcDevData data = {};
  582. nested_get_data(&data);
  583. MfClassicType type = mifare_nested_worker_get_tag_type(data.atqa[0], data.atqa[1], data.sak);
  584. uint64_t key = 0; // Found key for attack
  585. uint32_t found_key_type = 0;
  586. uint32_t key_block = 0;
  587. uint32_t sector_count = 0;
  588. uint32_t delay = 0;
  589. uint32_t distance = 0;
  590. uint32_t tries_count = 1;
  591. FURI_LOG_I(TAG, "Running nested attack");
  592. FuriString* tag_info = furi_string_alloc_printf("Tag UID: ");
  593. mifare_nested_worker_write_uid_string(&data, tag_info);
  594. FURI_LOG_I(TAG, "%s", furi_string_get_cstr(tag_info));
  595. furi_string_free(tag_info);
  596. if(type == MfClassicType4k) {
  597. sector_count = 40;
  598. FURI_LOG_I(TAG, "Found Mifare Classic 4K tag");
  599. } else if(type == MfClassicType1k) {
  600. sector_count = 16;
  601. FURI_LOG_I(TAG, "Found Mifare Classic 1K tag");
  602. } else if(type == MfClassicTypeMini) {
  603. sector_count = 5;
  604. FURI_LOG_I(TAG, "Found Mifare Classic Mini tag");
  605. }
  606. furi_string_set(folder_path, NESTED_FOLDER);
  607. storage_common_mkdir(storage, furi_string_get_cstr(folder_path));
  608. furi_string_free(folder_path);
  609. if(!mifare_nested_worker_read_key_cache(&data, mf_data)) {
  610. mifare_nested_worker->callback(
  611. MifareNestedWorkerEventNeedKey, mifare_nested_worker->context);
  612. nfc_deactivate();
  613. free(mf_data);
  614. free(nonces);
  615. return;
  616. }
  617. if(!mifare_nested_worker_check_initial_keys(
  618. nonces, mf_data, 3, sector_count, &key, &key_block, &found_key_type)) {
  619. mifare_nested_worker->callback(
  620. MifareNestedWorkerEventNeedKey, mifare_nested_worker->context);
  621. nfc_deactivate();
  622. free(mf_data);
  623. free(nonces);
  624. return;
  625. }
  626. FURI_LOG_I(
  627. TAG, "Using %c key for block %lu: %06llX", !found_key_type ? 'A' : 'B', key_block, key);
  628. while(mifare_nested_worker->state == MifareNestedWorkerStateCollecting) {
  629. FuriHalNfcTxRxContext tx_rx = {};
  630. uint32_t first_distance = 0;
  631. uint32_t second_distance = 0;
  632. mifare_nested_worker->callback(
  633. MifareNestedWorkerEventCalibrating, mifare_nested_worker->context);
  634. distance = nested_calibrate_distance(&tx_rx, key_block, found_key_type, key, delay, false);
  635. if(mifare_nested_worker->state == MifareNestedWorkerStateCollecting) {
  636. first_distance =
  637. nested_calibrate_distance(&tx_rx, key_block, found_key_type, key, delay, true);
  638. }
  639. if(mifare_nested_worker->state == MifareNestedWorkerStateCollecting) {
  640. second_distance =
  641. nested_calibrate_distance(&tx_rx, key_block, found_key_type, key, 10000, true);
  642. }
  643. if(first_distance == 0 && second_distance == 0) {
  644. mifare_nested_worker->callback(
  645. MifareNestedWorkerEventUnpredictablePRNG, mifare_nested_worker->context);
  646. nfc_deactivate();
  647. free(mf_data);
  648. free(nonces);
  649. return;
  650. }
  651. if(first_distance < second_distance - 100 && second_distance > 100) {
  652. FURI_LOG_E(
  653. TAG,
  654. "Discovered tag with PRNG that depends on time. PRNG values: %lu, %lu",
  655. first_distance,
  656. second_distance);
  657. struct distance_info info =
  658. nested_calibrate_distance_info(&tx_rx, key_block, found_key_type, key);
  659. if(info.invalid > 6) {
  660. // TODO: WIP
  661. FURI_LOG_W(
  662. TAG,
  663. "PRNG is too unpredictable (too much invalid values: %lu), fallback to delay method",
  664. info.invalid);
  665. delay = 1;
  666. } else if(info.max_prng - info.min_prng > 150) {
  667. FURI_LOG_W(
  668. TAG,
  669. "PRNG is too unpredictable (min/max values more than 150: %lu - %lu = %lu), fallback to delay method",
  670. info.max_prng,
  671. info.min_prng,
  672. info.max_prng - info.min_prng);
  673. delay = 1;
  674. } else {
  675. FURI_LOG_I(
  676. TAG,
  677. "PRNG is stable, using method without delay! (May be false positive, still will collect x3 times)");
  678. distance =
  679. nested_calibrate_distance(&tx_rx, key_block, found_key_type, key, delay, true);
  680. delay = 2;
  681. tries_count = 3;
  682. }
  683. }
  684. if(distance == 0 || delay == 1) {
  685. bool failed = false;
  686. // Tag need delay or unpredictable PRNG
  687. FURI_LOG_W(TAG, "Can't determine distance, trying to find timing...");
  688. mifare_nested_worker->callback(
  689. MifareNestedWorkerEventNeedPrediction, mifare_nested_worker->context);
  690. delay = mifare_nested_worker_predict_delay(
  691. &tx_rx, key_block, found_key_type, key, mifare_nested_worker);
  692. if(delay == 1) {
  693. FURI_LOG_E(TAG, "Can't determine delay");
  694. // Check that we didn't lost tag
  695. FuriHalNfcDevData lost_tag_data = {};
  696. nested_get_data(&lost_tag_data);
  697. if(lost_tag_data.uid_len == 0) {
  698. // We lost it.
  699. mifare_nested_worker->callback(
  700. MifareNestedWorkerEventNoTagDetected, mifare_nested_worker->context);
  701. while(mifare_nested_worker->state == MifareNestedWorkerStateCollecting &&
  702. lost_tag_data.cuid != data.cuid) {
  703. furi_delay_ms(250);
  704. nested_get_data(&lost_tag_data);
  705. }
  706. mifare_nested_worker->callback(
  707. MifareNestedWorkerEventCalibrating, mifare_nested_worker->context);
  708. continue;
  709. }
  710. if(mifare_nested_worker->state == MifareNestedWorkerStateCollecting) {
  711. mifare_nested_worker->callback(
  712. MifareNestedWorkerEventUnpredictablePRNG, mifare_nested_worker->context);
  713. }
  714. failed = true;
  715. }
  716. if(mifare_nested_worker->state == MifareNestedWorkerStateCollecting && !failed) {
  717. distance = nested_calibrate_distance(
  718. &tx_rx, key_block, found_key_type, key, delay, false);
  719. }
  720. if(distance == 0 && !failed) {
  721. FURI_LOG_E(TAG, "Found delay, but can't find distance");
  722. mifare_nested_worker->callback(
  723. MifareNestedWorkerEventAttackFailed, mifare_nested_worker->context);
  724. failed = true;
  725. }
  726. if(failed) {
  727. nfc_deactivate();
  728. free(mf_data);
  729. free(nonces);
  730. return;
  731. }
  732. tries_count = 3;
  733. }
  734. mifare_nested_worker->context->nonces = nonces;
  735. mifare_nested_worker->callback(
  736. MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
  737. for(uint8_t tries = 0; tries < tries_count; tries++) {
  738. for(uint8_t sector = 0; sector < sector_count; sector++) {
  739. for(uint8_t key_type = 0; key_type < 2; key_type++) {
  740. Nonces* info = nonces->nonces[sector][key_type][tries];
  741. if(info->collected) {
  742. FURI_LOG_I(
  743. TAG,
  744. "Skipping sector %u, block %u, key_type: %u as we already have a key",
  745. sector,
  746. mifare_nested_worker_get_block_by_sector(sector),
  747. key_type);
  748. info->skipped = true;
  749. nonces->nonces[sector][key_type][tries] = info;
  750. mifare_nested_worker->context->nonces = nonces;
  751. mifare_nested_worker->callback(
  752. MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
  753. }
  754. while(!info->collected) {
  755. if(mifare_nested_worker->state != MifareNestedWorkerStateCollecting) {
  756. break;
  757. }
  758. struct nonce_info result = nested_attack(
  759. &tx_rx,
  760. key_block,
  761. found_key_type,
  762. mifare_nested_worker_get_block_by_sector(sector),
  763. key_type,
  764. key,
  765. distance,
  766. delay);
  767. if(result.full) {
  768. FURI_LOG_I(
  769. TAG,
  770. "Accured nonces for sector %u, block %u, key_type: %u",
  771. sector,
  772. mifare_nested_worker_get_block_by_sector(sector),
  773. key_type);
  774. info = nonces->nonces[sector][key_type][tries];
  775. info->collected = true;
  776. memcpy(&info->target_nt, result.target_nt, sizeof(result.target_nt));
  777. memcpy(&info->target_ks, result.target_ks, sizeof(result.target_ks));
  778. memcpy(&info->parity, result.parity, sizeof(result.parity));
  779. nonces->nonces[sector][key_type][tries] = info;
  780. nonces->cuid = result.cuid;
  781. nonces->sector_count = sector_count;
  782. nonces->tries = tries_count;
  783. mifare_nested_worker->context->nonces = nonces;
  784. mifare_nested_worker->callback(
  785. MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
  786. break;
  787. } else {
  788. mifare_nested_worker->callback(
  789. MifareNestedWorkerEventNoTagDetected,
  790. mifare_nested_worker->context);
  791. }
  792. }
  793. }
  794. }
  795. }
  796. break;
  797. }
  798. mifare_nested_worker_write_nonces(
  799. &data, storage, nonces, tries_count, sector_count, delay, distance);
  800. free(mf_data);
  801. mifare_nested_worker->callback(
  802. MifareNestedWorkerEventNoncesCollected, mifare_nested_worker->context);
  803. nfc_deactivate();
  804. }
  805. bool* mifare_nested_worker_check_keys_exists(
  806. Storage* storage,
  807. char* path,
  808. uint64_t* keys,
  809. uint32_t key_count,
  810. MifareNestedWorker* mifare_nested_worker) {
  811. bool* old_keys = malloc(sizeof(bool) * key_count);
  812. Stream* file_stream = buffered_file_stream_alloc(storage);
  813. buffered_file_stream_open(file_stream, path, FSAM_READ, FSOM_OPEN_ALWAYS);
  814. FuriString* key_strings[key_count];
  815. for(uint32_t i = 0; i < key_count; i++) {
  816. old_keys[i] = false;
  817. key_strings[i] = furi_string_alloc_printf("%06llX\n", keys[i]);
  818. }
  819. while(mifare_nested_worker->state == MifareNestedWorkerStateValidating) {
  820. FuriString* next_line = furi_string_alloc();
  821. if(!stream_read_line(file_stream, next_line)) {
  822. break;
  823. }
  824. for(uint32_t i = 0; i < key_count; i++) {
  825. if(keys[i] == (uint64_t)-1) continue;
  826. if(furi_string_cmp(next_line, key_strings[i]) == 0) {
  827. old_keys[i] = true;
  828. }
  829. }
  830. furi_string_free(next_line);
  831. }
  832. for(uint32_t i = 0; i < key_count; i++) {
  833. furi_string_free(key_strings[i]);
  834. }
  835. buffered_file_stream_close(file_stream);
  836. free(file_stream);
  837. return old_keys;
  838. }
  839. void mifare_nested_worker_write_key(Storage* storage, FuriString* key) {
  840. Stream* file_stream = buffered_file_stream_alloc(storage);
  841. buffered_file_stream_open(
  842. file_stream,
  843. EXT_PATH("nfc/assets/mf_classic_dict_user.nfc"),
  844. FSAM_READ_WRITE,
  845. FSOM_OPEN_APPEND);
  846. stream_write_string(file_stream, key);
  847. buffered_file_stream_close(file_stream);
  848. }
  849. void mifare_nested_worker_check_keys(MifareNestedWorker* mifare_nested_worker) {
  850. KeyInfo_t* key_info = mifare_nested_worker->context->keys;
  851. Storage* storage = furi_record_open(RECORD_STORAGE);
  852. Stream* file_stream = buffered_file_stream_alloc(storage);
  853. FuriString* next_line = furi_string_alloc();
  854. FuriString* path = furi_string_alloc();
  855. FuriHalNfcDevData data = {};
  856. nested_get_data(&data);
  857. MfClassicType type = mifare_nested_worker_get_tag_type(data.atqa[0], data.atqa[1], data.sak);
  858. NestedCheckKeyResult result = NestedCheckKeyNoTag;
  859. FuriHalNfcTxRxContext tx_rx = {};
  860. uint32_t key_count = 0;
  861. uint32_t sector_key_count = 0;
  862. uint64_t keys[80];
  863. bool found_keys[2][40];
  864. bool unique_keys[2][40];
  865. uint32_t sector_count = 0;
  866. if(type == MfClassicType4k) {
  867. sector_count = 40;
  868. FURI_LOG_I(TAG, "Found Mifare Classic 4K tag");
  869. } else if(type == MfClassicType1k) {
  870. sector_count = 16;
  871. FURI_LOG_I(TAG, "Found Mifare Classic 1K tag");
  872. } else if(type == MfClassicTypeMini) {
  873. sector_count = 5;
  874. FURI_LOG_I(TAG, "Found Mifare Classic Mini tag");
  875. }
  876. uint32_t keys_count = sector_count * 2;
  877. for(uint8_t key = 0; key < 2; key++) {
  878. for(uint8_t i = 0; i < sector_count; i++) {
  879. found_keys[key][i] = false;
  880. unique_keys[key][i] = false;
  881. }
  882. }
  883. for(uint8_t i = 0; i < keys_count; i++) {
  884. keys[i] = -1;
  885. }
  886. mifare_nested_worker_get_found_keys_file_path(&data, path);
  887. if(!buffered_file_stream_open(
  888. file_stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  889. FURI_LOG_E(TAG, "Can't open %s", furi_string_get_cstr(path));
  890. buffered_file_stream_close(file_stream);
  891. mifare_nested_worker_get_nonces_file_path(&data, path);
  892. if(!buffered_file_stream_open(
  893. file_stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  894. mifare_nested_worker->callback(
  895. MifareNestedWorkerEventNeedCollection, mifare_nested_worker->context);
  896. } else {
  897. mifare_nested_worker->callback(
  898. MifareNestedWorkerEventNeedKeyRecovery, mifare_nested_worker->context);
  899. }
  900. buffered_file_stream_close(file_stream);
  901. free(file_stream);
  902. furi_string_free(path);
  903. furi_string_free(next_line);
  904. furi_record_close(RECORD_STORAGE);
  905. return;
  906. };
  907. while(true) {
  908. if(!stream_read_line(file_stream, next_line)) {
  909. break;
  910. }
  911. if(furi_string_start_with_str(next_line, "Key")) {
  912. uint8_t key_type = furi_string_get_char(next_line, 4) == 'B';
  913. uint8_t sector = atoi((char[]){furi_string_get_char(next_line, 13)}) * 10 +
  914. atoi((char[]){furi_string_get_char(next_line, 14)});
  915. if(!unique_keys[key_type][sector]) {
  916. unique_keys[key_type][sector] = true;
  917. sector_key_count++;
  918. }
  919. }
  920. key_count++;
  921. }
  922. stream_rewind(file_stream);
  923. key_info->total_keys = key_count;
  924. key_info->sector_keys = sector_key_count;
  925. while(mifare_nested_worker->state == MifareNestedWorkerStateValidating) {
  926. if(!stream_read_line(file_stream, next_line)) {
  927. break;
  928. }
  929. if(furi_string_start_with_str(next_line, "Key")) {
  930. // Key X sector XX: XX XX XX XX XX XX
  931. // 0000000000111111111122222222223333
  932. // 0123456789012345678901234567890123
  933. uint8_t keyChar[6];
  934. uint8_t count = 0;
  935. uint8_t key_type = furi_string_get_char(next_line, 4) == 'B';
  936. uint8_t sector = atoi((char[]){furi_string_get_char(next_line, 13)}) * 10 +
  937. atoi((char[]){furi_string_get_char(next_line, 14)});
  938. for(uint8_t i = 17; i < 33; i += 3) {
  939. hex_char_to_uint8(
  940. furi_string_get_char(next_line, i),
  941. furi_string_get_char(next_line, i + 1),
  942. &keyChar[count]);
  943. count++;
  944. }
  945. uint64_t key = nfc_util_bytes2num(keyChar, 6);
  946. key_info->checked_keys++;
  947. if(found_keys[key_type][sector]) {
  948. mifare_nested_worker->callback(
  949. MifareNestedWorkerEventKeyChecked, mifare_nested_worker->context);
  950. continue;
  951. }
  952. while(mifare_nested_worker->state == MifareNestedWorkerStateValidating) {
  953. result = nested_check_key(
  954. &tx_rx, mifare_nested_worker_get_block_by_sector(sector), key_type, key);
  955. if(result == NestedCheckKeyNoTag) {
  956. mifare_nested_worker->callback(
  957. MifareNestedWorkerEventNoTagDetected, mifare_nested_worker->context);
  958. furi_delay_ms(250);
  959. } else {
  960. break;
  961. }
  962. }
  963. if(result == NestedCheckKeyValid) {
  964. FURI_LOG_I(TAG, "Found valid %c key for sector %u: %06llX", key_type, sector, key);
  965. bool exists = false;
  966. for(uint8_t i = 0; i < keys_count; i++) {
  967. if(keys[i] == key) {
  968. exists = true;
  969. }
  970. }
  971. if(!exists) {
  972. keys[key_info->found_keys] = key;
  973. }
  974. key_info->found_keys++;
  975. found_keys[key_type][sector] = true;
  976. }
  977. mifare_nested_worker->callback(
  978. MifareNestedWorkerEventKeyChecked, mifare_nested_worker->context);
  979. }
  980. }
  981. furi_string_free(next_line);
  982. buffered_file_stream_close(file_stream);
  983. free(file_stream);
  984. mifare_nested_worker->callback(
  985. MifareNestedWorkerEventProcessingKeys, mifare_nested_worker->context);
  986. bool* old_keys = mifare_nested_worker_check_keys_exists(
  987. storage,
  988. EXT_PATH("nfc/assets/mf_classic_dict_user.nfc"),
  989. keys,
  990. keys_count,
  991. mifare_nested_worker);
  992. for(uint8_t i = 0; i < keys_count; i++) {
  993. if(old_keys[i]) {
  994. keys[i] = -1;
  995. }
  996. }
  997. old_keys = mifare_nested_worker_check_keys_exists(
  998. storage,
  999. EXT_PATH("nfc/assets/mf_classic_dict.nfc"),
  1000. keys,
  1001. keys_count,
  1002. mifare_nested_worker);
  1003. for(uint8_t i = 0; i < keys_count; i++) {
  1004. if(old_keys[i]) {
  1005. keys[i] = -1;
  1006. }
  1007. }
  1008. for(uint8_t i = 0; i < keys_count; i++) {
  1009. if(keys[i] == (uint64_t)-1) continue;
  1010. FuriString* key_string = furi_string_alloc_printf("%06llX\n", keys[i]);
  1011. mifare_nested_worker_write_key(storage, key_string);
  1012. FURI_LOG_I(TAG, "Added new key: %s", furi_string_get_cstr(key_string));
  1013. key_info->added_keys++;
  1014. furi_string_free(key_string);
  1015. }
  1016. if(!storage_simply_remove(storage, furi_string_get_cstr(path))) {
  1017. FURI_LOG_E(TAG, "Failed to remove key cache file");
  1018. };
  1019. furi_record_close(RECORD_STORAGE);
  1020. furi_string_free(path);
  1021. mifare_nested_worker->callback(
  1022. MifareNestedWorkerEventKeysFound, mifare_nested_worker->context);
  1023. return;
  1024. }