libnfc  1.4.2
nfc-emulate-uid.c
Go to the documentation of this file.
1 /*-
2  * Public platform independent Near Field Communication (NFC) library examples
3  *
4  * Copyright (C) 2009, Roel Verdult
5  * Copyright (C) 2010, Romuald Conty
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * 1) Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * 2 )Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  *
27  * Note that this license only applies on the examples, NFC library itself is under LGPL
28  *
29  */
30 
42 #ifdef HAVE_CONFIG_H
43 # include "config.h"
44 #endif // HAVE_CONFIG_H
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <stddef.h>
49 #include <stdint.h>
50 #include <string.h>
51 #include <signal.h>
52 
53 #include <nfc/nfc.h>
54 
55 #include <nfc/nfc-messages.h>
56 #include "nfc-utils.h"
57 
58 #define MAX_FRAME_LEN 264
59 
60 static byte_t abtRecv[MAX_FRAME_LEN];
61 static size_t szRecvBits;
62 static nfc_device_t *pnd;
63 
64 // ISO14443A Anti-Collision response
65 byte_t abtAtqa[2] = { 0x04, 0x00 };
66 byte_t abtUidBcc[5] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x62 };
67 byte_t abtSak[9] = { 0x08, 0xb6, 0xdd };
68 
69 void
70 intr_hdlr (void)
71 {
72  printf ("\nQuitting...\n");
73  if (pnd != NULL) {
74  nfc_disconnect(pnd);
75  }
76  exit (EXIT_FAILURE);
77 }
78 
79 void
80 print_usage (char *argv[])
81 {
82  printf ("Usage: %s [OPTIONS] [UID]\n", argv[0]);
83  printf ("Options:\n");
84  printf ("\t-h\tHelp. Print this message.\n");
85  printf ("\t-q\tQuiet mode. Silent output: received and sent frames will not be shown (improves timing).\n");
86  printf ("\n");
87  printf ("\t[UID]\tUID to emulate, specified as 8 HEX digits (default is DEADBEEF).\n");
88 }
89 
90 int
91 main (int argc, char *argv[])
92 {
93  byte_t *pbtTx = NULL;
94  size_t szTxBits;
95  bool quiet_output = false;
96 
97  int arg,
98  i;
99 
100  // Get commandline options
101  for (arg = 1; arg < argc; arg++) {
102  if (0 == strcmp (argv[arg], "-h")) {
103  print_usage (argv);
104  exit(EXIT_SUCCESS);
105  } else if (0 == strcmp (argv[arg], "-q")) {
106  printf ("Quiet mode.\n");
107  quiet_output = true;
108  } else if ((arg == argc - 1) && (strlen (argv[arg]) == 8)) { // See if UID was specified as HEX string
109  byte_t abtTmp[3] = { 0x00, 0x00, 0x00 };
110  printf ("[+] Using UID: %s\n", argv[arg]);
111  abtUidBcc[4] = 0x00;
112  for (i = 0; i < 4; ++i) {
113  memcpy (abtTmp, argv[arg] + i * 2, 2);
114  abtUidBcc[i] = (byte_t) strtol ((char *) abtTmp, NULL, 16);
115  abtUidBcc[4] ^= abtUidBcc[i];
116  }
117  } else {
118  ERR ("%s is not supported option.", argv[arg]);
119  print_usage (argv);
120  exit(EXIT_FAILURE);
121  }
122  }
123 
124 #ifdef WIN32
125  signal (SIGINT, (void (__cdecl *) (int)) intr_hdlr);
126 #else
127  signal (SIGINT, (void (*)()) intr_hdlr);
128 #endif
129 
130  // Try to open the NFC device
131  pnd = nfc_connect (NULL);
132 
133  if (pnd == NULL) {
134  printf ("Unable to connect to NFC device\n");
135  exit(EXIT_FAILURE);
136  }
137 
138  printf ("\n");
139  printf ("Connected to NFC device: %s\n", pnd->acName);
140  printf ("[+] Try to break out the auto-emulation, this requires a second NFC device!\n");
141  printf ("[+] To do this, please send any command after the anti-collision\n");
142  printf ("[+] For example, send a RATS command or use the \"nfc-anticol\" or \"nfc-list\" tool.\n");
143 
144  // 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.
145  nfc_target_t nt = {
146  .nm.nmt = NMT_ISO14443A,
147  .nm.nbr = NBR_UNDEFINED,
148  .nti.nai.abtAtqa = { 0x04, 0x00 },
149  .nti.nai.abtUid = { 0x08, 0xad, 0xbe, 0xef },
150  .nti.nai.btSak = 0x20,
151  .nti.nai.szUidLen = 4,
152  .nti.nai.szAtsLen = 0,
153  };
154  if (!nfc_target_init (pnd, &nt, abtRecv, &szRecvBits)) {
155  nfc_perror (pnd, "nfc_target_init");
156  ERR ("Could not come out of auto-emulation, no command was received");
157  exit(EXIT_FAILURE);
158  }
159  printf ("[+] Received initiator command: ");
160  print_hex_bits (abtRecv, szRecvBits);
161  printf ("[+] Configuring communication\n");
162  if (!nfc_configure (pnd, NDO_HANDLE_CRC, false) || !nfc_configure (pnd, NDO_HANDLE_PARITY, true)) {
163  nfc_perror (pnd, "nfc_configure");
164  exit (EXIT_FAILURE);
165  }
166  printf ("[+] Done, the emulated tag is initialized with UID: %02X%02X%02X%02X\n\n", abtUidBcc[0], abtUidBcc[1],
167  abtUidBcc[2], abtUidBcc[3]);
168 
169  while (true) {
170  // Test if we received a frame
171  if (nfc_target_receive_bits (pnd, abtRecv, &szRecvBits, NULL)) {
172  // Prepare the command to send back for the anti-collision request
173  switch (szRecvBits) {
174  case 7: // Request or Wakeup
175  pbtTx = abtAtqa;
176  szTxBits = 16;
177  // New anti-collsion session started
178  if (!quiet_output)
179  printf ("\n");
180  break;
181 
182  case 16: // Select All
183  pbtTx = abtUidBcc;
184  szTxBits = 40;
185  break;
186 
187  case 72: // Select Tag
188  pbtTx = abtSak;
189  szTxBits = 24;
190  break;
191 
192  default: // unknown length?
193  szTxBits = 0;
194  break;
195  }
196 
197  if (!quiet_output) {
198  printf ("R: ");
199  print_hex_bits (abtRecv, szRecvBits);
200  }
201  // Test if we know how to respond
202  if (szTxBits) {
203  // Send and print the command to the screen
204  if (!nfc_target_send_bits (pnd, pbtTx, szTxBits, NULL)) {
205  nfc_perror (pnd, "nfc_target_send_bits");
206  exit (EXIT_FAILURE);
207  }
208  if (!quiet_output) {
209  printf ("T: ");
210  print_hex_bits (pbtTx, szTxBits);
211  }
212  }
213  }
214  }
215 
216  nfc_disconnect (pnd);
217  exit (EXIT_SUCCESS);
218 }