mirror of
https://github.com/DarkFlippers/unleashed-firmware
synced 2024-11-22 20:43:07 +00:00
Added NFC plugin; Some parser (#3335)
* Add parser * Fix microel.c * Fix NFC parser positive return * fix mizip * Fix NFC parser positive return * Add parse to hi! tag * fix false positive reading and kdf * hi formatting * fix oom in kdf * nfc app: fix types in hi plugin * nfc app: fix return in function body in microel plugin Co-authored-by: gornekich <n.gorbadey@gmail.com> Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
parent
6bc63b7734
commit
ebd09a1981
4 changed files with 738 additions and 0 deletions
|
@ -29,6 +29,33 @@ App(
|
|||
sources=["plugins/supported_cards/all_in_one.c"],
|
||||
)
|
||||
|
||||
App(
|
||||
appid="microel_parser",
|
||||
apptype=FlipperAppType.PLUGIN,
|
||||
entry_point="microel_plugin_ep",
|
||||
targets=["f7"],
|
||||
requires=["nfc"],
|
||||
sources=["plugins/supported_cards/microel.c"],
|
||||
)
|
||||
|
||||
App(
|
||||
appid="mizip_parser",
|
||||
apptype=FlipperAppType.PLUGIN,
|
||||
entry_point="mizip_plugin_ep",
|
||||
targets=["f7"],
|
||||
requires=["nfc"],
|
||||
sources=["plugins/supported_cards/mizip.c"],
|
||||
)
|
||||
|
||||
App(
|
||||
appid="hi_parser",
|
||||
apptype=FlipperAppType.PLUGIN,
|
||||
entry_point="hi_plugin_ep",
|
||||
targets=["f7"],
|
||||
requires=["nfc"],
|
||||
sources=["plugins/supported_cards/hi.c"],
|
||||
)
|
||||
|
||||
App(
|
||||
appid="opal_parser",
|
||||
apptype=FlipperAppType.PLUGIN,
|
||||
|
|
226
applications/main/nfc/plugins/supported_cards/hi.c
Normal file
226
applications/main/nfc/plugins/supported_cards/hi.c
Normal file
|
@ -0,0 +1,226 @@
|
|||
#include "nfc_supported_card_plugin.h"
|
||||
#include <flipper_application/flipper_application.h>
|
||||
#include <nfc/nfc_device.h>
|
||||
#include <nfc/helpers/nfc_util.h>
|
||||
#include <nfc/protocols/mf_classic/mf_classic_poller_sync.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define TAG "HI!"
|
||||
#define KEY_LENGTH 6
|
||||
#define HI_KEY_TO_GEN 5
|
||||
#define UID_LENGTH 7
|
||||
|
||||
typedef struct {
|
||||
uint64_t a;
|
||||
uint64_t b;
|
||||
} MfClassicKeyPair;
|
||||
|
||||
typedef struct {
|
||||
MfClassicKeyPair* keys;
|
||||
uint32_t verify_sector;
|
||||
} HiCardConfig;
|
||||
|
||||
static MfClassicKeyPair hi_1k_keys[] = {
|
||||
{.a = 0xa0a1a2a3a4a5, .b = 0x30871CF60CF1}, // 000
|
||||
{.a = 0x000000000000, .b = 0x000000000000}, // 001
|
||||
{.a = 0x000000000000, .b = 0x000000000000}, // 002
|
||||
{.a = 0x000000000000, .b = 0x000000000000}, // 003
|
||||
{.a = 0x000000000000, .b = 0x000000000000}, // 004
|
||||
{.a = 0x42FFE4C76209, .b = 0x7B30CFD04CBD}, // 005
|
||||
{.a = 0x01ED8145BDF8, .b = 0x92257F472FCE}, // 006
|
||||
{.a = 0x7583A07D21A6, .b = 0x51CA6EA8EE26}, // 007
|
||||
{.a = 0x1E10BF5D6A1D, .b = 0x87B9B9BFABA6}, // 008
|
||||
{.a = 0xF9DB1B2B21BA, .b = 0x80A781F4134C}, // 009
|
||||
{.a = 0x7F5283FACB72, .b = 0x73250009D75A}, // 010
|
||||
{.a = 0xE48E86A03078, .b = 0xCFFBBF08A254}, // 011
|
||||
{.a = 0x39AB26301F60, .b = 0xC71A6E532C83}, // 012
|
||||
{.a = 0xAD656C6C639F, .b = 0xFD9819CBD20A}, // 013
|
||||
{.a = 0xF0E15160DB3E, .b = 0x3F622D515ADD}, // 014
|
||||
{.a = 0x03F44E033C42, .b = 0x61E897875F46}, // 015
|
||||
};
|
||||
|
||||
//KDF
|
||||
void hi_generate_key(uint8_t* uid, uint8_t keyA[5][KEY_LENGTH], uint8_t keyB[5][KEY_LENGTH]) {
|
||||
// Static XOR table for key generation
|
||||
static const uint8_t xor_table_keyB[4][6] = {
|
||||
{0x1F, 0xC4, 0x4D, 0x94, 0x6A, 0x31},
|
||||
{0x12, 0xC1, 0x5C, 0x70, 0xDF, 0x31},
|
||||
{0x56, 0xF0, 0x13, 0x1B, 0x63, 0xF2},
|
||||
{0x4E, 0xFA, 0xC2, 0xF8, 0xC9, 0xCC}};
|
||||
|
||||
static const uint8_t xor_table_keyA[4][6] = {
|
||||
{0xB6, 0xE6, 0xAE, 0x72, 0x91, 0x0D},
|
||||
{0x6D, 0x38, 0x50, 0xFB, 0x42, 0x89},
|
||||
{0x1E, 0x5F, 0xC7, 0xED, 0xAA, 0x02},
|
||||
{0x7E, 0xB9, 0xCA, 0xF1, 0x9C, 0x59}};
|
||||
|
||||
// Permutation table for rearranging elements in uid
|
||||
static const uint8_t xorOrderA[6] = {0, 1, 2, 3, 0, 2};
|
||||
static const uint8_t xorOrderB[6] = {1, 3, 3, 2, 1, 0};
|
||||
|
||||
// Generate key based on uid and XOR table
|
||||
for(uint8_t j = 1; j < 5; j++) {
|
||||
for(uint8_t i = 0; i < 6; i++) {
|
||||
keyA[j][i] = uid[xorOrderA[i]] ^ xor_table_keyA[j - 1][i];
|
||||
keyB[j][i] = uid[xorOrderB[i]] ^ xor_table_keyB[j - 1][i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool hi_get_card_config(HiCardConfig* config, MfClassicType type) {
|
||||
bool success = true;
|
||||
|
||||
if(type == MfClassicType1k) {
|
||||
config->verify_sector = 0;
|
||||
config->keys = hi_1k_keys;
|
||||
} else {
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool hi_verify_type(Nfc* nfc, MfClassicType type) {
|
||||
bool verified = false;
|
||||
|
||||
do {
|
||||
HiCardConfig cfg = {};
|
||||
if(!hi_get_card_config(&cfg, type)) break;
|
||||
|
||||
const uint8_t block_num = mf_classic_get_first_block_num_of_sector(cfg.verify_sector);
|
||||
FURI_LOG_D(TAG, "Verifying sector %li", cfg.verify_sector);
|
||||
|
||||
MfClassicKey key = {0};
|
||||
nfc_util_num2bytes(cfg.keys[cfg.verify_sector].b, COUNT_OF(key.data), key.data);
|
||||
|
||||
MfClassicAuthContext auth_context;
|
||||
MfClassicError error =
|
||||
mf_classic_poller_sync_auth(nfc, block_num, &key, MfClassicKeyTypeB, &auth_context);
|
||||
if(error != MfClassicErrorNone) {
|
||||
FURI_LOG_D(
|
||||
TAG, "Failed to read block %u: %d, this is not a HI card", block_num, error);
|
||||
break;
|
||||
}
|
||||
FURI_LOG_D(TAG, "Found a HI Card");
|
||||
verified = true;
|
||||
} while(false);
|
||||
|
||||
return verified;
|
||||
}
|
||||
|
||||
static bool hi_verify(Nfc* nfc) {
|
||||
return hi_verify_type(nfc, MfClassicType1k);
|
||||
}
|
||||
|
||||
static bool hi_read(Nfc* nfc, NfcDevice* device) {
|
||||
FURI_LOG_D(TAG, "Entering HI KDF");
|
||||
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 = MfClassicType1k;
|
||||
MfClassicError error = mf_classic_poller_sync_detect_type(nfc, &type);
|
||||
if(error != MfClassicErrorNone) break;
|
||||
|
||||
HiCardConfig cfg = {};
|
||||
if(!hi_get_card_config(&cfg, data->type)) break;
|
||||
|
||||
uint8_t uid[UID_LENGTH];
|
||||
memcpy(uid, data->iso14443_3a_data->uid, UID_LENGTH);
|
||||
|
||||
uint8_t keyA[HI_KEY_TO_GEN][KEY_LENGTH];
|
||||
uint8_t keyB[HI_KEY_TO_GEN][KEY_LENGTH];
|
||||
hi_generate_key(uid, keyA, keyB);
|
||||
|
||||
for(size_t i = 0; i < mf_classic_get_total_sectors_num(data->type); i++) {
|
||||
if(cfg.keys[i].a == 0x000000000000 && cfg.keys[i].b == 0x000000000000) {
|
||||
cfg.keys[i].a = nfc_util_bytes2num(keyA[i], KEY_LENGTH);
|
||||
cfg.keys[i].b = nfc_util_bytes2num(keyB[i], KEY_LENGTH);
|
||||
}
|
||||
}
|
||||
|
||||
MfClassicDeviceKeys keys = {};
|
||||
for(size_t i = 0; i < mf_classic_get_total_sectors_num(data->type); i++) {
|
||||
nfc_util_num2bytes(cfg.keys[i].a, sizeof(MfClassicKey), keys.key_a[i].data);
|
||||
FURI_BIT_SET(keys.key_a_mask, i);
|
||||
nfc_util_num2bytes(cfg.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 = mf_classic_is_card_read(data);
|
||||
} while(false);
|
||||
|
||||
mf_classic_free(data);
|
||||
|
||||
return is_read;
|
||||
}
|
||||
|
||||
static bool hi_parse(const NfcDevice* device, FuriString* parsed_data) {
|
||||
furi_assert(device);
|
||||
furi_assert(parsed_data);
|
||||
|
||||
const MfClassicData* data = nfc_device_get_data(device, NfcProtocolMfClassic);
|
||||
|
||||
bool parsed = false;
|
||||
|
||||
do {
|
||||
// Verify card type
|
||||
HiCardConfig cfg = {};
|
||||
if(!hi_get_card_config(&cfg, data->type)) break;
|
||||
|
||||
// Verify key
|
||||
MfClassicSectorTrailer* sec_tr =
|
||||
mf_classic_get_sector_trailer_by_sector(data, cfg.verify_sector);
|
||||
uint64_t key = nfc_util_bytes2num(sec_tr->key_b.data, 6);
|
||||
if(key != cfg.keys[cfg.verify_sector].b) return false;
|
||||
|
||||
//Get UID
|
||||
uint8_t uid[UID_LENGTH];
|
||||
memcpy(uid, data->iso14443_3a_data->uid, UID_LENGTH);
|
||||
|
||||
//parse data
|
||||
furi_string_cat_printf(parsed_data, "\e#HI! Card\n");
|
||||
furi_string_cat_printf(parsed_data, "UID:");
|
||||
for(size_t i = 0; i < UID_LENGTH; i++) {
|
||||
furi_string_cat_printf(parsed_data, " %02X", uid[i]);
|
||||
}
|
||||
furi_string_cat_printf(parsed_data, "\n");
|
||||
|
||||
parsed = true;
|
||||
} while(false);
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
/* Actual implementation of app<>plugin interface */
|
||||
static const NfcSupportedCardsPlugin hi_plugin = {
|
||||
.protocol = NfcProtocolMfClassic,
|
||||
.verify = hi_verify,
|
||||
.read = hi_read,
|
||||
.parse = hi_parse,
|
||||
};
|
||||
|
||||
/* Plugin descriptor to comply with basic plugin specification */
|
||||
static const FlipperAppPluginDescriptor hi_plugin_descriptor = {
|
||||
.appid = NFC_SUPPORTED_CARD_PLUGIN_APP_ID,
|
||||
.ep_api_version = NFC_SUPPORTED_CARD_PLUGIN_API_VERSION,
|
||||
.entry_point = &hi_plugin,
|
||||
};
|
||||
|
||||
/* Plugin entry point - must return a pointer to const descriptor */
|
||||
const FlipperAppPluginDescriptor* hi_plugin_ep() {
|
||||
return &hi_plugin_descriptor;
|
||||
}
|
228
applications/main/nfc/plugins/supported_cards/microel.c
Normal file
228
applications/main/nfc/plugins/supported_cards/microel.c
Normal file
|
@ -0,0 +1,228 @@
|
|||
#include "nfc_supported_card_plugin.h"
|
||||
#include <flipper_application/flipper_application.h>
|
||||
#include <nfc/nfc_device.h>
|
||||
#include <nfc/helpers/nfc_util.h>
|
||||
#include <nfc/protocols/mf_classic/mf_classic_poller_sync.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define TAG "Microel"
|
||||
#define KEY_LENGTH 6
|
||||
#define UID_LENGTH 4
|
||||
|
||||
typedef struct {
|
||||
uint64_t a;
|
||||
uint64_t b;
|
||||
} MfClassicKeyPair;
|
||||
|
||||
static MfClassicKeyPair microel_1k_keys[] = {
|
||||
{.a = 0x000000000000, .b = 0x000000000000}, // 000
|
||||
{.a = 0x000000000000, .b = 0x000000000000}, // 001
|
||||
{.a = 0xffffffffffff, .b = 0xffffffffffff}, // 002
|
||||
{.a = 0xffffffffffff, .b = 0xffffffffffff}, // 003
|
||||
{.a = 0xffffffffffff, .b = 0xffffffffffff}, // 004
|
||||
{.a = 0xffffffffffff, .b = 0xffffffffffff}, // 005
|
||||
{.a = 0xffffffffffff, .b = 0xffffffffffff}, // 006
|
||||
{.a = 0xffffffffffff, .b = 0xffffffffffff}, // 007
|
||||
{.a = 0xffffffffffff, .b = 0xffffffffffff}, // 008
|
||||
{.a = 0xffffffffffff, .b = 0xffffffffffff}, // 009
|
||||
{.a = 0xffffffffffff, .b = 0xffffffffffff}, // 010
|
||||
{.a = 0xffffffffffff, .b = 0xffffffffffff}, // 011
|
||||
{.a = 0xffffffffffff, .b = 0xffffffffffff}, // 012
|
||||
{.a = 0xffffffffffff, .b = 0xffffffffffff}, // 013
|
||||
{.a = 0xffffffffffff, .b = 0xffffffffffff}, // 014
|
||||
{.a = 0xffffffffffff, .b = 0xffffffffffff}, // 015
|
||||
};
|
||||
|
||||
const uint8_t verify_sector = 1;
|
||||
|
||||
void calculateSumHex(const uint8_t* uid, size_t uidSize, uint8_t sumHex[]) {
|
||||
const uint8_t xorKey[] = {0x01, 0x92, 0xA7, 0x75, 0x2B, 0xF9};
|
||||
int sum = 0;
|
||||
|
||||
for(size_t i = 0; i < uidSize; i++) {
|
||||
sum += uid[i];
|
||||
}
|
||||
|
||||
int sumTwoDigits = sum % 256;
|
||||
|
||||
if(sumTwoDigits % 2 == 1) {
|
||||
sumTwoDigits += 2;
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < sizeof(xorKey); i++) {
|
||||
sumHex[i] = sumTwoDigits ^ xorKey[i];
|
||||
}
|
||||
}
|
||||
|
||||
void generateKeyA(const uint8_t* uid, uint8_t uidSize, uint8_t keyA[]) {
|
||||
uint8_t sumHex[6];
|
||||
calculateSumHex(uid, uidSize, sumHex);
|
||||
uint8_t firstCharacter = (sumHex[0] >> 4) & 0xF;
|
||||
|
||||
if(firstCharacter == 0x2 || firstCharacter == 0x3 || firstCharacter == 0xA ||
|
||||
firstCharacter == 0xB) {
|
||||
// XOR WITH 0x40
|
||||
for(size_t i = 0; i < sizeof(sumHex); i++) {
|
||||
keyA[i] = 0x40 ^ sumHex[i];
|
||||
}
|
||||
} else if(
|
||||
firstCharacter == 0x6 || firstCharacter == 0x7 || firstCharacter == 0xE ||
|
||||
firstCharacter == 0xF) {
|
||||
// XOR WITH 0xC0
|
||||
for(size_t i = 0; i < sizeof(sumHex); i++) {
|
||||
keyA[i] = 0xC0 ^ sumHex[i];
|
||||
}
|
||||
} else {
|
||||
//Key a is the same as sumHex
|
||||
for(size_t i = 0; i < sizeof(sumHex); i++) {
|
||||
keyA[i] = sumHex[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void generateKeyB(uint8_t keyA[], size_t keyASize, uint8_t keyB[]) {
|
||||
for(size_t i = 0; i < keyASize; i++) {
|
||||
keyB[i] = 0xFF ^ keyA[i];
|
||||
}
|
||||
}
|
||||
|
||||
static bool microel_read(Nfc* nfc, NfcDevice* device) {
|
||||
FURI_LOG_D(TAG, "Entering Microel KDF");
|
||||
|
||||
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 = MfClassicType1k;
|
||||
MfClassicError error = mf_classic_poller_sync_detect_type(nfc, &type);
|
||||
if(error != MfClassicErrorNone) break;
|
||||
|
||||
//Get UID and check if it is 4 bytes
|
||||
size_t uid_len;
|
||||
const uint8_t* uid = mf_classic_get_uid(data, &uid_len);
|
||||
FURI_LOG_D(TAG, "UID identified: %02X%02X%02X%02X", uid[0], uid[1], uid[2], uid[3]);
|
||||
if(uid_len != UID_LENGTH) break;
|
||||
|
||||
// Generate keys
|
||||
uint8_t keyA[KEY_LENGTH];
|
||||
uint8_t keyB[KEY_LENGTH];
|
||||
generateKeyA(uid, UID_LENGTH, keyA);
|
||||
generateKeyB(keyA, KEY_LENGTH, keyB);
|
||||
|
||||
// Check key 0a to verify if it is a microel card
|
||||
MfClassicKey key = {0};
|
||||
nfc_util_num2bytes(nfc_util_bytes2num(keyA, KEY_LENGTH), COUNT_OF(key.data), key.data);
|
||||
const uint8_t block_num = mf_classic_get_first_block_num_of_sector(0); // This is 0
|
||||
MfClassicAuthContext auth_context;
|
||||
error =
|
||||
mf_classic_poller_sync_auth(nfc, block_num, &key, MfClassicKeyTypeA, &auth_context);
|
||||
if(error != MfClassicErrorNone) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Save keys generated to stucture
|
||||
for(size_t i = 0; i < mf_classic_get_total_sectors_num(data->type); i++) {
|
||||
if(microel_1k_keys[i].a == 0x000000000000) {
|
||||
microel_1k_keys[i].a = nfc_util_bytes2num(keyA, KEY_LENGTH);
|
||||
}
|
||||
if(microel_1k_keys[i].b == 0x000000000000) {
|
||||
microel_1k_keys[i].b = nfc_util_bytes2num(keyB, KEY_LENGTH);
|
||||
}
|
||||
}
|
||||
MfClassicDeviceKeys keys = {};
|
||||
for(size_t i = 0; i < mf_classic_get_total_sectors_num(data->type); i++) {
|
||||
nfc_util_num2bytes(microel_1k_keys[i].a, sizeof(MfClassicKey), keys.key_a[i].data);
|
||||
FURI_BIT_SET(keys.key_a_mask, i);
|
||||
nfc_util_num2bytes(microel_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 = mf_classic_is_card_read(data);
|
||||
} while(false);
|
||||
|
||||
mf_classic_free(data);
|
||||
|
||||
return is_read;
|
||||
}
|
||||
|
||||
static bool microel_parse(const NfcDevice* device, FuriString* parsed_data) {
|
||||
furi_assert(device);
|
||||
furi_assert(parsed_data);
|
||||
|
||||
const MfClassicData* data = nfc_device_get_data(device, NfcProtocolMfClassic);
|
||||
|
||||
bool parsed = false;
|
||||
|
||||
do {
|
||||
//Get UID
|
||||
size_t uid_len;
|
||||
const uint8_t* uid = mf_classic_get_uid(data, &uid_len);
|
||||
if(uid_len != UID_LENGTH) break;
|
||||
|
||||
// Generate key from uid
|
||||
uint8_t keyA[KEY_LENGTH];
|
||||
generateKeyA(uid, UID_LENGTH, keyA);
|
||||
|
||||
// Verify key
|
||||
MfClassicSectorTrailer* sec_tr =
|
||||
mf_classic_get_sector_trailer_by_sector(data, verify_sector);
|
||||
uint64_t key = nfc_util_bytes2num(sec_tr->key_a.data, 6);
|
||||
uint64_t key_for_check_from_array = nfc_util_bytes2num(keyA, KEY_LENGTH);
|
||||
if(key != key_for_check_from_array) break;
|
||||
|
||||
//Get credit in block number 8
|
||||
const uint8_t* temp_ptr = data->block[4].data;
|
||||
uint16_t balance = (temp_ptr[6] << 8) | (temp_ptr[5]);
|
||||
uint16_t previus_balance = (data->block[5].data[6] << 8) | (data->block[5].data[5]);
|
||||
furi_string_cat_printf(parsed_data, "\e#Microel Card\n");
|
||||
furi_string_cat_printf(parsed_data, "UID:");
|
||||
for(size_t i = 0; i < UID_LENGTH; i++) {
|
||||
furi_string_cat_printf(parsed_data, " %02X", uid[i]);
|
||||
}
|
||||
furi_string_cat_printf(
|
||||
parsed_data, "\nCurrent Credit: %d.%02d E \n", balance / 100, balance % 100);
|
||||
furi_string_cat_printf(
|
||||
parsed_data,
|
||||
"Previus Credit: %d.%02d E \n",
|
||||
previus_balance / 100,
|
||||
previus_balance % 100);
|
||||
|
||||
parsed = true;
|
||||
} while(false);
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
/* Actual implementation of app<>plugin interface */
|
||||
static const NfcSupportedCardsPlugin microel_plugin = {
|
||||
.protocol = NfcProtocolMfClassic,
|
||||
.verify =
|
||||
NULL, // the verification I need is based on verifying the keys generated via uid and try to authenticate not like on mizip that there is default b0 but added verify in read function
|
||||
.read = microel_read,
|
||||
.parse = microel_parse,
|
||||
};
|
||||
|
||||
/* Plugin descriptor to comply with basic plugin specification */
|
||||
static const FlipperAppPluginDescriptor microel_plugin_descriptor = {
|
||||
.appid = NFC_SUPPORTED_CARD_PLUGIN_APP_ID,
|
||||
.ep_api_version = NFC_SUPPORTED_CARD_PLUGIN_API_VERSION,
|
||||
.entry_point = µel_plugin,
|
||||
};
|
||||
|
||||
/* Plugin entry point - must return a pointer to const descriptor */
|
||||
const FlipperAppPluginDescriptor* microel_plugin_ep() {
|
||||
return µel_plugin_descriptor;
|
||||
}
|
257
applications/main/nfc/plugins/supported_cards/mizip.c
Normal file
257
applications/main/nfc/plugins/supported_cards/mizip.c
Normal file
|
@ -0,0 +1,257 @@
|
|||
#include "nfc_supported_card_plugin.h"
|
||||
#include <flipper_application/flipper_application.h>
|
||||
#include <nfc/nfc_device.h>
|
||||
#include <nfc/helpers/nfc_util.h>
|
||||
#include <nfc/protocols/mf_classic/mf_classic_poller_sync.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define TAG "MiZIP"
|
||||
#define KEY_LENGTH 6
|
||||
#define MIZIP_KEY_TO_GEN 5
|
||||
#define UID_LENGTH 4
|
||||
|
||||
typedef struct {
|
||||
uint64_t a;
|
||||
uint64_t b;
|
||||
} MfClassicKeyPair;
|
||||
|
||||
typedef struct {
|
||||
MfClassicKeyPair* keys;
|
||||
uint32_t verify_sector;
|
||||
} MizipCardConfig;
|
||||
|
||||
static MfClassicKeyPair mizip_1k_keys[] = {
|
||||
{.a = 0xa0a1a2a3a4a5, .b = 0xb4c132439eef}, // 000
|
||||
{.a = 0x000000000000, .b = 0x000000000000}, // 001
|
||||
{.a = 0x000000000000, .b = 0x000000000000}, // 002
|
||||
{.a = 0x000000000000, .b = 0x000000000000}, // 003
|
||||
{.a = 0x000000000000, .b = 0x000000000000}, // 004
|
||||
{.a = 0x0222179AB995, .b = 0x13321774F9B5}, // 005
|
||||
{.a = 0xB25CBD76A7B4, .b = 0x7571359B4274}, // 006
|
||||
{.a = 0xDA857B4907CC, .b = 0xD26B856175F7}, // 007
|
||||
{.a = 0x16D85830C443, .b = 0x8F790871A21E}, // 008
|
||||
{.a = 0x88BD5098FC82, .b = 0xFCD0D77745E4}, // 009
|
||||
{.a = 0x983349449D78, .b = 0xEA2631FBDEDD}, // 010
|
||||
{.a = 0xC599F962F3D9, .b = 0x949B70C14845}, // 011
|
||||
{.a = 0x72E668846BE8, .b = 0x45490B5AD707}, // 012
|
||||
{.a = 0xBCA105E5685E, .b = 0x248DAF9D674D}, // 013
|
||||
{.a = 0x4F6FE072D1FD, .b = 0x4250A05575FA}, // 014
|
||||
{.a = 0x56438ABE8152, .b = 0x59A45912B311}, // 015
|
||||
};
|
||||
|
||||
static MfClassicKeyPair mizip_mini_keys[] = {
|
||||
{.a = 0xa0a1a2a3a4a5, .b = 0xb4c132439eef}, // 000
|
||||
{.a = 0x000000000000, .b = 0x000000000000}, // 001
|
||||
{.a = 0x000000000000, .b = 0x000000000000}, // 002
|
||||
{.a = 0x000000000000, .b = 0x000000000000}, // 003
|
||||
{.a = 0x000000000000, .b = 0x000000000000}, // 004
|
||||
};
|
||||
|
||||
//KDF
|
||||
void mizip_generate_key(uint8_t* uid, uint8_t keyA[5][KEY_LENGTH], uint8_t keyB[5][KEY_LENGTH]) {
|
||||
// Static XOR table for key generation
|
||||
static const uint8_t xor_table_keyA[4][6] = {
|
||||
{0x09, 0x12, 0x5A, 0x25, 0x89, 0xE5},
|
||||
{0xAB, 0x75, 0xC9, 0x37, 0x92, 0x2F},
|
||||
{0xE2, 0x72, 0x41, 0xAF, 0x2C, 0x09},
|
||||
{0x31, 0x7A, 0xB7, 0x2F, 0x44, 0x90}};
|
||||
|
||||
static const uint8_t xor_table_keyB[4][6] = {
|
||||
{0xF1, 0x2C, 0x84, 0x53, 0xD8, 0x21},
|
||||
{0x73, 0xE7, 0x99, 0xFE, 0x32, 0x41},
|
||||
{0xAA, 0x4D, 0x13, 0x76, 0x56, 0xAE},
|
||||
{0xB0, 0x13, 0x27, 0x27, 0x2D, 0xFD}};
|
||||
|
||||
// Permutation table for rearranging elements in uid
|
||||
static const uint8_t xorOrderA[6] = {0, 1, 2, 3, 0, 1};
|
||||
static const uint8_t xorOrderB[6] = {2, 3, 0, 1, 2, 3};
|
||||
|
||||
// Generate key based on uid and XOR table
|
||||
for(uint8_t j = 1; j < 5; j++) {
|
||||
for(uint8_t i = 0; i < 6; i++) {
|
||||
keyA[j][i] = uid[xorOrderA[i]] ^ xor_table_keyA[j - 1][i];
|
||||
keyB[j][i] = uid[xorOrderB[i]] ^ xor_table_keyB[j - 1][i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool mizip_get_card_config(MizipCardConfig* config, MfClassicType type) {
|
||||
bool success = true;
|
||||
|
||||
if(type == MfClassicType1k) {
|
||||
config->verify_sector = 0;
|
||||
config->keys = mizip_1k_keys;
|
||||
} else if(type == MfClassicTypeMini) {
|
||||
config->verify_sector = 0;
|
||||
config->keys = mizip_mini_keys;
|
||||
} else {
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool mizip_verify_type(Nfc* nfc, MfClassicType type) {
|
||||
bool verified = false;
|
||||
|
||||
do {
|
||||
MizipCardConfig cfg = {};
|
||||
if(!mizip_get_card_config(&cfg, type)) break;
|
||||
|
||||
const uint8_t block_num = mf_classic_get_first_block_num_of_sector(cfg.verify_sector);
|
||||
FURI_LOG_D(TAG, "Verifying sector %li", cfg.verify_sector);
|
||||
|
||||
MfClassicKey key = {0};
|
||||
nfc_util_num2bytes(cfg.keys[cfg.verify_sector].b, COUNT_OF(key.data), key.data);
|
||||
|
||||
MfClassicAuthContext auth_context;
|
||||
MfClassicError error =
|
||||
mf_classic_poller_sync_auth(nfc, block_num, &key, MfClassicKeyTypeB, &auth_context);
|
||||
if(error != MfClassicErrorNone) {
|
||||
FURI_LOG_D(
|
||||
TAG, "Failed to read block %u: %d, this is not a MiZIP card", block_num, error);
|
||||
break;
|
||||
}
|
||||
FURI_LOG_D(TAG, "Found a MiZIP Card");
|
||||
verified = true;
|
||||
} while(false);
|
||||
|
||||
return verified;
|
||||
}
|
||||
|
||||
static bool mizip_verify(Nfc* nfc) {
|
||||
return mizip_verify_type(nfc, MfClassicType1k) || mizip_verify_type(nfc, MfClassicTypeMini);
|
||||
}
|
||||
|
||||
static bool mizip_read(Nfc* nfc, NfcDevice* device) {
|
||||
FURI_LOG_D(TAG, "Entering MiZIP KDF");
|
||||
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;
|
||||
|
||||
//temp fix but fix mf_classic_poller_sync_detect_type because view type mfclassic1k and not verify mfmini
|
||||
data->type = MfClassicTypeMini;
|
||||
MizipCardConfig cfg = {};
|
||||
if(!mizip_get_card_config(&cfg, data->type)) break;
|
||||
|
||||
uint8_t uid[UID_LENGTH];
|
||||
memcpy(uid, data->iso14443_3a_data->uid, UID_LENGTH);
|
||||
|
||||
uint8_t keyA[MIZIP_KEY_TO_GEN][KEY_LENGTH];
|
||||
uint8_t keyB[MIZIP_KEY_TO_GEN][KEY_LENGTH];
|
||||
mizip_generate_key(uid, keyA, keyB);
|
||||
|
||||
for(size_t i = 0; i < mf_classic_get_total_sectors_num(data->type); i++) {
|
||||
if(cfg.keys[i].a == 0x000000000000 && cfg.keys[i].b == 0x000000000000) {
|
||||
cfg.keys[i].a = nfc_util_bytes2num(keyA[i], KEY_LENGTH);
|
||||
cfg.keys[i].b = nfc_util_bytes2num(keyB[i], KEY_LENGTH);
|
||||
}
|
||||
}
|
||||
|
||||
MfClassicDeviceKeys keys = {};
|
||||
for(size_t i = 0; i < mf_classic_get_total_sectors_num(data->type); i++) {
|
||||
nfc_util_num2bytes(cfg.keys[i].a, sizeof(MfClassicKey), keys.key_a[i].data);
|
||||
FURI_BIT_SET(keys.key_a_mask, i);
|
||||
nfc_util_num2bytes(cfg.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 = mf_classic_is_card_read(data);
|
||||
} while(false);
|
||||
|
||||
mf_classic_free(data);
|
||||
|
||||
return is_read;
|
||||
}
|
||||
|
||||
static bool mizip_parse(const NfcDevice* device, FuriString* parsed_data) {
|
||||
furi_assert(device);
|
||||
furi_assert(parsed_data);
|
||||
|
||||
const MfClassicData* data = nfc_device_get_data(device, NfcProtocolMfClassic);
|
||||
|
||||
bool parsed = false;
|
||||
|
||||
do {
|
||||
// Verify card type
|
||||
MizipCardConfig cfg = {};
|
||||
if(!mizip_get_card_config(&cfg, data->type)) break;
|
||||
|
||||
// Verify key
|
||||
MfClassicSectorTrailer* sec_tr =
|
||||
mf_classic_get_sector_trailer_by_sector(data, cfg.verify_sector);
|
||||
uint64_t key = nfc_util_bytes2num(sec_tr->key_b.data, 6);
|
||||
if(key != cfg.keys[cfg.verify_sector].b) return false;
|
||||
|
||||
//Get UID
|
||||
uint8_t uid[UID_LENGTH];
|
||||
memcpy(uid, data->iso14443_3a_data->uid, UID_LENGTH);
|
||||
|
||||
//Get credit
|
||||
uint8_t credit_pointer = 0x08;
|
||||
uint8_t previus_credit_pointer = 0x09;
|
||||
if(data->block[10].data[0] == 0x55) {
|
||||
credit_pointer = 0x09;
|
||||
previus_credit_pointer = 0x08;
|
||||
}
|
||||
uint16_t balance = (data->block[credit_pointer].data[2] << 8) |
|
||||
(data->block[credit_pointer].data[1]);
|
||||
uint16_t previus_balance = (data->block[previus_credit_pointer].data[2] << 8) |
|
||||
(data->block[previus_credit_pointer].data[1]);
|
||||
|
||||
//parse data
|
||||
furi_string_cat_printf(parsed_data, "\e#MiZIP Card\n");
|
||||
furi_string_cat_printf(parsed_data, "UID:");
|
||||
for(size_t i = 0; i < UID_LENGTH; i++) {
|
||||
furi_string_cat_printf(parsed_data, " %02X", uid[i]);
|
||||
}
|
||||
furi_string_cat_printf(
|
||||
parsed_data, "\nCurrent Credit: %d.%02d E \n", balance / 100, balance % 100);
|
||||
furi_string_cat_printf(
|
||||
parsed_data,
|
||||
"Previus Credit: %d.%02d E \n",
|
||||
previus_balance / 100,
|
||||
previus_balance % 100);
|
||||
|
||||
parsed = true;
|
||||
} while(false);
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
/* Actual implementation of app<>plugin interface */
|
||||
static const NfcSupportedCardsPlugin mizip_plugin = {
|
||||
.protocol = NfcProtocolMfClassic,
|
||||
.verify = mizip_verify,
|
||||
.read = mizip_read,
|
||||
.parse = mizip_parse,
|
||||
};
|
||||
|
||||
/* Plugin descriptor to comply with basic plugin specification */
|
||||
static const FlipperAppPluginDescriptor mizip_plugin_descriptor = {
|
||||
.appid = NFC_SUPPORTED_CARD_PLUGIN_APP_ID,
|
||||
.ep_api_version = NFC_SUPPORTED_CARD_PLUGIN_API_VERSION,
|
||||
.entry_point = &mizip_plugin,
|
||||
};
|
||||
|
||||
/* Plugin entry point - must return a pointer to const descriptor */
|
||||
const FlipperAppPluginDescriptor* mizip_plugin_ep() {
|
||||
return &mizip_plugin_descriptor;
|
||||
}
|
Loading…
Reference in a new issue