From 8b2f4f9cf6c84861a5a812467828806b9404959f Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 10 Nov 2026 09:44:34 +0700 Subject: [PATCH] transit parsers: FeliCa and ST25TB card systems FeliCa parsers run from hf felica dump over the services it read without authentication: Japan IC (Suica, PASMO, ICOCA, ...) Metrodroid transit/suica issuer named from its service codes Octopus, Shenzhen Tong Metrodroid transit/octopus The dump adapter now keeps locked services in the list, without blocks, since the Japan IC issuer naming depends on them. ST25TB parser, run from hf 14b view / dump on SRT512: Intertic (French networks) pyscripts/intertic.py, Metroflip Self tests: Metrodroid SuicaTest service sets and OctopusTest balances, synthetic history blocks and Intertic tickets. Co-Authored-By: Claude Opus 5.5 --- client/CMakeLists.txt | 3 + client/Makefile | 3 + client/experimental_lib/CMakeLists.txt | 3 + client/src/cmdhffelica.c | 24 +- client/src/parsers/parseintertic.c | 492 +++++++++++++++++++++++++ client/src/parsers/parseintertic.h | 40 ++ client/src/parsers/parseoctopus.c | 116 ++++++ client/src/parsers/parseoctopus.h | 41 +++ client/src/parsers/parsesuica.c | 492 +++++++++++++++++++++++++ client/src/parsers/parsesuica.h | 42 +++ client/src/parsers/parsetransit.c | 6 + client/src/parsers/parsetransit.h | 2 +- 12 files changed, 1251 insertions(+), 13 deletions(-) create mode 100644 client/src/parsers/parseintertic.c create mode 100644 client/src/parsers/parseintertic.h create mode 100644 client/src/parsers/parseoctopus.c create mode 100644 client/src/parsers/parseoctopus.h create mode 100644 client/src/parsers/parsesuica.c create mode 100644 client/src/parsers/parsesuica.h diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 6d6c8358c..18aef6274 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -521,16 +521,19 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/parsers/parsebip.c ${PM3_ROOT}/client/src/parsers/parsecharlie.c ${PM3_ROOT}/client/src/parsers/parseclipper.c + ${PM3_ROOT}/client/src/parsers/parseintertic.c ${PM3_ROOT}/client/src/parsers/parseitso.c ${PM3_ROOT}/client/src/parsers/parsekazan.c ${PM3_ROOT}/client/src/parsers/parsemetromoney.c ${PM3_ROOT}/client/src/parsers/parsemyki.c ${PM3_ROOT}/client/src/parsers/parsenextfare.c ${PM3_ROOT}/client/src/parsers/parsenol.c + ${PM3_ROOT}/client/src/parsers/parseoctopus.c ${PM3_ROOT}/client/src/parsers/parseopal.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/parsesuica.c ${PM3_ROOT}/client/src/parsers/parsetmobilitat.c ${PM3_ROOT}/client/src/parsers/parsetroika.c ${PM3_ROOT}/client/src/parsers/parsetrt.c diff --git a/client/Makefile b/client/Makefile index 0ce9bd4f1..089f6b4b8 100644 --- a/client/Makefile +++ b/client/Makefile @@ -928,16 +928,19 @@ SRCS = mifare/aiddesfire.c \ parsers/parsebip.c \ parsers/parsecharlie.c \ parsers/parseclipper.c \ + parsers/parseintertic.c \ parsers/parseitso.c \ parsers/parsekazan.c \ parsers/parsemetromoney.c \ parsers/parsemyki.c \ parsers/parsenextfare.c \ parsers/parsenol.c \ + parsers/parseoctopus.c \ parsers/parseopal.c \ parsers/parseplantain.c \ parsers/parserenfe.c \ parsers/parsesmartrider.c \ + parsers/parsesuica.c \ parsers/parsetmobilitat.c \ parsers/parsetroika.c \ parsers/parsetrt.c \ diff --git a/client/experimental_lib/CMakeLists.txt b/client/experimental_lib/CMakeLists.txt index 74868dd5c..098a4a3cf 100644 --- a/client/experimental_lib/CMakeLists.txt +++ b/client/experimental_lib/CMakeLists.txt @@ -448,16 +448,19 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/parsers/parsebip.c ${PM3_ROOT}/client/src/parsers/parsecharlie.c ${PM3_ROOT}/client/src/parsers/parseclipper.c + ${PM3_ROOT}/client/src/parsers/parseintertic.c ${PM3_ROOT}/client/src/parsers/parseitso.c ${PM3_ROOT}/client/src/parsers/parsekazan.c ${PM3_ROOT}/client/src/parsers/parsemetromoney.c ${PM3_ROOT}/client/src/parsers/parsemyki.c ${PM3_ROOT}/client/src/parsers/parsenextfare.c ${PM3_ROOT}/client/src/parsers/parsenol.c + ${PM3_ROOT}/client/src/parsers/parseoctopus.c ${PM3_ROOT}/client/src/parsers/parseopal.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/parsesuica.c ${PM3_ROOT}/client/src/parsers/parsetmobilitat.c ${PM3_ROOT}/client/src/parsers/parsetroika.c ${PM3_ROOT}/client/src/parsers/parsetrt.c diff --git a/client/src/cmdhffelica.c b/client/src/cmdhffelica.c index b915a7a7c..70b2e7c8d 100644 --- a/client/src/cmdhffelica.c +++ b/client/src/cmdhffelica.c @@ -5453,25 +5453,25 @@ static void felica_dump_run_transit_parsers(const felica_dump_system_t *systems, size_t n = 0; for (size_t j = 0; j < sys->node_count; j++) { const felica_dump_node_t *node = &sys->nodes[j]; - if (node->type != FELICA_NODE_TYPE_SERVICE || node->block_count == 0) { + if (node->type != FELICA_NODE_TYPE_SERVICE) { continue; } - uint16_t blocks = 0; + // locked services stay listed without blocks, some parsers name the issuer by them + svc[n].code = node->node_code_le; + uint32_t blocks = 0; for (size_t k = 0; k < node->block_count; k++) { if (node->blocks[k].block_number >= blocks) { - blocks = node->blocks[k].block_number + 1; + blocks = (uint32_t)node->blocks[k].block_number + 1; } } - uint8_t *buf = calloc(blocks, FELICA_BLK_SIZE); - if (buf == NULL) { - continue; + uint8_t *buf = (blocks > 0) ? calloc(blocks, FELICA_BLK_SIZE) : NULL; + if (buf != NULL) { + for (size_t k = 0; k < node->block_count; k++) { + memcpy(buf + ((size_t)node->blocks[k].block_number * FELICA_BLK_SIZE), node->blocks[k].data, FELICA_BLK_SIZE); + } + svc[n].block_count = (uint16_t)blocks; + svc[n].blocks = buf; } - for (size_t k = 0; k < node->block_count; k++) { - memcpy(buf + (node->blocks[k].block_number * FELICA_BLK_SIZE), node->blocks[k].data, FELICA_BLK_SIZE); - } - svc[n].code = node->node_code_le; - svc[n].block_count = blocks; - svc[n].blocks = buf; n++; } tsys[tcount].system_code = sys->system_code; diff --git a/client/src/parsers/parseintertic.c b/client/src/parsers/parseintertic.c new file mode 100644 index 000000000..f9ea72ffe --- /dev/null +++ b/client/src/parsers/parseintertic.c @@ -0,0 +1,492 @@ +//----------------------------------------------------------------------------- +// 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. +//----------------------------------------------------------------------------- +// Intertic parser for ST25TB / SRT512 dumps +//----------------------------------------------------------------------------- + +#include "parseintertic.h" + +#include + +#include "commonutil.h" // ARRAYLEN +#include "ui.h" // PrintAndLogEx +#include "parsetransit.h" + +#define INTERTIC_STREAM_LEN 64 // blocks 0..15, the system block is not used +#define INTERTIC_PART_LEN 24 + +#define INTERTIC_COUNTRY_FRANCE 0x250 +#define INTERTIC_COUNTRY_MOROCCO 0x504 + +//----------------------------------------------------------------------------- +// Usage record layouts, one per network family, offsets in bits after the +// 10 bit date and 11 bit time. -1 when the layout has no such field. +//----------------------------------------------------------------------------- + +typedef enum { + IT_USAGE_GENERIC = 0, + IT_USAGE_1, + IT_USAGE_1_1, + IT_USAGE_1_2, + IT_USAGE_2, + IT_USAGE_2_1, + IT_USAGE_2_2, + IT_USAGE_3, + IT_USAGE_4, +} intertic_usage_t; + +typedef struct { + int16_t nature_off; + uint8_t nature_len; + int16_t type_off; + uint8_t type_len; + int16_t vehicle_off; // 16 bits + int16_t route_off; // 14 bits + int16_t dir_off; // 2 bits + int16_t pass_off; // 4 bits + int16_t first_off; // 11 bits +} intertic_layout_t; + +// indexed by intertic_usage_t, generic falls back to layout 1 +static const intertic_layout_t intertic_layouts[] = { + { -1, 0, -1, 0, -1, -1, -1, -1, 86 }, + { -1, 0, -1, 0, -1, -1, -1, -1, 86 }, + { 29, 5, 34, 5, 50, 66, 80, 82, 86 }, + { 31, 4, 35, 4, 50, 66, 80, 82, 86 }, + { 29, 5, 34, 5, -1, 50, 64, 66, 70 }, + { 29, 5, 34, 5, 74, 58, 72, 90, 94 }, + { 29, 5, 34, 5, 66, 50, 64, 100, 86 }, + { -1, 0, -1, 0, -1, -1, -1, -1, 48 }, + { -1, 0, -1, 0, -1, -1, -1, -1, 84 }, +}; + +typedef struct { + uint16_t country; + uint16_t authority; + uint8_t provider; + const char *city; + const char *company; + intertic_usage_t usage; +} intertic_network_t; + +static const intertic_network_t intertic_networks[] = { + { INTERTIC_COUNTRY_FRANCE, 0x000, 1, "Valenciennes", "Transvilles / Keolis", IT_USAGE_1_1 }, + { INTERTIC_COUNTRY_FRANCE, 0x000, 5, "Lille", "Ilevia / Keolis", IT_USAGE_1_1 }, + { INTERTIC_COUNTRY_FRANCE, 0x000, 7, "Lens-Bethune", "Tadao / Transdev", IT_USAGE_1_1 }, + { INTERTIC_COUNTRY_FRANCE, 0x006, 1, "Amiens", "Ametis / Keolis", IT_USAGE_GENERIC }, + { INTERTIC_COUNTRY_FRANCE, 0x008, 15, "Angouleme", "STGA", IT_USAGE_1_1 }, + { INTERTIC_COUNTRY_FRANCE, 0x013, 1, "Avignon", "Orizo", IT_USAGE_GENERIC }, + { INTERTIC_COUNTRY_FRANCE, 0x021, 1, "Bordeaux", "TBM / Keolis", IT_USAGE_1_1 }, + { INTERTIC_COUNTRY_FRANCE, 0x040, 28, "Colmar", "Trace / Keolis", IT_USAGE_1_1 }, + { INTERTIC_COUNTRY_FRANCE, 0x057, 1, "Lyon", "TCL / Keolis", IT_USAGE_1 }, + { INTERTIC_COUNTRY_FRANCE, 0x072, 1, "Tours", "filbleu / Keolis", IT_USAGE_1_1 }, + { INTERTIC_COUNTRY_FRANCE, 0x078, 4, "Reims", "Citura / Transdev", IT_USAGE_1_2 }, + { INTERTIC_COUNTRY_FRANCE, 0x091, 1, "Strasbourg", "CTS", IT_USAGE_4 }, + { INTERTIC_COUNTRY_FRANCE, 0x091, 5, "Strasbourg", "CTS / new", IT_USAGE_4 }, + { INTERTIC_COUNTRY_FRANCE, 0x502, 83, "Annecy", "Sibra", IT_USAGE_2 }, + { INTERTIC_COUNTRY_FRANCE, 0x502, 84, "Bourg-en-Bresse", "Rubis / Keolis", IT_USAGE_GENERIC }, + { INTERTIC_COUNTRY_FRANCE, 0x502, 10, "Clermont-Ferrand", "T2C", IT_USAGE_2_2 }, + { INTERTIC_COUNTRY_FRANCE, 0x907, 1, "Dijon", "Divia / Keolis", IT_USAGE_GENERIC }, + { INTERTIC_COUNTRY_FRANCE, 0x908, 1, "Rennes", "STAR / Keolis", IT_USAGE_2 }, + { INTERTIC_COUNTRY_FRANCE, 0x908, 8, "Saint-Malo", "MAT / RATP", IT_USAGE_1_1 }, + { INTERTIC_COUNTRY_FRANCE, 0x911, 5, "Besancon", "Ginko / Keolis", IT_USAGE_GENERIC }, + { INTERTIC_COUNTRY_FRANCE, 0x912, 3, "Le Havre", "Lia / Transdev", IT_USAGE_1_1 }, + { INTERTIC_COUNTRY_FRANCE, 0x912, 29, "Caen", "Twisto / RATP", IT_USAGE_2 }, + { INTERTIC_COUNTRY_FRANCE, 0x912, 35, "Cherbourg-en-Cotentin", "Cap Cotentin / Transdev", IT_USAGE_GENERIC }, + { INTERTIC_COUNTRY_FRANCE, 0x913, 3, "Nimes", "Tango / Transdev", IT_USAGE_3 }, + { INTERTIC_COUNTRY_FRANCE, 0x915, 1, "Metz", "Le Met' / TAMM", IT_USAGE_GENERIC }, + { INTERTIC_COUNTRY_FRANCE, 0x917, 4, "Angers", "Irigo / RATP", IT_USAGE_1_2 }, + { INTERTIC_COUNTRY_FRANCE, 0x917, 7, "Saint-Nazaire", "Stran", IT_USAGE_GENERIC }, + { INTERTIC_COUNTRY_FRANCE, 0x920, 9, "Aix-en-Provence", "Aixenbus / Keolis", IT_USAGE_2_1 }, + { INTERTIC_COUNTRY_MOROCCO, 0x001, 1, "Casablanca", "Casa Transports / RATP", IT_USAGE_2_1 }, +}; + +static const intertic_network_t *intertic_find_network(uint16_t country, uint16_t authority, uint8_t provider) { + for (size_t i = 0; i < ARRAYLEN(intertic_networks); i++) { + const intertic_network_t *n = &intertic_networks[i]; + if (n->country == country && n->authority == authority && n->provider == provider) { + return n; + } + } + return NULL; +} + +static const char *intertic_nature_name(uint8_t v, bool reims) { + if (reims) { + // Reims swaps these two + if (v == 0x4) { + return "urban bus"; + } + if (v == 0x1) { + return "tramway"; + } + return "?"; + } + switch (v) { + case 0x1: + return "urban bus"; + case 0x2: + return "interurban bus"; + case 0x3: + return "metro"; + case 0x4: + return "tramway"; + case 0x5: + return "train"; + case 0x8: + return "parking"; + default: + return "?"; + } +} + +static const char *intertic_type_name(uint8_t v) { + switch (v) { + case 0x01: + return "entry validation"; + case 0x02: + return "exit validation"; + case 0x04: + return "ticket inspecting"; + case 0x06: + return "connection entry validation"; + case 0x14: + return "test validation"; + case 0x15: + return "connection exit validation"; + case 0x16: + return "canceled validation"; + case 0x17: + return "invalidation"; + case 0x18: + return "distribution"; + default: + return "?"; + } +} + +static const char *intertic_direction_name(uint8_t v) { + static const char *const names[] = { "undefined", "outward", "inward", "circular" }; + return names[v & 3]; +} + +//----------------------------------------------------------------------------- +// Ticket layout. Each block is read least significant byte first, and the +// blocks then form one most significant bit first stream. +//----------------------------------------------------------------------------- + +typedef struct { + uint8_t key_id; + uint8_t pid; + uint8_t dist[INTERTIC_PART_LEN]; + uint8_t usage_a[INTERTIC_PART_LEN]; + uint8_t usage_b[INTERTIC_PART_LEN]; + uint16_t usage_bits; + uint8_t reloading; + uint32_t counter; + uint32_t swap; + uint16_t cer_a; + uint16_t cer_b; + uint32_t cer_dist; +} intertic_ticket_t; + +typedef struct { + uint16_t country; + uint16_t authority; + uint8_t version; + uint8_t provider; + uint16_t tariff; + uint16_t end_date; +} intertic_dist_t; + +static void intertic_stream(const uint8_t *data, uint8_t *stream) { + for (size_t b = 0; b < INTERTIC_STREAM_LEN; b += 4) { + stream[b + 0] = data[b + 3]; + stream[b + 1] = data[b + 2]; + stream[b + 2] = data[b + 1]; + stream[b + 3] = data[b + 0]; + } +} + +static void intertic_copy(uint8_t *dst, uint16_t *dst_off, const uint8_t *stream, uint16_t off, uint16_t n) { + for (uint16_t k = 0; k < n; k++) { + uint64_t bit = transit_bits_be(stream, INTERTIC_STREAM_LEN, off + k, 1); + transit_set_bits_be(dst, INTERTIC_PART_LEN, *dst_off + k, 1, bit); + } + *dst_off += n; +} + +static bool intertic_pid_known(uint8_t pid) { + return (pid == 0x10 || pid == 0x11 || pid == 0x19); +} + +static bool intertic_split(const uint8_t *stream, intertic_ticket_t *t) { + memset(t, 0, sizeof(*t)); + t->key_id = (uint8_t)transit_bits_be(stream, INTERTIC_STREAM_LEN, 23, 4); + t->pid = (uint8_t)transit_bits_be(stream, INTERTIC_STREAM_LEN, 27, 5); + if (intertic_pid_known(t->pid) == false) { + return false; + } + + uint16_t d = 0, a = 0, b = 0; + if (t->pid == 0x10) { + intertic_copy(t->dist, &d, stream, 32, 64); + intertic_copy(t->dist, &d, stream, 0, 23); + intertic_copy(t->usage_a, &a, stream, 96, 64); + intertic_copy(t->usage_a, &a, stream, 224, 80); + intertic_copy(t->usage_b, &b, stream, 320, 144); + t->cer_a = (uint16_t)transit_bits_be(stream, INTERTIC_STREAM_LEN, 304, 16); + } else { + intertic_copy(t->dist, &d, stream, 32, 128); + intertic_copy(t->dist, &d, stream, 0, 23); + intertic_copy(t->usage_a, &a, stream, 224, 112); + intertic_copy(t->usage_b, &b, stream, 352, 112); + t->cer_a = (uint16_t)transit_bits_be(stream, INTERTIC_STREAM_LEN, 336, 16); + } + t->usage_bits = a; + t->reloading = (uint8_t)transit_bits_be(stream, INTERTIC_STREAM_LEN, 160, 8); + t->counter = (uint32_t)transit_bits_be(stream, INTERTIC_STREAM_LEN, 168, 24); + t->swap = (uint32_t)transit_bits_be(stream, INTERTIC_STREAM_LEN, 192, 32); + t->cer_b = (uint16_t)transit_bits_be(stream, INTERTIC_STREAM_LEN, 464, 16); + t->cer_dist = (uint32_t)transit_bits_be(stream, INTERTIC_STREAM_LEN, 480, 32); + return true; +} + +static void intertic_read_dist(const uint8_t *p, intertic_dist_t *d) { + d->country = (uint16_t)transit_bits_be(p, INTERTIC_PART_LEN, 0, 12); + d->authority = (uint16_t)transit_bits_be(p, INTERTIC_PART_LEN, 12, 12); + d->version = (uint8_t)transit_bits_be(p, INTERTIC_PART_LEN, 24, 6); + d->provider = (uint8_t)transit_bits_be(p, INTERTIC_PART_LEN, 30, 8); + d->tariff = (uint16_t)transit_bits_be(p, INTERTIC_PART_LEN, 38, 16); + d->end_date = (uint16_t)transit_bits_be(p, INTERTIC_PART_LEN, 54, 14); +} + +static bool intertic_decode(const uint8_t *data, size_t datalen, intertic_ticket_t *t, intertic_dist_t *d) { + if (data == NULL || datalen != INTERTIC_DUMP_SIZE) { + return false; + } + uint8_t stream[INTERTIC_STREAM_LEN]; + intertic_stream(data, stream); + if (intertic_split(stream, t) == false) { + return false; + } + intertic_read_dist(t->dist, d); + return true; +} + +//----------------------------------------------------------------------------- +// Detection and printing +//----------------------------------------------------------------------------- + +bool is_valid_intertic_card(const uint8_t *data, size_t datalen, const uint8_t *uid) { + (void)uid; + intertic_ticket_t t; + intertic_dist_t d; + if (intertic_decode(data, datalen, &t, &d) == false) { + return false; + } + return (d.country == INTERTIC_COUNTRY_FRANCE || d.country == INTERTIC_COUNTRY_MOROCCO); +} + +static void intertic_print_usage(const char *title, const uint8_t *u, uint16_t cer, uint16_t end_date, + intertic_usage_t usage, bool reims) { + + if (transit_is_zero(u, INTERTIC_PART_LEN)) { + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "%s ( empty )", title); + return; + } + + const intertic_layout_t *l = &intertic_layouts[usage]; + uint16_t day = (uint16_t)transit_bits_be(u, INTERTIC_PART_LEN, 0, 10); + uint16_t time = (uint16_t)transit_bits_be(u, INTERTIC_PART_LEN, 10, 11); + + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "%s", title); + + // dated backwards from the end of validity + char s[24]; + transit_date_str(s, sizeof(s), TRANSIT_DAYS_TO_1997 + (int32_t)end_date - day); + transit_print_kv(" Date", "%s %02u:%02u", s, time / 60, time % 60); + + if (l->nature_off >= 0) { + uint8_t v = (uint8_t)transit_bits_be(u, INTERTIC_PART_LEN, l->nature_off, l->nature_len); + transit_print_kv(" Nature", "0x%X ( %s )", v, intertic_nature_name(v, reims)); + } + if (l->type_off >= 0) { + uint8_t v = (uint8_t)transit_bits_be(u, INTERTIC_PART_LEN, l->type_off, l->type_len); + transit_print_kv(" Type", "0x%X ( %s )", v, intertic_type_name(v)); + } + if (l->route_off >= 0) { + transit_print_kv(" Route", "%u", (unsigned)transit_bits_be(u, INTERTIC_PART_LEN, l->route_off, 14)); + } + if (l->dir_off >= 0) { + transit_print_kv(" Direction", "%s", intertic_direction_name((uint8_t)transit_bits_be(u, INTERTIC_PART_LEN, l->dir_off, 2))); + } + if (l->vehicle_off >= 0) { + transit_print_kv(" Vehicle", "%u", (unsigned)transit_bits_be(u, INTERTIC_PART_LEN, l->vehicle_off, 16)); + } + if (l->pass_off >= 0) { + transit_print_kv(" Passengers (?)", "%u", (unsigned)transit_bits_be(u, INTERTIC_PART_LEN, l->pass_off, 4)); + } + uint16_t first = (uint16_t)transit_bits_be(u, INTERTIC_PART_LEN, l->first_off, 11); + transit_print_kv(" First valid.", "%02u:%02u", first / 60, first % 60); + transit_print_kv(" Certificate", "%04X", cer); +} + +int intertic_parser_parse(const uint8_t *data, size_t datalen, const uint8_t *uid) { + + if (is_valid_intertic_card(data, datalen, uid) == false) { + return PM3_EINVARG; + } + + intertic_ticket_t t; + intertic_dist_t d; + (void)intertic_decode(data, datalen, &t, &d); + const intertic_network_t *n = intertic_find_network(d.country, d.authority, d.provider); + + transit_print_header("Intertic transport ticket"); + transit_print_kv("Country", "%03X ( %s )", d.country, (d.country == INTERTIC_COUNTRY_FRANCE) ? "France" : "Morocco"); + if (n != NULL) { + transit_print_kv("Network", _YELLOW_("%s") " ( %s )", n->city, n->company); + } else { + transit_print_kv("Network", "unknown"); + } + transit_print_kv("Authority", "%03X", d.authority); + transit_print_kv("Provider", "%u", d.provider); + transit_print_kv("Product id", "0x%02X, key id %u", t.pid, t.key_id); + transit_print_kv("App version", "%u", d.version); + transit_print_kv("Tariff", "%u", d.tariff); + + char s[24]; + transit_date_str(s, sizeof(s), TRANSIT_DAYS_TO_1997 + d.end_date); + transit_print_kv("Valid until", _YELLOW_("%s"), s); + transit_print_kv("Counter", _GREEN_("%u") " ( 0x%06X ), reloading 0x%02X", t.counter, t.counter, t.reloading); + transit_print_kv("Last usage", "%c ( swap 0x%08X )", (t.swap & 1) ? 'B' : 'A', t.swap); + transit_print_kv("Certificate", "%08X", t.cer_dist); + + intertic_usage_t usage = (n != NULL) ? n->usage : IT_USAGE_GENERIC; + if (usage == IT_USAGE_GENERIC) { + PrintAndLogEx(INFO, "Usage layout unknown for this network, decoded as the most common one"); + } + bool reims = (usage == IT_USAGE_1_2 && n != NULL && n->authority == 0x078); + intertic_print_usage("Usage A", t.usage_a, t.cer_a, d.end_date, usage, reims); + intertic_print_usage("Usage B", t.usage_b, t.cer_b, d.end_date, usage, reims); + return PM3_SUCCESS; +} + +//----------------------------------------------------------------------------- +// Self test +//----------------------------------------------------------------------------- + +static void intertic_stream_to_card(const uint8_t *stream, uint8_t *card) { + memset(card, 0, INTERTIC_DUMP_SIZE); + intertic_stream(stream, card); + // system block, OTP lock bytes as delivered + card[64] = 0xFF; + card[65] = 0xFF; + card[66] = 0xFF; + card[67] = 0xFF; +} + +int intertic_selftest(void) { + + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Testing Intertic decode"); + + bool ok = true; + + // PID 11, Rennes STAR: distribution fills 32..159 and then block 0 bits 0..22 + uint8_t s[INTERTIC_STREAM_LEN] = {0}; + transit_set_bits_be(s, sizeof(s), 23, 4, 0x3); + transit_set_bits_be(s, sizeof(s), 27, 5, 0x11); + transit_set_bits_be(s, sizeof(s), 32, 12, INTERTIC_COUNTRY_FRANCE); + transit_set_bits_be(s, sizeof(s), 44, 12, 0x908); + transit_set_bits_be(s, sizeof(s), 56, 6, 2); + transit_set_bits_be(s, sizeof(s), 62, 8, 1); + transit_set_bits_be(s, sizeof(s), 70, 16, 1234); + transit_set_bits_be(s, sizeof(s), 86, 14, 10000); + transit_set_bits_be(s, sizeof(s), 160, 8, 0x01); + transit_set_bits_be(s, sizeof(s), 168, 24, 9); + transit_set_bits_be(s, sizeof(s), 192, 32, 0x00000001); + // usage A at 224: day 3 before the end, 08:30, metro entry, route 42, inward + transit_set_bits_be(s, sizeof(s), 224 + 0, 10, 3); + transit_set_bits_be(s, sizeof(s), 224 + 10, 11, 510); + transit_set_bits_be(s, sizeof(s), 224 + 29, 5, 0x3); + transit_set_bits_be(s, sizeof(s), 224 + 34, 5, 0x1); + transit_set_bits_be(s, sizeof(s), 224 + 50, 14, 42); + transit_set_bits_be(s, sizeof(s), 224 + 64, 2, 2); + transit_set_bits_be(s, sizeof(s), 224 + 70, 11, 505); + transit_set_bits_be(s, sizeof(s), 336, 16, 0xBEEF); + transit_set_bits_be(s, sizeof(s), 480, 32, 0xCAFEF00D); + + uint8_t card[INTERTIC_DUMP_SIZE]; + intertic_stream_to_card(s, card); + + // PID sits in the low bits of the first byte as the card returns it + ok &= transit_check("card byte 0 holds the PID", card[0] & 0x1F, 0x11); + + intertic_ticket_t t; + intertic_dist_t d; + ok &= transit_check("decodes", intertic_decode(card, sizeof(card), &t, &d), 1); + ok &= transit_check("key id", t.key_id, 3); + ok &= transit_check("country", d.country, INTERTIC_COUNTRY_FRANCE); + ok &= transit_check("authority", d.authority, 0x908); + ok &= transit_check("version", d.version, 2); + ok &= transit_check("provider", d.provider, 1); + ok &= transit_check("tariff", d.tariff, 1234); + ok &= transit_check("end date", d.end_date, 10000); + ok &= transit_check("counter", t.counter, 9); + ok &= transit_check("usage A certificate", t.cer_a, 0xBEEF); + ok &= transit_check("distribution certificate", t.cer_dist, 0xCAFEF00D); + ok &= transit_check("usage A day", transit_bits_be(t.usage_a, INTERTIC_PART_LEN, 0, 10), 3); + ok &= transit_check("usage A route", transit_bits_be(t.usage_a, INTERTIC_PART_LEN, intertic_layouts[IT_USAGE_2].route_off, 14), 42); + + char str[24]; + transit_date_str(str, sizeof(str), TRANSIT_DAYS_TO_1997 + d.end_date); + ok &= transit_check_str("end date string", str, "2024-05-19"); + + const intertic_network_t *n = intertic_find_network(d.country, d.authority, d.provider); + ok &= transit_check_str("network", n ? n->city : NULL, "Rennes"); + ok &= transit_check("detected", is_valid_intertic_card(card, sizeof(card), NULL), 1); + ok &= transit_check("parse", (uint64_t)intertic_parser_parse(card, sizeof(card), NULL), PM3_SUCCESS); + + // PID 10, distribution runs 32..95 and on into block 0, usage A is split around the counter + memset(s, 0, sizeof(s)); + transit_set_bits_be(s, sizeof(s), 27, 5, 0x10); + transit_set_bits_be(s, sizeof(s), 32, 12, INTERTIC_COUNTRY_FRANCE); + transit_set_bits_be(s, sizeof(s), 44, 12, 0x000); + transit_set_bits_be(s, sizeof(s), 62, 8, 5); + transit_set_bits_be(s, sizeof(s), 86, 10, 0x2AA); // end date top 10 bits + transit_set_bits_be(s, sizeof(s), 0, 4, 0xB); // end date low 4 bits, from block 0 + transit_set_bits_be(s, sizeof(s), 96 + 50, 14, 0x1555); // usage A vehicle top 14 bits + transit_set_bits_be(s, sizeof(s), 224, 2, 0x2); // and its last 2 + intertic_stream_to_card(s, card); + ok &= transit_check("pid 10 decodes", intertic_decode(card, sizeof(card), &t, &d), 1); + ok &= transit_check("pid 10 end date across block 0", d.end_date, (0x2AA << 4) | 0xB); + ok &= transit_check("pid 10 usage A across counter", transit_bits_be(t.usage_a, INTERTIC_PART_LEN, 50, 16), (0x1555 << 2) | 0x2); + ok &= transit_check("pid 10 usage length", t.usage_bits, 144); + n = intertic_find_network(d.country, d.authority, d.provider); + ok &= transit_check_str("pid 10 network", n ? n->city : NULL, "Lille"); + + // wrong size, unknown PID and blank tickets are rejected + ok &= transit_check("SRIX4K size rejected", is_valid_intertic_card(card, 516, NULL), 0); + card[0] = (uint8_t)((card[0] & 0xE0) | 0x02); + ok &= transit_check("unknown pid rejected", is_valid_intertic_card(card, sizeof(card), NULL), 0); + memset(card, 0xFF, sizeof(card)); + ok &= transit_check("blank rejected", is_valid_intertic_card(card, sizeof(card), NULL), 0); + + return transit_selftest_result("Intertic", ok); +} diff --git a/client/src/parsers/parseintertic.h b/client/src/parsers/parseintertic.h new file mode 100644 index 000000000..2f2a37b76 --- /dev/null +++ b/client/src/parsers/parseintertic.h @@ -0,0 +1,40 @@ +//----------------------------------------------------------------------------- +// 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. +//----------------------------------------------------------------------------- +// Intertic parser for ST25TB / SRT512 dumps +// +// French and Moroccan disposable transport tickets. Ported from +// client/pyscripts/intertic.py by Benjamin Delpy and the Metroflip port +// (GPLv3). The usage records are reverse engineered, not specified. +//----------------------------------------------------------------------------- + +#ifndef PARSEINTERTIC_H__ +#define PARSEINTERTIC_H__ + +#include "common.h" + +// 16 blocks of 4 bytes plus the system block, as `hf 14b dump` stores SRT512 +#define INTERTIC_DUMP_SIZE 68 + +// True for a known product id with a France or Morocco network id +bool is_valid_intertic_card(const uint8_t *data, size_t datalen, const uint8_t *uid); + +// Print distribution data, counter and both usage records +int intertic_parser_parse(const uint8_t *data, size_t datalen, const uint8_t *uid); + +// Build tickets from the field layout and check the decoder reads them back +int intertic_selftest(void); + +#endif diff --git a/client/src/parsers/parseoctopus.c b/client/src/parsers/parseoctopus.c new file mode 100644 index 000000000..2ea4928c3 --- /dev/null +++ b/client/src/parsers/parseoctopus.c @@ -0,0 +1,116 @@ +//----------------------------------------------------------------------------- +// 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. +//----------------------------------------------------------------------------- +// Octopus (Hong Kong) and Shenzhen Tong parser for FeliCa dumps +//----------------------------------------------------------------------------- + +#include "parseoctopus.h" + +#include + +#include "commonutil.h" // MemBeToUint4byte, ARRAYLEN +#include "ui.h" // PrintAndLogEx +#include "util.h" // sprint_hex_inrow + +// The stored value is in HK$0.1 plus an offset that allows a negative balance. +// The offset went from 350 to 500 on 2017-10-01. A dump carries no scan date, +// so the current offset is used and the old one shown alongside. +#define OCTOPUS_OFFSET_1997 350 +#define OCTOPUS_OFFSET_2017 500 +#define SZT_OFFSET 350 + +// cents +static int64_t octopus_balance(const uint8_t *block, int32_t offset) { + return ((int64_t)MemBeToUint4byte(block) - offset) * 10; +} + +static const uint8_t *octopus_block(const transit_felica_card_t *card, uint16_t system, uint16_t service) { + const transit_felica_service_t *s = transit_felica_find_service(transit_felica_find_system(card, system), service); + return (s != NULL && s->block_count > 0) ? s->blocks : NULL; +} + +bool is_valid_octopus_card(const transit_felica_card_t *card) { + return (octopus_block(card, OCTOPUS_SYSTEM_CODE, OCTOPUS_SERVICE_BALANCE) != NULL) || + (octopus_block(card, SZT_SYSTEM_CODE, SZT_SERVICE_BALANCE) != NULL); +} + +int octopus_parser_parse(const transit_felica_card_t *card) { + + if (is_valid_octopus_card(card) == false) { + return PM3_EINVARG; + } + + const uint8_t *hk = octopus_block(card, OCTOPUS_SYSTEM_CODE, OCTOPUS_SERVICE_BALANCE); + const uint8_t *sz = octopus_block(card, SZT_SYSTEM_CODE, SZT_SERVICE_BALANCE); + + const char *name = "Octopus"; + if (hk != NULL && sz != NULL) { + name = "Octopus / Shenzhen Tong"; + } else if (hk == NULL) { + name = "Shenzhen Tong"; + } + transit_print_header(name); + + const transit_felica_system_t *sys = transit_felica_find_system(card, (hk != NULL) ? OCTOPUS_SYSTEM_CODE : SZT_SYSTEM_CODE); + transit_print_kv("IDm", "%s", sprint_hex_inrow(sys->idm, sizeof(sys->idm))); + + char s[32]; + if (hk != NULL) { + transit_money_str(s, sizeof(s), octopus_balance(hk, OCTOPUS_OFFSET_2017), 2, "HKD"); + transit_print_kv("Balance", _GREEN_("%s"), s); + transit_money_str(s, sizeof(s), octopus_balance(hk, OCTOPUS_OFFSET_1997), 2, "HKD"); + transit_print_kv("Pre 2017-10", "%s ( old negative balance offset )", s); + transit_print_kv("Raw", "%s", sprint_hex_inrow(hk, 16)); + } + if (sz != NULL) { + transit_money_str(s, sizeof(s), octopus_balance(sz, SZT_OFFSET), 2, "CNY"); + transit_print_kv("SZT balance", _GREEN_("%s"), s); + transit_print_kv("SZT raw", "%s", sprint_hex_inrow(sz, 16)); + } + return PM3_SUCCESS; +} + +int octopus_selftest(void) { + + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Testing Octopus decode"); + + // Metrodroid OctopusTest, HKD -14.40 after the 2017 change and -1.20 before it + const uint8_t c2018[16] = { 0x00, 0x00, 0x01, 0x64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x21 }; + const uint8_t c2016[16] = { 0x00, 0x00, 0x01, 0x52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x86, 0xB1 }; + + bool ok = true; + ok &= transit_check("2018 card, offset 500", (uint64_t)octopus_balance(c2018, OCTOPUS_OFFSET_2017), (uint64_t) -1440); + ok &= transit_check("2016 card, offset 350", (uint64_t)octopus_balance(c2016, OCTOPUS_OFFSET_1997), (uint64_t) -120); + + char s[32]; + transit_money_str(s, sizeof(s), octopus_balance(c2018, OCTOPUS_OFFSET_2017), 2, "HKD"); + ok &= transit_check_str("formatted", s, "-14.40 HKD"); + + const uint8_t empty[16] = {0}; + const transit_felica_service_t svc[] = { + { OCTOPUS_SERVICE_BALANCE, 1, c2018 }, + { 0x100B, 1, empty }, + }; + transit_felica_system_t sys = { .system_code = OCTOPUS_SYSTEM_CODE, .service_count = ARRAYLEN(svc), .services = svc }; + transit_felica_card_t card = { .system_count = 1, .systems = &sys }; + ok &= transit_check("detected", is_valid_octopus_card(&card), 1); + ok &= transit_check("parse", (uint64_t)octopus_parser_parse(&card), PM3_SUCCESS); + + sys.system_code = 0x0003; + ok &= transit_check("other system not detected", is_valid_octopus_card(&card), 0); + + return transit_selftest_result("Octopus", ok); +} diff --git a/client/src/parsers/parseoctopus.h b/client/src/parsers/parseoctopus.h new file mode 100644 index 000000000..b09cd6f0d --- /dev/null +++ b/client/src/parsers/parseoctopus.h @@ -0,0 +1,41 @@ +//----------------------------------------------------------------------------- +// 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. +//----------------------------------------------------------------------------- +// Octopus (Hong Kong) and Shenzhen Tong parser for FeliCa dumps +// +// Ported from Metrodroid (GPLv3), transit/octopus. +//----------------------------------------------------------------------------- + +#ifndef PARSEOCTOPUS_H__ +#define PARSEOCTOPUS_H__ + +#include "common.h" +#include "parsetransit.h" + +#define OCTOPUS_SYSTEM_CODE 0x8008 +#define OCTOPUS_SERVICE_BALANCE 0x0117 +#define SZT_SYSTEM_CODE 0x8005 +#define SZT_SERVICE_BALANCE 0x0118 + +// True when either balance service was read +bool is_valid_octopus_card(const transit_felica_card_t *card); + +// Print the Octopus and / or Shenzhen Tong balance +int octopus_parser_parse(const transit_felica_card_t *card); + +// Metrodroid OctopusTest vectors +int octopus_selftest(void); + +#endif diff --git a/client/src/parsers/parsesuica.c b/client/src/parsers/parsesuica.c new file mode 100644 index 000000000..e125b6a14 --- /dev/null +++ b/client/src/parsers/parsesuica.c @@ -0,0 +1,492 @@ +//----------------------------------------------------------------------------- +// 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. +//----------------------------------------------------------------------------- +// Japan IC transit card parser for FeliCa dumps +//----------------------------------------------------------------------------- + +#include "parsesuica.h" + +#include +#include + +#include "commonutil.h" // ARRAYLEN +#include "ui.h" // PrintAndLogEx +#include "util.h" // sprint_hex_inrow + +#define SUICA_BLOCK_SIZE 16 +#define SUICA_CONSOLE_BUS 0x05 +#define SUICA_PROCESS_CHARGE 0x02 + +//----------------------------------------------------------------------------- +// Card identification by service codes >= 0x1800, Metrodroid SuicaUtil +//----------------------------------------------------------------------------- + +typedef struct { + const char *name; + const uint16_t *codes; + size_t count; +} suica_issuer_t; + +static const uint16_t suica_hayakaken[] = { + 0x1f88, 0x1f8a, 0x2048, 0x204a, 0x2448, 0x244a, 0x2488, 0x248a, 0x24c8, 0x24ca, 0x2508, + 0x250a, 0x2548, 0x254a +}; +static const uint16_t suica_icoca[] = { 0x1a48, 0x1a4a, 0x1a88, 0x1a8a, 0x9608, 0x960a }; +static const uint16_t suica_kitaca[] = { + 0x1848, 0x184b, 0x2088, 0x208b, 0x20c8, 0x20cb, 0x2108, 0x210b, 0x2148, 0x214b, 0x2188, + 0x218b +}; +static const uint16_t suica_manaca[] = { + 0x9888, 0x988b, 0x98cc, 0x98cf, 0x9908, 0x990a, 0x9948, 0x994a, 0x9988, 0x998b +}; +static const uint16_t suica_nimoca[] = { + 0x1f48, 0x1f4a, 0x1f88, 0x1f8a, 0x1fc8, 0x1fca, 0x2008, 0x200a, 0x2048, 0x204a +}; +static const uint16_t suica_pasmo[] = { + 0x1848, 0x184b, 0x1908, 0x190a, 0x1948, 0x194b, 0x1988, 0x198b, 0x1cc8, 0x1cca, 0x1d08, + 0x1d0a, 0x2308, 0x230a, 0x2348, 0x234b, 0x2388, 0x238b, 0x23c8, 0x23cb +}; +static const uint16_t suica_pitapa[] = { 0x1b88, 0x1b8a, 0x9748, 0x974a }; +static const uint16_t suica_sugoca[] = { + 0x1f88, 0x1f8a, 0x2048, 0x204b, 0x21c8, 0x21cb, 0x2208, 0x220a, 0x2248, 0x224a, 0x2288, + 0x228a +}; +static const uint16_t suica_suica[] = { + 0x1808, 0x180a, 0x1848, 0x184b, 0x18c8, 0x18ca, 0x1908, 0x190a, 0x1948, 0x194b, 0x1988, + 0x198b, 0x2308, 0x230a, 0x2348, 0x234b, 0x2388, 0x238b, 0x23c8, 0x23cb +}; +static const uint16_t suica_toica[] = { + 0x1848, 0x184b, 0x1e08, 0x1e0a, 0x1e48, 0x1e4a, 0x1e88, 0x1e8a, 0x1e8b, 0x1ecc, 0x1ecf +}; + +static const suica_issuer_t suica_issuers[] = { + { "Hayakaken", suica_hayakaken, ARRAYLEN(suica_hayakaken) }, + { "ICOCA", suica_icoca, ARRAYLEN(suica_icoca) }, + { "Kitaca", suica_kitaca, ARRAYLEN(suica_kitaca) }, + { "manaca", suica_manaca, ARRAYLEN(suica_manaca) }, + { "nimoca", suica_nimoca, ARRAYLEN(suica_nimoca) }, + { "PASMO", suica_pasmo, ARRAYLEN(suica_pasmo) }, + { "PiTaPa", suica_pitapa, ARRAYLEN(suica_pitapa) }, + { "SUGOCA", suica_sugoca, ARRAYLEN(suica_sugoca) }, + { "Suica", suica_suica, ARRAYLEN(suica_suica) }, + { "TOICA", suica_toica, ARRAYLEN(suica_toica) }, +}; + +static bool suica_in_list(const uint16_t *codes, size_t count, uint16_t code) { + for (size_t i = 0; i < count; i++) { + if (codes[i] == code) { + return true; + } + } + return false; +} + +// A code counts for an issuer only when no other issuer lists it +static bool suica_code_unique_to(size_t issuer, uint16_t code) { + for (size_t j = 0; j < ARRAYLEN(suica_issuers); j++) { + if (j != issuer && suica_in_list(suica_issuers[j].codes, suica_issuers[j].count, code)) { + return false; + } + } + return true; +} + +// Exactly one issuer must match, otherwise the generic name +static const char *suica_card_name(const uint16_t *codes, size_t count) { + const char *found = NULL; + for (size_t i = 0; i < ARRAYLEN(suica_issuers); i++) { + bool hit = false; + for (size_t k = 0; k < count && hit == false; k++) { + hit = suica_in_list(suica_issuers[i].codes, suica_issuers[i].count, codes[k]) && + suica_code_unique_to(i, codes[k]); + } + if (hit) { + if (found != NULL) { + return "Japan IC"; + } + found = suica_issuers[i].name; + } + } + return (found != NULL) ? found : "Japan IC"; +} + +//----------------------------------------------------------------------------- +// Names, Metrodroid SuicaUtil after felicalib +//----------------------------------------------------------------------------- + +static const char *suica_console_name(uint8_t c) { + switch (c) { + case 0x03: + return "Fare adjustment"; + case 0x04: + return "Portable terminal"; + case 0x05: + return "Bus"; + case 0x07: + case 0x08: + return "Ticket machine"; + case 0x09: + return "Quick refill"; + case 0x12: + return "Monorail TVM"; + case 0x13: + case 0x14: + case 0x15: + return "TVM"; + case 0x16: + return "Turnstile"; + case 0x17: + return "Ticket validator"; + case 0x18: + return "Ticket booth"; + case 0x19: + return "Green window"; + case 0x1A: + return "Gate terminal"; + case 0x1B: + return "Mobile phone"; + case 0x1C: + return "Connection adj."; + case 0x1D: + return "Transfer adj."; + case 0x1F: + return "Simple deposit"; + case 0x46: + case 0x48: + return "VIEW ALTTE"; + case 0xC7: + return "POS"; + case 0xC8: + return "Vending machine"; + default: + return "?"; + } +} + +static const char *suica_process_name(uint8_t p) { + switch (p) { + case 0x01: + return "Exit turnstile"; + case 0x02: + return "Refill"; + case 0x03: + return "Magnetic ticket"; + case 0x04: + return "Fare adjustment"; + case 0x05: + return "Admission payment"; + case 0x06: + return "Booth exit"; + case 0x07: + return "New issue"; + case 0x08: + return "Booth payment"; + case 0x0D: + return "Bus (PiTaPa)"; + case 0x0F: + return "Bus (IruCa)"; + case 0x11: + return "Re-issue"; + case 0x13: + return "Shinkansen payment"; + case 0x14: + return "Entry, autorefill"; + case 0x15: + return "Exit, autorefill"; + case 0x1F: + return "Bus deposit"; + case 0x23: + return "Special ticket"; + case 0x46: + return "Purchase"; + case 0x48: + return "Bonus refill"; + case 0x49: + return "Register refill"; + case 0x4A: + return "Cancel purchase"; + case 0x4B: + return "Purchase/admission"; + case 0x84: + return "Payment, 3rd party"; + case 0x85: + return "Admission, 3rd party"; + case 0xC6: + return "Purchase, part cash"; + case 0xCB: + return "Admission, part cash"; + default: + return "?"; + } +} + +//----------------------------------------------------------------------------- +// History record, service 090F, newest first +//----------------------------------------------------------------------------- + +typedef struct { + uint8_t console; + uint8_t process; + bool used; + bool sale; + uint16_t year; + uint8_t month; + uint8_t day; + uint8_t hour; + uint8_t minute; + uint16_t from; + uint16_t to; + uint16_t balance; + uint8_t region; +} suica_trip_t; + +static bool suica_is_tvm(uint8_t c) { + return (c == 0x03 || c == 0x07 || c == 0x08 || c == 0x12 || c == 0x13 || c == 0x14 || c == 0x15); +} + +static void suica_read_trip(const uint8_t *d, suica_trip_t *t) { + memset(t, 0, sizeof(*t)); + t->console = d[0]; + t->process = d[1]; + t->used = (MemBeToUint2byte(d + 4) != 0); + t->sale = (t->console == 0xC7 || t->console == 0xC8); + t->year = 2000 + (uint16_t)transit_bits_be(d, SUICA_BLOCK_SIZE, 32, 7); + t->month = (uint8_t)transit_bits_be(d, SUICA_BLOCK_SIZE, 39, 4); + t->day = (uint8_t)transit_bits_be(d, SUICA_BLOCK_SIZE, 43, 5); + if (t->sale) { + t->hour = (uint8_t)transit_bits_be(d, SUICA_BLOCK_SIZE, 48, 5); + t->minute = (uint8_t)transit_bits_be(d, SUICA_BLOCK_SIZE, 53, 6); + } + // line << 8 | station for rail, line and stop numbers for bus + t->from = MemBeToUint2byte(d + 6); + t->to = MemBeToUint2byte(d + 8); + t->balance = MemLeToUint2byte(d + 10); + t->region = d[15]; +} + +static void suica_print_trip(uint16_t idx, const suica_trip_t *t, const suica_trip_t *older) { + char date[24]; + if (t->sale) { + snprintf(date, sizeof(date), "%04u-%02u-%02u %02u:%02u", t->year, t->month, t->day, t->hour, t->minute); + } else { + snprintf(date, sizeof(date), "%04u-%02u-%02u", t->year, t->month, t->day); + } + + // no stations for sales and refills, one for bus and ticket machines + char from[8] = "", to[8] = ""; + if (t->sale == false && t->process != SUICA_PROCESS_CHARGE) { + snprintf(from, sizeof(from), "%04X", t->from); + if (t->console != SUICA_CONSOLE_BUS && suica_is_tvm(t->console) == false) { + snprintf(to, sizeof(to), "%04X", t->to); + } + } + + char fare[24] = ""; + if (older != NULL) { + transit_money_str(fare, sizeof(fare), (int64_t)older->balance - t->balance, 0, NULL); + } + char bal[24]; + transit_money_str(bal, sizeof(bal), t->balance, 0, NULL); + + PrintAndLogEx(INFO, " %2u | %-16s | %-17s | %-20s | %-4s | %-4s | %6s | %6s", + idx, date, suica_console_name(t->console), suica_process_name(t->process), + from, to, fare, bal); +} + +//----------------------------------------------------------------------------- +// Detection and printing +//----------------------------------------------------------------------------- + +static const transit_felica_service_t *suica_history(const transit_felica_card_t *card) { + const transit_felica_system_t *sys = transit_felica_find_system(card, SUICA_SYSTEM_CODE); + return transit_felica_find_service(sys, SUICA_SERVICE_HISTORY); +} + +bool is_valid_suica_card(const transit_felica_card_t *card) { + const transit_felica_service_t *h = suica_history(card); + return (h != NULL && h->block_count > 0); +} + +int suica_parser_parse(const transit_felica_card_t *card) { + + if (is_valid_suica_card(card) == false) { + return PM3_EINVARG; + } + + const transit_felica_system_t *sys = transit_felica_find_system(card, SUICA_SYSTEM_CODE); + const transit_felica_service_t *h = suica_history(card); + + uint16_t codes[256]; + size_t ncodes = 0; + for (size_t i = 0; i < sys->service_count && ncodes < ARRAYLEN(codes); i++) { + codes[ncodes++] = sys->services[i].code; + } + + transit_print_header("Japan IC transit card"); + transit_print_kv("Card", _YELLOW_("%s"), suica_card_name(codes, ncodes)); + transit_print_kv("IDm", "%s", sprint_hex_inrow(sys->idm, sizeof(sys->idm))); + + suica_trip_t latest; + suica_read_trip(h->blocks, &latest); + if (latest.used) { + char s[24]; + transit_money_str(s, sizeof(s), latest.balance, 0, "JPY"); + transit_print_kv("Balance", _GREEN_("%s"), s); + // balance expires ten years after the last use + transit_print_kv("Expiry", "%04u-%02u-%02u", latest.year + 10, latest.month, latest.day); + } else { + transit_print_kv("Balance", "no history yet"); + } + + // attribute block keeps the balance little endian at offset 11 + const transit_felica_service_t *attr = transit_felica_find_service(sys, SUICA_SERVICE_ATTRIBUTE); + if (attr != NULL && attr->block_count > 0 && latest.used) { + uint16_t ab = MemLeToUint2byte(attr->blocks + 11); + transit_print_kv("Attribute 008B", "%u JPY ( %s )", ab, + (ab == latest.balance) ? _GREEN_("matches history") : _YELLOW_("differs from history")); + } + + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "History, newest first. Stations are line/station codes, region per record"); + PrintAndLogEx(INFO, " # | Date | Terminal | Process | From | To | Fare | Balance"); + PrintAndLogEx(INFO, "----+------------------+-------------------+----------------------+------+------+--------+--------"); + + uint16_t shown = 0; + for (uint16_t i = 0; i < h->block_count; i++) { + suica_trip_t t; + suica_read_trip(h->blocks + (i * SUICA_BLOCK_SIZE), &t); + if (t.used == false) { + continue; + } + suica_trip_t older; + bool has_older = false; + if (i + 1 < h->block_count) { + suica_read_trip(h->blocks + ((i + 1) * SUICA_BLOCK_SIZE), &older); + has_older = older.used; + } + suica_print_trip(i, &t, has_older ? &older : NULL); + shown++; + } + if (shown == 0) { + PrintAndLogEx(INFO, " no records"); + } + return PM3_SUCCESS; +} + +//----------------------------------------------------------------------------- +// Self test +//----------------------------------------------------------------------------- + +// Service code sets of real cards, Metrodroid SuicaTest +static const uint16_t suica_test_common[] = { + 0x48, 0x4a, 0x88, 0x8b, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0x810, 0x812, + 0x816, 0x850, 0x852, 0x856, 0x890, 0x892, 0x896, 0x8c8, 0x8ca, 0x90a, 0x90c, 0x90f, 0x1008, + 0x100a, 0x1048, 0x104a, 0x108c, 0x108f, 0x10c8, 0x10cb, 0x1108, 0x110a, 0x1148, 0x114a +}; + +static bool suica_check_name(const char *what, const uint16_t *extra, size_t extra_len, const char *want) { + uint16_t codes[96]; + size_t n = 0; + for (size_t i = 0; i < ARRAYLEN(suica_test_common); i++) { + codes[n++] = suica_test_common[i]; + } + for (size_t i = 0; i < extra_len && n < ARRAYLEN(codes); i++) { + codes[n++] = extra[i]; + } + return transit_check_str(what, suica_card_name(codes, n), want); +} + +int suica_selftest(void) { + + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "Testing Japan IC decode"); + + bool ok = true; + ok &= suica_check_name("identify Suica", suica_suica, ARRAYLEN(suica_suica), "Suica"); + ok &= suica_check_name("identify PASMO", suica_pasmo, ARRAYLEN(suica_pasmo), "PASMO"); + ok &= suica_check_name("identify ICOCA", suica_icoca, ARRAYLEN(suica_icoca), "ICOCA"); + ok &= suica_check_name("identify nimoca", suica_nimoca, ARRAYLEN(suica_nimoca), "nimoca"); + ok &= suica_check_name("identify Hayakaken", suica_hayakaken, ARRAYLEN(suica_hayakaken), "Hayakaken"); + + // open services only, shared by PASMO and Suica + const uint16_t ambiguous[] = { 0x8b, 0x90f, 0x108f, 0x10cb, 0x184b, 0x194b, 0x234b, 0x238b, 0x23cb }; + ok &= transit_check_str("ambiguous PASMO/Suica", suica_card_name(ambiguous, ARRAYLEN(ambiguous)), "Japan IC"); + + // history: newest is a turnstile trip on 2019-04-03 leaving 1234 JPY, + // the older one a refill on 2019-04-02 leaving 1400 JPY + uint8_t hist[3 * SUICA_BLOCK_SIZE] = {0}; + uint8_t *b0 = hist, *b1 = hist + SUICA_BLOCK_SIZE; + b0[0] = 0x16; + b0[1] = 0x01; + transit_set_bits_be(b0, SUICA_BLOCK_SIZE, 32, 7, 19); + transit_set_bits_be(b0, SUICA_BLOCK_SIZE, 39, 4, 4); + transit_set_bits_be(b0, SUICA_BLOCK_SIZE, 43, 5, 3); + b0[6] = 0xE3; + b0[7] = 0x29; + b0[8] = 0xE3; + b0[9] = 0x3D; + b0[10] = 0xD2; // 1234 little endian + b0[11] = 0x04; + b1[0] = 0x07; + b1[1] = 0x02; + transit_set_bits_be(b1, SUICA_BLOCK_SIZE, 32, 7, 19); + transit_set_bits_be(b1, SUICA_BLOCK_SIZE, 39, 4, 4); + transit_set_bits_be(b1, SUICA_BLOCK_SIZE, 43, 5, 2); + b1[10] = 0x78; // 1400 + b1[11] = 0x05; + + suica_trip_t t0, t1, t2; + suica_read_trip(b0, &t0); + suica_read_trip(b1, &t1); + suica_read_trip(hist + (2 * SUICA_BLOCK_SIZE), &t2); + ok &= transit_check("date raw word", MemBeToUint2byte(b0 + 4), 0x2683); + ok &= transit_check("year", t0.year, 2019); + ok &= transit_check("month", t0.month, 4); + ok &= transit_check("day", t0.day, 3); + ok &= transit_check("entry line/station", t0.from, 0xE329); + ok &= transit_check("exit line/station", t0.to, 0xE33D); + ok &= transit_check("balance", t0.balance, 1234); + ok &= transit_check("fare from older record", t1.balance - t0.balance, 166); + ok &= transit_check("empty record unused", t2.used, 0); + + // vending machine sale carries a time + uint8_t sale[SUICA_BLOCK_SIZE] = {0}; + sale[0] = 0xC8; + sale[1] = 0x46; + transit_set_bits_be(sale, sizeof(sale), 32, 7, 20); + transit_set_bits_be(sale, sizeof(sale), 39, 4, 12); + transit_set_bits_be(sale, sizeof(sale), 43, 5, 31); + transit_set_bits_be(sale, sizeof(sale), 48, 5, 23); + transit_set_bits_be(sale, sizeof(sale), 53, 6, 59); + suica_trip_t ts; + suica_read_trip(sale, &ts); + ok &= transit_check("sale hour", ts.hour, 23); + ok &= transit_check("sale minute", ts.minute, 59); + + // through the dispatcher view + const transit_felica_service_t svc[] = { + { SUICA_SERVICE_HISTORY, 3, hist }, + { 0x1808, 0, NULL }, + }; + transit_felica_system_t sys = { .system_code = SUICA_SYSTEM_CODE, .service_count = ARRAYLEN(svc), .services = svc }; + transit_felica_card_t card = { .system_count = 1, .systems = &sys }; + ok &= transit_check("detected", is_valid_suica_card(&card), 1); + ok &= transit_check("parse", (uint64_t)suica_parser_parse(&card), PM3_SUCCESS); + + sys.system_code = 0x8008; + ok &= transit_check("other system not detected", is_valid_suica_card(&card), 0); + + return transit_selftest_result("Japan IC", ok); +} diff --git a/client/src/parsers/parsesuica.h b/client/src/parsers/parsesuica.h new file mode 100644 index 000000000..0c15b9c04 --- /dev/null +++ b/client/src/parsers/parsesuica.h @@ -0,0 +1,42 @@ +//----------------------------------------------------------------------------- +// 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. +//----------------------------------------------------------------------------- +// Japan IC transit card parser for FeliCa dumps +// +// Suica, PASMO, ICOCA, Kitaca, manaca, nimoca, PiTaPa, SUGOCA, TOICA and +// Hayakaken share one layout in system 0003. Ported from Metrodroid +// (GPLv3), transit/suica. +//----------------------------------------------------------------------------- + +#ifndef PARSESUICA_H__ +#define PARSESUICA_H__ + +#include "common.h" +#include "parsetransit.h" + +#define SUICA_SYSTEM_CODE 0x0003 +#define SUICA_SERVICE_ATTRIBUTE 0x008B +#define SUICA_SERVICE_HISTORY 0x090F + +// True when system 0003 carries a readable history service +bool is_valid_suica_card(const transit_felica_card_t *card); + +// Name the card from its service codes, print balance and history +int suica_parser_parse(const transit_felica_card_t *card); + +// Decode built history blocks and identify cards from Metrodroid's service lists +int suica_selftest(void); + +#endif diff --git a/client/src/parsers/parsetransit.c b/client/src/parsers/parsetransit.c index 4db114c72..b919f9cbc 100644 --- a/client/src/parsers/parsetransit.c +++ b/client/src/parsers/parsetransit.c @@ -27,16 +27,19 @@ #include "parsebip.h" #include "parsecharlie.h" #include "parseclipper.h" +#include "parseintertic.h" #include "parseitso.h" #include "parsekazan.h" #include "parsemetromoney.h" #include "parsemyki.h" #include "parsenextfare.h" #include "parsenol.h" +#include "parseoctopus.h" #include "parseopal.h" #include "parseplantain.h" #include "parserenfe.h" #include "parsesmartrider.h" +#include "parsesuica.h" #include "parsetmobilitat.h" #include "parsetroika.h" #include "parsetrt.h" @@ -372,10 +375,13 @@ static const transit_buf_parser_t mfu_parsers[] = { }; static const transit_st25tb_parser_t st25tb_parsers[] = { + {"Intertic", is_valid_intertic_card, intertic_parser_parse, intertic_selftest}, {NULL, NULL, NULL, NULL} }; static const transit_felica_parser_t felica_parsers[] = { + {"Japan IC (Suica)", is_valid_suica_card, suica_parser_parse, suica_selftest}, + {"Octopus", is_valid_octopus_card, octopus_parser_parse, octopus_selftest}, {NULL, NULL, NULL, NULL} }; diff --git a/client/src/parsers/parsetransit.h b/client/src/parsers/parsetransit.h index 6d2185009..e59898d99 100644 --- a/client/src/parsers/parsetransit.h +++ b/client/src/parsers/parsetransit.h @@ -97,7 +97,7 @@ int transit_selftest_result(const char *name, bool ok); typedef struct { uint16_t code; uint16_t block_count; - const uint8_t *blocks; // block_count * 16 bytes, block 0 first + const uint8_t *blocks; // block_count * 16 bytes, block 0 first, NULL when not read } transit_felica_service_t; typedef struct {