From 5a7e507de2c7528a1b1c793a58ddb411ebed965a Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Tue, 27 Jul 2021 20:00:18 +0300 Subject: [PATCH] advanced record reading --- client/src/cmdhfmfdes.c | 26 ++++++++++++++++++++++---- client/src/util.c | 8 +++++--- client/src/util.h | 2 +- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 4e72da5b8..2dff491ee 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -6345,7 +6345,7 @@ static int CmdHF14ADesReadData(const char *Cmd) { if (resplen > 0) { PrintAndLogEx(SUCCESS, "Read %u bytes from file 0x%02x offset %u", resplen, fnum, offset); - print_buffer_with_offset(resp, resplen, offset); + print_buffer_with_offset(resp, resplen, offset, true); } else { PrintAndLogEx(SUCCESS, "Read operation returned no data from file %d", fnum); } @@ -6363,6 +6363,17 @@ static int CmdHF14ADesReadData(const char *Cmd) { } if (op == RFTRecord) { + res = DesfireReadRecords(&dctx, fnum, offset, 1, resp, &resplen); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire ReadRecords (len=1) command " _RED_("error") ". Result: %d", res); + DropField(); + return PM3_ESOFT; + } + + size_t reclen = resplen; + if (verbose) + PrintAndLogEx(INFO, "Record length %zu", reclen); + res = DesfireReadRecords(&dctx, fnum, offset, length, resp, &resplen); if (res != PM3_SUCCESS) { PrintAndLogEx(ERR, "Desfire ReadRecords command " _RED_("error") ". Result: %d", res); @@ -6371,8 +6382,15 @@ static int CmdHF14ADesReadData(const char *Cmd) { } if (resplen > 0) { - PrintAndLogEx(SUCCESS, "Read %u bytes from file 0x%02x offset %u", resplen, fnum, offset); - print_buffer_with_offset(resp, resplen, offset); + size_t reccount = resplen / reclen; + PrintAndLogEx(SUCCESS, "Read %u bytes from file 0x%02x from record %u record count %zu record length %zu", resplen, fnum, offset, reccount, reclen); + if (reccount > 0) + PrintAndLogEx(SUCCESS, "Lastest record at the bottom."); + for (int i = 0; i < reccount; i++) { + if (i != 0) + PrintAndLogEx(SUCCESS, "Record %d", i + offset); + print_buffer_with_offset(&resp[i * reclen], reclen, offset, (i == 0)); + } } else { PrintAndLogEx(SUCCESS, "Read operation returned no data from file %d", fnum); } @@ -6389,7 +6407,7 @@ static int CmdHF14ADesReadData(const char *Cmd) { if (resplen > 0) { if (resplen != 12) { PrintAndLogEx(WARNING, "Read wrong %u bytes from file 0x%02x offset %u", resplen, fnum, offset); - print_buffer_with_offset(resp, resplen, offset); + print_buffer_with_offset(resp, resplen, offset, true); } else { uint32_t cnt = MemLeToUint4byte(&resp[0]); PrintAndLogEx(SUCCESS, "Transaction counter: %d (0x%08x)", cnt, cnt); diff --git a/client/src/util.c b/client/src/util.c index 829af8d7a..7d2772794 100644 --- a/client/src/util.c +++ b/client/src/util.c @@ -249,9 +249,11 @@ void print_buffer(const uint8_t *data, const size_t len, int level) { print_buffer_ex(data, len, level, 16); } -void print_buffer_with_offset(const uint8_t *data, const size_t len, int offset) { - PrintAndLogEx(INFO, " Offset | Data | Ascii"); - PrintAndLogEx(INFO, "----------------------------------------------------------------------------"); +void print_buffer_with_offset(const uint8_t *data, const size_t len, int offset, bool print_header) { + if (print_header) { + PrintAndLogEx(INFO, " Offset | Data | Ascii"); + PrintAndLogEx(INFO, "----------------------------------------------------------------------------"); + } for (uint32_t i = 0; i < len; i += 16) { uint32_t l = len - i; diff --git a/client/src/util.h b/client/src/util.h index 14795773a..67ce0faa8 100644 --- a/client/src/util.h +++ b/client/src/util.h @@ -52,7 +52,7 @@ char *sprint_hex_ascii(const uint8_t *data, const size_t len); char *sprint_ascii(const uint8_t *data, const size_t len); char *sprint_ascii_ex(const uint8_t *data, const size_t len, const size_t min_str_len); -void print_buffer_with_offset(const uint8_t *data, const size_t len, int offset); +void print_buffer_with_offset(const uint8_t *data, const size_t len, int offset, bool print_header); void print_buffer(const uint8_t *data, const size_t len, int level); void print_blocks(uint32_t *data, size_t len);