Adds --check-magic flag to nfc-mfultralight

This commit is contained in:
Simon Yorkston 2015-11-18 23:03:36 +01:00
parent 3aa2d46588
commit 0cece94778

View file

@ -107,6 +107,43 @@ read_card(void)
return (!bFailure);
}
static bool check_magic() {
bool bFailure = false;
int uid_data;
for (uint32_t page = 0; page <= 1; page++) {
// 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;
memcpy(mp.mpd.abtData, &uid_data, sizeof uid_data);
memset(mp.mpd.abtData + 4, 0, 12);
//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);
}
//Check that the ID is now set to 0x000000000000
if (nfc_initiator_mifare_cmd(pnd, MC_READ, 0, &mp)) {
//printf("%u", mp.mpd.abtData);
for(int i = 0; i <= 7; i++) {
if (mp.mpd.abtData[i] != 0x00) return false;
}
} else {
return false;
}
return true;
}
static bool
write_card(bool write_otp, bool write_lock, bool write_uid)
{
@ -202,13 +239,13 @@ print_usage(const char *argv[])
int
main(int argc, const char *argv[])
{
bool bReadAction;
bool bOTP;
bool bLock;
bool bUID;
int iAction = 0;
bool bOTP = false;
bool bLock = false;
bool bUID = false;
FILE *pfDump;
if (argc < 3) {
if (argc == 0) {
print_usage(argv);
exit(EXIT_FAILURE);
}
@ -218,9 +255,9 @@ main(int argc, const char *argv[])
// Get commandline options
for (int arg = 1; arg < argc; arg++) {
if (0 == strcmp(argv[arg], "r")) {
bReadAction = true;
iAction = 1;
} else if (0 == strcmp(argv[arg], "w")) {
bReadAction = false;
iAction = 2;
} else if (0 == strcmp(argv[arg], "--full")) {
bOTP = true;
bLock = true;
@ -231,6 +268,8 @@ main(int argc, const char *argv[])
bLock = true;
} else if (0 == strcmp(argv[arg], "--uid")) {
bUID = true;
} else if (0 == strcmp(argv[arg], "--check-magic")) {
iAction = 3;
} else {
//Skip validation of the filename
if (arg != 2) {
@ -241,10 +280,9 @@ main(int argc, const char *argv[])
}
}
if (bReadAction) {
if (iAction == 1) {
memset(&mtDump, 0x00, sizeof(mtDump));
} else {
} else if (iAction == 2) {
pfDump = fopen(argv[2], "rb");
if (pfDump == NULL) {
@ -258,9 +296,14 @@ main(int argc, const char *argv[])
exit(EXIT_FAILURE);
}
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);
}
DBG("Successfully opened the dump file\n");
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
@ -316,7 +359,7 @@ main(int argc, const char *argv[])
}
printf("\n");
if (bReadAction) {
if (iAction == 1) {
if (read_card()) {
printf("Writing data to file: %s ... ", argv[2]);
fflush(stdout);
@ -337,8 +380,15 @@ main(int argc, const char *argv[])
fclose(pfDump);
printf("Done.\n");
}
} else {
} else if (iAction == 2) {
write_card(bOTP, bLock, bUID);
} else if (iAction == 3) {
if (!check_magic()) {
printf("Card is not magic\n");
exit(EXIT_FAILURE);
} else {
printf("Card is magic\n");
}
}
nfc_close(pnd);