bool unlock_card(void) { if (magic2) { #ifdef DEBUG_PRINTF fprintf(stderr,"Don't use R/W with this card, this is not required!\n"); #endif sprintf(message_erreur,"Don't use R/W with this card, this is not required !"); return false; } //! Configure the CRC if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) { nfc_perror(pnd, "nfc_configure"); nfc_strerror_r(pnd, message_erreur, TAILLE_MESSAGE_ERREUR); return false; } //! Use raw send/receive methods if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) { nfc_perror(pnd, "nfc_configure"); nfc_strerror_r(pnd, message_erreur, TAILLE_MESSAGE_ERREUR); return false; } iso14443a_crc_append(abtHalt, 2); transmit_bytes(abtHalt, 4); //! now send unlock if (!transmit_bits(abtUnlock1, 7)) { #ifdef DEBUG_PRINTF fprintf(stderr,"unlock failure!\n"); #endif sprintf(message_erreur,"Unlock failure !"); return false; } if (!transmit_bytes(abtUnlock2, 1)) { #ifdef DEBUG_PRINTF fprintf(stderr,"unlock failure!\n"); #endif sprintf(message_erreur,"Unlock failure !"); return false; } //! reset reader //! Configure the CRC if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_strerror_r(pnd, message_erreur, TAILLE_MESSAGE_ERREUR); return false; } //! Switch off raw send/receive methods if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_strerror_r(pnd, message_erreur, TAILLE_MESSAGE_ERREUR); return false; } return true; }
/** * Unlocking the card allows writing to block 0 of some pirate cards. */ bool mf_unlock() { static uint8_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 }; // Special unlock command static const uint8_t abtUnlock1[1] = { 0x40 }; static const uint8_t abtUnlock2[1] = { 0x43 }; // Disable CRC and parity checking if (nfc_device_set_property_bool(device, NP_HANDLE_CRC, false) < 0) return false; // Disable easy framing. Use raw send/receive methods if (nfc_device_set_property_bool (device, NP_EASY_FRAMING, false) < 0) return false; // Initialize transmision iso14443a_crc_append(abtHalt, 2); transmit_bytes(abtHalt, 4); // Send unlock if (!transmit_bits (abtUnlock1, 7)) return false; if (!transmit_bytes (abtUnlock2, 1)) return false; // Reset reader configuration. CRC and easy framing. if (nfc_device_set_property_bool (device, NP_HANDLE_CRC, true) < 0) return false; if (nfc_device_set_property_bool (device, NP_EASY_FRAMING, true) < 0) return false; return true; }
static bool unlock_card(void) { if (magic2) { printf("Don't use R/W with this card, this is not required!\n"); return false; } // Configure the CRC if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) { nfc_perror(pnd, "nfc_configure"); return false; } // Use raw send/receive methods if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) { nfc_perror(pnd, "nfc_configure"); return false; } iso14443a_crc_append(abtHalt, 2); transmit_bytes(abtHalt, 4); // now send unlock if (!transmit_bits(abtUnlock1, 7)) { printf("unlock failure!\n"); return false; } if (!transmit_bytes(abtUnlock2, 1)) { printf("unlock failure!\n"); return false; } // reset reader // Configure the CRC if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); return false; } // Switch off raw send/receive methods if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); return false; } return true; }
static bool unlock_card(void) { if (!raw_mode_start()) return false; iso14443a_crc_append(abtHalt, 2); transmit_bytes(abtHalt, 4); // now send unlock if (!transmit_bits(abtUnlock1, 7)) { return false; } if (!transmit_bytes(abtUnlock2, 1)) { return false; } if (!raw_mode_end()) return false; return true; }
static bool unlock_card(void) { // Configure the CRC if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) { nfc_perror(pnd, "nfc_configure"); return false; } // Use raw send/receive methods if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) { nfc_perror(pnd, "nfc_configure"); return false; } iso14443a_crc_append(abtHalt, 2); transmit_bytes(abtHalt, 4); // now send unlock if (!transmit_bits(abtUnlock1, 7)) { return false; } if (!transmit_bytes(abtUnlock2, 1)) { return false; } // reset reader // Configure the CRC if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); return false; } // Switch off raw send/receive methods if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); return false; } return true; }
int main (int argc, char *argv[]) { int arg; // Get commandline options for (arg = 1; arg < argc; arg++) { if (0 == strcmp (argv[arg], "-h")) { print_usage (argv); exit(EXIT_SUCCESS); } else if (0 == strcmp (argv[arg], "-q")) { quiet_output = true; } else { ERR ("%s is not supported option.", argv[arg]); print_usage (argv); exit(EXIT_FAILURE); } } // Try to open the NFC reader pnd = nfc_connect (NULL); if (!pnd) { printf ("Error connecting NFC reader\n"); exit(EXIT_FAILURE); } // Initialise NFC device as "initiator" nfc_initiator_init (pnd); // Drop the field for a while if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) { nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } // Configure the CRC if (!nfc_configure (pnd, NDO_HANDLE_CRC, false)) { nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } // Configure parity settings if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) { nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } // Use raw send/receive methods if (!nfc_configure (pnd, NDO_EASY_FRAMING, false)) { nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } // Disable 14443-4 autoswitching if (!nfc_configure (pnd, NDO_AUTO_ISO14443_4, false)) { nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } // Force 14443-A mode if (!nfc_configure (pnd, NDO_FORCE_ISO14443_A, true)) { nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } // Enable field so more power consuming cards can power themselves up if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) { nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } printf ("Connected to NFC reader: %s\n\n", pnd->acName); // Send the 7 bits request command specified in ISO 14443A (0x26) if (!transmit_bits (abtReqa, 7)) { printf ("Error: No tag available\n"); nfc_disconnect (pnd); return 1; } memcpy (abtAtqa, abtRx, 2); // Anti-collision transmit_bytes (abtSelectAll, 2); // Check answer if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) { printf("WARNING: BCC check failed!\n"); } // Save the UID CL1 memcpy (abtRawUid, abtRx, 4); //Prepare and send CL1 Select-Command memcpy (abtSelectTag + 2, abtRx, 5); iso14443a_crc_append (abtSelectTag, 7); transmit_bytes (abtSelectTag, 9); abtSak = abtRx[0]; // Test if we are dealing with a CL2 if (abtSak & CASCADE_BIT) { szCL = 2;//or more // Check answer if (abtRawUid[0] != 0x88) { printf("WARNING: Cascade bit set but CT != 0x88!\n"); } } if(szCL == 2) { // We have to do the anti-collision for cascade level 2 // Prepare CL2 commands abtSelectAll[0] = 0x95; // Anti-collision transmit_bytes (abtSelectAll, 2); // Check answer if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) { printf("WARNING: BCC check failed!\n"); } // Save UID CL2 memcpy (abtRawUid + 4, abtRx, 4); // Selection abtSelectTag[0] = 0x95; memcpy (abtSelectTag + 2, abtRx, 5); iso14443a_crc_append (abtSelectTag, 7); transmit_bytes (abtSelectTag, 9); abtSak = abtRx[0]; // Test if we are dealing with a CL3 if (abtSak & CASCADE_BIT) { szCL = 3; // Check answer if (abtRawUid[0] != 0x88) { printf("WARNING: Cascade bit set but CT != 0x88!\n"); } } if ( szCL == 3) { // We have to do the anti-collision for cascade level 3 // Prepare and send CL3 AC-Command abtSelectAll[0] = 0x97; transmit_bytes (abtSelectAll, 2); // Check answer if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) { printf("WARNING: BCC check failed!\n"); } // Save UID CL3 memcpy (abtRawUid + 8, abtRx, 4); // Prepare and send final Select-Command abtSelectTag[0] = 0x97; memcpy (abtSelectTag + 2, abtRx, 5); iso14443a_crc_append (abtSelectTag, 7); transmit_bytes (abtSelectTag, 9); abtSak = abtRx[0]; } } // Request ATS, this only applies to tags that support ISO 14443A-4 if (abtRx[0] & SAK_FLAG_ATS_SUPPORTED) { iso14443a_crc_append(abtRats, 2); transmit_bytes (abtRats, 4); } // Done, halt the tag now iso14443a_crc_append(abtHalt, 2); transmit_bytes (abtHalt, 4); printf ("\nFound tag with\n UID: "); switch (szCL) { case 1: printf ("%02x%02x%02x%02x", abtRawUid[0], abtRawUid[1], abtRawUid[2], abtRawUid[3]); break; case 2: printf ("%02x%02x%02x", abtRawUid[1], abtRawUid[2], abtRawUid[3]); printf ("%02x%02x%02x%02x", abtRawUid[4], abtRawUid[5], abtRawUid[6], abtRawUid[7]); break; case 3: printf ("%02x%02x%02x", abtRawUid[1], abtRawUid[2], abtRawUid[3]); printf ("%02x%02x%02x", abtRawUid[5], abtRawUid[6], abtRawUid[7]); printf ("%02x%02x%02x%02x", abtRawUid[8], abtRawUid[9], abtRawUid[10], abtRawUid[11]); break; } printf("\n"); printf("ATQA: %02x%02x\n SAK: %02x\n", abtAtqa[1], abtAtqa[0], abtSak); nfc_disconnect (pnd); return 0; }
int main(int argc, char *argv[]) { int arg, i; bool format = false; unsigned int c; char tmp[3] = { 0x00, 0x00, 0x00 }; // Get commandline options for (arg = 1; arg < argc; arg++) { if (0 == strcmp(argv[arg], "-h")) { print_usage(argv); exit(EXIT_SUCCESS); } else if (0 == strcmp(argv[arg], "-f")) { format = true; } else if (0 == strcmp(argv[arg], "-q")) { quiet_output = true; } else if (strlen(argv[arg]) == 8) { for (i = 0 ; i < 4 ; ++i) { memcpy(tmp, argv[arg] + i * 2, 2); sscanf(tmp, "%02x", &c); abtData[i] = (char) c; } abtData[4] = abtData[0] ^ abtData[1] ^ abtData[2] ^ abtData[3]; iso14443a_crc_append(abtData, 16); } else { ERR("%s is not supported option.", argv[arg]); print_usage(argv); exit(EXIT_FAILURE); } } nfc_context *context; nfc_init(&context); // Try to open the NFC reader pnd = nfc_open(context, NULL); if (!pnd) { printf("Error opening NFC reader\n"); exit(EXIT_FAILURE); } // Initialise NFC device as "initiator" if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); exit(EXIT_FAILURE); } // Configure the CRC if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); exit(EXIT_FAILURE); } // Use raw send/receive methods if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); exit(EXIT_FAILURE); } // Disable 14443-4 autoswitching if (nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); exit(EXIT_FAILURE); } printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); // Send the 7 bits request command specified in ISO 14443A (0x26) if (!transmit_bits(abtReqa, 7)) { printf("Error: No tag available\n"); nfc_close(pnd); nfc_exit(context); return 1; } memcpy(abtAtqa, abtRx, 2); // Anti-collision transmit_bytes(abtSelectAll, 2); // Check answer if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) { printf("WARNING: BCC check failed!\n"); } // Save the UID CL1 memcpy(abtRawUid, abtRx, 4); //Prepare and send CL1 Select-Command memcpy(abtSelectTag + 2, abtRx, 5); iso14443a_crc_append(abtSelectTag, 7); transmit_bytes(abtSelectTag, 9); abtSak = abtRx[0]; // Test if we are dealing with a CL2 if (abtSak & CASCADE_BIT) { szCL = 2;//or more // Check answer if (abtRawUid[0] != 0x88) { printf("WARNING: Cascade bit set but CT != 0x88!\n"); } } if (szCL == 2) { // We have to do the anti-collision for cascade level 2 // Prepare CL2 commands abtSelectAll[0] = 0x95; // Anti-collision transmit_bytes(abtSelectAll, 2); // Check answer if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) { printf("WARNING: BCC check failed!\n"); } // Save UID CL2 memcpy(abtRawUid + 4, abtRx, 4); // Selection abtSelectTag[0] = 0x95; memcpy(abtSelectTag + 2, abtRx, 5); iso14443a_crc_append(abtSelectTag, 7); transmit_bytes(abtSelectTag, 9); abtSak = abtRx[0]; // Test if we are dealing with a CL3 if (abtSak & CASCADE_BIT) { szCL = 3; // Check answer if (abtRawUid[0] != 0x88) { printf("WARNING: Cascade bit set but CT != 0x88!\n"); } } if (szCL == 3) { // We have to do the anti-collision for cascade level 3 // Prepare and send CL3 AC-Command abtSelectAll[0] = 0x97; transmit_bytes(abtSelectAll, 2); // Check answer if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) { printf("WARNING: BCC check failed!\n"); } // Save UID CL3 memcpy(abtRawUid + 8, abtRx, 4); // Prepare and send final Select-Command abtSelectTag[0] = 0x97; memcpy(abtSelectTag + 2, abtRx, 5); iso14443a_crc_append(abtSelectTag, 7); transmit_bytes(abtSelectTag, 9); abtSak = abtRx[0]; } } // Request ATS, this only applies to tags that support ISO 14443A-4 if (abtRx[0] & SAK_FLAG_ATS_SUPPORTED) { iso_ats_supported = true; } printf("\nFound tag with\n UID: "); switch (szCL) { case 1: printf("%02x%02x%02x%02x", abtRawUid[0], abtRawUid[1], abtRawUid[2], abtRawUid[3]); break; case 2: printf("%02x%02x%02x", abtRawUid[1], abtRawUid[2], abtRawUid[3]); printf("%02x%02x%02x%02x", abtRawUid[4], abtRawUid[5], abtRawUid[6], abtRawUid[7]); break; case 3: printf("%02x%02x%02x", abtRawUid[1], abtRawUid[2], abtRawUid[3]); printf("%02x%02x%02x", abtRawUid[5], abtRawUid[6], abtRawUid[7]); printf("%02x%02x%02x%02x", abtRawUid[8], abtRawUid[9], abtRawUid[10], abtRawUid[11]); break; } printf("\n"); printf("ATQA: %02x%02x\n SAK: %02x\n", abtAtqa[1], abtAtqa[0], abtSak); if (szAts > 1) { // if = 1, it's not actual ATS but error code printf(" ATS: "); print_hex(abtAts, szAts); } printf("\n"); // now reset UID iso14443a_crc_append(abtHalt, 2); transmit_bytes(abtHalt, 4); transmit_bits(abtUnlock1, 7); if (format) { transmit_bytes(abtWipe, 1); transmit_bytes(abtHalt, 4); transmit_bits(abtUnlock1, 7); } transmit_bytes(abtUnlock2, 1); transmit_bytes(abtWrite, 4); transmit_bytes(abtData, 18); if (format) { for (i = 3 ; i < 64 ; i += 4) { abtWrite[1] = (char) i; iso14443a_crc_append(abtWrite, 2); transmit_bytes(abtWrite, 4); transmit_bytes(abtBlank, 18); } } nfc_close(pnd); nfc_exit(context); return EXIT_SUCCESS; }