diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index de32a335b..a3183b014 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -6265,7 +6265,21 @@ static int CmdHF14ADesLsApp(const char *Cmd) { keysettings0 = buf[0]; numkeys0 = buf[1]; } - + + if (appcount > 0) { + for (int i = 0; i < appcount; i++) { + DesfireGetKeySettings(&dctx, buf, &buflen); + if (res == PM3_SUCCESS && buflen >= 2) { + AppList[i].keySettings = buf[0]; + AppList[i].numKeysRaw = buf[1]; + AppList[i].numberOfKeys = AppList[i].numKeysRaw & 0x1f; + AppList[i].isoFileIDEnabled = ((AppList[i].numKeysRaw & 0x20) != 0); + AppList[i].keyType = DesfireKeyTypeToAlgo(AppList[i].numKeysRaw >> 6); + } + } + } + + // field on-off zone AuthCommandsChk authCmdCheck0 = {0}; DesfireCheckAuthCommands(0x000000, NULL, 0, &authCmdCheck0); @@ -6275,21 +6289,26 @@ static int CmdHF14ADesLsApp(const char *Cmd) { } } - PrintAndLogEx(INFO, "------------------- " _CYAN_("PICC level") " ------------------"); - PrintAndLogEx(INFO, "Applications count: " _GREEN_("%zu") " free memory " _GREEN_("%d"), appcount, freemem); - PrintAndLogEx(INFO, "PICC level auth commands: " NOLF); + // print zone + PrintAndLogEx(SUCCESS, "------------------- " _CYAN_("PICC level") " ------------------"); + PrintAndLogEx(SUCCESS, "Applications count: " _GREEN_("%zu") " free memory " _GREEN_("%d"), appcount, freemem); + PrintAndLogEx(SUCCESS, "PICC level auth commands: " NOLF); DesfireCheckAuthCommandsPrint(&authCmdCheck0); if (numkeys0 > 0) PrintKeySettings(keysettings0, numkeys0, false, true); if (appcount > 0) { - PrintAndLogEx(INFO, "-------------- " _CYAN_("Alications list") " --------------"); + PrintAndLogEx(SUCCESS, ""); + PrintAndLogEx(SUCCESS, "-------------- " _CYAN_("Alications list") " --------------"); for (int i = 0; i < appcount; i++) { - PrintAndLogEx(INFO, "App num: 0x%02x iso id: 0x%04x name: %s", AppList[i].appNum, AppList[i].appISONum, AppList[i].appDFName); - PrintAndLogEx(INFO, "Auth commands: " NOLF); + PrintAndLogEx(SUCCESS, _CYAN_("Application number: 0x%02x") " iso id: " _GREEN_("0x%04x") " name: " _GREEN_("%s"), AppList[i].appNum, AppList[i].appISONum, AppList[i].appDFName); + PrintAndLogEx(SUCCESS, "Auth commands: " NOLF); DesfireCheckAuthCommandsPrint(&AppList[i].authCmdCheck); - PrintAndLogEx(INFO, ""); + PrintAndLogEx(SUCCESS, ""); + if (AppList[i].numberOfKeys > 0) { + PrintKeySettings(AppList[i].keySettings, AppList[i].numKeysRaw, true, true); + } } } diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index 42921ad4d..319b22af4 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -1427,7 +1427,7 @@ void DesfireCheckAuthCommands(uint32_t appAID, char *dfname, uint8_t keyNum, Aut } void DesfireCheckAuthCommandsPrint(AuthCommandsChk *authCmdCheck) { - PrintAndLogEx(NORMAL, "auth:%s auth iso: %s auth aes: %s auth ev2: %s auth iso native: %s", + PrintAndLogEx(NORMAL, "auth: %s auth iso: %s auth aes: %s auth ev2: %s auth iso native: %s", authCmdCheck->auth ? _GREEN_("YES") : _RED_("NO"), authCmdCheck->authISO ? _GREEN_("YES") : _RED_("NO"), authCmdCheck->authAES ? _GREEN_("YES") : _RED_("NO"), @@ -1702,42 +1702,12 @@ int DesfireUpdateRecord(DesfireContext *dctx, uint8_t fnum, uint32_t recnum, uin return DesfireCommandTxData(dctx, MFDES_UPDATE_RECORD, xdata, 10 + len); } -uint8_t DesfireKeyAlgoToType(DesfireCryptoAlgorythm keyType) { - switch (keyType) { - case T_DES: - return 0x00; - case T_3DES: - return 0x00; - case T_3K3DES: - return 0x01; - case T_AES: - return 0x02; - } - return 0; -} -static void PrintKeyType(uint8_t keytype) { - switch (keytype) { - case 00: - PrintAndLogEx(SUCCESS, "Key: 2TDEA"); - break; - case 01: - PrintAndLogEx(SUCCESS, "Key: 3TDEA"); - break; - case 02: - PrintAndLogEx(SUCCESS, "Key: AES"); - break; - default: - PrintAndLogEx(SUCCESS, "Key: unknown: 0x%02x", keytype); - break; - } -} - static void PrintKeySettingsPICC(uint8_t keysettings, uint8_t numkeys, bool print2ndbyte) { PrintAndLogEx(SUCCESS, "PICC level rights:"); - PrintAndLogEx(SUCCESS, "[%c...] CMK Configuration changeable : %s", (keysettings & (1 << 3)) ? '1' : '0', (keysettings & (1 << 3)) ? _GREEN_("YES") : "NO (frozen)"); + PrintAndLogEx(SUCCESS, "[%c...] CMK Configuration changeable : %s", (keysettings & (1 << 3)) ? '1' : '0', (keysettings & (1 << 3)) ? _GREEN_("YES") : _RED_("NO (frozen)")); PrintAndLogEx(SUCCESS, "[.%c..] CMK required for create/delete : %s", (keysettings & (1 << 2)) ? '1' : '0', (keysettings & (1 << 2)) ? _GREEN_("NO") : "YES"); PrintAndLogEx(SUCCESS, "[..%c.] Directory list access with CMK : %s", (keysettings & (1 << 1)) ? '1' : '0', (keysettings & (1 << 1)) ? _GREEN_("NO") : "YES"); - PrintAndLogEx(SUCCESS, "[...%c] CMK is changeable : %s", (keysettings & (1 << 0)) ? '1' : '0', (keysettings & (1 << 0)) ? _GREEN_("YES") : "NO (frozen)"); + PrintAndLogEx(SUCCESS, "[...%c] CMK is changeable : %s", (keysettings & (1 << 0)) ? '1' : '0', (keysettings & (1 << 0)) ? _GREEN_("YES") : _RED_("NO (frozen)")); PrintAndLogEx(SUCCESS, ""); if (print2ndbyte) @@ -1768,14 +1738,14 @@ static void PrintKeySettingsApp(uint8_t keysettings, uint8_t numkeys, bool print break; } - PrintAndLogEx(SUCCESS, "[%c...] AMK Configuration changeable : %s", (keysettings & (1 << 3)) ? '1' : '0', (keysettings & (1 << 3)) ? _GREEN_("YES") : "NO (frozen)"); - PrintAndLogEx(SUCCESS, "[.%c..] AMK required for create/delete : %s", (keysettings & (1 << 2)) ? '1' : '0', (keysettings & (1 << 2)) ? "NO" : "YES"); - PrintAndLogEx(SUCCESS, "[..%c.] Directory list access with AMK : %s", (keysettings & (1 << 1)) ? '1' : '0', (keysettings & (1 << 1)) ? "NO" : "YES"); - PrintAndLogEx(SUCCESS, "[...%c] AMK is changeable : %s", (keysettings & (1 << 0)) ? '1' : '0', (keysettings & (1 << 0)) ? _GREEN_("YES") : "NO (frozen)"); + PrintAndLogEx(SUCCESS, "[%c...] AMK Configuration changeable : %s", (keysettings & (1 << 3)) ? '1' : '0', (keysettings & (1 << 3)) ? _GREEN_("YES") : _RED_("NO (frozen)")); + PrintAndLogEx(SUCCESS, "[.%c..] AMK required for create/delete : %s", (keysettings & (1 << 2)) ? '1' : '0', (keysettings & (1 << 2)) ? _GREEN_("NO") : "YES"); + PrintAndLogEx(SUCCESS, "[..%c.] Directory list access with AMK : %s", (keysettings & (1 << 1)) ? '1' : '0', (keysettings & (1 << 1)) ? _GREEN_("NO") : "YES"); + PrintAndLogEx(SUCCESS, "[...%c] AMK is changeable : %s", (keysettings & (1 << 0)) ? '1' : '0', (keysettings & (1 << 0)) ? _GREEN_("YES") : _RED_("NO (frozen)")); PrintAndLogEx(SUCCESS, ""); if (print2ndbyte) { - PrintKeyType(numkeys >> 6); + DesfirePrintCardKeyType(numkeys >> 6); PrintAndLogEx(SUCCESS, "key count: %d", numkeys & 0x0f); if (numkeys & 0x20) PrintAndLogEx(SUCCESS, "iso file id: enabled"); diff --git a/client/src/mifare/desfirecore.h b/client/src/mifare/desfirecore.h index f7b97a161..9d3ad04e6 100644 --- a/client/src/mifare/desfirecore.h +++ b/client/src/mifare/desfirecore.h @@ -98,6 +98,12 @@ typedef struct { uint16_t appISONum; char appDFName[16]; AuthCommandsChk authCmdCheck; + + uint8_t keySettings; + uint8_t numKeysRaw; + bool isoFileIDEnabled; // from numKeysRaw + uint8_t numberOfKeys; // from numKeysRaw + DesfireCryptoAlgorythm keyType; // from numKeysRaw } AppListElmS; typedef AppListElmS AppListS[64]; @@ -151,7 +157,6 @@ int DesfireGetKeyVersion(DesfireContext *dctx, uint8_t *data, size_t len, uint8_ int DesfireGetKeySettings(DesfireContext *dctx, uint8_t *resp, size_t *resplen); int DesfireChangeKeySettings(DesfireContext *dctx, uint8_t *data, size_t len); void PrintKeySettings(uint8_t keysettings, uint8_t numkeys, bool applevel, bool print2ndbyte); -uint8_t DesfireKeyAlgoToType(DesfireCryptoAlgorythm keyType); int DesfireChangeKeyCmd(DesfireContext *dctx, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen); int DesfireChangeKey(DesfireContext *dctx, bool change_master_key, uint8_t newkeynum, DesfireCryptoAlgorythm newkeytype, uint32_t newkeyver, uint8_t *newkey, DesfireCryptoAlgorythm oldkeytype, uint8_t *oldkey, bool verbose); diff --git a/client/src/mifare/desfirecrypto.c b/client/src/mifare/desfirecrypto.c index e1670ada2..a83ce2e19 100644 --- a/client/src/mifare/desfirecrypto.c +++ b/client/src/mifare/desfirecrypto.c @@ -353,6 +353,50 @@ uint8_t DesfireDESKeyGetVersion(uint8_t *key) { return version; } +DesfireCryptoAlgorythm DesfireKeyTypeToAlgo(uint8_t keyType) { + switch (keyType) { + case 00: + return T_3DES; + case 01: + return T_3K3DES; + case 02: + return T_AES; + default: + return T_3DES; // unknown.... + } +} + +uint8_t DesfireKeyAlgoToType(DesfireCryptoAlgorythm keyType) { + switch (keyType) { + case T_DES: + return 0x00; + case T_3DES: + return 0x00; + case T_3K3DES: + return 0x01; + case T_AES: + return 0x02; + } + return 0; +} + +void DesfirePrintCardKeyType(uint8_t keyType) { + switch (keyType) { + case 00: + PrintAndLogEx(SUCCESS, "Key: 2TDEA"); + break; + case 01: + PrintAndLogEx(SUCCESS, "Key: 3TDEA"); + break; + case 02: + PrintAndLogEx(SUCCESS, "Key: AES"); + break; + default: + PrintAndLogEx(SUCCESS, "Key: unknown: 0x%02x", keyType); + break; + } +} + DesfireCommunicationMode DesfireFileCommModeToCommMode(uint8_t file_comm_mode) { DesfireCommunicationMode mode = DCMNone; switch (file_comm_mode & 0x03) { diff --git a/client/src/mifare/desfirecrypto.h b/client/src/mifare/desfirecrypto.h index b24b8a3ac..8670022a2 100644 --- a/client/src/mifare/desfirecrypto.h +++ b/client/src/mifare/desfirecrypto.h @@ -107,6 +107,10 @@ void DesfireCryptoCMAC(DesfireContext *ctx, uint8_t *srcdata, size_t srcdatalen, void DesfireDESKeySetVersion(uint8_t *key, DesfireCryptoAlgorythm keytype, uint8_t version); uint8_t DesfireDESKeyGetVersion(uint8_t *key); +DesfireCryptoAlgorythm DesfireKeyTypeToAlgo(uint8_t keyType); +uint8_t DesfireKeyAlgoToType(DesfireCryptoAlgorythm keyType); +void DesfirePrintCardKeyType(uint8_t keyType); + DesfireCommunicationMode DesfireFileCommModeToCommMode(uint8_t file_comm_mode); uint8_t DesfireCommModeToFileCommMode(DesfireCommunicationMode comm_mode);