13
13
14
14
#define TAG "MifareNestedWorker"
15
15
16
+ // possible sum property values
17
+ static uint16_t sums [] =
18
+ {0 , 32 , 56 , 64 , 80 , 96 , 104 , 112 , 120 , 128 , 136 , 144 , 152 , 160 , 176 , 192 , 200 , 224 , 256 };
19
+
16
20
void mifare_nested_worker_change_state (
17
21
MifareNestedWorker * mifare_nested_worker ,
18
22
MifareNestedWorkerState state ) {
@@ -468,7 +472,7 @@ uint32_t mifare_nested_worker_predict_delay(
468
472
}
469
473
470
474
free (crypto );
471
-
475
+
472
476
return 1 ;
473
477
}
474
478
@@ -876,50 +880,54 @@ void mifare_nested_worker_collect_nonces_hard(MifareNestedWorker* mifare_nested_
876
880
continue ;
877
881
}
878
882
879
- Stream * file_stream = file_stream_alloc (storage );
880
- FuriString * hardnested_file = furi_string_alloc ();
881
- mifare_nested_worker_get_hardnested_file_path (
882
- & data , hardnested_file , sector , key_type );
883
-
884
- file_stream_open (
885
- file_stream ,
886
- furi_string_get_cstr (hardnested_file ),
887
- FSAM_READ_WRITE ,
888
- FSOM_CREATE_ALWAYS );
889
-
890
- FuriString * header = furi_string_alloc_printf (
891
- "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" ,
892
- NESTED_NONCE_FORMAT_VERSION ,
893
- NESTED_RECOVER_KEYS_GITHUB_LINK ,
894
- !key_type ? 'A' : 'B' ,
895
- cuid ,
896
- sector );
897
-
898
- stream_write_string (file_stream , header );
899
- furi_string_free (header );
900
- furi_string_free (hardnested_file );
901
-
902
883
while (!info -> collected &&
903
884
mifare_nested_worker -> state == MifareNestedWorkerStateCollecting ) {
885
+ Stream * file_stream = file_stream_alloc (storage );
886
+ FuriString * hardnested_file = furi_string_alloc ();
887
+ mifare_nested_worker_get_hardnested_file_path (
888
+ & data , hardnested_file , sector , key_type );
889
+
890
+ file_stream_open (
891
+ file_stream ,
892
+ furi_string_get_cstr (hardnested_file ),
893
+ FSAM_READ_WRITE ,
894
+ FSOM_CREATE_ALWAYS );
895
+
896
+ FuriString * header = furi_string_alloc_printf (
897
+ "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" ,
898
+ NESTED_NONCE_FORMAT_VERSION ,
899
+ NESTED_RECOVER_KEYS_GITHUB_LINK ,
900
+ !key_type ? 'A' : 'B' ,
901
+ cuid ,
902
+ sector );
903
+
904
+ stream_write_string (file_stream , header );
905
+ furi_string_free (header );
906
+
907
+ uint32_t first_byte_sum = 0 ;
904
908
uint32_t * found = malloc (sizeof (uint32_t ) * 256 );
905
909
for (uint32_t i = 0 ; i < 256 ; i ++ ) {
906
910
found [i ] = 0 ;
907
911
}
908
912
909
913
while (mifare_nested_worker -> state == MifareNestedWorkerStateCollecting ) {
910
- struct nonce_info_hard result = hard_nested_collect_nonces (
914
+ struct nonce_info_hard result = nested_hard_nonce_attack (
911
915
& tx_rx ,
912
916
key_block ,
913
917
found_key_type ,
914
918
mifare_nested_worker_get_block_by_sector (sector ),
915
919
key_type ,
916
920
key ,
917
921
found ,
922
+ & first_byte_sum ,
918
923
file_stream );
919
924
920
925
if (result .static_encrypted ) {
921
- // TODO: Delete file?
922
926
file_stream_close (file_stream );
927
+
928
+ storage_simply_remove (storage , furi_string_get_cstr (hardnested_file ));
929
+
930
+ furi_string_free (hardnested_file );
923
931
free (found );
924
932
free (mf_data );
925
933
nfc_deactivate ();
@@ -946,6 +954,22 @@ void mifare_nested_worker_collect_nonces_hard(MifareNestedWorker* mifare_nested_
946
954
FURI_LOG_D (TAG , "Found states: %lu" , states );
947
955
948
956
if (states == 256 ) {
957
+ FURI_LOG_D (
958
+ TAG , "All states collected, first_byte_sum: %lu" , first_byte_sum );
959
+
960
+ bool valid = false;
961
+ for (uint8_t i = 0 ; i < sizeof (sums ); i ++ ) {
962
+ if (sums [i ] == first_byte_sum ) {
963
+ valid = true;
964
+ break ;
965
+ }
966
+ }
967
+
968
+ if (!valid ) {
969
+ FURI_LOG_E (TAG , "Invalid first_byte_sum!" );
970
+ break ;
971
+ }
972
+
949
973
info -> collected = true;
950
974
info -> hardnested = true;
951
975
nonces -> cuid = result .cuid ;
@@ -966,9 +990,9 @@ void mifare_nested_worker_collect_nonces_hard(MifareNestedWorker* mifare_nested_
966
990
}
967
991
968
992
free (found );
993
+ furi_string_free (hardnested_file );
994
+ file_stream_close (file_stream );
969
995
}
970
-
971
- file_stream_close (file_stream );
972
996
}
973
997
}
974
998
0 commit comments