int main (int argc, const char *argv[]) { nfc_device_t *pnd; (void) argc; (void) argv; // Display libnfc version const char *acLibnfcVersion = nfc_version (); printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion); // Connect using the first available NFC device pnd = nfc_connect (NULL); if (pnd == NULL) { ERR ("%s", "Unable to connect to NFC device."); return EXIT_FAILURE; } printf ("Connected to NFC device: %s\n", pnd->acName); // 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 char input = getchar (); int mode = input - '0' + 1; printf ("\n"); if (mode < VIRTUAL_CARD_MODE || mode > DUAL_CARD_MODE) { ERR ("%s", "Invalid selection."); return EXIT_FAILURE; } // Connect with the SAM sam_connection (pnd, mode); switch (mode) { case VIRTUAL_CARD_MODE: { // FIXME after the loop the device doesn't respond to host commands... printf ("Now the SAM is readable for 1 minute from an external reader.\n"); wait_one_minute (); } break; case WIRED_CARD_MODE: { nfc_target_t nt; // Set connected NFC device to initiator mode 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); } // Let the reader only try once to find a tag if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) { 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); } // Read the SAM's info const nfc_modulation_t nmSAM = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; if (!nfc_initiator_select_passive_target (pnd, nmSAM, NULL, 0, &nt)) { nfc_perror (pnd, "nfc_initiator_select_passive_target"); ERR ("%s", "Reading of SAM info failed."); return EXIT_FAILURE; } printf ("The following ISO14443A tag (SAM) was found:\n\n"); print_nfc_iso14443a_info (nt.nti.nai, true); } break; case DUAL_CARD_MODE: { byte_t abtRx[MAX_FRAME_LEN]; size_t szRx; nfc_target_t nt = { .nm.nmt = NMT_ISO14443A, .nm.nbr = NBR_UNDEFINED, .nti.nai.abtAtqa = { 0x04, 0x00 }, .nti.nai.abtUid = { 0x08, 0xad, 0xbe, 0xef }, .nti.nai.btSak = 0x20, .nti.nai.szUidLen = 4, .nti.nai.szAtsLen = 0, }; printf ("Now both, NFC device (configured as target) and SAM are readables from an external NFC initiator.\n"); printf ("Please note that NFC device (configured as target) stay in target mode until it receive RATS, ATR_REQ or proprietary command.\n"); if (!nfc_target_init (pnd, &nt, abtRx, &szRx)) { nfc_perror(pnd, "nfc_target_init"); return EXIT_FAILURE; } // wait_one_minute (); } break; } // Disconnect from the SAM sam_connection (pnd, NORMAL_MODE); // Disconnect from NFC device nfc_disconnect (pnd); return EXIT_SUCCESS; }
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[]) { // Try to open the NFC reader pnd = nfc_connect (NULL); if (pnd == NULL) { ERR("Unable to connect to NFC device"); return EXIT_FAILURE; } printf ("Connected to NFC device: %s\n", pnd->acName); printf ("Emulating NDEF tag now, please touch it with a second NFC device\n"); nfc_target_t nt = { .nm.nmt = NMT_ISO14443A, .nm.nbr = NBR_UNDEFINED, // Will be updated by nfc_target_init() .nti.nai.abtAtqa = { 0x00, 0x04 }, .nti.nai.abtUid = { 0x08, 0x00, 0xb0, 0x0b }, .nti.nai.btSak = 0x20, .nti.nai.szUidLen = 4, .nti.nai.szAtsLen = 0, }; if (!nfc_target_init (pnd, &nt, abtRx, &szRx)) { nfc_perror (pnd, "nfc_target_init"); ERR("Could not come out of auto-emulation, no command was received"); return EXIT_FAILURE; } if (!quiet_output) { printf ("Received data: "); print_hex (abtRx, szRx); } //Receiving data: e0 40 //= RATS, FSD=48 //Actually PN532 already sent back the ATS so nothing to send now receive_bytes(); //Receiving data: 00 a4 04 00 06 e1 03 e1 03 e1 03 //= App Select by name "e103e103e103" send_bytes((const byte_t*)"\x6a\x87",2); receive_bytes(); //Receiving data: 00 a4 04 00 06 e1 03 e1 03 e1 03 //= App Select by name "e103e103e103" send_bytes((const byte_t*)"\x6a\x87",2); receive_bytes(); //Receiving data: 00 a4 04 00 07 d2 76 00 00 85 01 00 //= App Select by name "D2760000850100" send_bytes((const byte_t*)"\x90\x00",2); receive_bytes(); //Receiving data: 00 a4 00 00 02 e1 03 //= Select CC send_bytes((const byte_t*)"\x90\x00",2); receive_bytes(); //Receiving data: 00 b0 00 00 0f //= ReadBinary CC //We send CC + OK send_bytes((const byte_t*)"\x00\x0f\x10\x00\x3b\x00\x34\x04\x06\xe1\x04\x0e\xe0\x00\x00\x90\x00",17); receive_bytes(); //Receiving data: 00 a4 00 00 02 e1 04 //= Select NDEF send_bytes((const byte_t*)"\x90\x00",2); receive_bytes(); //Receiving data: 00 b0 00 00 02 //= Read first 2 NDEF bytes //Sent NDEF Length=0x21 send_bytes((const byte_t*)"\x00\x21\x90\x00",4); receive_bytes(); //Receiving data: 00 b0 00 02 21 //= Read remaining of NDEF file send_bytes((const byte_t*)"\xd1\x02\x1c\x53\x70\x91\x01\x09\x54\x02\x65\x6e\x4c\x69\x62\x6e\x66\x63\x51\x01\x0b\x55\x03\x6c\x69\x62\x6e\x66\x63\x2e\x6f\x72\x67\x90\x00",35); nfc_disconnect(pnd); exit (EXIT_SUCCESS); }
int main(int argc, char *argv[]) { MYSQL *conn; MYSQL_RES *result; MYSQL_ROW row; int retval; conn = mysql_init(NULL); retval = mysql_real_connect(conn, def_host_name, def_user_name, def_password, def_db_name, def_port_num, def_socket_name, def_client_flag); if(!retval) { printf("Error connecting to database: %s\n", mysql_error(conn)); return -1; } printf("Connection successful\n"); int error = 0; nfc_device_t *device = NULL; MifareTag *tags = NULL; Mad mad; MifareClassicKey transport_key = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; char ndef_input[15] = {'\0'}; char ID[9] = {'\0'}; double balance = 0; printf("\nID: "); scanf("%s", ID); while (getchar() != '\n') continue; printf("\nBalance: "); scanf("%lf", &balance); while (getchar() != '\n') continue; ID[0] = toupper(ID[0]); ID[1] = toupper(ID[1]); char balance_char[6] = {'\0'}; snprintf(balance_char, 6, "%.2f", balance); //ensure balance always with 4 digit, 4.00 => 04.00 char final_balance[6] = {'\0'}; if(strlen(balance_char) != 5) { strcat(final_balance, "0"); strcat(final_balance, balance_char); } else { strcpy(final_balance, balance_char); } strcat(ndef_input, ID); strcat(ndef_input, final_balance); int i = 0; if (ndef_input == NULL) { printf("Write default message.\n"); for(i=0; i < sizeof(ndef_default_msg); i++) { ndef_msg[i] = (uint8_t)ndef_default_msg[i]; } ndef_msg_len = sizeof(ndef_default_msg); } else { for(i=0; i < strlen(ndef_input); i++) { ndef_msg[i] = (uint8_t)ndef_input[i]; } ndef_msg_len = strlen(ndef_input); } printf ("NDEF message is %zu bytes long.\n", ndef_msg_len); struct mifare_classic_key_and_type *card_write_keys; if (!(card_write_keys = malloc (40 * sizeof (*card_write_keys)))) { err (EXIT_FAILURE, "malloc"); } nfc_device_desc_t devices[8]; size_t device_count; nfc_list_devices (devices, 8, &device_count); if (!device_count) errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; d < device_count; d++) { device = nfc_connect (&(devices[d])); if (!device) { warnx ("nfc_connect() failed."); error = EXIT_FAILURE; continue; } tags = freefare_get_tags (device); if (!tags) { nfc_disconnect (device); errx (EXIT_FAILURE, "Error listing MIFARE classic tag."); } for (int i = 0; (!error) && tags[i]; i++) { switch (freefare_get_tag_type (tags[i])) { case CLASSIC_1K: case CLASSIC_4K: break; default: continue; } char *tag_uid = freefare_get_tag_uid (tags[i]); char buffer[BUFSIZ]; printf ("Found %s with UID %s.\n", freefare_get_tag_friendly_name (tags[i]), tag_uid); bool write_ndef = true; for (int n = 0; n < 40; n++) { memcpy(card_write_keys[n].key, transport_key, sizeof (transport_key)); card_write_keys[n].type = MFC_KEY_A; } if (write_ndef) { switch (freefare_get_tag_type (tags[i])) { case CLASSIC_4K: if (!search_sector_key (tags[i], 0x10, &(card_write_keys[0x10].key), &(card_write_keys[0x10].type))) { error = 1; goto error; } /* fallthrough */ case CLASSIC_1K: if (!search_sector_key (tags[i], 0x00, &(card_write_keys[0x00].key), &(card_write_keys[0x00].type))) { error = 1; goto error; } break; default: /* Keep compiler quiet */ break; } if (!error) { /* Ensure the auth key is always a B one. If not, change it! */ switch (freefare_get_tag_type (tags[i])) { case CLASSIC_4K: if (card_write_keys[0x10].type != MFC_KEY_B) { if( 0 != fix_mad_trailer_block (device, tags[i], 0x10, card_write_keys[0x10].key, card_write_keys[0x10].type)) { error = 1; goto error; } memcpy (&(card_write_keys[0x10].key), &default_keyb, sizeof (MifareClassicKey)); card_write_keys[0x10].type = MFC_KEY_B; } /* fallthrough */ case CLASSIC_1K: if (card_write_keys[0x00].type != MFC_KEY_B) { if( 0 != fix_mad_trailer_block (device, tags[i], 0x00, card_write_keys[0x00].key, card_write_keys[0x00].type)) { error = 1; goto error; } memcpy (&(card_write_keys[0x00].key), &default_keyb, sizeof (MifareClassicKey)); card_write_keys[0x00].type = MFC_KEY_B; } break; default: /* Keep compiler quiet */ break; } } size_t encoded_size; uint8_t *tlv_data = tlv_encode (3, ndef_msg, ndef_msg_len, &encoded_size); /* * At his point, we should have collected all information needed to * succeed. */ // If the card already has a MAD, load it. if ((mad = mad_read (tags[i]))) { // If our application already exists, erase it. MifareClassicSectorNumber *sectors, *p; sectors = p = mifare_application_find (mad, mad_nfcforum_aid); if (sectors) { while (*p) { if (mifare_classic_authenticate (tags[i], mifare_classic_sector_last_block(*p), default_keyb, MFC_KEY_B) < 0) { nfc_perror (device, "mifare_classic_authenticate"); error = 1; goto error; } if (mifare_classic_format_sector (tags[i], *p) < 0) { nfc_perror (device, "mifare_classic_format_sector"); error = 1; goto error; } p++; } } free (sectors); mifare_application_free (mad, mad_nfcforum_aid); } else { // Create a MAD and mark unaccessible sectors in the card if (!(mad = mad_new ((freefare_get_tag_type (tags[i]) == CLASSIC_4K) ? 2 : 1))) { perror ("mad_new"); error = 1; goto error; } MifareClassicSectorNumber max_s; switch (freefare_get_tag_type (tags[i])) { case CLASSIC_1K: max_s = 15; break; case CLASSIC_4K: max_s = 39; break; default: /* Keep compiler quiet */ break; } // Mark unusable sectors as so for (size_t s = max_s; s; s--) { if (s == 0x10) continue; if (!search_sector_key (tags[i], s, &(card_write_keys[s].key), &(card_write_keys[s].type))) { mad_set_aid (mad, s, mad_defect_aid); } else if ((memcmp (card_write_keys[s].key, transport_key, sizeof (transport_key)) != 0) && (card_write_keys[s].type != MFC_KEY_A)) { // Revert to transport configuration if (mifare_classic_format_sector (tags[i], s) < 0) { nfc_perror (device, "mifare_classic_format_sector"); error = 1; goto error; } } } } MifareClassicSectorNumber *sectors = mifare_application_alloc (mad, mad_nfcforum_aid, encoded_size); if (!sectors) { nfc_perror (device, "mifare_application_alloc"); error = EXIT_FAILURE; goto error; } if (mad_write (tags[i], mad, card_write_keys[0x00].key, card_write_keys[0x10].key) < 0) { nfc_perror (device, "mad_write"); error = EXIT_FAILURE; goto error; } int s = 0; while (sectors[s]) { MifareClassicBlockNumber block = mifare_classic_sector_last_block (sectors[s]); MifareClassicBlock block_data; mifare_classic_trailer_block (&block_data, mifare_classic_nfcforum_public_key_a, 0x0, 0x0, 0x0, 0x6, 0x40, default_keyb); if (mifare_classic_authenticate (tags[i], block, card_write_keys[sectors[s]].key, card_write_keys[sectors[s]].type) < 0) { nfc_perror (device, "mifare_classic_authenticate"); error = EXIT_FAILURE; goto error; } if (mifare_classic_write (tags[i], block, block_data) < 0) { nfc_perror (device, "mifare_classic_write"); error = EXIT_FAILURE; goto error; } s++; } if ((ssize_t) encoded_size != mifare_application_write (tags[i], mad, mad_nfcforum_aid, tlv_data, encoded_size, default_keyb, MCAB_WRITE_KEYB)) { nfc_perror (device, "mifare_application_write"); error = EXIT_FAILURE; goto error; } //create new student record in database char sql_stmnt[57] = {'\0'}; int n = 0; //filter tag_uid ulong uid_length = strlen(tag_uid); char uid_esc[(2 * uid_length)+1]; mysql_real_escape_string(conn, uid_esc, tag_uid, uid_length); //filter ID ulong id_length = strlen(ID); char id_esc[(2 * id_length)+1]; mysql_real_escape_string(conn, id_esc, ID, id_length); n = snprintf(sql_stmnt, 57, "INSERT INTO student VALUES('%s', '%s', %.1f)", uid_esc, id_esc, balance); retval = mysql_real_query(conn, sql_stmnt, n); if(retval) { printf("Inserting data from DB Failed\n"); return -1; } printf("Insert to DB successful\n"); free (sectors); free (tlv_data); free (mad); } error: free (tag_uid); } //should at the line 412, but the compiler keep on complaining //jump into scope of identifier with variably modified type //no idea why that happens, goto is always call inside any {} to outside error: //even at 412, still not outside enough freefare_free_tags (tags); nfc_disconnect (device); } free (card_write_keys); mysql_free_result(result); mysql_close(conn); exit (error); }
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"); return 1; } 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]); return 1; } if (fread (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) { ERR ("Could not read from dump file: %s\n", argv[2]); fclose (pfDump); return 1; } fclose (pfDump); } DBG ("Successfully opened the dump file\n"); // Try to open the NFC device pnd = nfc_connect (NULL); if (pnd == NULL) { ERR ("Error connecting NFC device\n"); return 1; } 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); } // Let the device only try once to find a tag if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) { nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) { nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } if (!nfc_configure (pnd, NDO_HANDLE_PARITY, 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 device: %s\n", pnd->acName); // Try to find a MIFARE Ultralight tag if (!nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt)) { ERR ("no tag was found\n"); nfc_disconnect (pnd); return 1; } // 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_disconnect (pnd); return 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]); return EXIT_FAILURE; } if (fwrite (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) { printf ("Could not write to file: %s\n", argv[2]); return EXIT_FAILURE; } fclose (pfDump); printf ("Done.\n"); } } else { write_card (); } nfc_disconnect (pnd); return EXIT_SUCCESS; }
int main(int argc, const char* argv[]) { byte* pbtTx = null; ui32 uiTxBits; // Try to open the NFC reader pdi = nfc_connect(); if (pdi == INVALID_DEVICE_INFO) { printf("Error connecting NFC second reader\n"); return 1; } printf("\n"); printf("[+] Connected to NFC reader: %s\n",pdi->acName); printf("[+] Try to break out the auto-simulation, this requires a second reader!\n"); printf("[+] To do this, please send any command after the anti-collision\n"); printf("[+] For example, send a RATS command or use the \"anticol\" tool\n"); if (!nfc_target_init(pdi,abtRecv,&uiRecvBits)) { printf("Error: Could not come out of auto-simulation, no command was received\n"); return 1; } printf("[+] Received initiator command: "); print_hex_bits(abtRecv,uiRecvBits); printf("[+] Configuring communication\n"); nfc_configure(pdi,DCO_HANDLE_CRC,false); nfc_configure(pdi,DCO_HANDLE_PARITY,true); printf("[+] Done, the simulated tag is initialized\n\n"); while(true) { // Test if we received a frame if (nfc_target_receive_bits(pdi,abtRecv,&uiRecvBits,null)) { // Prepare the command to send back for the anti-collision request switch(uiRecvBits) { case 7: // Request or Wakeup pbtTx = abtAtqa; uiTxBits = 16; // New anti-collsion session started printf("\n"); break; case 16: // Select All pbtTx = abtUidBcc; uiTxBits = 40; break; case 72: // Select Tag pbtTx = abtSak; uiTxBits = 24; break; default: // unknown length? uiTxBits = 0; break; } printf("R: "); print_hex_bits(abtRecv,uiRecvBits); // Test if we know how to respond if(uiTxBits) { // Send and print the command to the screen nfc_target_send_bits(pdi,pbtTx,uiTxBits,null); printf("T: "); print_hex_bits(pbtTx,uiTxBits); } } } nfc_disconnect(pdi); }
int main (int argc, const char *argv[]) { size_t szFound; size_t i; nfc_device_t *pnd; nfc_device_desc_t *pnddDevices; const char *acLibnfcVersion; bool result; byte_t abtRx[PN53x_EXTENDED_FRAME_MAX_LEN]; size_t szRx; const byte_t pncmd_diagnose_communication_line_test[] = { 0xD4, 0x00, 0x00, 0x06, 'l', 'i', 'b', 'n', 'f', 'c' }; const byte_t pncmd_diagnose_rom_test[] = { 0xD4, 0x00, 0x01 }; const byte_t pncmd_diagnose_ram_test[] = { 0xD4, 0x00, 0x02 }; if (argc > 1) { errx (1, "usage: %s", argv[0]); } // Display libnfc version acLibnfcVersion = nfc_version (); printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion); if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) { fprintf (stderr, "malloc() failed\n"); return EXIT_FAILURE; } nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound); if (szFound == 0) { printf ("No NFC device found.\n"); } for (i = 0; i < szFound; i++) { pnd = nfc_connect (&(pnddDevices[i])); if (pnd == NULL) { ERR ("%s", "Unable to connect to NFC device."); return EXIT_FAILURE; } printf ("NFC device [%s] connected.\n", pnd->acName); result = pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test), abtRx, &szRx); if (result) { result = (memcmp (pncmd_diagnose_communication_line_test + 2, abtRx, sizeof (pncmd_diagnose_communication_line_test) - 2) == 0); } printf (" Communication line test: %s\n", result ? "OK" : "Failed"); result = pn53x_transceive (pnd, pncmd_diagnose_rom_test, sizeof (pncmd_diagnose_rom_test), abtRx, &szRx); if (result) { result = ((szRx == 1) && (abtRx[0] == 0x00)); } printf (" ROM test: %s\n", result ? "OK" : "Failed"); result = pn53x_transceive (pnd, pncmd_diagnose_ram_test, sizeof (pncmd_diagnose_ram_test), abtRx, &szRx); if (result) { result = ((szRx == 1) && (abtRx[0] == 0x00)); } printf (" RAM test: %s\n", result ? "OK" : "Failed"); } }