Skip to content

Commit 8427ec0

Browse files
noprotoskotopesgornekich
authored
MIFARE Classic Key Recovery Improvements (#3822)
* Initial structure for nonce collection * Nonce logging * Dictionary attack structure * Fix compilation * Identified method to reduce candidate states * Use EXT_PATH instead of ANY_PATH * Use median calibrated distance, collect parity bits * Modify parity collection * Fixed parity bit collection * Add note to fix nonce logging * Fix nonce logging * Clean redundant code * Fix valid_nonce * First attempt disambiguous nonce implementation * FM11RF08S backdoor detection * Initial accelerated dictionary attack for weak PRNGs * Refactor to nested dictionary attack * Renaming some variables * Hard PRNG support for accelerated dictionary attack * Update found keys, initial attempt * Update found keys, second attempt * Code cleanup * Misc bugfixes * Only use dicts in search_dicts_for_nonce_key if we have them * Collect nonces again * Should be detecting both backdoors now * Relocate backdoor detection * Hardnested support * Fix regression for regular nested attack * Backdoor read * Backdoor working up to calibration * Backdoor nested calibration * Don't recalibrate hard PRNG tags * Static encrypted nonce collection * Update TODO * NFC app UI updates, MVP * Bump f18 API version (all functions are NFC related) * Add new backdoor key, fix UI status update carrying over from previous read * Clear TODO line * Fix v1/v2 backdoor nonce collection * Speed up backdoor detection, alert on new backdoor * Add additional condition to backdoor check * I'll try freeing memory, that's a good trick! * Do not enter nested attack if card is already finished * Do not reset the poller between collected nonces * Clean up various issues * Fix Hardnested sector/key type logging * Add nested_target_key 64 to TODO * Implement progress bar for upgraded attacks in NFC app * Typo * Zero nested_target_key and msb_count on exit * Note TODO (malloc) * Dismiss duplicate nonces * Fix calibration (ensure values are within 3 standard deviations) * Log static * No nested dictionary attack re-entry * Note minor inefficiency * Uniformly use crypto1_ prefix for symbols in Crypto1 API * Fix include paths * Fix include paths cont * Support CUID dictionary * Fix log levels * Avoid storage errors, clean up temporary files * Handle invalid key candidates * Fix memory leak in static encrypted attack * Fix memory leak, use COUNT_OF macro * Use single call to free FuriString * Refactor enums to avoid redefinition * Fix multiple crashes and state machine logic * Fix inconsistent assignment of known key and known key type/sector * Backdoor known key logic still needs the current key * Larger data type for 4K support * Fix typo * Fix issue with resume logic * Mark TODOs for next PR * Remove redundant assignment * Fix size_t format specifier * Simplify auth_passed condition Co-authored-by: Aleksandr Kutuzov <[email protected]> Co-authored-by: gornekich <[email protected]>
1 parent f8fa71c commit 8427ec0

20 files changed

+1929
-106
lines changed

applications/debug/unit_tests/tests/nfc/nfc_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ NfcCommand mf_classic_poller_send_frame_callback(NfcGenericEventEx event, void*
496496
MfClassicKey key = {
497497
.data = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
498498
};
499-
error = mf_classic_poller_auth(instance, 0, &key, MfClassicKeyTypeA, NULL);
499+
error = mf_classic_poller_auth(instance, 0, &key, MfClassicKeyTypeA, NULL, false);
500500
frame_test->state = (error == MfClassicErrorNone) ?
501501
NfcTestMfClassicSendFrameTestStateReadBlock :
502502
NfcTestMfClassicSendFrameTestStateFail;

applications/main/nfc/nfc_app_i.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@
7474
#define NFC_APP_MFKEY32_LOGS_FILE_NAME ".mfkey32.log"
7575
#define NFC_APP_MFKEY32_LOGS_FILE_PATH (NFC_APP_FOLDER "/" NFC_APP_MFKEY32_LOGS_FILE_NAME)
7676

77-
#define NFC_APP_MF_CLASSIC_DICT_USER_PATH (NFC_APP_FOLDER "/assets/mf_classic_dict_user.nfc")
77+
#define NFC_APP_MF_CLASSIC_DICT_USER_PATH (NFC_APP_FOLDER "/assets/mf_classic_dict_user.nfc")
78+
#define NFC_APP_MF_CLASSIC_DICT_USER_NESTED_PATH \
79+
(NFC_APP_FOLDER "/assets/mf_classic_dict_user_nested.nfc")
7880
#define NFC_APP_MF_CLASSIC_DICT_SYSTEM_PATH (NFC_APP_FOLDER "/assets/mf_classic_dict.nfc")
81+
#define NFC_APP_MF_CLASSIC_DICT_SYSTEM_NESTED_PATH \
82+
(NFC_APP_FOLDER "/assets/mf_classic_dict_nested.nfc")
7983

8084
typedef enum {
8185
NfcRpcStateIdle,
@@ -93,6 +97,12 @@ typedef struct {
9397
bool is_key_attack;
9498
uint8_t key_attack_current_sector;
9599
bool is_card_present;
100+
MfClassicNestedPhase nested_phase;
101+
MfClassicPrngType prng_type;
102+
MfClassicBackdoor backdoor;
103+
uint16_t nested_target_key;
104+
uint16_t msb_count;
105+
bool enhanced_dict;
96106
} NfcMfClassicDictAttackContext;
97107

98108
struct NfcApp {

applications/main/nfc/scenes/nfc_scene_mf_classic_dict_attack.c

Lines changed: 122 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
#include "../nfc_app_i.h"
22

3+
#include <bit_lib/bit_lib.h>
34
#include <dolphin/dolphin.h>
4-
#include <lib/nfc/protocols/mf_classic/mf_classic_poller.h>
55

66
#define TAG "NfcMfClassicDictAttack"
77

8+
// TODO FL-3926: Fix lag when leaving the dictionary attack view after Hardnested
9+
// TODO FL-3926: Re-enters backdoor detection between user and system dictionary if no backdoor is found
10+
811
typedef enum {
12+
DictAttackStateCUIDDictInProgress,
913
DictAttackStateUserDictInProgress,
1014
DictAttackStateSystemDictInProgress,
1115
} DictAttackState;
@@ -29,7 +33,9 @@ NfcCommand nfc_dict_attack_worker_callback(NfcGenericEvent event, void* context)
2933
} else if(mfc_event->type == MfClassicPollerEventTypeRequestMode) {
3034
const MfClassicData* mfc_data =
3135
nfc_device_get_data(instance->nfc_device, NfcProtocolMfClassic);
32-
mfc_event->data->poller_mode.mode = MfClassicPollerModeDictAttack;
36+
mfc_event->data->poller_mode.mode = (instance->nfc_dict_context.enhanced_dict) ?
37+
MfClassicPollerModeDictAttackEnhanced :
38+
MfClassicPollerModeDictAttackStandard;
3339
mfc_event->data->poller_mode.data = mfc_data;
3440
instance->nfc_dict_context.sectors_total =
3541
mf_classic_get_total_sectors_num(mfc_data->type);
@@ -58,6 +64,11 @@ NfcCommand nfc_dict_attack_worker_callback(NfcGenericEvent event, void* context)
5864
instance->nfc_dict_context.sectors_read = data_update->sectors_read;
5965
instance->nfc_dict_context.keys_found = data_update->keys_found;
6066
instance->nfc_dict_context.current_sector = data_update->current_sector;
67+
instance->nfc_dict_context.nested_phase = data_update->nested_phase;
68+
instance->nfc_dict_context.prng_type = data_update->prng_type;
69+
instance->nfc_dict_context.backdoor = data_update->backdoor;
70+
instance->nfc_dict_context.nested_target_key = data_update->nested_target_key;
71+
instance->nfc_dict_context.msb_count = data_update->msb_count;
6172
view_dispatcher_send_custom_event(
6273
instance->view_dispatcher, NfcCustomEventDictAttackDataUpdate);
6374
} else if(mfc_event->type == MfClassicPollerEventTypeNextSector) {
@@ -117,19 +128,75 @@ static void nfc_scene_mf_classic_dict_attack_update_view(NfcApp* instance) {
117128
dict_attack_set_keys_found(instance->dict_attack, mfc_dict->keys_found);
118129
dict_attack_set_current_dict_key(instance->dict_attack, mfc_dict->dict_keys_current);
119130
dict_attack_set_current_sector(instance->dict_attack, mfc_dict->current_sector);
131+
dict_attack_set_nested_phase(instance->dict_attack, mfc_dict->nested_phase);
132+
dict_attack_set_prng_type(instance->dict_attack, mfc_dict->prng_type);
133+
dict_attack_set_backdoor(instance->dict_attack, mfc_dict->backdoor);
134+
dict_attack_set_nested_target_key(instance->dict_attack, mfc_dict->nested_target_key);
135+
dict_attack_set_msb_count(instance->dict_attack, mfc_dict->msb_count);
120136
}
121137
}
122138

123139
static void nfc_scene_mf_classic_dict_attack_prepare_view(NfcApp* instance) {
124140
uint32_t state =
125141
scene_manager_get_scene_state(instance->scene_manager, NfcSceneMfClassicDictAttack);
142+
if(state == DictAttackStateCUIDDictInProgress) {
143+
size_t cuid_len = 0;
144+
const uint8_t* cuid = nfc_device_get_uid(instance->nfc_device, &cuid_len);
145+
FuriString* cuid_dict_path = furi_string_alloc_printf(
146+
"%s/mf_classic_dict_%08lx.nfc",
147+
EXT_PATH("nfc/assets"),
148+
(uint32_t)bit_lib_bytes_to_num_be(cuid + (cuid_len - 4), 4));
149+
150+
do {
151+
if(!keys_dict_check_presence(furi_string_get_cstr(cuid_dict_path))) {
152+
state = DictAttackStateUserDictInProgress;
153+
break;
154+
}
155+
156+
instance->nfc_dict_context.dict = keys_dict_alloc(
157+
furi_string_get_cstr(cuid_dict_path),
158+
KeysDictModeOpenExisting,
159+
sizeof(MfClassicKey));
160+
161+
if(keys_dict_get_total_keys(instance->nfc_dict_context.dict) == 0) {
162+
keys_dict_free(instance->nfc_dict_context.dict);
163+
state = DictAttackStateUserDictInProgress;
164+
break;
165+
}
166+
167+
dict_attack_set_header(instance->dict_attack, "MF Classic CUID Dictionary");
168+
} while(false);
169+
170+
furi_string_free(cuid_dict_path);
171+
}
126172
if(state == DictAttackStateUserDictInProgress) {
127173
do {
174+
instance->nfc_dict_context.enhanced_dict = true;
175+
176+
if(keys_dict_check_presence(NFC_APP_MF_CLASSIC_DICT_SYSTEM_NESTED_PATH)) {
177+
storage_common_remove(
178+
instance->storage, NFC_APP_MF_CLASSIC_DICT_SYSTEM_NESTED_PATH);
179+
}
180+
if(keys_dict_check_presence(NFC_APP_MF_CLASSIC_DICT_SYSTEM_PATH)) {
181+
storage_common_copy(
182+
instance->storage,
183+
NFC_APP_MF_CLASSIC_DICT_SYSTEM_PATH,
184+
NFC_APP_MF_CLASSIC_DICT_SYSTEM_NESTED_PATH);
185+
}
186+
128187
if(!keys_dict_check_presence(NFC_APP_MF_CLASSIC_DICT_USER_PATH)) {
129188
state = DictAttackStateSystemDictInProgress;
130189
break;
131190
}
132191

192+
if(keys_dict_check_presence(NFC_APP_MF_CLASSIC_DICT_USER_NESTED_PATH)) {
193+
storage_common_remove(instance->storage, NFC_APP_MF_CLASSIC_DICT_USER_NESTED_PATH);
194+
}
195+
storage_common_copy(
196+
instance->storage,
197+
NFC_APP_MF_CLASSIC_DICT_USER_PATH,
198+
NFC_APP_MF_CLASSIC_DICT_USER_NESTED_PATH);
199+
133200
instance->nfc_dict_context.dict = keys_dict_alloc(
134201
NFC_APP_MF_CLASSIC_DICT_USER_PATH, KeysDictModeOpenAlways, sizeof(MfClassicKey));
135202
if(keys_dict_get_total_keys(instance->nfc_dict_context.dict) == 0) {
@@ -164,7 +231,7 @@ void nfc_scene_mf_classic_dict_attack_on_enter(void* context) {
164231
NfcApp* instance = context;
165232

166233
scene_manager_set_scene_state(
167-
instance->scene_manager, NfcSceneMfClassicDictAttack, DictAttackStateUserDictInProgress);
234+
instance->scene_manager, NfcSceneMfClassicDictAttack, DictAttackStateCUIDDictInProgress);
168235
nfc_scene_mf_classic_dict_attack_prepare_view(instance);
169236
dict_attack_set_card_state(instance->dict_attack, true);
170237
view_dispatcher_switch_to_view(instance->view_dispatcher, NfcViewDictAttack);
@@ -193,7 +260,21 @@ bool nfc_scene_mf_classic_dict_attack_on_event(void* context, SceneManagerEvent
193260
scene_manager_get_scene_state(instance->scene_manager, NfcSceneMfClassicDictAttack);
194261
if(event.type == SceneManagerEventTypeCustom) {
195262
if(event.event == NfcCustomEventDictAttackComplete) {
196-
if(state == DictAttackStateUserDictInProgress) {
263+
bool ran_nested_dict = instance->nfc_dict_context.nested_phase !=
264+
MfClassicNestedPhaseNone;
265+
if(state == DictAttackStateCUIDDictInProgress) {
266+
nfc_poller_stop(instance->poller);
267+
nfc_poller_free(instance->poller);
268+
keys_dict_free(instance->nfc_dict_context.dict);
269+
scene_manager_set_scene_state(
270+
instance->scene_manager,
271+
NfcSceneMfClassicDictAttack,
272+
DictAttackStateUserDictInProgress);
273+
nfc_scene_mf_classic_dict_attack_prepare_view(instance);
274+
instance->poller = nfc_poller_alloc(instance->nfc, NfcProtocolMfClassic);
275+
nfc_poller_start(instance->poller, nfc_dict_attack_worker_callback, instance);
276+
consumed = true;
277+
} else if(state == DictAttackStateUserDictInProgress && !(ran_nested_dict)) {
197278
nfc_poller_stop(instance->poller);
198279
nfc_poller_free(instance->poller);
199280
keys_dict_free(instance->nfc_dict_context.dict);
@@ -222,7 +303,27 @@ bool nfc_scene_mf_classic_dict_attack_on_event(void* context, SceneManagerEvent
222303
} else if(event.event == NfcCustomEventDictAttackSkip) {
223304
const MfClassicData* mfc_data = nfc_poller_get_data(instance->poller);
224305
nfc_device_set_data(instance->nfc_device, NfcProtocolMfClassic, mfc_data);
225-
if(state == DictAttackStateUserDictInProgress) {
306+
bool ran_nested_dict = instance->nfc_dict_context.nested_phase !=
307+
MfClassicNestedPhaseNone;
308+
if(state == DictAttackStateCUIDDictInProgress) {
309+
if(instance->nfc_dict_context.is_card_present) {
310+
nfc_poller_stop(instance->poller);
311+
nfc_poller_free(instance->poller);
312+
keys_dict_free(instance->nfc_dict_context.dict);
313+
scene_manager_set_scene_state(
314+
instance->scene_manager,
315+
NfcSceneMfClassicDictAttack,
316+
DictAttackStateUserDictInProgress);
317+
nfc_scene_mf_classic_dict_attack_prepare_view(instance);
318+
instance->poller = nfc_poller_alloc(instance->nfc, NfcProtocolMfClassic);
319+
nfc_poller_start(instance->poller, nfc_dict_attack_worker_callback, instance);
320+
} else {
321+
nfc_scene_mf_classic_dict_attack_notify_read(instance);
322+
scene_manager_next_scene(instance->scene_manager, NfcSceneReadSuccess);
323+
dolphin_deed(DolphinDeedNfcReadSuccess);
324+
}
325+
consumed = true;
326+
} else if(state == DictAttackStateUserDictInProgress && !(ran_nested_dict)) {
226327
if(instance->nfc_dict_context.is_card_present) {
227328
nfc_poller_stop(instance->poller);
228329
nfc_poller_free(instance->poller);
@@ -240,7 +341,7 @@ bool nfc_scene_mf_classic_dict_attack_on_event(void* context, SceneManagerEvent
240341
dolphin_deed(DolphinDeedNfcReadSuccess);
241342
}
242343
consumed = true;
243-
} else if(state == DictAttackStateSystemDictInProgress) {
344+
} else {
244345
nfc_scene_mf_classic_dict_attack_notify_read(instance);
245346
scene_manager_next_scene(instance->scene_manager, NfcSceneReadSuccess);
246347
dolphin_deed(DolphinDeedNfcReadSuccess);
@@ -262,7 +363,7 @@ void nfc_scene_mf_classic_dict_attack_on_exit(void* context) {
262363

263364
dict_attack_reset(instance->dict_attack);
264365
scene_manager_set_scene_state(
265-
instance->scene_manager, NfcSceneMfClassicDictAttack, DictAttackStateUserDictInProgress);
366+
instance->scene_manager, NfcSceneMfClassicDictAttack, DictAttackStateCUIDDictInProgress);
266367

267368
keys_dict_free(instance->nfc_dict_context.dict);
268369

@@ -275,6 +376,20 @@ void nfc_scene_mf_classic_dict_attack_on_exit(void* context) {
275376
instance->nfc_dict_context.is_key_attack = false;
276377
instance->nfc_dict_context.key_attack_current_sector = 0;
277378
instance->nfc_dict_context.is_card_present = false;
379+
instance->nfc_dict_context.nested_phase = MfClassicNestedPhaseNone;
380+
instance->nfc_dict_context.prng_type = MfClassicPrngTypeUnknown;
381+
instance->nfc_dict_context.backdoor = MfClassicBackdoorUnknown;
382+
instance->nfc_dict_context.nested_target_key = 0;
383+
instance->nfc_dict_context.msb_count = 0;
384+
instance->nfc_dict_context.enhanced_dict = false;
385+
386+
// Clean up temporary files used for nested dictionary attack
387+
if(keys_dict_check_presence(NFC_APP_MF_CLASSIC_DICT_USER_NESTED_PATH)) {
388+
storage_common_remove(instance->storage, NFC_APP_MF_CLASSIC_DICT_USER_NESTED_PATH);
389+
}
390+
if(keys_dict_check_presence(NFC_APP_MF_CLASSIC_DICT_SYSTEM_NESTED_PATH)) {
391+
storage_common_remove(instance->storage, NFC_APP_MF_CLASSIC_DICT_SYSTEM_NESTED_PATH);
392+
}
278393

279394
nfc_blink_stop(instance);
280395
notification_message(instance->notifications, &sequence_display_backlight_enforce_auto);

0 commit comments

Comments
 (0)