int NFC_Mifare_Classic(int argc, const char *argv[]) //! Lecture - Écriture d'une carte MIFARE Classic { action_t atAction = ACTION_USAGE; uint8_t *pbtUID; int unlock = 0; if (argc < 2) { sprintf(message_erreur,"Syntaxe incorrecte !"); return EXIT_FAILURE; //! Échec ! } const char *command = argv[1]; if (strcmp(command, "r") == 0 || strcmp(command, "R") == 0) { if (argc < 4) { sprintf(message_erreur,"Syntaxe incorrecte !"); return EXIT_FAILURE; //! Échec ! } atAction = ACTION_READ; if (strcmp(command, "R") == 0) { unlock = 1; } bUseKeyA = tolower((int)((unsigned char) * (argv[2]))) == 'a'; bTolerateFailures = tolower((int)((unsigned char) * (argv[2]))) != (int)((unsigned char) * (argv[2])); bUseKeyFile = (argc > 4); bForceKeyFile = ((argc > 5) && (strcmp((char *)argv[5], "f") == 0)); } else if (strcmp(command, "w") == 0 || strcmp(command, "W") == 0) { if (argc < 4) { sprintf(message_erreur,"Syntaxe incorrecte !"); return EXIT_FAILURE; //! Échec ! } atAction = ACTION_WRITE; if (strcmp(command, "W") == 0) { unlock = 1; } bUseKeyA = tolower((int)((unsigned char) * (argv[2]))) == 'a'; bTolerateFailures = tolower((int)((unsigned char) * (argv[2]))) != (int)((unsigned char) * (argv[2])); bUseKeyFile = (argc > 4); bForceKeyFile = ((argc > 5) && (strcmp((char *)argv[5], "f") == 0)); } if (atAction == ACTION_USAGE) { sprintf(message_erreur,"Syntaxe incorrecte !"); return EXIT_FAILURE; //! Échec ! } //! We don't know yet the card size so let's read only the UID from the keyfile for the moment if (bUseKeyFile) { FILE *pfKeys = fopen(argv[4], "rb"); if (pfKeys == NULL) { sprintf(message_erreur,"Could not open keys file: %s\n", argv[4]); return EXIT_FAILURE; //! Échec ! } if (fread(&mtKeys, 1, 4, pfKeys) != 4) { sprintf(message_erreur,"Could not read UID from key file: %s !", argv[4]); fclose(pfKeys); return EXIT_FAILURE; //! Échec ! } fclose(pfKeys); } nfc_init(&context); if (context == NULL) { sprintf(message_erreur,"Unable to init libnfc (malloc) !"); return EXIT_FAILURE; //! Échec ! } //! Try to open the NFC reader pnd = nfc_open(context, NULL); if (pnd == NULL) { sprintf(message_erreur,"Error opening NFC reader !"); nfc_exit(context); return EXIT_FAILURE; //! Échec ! } if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); nfc_strerror_r(pnd, message_erreur, TAILLE_MESSAGE_ERREUR); nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; //! Échec ! }; //! Let the reader only try once to find a tag if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_strerror_r(pnd, message_erreur, TAILLE_MESSAGE_ERREUR); nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; //! Échec ! } //! Disable ISO14443-4 switching in order to read devices that emulate Mifare Classic with ISO14443-4 compliance. nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false); #ifdef DEBUG_PRINTF fprintf(stderr,"NFC reader: %s opened\n", nfc_device_get_name(pnd)); #endif //! Try to find a MIFARE Classic tag if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { sprintf(message_erreur,"Error: no tag was found !"); nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; //! Échec ! } //! Test if we are dealing with a MIFARE compatible tag if ((nt.nti.nai.btSak & 0x08) == 0) { #ifdef DEBUG_PRINTF fprintf(stderr,"Warning: tag is probably not a MFC !"); #endif } //! Get the info from the current tag pbtUID = nt.nti.nai.abtUid; if (bUseKeyFile) { uint8_t fileUid[4]; memcpy(fileUid, mtKeys.amb[0].mbm.abtUID, 4); //! Compare if key dump UID is the same as the current tag UID, at least for the first 4 bytes if (memcmp(pbtUID, fileUid, 4) != 0) { #ifdef DEBUG_PRINTF fprintf(stderr,"Expected MIFARE Classic card with UID starting as: %02x%02x%02x%02x\n", fileUid[0], fileUid[1], fileUid[2], fileUid[3]); fprintf(stderr,"Got card with UID starting as: %02x%02x%02x%02x\n", pbtUID[0], pbtUID[1], pbtUID[2], pbtUID[3]); #endif if (! bForceKeyFile) { sprintf(message_erreur,"Aborting !"); nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; //! Échec ! } } } #ifdef DEBUG_PRINTF fprintf(stderr,"Found MIFARE Classic card:\n"); #endif print_nfc_target(&nt, false); //! Guessing size if ((nt.nti.nai.abtAtqa[1] & 0x02) == 0x02) //! 4K uiBlocks = 0xff; else if ((nt.nti.nai.btSak & 0x01) == 0x01) //! 320b uiBlocks = 0x13; else //! 1K/2K, checked through RATS uiBlocks = 0x3f; //! Testing RATS int res; if ((res = get_rats()) > 0) { if ((res >= 10) && (abtRx[5] == 0xc1) && (abtRx[6] == 0x05) && (abtRx[7] == 0x2f) && (abtRx[8] == 0x2f) && ((nt.nti.nai.abtAtqa[1] & 0x02) == 0x00)) { //! MIFARE Plus 2K uiBlocks = 0x7f; } //! Chinese magic emulation card, ATS=0978009102:dabc1910 if ((res == 9) && (abtRx[5] == 0xda) && (abtRx[6] == 0xbc) && (abtRx[7] == 0x19) && (abtRx[8] == 0x10)) { magic2 = true; } } #ifdef DEBUG_PRINTF fprintf(stderr,"Guessing size: seems to be a %i-byte card\n", (uiBlocks + 1) * 16); #endif if (bUseKeyFile) { FILE *pfKeys = fopen(argv[4], "rb"); if (pfKeys == NULL) { sprintf(message_erreur,"Could not open keys file: %s !", argv[4]); return EXIT_FAILURE; //! Échec ! } if (fread(&mtKeys, 1, (uiBlocks + 1) * sizeof(mifare_classic_block), pfKeys) != (uiBlocks + 1) * sizeof(mifare_classic_block)) { sprintf(message_erreur,"Could not read keys file: %s !", argv[4]); fclose(pfKeys); return EXIT_FAILURE; //! Échec ! } fclose(pfKeys); } if (atAction == ACTION_READ) { memset(&mtDump, 0x00, sizeof(mtDump)); } else { FILE *pfDump = fopen(argv[3], "rb"); if (pfDump == NULL) { sprintf(message_erreur,"Could not open dump file: %s !", argv[3]); return EXIT_FAILURE; //! Échec ! } if (fread(&mtDump, 1, (uiBlocks + 1) * sizeof(mifare_classic_block), pfDump) != (uiBlocks + 1) * sizeof(mifare_classic_block)) { sprintf(message_erreur,"Could not read dump file: %s !", argv[3]); fclose(pfDump); return EXIT_FAILURE; //! Échec ! } fclose(pfDump); } if (atAction == ACTION_READ) { if (read_card(unlock)) { #ifdef DEBUG_PRINTF fprintf(stderr,"Writing data to file: %s ...", argv[3]); #endif fflush(stdout); FILE *pfDump = fopen(argv[3], "wb"); if (pfDump == NULL) { sprintf(message_erreur,"Could not open dump file: %s !", argv[3]); nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; //! Échec ! } if (fwrite(&mtDump, 1, (uiBlocks + 1) * sizeof(mifare_classic_block), pfDump) != ((uiBlocks + 1) * sizeof(mifare_classic_block))) { sprintf(message_erreur,"\nCould not write to file: %s !", argv[3]); fclose(pfDump); nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; //! Échec ! } #ifdef DEBUG_PRINTF fprintf(stderr,"Done.\n"); #endif fclose(pfDump); } else { nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; } } else if (atAction == ACTION_WRITE) { write_card(unlock); } nfc_close(pnd); nfc_exit(context); return EXIT_SUCCESS; //! Succès ! }
static int get_rats(void) { int res; uint8_t abtRats[2] = { 0xe0, 0x50}; // Use raw send/receive methods if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) { nfc_perror(pnd, "nfc_configure"); return -1; } res = nfc_initiator_transceive_bytes(pnd, abtRats, sizeof(abtRats), abtRx, sizeof(abtRx), 0); if (res > 0) { // ISO14443-4 card, turn RF field off/on to access ISO14443-3 again if (nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false) < 0) { nfc_perror(pnd, "nfc_configure"); return -1; } if (nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, true) < 0) { nfc_perror(pnd, "nfc_configure"); return -1; } } // Reselect tag if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { printf("Error: tag disappeared\n"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } return res; }
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; }
int get_rats(void) { int res; uint8_t abtRats[2] = { 0xe0, 0x50}; //! 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 -1; } res = nfc_initiator_transceive_bytes(pnd, abtRats, sizeof(abtRats), abtRx, sizeof(abtRx), 0); if (res > 0) { //! ISO14443-4 card, turn RF field off/on to access ISO14443-3 again nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false); nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, true); } //! Reselect tag if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { sprintf(message_erreur,"Error: tag disappeared !"); nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; //! Échec ! } return res; }
/* * Get a list of the MIFARE targets near to the provided NFC initiator. * * The list has to be freed using the freefare_free_tags() function. */ MifareTag * freefare_get_tags (nfc_device *device) { MifareTag *tags = NULL; int tag_count = 0; nfc_initiator_init(device); // Drop the field for a while nfc_device_set_property_bool(device,NP_ACTIVATE_FIELD,false); // Configure the CRC and Parity settings nfc_device_set_property_bool(device,NP_HANDLE_CRC,true); nfc_device_set_property_bool(device,NP_HANDLE_PARITY,true); nfc_device_set_property_bool(device,NP_AUTO_ISO14443_4,true); // Enable field so more power consuming cards can power themselves up nfc_device_set_property_bool(device,NP_ACTIVATE_FIELD,true); // Poll for a ISO14443A (MIFARE) tag nfc_target *candidates = pvPortMalloc(MAX_CANDIDATES*sizeof(nfc_target)); int candidates_count; nfc_modulation modulation = { .nmt = NMT_ISO14443A, .nbr = NBR_106 }; if ((candidates_count = nfc_initiator_list_passive_targets(device, modulation, candidates, MAX_CANDIDATES)) < 0) { vPortFree(candidates); return NULL; } tags = pvPortMalloc(MAX_CANDIDATES*sizeof (void *)); if(!tags) { vPortFree(candidates); return NULL; } memset (tags,0,MAX_CANDIDATES*sizeof (void *)); for (int c = 0; c < candidates_count; c++) { MifareTag t; if ((t = freefare_tag_new(device, candidates[c].nti.nai))) { /* (Re)Allocate memory for the found MIFARE targets array */ /* MifareTag *p = pvPortRealloc (tags, (tag_count + 2) * sizeof (MifareTag)); if (p) tags = p; else { vPortFree(candidates); return tags; // FAIL! Return what has been found so far. }*/ tags[tag_count++] = t; // tags[tag_count] = NULL; } } vPortFree(candidates); return tags; }
NFCReaderUnit::WriteUIDConfigGuard::~WriteUIDConfigGuard() { int ret; // Revert the various flag to their default value ret = nfc_device_set_property_bool(ru_.getDevice(), NP_HANDLE_CRC, true); assert(ret >= 0); ret = nfc_device_set_property_bool(ru_.getDevice(), NP_EASY_FRAMING, true); assert(ret >= 0); ret = nfc_device_set_property_bool(ru_.getDevice(), NP_AUTO_ISO14443_4, true); assert(ret >= 0); ru_.getDefaultNFCReaderCardAdapter()->ignoreAllError(rca_error_flag_); std::dynamic_pointer_cast<NFCDataTransport>(ru_.getDefaultNFCReaderCardAdapter()->getDataTransport()) ->ignoreAllError(dt_error_flag_); }
static bool raw_mode_start(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; } return true; }
static bool raw_mode_end(void) { // 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; }
void TagInit(ReaderTag *rt){ nfc_init(&rt->context); if (rt->context == NULL) { ERR("Unable to init libnfc (malloc)"); exit(EXIT_FAILURE); } // Try to open the NFC device rt->device = nfc_open(rt->context, NULL); if (rt->device == NULL) { ERR("Error opening NFC device"); nfc_exit(rt->context); exit(EXIT_FAILURE); } if (nfc_initiator_init(rt->device) < 0) { nfc_perror(rt->device, "nfc_initiator_init"); nfc_close(rt->device); nfc_exit(rt->context); exit(EXIT_FAILURE); } // Scan until the cows come home, or the tag, which ever comes first. if (nfc_device_set_property_bool(rt->device, NP_INFINITE_SELECT, true) < 0) { nfc_perror(rt->device, "nfc_device_set_property_bool"); nfc_close(rt->device); nfc_exit(rt->context); exit(EXIT_FAILURE); } printf("NFC device: %s opened\n", nfc_device_get_name(rt->device)); printf("\nWaiting for tag\n"); // Try to find a MIFARE Ultralight tag if (nfc_initiator_select_passive_target(rt->device, nmMifare, NULL, 0, &rt->nt) <= 0) { ERR("no tag was found\n"); nfc_close(rt->device); nfc_exit(rt->context); exit(EXIT_FAILURE); } // Test if we are dealing with a MIFARE compatible tag if (rt->nt.nti.nai.abtAtqa[1] != 0x44) { ERR("tag is not a MIFARE Ultralight card\n"); nfc_close(rt->device); nfc_exit(rt->context); exit(EXIT_FAILURE); } // Get the info from the current tag printf("Found MIFARE Ultralight card with UID: "); size_t szPos; for (szPos = 0; szPos < rt->nt.nti.nai.szUidLen; szPos++) { printf("%02x", rt->nt.nti.nai.abtUid[szPos]); } printf("\n"); }
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; }
NFCReaderUnit::WriteUIDConfigGuard::WriteUIDConfigGuard(NFCReaderUnit &ru) : ru_(ru) { int ret; // Configure the CRC ret = nfc_device_set_property_bool(ru.getDevice(), NP_HANDLE_CRC, false); assert(ret >= 0); // Use raw send/receive methods ret = nfc_device_set_property_bool(ru.getDevice(), NP_EASY_FRAMING, false); assert(ret >= 0); // Disable 14443-4 autoswitching ret = nfc_device_set_property_bool(ru.getDevice(), NP_AUTO_ISO14443_4, false); assert(ret >= 0); // Disable all error checking. This is required for changing the UID. rca_error_flag_ = ru.getDefaultNFCReaderCardAdapter()->ignoreAllError(true); dt_error_flag_ = std::dynamic_pointer_cast<NFCDataTransport>(ru.getDefaultNFCReaderCardAdapter()->getDataTransport()) ->ignoreAllError(true); }
/* * Callback for freefare_tag_new to test presence of a MIFARE UltralightC on the reader. */ bool is_mifare_ultralightc_on_reader (nfc_device *device, nfc_iso14443a_info nai) { int ret; uint8_t cmd_step1[2]; uint8_t res_step1[9]; cmd_step1[0] = 0x1A; cmd_step1[1] = 0x00; nfc_target pnti; nfc_modulation modulation = { .nmt = NMT_ISO14443A, .nbr = NBR_106 }; nfc_initiator_select_passive_target (device, modulation, nai.abtUid, nai.szUidLen, &pnti); nfc_device_set_property_bool (device, NP_EASY_FRAMING, false); ret = nfc_initiator_transceive_bytes (device, cmd_step1, sizeof (cmd_step1), res_step1, sizeof(res_step1), 0); nfc_device_set_property_bool (device, NP_EASY_FRAMING, true); nfc_initiator_deselect_target (device); return ret >= 0; }
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; }
bool mf_configure_device() { // Disallow invalid frame if (nfc_device_set_property_bool(device, NP_ACCEPT_INVALID_FRAMES, false) < 0) return false; // Disallow multiple frames if (nfc_device_set_property_bool(device, NP_ACCEPT_MULTIPLE_FRAMES, false) < 0) return false; // Make sure we reset the CRC and parity to chip handling. if (nfc_device_set_property_bool(device, NP_HANDLE_CRC, true) < 0) return false; if (nfc_device_set_property_bool(device, NP_HANDLE_PARITY, true) < 0) return false; // Disable ISO14443-4 switching in order to read devices that emulate // Mifare Classic with ISO14443-4 compliance. if (nfc_device_set_property_bool(device, NP_AUTO_ISO14443_4, false) < 0) return false; // Activate "easy framing" feature by default if (nfc_device_set_property_bool(device, NP_EASY_FRAMING, true) < 0) return false; // Deactivate the CRYPTO1 cipher, it may could cause problems when // still active if (nfc_device_set_property_bool(device, NP_ACTIVATE_CRYPTO1, false) < 0) return false; // Drop explicitely the field if (nfc_device_set_property_bool(device, NP_ACTIVATE_FIELD, false) < 0) return false; // Override default initialization option, only try to select a tag once. if (nfc_device_set_property_bool(device, NP_INFINITE_SELECT, false) < 0) return false; return true; }
int main(int argc, const char *argv[]) { (void) argc; (void) argv; nfc_context *context; nfc_init(&context); if (context == NULL) { ERR("Unable to init libnfc (malloc)"); exit(EXIT_FAILURE); } // Display libnfc version const char *acLibnfcVersion = nfc_version(); printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion); // Open using the first available NFC device nfc_device *pnd; pnd = nfc_open(context, NULL); if (pnd == NULL) { ERR("%s", "Unable to open NFC device."); nfc_exit(context); exit(EXIT_FAILURE); } printf("NFC device: %s opened\n", nfc_device_get_name(pnd)); // Print the example's menu printf("\nSelect the communication mode:\n"); printf("[1] Virtual card mode.\n"); printf("[2] Wired card mode.\n"); printf("[3] Dual card mode.\n"); printf(">> "); // Take user's choice int input = getchar(); printf("\n"); if ((input < '1') || (input > '3')) { ERR("%s", "Invalid selection."); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } /* * '1' -> "Virtual mode" (0x02) * '2' -> "Wired card" (0x03) * '3' -> "Dual card" (0x04) */ int iMode = input - '0' + 0x01; pn532_sam_mode mode = iMode; // Connect with the SAM switch (mode) { case PSM_VIRTUAL_CARD: { // FIXME Its a private pn53x function if (pn532_SAMConfiguration(pnd, mode, 0) < 0) { nfc_perror(pnd, "pn53x_SAMConfiguration"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } printf("Now the SAM is readable for 1 minute from an external reader.\n"); wait_one_minute(); } break; case PSM_WIRED_CARD: { // Set opened NFC device to initiator mode if (nfc_initiator_init_secure_element(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init_secure_element"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Let the reader only try once to find a tag if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Read the SAM's info const nfc_modulation nmSAM = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; nfc_target nt; int res; if ((res = nfc_initiator_select_passive_target(pnd, nmSAM, NULL, 0, &nt)) < 0) { nfc_perror(pnd, "nfc_initiator_select_passive_target"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } else if (res == 0) { ERR("No SAM found."); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } else if (res == 1) { printf("The following ISO14443A tag (SAM) was found:\n"); print_nfc_target(&nt, true); } else { ERR("%s", "More than one ISO14442 tag found as SAM."); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } } break; case PSM_DUAL_CARD: { // FIXME Its a private pn53x function if (pn532_SAMConfiguration(pnd, mode, 0) < 0) { nfc_perror(pnd, "pn53x_SAMConfiguration"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } uint8_t abtRx[MAX_FRAME_LEN]; nfc_target nt = { .nm = { .nmt = NMT_ISO14443A, .nbr = NBR_UNDEFINED, }, .nti = { .nai = { .abtAtqa = { 0x04, 0x00 }, .abtUid = { 0x08, 0xad, 0xbe, 0xef }, .btSak = 0x20, .szUidLen = 4, .szAtsLen = 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; }
void nfosc_start() { if (running) return; max_symbol_id = 0; session_id = -1; buffer_size = 0; // try to open the NFC device printf("nfOSC v0.5 using libnfc v%s\n", nfc_version()); printf("looking for NFC devices ...\n"); fflush(stdout); nfc_init(&context); if (context == NULL) { fprintf(stderr, "unable to init libnfc (malloc)\n"); exit(1); } nfc_connstring connstrings[MAX_DEVICE_COUNT]; size_t szFound = nfc_list_devices (context, connstrings, MAX_DEVICE_COUNT); no_devices = (int)szFound; for (int dev=0;dev<no_devices;dev++) { pnd[device_count] = nfc_open(context, connstrings[dev]); if (pnd[device_count] == NULL) continue; nfc_initiator_init(pnd[device_count]); // drop the field for a while nfc_device_set_property_bool(pnd[device_count],NP_ACTIVATE_FIELD,false); // let the reader only try once to find a tag nfc_device_set_property_bool(pnd[device_count],NP_INFINITE_SELECT,false); // configure the CRC and Parity settings nfc_device_set_property_bool(pnd[device_count],NP_HANDLE_CRC,true); nfc_device_set_property_bool(pnd[device_count],NP_HANDLE_PARITY,true); // enable field so more power consuming cards can power themselves up nfc_device_set_property_bool(pnd[device_count],NP_ACTIVATE_FIELD,true); printf("connected to NFC reader #%d: %s\n",device_count,nfc_device_get_name(pnd[device_count])); device_count++; } no_devices = device_count; if (device_count==0) { printf("no device found!\n"); return; } else if (device_count==1) { printf("1 device found\n"); sprintf(source_string, "NFOSC"); } else printf("%d devices found\n", device_count); read_database(); printf("sending OSC packets to %s %s\n",host,port); target = lo_address_new(host, port); running = true; pthread_create(&main_thread , NULL, (void *)&main_loop, NULL); }
void main_loop(void *data) { int i,j,n; int32_t fseq = 0; //verbose = 2; // get the local IP adress for the TUIO2 source attribute char hostname[64]; struct hostent *hp = NULL; struct in_addr *addr = NULL; gethostname(hostname, 64); hp = gethostbyname(hostname); if (hp==NULL) { sprintf(hostname, "%s.local", hostname); hp = gethostbyname(hostname); } if (hp!=NULL) { for (i = 0; hp->h_addr_list[i] != 0; ++i) { addr = (struct in_addr *)(hp->h_addr_list[i]); } } else { //generate a random internet address srand ( (unsigned int)time(NULL) ); int32_t r = rand(); addr = (struct in_addr*)&r; } while (running) { uint8_t tag_count = 0; bool updated = false; lo_bundle osc_bundle = lo_bundle_new(LO_TT_IMMEDIATE); nfc_target ant[MAX_TARGET_COUNT]; // List ISO14443A targets if (verbose>1) printf("polling for a ISO14443A (MIFARE) tag:\n"); nfc_modulation nm = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; lo_timetag frame_time; lo_timetag_now (&frame_time); for (int dev=0;dev<no_devices;dev++) { if (pnd[dev] == NULL) continue; if (!running) return; lo_message frm_message = lo_message_new(); lo_message_add_int32(frm_message, fseq); lo_message_add_timetag(frm_message, frame_time); lo_message_add_int32(frm_message, 0); // sensor dim if (device_count>1) sprintf(source_string, "NFOSC:%d",dev); lo_message_add_string(frm_message, source_string); // source name lo_bundle_add_message(osc_bundle, "/tuio2/frm", frm_message); fseq++; int szTargetFound = nfc_initiator_list_passive_targets (pnd[dev], nm, ant, MAX_TARGET_COUNT); if (szTargetFound<0) { if (no_devices==1) printf("NFC reader disconnected ...\n"); else printf("NFC reader #%d disconnected ...\n",dev); pnd[dev] = NULL; device_count--; if (device_count==0) { running=false; return; } continue; } else if (szTargetFound>0) { for (n = 0; n < szTargetFound; n++) { if (!running) return; bool added = false; nfosc_t found_tag; found_tag.type_id = MIFARE_OTHER; found_tag.device_id = dev; decode_hex(ant[n].nti.nai.abtUid,ant[n].nti.nai.szUidLen); strcpy(found_tag.uid_str,uid_str); if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x04) && (ant[n].nti.nai.btSak == 0x09)) { if (verbose>1) printf("NXP MIFARE Mini - UID: %s\n",uid_str); found_tag.type_id = MIFARE_MINI; } else if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x04) && (ant[n].nti.nai.btSak == 0x08)) { if (verbose>1) printf("NXP MIFARE Classic 1K - UID: %s\n",uid_str); found_tag.type_id = MIFARE_CLASSIC_1K; } else if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x02) && (ant[n].nti.nai.btSak == 0x18)) { if (verbose>1) printf("NXP MIFARE Classic 4K - UID: %s\n",uid_str); found_tag.type_id = MIFARE_CLASSIC_4K; } else if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x02) && (ant[n].nti.nai.btSak == 0x38)) { if (verbose>1) printf("Nokia MIFARE Classic 4K - emulated - UID: %s\n",uid_str); found_tag.type_id = MIFARE_CLASSIC_4K; } else if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x44) && (ant[n].nti.nai.btSak == 0x00)) { if (verbose>1) printf("NXP MIFARE Ultralight - UID: %s\n",uid_str); found_tag.type_id = MIFARE_ULTRALIGHT; } else if ((ant[n].nti.nai.abtAtqa[0] == 0x03) && (ant[n].nti.nai.abtAtqa[1] == 0x44) && (ant[n].nti.nai.btSak == 0x20)) { if (verbose>1) printf("NXP MIFARE DESFire - UID: %s\n",uid_str); } else if ((ant[n].nti.nai.abtAtqa[0] == 0x03) && (ant[n].nti.nai.abtAtqa[1] == 0x04) && (ant[n].nti.nai.btSak == 0x28)) { if (verbose>1) printf("NXP JCOP31 - UID: %s\n",uid_str); } else if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x48) && (ant[n].nti.nai.btSak == 0x20)) { /* @todo handle ATS to be able to know which one it is */ if (verbose>1) printf("NXP JCOP31 or JCOP41 - UID: %s\n",uid_str); } else if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x04) && (ant[n].nti.nai.btSak == 0x28)) { if (verbose>1) printf("NXP JCOP41 - UID: %s\n",uid_str); } else if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x04) && (ant[n].nti.nai.btSak == 0x88)) { if (verbose>1) printf("Infineon MIFARE Classic 1K - UID: %s\n",uid_str); found_tag.type_id = MIFARE_CLASSIC_1K; } else if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x02) && (ant[n].nti.nai.btSak == 0x98)) { if (verbose>1) printf("Gemplus MPCOS - UID: %s\n",uid_str); } else if ((ant[n].nti.nai.abtAtqa[0] == 0x0C) && (ant[n].nti.nai.abtAtqa[1] == 0x00)) { /* @note not sure if Jewel can be detected using this modulation */ if (verbose>1) printf("Innovision R&T Jewel - UID: %s\n",uid_str); } else { if (verbose>1) { printf("ATQA (SENS_RES): %s\n", decode_hex(ant[n].nti.nai.abtAtqa,2)); printf(" UID (NFCID%c): ",(ant[n].nti.nai.abtUid[0]==0x08?'3':'1')); printf("%s\n",decode_hex(ant[n].nti.nai.abtUid,ant[n].nti.nai.szUidLen)); printf(" SAK (SEL_RES): %s\n", decode_hex(&ant[n].nti.nai.btSak,1)); if (ant[n].nti.nai.szAtsLen) { printf(" ATS (ATR): %s\n",decode_hex(ant[n].nti.nai.abtAts,ant[n].nti.nai.szAtsLen)); } printf("\n"); } } int32_t symbol_id = max_symbol_id; int32_t i; for (i=0;i<max_symbol_id;i++) { if (strcmp(tag_database[i].uid_str,found_tag.uid_str)==0) { symbol_id = i; found_tag.symbol_id = symbol_id; break; } } if (symbol_id==max_symbol_id) { symbol_id = max_symbol_id; max_symbol_id++; found_tag.symbol_id = symbol_id; session_id++; found_tag.session_id = session_id; tag_database[symbol_id] = found_tag; tag_buffer[buffer_size] = found_tag; buffer_size++; if (verbose) printf("assigning ID %d to UID %s\n",found_tag.symbol_id,found_tag.uid_str); added = true; } else { int32_t b_pos = buffer_size; for (i=0;i<buffer_size;i++) { if ((strcmp(tag_buffer[i].uid_str,found_tag.uid_str)==0) && (tag_buffer[i].device_id==found_tag.device_id)) { found_tag.session_id = tag_buffer[i].session_id; b_pos=i; break; } } if (b_pos==buffer_size) { session_id++; found_tag.session_id = session_id; tag_buffer[buffer_size] = found_tag; buffer_size++; added=true; } } lo_message sym_message = lo_message_new(); lo_message_add_int32(sym_message, found_tag.session_id); lo_message_add_int32(sym_message, found_tag.type_id); lo_message_add_int32(sym_message, found_tag.symbol_id); switch (found_tag.type_id) { case MIFARE_ULTRALIGHT: lo_message_add_string(sym_message, "mifare/ul"); break; case MIFARE_CLASSIC_1K: lo_message_add_string(sym_message, "mifare/1k"); break; case MIFARE_CLASSIC_4K: lo_message_add_string(sym_message, "mifare/4k"); break; case MIFARE_MINI: lo_message_add_string(sym_message, "mifare/mini"); break; default: lo_message_add_string(sym_message, "mifare/other"); } lo_message_add_string(sym_message, found_tag.uid_str); lo_bundle_add_message(osc_bundle, "/tuio2/sym", sym_message); if (added) { lo_message add_message = lo_message_new(); lo_message_add_int32(add_message, found_tag.device_id); lo_message_add_int32(add_message, found_tag.symbol_id); lo_message_add_int32(add_message, found_tag.type_id); lo_message_add_string(add_message, found_tag.uid_str); lo_bundle_add_message(osc_bundle, "/nfosc/add", add_message); updated = true; if (verbose) printf("add %d %d %d %s\n",found_tag.device_id,found_tag.symbol_id,found_tag.type_id,found_tag.uid_str); } frame_buffer[tag_count] = found_tag; tag_count++; } } lo_message sid_message = lo_message_new(); for (i=0;i<tag_count;i++) if (frame_buffer[i].device_id==dev) lo_message_add_int32(sid_message, frame_buffer[i].session_id); lo_bundle_add_message(osc_bundle, "/tuio2/alv", sid_message); } for (i=0;i<buffer_size;i++) { bool removed = true; for (j=0;j<=tag_count;j++) { if ((strcmp(tag_buffer[i].uid_str,frame_buffer[j].uid_str)==0) && (tag_buffer[i].device_id == frame_buffer[j].device_id)) { removed=false; break; } } if (removed) { lo_message del_message = lo_message_new(); lo_message_add_int32(del_message, tag_buffer[i].device_id); lo_message_add_int32(del_message, tag_buffer[i].symbol_id); lo_message_add_int32(del_message, tag_buffer[i].type_id); lo_message_add_string(del_message, tag_buffer[i].uid_str); lo_bundle_add_message(osc_bundle, "/nfosc/del", del_message); updated = true; if (verbose) printf("del %d %d %d %s\n",tag_buffer[i].device_id,tag_buffer[i].symbol_id,tag_buffer[i].type_id,tag_buffer[i].uid_str); } } if (updated) { if(lo_send_bundle(target, osc_bundle) == -1) { fprintf(stderr, "an OSC error occured: %s\n", lo_address_errstr(target)); } } if (verbose>1) { if (tag_count==0) printf("no tag was found ...\n\n"); else if (tag_count==1) printf("1 tag was found ...\n\n"); else printf("%d tags were found ...\n\n",tag_count); } buffer_size = tag_count; for (i=0; i<tag_count; i++) { tag_buffer[i] = frame_buffer[i]; frame_buffer[i] = empty_tag; } for (int dev=0;dev<no_devices;dev++) { if (pnd[dev] == NULL) continue; if (nfc_device_set_property_bool(pnd[dev],NP_ACTIVATE_FIELD,false)<0) { if (running) { if (no_devices==1) printf("NFC reader disconnected ...\n"); else printf("NFC reader #%d disconnected ...\n",dev); } pnd[dev] = NULL; device_count--; if (device_count==0) { running=false; return; } } else { nfc_device_set_property_bool(pnd[dev],NP_ACTIVATE_FIELD,true); } } usleep(1000/no_devices); } }
int main(int argc, const char *argv[]) { int iAction = 0; uint8_t iDumpSize = sizeof(mifareul_tag); uint8_t iUID[MAX_UID_LEN] = { 0x0 }; size_t szUID = 0; bool bOTP = false; bool bLock = false; bool bUID = false; bool bPWD = false; bool bPart = false; bool bFilename = false; FILE *pfDump; if (argc < 3) { print_usage(argv); exit(EXIT_FAILURE); } DBG("\nChecking arguments and settings\n"); // Get commandline options for (int arg = 1; arg < argc; arg++) { if (0 == strcmp(argv[arg], "r")) { iAction = 1; } else if (0 == strcmp(argv[arg], "w")) { iAction = 2; } else if (0 == strcmp(argv[arg], "--with-uid")) { if (arg + 1 == argc) { ERR("Please supply a UID of 4, 7 or 10 bytes long. Ex: a1:b2:c3:d4"); exit(EXIT_FAILURE); } szUID = str_to_uid(argv[++arg], iUID); } else if (0 == strcmp(argv[arg], "--full")) { bOTP = true; bLock = true; bUID = true; } else if (0 == strcmp(argv[arg], "--otp")) { bOTP = true; } else if (0 == strcmp(argv[arg], "--lock")) { bLock = true; } else if (0 == strcmp(argv[arg], "--uid")) { bUID = true; } else if (0 == strcmp(argv[arg], "--check-magic")) { iAction = 3; } else if (0 == strcmp(argv[arg], "--partial")) { bPart = true; } else if (0 == strcmp(argv[arg], "--pw")) { bPWD = true; if (arg + 1 == argc || strlen(argv[++arg]) != 8 || ! ev1_load_pwd(iPWD, argv[arg])) { ERR("Please supply a PASSWORD of 8 HEX digits"); exit(EXIT_FAILURE); } } else { //Skip validation of the filename if (arg != 2) { ERR("%s is not a supported option.", argv[arg]); print_usage(argv); exit(EXIT_FAILURE); } else { bFilename = true; } } } if (! bFilename) { ERR("Please supply a Mifare Dump filename"); exit(EXIT_FAILURE); } nfc_context *context; nfc_init(&context); if (context == NULL) { ERR("Unable to init libnfc (malloc)"); exit(EXIT_FAILURE); } // Try to open the NFC device pnd = nfc_open(context, NULL); if (pnd == NULL) { ERR("Error opening NFC device"); nfc_exit(context); exit(EXIT_FAILURE); } printf("NFC device: %s opened\n", nfc_device_get_name(pnd)); if (list_passive_targets(pnd)) { nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Let the device only try once to find a tag if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Try to find a MIFARE Ultralight tag if (nfc_initiator_select_passive_target(pnd, nmMifare, (szUID) ? iUID : NULL, szUID, &nt) <= 0) { ERR("no tag was found\n"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Test if we are dealing with a MIFARE compatible tag if (nt.nti.nai.abtAtqa[1] != 0x44) { ERR("tag is not a MIFARE Ultralight card\n"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Get the info from the current tag printf("Using MIFARE Ultralight card with UID: "); size_t szPos; for (szPos = 0; szPos < nt.nti.nai.szUidLen; szPos++) { printf("%02x", nt.nti.nai.abtUid[szPos]); } printf("\n"); // test if tag is EV1 if (get_ev1_version()) { if (!bPWD) printf("Tag is EV1 - PASSWORD may be required\n"); printf("EV1 storage size: "); if (abtRx[6] == 0x0b) { printf("48 bytes\n"); uiBlocks = 0x14; iEV1Type = EV1_UL11; iDumpSize = sizeof(mifareul_ev1_mf0ul11_tag); } else if (abtRx[6] == 0x0e) { printf("128 bytes\n"); uiBlocks = 0x29; iEV1Type = EV1_UL21; iDumpSize = sizeof(mifareul_ev1_mf0ul21_tag); } else printf("unknown!\n"); } else { // re-init non EV1 tag if (nfc_initiator_select_passive_target(pnd, nmMifare, (szUID) ? iUID : NULL, szUID, &nt) <= 0) { ERR("no tag was found\n"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } } // EV1 login required if (bPWD) { printf("Authing with PWD: %02x%02x%02x%02x ", iPWD[0], iPWD[1], iPWD[2], iPWD[3]); if (!ev1_pwd_auth(iPWD)) { printf("\n"); ERR("AUTH failed!\n"); exit(EXIT_FAILURE); } else { printf("Success - PACK: %02x%02x\n", abtRx[0], abtRx[1]); memcpy(iPACK, abtRx, 2); } } if (iAction == 1) { memset(&mtDump, 0x00, sizeof(mtDump)); } else if (iAction == 2) { pfDump = fopen(argv[2], "rb"); if (pfDump == NULL) { ERR("Could not open dump file: %s\n", argv[2]); exit(EXIT_FAILURE); } size_t szDump; if (((szDump = fread(&mtDump, 1, sizeof(mtDump), pfDump)) != iDumpSize && !bPart) || szDump <= 0) { ERR("Could not read from dump file or size mismatch: %s\n", argv[2]); fclose(pfDump); exit(EXIT_FAILURE); } if (szDump != iDumpSize) printf("Performing partial write\n"); 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); } if (iAction == 1) { bool bRF = read_card(); printf("Writing data to file: %s ... ", argv[2]); fflush(stdout); pfDump = fopen(argv[2], "wb"); if (pfDump == NULL) { printf("Could not open file: %s\n", argv[2]); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } if (fwrite(&mtDump, 1, uiReadPages * 4, pfDump) != uiReadPages * 4) { printf("Could not write to file: %s\n", argv[2]); fclose(pfDump); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } fclose(pfDump); printf("Done.\n"); if (!bRF) printf("Warning! Read failed - partial data written to file!\n"); } else if (iAction == 2) { write_card(bOTP, bLock, bUID); } else if (iAction == 3) { if (!check_magic()) { printf("Card is not magic\n"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } else { printf("Card is magic\n"); } } nfc_close(pnd); nfc_exit(context); exit(EXIT_SUCCESS); }
int NFC_Mifare_Ultralight(int argc, const char *argv[]) //! Lecture - Écriture d'une carte MIFARE Ultralight { bool bReadAction; FILE *pfDump; if (argc < 3) { sprintf(message_erreur,"Syntaxe incorrecte !"); return EXIT_FAILURE; //! Échec ! } DBG("\nChecking arguments and settings\n"); bReadAction = tolower((int)((unsigned char) * (argv[1])) == 'r'); if (bReadAction) //! LECTURE { memset(&mtDump, 0x00, sizeof(mtDump)); } else //! ÉCRITURE { if (argc !=3 && argc !=6) { sprintf(message_erreur,"Syntaxe incorrecte !"); return EXIT_FAILURE; //! Échec ! } pfDump = fopen(argv[2], "rb"); if (pfDump == NULL) { sprintf(message_erreur,"Could not open dump file: %s !", argv[2]); return EXIT_FAILURE; //! Échec ! } if (fread(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { sprintf(message_erreur,"Could not read from dump file: %s !", argv[2]); fclose(pfDump); return EXIT_FAILURE; //! Échec ! } fclose(pfDump); } DBG("Successfully opened the dump file\n"); nfc_context *context; nfc_init(&context); if (context == NULL) { sprintf(message_erreur,"Unable to init libnfc (malloc) !"); return EXIT_FAILURE; //! Échec ! } //! Try to open the NFC device pnd = nfc_open(context, NULL); if (pnd == NULL) { sprintf(message_erreur,"Error opening NFC device !"); nfc_exit(context); return EXIT_FAILURE; //! Échec ! } if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); nfc_strerror_r(pnd, message_erreur, TAILLE_MESSAGE_ERREUR); nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; //! Échec ! } //! Let the device only try once to find a tag if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_strerror_r(pnd, message_erreur, TAILLE_MESSAGE_ERREUR); nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; //! Échec ! } #ifdef DEBUG_PRINTF fprintf(stderr,"NFC device: %s opened\n", nfc_device_get_name(pnd)); #endif //! Try to find a MIFARE Ultralight tag if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { sprintf(message_erreur,"No tag was found !"); nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; //! Échec ! } //! Test if we are dealing with a MIFARE compatible tag if (nt.nti.nai.abtAtqa[1] != 0x44) { sprintf(message_erreur,"Tag is not a MIFARE Ultralight card !"); nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; //! Échec ! } //! Get the info from the current tag #ifdef DEBUG_PRINTF fprintf(stderr,"Found MIFARE Ultralight card with UID: "); #endif size_t szPos; for (szPos = 0; szPos < nt.nti.nai.szUidLen; szPos++) { #ifdef DEBUG_PRINTF fprintf(stderr,"%02x", nt.nti.nai.abtUid[szPos]); #endif } #ifdef DEBUG_PRINTF fprintf(stderr,"\n"); #endif if (bReadAction) { if (read_card_ultralight()) { #ifdef DEBUG_PRINTF fprintf(stderr,"Writing data to file: %s ... ", argv[2]); #endif fflush(stdout); pfDump = fopen(argv[2], "wb"); if (pfDump == NULL) { sprintf(message_erreur,"Could not open file: %s !", argv[2]); nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; //! Échec ! } if (fwrite(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { sprintf(message_erreur,"Could not write to file: %s !", argv[2]); fclose(pfDump); nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; //! Échec ! } fclose(pfDump); #ifdef DEBUG_PRINTF fprintf(stderr,"Done.\n"); #endif } else //! Échec ! { nfc_close(pnd); nfc_exit(context); return EXIT_FAILURE; } } else { write_card_ultralight(argc,argv); } nfc_close(pnd); nfc_exit(context); return EXIT_SUCCESS; //! Succès ! }
/* * Get a list of the MIFARE targets near to the provided NFC initiator. * * The list has to be freed using the freefare_free_tags() function. */ FreefareTag * freefare_get_tags (nfc_device *device) { FreefareTag *tags = NULL; int tag_count = 0; nfc_initiator_init(device); // Drop the field for a while nfc_device_set_property_bool(device,NP_ACTIVATE_FIELD,false); // Configure the CRC and Parity settings nfc_device_set_property_bool(device,NP_HANDLE_CRC,true); nfc_device_set_property_bool(device,NP_HANDLE_PARITY,true); nfc_device_set_property_bool(device,NP_AUTO_ISO14443_4,true); // Enable field so more power consuming cards can power themselves up nfc_device_set_property_bool(device,NP_ACTIVATE_FIELD,true); // Poll for a ISO14443A (MIFARE) tag nfc_target candidates[MAX_CANDIDATES]; int candidates_count; nfc_modulation modulation = { .nmt = NMT_ISO14443A, .nbr = NBR_106 }; if ((candidates_count = nfc_initiator_list_passive_targets(device, modulation, candidates, MAX_CANDIDATES)) < 0) return NULL; tags = malloc(sizeof (void *)); if(!tags) return NULL; tags[0] = NULL; for (int c = 0; c < candidates_count; c++) { FreefareTag t; if ((t = freefare_tag_new(device, candidates[c]))) { /* (Re)Allocate memory for the found MIFARE targets array */ FreefareTag *p = realloc (tags, (tag_count + 2) * sizeof (FreefareTag)); if (p) tags = p; else return tags; // FAIL! Return what has been found so far. tags[tag_count++] = t; tags[tag_count] = NULL; } } // Poll for a FELICA tag modulation.nmt = NMT_FELICA; modulation.nbr = NBR_424; // FIXME NBR_212 should also be supported if ((candidates_count = nfc_initiator_list_passive_targets(device, modulation, candidates, MAX_CANDIDATES)) < 0) return NULL; for (int c = 0; c < candidates_count; c++) { FreefareTag t; if ((t = freefare_tag_new(device, candidates[c]))) { /* (Re)Allocate memory for the found FELICA targets array */ FreefareTag *p = realloc (tags, (tag_count + 2) * sizeof (FreefareTag)); if (p) tags = p; else return tags; // FAIL! Return what has been found so far. tags[tag_count++] = t; tags[tag_count] = NULL; } } return tags; }
int main(int argc, const char *argv[]) { int iAction = 0; uint8_t iUID[MAX_UID_LEN] = { 0x0 }; size_t szUID = 0; bool bOTP = false; bool bLock = false; bool bUID = false; FILE *pfDump; if (argc < 2) { print_usage(argv); exit(EXIT_FAILURE); } DBG("\nChecking arguments and settings\n"); // Get commandline options for (int arg = 1; arg < argc; arg++) { if (0 == strcmp(argv[arg], "r")) { iAction = 1; } else if (0 == strcmp(argv[arg], "w")) { iAction = 2; } else if (0 == strcmp(argv[arg], "--with-uid")) { if (argc < 5) { ERR("Please supply a UID of 4, 7 or 10 bytes long. Ex: a1:b2:c3:d4"); exit(EXIT_FAILURE); } szUID = str_to_uid(argv[4], iUID); } else if (0 == strcmp(argv[arg], "--full")) { bOTP = true; bLock = true; bUID = true; } else if (0 == strcmp(argv[arg], "--otp")) { bOTP = true; } else if (0 == strcmp(argv[arg], "--lock")) { 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) && (arg != 4)) { ERR("%s is not supported option.", argv[arg]); print_usage(argv); exit(EXIT_FAILURE); } } } if (iAction == 1) { memset(&mtDump, 0x00, sizeof(mtDump)); } else if (iAction == 2) { pfDump = fopen(argv[2], "rb"); if (pfDump == NULL) { ERR("Could not open dump file: %s\n", argv[2]); exit(EXIT_FAILURE); } if (fread(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { ERR("Could not read from dump file: %s\n", argv[2]); fclose(pfDump); 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); } nfc_context *context; nfc_init(&context); if (context == NULL) { ERR("Unable to init libnfc (malloc)"); exit(EXIT_FAILURE); } // Try to open the NFC device pnd = nfc_open(context, NULL); if (pnd == NULL) { ERR("Error opening NFC device"); nfc_exit(context); exit(EXIT_FAILURE); } printf("NFC device: %s opened\n", nfc_device_get_name(pnd)); if (list_passive_targets(pnd)) { nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Let the device only try once to find a tag if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Try to find a MIFARE Ultralight tag if (nfc_initiator_select_passive_target(pnd, nmMifare, (szUID) ? iUID : NULL, szUID, &nt) <= 0) { ERR("no tag was found\n"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Test if we are dealing with a MIFARE compatible tag if (nt.nti.nai.abtAtqa[1] != 0x44) { ERR("tag is not a MIFARE Ultralight card\n"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Get the info from the current tag printf("Using MIFARE Ultralight card with UID: "); size_t szPos; for (szPos = 0; szPos < nt.nti.nai.szUidLen; szPos++) { printf("%02x", nt.nti.nai.abtUid[szPos]); } printf("\n"); if (iAction == 1) { if (read_card()) { printf("Writing data to file: %s ... ", argv[2]); fflush(stdout); pfDump = fopen(argv[2], "wb"); if (pfDump == NULL) { printf("Could not open file: %s\n", argv[2]); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } if (fwrite(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { printf("Could not write to file: %s\n", argv[2]); fclose(pfDump); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } fclose(pfDump); printf("Done.\n"); } } else if (iAction == 2) { write_card(bOTP, bLock, bUID); } else if (iAction == 3) { if (!check_magic()) { printf("Card is not magic\n"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } else { printf("Card is magic\n"); } } nfc_close(pnd); nfc_exit(context); exit(EXIT_SUCCESS); }
int main(int argc, const char *argv[]) { bool bReadAction; FILE *pfDump; if (argc < 3) { printf("\n"); printf("%s r|w <dump.mfd>\n", argv[0]); printf("\n"); printf("r|w - Perform read from or write to card\n"); printf("<dump.mfd> - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n"); printf("\n"); exit(EXIT_FAILURE); } DBG("\nChecking arguments and settings\n"); bReadAction = tolower((int)((unsigned char) * (argv[1])) == 'r'); if (bReadAction) { memset(&mtDump, 0x00, sizeof(mtDump)); } else { pfDump = fopen(argv[2], "rb"); if (pfDump == NULL) { ERR("Could not open dump file: %s\n", argv[2]); exit(EXIT_FAILURE); } if (fread(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { ERR("Could not read from dump file: %s\n", argv[2]); fclose(pfDump); exit(EXIT_FAILURE); } fclose(pfDump); } DBG("Successfully opened the dump file\n"); nfc_context *context; nfc_init(&context); if (context == NULL) { ERR("Unable to init libnfc (malloc)"); exit(EXIT_FAILURE); } // Try to open the NFC device pnd = nfc_open(context, NULL); if (pnd == NULL) { ERR("Error opening NFC device"); nfc_exit(context); exit(EXIT_FAILURE); } if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Let the device only try once to find a tag if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } printf("NFC device: %s opened\n", nfc_device_get_name(pnd)); // Try to find a MIFARE Ultralight tag if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { ERR("no tag was found\n"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Test if we are dealing with a MIFARE compatible tag if (nt.nti.nai.abtAtqa[1] != 0x44) { ERR("tag is not a MIFARE Ultralight card\n"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Get the info from the current tag printf("Found MIFARE Ultralight card with UID: "); size_t szPos; for (szPos = 0; szPos < nt.nti.nai.szUidLen; szPos++) { printf("%02x", nt.nti.nai.abtUid[szPos]); } printf("\n"); if (bReadAction) { if (read_card()) { printf("Writing data to file: %s ... ", argv[2]); fflush(stdout); pfDump = fopen(argv[2], "wb"); if (pfDump == NULL) { printf("Could not open file: %s\n", argv[2]); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } if (fwrite(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { printf("Could not write to file: %s\n", argv[2]); fclose(pfDump); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } fclose(pfDump); printf("Done.\n"); } } else { write_card(); } nfc_close(pnd); nfc_exit(context); exit(EXIT_SUCCESS); }
int main(int argc, const char *argv[]) { if (argc < 2) { usage(); exit(EXIT_FAILURE); } parseopts(argv[1]); nfc_init(&context); if (context == NULL) { ERR("Unable to init libnfc (malloc)"); exit(EXIT_FAILURE); } // Try to open the NFC reader pnd = nfc_open(context, NULL); if (pnd == NULL) { ERR("Error opening NFC reader"); nfc_exit(context); exit(EXIT_FAILURE); } if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); }; // Let the reader only try once to find a tag if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Disable ISO14443-4 switching in order to read devices that emulate Mifare Classic with ISO14443-4 compliance. if (nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); nfc_close(pnd); nfc_exit(context); 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"); nfc_close(pnd); nfc_exit(context); 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"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); // Try to find a MIFARE Classic tag if (select_target(pnd, &nt) <= 0) { printf("Error: no tag was found\n"); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } // Test if we are dealing with a MIFARE compatible tag if ((nt.nti.nai.btSak & 0x08) == 0) { printf("Warning: tag is probably not a MFC!\n"); } printf("Found MIFARE Classic card:\n"); nt.nm = nmMifare; print_nfc_target(&nt, false); // Guessing size if ((nt.nti.nai.abtAtqa[1] & 0x02) == 0x02) // 4K uiBlocks = 0xff; else if ((nt.nti.nai.btSak & 0x01) == 0x01) // 320b uiBlocks = 0x13; else // 1K/2K, checked through RATS uiBlocks = 0x3f; printf("Guessing size: seems to be a %i-byte card\n", (uiBlocks + 1) * 16); if (parse_card()) { printf("Done, %d blocks read.\n", uiBlocks + 1); fflush(stdout); } if(is_addv) if(!easy_add_value(0xff) || !parse_card()) printf("Failed Add Value!!\n"); printTag(&e); nfc_close(pnd); nfc_exit(context); exit(EXIT_SUCCESS); }
bool nfc_initiator_mifare_cmd(nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp, FILE *output) { uint8_t abtRx[265]; size_t szParamLen; uint8_t abtCmd[265]; //bool bEasyFraming; output_file = output; abtCmd[0] = mc; // The MIFARE Classic command abtCmd[1] = ui8Block; // The block address (1K=0x00..0x39, 4K=0x00..0xff) switch (mc) { // Read and store command have no parameter case MC_READ: case MC_STORE: szParamLen = 0; break; // Authenticate command case MC_AUTH_A: case MC_AUTH_B: szParamLen = sizeof(struct mifare_param_auth); break; // Data command case MC_WRITE: szParamLen = sizeof(struct mifare_param_data); break; // Value command case MC_DECREMENT: case MC_INCREMENT: case MC_TRANSFER: szParamLen = sizeof(struct mifare_param_value); break; // Please fix your code, you never should reach this statement default: return false; } // When available, copy the parameter bytes if (szParamLen) memcpy(abtCmd + 2, (uint8_t *) pmp, szParamLen); // FIXME: Save and restore bEasyFraming // bEasyFraming = nfc_device_get_property_bool (pnd, NP_EASY_FRAMING, &bEasyFraming); if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0) { fprintf(output_file, "nfc_device_set_property_bool"); //This should never happen return false; } // Fire the mifare command int res; if ((res = nfc_initiator_transceive_bytes(pnd, abtCmd, 2 + szParamLen, abtRx, sizeof(abtRx), -1)) < 0) { if (res == NFC_ERFTRANS) { // "Invalid received frame", usual means we are // authenticated on a sector but the requested MIFARE cmd (read, write) // is not permitted by current access bytes; // So there is nothing to do here. } else { fprintf(output_file, "Mifare authentication failed: nfc_initiator_transceive_bytes"); } // XXX nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming); return false; } // When we have executed a read command, copy the received bytes into the param if (mc == MC_READ) { if (res == 16) { memcpy(pmp->mpd.abtData, abtRx, 16); } else { return false; } } return true; }