2010-01-15 11:18:11 +01:00
|
|
|
/*-
|
2013-03-10 16:15:23 +01:00
|
|
|
* Free/Libre Near Field Communication (NFC) library
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2013-03-10 16:15:23 +01:00
|
|
|
* Libnfc historical contributors:
|
|
|
|
* Copyright (C) 2009 Roel Verdult
|
|
|
|
* Copyright (C) 2009-2013 Romuald Conty
|
|
|
|
* Copyright (C) 2010-2012 Romain Tartière
|
2017-02-27 17:22:42 +01:00
|
|
|
* Copyright (C) 2010-2017 Philippe Teuwen
|
2013-03-10 16:15:23 +01:00
|
|
|
* Copyright (C) 2012-2013 Ludovic Rousseau
|
2013-07-17 13:57:56 +02:00
|
|
|
* See AUTHORS file for a more comprehensive list of contributors.
|
2013-03-10 16:15:23 +01:00
|
|
|
* Additional contributors of this file:
|
2018-09-15 17:19:35 +02:00
|
|
|
* Copyright (C) 2013-2018 Adam Laurie
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2010-11-17 15:27:11 +01:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* 1) Redistributions of source code must retain the above copyright notice,
|
2012-05-29 02:33:22 +02:00
|
|
|
* this list of conditions and the following disclaimer.
|
2010-11-17 15:27:11 +01:00
|
|
|
* 2 )Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2010-11-17 15:27:11 +01:00
|
|
|
* Note that this license only applies on the examples, NFC library itself is under LGPL
|
2009-10-12 16:52:26 +02:00
|
|
|
*
|
2010-01-15 11:18:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2011-02-28 10:47:31 +01:00
|
|
|
* @file nfc-mfultralight.c
|
2010-10-04 14:37:43 +02:00
|
|
|
* @brief MIFARE Ultralight dump/restore tool
|
2009-10-12 16:52:26 +02:00
|
|
|
*/
|
2009-09-02 22:15:21 +02:00
|
|
|
|
2010-01-15 11:18:11 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-06-24 12:19:01 +02:00
|
|
|
# include "config.h"
|
2010-01-15 11:18:11 +01:00
|
|
|
#endif // HAVE_CONFIG_H
|
|
|
|
|
2009-09-02 22:15:21 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2009-12-01 15:23:00 +01:00
|
|
|
#include <nfc/nfc.h>
|
2009-11-02 12:34:58 +01:00
|
|
|
|
2011-03-05 10:07:43 +01:00
|
|
|
#include "nfc-utils.h"
|
2010-06-15 17:33:22 +02:00
|
|
|
#include "mifare.h"
|
2009-09-02 22:15:21 +02:00
|
|
|
|
2016-05-11 09:20:42 +02:00
|
|
|
#define MAX_TARGET_COUNT 16
|
|
|
|
#define MAX_UID_LEN 10
|
|
|
|
|
2018-09-17 15:37:09 +02:00
|
|
|
#define EV1_NONE 0
|
|
|
|
#define EV1_UL11 1
|
|
|
|
#define EV1_UL21 2
|
2018-09-15 17:19:35 +02:00
|
|
|
#define EV1_NTAG213 3
|
|
|
|
#define EV1_NTAG215 4
|
|
|
|
#define EV1_NTAG216 5
|
2017-03-23 17:04:02 +01:00
|
|
|
|
2018-09-17 15:37:09 +02:00
|
|
|
#define NTAG_NONE 0
|
|
|
|
#define NTAG_213 1
|
|
|
|
#define NTAG_215 2
|
|
|
|
#define NTAG_216 3
|
|
|
|
|
2011-11-23 16:55:40 +01:00
|
|
|
static nfc_device *pnd;
|
|
|
|
static nfc_target nt;
|
2009-09-02 22:15:21 +02:00
|
|
|
static mifare_param mp;
|
2018-09-15 17:19:35 +02:00
|
|
|
static maxtag mtDump; // use the largest tag type for internal storage
|
2017-04-01 00:25:14 +02:00
|
|
|
static uint32_t uiBlocks = 0x10;
|
2017-03-23 17:04:02 +01:00
|
|
|
static uint32_t uiReadPages = 0;
|
|
|
|
static uint8_t iPWD[4] = { 0x0 };
|
|
|
|
static uint8_t iPACK[2] = { 0x0 };
|
2017-04-01 00:46:12 +02:00
|
|
|
static uint8_t iEV1Type = EV1_NONE;
|
2018-09-17 15:37:09 +02:00
|
|
|
static uint8_t iNTAGType = NTAG_NONE;
|
2009-11-27 18:02:53 +01:00
|
|
|
|
2016-04-08 17:32:14 +02:00
|
|
|
// special unlock command
|
|
|
|
uint8_t abtUnlock1[1] = { 0x40 };
|
|
|
|
uint8_t abtUnlock2[1] = { 0x43 };
|
|
|
|
|
2017-03-23 00:05:48 +01:00
|
|
|
// EV1 commands
|
|
|
|
uint8_t abtEV1[3] = { 0x60, 0x00, 0x00 };
|
|
|
|
uint8_t abtPWAuth[7] = { 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
|
2016-04-08 17:32:14 +02:00
|
|
|
//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;
|
2017-03-23 00:05:48 +01:00
|
|
|
static int szRx;
|
2016-04-08 17:32:14 +02:00
|
|
|
|
2011-11-23 16:55:40 +01:00
|
|
|
static const nfc_modulation nmMifare = {
|
2010-10-13 19:43:23 +02:00
|
|
|
.nmt = NMT_ISO14443A,
|
|
|
|
.nbr = NBR_106,
|
|
|
|
};
|
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
static void
|
2017-02-27 17:22:42 +01:00
|
|
|
print_success_or_failure(bool bFailure, uint32_t *uiOkCounter, uint32_t *uiFailedCounter)
|
2009-11-27 18:02:53 +01:00
|
|
|
{
|
2017-02-27 17:22:42 +01:00
|
|
|
printf("%c", (bFailure) ? 'f' : '.');
|
|
|
|
if (uiOkCounter)
|
|
|
|
*uiOkCounter += (bFailure) ? 0 : 1;
|
|
|
|
if (uiFailedCounter)
|
|
|
|
*uiFailedCounter += (bFailure) ? 1 : 0;
|
2009-11-27 18:02:53 +01:00
|
|
|
}
|
2009-09-02 22:15:21 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
static bool
|
2012-05-29 17:54:36 +02:00
|
|
|
read_card(void)
|
2009-09-02 22:15:21 +02:00
|
|
|
{
|
2009-11-27 18:02:53 +01:00
|
|
|
uint32_t page;
|
2010-09-07 19:51:03 +02:00
|
|
|
bool bFailure = false;
|
2017-02-27 17:22:42 +01:00
|
|
|
uint32_t uiFailedPages = 0;
|
2009-11-27 18:02:53 +01:00
|
|
|
|
2017-04-01 00:25:14 +02:00
|
|
|
printf("Reading %d pages |", uiBlocks);
|
2009-11-27 18:02:53 +01:00
|
|
|
|
2017-04-01 00:25:14 +02:00
|
|
|
for (page = 0; page < uiBlocks; page += 4) {
|
2010-06-24 12:19:01 +02:00
|
|
|
// Try to read out the data block
|
2012-05-29 17:54:36 +02:00
|
|
|
if (nfc_initiator_mifare_cmd(pnd, MC_READ, page, &mp)) {
|
2018-09-15 17:19:35 +02:00
|
|
|
memcpy(mtDump.ul[page / 4].mbd.abtData, mp.mpd.abtData, uiBlocks - page < 4 ? (uiBlocks - page) * 4 : 16);
|
2010-06-24 12:19:01 +02:00
|
|
|
} else {
|
|
|
|
bFailure = true;
|
|
|
|
}
|
2017-04-01 00:46:12 +02:00
|
|
|
for (uint8_t i = 0; i < (uiBlocks - page < 4 ? uiBlocks - page : 4); i++) {
|
2017-04-01 00:25:14 +02:00
|
|
|
print_success_or_failure(bFailure, &uiReadPages, &uiFailedPages);
|
|
|
|
}
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("|\n");
|
2017-04-01 00:25:14 +02:00
|
|
|
printf("Done, %d of %d pages read (%d pages failed).\n", uiReadPages, uiBlocks, uiFailedPages);
|
2012-05-29 17:54:36 +02:00
|
|
|
fflush(stdout);
|
2009-11-27 18:02:53 +01:00
|
|
|
|
2017-03-23 17:04:02 +01:00
|
|
|
// copy EV1 secrets to dump data
|
2017-04-01 00:46:12 +02:00
|
|
|
switch (iEV1Type) {
|
2017-03-23 17:04:02 +01:00
|
|
|
case EV1_UL11:
|
2018-09-15 17:19:35 +02:00
|
|
|
memcpy(mtDump.ul[4].mbc11.pwd, iPWD, 4);
|
|
|
|
memcpy(mtDump.ul[4].mbc11.pack, iPACK, 2);
|
2017-03-23 17:04:02 +01:00
|
|
|
break;
|
|
|
|
case EV1_UL21:
|
2018-09-15 17:19:35 +02:00
|
|
|
memcpy(mtDump.ul[9].mbc21a.pwd, iPWD, 4);
|
|
|
|
memcpy(mtDump.ul[9].mbc21b.pack, iPACK, 2);
|
|
|
|
break;
|
|
|
|
case EV1_NTAG213:
|
|
|
|
memcpy(mtDump.nt[43].mbc21356d.pwd, iPWD, 4);
|
|
|
|
memcpy(mtDump.nt[44].mbc21356e.pack, iPACK, 2);
|
|
|
|
break;
|
|
|
|
case EV1_NTAG215:
|
|
|
|
memcpy(mtDump.nt[133].mbc21356d.pwd, iPWD, 4);
|
|
|
|
memcpy(mtDump.nt[134].mbc21356e.pack, iPACK, 2);
|
|
|
|
break;
|
|
|
|
case EV1_NTAG216:
|
|
|
|
memcpy(mtDump.nt[229].mbc21356d.pwd, iPWD, 4);
|
|
|
|
memcpy(mtDump.nt[230].mbc21356e.pack, iPACK, 2);
|
2017-03-23 17:04:02 +01:00
|
|
|
break;
|
|
|
|
case EV1_NONE:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-11-27 18:02:53 +01:00
|
|
|
return (!bFailure);
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
|
|
|
|
2016-04-08 17:32:14 +02:00
|
|
|
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)
|
|
|
|
{
|
2017-03-23 00:05:48 +01:00
|
|
|
if ((szRx = nfc_initiator_transceive_bytes(pnd, pbtTx, szTx, abtRx, sizeof(abtRx), 0)) < 0)
|
2016-04-08 17:32:14 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-03-23 00:05:48 +01:00
|
|
|
raw_mode_start(void)
|
2016-04-08 17:32:14 +02:00
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
2017-03-23 00:05:48 +01:00
|
|
|
return true;
|
|
|
|
}
|
2016-04-08 17:32:14 +02:00
|
|
|
|
2017-03-23 00:05:48 +01:00
|
|
|
static bool
|
|
|
|
raw_mode_end(void)
|
|
|
|
{
|
2016-04-08 17:32:14 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2017-03-23 00:05:48 +01:00
|
|
|
static bool
|
|
|
|
get_ev1_version(void)
|
|
|
|
{
|
|
|
|
if (!raw_mode_start())
|
|
|
|
return false;
|
|
|
|
iso14443a_crc_append(abtEV1, 1);
|
2017-04-01 00:46:12 +02:00
|
|
|
if (!transmit_bytes(abtEV1, 3)) {
|
2017-03-23 00:05:48 +01:00
|
|
|
raw_mode_end();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!raw_mode_end())
|
|
|
|
return false;
|
2017-04-01 00:46:12 +02:00
|
|
|
if (!szRx)
|
|
|
|
return false;
|
2017-03-23 00:05:48 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
ev1_load_pwd(uint8_t target[4], const char *pwd)
|
|
|
|
{
|
2017-04-01 01:03:41 +02:00
|
|
|
unsigned int tmp[4];
|
|
|
|
if (sscanf(pwd, "%2x%2x%2x%2x", &tmp[0], &tmp[1], &tmp[2], &tmp[3]) != 4)
|
2017-03-23 00:05:48 +01:00
|
|
|
return false;
|
2017-04-01 01:03:41 +02:00
|
|
|
target[0] = tmp[0];
|
|
|
|
target[1] = tmp[1];
|
|
|
|
target[2] = tmp[2];
|
|
|
|
target[3] = tmp[3];
|
2017-03-23 00:05:48 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
ev1_pwd_auth(uint8_t *pwd)
|
|
|
|
{
|
|
|
|
if (!raw_mode_start())
|
|
|
|
return false;
|
|
|
|
memcpy(&abtPWAuth[1], pwd, 4);
|
|
|
|
iso14443a_crc_append(abtPWAuth, 5);
|
2017-04-01 00:46:12 +02:00
|
|
|
if (!transmit_bytes(abtPWAuth, 7))
|
2017-03-23 00:05:48 +01:00
|
|
|
return false;
|
|
|
|
if (!raw_mode_end())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
unlock_card(void)
|
|
|
|
{
|
|
|
|
if (!raw_mode_start())
|
|
|
|
return false;
|
|
|
|
iso14443a_crc_append(abtHalt, 2);
|
|
|
|
transmit_bytes(abtHalt, 4);
|
|
|
|
// now send unlock
|
|
|
|
if (!transmit_bits(abtUnlock1, 7)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!transmit_bytes(abtUnlock2, 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!raw_mode_end())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-18 13:05:59 +01:00
|
|
|
static bool check_magic()
|
|
|
|
{
|
|
|
|
bool bFailure = false;
|
|
|
|
int uid_data;
|
2016-04-08 17:32:14 +02:00
|
|
|
|
2017-02-18 13:05:59 +01:00
|
|
|
for (uint32_t page = 0; page <= 1; page++) {
|
2015-11-18 23:03:36 +01:00
|
|
|
// Show if the readout went well
|
|
|
|
if (bFailure) {
|
|
|
|
// When a failure occured we need to redo the anti-collision
|
|
|
|
if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) {
|
|
|
|
ERR("tag was removed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bFailure = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uid_data = 0x00000000;
|
2016-05-11 09:35:48 +02:00
|
|
|
|
2015-11-18 23:03:36 +01:00
|
|
|
memcpy(mp.mpd.abtData, &uid_data, sizeof uid_data);
|
|
|
|
memset(mp.mpd.abtData + 4, 0, 12);
|
2016-05-11 09:35:48 +02:00
|
|
|
|
2015-11-18 23:03:36 +01:00
|
|
|
//Force the write without checking for errors - otherwise the writes to the sector 0 seem to complain
|
|
|
|
nfc_initiator_mifare_cmd(pnd, MC_WRITE, page, &mp);
|
|
|
|
}
|
2016-05-11 09:35:48 +02:00
|
|
|
|
2017-02-18 13:05:59 +01:00
|
|
|
//Check that the ID is now set to 0x000000000000
|
|
|
|
if (nfc_initiator_mifare_cmd(pnd, MC_READ, 0, &mp)) {
|
|
|
|
//printf("%u", mp.mpd.abtData);
|
|
|
|
bool result = true;
|
|
|
|
for (int i = 0; i <= 7; i++) {
|
|
|
|
if (mp.mpd.abtData[i] != 0x00) result = false;
|
2016-05-11 09:35:48 +02:00
|
|
|
}
|
2016-04-08 19:42:01 +02:00
|
|
|
|
2017-02-18 13:05:59 +01:00
|
|
|
if (result) {
|
2016-04-08 19:42:01 +02:00
|
|
|
return true;
|
2015-11-18 23:03:36 +01:00
|
|
|
}
|
2016-04-08 17:32:14 +02:00
|
|
|
|
2017-02-18 13:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Initially check if we can unlock via the MF method
|
|
|
|
if (unlock_card()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-18 23:03:36 +01:00
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
static bool
|
2018-09-17 17:58:49 +02:00
|
|
|
write_card(bool write_otp, bool write_lock, bool write_dyn_lock, bool write_uid)
|
2009-09-02 22:15:21 +02:00
|
|
|
{
|
|
|
|
uint32_t uiBlock = 0;
|
2010-09-07 19:51:03 +02:00
|
|
|
bool bFailure = false;
|
2017-02-17 10:11:34 +01:00
|
|
|
uint32_t uiWrittenPages = 0;
|
2013-08-31 17:15:27 +02:00
|
|
|
uint32_t uiSkippedPages = 0;
|
2017-02-27 17:22:42 +01:00
|
|
|
uint32_t uiFailedPages = 0;
|
2009-09-02 22:15:21 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
char buffer[BUFSIZ];
|
2010-08-10 21:50:29 +02:00
|
|
|
|
2018-09-17 17:58:49 +02:00
|
|
|
// NTAG does not have explicit OTP bytes
|
|
|
|
if (!write_otp && iNTAGType == NTAG_NONE) {
|
|
|
|
printf("Write OTP Bytes ? [yN] ");
|
2015-11-18 08:56:48 +01:00
|
|
|
if (!fgets(buffer, BUFSIZ, stdin)) {
|
|
|
|
ERR("Unable to read standard input.");
|
|
|
|
}
|
|
|
|
write_otp = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
|
2011-02-10 11:38:21 +01:00
|
|
|
}
|
2015-11-18 08:56:48 +01:00
|
|
|
|
2018-09-17 17:58:49 +02:00
|
|
|
// Lock Bytes are OTP if set, so warn
|
2016-05-11 09:35:48 +02:00
|
|
|
if (!write_lock) {
|
2018-09-17 17:58:49 +02:00
|
|
|
printf("Write Lock Bytes (Warning: OTP if set) ? [yN] ");
|
2015-11-18 08:56:48 +01:00
|
|
|
if (!fgets(buffer, BUFSIZ, stdin)) {
|
|
|
|
ERR("Unable to read standard input.");
|
|
|
|
}
|
|
|
|
write_lock = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
|
2011-02-10 11:38:21 +01:00
|
|
|
}
|
2015-11-18 08:56:48 +01:00
|
|
|
|
2018-09-17 17:58:49 +02:00
|
|
|
// NTAG and MF0UL21 have additional lock bytes
|
|
|
|
if (!write_dyn_lock && (iNTAGType != NTAG_NONE || iEV1Type == EV1_UL21)) {
|
|
|
|
printf("Write Dynamic Lock Bytes ? [yN] ");
|
|
|
|
if (!fgets(buffer, BUFSIZ, stdin)) {
|
|
|
|
ERR("Unable to read standard input.");
|
|
|
|
}
|
|
|
|
write_dyn_lock = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
|
|
|
|
}
|
|
|
|
|
2016-05-11 09:35:48 +02:00
|
|
|
if (!write_uid) {
|
2015-11-18 08:56:48 +01:00
|
|
|
printf("Write UID bytes (only for special writeable UID cards) ? [yN] ");
|
|
|
|
if (!fgets(buffer, BUFSIZ, stdin)) {
|
|
|
|
ERR("Unable to read standard input.");
|
|
|
|
}
|
|
|
|
write_uid = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
|
2013-08-31 17:15:27 +02:00
|
|
|
}
|
2010-06-30 17:06:59 +02:00
|
|
|
|
2017-04-01 00:25:14 +02:00
|
|
|
printf("Writing %d pages |", uiBlocks);
|
2013-08-31 17:15:27 +02:00
|
|
|
/* We may need to skip 2 first pages. */
|
2013-08-31 17:35:41 +02:00
|
|
|
if (!write_uid) {
|
2013-08-31 17:15:27 +02:00
|
|
|
printf("ss");
|
|
|
|
uiSkippedPages = 2;
|
2016-04-08 17:32:14 +02:00
|
|
|
} else {
|
2016-04-08 19:42:01 +02:00
|
|
|
if (!check_magic()) {
|
|
|
|
printf("\nUnable to unlock card - are you sure the card is magic?\n");
|
2016-05-11 16:27:52 +02:00
|
|
|
return false;
|
2016-04-08 19:42:01 +02:00
|
|
|
}
|
2013-08-31 17:15:27 +02:00
|
|
|
}
|
2011-01-04 10:55:43 +01:00
|
|
|
|
2017-04-01 00:25:14 +02:00
|
|
|
for (uint32_t page = uiSkippedPages; page < uiBlocks; page++) {
|
2018-09-17 17:58:49 +02:00
|
|
|
if ((!write_lock) && page == 0x2) {
|
|
|
|
printf("s");
|
|
|
|
uiSkippedPages++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// NTAG doesn't have OTP blocks
|
|
|
|
if ((iNTAGType == NTAG_NONE && page == 0x3) && (!write_otp)) {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("s");
|
2011-01-04 10:55:43 +01:00
|
|
|
uiSkippedPages++;
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-17 17:58:49 +02:00
|
|
|
// NTAG and MF0UL21 have Dynamic Lock Bytes
|
|
|
|
if (((iEV1Type == EV1_UL21 && page == 0x24) || \
|
|
|
|
(iNTAGType == NTAG_213 && page == 0x28) || \
|
|
|
|
(iNTAGType == NTAG_215 && page == 0x82) || \
|
|
|
|
(iNTAGType == NTAG_216 && page == 0xe2)) && (!write_dyn_lock)) {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("s");
|
2011-01-04 10:55:43 +01:00
|
|
|
uiSkippedPages++;
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-27 17:22:42 +01:00
|
|
|
// Check if the previous readout went well
|
2010-06-24 12:19:01 +02:00
|
|
|
if (bFailure) {
|
|
|
|
// When a failure occured we need to redo the anti-collision
|
2013-02-01 20:52:49 +01:00
|
|
|
if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) {
|
2012-05-29 17:54:36 +02:00
|
|
|
ERR("tag was removed");
|
2010-06-24 12:19:01 +02:00
|
|
|
return false;
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
2010-06-24 12:19:01 +02:00
|
|
|
bFailure = false;
|
|
|
|
}
|
2010-06-30 16:54:34 +02:00
|
|
|
// For the Mifare Ultralight, this write command can be used
|
2012-05-29 02:33:22 +02:00
|
|
|
// in compatibility mode, which only actually writes the first
|
2010-06-30 16:54:34 +02:00
|
|
|
// page (4 bytes). The Ultralight-specific Write command only
|
|
|
|
// writes one page at a time.
|
|
|
|
uiBlock = page / 4;
|
2018-09-15 17:19:35 +02:00
|
|
|
memcpy(mp.mpd.abtData, mtDump.ul[uiBlock].mbd.abtData + ((page % 4) * 4), 4);
|
2014-04-14 23:26:44 +02:00
|
|
|
memset(mp.mpd.abtData + 4, 0, 12);
|
2012-05-29 17:54:36 +02:00
|
|
|
if (!nfc_initiator_mifare_cmd(pnd, MC_WRITE, page, &mp))
|
2010-09-07 19:51:03 +02:00
|
|
|
bFailure = true;
|
2017-02-27 17:22:42 +01:00
|
|
|
print_success_or_failure(bFailure, &uiWrittenPages, &uiFailedPages);
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("|\n");
|
2017-04-01 00:25:14 +02:00
|
|
|
printf("Done, %d of %d pages written (%d pages skipped, %d pages failed).\n", uiWrittenPages, uiBlocks, uiSkippedPages, uiFailedPages);
|
2009-10-02 11:52:02 +02:00
|
|
|
|
2009-09-02 22:15:21 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-11 09:20:42 +02:00
|
|
|
static int list_passive_targets(nfc_device *_pnd)
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
nfc_target ant[MAX_TARGET_COUNT];
|
|
|
|
|
|
|
|
if (nfc_initiator_init(_pnd) < 0) {
|
|
|
|
return -EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((res = nfc_initiator_list_passive_targets(_pnd, nmMifare, ant, MAX_TARGET_COUNT)) >= 0) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (res > 0)
|
|
|
|
printf("%d ISO14443A passive target(s) found:\n", res);
|
|
|
|
|
|
|
|
for (i = 0; i < res; i++) {
|
|
|
|
size_t szPos;
|
|
|
|
|
|
|
|
printf("\t");
|
|
|
|
for (szPos = 0; szPos < ant[i].nti.nai.szUidLen; szPos++) {
|
|
|
|
printf("%02x", ant[i].nti.nai.abtUid[szPos]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t str_to_uid(const char *str, uint8_t *uid)
|
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
|
|
|
|
memset(uid, 0x0, MAX_UID_LEN);
|
|
|
|
i = 0;
|
2017-02-18 13:05:59 +01:00
|
|
|
while ((*str != '\0') && ((i >> 1) < MAX_UID_LEN)) {
|
2016-05-11 09:20:42 +02:00
|
|
|
char nibble[2] = { 0x00, '\n' }; /* for strtol */
|
|
|
|
|
|
|
|
nibble[0] = *str++;
|
|
|
|
if (isxdigit(nibble[0])) {
|
|
|
|
if (isupper(nibble[0]))
|
|
|
|
nibble[0] = tolower(nibble[0]);
|
|
|
|
uid[i >> 1] |= strtol(nibble, NULL, 16) << ((i % 2) ? 0 : 4) & ((i % 2) ? 0x0f : 0xf0);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i >> 1;
|
|
|
|
}
|
|
|
|
|
2015-11-18 08:56:48 +01:00
|
|
|
static void
|
|
|
|
print_usage(const char *argv[])
|
|
|
|
{
|
|
|
|
printf("Usage: %s r|w <dump.mfd> [OPTIONS]\n", argv[0]);
|
2017-03-23 19:19:59 +01:00
|
|
|
printf("Arguments:\n");
|
|
|
|
printf("\tr|w - Perform read or write\n");
|
|
|
|
printf("\t<dump.mfd> - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n");
|
2015-11-18 08:56:48 +01:00
|
|
|
printf("Options:\n");
|
2018-09-17 17:58:49 +02:00
|
|
|
printf("\t--otp - Don't prompt for OTP Bytes writing (Assume yes)\n");
|
|
|
|
printf("\t--lock - Don't prompt for Lock Bytes (OTP) writing (Assume yes)\n");
|
|
|
|
printf("\t--dynlock - Don't prompt for Dynamic Lock Bytes writing (Assume yes)\n");
|
2017-03-23 19:19:59 +01:00
|
|
|
printf("\t--uid - Don't prompt for UID writing (Assume yes)\n");
|
2018-09-17 17:58:49 +02:00
|
|
|
printf("\t--full - Assume full card write (UID + OTP + Lockbytes + Dynamic Lockbytes)\n");
|
2017-03-23 19:19:59 +01:00
|
|
|
printf("\t--with-uid <UID> - Specify UID to read/write from\n");
|
|
|
|
printf("\t--pw <PWD> - Specify 8 HEX digit PASSWORD for EV1\n");
|
|
|
|
printf("\t--partial - Allow source data size to be other than tag capacity\n");
|
2015-11-18 08:56:48 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
int
|
2012-05-29 17:54:36 +02:00
|
|
|
main(int argc, const char *argv[])
|
2010-06-24 12:19:01 +02:00
|
|
|
{
|
2015-11-18 23:03:36 +01:00
|
|
|
int iAction = 0;
|
2018-09-15 17:19:35 +02:00
|
|
|
size_t iDumpSize = sizeof(mifareul_tag);
|
2016-05-11 09:20:42 +02:00
|
|
|
uint8_t iUID[MAX_UID_LEN] = { 0x0 };
|
|
|
|
size_t szUID = 0;
|
2015-11-18 23:03:36 +01:00
|
|
|
bool bOTP = false;
|
|
|
|
bool bLock = false;
|
2018-09-17 17:58:49 +02:00
|
|
|
bool bDynLock = false;
|
2015-11-18 23:03:36 +01:00
|
|
|
bool bUID = false;
|
2017-03-23 00:05:48 +01:00
|
|
|
bool bPWD = false;
|
2017-03-23 19:19:59 +01:00
|
|
|
bool bPart = false;
|
2017-03-31 23:21:17 +02:00
|
|
|
bool bFilename = false;
|
2010-09-07 19:51:03 +02:00
|
|
|
FILE *pfDump;
|
2010-06-24 12:19:01 +02:00
|
|
|
|
2017-03-31 23:21:17 +02:00
|
|
|
if (argc < 3) {
|
2017-02-18 13:05:59 +01:00
|
|
|
print_usage(argv);
|
|
|
|
exit(EXIT_FAILURE);
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
DBG("\nChecking arguments and settings\n");
|
2009-09-02 22:15:21 +02:00
|
|
|
|
2015-11-18 08:56:48 +01:00
|
|
|
// Get commandline options
|
|
|
|
for (int arg = 1; arg < argc; arg++) {
|
|
|
|
if (0 == strcmp(argv[arg], "r")) {
|
2015-11-18 23:03:36 +01:00
|
|
|
iAction = 1;
|
2015-11-18 08:56:48 +01:00
|
|
|
} else if (0 == strcmp(argv[arg], "w")) {
|
2015-11-18 23:03:36 +01:00
|
|
|
iAction = 2;
|
2016-05-11 09:20:42 +02:00
|
|
|
} else if (0 == strcmp(argv[arg], "--with-uid")) {
|
2017-04-01 00:46:12 +02:00
|
|
|
if (arg + 1 == argc) {
|
2016-05-11 09:20:42 +02:00
|
|
|
ERR("Please supply a UID of 4, 7 or 10 bytes long. Ex: a1:b2:c3:d4");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-03-23 00:05:48 +01:00
|
|
|
szUID = str_to_uid(argv[++arg], iUID);
|
2015-11-18 08:56:48 +01:00
|
|
|
} else if (0 == strcmp(argv[arg], "--full")) {
|
|
|
|
bOTP = true;
|
|
|
|
bLock = true;
|
2018-09-17 17:58:49 +02:00
|
|
|
bDynLock = true;
|
2015-11-18 08:56:48 +01:00
|
|
|
bUID = true;
|
|
|
|
} else if (0 == strcmp(argv[arg], "--otp")) {
|
|
|
|
bOTP = true;
|
|
|
|
} else if (0 == strcmp(argv[arg], "--lock")) {
|
|
|
|
bLock = true;
|
2018-09-17 17:58:49 +02:00
|
|
|
} else if (0 == strcmp(argv[arg], "--dynlock")) {
|
|
|
|
bDynLock = true;
|
2015-11-18 08:56:48 +01:00
|
|
|
} else if (0 == strcmp(argv[arg], "--uid")) {
|
|
|
|
bUID = true;
|
2015-11-18 23:03:36 +01:00
|
|
|
} else if (0 == strcmp(argv[arg], "--check-magic")) {
|
|
|
|
iAction = 3;
|
2017-03-23 19:19:59 +01:00
|
|
|
} else if (0 == strcmp(argv[arg], "--partial")) {
|
2017-04-01 00:46:12 +02:00
|
|
|
bPart = true;
|
2017-03-23 00:05:48 +01:00
|
|
|
} else if (0 == strcmp(argv[arg], "--pw")) {
|
2017-04-01 00:46:12 +02:00
|
|
|
bPWD = true;
|
|
|
|
if (arg + 1 == argc || strlen(argv[++arg]) != 8 || ! ev1_load_pwd(iPWD, argv[arg])) {
|
2017-03-23 00:05:48 +01:00
|
|
|
ERR("Please supply a PASSWORD of 8 HEX digits");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2015-11-18 08:56:48 +01:00
|
|
|
} else {
|
|
|
|
//Skip validation of the filename
|
2017-03-31 23:21:17 +02:00
|
|
|
if (arg != 2) {
|
|
|
|
ERR("%s is not a supported option.", argv[arg]);
|
2015-11-18 08:56:48 +01:00
|
|
|
print_usage(argv);
|
|
|
|
exit(EXIT_FAILURE);
|
2017-03-31 23:21:17 +02:00
|
|
|
} else {
|
|
|
|
bFilename = true;
|
2015-11-18 08:56:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-31 23:21:17 +02:00
|
|
|
if (! bFilename) {
|
|
|
|
ERR("Please supply a Mifare Dump filename");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2015-11-18 08:56:48 +01:00
|
|
|
|
2012-12-04 19:29:57 +01:00
|
|
|
nfc_context *context;
|
|
|
|
nfc_init(&context);
|
2013-03-05 22:24:59 +01:00
|
|
|
if (context == NULL) {
|
|
|
|
ERR("Unable to init libnfc (malloc)");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2012-05-29 02:33:22 +02:00
|
|
|
|
2010-10-04 14:37:43 +02:00
|
|
|
// Try to open the NFC device
|
2012-12-04 19:29:57 +01:00
|
|
|
pnd = nfc_open(context, NULL);
|
2010-06-24 12:19:01 +02:00
|
|
|
if (pnd == NULL) {
|
2013-03-05 22:24:59 +01:00
|
|
|
ERR("Error opening NFC device");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
2016-05-11 09:20:42 +02:00
|
|
|
printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
|
|
|
|
|
|
|
|
if (list_passive_targets(pnd)) {
|
|
|
|
nfc_perror(pnd, "nfc_device_set_property_bool");
|
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2009-09-02 22:15:21 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
if (nfc_initiator_init(pnd) < 0) {
|
|
|
|
nfc_perror(pnd, "nfc_initiator_init");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2012-01-05 11:33:50 +01:00
|
|
|
}
|
2009-09-02 22:15:21 +02:00
|
|
|
|
2010-10-04 14:37:43 +02:00
|
|
|
// Let the device only try once to find a tag
|
2012-05-29 17:54:36 +02:00
|
|
|
if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) {
|
|
|
|
nfc_perror(pnd, "nfc_device_set_property_bool");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2010-08-18 19:22:13 +02:00
|
|
|
}
|
2009-09-02 22:15:21 +02:00
|
|
|
|
|
|
|
// Try to find a MIFARE Ultralight tag
|
2016-05-11 09:20:42 +02:00
|
|
|
if (nfc_initiator_select_passive_target(pnd, nmMifare, (szUID) ? iUID : NULL, szUID, &nt) <= 0) {
|
2012-05-29 17:54:36 +02:00
|
|
|
ERR("no tag was found\n");
|
|
|
|
nfc_close(pnd);
|
2012-12-04 19:29:57 +01:00
|
|
|
nfc_exit(context);
|
2013-03-05 19:44:59 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
|
|
|
|
2017-03-23 17:04:02 +01:00
|
|
|
// Test if we are dealing with a MIFARE compatible tag
|
2010-10-14 13:44:43 +02:00
|
|
|
if (nt.nti.nai.abtAtqa[1] != 0x44) {
|
2012-05-29 17:54:36 +02:00
|
|
|
ERR("tag is not a MIFARE Ultralight card\n");
|
|
|
|
nfc_close(pnd);
|
2012-12-04 19:29:57 +01:00
|
|
|
nfc_exit(context);
|
2013-03-05 19:44:59 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
2010-12-10 00:40:03 +01:00
|
|
|
// Get the info from the current tag
|
2016-05-11 09:20:42 +02:00
|
|
|
printf("Using MIFARE Ultralight card with UID: ");
|
2010-12-10 00:40:03 +01:00
|
|
|
size_t szPos;
|
|
|
|
for (szPos = 0; szPos < nt.nti.nai.szUidLen; szPos++) {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("%02x", nt.nti.nai.abtUid[szPos]);
|
2010-12-10 00:40:03 +01:00
|
|
|
}
|
|
|
|
printf("\n");
|
2010-06-24 12:19:01 +02:00
|
|
|
|
2017-03-23 00:05:48 +01:00
|
|
|
// test if tag is EV1
|
2017-04-01 00:46:12 +02:00
|
|
|
if (get_ev1_version()) {
|
|
|
|
if (!bPWD)
|
2017-03-23 00:05:48 +01:00
|
|
|
printf("Tag is EV1 - PASSWORD may be required\n");
|
2018-09-15 17:19:35 +02:00
|
|
|
printf("EV1 type: ");
|
2017-04-01 00:46:12 +02:00
|
|
|
if (abtRx[6] == 0x0b) {
|
2018-09-15 17:19:35 +02:00
|
|
|
printf("MF0UL11 (48 bytes)\n");
|
|
|
|
uiBlocks = 20; // total number of 4 byte 'pages'
|
2018-09-17 17:58:49 +02:00
|
|
|
iDumpSize = uiBlocks * 4;
|
2017-04-01 00:46:12 +02:00
|
|
|
iEV1Type = EV1_UL11;
|
|
|
|
} else if (abtRx[6] == 0x0e) {
|
2018-09-15 17:19:35 +02:00
|
|
|
printf("MF0UL21 (128 user bytes)\n");
|
|
|
|
uiBlocks = 41;
|
2018-09-17 17:58:49 +02:00
|
|
|
iDumpSize = uiBlocks * 4;
|
2017-04-01 00:46:12 +02:00
|
|
|
iEV1Type = EV1_UL21;
|
2018-09-15 17:19:35 +02:00
|
|
|
} else if (abtRx[6] == 0x0f) {
|
|
|
|
printf("NTAG213 (144 user bytes)\n");
|
|
|
|
uiBlocks = 45;
|
2018-09-17 17:58:49 +02:00
|
|
|
iDumpSize = uiBlocks * 4;
|
2018-09-15 17:19:35 +02:00
|
|
|
iEV1Type = EV1_NTAG213;
|
2018-09-17 15:37:09 +02:00
|
|
|
iNTAGType = NTAG_213;
|
2018-09-15 17:19:35 +02:00
|
|
|
} else if (abtRx[6] == 0x11) {
|
|
|
|
printf("NTAG215 (504 user bytes)\n");
|
|
|
|
uiBlocks = 135;
|
2018-09-17 17:58:49 +02:00
|
|
|
iDumpSize = uiBlocks * 4;
|
2018-09-15 17:19:35 +02:00
|
|
|
iEV1Type = EV1_NTAG215;
|
2018-09-17 15:37:09 +02:00
|
|
|
iNTAGType = NTAG_215;
|
2018-09-15 17:19:35 +02:00
|
|
|
} else if (abtRx[6] == 0x13) {
|
|
|
|
printf("NTAG216 (888 user bytes)\n");
|
|
|
|
uiBlocks = 231;
|
2018-09-17 17:58:49 +02:00
|
|
|
iDumpSize = uiBlocks * 4;
|
2018-09-15 17:19:35 +02:00
|
|
|
iEV1Type = EV1_NTAG216;
|
2018-09-17 15:37:09 +02:00
|
|
|
iNTAGType = NTAG_216;
|
2018-09-15 17:19:35 +02:00
|
|
|
} else {
|
|
|
|
printf("unknown! (0x%02x)\n", abtRx[6]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-04-01 00:46:12 +02:00
|
|
|
} else {
|
|
|
|
// re-init non EV1 tag
|
2017-03-23 17:04:02 +01:00
|
|
|
if (nfc_initiator_select_passive_target(pnd, nmMifare, (szUID) ? iUID : NULL, szUID, &nt) <= 0) {
|
|
|
|
ERR("no tag was found\n");
|
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2017-03-23 00:05:48 +01:00
|
|
|
|
|
|
|
// EV1 login required
|
2017-04-01 00:46:12 +02:00
|
|
|
if (bPWD) {
|
2017-03-23 13:03:34 +01:00
|
|
|
printf("Authing with PWD: %02x%02x%02x%02x ", iPWD[0], iPWD[1], iPWD[2], iPWD[3]);
|
2017-04-01 00:46:12 +02:00
|
|
|
if (!ev1_pwd_auth(iPWD)) {
|
2017-03-23 13:03:34 +01:00
|
|
|
printf("\n");
|
2017-03-23 00:05:48 +01:00
|
|
|
ERR("AUTH failed!\n");
|
|
|
|
exit(EXIT_FAILURE);
|
2017-04-01 00:46:12 +02:00
|
|
|
} else {
|
2017-03-23 13:03:34 +01:00
|
|
|
printf("Success - PACK: %02x%02x\n", abtRx[0], abtRx[1]);
|
2017-03-23 17:04:02 +01:00
|
|
|
memcpy(iPACK, abtRx, 2);
|
|
|
|
}
|
2017-03-23 00:05:48 +01:00
|
|
|
}
|
|
|
|
|
2017-03-23 17:42:10 +01:00
|
|
|
if (iAction == 1) {
|
|
|
|
memset(&mtDump, 0x00, sizeof(mtDump));
|
|
|
|
} else if (iAction == 2) {
|
|
|
|
pfDump = fopen(argv[2], "rb");
|
|
|
|
|
|
|
|
if (pfDump == NULL) {
|
|
|
|
ERR("Could not open dump file: %s\n", argv[2]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2017-03-23 19:19:59 +01:00
|
|
|
size_t szDump;
|
2017-04-01 00:46:12 +02:00
|
|
|
if (((szDump = fread(&mtDump, 1, sizeof(mtDump), pfDump)) != iDumpSize && !bPart) || szDump <= 0) {
|
2018-09-17 17:58:49 +02:00
|
|
|
ERR("Could not read from dump file or size mismatch: %s (read %lu, expected %lu)\n", argv[2], szDump, iDumpSize);
|
2017-03-23 17:42:10 +01:00
|
|
|
fclose(pfDump);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-04-01 00:46:12 +02:00
|
|
|
if (szDump != iDumpSize)
|
2017-03-23 19:19:59 +01:00
|
|
|
printf("Performing partial write\n");
|
2017-03-23 17:42:10 +01:00
|
|
|
fclose(pfDump);
|
|
|
|
DBG("Successfully opened the dump file\n");
|
|
|
|
} else if (iAction == 3) {
|
|
|
|
DBG("Switching to Check Magic Mode\n");
|
|
|
|
} else {
|
|
|
|
ERR("Unable to determine operating mode");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-11-18 23:03:36 +01:00
|
|
|
if (iAction == 1) {
|
2017-04-01 00:46:12 +02:00
|
|
|
bool bRF = read_card();
|
2017-03-23 17:04:02 +01:00
|
|
|
printf("Writing data to file: %s ... ", argv[2]);
|
|
|
|
fflush(stdout);
|
|
|
|
pfDump = fopen(argv[2], "wb");
|
|
|
|
if (pfDump == NULL) {
|
|
|
|
printf("Could not open file: %s\n", argv[2]);
|
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (fwrite(&mtDump, 1, uiReadPages * 4, pfDump) != uiReadPages * 4) {
|
|
|
|
printf("Could not write to file: %s\n", argv[2]);
|
2012-05-29 17:54:36 +02:00
|
|
|
fclose(pfDump);
|
2017-03-23 17:04:02 +01:00
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
2017-03-23 17:04:02 +01:00
|
|
|
fclose(pfDump);
|
|
|
|
printf("Done.\n");
|
2017-04-01 00:46:12 +02:00
|
|
|
if (!bRF)
|
2017-03-23 17:04:02 +01:00
|
|
|
printf("Warning! Read failed - partial data written to file!\n");
|
2015-11-18 23:03:36 +01:00
|
|
|
} else if (iAction == 2) {
|
2018-09-17 17:58:49 +02:00
|
|
|
write_card(bOTP, bLock, bDynLock, bUID);
|
2015-11-18 23:03:36 +01:00
|
|
|
} else if (iAction == 3) {
|
|
|
|
if (!check_magic()) {
|
2017-02-18 13:05:59 +01:00
|
|
|
printf("Card is not magic\n");
|
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
2015-11-18 23:03:36 +01:00
|
|
|
} else {
|
2017-02-18 13:05:59 +01:00
|
|
|
printf("Card is magic\n");
|
2015-11-18 23:03:36 +01:00
|
|
|
}
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
nfc_close(pnd);
|
2012-12-04 19:29:57 +01:00
|
|
|
nfc_exit(context);
|
2013-03-05 19:44:59 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|