From f5650a53afec108f8c3f3dd3a30128ba36faa46a Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 18 Feb 2025 18:48:33 +0100 Subject: [PATCH] text and style --- client/src/cmdhficlass.c | 16 +----- client/src/emv/cmdemv.c | 10 ++-- client/src/fileutils.c | 2 +- client/src/mifare/desfirecore.c | 91 ++++++++++++++++++------------- client/src/mifare/desfirecrypto.c | 8 +-- client/src/mifare/mad.c | 14 +++-- client/src/mifare/mifarehost.c | 11 +++- 7 files changed, 86 insertions(+), 66 deletions(-) diff --git a/client/src/cmdhficlass.c b/client/src/cmdhficlass.c index b08b7ba8c..d99cbe986 100644 --- a/client/src/cmdhficlass.c +++ b/client/src/cmdhficlass.c @@ -1344,10 +1344,7 @@ static int CmdHFiClassEView(const char *Cmd) { PrintAndLogEx(NORMAL, ""); printIclassDumpContents(dump, 1, blocks, bytes, dense_output); - - if (verbose) { - print_iclass_sio(dump, bytes); - } + print_iclass_sio(dump, bytes, verbose); free(dump); return PM3_SUCCESS; @@ -1708,11 +1705,7 @@ static int CmdHFiClassDecrypt(const char *Cmd) { } printIclassDumpContents(decrypted, 1, (decryptedlen / 8), decryptedlen, dense_output); - - if (verbose) { - print_iclass_sio(decrypted, decryptedlen); - } - + print_iclass_sio(decrypted, decryptedlen, verbose); PrintAndLogEx(NORMAL, ""); // decode block 6 @@ -3518,10 +3511,7 @@ static int CmdHFiClassView(const char *Cmd) { print_picopass_info((picopass_hdr_t *) dump); printIclassDumpContents(dump, startblock, endblock, bytes_read, dense_output); iclass_decode_credentials(dump); - - if (verbose) { - print_iclass_sio(dump, bytes_read); - } + print_iclass_sio(dump, bytes_read, verbose); free(dump); return PM3_SUCCESS; diff --git a/client/src/emv/cmdemv.c b/client/src/emv/cmdemv.c index 4d7daec9e..4c38dae53 100644 --- a/client/src/emv/cmdemv.c +++ b/client/src/emv/cmdemv.c @@ -2122,9 +2122,9 @@ static int CmdEMVScan(const char *Cmd) { uint8_t psenum = (channel == CC_CONTACT) ? 1 : 2; - char filename[FILE_PATH_SIZE] = {0}; - int fnlen = 0; - CLIParamStrToBuf(arg_get_str(ctx, 12), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen); + uint8_t filename[FILE_PATH_SIZE] = {0}; + int filenamelen = sizeof(filename) - 1; // CLIGetStrWithReturn does not guarantee string to be null-terminated + CLIGetStrWithReturn(ctx, 12, filename, &filenamelen); CLIParserFree(ctx); @@ -2507,7 +2507,7 @@ static int CmdEMVRoca(const char *Cmd) { void *argtable[] = { arg_param_begin, - arg_lit0("t", "selftest", "Self test"), + arg_lit0(NULL, "test", "Perform self tests"), arg_lit0("a", "apdu", "Show APDU requests and responses"), arg_lit0("w", "wired", "Send data via contact (iso7816) interface. (def: Contactless interface)"), arg_param_end @@ -2981,7 +2981,7 @@ static command_t CommandTable[] = { {"-----------", CmdHelp, AlwaysAvailable, "----------------------- " _CYAN_("General") " -----------------------"}, {"help", CmdHelp, AlwaysAvailable, "This help"}, {"list", CmdEMVList, AlwaysAvailable, "List ISO7816 history"}, - {"test", CmdEMVTest, AlwaysAvailable, "Crypto logic selftest"}, + {"test", CmdEMVTest, AlwaysAvailable, "Perform crypto logic self tests"}, {"-----------", CmdHelp, IfPm3Iso14443a, "---------------------- " _CYAN_("Operations") " ---------------------"}, {"challenge", CmdEMVGenerateChallenge, IfPm3Iso14443, "Generate challenge"}, {"exec", CmdEMVExec, IfPm3Iso14443, "Executes EMV contactless transaction"}, diff --git a/client/src/fileutils.c b/client/src/fileutils.c index 86a3d06b5..73b992fd2 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -809,7 +809,7 @@ int saveFileJSONrootEx(const char *preferredName, const void *root, size_t flags if (res == 0) { if (verbose) { - PrintAndLogEx(SUCCESS, "Saved to json file `" _YELLOW_("%s") "`", filename); + PrintAndLogEx(SUCCESS, "Saved to json file " _YELLOW_("%s"), filename); } free(filename); return PM3_SUCCESS; diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index ac44508f2..07297d05a 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -1008,7 +1008,7 @@ void DesfirePrintAIDFunctions(uint32_t appid) { if ((aid[2] >> 4) == 0xF) { uint16_t short_aid = ((aid[2] & 0xF) << 12) | (aid[1] << 4) | (aid[0] >> 4); PrintAndLogEx(SUCCESS, " AID mapped to MIFARE Classic AID (MAD): " _YELLOW_("%02X"), short_aid); - PrintAndLogEx(SUCCESS, " MAD AID Cluster 0x%02X : " _YELLOW_("%s"), short_aid >> 8, nxp_cluster_to_text(short_aid >> 8)); + PrintAndLogEx(SUCCESS, " MAD AID Cluster 0x%02X..... " _YELLOW_("%s"), short_aid >> 8, nxp_cluster_to_text(short_aid >> 8)); MADDFDecodeAndPrint(short_aid, false); } else { AIDDFDecodeAndPrint(aid); @@ -1016,53 +1016,64 @@ void DesfirePrintAIDFunctions(uint32_t appid) { } int DesfireSelectAndAuthenticateEx(DesfireContext_t *dctx, DesfireSecureChannel secureChannel, uint32_t aid, bool noauth, bool verbose) { - if (verbose) + if (verbose) { DesfirePrintContext(dctx); + } // needs card uid for diversification - if (dctx->kdfAlgo == MFDES_KDF_ALGO_GALLAGHER) + if (dctx->kdfAlgo == MFDES_KDF_ALGO_GALLAGHER) { DesfireGetCardUID(dctx); + } bool isosw = false; if (dctx->cmdSet == DCCISO) { dctx->cmdSet = DCCNativeISO; isosw = true; - if (verbose) + if (verbose) { PrintAndLogEx(INFO, "Switch to " _CYAN_("native") " for select"); } + } int res; if (aid == 0x000000) { res = DesfireAnticollision(verbose); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire anticollision " _RED_("error") "."); + PrintAndLogEx(ERR, "Desfire anticollision " _RED_("fail")); return 200; } - if (verbose) + + if (verbose) { PrintAndLogEx(INFO, "Anticollision " _GREEN_("ok")); + } + } else { res = DesfireSelectAIDHex(dctx, aid, false, 0); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire select " _RED_("error") "."); + PrintAndLogEx(ERR, "Desfire select " _RED_("fail")); return 200; } - if (verbose) + + if (verbose) { PrintAndLogEx(INFO, "App %06x " _GREEN_("selected"), aid); } + } - if (isosw) + if (isosw) { dctx->cmdSet = DCCISO; + } if (noauth == false) { + res = DesfireAuthenticate(dctx, secureChannel, verbose); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire authenticate " _RED_("error") ". Result: [%d] %s", res, DesfireAuthErrorToStr(res)); + PrintAndLogEx(ERR, "Desfire authenticate " _RED_("fail") ". Result: [%d] %s", res, DesfireAuthErrorToStr(res)); return res; } if (DesfireIsAuthenticated(dctx)) { - if (verbose) + if (verbose) { PrintAndLogEx(INFO, "Desfire " _GREEN_("authenticated")); + } } else { return 201; } @@ -1087,7 +1098,7 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s res = DesfireSelectAIDHex(dctx, id, false, 0); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire select " _RED_("error") "."); + PrintAndLogEx(ERR, "Desfire select " _RED_("error")); return 200; } if (verbose) @@ -1097,7 +1108,7 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s } else { res = DesfireSelectEx(dctx, true, way, id, NULL); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire %s select " _RED_("error") ".", DesfireSelectWayToStr(way)); + PrintAndLogEx(ERR, "Desfire %s select " _RED_("error"), DesfireSelectWayToStr(way)); return 202; } if (verbose) @@ -1107,13 +1118,14 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s if (selectfile) { res = DesfireSelectEx(dctx, false, ISWIsoID, isofileid, NULL); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire iso file select " _RED_("error") "."); + PrintAndLogEx(ERR, "Desfire iso file select " _RED_("error")); return 203; } - if (verbose) + if (verbose) { PrintAndLogEx(INFO, "Application %s file iso id %04x is " _GREEN_("selected"), DesfireWayIDStr(way, id), isofileid); } + } if (!noauth) { res = DesfireAuthenticate(dctx, secureChannel, verbose); @@ -1123,8 +1135,9 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s } if (DesfireIsAuthenticated(dctx)) { - if (verbose) + if (verbose) { PrintAndLogEx(INFO, "Desfire " _GREEN_("authenticated")); + } } else { return 201; } @@ -1864,17 +1877,21 @@ int DesfireFillAppList(DesfireContext_t *dctx, PICCInfo_t *PICCInfo, AppListS ap void DesfirePrintPICCInfo(DesfireContext_t *dctx, PICCInfo_t *PICCInfo) { PrintAndLogEx(SUCCESS, "------------------------------------ " _CYAN_("PICC level") " -------------------------------------"); - if (PICCInfo->freemem == 0xffffffff) - PrintAndLogEx(SUCCESS, "Applications count: " _GREEN_("%zu") " free memory " _YELLOW_("n/a"), PICCInfo->appCount); - else - PrintAndLogEx(SUCCESS, "Applications count: " _GREEN_("%zu") " free memory " _GREEN_("%d") " bytes", PICCInfo->appCount, PICCInfo->freemem); + if (PICCInfo->freemem == 0xffffffff) { + PrintAndLogEx(SUCCESS, "# applications....... " _YELLOW_("%zu"), PICCInfo->appCount); + } else { + PrintAndLogEx(SUCCESS, "# applications....... " _YELLOW_("%zu"), PICCInfo->appCount); + } + PrintAndLogEx(SUCCESS, ""); + if (PICCInfo->authCmdCheck.checked) { - PrintAndLogEx(SUCCESS, "PICC level auth commands: "); + PrintAndLogEx(SUCCESS, "PICC level auth commands"); DesfireCheckAuthCommandsPrint(&PICCInfo->authCmdCheck); } + if (PICCInfo->numberOfKeys > 0) { PrintKeySettings(PICCInfo->keySettings, PICCInfo->numKeysRaw, false, true); - PrintAndLogEx(SUCCESS, "PICC key 0 version: %d (0x%02x)", PICCInfo->keyVersion0, PICCInfo->keyVersion0); + PrintAndLogEx(SUCCESS, "PICC key "_YELLOW_("0") " version: %d (0x%02x)", PICCInfo->keyVersion0, PICCInfo->keyVersion0); } } @@ -1886,14 +1903,14 @@ void DesfirePrintAppList(DesfireContext_t *dctx, PICCInfo_t *PICCInfo, AppListS PrintAndLogEx(SUCCESS, "--------------------------------- " _CYAN_("Applications list") " ---------------------------------"); for (int i = 0; i < PICCInfo->appCount; i++) { - PrintAndLogEx(SUCCESS, _CYAN_("Application number: 0x%02X"), appList[i].appNum); - PrintAndLogEx(SUCCESS, " ISO id.... " _GREEN_("0x%04X"), appList[i].appISONum); - PrintAndLogEx(SUCCESS, " DF name... " _GREEN_("%s") " ( %s)", appList[i].appDFName, sprint_hex((uint8_t *)appList[i].appDFName, sizeof(appList[i].appDFName))); + PrintAndLogEx(SUCCESS, "Application ID....... " _CYAN_("0x%02X"), appList[i].appNum); + PrintAndLogEx(SUCCESS, " ISO id............ " _GREEN_("0x%04X"), appList[i].appISONum); + PrintAndLogEx(SUCCESS, " DF name........... " _GREEN_("%s") " ( %s )", appList[i].appDFName, sprint_hex_inrow((uint8_t *)appList[i].appDFName, sizeof(appList[i].appDFName))); DesfirePrintAIDFunctions(appList[i].appNum); if (PICCInfo->authCmdCheck.checked) { - PrintAndLogEx(SUCCESS, "Auth commands: "); + PrintAndLogEx(SUCCESS, "Auth commands"); DesfireCheckAuthCommandsPrint(&appList[i].authCmdCheck); PrintAndLogEx(SUCCESS, ""); } @@ -1902,7 +1919,7 @@ void DesfirePrintAppList(DesfireContext_t *dctx, PICCInfo_t *PICCInfo, AppListS PrintKeySettings(appList[i].keySettings, appList[i].numKeysRaw, true, true); if (appList[i].numberOfKeys > 0) { - PrintAndLogEx(SUCCESS, "Key versions [0..%d]: " NOLF, appList[i].numberOfKeys - 1); + PrintAndLogEx(SUCCESS, "Key versions [0..%d] " NOLF, appList[i].numberOfKeys - 1); for (uint8_t keyn = 0; keyn < appList[i].numberOfKeys; keyn++) { PrintAndLogEx(NORMAL, "%s %02x" NOLF, (keyn == 0) ? "" : ",", appList[i].keyVersions[keyn]); } @@ -2254,7 +2271,7 @@ int DesfireUpdateRecord(DesfireContext_t *dctx, uint8_t fnum, uint32_t recnum, u } static void PrintKeySettingsPICC(uint8_t keysettings, uint8_t numkeys, bool print2ndbyte) { - PrintAndLogEx(SUCCESS, "PICC level rights:"); + PrintAndLogEx(SUCCESS, "PICC level rights"); 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"); @@ -2263,27 +2280,27 @@ static void PrintKeySettingsPICC(uint8_t keysettings, uint8_t numkeys, bool prin if (print2ndbyte) { DesfirePrintCardKeyType(numkeys >> 6); - PrintAndLogEx(SUCCESS, "key count: %d", numkeys & 0x0f); + PrintAndLogEx(SUCCESS, "Key cnt.... " _YELLOW_("%d"), numkeys & 0x0F); } } static void PrintKeySettingsApp(uint8_t keysettings, uint8_t numkeys, bool print2ndbyte) { // Access rights. - PrintAndLogEx(SUCCESS, "Application level rights:"); + PrintAndLogEx(SUCCESS, "Application level rights"); uint8_t rights = ((keysettings >> 4) & 0x0F); switch (rights) { case 0x0: - PrintAndLogEx(SUCCESS, "-- AMK authentication is necessary to change any key (default)"); + PrintAndLogEx(SUCCESS, " - AMK authentication is necessary to change any key (default)"); break; case 0xE: - PrintAndLogEx(SUCCESS, "-- Authentication with the key to be changed (same KeyNo) is necessary to change a key"); + PrintAndLogEx(SUCCESS, " - Authentication with the key to be changed (same KeyNo) is necessary to change a key"); break; case 0xF: - PrintAndLogEx(SUCCESS, "-- All keys (except AMK,see Bit0) within this application are frozen"); + PrintAndLogEx(SUCCESS, " - All keys (except AMK,see Bit0) within this application are frozen"); break; default: PrintAndLogEx(SUCCESS, - "-- Authentication with the specified key " _YELLOW_("(0x%02x)") " is necessary to change any key.\n" + " - Authentication with the specified key " _YELLOW_("(0x%02x)") " is necessary to change any key.\n" "A change key and a PICC master key (CMK) can only be changed after authentication with the master key.\n" "For keys other then the master or change key, an authentication with the same key is needed.", rights & 0x0f @@ -2299,10 +2316,10 @@ static void PrintKeySettingsApp(uint8_t keysettings, uint8_t numkeys, bool print if (print2ndbyte) { DesfirePrintCardKeyType(numkeys >> 6); - PrintAndLogEx(SUCCESS, "key count: %d", numkeys & 0x0f); - if (numkeys & 0x20) + PrintAndLogEx(SUCCESS, "Key cnt.... " _YELLOW_("%d"), numkeys & 0x0F); + if (numkeys & 0x20) { PrintAndLogEx(SUCCESS, "iso file id: enabled"); - PrintAndLogEx(SUCCESS, ""); + } } } diff --git a/client/src/mifare/desfirecrypto.c b/client/src/mifare/desfirecrypto.c index 6263c2321..98c3de0e9 100644 --- a/client/src/mifare/desfirecrypto.c +++ b/client/src/mifare/desfirecrypto.c @@ -504,16 +504,16 @@ uint8_t DesfireKeyAlgoToType(DesfireCryptoAlgorithm keyType) { void DesfirePrintCardKeyType(uint8_t keyType) { switch (keyType) { case 00: - PrintAndLogEx(SUCCESS, "Key: 2TDEA"); + PrintAndLogEx(SUCCESS, "Key type... " _YELLOW_("2TDEA")); break; case 01: - PrintAndLogEx(SUCCESS, "Key: 3TDEA"); + PrintAndLogEx(SUCCESS, "Key type... " _YELLOW_("3TDEA")); break; case 02: - PrintAndLogEx(SUCCESS, "Key: AES"); + PrintAndLogEx(SUCCESS, "Key type... " _YELLOW_("AES")); break; default: - PrintAndLogEx(SUCCESS, "Key: unknown: 0x%02x", keyType); + PrintAndLogEx(SUCCESS, "Key type... " _YELLOW_("unknown") " - 0x%02x", keyType); break; } } diff --git a/client/src/mifare/mad.c b/client/src/mifare/mad.c index 54f67a6a4..e254fd187 100644 --- a/client/src/mifare/mad.c +++ b/client/src/mifare/mad.c @@ -69,8 +69,13 @@ static int open_mad_file(json_t **root, bool verbose) { goto out; } - if (verbose) - PrintAndLogEx(SUCCESS, "Loaded file " _YELLOW_("`%s`") " (%s) %zu records.", path, _GREEN_("ok"), json_array_size(*root)); + if (verbose) { + PrintAndLogEx(SUCCESS, "Loaded file `" _YELLOW_("%s") "` " _GREEN_("%zu") " records ( " _GREEN_("ok") " )" + , path + , json_array_size(*root) + ); + } + out: free(path); return retval; @@ -415,7 +420,7 @@ int MADDFDecodeAndPrint(uint32_t short_aid, bool verbose) { open_mad_file(&mad_known_aids, false); char fmt[128]; - snprintf(fmt, sizeof(fmt), " MAD AID Function 0x%04X :" _YELLOW_("%s"), short_aid, "%s"); + snprintf(fmt, sizeof(fmt), " MAD AID Function 0x%04X... " _YELLOW_("%s"), short_aid, "%s"); print_aid_description(mad_known_aids, short_aid, fmt, verbose); close_mad_file(mad_known_aids); return PM3_SUCCESS; @@ -429,8 +434,9 @@ bool HasMADKey(uint8_t *d) { } int DetectHID(uint8_t *d, uint16_t manufacture) { - if (d == NULL) + if (d == NULL) { return -1; + } // find HID for (int i = 1; i < 16; i++) { diff --git a/client/src/mifare/mifarehost.c b/client/src/mifare/mifarehost.c index 18f22af7c..0a9d7aba9 100644 --- a/client/src/mifare/mifarehost.c +++ b/client/src/mifare/mifarehost.c @@ -291,9 +291,16 @@ int mf_check_keys_fast_ex(uint8_t sectorsCnt, uint8_t firstChunk, uint8_t lastCh if ((singleSectorParams >> 15) & 1) { if (curr_keys) { - uint64_t foo = bytes_to_num(resp.data.asBytes, 6); + // uint64_t foo = bytes_to_num(resp.data.asBytes, 6); PrintAndLogEx(NORMAL, ""); - PrintAndLogEx(SUCCESS, _GREEN_("Key %s for block %2i found: %012" PRIx64), (singleSectorParams >> 8) & 1 ? "B" : "A", singleSectorParams & 0xFF, foo); +// PrintAndLogEx(SUCCESS, "found Key %s for block %2i found: " _GREEN_("%012" PRIx64), (singleSectorParams >> 8) & 1 ? "B" : "A", singleSectorParams & 0xFF, foo); + + PrintAndLogEx(SUCCESS, "\nTarget block %4u key type %c -- found valid key [ " _GREEN_("%s") " ]", + singleSectorParams & 0xFF, + ((singleSectorParams >> 8) & 1) ? 'B' : 'A', + sprint_hex_inrow(resp.data.asBytes, MIFARE_KEY_SIZE) + ); + return PM3_SUCCESS; } }