Skip to content

Commit

Permalink
adds write condition check in unlock_card()
Browse files Browse the repository at this point in the history
  • Loading branch information
gelotus committed Jun 26, 2020
1 parent f7b9b0e commit db957aa
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions utils/nfc-mfclassic.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ authenticate(uint32_t uiBlock)
}

static bool
unlock_card(void)
unlock_card(bool write)
{
// Configure the CRC
if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) {
Expand All @@ -266,8 +266,10 @@ unlock_card(void)
// now send unlock
if (!transmit_bits(abtUnlock1, 7)) {
printf("Warning: Unlock command [1/2]: failed / not acknowledged.\n");
printf("Trying to rewrite block 0 on a gen2 tag.\n");
magic2 = true;
if (write) {
printf("Trying to rewrite block 0 on a gen2 tag.\n");
magic2 = true;
}
} else {
if (transmit_bytes(abtUnlock2, 1)) {
printf("Card unlocked\n");
Expand Down Expand Up @@ -333,7 +335,7 @@ get_rats(void)
}

static bool
read_card(int read_unlocked)
read_card(bool read_unlocked)
{
int32_t iBlock;
bool bFailure = false;
Expand All @@ -347,7 +349,7 @@ read_card(int read_unlocked)
read_unlocked = 0;
} else {
//If User has requested an unlocked read, but we're unable to unlock the card, we'll error out.
if (!unlock_card()) {
if (!unlock_card(false)) {
return false;
}
}
Expand Down Expand Up @@ -420,7 +422,7 @@ read_card(int read_unlocked)
}

static bool
write_card(int write_block_zero)
write_card(bool write_block_zero)
{
uint32_t uiBlock;
bool bFailure = false;
Expand All @@ -429,7 +431,7 @@ write_card(int write_block_zero)
//Determine if we have to unlock the card
if (write_block_zero) {
if (!magic2 && !magic3) {
if (!unlock_card()) {
if (!unlock_card(true)) {
return false;
}
}
Expand Down Expand Up @@ -663,7 +665,7 @@ main(int argc, const char *argv[])
uint8_t _tag_uid[4];
uint8_t *tag_uid = _tag_uid;

int unlock = 0;
bool unlock = false;

if (argc < 2) {
print_usage(argv[0]);
Expand All @@ -678,15 +680,15 @@ main(int argc, const char *argv[])
if (strcmp(command, "r") == 0 || strcmp(command, "R") == 0) {
atAction = ACTION_READ;
if (strcmp(command, "R") == 0)
unlock = 1;
unlock = true;
bUseKeyA = tolower((int)((unsigned char) * (argv[2]))) == 'a';
bTolerateFailures = tolower((int)((unsigned char) * (argv[2]))) != (int)((unsigned char) * (argv[2]));
bUseKeyFile = (argc > 5) && strcmp(argv[5], "v");
bForceKeyFile = ((argc > 6) && (strcmp((char *)argv[6], "f") == 0));
} else if (strcmp(command, "w") == 0 || strcmp(command, "W") == 0 || strcmp(command, "f") == 0) {
atAction = ACTION_WRITE;
if (strcmp(command, "W") == 0)
unlock = 1;
unlock = true;
bFormatCard = (strcmp(command, "f") == 0);
bUseKeyA = tolower((int)((unsigned char) * (argv[2]))) == 'a';
bTolerateFailures = tolower((int)((unsigned char) * (argv[2]))) != (int)((unsigned char) * (argv[2]));
Expand Down

0 comments on commit db957aa

Please sign in to comment.