diff --git a/CHANGELOG.md b/CHANGELOG.md index c5f50927c..7692bd102 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] +- Changed `hf seos info` - also prints UID, ATQA, SAK, ATS, MIFARE Classic emulation, which SEOS AIDs answer and the known AIDs found on the card (@antoniovazquezblanco) - Improved PM5 nonce-acquisition throughput for mfc by replacing the fixed 68-pair cap with a 2900 ms target budget (@nip-was-here) - Fixed PM5 tick-count reads across RTC second rollovers to prevent false receive timeouts - Fixed iso14443a - reject incomplete encrypted nonce frames instead of delaying nested authentication on PM5 (@nip-was-here) diff --git a/client/src/cmdhf14a.c b/client/src/cmdhf14a.c index 67ba85d71..d8758e23f 100644 --- a/client/src/cmdhf14a.c +++ b/client/src/cmdhf14a.c @@ -2933,6 +2933,107 @@ static void get_compact_tlv(uint8_t *d, uint8_t n) { } } +// selects every AID in aidlist.json, returns how many were found +int hf14a_aid_search(bool verbose) { + + PrintAndLogEx(INFO, "-------------------- " _CYAN_("AID Search") " --------------------"); + + json_t *root = AIDSearchInit(verbose); + if (root == NULL) { + return 0; + } + + int found = 0; + bool ActivateField = true; + for (size_t elmindx = 0; elmindx < json_array_size(root); elmindx++) { + + if (kbd_enter_pressed()) { + break; + } + + json_t *data = AIDSearchGetElm(root, elmindx); + uint8_t vaid[200] = {0}; + int vaidlen = 0; + if ((AIDGetFromElm(data, vaid, sizeof(vaid), &vaidlen) == false) || (vaidlen == 0)) { + continue; + } + if (AIDSeenBefore(root, vaid, (size_t)vaidlen, elmindx)) { + continue; + } + + uint16_t sw = 0; + uint8_t result[1024] = {0}; + size_t resultlen = 0; + int res = Iso7816Select(CC_CONTACTLESS, ActivateField, true, vaid, vaidlen, result, sizeof(result), &resultlen, &sw); + ActivateField = false; + if (res) { + continue; + } + + uint8_t dfname[200] = {0}; + size_t dfnamelen = 0; + if (resultlen > 3) { + struct tlvdb *tlv = tlvdb_parse_multi(result, resultlen); + if (tlv) { + // 0x84 Dedicated File (DF) Name + const struct tlv *dfnametlv = tlvdb_get_tlv(tlvdb_find_full(tlv, 0x84)); + if (dfnametlv) { + dfnamelen = dfnametlv->len; + memcpy(dfname, dfnametlv->value, dfnamelen); + } + tlvdb_free(tlv); + } + } + + if (sw == ISO7816_OK || sw == ISO7816_INVALID_DF || sw == ISO7816_FILE_TERMINATED) { + if (sw == ISO7816_OK) { + if (verbose) PrintAndLogEx(SUCCESS, "Application ( " _GREEN_("ok") " )"); + } else { + if (verbose) PrintAndLogEx(WARNING, "Application ( " _RED_("blocked") " )"); + } + + uint8_t response_for_match[1026] = {0}; + size_t response_for_match_len = resultlen; + memcpy(response_for_match, result, MIN(resultlen, sizeof(response_for_match))); + if ((response_for_match_len + 2) <= sizeof(response_for_match)) { + response_for_match[response_for_match_len] = sw >> 8; + response_for_match[response_for_match_len + 1] = sw & 0xff; + response_for_match_len += 2; + } + PrintAIDDescriptionEx(root, sprint_hex_inrow(vaid, vaidlen), response_for_match, response_for_match_len, verbose); + + if (dfnamelen) { + if (dfnamelen == vaidlen) { + + if (memcmp(dfname, vaid, vaidlen) == 0) { + if (verbose) PrintAndLogEx(INFO, "(DF) Name found and equal to AID"); + } else { + PrintAndLogEx(INFO, "(DF) Name not equal to AID: " _YELLOW_("%s"), sprint_hex_inrow(dfname, dfnamelen)); + PrintAIDDescriptionBuf(root, dfname, dfnamelen, verbose); + } + + } else { + PrintAndLogEx(INFO, "(DF) Name not equal to AID: " _YELLOW_("%s"), sprint_hex_inrow(dfname, dfnamelen)); + PrintAIDDescriptionBuf(root, dfname, dfnamelen, verbose); + } + + } else { + if (verbose) PrintAndLogEx(INFO, "(DF) Name not found"); + } + + if (verbose) PrintAndLogEx(SUCCESS, "----------------------------------------------------"); + found++; + } + + } + DropField(); + if (verbose == false && found) { + PrintAndLogEx(INFO, "----------------------------------------------------"); + } + AIDSearchFree(root); + return found; +} + int infoHF14A(bool verbose, bool do_nack_test, bool do_aid_search) { uint8_t dbg_curr = DBG_NONE; @@ -3415,99 +3516,8 @@ int infoHF14A(bool verbose, bool do_nack_test, bool do_aid_search) { } if (do_aid_search) { - - PrintAndLogEx(INFO, "-------------------- " _CYAN_("AID Search") " --------------------"); - - json_t *root = AIDSearchInit(verbose); - if (root != NULL) { - bool found = false; - bool ActivateField = true; - for (size_t elmindx = 0; elmindx < json_array_size(root); elmindx++) { - - if (kbd_enter_pressed()) { - break; - } - - json_t *data = AIDSearchGetElm(root, elmindx); - uint8_t vaid[200] = {0}; - int vaidlen = 0; - if ((AIDGetFromElm(data, vaid, sizeof(vaid), &vaidlen) == false) || (vaidlen == 0)) { - continue; - } - if (AIDSeenBefore(root, vaid, (size_t)vaidlen, elmindx)) { - continue; - } - - uint16_t sw = 0; - uint8_t result[1024] = {0}; - size_t resultlen = 0; - res = Iso7816Select(CC_CONTACTLESS, ActivateField, true, vaid, vaidlen, result, sizeof(result), &resultlen, &sw); - ActivateField = false; - if (res) { - continue; - } - - uint8_t dfname[200] = {0}; - size_t dfnamelen = 0; - if (resultlen > 3) { - struct tlvdb *tlv = tlvdb_parse_multi(result, resultlen); - if (tlv) { - // 0x84 Dedicated File (DF) Name - const struct tlv *dfnametlv = tlvdb_get_tlv(tlvdb_find_full(tlv, 0x84)); - if (dfnametlv) { - dfnamelen = dfnametlv->len; - memcpy(dfname, dfnametlv->value, dfnamelen); - } - tlvdb_free(tlv); - } - } - - if (sw == ISO7816_OK || sw == ISO7816_INVALID_DF || sw == ISO7816_FILE_TERMINATED) { - if (sw == ISO7816_OK) { - if (verbose) PrintAndLogEx(SUCCESS, "Application ( " _GREEN_("ok") " )"); - } else { - if (verbose) PrintAndLogEx(WARNING, "Application ( " _RED_("blocked") " )"); - } - - uint8_t response_for_match[1026] = {0}; - size_t response_for_match_len = resultlen; - memcpy(response_for_match, result, MIN(resultlen, sizeof(response_for_match))); - if ((response_for_match_len + 2) <= sizeof(response_for_match)) { - response_for_match[response_for_match_len] = sw >> 8; - response_for_match[response_for_match_len + 1] = sw & 0xff; - response_for_match_len += 2; - } - PrintAIDDescriptionEx(root, sprint_hex_inrow(vaid, vaidlen), response_for_match, response_for_match_len, verbose); - - if (dfnamelen) { - if (dfnamelen == vaidlen) { - - if (memcmp(dfname, vaid, vaidlen) == 0) { - if (verbose) PrintAndLogEx(INFO, "(DF) Name found and equal to AID"); - } else { - PrintAndLogEx(INFO, "(DF) Name not equal to AID: " _YELLOW_("%s"), sprint_hex_inrow(dfname, dfnamelen)); - PrintAIDDescriptionBuf(root, dfname, dfnamelen, verbose); - } - - } else { - PrintAndLogEx(INFO, "(DF) Name not equal to AID: " _YELLOW_("%s"), sprint_hex_inrow(dfname, dfnamelen)); - PrintAIDDescriptionBuf(root, dfname, dfnamelen, verbose); - } - - } else { - if (verbose) PrintAndLogEx(INFO, "(DF) Name not found"); - } - - if (verbose) PrintAndLogEx(SUCCESS, "----------------------------------------------------"); - found = true; - isEMV = true; - } - - } - DropField(); - if (verbose == false && found) { - PrintAndLogEx(INFO, "----------------------------------------------------"); - } + if (hf14a_aid_search(verbose) > 0) { + isEMV = true; } } diff --git a/client/src/cmdhf14a.h b/client/src/cmdhf14a.h index 830d9f974..6d2b8bd4f 100644 --- a/client/src/cmdhf14a.h +++ b/client/src/cmdhf14a.h @@ -88,6 +88,7 @@ int hf14a_getconfig(hf14a_config_t *config); int hf14a_setconfig(hf14a_config_t *config, bool verbose); int infoHF14A(bool verbose, bool do_nack_test, bool do_aid_search); int infoHF14A4Applications(bool verbose); +int hf14a_aid_search(bool verbose); const char *getTagInfo(uint8_t uid); int Hf14443_4aGetCardData(iso14a_card_select_t *card); int ExchangeAPDU14a(const uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen); diff --git a/client/src/cmdhfseos.c b/client/src/cmdhfseos.c index ef2289514..df456ce0f 100644 --- a/client/src/cmdhfseos.c +++ b/client/src/cmdhfseos.c @@ -36,6 +36,7 @@ #include "wiegand_formats.h" #include "wiegand_formatutils.h" #include "iso7816/apduinfo.h" // GetAPDUCodeDescription +#include "iso7816/iso7816core.h" // Iso7816Select #include "crypto/asn1utils.h" // ASN1 decode / print #include "crypto/libpcrypto.h" // AES decrypt #include "commonutil.h" // get_sw @@ -1674,10 +1675,69 @@ static int seos_load_keys(char *filename) { return PM3_SUCCESS; } +static void seos_print_aid_status(const uint8_t *aid, int aid_len) { + uint8_t response[PM3_CMD_DATA_SIZE] = {0}; + size_t resplen = 0; + uint16_t sw = 0; + // fresh activation, an already selected applet may answer the SELECT itself + Iso7816Select(CC_CONTACTLESS, true, true, (uint8_t *)aid, aid_len, response, sizeof(response), &resplen, &sw); + + // sw stays 0 when the card did not answer + if (sw == ISO7816_OK) { + PrintAndLogEx(SUCCESS, "%s ( " _GREEN_("ok") " )", sprint_hex_inrow(aid, aid_len)); + } else if (sw != 0) { + PrintAndLogEx(INFO, "%s ( %04X - %s )", sprint_hex_inrow(aid, aid_len), sw, GetAPDUCodeDescription(sw >> 8, sw & 0xff)); + } else { + PrintAndLogEx(FAILED, "%s ( " _RED_("no answer") " )", sprint_hex_inrow(aid, aid_len)); + } +} + +static int seos_print_card(const uint8_t *custom_aid, int custom_aid_len) { + iso14a_card_select_t card; + int res = SelectCard14443A_4(false, true, &card); + if (res != PM3_SUCCESS) { + DropField(); + return res; + } + + PrintAndLogEx(INFO, "--- " _CYAN_("Card") " ---------------------------"); + PrintAndLogEx(SUCCESS, "UID.............................. " _YELLOW_("%s"), sprint_hex(card.uid, card.uidlen)); + PrintAndLogEx(SUCCESS, "ATQA............................. " _YELLOW_("%02X %02X"), card.atqa[1], card.atqa[0]); + PrintAndLogEx(SUCCESS, "SAK.............................. " _YELLOW_("%02X"), card.sak); + // ats_len includes CRC + if (card.ats_len > 2) { + PrintAndLogEx(SUCCESS, "ATS.............................. " _YELLOW_("%s"), sprint_hex(card.ats, card.ats_len - 2)); + } + // SAK b4: MIFARE Classic, b5: 4K + if ((card.sak & 0x08) == 0x08) { + PrintAndLogEx(SUCCESS, "MIFARE Classic emulation......... " _YELLOW_("%s"), ((card.sak & 0x10) == 0x10) ? "4K" : "1K"); + } else { + PrintAndLogEx(SUCCESS, "MIFARE Classic emulation......... " _YELLOW_("none")); + } + + PrintAndLogEx(INFO, "--- " _CYAN_("SEOS AIDs") " ---------------------------"); + if (custom_aid_len > 0) { + seos_print_aid_status(custom_aid, custom_aid_len); + } else { + for (size_t i = 0; i < ARRAYLEN(known_seos_aids); i++) { + uint8_t aid[MAX_SEOS_AID_LEN] = {0}; + int aid_len = 0; + param_gethex_to_eol(known_seos_aids[i], 0, aid, sizeof(aid), &aid_len); + seos_print_aid_status(aid, aid_len); + } + } + DropField(); + + hf14a_aid_search(false); + return PM3_SUCCESS; +} + static int CmdHfSeosInfo(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "hf seos info", - "Requests the unauthenticated information from the default ADF of a SEOS card\n" + "Prints the card's UID, ATQA, SAK, ATS and MIFARE Classic emulation, which SEOS AIDs it answers\n" + "and which aidlist.json AIDs it has, then\n" + "requests the unauthenticated information from the default ADF of a SEOS card\n" "- If the card is a SEOS card\n" "- Are static RND.ICC keys used (can detect SEOS default keyset)\n" "- What encryption and hashing algorithm is use\n\n" @@ -1725,6 +1785,11 @@ static int CmdHfSeosInfo(const char *Cmd) { if (res != PM3_SUCCESS) { return res; } + + res = seos_print_card(aid, aid_len); + if (res != PM3_SUCCESS) { + return res; + } return seos_select(aid, aid_len, privacy_key_index, auth_key_index); } diff --git a/doc/commands.json b/doc/commands.json index e458e2714..513af15b2 100644 --- a/doc/commands.json +++ b/doc/commands.json @@ -10245,7 +10245,7 @@ }, "hf seos info": { "command": "hf seos info", - "description": "Requests the unauthenticated information from the default ADF of a SEOS card - If the card is a SEOS card - Are static RND.ICC keys used (can detect SEOS default keyset) - What encryption and hashing algorithm is use By default: - Privacy Key: 0 The selected privacy slot provides both encryption and MAC subkeys for ADF handling. - Auth Key : 0 The selected auth slot is sent to the card for challenge/authentication.", + "description": "Prints the card's UID, ATQA, SAK, ATS and MIFARE Classic emulation, which SEOS AIDs it answers and which aidlist.json AIDs it has, then requests the unauthenticated information from the default ADF of a SEOS card - If the card is a SEOS card - Are static RND.ICC keys used (can detect SEOS default keyset) - What encryption and hashing algorithm is use By default: - Privacy Key: 0 The selected privacy slot provides both encryption and MAC subkeys for ADF handling. - Auth Key : 0 The selected auth slot is sent to the card for challenge/authentication.", "notes": [ "hf seos info", "hf seos info --privacy-key 8 --auth-key 9",