From 1a21f0e3c99d53a3f29ab271faa4dcbf174ea785 Mon Sep 17 00:00:00 2001 From: Methodius Date: Tue, 21 Nov 2023 12:21:22 +0900 Subject: [PATCH 1/7] Umarsh transport cards parser added --- applications/main/nfc/application.fam | 9 ++ .../main/nfc/plugins/supported_cards/umarsh.c | 131 ++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 applications/main/nfc/plugins/supported_cards/umarsh.c diff --git a/applications/main/nfc/application.fam b/applications/main/nfc/application.fam index c49170d08..dcfde7c36 100644 --- a/applications/main/nfc/application.fam +++ b/applications/main/nfc/application.fam @@ -83,6 +83,15 @@ App( sources=["plugins/supported_cards/two_cities.c"], ) +App( + appid="umarsh_parser", + apptype=FlipperAppType.PLUGIN, + entry_point="umarsh_plugin_ep", + targets=["f7"], + requires=["nfc"], + sources=["plugins/supported_cards/umarsh.c"], +) + App( appid="nfc_start", targets=["f7"], diff --git a/applications/main/nfc/plugins/supported_cards/umarsh.c b/applications/main/nfc/plugins/supported_cards/umarsh.c new file mode 100644 index 000000000..080b4bcbc --- /dev/null +++ b/applications/main/nfc/plugins/supported_cards/umarsh.c @@ -0,0 +1,131 @@ +/* + * Parser for Umarsh card (Russia). + * + * Copyright 2023 Leptoptilos + * Thanks https://github.com/krolchonok for the provided dumps and their analysis + * + * Note: All meaningful data is stored in sectors 0, 8 and 12, reading data + * from which is possible only with the B key. The key B for these sectors + * is unique for each card. To get it, you should use a nested attack. + * More info about Umarsh cards: https://github.com/metrodroid/metrodroid/wiki/Umarsh + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "core/core_defines.h" +#include "nfc_supported_card_plugin.h" + +#include "protocols/mf_classic/mf_classic.h" +#include + +#include +#include +#include +#include +#include +#include + +#define TAG "Umarsh" + +static bool umarsh_parse(const NfcDevice* device, FuriString* parsed_data) { + furi_assert(device); + + const MfClassicData* data = nfc_device_get_data(device, NfcProtocolMfClassic); + + bool parsed = false; + + do { + // Verify card type + if(data->type != MfClassicType1k) break; + + const uint32_t ticket_sector = 8; + + const uint8_t ticket_sector_start_block_number = + mf_classic_get_first_block_num_of_sector(ticket_sector); + + //Validate specific for Umarsh ticket sector header + const uint8_t* block_start_ptr = &data->block[ticket_sector_start_block_number].data[0]; + + uint32_t header = block_start_ptr[0] << 24 | block_start_ptr[1] << 16 | + block_start_ptr[2] << 8 | block_start_ptr[3]; + if(header != 0xFFFFFF7F && header != 0xFEFFFF7F && header != 0xE3FFFF7F) break; + + // Data parsing from block 1 + block_start_ptr = &data->block[ticket_sector_start_block_number + 1].data[0]; + uint8_t region_number = (((block_start_ptr[8] >> 5) & 0x07) << 4) | + (block_start_ptr[12] & 0x0f); + uint32_t card_number = (block_start_ptr[8] << 24 | block_start_ptr[9] << 16 | + block_start_ptr[10] << 8 | block_start_ptr[11]) & + 0x3FFFFFFF; + uint8_t refill_counter = (block_start_ptr[7]); + + if(card_number == 0) break; + + // Data parsing from block 2 + block_start_ptr = &data->block[ticket_sector_start_block_number + 2].data[0]; + uint16_t expiry_date = (block_start_ptr[0] << 8 | block_start_ptr[1]); + uint32_t terminal_number = + (block_start_ptr[3] << 16 | block_start_ptr[4] << 8 | block_start_ptr[5]); + uint16_t last_refill_date = (block_start_ptr[6] << 8 | block_start_ptr[7]); + uint16_t balance = (block_start_ptr[8] << 8 | block_start_ptr[9]) & 0x7FFF; + + FuriHalRtcDateTime expiry_datetime; + expiry_datetime.year = 2000 + (expiry_date >> 9); + expiry_datetime.month = expiry_date >> 5 & 0x0F; + expiry_datetime.day = expiry_date & 0x1F; + + FuriHalRtcDateTime last_refill_datetime; + last_refill_datetime.year = 2000 + (last_refill_date >> 9); + last_refill_datetime.month = last_refill_date >> 5 & 0x0F; + last_refill_datetime.day = last_refill_date & 0x1F; + + furi_string_printf( + parsed_data, + "\e#Umarsh\nCard number: %lu\nRegion: %02u\nBalance: %u RUR\nTerminal number: %lu\nRefill counter: %u\nLast refill: %02u.%02u.%u\nExpires: %02u.%02u.%u", + card_number, + region_number, + balance, + terminal_number, + refill_counter, + last_refill_datetime.day, + last_refill_datetime.month, + last_refill_datetime.year, + expiry_datetime.day, + expiry_datetime.month, + expiry_datetime.year); + parsed = true; + } while(false); + + return parsed; +} + +/* Actual implementation of app<>plugin interface */ +static const NfcSupportedCardsPlugin umarsh_plugin = { + .protocol = NfcProtocolMfClassic, + .verify = NULL, + .read = NULL, + .parse = umarsh_parse, +}; + +/* Plugin descriptor to comply with basic plugin specification */ +static const FlipperAppPluginDescriptor umarsh_plugin_descriptor = { + .appid = NFC_SUPPORTED_CARD_PLUGIN_APP_ID, + .ep_api_version = NFC_SUPPORTED_CARD_PLUGIN_API_VERSION, + .entry_point = &umarsh_plugin, +}; + +/* Plugin entry point - must return a pointer to const descriptor */ +const FlipperAppPluginDescriptor* umarsh_plugin_ep() { + return &umarsh_plugin_descriptor; +} From b6ad07b47cc17c8e0c391d065454ad20cfc498fb Mon Sep 17 00:00:00 2001 From: Methodius Date: Tue, 21 Nov 2023 13:18:18 +0900 Subject: [PATCH 2/7] Umarsh parser: kopecks support added --- applications/main/nfc/plugins/supported_cards/umarsh.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/applications/main/nfc/plugins/supported_cards/umarsh.c b/applications/main/nfc/plugins/supported_cards/umarsh.c index 080b4bcbc..5e29f5c03 100644 --- a/applications/main/nfc/plugins/supported_cards/umarsh.c +++ b/applications/main/nfc/plugins/supported_cards/umarsh.c @@ -64,7 +64,7 @@ static bool umarsh_parse(const NfcDevice* device, FuriString* parsed_data) { // Data parsing from block 1 block_start_ptr = &data->block[ticket_sector_start_block_number + 1].data[0]; uint8_t region_number = (((block_start_ptr[8] >> 5) & 0x07) << 4) | - (block_start_ptr[12] & 0x0f); + (block_start_ptr[12] & 0x0F); uint32_t card_number = (block_start_ptr[8] << 24 | block_start_ptr[9] << 16 | block_start_ptr[10] << 8 | block_start_ptr[11]) & 0x3FFFFFFF; @@ -78,7 +78,8 @@ static bool umarsh_parse(const NfcDevice* device, FuriString* parsed_data) { uint32_t terminal_number = (block_start_ptr[3] << 16 | block_start_ptr[4] << 8 | block_start_ptr[5]); uint16_t last_refill_date = (block_start_ptr[6] << 8 | block_start_ptr[7]); - uint16_t balance = (block_start_ptr[8] << 8 | block_start_ptr[9]) & 0x7FFF; + uint16_t balance_rub = (block_start_ptr[8] << 8 | block_start_ptr[9]) & 0x7FFF; + uint8_t balance_kop = block_start_ptr[10] & 0x7F; FuriHalRtcDateTime expiry_datetime; expiry_datetime.year = 2000 + (expiry_date >> 9); @@ -92,10 +93,11 @@ static bool umarsh_parse(const NfcDevice* device, FuriString* parsed_data) { furi_string_printf( parsed_data, - "\e#Umarsh\nCard number: %lu\nRegion: %02u\nBalance: %u RUR\nTerminal number: %lu\nRefill counter: %u\nLast refill: %02u.%02u.%u\nExpires: %02u.%02u.%u", + "\e#Umarsh\nCard number: %lu\nRegion: %02u\nBalance: %u.%u RUR\nTerminal number: %lu\nRefill counter: %u\nLast refill: %02u.%02u.%u\nExpires: %02u.%02u.%u", card_number, region_number, - balance, + balance_rub, + balance_kop, terminal_number, refill_counter, last_refill_datetime.day, From 7fd921227cfc498a1124d883109660f8ddd57bdd Mon Sep 17 00:00:00 2001 From: assasinfil Date: Tue, 21 Nov 2023 11:54:06 +0300 Subject: [PATCH 3/7] Fixes --- .../plugins/supported_cards/social_moscow.c | 14 +-- .../main/nfc/plugins/supported_cards/troika.c | 103 ++++++++++++------ 2 files changed, 79 insertions(+), 38 deletions(-) diff --git a/applications/main/nfc/plugins/supported_cards/social_moscow.c b/applications/main/nfc/plugins/supported_cards/social_moscow.c index 56cb88d45..79704ee54 100644 --- a/applications/main/nfc/plugins/supported_cards/social_moscow.c +++ b/applications/main/nfc/plugins/supported_cards/social_moscow.c @@ -511,7 +511,7 @@ void from_minutes_to_datetime(uint32_t minutes, FuriHalRtcDateTime* datetime, ui bool parse_transport_block(const MfClassicBlock* block, FuriString* result) { uint16_t transport_departament = bit_lib_get_bits_16(block->data, 0, 10); - FURI_LOG_D(TAG, "Transport departament: %x", transport_departament); + FURI_LOG_I(TAG, "Transport departament: %x", transport_departament); uint16_t layout_type = bit_lib_get_bits_16(block->data, 52, 4); if(layout_type == 0xE) { @@ -520,7 +520,7 @@ bool parse_transport_block(const MfClassicBlock* block, FuriString* result) { layout_type = bit_lib_get_bits_16(block->data, 52, 14); } - FURI_LOG_D(TAG, "Layout type %x", layout_type); + FURI_LOG_I(TAG, "Layout type %x", layout_type); uint16_t card_view = 0; uint16_t card_type = 0; @@ -1483,8 +1483,7 @@ static bool social_moscow_verify_type(Nfc* nfc, MfClassicType type) { } static bool social_moscow_verify(Nfc* nfc) { - return social_moscow_verify_type(nfc, MfClassicType1k) || - social_moscow_verify_type(nfc, MfClassicType4k); + return social_moscow_verify_type(nfc, MfClassicType4k); } static bool social_moscow_read(Nfc* nfc, NfcDevice* device) { @@ -1564,12 +1563,13 @@ static bool social_moscow_parse(const NfcDevice* device, FuriString* parsed_data data->block[60].data[14], furi_string_get_cstr(metro_result), furi_string_get_cstr(ground_result)); - furi_string_free(metro_result); - furi_string_free(ground_result); + parsed = true; } else { - return false; + parsed = false; } + furi_string_free(ground_result); + furi_string_free(metro_result); } while(false); return parsed; diff --git a/applications/main/nfc/plugins/supported_cards/troika.c b/applications/main/nfc/plugins/supported_cards/troika.c index 24101ed8e..5c44f086f 100644 --- a/applications/main/nfc/plugins/supported_cards/troika.c +++ b/applications/main/nfc/plugins/supported_cards/troika.c @@ -530,7 +530,7 @@ void from_minutes_to_datetime(uint32_t minutes, FuriHalRtcDateTime* datetime, ui bool parse_transport_block(const MfClassicBlock* block, FuriString* result) { uint16_t transport_departament = bit_lib_get_bits_16(block->data, 0, 10); - FURI_LOG_D(TAG, "Transport departament: %x", transport_departament); + FURI_LOG_I(TAG, "Transport departament: %x", transport_departament); uint16_t layout_type = bit_lib_get_bits_16(block->data, 52, 4); if(layout_type == 0xE) { @@ -539,7 +539,7 @@ bool parse_transport_block(const MfClassicBlock* block, FuriString* result) { layout_type = bit_lib_get_bits_16(block->data, 52, 14); } - FURI_LOG_D(TAG, "Layout type %x", layout_type); + FURI_LOG_I(TAG, "Layout type %x", layout_type); uint16_t card_view = 0; uint16_t card_type = 0; @@ -612,6 +612,31 @@ bool parse_transport_block(const MfClassicBlock* block, FuriString* result) { card_write_enabled, card_rfu2, card_crc16_2); + if(card_valid_by_date == 0) { + return false; + } + FuriHalRtcDateTime card_use_before_date_s = {0}; + from_days_to_datetime(card_valid_by_date, &card_use_before_date_s, 1992); + + FuriHalRtcDateTime card_start_trip_minutes_s = {0}; + from_minutes_to_datetime( + (card_start_trip_date) * 24 * 60 + card_start_trip_time, + &card_start_trip_minutes_s, + 1992); + furi_string_printf( + result, + "Number: %010lu\nValid for: %02d.%02d.%04d\nTrips: %d\nTrip from: %02d.%02d.%04d %02d:%02d\nValidator: %05d", + card_number, + card_use_before_date_s.day, + card_use_before_date_s.month, + card_use_before_date_s.year, + card_total_trips, + card_start_trip_minutes_s.day, + card_start_trip_minutes_s.month, + card_start_trip_minutes_s.year, + card_start_trip_minutes_s.hour, + card_start_trip_minutes_s.minute, + card_validator); break; } case 0x06: { @@ -668,11 +693,11 @@ bool parse_transport_block(const MfClassicBlock* block, FuriString* result) { card_crc16_2); card_validator = card_validator1 * 1024 + card_validator2; FuriHalRtcDateTime card_use_before_date_s = {0}; - from_days_to_datetime(card_valid_by_date - 1, &card_use_before_date_s, 1992); + from_days_to_datetime(card_valid_by_date, &card_use_before_date_s, 1992); FuriHalRtcDateTime card_start_trip_minutes_s = {0}; from_minutes_to_datetime( - (card_start_trip_date - 1) * 24 * 60 + card_start_trip_time, + (card_start_trip_date) * 24 * 60 + card_start_trip_time, &card_start_trip_minutes_s, 1992); furi_string_printf( @@ -730,7 +755,7 @@ bool parse_transport_block(const MfClassicBlock* block, FuriString* result) { card_valid_from_date, card_rfu3); FuriHalRtcDateTime card_use_before_date_s = {0}; - from_days_to_datetime(card_use_before_date - 1, &card_use_before_date_s, 1992); + from_days_to_datetime(card_use_before_date, &card_use_before_date_s, 1992); furi_string_printf( result, @@ -783,11 +808,10 @@ bool parse_transport_block(const MfClassicBlock* block, FuriString* result) { card_transport_type4, card_hash); FuriHalRtcDateTime card_use_before_date_s = {0}; - from_days_to_datetime(card_use_before_date - 1, &card_use_before_date_s, 2016); + from_days_to_datetime(card_use_before_date, &card_use_before_date_s, 2016); FuriHalRtcDateTime card_start_trip_minutes_s = {0}; - from_minutes_to_datetime( - card_start_trip_minutes - (2 * 24 * 60), &card_start_trip_minutes_s, 2016); + from_minutes_to_datetime(card_start_trip_minutes, &card_start_trip_minutes_s, 2016); furi_string_printf( result, "Number: %010lu\nValid for: %02d.%02d.%04d\nTrip from: %02d.%02d.%04d %02d:%02d\nTrips left: %d\nValidator: %05d", @@ -844,10 +868,10 @@ bool parse_transport_block(const MfClassicBlock* block, FuriString* result) { card_rfu3, card_transfer_in_metro); FuriHalRtcDateTime card_use_before_date_s = {0}; - from_days_to_datetime(card_use_before_date - 1, &card_use_before_date_s, 1992); + from_days_to_datetime(card_use_before_date, &card_use_before_date_s, 1992); FuriHalRtcDateTime card_start_trip_minutes_s = {0}; from_minutes_to_datetime( - (card_start_trip_date - 1) * 24 * 60 + card_start_trip_time, + (card_start_trip_date) * 24 * 60 + card_start_trip_time, &card_start_trip_minutes_s, 1992); furi_string_printf( @@ -925,10 +949,10 @@ bool parse_transport_block(const MfClassicBlock* block, FuriString* result) { card_rfu5, card_transfer_in_metro); FuriHalRtcDateTime card_use_before_date_s = {0}; - from_days_to_datetime(card_use_before_date - 1, &card_use_before_date_s, 1992); + from_days_to_datetime(card_use_before_date, &card_use_before_date_s, 1992); FuriHalRtcDateTime card_start_trip_minutes_s = {0}; from_minutes_to_datetime( - (card_start_trip_date - 1) * 24 * 60 + card_start_trip_time, + (card_start_trip_date) * 24 * 60 + card_start_trip_time, &card_start_trip_minutes_s, 1992); furi_string_printf( @@ -995,11 +1019,10 @@ bool parse_transport_block(const MfClassicBlock* block, FuriString* result) { card_zoo, card_hash); FuriHalRtcDateTime card_use_before_date_s = {0}; - from_days_to_datetime(card_use_before_date - 1, &card_use_before_date_s, 1992); + from_days_to_datetime(card_use_before_date, &card_use_before_date_s, 1992); FuriHalRtcDateTime card_start_trip_minutes_s = {0}; - from_minutes_to_datetime( - card_start_trip_minutes - (2 * 24 * 60), &card_start_trip_minutes_s, 1992); + from_minutes_to_datetime(card_start_trip_minutes, &card_start_trip_minutes_s, 1992); furi_string_printf( result, "Number: %010lu\nValid for: %02d.%02d.%04d\nTrip from: %02d.%02d.%04d %02d:%02d\nValidator: %05d", @@ -1065,12 +1088,11 @@ bool parse_transport_block(const MfClassicBlock* block, FuriString* result) { card_extended, card_hash); FuriHalRtcDateTime card_use_before_date_s = {0}; - from_days_to_datetime(card_use_before_date - 1, &card_use_before_date_s, 2016); + from_days_to_datetime(card_use_before_date, &card_use_before_date_s, 2016); FuriHalRtcDateTime card_start_trip_minutes_s = {0}; from_minutes_to_datetime( - (card_valid_to_date - 1) * 24 * 60 + card_valid_for_minutes - - card_start_trip_neg_minutes, + (card_valid_to_date) * 24 * 60 + card_valid_for_minutes - card_start_trip_neg_minutes, &card_start_trip_minutes_s, 2016); //-time furi_string_printf( @@ -1405,7 +1427,7 @@ bool parse_transport_block(const MfClassicBlock* block, FuriString* result) { card_type4, card_hash); FuriHalRtcDateTime card_use_before_date_s = {0}; - from_days_to_datetime(card_valid_by_date - 1, &card_use_before_date_s, 1992); + from_days_to_datetime(card_valid_by_date, &card_use_before_date_s, 1992); furi_string_printf( result, @@ -1441,7 +1463,7 @@ bool parse_transport_block(const MfClassicBlock* block, FuriString* result) { card_valid_by_date, card_hash); FuriHalRtcDateTime card_use_before_date_s = {0}; - from_days_to_datetime(card_valid_by_date - 1, &card_use_before_date_s, 1992); + from_days_to_datetime(card_valid_by_date, &card_use_before_date_s, 1992); furi_string_printf( result, @@ -1464,10 +1486,10 @@ static bool troika_get_card_config(TroikaCardConfig* config, MfClassicType type) bool success = true; if(type == MfClassicType1k) { - config->data_sector = 11; + config->data_sector = 4; config->keys = troika_1k_keys; } else if(type == MfClassicType4k) { - config->data_sector = 11; + config->data_sector = 8; config->keys = troika_4k_keys; } else { success = false; @@ -1573,20 +1595,39 @@ static bool troika_parse(const NfcDevice* device, FuriString* parsed_data) { FuriString* metro_result = furi_string_alloc(); FuriString* ground_result = furi_string_alloc(); + FuriString* tat_result = furi_string_alloc(); bool result1 = parse_transport_block(&data->block[32], metro_result); bool result2 = parse_transport_block(&data->block[28], ground_result); - if(result1 || result2) { + bool result3 = parse_transport_block(&data->block[16], tat_result); + furi_string_printf(parsed_data, "#Troyka\n"); + if(result1) { furi_string_printf( parsed_data, - "\e#Troika\n%s\n\e#Ediniy\n%s\n\e#TAT\n", - furi_string_get_cstr(metro_result), - furi_string_get_cstr(ground_result)); - furi_string_free(metro_result); - furi_string_free(ground_result); - parsed = true; - } else { - return false; + "%s\n\e#Metro\n%s\n", + furi_string_get_cstr(parsed_data), + furi_string_get_cstr(metro_result)); + FURI_LOG_D(TAG, "Metro branch"); } + if(result2) { + furi_string_printf( + parsed_data, + "%s\e#Ediniy\n%s\n", + furi_string_get_cstr(parsed_data), + furi_string_get_cstr(ground_result)); + FURI_LOG_D(TAG, "Ediny branch"); + } + if(result3) { + furi_string_printf( + parsed_data, + "%s\e#TAT\n%s\n", + furi_string_get_cstr(parsed_data), + furi_string_get_cstr(tat_result)); + FURI_LOG_D(TAG, "TAT branch"); + } + furi_string_free(tat_result); + furi_string_free(ground_result); + furi_string_free(metro_result); + parsed = result1 || result2 || result3; } while(false); return parsed; From 4a84fbc6e3045beff4c02f00bf889d3fcb8a6ab3 Mon Sep 17 00:00:00 2001 From: assasinfil Date: Tue, 21 Nov 2023 13:12:24 +0300 Subject: [PATCH 4/7] UI printing bugfix --- .../plugins/supported_cards/social_moscow.c | 52 ++++++++++++------- .../main/nfc/plugins/supported_cards/troika.c | 25 +++------ 2 files changed, 38 insertions(+), 39 deletions(-) diff --git a/applications/main/nfc/plugins/supported_cards/social_moscow.c b/applications/main/nfc/plugins/supported_cards/social_moscow.c index 79704ee54..4899cbf6a 100644 --- a/applications/main/nfc/plugins/supported_cards/social_moscow.c +++ b/applications/main/nfc/plugins/supported_cards/social_moscow.c @@ -1446,7 +1446,7 @@ static bool social_moscow_get_card_config(SocialMoscowCardConfig* config, MfClas bool success = true; if(type == MfClassicType4k) { - config->data_sector = 15; + config->data_sector = 32; config->keys = social_moscow_4k_keys; } else { success = false; @@ -1536,6 +1536,17 @@ static bool social_moscow_parse(const NfcDevice* device, FuriString* parsed_data bool parsed = false; do { + // Verify card type + SocialMoscowCardConfig cfg = {}; + if(!social_moscow_get_card_config(&cfg, data->type)) break; + + // Verify key + const MfClassicSectorTrailer* sec_tr = + mf_classic_get_sector_trailer_by_sector(data, cfg.data_sector); + + const uint64_t key = nfc_util_bytes2num(sec_tr->key_a.data, COUNT_OF(sec_tr->key_a.data)); + if(key != cfg.keys[cfg.data_sector].a) break; + uint32_t card_code = bit_lib_get_bits_32(data->block[60].data, 8, 24); uint8_t card_region = bit_lib_get_bits(data->block[60].data, 32, 8); uint64_t card_number = bit_lib_get_bits_64(data->block[60].data, 40, 40); @@ -1548,28 +1559,29 @@ static bool social_moscow_parse(const NfcDevice* device, FuriString* parsed_data FuriString* ground_result = furi_string_alloc(); bool result1 = parse_transport_block(&data->block[4], metro_result); bool result2 = parse_transport_block(&data->block[16], ground_result); - if(result1 || result2) { - furi_string_printf( - parsed_data, - "\e#Social \ecard\nNumber: %lx %x %llx %x\nOMC: %llx\nValid for: %02x/%02x %02x%02x\n\e#Metro\n%s\n\e#Ground\n%s", - card_code, - card_region, - card_number, - card_control, - omc_number, - month, - year, - data->block[60].data[13], - data->block[60].data[14], - furi_string_get_cstr(metro_result), - furi_string_get_cstr(ground_result)); - - parsed = true; - } else { - parsed = false; + furi_string_cat_printf( + parsed_data, + "\e#Social \ecard\nNumber: %lx %x %llx %x\nOMC: %llx\nValid for: %02x/%02x %02x%02x\n", + card_code, + card_region, + card_number, + card_control, + omc_number, + month, + year, + data->block[60].data[13], + data->block[60].data[14]); + if(result1) { + furi_string_cat_printf( + parsed_data, "\e#Metro\n%s\n", furi_string_get_cstr(metro_result)); + } + if(result2) { + furi_string_cat_printf( + parsed_data, "\e#Ground\n%s\n", furi_string_get_cstr(ground_result)); } furi_string_free(ground_result); furi_string_free(metro_result); + parsed = result1 || result2; } while(false); return parsed; diff --git a/applications/main/nfc/plugins/supported_cards/troika.c b/applications/main/nfc/plugins/supported_cards/troika.c index 5c44f086f..b714739cc 100644 --- a/applications/main/nfc/plugins/supported_cards/troika.c +++ b/applications/main/nfc/plugins/supported_cards/troika.c @@ -1599,30 +1599,17 @@ static bool troika_parse(const NfcDevice* device, FuriString* parsed_data) { bool result1 = parse_transport_block(&data->block[32], metro_result); bool result2 = parse_transport_block(&data->block[28], ground_result); bool result3 = parse_transport_block(&data->block[16], tat_result); - furi_string_printf(parsed_data, "#Troyka\n"); + furi_string_cat_printf(parsed_data, "\e#Troyka card\n"); if(result1) { - furi_string_printf( - parsed_data, - "%s\n\e#Metro\n%s\n", - furi_string_get_cstr(parsed_data), - furi_string_get_cstr(metro_result)); - FURI_LOG_D(TAG, "Metro branch"); + furi_string_cat_printf( + parsed_data, "\e#Metro\n%s\n", furi_string_get_cstr(metro_result)); } if(result2) { - furi_string_printf( - parsed_data, - "%s\e#Ediniy\n%s\n", - furi_string_get_cstr(parsed_data), - furi_string_get_cstr(ground_result)); - FURI_LOG_D(TAG, "Ediny branch"); + furi_string_cat_printf( + parsed_data, "\e#Ediniy\n%s\n", furi_string_get_cstr(ground_result)); } if(result3) { - furi_string_printf( - parsed_data, - "%s\e#TAT\n%s\n", - furi_string_get_cstr(parsed_data), - furi_string_get_cstr(tat_result)); - FURI_LOG_D(TAG, "TAT branch"); + furi_string_cat_printf(parsed_data, "\e#TAT\n%s\n", furi_string_get_cstr(tat_result)); } furi_string_free(tat_result); furi_string_free(ground_result); From 3daaea6ecfcd6b69278dc92130e25961dfd054a5 Mon Sep 17 00:00:00 2001 From: assasinfil Date: Tue, 21 Nov 2023 15:03:46 +0300 Subject: [PATCH 5/7] Added new social card --- .../plugins/supported_cards/social_moscow.c | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/applications/main/nfc/plugins/supported_cards/social_moscow.c b/applications/main/nfc/plugins/supported_cards/social_moscow.c index 4899cbf6a..3ccdadc1f 100644 --- a/applications/main/nfc/plugins/supported_cards/social_moscow.c +++ b/applications/main/nfc/plugins/supported_cards/social_moscow.c @@ -22,6 +22,24 @@ typedef struct { uint32_t data_sector; } SocialMoscowCardConfig; +static const MfClassicKeyPair social_moscow_1k_keys[] = { + {.a = 0xa0a1a2a3a4a5, .b = 0x7de02a7f6025}, + {.a = 0x2735fc181807, .b = 0xbf23a53c1f63}, + {.a = 0x2aba9519f574, .b = 0xcb9a1f2d7368}, + {.a = 0x84fd7f7a12b6, .b = 0xc7c0adb3284f}, + {.a = 0x73068f118c13, .b = 0x2b7f3253fac5}, + {.a = 0x186d8c4b93f9, .b = 0x9f131d8c2057}, + {.a = 0x3a4bba8adaf0, .b = 0x67362d90f973}, + {.a = 0x8765b17968a2, .b = 0x6202a38f69e2}, + {.a = 0x40ead80721ce, .b = 0x100533b89331}, + {.a = 0x0db5e6523f7c, .b = 0x653a87594079}, + {.a = 0x51119dae5216, .b = 0xd8a274b2e026}, + {.a = 0x51119dae5216, .b = 0xd8a274b2e026}, + {.a = 0x51119dae5216, .b = 0xd8a274b2e026}, + {.a = 0x2aba9519f574, .b = 0xcb9a1f2d7368}, + {.a = 0x84fd7f7a12b6, .b = 0xc7c0adb3284f}, + {.a = 0xa0a1a2a3a4a5, .b = 0x7de02a7f6025}}; + static const MfClassicKeyPair social_moscow_4k_keys[] = { {.a = 0xa0a1a2a3a4a5, .b = 0x7de02a7f6025}, {.a = 0x2735fc181807, .b = 0xbf23a53c1f63}, {.a = 0x2aba9519f574, .b = 0xcb9a1f2d7368}, {.a = 0x84fd7f7a12b6, .b = 0xc7c0adb3284f}, @@ -1444,8 +1462,10 @@ bool parse_transport_block(const MfClassicBlock* block, FuriString* result) { static bool social_moscow_get_card_config(SocialMoscowCardConfig* config, MfClassicType type) { bool success = true; - - if(type == MfClassicType4k) { + if(type == MfClassicType1k) { + config->data_sector = 32; + config->keys = social_moscow_1k_keys; + } else if(type == MfClassicType4k) { config->data_sector = 32; config->keys = social_moscow_4k_keys; } else { @@ -1483,7 +1503,8 @@ static bool social_moscow_verify_type(Nfc* nfc, MfClassicType type) { } static bool social_moscow_verify(Nfc* nfc) { - return social_moscow_verify_type(nfc, MfClassicType4k); + return social_moscow_verify_type(nfc, MfClassicType1k) || + social_moscow_verify_type(nfc, MfClassicType4k); } static bool social_moscow_read(Nfc* nfc, NfcDevice* device) { From 4261063c99e6d35fbd0dde22a6f5c4db765068cf Mon Sep 17 00:00:00 2001 From: Methodius Date: Wed, 22 Nov 2023 00:39:23 +0900 Subject: [PATCH 6/7] Metromoney transport card parser added --- applications/main/nfc/application.fam | 9 + .../nfc/plugins/supported_cards/metromoney.c | 191 ++++++++++++++++++ 2 files changed, 200 insertions(+) create mode 100644 applications/main/nfc/plugins/supported_cards/metromoney.c diff --git a/applications/main/nfc/application.fam b/applications/main/nfc/application.fam index dcfde7c36..44e3d842f 100644 --- a/applications/main/nfc/application.fam +++ b/applications/main/nfc/application.fam @@ -92,6 +92,15 @@ App( sources=["plugins/supported_cards/umarsh.c"], ) +App( + appid="metromoney_parser", + apptype=FlipperAppType.PLUGIN, + entry_point="metromoney_plugin_ep", + targets=["f7"], + requires=["nfc"], + sources=["plugins/supported_cards/metromoney.c"], +) + App( appid="nfc_start", targets=["f7"], diff --git a/applications/main/nfc/plugins/supported_cards/metromoney.c b/applications/main/nfc/plugins/supported_cards/metromoney.c new file mode 100644 index 000000000..263bbc44e --- /dev/null +++ b/applications/main/nfc/plugins/supported_cards/metromoney.c @@ -0,0 +1,191 @@ +/* + * Parser for Metromoney card (Georgia). + * + * Copyright 2023 Leptoptilos + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "nfc_supported_card_plugin.h" + +#include "protocols/mf_classic/mf_classic.h" +#include + +#include +#include +#include +#include + +#define TAG "Metromoney" + +typedef struct { + uint64_t a; + uint64_t b; +} MfClassicKeyPair; + +static const MfClassicKeyPair metromoney_1k_keys[] = { + {.a = 0x2803BCB0C7E1, .b = 0x4FA9EB49F75E}, + {.a = 0x9C616585E26D, .b = 0xD1C71E590D16}, + {.a = 0x9C616585E26D, .b = 0xA160FCD5EC4C}, + {.a = 0x9C616585E26D, .b = 0xA160FCD5EC4C}, + {.a = 0x9C616585E26D, .b = 0xA160FCD5EC4C}, + {.a = 0x9C616585E26D, .b = 0xA160FCD5EC4C}, + {.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF}, + {.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF}, + {.a = 0x112233445566, .b = 0x361A62F35BC9}, + {.a = 0x112233445566, .b = 0x361A62F35BC9}, + {.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF}, + {.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF}, + {.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF}, + {.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF}, + {.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF}, + {.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF}, +}; + +static bool metromoney_verify(Nfc* nfc) { + bool verified = false; + + do { + const uint8_t ticket_sector_number = 1; + const uint8_t ticket_block_number = + mf_classic_get_first_block_num_of_sector(ticket_sector_number) + 1; + FURI_LOG_D(TAG, "Verifying sector %u", ticket_sector_number); + + MfClassicKey key = {0}; + nfc_util_num2bytes( + metromoney_1k_keys[ticket_sector_number].a, COUNT_OF(key.data), key.data); + + MfClassicAuthContext auth_context; + MfClassicError error = mf_classic_poller_sync_auth( + nfc, ticket_block_number, &key, MfClassicKeyTypeA, &auth_context); + if(error != MfClassicErrorNone) { + FURI_LOG_D(TAG, "Failed to read block %u: %d", ticket_block_number, error); + break; + } + + verified = true; + } while(false); + + return verified; +} + +static bool metromoney_read(Nfc* nfc, NfcDevice* device) { + furi_assert(nfc); + furi_assert(device); + + bool is_read = false; + + MfClassicData* data = mf_classic_alloc(); + nfc_device_copy_data(device, NfcProtocolMfClassic, data); + + do { + MfClassicType type = MfClassicTypeMini; + MfClassicError error = mf_classic_poller_sync_detect_type(nfc, &type); + if(error != MfClassicErrorNone) break; + + data->type = type; + if(type != MfClassicType1k) break; + + MfClassicDeviceKeys keys = { + .key_a_mask = 0, + .key_b_mask = 0, + }; + for(size_t i = 0; i < mf_classic_get_total_sectors_num(data->type); i++) { + nfc_util_num2bytes(metromoney_1k_keys[i].a, sizeof(MfClassicKey), keys.key_a[i].data); + FURI_BIT_SET(keys.key_a_mask, i); + nfc_util_num2bytes(metromoney_1k_keys[i].b, sizeof(MfClassicKey), keys.key_b[i].data); + FURI_BIT_SET(keys.key_b_mask, i); + } + + error = mf_classic_poller_sync_read(nfc, &keys, data); + if(error != MfClassicErrorNone) { + FURI_LOG_W(TAG, "Failed to read data"); + break; + } + + nfc_device_set_data(device, NfcProtocolMfClassic, data); + + is_read = true; + } while(false); + + mf_classic_free(data); + + return is_read; +} + +static bool metromoney_parse(const NfcDevice* device, FuriString* parsed_data) { + furi_assert(device); + + const MfClassicData* data = nfc_device_get_data(device, NfcProtocolMfClassic); + + bool parsed = false; + + do { + // Verify key + const uint8_t ticket_sector_number = 1; + const uint8_t ticket_block_number = 1; + + const MfClassicSectorTrailer* sec_tr = + mf_classic_get_sector_trailer_by_sector(data, ticket_sector_number); + + const uint64_t key = nfc_util_bytes2num(sec_tr->key_a.data, COUNT_OF(sec_tr->key_a.data)); + if(key != metromoney_1k_keys[ticket_sector_number].a) break; + + // Parse data + const uint8_t start_block_num = + mf_classic_get_first_block_num_of_sector(ticket_sector_number); + + const uint8_t* block_start_ptr = + &data->block[start_block_num + ticket_block_number].data[0]; + + uint32_t balance = (block_start_ptr[3] << 24) | (block_start_ptr[2] << 16) | + (block_start_ptr[1] << 8) | (block_start_ptr[0]); + + uint32_t balance_lari = balance / 100; + uint8_t balance_tetri = balance % 100; + + size_t uid_len = 0; + const uint8_t* uid = mf_classic_get_uid(data, &uid_len); + uint32_t card_number = (uid[3] << 24) | (uid[2] << 16) | (uid[1] << 8) | (uid[0]); + + furi_string_printf( + parsed_data, + "\e#Metromoney\nCard number: %lu\nBalance: %lu.%02u GEL", + card_number, + balance_lari, + balance_tetri); + parsed = true; + } while(false); + + return parsed; +} + +/* Actual implementation of app<>plugin interface */ +static const NfcSupportedCardsPlugin metromoney_plugin = { + .protocol = NfcProtocolMfClassic, + .verify = metromoney_verify, + .read = metromoney_read, + .parse = metromoney_parse, +}; + +/* Plugin descriptor to comply with basic plugin specification */ +static const FlipperAppPluginDescriptor metromoney_plugin_descriptor = { + .appid = NFC_SUPPORTED_CARD_PLUGIN_APP_ID, + .ep_api_version = NFC_SUPPORTED_CARD_PLUGIN_API_VERSION, + .entry_point = &metromoney_plugin, +}; + +/* Plugin entry point - must return a pointer to const descriptor */ +const FlipperAppPluginDescriptor* metromoney_plugin_ep() { + return &metromoney_plugin_descriptor; +} \ No newline at end of file From c145cad65356244cb073c6ef896e4e742dbed79b Mon Sep 17 00:00:00 2001 From: assasinfil Date: Tue, 21 Nov 2023 22:34:29 +0300 Subject: [PATCH 7/7] Verify card bugfix --- .../main/nfc/plugins/supported_cards/social_moscow.c | 10 +++++----- applications/main/nfc/plugins/supported_cards/troika.c | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/applications/main/nfc/plugins/supported_cards/social_moscow.c b/applications/main/nfc/plugins/supported_cards/social_moscow.c index 3ccdadc1f..ca52661b2 100644 --- a/applications/main/nfc/plugins/supported_cards/social_moscow.c +++ b/applications/main/nfc/plugins/supported_cards/social_moscow.c @@ -10,7 +10,7 @@ #include #include -#define TAG "Social Moscow" +#define TAG "Social_Moscow" typedef struct { uint64_t a; @@ -1463,10 +1463,10 @@ bool parse_transport_block(const MfClassicBlock* block, FuriString* result) { static bool social_moscow_get_card_config(SocialMoscowCardConfig* config, MfClassicType type) { bool success = true; if(type == MfClassicType1k) { - config->data_sector = 32; + config->data_sector = 15; config->keys = social_moscow_1k_keys; } else if(type == MfClassicType4k) { - config->data_sector = 32; + config->data_sector = 15; config->keys = social_moscow_4k_keys; } else { success = false; @@ -1495,7 +1495,7 @@ static bool social_moscow_verify_type(Nfc* nfc, MfClassicType type) { FURI_LOG_D(TAG, "Failed to read block %u: %d", block_num, error); break; } - + FURI_LOG_D(TAG, "Verify success!"); verified = true; } while(false); @@ -1517,7 +1517,7 @@ static bool social_moscow_read(Nfc* nfc, NfcDevice* device) { nfc_device_copy_data(device, NfcProtocolMfClassic, data); do { - MfClassicType type = MfClassicTypeMini; + MfClassicType type = MfClassicType4k; MfClassicError error = mf_classic_poller_sync_detect_type(nfc, &type); if(error != MfClassicErrorNone) break; diff --git a/applications/main/nfc/plugins/supported_cards/troika.c b/applications/main/nfc/plugins/supported_cards/troika.c index b714739cc..3f252390f 100644 --- a/applications/main/nfc/plugins/supported_cards/troika.c +++ b/applications/main/nfc/plugins/supported_cards/troika.c @@ -1486,10 +1486,10 @@ static bool troika_get_card_config(TroikaCardConfig* config, MfClassicType type) bool success = true; if(type == MfClassicType1k) { - config->data_sector = 4; + config->data_sector = 11; config->keys = troika_1k_keys; } else if(type == MfClassicType4k) { - config->data_sector = 8; + config->data_sector = 11; config->keys = troika_4k_keys; } else { success = false; @@ -1518,7 +1518,7 @@ static bool troika_verify_type(Nfc* nfc, MfClassicType type) { FURI_LOG_D(TAG, "Failed to read block %u: %d", block_num, error); break; } - + FURI_LOG_D(TAG, "Verify success!"); verified = true; } while(false);