From 2677ee541e0188ec817050d6e624c0d09efffd97 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sat, 29 Aug 2026 12:33:05 +0200 Subject: [PATCH] added support for flipper iso15693 .nfc dump files. Thanks Claude --- CHANGELOG.md | 1 + client/src/fileutils.c | 156 +++++++++++++++++++++++++++++++++++++++++ client/src/fileutils.h | 2 + 3 files changed, 159 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28267ebf8..61bfdac2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ 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] +- Support for flipper ISO15693.nfc dump files (@iceman100) - Change NG: convert CMD_LF_EM4X50_ESET to em4x50_eset_t (@iceman1001) - Change NG: convert CMD_LF_T55XX_SET_CONFIG to t55xx_setconfig_t (@iceman1001) - Added a new ISo15693 v5 dump file format to handle 0x100 number of pages and 8 byte pages better (@iceman1001) diff --git a/client/src/fileutils.c b/client/src/fileutils.c index e66d0c998..6f30468e0 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -1361,6 +1361,58 @@ int loadFileEML_safe(const char *preferredName, void **pdata, size_t *datalen) { return retval; } +// Convert a run of "XX XX XX ..." hex text into bytes. +// +// `head` is the part of the line the caller already has, `more` says whether the +// line continued past the caller's buffer, in which case the rest is pulled from +// `f` one character at a time and converted as it goes. That keeps the caller's +// line buffer small: an ISO15693 "Data Content" line carries the whole tag and +// runs to several thousand characters. +// +// Returns the number of bytes the text held, which may be larger than `destlen`. +// Only the first `destlen` bytes are stored, so the caller can spot an overflow. +static size_t hexstream_to_bytes(FILE *f, const char *head, bool more, uint8_t *dest, size_t destlen) { + + size_t cnt = 0; + int hi = -1; + + for (;;) { + + int c; + if (*head) { + c = (unsigned char) * head++; + } else if (more) { + c = fgetc(f); + } else { + break; + } + + if ((c == EOF) || (c == '\n') || (c == '\r')) { + break; + } + + if (isxdigit(c) == 0) { + // separator + continue; + } + + int v = (c <= '9') ? (c - '0') : ((c | 0x20) - 'a' + 10); + + if (hi < 0) { + hi = v; + continue; + } + + if (cnt < destlen) { + dest[cnt] = (hi << 4) | v; + } + cnt++; + hi = -1; + } + + return cnt; +} + int loadFileNFC_safe(const char *preferredName, void *data, size_t maxdatalen, size_t *datalen, nfc_df_e ft) { if (data == NULL) { @@ -1391,6 +1443,7 @@ int loadFileNFC_safe(const char *preferredName, void *data, size_t maxdatalen, s udata_t udata = (udata_t)data; int n = 0; uint32_t counter = 0; + size_t iso15_datalen = 0; while (!feof(f)) { @@ -1409,16 +1462,76 @@ int loadFileNFC_safe(const char *preferredName, void *data, size_t maxdatalen, s continue; } + // did this fgets reach the end of the line, or is the line longer than + // the buffer? ISO15693 puts a whole tag on its "Data Content" line. + bool line_truncated = ((strchr(line, '\n') == NULL) && (feof(f) == false)); + str_cleanrn(line, sizeof(line)); str_lower(line); if (str_startswith(line, "uid:")) { if (ft == NFC_DF_MFC) { // param_gethex_to_eol(line + 4, 0, udata.mfc->card_info.uid, sizeof(udata.mfc->card_info.uid), &n); + } else if (ft == NFC_DF_15) { + // iso15_tag_t keeps the UID in transmission order (LSB first), + // Flipper writes it the way it is displayed (MSB first). + param_gethex_to_eol(line + 4, 0, udata.iso15->uid, sizeof(udata.iso15->uid), &n); + reverse_array(udata.iso15->uid, sizeof(udata.iso15->uid)); } continue; } + if (ft == NFC_DF_15) { + + if (str_startswith(line, "dsfid:")) { + uint8_t v = 0; + param_gethex_to_eol(line + 6, 0, &v, sizeof(v), &n); + udata.iso15->dsfid = v; + continue; + } + + if (str_startswith(line, "afi:")) { + uint8_t v = 0; + param_gethex_to_eol(line + 4, 0, &v, sizeof(v), &n); + udata.iso15->afi = v; + continue; + } + + if (str_startswith(line, "ic reference:")) { + uint8_t v = 0; + param_gethex_to_eol(line + 13, 0, &v, sizeof(v), &n); + udata.iso15->ic = v; + continue; + } + + // Flipper writes the block count in decimal but the block size in hex + if (str_startswith(line, "block count:")) { + int v = 0; + sscanf(line, "block count: %d", &v); + udata.iso15->pagesCount = v; + continue; + } + + if (str_startswith(line, "block size:")) { + uint8_t v = 0; + param_gethex_to_eol(line + 11, 0, &v, sizeof(v), &n); + udata.iso15->bytesPerPage = v; + continue; + } + + if (str_startswith(line, "data content:")) { + iso15_datalen = hexstream_to_bytes(f, line + 13, line_truncated + , udata.iso15->data + , sizeof(udata.iso15->data)); + continue; + } + + if (str_startswith(line, "password privacy:")) { + param_gethex_to_eol(line + 17, 0, udata.iso15->privacyPasswd, sizeof(udata.iso15->privacyPasswd), &n); + continue; + } + } + if (str_startswith(line, "atqa:")) { if (ft == NFC_DF_MFC) { // param_gethex_to_eol(line + 5, 0, udata.mfc->card_info.atqa, sizeof(udata.mfc->card_info.atqa), &n); @@ -1590,6 +1703,48 @@ int loadFileNFC_safe(const char *preferredName, void *data, size_t maxdatalen, s *datalen = counter; } else if (ft == NFC_DF_MFU) { *datalen += MFU_DUMP_PREFIX_LENGTH; + } else if (ft == NFC_DF_15) { + + if ((udata.iso15->pagesCount == 0) || (udata.iso15->bytesPerPage == 0)) { + fclose(f); + PrintAndLogEx(FAILED, "missing block count / block size in `" _YELLOW_("%s") "`", preferredName); + return PM3_ESOFT; + } + + if (udata.iso15->pagesCount > ISO15693_TAG_MAX_PAGES) { + fclose(f); + PrintAndLogEx(FAILED, "block count ( %u ) exceeds max ( %u )" + , udata.iso15->pagesCount + , ISO15693_TAG_MAX_PAGES); + return PM3_ESOFT; + } + + uint32_t expected = (uint32_t)udata.iso15->pagesCount * udata.iso15->bytesPerPage; + if (expected > ISO15693_TAG_MAX_SIZE) { + fclose(f); + PrintAndLogEx(FAILED, "tag memory ( %u bytes ) exceeds max ( %u bytes )" + , expected + , ISO15693_TAG_MAX_SIZE); + return PM3_ESOFT; + } + + if (iso15_datalen > sizeof(udata.iso15->data)) { + fclose(f); + PrintAndLogEx(FAILED, "data content ( %zu bytes ) exceeds max ( %zu bytes )" + , iso15_datalen + , sizeof(udata.iso15->data)); + return PM3_ESOFT; + } + + if (iso15_datalen != expected) { + PrintAndLogEx(WARNING, "data content is %zu bytes, header says %u x %u = %u bytes" + , iso15_datalen + , udata.iso15->pagesCount + , udata.iso15->bytesPerPage + , expected); + } + + *datalen = sizeof(iso15_tag_t); } fclose(f); @@ -3015,6 +3170,7 @@ int detect_nfc_dump_format(const char *preferredName, nfc_df_e *dump_type, bool break; case NFC_DF_15: PrintAndLogEx(INFO, "Detected ISO15693 based dump format"); + break; case NFC_DF_UNKNOWN: PrintAndLogEx(WARNING, "Failed to detected dump format"); break; diff --git a/client/src/fileutils.h b/client/src/fileutils.h index c8a7d6e89..91afa5cd0 100644 --- a/client/src/fileutils.h +++ b/client/src/fileutils.h @@ -32,6 +32,7 @@ #include "protocols.h" // iclass defines #include "cmdhftopaz.h" // TOPAZ defines #include "mifare/mifaredefault.h" // MFP / AES defines +#include "iso15.h" // iso15_tag_t typedef union { void *v; @@ -40,6 +41,7 @@ typedef union { topaz_tag_t *topaz; iso14a_mf_extdump_t *mfc; iso14a_mf_dump_ev1_t *mfc_ev1; + iso15_tag_t *iso15; } udata_t; typedef enum {