Skip to content

Commit 972054b

Browse files
authored
Merge pull request #655 from DarkFlippers/nfc-parsers
Nfc parsers
2 parents e1c8983 + c145cad commit 972054b

File tree

5 files changed

+462
-59
lines changed

5 files changed

+462
-59
lines changed

applications/main/nfc/application.fam

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ App(
8383
sources=["plugins/supported_cards/two_cities.c"],
8484
)
8585

86+
App(
87+
appid="umarsh_parser",
88+
apptype=FlipperAppType.PLUGIN,
89+
entry_point="umarsh_plugin_ep",
90+
targets=["f7"],
91+
requires=["nfc"],
92+
sources=["plugins/supported_cards/umarsh.c"],
93+
)
94+
95+
App(
96+
appid="metromoney_parser",
97+
apptype=FlipperAppType.PLUGIN,
98+
entry_point="metromoney_plugin_ep",
99+
targets=["f7"],
100+
requires=["nfc"],
101+
sources=["plugins/supported_cards/metromoney.c"],
102+
)
103+
86104
App(
87105
appid="nfc_start",
88106
targets=["f7"],
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/*
2+
* Parser for Metromoney card (Georgia).
3+
*
4+
* Copyright 2023 Leptoptilos <[email protected]>
5+
*
6+
* This program is free software: you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful, but
12+
* WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#include "nfc_supported_card_plugin.h"
20+
21+
#include "protocols/mf_classic/mf_classic.h"
22+
#include <flipper_application/flipper_application.h>
23+
24+
#include <nfc/nfc_device.h>
25+
#include <nfc/helpers/nfc_util.h>
26+
#include <nfc/protocols/mf_classic/mf_classic_poller_sync.h>
27+
#include <stdint.h>
28+
29+
#define TAG "Metromoney"
30+
31+
typedef struct {
32+
uint64_t a;
33+
uint64_t b;
34+
} MfClassicKeyPair;
35+
36+
static const MfClassicKeyPair metromoney_1k_keys[] = {
37+
{.a = 0x2803BCB0C7E1, .b = 0x4FA9EB49F75E},
38+
{.a = 0x9C616585E26D, .b = 0xD1C71E590D16},
39+
{.a = 0x9C616585E26D, .b = 0xA160FCD5EC4C},
40+
{.a = 0x9C616585E26D, .b = 0xA160FCD5EC4C},
41+
{.a = 0x9C616585E26D, .b = 0xA160FCD5EC4C},
42+
{.a = 0x9C616585E26D, .b = 0xA160FCD5EC4C},
43+
{.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF},
44+
{.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF},
45+
{.a = 0x112233445566, .b = 0x361A62F35BC9},
46+
{.a = 0x112233445566, .b = 0x361A62F35BC9},
47+
{.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF},
48+
{.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF},
49+
{.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF},
50+
{.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF},
51+
{.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF},
52+
{.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF},
53+
};
54+
55+
static bool metromoney_verify(Nfc* nfc) {
56+
bool verified = false;
57+
58+
do {
59+
const uint8_t ticket_sector_number = 1;
60+
const uint8_t ticket_block_number =
61+
mf_classic_get_first_block_num_of_sector(ticket_sector_number) + 1;
62+
FURI_LOG_D(TAG, "Verifying sector %u", ticket_sector_number);
63+
64+
MfClassicKey key = {0};
65+
nfc_util_num2bytes(
66+
metromoney_1k_keys[ticket_sector_number].a, COUNT_OF(key.data), key.data);
67+
68+
MfClassicAuthContext auth_context;
69+
MfClassicError error = mf_classic_poller_sync_auth(
70+
nfc, ticket_block_number, &key, MfClassicKeyTypeA, &auth_context);
71+
if(error != MfClassicErrorNone) {
72+
FURI_LOG_D(TAG, "Failed to read block %u: %d", ticket_block_number, error);
73+
break;
74+
}
75+
76+
verified = true;
77+
} while(false);
78+
79+
return verified;
80+
}
81+
82+
static bool metromoney_read(Nfc* nfc, NfcDevice* device) {
83+
furi_assert(nfc);
84+
furi_assert(device);
85+
86+
bool is_read = false;
87+
88+
MfClassicData* data = mf_classic_alloc();
89+
nfc_device_copy_data(device, NfcProtocolMfClassic, data);
90+
91+
do {
92+
MfClassicType type = MfClassicTypeMini;
93+
MfClassicError error = mf_classic_poller_sync_detect_type(nfc, &type);
94+
if(error != MfClassicErrorNone) break;
95+
96+
data->type = type;
97+
if(type != MfClassicType1k) break;
98+
99+
MfClassicDeviceKeys keys = {
100+
.key_a_mask = 0,
101+
.key_b_mask = 0,
102+
};
103+
for(size_t i = 0; i < mf_classic_get_total_sectors_num(data->type); i++) {
104+
nfc_util_num2bytes(metromoney_1k_keys[i].a, sizeof(MfClassicKey), keys.key_a[i].data);
105+
FURI_BIT_SET(keys.key_a_mask, i);
106+
nfc_util_num2bytes(metromoney_1k_keys[i].b, sizeof(MfClassicKey), keys.key_b[i].data);
107+
FURI_BIT_SET(keys.key_b_mask, i);
108+
}
109+
110+
error = mf_classic_poller_sync_read(nfc, &keys, data);
111+
if(error != MfClassicErrorNone) {
112+
FURI_LOG_W(TAG, "Failed to read data");
113+
break;
114+
}
115+
116+
nfc_device_set_data(device, NfcProtocolMfClassic, data);
117+
118+
is_read = true;
119+
} while(false);
120+
121+
mf_classic_free(data);
122+
123+
return is_read;
124+
}
125+
126+
static bool metromoney_parse(const NfcDevice* device, FuriString* parsed_data) {
127+
furi_assert(device);
128+
129+
const MfClassicData* data = nfc_device_get_data(device, NfcProtocolMfClassic);
130+
131+
bool parsed = false;
132+
133+
do {
134+
// Verify key
135+
const uint8_t ticket_sector_number = 1;
136+
const uint8_t ticket_block_number = 1;
137+
138+
const MfClassicSectorTrailer* sec_tr =
139+
mf_classic_get_sector_trailer_by_sector(data, ticket_sector_number);
140+
141+
const uint64_t key = nfc_util_bytes2num(sec_tr->key_a.data, COUNT_OF(sec_tr->key_a.data));
142+
if(key != metromoney_1k_keys[ticket_sector_number].a) break;
143+
144+
// Parse data
145+
const uint8_t start_block_num =
146+
mf_classic_get_first_block_num_of_sector(ticket_sector_number);
147+
148+
const uint8_t* block_start_ptr =
149+
&data->block[start_block_num + ticket_block_number].data[0];
150+
151+
uint32_t balance = (block_start_ptr[3] << 24) | (block_start_ptr[2] << 16) |
152+
(block_start_ptr[1] << 8) | (block_start_ptr[0]);
153+
154+
uint32_t balance_lari = balance / 100;
155+
uint8_t balance_tetri = balance % 100;
156+
157+
size_t uid_len = 0;
158+
const uint8_t* uid = mf_classic_get_uid(data, &uid_len);
159+
uint32_t card_number = (uid[3] << 24) | (uid[2] << 16) | (uid[1] << 8) | (uid[0]);
160+
161+
furi_string_printf(
162+
parsed_data,
163+
"\e#Metromoney\nCard number: %lu\nBalance: %lu.%02u GEL",
164+
card_number,
165+
balance_lari,
166+
balance_tetri);
167+
parsed = true;
168+
} while(false);
169+
170+
return parsed;
171+
}
172+
173+
/* Actual implementation of app<>plugin interface */
174+
static const NfcSupportedCardsPlugin metromoney_plugin = {
175+
.protocol = NfcProtocolMfClassic,
176+
.verify = metromoney_verify,
177+
.read = metromoney_read,
178+
.parse = metromoney_parse,
179+
};
180+
181+
/* Plugin descriptor to comply with basic plugin specification */
182+
static const FlipperAppPluginDescriptor metromoney_plugin_descriptor = {
183+
.appid = NFC_SUPPORTED_CARD_PLUGIN_APP_ID,
184+
.ep_api_version = NFC_SUPPORTED_CARD_PLUGIN_API_VERSION,
185+
.entry_point = &metromoney_plugin,
186+
};
187+
188+
/* Plugin entry point - must return a pointer to const descriptor */
189+
const FlipperAppPluginDescriptor* metromoney_plugin_ep() {
190+
return &metromoney_plugin_descriptor;
191+
}

applications/main/nfc/plugins/supported_cards/social_moscow.c

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <furi_hal_rtc.h>
1111
#include <core/check.h>
1212

13-
#define TAG "Social Moscow"
13+
#define TAG "Social_Moscow"
1414

1515
typedef struct {
1616
uint64_t a;
@@ -22,6 +22,24 @@ typedef struct {
2222
uint32_t data_sector;
2323
} SocialMoscowCardConfig;
2424

25+
static const MfClassicKeyPair social_moscow_1k_keys[] = {
26+
{.a = 0xa0a1a2a3a4a5, .b = 0x7de02a7f6025},
27+
{.a = 0x2735fc181807, .b = 0xbf23a53c1f63},
28+
{.a = 0x2aba9519f574, .b = 0xcb9a1f2d7368},
29+
{.a = 0x84fd7f7a12b6, .b = 0xc7c0adb3284f},
30+
{.a = 0x73068f118c13, .b = 0x2b7f3253fac5},
31+
{.a = 0x186d8c4b93f9, .b = 0x9f131d8c2057},
32+
{.a = 0x3a4bba8adaf0, .b = 0x67362d90f973},
33+
{.a = 0x8765b17968a2, .b = 0x6202a38f69e2},
34+
{.a = 0x40ead80721ce, .b = 0x100533b89331},
35+
{.a = 0x0db5e6523f7c, .b = 0x653a87594079},
36+
{.a = 0x51119dae5216, .b = 0xd8a274b2e026},
37+
{.a = 0x51119dae5216, .b = 0xd8a274b2e026},
38+
{.a = 0x51119dae5216, .b = 0xd8a274b2e026},
39+
{.a = 0x2aba9519f574, .b = 0xcb9a1f2d7368},
40+
{.a = 0x84fd7f7a12b6, .b = 0xc7c0adb3284f},
41+
{.a = 0xa0a1a2a3a4a5, .b = 0x7de02a7f6025}};
42+
2543
static const MfClassicKeyPair social_moscow_4k_keys[] = {
2644
{.a = 0xa0a1a2a3a4a5, .b = 0x7de02a7f6025}, {.a = 0x2735fc181807, .b = 0xbf23a53c1f63},
2745
{.a = 0x2aba9519f574, .b = 0xcb9a1f2d7368}, {.a = 0x84fd7f7a12b6, .b = 0xc7c0adb3284f},
@@ -511,7 +529,7 @@ void from_minutes_to_datetime(uint32_t minutes, FuriHalRtcDateTime* datetime, ui
511529
bool parse_transport_block(const MfClassicBlock* block, FuriString* result) {
512530
uint16_t transport_departament = bit_lib_get_bits_16(block->data, 0, 10);
513531

514-
FURI_LOG_D(TAG, "Transport departament: %x", transport_departament);
532+
FURI_LOG_I(TAG, "Transport departament: %x", transport_departament);
515533

516534
uint16_t layout_type = bit_lib_get_bits_16(block->data, 52, 4);
517535
if(layout_type == 0xE) {
@@ -520,7 +538,7 @@ bool parse_transport_block(const MfClassicBlock* block, FuriString* result) {
520538
layout_type = bit_lib_get_bits_16(block->data, 52, 14);
521539
}
522540

523-
FURI_LOG_D(TAG, "Layout type %x", layout_type);
541+
FURI_LOG_I(TAG, "Layout type %x", layout_type);
524542

525543
uint16_t card_view = 0;
526544
uint16_t card_type = 0;
@@ -1444,8 +1462,10 @@ bool parse_transport_block(const MfClassicBlock* block, FuriString* result) {
14441462

14451463
static bool social_moscow_get_card_config(SocialMoscowCardConfig* config, MfClassicType type) {
14461464
bool success = true;
1447-
1448-
if(type == MfClassicType4k) {
1465+
if(type == MfClassicType1k) {
1466+
config->data_sector = 15;
1467+
config->keys = social_moscow_1k_keys;
1468+
} else if(type == MfClassicType4k) {
14491469
config->data_sector = 15;
14501470
config->keys = social_moscow_4k_keys;
14511471
} else {
@@ -1475,7 +1495,7 @@ static bool social_moscow_verify_type(Nfc* nfc, MfClassicType type) {
14751495
FURI_LOG_D(TAG, "Failed to read block %u: %d", block_num, error);
14761496
break;
14771497
}
1478-
1498+
FURI_LOG_D(TAG, "Verify success!");
14791499
verified = true;
14801500
} while(false);
14811501

@@ -1497,7 +1517,7 @@ static bool social_moscow_read(Nfc* nfc, NfcDevice* device) {
14971517
nfc_device_copy_data(device, NfcProtocolMfClassic, data);
14981518

14991519
do {
1500-
MfClassicType type = MfClassicTypeMini;
1520+
MfClassicType type = MfClassicType4k;
15011521
MfClassicError error = mf_classic_poller_sync_detect_type(nfc, &type);
15021522
if(error != MfClassicErrorNone) break;
15031523

@@ -1537,6 +1557,17 @@ static bool social_moscow_parse(const NfcDevice* device, FuriString* parsed_data
15371557
bool parsed = false;
15381558

15391559
do {
1560+
// Verify card type
1561+
SocialMoscowCardConfig cfg = {};
1562+
if(!social_moscow_get_card_config(&cfg, data->type)) break;
1563+
1564+
// Verify key
1565+
const MfClassicSectorTrailer* sec_tr =
1566+
mf_classic_get_sector_trailer_by_sector(data, cfg.data_sector);
1567+
1568+
const uint64_t key = nfc_util_bytes2num(sec_tr->key_a.data, COUNT_OF(sec_tr->key_a.data));
1569+
if(key != cfg.keys[cfg.data_sector].a) break;
1570+
15401571
uint32_t card_code = bit_lib_get_bits_32(data->block[60].data, 8, 24);
15411572
uint8_t card_region = bit_lib_get_bits(data->block[60].data, 32, 8);
15421573
uint64_t card_number = bit_lib_get_bits_64(data->block[60].data, 40, 40);
@@ -1549,27 +1580,29 @@ static bool social_moscow_parse(const NfcDevice* device, FuriString* parsed_data
15491580
FuriString* ground_result = furi_string_alloc();
15501581
bool result1 = parse_transport_block(&data->block[4], metro_result);
15511582
bool result2 = parse_transport_block(&data->block[16], ground_result);
1552-
if(result1 || result2) {
1553-
furi_string_printf(
1554-
parsed_data,
1555-
"\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",
1556-
card_code,
1557-
card_region,
1558-
card_number,
1559-
card_control,
1560-
omc_number,
1561-
month,
1562-
year,
1563-
data->block[60].data[13],
1564-
data->block[60].data[14],
1565-
furi_string_get_cstr(metro_result),
1566-
furi_string_get_cstr(ground_result));
1567-
furi_string_free(metro_result);
1568-
furi_string_free(ground_result);
1569-
parsed = true;
1570-
} else {
1571-
return false;
1583+
furi_string_cat_printf(
1584+
parsed_data,
1585+
"\e#Social \ecard\nNumber: %lx %x %llx %x\nOMC: %llx\nValid for: %02x/%02x %02x%02x\n",
1586+
card_code,
1587+
card_region,
1588+
card_number,
1589+
card_control,
1590+
omc_number,
1591+
month,
1592+
year,
1593+
data->block[60].data[13],
1594+
data->block[60].data[14]);
1595+
if(result1) {
1596+
furi_string_cat_printf(
1597+
parsed_data, "\e#Metro\n%s\n", furi_string_get_cstr(metro_result));
1598+
}
1599+
if(result2) {
1600+
furi_string_cat_printf(
1601+
parsed_data, "\e#Ground\n%s\n", furi_string_get_cstr(ground_result));
15721602
}
1603+
furi_string_free(ground_result);
1604+
furi_string_free(metro_result);
1605+
parsed = result1 || result2;
15731606
} while(false);
15741607

15751608
return parsed;

0 commit comments

Comments
 (0)