Skip to content

Commit 34a5408

Browse files
committed
1.3.0 - Check first_byte_sum (no more invalid ones)
1 parent f15f924 commit 34a5408

File tree

4 files changed

+63
-36
lines changed

4 files changed

+63
-36
lines changed

lib/nested/nested.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -545,14 +545,15 @@ struct nonce_info nested_attack(
545545
return r;
546546
}
547547

548-
struct nonce_info_hard hard_nested_collect_nonces(
548+
struct nonce_info_hard nested_hard_nonce_attack(
549549
FuriHalNfcTxRxContext* tx_rx,
550550
uint8_t blockNo,
551551
uint8_t keyType,
552552
uint8_t targetBlockNo,
553553
uint8_t targetKeyType,
554554
uint64_t ui64Key,
555555
uint32_t* found,
556+
uint32_t* first_byte_sum,
556557
Stream* file_stream) {
557558
uint32_t cuid = 0;
558559
uint8_t same = 0;
@@ -585,11 +586,6 @@ struct nonce_info_hard hard_nested_collect_nonces(
585586
(oddparity8(tx_rx->rx_data[j]) != ((tx_rx->rx_parity[0] >> (7 - j)) & 0x01));
586587
}
587588

588-
// update unique nonces
589-
if(!found[tx_rx->rx_data[0]]) {
590-
found[tx_rx->rx_data[0]]++;
591-
}
592-
593589
uint8_t pbits = 0;
594590
for(uint8_t j = 0; j < 4; j++) {
595591
uint8_t p = oddparity8(tx_rx->rx_data[j]);
@@ -600,6 +596,12 @@ struct nonce_info_hard hard_nested_collect_nonces(
600596
pbits |= p;
601597
}
602598

599+
// update unique nonces
600+
if(!found[tx_rx->rx_data[0]]) {
601+
*first_byte_sum += evenparity32(pbits & 0x08);
602+
found[tx_rx->rx_data[0]]++;
603+
}
604+
603605
if(nt == previous) {
604606
same++;
605607
}

lib/nested/nested.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ struct nonce_info nested_attack(
6262
uint32_t distance,
6363
uint32_t delay);
6464

65-
struct nonce_info_hard hard_nested_collect_nonces(
65+
struct nonce_info_hard nested_hard_nonce_attack(
6666
FuriHalNfcTxRxContext* tx_rx,
6767
uint8_t blockNo,
6868
uint8_t keyType,
6969
uint8_t targetBlockNo,
7070
uint8_t targetKeyType,
7171
uint64_t ui64Key,
7272
uint32_t* found,
73+
uint32_t* first_byte_sum,
7374
Stream* file_stream);
7475

7576
uint32_t nested_calibrate_distance(

mifare_nested_i.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <lib/nfc/nfc_device.h>
2020
#include "mifare_nested_icons.h"
2121

22-
#define NESTED_VERSION_APP "1.2.5"
22+
#define NESTED_VERSION_APP "1.3.0"
2323
#define NESTED_GITHUB_LINK "https://github.com/AloneLiberty/FlipperNested"
2424
#define NESTED_RECOVER_KEYS_GITHUB_LINK "https://github.com/AloneLiberty/FlipperNestedRecovery"
2525
#define NESTED_NONCE_FORMAT_VERSION "3"

mifare_nested_worker.c

+52-28
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
#define TAG "MifareNestedWorker"
1515

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+
1620
void mifare_nested_worker_change_state(
1721
MifareNestedWorker* mifare_nested_worker,
1822
MifareNestedWorkerState state) {
@@ -468,7 +472,7 @@ uint32_t mifare_nested_worker_predict_delay(
468472
}
469473

470474
free(crypto);
471-
475+
472476
return 1;
473477
}
474478

@@ -876,50 +880,54 @@ void mifare_nested_worker_collect_nonces_hard(MifareNestedWorker* mifare_nested_
876880
continue;
877881
}
878882

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-
902883
while(!info->collected &&
903884
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;
904908
uint32_t* found = malloc(sizeof(uint32_t) * 256);
905909
for(uint32_t i = 0; i < 256; i++) {
906910
found[i] = 0;
907911
}
908912

909913
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(
911915
&tx_rx,
912916
key_block,
913917
found_key_type,
914918
mifare_nested_worker_get_block_by_sector(sector),
915919
key_type,
916920
key,
917921
found,
922+
&first_byte_sum,
918923
file_stream);
919924

920925
if(result.static_encrypted) {
921-
// TODO: Delete file?
922926
file_stream_close(file_stream);
927+
928+
storage_simply_remove(storage, furi_string_get_cstr(hardnested_file));
929+
930+
furi_string_free(hardnested_file);
923931
free(found);
924932
free(mf_data);
925933
nfc_deactivate();
@@ -946,6 +954,22 @@ void mifare_nested_worker_collect_nonces_hard(MifareNestedWorker* mifare_nested_
946954
FURI_LOG_D(TAG, "Found states: %lu", states);
947955

948956
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+
949973
info->collected = true;
950974
info->hardnested = true;
951975
nonces->cuid = result.cuid;
@@ -966,9 +990,9 @@ void mifare_nested_worker_collect_nonces_hard(MifareNestedWorker* mifare_nested_
966990
}
967991

968992
free(found);
993+
furi_string_free(hardnested_file);
994+
file_stream_close(file_stream);
969995
}
970-
971-
file_stream_close(file_stream);
972996
}
973997
}
974998

0 commit comments

Comments
 (0)