2010-01-15 11:18:11 +01:00
|
|
|
/*-
|
2009-10-12 16:52:26 +02:00
|
|
|
* Public platform independent Near Field Communication (NFC) library
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009, Roel Verdult
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2010-01-15 11:18:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-10-08 16:05:10 +02:00
|
|
|
* @file nfc-emulate-uid.c
|
2010-10-14 13:53:27 +02:00
|
|
|
* @brief Emulates a tag which which have a "really" custom UID
|
2010-10-08 16:05:10 +02:00
|
|
|
*
|
2010-10-08 21:24:54 +02:00
|
|
|
* NFC devices are able to emulate passive tags but manufacturers restrict the
|
|
|
|
* customization of UID. With PN53x, UID is only 4-byte long and the first
|
|
|
|
* byte of emulated UID is hard-wired to 0x08 which is the standard way to say
|
|
|
|
* this is a random UID. This example shows how to emulate a fully customized
|
|
|
|
* UID by "manually" replying to anti-collision process sent by the initiator.
|
2009-10-12 16:52:26 +02:00
|
|
|
*/
|
2009-05-01 17:47:44 +02:00
|
|
|
|
2010-01-15 11:18:11 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-09-07 19:51:03 +02:00
|
|
|
# include "config.h"
|
2010-01-15 11:18:11 +01:00
|
|
|
#endif // HAVE_CONFIG_H
|
|
|
|
|
2009-05-01 17:47:44 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-05-27 14:18:21 +02:00
|
|
|
#include <stddef.h>
|
2009-05-27 12:13:19 +02:00
|
|
|
#include <stdint.h>
|
2009-05-01 17:47:44 +02:00
|
|
|
#include <string.h>
|
2010-10-12 17:51:57 +02:00
|
|
|
#include <signal.h>
|
2009-05-27 12:13:19 +02:00
|
|
|
|
2009-12-01 15:23:00 +01:00
|
|
|
#include <nfc/nfc.h>
|
2009-11-02 12:34:58 +01:00
|
|
|
|
2009-12-01 15:23:00 +01:00
|
|
|
#include <nfc/nfc-messages.h>
|
2010-04-16 18:38:57 +02:00
|
|
|
#include "nfc-utils.h"
|
2009-05-01 17:47:44 +02:00
|
|
|
|
2009-11-10 10:47:59 +01:00
|
|
|
#define MAX_FRAME_LEN 264
|
|
|
|
|
2009-07-16 14:09:06 +02:00
|
|
|
static byte_t abtRecv[MAX_FRAME_LEN];
|
2009-10-02 11:52:02 +02:00
|
|
|
static size_t szRecvBits;
|
2010-09-07 19:51:03 +02:00
|
|
|
static nfc_device_t *pnd;
|
2009-05-01 17:47:44 +02:00
|
|
|
|
|
|
|
// ISO14443A Anti-Collision response
|
2010-09-07 19:51:03 +02:00
|
|
|
byte_t abtAtqa[2] = { 0x04, 0x00 };
|
2010-10-14 13:53:27 +02:00
|
|
|
byte_t abtUidBcc[5] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x62 };
|
2010-09-07 19:51:03 +02:00
|
|
|
byte_t abtSak[9] = { 0x08, 0xb6, 0xdd };
|
2009-05-01 17:47:44 +02:00
|
|
|
|
2010-10-12 17:51:57 +02:00
|
|
|
void
|
|
|
|
intr_hdlr (void)
|
|
|
|
{
|
|
|
|
printf ("\nQuitting...\n");
|
|
|
|
if (pnd != NULL) {
|
|
|
|
nfc_disconnect(pnd);
|
|
|
|
}
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
void
|
|
|
|
print_usage (char *argv[])
|
2009-09-07 12:15:34 +02:00
|
|
|
{
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("Usage: %s [OPTIONS] [UID]\n", argv[0]);
|
|
|
|
printf ("Options:\n");
|
|
|
|
printf ("\t-h\tHelp. Print this message.\n");
|
2010-10-08 16:05:10 +02:00
|
|
|
printf ("\t-q\tQuiet mode. Silent output: received and sent frames will not be shown (improves timing).\n");
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("\n");
|
2010-10-14 13:53:27 +02:00
|
|
|
printf ("\t[UID]\tUID to emulate, specified as 8 HEX digits (default is DEADBEEF).\n");
|
2009-09-07 12:15:34 +02:00
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
2009-09-07 12:15:34 +02:00
|
|
|
{
|
2010-09-07 19:51:03 +02:00
|
|
|
byte_t *pbtTx = NULL;
|
|
|
|
size_t szTxBits;
|
|
|
|
bool quiet_output = false;
|
2009-08-23 11:50:46 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
int arg,
|
|
|
|
i;
|
2009-09-07 12:15:34 +02:00
|
|
|
|
2009-08-23 11:50:46 +02:00
|
|
|
// Get commandline options
|
2010-09-07 19:51:03 +02:00
|
|
|
for (arg = 1; arg < argc; arg++) {
|
|
|
|
if (0 == strcmp (argv[arg], "-h")) {
|
|
|
|
print_usage (argv);
|
2010-10-08 16:05:10 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2010-09-07 19:51:03 +02:00
|
|
|
} else if (0 == strcmp (argv[arg], "-q")) {
|
2010-10-08 16:05:10 +02:00
|
|
|
printf ("Quiet mode.\n");
|
2009-08-26 12:57:38 +02:00
|
|
|
quiet_output = true;
|
2010-09-07 19:51:03 +02:00
|
|
|
} else if ((arg == argc - 1) && (strlen (argv[arg]) == 8)) { // See if UID was specified as HEX string
|
|
|
|
byte_t abtTmp[3] = { 0x00, 0x00, 0x00 };
|
|
|
|
printf ("[+] Using UID: %s\n", argv[arg]);
|
|
|
|
abtUidBcc[4] = 0x00;
|
|
|
|
for (i = 0; i < 4; ++i) {
|
|
|
|
memcpy (abtTmp, argv[arg] + i * 2, 2);
|
|
|
|
abtUidBcc[i] = (byte_t) strtol ((char *) abtTmp, NULL, 16);
|
2009-09-07 12:15:34 +02:00
|
|
|
abtUidBcc[4] ^= abtUidBcc[i];
|
|
|
|
}
|
|
|
|
} else {
|
2010-09-07 19:51:03 +02:00
|
|
|
ERR ("%s is not supported option.", argv[arg]);
|
|
|
|
print_usage (argv);
|
2010-10-08 16:05:10 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2009-08-23 11:50:46 +02:00
|
|
|
}
|
2009-08-21 15:34:53 +02:00
|
|
|
}
|
|
|
|
|
2010-10-12 17:51:57 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
signal (SIGINT, (void (__cdecl *) (int)) intr_hdlr);
|
|
|
|
#else
|
|
|
|
signal (SIGINT, (void (*)()) intr_hdlr);
|
|
|
|
#endif
|
|
|
|
|
2010-10-08 16:05:10 +02:00
|
|
|
// Try to open the NFC device
|
2010-09-07 19:51:03 +02:00
|
|
|
pnd = nfc_connect (NULL);
|
2009-09-07 12:15:34 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
if (pnd == NULL) {
|
2010-10-08 16:05:10 +02:00
|
|
|
printf ("Unable to connect to NFC device\n");
|
|
|
|
exit(EXIT_FAILURE);
|
2009-05-01 17:47:44 +02:00
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("\n");
|
2010-10-08 16:05:10 +02:00
|
|
|
printf ("Connected to NFC device: %s\n", pnd->acName);
|
|
|
|
printf ("[+] Try to break out the auto-emulation, this requires a second NFC device!\n");
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("[+] To do this, please send any command after the anti-collision\n");
|
2010-10-08 16:05:10 +02:00
|
|
|
printf ("[+] For example, send a RATS command or use the \"nfc-anticol\" or \"nfc-list\" tool.\n");
|
2010-10-04 14:46:03 +02:00
|
|
|
|
|
|
|
// Note: We have to build a "fake" nfc_target_t in order to do exactly the same that was done before the new nfc_target_init() was introduced.
|
|
|
|
nfc_target_t nt = {
|
2010-10-13 21:17:51 +02:00
|
|
|
.nm.nmt = NMT_ISO14443A,
|
|
|
|
.nm.nbr = NBR_UNDEFINED,
|
2010-10-04 20:00:17 +02:00
|
|
|
.nti.nai.abtAtqa = { 0x04, 0x00 },
|
2010-10-14 13:53:27 +02:00
|
|
|
.nti.nai.abtUid = { 0xde, 0xad, 0xbe, 0xef },
|
2010-10-04 14:46:03 +02:00
|
|
|
.nti.nai.btSak = 0x20,
|
2010-10-12 16:04:30 +02:00
|
|
|
.nti.nai.szUidLen = 4,
|
2010-10-04 14:46:03 +02:00
|
|
|
.nti.nai.szAtsLen = 0,
|
|
|
|
};
|
2010-10-15 16:32:10 +02:00
|
|
|
if (!nfc_target_init (pnd, &nt, abtRecv, &szRecvBits)) {
|
2010-10-08 16:05:10 +02:00
|
|
|
ERR ("Could not come out of auto-emulation, no command was received");
|
|
|
|
exit(EXIT_FAILURE);
|
2009-05-01 17:47:44 +02:00
|
|
|
}
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("[+] Received initiator command: ");
|
|
|
|
print_hex_bits (abtRecv, szRecvBits);
|
|
|
|
printf ("[+] Configuring communication\n");
|
|
|
|
if (!nfc_configure (pnd, NDO_HANDLE_CRC, false) || !nfc_configure (pnd, NDO_HANDLE_PARITY, true)) {
|
|
|
|
nfc_perror (pnd, "nfc_configure");
|
|
|
|
exit (EXIT_FAILURE);
|
2010-08-18 19:22:13 +02:00
|
|
|
}
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("[+] Done, the emulated tag is initialized with UID: %02X%02X%02X%02X\n\n", abtUidBcc[0], abtUidBcc[1],
|
|
|
|
abtUidBcc[2], abtUidBcc[3]);
|
2009-05-01 17:47:44 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
while (true) {
|
2009-05-01 17:47:44 +02:00
|
|
|
// Test if we received a frame
|
2010-09-07 19:51:03 +02:00
|
|
|
if (nfc_target_receive_bits (pnd, abtRecv, &szRecvBits, NULL)) {
|
2009-05-01 17:47:44 +02:00
|
|
|
// Prepare the command to send back for the anti-collision request
|
2010-09-07 19:51:03 +02:00
|
|
|
switch (szRecvBits) {
|
|
|
|
case 7: // Request or Wakeup
|
|
|
|
pbtTx = abtAtqa;
|
|
|
|
szTxBits = 16;
|
|
|
|
// New anti-collsion session started
|
|
|
|
if (!quiet_output)
|
|
|
|
printf ("\n");
|
2009-05-01 17:47:44 +02:00
|
|
|
break;
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
case 16: // Select All
|
|
|
|
pbtTx = abtUidBcc;
|
|
|
|
szTxBits = 40;
|
2009-05-01 17:47:44 +02:00
|
|
|
break;
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
case 72: // Select Tag
|
|
|
|
pbtTx = abtSak;
|
|
|
|
szTxBits = 24;
|
2009-05-01 17:47:44 +02:00
|
|
|
break;
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
default: // unknown length?
|
|
|
|
szTxBits = 0;
|
2009-05-01 17:47:44 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!quiet_output) {
|
|
|
|
printf ("R: ");
|
|
|
|
print_hex_bits (abtRecv, szRecvBits);
|
2009-08-23 11:50:46 +02:00
|
|
|
}
|
2009-05-01 17:47:44 +02:00
|
|
|
// Test if we know how to respond
|
2010-09-07 19:51:03 +02:00
|
|
|
if (szTxBits) {
|
2009-05-01 17:47:44 +02:00
|
|
|
// Send and print the command to the screen
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!nfc_target_send_bits (pnd, pbtTx, szTxBits, NULL)) {
|
|
|
|
nfc_perror (pnd, "nfc_target_send_bits");
|
|
|
|
exit (EXIT_FAILURE);
|
2010-08-18 19:22:13 +02:00
|
|
|
}
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!quiet_output) {
|
|
|
|
printf ("T: ");
|
|
|
|
print_hex_bits (pbtTx, szTxBits);
|
2009-08-23 11:50:46 +02:00
|
|
|
}
|
2009-05-01 17:47:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
nfc_disconnect (pnd);
|
|
|
|
exit (EXIT_SUCCESS);
|
2009-05-01 17:47:44 +02:00
|
|
|
}
|