2010-01-12 13:36:43 +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
|
|
|
|
* Copyright (C) 2010-2013 Philippe Teuwen
|
|
|
|
* Copyright (C) 2012-2013 Ludovic Rousseau
|
|
|
|
* Additional contributors of this file:
|
|
|
|
* Copyright (C) 2011 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-12 13:36:43 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file nfc-mfclassic.c
|
|
|
|
* @brief MIFARE Classic manipulation example
|
2009-10-12 16:52:26 +02:00
|
|
|
*/
|
2009-04-29 14:51:13 +02:00
|
|
|
|
2010-01-15 11:18:11 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-06-07 11:05:35 +02:00
|
|
|
# include "config.h"
|
2010-01-15 11:18:11 +01:00
|
|
|
#endif // HAVE_CONFIG_H
|
|
|
|
|
2009-04-29 14:51:13 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-05-27 12:13:19 +02:00
|
|
|
#include <stdint.h>
|
2009-05-27 14:18:21 +02:00
|
|
|
#include <stddef.h>
|
2009-05-27 16:05:07 +02:00
|
|
|
#include <stdbool.h>
|
2009-05-27 14:18:21 +02:00
|
|
|
|
2009-04-29 14:51:13 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2009-12-01 15:23:00 +01:00
|
|
|
#include <nfc/nfc.h>
|
2009-11-02 15:05:03 +01:00
|
|
|
|
2010-06-15 17:33:22 +02:00
|
|
|
#include "mifare.h"
|
2010-04-16 18:38:57 +02:00
|
|
|
#include "nfc-utils.h"
|
2009-04-29 14:51:13 +02:00
|
|
|
|
2012-12-04 19:29:57 +01:00
|
|
|
static nfc_context *context;
|
2011-11-23 16:55:40 +01:00
|
|
|
static nfc_device *pnd;
|
|
|
|
static nfc_target nt;
|
2009-04-29 14:51:13 +02:00
|
|
|
static mifare_param mp;
|
2010-06-15 17:33:22 +02:00
|
|
|
static mifare_classic_tag mtKeys;
|
|
|
|
static mifare_classic_tag mtDump;
|
2009-04-29 14:51:13 +02:00
|
|
|
static bool bUseKeyA;
|
2009-10-05 14:47:00 +02:00
|
|
|
static bool bUseKeyFile;
|
2013-03-02 22:29:10 +01:00
|
|
|
static bool bTolerateFailures;
|
2010-04-07 17:08:04 +02:00
|
|
|
static uint8_t uiBlocks;
|
2011-11-24 11:54:42 +01:00
|
|
|
static uint8_t keys[] = {
|
2010-06-07 11:05:35 +02:00
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7,
|
|
|
|
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5,
|
|
|
|
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5,
|
|
|
|
0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd,
|
|
|
|
0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a,
|
|
|
|
0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
|
2010-09-24 18:29:17 +02:00
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2010-09-24 18:51:30 +02:00
|
|
|
0xab, 0xcd, 0xef, 0x12, 0x34, 0x56
|
2009-10-05 14:47:00 +02:00
|
|
|
};
|
2009-04-29 14:51:13 +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,
|
|
|
|
};
|
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
static size_t num_keys = sizeof(keys) / 6;
|
2010-06-07 11:05:35 +02:00
|
|
|
|
2011-09-05 16:40:56 +02:00
|
|
|
#define MAX_FRAME_LEN 264
|
|
|
|
|
2011-11-24 11:54:42 +01:00
|
|
|
static uint8_t abtRx[MAX_FRAME_LEN];
|
2012-01-04 16:30:42 +01:00
|
|
|
static int szRxBits;
|
2011-09-05 16:40:56 +02:00
|
|
|
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 };
|
2011-09-05 16:40:56 +02:00
|
|
|
|
|
|
|
// special unlock command
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t abtUnlock1[1] = { 0x40 };
|
|
|
|
uint8_t abtUnlock2[1] = { 0x43 };
|
2011-09-05 16:40:56 +02:00
|
|
|
|
|
|
|
static bool
|
2012-05-29 17:54:36 +02:00
|
|
|
transmit_bits(const uint8_t *pbtTx, const size_t szTxBits)
|
2011-09-05 16:40:56 +02:00
|
|
|
{
|
|
|
|
// Show transmitted command
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Sent bits: ");
|
|
|
|
print_hex_bits(pbtTx, szTxBits);
|
2011-09-05 16:40:56 +02:00
|
|
|
// Transmit the bit frame command, we don't use the arbitrary parity feature
|
2012-11-24 13:00:23 +01:00
|
|
|
if ((szRxBits = nfc_initiator_transceive_bits(pnd, pbtTx, szTxBits, NULL, abtRx, sizeof(abtRx), NULL)) < 0)
|
2011-09-05 16:40:56 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Show received answer
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Received bits: ");
|
|
|
|
print_hex_bits(abtRx, szRxBits);
|
2011-09-05 16:40:56 +02:00
|
|
|
// Succesful transfer
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
2012-05-29 17:54:36 +02:00
|
|
|
transmit_bytes(const uint8_t *pbtTx, const size_t szTx)
|
2011-09-05 16:40:56 +02:00
|
|
|
{
|
|
|
|
// Show transmitted command
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Sent bits: ");
|
|
|
|
print_hex(pbtTx, szTx);
|
2011-09-05 16:40:56 +02:00
|
|
|
// Transmit the command bytes
|
2012-05-27 23:06:22 +02:00
|
|
|
int res;
|
2012-05-29 17:54:36 +02:00
|
|
|
if ((res = nfc_initiator_transceive_bytes(pnd, pbtTx, szTx, abtRx, sizeof(abtRx), 0)) < 0)
|
2011-09-05 16:40:56 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Show received answer
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Received bits: ");
|
|
|
|
print_hex(abtRx, res);
|
2011-09-05 16:40:56 +02:00
|
|
|
// Succesful transfer
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
static void
|
2012-05-29 17:54:36 +02:00
|
|
|
print_success_or_failure(bool bFailure, uint32_t *uiBlockCounter)
|
2009-11-27 17:51:39 +01:00
|
|
|
{
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("%c", (bFailure) ? 'x' : '.');
|
2010-08-11 11:26:22 +02:00
|
|
|
if (uiBlockCounter && !bFailure)
|
2013-03-12 10:55:45 +01:00
|
|
|
*uiBlockCounter += 1;
|
2009-11-27 17:51:39 +01:00
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
static bool
|
2012-05-29 17:54:36 +02:00
|
|
|
is_first_block(uint32_t uiBlock)
|
2009-04-29 14:51:13 +02:00
|
|
|
{
|
|
|
|
// Test if we are in the small or big sectors
|
2010-06-07 11:05:35 +02:00
|
|
|
if (uiBlock < 128)
|
|
|
|
return ((uiBlock) % 4 == 0);
|
|
|
|
else
|
|
|
|
return ((uiBlock) % 16 == 0);
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
static bool
|
2012-05-29 17:54:36 +02:00
|
|
|
is_trailer_block(uint32_t uiBlock)
|
2009-04-29 14:51:13 +02:00
|
|
|
{
|
|
|
|
// Test if we are in the small or big sectors
|
2010-06-07 11:05:35 +02:00
|
|
|
if (uiBlock < 128)
|
|
|
|
return ((uiBlock + 1) % 4 == 0);
|
|
|
|
else
|
|
|
|
return ((uiBlock + 1) % 16 == 0);
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
static uint32_t
|
2012-05-29 17:54:36 +02:00
|
|
|
get_trailer_block(uint32_t uiFirstBlock)
|
2009-04-29 14:51:13 +02:00
|
|
|
{
|
|
|
|
// Test if we are in the small or big sectors
|
2009-11-09 10:39:38 +01:00
|
|
|
uint32_t trailer_block = 0;
|
|
|
|
if (uiFirstBlock < 128) {
|
|
|
|
trailer_block = uiFirstBlock + (3 - (uiFirstBlock % 4));
|
|
|
|
} else {
|
|
|
|
trailer_block = uiFirstBlock + (15 - (uiFirstBlock % 16));
|
|
|
|
}
|
|
|
|
return trailer_block;
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
static bool
|
2012-05-29 17:54:36 +02:00
|
|
|
authenticate(uint32_t uiBlock)
|
2009-10-05 14:47:00 +02:00
|
|
|
{
|
|
|
|
mifare_cmd mc;
|
|
|
|
uint32_t uiTrailerBlock;
|
2010-06-07 11:05:35 +02:00
|
|
|
|
2011-05-27 00:49:45 +02:00
|
|
|
// Set the authentication information (uid)
|
2012-06-03 23:02:54 +02:00
|
|
|
memcpy(mp.mpa.abtAuthUid, nt.nti.nai.abtUid + nt.nti.nai.szUidLen - 4, 4);
|
2011-05-27 00:49:45 +02:00
|
|
|
|
|
|
|
// Should we use key A or B?
|
|
|
|
mc = (bUseKeyA) ? MC_AUTH_A : MC_AUTH_B;
|
|
|
|
|
2009-10-05 14:47:00 +02:00
|
|
|
// Key file authentication.
|
2010-06-07 11:05:35 +02:00
|
|
|
if (bUseKeyFile) {
|
2009-10-05 14:47:00 +02:00
|
|
|
|
|
|
|
// Locate the trailer (with the keys) used for this sector
|
2012-05-29 17:54:36 +02:00
|
|
|
uiTrailerBlock = get_trailer_block(uiBlock);
|
2010-06-07 11:05:35 +02:00
|
|
|
|
2011-05-27 00:49:45 +02:00
|
|
|
// Extract the right key from dump file
|
|
|
|
if (bUseKeyA)
|
2012-05-29 17:54:36 +02:00
|
|
|
memcpy(mp.mpa.abtKey, mtKeys.amb[uiTrailerBlock].mbt.abtKeyA, 6);
|
2011-05-27 00:49:45 +02:00
|
|
|
else
|
2012-05-29 17:54:36 +02:00
|
|
|
memcpy(mp.mpa.abtKey, mtKeys.amb[uiTrailerBlock].mbt.abtKeyB, 6);
|
2009-10-05 14:47:00 +02:00
|
|
|
|
|
|
|
// Try to authenticate for the current sector
|
2012-05-29 17:54:36 +02:00
|
|
|
if (nfc_initiator_mifare_cmd(pnd, mc, uiBlock, &mp))
|
2010-06-07 11:05:35 +02:00
|
|
|
return true;
|
2011-05-27 00:49:45 +02:00
|
|
|
} else {
|
|
|
|
// Try to guess the right key
|
2013-03-06 00:35:36 +01:00
|
|
|
for (size_t key_index = 0; key_index < num_keys; key_index++) {
|
2012-05-29 17:54:36 +02:00
|
|
|
memcpy(mp.mpa.abtKey, keys + (key_index * 6), 6);
|
|
|
|
if (nfc_initiator_mifare_cmd(pnd, mc, uiBlock, &mp)) {
|
2009-10-05 14:47:00 +02:00
|
|
|
if (bUseKeyA)
|
2012-05-29 17:54:36 +02:00
|
|
|
memcpy(mtKeys.amb[uiBlock].mbt.abtKeyA, &mp.mpa.abtKey, 6);
|
2009-10-05 14:47:00 +02:00
|
|
|
else
|
2012-05-29 17:54:36 +02:00
|
|
|
memcpy(mtKeys.amb[uiBlock].mbt.abtKeyB, &mp.mpa.abtKey, 6);
|
2009-10-05 14:47:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
nfc_initiator_select_passive_target(pnd, nmMifare, nt.nti.nai.abtUid, nt.nti.nai.szUidLen, NULL);
|
2009-10-05 14:47:00 +02:00
|
|
|
}
|
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
|
2009-10-05 14:47:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-06 23:17:38 +02:00
|
|
|
static bool
|
2012-05-29 17:54:36 +02:00
|
|
|
unlock_card(void)
|
2011-09-06 23:17:38 +02:00
|
|
|
{
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Unlocking card\n");
|
2011-09-06 23:17:38 +02:00
|
|
|
|
|
|
|
// Configure the CRC
|
2012-05-29 17:54:36 +02:00
|
|
|
if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) {
|
|
|
|
nfc_perror(pnd, "nfc_configure");
|
2013-03-05 19:44:59 +01:00
|
|
|
return false;
|
2011-09-06 23:17:38 +02:00
|
|
|
}
|
|
|
|
// Use raw send/receive methods
|
2012-05-29 17:54:36 +02:00
|
|
|
if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) {
|
|
|
|
nfc_perror(pnd, "nfc_configure");
|
2013-03-05 19:44:59 +01:00
|
|
|
return false;
|
2011-09-06 23:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
iso14443a_crc_append(abtHalt, 2);
|
2012-05-29 17:54:36 +02:00
|
|
|
transmit_bytes(abtHalt, 4);
|
2011-09-06 23:17:38 +02:00
|
|
|
// now send unlock
|
2012-05-29 17:54:36 +02:00
|
|
|
if (!transmit_bits(abtUnlock1, 7)) {
|
2011-09-06 23:17:38 +02:00
|
|
|
printf("unlock failure!\n");
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
if (!transmit_bytes(abtUnlock2, 1)) {
|
2011-09-06 23:17:38 +02:00
|
|
|
printf("unlock failure!\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset reader
|
|
|
|
// Configure the CRC
|
2012-05-29 17:54:36 +02:00
|
|
|
if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true) < 0) {
|
|
|
|
nfc_perror(pnd, "nfc_device_set_property_bool");
|
2013-03-05 19:44:59 +01:00
|
|
|
return false;
|
2011-09-06 23:17:38 +02:00
|
|
|
}
|
|
|
|
// Switch off raw send/receive methods
|
2012-05-29 17:54:36 +02:00
|
|
|
if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0) {
|
|
|
|
nfc_perror(pnd, "nfc_device_set_property_bool");
|
2013-03-05 19:44:59 +01:00
|
|
|
return false;
|
2011-09-06 23:17:38 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-12 13:35:31 +01:00
|
|
|
static int
|
|
|
|
get_rats(void)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
uint8_t abtRats[2] = { 0xe0, 0x50};
|
|
|
|
// Use raw send/receive methods
|
|
|
|
if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) {
|
|
|
|
nfc_perror(pnd, "nfc_configure");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
res = nfc_initiator_transceive_bytes(pnd, abtRats, sizeof(abtRats), abtRx, sizeof(abtRx), 0);
|
|
|
|
if (res > 0) {
|
|
|
|
// ISO14443-4 card, turn RF field off/on to access ISO14443-3 again
|
|
|
|
nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false);
|
|
|
|
nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, true);
|
|
|
|
}
|
|
|
|
// Reselect tag
|
|
|
|
if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) {
|
|
|
|
printf("Error: tag disappeared\n");
|
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
static bool
|
2012-05-29 17:54:36 +02:00
|
|
|
read_card(int read_unlocked)
|
2009-04-29 14:51:13 +02:00
|
|
|
{
|
2009-05-27 12:13:19 +02:00
|
|
|
int32_t iBlock;
|
2010-09-07 19:51:03 +02:00
|
|
|
bool bFailure = false;
|
2009-11-27 17:51:39 +01:00
|
|
|
uint32_t uiReadBlocks = 0;
|
2009-04-29 14:51:13 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
if (read_unlocked)
|
2011-09-06 23:17:38 +02:00
|
|
|
if (!unlock_card())
|
|
|
|
return false;
|
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Reading out %d blocks |", uiBlocks + 1);
|
2009-04-29 14:51:13 +02:00
|
|
|
// Read the card from end to begin
|
2010-06-07 11:05:35 +02:00
|
|
|
for (iBlock = uiBlocks; iBlock >= 0; iBlock--) {
|
2009-04-29 14:51:13 +02:00
|
|
|
// Authenticate everytime we reach a trailer block
|
2012-05-29 17:54:36 +02:00
|
|
|
if (is_trailer_block(iBlock)) {
|
2010-06-07 11:05:35 +02:00
|
|
|
if (bFailure) {
|
2009-04-29 14:51:13 +02:00
|
|
|
// 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
|
|
|
printf("!\nError: tag was removed\n");
|
2010-06-07 10:16:27 +02:00
|
|
|
return false;
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|
|
|
|
bFailure = false;
|
|
|
|
}
|
2009-11-27 17:51:39 +01:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
fflush(stdout);
|
2010-06-07 11:05:35 +02:00
|
|
|
|
2009-04-29 14:51:13 +02:00
|
|
|
// Try to authenticate for the current sector
|
2012-05-29 17:54:36 +02:00
|
|
|
if (!read_unlocked && !authenticate(iBlock)) {
|
|
|
|
printf("!\nError: authentication failed for block 0x%02x\n", iBlock);
|
2009-04-29 14:51:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Try to read out the trailer
|
2012-05-29 17:54:36 +02:00
|
|
|
if (nfc_initiator_mifare_cmd(pnd, MC_READ, iBlock, &mp)) {
|
|
|
|
if (read_unlocked) {
|
|
|
|
memcpy(mtDump.amb[iBlock].mbd.abtData, mp.mpd.abtData, 16);
|
2011-09-07 11:41:04 +02:00
|
|
|
} else {
|
|
|
|
// Copy the keys over from our key dump and store the retrieved access bits
|
2012-05-29 17:54:36 +02:00
|
|
|
memcpy(mtDump.amb[iBlock].mbt.abtKeyA, mtKeys.amb[iBlock].mbt.abtKeyA, 6);
|
|
|
|
memcpy(mtDump.amb[iBlock].mbt.abtAccessBits, mp.mpd.abtData + 6, 4);
|
|
|
|
memcpy(mtDump.amb[iBlock].mbt.abtKeyB, mtKeys.amb[iBlock].mbt.abtKeyB, 6);
|
2011-09-07 11:41:04 +02:00
|
|
|
}
|
2010-08-18 15:20:40 +02:00
|
|
|
} else {
|
2013-03-02 22:29:10 +01:00
|
|
|
printf("!\nfailed to read trailer block 0x%02x\n", iBlock);
|
|
|
|
bFailure = true;
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Make sure a earlier readout did not fail
|
2010-06-07 11:05:35 +02:00
|
|
|
if (!bFailure) {
|
2009-04-29 14:51:13 +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, iBlock, &mp)) {
|
|
|
|
memcpy(mtDump.amb[iBlock].mbd.abtData, mp.mpd.abtData, 16);
|
2009-04-29 14:51:13 +02:00
|
|
|
} else {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("!\nError: unable to read block 0x%02x\n", iBlock);
|
2013-03-02 22:29:10 +01:00
|
|
|
bFailure = true;
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-02 22:29:10 +01:00
|
|
|
// Show if the readout went well for each block
|
|
|
|
print_success_or_failure(bFailure, &uiReadBlocks);
|
|
|
|
if ((! bTolerateFailures) && bFailure)
|
|
|
|
return false;
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("|\n");
|
|
|
|
printf("Done, %d of %d blocks read.\n", uiReadBlocks, uiBlocks + 1);
|
|
|
|
fflush(stdout);
|
2009-04-29 14:51:13 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
static bool
|
2012-05-29 17:54:36 +02:00
|
|
|
write_card(int write_block_zero)
|
2009-04-29 14:51:13 +02:00
|
|
|
{
|
2009-05-27 12:13:19 +02:00
|
|
|
uint32_t uiBlock;
|
2010-09-07 19:51:03 +02:00
|
|
|
bool bFailure = false;
|
2009-11-27 17:51:39 +01:00
|
|
|
uint32_t uiWriteBlocks = 0;
|
2009-04-29 14:51:13 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
if (write_block_zero)
|
2011-09-06 23:17:38 +02:00
|
|
|
if (!unlock_card())
|
2011-09-05 16:40:56 +02:00
|
|
|
return false;
|
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Writing %d blocks |", uiBlocks + 1);
|
2009-04-29 14:51:13 +02:00
|
|
|
// Write the card from begin to end;
|
2010-06-07 11:05:35 +02:00
|
|
|
for (uiBlock = 0; uiBlock <= uiBlocks; uiBlock++) {
|
2009-04-29 14:51:13 +02:00
|
|
|
// Authenticate everytime we reach the first sector of a new block
|
2012-05-29 17:54:36 +02:00
|
|
|
if (is_first_block(uiBlock)) {
|
2010-06-07 11:05:35 +02:00
|
|
|
if (bFailure) {
|
2009-04-29 14:51:13 +02:00
|
|
|
// 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
|
|
|
printf("!\nError: tag was removed\n");
|
2009-04-29 14:51:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bFailure = false;
|
|
|
|
}
|
2009-11-27 17:51:39 +01:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
fflush(stdout);
|
2009-04-29 14:51:13 +02:00
|
|
|
|
|
|
|
// Try to authenticate for the current sector
|
2012-05-29 17:54:36 +02:00
|
|
|
if (!write_block_zero && !authenticate(uiBlock)) {
|
|
|
|
printf("!\nError: authentication failed for block %02x\n", uiBlock);
|
2009-04-29 14:51:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-10-02 11:52:02 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
if (is_trailer_block(uiBlock)) {
|
2009-04-29 14:51:13 +02:00
|
|
|
// Copy the keys over from our key dump and store the retrieved access bits
|
2012-05-29 17:54:36 +02:00
|
|
|
memcpy(mp.mpd.abtData, mtDump.amb[uiBlock].mbt.abtKeyA, 6);
|
|
|
|
memcpy(mp.mpd.abtData + 6, mtDump.amb[uiBlock].mbt.abtAccessBits, 4);
|
|
|
|
memcpy(mp.mpd.abtData + 10, mtDump.amb[uiBlock].mbt.abtKeyB, 6);
|
2009-04-29 14:51:13 +02:00
|
|
|
|
|
|
|
// Try to write the trailer
|
2012-05-29 17:54:36 +02:00
|
|
|
if (nfc_initiator_mifare_cmd(pnd, MC_WRITE, uiBlock, &mp) == false) {
|
|
|
|
printf("failed to write trailer block %d \n", uiBlock);
|
2009-11-09 10:39:38 +01:00
|
|
|
bFailure = true;
|
|
|
|
}
|
2009-04-29 14:51:13 +02:00
|
|
|
} else {
|
|
|
|
// The first block 0x00 is read only, skip this
|
2011-09-05 16:40:56 +02:00
|
|
|
if (uiBlock == 0 && ! write_block_zero)
|
2010-06-07 11:05:35 +02:00
|
|
|
continue;
|
2009-04-29 14:51:13 +02:00
|
|
|
|
2011-09-05 16:40:56 +02:00
|
|
|
|
2009-04-29 14:51:13 +02:00
|
|
|
// Make sure a earlier write did not fail
|
2010-06-07 11:05:35 +02:00
|
|
|
if (!bFailure) {
|
2009-04-29 14:51:13 +02:00
|
|
|
// Try to write the data block
|
2012-05-29 17:54:36 +02:00
|
|
|
memcpy(mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData, 16);
|
2011-09-05 16:40:56 +02:00
|
|
|
// do not write a block 0 with incorrect BCC - card will be made invalid!
|
|
|
|
if (uiBlock == 0) {
|
2012-05-29 17:54:36 +02:00
|
|
|
if ((mp.mpd.abtData[0] ^ mp.mpd.abtData[1] ^ mp.mpd.abtData[2] ^ mp.mpd.abtData[3] ^ mp.mpd.abtData[4]) != 0x00) {
|
|
|
|
printf("!\nError: incorrect BCC in MFD file!\n");
|
2011-09-05 16:40:56 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
if (!nfc_initiator_mifare_cmd(pnd, MC_WRITE, uiBlock, &mp))
|
2010-06-07 11:05:35 +02:00
|
|
|
bFailure = true;
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|
|
|
|
}
|
2013-03-02 22:29:10 +01:00
|
|
|
// Show if the write went well for each block
|
|
|
|
print_success_or_failure(bFailure, &uiWriteBlocks);
|
|
|
|
if ((! bTolerateFailures) && bFailure)
|
|
|
|
return false;
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("|\n");
|
|
|
|
printf("Done, %d of %d blocks written.\n", uiWriteBlocks, uiBlocks + 1);
|
|
|
|
fflush(stdout);
|
2009-10-02 11:52:02 +02:00
|
|
|
|
2009-04-29 14:51:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
typedef enum {
|
2009-10-26 14:37:12 +01:00
|
|
|
ACTION_READ,
|
|
|
|
ACTION_WRITE,
|
2011-09-06 23:17:38 +02:00
|
|
|
ACTION_USAGE
|
2009-10-26 14:37:12 +01:00
|
|
|
} action_t;
|
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
static void
|
2012-05-29 17:54:36 +02:00
|
|
|
print_usage(const char *pcProgramName)
|
2009-10-26 14:37:12 +01:00
|
|
|
{
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Usage: ");
|
|
|
|
printf("%s r|R|w|W a|b <dump.mfd> [<keys.mfd>]\n", pcProgramName);
|
|
|
|
printf(" r|R|w|W - Perform read from (r) or unlocked read from (R) or write to (w) or unlocked write to (W) card\n");
|
|
|
|
printf(" *** note that unlocked write will attempt to overwrite block 0 including UID\n");
|
|
|
|
printf(" *** unlocked read does not require authentication and will reveal A and B keys\n");
|
|
|
|
printf(" *** unlocking only works with special Mifare 1K cards (Chinese clones)\n");
|
2013-03-02 22:29:10 +01:00
|
|
|
printf(" a|A|b|B - Use A or B keys for action; Halt on errors (a|b) or tolerate errors (A|B)\n");
|
2012-05-29 17:54:36 +02:00
|
|
|
printf(" <dump.mfd> - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n");
|
|
|
|
printf(" <keys.mfd> - MiFare Dump (MFD) that contain the keys (optional)\n");
|
2009-10-26 14:37:12 +01:00
|
|
|
}
|
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
int
|
2012-05-29 17:54:36 +02:00
|
|
|
main(int argc, const char *argv[])
|
2009-10-02 11:52:02 +02:00
|
|
|
{
|
2010-01-12 13:36:43 +01:00
|
|
|
action_t atAction = ACTION_USAGE;
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t *pbtUID;
|
2012-05-29 17:54:00 +02:00
|
|
|
int unlock = 0;
|
2010-06-07 11:05:35 +02:00
|
|
|
|
2011-04-05 15:30:49 +02:00
|
|
|
if (argc < 2) {
|
2012-05-29 17:54:36 +02:00
|
|
|
print_usage(argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|
2011-04-05 15:30:49 +02:00
|
|
|
const char *command = argv[1];
|
2009-04-29 14:51:13 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
if (strcmp(command, "r") == 0 || strcmp(command, "R") == 0) {
|
2011-04-05 15:30:49 +02:00
|
|
|
if (argc < 4) {
|
2012-05-29 17:54:36 +02:00
|
|
|
print_usage(argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
2011-04-05 15:30:49 +02:00
|
|
|
}
|
2009-10-26 14:37:12 +01:00
|
|
|
atAction = ACTION_READ;
|
2012-05-29 17:54:36 +02:00
|
|
|
if (strcmp(command, "R") == 0)
|
2012-05-29 17:54:00 +02:00
|
|
|
unlock = 1;
|
2012-05-29 17:54:36 +02:00
|
|
|
bUseKeyA = tolower((int)((unsigned char) * (argv[2]))) == 'a';
|
2013-03-02 22:29:10 +01:00
|
|
|
bTolerateFailures = tolower((int)((unsigned char) * (argv[2]))) != (int)((unsigned char) * (argv[2]));
|
2009-10-26 14:37:12 +01:00
|
|
|
bUseKeyFile = (argc > 4);
|
2012-05-29 17:54:36 +02:00
|
|
|
} else if (strcmp(command, "w") == 0 || strcmp(command, "W") == 0) {
|
2011-04-05 15:30:49 +02:00
|
|
|
if (argc < 4) {
|
2012-05-29 17:54:36 +02:00
|
|
|
print_usage(argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
2011-04-05 15:30:49 +02:00
|
|
|
}
|
2009-10-26 14:37:12 +01:00
|
|
|
atAction = ACTION_WRITE;
|
2012-05-29 17:54:36 +02:00
|
|
|
if (strcmp(command, "W") == 0)
|
2012-05-29 17:54:00 +02:00
|
|
|
unlock = 1;
|
2012-05-29 17:54:36 +02:00
|
|
|
bUseKeyA = tolower((int)((unsigned char) * (argv[2]))) == 'a';
|
2013-03-02 22:29:10 +01:00
|
|
|
bTolerateFailures = tolower((int)((unsigned char) * (argv[2]))) != (int)((unsigned char) * (argv[2]));
|
2009-10-26 14:37:12 +01:00
|
|
|
bUseKeyFile = (argc > 4);
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|
|
|
|
|
2013-03-05 19:44:59 +01:00
|
|
|
if (atAction == ACTION_USAGE) {
|
|
|
|
print_usage(argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2013-03-12 10:56:57 +01:00
|
|
|
// We don't know yet the card size so let's read only the UID from the keyfile for the moment
|
2013-03-05 19:44:59 +01:00
|
|
|
if (bUseKeyFile) {
|
2013-03-06 11:02:47 +01:00
|
|
|
FILE *pfKeys = fopen(argv[4], "rb");
|
2013-03-05 19:44:59 +01:00
|
|
|
if (pfKeys == NULL) {
|
|
|
|
printf("Could not open keys file: %s\n", argv[4]);
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-03-05 19:44:59 +01:00
|
|
|
}
|
2013-03-12 10:56:57 +01:00
|
|
|
if (fread(&mtKeys, 1, 4, pfKeys) != 4) {
|
|
|
|
printf("Could not read UID from key file: %s\n", argv[4]);
|
2013-03-05 19:44:59 +01:00
|
|
|
fclose(pfKeys);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
fclose(pfKeys);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2013-03-05 19:44:59 +01:00
|
|
|
// Try to open the NFC reader
|
|
|
|
pnd = nfc_open(context, NULL);
|
|
|
|
if (pnd == NULL) {
|
2013-03-05 22:24:59 +01:00
|
|
|
ERR("Error opening NFC reader");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
|
2013-03-05 19:44:59 +01:00
|
|
|
if (nfc_initiator_init(pnd) < 0) {
|
|
|
|
nfc_perror(pnd, "nfc_initiator_init");
|
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
};
|
2010-06-07 11:05:35 +02:00
|
|
|
|
2013-03-05 19:44:59 +01:00
|
|
|
// Let the reader only try once to find a tag
|
|
|
|
if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) {
|
|
|
|
nfc_perror(pnd, "nfc_device_set_property_bool");
|
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
// Disable ISO14443-4 switching in order to read devices that emulate Mifare Classic with ISO14443-4 compliance.
|
|
|
|
nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false);
|
2010-08-19 12:59:45 +02:00
|
|
|
|
2013-03-05 19:44:59 +01:00
|
|
|
printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
|
2010-06-07 11:05:35 +02:00
|
|
|
|
2013-03-05 19:44:59 +01:00
|
|
|
// Try to find a MIFARE Classic tag
|
|
|
|
if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) {
|
|
|
|
printf("Error: no tag was found\n");
|
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
// Test if we are dealing with a MIFARE compatible tag
|
|
|
|
if ((nt.nti.nai.btSak & 0x08) == 0) {
|
|
|
|
printf("Warning: tag is probably not a MFC!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the info from the current tag
|
|
|
|
pbtUID = nt.nti.nai.abtUid;
|
|
|
|
|
|
|
|
if (bUseKeyFile) {
|
|
|
|
uint8_t fileUid[4];
|
|
|
|
memcpy(fileUid, mtKeys.amb[0].mbm.abtUID, 4);
|
|
|
|
// Compare if key dump UID is the same as the current tag UID, at least for the first 4 bytes
|
|
|
|
if (memcmp(pbtUID, fileUid, 4) != 0) {
|
|
|
|
printf("Expected MIFARE Classic card with UID starting as: %02x%02x%02x%02x\n",
|
|
|
|
fileUid[0], fileUid[1], fileUid[2], fileUid[3]);
|
2013-03-12 10:56:20 +01:00
|
|
|
printf("Got card with UID starting as: %02x%02x%02x%02x\n",
|
|
|
|
pbtUID[0], pbtUID[1], pbtUID[2], pbtUID[3]);
|
|
|
|
printf("Aborting!\n");
|
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
2013-03-05 19:44:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("Found MIFARE Classic card:\n");
|
2013-03-04 05:40:07 +01:00
|
|
|
print_nfc_target(&nt, false);
|
2013-03-05 19:44:59 +01:00
|
|
|
|
|
|
|
// Guessing size
|
|
|
|
if ((nt.nti.nai.abtAtqa[1] & 0x02) == 0x02)
|
|
|
|
// 4K
|
|
|
|
uiBlocks = 0xff;
|
|
|
|
else if ((nt.nti.nai.btSak & 0x01) == 0x01)
|
|
|
|
// 320b
|
|
|
|
uiBlocks = 0x13;
|
|
|
|
else
|
2013-03-12 13:35:31 +01:00
|
|
|
// 1K/2K, checked through RATS
|
2013-03-05 19:44:59 +01:00
|
|
|
uiBlocks = 0x3f;
|
2013-03-12 13:35:31 +01:00
|
|
|
// Testing RATS
|
|
|
|
int res;
|
|
|
|
if ((res = get_rats()) > 0) {
|
|
|
|
if ((res >= 10) && (abtRx[5] == 0xc1) && (abtRx[6] == 0x05)
|
|
|
|
&& (abtRx[7] == 0x2f) && (abtRx[8] == 0x2f)
|
|
|
|
&& ((nt.nti.nai.abtAtqa[1] & 0x02) == 0x00)) {
|
|
|
|
// MIFARE Plus 2K
|
|
|
|
uiBlocks = 0x7f;
|
|
|
|
}
|
|
|
|
}
|
2013-03-05 19:44:59 +01:00
|
|
|
printf("Guessing size: seems to be a %i-byte card\n", (uiBlocks + 1) * 16);
|
|
|
|
|
2013-03-12 10:56:57 +01:00
|
|
|
if (bUseKeyFile) {
|
|
|
|
FILE *pfKeys = fopen(argv[4], "rb");
|
|
|
|
if (pfKeys == NULL) {
|
|
|
|
printf("Could not open keys file: %s\n", argv[4]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (fread(&mtKeys, 1, (uiBlocks + 1) * sizeof(mifare_classic_block), pfKeys) != (uiBlocks + 1) * sizeof(mifare_classic_block)) {
|
|
|
|
printf("Could not read keys file: %s\n", argv[4]);
|
|
|
|
fclose(pfKeys);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
fclose(pfKeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atAction == ACTION_READ) {
|
|
|
|
memset(&mtDump, 0x00, sizeof(mtDump));
|
|
|
|
} else {
|
|
|
|
FILE *pfDump = fopen(argv[3], "rb");
|
|
|
|
|
|
|
|
if (pfDump == NULL) {
|
|
|
|
printf("Could not open dump file: %s\n", argv[3]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fread(&mtDump, 1, (uiBlocks + 1) * sizeof(mifare_classic_block), pfDump) != (uiBlocks + 1) * sizeof(mifare_classic_block)) {
|
|
|
|
printf("Could not read dump file: %s\n", argv[3]);
|
|
|
|
fclose(pfDump);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
fclose(pfDump);
|
|
|
|
}
|
|
|
|
// printf("Successfully opened required files\n");
|
|
|
|
|
2013-03-05 19:44:59 +01:00
|
|
|
if (atAction == ACTION_READ) {
|
|
|
|
if (read_card(unlock)) {
|
|
|
|
printf("Writing data to file: %s ...", argv[3]);
|
|
|
|
fflush(stdout);
|
2013-03-06 11:02:47 +01:00
|
|
|
FILE *pfDump = fopen(argv[3], "wb");
|
2013-03-05 19:44:59 +01:00
|
|
|
if (pfDump == NULL) {
|
|
|
|
printf("Could not open dump file: %s\n", argv[3]);
|
2012-05-29 17:54:36 +02:00
|
|
|
nfc_close(pnd);
|
2012-12-04 19:29:57 +01:00
|
|
|
nfc_exit(context);
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2012-05-29 17:52:51 +02:00
|
|
|
}
|
2013-03-12 10:56:57 +01:00
|
|
|
if (fwrite(&mtDump, 1, (uiBlocks + 1) * sizeof(mifare_classic_block), pfDump) != ((uiBlocks + 1) * sizeof(mifare_classic_block))) {
|
2013-03-05 19:44:59 +01:00
|
|
|
printf("\nCould not write to file: %s\n", argv[3]);
|
|
|
|
fclose(pfDump);
|
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
2009-10-26 14:37:12 +01:00
|
|
|
}
|
2013-03-05 19:44:59 +01:00
|
|
|
printf("Done.\n");
|
|
|
|
fclose(pfDump);
|
|
|
|
}
|
|
|
|
} else if (atAction == ACTION_WRITE) {
|
|
|
|
write_card(unlock);
|
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_close(pnd);
|
2012-12-04 19:29:57 +01:00
|
|
|
nfc_exit(context);
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|