diff --git a/utils/nfc-mfultralight.c b/utils/nfc-mfultralight.c index 4d25114..01752ef 100644 --- a/utils/nfc-mfultralight.c +++ b/utils/nfc-mfultralight.c @@ -64,6 +64,18 @@ static mifare_param mp; static mifareul_tag mtDump; static uint32_t uiBlocks = 0xF; +// special unlock command +uint8_t abtUnlock1[1] = { 0x40 }; +uint8_t abtUnlock2[1] = { 0x43 }; + +//Halt command +uint8_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 }; + +#define MAX_FRAME_LEN 264 + +static uint8_t abtRx[MAX_FRAME_LEN]; +static int szRxBits; + static const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, @@ -107,10 +119,77 @@ read_card(void) return (!bFailure); } +static bool +transmit_bits(const uint8_t *pbtTx, const size_t szTxBits) +{ + // Transmit the bit frame command, we don't use the arbitrary parity feature + if ((szRxBits = nfc_initiator_transceive_bits(pnd, pbtTx, szTxBits, NULL, abtRx, sizeof(abtRx), NULL)) < 0) + return false; + + return true; +} + + +static bool +transmit_bytes(const uint8_t *pbtTx, const size_t szTx) +{ + int res; + if ((res = nfc_initiator_transceive_bytes(pnd, pbtTx, szTx, abtRx, sizeof(abtRx), 0)) < 0) + return false; + + return true; +} + +static bool +unlock_card(void) +{ + // Configure the CRC + if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) { + nfc_perror(pnd, "nfc_configure"); + return false; + } + // Use raw send/receive methods + if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) { + nfc_perror(pnd, "nfc_configure"); + return false; + } + + iso14443a_crc_append(abtHalt, 2); + transmit_bytes(abtHalt, 4); + // now send unlock + if (!transmit_bits(abtUnlock1, 7)) { + printf("unlock failure!\n"); + return false; + } + if (!transmit_bytes(abtUnlock2, 1)) { + printf("unlock failure!\n"); + return false; + } + + // reset reader + // Configure the CRC + if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true) < 0) { + nfc_perror(pnd, "nfc_device_set_property_bool"); + return false; + } + // Switch off raw send/receive methods + if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0) { + nfc_perror(pnd, "nfc_device_set_property_bool"); + return false; + } + return true; +} + static bool check_magic() { bool bFailure = false; int uid_data; - + + //Initially check if we can unlock via the MF method + if (unlock_card()) { + printf("Ultralight Magic Gen 2 Detected\n"); + return true; + } + for (uint32_t page = 0; page <= 1; page++) { // Show if the readout went well if (bFailure) { @@ -140,7 +219,8 @@ static bool check_magic() { } else { return false; } - + + printf("Ultralight Magic Gen 1 Detected\n"); return true; } @@ -183,6 +263,8 @@ write_card(bool write_otp, bool write_lock, bool write_uid) if (!write_uid) { printf("ss"); uiSkippedPages = 2; + } else { + unlock_card(); } for (int page = uiSkippedPages; page <= 0xF; page++) {