From 42b8fb836705317e4c2966156f983b36dcc5f3e8 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 22 Sep 2026 14:22:18 +0200 Subject: [PATCH] hf mf: carry --suffix into the FM11RF08S nonce recovery `hf mf autopwn --suffix ` names its output files `hf-mf---`, but on an FM11RF08S the suffix was dropped. autopwn detects the static encrypted nonce and returns into HFMFSENRecover from five separate places, all of them before its own file naming runs, and the recovery path built `hf-mf--key` and `hf-mf--dump` from the card UID alone. Two runs against the same card overwrote each other, which is the collision the suffix exists to prevent. The suffix now travels with the call, so a card autopwn hands over is named the same way as one autopwn finishes itself. `hf mf sen` gains the same `--suffix` option, so the naming no longer depends on which command reached the recovery. It covers the `--keep-nonces` evidence file too, which collided the same way. The three names are built by fm11_build_filename() instead of by three snprintf calls that have to agree on the template. Co-Authored-By: Claude Opus 5 (1M context) --- CHANGELOG.md | 2 + client/src/cmdhfmf.c | 14 ++-- client/src/cmdhfmfsen.c | 168 +++++++++++++++++++++++++++++++++------- client/src/cmdhfmfsen.h | 2 +- 4 files changed, 150 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 789bc9b74..b7039ba93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Added `hf mf sen --suffix` - generated files can be named `hf-mf---` (@iceman1001) +- Fixed `hf mf autopwn --suffix` on FM11RF08S - the suffix is now forwarded to the static encrypted nonce recovery instead of being dropped (@iceman1001) - Fixed `hf mf sen` and `hf mf autopwn` on FM11RF08S - the recovered card dump is now saved as JSON as well as BIN (@iceman1001) - Fixed `hf mfdes chk` - a key given with `--key` is now tried every round instead of being overwritten by the dictionary and pattern fills (@iceman1001) - Fixed `hf mfdes chk` - when key settings are unreadable, key numbers are found with GetKeyVersion instead of the file access rights, which are gated by the same bit (@iceman1001) diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index 80972625b..8cc087090 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -72,9 +72,9 @@ typedef struct { static int CmdHelp(const char *Cmd); -static int HFMFAutoPwnSEN(sector_t *e_sector, size_t sector_cnt) { +static int HFMFAutoPwnSEN(sector_t *e_sector, size_t sector_cnt, const char *suffix) { DropField(); - return HFMFSENRecover(false, false, false, false, 0, 0x1, true, e_sector, sector_cnt); + return HFMFSENRecover(false, false, false, false, 0, 0x1, true, e_sector, sector_cnt, suffix); } // Static array for Saflok key levels @@ -3367,7 +3367,7 @@ static int CmdHF14AMfAutoPWN(const char *Cmd) { } if (has_staticnonce == NONCE_STATIC_ENC) { - int sen_res = HFMFAutoPwnSEN(e_sector, sector_cnt); + int sen_res = HFMFAutoPwnSEN(e_sector, sector_cnt, outfnlen ? outfilename : NULL); free(e_sector); free(fptr); return sen_res; @@ -3537,7 +3537,7 @@ static int CmdHF14AMfAutoPWN(const char *Cmd) { if (known_key && ((has_staticnonce == NONCE_NORMAL) || (has_staticnonce == NONCE_FAIL))) { has_staticnonce = detect_classic_static_encrypted_nonce(mfFirstBlockOfSector(sectorno), keytype, key); if (has_staticnonce == NONCE_STATIC_ENC) { - int sen_res = HFMFAutoPwnSEN(e_sector, sector_cnt); + int sen_res = HFMFAutoPwnSEN(e_sector, sector_cnt, outfnlen ? outfilename : NULL); free(keyBlock); free(e_sector); free(fptr); @@ -3613,7 +3613,7 @@ noValidKeyFound: if (known_key && has_staticnonce == NONCE_NORMAL) { has_staticnonce = detect_classic_static_encrypted_nonce(mfFirstBlockOfSector(sectorno), keytype, key); if (has_staticnonce == NONCE_STATIC_ENC) { - int sen_res = HFMFAutoPwnSEN(e_sector, sector_cnt); + int sen_res = HFMFAutoPwnSEN(e_sector, sector_cnt, outfnlen ? outfilename : NULL); free(keyBlock); free(e_sector); free(fptr); @@ -3803,7 +3803,7 @@ tryNested: case PM3_ESTATIC_NONCE: { e_sector[current_sector_i].Key[current_key_type_i] = 0xffffffffffff; e_sector[current_sector_i].foundKey[current_key_type_i] = false; - int sen_res = HFMFAutoPwnSEN(e_sector, sector_cnt); + int sen_res = HFMFAutoPwnSEN(e_sector, sector_cnt, outfnlen ? outfilename : NULL); free(e_sector); free(fptr); return sen_res; @@ -3861,7 +3861,7 @@ tryHardnested: // If the nested attack fails then we try the hardnested attack case PM3_ESTATIC_NONCE: { e_sector[current_sector_i].Key[current_key_type_i] = 0xffffffffffff; e_sector[current_sector_i].foundKey[current_key_type_i] = false; - int sen_res = HFMFAutoPwnSEN(e_sector, sector_cnt); + int sen_res = HFMFAutoPwnSEN(e_sector, sector_cnt, outfnlen ? outfilename : NULL); free(e_sector); free(fptr); return sen_res; diff --git a/client/src/cmdhfmfsen.c b/client/src/cmdhfmfsen.c index 7f4a48d37..bca95d28b 100644 --- a/client/src/cmdhfmfsen.c +++ b/client/src/cmdhfmfsen.c @@ -2729,12 +2729,24 @@ static void fm11_print_key_table(const uint64_t keys_found[FM11RF08S_SECTORS][2] PrintAndLogEx(NORMAL, ""); } +// `hf-mf--[-]`, matching the template `hf mf autopwn` uses +static void fm11_build_filename(char *dst, size_t dst_len, const iso14a_card_select_t *card, const char *base, const char *suffix) { + + snprintf(dst, dst_len, "hf-mf-%s-%s%s%s" + , sprint_hex_inrow(card->uid, card->uidlen) + , base + , (suffix != NULL && suffix[0] != '\0') ? "-" : "" + , (suffix != NULL) ? suffix : "" + ); +} + static int fm11_save_recovery_outputs(const iso14a_card_select_t *card, const iso14a_fm11rf08s_nonces_with_data_t *nonces, const uint64_t keys_found[FM11RF08S_SECTORS][2], const bool found_key[FM11RF08S_SECTORS][2], - bool no_oob, bool has_data) { + bool no_oob, bool has_data, const char *suffix) { + char fn[FILE_PATH_SIZE] = {0}; - snprintf(fn, sizeof(fn), "hf-mf-%s-key", sprint_hex_inrow(card->uid, card->uidlen)); + fm11_build_filename(fn, sizeof(fn), card, "key", suffix); const uint64_t unknown_key = bytes_to_num(g_mifare_default_key, MIFARE_KEY_SIZE); uint8_t sector_count = FM11RF08S_NORMAL_SECTORS + (no_oob ? 0 : 1); @@ -2771,7 +2783,7 @@ static int fm11_save_recovery_outputs(const iso14a_card_select_t *card, const is num_to_bytes(ka, MIFARE_KEY_SIZE, dump + trailer * MFBLOCK_SIZE); num_to_bytes(kb, MIFARE_KEY_SIZE, dump + trailer * MFBLOCK_SIZE + 10); } - snprintf(fn, sizeof(fn), "hf-mf-%s-dump", sprint_hex_inrow(card->uid, card->uidlen)); + fm11_build_filename(fn, sizeof(fn), card, "dump", suffix); return pm3_save_mf_dump(fn, dump, sizeof(dump), jsfCardMemory); } @@ -2869,7 +2881,9 @@ static int fm11_select_mifare_classic(iso14a_card_select_t *card_out) { return PM3_SUCCESS; } -int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline_only, int max_online_candidates, uint8_t parity_mask, bool skip_default_key_check, const sector_t *known_sectors, size_t known_sector_count) { +int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline_only, + int max_online_candidates, uint8_t parity_mask, bool skip_default_key_check, + const sector_t *known_sectors, size_t known_sector_count, const char *suffix) { iso14a_card_select_t card = {0}; iso14a_fm11rf08s_nonces_with_data_t nonces = {0}; @@ -2930,15 +2944,18 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline } uint32_t nonce_count = FM11RF08S_SECTORS * 2; - char activity[80]; - snprintf(activity, sizeof(activity), "Loaded card UID %08X using %s key %s", uid, - collected_with_backdoor ? "backdoor" : "dictionary", - sprint_hex_inrow(active_key, MIFARE_KEY_SIZE)); + char activity[80] = {0}; + snprintf(activity, sizeof(activity), "Loaded card UID %08X using %s key %s" + , uid + , collected_with_backdoor ? "backdoor" : "dictionary" + , sprint_hex_inrow(active_key, MIFARE_KEY_SIZE) + ); + fm11_sen_progress(nonce_count, activity, 0, 0); + if (keep_nonces) { char nonce_fn[FILE_PATH_SIZE] = {0}; - snprintf(nonce_fn, sizeof(nonce_fn), "hf-mf-%s-nonces%s", sprint_hex_inrow(card.uid, card.uidlen), - collected_with_data ? "_with_data" : ""); + fm11_build_filename(nonce_fn, sizeof(nonce_fn), &card, collected_with_data ? "nonces_with_data" : "nonces", suffix); pm3_save_fm11rf08s_nonces(nonce_fn, &nonces, collected_with_data); fm11_sen_progress(nonce_count, "Saved nonce/data evidence JSON", 0, 0); } @@ -2967,6 +2984,7 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline (void)fm11_keylist_add_unique(&confirmed_reuse_keys, known_key); } } + if (seeded_known > 0) { fm11_sen_progress(nonce_count, "Seed known keys from earlier autopwn stages", seeded_known, 0); } @@ -2989,24 +3007,30 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline if (skip_default_key_check) { fm11_sen_progress(nonce_count, "Skip default-key dictionary check already done by autopwn", seeded_known, 0); } else { + fm11_sen_progress(nonce_count, "Check default-key dictionary against nonce evidence", default_keys.count, 0); + retval = fm11_check_default_keys(uid, nonce_count, &nonces, &default_keys, parity_mask, keys_found, found_key); if (retval != PM3_SUCCESS) { goto out; } + for (uint8_t sec = 0; sec < FM11RF08S_SECTORS; sec++) { for (uint8_t kt = 0; kt < 2; kt++) { if (found_key[sec][kt] == false) { continue; } + uint64_t key = keys_found[sec][kt] & 0xFFFFFFFFFFFFULL; if (fm11_keylist_has_key(&confirmed_reuse_keys, key)) { continue; } + retval = fm11_keylist_add_unique(&confirmed_reuse_keys, key); if (retval != PM3_SUCCESS) { goto out; } + uint32_t reuse_found = fm11_propagate_key_reuse_online(nonce_count, key, keys_found, found_key); if (reuse_found > 0) { snprintf(activity, sizeof(activity), "Key re-use propagation confirmed %u additional key slots", reuse_found); @@ -3022,12 +3046,16 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline } fm11_sen_progress(nonce_count, "Generate first-pass candidates from nT/{nT}/parity", 0, 0); + for (uint8_t sec = 0; sec < FM11RF08S_SECTORS; sec++) { + uint8_t real_sec = fm11_real_sector(sec); for (uint8_t kt = 0; kt < 2; kt++) { + if (found_key[sec][kt]) { continue; } + uint32_t nt = fm11_bytes_to_u32(nonces.nt[sec][kt]); uint32_t nt_enc = fm11_bytes_to_u32(nonces.nt_enc[sec][kt]); int res = fm11_generate_1nt_candidates(uid, nt, nt_enc, nonces.par_err[sec][kt], parity_mask, &candidates[sec][kt]); @@ -3037,19 +3065,23 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline } fm11_prioritize_defaults(&candidates[sec][kt], &default_keys); } + uint32_t nt_a = fm11_bytes_to_u32(nonces.nt[sec][0]); uint32_t nt_b = fm11_bytes_to_u32(nonces.nt[sec][1]); + if (nt_a != nt_b && found_key[sec][0] == false && found_key[sec][1] == false) { int res = fm11_intersect_pair(nt_a, &candidates[sec][0], nt_b, &candidates[sec][1]); if (res != PM3_SUCCESS) { retval = res; goto out; } + res = fm11_prioritize_by_paired_seed(&candidates[sec][0], &candidates[sec][1]); if (res != PM3_SUCCESS) { retval = res; goto out; } + res = fm11_prioritize_by_paired_seed(&candidates[sec][1], &candidates[sec][0]); if (res != PM3_SUCCESS) { retval = res; @@ -3057,27 +3089,37 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline } fm11_sort_candidates_by_seed_bucket(&candidates[sec][0], &candidates[sec][1]); } + fm11_prioritize_defaults(&candidates[sec][0], &default_keys); fm11_prioritize_defaults(&candidates[sec][1], &default_keys); + if (real_sec == 32) { fm11_prioritize_0000_prefix(&candidates[sec][1]); } - snprintf(activity, sizeof(activity), "Prepared sec %03u candidates (A %u / B %u)", - real_sec, candidates[sec][0].count, candidates[sec][1].count); + snprintf(activity, sizeof(activity), "Prepared sec %03u candidates (A %u / B %u)" + , real_sec + , candidates[sec][0].count + , candidates[sec][1].count + ); fm11_sen_progress(nonce_count, activity, candidates[sec][0].count + candidates[sec][1].count, 0); } if (reader_mode) { + uint32_t accepted = fm11_apply_reader_material(&nonces, candidates, keys_found, found_key, reader_keys, reader_found); if (accepted > 0) { + snprintf(activity, sizeof(activity), "Applied %u reader-recovered key%s", accepted, (accepted == 1) ? "" : "s"); fm11_sen_progress(nonce_count, activity, accepted, 0); + uint32_t reduced = 0; uint32_t derived = fm11_offline_fixpoint(&nonces, candidates, keys_found, found_key, &reduced, offline_only); + if (reduced > 0) { snprintf(activity, sizeof(activity), "Reduced paired candidate sets by %u keys", reduced); fm11_sen_progress(nonce_count, activity, reduced, 0); } + if (derived > 0) { snprintf(activity, sizeof(activity), "Derived %u paired keys from reader material", derived); fm11_sen_progress(nonce_count, activity, derived, 0); @@ -3090,12 +3132,14 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline total_candidates += candidates[sec][0].count; total_candidates += candidates[sec][1].count; } + uint32_t duplicate_candidates = 0; uint32_t duplicate_prefix[FM11RF08S_SECTORS][2] = {{0}}; retval = fm11_prioritize_duplicates(candidates, duplicate_prefix, &duplicate_candidates); if (retval != PM3_SUCCESS) { goto out; } + uint32_t verify_eta = (total_candidates / 2 / FM11RF08S_FCHK_KEYS_PER_SECOND) + 5; snprintf(activity, sizeof(activity), "Prioritized candidates (%u duplicate-priority)", duplicate_candidates); fm11_sen_progress(nonce_count, activity, total_candidates, verify_eta); @@ -3104,6 +3148,7 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline if (retval != PM3_SUCCESS) { goto out; } + snprintf(activity, sizeof(activity), "Indexed %u candidate refs for key-reuse cascades", reuse_index.ref_count); fm11_sen_progress(nonce_count, activity, reuse_index.ref_count, 0); @@ -3114,22 +3159,29 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline fm11_keylist_free(&priority); goto out; } + uint32_t found_before = fm11_count_found_keys(found_key); uint32_t priority_count = priority.count; + if (priority_count > 0) { - snprintf(activity, sizeof(activity), "Batch-check top %u/%u cross-sector reuse keys with fchk", - priority_count, reuse_eligible); + + snprintf(activity, sizeof(activity), "Batch-check top %u/%u cross-sector reuse keys with fchk", priority_count, reuse_eligible); + fm11_sen_progress(nonce_count, activity, priority_count, (priority_count / FM11RF08S_FCHK_KEYS_PER_SECOND) + 2); + retval = fm11_verify_global_priority_keys(&priority, keys_found, found_key); fm11_keylist_free(&priority); + if (retval != PM3_SUCCESS) { goto out; } + uint32_t found_after = fm11_count_found_keys(found_key); if (found_after > found_before) { snprintf(activity, sizeof(activity), "Reuse-priority fchk recovered %u normal-sector keys", found_after - found_before); fm11_sen_progress(nonce_count, activity, found_after - found_before, 0); } + } else { fm11_keylist_free(&priority); } @@ -3140,21 +3192,29 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline fm11_keylist_free(&priority); goto out; } + found_before = fm11_count_found_keys(found_key); priority_count = priority.count; + if (priority_count > 0 && priority_count <= FM11RF08S_MAX_GLOBAL_PRIORITY_KEYS) { + snprintf(activity, sizeof(activity), "Batch-check %u duplicate-priority keys with fchk", priority_count); fm11_sen_progress(nonce_count, activity, priority_count, (priority_count / FM11RF08S_FCHK_KEYS_PER_SECOND) + 2); + retval = fm11_verify_global_priority_keys(&priority, keys_found, found_key); + fm11_keylist_free(&priority); + if (retval != PM3_SUCCESS) { goto out; } + uint32_t found_after = fm11_count_found_keys(found_key); if (found_after > found_before) { snprintf(activity, sizeof(activity), "Batch fchk recovered %u normal-sector keys", found_after - found_before); fm11_sen_progress(nonce_count, activity, found_after - found_before, 0); } + } else { fm11_keylist_free(&priority); if (priority_count > FM11RF08S_MAX_GLOBAL_PRIORITY_KEYS) { @@ -3162,6 +3222,7 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline fm11_sen_progress(nonce_count, activity, priority_count, 0); } } + uint32_t propagated = fm11_propagate_found_keys(&nonces, candidates, keys_found, found_key); if (propagated > 0) { snprintf(activity, sizeof(activity), "Propagated %u reused candidate key matches", propagated); @@ -3174,6 +3235,7 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline snprintf(activity, sizeof(activity), "Reduced paired candidate sets by %u keys", reduced); fm11_sen_progress(nonce_count, activity, reduced, 0); } + if (derived > 0) { snprintf(activity, sizeof(activity), "Derived %u paired keys from matching FM11RF08S seeds", derived); fm11_sen_progress(nonce_count, activity, derived, 0); @@ -3183,22 +3245,24 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline if (retval != PM3_SUCCESS) { goto out; } + snprintf(activity, sizeof(activity), "Refreshed %u candidate refs for key-reuse cascades", reuse_index.ref_count); fm11_sen_progress(nonce_count, activity, reuse_index.ref_count, 0); total_candidates = fm11_count_unfound_candidates(candidates, found_key); if (max_online_candidates > 0 && (int)total_candidates > max_online_candidates) { - PrintAndLogEx(WARNING, "Total unfound candidates (%u) exceeds --max-online-candidates limit (%d), stopping", - total_candidates, max_online_candidates); + PrintAndLogEx(WARNING, "Total unfound candidates (%u) exceeds --max-online-candidates limit (%d), stopping", total_candidates, max_online_candidates); goto out; } + if (offline_only) { fm11_sen_progress_footer(); fm11_print_key_table(keys_found, found_key); - retval = fm11_save_recovery_outputs(&card, &nonces, keys_found, found_key, no_oob, collected_with_data); + retval = fm11_save_recovery_outputs(&card, &nonces, keys_found, found_key, no_oob, collected_with_data, suffix); PrintAndLogEx(SUCCESS, "time in sen " _YELLOW_("%.0f") " seconds", (float)(msclock() - t1) / 1000.0); goto out; } + verify_eta = (total_candidates / 2 / FM11RF08S_FCHK_KEYS_PER_SECOND) + 5; fm11_sen_progress(nonce_count, "Measurement: score unresolved candidate paths", total_candidates, 0); fm11_measure_candidate_paths(&nonces, candidates, keys_found, found_key, nonce_count); @@ -3210,20 +3274,25 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline */ bool exhausted[FM11RF08S_SECTORS][2] = {{false}}; uint32_t probe_pos = 0; + while (true) { + if (kbd_enter_pressed()) { retval = PM3_EOPABORTED; goto out; } - /* find the cheapest unfound, non-exhausted (sec, kt) pair */ + + // find the cheapest unfound, non-exhausted (sec, kt) pair uint8_t best_sec = 0xFF, best_kt = 0; uint32_t best_cost = UINT32_MAX; uint32_t best_count = UINT32_MAX; + for (uint8_t sec = 0; sec < FM11RF08S_SECTORS; sec++) { for (uint8_t kt = 0; kt < 2; kt++) { if (found_key[sec][kt] || exhausted[sec][kt]) { continue; } + uint32_t cnt = candidates[sec][kt].count; uint32_t cost = fm11_estimate_candidate_path_cost(&nonces, candidates, keys_found, found_key, sec, kt); if (sec == FM11RF08S_SECTORS - 1 && kt == 1 && cnt > 0) { @@ -3232,6 +3301,7 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline cost = best_cost > 0 ? best_cost - 1 : 0; } } + if (cost < best_cost || (cost == best_cost && cnt < best_count)) { best_cost = cost; best_count = cnt; @@ -3240,87 +3310,112 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline } } } + + // all sectors/keys found or exhausted if (best_sec == 0xFF) { - break; /* all sectors/keys found or exhausted */ + break; } + uint8_t sec = best_sec; uint8_t kt = best_kt; uint8_t real_sec = fm11_real_sector(sec); + if (found_key[sec][kt ^ 1]) { + uint32_t nt_target = fm11_bytes_to_u32(nonces.nt[sec][kt]); uint32_t nt_known = fm11_bytes_to_u32(nonces.nt[sec][kt ^ 1]); + if (nt_target != nt_known) { + uint16_t seed = fm11_compute_seednt16_nt32(nt_known, keys_found[sec][kt ^ 1]); uint32_t old_count = 0; uint32_t new_count = 0; + int filter_res = fm11_filter_candidates_by_seed(&candidates[sec][kt], nt_target, seed, &old_count, &new_count); if (filter_res != PM3_SUCCESS) { retval = filter_res; goto out; } + if (old_count > new_count) { snprintf(activity, sizeof(activity), "Reduced paired candidate set by %u keys", old_count - new_count); fm11_sen_progress(nonce_count, activity, old_count - new_count, 0); } + } else { fm11_keylist_promote_existing(&candidates[sec][kt], keys_found[sec][kt ^ 1]); } } - snprintf(activity, sizeof(activity), "sec %03u key %c - checking %u candidates", real_sec, kt ? 'B' : 'A', candidates[sec][kt].count); - fm11_sen_progress(nonce_count, activity, candidates[sec][kt].count, - (candidates[sec][kt].count / 2 / FM11RF08S_FCHK_KEYS_PER_SECOND) + 1); + snprintf(activity, sizeof(activity), "sec %03u key %c - checking %u candidates", real_sec, kt ? 'B' : 'A', candidates[sec][kt].count); + fm11_sen_progress(nonce_count, activity, candidates[sec][kt].count, (candidates[sec][kt].count / 2 / FM11RF08S_FCHK_KEYS_PER_SECOND) + 1); + uint64_t key = 0; int res = fm11_verify_candidates(real_sec, kt, &candidates[sec][kt], &key); if (res == PM3_SUCCESS) { + propagated = fm11_accept_found_key_global(&nonces, candidates, keys_found, found_key, &reuse_index, sec, kt, key, &probe_queue); fm11_print_key_hit_row(nonce_count, sec, kt, key, found_key); + uint32_t reuse_found = 0; if (fm11_keylist_has_key(&confirmed_reuse_keys, key) == false) { (void)fm11_keylist_add_unique(&confirmed_reuse_keys, key); reuse_found = fm11_propagate_key_reuse_online(nonce_count, key, keys_found, found_key); } + if (reuse_found > 0) { snprintf(activity, sizeof(activity), "Key re-use propagation confirmed %u additional key slots", reuse_found); fm11_sen_progress(nonce_count, activity, reuse_found, 0); propagated += fm11_accept_found_key_global(&nonces, candidates, keys_found, found_key, &reuse_index, sec, kt, key, &probe_queue); } + if (propagated > 1) { snprintf(activity, sizeof(activity), "Propagated %u reused candidate key matches", propagated - 1); fm11_sen_progress(nonce_count, activity, propagated - 1, 0); } + reduced = 0; derived = fm11_offline_fixpoint(&nonces, candidates, keys_found, found_key, &reduced, false); if (reduced > 0) { snprintf(activity, sizeof(activity), "Reduced paired candidate sets by %u keys", reduced); fm11_sen_progress(nonce_count, activity, reduced, 0); } + if (derived > 0) { snprintf(activity, sizeof(activity), "Derived %u additional paired keys", derived); fm11_sen_progress(nonce_count, activity, derived, 0); } + while (probe_pos < probe_queue.count) { uint8_t ps = probe_queue.entries[probe_pos].sec; uint8_t pk = probe_queue.entries[probe_pos].key_type; uint64_t pkey = probe_queue.entries[probe_pos].key; + probe_pos++; if (found_key[ps][pk]) { continue; } + fm11_keylist_t single = {0}; + if (fm11_keylist_push(&single, pkey, 0) == PM3_SUCCESS) { + uint8_t real_ps = fm11_real_sector(ps); uint64_t out_key = 0; + if (fm11_verify_candidates(real_ps, pk, &single, &out_key) == PM3_SUCCESS) { + uint32_t prop = fm11_accept_found_key_global(&nonces, candidates, keys_found, found_key, &reuse_index, ps, pk, out_key, &probe_queue); if (prop > 0) { fm11_print_key_hit_row(nonce_count, ps, pk, out_key, found_key); } + uint32_t reuse_extra = 0; if (fm11_keylist_has_key(&confirmed_reuse_keys, out_key) == false) { (void)fm11_keylist_add_unique(&confirmed_reuse_keys, out_key); reuse_extra = fm11_propagate_key_reuse_online(nonce_count, out_key, keys_found, found_key); } + if (reuse_extra > 0) { snprintf(activity, sizeof(activity), "Key re-use propagation confirmed %u additional key slots", reuse_extra); fm11_sen_progress(nonce_count, activity, reuse_extra, 0); @@ -3330,37 +3425,46 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline } } total_candidates = fm11_count_unfound_candidates(candidates, found_key); + } else if (res == PM3_ETIMEOUT || res == PM3_EOPABORTED) { retval = res; goto out; } else { - /* candidate set exhausted without finding a key */ + // candidate set exhausted without finding a key exhausted[sec][kt] = true; } } - /* drain probe queue: verify single-key probes scheduled by fm11_accept_found_key_global */ + // drain probe queue: verify single-key probes scheduled by fm11_accept_found_key_global for (uint32_t pi = probe_pos; pi < probe_queue.count; pi++) { + uint8_t ps = probe_queue.entries[pi].sec; uint8_t pk = probe_queue.entries[pi].key_type; uint64_t pkey = probe_queue.entries[pi].key; + if (found_key[ps][pk]) { continue; } + fm11_keylist_t single = {0}; if (fm11_keylist_push(&single, pkey, 0) == PM3_SUCCESS) { + uint8_t real_ps = fm11_real_sector(ps); uint64_t out_key = 0; + if (fm11_verify_candidates(real_ps, pk, &single, &out_key) == PM3_SUCCESS) { + uint32_t prop = fm11_accept_found_key_global(&nonces, candidates, keys_found, found_key, &reuse_index, ps, pk, out_key, &probe_queue); if (prop > 0) { fm11_print_key_hit_row(nonce_count, ps, pk, out_key, found_key); } + uint32_t reuse_extra = 0; if (fm11_keylist_has_key(&confirmed_reuse_keys, out_key) == false) { (void)fm11_keylist_add_unique(&confirmed_reuse_keys, out_key); reuse_extra = fm11_propagate_key_reuse_online(nonce_count, out_key, keys_found, found_key); } + if (reuse_extra > 0) { snprintf(activity, sizeof(activity), "Key re-use propagation confirmed %u additional key slots", reuse_extra); fm11_sen_progress(nonce_count, activity, reuse_extra, 0); @@ -3372,7 +3476,7 @@ int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline fm11_sen_progress_footer(); fm11_print_key_table(keys_found, found_key); - retval = fm11_save_recovery_outputs(&card, &nonces, keys_found, found_key, no_oob, collected_with_data); + retval = fm11_save_recovery_outputs(&card, &nonces, keys_found, found_key, no_oob, collected_with_data, suffix); PrintAndLogEx(SUCCESS, "time in sen " _YELLOW_("%.0f") " seconds", (float)(msclock() - t1) / 1000.0); out: @@ -3393,7 +3497,8 @@ int CmdHF14AMfSEN(const char *Cmd) { "hf mf sen\n" "hf mf sen --keep-nonces\n" "hf mf sen --no-oob\n" - "hf mf sen --reader\n"); + "hf mf sen --reader\n" + "hf mf sen --suffix mycard\n"); void *argtable[] = { arg_param_begin, arg_lit0(NULL, "keep-nonces", "save collected nonce/data JSON evidence"), @@ -3403,6 +3508,7 @@ int CmdHF14AMfSEN(const char *Cmd) { arg_lit0(NULL, "online-confirm", "only do online confirmation (skip generation if nonces loaded)"), arg_int0(NULL, "max-online-candidates", "", "abort online phase if total candidates exceed this limit"), arg_str0(NULL, "parity-mask", "", "parity filter mask 1..F (default 1 = vetted staticnested_1nt behavior)"), + arg_str0(NULL, "suffix", "", "Add this suffix to generated files"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); @@ -3412,10 +3518,12 @@ int CmdHF14AMfSEN(const char *Cmd) { bool offline_only = arg_get_lit(ctx, 4); bool online_confirm = arg_get_lit(ctx, 5); int max_online_candidates = arg_get_int_def(ctx, 6, 0); + /* Only bit 0 (byte 0 parity) carries reliable data leakage from FM11RF08S. * Bits 1-3 are not valid keystream leakage for this card variant - using * them filters out correct keys. Override via --parity-mask if needed for - * any weird chinese clones, normally you should never use this parameter. */ + * any weird chinese clones, normally you should never use this parameter. + */ uint8_t parity_mask = 0x1; { int pm_len = 0; @@ -3429,7 +3537,11 @@ int CmdHF14AMfSEN(const char *Cmd) { } } (void)online_confirm; + + int sfxlen = 0; + char suffix[FILE_PATH_SIZE] = {0}; + CLIParamStrToBuf(arg_get_str(ctx, 8), (uint8_t *)suffix, FILE_PATH_SIZE, &sfxlen); CLIParserFree(ctx); - return HFMFSENRecover(keep_nonces, no_oob, reader_mode, offline_only, max_online_candidates, parity_mask, false, NULL, 0); + return HFMFSENRecover(keep_nonces, no_oob, reader_mode, offline_only, max_online_candidates, parity_mask, false, NULL, 0, suffix); } diff --git a/client/src/cmdhfmfsen.h b/client/src/cmdhfmfsen.h index 7f0c502a1..8f1938c69 100644 --- a/client/src/cmdhfmfsen.h +++ b/client/src/cmdhfmfsen.h @@ -34,7 +34,7 @@ extern "C" { extern const uint8_t fm11_backdoor_keys[FM11_BACKDOOR_KEY_COUNT][MIFARE_KEY_SIZE]; int fm11_collect_nonces(const uint8_t *key, iso14a_card_select_t *card, iso14a_fm11rf08s_nonces_with_data_t *nonces); -int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline_only, int max_online_candidates, uint8_t parity_mask, bool skip_default_key_check, const sector_t *known_sectors, size_t known_sector_count); +int HFMFSENRecover(bool keep_nonces, bool no_oob, bool reader_mode, bool offline_only, int max_online_candidates, uint8_t parity_mask, bool skip_default_key_check, const sector_t *known_sectors, size_t known_sector_count, const char *suffix); int CmdHF14AMfSEN(const char *Cmd); #ifdef __cplusplus