2010-01-12 13:36:43 +01:00
|
|
|
/*-
|
2009-10-12 16:52:26 +02:00
|
|
|
* Public platform independent Near Field Communication (NFC) library
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009, Roel Verdult
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
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
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
static nfc_device_t *pnd;
|
2009-11-18 12:52:18 +01:00
|
|
|
static nfc_target_info_t nti;
|
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;
|
2010-04-07 17:08:04 +02:00
|
|
|
static uint8_t uiBlocks;
|
2009-10-05 14:47:00 +02:00
|
|
|
static byte_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,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
2009-10-05 14:47:00 +02:00
|
|
|
};
|
2009-04-29 14:51:13 +02:00
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
static size_t num_keys = sizeof (keys) / 6;
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_success_or_failure (bool bFailure, uint32_t * uiBlockCounter)
|
2009-11-27 17:51:39 +01:00
|
|
|
{
|
2010-06-07 11:05:35 +02:00
|
|
|
printf ("%c", (bFailure) ? 'x' : '.');
|
2010-08-11 11:26:22 +02:00
|
|
|
if (uiBlockCounter && !bFailure)
|
|
|
|
*uiBlockCounter += (*uiBlockCounter < 128) ? 4 : 16;
|
2009-11-27 17:51:39 +01:00
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
static bool
|
2010-06-07 11:05:35 +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
|
2010-06-07 11:05:35 +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
|
2010-06-07 11:05:35 +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
|
2010-06-07 11:05:35 +02:00
|
|
|
authenticate (uint32_t uiBlock)
|
2009-10-05 14:47:00 +02:00
|
|
|
{
|
|
|
|
mifare_cmd mc;
|
|
|
|
uint32_t uiTrailerBlock;
|
2010-09-07 19:51:03 +02:00
|
|
|
size_t key_index;
|
2010-06-07 11:05:35 +02:00
|
|
|
|
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
|
|
|
// Set the authentication information (uid)
|
2010-06-07 11:05:35 +02:00
|
|
|
memcpy (mp.mpa.abtUid, nti.nai.abtUid, 4);
|
2009-10-05 14:47:00 +02:00
|
|
|
|
|
|
|
// Locate the trailer (with the keys) used for this sector
|
2010-06-07 11:05:35 +02:00
|
|
|
uiTrailerBlock = get_trailer_block (uiBlock);
|
|
|
|
|
2009-10-05 14:47:00 +02:00
|
|
|
// Determin if we should use the a or the b key
|
2010-06-07 11:05:35 +02:00
|
|
|
if (bUseKeyA) {
|
2009-10-05 14:47:00 +02:00
|
|
|
mc = MC_AUTH_A;
|
2010-06-07 11:05:35 +02:00
|
|
|
memcpy (mp.mpa.abtKey, mtKeys.amb[uiTrailerBlock].mbt.abtKeyA, 6);
|
2009-10-05 14:47:00 +02:00
|
|
|
} else {
|
|
|
|
mc = MC_AUTH_B;
|
2010-06-07 11:05:35 +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
|
2010-06-07 11:05:35 +02:00
|
|
|
if (nfc_initiator_mifare_cmd (pnd, mc, uiBlock, &mp))
|
|
|
|
return true;
|
2009-10-05 14:47:00 +02:00
|
|
|
}
|
|
|
|
// Auto authentication.
|
2010-06-07 11:05:35 +02:00
|
|
|
else {
|
2009-10-05 14:47:00 +02:00
|
|
|
// Determin if we should use the a or the b key
|
|
|
|
mc = (bUseKeyA) ? MC_AUTH_A : MC_AUTH_B;
|
2010-06-07 11:05:35 +02:00
|
|
|
|
2009-10-05 14:47:00 +02:00
|
|
|
// Set the authentication information (uid)
|
2010-06-07 11:05:35 +02:00
|
|
|
memcpy (mp.mpa.abtUid, nti.nai.abtUid, 4);
|
|
|
|
|
|
|
|
for (key_index = 0; key_index < num_keys; key_index++) {
|
|
|
|
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)
|
2010-06-07 11:05:35 +02:00
|
|
|
memcpy (mtKeys.amb[uiBlock].mbt.abtKeyA, &mp.mpa.abtKey, 6);
|
2009-10-05 14:47:00 +02:00
|
|
|
else
|
2010-06-07 11:05:35 +02:00
|
|
|
memcpy (mtKeys.amb[uiBlock].mbt.abtKeyB, &mp.mpa.abtKey, 6);
|
|
|
|
|
2009-10-05 14:47:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
|
2010-07-21 12:37:37 +02:00
|
|
|
nfc_initiator_select_passive_target (pnd, NM_ISO14443A_106, mp.mpa.abtUid, 4, 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;
|
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
static bool
|
2010-06-07 11:05:35 +02:00
|
|
|
read_card (void)
|
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
|
|
|
|
2010-06-07 11:05:35 +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
|
2010-06-07 11:05:35 +02:00
|
|
|
if (is_trailer_block (iBlock)) {
|
2009-11-27 17:51:39 +01:00
|
|
|
// Skip this the first time, bFailure it means nothing (yet)
|
|
|
|
if (iBlock != uiBlocks)
|
2010-06-07 11:05:35 +02:00
|
|
|
print_success_or_failure (bFailure, &uiReadBlocks);
|
2009-11-27 17:51:39 +01:00
|
|
|
|
2009-04-29 14:51:13 +02:00
|
|
|
// Show if the readout went well
|
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
|
2010-07-21 12:37:37 +02:00
|
|
|
if (!nfc_initiator_select_passive_target (pnd, NM_ISO14443A_106, NULL, 0, &nti)) {
|
2010-06-07 11:05:35 +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
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
fflush (stdout);
|
|
|
|
|
2009-04-29 14:51:13 +02:00
|
|
|
// Try to authenticate for the current sector
|
2010-06-07 11:05:35 +02:00
|
|
|
if (!authenticate (iBlock)) {
|
2010-08-18 15:20:40 +02:00
|
|
|
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
|
2010-06-07 11:05:35 +02:00
|
|
|
if (nfc_initiator_mifare_cmd (pnd, MC_READ, iBlock, &mp)) {
|
2009-04-29 14:51:13 +02:00
|
|
|
// Copy the keys over from our key dump and store the retrieved access bits
|
2010-06-07 11:05:35 +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);
|
2010-08-18 15:20:40 +02:00
|
|
|
} else {
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("!\nError: unable to read trailer block 0x%02x\n", iBlock);
|
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
|
2010-06-07 11:05:35 +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 {
|
|
|
|
bFailure = true;
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("!\nError: unable to read block 0x%02x\n", iBlock);
|
2010-08-18 15:20:40 +02:00
|
|
|
return false;
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
print_success_or_failure (bFailure, &uiReadBlocks);
|
|
|
|
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
|
2010-06-07 11:05:35 +02:00
|
|
|
write_card (void)
|
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
|
|
|
|
2010-06-07 11:05:35 +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
|
2010-06-07 11:05:35 +02:00
|
|
|
if (is_first_block (uiBlock)) {
|
2009-11-27 17:51:39 +01:00
|
|
|
// Skip this the first time, bFailure it means nothing (yet)
|
2010-06-07 10:16:27 +02:00
|
|
|
if (uiBlock != 0)
|
2010-06-07 11:05:35 +02:00
|
|
|
print_success_or_failure (bFailure, &uiWriteBlocks);
|
2009-11-27 17:51:39 +01:00
|
|
|
|
2009-04-29 14:51:13 +02:00
|
|
|
// Show if the readout went well
|
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
|
2010-07-21 12:37:37 +02:00
|
|
|
if (!nfc_initiator_select_passive_target (pnd, NM_ISO14443A_106, NULL, 0, &nti)) {
|
2010-06-07 11:05:35 +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
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
fflush (stdout);
|
2009-04-29 14:51:13 +02:00
|
|
|
|
|
|
|
// Try to authenticate for the current sector
|
2010-06-07 11:05:35 +02:00
|
|
|
if (!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
|
|
|
|
2010-06-07 11:05:35 +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
|
2010-06-07 11:05:35 +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
|
2010-06-07 11:05:35 +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
|
2010-06-07 11:05:35 +02:00
|
|
|
if (uiBlock == 0)
|
|
|
|
continue;
|
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
|
2010-06-07 11:05:35 +02:00
|
|
|
memcpy (mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData, 16);
|
|
|
|
if (!nfc_initiator_mifare_cmd (pnd, MC_WRITE, uiBlock, &mp))
|
|
|
|
bFailure = true;
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
print_success_or_failure (bFailure, &uiWriteBlocks);
|
|
|
|
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-06-07 11:05:35 +02:00
|
|
|
static void
|
|
|
|
mifare_classic_extract_payload (const char *abDump, char *pbPayload)
|
2009-10-26 14:37:12 +01:00
|
|
|
{
|
|
|
|
uint8_t uiSectorIndex;
|
|
|
|
uint8_t uiBlockIndex;
|
2010-09-07 19:51:03 +02:00
|
|
|
size_t szDumpOffset;
|
|
|
|
size_t szPayloadIndex = 0;
|
2009-10-26 14:37:12 +01:00
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
for (uiSectorIndex = 1; uiSectorIndex < 16; uiSectorIndex++) {
|
|
|
|
for (uiBlockIndex = 0; uiBlockIndex < 3; uiBlockIndex++) {
|
|
|
|
szDumpOffset = uiSectorIndex * 16 * 4 + uiBlockIndex * 16;
|
2009-10-26 14:37:12 +01:00
|
|
|
// for(uint8_t uiByteIndex=0; uiByteIndex<16; uiByteIndex++) printf("%02x ", abDump[szPayloadIndex+uiByteIndex]);
|
2010-06-07 11:05:35 +02:00
|
|
|
memcpy (pbPayload + szPayloadIndex, abDump + szDumpOffset, 16);
|
2009-10-26 14:37:12 +01:00
|
|
|
szPayloadIndex += 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
typedef enum {
|
2009-10-26 14:37:12 +01:00
|
|
|
ACTION_READ,
|
|
|
|
ACTION_WRITE,
|
2010-01-12 13:36:43 +01:00
|
|
|
ACTION_EXTRACT,
|
|
|
|
ACTION_USAGE
|
2009-10-26 14:37:12 +01:00
|
|
|
} action_t;
|
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
static void
|
|
|
|
print_usage (const char *pcProgramName)
|
2009-10-26 14:37:12 +01:00
|
|
|
{
|
2010-06-07 11:05:35 +02:00
|
|
|
printf ("Usage: ");
|
|
|
|
printf ("%s r|w a|b <dump.mfd> [<keys.mfd>]\n", pcProgramName);
|
|
|
|
printf (" r|w - Perform read from (r) or write to (w) card\n");
|
|
|
|
printf (" a|b - Use A or B keys for action\n");
|
|
|
|
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");
|
|
|
|
printf ("Or: ");
|
|
|
|
printf ("%s x <dump.mfd> <payload.bin>\n", pcProgramName);
|
|
|
|
printf (" x - Extract payload (data blocks) from MFD\n");
|
|
|
|
printf (" <dump.mfd> - MiFare Dump (MFD) that contains wanted payload\n");
|
|
|
|
printf (" <payload.bin> - Binary file where payload will be extracted\n");
|
2009-10-26 14:37:12 +01:00
|
|
|
}
|
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
int
|
|
|
|
main (int argc, const char *argv[])
|
2009-10-02 11:52:02 +02:00
|
|
|
{
|
2010-09-07 19:51:03 +02:00
|
|
|
bool b4K;
|
2010-01-12 13:36:43 +01:00
|
|
|
action_t atAction = ACTION_USAGE;
|
2010-06-07 11:05:35 +02:00
|
|
|
byte_t *pbtUID;
|
2010-09-07 19:51:03 +02:00
|
|
|
FILE *pfKeys = NULL;
|
|
|
|
FILE *pfDump = NULL;
|
2010-06-07 11:05:35 +02:00
|
|
|
const char *command = argv[1];
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
print_usage (argv[0]);
|
|
|
|
exit (EXIT_FAILURE);
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
if (strcmp (command, "r") == 0) {
|
2009-10-26 14:37:12 +01:00
|
|
|
atAction = ACTION_READ;
|
2010-06-07 11:05:35 +02:00
|
|
|
bUseKeyA = tolower ((int) ((unsigned char) *(argv[2]))) == 'a';
|
2009-10-26 14:37:12 +01:00
|
|
|
bUseKeyFile = (argc > 4);
|
2010-06-07 11:05:35 +02:00
|
|
|
} else if (strcmp (command, "w") == 0) {
|
2009-10-26 14:37:12 +01:00
|
|
|
atAction = ACTION_WRITE;
|
2010-06-07 11:05:35 +02:00
|
|
|
bUseKeyA = tolower ((int) ((unsigned char) *(argv[2]))) == 'a';
|
2009-10-26 14:37:12 +01:00
|
|
|
bUseKeyFile = (argc > 4);
|
2010-06-07 11:05:35 +02:00
|
|
|
} else if (strcmp (command, "x") == 0) {
|
2009-10-26 14:37:12 +01:00
|
|
|
atAction = ACTION_EXTRACT;
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
switch (atAction) {
|
|
|
|
case ACTION_USAGE:
|
|
|
|
print_usage (argv[0]);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
case ACTION_READ:
|
|
|
|
case ACTION_WRITE:
|
|
|
|
if (argc < 4) {
|
|
|
|
print_usage (argv[0]);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2009-10-05 14:47:00 +02:00
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
if (bUseKeyFile) {
|
|
|
|
pfKeys = fopen (argv[4], "rb");
|
|
|
|
if (pfKeys == NULL) {
|
|
|
|
printf ("Could not open keys file: %s\n", argv[4]);
|
|
|
|
exit (EXIT_FAILURE);
|
2009-10-26 14:37:12 +01:00
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
if (fread (&mtKeys, 1, sizeof (mtKeys), pfKeys) != sizeof (mtKeys)) {
|
|
|
|
printf ("Could not read keys file: %s\n", argv[4]);
|
|
|
|
fclose (pfKeys);
|
|
|
|
exit (EXIT_FAILURE);
|
2009-10-26 14:37:12 +01:00
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
fclose (pfKeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atAction == ACTION_READ) {
|
|
|
|
memset (&mtDump, 0x00, sizeof (mtDump));
|
|
|
|
} else {
|
|
|
|
pfDump = fopen (argv[3], "rb");
|
|
|
|
|
|
|
|
if (pfDump == NULL) {
|
|
|
|
printf ("Could not open dump file: %s\n", argv[3]);
|
|
|
|
exit (EXIT_FAILURE);
|
2009-10-26 14:37:12 +01:00
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
|
|
|
|
if (fread (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) {
|
|
|
|
printf ("Could not read dump file: %s\n", argv[3]);
|
|
|
|
fclose (pfDump);
|
|
|
|
exit (EXIT_FAILURE);
|
2009-10-26 14:37:12 +01:00
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
fclose (pfDump);
|
|
|
|
}
|
|
|
|
// printf("Successfully opened required files\n");
|
|
|
|
|
|
|
|
// Try to open the NFC reader
|
|
|
|
pnd = nfc_connect (NULL);
|
|
|
|
if (pnd == NULL) {
|
|
|
|
printf ("Error connecting NFC reader\n");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
nfc_initiator_init (pnd);
|
|
|
|
|
|
|
|
// Drop the field for a while
|
2010-08-18 19:22:13 +02:00
|
|
|
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) {
|
2010-09-07 19:51:03 +02:00
|
|
|
nfc_perror (pnd, "nfc_configure");
|
2010-08-18 19:22:13 +02:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
// Let the reader only try once to find a tag
|
2010-08-18 19:22:13 +02:00
|
|
|
if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) {
|
2010-09-07 19:51:03 +02:00
|
|
|
nfc_perror (pnd, "nfc_configure");
|
2010-08-18 19:22:13 +02:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) {
|
2010-09-07 19:51:03 +02:00
|
|
|
nfc_perror (pnd, "nfc_configure");
|
2010-08-18 19:22:13 +02:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) {
|
2010-09-07 19:51:03 +02:00
|
|
|
nfc_perror (pnd, "nfc_configure");
|
2010-08-18 19:22:13 +02:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
// Enable field so more power consuming cards can power themselves up
|
2010-08-18 19:22:13 +02:00
|
|
|
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) {
|
2010-09-07 19:51:03 +02:00
|
|
|
nfc_perror (pnd, "nfc_configure");
|
2010-08-18 19:22:13 +02:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2010-08-19 12:59:45 +02:00
|
|
|
// Disable ISO14443-4 switching in order to read devices that emulate Mifare Classic with ISO14443-4 compliance.
|
2010-09-07 19:51:03 +02:00
|
|
|
nfc_configure (pnd, NDO_AUTO_ISO14443_4, false);
|
2010-08-19 12:59:45 +02:00
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
printf ("Connected to NFC reader: %s\n", pnd->acName);
|
|
|
|
|
|
|
|
// Try to find a MIFARE Classic tag
|
2010-07-21 12:37:37 +02:00
|
|
|
if (!nfc_initiator_select_passive_target (pnd, NM_ISO14443A_106, NULL, 0, &nti)) {
|
2010-06-07 11:05:35 +02:00
|
|
|
printf ("Error: no tag was found\n");
|
|
|
|
nfc_disconnect (pnd);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
// Test if we are dealing with a MIFARE compatible tag
|
|
|
|
if ((nti.nai.btSak & 0x08) == 0) {
|
|
|
|
printf ("Error: tag is not a MIFARE Classic card\n");
|
|
|
|
nfc_disconnect (pnd);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bUseKeyFile) {
|
|
|
|
// Get the info from the key dump
|
|
|
|
b4K = (mtKeys.amb[0].mbm.abtATQA[1] == 0x02);
|
|
|
|
pbtUID = mtKeys.amb[0].mbm.abtUID;
|
|
|
|
|
|
|
|
// Compare if key dump UID is the same as the current tag UID
|
|
|
|
if (memcmp (nti.nai.abtUid, pbtUID, 4) != 0) {
|
2010-08-19 12:59:45 +02:00
|
|
|
printf ("Expected MIFARE Classic %ck card with UID: %02x%02x%02x%02x\n", b4K ? '4' : '1', pbtUID[3], pbtUID[2],
|
2010-06-07 11:05:35 +02:00
|
|
|
pbtUID[1], pbtUID[0]);
|
2009-10-26 14:37:12 +01:00
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
}
|
|
|
|
// Get the info from the current tag
|
|
|
|
pbtUID = nti.nai.abtUid;
|
|
|
|
b4K = (nti.nai.abtAtqa[1] == 0x02);
|
2010-08-19 12:59:45 +02:00
|
|
|
printf ("Found MIFARE Classic %ck card with UID: %02x%02x%02x%02x\n", b4K ? '4' : '1', pbtUID[3], pbtUID[2],
|
2010-06-07 11:05:35 +02:00
|
|
|
pbtUID[1], pbtUID[0]);
|
|
|
|
|
|
|
|
uiBlocks = (b4K) ? 0xff : 0x3f;
|
|
|
|
|
|
|
|
if (atAction == ACTION_READ) {
|
|
|
|
if (read_card ()) {
|
2010-08-19 12:59:45 +02:00
|
|
|
printf ("Writing data to file: %s ...", argv[3]);
|
2010-06-07 11:05:35 +02:00
|
|
|
fflush (stdout);
|
|
|
|
pfDump = fopen (argv[3], "wb");
|
|
|
|
if (fwrite (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) {
|
|
|
|
printf ("\nCould not write to file: %s\n", argv[3]);
|
|
|
|
exit (EXIT_FAILURE);
|
2009-10-26 14:37:12 +01:00
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
printf ("Done.\n");
|
|
|
|
fclose (pfDump);
|
2009-10-26 14:37:12 +01:00
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
} else {
|
|
|
|
write_card ();
|
|
|
|
}
|
|
|
|
|
|
|
|
nfc_disconnect (pnd);
|
|
|
|
break;
|
2009-04-29 14:51:13 +02:00
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
case ACTION_EXTRACT:{
|
|
|
|
const char *pcDump = argv[2];
|
|
|
|
const char *pcPayload = argv[3];
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
FILE *pfDump = NULL;
|
|
|
|
FILE *pfPayload = NULL;
|
2009-04-29 14:51:13 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
char abDump[4096];
|
|
|
|
char abPayload[4096];
|
2009-04-29 14:51:13 +02:00
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
pfDump = fopen (pcDump, "rb");
|
|
|
|
|
|
|
|
if (pfDump == NULL) {
|
|
|
|
printf ("Could not open dump file: %s\n", pcDump);
|
|
|
|
exit (EXIT_FAILURE);
|
2009-10-26 14:37:12 +01:00
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
|
|
|
|
if (fread (abDump, 1, sizeof (abDump), pfDump) != sizeof (abDump)) {
|
|
|
|
printf ("Could not read dump file: %s\n", pcDump);
|
|
|
|
fclose (pfDump);
|
|
|
|
exit (EXIT_FAILURE);
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
fclose (pfDump);
|
2009-04-29 14:51:13 +02:00
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
mifare_classic_extract_payload (abDump, abPayload);
|
2009-10-26 14:37:12 +01:00
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
printf ("Writing data to file: %s\n", pcPayload);
|
|
|
|
pfPayload = fopen (pcPayload, "wb");
|
|
|
|
if (fwrite (abPayload, 1, sizeof (abPayload), pfPayload) != sizeof (abPayload)) {
|
|
|
|
printf ("Could not write to file: %s\n", pcPayload);
|
|
|
|
exit (EXIT_FAILURE);
|
2009-10-26 14:37:12 +01:00
|
|
|
}
|
2010-06-07 11:05:35 +02:00
|
|
|
fclose (pfPayload);
|
|
|
|
printf ("Done, all bytes have been extracted!\n");
|
2009-10-26 14:37:12 +01:00
|
|
|
}
|
|
|
|
};
|
2009-04-29 14:51:13 +02:00
|
|
|
|
2010-06-07 11:05:35 +02:00
|
|
|
exit (EXIT_SUCCESS);
|
2009-04-29 14:51:13 +02:00
|
|
|
}
|