From 29b955f45acf32bb44d20b82b4f5c2e9d18cd4b0 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Wed, 28 Jul 2021 23:17:26 +0300 Subject: [PATCH 1/4] lsfiles command --- client/src/cmdhfmfdes.c | 86 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 1b4bc4f7a..eddce2944 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -6726,6 +6726,89 @@ static int CmdHF14ADesWriteData(const char *Cmd) { return PM3_SUCCESS; } +static int CmdHF14ADesLsFiles(const char *Cmd) { + CLIParserContext *ctx; + CLIParserInit(&ctx, "hf mfdes lsfiles", + "Show file list. Master key needs to be provided or flag --no-auth set (depend on cards settings).", + "hf mfdes lsfiles --aid 123456 -> show file list for: app=123456 with defaults from `default` command"); + + void *argtable[] = { + arg_param_begin, + arg_lit0("a", "apdu", "show APDU requests and responses"), + arg_lit0("v", "verbose", "show technical data"), + arg_int0("n", "keyno", "", "Key number"), + arg_str0("t", "algo", "", "Crypt algo: DES, 2TDEA, 3TDEA, AES"), + arg_str0("k", "key", "", "Key for authenticate (HEX 8(DES), 16(2TDEA or AES) or 24(3TDEA) bytes)"), + arg_str0("f", "kdf", "", "Key Derivation Function (KDF): None, AN10922, Gallagher"), + arg_str0("i", "kdfi", "", "KDF input (HEX 1-31 bytes)"), + arg_str0("m", "cmode", "", "Communicaton mode: plain/mac/encrypt"), + arg_str0("c", "ccset", "", "Communicaton command set: native/niso/iso"), + arg_str0("s", "schann", "", "Secure channel: d40/ev1/ev2"), + arg_str0(NULL, "aid", "", "Application ID (3 hex bytes, big endian)"), + arg_lit0(NULL, "no-auth", "execute without authentication"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, false); + + bool APDULogging = arg_get_lit(ctx, 1); + bool verbose = arg_get_lit(ctx, 2); + bool noauth = arg_get_lit(ctx, 12); + + DesfireContext dctx; + int securechann = defaultSecureChannel; + uint32_t appid = 0x000000; + int res = CmdDesGetSessionParameters(ctx, &dctx, 3, 4, 5, 6, 7, 8, 9, 10, 11, &securechann, DCMPlain, &appid); + if (res) { + CLIParserFree(ctx); + return res; + } + + SetAPDULogging(APDULogging); + CLIParserFree(ctx); + + if (noauth) { + res = DesfireSelectAIDHex(&dctx, appid, false, 0); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire select " _RED_("error") "."); + DropField(); + return res; + } + } else { + res = DesfireSelectAndAuthenticate(&dctx, securechann, appid, verbose); + if (res != PM3_SUCCESS) { + DropField(); + return res; + } + } + +//int DesfireGetFileIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen); +//int DesfireGetFileISOIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen); + + + + uint8_t buf[APDU_RES_LEN] = {0}; + size_t buflen = 0; + + res = DesfireGetFileIDList(&dctx, buf, &buflen); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire GetFileIDList command " _RED_("error") ". Result: %d", res); + DropField(); + return PM3_ESOFT; + } + + if (buflen > 0) { + PrintAndLogEx(INFO, "---- " _CYAN_("File list") " ----"); + for (int i = 0; i < buflen; i++) + PrintAndLogEx(INFO, "File ID: %02x", buf[i]); + } else { + PrintAndLogEx(INFO, "There is no files in the application %06x", appid); + } + + + DropField(); + return PM3_SUCCESS; +} + static int CmdHF14ADesTest(const char *Cmd) { DesfireTest(true); return PM3_SUCCESS; @@ -6761,6 +6844,8 @@ static command_t CommandTable[] = { {"-----------", CmdHelp, IfPm3Iso14443a, "----------------------- " _CYAN_("Files") " -----------------------"}, {"getfileids", CmdHF14ADesGetFileIDs, IfPm3Iso14443a, "[new]Get File IDs list"}, {"getfileisoids", CmdHF14ADesGetFileISOIDs, IfPm3Iso14443a, "[new]Get File ISO IDs list"}, + {"lsfiles", CmdHF14ADesLsFiles, IfPm3Iso14443a, "[new]Show all files list"}, + {"dump", CmdHF14ADesDump, IfPm3Iso14443a, "Dump all files"}, {"createfile", CmdHF14ADesCreateFile, IfPm3Iso14443a, "[new]Create Standard/Backup File"}, {"createvaluefile", CmdHF14ADesCreateValueFile, IfPm3Iso14443a, "[new]Create Value File"}, {"createrecordfile", CmdHF14ADesCreateRecordFile, IfPm3Iso14443a, "[new]Create Linear/Cyclic Record File"}, @@ -6768,7 +6853,6 @@ static command_t CommandTable[] = { {"deletefile", CmdHF14ADesDeleteFile, IfPm3Iso14443a, "[new]Delete File"}, {"getfilesettings", CmdHF14ADesGetFileSettings, IfPm3Iso14443a, "[new]Get file settings"}, {"chfilesettings", CmdHF14ADesChFileSettings, IfPm3Iso14443a, "[new]Change file settings"}, - {"dump", CmdHF14ADesDump, IfPm3Iso14443a, "Dump all files"}, {"read", CmdHF14ADesReadData, IfPm3Iso14443a, "[new]Read data from standard/backup/record/value/mac file"}, {"write", CmdHF14ADesWriteData, IfPm3Iso14443a, "[new]Write data to standard/backup/record/value file"}, {"value", CmdHF14ADesValueOperations, IfPm3Iso14443a, "[new]Operations with value file (get/credit/limited credit/debit/clear)"}, From 907c4a4b3958d1cf6b8aca84cf903c33ae2de312 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Thu, 29 Jul 2021 00:00:38 +0300 Subject: [PATCH 2/4] data ready --- client/src/cmdhfmfdes.c | 53 ++++++++++++++++++++++++++++----- client/src/mifare/desfirecore.c | 10 +++++++ client/src/mifare/desfirecore.h | 9 ++++++ 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index eddce2944..5826282d6 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -6784,7 +6784,8 @@ static int CmdHF14ADesLsFiles(const char *Cmd) { //int DesfireGetFileIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen); //int DesfireGetFileISOIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen); - + FileListS FileList; + memset(FileList, 0, sizeof(FileList)); uint8_t buf[APDU_RES_LEN] = {0}; size_t buflen = 0; @@ -6795,14 +6796,50 @@ static int CmdHF14ADesLsFiles(const char *Cmd) { DropField(); return PM3_ESOFT; } - - if (buflen > 0) { - PrintAndLogEx(INFO, "---- " _CYAN_("File list") " ----"); - for (int i = 0; i < buflen; i++) - PrintAndLogEx(INFO, "File ID: %02x", buf[i]); - } else { + + if (buflen == 0) { PrintAndLogEx(INFO, "There is no files in the application %06x", appid); - } + DropField(); + return PM3_SUCCESS; + } + + for (int i = 0; i < buflen; i++) { + FileList[i].fileNum = buf[i]; + DesfireGetFileSettingsStruct(&dctx, FileList[i].fileNum, &FileList[i].fileSettings); + } + size_t filescount = buflen; + + buflen = 0; + res = DesfireGetFileISOIDList(&dctx, buf, &buflen); + if (res != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Desfire GetFileISOIDList command " _RED_("error") ". Result: %d", res); + } + + if (buflen > 0) { + size_t isoindx = 0; + for (int i = 0; i < filescount; i++) { + if (FileList[i].fileSettings.fileType != 0x03 && FileList[i].fileSettings.fileType != 0x05) { + FileList[i].fileISONum = MemBeToUint2byte(&buf[isoindx * 2]); + isoindx++; + } + } + if (isoindx * 2 != buflen) + PrintAndLogEx(WARNING, "Wrong ISO ID list length. must be %d but %d", buflen, isoindx * 2); + } else { + PrintAndLogEx(WARNING, "ISO ID list returned no data"); + } + + PrintAndLogEx(INFO, "---- " _CYAN_("File list") " ----"); + for (int i = 0; i < filescount; i++) { + PrintAndLogEx(SUCCESS, "ID: " _GREEN_("%02x ") NOLF, FileList[i].fileNum); + if (FileList[i].fileISONum != 0) + PrintAndLogEx(NORMAL, "ISO ID: %04x " NOLF, FileList[i].fileISONum); + else + PrintAndLogEx(NORMAL, "ISO ID: n/a " NOLF); + + DesfirePrintFileSettingsOneLine(&FileList[i].fileSettings); + } + DropField(); diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index d97dbe16a..dbe562d1d 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -1473,6 +1473,16 @@ void DesfireFillFileSettings(uint8_t *data, size_t datalen, FileSettingsS *fsett } } +void DesfirePrintFileSettingsOneLine(FileSettingsS *fsettings) { + PrintAndLogEx(NORMAL, "(%-5s) " NOLF, GetDesfireCommunicationMode(fsettings->fileCommMode)); + PrintAndLogEx(NORMAL, "[0x%02x] %-13s ", fsettings->fileType, GetDesfireFileType(fsettings->fileType)); + + + /* PrintAndLogEx(SUCCESS, "read : %s", GetDesfireAccessRightStr(r)); + PrintAndLogEx(SUCCESS, "write : %s", GetDesfireAccessRightStr(w)); + PrintAndLogEx(SUCCESS, "readwrite: %s", GetDesfireAccessRightStr(rw)); + PrintAndLogEx(SUCCESS, "change : %s", GetDesfireAccessRightStr(ch));*/ +} static void DesfirePrintFileSettDynPart(uint8_t filetype, uint8_t *data, size_t datalen, uint8_t *dynlen, bool create) { switch (filetype) { diff --git a/client/src/mifare/desfirecore.h b/client/src/mifare/desfirecore.h index 8933e7e10..0a4a8f0dd 100644 --- a/client/src/mifare/desfirecore.h +++ b/client/src/mifare/desfirecore.h @@ -68,6 +68,14 @@ typedef struct { } FileSettingsS; +typedef struct { + uint8_t fileNum; + uint16_t fileISONum; + FileSettingsS fileSettings; +} FileListElmS; + +typedef FileListElmS FileListS[32]; + typedef enum { RFTAuto, RFTData, @@ -125,6 +133,7 @@ int DesfireGetFileIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen); int DesfireGetFileISOIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen); void DesfireFillFileSettings(uint8_t *data, size_t datalen, FileSettingsS *fsettings); +void DesfirePrintFileSettingsOneLine(FileSettingsS *fsettings); int DesfireGetFileSettings(DesfireContext *dctx, uint8_t fileid, uint8_t *resp, size_t *resplen); int DesfireGetFileSettingsStruct(DesfireContext *dctx, uint8_t fileid, FileSettingsS *fsettings); int DesfireChangeFileSettings(DesfireContext *dctx, uint8_t *data, size_t datalen); From a5f7e4172b5199770b5af19efe4341db7cab2c78 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Thu, 29 Jul 2021 00:21:16 +0300 Subject: [PATCH 3/4] formatting --- client/src/cmdhfmfdes.c | 23 +++++++++--------- client/src/mifare/desfirecore.c | 42 +++++++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 5826282d6..465ec1f42 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -6781,9 +6781,6 @@ static int CmdHF14ADesLsFiles(const char *Cmd) { } } -//int DesfireGetFileIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen); -//int DesfireGetFileISOIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen); - FileListS FileList; memset(FileList, 0, sizeof(FileList)); @@ -6815,33 +6812,35 @@ static int CmdHF14ADesLsFiles(const char *Cmd) { PrintAndLogEx(ERR, "Desfire GetFileISOIDList command " _RED_("error") ". Result: %d", res); } + size_t isoindx = 0; if (buflen > 0) { - size_t isoindx = 0; for (int i = 0; i < filescount; i++) { - if (FileList[i].fileSettings.fileType != 0x03 && FileList[i].fileSettings.fileType != 0x05) { + if (FileList[i].fileSettings.fileType != 0x02 && FileList[i].fileSettings.fileType != 0x05) { FileList[i].fileISONum = MemBeToUint2byte(&buf[isoindx * 2]); isoindx++; } } + if (isoindx > 0) + isoindx--; if (isoindx * 2 != buflen) PrintAndLogEx(WARNING, "Wrong ISO ID list length. must be %d but %d", buflen, isoindx * 2); } else { PrintAndLogEx(WARNING, "ISO ID list returned no data"); } - PrintAndLogEx(INFO, "---- " _CYAN_("File list") " ----"); + PrintAndLogEx(INFO, "---------------------------- " _CYAN_("File list") " ----------(r w rw ch)------------------"); for (int i = 0; i < filescount; i++) { PrintAndLogEx(SUCCESS, "ID: " _GREEN_("%02x ") NOLF, FileList[i].fileNum); - if (FileList[i].fileISONum != 0) - PrintAndLogEx(NORMAL, "ISO ID: %04x " NOLF, FileList[i].fileISONum); - else - PrintAndLogEx(NORMAL, "ISO ID: n/a " NOLF); + if (isoindx > 0) { + if (FileList[i].fileISONum != 0) + PrintAndLogEx(NORMAL, "ISO ID: " _CYAN_("%04x ") NOLF, FileList[i].fileISONum); + else + PrintAndLogEx(NORMAL, "ISO ID: " _YELLOW_("n/a ") NOLF); + } DesfirePrintFileSettingsOneLine(&FileList[i].fileSettings); } - - DropField(); return PM3_SUCCESS; } diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index dbe562d1d..34328afe4 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -1475,13 +1475,45 @@ void DesfireFillFileSettings(uint8_t *data, size_t datalen, FileSettingsS *fsett void DesfirePrintFileSettingsOneLine(FileSettingsS *fsettings) { PrintAndLogEx(NORMAL, "(%-5s) " NOLF, GetDesfireCommunicationMode(fsettings->fileCommMode)); - PrintAndLogEx(NORMAL, "[0x%02x] %-13s ", fsettings->fileType, GetDesfireFileType(fsettings->fileType)); + PrintAndLogEx(NORMAL, "[0x%02x] %-13s " NOLF, fsettings->fileType, GetDesfireFileType(fsettings->fileType)); + switch (fsettings->fileType) { + case 0x00: + case 0x01: { + PrintAndLogEx(NORMAL, "size: %d [0x%x] " NOLF, fsettings->fileSize, fsettings->fileSize); + break; + } + /* case 0x02: { + fsettings->lowerLimit = MemLeToUint4byte(&data[4]); + fsettings->upperLimit = MemLeToUint4byte(&data[8]); + fsettings->value = MemLeToUint4byte(&data[12]); + fsettings->limitedCredit = data[16]; + reclen = 4 + 13; + break; + } + case 0x03: + case 0x04: { + fsettings->recordSize = MemLeToUint3byte(&data[4]); + fsettings->maxRecordCount = MemLeToUint3byte(&data[7]); + fsettings->curRecordCount = MemLeToUint3byte(&data[10]); + reclen = 4 + 9; + break; + } + case 0x05: { + fsettings->keyType = data[4]; + fsettings->keyVersion = data[5]; + break; + }*/ + default: { + break; + } + } - /* PrintAndLogEx(SUCCESS, "read : %s", GetDesfireAccessRightStr(r)); - PrintAndLogEx(SUCCESS, "write : %s", GetDesfireAccessRightStr(w)); - PrintAndLogEx(SUCCESS, "readwrite: %s", GetDesfireAccessRightStr(rw)); - PrintAndLogEx(SUCCESS, "change : %s", GetDesfireAccessRightStr(ch));*/ + PrintAndLogEx(NORMAL, "(%s %s %s %s)", + GetDesfireAccessRightStr(fsettings->rAccess), + GetDesfireAccessRightStr(fsettings->wAccess), + GetDesfireAccessRightStr(fsettings->rwAccess), + GetDesfireAccessRightStr(fsettings->chAccess)); } static void DesfirePrintFileSettDynPart(uint8_t filetype, uint8_t *data, size_t datalen, uint8_t *dynlen, bool create) { From 74fc92f1db04fe5946a3a2cd582668ba17f8bad6 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Thu, 29 Jul 2021 09:01:10 +0300 Subject: [PATCH 4/4] works --- client/src/cmdhfmfdes.c | 2 +- client/src/mifare/desfirecore.c | 22 ++++++++-------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 465ec1f42..34b9b7647 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -6828,7 +6828,7 @@ static int CmdHF14ADesLsFiles(const char *Cmd) { PrintAndLogEx(WARNING, "ISO ID list returned no data"); } - PrintAndLogEx(INFO, "---------------------------- " _CYAN_("File list") " ----------(r w rw ch)------------------"); + PrintAndLogEx(INFO, "---------------------------- " _CYAN_("File list") " -----------------------(r w rw ch)-----"); for (int i = 0; i < filescount; i++) { PrintAndLogEx(SUCCESS, "ID: " _GREEN_("%02x ") NOLF, FileList[i].fileNum); if (isoindx > 0) { diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index 34328afe4..21226fecb 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -1475,7 +1475,7 @@ void DesfireFillFileSettings(uint8_t *data, size_t datalen, FileSettingsS *fsett void DesfirePrintFileSettingsOneLine(FileSettingsS *fsettings) { PrintAndLogEx(NORMAL, "(%-5s) " NOLF, GetDesfireCommunicationMode(fsettings->fileCommMode)); - PrintAndLogEx(NORMAL, "[0x%02x] %-13s " NOLF, fsettings->fileType, GetDesfireFileType(fsettings->fileType)); + PrintAndLogEx(NORMAL, "[0x%02x] " _CYAN_("%-13s ") NOLF, fsettings->fileType, GetDesfireFileType(fsettings->fileType)); switch (fsettings->fileType) { case 0x00: @@ -1483,27 +1483,21 @@ void DesfirePrintFileSettingsOneLine(FileSettingsS *fsettings) { PrintAndLogEx(NORMAL, "size: %d [0x%x] " NOLF, fsettings->fileSize, fsettings->fileSize); break; } - /* case 0x02: { - fsettings->lowerLimit = MemLeToUint4byte(&data[4]); - fsettings->upperLimit = MemLeToUint4byte(&data[8]); - fsettings->value = MemLeToUint4byte(&data[12]); - fsettings->limitedCredit = data[16]; - reclen = 4 + 13; + case 0x02: { + PrintAndLogEx(NORMAL, "[%d .. %d] lim cred: 0x%02x (%d [0x%x]) " NOLF, + fsettings->lowerLimit, fsettings->upperLimit, fsettings->limitedCredit, fsettings->value, fsettings->value); break; } case 0x03: case 0x04: { - fsettings->recordSize = MemLeToUint3byte(&data[4]); - fsettings->maxRecordCount = MemLeToUint3byte(&data[7]); - fsettings->curRecordCount = MemLeToUint3byte(&data[10]); - reclen = 4 + 9; + PrintAndLogEx(NORMAL, "%d/%d record size: %d [0x%x]b " NOLF, + fsettings->curRecordCount, fsettings->maxRecordCount, fsettings->recordSize, fsettings->recordSize); break; } case 0x05: { - fsettings->keyType = data[4]; - fsettings->keyVersion = data[5]; + PrintAndLogEx(NORMAL, "key type: 0x%02x version: 0x%02x " NOLF, fsettings->keyType, fsettings->keyVersion); break; - }*/ + } default: { break; }