From 4ff1a5028833af0dc12a930d9052054c2ef19b33 Mon Sep 17 00:00:00 2001 From: Sanduuz Date: Sat, 20 Jun 2026 23:53:13 +0300 Subject: [PATCH] Moved HRT parser logic under client/src/parsers + Restructured parser calling code to its own file that is hooked from `hf mfdes info` command --- client/CMakeLists.txt | 3 +- client/Makefile | 3 +- client/experimental_lib/CMakeLists.txt | 3 +- client/src/cmdhfmfdes.c | 76 +--------- .../hrtparser/hrtparser.c} | 4 +- .../hrtparser/hrtparser.h} | 6 +- client/src/parsers/parsehrt.c | 135 ++++++++++++++++++ client/src/parsers/parsehrt.h | 11 ++ 8 files changed, 161 insertions(+), 80 deletions(-) rename client/src/{cmdparsehrt.c => parsers/hrtparser/hrtparser.c} (99%) rename client/src/{cmdparsehrt.h => parsers/hrtparser/hrtparser.h} (99%) create mode 100644 client/src/parsers/parsehrt.c create mode 100644 client/src/parsers/parsehrt.h diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index a08ee88ec..409a0cf2e 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -468,7 +468,6 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/cmdmain.c ${PM3_ROOT}/client/src/cmdmqtt.c ${PM3_ROOT}/client/src/cmdnfc.c - ${PM3_ROOT}/client/src/cmdparsehrt.c ${PM3_ROOT}/client/src/cmdparser.c ${PM3_ROOT}/client/src/cmdpiv.c ${PM3_ROOT}/client/src/cmdscript.c @@ -484,6 +483,8 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/iso4217.c ${PM3_ROOT}/client/src/jansson_path.c ${PM3_ROOT}/client/src/lua_bitlib.c + ${PM3_ROOT}/client/src/parsers/parsehrt.c + ${PM3_ROOT}/client/src/parsers/hrtparser/hrtparser.c ${PM3_ROOT}/client/src/pla.c ${PM3_ROOT}/client/src/preferences.c ${PM3_ROOT}/client/src/pm3.c diff --git a/client/Makefile b/client/Makefile index f511239ca..48e291fcd 100644 --- a/client/Makefile +++ b/client/Makefile @@ -809,7 +809,6 @@ SRCS = mifare/aiddesfire.c \ cmdmain.c \ cmdmqtt.c \ cmdnfc.c \ - cmdparsehrt.c \ cmdparser.c \ cmdpiv.c \ cmdscript.c \ @@ -883,6 +882,8 @@ SRCS = mifare/aiddesfire.c \ mifare/prime.c \ mifare/gen4.c \ nfc/ndef.c \ + parsers/parsehrt.c \ + parsers/hrtparser/hrtparser.c \ pm3.c \ pm3_binlib.c \ pm3_bitlib.c \ diff --git a/client/experimental_lib/CMakeLists.txt b/client/experimental_lib/CMakeLists.txt index 171204011..f3cda3e7b 100644 --- a/client/experimental_lib/CMakeLists.txt +++ b/client/experimental_lib/CMakeLists.txt @@ -388,7 +388,6 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/cmdmain.c ${PM3_ROOT}/client/src/cmdmqtt.c ${PM3_ROOT}/client/src/cmdnfc.c - ${PM3_ROOT}/client/src/cmdparsehrt.c ${PM3_ROOT}/client/src/cmdparser.c ${PM3_ROOT}/client/src/cmdpiv.c ${PM3_ROOT}/client/src/cmdscript.c @@ -404,6 +403,8 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/iso4217.c ${PM3_ROOT}/client/src/jansson_path.c ${PM3_ROOT}/client/src/lua_bitlib.c + ${PM3_ROOT}/client/src/parsers/parsehrt.c + ${PM3_ROOT}/client/src/parsers/hrtparser/hrtparser.c ${PM3_ROOT}/client/src/pla.c ${PM3_ROOT}/client/src/preferences.c ${PM3_ROOT}/client/src/pm3.c diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 2ded59c41..3d96e192f 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -24,7 +24,6 @@ #include #include "jansson.h" #include "commonutil.h" // ARRAYLEN -#include "cmdparsehrt.h" #include "cmdparser.h" // command_t #include "comms.h" #include "mifare/desfirecrypto.h" @@ -35,6 +34,7 @@ #include "crypto/libpcrypto.h" #include "crypto/duoxcrypto.h" #include "protocols.h" +#include "parsers/parsehrt.h" #include "cmdtrace.h" #include "cliparser.h" #include "iso7816/apduinfo.h" // APDU manipulation / errorcodes @@ -420,76 +420,6 @@ static const char *getProductTypeStr(const uint8_t *versionhw) { return "UNKNOWN PROD"; } -static bool hf_mfdes_try_parse_hrt(DesfireContext_t *dctx) { - DesfireSetCommMode(dctx, DCMPlain); - DesfireSetCommandSet(dctx, DCCNative); - - // TODO: Add support for older travelcard (AID EF2011) - - if (DesfireSelectAIDHex(dctx, 0xEF2014, false, 0) == PM3_SUCCESS) { - uint8_t buf[APDU_RES_LEN] = {0}; - size_t len = 0; - - hrt_travel_card_t card; - hrt_travelcard_init_empty(&card, 2); - - // File 0x08: Application info (11 bytes) - len = 0; - if (DesfireReadFile(dctx, 0x08, 0, 11, buf, &len) == PM3_SUCCESS) { - hrt_travelcard_set_application_info(&card, buf, len); - } - - // Check that application info parses before reading other files - const char *application_id = hrt_travelcard_get_application_instance_id(&card); - if (application_id == NULL || application_id[0] == '\0') { - hrt_travelcard_free(&card); - return false; - } - PrintAndLogEx(SUCCESS, "HRT travel card detected (AID EF2014)"); - - // File 0x00: Control info (10 bytes) - len = 0; - if (DesfireReadFile(dctx, 0x00, 0, 10, buf, &len) == PM3_SUCCESS) { - hrt_travelcard_set_control_info(&card, buf, len); - } - - // File 0x01: Period pass (35 bytes) - len = 0; - if (DesfireReadFile(dctx, 0x01, 0, 35, buf, &len) == PM3_SUCCESS) { - hrt_travelcard_set_period_pass(&card, buf, len); - } - - // File 0x02: Stored value (13 bytes) - len = 0; - if (DesfireReadFile(dctx, 0x02, 0, 13, buf, &len) == PM3_SUCCESS) { - hrt_travelcard_set_stored_value(&card, buf, len); - - // Demo: Read card stored value - int stored_value = hrt_travelcard_get_stored_value_counter(&card); - char price_buf[32] = {0}; - hrt_price_to_string(stored_value, price_buf, sizeof(price_buf)); - PrintAndLogEx(SUCCESS, "HRT stored value: %s", price_buf); - } - - // File 0x03: eTicket (45 bytes) - len = 0; - if (DesfireReadFile(dctx, 0x03, 0, 45, buf, &len) == PM3_SUCCESS) { - hrt_travelcard_set_eticket(&card, buf, len); - } - - // File 0x04: History records (all) - len = 0; - if (DesfireReadRecords(dctx, 0x04, 0, 0, buf, &len) == PM3_SUCCESS) { - hrt_travelcard_set_history(&card, buf, len); - } - - hrt_travelcard_free(&card); - return true; - } - - return false; -} - int mfdes_get_info(mfdes_info_res_t *info) { PacketResponseNG resp; @@ -1202,7 +1132,9 @@ static int CmdHF14ADesInfo(const char *Cmd) { */ - (void)hf_mfdes_try_parse_hrt(&dctx); + if (is_valid_hrt_card(&dctx, aidbuf, aidbuflen)) { + (void)hrt_parser_parse(&dctx); + } DropField(); return PM3_SUCCESS; diff --git a/client/src/cmdparsehrt.c b/client/src/parsers/hrtparser/hrtparser.c similarity index 99% rename from client/src/cmdparsehrt.c rename to client/src/parsers/hrtparser/hrtparser.c index 135fe019e..7161f40da 100644 --- a/client/src/cmdparsehrt.c +++ b/client/src/parsers/hrtparser/hrtparser.c @@ -23,8 +23,8 @@ #endif #include "cmdparser.h" -#include "cmdparsehrt.h" #include "comms.h" +#include "hrtparser.h" #include "common.h" #include "ui.h" #include @@ -812,7 +812,7 @@ static int CmdHelp(const char *Cmd) { return PM3_SUCCESS; } -int CmdParseHRT(const char *Cmd) { +int HRTParser(const char *Cmd) { clearCommandBuffer(); return CmdsParse(CommandTable, Cmd); } diff --git a/client/src/cmdparsehrt.h b/client/src/parsers/hrtparser/hrtparser.h similarity index 99% rename from client/src/cmdparsehrt.h rename to client/src/parsers/hrtparser/hrtparser.h index cffdab461..89b489d26 100644 --- a/client/src/cmdparsehrt.h +++ b/client/src/parsers/hrtparser/hrtparser.h @@ -16,8 +16,8 @@ // Parse Helsinki Region Transport (HRT) travel cards //----------------------------------------------------------------------------- -#ifndef CMDParseHRT_H__ -#define CMDParseHRT_H__ +#ifndef HRTPARSER_H__ +#define HRTPARSER_H__ #include "common.h" #include @@ -221,7 +221,7 @@ int hrt_travelcard_get_period_loading_device_number(const hrt_travel_card int hrt_travelcard_get_app_status(const hrt_travel_card_t *card); int hrt_travelcard_get_version(const hrt_travel_card_t *card); -int CmdParseHRT(const char *Cmd); +int HRTParser(const char *Cmd); // Setter Functions void hrt_travelcard_init_empty(hrt_travel_card_t *card, int version); diff --git a/client/src/parsers/parsehrt.c b/client/src/parsers/parsehrt.c new file mode 100644 index 000000000..56388db23 --- /dev/null +++ b/client/src/parsers/parsehrt.c @@ -0,0 +1,135 @@ +#include "hrtparser/hrtparser.h" +#include "iso7816/iso7816core.h" +#include "mifare/desfirecore.h" +#include "mifare/desfirecrypto.h" +#include "parsehrt.h" +#include "pm3_cmd.h" +#include "ui.h" + +#define HRT_NEW_AID 0xEF2014 +#define HRT_OLD_AID 0xEF2011 +#define HRT_APPLICATION_INFO_FILE_ID 0x08 +#define HRT_APPLICATION_INFO_LEN 11 + +static bool hrt_aid_list_contains(const uint8_t *aidbuf, size_t aidbuflen) { + if (aidbuf == NULL || aidbuflen < 3) return false; + + for (size_t i = 0; i + 2 < aidbuflen; i += 3) { + // TODO: Also add HRT_OLD_AID check. Currently it is not + // checked since parsing the older cards is not yet implemented. + if (DesfireAIDByteToUint(&aidbuf[i]) == HRT_NEW_AID) { + return true; + } + } + + return false; +} + +static bool hrt_read_and_parse_application_info(DesfireContext_t *dctx, hrt_travel_card_t *card) { + if (dctx == NULL || card == NULL) return false; + + uint8_t buf[APDU_RES_LEN] = {0}; + size_t len = 0; + + if (DesfireReadFile(dctx, HRT_APPLICATION_INFO_FILE_ID, 0, HRT_APPLICATION_INFO_LEN, buf, &len) != PM3_SUCCESS) { + return false; + } + + if (hrt_travelcard_set_application_info(card, buf, len) == false) { + return false; + } + + const char *application_id = hrt_travelcard_get_application_instance_id(card); + return application_id != NULL && application_id[0] != '\0'; +} + +bool is_valid_hrt_card(DesfireContext_t *dctx, const uint8_t *aidbuf, size_t aidbuflen) { + if (dctx == NULL) return false; + + if (hrt_aid_list_contains(aidbuf, aidbuflen) == false) { + return false; + } + + DesfireSetCommMode(dctx, DCMPlain); + DesfireSetCommandSet(dctx, DCCNative); + + // TODO: Add support for older travelcard (AID EF2011) + + if (DesfireSelectAIDHex(dctx, HRT_NEW_AID, false, 0) != PM3_SUCCESS) { + return false; + } + + hrt_travel_card_t card; + hrt_travelcard_init_empty(&card, 2); + + bool is_valid = hrt_read_and_parse_application_info(dctx, &card); + + hrt_travelcard_free(&card); + return is_valid; +} + +bool hrt_parser_parse(DesfireContext_t *dctx) { + if (dctx == NULL) return false; + + DesfireSetCommMode(dctx, DCMPlain); + DesfireSetCommandSet(dctx, DCCNative); + + // TODO: Add support for older travelcard (AID EF2011) + + if (DesfireSelectAIDHex(dctx, HRT_NEW_AID, false, 0) == PM3_SUCCESS) { + uint8_t buf[APDU_RES_LEN] = {0}; + size_t len = 0; + + hrt_travel_card_t card; + hrt_travelcard_init_empty(&card, 2); + + // File 0x08: Application info (11 bytes) + // Check that application info parses before reading other files + if (hrt_read_and_parse_application_info(dctx, &card) == false) { + hrt_travelcard_free(&card); + return false; + } + PrintAndLogEx(SUCCESS, "HRT travel card detected (AID %06X)", HRT_NEW_AID); + + // File 0x00: Control info (10 bytes) + len = 0; + if (DesfireReadFile(dctx, 0x00, 0, 10, buf, &len) == PM3_SUCCESS) { + hrt_travelcard_set_control_info(&card, buf, len); + } + + // File 0x01: Period pass (35 bytes) + len = 0; + if (DesfireReadFile(dctx, 0x01, 0, 35, buf, &len) == PM3_SUCCESS) { + hrt_travelcard_set_period_pass(&card, buf, len); + } + + // File 0x02: Stored value (13 bytes) + len = 0; + if (DesfireReadFile(dctx, 0x02, 0, 13, buf, &len) == PM3_SUCCESS) { + hrt_travelcard_set_stored_value(&card, buf, len); + + // Demo: Read card stored value + int stored_value = hrt_travelcard_get_stored_value_counter(&card); + char price_buf[32] = {0}; + hrt_price_to_string(stored_value, price_buf, sizeof(price_buf)); + PrintAndLogEx(SUCCESS, "HRT stored value: %s", price_buf); + } + + // File 0x03: eTicket (45 bytes) + len = 0; + if (DesfireReadFile(dctx, 0x03, 0, 45, buf, &len) == PM3_SUCCESS) { + hrt_travelcard_set_eticket(&card, buf, len); + } + + // File 0x04: History records (all) + len = 0; + if (DesfireReadRecords(dctx, 0x04, 0, 0, buf, &len) == PM3_SUCCESS) { + hrt_travelcard_set_history(&card, buf, len); + } + + hrt_travelcard_free(&card); + return true; + } + + return false; +} diff --git a/client/src/parsers/parsehrt.h b/client/src/parsers/parsehrt.h new file mode 100644 index 000000000..09b8d75a6 --- /dev/null +++ b/client/src/parsers/parsehrt.h @@ -0,0 +1,11 @@ +#ifndef PARSEHRT_H__ +#define PARSEHRT_H__ + +#include "mifare/desfirecore.h" +#include +#include + +bool is_valid_hrt_card(DesfireContext_t *dctx, const uint8_t *aidbuf, size_t aidbuflen); +bool hrt_parser_parse(DesfireContext_t *dctx); + +#endif