#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <libnfc.h>
#include "mifaretag.h"
int main(int argc, const char* argv[])
{
bool b4K;
mifare_tag mtDump;
byte_t* pbtUID;
dev_info* pdi;
tag_info ti;
mifare_param mp;
uint32_t block;
int key;
mifare_cmd mc;
byte_t defaultKeys[][6] = {
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // First key
{0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}, // Second key
{0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5}, // Third key
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd},
{0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a},
{0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7},
{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}
};
if (argc < 3)
{
printf("\nSyntax: %s <block number(0-FF)> <key number(1-8)>\n",argv[0]);
printf("Default keys:\nffffffffffff - 1\na0a1a2a3a4a5 - 2\nb0b1b2b3b4b5 - 3\n000000000000 - 4\n4d3a99c351dd - 5\n1a982c7e459a - 6\nd3f7d3f7d3f7 - 7\naabbccddeeff - 8\n");
return 1;
}
sscanf(argv[2],"%i",&key);
sscanf(argv[1],"%02x",&block);
// Try to open the NFC reader
pdi = nfc_connect();
if (pdi == INVALID_DEVICE_INFO)
{
printf("Error connecting NFC reader\n");
return 1;
}
// Configure reader settings
nfc_initiator_init(pdi);
// Drop the field for a while
nfc_configure(pdi,DCO_ACTIVATE_FIELD,false);
// Let the reader only try once to find a tag
nfc_configure(pdi,DCO_INFINITE_SELECT,false);
nfc_configure(pdi,DCO_HANDLE_CRC,true);
nfc_configure(pdi,DCO_HANDLE_PARITY,true);
// Enable field so more power consuming cards can power themselves up
nfc_configure(pdi,DCO_ACTIVATE_FIELD,true);
printf("Connected to NFC reader: %s\n",pdi->acName);
// Try to find a MIFARE Classic tag
if (!nfc_initiator_select_tag(pdi,IM_ISO14443A_106,NULL,0,&ti))
{
printf("Error: no tag was found\n");
nfc_disconnect(pdi);
return 1;
}
// Test if we are dealing with a MIFARE compatible tag
if ((ti.tia.btSak & 0x08) == 0)
{
printf("Error: tag is not a MIFARE Classic card\n");
nfc_disconnect(pdi);
return 1;
}
// Get the info from the current tag
pbtUID = ti.tia.abtUid;
b4K = (ti.tia.abtAtqa[1] == 0x02);
printf("Found MIFARE Classic %cK card with uid: %08x\n",b4K?'4':'1',swap_endian32(pbtUID));
// Set the authentication information (uid)
memcpy(mp.mpa.abtUid,ti.tia.abtUid,4);
memcpy(mp.mpa.abtKey,defaultKeys[key-1], 6);
if (!nfc_initiator_mifare_cmd(pdi,MC_AUTH_A,block,&mp))
{
printf("Authentication failed for block %02x\n", block);
return 1;
}
printf("Reading Block %02x\n", block);
//Try to read out the data block
if (nfc_initiator_mifare_cmd(pdi,MC_READ,block,&mp))
{
memcpy(mtDump.amb[block].mbd.abtData,mp.mpd.abtData,16);
print_hex(mtDump.amb[block].mbd.abtData, 16);
} else
{
printf("Read Error!");
}
// Reset the "advanced" configuration to normal
nfc_configure(pdi,DCO_HANDLE_CRC,true);
nfc_configure(pdi,DCO_HANDLE_PARITY,true);
// Clean up and release device
nfc_disconnect(pdi);
return 0;
}