diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 6957aa63c..32373dac9 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -518,6 +518,17 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/parsers/parserkf.c ${PM3_ROOT}/client/src/parsers/parsenortic.c ${PM3_ROOT}/client/src/parsers/parsetransit.c + ${PM3_ROOT}/client/src/parsers/parsebip.c + ${PM3_ROOT}/client/src/parsers/parsecharlie.c + ${PM3_ROOT}/client/src/parsers/parsekazan.c + ${PM3_ROOT}/client/src/parsers/parsemetromoney.c + ${PM3_ROOT}/client/src/parsers/parsenextfare.c + ${PM3_ROOT}/client/src/parsers/parseplantain.c + ${PM3_ROOT}/client/src/parsers/parserenfe.c + ${PM3_ROOT}/client/src/parsers/parsesmartrider.c + ${PM3_ROOT}/client/src/parsers/parsetroika.c + ${PM3_ROOT}/client/src/parsers/parseumarsh.c + ${PM3_ROOT}/client/src/parsers/parsezolotayakorona.c ${PM3_ROOT}/client/src/parsers/hrtparser/hrtparser.c ${PM3_ROOT}/client/src/pla.c ${PM3_ROOT}/client/src/preferences.c diff --git a/client/Makefile b/client/Makefile index 8ea4a095f..2e8465431 100644 --- a/client/Makefile +++ b/client/Makefile @@ -925,6 +925,17 @@ SRCS = mifare/aiddesfire.c \ parsers/parserkf.c \ parsers/parsenortic.c \ parsers/parsetransit.c \ + parsers/parsebip.c \ + parsers/parsecharlie.c \ + parsers/parsekazan.c \ + parsers/parsemetromoney.c \ + parsers/parsenextfare.c \ + parsers/parseplantain.c \ + parsers/parserenfe.c \ + parsers/parsesmartrider.c \ + parsers/parsetroika.c \ + parsers/parseumarsh.c \ + parsers/parsezolotayakorona.c \ parsers/hrtparser/hrtparser.c \ pm3.c \ pm3_binlib.c \ diff --git a/client/experimental_lib/CMakeLists.txt b/client/experimental_lib/CMakeLists.txt index b9290140b..d333bf7f8 100644 --- a/client/experimental_lib/CMakeLists.txt +++ b/client/experimental_lib/CMakeLists.txt @@ -445,6 +445,17 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/parsers/parserkf.c ${PM3_ROOT}/client/src/parsers/parsenortic.c ${PM3_ROOT}/client/src/parsers/parsetransit.c + ${PM3_ROOT}/client/src/parsers/parsebip.c + ${PM3_ROOT}/client/src/parsers/parsecharlie.c + ${PM3_ROOT}/client/src/parsers/parsekazan.c + ${PM3_ROOT}/client/src/parsers/parsemetromoney.c + ${PM3_ROOT}/client/src/parsers/parsenextfare.c + ${PM3_ROOT}/client/src/parsers/parseplantain.c + ${PM3_ROOT}/client/src/parsers/parserenfe.c + ${PM3_ROOT}/client/src/parsers/parsesmartrider.c + ${PM3_ROOT}/client/src/parsers/parsetroika.c + ${PM3_ROOT}/client/src/parsers/parseumarsh.c + ${PM3_ROOT}/client/src/parsers/parsezolotayakorona.c ${PM3_ROOT}/client/src/parsers/hrtparser/hrtparser.c ${PM3_ROOT}/client/src/pla.c ${PM3_ROOT}/client/src/preferences.c diff --git a/client/src/parsers/parsebip.c b/client/src/parsers/parsebip.c new file mode 100644 index 000000000..ca6bf7252 --- /dev/null +++ b/client/src/parsers/parsebip.c @@ -0,0 +1,221 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Bip! (Santiago de Chile) parser for MIFARE Classic dumps +//----------------------------------------------------------------------------- + +#include "parsebip.h" + +#include +#include + +#include "commonutil.h" // MemLeToUint4byte +#include "ui.h" // PrintAndLogEx +#include "parsetransit.h" + +#define BIP_DUMP_MIN 1024 +#define BIP_BLK(s, b) ((((s) * 4) + (b)) * 16) + +// Key A and key B of sector 0, key A and key B of sector 1 +static const uint64_t bip_keys[4] = { + 0x3A42F33AF429, 0x1FC235AC1309, 0x6338A371C0ED, 0x243F160918D1 +}; + +static uint64_t bip_key(const uint8_t *dump, uint8_t sector, bool key_b) { + return MemBeToUint6byte(dump + BIP_BLK(sector, 3) + (key_b ? 10 : 0)); +} + +static bool bip_sector_key_ok(const uint8_t *dump, uint8_t sector) { + return bip_key(dump, sector, false) == bip_keys[sector * 2] || + bip_key(dump, sector, true) == bip_keys[(sector * 2) + 1]; +} + +bool is_valid_bip_card(const uint8_t *dump, size_t dumplen) { + if (dump == NULL || dumplen < BIP_DUMP_MIN) { + return false; + } + return bip_sector_key_ok(dump, 0) && bip_sector_key_ok(dump, 1); +} + +// Timestamp packed least significant bit first in the first five bytes +static void bip_timestamp_str(const uint8_t *blk, char *out, size_t outlen) { + unsigned year = 2000 + (unsigned)transit_bits_le(blk, 16, 15, 5); + unsigned month = (unsigned)transit_bits_le(blk, 16, 11, 4); + unsigned day = (unsigned)transit_bits_le(blk, 16, 6, 5); + unsigned hour = (unsigned)transit_bits_le(blk, 16, 20, 5); + unsigned minute = (unsigned)transit_bits_le(blk, 16, 25, 6); + unsigned second = (unsigned)transit_bits_le(blk, 16, 31, 6); + snprintf(out, outlen, "%04u-%02u-%02u %02u:%02u:%02u", year, month, day, hour, minute, second); +} + +static int32_t bip_balance(const uint8_t *dump) { + const uint8_t *b = dump + BIP_BLK(8, 1); + int32_t v = (int32_t)MemLeToUint3byte(b); + return (b[3] & 0x7F) ? -v : v; +} + +static uint32_t bip_holder_id(const uint8_t *dump) { + return MemLeToUint4byte(dump + BIP_BLK(3, 1) + 3); +} + +// An event slot is empty when everything but the type and checksum is zero +static bool bip_slot_empty(const uint8_t *blk) { + return transit_is_zero(blk + 1, 14); +} + +static const char *bip_trip_mode(uint8_t type) { + switch (type) { + case 0x45: + return "metro"; + case 0x46: + return "bus"; + default: + return "other"; + } +} + +int bip_parser_parse(const uint8_t *dump, size_t dumplen) { + if (is_valid_bip_card(dump, dumplen) == false) { + return PM3_EINVARG; + } + + char s[64]; + transit_print_header("Bip! - Santiago de Chile"); + transit_print_kv("Card number", _YELLOW_("%u"), MemLeToUint4byte(dump + BIP_BLK(0, 1) + 4)); + + uint32_t holder = bip_holder_id(dump); + transit_print_kv("Card type", "%s", (holder == 0) ? "anonymous" : "personal"); + if (holder) { + transit_print_kv("Holder id", "%u", holder); + } + + // name kept reversed in sector 3 block 0, bytes 1..14 + const uint8_t *nb = dump + BIP_BLK(3, 0); + if (nb[14] != 0) { + char name[15] = {0}; + size_t n = 0; + for (int i = 14; i >= 1; i--) { + if (nb[i] >= 0x20 && nb[i] < 0x7F) { + name[n++] = (char)nb[i]; + } + } + transit_print_kv("Holder name", "%s", name); + } + + transit_money_str(s, sizeof(s), bip_balance(dump), 0, "CLP"); + transit_print_kv("Balance", _GREEN_("%s"), s); + + const uint8_t *tw = dump + BIP_BLK(5, 1); + if (transit_is_zero(tw, 8) == false) { + bip_timestamp_str(tw, s, sizeof(s)); + transit_print_kv("Trip window end", "%s", s); + } + + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Trips"); + for (uint8_t i = 0; i < 3; i++) { + const uint8_t *b = dump + BIP_BLK(11, i); + if (bip_slot_empty(b)) { + continue; + } + char fare[32]; + bip_timestamp_str(b, s, sizeof(s)); + transit_money_str(fare, sizeof(fare), (int64_t)transit_bits_le(b, 16, 82, 16), 0, "CLP"); + PrintAndLogEx(INFO, " %s %-5s %12s", s, bip_trip_mode(b[8]), fare); + } + + PrintAndLogEx(INFO, "Top ups"); + for (uint8_t i = 0; i < 3; i++) { + const uint8_t *b = dump + BIP_BLK(10, i); + if (bip_slot_empty(b)) { + continue; + } + char fare[32]; + bip_timestamp_str(b, s, sizeof(s)); + transit_money_str(fare, sizeof(fare), (int64_t)transit_bits_le(b, 16, 74, 16), 0, "CLP"); + PrintAndLogEx(INFO, " %s +%12s", s, fare); + } + return PM3_SUCCESS; +} + +//----------------------------------------------------------------------------- +// Self test, synthetic card built from the layout above +//----------------------------------------------------------------------------- + +static void bip_set_timestamp(uint8_t *blk, unsigned y, unsigned mo, unsigned d, unsigned h, unsigned mi, unsigned sec) { + transit_set_bits_le(blk, 16, 6, 5, d); + transit_set_bits_le(blk, 16, 11, 4, mo); + transit_set_bits_le(blk, 16, 15, 5, y - 2000); + transit_set_bits_le(blk, 16, 20, 5, h); + transit_set_bits_le(blk, 16, 25, 6, mi); + transit_set_bits_le(blk, 16, 31, 6, sec); +} + +int bip_selftest(void) { + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Testing Bip! decode"); + + static uint8_t d[BIP_DUMP_MIN]; + memset(d, 0, sizeof(d)); + + for (uint8_t s = 0; s < 2; s++) { + uint8_t *t = d + BIP_BLK(s, 3); + for (int i = 0; i < 6; i++) { + t[i] = (uint8_t)(bip_keys[s * 2] >> (40 - (8 * i))); + t[10 + i] = (uint8_t)(bip_keys[(s * 2) + 1] >> (40 - (8 * i))); + } + } + + // card 12345678, balance -1500 CLP, holder 7654321 + uint8_t *b = d + BIP_BLK(0, 1) + 4; + b[0] = 0x4E; + b[1] = 0x61; + b[2] = 0xBC; + b[3] = 0x00; + b = d + BIP_BLK(8, 1); + b[0] = 0xDC; + b[1] = 0x05; + b[3] = 0x01; + b = d + BIP_BLK(3, 1) + 3; + b[0] = 0xB1; + b[1] = 0xCB; + b[2] = 0x74; + + uint8_t *trip = d + BIP_BLK(11, 0); + bip_set_timestamp(trip, 2023, 11, 28, 18, 45, 59); + trip[8] = 0x45; + transit_set_bits_le(trip, 16, 82, 16, 800); + + bool ok = true; + ok &= transit_check("detected", is_valid_bip_card(d, sizeof(d)), true); + ok &= transit_check("card number", MemLeToUint4byte(d + BIP_BLK(0, 1) + 4), 12345678); + ok &= transit_check("balance, negated", (uint64_t)(-bip_balance(d)), 1500); + ok &= transit_check("holder id", bip_holder_id(d), 7654321); + ok &= transit_check("trip fare", transit_bits_le(trip, 16, 82, 16), 800); + + char s[64]; + bip_timestamp_str(trip, s, sizeof(s)); + ok &= transit_check_str("trip time", s, "2023-11-28 18:45:59"); + ok &= transit_check("parse", bip_parser_parse(d, sizeof(d)), PM3_SUCCESS); + + // one wrong key in sector 1 is still a Bip! card, both wrong is not + d[BIP_BLK(1, 3)] ^= 0xFF; + ok &= transit_check("sector 1 key B alone", is_valid_bip_card(d, sizeof(d)), true); + d[BIP_BLK(1, 3) + 10] ^= 0xFF; + ok &= transit_check("no sector 1 key", is_valid_bip_card(d, sizeof(d)), false); + ok &= transit_check("short dump", is_valid_bip_card(d, 512), false); + + return transit_selftest_result("Bip!", ok); +} diff --git a/client/src/parsers/parsebip.h b/client/src/parsers/parsebip.h new file mode 100644 index 000000000..bb054a394 --- /dev/null +++ b/client/src/parsers/parsebip.h @@ -0,0 +1,36 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Bip! (Santiago de Chile) parser for MIFARE Classic dumps +// +// Layout from Metrodroid ChileBipTransitData and the Flipper Zero bip plugin, +// both GPLv3. +//----------------------------------------------------------------------------- + +#ifndef PARSEBIP_H__ +#define PARSEBIP_H__ + +#include "common.h" + +// True when the sector 0 and 1 keys are the Bip! ones +bool is_valid_bip_card(const uint8_t *dump, size_t dumplen); + +// Card number, holder, balance, last trips and top ups +int bip_parser_parse(const uint8_t *dump, size_t dumplen); + +// Decode a synthetic card built from the layout +int bip_selftest(void); + +#endif diff --git a/client/src/parsers/parsecharlie.c b/client/src/parsers/parsecharlie.c new file mode 100644 index 000000000..d6f7390d3 --- /dev/null +++ b/client/src/parsers/parsecharlie.c @@ -0,0 +1,223 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// CharlieCard (MBTA, Boston) parser for MIFARE Classic dumps +//----------------------------------------------------------------------------- + +#include "parsecharlie.h" + +#include +#include + +#include "commonutil.h" // MemBeToUint4byte +#include "ui.h" // PrintAndLogEx +#include "parsetransit.h" + +#define CHARLIE_DUMP_MIN 1024 +#define CHARLIE_BLK(s, b) ((((s) * 4) + (b)) * 16) +#define CHARLIE_TRIPS 12 + +// Sector 0 keys, either may sit in A or B depending on the dump +static const uint64_t charlie_s0_keys[2] = { 0x3060206F5B0A, 0xF1B9F5669CC8 }; +// Data sector keys, A and B +static const uint64_t charlie_data_keys[2] = { 0x5EC39B022F2B, 0xF662248E7E89 }; + +static uint64_t charlie_key(const uint8_t *dump, uint8_t sector, bool key_b) { + return MemBeToUint6byte(dump + CHARLIE_BLK(sector, 3) + (key_b ? 10 : 0)); +} + +static bool charlie_key_in(uint64_t k, const uint64_t *set) { + return k == set[0] || k == set[1]; +} + +bool is_valid_charlie_card(const uint8_t *dump, size_t dumplen) { + if (dump == NULL || dumplen < CHARLIE_DUMP_MIN) { + return false; + } + bool s0 = charlie_key_in(charlie_key(dump, 0, false), charlie_s0_keys) || + charlie_key_in(charlie_key(dump, 0, true), charlie_s0_keys); + bool s1 = charlie_key_in(charlie_key(dump, 1, false), charlie_data_keys) || + charlie_key_in(charlie_key(dump, 1, true), charlie_data_keys); + return s0 && s1; +} + +// Money in half cents, top bit is the sign +static int32_t charlie_price(const uint8_t *p) { + int32_t v = (int32_t)MemBeToUint2byte(p); + if (v & 0x8000) { + v = -(v & 0x7FFF); + } + return v / 2; +} + +// Minutes since 2003-01-01, Boston local time +static void charlie_time_str(uint32_t minutes, char *out, size_t outlen) { + int64_t secs = ((int64_t)transit_days_from_civil(2003, 1, 1) * 86400) + ((int64_t)minutes * 60); + transit_datetime_str(out, outlen, secs); +} + +// Sectors 2 and 3 alternate, the one with the higher sequence is current +static uint8_t charlie_balance_sector(const uint8_t *dump) { + uint64_t s2 = transit_bits_be(dump + CHARLIE_BLK(2, 0), 16, 81, 16); + uint64_t s3 = transit_bits_be(dump + CHARLIE_BLK(3, 0), 16, 81, 16); + return (s2 > s3) ? 2 : 3; +} + +static const char *charlie_card_type(uint16_t type) { + switch (type) { + case 366: + return "SV Adult"; + case 367: + return "Adult"; + case 410: + return "Employee ID without passback"; + case 414: + return "Employee ID with passback"; + case 415: + return "Retiree"; + case 416: + return "Police/Fire"; + case 417: + return "Blind"; + case 418: + return "Student"; + case 419: + return "Senior"; + case 420: + return "TAP"; + case 426: + return "Child"; + default: + return "unknown"; + } +} + +static const char *charlie_mode(uint16_t validator) { + switch (validator & 7) { + case 0: + return "ticket machine"; + case 1: + return "bus"; + default: + return "other"; + } +} + +int charlie_parser_parse(const uint8_t *dump, size_t dumplen) { + if (is_valid_charlie_card(dump, dumplen) == false) { + return PM3_EINVARG; + } + + char s[64]; + transit_print_header("CharlieCard - MBTA Boston"); + transit_print_kv("Serial", _YELLOW_("5-%u"), MemBeToUint4byte(dump)); + + uint32_t second = MemBeToUint4byte(dump + CHARLIE_BLK(8, 0)); + if (second != 0 && second != 0xFFFFFFFF) { + transit_print_kv("2nd card number", "A%010u", second); + } + + uint8_t bs = charlie_balance_sector(dump); + const uint8_t *b0 = dump + CHARLIE_BLK(bs, 0); + const uint8_t *b1 = dump + CHARLIE_BLK(bs, 1); + + uint16_t type = MemBeToUint2byte(b1) >> 6; + transit_print_kv("Card type", "%u ( %s )", type, charlie_card_type(type)); + + transit_money_str(s, sizeof(s), charlie_price(b1 + 5), 2, "USD"); + transit_print_kv("Balance", _GREEN_("%s") " ( sector %u )", s, bs); + + uint32_t issued = MemBeToUint3byte(b0 + 6); + charlie_time_str(issued, s, sizeof(s)); + transit_print_kv("Issued", "%s", s); + + // end of validity in units of 8 minutes, zero or before issue means unset + uint32_t ev = (MemBeToUint3byte(b1 + 1) & 0x1FFFFF) * 8; + if (ev != 0 && ev >= issued) { + charlie_time_str(ev, s, sizeof(s)); + transit_print_kv("Valid until", "%s", s); + } + + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Trips, sectors 6 and 7, times local"); + for (uint8_t i = 0; i < CHARLIE_TRIPS; i++) { + const uint8_t *t = dump + CHARLIE_BLK(6 + (i / 6), (i / 2) % 3) + (7 * (i % 2)); + if (MemBeToUint4byte(t) == 0) { + continue; + } + char fare[32]; + uint16_t validator = MemBeToUint2byte(t + 3); + charlie_time_str(MemBeToUint3byte(t), s, sizeof(s)); + transit_money_str(fare, sizeof(fare), charlie_price(t + 5), 2, "USD"); + PrintAndLogEx(INFO, " %s %-14s station %5u %12s", s, charlie_mode(validator), validator >> 3, fare); + } + return PM3_SUCCESS; +} + +//----------------------------------------------------------------------------- +// Self test, synthetic card built from the layout above +//----------------------------------------------------------------------------- + +static void charlie_put_be(uint8_t *p, uint32_t v, uint8_t n) { + for (uint8_t i = 0; i < n; i++) { + p[i] = (uint8_t)(v >> (8 * (n - 1 - i))); + } +} + +int charlie_selftest(void) { + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Testing CharlieCard decode"); + + static uint8_t d[CHARLIE_DUMP_MIN]; + memset(d, 0, sizeof(d)); + + charlie_put_be(d, 0x12345678, 4); + charlie_put_be(d + CHARLIE_BLK(0, 3), (uint32_t)(charlie_s0_keys[1] >> 16), 4); + charlie_put_be(d + CHARLIE_BLK(0, 3) + 4, (uint32_t)(charlie_s0_keys[1] & 0xFFFF), 2); + charlie_put_be(d + CHARLIE_BLK(1, 3), (uint32_t)(charlie_data_keys[0] >> 16), 4); + charlie_put_be(d + CHARLIE_BLK(1, 3) + 4, (uint32_t)(charlie_data_keys[0] & 0xFFFF), 2); + + // sector 3 newer than sector 2, adult card, $12.50, issued 2020-01-01 00:00 + transit_set_bits_be(d + CHARLIE_BLK(2, 0), 16, 81, 16, 4); + transit_set_bits_be(d + CHARLIE_BLK(3, 0), 16, 81, 16, 5); + uint8_t *b1 = d + CHARLIE_BLK(3, 1); + charlie_put_be(b1, 367 << 6, 2); + charlie_put_be(b1 + 5, 2500, 2); + uint32_t issued = (uint32_t)(transit_days_from_civil(2020, 1, 1) - transit_days_from_civil(2003, 1, 1)) * 1440; + charlie_put_be(d + CHARLIE_BLK(3, 0) + 6, issued, 3); + + // one bus trip of $1.70 at station 1234, second slot of block 0 + uint8_t *t = d + CHARLIE_BLK(6, 0) + 7; + charlie_put_be(t, issued + 600, 3); + charlie_put_be(t + 3, (1234 << 3) | 1, 2); + charlie_put_be(t + 5, 340, 2); + + bool ok = true; + ok &= transit_check("detected", is_valid_charlie_card(d, sizeof(d)), true); + ok &= transit_check("balance sector", charlie_balance_sector(d), 3); + ok &= transit_check("balance cents", (uint64_t)charlie_price(b1 + 5), 1250); + ok &= transit_check("card type", MemBeToUint2byte(b1) >> 6, 367); + ok &= transit_check("negative price", (uint64_t)(-charlie_price((const uint8_t *)"\x80\x64")), 50); + + char s[32]; + charlie_time_str(MemBeToUint3byte(t), s, sizeof(s)); + ok &= transit_check_str("trip time", s, "2020-01-01 10:00:00"); + ok &= transit_check("parse", charlie_parser_parse(d, sizeof(d)), PM3_SUCCESS); + + d[CHARLIE_BLK(1, 3)] ^= 0xFF; + ok &= transit_check("wrong data key", is_valid_charlie_card(d, sizeof(d)), false); + + return transit_selftest_result("CharlieCard", ok); +} diff --git a/client/src/parsers/parsecharlie.h b/client/src/parsers/parsecharlie.h new file mode 100644 index 000000000..cc86e20a6 --- /dev/null +++ b/client/src/parsers/parsecharlie.h @@ -0,0 +1,36 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// CharlieCard (MBTA, Boston) parser for MIFARE Classic dumps +// +// Layout from Metrodroid CharlieCardTransitData and the Flipper Zero +// charliecard plugin, both GPLv3. +//----------------------------------------------------------------------------- + +#ifndef PARSECHARLIE_H__ +#define PARSECHARLIE_H__ + +#include "common.h" + +// True when the sector 0 and 1 keys are the CharlieCard ones +bool is_valid_charlie_card(const uint8_t *dump, size_t dumplen); + +// Serial, card type, balance, issue / validity dates and trips +int charlie_parser_parse(const uint8_t *dump, size_t dumplen); + +// Decode a synthetic card built from the layout +int charlie_selftest(void); + +#endif diff --git a/client/src/parsers/parsekazan.c b/client/src/parsers/parsekazan.c new file mode 100644 index 000000000..db0333e28 --- /dev/null +++ b/client/src/parsers/parsekazan.c @@ -0,0 +1,158 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Kazan transport card parser for MIFARE Classic dumps +//----------------------------------------------------------------------------- + +#include "parsekazan.h" + +#include +#include + +#include "commonutil.h" // ARRAYLEN, MemLeToUint4byte +#include "ui.h" // PrintAndLogEx +#include "parsetransit.h" +#include "parsetransitmfc.h" + +#define KAZAN_TICKET_SECTOR 8 +#define KAZAN_COUNTER_SECTOR 9 +#define KAZAN_TYPE_PURSE 0x53 + +// Sector 8 keys of the known key sets +static const uint64_t kazan_keys[] = { + 0xE954024EE754, 0x0CD464CDC100, + 0x2058EAEE8446, 0xCB9B23815F87, +}; + +typedef struct { + uint8_t type; + const char *name; + const char *counts; // what the counter holds +} kazan_tariff_t; + +static const kazan_tariff_t kazan_tariffs[] = { + { 0x00, "blank", "counter" }, + { 0x01, "token", "trips left" }, + { 0x0F, "underground only", "trips left" }, + { 0x51, "social, adult", "days" }, + { 0x53, "standard purse", "balance" }, + { 0x60, "unlimited", "counter" }, + { 0x67, "ground electric transport, 1 month", "days" }, + { 0x6D, "tram, 60 minutes, transfer, 10 trips", "trips left" }, +}; + +bool is_valid_kazan_card(const uint8_t *dump, size_t dumplen) { + if (transit_mfc_block(dump, dumplen, KAZAN_COUNTER_SECTOR, 3) == NULL) { + return false; + } + for (size_t i = 0; i < ARRAYLEN(kazan_keys); i++) { + if (transit_mfc_has_key(dump, dumplen, KAZAN_TICKET_SECTOR, kazan_keys[i])) { + return true; + } + } + return false; +} + +// YY MM DD, false when unset or out of range +static bool kazan_date(const uint8_t *p, char *out, size_t outlen) { + if (p[0] == 0 && p[1] == 0 && p[2] == 0) { + return false; + } + if (p[1] < 1 || p[1] > 12 || p[2] < 1 || p[2] > 31) { + return false; + } + snprintf(out, outlen, "%04u-%02u-%02u", 2000 + p[0], p[1], p[2]); + return true; +} + +int kazan_parser_parse(const uint8_t *dump, size_t dumplen) { + if (is_valid_kazan_card(dump, dumplen) == false) { + return PM3_EINVARG; + } + + const uint8_t *b0 = transit_mfc_block(dump, dumplen, KAZAN_TICKET_SECTOR, 0); + const uint8_t *b2 = transit_mfc_block(dump, dumplen, KAZAN_TICKET_SECTOR, 2); + const uint8_t *cb = transit_mfc_block(dump, dumplen, KAZAN_COUNTER_SECTOR, 0); + uint32_t counter = MemLeToUint4byte(cb); + uint8_t type = b0[6]; + + const kazan_tariff_t *tariff = NULL; + for (size_t i = 0; i < ARRAYLEN(kazan_tariffs); i++) { + if (kazan_tariffs[i].type == type) { + tariff = &kazan_tariffs[i]; + } + } + + char buf[40]; + transit_print_header("Kazan transport card"); + transit_print_kv("Card number", _YELLOW_("%010u"), MemLeToUint4byte(dump)); + transit_print_kv("Tariff", "%02X ( %s )", type, (tariff != NULL) ? tariff->name : "unknown"); + + if (type == KAZAN_TYPE_PURSE) { + transit_money_str(buf, sizeof(buf), (int64_t)counter * 100, 2, "RUB"); + transit_print_kv("Balance", _YELLOW_("%s"), buf); + } else { + transit_print_kv((tariff != NULL) ? tariff->counts : "counter", "%u", counter); + } + + if (kazan_date(b0 + 7, buf, sizeof(buf))) { + transit_print_kv("Valid from", "%s", buf); + } + if (kazan_date(b0 + 10, buf, sizeof(buf))) { + transit_print_kv("Valid to", "%s", buf); + } + + const uint8_t *t = b2 + 1; + if (kazan_date(t, buf, sizeof(buf)) && t[3] < 24 && t[4] < 60) { + transit_print_kv("Last trip", "%s %02u:%02u", buf, t[3], t[4]); + } + return PM3_SUCCESS; +} + +// Synthetic card built from the layout +int kazan_selftest(void) { + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Testing Kazan decode"); + + uint8_t dump[1024] = {0}; + const uint8_t uid[] = { 0x78, 0x56, 0x34, 0x12 }; + memcpy(dump, uid, sizeof(uid)); + transit_mfc_set_key(dump, sizeof(dump), KAZAN_TICKET_SECTOR, false, 0x2058EAEE8446); + + uint8_t *s8 = dump + (32 * 16); + const uint8_t ticket[] = { 0, 0, 0, 0, 0, 0, KAZAN_TYPE_PURSE, 23, 1, 15, 25, 12, 31 }; + const uint8_t trip[] = { 0, 24, 3, 7, 8, 45 }; + memcpy(s8, ticket, sizeof(ticket)); + memcpy(s8 + 32, trip, sizeof(trip)); + dump[36 * 16] = 150; + + bool ok = transit_check("detected", is_valid_kazan_card(dump, sizeof(dump)), 1); + ok &= transit_check("card number", MemLeToUint4byte(dump), 0x12345678); + ok &= transit_check("counter", MemLeToUint4byte(dump + (36 * 16)), 150); + + char buf[20]; + ok &= transit_check("valid from set", kazan_date(s8 + 7, buf, sizeof(buf)), 1); + ok &= transit_check_str("valid from", buf, "2023-01-15"); + ok &= transit_check("valid to set", kazan_date(s8 + 10, buf, sizeof(buf)), 1); + ok &= transit_check_str("valid to", buf, "2025-12-31"); + ok &= transit_check("last trip set", kazan_date(s8 + 33, buf, sizeof(buf)), 1); + ok &= transit_check_str("last trip", buf, "2024-03-07"); + ok &= transit_check("bad month rejected", kazan_date((const uint8_t[]) {24, 13, 1}, buf, sizeof(buf)), 0); + + transit_mfc_set_key(dump, sizeof(dump), KAZAN_TICKET_SECTOR, false, 0xFFFFFFFFFFFF); + ok &= transit_check("wrong key rejected", is_valid_kazan_card(dump, sizeof(dump)), 0); + + return transit_selftest_result("Kazan", ok); +} diff --git a/client/src/parsers/parsekazan.h b/client/src/parsers/parsekazan.h new file mode 100644 index 000000000..af0948f7b --- /dev/null +++ b/client/src/parsers/parsekazan.h @@ -0,0 +1,32 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Kazan transport card parser for MIFARE Classic dumps +// +// Layout from Metrodroid (GPLv3) transit/kazan and the Flipper Zero kazan +// plugin (GPLv3), which also supplies the sector 8 keys and tariff names. +//----------------------------------------------------------------------------- + +#ifndef PARSEKAZAN_H__ +#define PARSEKAZAN_H__ + +#include "common.h" + +// Sector 8 under one of the Kazan key sets +bool is_valid_kazan_card(const uint8_t *dump, size_t dumplen); +int kazan_parser_parse(const uint8_t *dump, size_t dumplen); +int kazan_selftest(void); + +#endif diff --git a/client/src/parsers/parsemetromoney.c b/client/src/parsers/parsemetromoney.c new file mode 100644 index 000000000..4dd52cace --- /dev/null +++ b/client/src/parsers/parsemetromoney.c @@ -0,0 +1,84 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Metromoney (Tbilisi) parser for MIFARE Classic dumps +//----------------------------------------------------------------------------- + +#include "parsemetromoney.h" + +#include + +#include "commonutil.h" // MemLeToUint4byte +#include "ui.h" // PrintAndLogEx +#include "parsetransit.h" +#include "parsetransitmfc.h" + +#define METROMONEY_S0_KEY_A 0x2803BCB0C7E1 +#define METROMONEY_S0_KEY_B 0x4FA9EB49F75E +#define METROMONEY_S1_KEY_A 0x9C616585E26D +#define METROMONEY_S1_KEY_B 0xD1C71E590D16 + +// The stored balance carries a +1.00 GEL offset (Flipper), Metrodroid shows it raw +#define METROMONEY_OFFSET 100 + +bool is_valid_metromoney_card(const uint8_t *dump, size_t dumplen) { + if (transit_mfc_block(dump, dumplen, 1, 3) == NULL) { + return false; + } + bool s0 = transit_mfc_has_key(dump, dumplen, 0, METROMONEY_S0_KEY_A) || transit_mfc_has_key(dump, dumplen, 0, METROMONEY_S0_KEY_B); + bool s1 = transit_mfc_has_key(dump, dumplen, 1, METROMONEY_S1_KEY_A) || transit_mfc_has_key(dump, dumplen, 1, METROMONEY_S1_KEY_B); + return s0 && s1; +} + +int metromoney_parser_parse(const uint8_t *dump, size_t dumplen) { + if (is_valid_metromoney_card(dump, dumplen) == false) { + return PM3_EINVARG; + } + + uint32_t stored = MemLeToUint4byte(transit_mfc_block(dump, dumplen, 1, 1)); + + transit_print_header("Metromoney"); + transit_print_kv("Card number", _YELLOW_("%010u"), MemLeToUint4byte(dump)); + if (stored >= METROMONEY_OFFSET) { + char buf[32]; + transit_money_str(buf, sizeof(buf), (int64_t)stored - METROMONEY_OFFSET, 2, "GEL"); + transit_print_kv("Balance", _YELLOW_("%s"), buf); + } + transit_print_kv("Stored value", "%u ( raw )", stored); + return PM3_SUCCESS; +} + +// Synthetic card built from the layout +int metromoney_selftest(void) { + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Testing Metromoney decode"); + + uint8_t dump[1024] = {0}; + const uint8_t uid[] = { 0xEF, 0xBE, 0xAD, 0xDE }; + memcpy(dump, uid, sizeof(uid)); + transit_mfc_set_key(dump, sizeof(dump), 0, false, METROMONEY_S0_KEY_A); + transit_mfc_set_key(dump, sizeof(dump), 1, false, METROMONEY_S1_KEY_A); + const uint8_t value[] = { 0xD2, 0x04, 0x00, 0x00 }; + memcpy(dump + (5 * 16), value, sizeof(value)); + + bool ok = transit_check("detected", is_valid_metromoney_card(dump, sizeof(dump)), 1); + ok &= transit_check("card number", MemLeToUint4byte(dump), 0xDEADBEEF); + ok &= transit_check("stored value", MemLeToUint4byte(dump + (5 * 16)), 1234); + + transit_mfc_set_key(dump, sizeof(dump), 0, false, 0xFFFFFFFFFFFF); + ok &= transit_check("sector 0 key required", is_valid_metromoney_card(dump, sizeof(dump)), 0); + + return transit_selftest_result("Metromoney", ok); +} diff --git a/client/src/parsers/parsemetromoney.h b/client/src/parsers/parsemetromoney.h new file mode 100644 index 000000000..d301e7ce8 --- /dev/null +++ b/client/src/parsers/parsemetromoney.h @@ -0,0 +1,32 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Metromoney (Tbilisi) parser for MIFARE Classic dumps +// +// Layout from the Flipper Zero metromoney plugin (GPLv3) and Metrodroid +// (GPLv3) transit/metromoney. +//----------------------------------------------------------------------------- + +#ifndef PARSEMETROMONEY_H__ +#define PARSEMETROMONEY_H__ + +#include "common.h" + +// Sectors 0 and 1 under the Metromoney keys +bool is_valid_metromoney_card(const uint8_t *dump, size_t dumplen); +int metromoney_parser_parse(const uint8_t *dump, size_t dumplen); +int metromoney_selftest(void); + +#endif diff --git a/client/src/parsers/parsenextfare.c b/client/src/parsers/parsenextfare.c new file mode 100644 index 000000000..025e8b066 --- /dev/null +++ b/client/src/parsers/parsenextfare.c @@ -0,0 +1,370 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Cubic Nextfare parser for MIFARE Classic dumps +//----------------------------------------------------------------------------- + +#include "parsenextfare.h" + +#include +#include + +#include "commonutil.h" // MemLeToUint2byte +#include "ui.h" // PrintAndLogEx +#include "util.h" // sprint_hex_inrow +#include "parsetransit.h" + +#define NF_DUMP_MIN 1024 +#define NF_BLK(s, b) ((((s) * 4) + (b)) * 16) +#define NF_MAX_TAPS 12 + +static const uint8_t nf_manufacturer[8] = { 0x16, 0x18, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F }; +static const uint8_t nf_seqgo_code1[6] = { 0x5A, 0x5B, 0x20, 0x21, 0x22, 0x23 }; +static const uint8_t nf_seqgo_code2[6] = { 0x20, 0x21, 0x22, 0x23, 0x01, 0x01 }; +static const uint8_t nf_us_code[6] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }; +static const uint8_t nf_msp_block2[16] = { + 0x3F, 0x33, 0x22, 0x11, 0xC0, 0xCC, 0xDD, 0xEE, 0x3F, 0x33, 0x22, 0x11, 0x01, 0xFE, 0x01, 0xFE +}; + +typedef enum { + NF_UNKNOWN = 0, + NF_SEQGO, + NF_LAXTAP, + NF_MSPGOTO, +} nf_system_t; + +bool is_valid_nextfare_card(const uint8_t *dump, size_t dumplen) { + if (dump == NULL || dumplen < NF_DUMP_MIN) { + return false; + } + return memcmp(dump + NF_BLK(0, 1) + 1, nf_manufacturer, sizeof(nf_manufacturer)) == 0; +} + +static nf_system_t nf_system(const uint8_t *dump) { + const uint8_t *code = dump + NF_BLK(0, 1) + 9; + const uint8_t *b2 = dump + NF_BLK(0, 2); + if (memcmp(code, nf_seqgo_code1, 6) == 0 || memcmp(code, nf_seqgo_code2, 6) == 0) { + return NF_SEQGO; + } + if (memcmp(code, nf_us_code, 6) == 0) { + if (memcmp(b2, nf_msp_block2, sizeof(nf_msp_block2)) == 0) { + return NF_MSPGOTO; + } + if (transit_is_zero(b2, 4)) { + return NF_LAXTAP; + } + } + return NF_UNKNOWN; +} + +// "0160 nnnn nnnn nnnL", eleven digit serial plus a Luhn digit +static void nf_serial_str(uint32_t serial, char *out, size_t outlen) { + char digits[20]; + snprintf(digits, sizeof(digits), "0160%011u", serial); + int sum = 0; + size_t n = strlen(digits); + for (size_t i = 0; i < n; i++) { + int v = digits[n - 1 - i] - '0'; + if ((i & 1) == 0) { + v *= 2; + if (v > 9) { + v -= 9; + } + } + sum += v; + } + int check = (10 - (sum % 10)) % 10; + snprintf(out, outlen, "%.4s %.4s %.4s %.3s%d", digits, digits + 4, digits + 8, digits + 12, check); +} + +// Packed date: day 5 bits, month 4, year 7 from 2000, minute of day 11 +static bool nf_date_str(const uint8_t *p, char *out, size_t outlen) { + uint32_t ts = MemLeToUint4byte(p); + unsigned day = ts & 0x1F; + unsigned month = (ts >> 5) & 0x0F; + unsigned year = ((ts >> 9) & 0x7F) + 2000; + unsigned minute = (ts >> 16) & 0x7FF; + if (minute > 1440 || month > 12) { + snprintf(out, outlen, "invalid ( %08X )", ts); + return false; + } + snprintf(out, outlen, "%04u-%02u-%02u %02u:%02u", year, month, day, minute / 60, minute % 60); + return true; +} + +typedef struct { + uint8_t version; + int32_t balance; + bool travel_pass; +} nf_balance_t; + +static void nf_read_balance(const uint8_t *b, nf_balance_t *r) { + r->version = b[13]; + int32_t v = MemLeToUint2byte(b + 2); + if (v & 0x8000) { + v = -(v & 0x7FFF); + } else if (b[1] & 0x80) { + v = -v; + } + r->balance = v; + r->travel_pass = (b[7] != 0); +} + +typedef struct { + uint8_t type; + uint8_t mode; + uint32_t ts; + uint16_t journey; + bool continuation; + int32_t value; + uint16_t station; + const uint8_t *raw; +} nf_tap_t; + +// False for an empty slot or one of the record types Metrodroid ignores +static bool nf_read_tap(const uint8_t *b, nf_tap_t *t) { + if (b[0] == 0x71 || b[0] == 0x79 || b[0] == 0x89) { + return false; + } + if (transit_is_zero(b + 1, 8)) { + return false; + } + t->type = b[0]; + t->mode = b[1]; + t->ts = MemLeToUint4byte(b + 2); + uint16_t j = MemLeToUint2byte(b + 5); + t->journey = j >> 5; + t->continuation = (j & 0x10) != 0; + int32_t v = MemLeToUint2byte(b + 7); + if (v > 0x8000) { + v = -(v & 0x7FFF); + } + t->value = v; + t->station = MemLeToUint2byte(b + 12); + t->raw = b; + return true; +} + +static const char *nf_tap_type(uint8_t type) { + switch (type) { + case 0x05: + return "travel pass trip"; + case 0x31: + return "stored value trip"; + case 0x41: + return "travel pass sale"; + default: + return "unknown"; + } +} + +static const char *nf_seqgo_ticket(uint16_t t) { + switch (t) { + case 0x000F: + return "Adult Explore"; + case 0x0801: + case 0x0C01: + return "Adult"; + case 0x0803: + return "Child"; + case 0x08A5: + return "Concession"; + default: + return "unknown"; + } +} + +int nextfare_parser_parse(const uint8_t *dump, size_t dumplen) { + if (is_valid_nextfare_card(dump, dumplen) == false) { + return PM3_EINVARG; + } + + nf_system_t sys = nf_system(dump); + const char *title = "Nextfare - unknown system"; + const char *cur = ""; + switch (sys) { + case NF_SEQGO: + title = "Go card - Translink Brisbane"; + cur = "AUD"; + break; + case NF_LAXTAP: + title = "TAP - Los Angeles"; + cur = "USD"; + break; + case NF_MSPGOTO: + title = "Go-To card - Minneapolis"; + cur = "USD"; + break; + case NF_UNKNOWN: + break; + } + + char s[64]; + transit_print_header(title); + nf_serial_str(MemLeToUint4byte(dump), s, sizeof(s)); + transit_print_kv("Card number", _YELLOW_("%s"), s); + transit_print_kv("System code", "%s", sprint_hex_inrow(dump + NF_BLK(0, 1) + 9, 6)); + + // two balance records, the higher version is current except across the wrap + nf_balance_t b0, b1; + nf_read_balance(dump + NF_BLK(1, 0), &b0); + nf_read_balance(dump + NF_BLK(1, 1), &b1); + const nf_balance_t *hi = (b1.version > b0.version) ? &b1 : &b0; + const nf_balance_t *lo = (hi == &b0) ? &b1 : &b0; + const nf_balance_t *bal = (hi->version >= 240 && lo->version <= 10) ? lo : hi; + transit_money_str(s, sizeof(s), bal->balance, 2, cur); + transit_print_kv("Balance", _GREEN_("%s"), s); + if (bal->travel_pass) { + transit_print_kv("Travel pass", "available"); + } + + const uint8_t *cfg = dump + NF_BLK(1, 2); + uint16_t ticket = MemLeToUint2byte(cfg + 8); + if (sys == NF_SEQGO) { + transit_print_kv("Ticket type", "0x%04X ( %s )", ticket, nf_seqgo_ticket(ticket)); + } else { + transit_print_kv("Ticket type", "0x%04X", ticket); + } + nf_date_str(cfg + 4, s, sizeof(s)); + transit_print_kv("Expiry", "%s", s); + + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Top ups, times local"); + for (uint8_t i = 0; i < 3; i++) { + const uint8_t *b = dump + NF_BLK(2, i); + if (transit_is_zero(b + 2, 6)) { + continue; + } + char amt[32]; + nf_date_str(b + 2, s, sizeof(s)); + transit_money_str(amt, sizeof(amt), MemLeToUint2byte(b + 6) & 0x7FFF, 2, cur); + PrintAndLogEx(INFO, " %s +%-12s station %5u%s", s, amt, MemLeToUint2byte(b + 12), + (b[0] == 0x31) ? " automatic" : ""); + } + + for (uint8_t i = 0; i < 3; i++) { + const uint8_t *b = dump + NF_BLK(3, i); + if (MemBeToUint4byte(b + 2) == 0 || b[13] == 0) { + continue; + } + nf_date_str(b + 2, s, sizeof(s)); + PrintAndLogEx(INFO, "Travel pass, version %u, %s", b[13], s); + } + + // taps, sectors 5..8, sorted by journey then time, newest first + nf_tap_t taps[NF_MAX_TAPS]; + uint8_t n = 0; + for (uint8_t sec = 5; sec <= 8; sec++) { + for (uint8_t i = 0; i < 3 && n < NF_MAX_TAPS; i++) { + if (nf_read_tap(dump + NF_BLK(sec, i), &taps[n])) { + n++; + } + } + } + for (uint8_t i = 1; i < n; i++) { + for (uint8_t j = i; j > 0; j--) { + const nf_tap_t *a = &taps[j - 1], *c = &taps[j]; + bool newer = (c->journey != a->journey) ? (c->journey > a->journey) : (c->ts > a->ts); + if (newer == false) { + break; + } + nf_tap_t tmp = taps[j]; + taps[j] = taps[j - 1]; + taps[j - 1] = tmp; + } + } + + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Taps, times local"); + for (uint8_t i = 0; i < n; i++) { + char amt[32]; + nf_date_str(taps[i].raw + 2, s, sizeof(s)); + transit_money_str(amt, sizeof(amt), -taps[i].value, 2, cur); + PrintAndLogEx(INFO, " %s journey %4u%s mode %3u station %5u %12s %s", s, + taps[i].journey, taps[i].continuation ? "+" : " ", taps[i].mode, + taps[i].station, amt, nf_tap_type(taps[i].type)); + } + return PM3_SUCCESS; +} + +//----------------------------------------------------------------------------- +// Self test, Metrodroid NextfareTest vectors +//----------------------------------------------------------------------------- + +int nextfare_selftest(void) { + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Testing Nextfare decode"); + + bool ok = true; + char s[64]; + + static const uint8_t cfg[3][16] = { + {0x01, 0x03, 0x00, 0x00, 0xC2, 0x32, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0xBF, 0x0C}, + {0x01, 0x03, 0x00, 0x00, 0x39, 0x31, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x92, 0x4A}, + {0x01, 0x03, 0x00, 0x01, 0x0F, 0x25, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xFB, 0x75, 0xC2, 0xF7}, + }; + static const char *expiry[3] = { "2025-06-02 00:00", "2024-09-25 00:00", "2018-08-15 00:00" }; + for (int i = 0; i < 3; i++) { + nf_date_str(cfg[i] + 4, s, sizeof(s)); + ok &= transit_check_str("config expiry", s, expiry[i]); + } + + static const uint8_t bal[3][16] = { + {0x01, 0x28, 0xD2, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0xFF, 0xFF}, + {0x01, 0xA8, 0xE8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0xFF, 0xFF}, + {0x01, 0xA0, 0xE8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0xFF, 0xFF}, + }; + nf_balance_t r; + nf_read_balance(bal[0], &r); + ok &= transit_check("balance 12.34", (uint64_t)r.balance, 1234); + ok &= transit_check("balance version", r.version, 0x12); + nf_read_balance(bal[1], &r); + ok &= transit_check("balance -10.00 negated", (uint64_t)(-r.balance), 1000); + nf_read_balance(bal[2], &r); + ok &= transit_check("balance -10.00 negated", (uint64_t)(-r.balance), 1000); + + static const uint8_t null_tap[16] = {0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x7F, 0x28}; + nf_tap_t t; + ok &= transit_check("null transaction", nf_read_tap(null_tap, &t), false); + + nf_serial_str(123456789, s, sizeof(s)); + ok &= transit_check_str("Go card serial", s, "0160 0012 3456 7893"); + nf_serial_str(987654321, s, sizeof(s)); + ok &= transit_check_str("Go card serial", s, "0160 0098 7654 3213"); + nf_serial_str(0xC0CD0DC4, s, sizeof(s)); + ok &= transit_check_str("TAP serial", s, "0160 0323 4663 8769"); + + // card as NextfareTest.buildNextfareCard makes it + static uint8_t d[NF_DUMP_MIN]; + memset(d, 0, sizeof(d)); + memcpy(d, "\x15\xcd\x5b\x07", 4); + memcpy(d + NF_BLK(0, 1) + 1, nf_manufacturer, sizeof(nf_manufacturer)); + memcpy(d + NF_BLK(0, 1) + 9, nf_seqgo_code1, 6); + memcpy(d + NF_BLK(1, 0), bal[0], 16); + memcpy(d + NF_BLK(1, 2), cfg[0], 16); + ok &= transit_check("detected", is_valid_nextfare_card(d, sizeof(d)), true); + ok &= transit_check("Go card system", nf_system(d), NF_SEQGO); + ok &= transit_check("parse", nextfare_parser_parse(d, sizeof(d)), PM3_SUCCESS); + + memcpy(d + NF_BLK(0, 1) + 9, nf_us_code, 6); + memcpy(d + NF_BLK(0, 2), nf_msp_block2, sizeof(nf_msp_block2)); + ok &= transit_check("Go-To system", nf_system(d), NF_MSPGOTO); + memset(d + NF_BLK(0, 2), 0, 16); + ok &= transit_check("TAP system", nf_system(d), NF_LAXTAP); + memset(d + NF_BLK(0, 2), 0xFF, 16); + ok &= transit_check("unknown system", nf_system(d), NF_UNKNOWN); + + return transit_selftest_result("Nextfare", ok); +} diff --git a/client/src/parsers/parsenextfare.h b/client/src/parsers/parsenextfare.h new file mode 100644 index 000000000..db89d2670 --- /dev/null +++ b/client/src/parsers/parsenextfare.h @@ -0,0 +1,37 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Cubic Nextfare parser for MIFARE Classic dumps: Go card (Brisbane), +// TAP (Los Angeles), Go-To (Minneapolis) and other Nextfare systems +// +// Layout from Metrodroid NextfareTransitData, SeqGoTransitData, +// LaxTapTransitData and MspGotoTransitData, GPLv3. +//----------------------------------------------------------------------------- + +#ifndef PARSENEXTFARE_H__ +#define PARSENEXTFARE_H__ + +#include "common.h" + +// True when sector 0 block 1 carries the Nextfare manufacturer signature +bool is_valid_nextfare_card(const uint8_t *dump, size_t dumplen); + +// Card number, balance, ticket type, expiry, top ups and taps +int nextfare_parser_parse(const uint8_t *dump, size_t dumplen); + +// Metrodroid NextfareTest vectors +int nextfare_selftest(void); + +#endif diff --git a/client/src/parsers/parseplantain.c b/client/src/parsers/parseplantain.c new file mode 100644 index 000000000..42f3b4dad --- /dev/null +++ b/client/src/parsers/parseplantain.c @@ -0,0 +1,252 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Podorozhnik / Plantain (Saint Petersburg) parser for MIFARE Classic dumps +//----------------------------------------------------------------------------- + +#include "parseplantain.h" + +#include +#include + +#include "commonutil.h" // MemLeToUint4byte +#include "ui.h" // PrintAndLogEx +#include "parsetransit.h" +#include "parsetransitmfc.h" + +#define PLANTAIN_S4_KEY_A 0xE56AC127DD45 +#define PLANTAIN_S4_KEY_B 0x19FC84A3784B +#define PLANTAIN_S5_KEY_A 0x77DABC9825E1 +#define PLANTAIN_S5_KEY_B 0x9764FEC3154A + +// 2010-01-01, minutes count in Moscow local time +#define PLANTAIN_DAYS_TO_2010 14610 + +#define PLANTAIN_TRANSPORT_METRO 1 +#define PLANTAIN_TRANSPORT_BUS_MOBILE 3 +#define PLANTAIN_TRANSPORT_BUS 4 +#define PLANTAIN_TRANSPORT_SHARED_TAXI 7 + +typedef struct { + int32_t balance; // kopecks + uint32_t topup_time; // minutes since 2010 + uint8_t topup_agency; + uint16_t topup_machine; + uint32_t topup_amount; + + uint32_t trip_time; + uint8_t trip_transport; + uint16_t trip_validator; + uint32_t trip_fare; + uint8_t subway_trips; + uint8_t ground_trips; + uint8_t extra_count; + uint32_t extra_times[2]; +} plantain_t; + +static uint32_t plantain_le(const uint8_t *p, uint8_t n) { + uint32_t v = 0; + for (uint8_t i = n; i > 0; i--) { + v = (v << 8) | p[i - 1]; + } + return v; +} + +static void plantain_time_str(char *out, size_t outlen, uint32_t mins) { + transit_datetime_str(out, outlen, ((int64_t)PLANTAIN_DAYS_TO_2010 * 86400) + ((int64_t)mins * 60)); +} + +bool is_valid_plantain_card(const uint8_t *dump, size_t dumplen) { + if (transit_mfc_block(dump, dumplen, 5, 3) == NULL) { + return false; + } + bool s4 = transit_mfc_has_key(dump, dumplen, 4, PLANTAIN_S4_KEY_A) || transit_mfc_has_key(dump, dumplen, 4, PLANTAIN_S4_KEY_B); + bool s5 = transit_mfc_has_key(dump, dumplen, 5, PLANTAIN_S5_KEY_A) || transit_mfc_has_key(dump, dumplen, 5, PLANTAIN_S5_KEY_B); + return s4 && s5; +} + +static void plantain_decode(const uint8_t *dump, size_t dumplen, plantain_t *p) { + memset(p, 0, sizeof(*p)); + + // sector 4 blocks 0 and 1 are copies + const uint8_t *b = transit_mfc_block(dump, dumplen, 4, 0); + p->balance = (int32_t)MemLeToUint4byte(b); + b = transit_mfc_block(dump, dumplen, 4, 2); + p->topup_time = plantain_le(b + 2, 3); + p->topup_agency = b[5]; + p->topup_machine = (uint16_t)plantain_le(b + 6, 2); + p->topup_amount = plantain_le(b + 8, 3); + + const uint8_t *b0 = transit_mfc_block(dump, dumplen, 5, 0); + const uint8_t *b1 = transit_mfc_block(dump, dumplen, 5, 1); + const uint8_t *b2 = transit_mfc_block(dump, dumplen, 5, 2); + p->trip_time = plantain_le(b0, 3); + p->trip_transport = b0[3]; + p->trip_validator = (uint16_t)plantain_le(b0 + 4, 2); + p->trip_fare = plantain_le(b0 + 6, 4); + + // blocks 1 and 2 are usually equal, take the counters of the newer one + uint32_t t1 = plantain_le(b1 + 2, 3); + uint32_t t2 = plantain_le(b2 + 2, 3); + const uint8_t *cnt = (t2 > t1) ? b2 : b1; + p->subway_trips = cnt[0]; + p->ground_trips = cnt[1]; + + if (t1 != p->trip_time) { + p->extra_times[p->extra_count++] = t1; + } + if (t2 != p->trip_time && t2 != t1) { + p->extra_times[p->extra_count++] = t2; + } +} + +// 9643 3078 + block 0 bytes 0..6 little endian, 17 digits, then a Luhn digit +static void plantain_serial_str(char *out, size_t outlen, const uint8_t *blk0) { + uint64_t v = 0; + for (uint8_t i = 7; i > 0; i--) { + v = (v << 8) | blk0[i - 1]; + } + char d[32]; + snprintf(d, sizeof(d), "96433078%017" PRIu64, v); + + // Luhn over the 25 digits, doubling from the rightmost + int sum = 0; + size_t n = strlen(d); + for (size_t i = 0; i < n; i++) { + int x = d[n - 1 - i] - '0'; + if ((i & 1) == 0) { + x *= 2; + if (x > 9) { + x -= 9; + } + } + sum += x; + } + int luhn = (10 - (sum % 10)) % 10; + snprintf(out, outlen, "%.4s %.4s %.4s %.4s %.4s %.4s %.1s%d", d, d + 4, d + 8, d + 12, d + 16, d + 20, d + 24, luhn); +} + +static const char *plantain_transport_str(uint8_t transport, uint16_t validator) { + switch (transport) { + case PLANTAIN_TRANSPORT_METRO: + // misconfigured validators show up as metro, station 0 + return (validator == 0) ? "bus" : "metro"; + case PLANTAIN_TRANSPORT_BUS: + case PLANTAIN_TRANSPORT_BUS_MOBILE: + return "bus"; + case PLANTAIN_TRANSPORT_SHARED_TAXI: + return "shared taxi"; + default: + return "unknown"; + } +} + +int plantain_parser_parse(const uint8_t *dump, size_t dumplen) { + if (is_valid_plantain_card(dump, dumplen) == false) { + return PM3_EINVARG; + } + + plantain_t p; + plantain_decode(dump, dumplen, &p); + + char buf[64]; + transit_print_header("Podorozhnik"); + plantain_serial_str(buf, sizeof(buf), transit_mfc_block(dump, dumplen, 0, 0)); + transit_print_kv("Card number", _YELLOW_("%s"), buf); + transit_money_str(buf, sizeof(buf), p.balance, 2, "RUB"); + transit_print_kv("Balance", _YELLOW_("%s"), buf); + transit_print_kv("Metro trips", "%u", p.subway_trips); + transit_print_kv("Ground trips", "%u", p.ground_trips); + + if (p.trip_time != 0) { + char fare[24]; + plantain_time_str(buf, sizeof(buf), p.trip_time); + transit_money_str(fare, sizeof(fare), p.trip_fare, 2, "RUB"); + if (p.trip_transport == PLANTAIN_TRANSPORT_METRO && p.trip_validator != 0) { + transit_print_kv("Last trip", "%s metro, station %u gate %u, fare %s", buf, + p.trip_validator >> 6, p.trip_validator & 0x3F, fare); + } else { + transit_print_kv("Last trip", "%s %s, validator %u, fare %s", buf, + plantain_transport_str(p.trip_transport, p.trip_validator), p.trip_validator, fare); + } + } + for (uint8_t i = 0; i < p.extra_count; i++) { + plantain_time_str(buf, sizeof(buf), p.extra_times[i]); + transit_print_kv("Trip", "%s", buf); + } + if (p.topup_time != 0) { + char amount[24]; + plantain_time_str(buf, sizeof(buf), p.topup_time); + transit_money_str(amount, sizeof(amount), p.topup_amount, 2, "RUB"); + transit_print_kv("Last top-up", "%s %s, agency %u, machine %u", buf, amount, p.topup_agency, p.topup_machine); + } + PrintAndLogEx(INFO, "Times are Moscow local time"); + return PM3_SUCCESS; +} + +// Synthetic card built from the Metrodroid layout +int plantain_selftest(void) { + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Testing Podorozhnik decode"); + + uint8_t dump[1024] = {0}; + const uint8_t blk0[16] = { 0x04, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x88, 0x44, 0x00, 0xC8, 0x20, 0x00, 0x00, 0x00, 0x16 }; + memcpy(dump, blk0, sizeof(blk0)); + transit_mfc_set_key(dump, sizeof(dump), 4, false, PLANTAIN_S4_KEY_A); + transit_mfc_set_key(dump, sizeof(dump), 5, true, PLANTAIN_S5_KEY_B); + + uint8_t *s4 = dump + (16 * 16); + uint8_t *s5 = dump + (20 * 16); + // balance 123.45, top-up of 100.00 at minute 0x0ABCDE by agency 1 machine 0x1234 + const uint8_t s4b0[] = { 0x39, 0x30, 0x00, 0x00 }; + const uint8_t s4b2[] = { 0x00, 0x00, 0xDE, 0xBC, 0x0A, 0x01, 0x34, 0x12, 0x10, 0x27, 0x00 }; + memcpy(s4, s4b0, sizeof(s4b0)); + memcpy(s4 + 32, s4b2, sizeof(s4b2)); + // trip at minute 0x0ABCE0 by metro, validator 0x0105, fare 50.00 + const uint8_t s5b0[] = { 0xE0, 0xBC, 0x0A, 0x01, 0x05, 0x01, 0x88, 0x13, 0x00, 0x00 }; + const uint8_t s5b1[] = { 0x03, 0x02, 0xE0, 0xBC, 0x0A }; + const uint8_t s5b2[] = { 0x02, 0x02, 0xD0, 0xBC, 0x0A }; + memcpy(s5, s5b0, sizeof(s5b0)); + memcpy(s5 + 16, s5b1, sizeof(s5b1)); + memcpy(s5 + 32, s5b2, sizeof(s5b2)); + + bool ok = transit_check("detected", is_valid_plantain_card(dump, sizeof(dump)), 1); + + plantain_t p; + plantain_decode(dump, sizeof(dump), &p); + ok &= transit_check("balance", (uint64_t)p.balance, 12345); + ok &= transit_check("top-up time", p.topup_time, 0x0ABCDE); + ok &= transit_check("top-up agency", p.topup_agency, 1); + ok &= transit_check("top-up machine", p.topup_machine, 0x1234); + ok &= transit_check("top-up amount", p.topup_amount, 10000); + ok &= transit_check("trip time", p.trip_time, 0x0ABCE0); + ok &= transit_check("trip validator", p.trip_validator, 0x0105); + ok &= transit_check("trip fare", p.trip_fare, 5000); + ok &= transit_check("metro trips", p.subway_trips, 3); + ok &= transit_check("ground trips", p.ground_trips, 2); + ok &= transit_check("detached trips", p.extra_count, 1); + + char buf[64]; + plantain_time_str(buf, sizeof(buf), 0); + ok &= transit_check_str("epoch", buf, "2010-01-01 00:00:00"); + plantain_serial_str(buf, sizeof(buf), blk0); + // 0x66554433221104 = 28804199028494596, Luhn digit 0 + ok &= transit_check_str("card number", buf, "9643 3078 2880 4199 0284 9459 60"); + + transit_mfc_set_key(dump, sizeof(dump), 5, true, 0xFFFFFFFFFFFF); + ok &= transit_check("wrong sector 5 key rejected", is_valid_plantain_card(dump, sizeof(dump)), 0); + + return transit_selftest_result("Podorozhnik", ok); +} diff --git a/client/src/parsers/parseplantain.h b/client/src/parsers/parseplantain.h new file mode 100644 index 000000000..821402cef --- /dev/null +++ b/client/src/parsers/parseplantain.h @@ -0,0 +1,33 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Podorozhnik / Plantain (Saint Petersburg) parser for MIFARE Classic dumps +// +// Layout ported from Metrodroid (GPLv3) transit/podorozhnik, sector keys from +// the Flipper Zero plantain plugin (GPLv3). Also covers the purse half of the +// Two Cities (Troika + Podorozhnik) card, the Troika parser takes the rest. +//----------------------------------------------------------------------------- + +#ifndef PARSEPLANTAIN_H__ +#define PARSEPLANTAIN_H__ + +#include "common.h" + +// Sectors 4 and 5 under the Podorozhnik keys +bool is_valid_plantain_card(const uint8_t *dump, size_t dumplen); +int plantain_parser_parse(const uint8_t *dump, size_t dumplen); +int plantain_selftest(void); + +#endif diff --git a/client/src/parsers/parserenfe.c b/client/src/parsers/parserenfe.c new file mode 100644 index 000000000..9271f9749 --- /dev/null +++ b/client/src/parsers/parserenfe.c @@ -0,0 +1,177 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// RENFE Cercanias Suma 10 parser for MIFARE Classic dumps +//----------------------------------------------------------------------------- + +#include "parserenfe.h" + +#include +#include + +#include "commonutil.h" // MemBeToUint2byte +#include "ui.h" // PrintAndLogEx +#include "util.h" // sprint_hex_inrow +#include "parsetransit.h" + +#define RENFE_DUMP_MIN 1024 +#define RENFE_BLK(b) ((b) * 16) + +static const uint64_t renfe_keys[2] = { 0xA8844B0BCA06, 0xCB5ED0E57B08 }; + +// Blocks Metroflip found the event log in +static const uint8_t renfe_history_blocks[] = { 18, 22, 28, 29, 30, 44, 45, 46 }; + +bool is_valid_renfe_card(const uint8_t *dump, size_t dumplen) { + if (dump == NULL || dumplen < RENFE_DUMP_MIN) { + return false; + } + return MemBeToUint6byte(dump + RENFE_BLK(3)) == renfe_keys[0] && + MemBeToUint6byte(dump + RENFE_BLK(7)) == renfe_keys[1]; +} + +static const char *renfe_event_name(uint8_t type) { + switch (type) { + case 0x13: + return "entry"; + case 0x16: + return "validation"; + case 0x17: + return "inspection"; + case 0x18: + return "check"; + case 0x1A: + return "exit"; + case 0x1E: + return "transfer"; + case 0x23: + return "discount"; + case 0x2A: + return "penalty"; + case 0x2B: + return "special"; + case 0x33: + return "top up"; + case 0x3A: + return "charge"; + default: + return "unknown"; + } +} + +// Metroflip's test for a used event slot +static bool renfe_is_event(const uint8_t *b) { + uint16_t station = MemBeToUint2byte(b + 9); + if (station == 0x0000 || station == 0x2000 || station == 0xFFFF) { + return false; + } + uint32_t ts = MemBeToUint3byte(b + 2); + if (ts == 0 || ts == 0xFFFFFF) { + return false; + } + if (b[1] == 0x98 && b[0] != 0x00) { + return true; + } + switch (b[0]) { + case 0x13: + case 0x16: + case 0x1A: + case 0x1E: + case 0x33: + case 0x3A: + return b[1] != 0xFF; + default: + return false; + } +} + +static uint8_t renfe_collect(const uint8_t *dump, const uint8_t **ev, uint8_t max) { + uint8_t n = 0; + for (size_t i = 0; i < ARRAYLEN(renfe_history_blocks) && n < max; i++) { + const uint8_t *b = dump + RENFE_BLK(renfe_history_blocks[i]); + if (renfe_is_event(b)) { + ev[n++] = b; + } + } + // newest first by the raw time field + for (uint8_t i = 1; i < n; i++) { + for (uint8_t j = i; j > 0 && MemBeToUint3byte(ev[j] + 2) > MemBeToUint3byte(ev[j - 1] + 2); j--) { + const uint8_t *tmp = ev[j]; + ev[j] = ev[j - 1]; + ev[j - 1] = tmp; + } + } + return n; +} + +int renfe_parser_parse(const uint8_t *dump, size_t dumplen) { + if (is_valid_renfe_card(dump, dumplen) == false) { + return PM3_EINVARG; + } + + transit_print_header("RENFE Cercanias - Suma 10"); + transit_print_kv("UID", _YELLOW_("%s"), sprint_hex_inrow(dump, 4)); + + const uint8_t *b5 = dump + RENFE_BLK(5); + if (b5[0] == 0x01 && b5[1] == 0x00 && b5[2] == 0x00 && b5[3] == 0x00) { + transit_print_kv("Trips left", _GREEN_("%u"), b5[4]); + } + transit_print_kv("Zone code", "%04X", MemBeToUint2byte(b5 + 5)); + + const uint8_t *ev[ARRAYLEN(renfe_history_blocks)]; + uint8_t n = renfe_collect(dump, ev, ARRAYLEN(renfe_history_blocks)); + + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "History, newest first ( time field undecoded )"); + for (uint8_t i = 0; i < n; i++) { + PrintAndLogEx(INFO, " %-10s station %04X time %06X", renfe_event_name(ev[i][0]), + MemBeToUint2byte(ev[i] + 9), MemBeToUint3byte(ev[i] + 2)); + } + return PM3_SUCCESS; +} + +//----------------------------------------------------------------------------- +// Self test, synthetic card built from the layout above +//----------------------------------------------------------------------------- + +int renfe_selftest(void) { + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Testing RENFE Suma 10 decode"); + + static uint8_t d[RENFE_DUMP_MIN]; + memset(d, 0, sizeof(d)); + memcpy(d + RENFE_BLK(3), "\xA8\x84\x4B\x0B\xCA\x06", 6); + memcpy(d + RENFE_BLK(7), "\xCB\x5E\xD0\xE5\x7B\x08", 6); + memcpy(d + RENFE_BLK(5), "\x01\x00\x00\x00\x07\x6C\x00", 7); + + // an exit newer than an entry, a top up at the default station is dropped + memcpy(d + RENFE_BLK(18), "\x13\x00\x01\x02\x03\x00\x00\x00\x00\x12\x34", 11); + memcpy(d + RENFE_BLK(22), "\x1A\x00\x01\x02\x09\x00\x00\x00\x00\x12\x35", 11); + memcpy(d + RENFE_BLK(28), "\x33\x00\x01\x02\x0A\x00\x00\x00\x00\x20\x00", 11); + + const uint8_t *ev[ARRAYLEN(renfe_history_blocks)]; + uint8_t n = renfe_collect(d, ev, ARRAYLEN(renfe_history_blocks)); + + bool ok = true; + ok &= transit_check("detected", is_valid_renfe_card(d, sizeof(d)), true); + ok &= transit_check("events", n, 2); + ok &= transit_check("newest is exit", (n > 0) ? ev[0][0] : 0, 0x1A); + ok &= transit_check("parse", renfe_parser_parse(d, sizeof(d)), PM3_SUCCESS); + + d[RENFE_BLK(7)] ^= 0xFF; + ok &= transit_check("wrong sector 1 key", is_valid_renfe_card(d, sizeof(d)), false); + + return transit_selftest_result("RENFE Suma 10", ok); +} diff --git a/client/src/parsers/parserenfe.h b/client/src/parsers/parserenfe.h new file mode 100644 index 000000000..12b9732f5 --- /dev/null +++ b/client/src/parsers/parserenfe.h @@ -0,0 +1,36 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// RENFE Cercanias Suma 10 parser for MIFARE Classic dumps +// +// Layout from the Metroflip renfe_sum10 plugin, GPLv3. Only the parts it +// states plainly are decoded, the event timestamp format is not known. +//----------------------------------------------------------------------------- + +#ifndef PARSERENFE_H__ +#define PARSERENFE_H__ + +#include "common.h" + +// True when the sector 0 and 1 key A are the RENFE ones +bool is_valid_renfe_card(const uint8_t *dump, size_t dumplen); + +// Remaining trips, zone code and history events +int renfe_parser_parse(const uint8_t *dump, size_t dumplen); + +// Decode a synthetic card built from the layout +int renfe_selftest(void); + +#endif diff --git a/client/src/parsers/parsesmartrider.c b/client/src/parsers/parsesmartrider.c new file mode 100644 index 000000000..6c341c16d --- /dev/null +++ b/client/src/parsers/parsesmartrider.c @@ -0,0 +1,319 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// SmartRider (Perth) and MyWay (Canberra) parser for MIFARE Classic dumps +//----------------------------------------------------------------------------- + +#include "parsesmartrider.h" + +#include +#include + +#include "commonutil.h" // MemLeToUint2byte +#include "ui.h" // PrintAndLogEx +#include "util.h" // sprint_hex_inrow +#include "mbedtls/md5.h" +#include "parsetransit.h" + +#define SR_DUMP_MIN 1024 +#define SR_BLK(s, b) ((((s) * 4) + (b)) * 16) +#define SR_MAX_TAGS 12 +#define SR_DAYS_TO_2000 10957 + +typedef enum { + SR_UNKNOWN = 0, + SR_SMARTRIDER, + SR_MYWAY, +} sr_type_t; + +// Sector 7 common keys, SmartRider key A and key B +static const uint64_t sr_s7_keys[2] = { 0x9189449EA24E, 0x568C9083F71C }; + +// md5("myway" || key || "myway") of the MyWay sector 7 key, from Metrodroid +static const uint8_t sr_myway_digest[16] = { + 0x29, 0xA6, 0x1B, 0x3A, 0x4D, 0x5C, 0x81, 0x84, + 0x15, 0x35, 0x08, 0x04, 0xC8, 0x2C, 0xD8, 0x34 +}; + +static bool sr_key_digest_is(const uint8_t *key, const char *salt, const uint8_t *digest) { + uint8_t buf[32]; + size_t sl = strlen(salt); + if ((2 * sl) + 6 > sizeof(buf)) { + return false; + } + memcpy(buf, salt, sl); + memcpy(buf + sl, key, 6); + memcpy(buf + sl + 6, salt, sl); + uint8_t md[16]; + if (mbedtls_md5_ret(buf, (2 * sl) + 6, md) != 0) { + return false; + } + return memcmp(md, digest, sizeof(md)) == 0; +} + +static sr_type_t sr_detect(const uint8_t *dump, size_t dumplen) { + if (dump == NULL || dumplen < SR_DUMP_MIN) { + return SR_UNKNOWN; + } + const uint8_t *t = dump + SR_BLK(7, 3); + if (MemBeToUint6byte(t) == sr_s7_keys[0] || MemBeToUint6byte(t + 10) == sr_s7_keys[1]) { + return SR_SMARTRIDER; + } + if (sr_key_digest_is(t, "myway", sr_myway_digest) || sr_key_digest_is(t + 10, "myway", sr_myway_digest)) { + return SR_MYWAY; + } + return SR_UNKNOWN; +} + +bool is_valid_smartrider_card(const uint8_t *dump, size_t dumplen) { + return sr_detect(dump, dumplen) != SR_UNKNOWN; +} + +// Seconds since 2000-01-01 local time +static void sr_time_str(uint32_t secs, char *out, size_t outlen) { + transit_datetime_str(out, outlen, ((int64_t)SR_DAYS_TO_2000 * 86400) + secs); +} + +// Data blocks of a sector, 48 bytes +static void sr_sector_data(const uint8_t *dump, uint8_t sector, uint8_t *out) { + memcpy(out, dump + SR_BLK(sector, 0), 48); +} + +typedef struct { + uint16_t txn; + int32_t balance; + uint16_t total_fare; + uint16_t journey; +} sr_balance_t; + +static void sr_read_balance(const uint8_t *dump, uint8_t sector, sr_balance_t *b) { + uint8_t d[48]; + sr_sector_data(dump, sector, d); + b->txn = MemLeToUint2byte(d + 3); + b->balance = MemLeToUint2byte(d + 39); + if (d[2] & 0x80) { + b->balance = -b->balance; + } + b->total_fare = MemLeToUint2byte(d + 33); + b->journey = MemLeToUint2byte(d + 43); +} + +static const char *sr_concession(sr_type_t type, uint8_t token) { + if (type != SR_SMARTRIDER) { + return ""; + } + switch (token) { + case 0x01: + return "standard"; + case 0x02: + return "student"; + case 0x04: + return "tertiary"; + case 0x06: + return "senior"; + case 0x07: + return "concession"; + case 0x0E: + return "staff"; + case 0x0F: + return "pensioner"; + case 0x10: + return "convention"; + default: + return "unknown"; + } +} + +static const char *sr_mode(sr_type_t type, uint8_t bitfield) { + switch (bitfield & 0x03) { + case 0: + return "bus"; + case 1: + return (type == SR_MYWAY) ? "tram" : "train"; + case 2: + return "ferry"; + default: + return "other"; + } +} + +typedef struct { + uint32_t ts; + uint8_t bitfield; + char route[5]; + uint16_t cost; +} sr_tag_t; + +static void sr_read_tag(const uint8_t *blk, sr_tag_t *t) { + t->ts = MemLeToUint4byte(blk + 3); + t->bitfield = blk[7]; + size_t n = 0; + for (uint8_t i = 0; i < 4; i++) { + if (blk[8 + i] >= 0x20 && blk[8 + i] < 0x7F) { + t->route[n++] = (char)blk[8 + i]; + } + } + t->route[n] = '\0'; + t->cost = MemLeToUint2byte(blk + 13); +} + +int smartrider_parser_parse(const uint8_t *dump, size_t dumplen) { + sr_type_t type = sr_detect(dump, dumplen); + if (type == SR_UNKNOWN) { + return PM3_EINVARG; + } + + char s[64]; + transit_print_header((type == SR_MYWAY) ? "MyWay - Canberra" : "SmartRider - Perth"); + + // serial, ten BCD digits of sector 0 block 1 with one leading zero dropped + char serial[16]; + snprintf(serial, sizeof(serial), "%s", sprint_hex_inrow(dump + SR_BLK(0, 1) + 6, 5)); + transit_print_kv("Serial", _YELLOW_("%s"), (serial[0] == '0') ? serial + 1 : serial); + + uint8_t cfg[48]; + sr_sector_data(dump, 1, cfg); + uint8_t token = cfg[24]; + transit_print_kv("Token type", "%u %s", token, sr_concession(type, token)); + + uint16_t issue = MemLeToUint2byte(cfg + 16); + uint16_t expiry = MemLeToUint2byte(cfg + 18); + if (issue) { + transit_date_str(s, sizeof(s), TRANSIT_DAYS_TO_1997 + issue); + transit_print_kv("Issued", "%s", s); + } + if (expiry) { + transit_date_str(s, sizeof(s), TRANSIT_DAYS_TO_1997 + expiry); + transit_print_kv("Expiry", "%s", s); + } + + sr_balance_t ba, bb; + sr_read_balance(dump, 2, &ba); + sr_read_balance(dump, 3, &bb); + const sr_balance_t *bal = (bb.txn > ba.txn) ? &bb : &ba; + transit_money_str(s, sizeof(s), bal->balance, 2, "AUD"); + transit_print_kv("Balance", _GREEN_("%s"), s); + transit_print_kv("Transaction no", "%u", bal->txn); + transit_print_kv("Journey no", "%u", bal->journey); + + if (type == SR_SMARTRIDER) { + char v[32]; + transit_money_str(s, sizeof(s), MemLeToUint2byte(cfg + 20), 2, "AUD"); + transit_money_str(v, sizeof(v), MemLeToUint2byte(cfg + 22), 2, "AUD"); + transit_print_kv("Autoload", "%s when below %s", v, s); + } + + // tag records, sectors 10..13, newest first + sr_tag_t tags[SR_MAX_TAGS]; + uint8_t n = 0; + for (uint8_t sec = 10; sec <= 13; sec++) { + for (uint8_t b = 0; b < 3; b++) { + sr_tag_t t; + sr_read_tag(dump + SR_BLK(sec, b), &t); + if (t.ts != 0 && n < SR_MAX_TAGS) { + tags[n++] = t; + } + } + } + for (uint8_t i = 1; i < n; i++) { + for (uint8_t j = i; j > 0 && tags[j].ts > tags[j - 1].ts; j--) { + sr_tag_t tmp = tags[j]; + tags[j] = tags[j - 1]; + tags[j - 1] = tmp; + } + } + + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Tag on / off, times local"); + for (uint8_t i = 0; i < n; i++) { + char cost[32]; + sr_time_str(tags[i].ts, s, sizeof(s)); + transit_money_str(cost, sizeof(cost), tags[i].cost, 2, "AUD"); + PrintAndLogEx(INFO, " %s %-3s %-5s route %-4s %12s%s", s, + (tags[i].bitfield & 0x10) ? "on" : "off", + sr_mode(type, tags[i].bitfield), tags[i].route, cost, + (tags[i].bitfield & 0x08) ? " transfer" : ""); + } + return PM3_SUCCESS; +} + +//----------------------------------------------------------------------------- +// Self test. The timestamp is Metrodroid's SmartRiderTest vector, the card is +// synthetic, built from the layout above. +//----------------------------------------------------------------------------- + +int smartrider_selftest(void) { + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Testing SmartRider decode"); + + char s[32]; + bool ok = true; + sr_time_str(529800999, s, sizeof(s)); + ok &= transit_check_str("Metrodroid timestamp", s, "2016-10-14 22:56:39"); + + static uint8_t d[SR_DUMP_MIN]; + memset(d, 0, sizeof(d)); + + // key A of sector 7 + uint8_t *t = d + SR_BLK(7, 3); + for (int i = 0; i < 6; i++) { + t[i] = (uint8_t)(sr_s7_keys[0] >> (40 - (8 * i))); + } + ok &= transit_check("detected", is_valid_smartrider_card(d, sizeof(d)), true); + + // Metrodroid's published digest of the SmartRider key A proves the hashing + static const uint8_t sr_key2_digest[16] = { + 0xE0, 0x91, 0x35, 0x18, 0xA5, 0x00, 0x8C, 0x03, + 0xE1, 0xB3, 0xF2, 0xBB, 0x3A, 0x43, 0xFF, 0x78 + }; + ok &= transit_check("SmartRider key digest", sr_key_digest_is(t, "smartrider", sr_key2_digest), true); + ok &= transit_check("not MyWay digest", sr_key_digest_is(t, "myway", sr_myway_digest), false); + + // serial 0123456789, standard token, balance -$2.50 in the newer sector 3 + memcpy(d + SR_BLK(0, 1) + 6, "\x01\x23\x45\x67\x89", 5); + d[SR_BLK(1, 1) + 8] = 0x01; + d[SR_BLK(2, 0) + 3] = 0x07; + uint8_t *b3 = d + SR_BLK(3, 0); + b3[2] = 0x80; + b3[3] = 0x08; + b3[39] = 0xFA; + + sr_balance_t bb; + sr_read_balance(d, 3, &bb); + ok &= transit_check("txn number", bb.txn, 8); + ok &= transit_check("balance negated", (uint64_t)(-bb.balance), 250); + + // tag on a train, route "MAN", $3.20 + uint8_t *tag = d + SR_BLK(10, 0); + tag[3] = 0x27; + tag[4] = 0x1F; + tag[5] = 0x94; + tag[6] = 0x1F; + tag[7] = 0x11; + memcpy(tag + 8, "MAN\0", 4); + tag[13] = 0x40; + tag[14] = 0x01; + sr_tag_t tg; + sr_read_tag(tag, &tg); + ok &= transit_check("tag time", tg.ts, 529800999); + ok &= transit_check_str("tag route", tg.route, "MAN"); + ok &= transit_check("tag cost", tg.cost, 320); + ok &= transit_check("parse", smartrider_parser_parse(d, sizeof(d)), PM3_SUCCESS); + + t[0] ^= 0xFF; + ok &= transit_check("wrong sector 7 key", is_valid_smartrider_card(d, sizeof(d)), false); + + return transit_selftest_result("SmartRider", ok); +} diff --git a/client/src/parsers/parsesmartrider.h b/client/src/parsers/parsesmartrider.h new file mode 100644 index 000000000..36827ce13 --- /dev/null +++ b/client/src/parsers/parsesmartrider.h @@ -0,0 +1,35 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// SmartRider (Perth) and MyWay (Canberra) parser for MIFARE Classic dumps +// +// Layout from Metrodroid SmartRiderTransitData, GPLv3. +//----------------------------------------------------------------------------- + +#ifndef PARSESMARTRIDER_H__ +#define PARSESMARTRIDER_H__ + +#include "common.h" + +// True when the sector 7 keys are the SmartRider or MyWay common keys +bool is_valid_smartrider_card(const uint8_t *dump, size_t dumplen); + +// Serial, concession, balance, dates, autoload and tag on / off records +int smartrider_parser_parse(const uint8_t *dump, size_t dumplen); + +// Metrodroid timestamp vector plus a synthetic card +int smartrider_selftest(void); + +#endif diff --git a/client/src/parsers/parsetransit.c b/client/src/parsers/parsetransit.c index f0bcb7b6f..78b6e87a7 100644 --- a/client/src/parsers/parsetransit.c +++ b/client/src/parsers/parsetransit.c @@ -24,6 +24,18 @@ #include "ui.h" // PrintAndLogEx +#include "parsebip.h" +#include "parsecharlie.h" +#include "parsekazan.h" +#include "parsemetromoney.h" +#include "parsenextfare.h" +#include "parseplantain.h" +#include "parserenfe.h" +#include "parsesmartrider.h" +#include "parsetroika.h" +#include "parseumarsh.h" +#include "parsezolotayakorona.h" + #define TRANSIT_LABEL_WIDTH 17 //----------------------------------------------------------------------------- @@ -320,6 +332,17 @@ typedef struct { } transit_calypso_parser_t; static const transit_buf_parser_t mfc_parsers[] = { + {"Bip!", is_valid_bip_card, bip_parser_parse, bip_selftest}, + {"CharlieCard", is_valid_charlie_card, charlie_parser_parse, charlie_selftest}, + {"Kazan", is_valid_kazan_card, kazan_parser_parse, kazan_selftest}, + {"Metromoney", is_valid_metromoney_card, metromoney_parser_parse, metromoney_selftest}, + {"Nextfare", is_valid_nextfare_card, nextfare_parser_parse, nextfare_selftest}, + {"Podorozhnik", is_valid_plantain_card, plantain_parser_parse, plantain_selftest}, + {"RENFE Suma 10", is_valid_renfe_card, renfe_parser_parse, renfe_selftest}, + {"SmartRider", is_valid_smartrider_card, smartrider_parser_parse, smartrider_selftest}, + {"Troika", is_valid_troika_card, troika_parser_parse, troika_selftest}, + {"Umarsh", is_valid_umarsh_card, umarsh_parser_parse, umarsh_selftest}, + {"Zolotaya Korona", is_valid_zolotayakorona_card, zolotayakorona_parser_parse, zolotayakorona_selftest}, {NULL, NULL, NULL, NULL} }; @@ -328,6 +351,7 @@ static const transit_mfdes_parser_t mfdes_parsers[] = { }; static const transit_buf_parser_t mfu_parsers[] = { + {"Troika Ultralight", is_valid_troika_ul_card, troika_ul_parser_parse, troika_ul_selftest}, {NULL, NULL, NULL, NULL} }; diff --git a/client/src/parsers/parsetransitmfc.h b/client/src/parsers/parsetransitmfc.h new file mode 100644 index 000000000..b217b97d0 --- /dev/null +++ b/client/src/parsers/parsetransitmfc.h @@ -0,0 +1,77 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// MIFARE Classic dump access for the transit parsers, 1K/4K block layout +//----------------------------------------------------------------------------- + +#ifndef PARSETRANSITMFC_H__ +#define PARSETRANSITMFC_H__ + +#include "common.h" + +#define TRANSIT_MFC_BLOCK_SIZE 16 + +static inline size_t transit_mfc_first_block(uint8_t sector) { + return (sector < 32) ? ((size_t)sector * 4) : (128 + ((size_t)(sector - 32) * 16)); +} + +static inline uint8_t transit_mfc_blocks_in_sector(uint8_t sector) { + return (sector < 32) ? 4 : 16; +} + +// Data block of a sector, NULL when the dump does not reach it +static inline const uint8_t *transit_mfc_block(const uint8_t *dump, size_t dumplen, uint8_t sector, uint8_t blk) { + if (dump == NULL || blk >= transit_mfc_blocks_in_sector(sector)) { + return NULL; + } + size_t off = (transit_mfc_first_block(sector) + blk) * TRANSIT_MFC_BLOCK_SIZE; + if (off + TRANSIT_MFC_BLOCK_SIZE > dumplen) { + return NULL; + } + return dump + off; +} + +// Key A or B from the sector trailer, as a 48 bit big endian number, 0 when out of range +static inline uint64_t transit_mfc_key(const uint8_t *dump, size_t dumplen, uint8_t sector, bool key_b) { + const uint8_t *t = transit_mfc_block(dump, dumplen, sector, transit_mfc_blocks_in_sector(sector) - 1); + if (t == NULL) { + return 0; + } + const uint8_t *k = t + (key_b ? 10 : 0); + uint64_t v = 0; + for (uint8_t i = 0; i < 6; i++) { + v = (v << 8) | k[i]; + } + return v; +} + +// True when key A or key B of the sector is the given one +static inline bool transit_mfc_has_key(const uint8_t *dump, size_t dumplen, uint8_t sector, uint64_t key) { + return (transit_mfc_key(dump, dumplen, sector, false) == key) || (transit_mfc_key(dump, dumplen, sector, true) == key); +} + +// Writes a key into a sector trailer, for building self test dumps +static inline void transit_mfc_set_key(uint8_t *dump, size_t dumplen, uint8_t sector, bool key_b, uint64_t key) { + size_t off = (transit_mfc_first_block(sector) + transit_mfc_blocks_in_sector(sector) - 1) * TRANSIT_MFC_BLOCK_SIZE; + if (off + TRANSIT_MFC_BLOCK_SIZE > dumplen) { + return; + } + uint8_t *k = dump + off + (key_b ? 10 : 0); + for (uint8_t i = 0; i < 6; i++) { + k[i] = (uint8_t)(key >> (40 - (8 * i))); + } +} + +#endif diff --git a/client/src/parsers/parsetroika.c b/client/src/parsers/parsetroika.c new file mode 100644 index 000000000..0b95ffd73 --- /dev/null +++ b/client/src/parsers/parsetroika.c @@ -0,0 +1,647 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Troika (Moscow) parser for MIFARE Classic and Ultralight dumps +//----------------------------------------------------------------------------- + +#include "parsetroika.h" + +#include +#include + +#include "commonutil.h" // ARRAYLEN +#include "ui.h" // PrintAndLogEx +#include "parsetransit.h" +#include "parsetransitmfc.h" + +// Troika block, the first 32 bytes of a sector or of pages 4..11 +#define TROIKA_BLOCK_LEN 32 + +// Moscow local epochs, as days since 1970-01-01 +#define TROIKA_DAYS_TO_1992 8035 +#define TROIKA_DAYS_TO_2016 16801 +#define TROIKA_DAYS_TO_2019 17897 + +#define TROIKA_UL_FIRST_PAGE 4 + +typedef struct { + bool valid; + bool date_only; + int64_t secs; // Moscow local time +} troika_time_t; + +typedef enum { + TROIKA_TT_NONE, + TROIKA_TT_UNKNOWN, + TROIKA_TT_SUBWAY, + TROIKA_TT_MONORAIL, + TROIKA_TT_GROUND, + TROIKA_TT_MCC, +} troika_transport_t; + +typedef struct { + uint32_t serial; + uint16_t ticket_type; + uint8_t layout; + uint8_t sublayout; // layout E only + + troika_time_t expiry; + troika_time_t last_validation; + troika_time_t validity_start; + troika_time_t validity_end; + troika_time_t last_refill; + + int32_t validity_minutes; // -1 when absent + int32_t remaining_trips; + int32_t validator; + int32_t balance; // kopecks + int32_t refill_counter; + int32_t purse_trips; + + uint8_t transfer_count; + int32_t transfers[2]; // minutes after the first validation + + // transport of the last trip, see troika_transport() + int32_t lead_code; // -1 when absent + int32_t long_code; + int32_t e2_code; // layout E2 keeps its own 2 bit code + const char *fare; + bool is_purse; + bool known_layout; +} troika_block_t; + +// Sector keys A, 1K and 4K agree on these +static const uint64_t troika_sector_keys[16] = { + [1] = 0xA82607B01C0D, + [4] = 0x73068F118C13, + [7] = 0xAE3D65A3DAD4, + [8] = 0xA73F5DC1D333, +}; + +// Sector 1 on a 4K card uses the MAD key A, key B tells it apart +#define TROIKA_4K_S1_KEY_B 0x7DE02A7F6025 + +// Metrodroid decodes these sectors in this order +static const uint8_t troika_sectors[] = { 8, 7, 4, 1 }; + +static uint64_t troika_bits(const uint8_t *b, size_t off, uint8_t n) { + return transit_bits_be(b, TROIKA_BLOCK_LEN, off, n); +} + +//----------------------------------------------------------------------------- +// Time conversion, day 1 is the epoch itself +//----------------------------------------------------------------------------- + +static troika_time_t troika_dt(int32_t epoch, uint32_t days, uint32_t mins) { + troika_time_t t = {0}; + if (days == 0 && mins == 0) { + return t; + } + t.valid = true; + t.secs = ((int64_t)(epoch + (int32_t)days - 1) * 86400) + ((int64_t)mins * 60); + return t; +} + +static troika_time_t troika_date(int32_t epoch, uint32_t days) { + troika_time_t t = {0}; + if (days == 0) { + return t; + } + t.valid = true; + t.date_only = true; + t.secs = (int64_t)(epoch + (int32_t)days - 1) * 86400; + return t; +} + +static void troika_time_str(char *out, size_t outlen, troika_time_t t) { + if (t.date_only) { + transit_date_str(out, outlen, (int32_t)(t.secs / 86400)); + } else { + transit_datetime_str(out, outlen, t.secs); + } +} + +//----------------------------------------------------------------------------- +// Block decoding +//----------------------------------------------------------------------------- + +static bool troika_check(const uint8_t *b) { + uint64_t v = troika_bits(b, 0, 10); + return (v == 0x117 || v == 0x108 || v == 0x106); +} + +static void troika_decode(const uint8_t *b, troika_block_t *t) { + memset(t, 0, sizeof(*t)); + t->serial = (uint32_t)troika_bits(b, 20, 32); + t->ticket_type = (uint16_t)troika_bits(b, 4, 16); + t->layout = (uint8_t)troika_bits(b, 52, 4); + t->validity_minutes = -1; + t->remaining_trips = -1; + t->validator = -1; + t->balance = -1; + t->refill_counter = -1; + t->purse_trips = -1; + t->lead_code = -1; + t->long_code = -1; + t->e2_code = -1; + t->known_layout = true; + + switch (t->layout) { + case 0x2: { + t->expiry = troika_dt(TROIKA_DAYS_TO_1992, (uint32_t)troika_bits(b, 56, 16), 0); + t->last_validation = troika_dt(TROIKA_DAYS_TO_1992, (uint32_t)troika_bits(b, 141, 16), (uint32_t)troika_bits(b, 130, 11)); + t->validity_start = troika_dt(TROIKA_DAYS_TO_1992, (uint32_t)troika_bits(b, 157, 16), 0); + t->validity_end = troika_dt(TROIKA_DAYS_TO_1992, (uint32_t)troika_bits(b, 173, 16), 0); + t->validator = (int32_t)troika_bits(b, 205, 16); + break; + } + case 0xA: { + uint32_t start = (uint32_t)troika_bits(b, 67, 9); + uint32_t len = (uint32_t)troika_bits(b, 76, 19); + t->validity_minutes = (int32_t)len; + t->last_validation = troika_dt(TROIKA_DAYS_TO_2016, start, (uint32_t)troika_bits(b, 96, 19)); + t->transfers[t->transfer_count++] = (int32_t)troika_bits(b, 119, 7); + t->remaining_trips = (int32_t)troika_bits(b, 128, 8); + t->validator = (int32_t)troika_bits(b, 136, 16); + t->lead_code = (int32_t)troika_bits(b, 126, 2); + t->long_code = (int32_t)troika_bits(b, 152, 8); + t->validity_end = troika_dt(TROIKA_DAYS_TO_2016, start, len - 1); + t->validity_start = troika_dt(TROIKA_DAYS_TO_2016, start, 0); + break; + } + case 0xD: { + t->validity_end = troika_date(TROIKA_DAYS_TO_1992, (uint32_t)troika_bits(b, 64, 16)); + t->validity_start = troika_date(TROIKA_DAYS_TO_1992, (uint32_t)troika_bits(b, 128, 16)); + t->validity_minutes = (int32_t)troika_bits(b, 144, 8) * 60 * 24; + t->transfers[t->transfer_count++] = (int32_t)troika_bits(b, 155, 5) * 5; + t->lead_code = (int32_t)troika_bits(b, 160, 2); + t->long_code = (int32_t)troika_bits(b, 251, 2); + t->remaining_trips = (int32_t)troika_bits(b, 166, 10); + t->validator = (int32_t)troika_bits(b, 176, 16); + t->last_validation = troika_dt(TROIKA_DAYS_TO_1992, (uint32_t)troika_bits(b, 224, 16), (uint32_t)troika_bits(b, 240, 11)); + break; + } + case 0xE: { + t->sublayout = (uint8_t)troika_bits(b, 56, 5); + if (t->sublayout == 2) { + uint32_t len = (uint32_t)troika_bits(b, 131, 20); + uint32_t start = (uint32_t)troika_bits(b, 97, 16); + t->e2_code = (int32_t)troika_bits(b, 163, 2); + t->expiry = troika_dt(TROIKA_DAYS_TO_1992, (uint32_t)troika_bits(b, 71, 16), 0); + t->validity_minutes = (int32_t)len; + t->transfers[t->transfer_count++] = (int32_t)troika_bits(b, 154, 8); + t->remaining_trips = (int32_t)troika_bits(b, 167, 10); + t->validator = (int32_t)troika_bits(b, 177, 16); + t->validity_start = troika_dt(TROIKA_DAYS_TO_1992, start, 0); + t->validity_end = troika_dt(TROIKA_DAYS_TO_1992, start, len - 1); + t->last_validation = troika_dt(TROIKA_DAYS_TO_1992, start, len - (uint32_t)troika_bits(b, 196, 20)); + } else if (t->sublayout == 3) { + t->is_purse = true; + t->expiry = troika_dt(TROIKA_DAYS_TO_1992, (uint32_t)troika_bits(b, 61, 16), 0); + t->validator = (int32_t)troika_bits(b, 128, 16); + t->last_validation = troika_dt(TROIKA_DAYS_TO_2016, 0, (uint32_t)troika_bits(b, 144, 23)); + t->transfers[t->transfer_count++] = (int32_t)troika_bits(b, 171, 7); + t->lead_code = (int32_t)troika_bits(b, 178, 2); + t->long_code = (int32_t)troika_bits(b, 180, 8); + t->balance = (int32_t)troika_bits(b, 188, 22); + switch (troika_bits(b, 210, 2)) { + case 1: + t->fare = "single trip"; + break; + case 2: + t->fare = "90 minutes"; + break; + default: + break; + } + } else if (t->sublayout == 5) { + t->is_purse = true; + t->expiry = troika_date(TROIKA_DAYS_TO_2019, (uint32_t)troika_bits(b, 61, 13)); + t->last_refill = troika_dt(TROIKA_DAYS_TO_2019, 0, (uint32_t)troika_bits(b, 84, 23)); + t->refill_counter = (int32_t)troika_bits(b, 107, 10); + t->last_validation = troika_dt(TROIKA_DAYS_TO_2019, 0, (uint32_t)troika_bits(b, 128, 23)); + t->transfers[t->transfer_count++] = (int32_t)troika_bits(b, 151, 7); + t->transfers[t->transfer_count++] = (int32_t)troika_bits(b, 158, 7); + t->balance = (int32_t)troika_bits(b, 167, 19); + t->validator = (int32_t)troika_bits(b, 186, 16); + t->purse_trips = (int32_t)troika_bits(b, 216, 7); + } else { + t->known_layout = false; + } + break; + } + default: + t->known_layout = false; + break; + } +} + +// Transport of the last (get_last) or first leg of a journey +static troika_transport_t troika_transport(const troika_block_t *t, bool get_last) { + if (t->e2_code >= 0) { + switch (t->e2_code) { + case 0: + return TROIKA_TT_NONE; + case 1: + return TROIKA_TT_SUBWAY; + case 2: + return TROIKA_TT_MONORAIL; + default: + return TROIKA_TT_GROUND; + } + } + + switch (t->lead_code) { + case 0: + return TROIKA_TT_NONE; + case 1: + break; + case 2: + return get_last ? TROIKA_TT_GROUND : TROIKA_TT_UNKNOWN; + default: + return TROIKA_TT_UNKNOWN; + } + + if (t->long_code <= 0) { + return TROIKA_TT_UNKNOWN; + } + + // four 2 bit codes used in sequence, most significant first + troika_transport_t first = TROIKA_TT_UNKNOWN, last = TROIKA_TT_UNKNOWN; + int found = 0; + for (int i = 6; i >= 0; i -= 2) { + int sc = (t->long_code >> i) & 3; + if (sc == 0) { + continue; + } + troika_transport_t type = (sc == 1) ? TROIKA_TT_SUBWAY : (sc == 2) ? TROIKA_TT_MONORAIL : TROIKA_TT_MCC; + if (found == 0) { + first = type; + } + last = type; + found++; + } + if (found == 1 && get_last == false) { + return TROIKA_TT_UNKNOWN; + } + return get_last ? last : first; +} + +static const char *troika_transport_str(troika_transport_t tt) { + switch (tt) { + case TROIKA_TT_NONE: + return "none"; + case TROIKA_TT_SUBWAY: + return "metro"; + case TROIKA_TT_MONORAIL: + return "monorail"; + case TROIKA_TT_GROUND: + return "ground transport"; + case TROIKA_TT_MCC: + return "MCC"; + case TROIKA_TT_UNKNOWN: + default: + return "unknown"; + } +} + +static const char *troika_ticket_str(uint16_t type) { + switch (type) { + case 0x5D3D: + case 0x5D3E: + case 0x5D48: + case 0x2135: + return "empty ticket holder"; + case 0x183D: + case 0x2129: + return "Druzhinnik card"; + case 0x5D9A: + case 0x5D9B: + return "1 ride"; + case 0x5D9C: + return "2 rides"; + case 0x5DA0: + return "20 rides"; + case 0x5DB1: + return "Troika purse"; + case 0x5DD3: + return "60 rides"; + default: + return "unknown"; + } +} + +static bool troika_has_subscription(const troika_block_t *t) { + if (t->is_purse || t->known_layout == false) { + return false; + } + // layout 2 placeholder of an empty holder + if (t->layout == 0x2) { + switch (t->ticket_type) { + case 0x5D3D: + case 0x5D3E: + case 0x5D48: + case 0x2135: + case 0x2141: + return false; + default: + break; + } + } + return true; +} + +static void troika_serial_str(char *out, size_t outlen, uint32_t sn) { + char d[16]; + snprintf(d, sizeof(d), "%010u", sn); + snprintf(out, outlen, "%.4s %.3s %.3s", d, d + 4, d + 7); +} + +//----------------------------------------------------------------------------- +// Printing +//----------------------------------------------------------------------------- + +static void troika_print_block(const troika_block_t *t, const char *where) { + char buf[64]; + + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "%s ( layout %X%s )", where, t->layout, + (t->layout == 0xE) ? ((t->sublayout == 2) ? ", E2" : (t->sublayout == 3) ? ", E3 purse" : (t->sublayout == 5) ? ", E5 purse" : ", unknown") : ""); + + transit_print_kv("Ticket", "%04X ( %s )", t->ticket_type, troika_ticket_str(t->ticket_type)); + + if (t->known_layout == false) { + transit_print_kv("Layout", "not decoded"); + return; + } + + if (t->balance >= 0) { + transit_money_str(buf, sizeof(buf), t->balance, 2, "RUB"); + transit_print_kv("Balance", _YELLOW_("%s"), buf); + } + if (t->expiry.valid) { + troika_time_str(buf, sizeof(buf), t->expiry); + transit_print_kv("Expiry", "%s", buf); + } + if (troika_has_subscription(t)) { + if (t->validity_start.valid) { + troika_time_str(buf, sizeof(buf), t->validity_start); + transit_print_kv("Valid from", "%s", buf); + } + if (t->validity_end.valid) { + troika_time_str(buf, sizeof(buf), t->validity_end); + transit_print_kv("Valid to", "%s", buf); + } + if (t->validity_minutes > 0) { + transit_print_kv("Validity", "%d min", t->validity_minutes); + } + if (t->remaining_trips >= 0) { + transit_print_kv("Trips left", "%d", t->remaining_trips); + } + } + if (t->last_refill.valid) { + troika_time_str(buf, sizeof(buf), t->last_refill); + transit_print_kv("Last refill", "%s", buf); + } + if (t->refill_counter >= 0) { + transit_print_kv("Refill counter", "%d", t->refill_counter); + } + if (t->purse_trips >= 0) { + transit_print_kv("Purse trips", "%d", t->purse_trips); + } + if (t->fare != NULL) { + transit_print_kv("Last fare", "%s", t->fare); + } + + if (t->last_validation.valid == false) { + return; + } + + // latest leg first, the base time is the first validation of the journey + int32_t legs[3]; + uint8_t n = 0; + for (uint8_t i = 0; i < t->transfer_count; i++) { + if (t->transfers[i] != 0) { + legs[n++] = t->transfers[i]; + } + } + if (n == 2 && legs[1] > legs[0]) { + int32_t tmp = legs[0]; + legs[0] = legs[1]; + legs[1] = tmp; + } + legs[n++] = 0; + + for (uint8_t i = 0; i < n; i++) { + troika_time_t lt = t->last_validation; + lt.secs += (int64_t)legs[i] * 60; + troika_time_str(buf, sizeof(buf), lt); + if (i == 0) { + if (t->validator >= 0) { + transit_print_kv("Trip", "%s %s, validator 0x%04X", buf, troika_transport_str(troika_transport(t, true)), (unsigned)t->validator); + } else { + transit_print_kv("Trip", "%s %s", buf, troika_transport_str(troika_transport(t, true))); + } + } else { + transit_print_kv("Trip", "%s %s", buf, troika_transport_str(troika_transport(t, false))); + } + } +} + +//----------------------------------------------------------------------------- +// MIFARE Classic +//----------------------------------------------------------------------------- + +static bool troika_sector_has_key(const uint8_t *dump, size_t dumplen, uint8_t sector) { + if (transit_mfc_has_key(dump, dumplen, sector, troika_sector_keys[sector])) { + return true; + } + return (sector == 1) && transit_mfc_has_key(dump, dumplen, sector, TROIKA_4K_S1_KEY_B); +} + +// The 32 byte block of a sector, when it carries the Troika keys and header +static bool troika_sector_block(const uint8_t *dump, size_t dumplen, uint8_t sector, uint8_t *out) { + const uint8_t *b0 = transit_mfc_block(dump, dumplen, sector, 0); + const uint8_t *b1 = transit_mfc_block(dump, dumplen, sector, 1); + if (b0 == NULL || b1 == NULL || troika_sector_has_key(dump, dumplen, sector) == false) { + return false; + } + memcpy(out, b0, 16); + memcpy(out + 16, b1, 16); + return troika_check(out); +} + +bool is_valid_troika_card(const uint8_t *dump, size_t dumplen) { + uint8_t b[TROIKA_BLOCK_LEN]; + return troika_sector_block(dump, dumplen, 8, b) || troika_sector_block(dump, dumplen, 4, b); +} + +int troika_parser_parse(const uint8_t *dump, size_t dumplen) { + if (is_valid_troika_card(dump, dumplen) == false) { + return PM3_EINVARG; + } + + transit_print_header("Troika"); + + bool first = true; + for (size_t i = 0; i < ARRAYLEN(troika_sectors); i++) { + uint8_t b[TROIKA_BLOCK_LEN]; + if (troika_sector_block(dump, dumplen, troika_sectors[i], b) == false) { + continue; + } + troika_block_t t; + troika_decode(b, &t); + if (first) { + char sn[20]; + troika_serial_str(sn, sizeof(sn), t.serial); + transit_print_kv("Card number", _YELLOW_("%s"), sn); + first = false; + } + char where[16]; + snprintf(where, sizeof(where), "Sector %u", troika_sectors[i]); + troika_print_block(&t, where); + } + PrintAndLogEx(INFO, "Times are Moscow local time"); + return PM3_SUCCESS; +} + +//----------------------------------------------------------------------------- +// MIFARE Ultralight +//----------------------------------------------------------------------------- + +bool is_valid_troika_ul_card(const uint8_t *data, size_t datalen) { + if (data == NULL || datalen < (TROIKA_UL_FIRST_PAGE * 4) + TROIKA_BLOCK_LEN) { + return false; + } + return troika_check(data + (TROIKA_UL_FIRST_PAGE * 4)); +} + +int troika_ul_parser_parse(const uint8_t *data, size_t datalen) { + if (is_valid_troika_ul_card(data, datalen) == false) { + return PM3_EINVARG; + } + + troika_block_t t; + troika_decode(data + (TROIKA_UL_FIRST_PAGE * 4), &t); + + char sn[20]; + troika_serial_str(sn, sizeof(sn), t.serial); + transit_print_header("Troika ticket"); + transit_print_kv("Card number", _YELLOW_("%s"), sn); + troika_print_block(&t, "Pages 4..11"); + PrintAndLogEx(INFO, "Times are Moscow local time"); + return PM3_SUCCESS; +} + +//----------------------------------------------------------------------------- +// Self tests +//----------------------------------------------------------------------------- + +// Metrodroid test asset troika/troikaul.json, pages 0..19 +static const uint8_t troika_ul_vector[] = { + 0x12, 0x34, 0x56, 0xBD, 0x77, 0x88, 0x99, 0x00, 0x98, 0x48, 0xF0, 0x00, 0xFF, 0xFF, 0xFF, 0xFC, + 0x45, 0xD9, 0xA1, 0x23, 0x45, 0x67, 0x8D, 0x00, 0x26, 0x01, 0x00, 0x00, 0x26, 0x01, 0x00, 0x00, + 0x25, 0xBC, 0x05, 0x00, 0x80, 0x00, 0x78, 0xAA, 0x4F, 0x84, 0xE6, 0x0C, 0x25, 0xBC, 0x3B, 0xA0, + 0x25, 0xBC, 0x05, 0x00, 0x80, 0x00, 0x78, 0xAA, 0x4F, 0x84, 0xE6, 0x0C, 0x25, 0xBC, 0x3B, 0xA0, + 0x00, 0x00, 0x00, 0xFF, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +}; + +// Checks the decode against Metrodroid's parsed/troikaul.json +static bool troika_check_ul_vector(const uint8_t *b) { + troika_block_t t; + troika_decode(b, &t); + + char buf[40]; + bool ok = true; + troika_serial_str(buf, sizeof(buf), t.serial); + ok &= transit_check_str("serial", buf, "0305 419 896"); + ok &= transit_check("layout", t.layout, 0xD); + ok &= transit_check("ticket type", t.ticket_type, 0x5D9A); + ok &= transit_check("valid from (days)", (uint64_t)(t.validity_start.secs / 86400), 17694); + ok &= transit_check("valid to (days)", (uint64_t)(t.validity_end.secs / 86400), 17763); + ok &= transit_check("trips left", (uint64_t)t.remaining_trips, 0); + ok &= transit_check("validator", (uint64_t)t.validator, 0x78AA); + // 1528779420 is UTC, Moscow is three hours ahead + ok &= transit_check("last validation", (uint64_t)t.last_validation.secs, 1528779420 + (3 * 3600)); + ok &= transit_check("transport is ground", troika_transport(&t, true), TROIKA_TT_GROUND); + return ok; +} + +int troika_ul_selftest(void) { + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Testing Troika Ultralight decode"); + + bool ok = transit_check("detected", is_valid_troika_ul_card(troika_ul_vector, sizeof(troika_ul_vector)), 1); + ok &= troika_check_ul_vector(troika_ul_vector + (TROIKA_UL_FIRST_PAGE * 4)); + + uint8_t bad[sizeof(troika_ul_vector)]; + memcpy(bad, troika_ul_vector, sizeof(bad)); + bad[16] = 0x00; + ok &= transit_check("bad header rejected", is_valid_troika_ul_card(bad, sizeof(bad)), 0); + return transit_selftest_result("Troika Ultralight", ok); +} + +int troika_selftest(void) { + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Testing Troika MIFARE Classic decode"); + + // synthetic 1K: the Ultralight block in sector 8, an E5 purse built from the layout in sector 4 + uint8_t dump[1024] = {0}; + memcpy(dump + (32 * 16), troika_ul_vector + 16, 32); + for (uint8_t s = 0; s < 16; s++) { + transit_mfc_set_key(dump, sizeof(dump), s, false, 0xFFFFFFFFFFFF); + } + transit_mfc_set_key(dump, sizeof(dump), 8, false, troika_sector_keys[8]); + + bool ok = transit_check("detected", is_valid_troika_card(dump, sizeof(dump)), 1); + + uint8_t b[TROIKA_BLOCK_LEN]; + ok &= transit_check("sector 8 read", troika_sector_block(dump, sizeof(dump), 8, b), 1); + ok &= troika_check_ul_vector(b); + + uint8_t *p = dump + (16 * 16); + transit_set_bits_be(p, 32, 0, 10, 0x117); + transit_set_bits_be(p, 32, 4, 16, 0x5DB1); + transit_set_bits_be(p, 32, 20, 32, 305419896); + transit_set_bits_be(p, 32, 52, 4, 0xE); + transit_set_bits_be(p, 32, 56, 5, 5); + transit_set_bits_be(p, 32, 61, 13, 2000); + transit_set_bits_be(p, 32, 107, 10, 7); + transit_set_bits_be(p, 32, 128, 23, 1440 + 90); + transit_set_bits_be(p, 32, 167, 19, 12345); + transit_set_bits_be(p, 32, 186, 16, 4242); + + ok &= transit_check("sector 4 needs its key", troika_sector_block(dump, sizeof(dump), 4, b), 0); + transit_mfc_set_key(dump, sizeof(dump), 4, false, troika_sector_keys[4]); + ok &= transit_check("sector 4 with its key", troika_sector_block(dump, sizeof(dump), 4, b), 1); + + troika_block_t t; + troika_decode(b, &t); + ok &= transit_check("E5 purse", t.is_purse && t.sublayout == 5, 1); + ok &= transit_check("E5 balance", (uint64_t)t.balance, 12345); + ok &= transit_check("E5 refill counter", (uint64_t)t.refill_counter, 7); + ok &= transit_check("E5 validator", (uint64_t)t.validator, 4242); + ok &= transit_check("E5 expiry (days)", (uint64_t)(t.expiry.secs / 86400), TROIKA_DAYS_TO_2019 + 1999); + // minutes since 2019-01-01 00:00 minus a day, Metrodroid's day zero + ok &= transit_check("E5 last validation", (uint64_t)t.last_validation.secs, ((uint64_t)TROIKA_DAYS_TO_2019 * 86400) + (90 * 60)); + + // a matching header under the wrong keys is not Troika + transit_mfc_set_key(dump, sizeof(dump), 8, false, 0xFFFFFFFFFFFF); + transit_mfc_set_key(dump, sizeof(dump), 4, false, 0xFFFFFFFFFFFF); + ok &= transit_check("wrong keys rejected", is_valid_troika_card(dump, sizeof(dump)), 0); + ok &= transit_check("short dump rejected", is_valid_troika_card(dump, 512), 0); + + return transit_selftest_result("Troika", ok); +} diff --git a/client/src/parsers/parsetroika.h b/client/src/parsers/parsetroika.h new file mode 100644 index 000000000..33eb73d8e --- /dev/null +++ b/client/src/parsers/parsetroika.h @@ -0,0 +1,37 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Troika (Moscow) parser for MIFARE Classic and Ultralight dumps +// +// Block layouts ported from Metrodroid (GPLv3) transit/troika, sector keys +// from the Flipper Zero troika plugin (GPLv3). +//----------------------------------------------------------------------------- + +#ifndef PARSETROIKA_H__ +#define PARSETROIKA_H__ + +#include "common.h" + +// MIFARE Classic, a Troika block in sector 8 or 4 under the Troika keys +bool is_valid_troika_card(const uint8_t *dump, size_t dumplen); +int troika_parser_parse(const uint8_t *dump, size_t dumplen); +int troika_selftest(void); + +// MIFARE Ultralight, the Troika block starts at page 4 +bool is_valid_troika_ul_card(const uint8_t *data, size_t datalen); +int troika_ul_parser_parse(const uint8_t *data, size_t datalen); +int troika_ul_selftest(void); + +#endif diff --git a/client/src/parsers/parseumarsh.c b/client/src/parsers/parseumarsh.c new file mode 100644 index 000000000..a19c7bb35 --- /dev/null +++ b/client/src/parsers/parseumarsh.c @@ -0,0 +1,346 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Umarsh parser for MIFARE Classic dumps +//----------------------------------------------------------------------------- + +#include "parseumarsh.h" + +#include +#include + +#include "commonutil.h" // ARRAYLEN, MemLeToUint4byte +#include "ui.h" // PrintAndLogEx +#include "parsetransit.h" +#include "parsetransitmfc.h" + +#define UMARSH_SECTOR 8 +#define UMARSH_EXTRA_SECTOR 7 // aerial tramway, Nizhny Novgorod only +#define UMARSH_REGION_NN 52 + +typedef enum { + UMARSH_DENOM_DEFAULT, // purse when total is 0, else trips + UMARSH_DENOM_UNLIMITED, + UMARSH_DENOM_TRIPS, + UMARSH_DENOM_RUB, +} umarsh_denom_t; + +typedef struct { + uint8_t region; + const char *name; +} umarsh_system_t; + +static const umarsh_system_t umarsh_systems[] = { + { 12, "Yoshkar-Ola" }, + { 18, "Strizh, Izhevsk" }, + { 22, "Barnaul" }, + { 33, "Siticard, Vladimir" }, + { 43, "Kirov" }, + { 52, "Siticard, Nizhny Novgorod" }, + { 55, "Omka, Omsk" }, + { 58, "Penza" }, + { 66, "E-karta, Ekaterinburg" }, + { 91, "Crimea trolleybus" }, +}; + +typedef struct { + uint8_t region; + uint32_t tariff; + umarsh_denom_t denom; +} umarsh_tariff_t; + +// Tariffs Metrodroid gives an explicit denomination +static const umarsh_tariff_t umarsh_tariffs[] = { + { 52, 0x0A7F00, UMARSH_DENOM_RUB }, + { 52, 0x0A007F, UMARSH_DENOM_RUB }, + { 52, 0x21007F, UMARSH_DENOM_RUB }, + { 52, 0x2564FF, UMARSH_DENOM_UNLIMITED }, + { 52, 0x31002F, UMARSH_DENOM_RUB }, + { 52, 0x33690F, UMARSH_DENOM_TRIPS }, + { 52, 0x34690F, UMARSH_DENOM_TRIPS }, + { 55, 0x5700FF, UMARSH_DENOM_RUB }, + { 55, 0x5780FF, UMARSH_DENOM_RUB }, + { 55, 0x6300FF, UMARSH_DENOM_RUB }, + { 55, 0x5800FF, UMARSH_DENOM_RUB }, + { 55, 0x5900FF, UMARSH_DENOM_RUB }, + { 55, 0x15EAFF, UMARSH_DENOM_TRIPS }, + { 55, 0x5500FF, UMARSH_DENOM_UNLIMITED }, + { 55, 0x5600FF, UMARSH_DENOM_UNLIMITED }, + { 55, 0x5B00FF, UMARSH_DENOM_UNLIMITED }, + { 55, 0x5C00FF, UMARSH_DENOM_UNLIMITED }, + { 55, 0x6000FF, UMARSH_DENOM_RUB }, + { 55, 0x6100FF, UMARSH_DENOM_TRIPS }, + { 55, 0x6200FF, UMARSH_DENOM_TRIPS }, + { 55, 0x5300FF, UMARSH_DENOM_TRIPS }, + { 55, 0x5400FF, UMARSH_DENOM_TRIPS }, + { 66, 0x42640F, UMARSH_DENOM_UNLIMITED }, + { 91, 0x3D7F00, UMARSH_DENOM_UNLIMITED }, + { 91, 0x467F00, UMARSH_DENOM_UNLIMITED }, +}; + +typedef struct { + uint32_t counter; + uint16_t expiry; // packed date, see umarsh_date_str() + uint16_t total; + uint8_t refill_counter; + uint8_t region; + uint32_t serial; + uint32_t tariff; + uint16_t valid_to; + uint32_t machine; + uint16_t last_refill; + uint16_t balance_rub; + uint8_t balance_kop; +} umarsh_sector_t; + +static uint32_t umarsh_be(const uint8_t *p, uint8_t n) { + uint32_t v = 0; + for (uint8_t i = 0; i < n; i++) { + v = (v << 8) | p[i]; + } + return v; +} + +// 7 bit year since 2000, 4 bit month, 5 bit day +static bool umarsh_date_str(char *out, size_t outlen, uint16_t d) { + if (d == 0 || d == 0xFFFF) { + return false; + } + snprintf(out, outlen, "%04u-%02u-%02u", 2000 + (d >> 9), (d >> 5) & 0x0F, d & 0x1F); + return true; +} + +// Value block whose value sits just under 0x7FFFFFFF, address byte 0x70..0x7F +static bool umarsh_check(const uint8_t *b) { + uint32_t v = MemLeToUint4byte(b); + return (v == (uint32_t)~MemLeToUint4byte(b + 4)) + && (v == MemLeToUint4byte(b + 8)) + && ((uint8_t)(b[12] + b[13]) == 0xFF) + && (b[12] == b[14]) && (b[13] == b[15]) + && (v >= 0x7FFFFF00) && (v <= 0x7FFFFFFF) + && (b[13] >= 0x70) && (b[13] <= 0x7F); +} + +static void umarsh_decode(const uint8_t *b0, const uint8_t *b1, const uint8_t *b2, umarsh_sector_t *s) { + memset(s, 0, sizeof(*s)); + s->counter = 0x7FFFFFFF - MemLeToUint4byte(b0); + s->expiry = (uint16_t)umarsh_be(b1 + 1, 2); + s->total = (uint16_t)umarsh_be(b1 + 5, 2); + s->refill_counter = b1[7]; + s->region = (uint8_t)(transit_bits_be(b1, 16, 100, 4) | (transit_bits_be(b1, 16, 64, 3) << 4)); + s->serial = (uint32_t)transit_bits_be(b1, 16, 67, 29); + s->tariff = umarsh_be(b1 + 13, 3); + s->valid_to = (uint16_t)umarsh_be(b2, 2); + s->machine = umarsh_be(b2 + 3, 3); + s->last_refill = (uint16_t)umarsh_be(b2 + 6, 2); + s->balance_rub = (uint16_t)transit_bits_be(b2, 16, 65, 15); + s->balance_kop = b2[10] & 0x7F; +} + +static bool umarsh_read_sector(const uint8_t *dump, size_t dumplen, uint8_t sector, umarsh_sector_t *s) { + const uint8_t *b0 = transit_mfc_block(dump, dumplen, sector, 0); + const uint8_t *b1 = transit_mfc_block(dump, dumplen, sector, 1); + const uint8_t *b2 = transit_mfc_block(dump, dumplen, sector, 2); + if (b0 == NULL || b1 == NULL || b2 == NULL || umarsh_check(b0) == false) { + return false; + } + umarsh_decode(b0, b1, b2, s); + return true; +} + +bool is_valid_umarsh_card(const uint8_t *dump, size_t dumplen) { + umarsh_sector_t s; + return umarsh_read_sector(dump, dumplen, UMARSH_SECTOR, &s) && (s.serial != 0); +} + +static umarsh_denom_t umarsh_denom(const umarsh_sector_t *s) { + for (size_t i = 0; i < ARRAYLEN(umarsh_tariffs); i++) { + if (umarsh_tariffs[i].region == s->region && umarsh_tariffs[i].tariff == s->tariff) { + return umarsh_tariffs[i].denom; + } + } + return (s->total == 0) ? UMARSH_DENOM_RUB : UMARSH_DENOM_TRIPS; +} + +static const char *umarsh_system_name(uint8_t region) { + for (size_t i = 0; i < ARRAYLEN(umarsh_systems); i++) { + if (umarsh_systems[i].region == region) { + return umarsh_systems[i].name; + } + } + return "unknown"; +} + +static void umarsh_print_sector(const umarsh_sector_t *s, uint8_t sector) { + char buf[40]; + + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Sector %u%s", sector, (sector == UMARSH_EXTRA_SECTOR) ? " ( aerial tramway )" : ""); + transit_print_kv("Tariff", "%06X", s->tariff); + + switch (umarsh_denom(s)) { + case UMARSH_DENOM_RUB: + transit_money_str(buf, sizeof(buf), ((int64_t)s->balance_rub * 100) + s->balance_kop, 2, "RUB"); + transit_print_kv("Balance", _YELLOW_("%s"), buf); + break; + case UMARSH_DENOM_TRIPS: + transit_print_kv("Trips left", "%u of %u", s->balance_rub, s->total); + break; + case UMARSH_DENOM_UNLIMITED: + transit_print_kv("Trips", "unlimited"); + break; + case UMARSH_DENOM_DEFAULT: + default: + break; + } + if (umarsh_date_str(buf, sizeof(buf), s->valid_to)) { + transit_print_kv("Valid to", "%s", buf); + } + if (umarsh_date_str(buf, sizeof(buf), s->expiry)) { + transit_print_kv("Card expiry", "%s", buf); + } + if (umarsh_date_str(buf, sizeof(buf), s->last_refill)) { + transit_print_kv("Last refill", "%s machine %u", buf, s->machine); + } + transit_print_kv("Refill counter", "%u", s->refill_counter); +} + +static const char *umarsh_mode_str(uint8_t trans) { + switch (trans) { + case 1: + case 4: + case 5: + return "bus"; + case 2: + return "trolleybus"; + case 3: + return "tram"; + default: + return "other"; + } +} + +int umarsh_parser_parse(const uint8_t *dump, size_t dumplen) { + umarsh_sector_t s8; + if (is_valid_umarsh_card(dump, dumplen) == false || umarsh_read_sector(dump, dumplen, UMARSH_SECTOR, &s8) == false) { + return PM3_EINVARG; + } + + char buf[40]; + char sn[16]; + snprintf(sn, sizeof(sn), "%09u", s8.serial); + + transit_print_header("Umarsh"); + transit_print_kv("System", "%s", umarsh_system_name(s8.region)); + transit_print_kv("Card number", _YELLOW_("%.3s %.3s %.3s"), sn, sn + 3, sn + 6); + transit_print_kv("Region", "%u", s8.region); + + umarsh_print_sector(&s8, UMARSH_SECTOR); + + umarsh_sector_t s7; + if (s8.region == UMARSH_REGION_NN && umarsh_read_sector(dump, dumplen, UMARSH_EXTRA_SECTOR, &s7)) { + umarsh_print_sector(&s7, UMARSH_EXTRA_SECTOR); + } + + // last validation, sector 0 block 1 + const uint8_t *v = transit_mfc_block(dump, dumplen, 0, 1); + uint16_t vdate = (uint16_t)umarsh_be(v + 2, 2); + if (umarsh_date_str(buf, sizeof(buf), vdate)) { + uint8_t trans = (uint8_t)transit_bits_be(v, 16, 0, 3); + uint16_t tm = (uint16_t)transit_bits_be(v, 16, 3, 13); + char route[7], veh[7]; + memcpy(route, v + 4, 6); + memcpy(veh, v + 10, 6); + route[6] = veh[6] = '\0'; + for (uint8_t i = 0; i < 6; i++) { + if (route[i] < 0x20 || route[i] > 0x7E) { + route[i] = '.'; + } + if (veh[i] < 0x20 || veh[i] > 0x7E) { + veh[i] = '.'; + } + } + PrintAndLogEx(INFO, ""); + transit_print_kv("Last trip", "%s %02u:%02u %s, route %s, vehicle %s", buf, tm / 100, tm % 100, + umarsh_mode_str(trans), route, veh); + } + PrintAndLogEx(INFO, "Times are local time of the region"); + return PM3_SUCCESS; +} + +// Synthetic card built from the Metrodroid layout +int umarsh_selftest(void) { + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Testing Umarsh decode"); + + uint8_t dump[1024] = {0}; + uint8_t *s8 = dump + (32 * 16); + + // value block 0x7FFFFFF0, address 0x7A + const uint8_t vb[16] = { + 0xF0, 0xFF, 0xFF, 0x7F, 0x0F, 0x00, 0x00, 0x80, + 0xF0, 0xFF, 0xFF, 0x7F, 0x85, 0x7A, 0x85, 0x7A + }; + memcpy(s8, vb, sizeof(vb)); + + // region 55 (bits 64..66 = 3, bits 100..103 = 7), serial 123456789, tariff 0x5800FF + uint8_t *b1 = s8 + 16; + transit_set_bits_be(b1, 16, 8, 16, (26 << 9) | (12 << 5) | 31); + b1[7] = 4; + transit_set_bits_be(b1, 16, 64, 3, 3); + transit_set_bits_be(b1, 16, 67, 29, 123456789); + transit_set_bits_be(b1, 16, 100, 4, 7); + b1[13] = 0x58; + b1[14] = 0x00; + b1[15] = 0xFF; + + // balance 250 RUB, machine 0x010203, last refill 2024-05-06 + uint8_t *b2 = s8 + 32; + transit_set_bits_be(b2, 16, 0, 16, (25 << 9) | (6 << 5) | 30); + b2[3] = 0x01; + b2[4] = 0x02; + b2[5] = 0x03; + transit_set_bits_be(b2, 16, 48, 16, (24 << 9) | (5 << 5) | 6); + transit_set_bits_be(b2, 16, 65, 15, 250); + b2[10] = 0x80; + + bool ok = transit_check("detected", is_valid_umarsh_card(dump, sizeof(dump)), 1); + + umarsh_sector_t s; + umarsh_read_sector(dump, sizeof(dump), UMARSH_SECTOR, &s); + ok &= transit_check("counter", s.counter, 15); + ok &= transit_check("region", s.region, 55); + ok &= transit_check("serial", s.serial, 123456789); + ok &= transit_check("tariff", s.tariff, 0x5800FF); + ok &= transit_check("denomination rub", umarsh_denom(&s), UMARSH_DENOM_RUB); + ok &= transit_check("balance rub", s.balance_rub, 250); + ok &= transit_check("balance kop", s.balance_kop, 0); + ok &= transit_check("machine", s.machine, 0x010203); + ok &= transit_check("refill counter", s.refill_counter, 4); + + char buf[20]; + umarsh_date_str(buf, sizeof(buf), s.expiry); + ok &= transit_check_str("card expiry", buf, "2026-12-31"); + umarsh_date_str(buf, sizeof(buf), s.last_refill); + ok &= transit_check_str("last refill", buf, "2024-05-06"); + + s8[13] = 0x6A; + s8[15] = 0x6A; + s8[12] = 0x95; + s8[14] = 0x95; + ok &= transit_check("address below 0x70 rejected", is_valid_umarsh_card(dump, sizeof(dump)), 0); + + return transit_selftest_result("Umarsh", ok); +} diff --git a/client/src/parsers/parseumarsh.h b/client/src/parsers/parseumarsh.h new file mode 100644 index 000000000..145de7169 --- /dev/null +++ b/client/src/parsers/parseumarsh.h @@ -0,0 +1,33 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Umarsh parser for MIFARE Classic dumps +// +// Regional Russian cards on the Umarsh format (Siticard, Omka, E-karta, ...). +// Layout ported from Metrodroid (GPLv3) transit/umarsh and the Flipper Zero +// umarsh plugin (GPLv3). +//----------------------------------------------------------------------------- + +#ifndef PARSEUMARSH_H__ +#define PARSEUMARSH_H__ + +#include "common.h" + +// Sector 8 block 0 holds the Umarsh value block header +bool is_valid_umarsh_card(const uint8_t *dump, size_t dumplen); +int umarsh_parser_parse(const uint8_t *dump, size_t dumplen); +int umarsh_selftest(void); + +#endif diff --git a/client/src/parsers/parsezolotayakorona.c b/client/src/parsers/parsezolotayakorona.c new file mode 100644 index 000000000..ead4d00b9 --- /dev/null +++ b/client/src/parsers/parsezolotayakorona.c @@ -0,0 +1,258 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Zolotaya Korona transport card parser for MIFARE Classic dumps +//----------------------------------------------------------------------------- + +#include "parsezolotayakorona.h" + +#include +#include + +#include "commonutil.h" // ARRAYLEN, MemLeToUint4byte +#include "ui.h" // PrintAndLogEx +#include "util.h" // sprint_hex_inrow +#include "parsetransit.h" +#include "parsetransitmfc.h" + +#define ZK_TRIP_SECTOR 4 +#define ZK_PURSE_SECTOR 6 +#define ZK_INFO_SECTOR 15 +#define ZK_MAD_AID_HI 0x18 +#define ZK_MAD_AID_LO 0xEE + +// Sector 15 blocks 0..1, "ZAO Zolotaya Korona" in CP866 with one byte off +static const uint8_t zk_signature[] = { + 0xE2, 0x87, 0x80, 0x8E, 0x20, 0x87, 0xAE, 0xAB, 0xAE, 0xF2, 0xA0, 0xEF, 0x20, 0x8A, 0xAE, 0xE0, + 0xAE, 0xAD, 0xA0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + +typedef struct { + uint32_t type; + const char *name; +} zk_card_t; + +static const zk_card_t zk_cards[] = { + { 0x230100, "Krasnodar ETK" }, + { 0x560200, "Orenburg EKG" }, + { 0x562300, "Orenburg school card" }, + { 0x562400, "Orenburg student card" }, + { 0x631500, "Samara school card" }, + { 0x632600, "Samara ETK" }, + { 0x632700, "Samara student card" }, + { 0x633500, "Samara garden dacha card" }, + { 0x760500, "Yaroslavl ETK" }, +}; + +typedef struct { + uint32_t type; + int32_t balance; // kopecks + uint8_t status; + uint16_t seqno; + uint8_t discount; + + bool has_refill; + uint16_t refill_machine; + uint32_t refill_time; // local pseudo unix time + uint32_t refill_amount; + uint16_t refill_counter; + + bool has_trip; + uint32_t trip_validator; // printed as hex + uint32_t trip_time; + uint8_t trip_track; + uint32_t trip_prev_balance; +} zk_t; + +static uint32_t zk_le(const uint8_t *p, uint8_t n) { + uint32_t v = 0; + for (uint8_t i = n; i > 0; i--) { + v = (v << 8) | p[i - 1]; + } + return v; +} + +static bool zk_has_signature(const uint8_t *dump, size_t dumplen) { + const uint8_t *b0 = transit_mfc_block(dump, dumplen, ZK_INFO_SECTOR, 0); + const uint8_t *b1 = transit_mfc_block(dump, dumplen, ZK_INFO_SECTOR, 1); + if (b0 == NULL || b1 == NULL) { + return false; + } + return (memcmp(b0, zk_signature, 16) == 0) && (memcmp(b1, zk_signature + 16, sizeof(zk_signature) - 16) == 0); +} + +static bool zk_has_mad(const uint8_t *dump, size_t dumplen) { + const uint8_t *toc = transit_mfc_block(dump, dumplen, 0, 1); + if (toc == NULL) { + return false; + } + return toc[8] == ZK_MAD_AID_HI && toc[9] == ZK_MAD_AID_LO && toc[12] == ZK_MAD_AID_HI && toc[13] == ZK_MAD_AID_LO; +} + +bool is_valid_zolotayakorona_card(const uint8_t *dump, size_t dumplen) { + if (transit_mfc_block(dump, dumplen, ZK_INFO_SECTOR, 3) == NULL) { + return false; + } + return zk_has_signature(dump, dumplen) || zk_has_mad(dump, dumplen); +} + +static void zk_decode(const uint8_t *dump, size_t dumplen, zk_t *z) { + memset(z, 0, sizeof(*z)); + + const uint8_t *b = transit_mfc_block(dump, dumplen, ZK_INFO_SECTOR, 1); + z->type = ((uint32_t)b[10] << 16) | ((uint32_t)b[11] << 8) | b[12]; + + z->balance = (int32_t)MemLeToUint4byte(transit_mfc_block(dump, dumplen, ZK_PURSE_SECTOR, 0)); + + b = transit_mfc_block(dump, dumplen, ZK_TRIP_SECTOR, 0); + z->status = (uint8_t)transit_bits_be(b, 16, 60, 4); + z->seqno = (uint16_t)((b[8] << 8) | b[9]); + z->discount = b[10]; + + b = transit_mfc_block(dump, dumplen, ZK_TRIP_SECTOR, 1); + if (transit_is_zero(b, 16) == false) { + z->has_refill = true; + // high bits of the machine id are not on the card + z->refill_machine = (uint16_t)zk_le(b + 1, 2); + z->refill_time = zk_le(b + 3, 4); + z->refill_amount = zk_le(b + 7, 4); + z->refill_counter = (uint16_t)zk_le(b + 11, 2); + } + + b = transit_mfc_block(dump, dumplen, ZK_TRIP_SECTOR, 2); + if (transit_is_zero(b, 16) == false) { + z->has_trip = true; + z->trip_validator = ((uint32_t)b[2] << 16) | ((uint32_t)b[3] << 8) | b[4]; + z->trip_time = zk_le(b + 6, 4); + z->trip_track = b[10]; + z->trip_prev_balance = zk_le(b + 11, 4); + } +} + +// Sector 15 block 2 bytes 4..13 as hex, first 19 digits grouped 4 5 5 5 +static void zk_serial_str(char *out, size_t outlen, const uint8_t *b) { + char h[24]; + snprintf(h, sizeof(h), "%s", sprint_hex_inrow(b + 4, 10)); + snprintf(out, outlen, "%.4s %.5s %.5s %.5s", h, h + 4, h + 9, h + 14); +} + +static const char *zk_card_name(uint32_t type) { + for (size_t i = 0; i < ARRAYLEN(zk_cards); i++) { + if (zk_cards[i].type == type) { + return zk_cards[i].name; + } + } + return "unknown"; +} + +int zolotayakorona_parser_parse(const uint8_t *dump, size_t dumplen) { + if (is_valid_zolotayakorona_card(dump, dumplen) == false) { + return PM3_EINVARG; + } + + zk_t z; + zk_decode(dump, dumplen, &z); + + char buf[64]; + transit_print_header("Zolotaya Korona"); + transit_print_kv("Card type", "%06X ( %s )", z.type, zk_card_name(z.type)); + transit_print_kv("Region", "%02X", (z.type >> 16) & 0xFF); + zk_serial_str(buf, sizeof(buf), transit_mfc_block(dump, dumplen, ZK_INFO_SECTOR, 2)); + transit_print_kv("Card number", _YELLOW_("%s"), buf); + transit_print_kv("Card serial", "%s", sprint_hex_inrow(dump, 4)); + transit_money_str(buf, sizeof(buf), z.balance, 2, "RUB"); + transit_print_kv("Balance", _YELLOW_("%s"), buf); + transit_print_kv("Discount", "%02X", z.discount); + transit_print_kv("Status", "%u, issue seqno %u", z.status, z.seqno); + + if (z.has_trip && z.trip_time != 0) { + char prev[24]; + transit_datetime_str(buf, sizeof(buf), z.trip_time); + transit_money_str(prev, sizeof(prev), z.trip_prev_balance, 2, "RUB"); + transit_print_kv("Last trip", "%s validator %06X, track %u, balance before %s", buf, + z.trip_validator, z.trip_track, prev); + } + if (z.has_refill && z.refill_time != 0) { + char amount[24]; + transit_datetime_str(buf, sizeof(buf), z.refill_time); + transit_money_str(amount, sizeof(amount), z.refill_amount, 2, "RUB"); + transit_print_kv("Last refill", "%s %s, machine %u", buf, amount, z.refill_machine); + } + transit_print_kv("Refill counter", "%u", z.has_refill ? z.refill_counter : 0); + PrintAndLogEx(INFO, "Times are local time of the region"); + return PM3_SUCCESS; +} + +// Synthetic card built from the Metrodroid layout +int zolotayakorona_selftest(void) { + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Testing Zolotaya Korona decode"); + + uint8_t dump[1024] = {0}; + uint8_t *s15 = dump + (60 * 16); + memcpy(s15, zk_signature, sizeof(zk_signature)); + s15[16 + 10] = 0x76; + s15[16 + 11] = 0x05; + s15[16 + 12] = 0x00; + const uint8_t sn[] = { 0x96, 0x43, 0x07, 0x60, 0x50, 0x01, 0x23, 0x45, 0x67, 0x89 }; + memcpy(s15 + 32 + 4, sn, sizeof(sn)); + + const uint8_t purse[] = { 0x10, 0x27, 0x00, 0x00 }; + memcpy(dump + (24 * 16), purse, sizeof(purse)); + + uint8_t *s4 = dump + (16 * 16); + s4[7] = 0x03; + s4[8] = 0x00; + s4[9] = 0x05; + s4[10] = 0x47; + // refill of 200.00 at 1700000000, counter 3, machine 0x0102 + const uint8_t refill[] = { 0x00, 0x02, 0x01, 0x00, 0xF1, 0x53, 0x65, 0x20, 0x4E, 0x00, 0x00, 0x03, 0x00 }; + memcpy(s4 + 16, refill, sizeof(refill)); + // trip at 1700003600, validator 123456, balance before 115.00 + const uint8_t trip[] = { 0x00, 0x00, 0x12, 0x34, 0x56, 0x00, 0x10, 0xFF, 0x53, 0x65, 0x07, 0xEC, 0x2C, 0x00, 0x00 }; + memcpy(s4 + 32, trip, sizeof(trip)); + + bool ok = transit_check("detected by signature", is_valid_zolotayakorona_card(dump, sizeof(dump)), 1); + + zk_t z; + zk_decode(dump, sizeof(dump), &z); + ok &= transit_check("card type", z.type, 0x760500); + ok &= transit_check("balance", (uint64_t)z.balance, 10000); + ok &= transit_check("status", z.status, 3); + ok &= transit_check("issue seqno", z.seqno, 5); + ok &= transit_check("discount", z.discount, 0x47); + ok &= transit_check("refill time", z.refill_time, 1700000000); + ok &= transit_check("refill amount", z.refill_amount, 20000); + ok &= transit_check("refill counter", z.refill_counter, 3); + ok &= transit_check("refill machine", z.refill_machine, 0x0102); + ok &= transit_check("trip validator", z.trip_validator, 0x123456); + ok &= transit_check("trip time", z.trip_time, 1700003600); + ok &= transit_check("trip track", z.trip_track, 7); + ok &= transit_check("balance before", z.trip_prev_balance, 11500); + + char buf[32]; + zk_serial_str(buf, sizeof(buf), s15 + 32); + ok &= transit_check_str("card number", buf, "9643 07605 00123 45678"); + + s15[11] ^= 0x01; + ok &= transit_check("bad signature rejected", is_valid_zolotayakorona_card(dump, sizeof(dump)), 0); + dump[16 + 8] = ZK_MAD_AID_HI; + dump[16 + 9] = ZK_MAD_AID_LO; + dump[16 + 12] = ZK_MAD_AID_HI; + dump[16 + 13] = ZK_MAD_AID_LO; + ok &= transit_check("detected by MAD", is_valid_zolotayakorona_card(dump, sizeof(dump)), 1); + + return transit_selftest_result("Zolotaya Korona", ok); +} diff --git a/client/src/parsers/parsezolotayakorona.h b/client/src/parsers/parsezolotayakorona.h new file mode 100644 index 000000000..fb072645a --- /dev/null +++ b/client/src/parsers/parsezolotayakorona.h @@ -0,0 +1,33 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Zolotaya Korona transport card parser for MIFARE Classic dumps +// +// Regional Russian cards (Krasnodar, Samara, Orenburg, Yaroslavl, ...). +// Layout ported from Metrodroid (GPLv3) transit/zolotayakorona, the info +// sector signature from the Flipper Zero zolotaya_korona plugin (GPLv3). +//----------------------------------------------------------------------------- + +#ifndef PARSEZOLOTAYAKORONA_H__ +#define PARSEZOLOTAYAKORONA_H__ + +#include "common.h" + +// MAD entries for AID 18EE or the issuer name in sector 15 +bool is_valid_zolotayakorona_card(const uint8_t *dump, size_t dumplen); +int zolotayakorona_parser_parse(const uint8_t *dump, size_t dumplen); +int zolotayakorona_selftest(void); + +#endif