transit parsers: DESFire and Ultralight card systems

DESFire parsers run from hf mfdes view / dump and are detected by AID.
They decode the free-read files; `hf mfdes dump --no-auth` is what reads
those, and a parser says so when a file it needs is missing. Locked
balance/trip files (myki, nol, T-Mobilitat) need operator keys and are
only named.

  Clipper (SF Bay)      Metrodroid, Flipper clipper.c
  Opal (Sydney)         Metrodroid
  myki (Melbourne)      Metrodroid, card number only
  ITSO (UK)             Flipper itso.c
  nol (Dubai)           Metrodroid, Metroflip
  T-Mobilitat           Metroflip, number from ATS historical bytes

Ultralight parsers run from hf mfu view / dump:

  TRT                   Flipper trt.c
  Ventra (Chicago)      Flipper ventra.c, Metrodroid Nextfare UL

Self tests: Metrodroid ClipperTest, MykiTest, OpalTest plus the real
opal-transit-litter dump matched against Metrodroid's parsed output;
synthetic for the rest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
iceman1001andClaude Opus 5.5 committed 2026-11-10 10:05:13 +07:00
1 parent 4fcd3c274e
commit b2335fcc90
20 files changed
+2016

No files matched your search

+8
View File
@@ -520,14 +520,22 @@ set (TARGET_SOURCES
${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/parseclipper.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/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/parsetmobilitat.c
${PM3_ROOT}/client/src/parsers/parsetroika.c
${PM3_ROOT}/client/src/parsers/parsetrt.c
${PM3_ROOT}/client/src/parsers/parseumarsh.c
${PM3_ROOT}/client/src/parsers/parseventra.c
${PM3_ROOT}/client/src/parsers/parsezolotayakorona.c
${PM3_ROOT}/client/src/parsers/hrtparser/hrtparser.c
${PM3_ROOT}/client/src/pla.c
+8
View File
@@ -927,14 +927,22 @@ SRCS = mifare/aiddesfire.c \
parsers/parsetransit.c \
parsers/parsebip.c \
parsers/parsecharlie.c \
parsers/parseclipper.c \
parsers/parseitso.c \
parsers/parsekazan.c \
parsers/parsemetromoney.c \
parsers/parsemyki.c \
parsers/parsenextfare.c \
parsers/parsenol.c \
parsers/parseopal.c \
parsers/parseplantain.c \
parsers/parserenfe.c \
parsers/parsesmartrider.c \
parsers/parsetmobilitat.c \
parsers/parsetroika.c \
parsers/parsetrt.c \
parsers/parseumarsh.c \
parsers/parseventra.c \
parsers/parsezolotayakorona.c \
parsers/hrtparser/hrtparser.c \
pm3.c \
+8
View File
@@ -447,14 +447,22 @@ set (TARGET_SOURCES
${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/parseclipper.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/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/parsetmobilitat.c
${PM3_ROOT}/client/src/parsers/parsetroika.c
${PM3_ROOT}/client/src/parsers/parsetrt.c
${PM3_ROOT}/client/src/parsers/parseumarsh.c
${PM3_ROOT}/client/src/parsers/parseventra.c
${PM3_ROOT}/client/src/parsers/parsezolotayakorona.c
${PM3_ROOT}/client/src/parsers/hrtparser/hrtparser.c
${PM3_ROOT}/client/src/pla.c
+490
View File
@@ -0,0 +1,490 @@
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
// Clipper (San Francisco Bay Area) parser for MIFARE DESFire dumps
//-----------------------------------------------------------------------------
#include "parseclipper.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "commonutil.h" // ARRAYLEN, MemBeToUint
#include "ui.h" // PrintAndLogEx
#include "util.h" // sprint_hex_inrow
#include "parsetransit.h"
#define CLIPPER_FID_INFO 0x01 // expiry
#define CLIPPER_FID_ECASH 0x02 // balance
#define CLIPPER_FID_REFILLS 0x04
#define CLIPPER_FID_IDENTITY 0x08 // serial
#define CLIPPER_FID_HISTORY 0x0E
#define CLIPPER_RECORD_LEN 32
#define CLIPPER_MAX_RECORDS 64
// Timestamps count seconds, the expiry days, both from 1900-01-01 UTC
#define CLIPPER_EPOCH_OFFSET 2208988800LL
#define CLIPPER_DAYS_TO_1970 25567
#define CLIPPER_AGENCY_BART 0x0004
typedef struct {
uint16_t id;
const char *name;
} clipper_name_t;
static const clipper_name_t clipper_agencies[] = {
{0x0001, "AC Transit"},
{0x0004, "BART"},
{0x0006, "Caltrain"},
{0x0008, "CCTA"},
{0x000b, "GGT"},
{0x000c, "SMART"},
{0x000f, "SamTrans"},
{0x0011, "VTA"},
{0x0012, "Muni"},
{0x0019, "GG Ferry"},
{0x001b, "SF Bay Ferry"},
};
static const clipper_name_t clipper_bart_stations[] = {
{0x0001, "Colma"},
{0x0002, "Daly City"},
{0x0003, "Balboa Park"},
{0x0004, "Glen Park"},
{0x0005, "24th St Mission"},
{0x0006, "16th St Mission"},
{0x0007, "Civic Center/UN Plaza"},
{0x0008, "Powell St"},
{0x0009, "Montgomery St"},
{0x000a, "Embarcadero"},
{0x000b, "West Oakland"},
{0x000c, "12th St/Oakland City Center"},
{0x000d, "19th St/Oakland"},
{0x000e, "MacArthur"},
{0x000f, "Rockridge"},
{0x0010, "Orinda"},
{0x0011, "Lafayette"},
{0x0012, "Walnut Creek"},
{0x0013, "Pleasant Hill/Contra Costa Centre"},
{0x0014, "Concord"},
{0x0015, "North Concord/Martinez"},
{0x0016, "Pittsburg/Bay Point"},
{0x0017, "Ashby"},
{0x0018, "Downtown Berkeley"},
{0x0019, "North Berkeley"},
{0x001a, "El Cerrito Plaza"},
{0x001b, "El Cerrito Del Norte"},
{0x001c, "Richmond"},
{0x001d, "Lake Merritt"},
{0x001e, "Fruitvale"},
{0x001f, "Coliseum"},
{0x0020, "San Leandro"},
{0x0021, "Bay Fair"},
{0x0022, "Hayward"},
{0x0023, "South Hayward"},
{0x0024, "Union City"},
{0x0025, "Fremont"},
{0x0026, "Castro Valley"},
{0x0027, "Dublin/Pleasanton"},
{0x0028, "South San Francisco"},
{0x0029, "San Bruno"},
{0x002a, "SFO Airport"},
{0x002b, "Millbrae"},
{0x002c, "West Dublin/Pleasanton"},
{0x002d, "OAK Airport"},
{0x002e, "Warm Springs/South Fremont"},
{0x002f, "Milpitas"},
{0x0030, "Berryessa/North San Jose"},
};
typedef struct {
uint16_t agency;
int16_t fare;
uint16_t vehicle;
uint32_t start;
uint32_t end;
uint16_t from;
uint16_t to;
uint16_t route;
uint16_t transport;
} clipper_trip_t;
//-----------------------------------------------------------------------------
// Dump access
//-----------------------------------------------------------------------------
static const desfire_dump_app_t *clipper_find_app(const desfire_dump_t *dump) {
if (dump == NULL) {
return NULL;
}
for (uint8_t i = 0; i < dump->appcount && i < DESFIRE_MAX_APP_COUNT; i++) {
if (dump->app[i].aid == CLIPPER_AID_CARD || dump->app[i].aid == CLIPPER_AID_MOBILE) {
return &dump->app[i];
}
}
return NULL;
}
static const desfire_dump_file_t *clipper_find_file(const desfire_dump_app_t *app, uint8_t fid) {
for (uint8_t i = 0; i < app->filecount && i < DESFIRE_MAX_FILE_COUNT; i++) {
if (app->files[i].num == fid) {
return &app->files[i];
}
}
return NULL;
}
// File contents when read and at least minlen long, NULL otherwise
static const uint8_t *clipper_file_data(const desfire_dump_app_t *app, uint8_t fid, uint32_t minlen, uint32_t *len) {
const desfire_dump_file_t *f = clipper_find_file(app, fid);
if (f == NULL || f->read_ok == false || f->data == NULL || f->datalen < minlen) {
return NULL;
}
if (len != NULL) {
*len = f->datalen;
}
return f->data;
}
//-----------------------------------------------------------------------------
// Decoding
//-----------------------------------------------------------------------------
static const char *clipper_lookup(const clipper_name_t *t, size_t n, uint16_t id) {
for (size_t i = 0; i < n; i++) {
if (t[i].id == id) {
return t[i].name;
}
}
return NULL;
}
static const char *clipper_agency_name(uint16_t agency) {
const char *s = clipper_lookup(clipper_agencies, ARRAYLEN(clipper_agencies), agency);
return (s != NULL) ? s : "unknown";
}
static void clipper_station_str(char *out, size_t outlen, uint16_t agency, uint16_t station) {
const char *s = NULL;
if (agency == CLIPPER_AGENCY_BART) {
s = clipper_lookup(clipper_bart_stations, ARRAYLEN(clipper_bart_stations), station);
}
if (s != NULL) {
snprintf(out, outlen, "%s", s);
} else {
snprintf(out, outlen, "%04X", station);
}
}
// Newer Muni readers store a 4 digit vehicle number and a letter, 20461 is 2046A
static void clipper_vehicle_str(char *out, size_t outlen, uint16_t v) {
if (v == 0 || v == 0xFFFF) {
snprintf(out, outlen, "-");
} else if (v <= 9999) {
snprintf(out, outlen, "%u", v);
} else {
snprintf(out, outlen, "%u%X", v / 10, (v % 10) + 9);
}
}
static const char *clipper_mode_str(uint16_t transport, uint16_t agency) {
switch (transport) {
case 0x62:
if (agency == 0x19 || agency == 0x1b) {
return "ferry";
}
if (agency == 0x06 || agency == 0x0c) {
return "train";
}
return "tram";
case 0x6f:
return "metro";
case 0x61:
case 0x75:
case 0x77:
return "bus";
case 0x73:
return "ferry";
case 0x78:
return "train";
default:
return "other";
}
}
static void clipper_time_str(char *out, size_t outlen, uint32_t t) {
if (t == 0) {
snprintf(out, outlen, "-");
return;
}
transit_datetime_str(out, outlen, (int64_t)t - CLIPPER_EPOCH_OFFSET);
}
static void clipper_read_trip(const uint8_t *r, clipper_trip_t *t) {
t->agency = MemBeToUint2byte(r + 0x02);
t->fare = (int16_t)MemBeToUint2byte(r + 0x06);
t->vehicle = MemBeToUint2byte(r + 0x0A);
t->start = MemBeToUint4byte(r + 0x0C);
t->end = MemBeToUint4byte(r + 0x10);
t->from = MemBeToUint2byte(r + 0x14);
t->to = MemBeToUint2byte(r + 0x16);
t->route = MemBeToUint2byte(r + 0x1C);
t->transport = MemBeToUint2byte(r + 0x1E);
}
static int clipper_trip_cmp(const void *a, const void *b) {
uint32_t x = ((const clipper_trip_t *)a)->start;
uint32_t y = ((const clipper_trip_t *)b)->start;
return (x < y) ? 1 : (x > y) ? -1 : 0;
}
// Temporary records share the start time of the final one, keep the one with an exit
static size_t clipper_collect_trips(const uint8_t *d, uint32_t len, clipper_trip_t *out, size_t max) {
size_t n = 0;
for (uint32_t pos = 0; pos + CLIPPER_RECORD_LEN <= len; pos += CLIPPER_RECORD_LEN) {
clipper_trip_t t;
clipper_read_trip(d + pos, &t);
if (t.agency == 0) {
continue;
}
bool skip = false;
for (size_t i = 0; i < n; i++) {
if (out[i].start == t.start) {
if (out[i].end == 0) {
out[i] = t;
}
skip = true;
break;
}
}
if (skip == false && n < max) {
out[n++] = t;
}
}
qsort(out, n, sizeof(clipper_trip_t), clipper_trip_cmp);
return n;
}
//-----------------------------------------------------------------------------
// Detection and printing
//-----------------------------------------------------------------------------
bool is_valid_clipper_card(const desfire_dump_t *dump) {
return (clipper_find_app(dump) != NULL);
}
static void clipper_print_trips(const desfire_dump_app_t *app) {
uint32_t len = 0;
const uint8_t *d = clipper_file_data(app, CLIPPER_FID_HISTORY, CLIPPER_RECORD_LEN, &len);
if (d == NULL) {
return;
}
clipper_trip_t *trips = calloc(CLIPPER_MAX_RECORDS, sizeof(clipper_trip_t));
if (trips == NULL) {
return;
}
size_t n = clipper_collect_trips(d, len, trips, CLIPPER_MAX_RECORDS);
transit_print_header("Clipper trips, newest first");
char ts[32], te[32], from[40], to[40], veh[16], fare[24];
for (size_t i = 0; i < n; i++) {
const clipper_trip_t *t = &trips[i];
clipper_time_str(ts, sizeof(ts), t->start);
clipper_time_str(te, sizeof(te), t->end);
clipper_station_str(from, sizeof(from), t->agency, t->from);
clipper_station_str(to, sizeof(to), t->agency, t->to);
clipper_vehicle_str(veh, sizeof(veh), t->vehicle);
transit_money_str(fare, sizeof(fare), t->fare, 2, "USD");
PrintAndLogEx(INFO, "%s UTC %-12s %-5s fare " _YELLOW_("%s"), ts,
clipper_agency_name(t->agency), clipper_mode_str(t->transport, t->agency), fare);
PrintAndLogEx(INFO, " from %s to %s exit %s vehicle %s route %04X", from,
(t->end != 0) ? to : "-", te, veh, t->route);
}
if (n == 0) {
PrintAndLogEx(INFO, "no trips recorded");
}
free(trips);
}
static void clipper_print_refills(const desfire_dump_app_t *app) {
uint32_t len = 0;
const uint8_t *d = clipper_file_data(app, CLIPPER_FID_REFILLS, CLIPPER_RECORD_LEN, &len);
if (d == NULL) {
return;
}
transit_print_header("Clipper refills");
char ts[32], amount[24];
int n = 0;
for (uint32_t pos = 0; pos + CLIPPER_RECORD_LEN <= len; pos += CLIPPER_RECORD_LEN) {
const uint8_t *r = d + pos;
uint32_t t = MemBeToUint4byte(r + 0x04);
if (t == 0) {
continue;
}
uint16_t agency = MemBeToUint2byte(r + 0x02);
clipper_time_str(ts, sizeof(ts), t);
transit_money_str(amount, sizeof(amount), MemBeToUint2byte(r + 0x0E), 2, "USD");
PrintAndLogEx(INFO, "%s UTC " _YELLOW_("+%s") " agency %04X ( %s ) machine %s", ts, amount,
agency, clipper_agency_name(agency), sprint_hex_inrow(r + 0x08, 4));
n++;
}
if (n == 0) {
PrintAndLogEx(INFO, "no refills recorded");
}
}
int clipper_parser_parse(const desfire_dump_t *dump) {
const desfire_dump_app_t *app = clipper_find_app(dump);
if (app == NULL) {
return PM3_EINVARG;
}
transit_print_header("Clipper");
transit_print_kv("Issuer", "Metropolitan Transportation Commission, SF Bay Area");
transit_print_kv("Application", "%06X ( %s )", app->aid,
(app->aid == CLIPPER_AID_MOBILE) ? "mobile device" : "card");
bool any = false;
const uint8_t *d = clipper_file_data(app, CLIPPER_FID_IDENTITY, 5, NULL);
if (d != NULL) {
transit_print_kv("Serial", _YELLOW_("%u"), MemBeToUint4byte(d + 1));
any = true;
}
uint32_t len = 0;
d = clipper_file_data(app, CLIPPER_FID_ECASH, 0x14, &len);
if (d != NULL) {
char buf[32];
transit_money_str(buf, sizeof(buf), (int16_t)MemBeToUint2byte(d + 0x12), 2, "USD");
transit_print_kv("Balance", _GREEN_("%s"), buf);
uint32_t upd = MemBeToUint4byte(d + 0x04);
clipper_time_str(buf, sizeof(buf), upd);
transit_print_kv("Last update", "%s%s", buf, (upd != 0) ? " UTC" : "");
transit_print_kv("Terminal", "%04X", MemBeToUint2byte(d + 0x08));
transit_print_kv("Transaction id", "%u", MemBeToUint2byte(d + 0x10));
transit_print_kv("Counter", "%u", MemBeToUint2byte(d + 0x02));
any = true;
}
d = clipper_file_data(app, CLIPPER_FID_INFO, 10, NULL);
if (d != NULL) {
uint16_t exp = MemBeToUint2byte(d + 8);
if (exp != 0) {
char buf[16];
transit_date_str(buf, sizeof(buf), (int32_t)exp - CLIPPER_DAYS_TO_1970);
transit_print_kv("Expiry", "%s", buf);
}
}
clipper_print_trips(app);
clipper_print_refills(app);
if (any == false) {
PrintAndLogEx(HINT, "Hint: the Clipper files are free to read, try `" _YELLOW_("hf mfdes dump --no-auth") "`");
}
return PM3_SUCCESS;
}
//-----------------------------------------------------------------------------
// Self test, Metrodroid ClipperTest vectors
//-----------------------------------------------------------------------------
static void clipper_test_file(desfire_dump_file_t *f, uint8_t fid, uint8_t *data, uint32_t len) {
f->num = fid;
f->read_ok = true;
f->data = data;
f->datalen = len;
}
int clipper_selftest(void) {
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "Testing Clipper decode");
// mocked card from Metrodroid
static uint8_t ecash[20] = {
[0x12] = 0x77, [0x13] = 0x77
};
static uint8_t identity[] = { 0x00, 0x22, 0x22, 0x95, 0x33 };
static uint8_t refill[CLIPPER_RECORD_LEN] = {
0x00, 0x00, 0x02, 0xcf, 0xde, 0x44, 0x00, 0x00, 0x78, 0x12, 0x34, 0x56, 0x00, 0x00, 0x13, 0x88
};
static uint8_t trip[CLIPPER_RECORD_LEN] = {
0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x02, 0x76, 0x00, 0x00, 0x00, 0x00, 0xde, 0x58, 0x00, 0x00,
0xde, 0x58, 0x10, 0x00, 0x00, 0x08, 0x00, 0x27, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6f
};
desfire_dump_t *dump = calloc(1, sizeof(desfire_dump_t));
if (dump == NULL) {
return PM3_EMALLOC;
}
dump->appcount = 1;
desfire_dump_app_t *app = &dump->app[0];
app->aid = CLIPPER_AID_CARD;
app->filecount = 4;
clipper_test_file(&app->files[0], CLIPPER_FID_ECASH, ecash, sizeof(ecash));
clipper_test_file(&app->files[1], CLIPPER_FID_REFILLS, refill, sizeof(refill));
clipper_test_file(&app->files[2], CLIPPER_FID_IDENTITY, identity, sizeof(identity));
clipper_test_file(&app->files[3], CLIPPER_FID_HISTORY, trip, sizeof(trip));
bool ok = true;
ok &= transit_check("detected", is_valid_clipper_card(dump), true);
ok &= transit_check("serial", MemBeToUint4byte(clipper_file_data(app, CLIPPER_FID_IDENTITY, 5, NULL) + 1), 572691763);
ok &= transit_check("balance cents", (uint16_t)MemBeToUint2byte(ecash + 0x12), 30583);
char buf[40];
clipper_time_str(buf, sizeof(buf), MemBeToUint4byte(refill + 4));
ok &= transit_check_str("refill time", buf, "2018-03-02 16:53:20");
ok &= transit_check("refill amount", MemBeToUint2byte(refill + 0x0E), 5000);
clipper_trip_t t[2];
ok &= transit_check("trip count", clipper_collect_trips(trip, sizeof(trip), t, ARRAYLEN(t)), 1);
ok &= transit_check("trip agency", t[0].agency, CLIPPER_AGENCY_BART);
ok &= transit_check("trip fare", (uint64_t)t[0].fare, 630);
clipper_time_str(buf, sizeof(buf), t[0].start);
ok &= transit_check_str("trip start", buf, "2018-03-17 20:58:40");
clipper_station_str(buf, sizeof(buf), t[0].agency, t[0].from);
ok &= transit_check_str("trip from", buf, "Powell St");
clipper_station_str(buf, sizeof(buf), t[0].agency, t[0].to);
ok &= transit_check_str("trip to", buf, "Dublin/Pleasanton");
ok &= transit_check_str("trip mode", clipper_mode_str(t[0].transport, t[0].agency), "metro");
// vehicle numbers from real cards
clipper_vehicle_str(buf, sizeof(buf), 0x0422);
ok &= transit_check_str("vehicle 0422", buf, "1058");
clipper_vehicle_str(buf, sizeof(buf), 0x4e85);
ok &= transit_check_str("vehicle 4E85", buf, "2010A");
clipper_vehicle_str(buf, sizeof(buf), 0x5084);
ok &= transit_check_str("vehicle 5084", buf, "2061B");
clipper_vehicle_str(buf, sizeof(buf), 0xffff);
ok &= transit_check_str("vehicle FFFF", buf, "-");
app->aid = 0x534531;
ok &= transit_check("other AID rejected", is_valid_clipper_card(dump), false);
app->aid = CLIPPER_AID_CARD;
if (ok) {
clipper_parser_parse(dump);
}
free(dump);
return transit_selftest_result("Clipper", ok);
}
+36
View File
@@ -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.
//-----------------------------------------------------------------------------
// Clipper (San Francisco Bay Area) parser for MIFARE DESFire dumps
//
// Layout from Metrodroid ClipperTransitData and the Flipper Zero clipper
// plugin by Jeremy Cooper, both GPLv3.
//-----------------------------------------------------------------------------
#ifndef PARSECLIPPER_H__
#define PARSECLIPPER_H__
#include "common.h"
#include "fileutils.h" // desfire_dump_t
// AIDs as pm3 holds them, wire order 90 11 F2 and 91 11 F2
#define CLIPPER_AID_CARD 0xF21190
#define CLIPPER_AID_MOBILE 0xF21191
bool is_valid_clipper_card(const desfire_dump_t *dump);
int clipper_parser_parse(const desfire_dump_t *dump);
int clipper_selftest(void);
#endif
+150
View File
@@ -0,0 +1,150 @@
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
// ITSO (UK) parser for MIFARE DESFire dumps
//-----------------------------------------------------------------------------
#include "parseitso.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "commonutil.h" // MemBeToUint2byte
#include "ui.h" // PrintAndLogEx
#include "util.h" // sprint_hex_inrow
#include "parsetransit.h"
#define ITSO_SHELL_MIN_LEN 16
#define ITSO_ISRN_DIGITS 18
// All ISRNs carry the ITSO issuer identification number
#define ITSO_IIN "633597"
static const desfire_dump_app_t *itso_find_app(const desfire_dump_t *dump) {
if (dump == NULL) {
return NULL;
}
for (uint8_t i = 0; i < dump->appcount && i < DESFIRE_MAX_APP_COUNT; i++) {
if (dump->app[i].aid == ITSO_AID) {
return &dump->app[i];
}
}
return NULL;
}
static const desfire_dump_file_t *itso_find_file(const desfire_dump_app_t *app, uint8_t fid) {
for (uint8_t i = 0; i < app->filecount && i < DESFIRE_MAX_FILE_COUNT; i++) {
if (app->files[i].num == fid) {
return &app->files[i];
}
}
return NULL;
}
// ISRN is BCD from the fifth nibble on, false when it is not ITSO's
static bool itso_isrn_str(const uint8_t *d, char *out, size_t outlen) {
char digits[ITSO_ISRN_DIGITS + 1];
for (int i = 0; i < ITSO_ISRN_DIGITS; i++) {
int nib = 4 + i;
uint8_t v = (nib & 1) ? (d[nib >> 1] & 0x0F) : (d[nib >> 1] >> 4);
if (v > 9) {
return false;
}
digits[i] = (char)('0' + v);
}
digits[ITSO_ISRN_DIGITS] = '\0';
if (strncmp(digits, ITSO_IIN, strlen(ITSO_IIN)) != 0) {
return false;
}
snprintf(out, outlen, "%.6s %.4s %.4s %.4s", digits, digits + 6, digits + 10, digits + 14);
return true;
}
bool is_valid_itso_card(const desfire_dump_t *dump) {
return (itso_find_app(dump) != NULL);
}
int itso_parser_parse(const desfire_dump_t *dump) {
const desfire_dump_app_t *app = itso_find_app(dump);
if (app == NULL) {
return PM3_EINVARG;
}
transit_print_header("ITSO");
transit_print_kv("Scheme", "ITSO smartcard, United Kingdom");
const desfire_dump_file_t *f = itso_find_file(app, ITSO_FID_SHELL);
if (f == NULL || f->read_ok == false || f->data == NULL || f->datalen < ITSO_SHELL_MIN_LEN) {
PrintAndLogEx(HINT, "Hint: file %02X is free to read, try `" _YELLOW_("hf mfdes dump --no-auth") "`", ITSO_FID_SHELL);
return PM3_SUCCESS;
}
char isrn[32];
if (itso_isrn_str(f->data, isrn, sizeof(isrn)) == false) {
transit_print_kv("Shell", "%s", sprint_hex_inrow(f->data, ITSO_SHELL_MIN_LEN));
PrintAndLogEx(INFO, "No ITSO ISRN where one is expected");
return PM3_SUCCESS;
}
transit_print_kv("ISRN", _YELLOW_("%s"), isrn);
// EN 1545 date stamp
char buf[16];
transit_date_str(buf, sizeof(buf), TRANSIT_DAYS_TO_1997 + MemBeToUint2byte(f->data + 14));
transit_print_kv("Expiry", "%s", buf);
return PM3_SUCCESS;
}
int itso_selftest(void) {
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "Testing ITSO decode");
// synthetic shell, expiry day 0x2710 = 2024-05-19
static uint8_t shell[32] = {
0x13, 0x10, 0x63, 0x35, 0x97, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x30, 0x00, 0x00, 0x27, 0x10
};
bool ok = true;
char buf[32] = {0};
ok &= transit_check("ISRN decoded", itso_isrn_str(shell, buf, sizeof(buf)), true);
ok &= transit_check_str("ISRN", buf, "633597 1234 5678 9012");
transit_date_str(buf, sizeof(buf), TRANSIT_DAYS_TO_1997 + MemBeToUint2byte(shell + 14));
ok &= transit_check_str("expiry", buf, "2024-05-19");
uint8_t other[16] = { 0x13, 0x10, 0x63, 0x35, 0x98 };
ok &= transit_check("other IIN rejected", itso_isrn_str(other, buf, sizeof(buf)), false);
desfire_dump_t *dump = calloc(1, sizeof(desfire_dump_t));
if (dump == NULL) {
return PM3_EMALLOC;
}
dump->appcount = 1;
dump->app[0].aid = ITSO_AID;
dump->app[0].filecount = 1;
dump->app[0].files[0].num = ITSO_FID_SHELL;
dump->app[0].files[0].read_ok = true;
dump->app[0].files[0].data = shell;
dump->app[0].files[0].datalen = sizeof(shell);
ok &= transit_check("detected", is_valid_itso_card(dump), true);
if (ok) {
itso_parser_parse(dump);
}
free(dump);
return transit_selftest_result("ITSO", ok);
}
+35
View File
@@ -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.
//-----------------------------------------------------------------------------
// ITSO (UK) parser for MIFARE DESFire dumps
//
// Layout from the Flipper Zero itso plugin (GPLv3): ISRN and expiry in file 0F.
//-----------------------------------------------------------------------------
#ifndef PARSEITSO_H__
#define PARSEITSO_H__
#include "common.h"
#include "fileutils.h" // desfire_dump_t
// AID as pm3 holds it, wire order 16 02 A0
#define ITSO_AID 0xA00216
#define ITSO_FID_SHELL 0x0F
bool is_valid_itso_card(const desfire_dump_t *dump);
int itso_parser_parse(const desfire_dump_t *dump);
int itso_selftest(void);
#endif
+152
View File
@@ -0,0 +1,152 @@
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
// myki (Melbourne) parser for MIFARE DESFire dumps
//-----------------------------------------------------------------------------
#include "parsemyki.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "commonutil.h" // MemLeToUint4byte
#include "ui.h" // PrintAndLogEx
#include "parsetransit.h"
// Every card number starts with 308425
#define MYKI_PREFIX 308425
static const desfire_dump_app_t *myki_find_app(const desfire_dump_t *dump, uint32_t aid) {
if (dump == NULL) {
return NULL;
}
for (uint8_t i = 0; i < dump->appcount && i < DESFIRE_MAX_APP_COUNT; i++) {
if (dump->app[i].aid == aid) {
return &dump->app[i];
}
}
return NULL;
}
static const desfire_dump_file_t *myki_find_file(const desfire_dump_app_t *app, uint8_t fid) {
for (uint8_t i = 0; i < app->filecount && i < DESFIRE_MAX_FILE_COUNT; i++) {
if (app->files[i].num == fid) {
return &app->files[i];
}
}
return NULL;
}
// Luhn check digit for a string of decimal digits
static int myki_luhn(const char *s) {
int sum = 0;
size_t n = strlen(s);
for (size_t i = 0; i < n; i++) {
int d = s[n - 1 - i] - '0';
if ((i & 1) == 0) {
d *= 2;
if (d > 9) {
d -= 9;
}
}
sum += d;
}
return (10 - (sum % 10)) % 10;
}
// 15 digit card number, false when the file does not carry one
static bool myki_serial_str(const uint8_t *d, char *out, size_t outlen) {
uint32_t s1 = MemLeToUint4byte(d);
uint32_t s2 = MemLeToUint4byte(d + 4);
if (s1 != MYKI_PREFIX || s2 > 99999999) {
return false;
}
char digits[16];
snprintf(digits, sizeof(digits), "%06u%08u", s1, s2);
snprintf(out, outlen, "%s%d", digits, myki_luhn(digits));
return true;
}
bool is_valid_myki_card(const desfire_dump_t *dump) {
return (myki_find_app(dump, MYKI_AID_1) != NULL) && (myki_find_app(dump, MYKI_AID_2) != NULL);
}
int myki_parser_parse(const desfire_dump_t *dump) {
if (is_valid_myki_card(dump) == false) {
return PM3_EINVARG;
}
transit_print_header("myki");
transit_print_kv("Issuer", "Public Transport Victoria, Melbourne");
const desfire_dump_file_t *f = myki_find_file(myki_find_app(dump, MYKI_AID_1), MYKI_FID_SERIAL);
if (f == NULL || f->read_ok == false || f->data == NULL || f->datalen < 8) {
PrintAndLogEx(HINT, "Hint: file %02X of %06X is free to read, try `" _YELLOW_("hf mfdes dump --no-auth") "`",
MYKI_FID_SERIAL, MYKI_AID_1);
return PM3_SUCCESS;
}
char serial[24];
if (myki_serial_str(f->data, serial, sizeof(serial))) {
transit_print_kv("Serial", _YELLOW_("%s"), serial);
} else {
transit_print_kv("Serial", "not a myki number");
}
PrintAndLogEx(INFO, "Balance and trips are in files that need the operator keys");
return PM3_SUCCESS;
}
int myki_selftest(void) {
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "Testing myki decode");
// Metrodroid MykiTest mocked data
static uint8_t file[16] = {
0xC9, 0xB4, 0x04, 0x00, 0x4E, 0x61, 0xBC, 0x00
};
bool ok = true;
char serial[24] = {0};
ok &= transit_check("serial decoded", myki_serial_str(file, serial, sizeof(serial)), true);
ok &= transit_check_str("serial", serial, "308425123456780");
uint8_t bad[8] = { 0xC9, 0xB4, 0x04, 0x01 };
ok &= transit_check("wrong prefix rejected", myki_serial_str(bad, serial, sizeof(serial)), false);
desfire_dump_t *dump = calloc(1, sizeof(desfire_dump_t));
if (dump == NULL) {
return PM3_EMALLOC;
}
dump->appcount = 2;
dump->app[0].aid = MYKI_AID_1;
dump->app[0].filecount = 1;
dump->app[0].files[0].num = MYKI_FID_SERIAL;
dump->app[0].files[0].read_ok = true;
dump->app[0].files[0].data = file;
dump->app[0].files[0].datalen = sizeof(file);
dump->app[1].aid = MYKI_AID_2;
ok &= transit_check("detected", is_valid_myki_card(dump), true);
if (ok) {
myki_parser_parse(dump);
}
dump->appcount = 1;
ok &= transit_check("one AID only rejected", is_valid_myki_card(dump), false);
free(dump);
return transit_selftest_result("myki", ok);
}
+37
View File
@@ -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.
//-----------------------------------------------------------------------------
// myki (Melbourne) parser for MIFARE DESFire dumps
//
// Layout from Metrodroid MykiTransitData (GPLv3). Only the card number is
// free to read, everything else is locked.
//-----------------------------------------------------------------------------
#ifndef PARSEMYKI_H__
#define PARSEMYKI_H__
#include "common.h"
#include "fileutils.h" // desfire_dump_t
// AIDs as pm3 holds them, wire order 00 11 F2 and F0 10 F2
#define MYKI_AID_1 0xF21100
#define MYKI_AID_2 0xF210F0
#define MYKI_FID_SERIAL 0x0F
bool is_valid_myki_card(const desfire_dump_t *dump);
int myki_parser_parse(const desfire_dump_t *dump);
int myki_selftest(void);
#endif
+136
View File
@@ -0,0 +1,136 @@
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
// nol (Dubai) parser for MIFARE DESFire dumps
//-----------------------------------------------------------------------------
#include "parsenol.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "commonutil.h" // MemBeToUint2byte
#include "ui.h" // PrintAndLogEx
#include "parsetransit.h"
// serial bits 61..92, card type at byte 12
#define NOL_SERIAL_MIN_LEN 14
static const desfire_dump_app_t *nol_find_app(const desfire_dump_t *dump, uint32_t aid) {
if (dump == NULL) {
return NULL;
}
for (uint8_t i = 0; i < dump->appcount && i < DESFIRE_MAX_APP_COUNT; i++) {
if (dump->app[i].aid == aid) {
return &dump->app[i];
}
}
return NULL;
}
static const desfire_dump_file_t *nol_find_file(const desfire_dump_app_t *app, uint8_t fid) {
for (uint8_t i = 0; i < app->filecount && i < DESFIRE_MAX_FILE_COUNT; i++) {
if (app->files[i].num == fid) {
return &app->files[i];
}
}
return NULL;
}
static const char *nol_type_str(uint16_t type) {
switch (type) {
case 0x4d5:
return "Silver";
case 0x4d9:
return "Red";
default:
return "unknown";
}
}
static void nol_serial_str(const uint8_t *d, char *out, size_t outlen) {
uint32_t s = (uint32_t)transit_bits_be(d, NOL_SERIAL_MIN_LEN, 61, 32);
snprintf(out, outlen, "%03u %03u %04u", (s / 10000000U) % 1000U, (s / 10000U) % 1000U, s % 10000U);
}
bool is_valid_nol_card(const desfire_dump_t *dump) {
return (nol_find_app(dump, NOL_AID_TRANSIT) != NULL) && (nol_find_app(dump, NOL_AID_SERIAL) != NULL);
}
int nol_parser_parse(const desfire_dump_t *dump) {
if (is_valid_nol_card(dump) == false) {
return PM3_EINVARG;
}
transit_print_header("nol");
transit_print_kv("Issuer", "Roads and Transport Authority, Dubai");
const desfire_dump_file_t *f = nol_find_file(nol_find_app(dump, NOL_AID_SERIAL), NOL_FID_SERIAL);
if (f == NULL || f->read_ok == false || f->data == NULL || f->datalen < NOL_SERIAL_MIN_LEN) {
PrintAndLogEx(HINT, "Hint: file %02X of %06X is free to read, try `" _YELLOW_("hf mfdes dump --no-auth") "`",
NOL_FID_SERIAL, NOL_AID_SERIAL);
return PM3_SUCCESS;
}
char buf[24];
nol_serial_str(f->data, buf, sizeof(buf));
transit_print_kv("Serial", _YELLOW_("%s"), buf);
uint16_t type = MemBeToUint2byte(f->data + 12);
transit_print_kv("Card type", "%s ( %03X )", nol_type_str(type), type);
PrintAndLogEx(INFO, "Balance and trips are in files that need the operator keys");
return PM3_SUCCESS;
}
int nol_selftest(void) {
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "Testing nol decode");
// synthetic file, serial 1234567890 at bit 61, silver card
uint8_t file[64] = {0};
transit_set_bits_be(file, sizeof(file), 61, 32, 1234567890);
file[12] = 0x04;
file[13] = 0xd5;
bool ok = true;
char buf[24];
nol_serial_str(file, buf, sizeof(buf));
ok &= transit_check_str("serial", buf, "123 456 7890");
ok &= transit_check_str("type", nol_type_str(MemBeToUint2byte(file + 12)), "Silver");
desfire_dump_t *dump = calloc(1, sizeof(desfire_dump_t));
if (dump == NULL) {
return PM3_EMALLOC;
}
dump->appcount = 2;
dump->app[0].aid = NOL_AID_TRANSIT;
dump->app[1].aid = NOL_AID_SERIAL;
dump->app[1].filecount = 1;
dump->app[1].files[0].num = NOL_FID_SERIAL;
dump->app[1].files[0].read_ok = true;
dump->app[1].files[0].data = file;
dump->app[1].files[0].datalen = sizeof(file);
ok &= transit_check("detected", is_valid_nol_card(dump), true);
if (ok) {
nol_parser_parse(dump);
}
dump->app[0].aid = 0x000001;
ok &= transit_check("FFFFFF alone rejected", is_valid_nol_card(dump), false);
free(dump);
return transit_selftest_result("nol", ok);
}
+37
View File
@@ -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.
//-----------------------------------------------------------------------------
// nol (Dubai) parser for MIFARE DESFire dumps
//
// Layout from Metrodroid NolTransitData and Metroflip nol plugin, both GPLv3.
// Only the card number and type are free to read.
//-----------------------------------------------------------------------------
#ifndef PARSENOL_H__
#define PARSENOL_H__
#include "common.h"
#include "fileutils.h" // desfire_dump_t
// AIDs as pm3 holds them, wire order 00 40 78 and FF FF FF
#define NOL_AID_TRANSIT 0x784000
#define NOL_AID_SERIAL 0xFFFFFF
#define NOL_FID_SERIAL 0x08
bool is_valid_nol_card(const desfire_dump_t *dump);
int nol_parser_parse(const desfire_dump_t *dump);
int nol_selftest(void);
#endif
+240
View File
@@ -0,0 +1,240 @@
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
// Opal (Sydney) parser for MIFARE DESFire dumps
//-----------------------------------------------------------------------------
#include "parseopal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ui.h" // PrintAndLogEx
#include "parsetransit.h"
#define OPAL_CARD_LEN 16
#define OPAL_SERIAL_BASE 3085220000000000ULL
typedef struct {
uint16_t checksum;
uint8_t weekly_trips;
bool auto_topup;
uint8_t action;
uint8_t mode;
uint16_t minute;
uint16_t day; // days since 1980-01-01, Sydney local time
int32_t balance; // cents
uint16_t txn_number;
uint8_t last_digit;
uint32_t serial;
} opal_card_t;
static const char *opal_mode_str(uint8_t m) {
switch (m) {
case 0:
return "rail";
case 1:
return "ferry / light rail";
case 2:
return "bus";
default:
return "unknown";
}
}
static const char *opal_action_str(uint8_t a) {
static const char *actions[] = {
"none",
"new journey",
"transfer, same mode",
"transfer, other mode",
"Manly ferry, new journey",
"Manly ferry, transfer same mode",
"Manly ferry, transfer other mode",
"journey completed, distance fare",
"journey completed, flat rate",
"journey completed, automatic on",
"journey completed, automatic off",
"tap on reversal",
"tap on rejected",
};
return (a < 13) ? actions[a] : "unknown";
}
static const desfire_dump_app_t *opal_find_app(const desfire_dump_t *dump) {
if (dump == NULL) {
return NULL;
}
for (uint8_t i = 0; i < dump->appcount && i < DESFIRE_MAX_APP_COUNT; i++) {
if (dump->app[i].aid == OPAL_AID) {
return &dump->app[i];
}
}
return NULL;
}
static const desfire_dump_file_t *opal_find_file(const desfire_dump_app_t *app, uint8_t fid) {
for (uint8_t i = 0; i < app->filecount && i < DESFIRE_MAX_FILE_COUNT; i++) {
if (app->files[i].num == fid) {
return &app->files[i];
}
}
return NULL;
}
// The 16 bytes read as one little endian 128 bit number, field 0 at the top
static void opal_decode(const uint8_t *raw, opal_card_t *c) {
uint8_t d[OPAL_CARD_LEN];
for (int i = 0; i < OPAL_CARD_LEN; i++) {
d[i] = raw[OPAL_CARD_LEN - 1 - i];
}
c->checksum = (uint16_t)transit_bits_be(d, sizeof(d), 0, 16);
c->weekly_trips = (uint8_t)transit_bits_be(d, sizeof(d), 16, 4);
c->auto_topup = transit_bits_be(d, sizeof(d), 20, 1);
c->action = (uint8_t)transit_bits_be(d, sizeof(d), 21, 4);
c->mode = (uint8_t)transit_bits_be(d, sizeof(d), 25, 3);
c->minute = (uint16_t)transit_bits_be(d, sizeof(d), 28, 11);
c->day = (uint16_t)transit_bits_be(d, sizeof(d), 39, 15);
c->balance = (int32_t)transit_sign_extend(transit_bits_be(d, sizeof(d), 54, 21), 21);
c->txn_number = (uint16_t)transit_bits_be(d, sizeof(d), 75, 16);
c->last_digit = (uint8_t)transit_bits_be(d, sizeof(d), 92, 4);
c->serial = (uint32_t)transit_bits_be(d, sizeof(d), 96, 32);
}
static void opal_serial_str(char *out, size_t outlen, const opal_card_t *c) {
uint64_t n = OPAL_SERIAL_BASE + ((uint64_t)c->serial * 10) + c->last_digit;
snprintf(out, outlen, "%04u %04u %04u %04u",
(unsigned)(n / 1000000000000ULL), (unsigned)((n / 100000000ULL) % 10000),
(unsigned)((n / 10000) % 10000), (unsigned)(n % 10000));
}
static void opal_time_str(char *out, size_t outlen, const opal_card_t *c) {
int64_t secs = (((int64_t)TRANSIT_DAYS_TO_1980 + c->day) * 86400) + ((int64_t)c->minute * 60);
transit_datetime_str(out, outlen, secs);
// minutes only
size_t n = strlen(out);
if (n > 3) {
out[n - 3] = '\0';
}
}
bool is_valid_opal_card(const desfire_dump_t *dump) {
return (opal_find_app(dump) != NULL);
}
int opal_parser_parse(const desfire_dump_t *dump) {
const desfire_dump_app_t *app = opal_find_app(dump);
if (app == NULL) {
return PM3_EINVARG;
}
transit_print_header("Opal");
transit_print_kv("Issuer", "Transport for NSW, Sydney");
const desfire_dump_file_t *f = opal_find_file(app, OPAL_FID_CARD);
if (f == NULL || f->read_ok == false || f->data == NULL || f->datalen < OPAL_CARD_LEN) {
PrintAndLogEx(HINT, "Hint: file %02X is free to read, try `" _YELLOW_("hf mfdes dump --no-auth") "`", OPAL_FID_CARD);
return PM3_SUCCESS;
}
opal_card_t c;
opal_decode(f->data, &c);
char buf[40];
opal_serial_str(buf, sizeof(buf), &c);
transit_print_kv("Serial", _YELLOW_("%s"), buf);
transit_money_str(buf, sizeof(buf), c.balance, 2, "AUD");
transit_print_kv("Balance", _GREEN_("%s"), buf);
transit_print_kv("Auto top up", "%s", c.auto_topup ? "yes" : "no");
transit_print_kv("Weekly trips", "%u", c.weekly_trips);
transit_print_header("Opal last transaction");
opal_time_str(buf, sizeof(buf), &c);
transit_print_kv("Date", "%s Sydney time", buf);
transit_print_kv("Mode", "%s", opal_mode_str(c.mode));
transit_print_kv("Action", "%s", opal_action_str(c.action));
transit_print_kv("Counter", "%u", c.txn_number);
transit_print_kv("Checksum", "%04X", c.checksum);
return PM3_SUCCESS;
}
//-----------------------------------------------------------------------------
// Self test, Metrodroid OpalTest and opal-transit-litter vectors
//-----------------------------------------------------------------------------
static bool opal_check_vector(const char *name, const uint8_t *raw, const char *serial, int32_t balance,
const char *when, uint8_t mode, uint8_t action, uint16_t txn, uint8_t weekly) {
opal_card_t c;
opal_decode(raw, &c);
char buf[40];
bool ok = true;
PrintAndLogEx(INFO, " %s", name);
opal_serial_str(buf, sizeof(buf), &c);
ok &= transit_check_str("serial", buf, serial);
ok &= transit_check("balance + 1000", (uint64_t)(c.balance + 1000), (uint64_t)(balance + 1000));
opal_time_str(buf, sizeof(buf), &c);
ok &= transit_check_str("last transaction", buf, when);
ok &= transit_check("mode", c.mode, mode);
ok &= transit_check("action", c.action, action);
ok &= transit_check("counter", c.txn_number, txn);
ok &= transit_check("weekly trips", c.weekly_trips, weekly);
return ok;
}
int opal_selftest(void) {
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "Testing Opal decode");
// mocked data, 2015-10-05 09:06 Sydney
static uint8_t demo[OPAL_CARD_LEN] = {
0x87, 0xd6, 0x12, 0x00, 0xe0, 0x04, 0x00, 0x2a, 0x00, 0x14, 0xcc, 0x44, 0xa4, 0x13, 0x39, 0x30
};
// real card thrown away in litter
static uint8_t litter[OPAL_CARD_LEN] = {
0x60, 0xe0, 0x77, 0x00, 0xa2, 0x02, 0x40, 0xe9, 0xff, 0xff, 0xca, 0xa0, 0x88, 0x23, 0x64, 0x31
};
bool ok = true;
ok &= opal_check_vector("demo card", demo, "3085 2200 1234 5670", 336, "2015-10-05 09:06", 2, 7, 39, 1);
ok &= opal_check_vector("litter card", litter, "3085 2200 7856 2242", -182, "2015-07-27 18:24", 0, 7, 21, 2);
opal_card_t c;
opal_decode(litter, &c);
ok &= transit_check("litter checksum", c.checksum, 12644);
desfire_dump_t *dump = calloc(1, sizeof(desfire_dump_t));
if (dump == NULL) {
return PM3_EMALLOC;
}
dump->appcount = 1;
dump->app[0].aid = OPAL_AID;
dump->app[0].filecount = 1;
dump->app[0].files[0].num = OPAL_FID_CARD;
dump->app[0].files[0].read_ok = true;
dump->app[0].files[0].data = litter;
dump->app[0].files[0].datalen = sizeof(litter);
ok &= transit_check("detected", is_valid_opal_card(dump), true);
if (ok) {
opal_parser_parse(dump);
}
dump->app[0].aid = 0xF21190;
ok &= transit_check("other AID rejected", is_valid_opal_card(dump), false);
free(dump);
return transit_selftest_result("Opal", ok);
}
+35
View File
@@ -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.
//-----------------------------------------------------------------------------
// Opal (Sydney) parser for MIFARE DESFire dumps
//
// Layout from Metrodroid OpalTransitData (GPLv3), free read file 07.
//-----------------------------------------------------------------------------
#ifndef PARSEOPAL_H__
#define PARSEOPAL_H__
#include "common.h"
#include "fileutils.h" // desfire_dump_t
// AID as pm3 holds it, wire order 31 45 53
#define OPAL_AID 0x534531
#define OPAL_FID_CARD 0x07
bool is_valid_opal_card(const desfire_dump_t *dump);
int opal_parser_parse(const desfire_dump_t *dump);
int opal_selftest(void);
#endif
+138
View File
@@ -0,0 +1,138 @@
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
// T-Mobilitat (Barcelona) parser for MIFARE DESFire dumps
//-----------------------------------------------------------------------------
#include "parsetmobilitat.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "commonutil.h" // MemBeToUint4byte
#include "ui.h" // PrintAndLogEx
#include "util.h" // sprint_hex_inrow
#include "parsetransit.h"
// Historical bytes open with this tag on T-Mobilitat cards
static const uint8_t tmobilitat_hist_tag[] = { 0x2A, 0x26 };
// Historical bytes of the ATS, NULL when there are none
static const uint8_t *tmobilitat_hist(const iso14a_card_select_t *card, uint8_t *len) {
*len = 0;
if (card->ats_len < 2) {
return NULL;
}
uint8_t tl = card->ats[0];
uint8_t t0 = card->ats[1];
uint8_t pos = 2;
pos += ((t0 & 0x10) ? 1 : 0) + ((t0 & 0x20) ? 1 : 0) + ((t0 & 0x40) ? 1 : 0);
if (tl > card->ats_len || pos >= tl) {
return NULL;
}
*len = tl - pos;
return card->ats + pos;
}
// Two check letters, DNI style
static void tmobilitat_control(uint32_t n, char *out) {
static const char table[] = "TRWAGMYFPDXBNJZSQVHLCKE";
uint32_t r = n % 23;
uint32_t q = n / 23;
out[0] = table[(q + r + 1) % 23];
out[1] = table[r];
out[2] = '\0';
}
static void tmobilitat_number_str(uint32_t n, char *out, size_t outlen) {
char ctl[3];
tmobilitat_control(n, ctl);
snprintf(out, outlen, "%03u %03u %03u%s", (n / 1000000) % 1000, (n / 1000) % 1000, n % 1000, ctl);
}
bool is_valid_tmobilitat_card(const desfire_dump_t *dump) {
if (dump == NULL) {
return false;
}
bool aid = false;
for (uint8_t i = 0; i < dump->appcount && i < DESFIRE_MAX_APP_COUNT; i++) {
if (dump->app[i].aid == TMOBILITAT_AID) {
aid = true;
break;
}
}
uint8_t hlen = 0;
const uint8_t *h = tmobilitat_hist(&dump->card_info, &hlen);
return aid && h != NULL && hlen >= 6 && memcmp(h, tmobilitat_hist_tag, sizeof(tmobilitat_hist_tag)) == 0;
}
int tmobilitat_parser_parse(const desfire_dump_t *dump) {
if (is_valid_tmobilitat_card(dump) == false) {
return PM3_EINVARG;
}
uint8_t hlen = 0;
const uint8_t *h = tmobilitat_hist(&dump->card_info, &hlen);
char buf[32];
tmobilitat_number_str(MemBeToUint4byte(h + hlen - 4), buf, sizeof(buf));
transit_print_header("T-Mobilitat");
transit_print_kv("Issuer", "ATM, Barcelona area");
transit_print_kv("Card number", _YELLOW_("%s"), buf);
transit_print_kv("Historical", "%s", sprint_hex_inrow(h, hlen));
PrintAndLogEx(INFO, "Balance and trips are in files that need the operator keys");
return PM3_SUCCESS;
}
int tmobilitat_selftest(void) {
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "Testing T-Mobilitat decode");
bool ok = true;
char buf[32];
tmobilitat_number_str(123456789, buf, sizeof(buf));
ok &= transit_check_str("card number", buf, "123 456 789GB");
desfire_dump_t *dump = calloc(1, sizeof(desfire_dump_t));
if (dump == NULL) {
return PM3_EMALLOC;
}
// synthetic ATS, TL T0 TA TB TC then historical bytes ending in 075BCD15
const uint8_t ats[] = { 0x0D, 0x78, 0x77, 0x71, 0x02, 0x2A, 0x26, 0x01, 0x02, 0x07, 0x5B, 0xCD, 0x15 };
memcpy(dump->card_info.ats, ats, sizeof(ats));
dump->card_info.ats_len = sizeof(ats);
dump->appcount = 1;
dump->app[0].aid = TMOBILITAT_AID;
uint8_t hlen = 0;
const uint8_t *h = tmobilitat_hist(&dump->card_info, &hlen);
ok &= transit_check("historical length", hlen, 8);
ok &= transit_check("number", (h != NULL) ? MemBeToUint4byte(h + hlen - 4) : 0, 123456789);
ok &= transit_check("detected", is_valid_tmobilitat_card(dump), true);
if (ok) {
tmobilitat_parser_parse(dump);
}
// Ljubljana Urbana shares the AID
dump->card_info.ats[5] = 0x80;
ok &= transit_check("other ATS rejected", is_valid_tmobilitat_card(dump), false);
free(dump);
return transit_selftest_result("T-Mobilitat", ok);
}
+35
View File
@@ -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.
//-----------------------------------------------------------------------------
// T-Mobilitat (Barcelona) parser for MIFARE DESFire dumps
//
// From the Metroflip tmobilitat plugin (GPLv3): the card number sits in the
// ATS historical bytes, the files are locked.
//-----------------------------------------------------------------------------
#ifndef PARSETMOBILITAT_H__
#define PARSETMOBILITAT_H__
#include "common.h"
#include "fileutils.h" // desfire_dump_t
// AID as pm3 holds it, wire order 05 00 00. Ljubljana Urbana uses it too.
#define TMOBILITAT_AID 0x000005
bool is_valid_tmobilitat_card(const desfire_dump_t *dump);
int tmobilitat_parser_parse(const desfire_dump_t *dump);
int tmobilitat_selftest(void);
#endif
+16
View File
@@ -26,14 +26,22 @@
#include "parsebip.h"
#include "parsecharlie.h"
#include "parseclipper.h"
#include "parseitso.h"
#include "parsekazan.h"
#include "parsemetromoney.h"
#include "parsemyki.h"
#include "parsenextfare.h"
#include "parsenol.h"
#include "parseopal.h"
#include "parseplantain.h"
#include "parserenfe.h"
#include "parsesmartrider.h"
#include "parsetmobilitat.h"
#include "parsetroika.h"
#include "parsetrt.h"
#include "parseumarsh.h"
#include "parseventra.h"
#include "parsezolotayakorona.h"
#define TRANSIT_LABEL_WIDTH 17
@@ -347,11 +355,19 @@ static const transit_buf_parser_t mfc_parsers[] = {
};
static const transit_mfdes_parser_t mfdes_parsers[] = {
{"Clipper", is_valid_clipper_card, clipper_parser_parse, clipper_selftest},
{"ITSO", is_valid_itso_card, itso_parser_parse, itso_selftest},
{"myki", is_valid_myki_card, myki_parser_parse, myki_selftest},
{"nol", is_valid_nol_card, nol_parser_parse, nol_selftest},
{"Opal", is_valid_opal_card, opal_parser_parse, opal_selftest},
{"T-Mobilitat", is_valid_tmobilitat_card, tmobilitat_parser_parse, tmobilitat_selftest},
{NULL, NULL, NULL, NULL}
};
static const transit_buf_parser_t mfu_parsers[] = {
{"Troika Ultralight", is_valid_troika_ul_card, troika_ul_parser_parse, troika_ul_selftest},
{"TRT", is_valid_trt_card, trt_parser_parse, trt_selftest},
{"Ventra", is_valid_ventra_card, ventra_parser_parse, ventra_selftest},
{NULL, NULL, NULL, NULL}
};
+135
View File
@@ -0,0 +1,135 @@
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
// TRT, Tianjin Railway Transit single ticket parser for MIFARE Ultralight
//-----------------------------------------------------------------------------
#include "parsetrt.h"
#include <stdio.h>
#include <string.h>
#include "commonutil.h" // MemBeToUint2byte
#include "ui.h" // PrintAndLogEx
#include "parsetransit.h"
#define TRT_PAGE_SIZE 4
#define TRT_PAGE_BALANCE 0x08
#define TRT_PAGE_SALE_FULL 0x09
#define TRT_PAGE_SALE_A 0x0C
#define TRT_PAGE_SALE_B 0x0E
#define TRT_SALE_MARKER 0x02
#define TRT_MIN_LEN ((TRT_PAGE_SALE_B + 1) * TRT_PAGE_SIZE)
// NDEF capability container, never on a ticket
#define TRT_NDEF_CC_MAGIC 0xE1
typedef struct {
uint16_t year;
uint8_t month;
uint8_t day;
uint8_t hour;
uint8_t minute;
uint16_t balance; // fen
} trt_ticket_t;
// Latest sale is kept twice, the short copy right before the marker page
static bool trt_decode(const uint8_t *data, size_t datalen, trt_ticket_t *t) {
if (data == NULL || datalen < TRT_MIN_LEN || data[3 * TRT_PAGE_SIZE] == TRT_NDEF_CC_MAGIC) {
return false;
}
uint8_t page;
if (data[TRT_PAGE_SALE_A * TRT_PAGE_SIZE] == TRT_SALE_MARKER) {
page = TRT_PAGE_SALE_A;
} else if (data[TRT_PAGE_SALE_B * TRT_PAGE_SIZE] == TRT_SALE_MARKER) {
page = TRT_PAGE_SALE_B;
} else {
return false;
}
// yyyyyyy mmmm ddddd hhhhh nnnnnn
uint32_t part = (uint32_t)transit_bits_be(data, datalen, ((page - 1) * TRT_PAGE_SIZE * 8) + 3, 20);
uint32_t full = (uint32_t)transit_bits_be(data, datalen, TRT_PAGE_SALE_FULL * TRT_PAGE_SIZE * 8, 27);
if (part == 0 || full == 0 || part != (full & 0xFFFFF)) {
return false;
}
t->year = ((full >> 20) & 0x7F) + 2000;
t->month = (full >> 16) & 0x0F;
t->day = (full >> 11) & 0x1F;
t->hour = (full >> 6) & 0x1F;
t->minute = full & 0x3F;
t->balance = MemBeToUint2byte(data + (TRT_PAGE_BALANCE * TRT_PAGE_SIZE) + 2);
return (t->month >= 1 && t->month <= 12 && t->day >= 1 && t->hour < 24 && t->minute < 60);
}
bool is_valid_trt_card(const uint8_t *data, size_t datalen) {
trt_ticket_t t;
return trt_decode(data, datalen, &t);
}
int trt_parser_parse(const uint8_t *data, size_t datalen) {
trt_ticket_t t;
if (trt_decode(data, datalen, &t) == false) {
return PM3_EINVARG;
}
char buf[24];
transit_print_header("TRT Tianjin Metro");
transit_print_kv("Type", "single use ticket");
transit_money_str(buf, sizeof(buf), t.balance, 2, "CNY");
transit_print_kv("Balance", _GREEN_("%s"), buf);
transit_print_kv("Sold", "%04u-%02u-%02u %02u:%02u", t.year, t.month, t.day, t.hour, t.minute);
return PM3_SUCCESS;
}
int trt_selftest(void) {
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "Testing TRT decode");
// synthetic ticket, 2.00 CNY sold 2024-05-19 13:45
uint8_t data[20 * TRT_PAGE_SIZE] = {0};
uint32_t full = (24U << 20) | (5U << 16) | (19U << 11) | (13U << 6) | 45U;
transit_set_bits_be(data, sizeof(data), TRT_PAGE_SALE_FULL * 32, 27, full);
transit_set_bits_be(data, sizeof(data), ((TRT_PAGE_SALE_A - 1) * 32) + 3, 20, full & 0xFFFFF);
data[TRT_PAGE_SALE_A * TRT_PAGE_SIZE] = TRT_SALE_MARKER;
data[(TRT_PAGE_BALANCE * TRT_PAGE_SIZE) + 3] = 200;
bool ok = true;
trt_ticket_t t = {0};
ok &= transit_check("decoded", trt_decode(data, sizeof(data), &t), true);
ok &= transit_check("year", t.year, 2024);
ok &= transit_check("month", t.month, 5);
ok &= transit_check("day", t.day, 19);
ok &= transit_check("hour", t.hour, 13);
ok &= transit_check("minute", t.minute, 45);
ok &= transit_check("balance", t.balance, 200);
if (ok) {
trt_parser_parse(data, sizeof(data));
}
ok &= transit_check("short dump rejected", is_valid_trt_card(data, TRT_MIN_LEN - 1), false);
data[(TRT_PAGE_SALE_A - 1) * TRT_PAGE_SIZE] ^= 0x01;
ok &= transit_check("copy mismatch rejected", is_valid_trt_card(data, sizeof(data)), false);
data[(TRT_PAGE_SALE_A - 1) * TRT_PAGE_SIZE] ^= 0x01;
data[3 * TRT_PAGE_SIZE] = TRT_NDEF_CC_MAGIC;
ok &= transit_check("NDEF tag rejected", is_valid_trt_card(data, sizeof(data)), false);
return transit_selftest_result("TRT", ok);
}
+30
View File
@@ -0,0 +1,30 @@
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
// TRT, Tianjin Railway Transit single ticket parser for MIFARE Ultralight
//
// Layout from the Flipper Zero trt plugin by @zinongli (GPLv3).
//-----------------------------------------------------------------------------
#ifndef PARSETRT_H__
#define PARSETRT_H__
#include "common.h"
bool is_valid_trt_card(const uint8_t *data, size_t datalen);
int trt_parser_parse(const uint8_t *data, size_t datalen);
int trt_selftest(void);
#endif
+259
View File
@@ -0,0 +1,259 @@
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
// Ventra (Chicago) paper ticket parser for MIFARE Ultralight
//-----------------------------------------------------------------------------
#include "parseventra.h"
#include <stdio.h>
#include <string.h>
#include "commonutil.h" // MemBeToUint4byte
#include "ui.h" // PrintAndLogEx
#include "parsetransit.h"
#define VENTRA_PAGE_SIZE 4
#define VENTRA_MIN_LEN (16 * VENTRA_PAGE_SIZE)
// Two transaction records of four pages each
static const uint8_t ventra_txn_pages[] = { 8, 12 };
typedef struct {
bool used;
uint8_t type;
uint16_t minutes; // time of day
uint8_t day; // days before the base date
uint8_t seq;
uint16_t locus; // stop or station
uint8_t line; // 0 purchase, 1 train, 2 bus
} ventra_txn_t;
static const uint8_t *ventra_page(const uint8_t *data, uint8_t page) {
return data + (page * VENTRA_PAGE_SIZE);
}
bool is_valid_ventra_card(const uint8_t *data, size_t datalen) {
if (data == NULL || datalen < VENTRA_MIN_LEN) {
return false;
}
const uint8_t *p4 = ventra_page(data, 4);
const uint8_t *p5 = ventra_page(data, 5);
const uint8_t *p6 = ventra_page(data, 6);
if (p4[0] != 0x0A || (p4[1] != 0x04 && p4[1] != 0x08) || p4[2] != 0x00) {
return false;
}
if (p5[1] != 0x01 || (p5[2] & 0x80) || p5[3] != 0x00) {
return false;
}
return (p6[0] == 0 && p6[1] == 0 && p6[2] == 0);
}
static int ventra_luhn(const char *s) {
int sum = 0;
size_t n = strlen(s);
for (size_t i = 0; i < n; i++) {
int d = s[n - 1 - i] - '0';
if ((i & 1) == 0) {
d *= 2;
if (d > 9) {
d -= 9;
}
}
sum += d;
}
return (10 - (sum % 10)) % 10;
}
// UID based number as printed on the ticket, 20 digits in groups of four
static void ventra_serial_str(const uint8_t *data, char *out, size_t outlen) {
uint64_t num = (((uint64_t)((data[1] << 8) | data[2]) << 32) | MemBeToUint4byte(data + 4)) + 1000000000000000ULL;
char digits[24];
snprintf(digits, sizeof(digits), "%" PRIu64, num);
char f[24];
snprintf(f, sizeof(f), "%019" PRIu64 "%d", num, ventra_luhn(digits));
snprintf(out, outlen, "%.4s %.4s %.4s %.4s %.4s", f, f + 4, f + 8, f + 12, f + 16);
}
static int32_t ventra_base_days(const uint8_t *data) {
uint16_t b = ventra_page(data, 4)[3] | (ventra_page(data, 5)[0] << 8);
return transit_days_from_civil((b >> 9) + 2000, (b >> 5) & 0x0F, b & 0x1F);
}
static void ventra_read_txn(const uint8_t *data, uint8_t page, ventra_txn_t *t) {
const uint8_t *p0 = ventra_page(data, page);
const uint8_t *p1 = ventra_page(data, page + 1);
const uint8_t *p2 = ventra_page(data, page + 2);
uint16_t ts = p0[0] | (p0[1] << 8);
uint32_t work = p1[0] | (p1[1] << 8) | ((uint32_t)p1[2] << 16);
t->type = ts & 0x1F;
t->minutes = (ts >> 5) & 0x7FF;
t->day = p0[2];
t->seq = work & 0x7F;
t->locus = p2[1] | (p2[2] << 8);
t->line = p2[3];
t->used = (t->seq != 0);
}
static const char *ventra_product_str(uint8_t code, bool *is_pass) {
*is_pass = true;
switch (code) {
case 0x02:
case 0x1F:
*is_pass = false;
return "single ride ticket";
case 0x03:
case 0x3F:
return "1-day pass";
case 0x04:
return "3-day pass";
default:
return "unknown pass";
}
}
// OTP bits mark off the rides of a ticket
static int ventra_rides_left(uint8_t otp) {
switch (otp) {
case 0x00:
return 3;
case 0x02:
return 2;
case 0x06:
return 1;
case 0x0E:
case 0x7E:
return 0;
default:
return -1;
}
}
static void ventra_print_txn(const ventra_txn_t *t, int32_t base) {
static const char line_names[] = "PTB";
char when[32];
transit_datetime_str(when, sizeof(when), (((int64_t)base - t->day) * 86400) + ((int64_t)t->minutes * 60));
when[16] = '\0';
char line = (t->line < 3) ? line_names[t->line] : '?';
if (t->line == 2) {
PrintAndLogEx(INFO, " seq %3u %s %c stop %5u type %u", t->seq, when, line, t->locus, t->type);
} else {
PrintAndLogEx(INFO, " seq %3u %s %c stop %04X type %u", t->seq, when, line, t->locus, t->type);
}
}
int ventra_parser_parse(const uint8_t *data, size_t datalen) {
if (is_valid_ventra_card(data, datalen) == false) {
return PM3_EINVARG;
}
bool is_pass;
const char *product = ventra_product_str(ventra_page(data, 5)[2], &is_pass);
int32_t base = ventra_base_days(data);
char buf[40];
transit_print_header("Ventra");
transit_print_kv("Issuer", "Chicago Transit Authority");
ventra_serial_str(data, buf, sizeof(buf));
transit_print_kv("Serial", _YELLOW_("%s"), buf);
transit_print_kv("Product", "%s ( %02X )", product, ventra_page(data, 5)[2]);
transit_date_str(buf, sizeof(buf), base);
transit_print_kv("Expiry", "%s", buf);
if (is_pass == false) {
int left = ventra_rides_left(ventra_page(data, 3)[0]);
if (left >= 0) {
transit_print_kv("Rides left", "%d", left);
} else {
transit_print_kv("Rides left", "unknown, OTP %02X", ventra_page(data, 3)[0]);
}
}
transit_print_kv("TVM id", "%02X%02X", ventra_page(data, 7)[1], ventra_page(data, 7)[0]);
ventra_txn_t t[ARRAYLEN(ventra_txn_pages)];
uint8_t high = 0;
for (size_t i = 0; i < ARRAYLEN(ventra_txn_pages); i++) {
ventra_read_txn(data, ventra_txn_pages[i], &t[i]);
if (t[i].seq > high) {
high = t[i].seq;
}
}
transit_print_kv("Transactions", "%u", high);
transit_print_header("Ventra transactions, newest first");
bool first_newer = (t[0].seq >= t[1].seq);
for (size_t i = 0; i < 2; i++) {
const ventra_txn_t *x = first_newer ? &t[i] : &t[1 - i];
if (x->used) {
ventra_print_txn(x, base);
}
}
return PM3_SUCCESS;
}
int ventra_selftest(void) {
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "Testing Ventra decode");
// synthetic single ride ticket, expiry 2024-05-19, one train ride
uint8_t data[20 * VENTRA_PAGE_SIZE] = {
0x04, 0x12, 0x34, 0x00, 0x56, 0x78, 0x9A, 0xBC,
};
uint16_t b = (24 << 9) | (5 << 5) | 19;
uint8_t *p = data;
p[3 * 4] = 0x02; // one ride used
p[4 * 4] = 0x0A;
p[4 * 4 + 1] = 0x04;
p[4 * 4 + 3] = b & 0xFF;
p[5 * 4] = b >> 8;
p[5 * 4 + 1] = 0x01;
p[5 * 4 + 2] = 0x02;
p[7 * 4] = 0x34;
p[7 * 4 + 1] = 0x12;
uint16_t ts = ((8 * 60 + 30) << 5) | 1; // 08:30
p[8 * 4] = ts & 0xFF;
p[8 * 4 + 1] = ts >> 8;
p[8 * 4 + 2] = 3; // three days before expiry
p[9 * 4] = 0x02; // seq 2
p[10 * 4 + 1] = 0x2A;
p[10 * 4 + 3] = 1; // train
bool ok = true;
ok &= transit_check("detected", is_valid_ventra_card(data, sizeof(data)), true);
char buf[40];
transit_date_str(buf, sizeof(buf), ventra_base_days(data));
ok &= transit_check_str("expiry", buf, "2024-05-19");
ventra_txn_t t;
ventra_read_txn(data, 8, &t);
ok &= transit_check("seq", t.seq, 2);
ok &= transit_check("minutes", t.minutes, 510);
ok &= transit_check("locus", t.locus, 0x2A);
ok &= transit_check("rides left", (uint64_t)ventra_rides_left(data[12]), 2);
// Luhn of 308425 1234 5678 from the myki vector is 0
ok &= transit_check("luhn", (uint64_t)ventra_luhn("30842512345678"), 0);
ventra_serial_str(data, buf, sizeof(buf));
// uid 123456789ABC + 10^15, Luhn digit 1
ok &= transit_check_str("serial", buf, "0001 0200 1599 8343 8681");
if (ok) {
ventra_parser_parse(data, sizeof(data));
}
p[4 * 4] = 0x0B;
ok &= transit_check("other header rejected", is_valid_ventra_card(data, sizeof(data)), false);
return transit_selftest_result("Ventra", ok);
}
+31
View File
@@ -0,0 +1,31 @@
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
// Ventra (Chicago) paper ticket parser for MIFARE Ultralight
//
// Layout from the Flipper Zero ventra plugin by @hazardousvoltage and the
// Metrodroid Nextfare Ultralight code, both GPLv3.
//-----------------------------------------------------------------------------
#ifndef PARSEVENTRA_H__
#define PARSEVENTRA_H__
#include "common.h"
bool is_valid_ventra_card(const uint8_t *data, size_t datalen);
int ventra_parser_parse(const uint8_t *data, size_t datalen);
int ventra_selftest(void);
#endif