]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhf14a.c
fix ATR length (#790)
[proxmark3-svn] / client / cmdhf14a.c
1 //-----------------------------------------------------------------------------
2 // 2011, 2017 Merlok
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>, Hagen Fritsch
4 //
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
7 // the license.
8 //-----------------------------------------------------------------------------
9 // High frequency ISO14443A commands
10 //-----------------------------------------------------------------------------
11
12 #include "cmdhf14a.h"
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <inttypes.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <ctype.h>
20 #include "util.h"
21 #include "util_posix.h"
22 #include "iso14443crc.h"
23 #include "comms.h"
24 #include "ui.h"
25 #include "cmdparser.h"
26 #include "common.h"
27 #include "cmdmain.h"
28 #include "mifare.h"
29 #include "cmdhfmfu.h"
30 #include "mifarehost.h"
31 #include "cliparser/cliparser.h"
32 #include "emv/apduinfo.h"
33 #include "emv/emvcore.h"
34
35 static int CmdHelp(const char *Cmd);
36 static int waitCmd(uint8_t iLen);
37
38 // structure and database for uid -> tagtype lookups
39 typedef struct {
40 uint8_t uid;
41 char* desc;
42 } manufactureName;
43
44 static const manufactureName manufactureMapping[] = {
45 // ID, "Vendor Country"
46 { 0x01, "Motorola UK" },
47 { 0x02, "ST Microelectronics SA France" },
48 { 0x03, "Hitachi, Ltd Japan" },
49 { 0x04, "NXP Semiconductors Germany" },
50 { 0x05, "Infineon Technologies AG Germany" },
51 { 0x06, "Cylink USA" },
52 { 0x07, "Texas Instrument France" },
53 { 0x08, "Fujitsu Limited Japan" },
54 { 0x09, "Matsushita Electronics Corporation, Semiconductor Company Japan" },
55 { 0x0A, "NEC Japan" },
56 { 0x0B, "Oki Electric Industry Co. Ltd Japan" },
57 { 0x0C, "Toshiba Corp. Japan" },
58 { 0x0D, "Mitsubishi Electric Corp. Japan" },
59 { 0x0E, "Samsung Electronics Co. Ltd Korea" },
60 { 0x0F, "Hynix / Hyundai, Korea" },
61 { 0x10, "LG-Semiconductors Co. Ltd Korea" },
62 { 0x11, "Emosyn-EM Microelectronics USA" },
63 { 0x12, "INSIDE Technology France" },
64 { 0x13, "ORGA Kartensysteme GmbH Germany" },
65 { 0x14, "SHARP Corporation Japan" },
66 { 0x15, "ATMEL France" },
67 { 0x16, "EM Microelectronic-Marin SA Switzerland" },
68 { 0x17, "KSW Microtec GmbH Germany" },
69 { 0x18, "ZMD AG Germany" },
70 { 0x19, "XICOR, Inc. USA" },
71 { 0x1A, "Sony Corporation Japan Identifier Company Country" },
72 { 0x1B, "Malaysia Microelectronic Solutions Sdn. Bhd Malaysia" },
73 { 0x1C, "Emosyn USA" },
74 { 0x1D, "Shanghai Fudan Microelectronics Co. Ltd. P.R. China" },
75 { 0x1E, "Magellan Technology Pty Limited Australia" },
76 { 0x1F, "Melexis NV BO Switzerland" },
77 { 0x20, "Renesas Technology Corp. Japan" },
78 { 0x21, "TAGSYS France" },
79 { 0x22, "Transcore USA" },
80 { 0x23, "Shanghai belling corp., ltd. China" },
81 { 0x24, "Masktech Germany Gmbh Germany" },
82 { 0x25, "Innovision Research and Technology Plc UK" },
83 { 0x26, "Hitachi ULSI Systems Co., Ltd. Japan" },
84 { 0x27, "Cypak AB Sweden" },
85 { 0x28, "Ricoh Japan" },
86 { 0x29, "ASK France" },
87 { 0x2A, "Unicore Microsystems, LLC Russian Federation" },
88 { 0x2B, "Dallas Semiconductor/Maxim USA" },
89 { 0x2C, "Impinj, Inc. USA" },
90 { 0x2D, "RightPlug Alliance USA" },
91 { 0x2E, "Broadcom Corporation USA" },
92 { 0x2F, "MStar Semiconductor, Inc Taiwan, ROC" },
93 { 0x30, "BeeDar Technology Inc. USA" },
94 { 0x31, "RFIDsec Denmark" },
95 { 0x32, "Schweizer Electronic AG Germany" },
96 { 0x33, "AMIC Technology Corp Taiwan" },
97 { 0x34, "Mikron JSC Russia" },
98 { 0x35, "Fraunhofer Institute for Photonic Microsystems Germany" },
99 { 0x36, "IDS Microchip AG Switzerland" },
100 { 0x37, "Kovio USA" },
101 { 0x38, "HMT Microelectronic Ltd Switzerland Identifier Company Country" },
102 { 0x39, "Silicon Craft Technology Thailand" },
103 { 0x3A, "Advanced Film Device Inc. Japan" },
104 { 0x3B, "Nitecrest Ltd UK" },
105 { 0x3C, "Verayo Inc. USA" },
106 { 0x3D, "HID Global USA" },
107 { 0x3E, "Productivity Engineering Gmbh Germany" },
108 { 0x3F, "Austriamicrosystems AG (reserved) Austria" },
109 { 0x40, "Gemalto SA France" },
110 { 0x41, "Renesas Electronics Corporation Japan" },
111 { 0x42, "3Alogics Inc Korea" },
112 { 0x43, "Top TroniQ Asia Limited Hong Kong" },
113 { 0x44, "Gentag Inc (USA) USA" },
114 { 0x00, "no tag-info available" } // must be the last entry
115 };
116
117 // get a product description based on the UID
118 // uid[8] tag uid
119 // returns description of the best match
120 char* getTagInfo(uint8_t uid) {
121
122 int i;
123 int len = sizeof(manufactureMapping) / sizeof(manufactureName);
124
125 for ( i = 0; i < len; ++i )
126 if ( uid == manufactureMapping[i].uid)
127 return manufactureMapping[i].desc;
128
129 //No match, return default
130 return manufactureMapping[len-1].desc;
131 }
132
133 int CmdHF14AList(const char *Cmd)
134 {
135 PrintAndLog("Deprecated command, use 'hf list 14a' instead");
136 return 0;
137 }
138
139 int Hf14443_4aGetCardData(iso14a_card_select_t * card) {
140 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT, 0, 0}};
141 SendCommand(&c);
142
143 UsbCommand resp;
144 WaitForResponse(CMD_ACK,&resp);
145
146 memcpy(card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));
147
148 uint64_t select_status = resp.arg[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
149
150 if(select_status == 0) {
151 PrintAndLog("E->iso14443a card select failed");
152 return 1;
153 }
154
155 if(select_status == 2) {
156 PrintAndLog("E->Card doesn't support iso14443-4 mode");
157 return 1;
158 }
159
160 if(select_status == 3) {
161 PrintAndLog("E->Card doesn't support standard iso14443-3 anticollision");
162 PrintAndLog("\tATQA : %02x %02x", card->atqa[1], card->atqa[0]);
163 return 1;
164 }
165
166 PrintAndLog(" UID: %s", sprint_hex(card->uid, card->uidlen));
167 PrintAndLog("ATQA: %02x %02x", card->atqa[1], card->atqa[0]);
168 PrintAndLog(" SAK: %02x [%" PRIu64 "]", card->sak, resp.arg[0]);
169 if(card->ats_len < 3) { // a valid ATS consists of at least the length byte (TL) and 2 CRC bytes
170 PrintAndLog("E-> Error ATS length(%d) : %s", card->ats_len, sprint_hex(card->ats, card->ats_len));
171 return 1;
172 }
173 PrintAndLog(" ATS: %s", sprint_hex(card->ats, card->ats_len));
174
175 return 0;
176 }
177
178 int CmdHF14AReader(const char *Cmd) {
179 uint32_t cm = ISO14A_CONNECT;
180 bool leaveSignalON = false;
181
182 CLIParserInit("hf 14a reader", "Executes ISO1443A anticollision-select group of commands.", NULL);
183 void* argtable[] = {
184 arg_param_begin,
185 arg_lit0("kK", "keep", "keep the field active after command executed"),
186 arg_lit0("xX", "drop", "just drop the signal field"),
187 arg_lit0("3", NULL, "ISO14443-3 select only (skip RATS)"),
188 arg_param_end
189 };
190 if (CLIParserParseString(Cmd, argtable, arg_getsize(argtable), true)){
191 CLIParserFree();
192 return 0;
193 }
194
195 leaveSignalON = arg_get_lit(1);
196 if (arg_get_lit(2)) {
197 cm = cm - ISO14A_CONNECT;
198 }
199 if (arg_get_lit(3)) {
200 cm |= ISO14A_NO_RATS;
201 }
202
203 CLIParserFree();
204
205 if (leaveSignalON)
206 cm |= ISO14A_NO_DISCONNECT;
207
208 UsbCommand c = {CMD_READER_ISO_14443a, {cm, 0, 0}};
209 SendCommand(&c);
210
211 if (ISO14A_CONNECT & cm) {
212 UsbCommand resp;
213 WaitForResponse(CMD_ACK,&resp);
214
215 iso14a_card_select_t card;
216 memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));
217
218 uint64_t select_status = resp.arg[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
219
220 if(select_status == 0) {
221 PrintAndLog("iso14443a card select failed");
222 return 1;
223 }
224
225 if(select_status == 3) {
226 PrintAndLog("Card doesn't support standard iso14443-3 anticollision");
227 PrintAndLog("ATQA : %02x %02x", card.atqa[1], card.atqa[0]);
228 return 1;
229 }
230
231 PrintAndLog(" UID : %s", sprint_hex(card.uid, card.uidlen));
232 PrintAndLog("ATQA : %02x %02x", card.atqa[1], card.atqa[0]);
233 PrintAndLog(" SAK : %02x [%" PRIu64 "]", card.sak, resp.arg[0]);
234 if(card.ats_len >= 3) { // a valid ATS consists of at least the length byte (TL) and 2 CRC bytes
235 PrintAndLog(" ATS : %s", sprint_hex(card.ats, card.ats_len));
236 }
237 if (leaveSignalON) {
238 PrintAndLog("Card is selected. You can now start sending commands");
239 }
240 }
241
242 if (!leaveSignalON) {
243 PrintAndLog("Field dropped.");
244 }
245
246 return 0;
247 }
248
249 int CmdHF14AInfo(const char *Cmd)
250 {
251 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}};
252 SendCommand(&c);
253
254 UsbCommand resp;
255 WaitForResponse(CMD_ACK,&resp);
256
257 iso14a_card_select_t card;
258 memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));
259
260 uint64_t select_status = resp.arg[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
261
262 if(select_status == 0) {
263 if (Cmd[0] != 's') PrintAndLog("iso14443a card select failed");
264 // disconnect
265 c.arg[0] = 0;
266 c.arg[1] = 0;
267 c.arg[2] = 0;
268 SendCommand(&c);
269 return 0;
270 }
271
272 if(select_status == 3) {
273 PrintAndLog("Card doesn't support standard iso14443-3 anticollision");
274 PrintAndLog("ATQA : %02x %02x", card.atqa[1], card.atqa[0]);
275 // disconnect
276 c.arg[0] = 0;
277 c.arg[1] = 0;
278 c.arg[2] = 0;
279 SendCommand(&c);
280 return 0;
281 }
282
283 PrintAndLog(" UID : %s", sprint_hex(card.uid, card.uidlen));
284 PrintAndLog("ATQA : %02x %02x", card.atqa[1], card.atqa[0]);
285 PrintAndLog(" SAK : %02x [%" PRIu64 "]", card.sak, resp.arg[0]);
286
287 bool isMifareClassic = true;
288 switch (card.sak) {
289 case 0x00:
290 isMifareClassic = false;
291
292 //***************************************test****************
293 // disconnect
294 c.arg[0] = 0;
295 c.arg[1] = 0;
296 c.arg[2] = 0;
297 SendCommand(&c);
298
299 uint32_t tagT = GetHF14AMfU_Type();
300 ul_print_type(tagT, 0);
301
302 //reconnect for further tests
303 c.arg[0] = ISO14A_CONNECT | ISO14A_NO_DISCONNECT;
304 c.arg[1] = 0;
305 c.arg[2] = 0;
306
307 SendCommand(&c);
308
309 UsbCommand resp;
310 WaitForResponse(CMD_ACK,&resp);
311
312 memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));
313
314 select_status = resp.arg[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS
315
316 if(select_status == 0) {
317 //PrintAndLog("iso14443a card select failed");
318 // disconnect
319 c.arg[0] = 0;
320 c.arg[1] = 0;
321 c.arg[2] = 0;
322 SendCommand(&c);
323 return 0;
324 }
325
326 /* orig
327 // check if the tag answers to GETVERSION (0x60)
328 c.arg[0] = ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT;
329 c.arg[1] = 1;
330 c.arg[2] = 0;
331 c.d.asBytes[0] = 0x60;
332 SendCommand(&c);
333 WaitForResponse(CMD_ACK,&resp);
334
335 uint8_t version[10] = {0};
336 memcpy(version, resp.d.asBytes, resp.arg[0] < sizeof(version) ? resp.arg[0] : sizeof(version));
337 uint8_t len = resp.arg[0] & 0xff;
338 switch ( len ){
339 // todo, identify "Magic UL-C tags". // they usually have a static nonce response to 0x1A command.
340 // UL-EV1, size, check version[6] == 0x0b (smaller) 0x0b * 4 == 48
341 case 0x0A:PrintAndLog("TYPE : NXP MIFARE Ultralight EV1 %d bytes", (version[6] == 0xB) ? 48 : 128);break;
342 case 0x01:PrintAndLog("TYPE : NXP MIFARE Ultralight C");break;
343 case 0x00:PrintAndLog("TYPE : NXP MIFARE Ultralight");break;
344 }
345 */
346 break;
347 case 0x01: PrintAndLog("TYPE : NXP TNP3xxx Activision Game Appliance"); break;
348 case 0x04: PrintAndLog("TYPE : NXP MIFARE (various !DESFire !DESFire EV1)"); break;
349 case 0x08: PrintAndLog("TYPE : NXP MIFARE CLASSIC 1k | Plus 2k SL1"); break;
350 case 0x09: PrintAndLog("TYPE : NXP MIFARE Mini 0.3k"); break;
351 case 0x10: PrintAndLog("TYPE : NXP MIFARE Plus 2k SL2"); break;
352 case 0x11: PrintAndLog("TYPE : NXP MIFARE Plus 4k SL2"); break;
353 case 0x18: PrintAndLog("TYPE : NXP MIFARE Classic 4k | Plus 4k SL1"); break;
354 case 0x20: PrintAndLog("TYPE : NXP MIFARE DESFire 4k | DESFire EV1 2k/4k/8k | Plus 2k/4k SL3 | JCOP 31/41"); break;
355 case 0x24: PrintAndLog("TYPE : NXP MIFARE DESFire | DESFire EV1"); break;
356 case 0x28: PrintAndLog("TYPE : JCOP31 or JCOP41 v2.3.1"); break;
357 case 0x38: PrintAndLog("TYPE : Nokia 6212 or 6131 MIFARE CLASSIC 4K"); break;
358 case 0x88: PrintAndLog("TYPE : Infineon MIFARE CLASSIC 1K"); break;
359 case 0x98: PrintAndLog("TYPE : Gemplus MPCOS"); break;
360 default: ;
361 }
362
363 // Double & triple sized UID, can be mapped to a manufacturer.
364 // HACK: does this apply for Ultralight cards?
365 if ( card.uidlen > 4 ) {
366 PrintAndLog("MANUFACTURER : %s", getTagInfo(card.uid[0]));
367 }
368
369 // try to request ATS even if tag claims not to support it
370 if (select_status == 2) {
371 uint8_t rats[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0
372 c.arg[0] = ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT;
373 c.arg[1] = 2;
374 c.arg[2] = 0;
375 memcpy(c.d.asBytes, rats, 2);
376 SendCommand(&c);
377 WaitForResponse(CMD_ACK,&resp);
378
379 memcpy(card.ats, resp.d.asBytes, resp.arg[0]);
380 card.ats_len = resp.arg[0]; // note: ats_len includes CRC Bytes
381 }
382
383 if(card.ats_len >= 3) { // a valid ATS consists of at least the length byte (TL) and 2 CRC bytes
384 bool ta1 = 0, tb1 = 0, tc1 = 0;
385 int pos;
386
387 if (select_status == 2) {
388 PrintAndLog("SAK incorrectly claims that card doesn't support RATS");
389 }
390 PrintAndLog(" ATS : %s", sprint_hex(card.ats, card.ats_len));
391 PrintAndLog(" - TL : length is %d bytes", card.ats[0]);
392 if (card.ats[0] != card.ats_len - 2) {
393 PrintAndLog("ATS may be corrupted. Length of ATS (%d bytes incl. 2 Bytes CRC) doesn't match TL", card.ats_len);
394 }
395
396 if (card.ats[0] > 1) { // there is a format byte (T0)
397 ta1 = (card.ats[1] & 0x10) == 0x10;
398 tb1 = (card.ats[1] & 0x20) == 0x20;
399 tc1 = (card.ats[1] & 0x40) == 0x40;
400 int16_t fsci = card.ats[1] & 0x0f;
401 PrintAndLog(" - T0 : TA1 is%s present, TB1 is%s present, "
402 "TC1 is%s present, FSCI is %d (FSC = %ld)",
403 (ta1 ? "" : " NOT"), (tb1 ? "" : " NOT"), (tc1 ? "" : " NOT"),
404 fsci,
405 fsci < 5 ? (fsci - 2) * 8 :
406 fsci < 8 ? (fsci - 3) * 32 :
407 fsci == 8 ? 256 :
408 -1
409 );
410 }
411 pos = 2;
412 if (ta1) {
413 char dr[16], ds[16];
414 dr[0] = ds[0] = '\0';
415 if (card.ats[pos] & 0x10) strcat(ds, "2, ");
416 if (card.ats[pos] & 0x20) strcat(ds, "4, ");
417 if (card.ats[pos] & 0x40) strcat(ds, "8, ");
418 if (card.ats[pos] & 0x01) strcat(dr, "2, ");
419 if (card.ats[pos] & 0x02) strcat(dr, "4, ");
420 if (card.ats[pos] & 0x04) strcat(dr, "8, ");
421 if (strlen(ds) != 0) ds[strlen(ds) - 2] = '\0';
422 if (strlen(dr) != 0) dr[strlen(dr) - 2] = '\0';
423 PrintAndLog(" - TA1 : different divisors are%s supported, "
424 "DR: [%s], DS: [%s]",
425 (card.ats[pos] & 0x80 ? " NOT" : ""), dr, ds);
426 pos++;
427 }
428 if (tb1) {
429 uint32_t sfgi = card.ats[pos] & 0x0F;
430 uint32_t fwi = card.ats[pos] >> 4;
431 PrintAndLog(" - TB1 : SFGI = %d (SFGT = %s%ld/fc), FWI = %d (FWT = %ld/fc)",
432 (sfgi),
433 sfgi ? "" : "(not needed) ",
434 sfgi ? (1 << 12) << sfgi : 0,
435 fwi,
436 (1 << 12) << fwi
437 );
438 pos++;
439 }
440 if (tc1) {
441 PrintAndLog(" - TC1 : NAD is%s supported, CID is%s supported",
442 (card.ats[pos] & 0x01) ? "" : " NOT",
443 (card.ats[pos] & 0x02) ? "" : " NOT");
444 pos++;
445 }
446 if (card.ats[0] > pos) {
447 char *tip = "";
448 if (card.ats[0] - pos >= 7) {
449 if (memcmp(card.ats + pos, "\xC1\x05\x2F\x2F\x01\xBC\xD6", 7) == 0) {
450 tip = "-> MIFARE Plus X 2K or 4K";
451 } else if (memcmp(card.ats + pos, "\xC1\x05\x2F\x2F\x00\x35\xC7", 7) == 0) {
452 tip = "-> MIFARE Plus S 2K or 4K";
453 }
454 }
455 PrintAndLog(" - HB : %s%s", sprint_hex(card.ats + pos, card.ats[0] - pos), tip);
456 if (card.ats[pos] == 0xC1) {
457 PrintAndLog(" c1 -> Mifare or (multiple) virtual cards of various type");
458 PrintAndLog(" %02x -> Length is %d bytes",
459 card.ats[pos + 1], card.ats[pos + 1]);
460 switch (card.ats[pos + 2] & 0xf0) {
461 case 0x10:
462 PrintAndLog(" 1x -> MIFARE DESFire");
463 break;
464 case 0x20:
465 PrintAndLog(" 2x -> MIFARE Plus");
466 break;
467 }
468 switch (card.ats[pos + 2] & 0x0f) {
469 case 0x00:
470 PrintAndLog(" x0 -> <1 kByte");
471 break;
472 case 0x01:
473 PrintAndLog(" x1 -> 1 kByte");
474 break;
475 case 0x02:
476 PrintAndLog(" x2 -> 2 kByte");
477 break;
478 case 0x03:
479 PrintAndLog(" x3 -> 4 kByte");
480 break;
481 case 0x04:
482 PrintAndLog(" x4 -> 8 kByte");
483 break;
484 }
485 switch (card.ats[pos + 3] & 0xf0) {
486 case 0x00:
487 PrintAndLog(" 0x -> Engineering sample");
488 break;
489 case 0x20:
490 PrintAndLog(" 2x -> Released");
491 break;
492 }
493 switch (card.ats[pos + 3] & 0x0f) {
494 case 0x00:
495 PrintAndLog(" x0 -> Generation 1");
496 break;
497 case 0x01:
498 PrintAndLog(" x1 -> Generation 2");
499 break;
500 case 0x02:
501 PrintAndLog(" x2 -> Generation 3");
502 break;
503 }
504 switch (card.ats[pos + 4] & 0x0f) {
505 case 0x00:
506 PrintAndLog(" x0 -> Only VCSL supported");
507 break;
508 case 0x01:
509 PrintAndLog(" x1 -> VCS, VCSL, and SVC supported");
510 break;
511 case 0x0E:
512 PrintAndLog(" xE -> no VCS command supported");
513 break;
514 }
515 }
516 }
517 } else {
518 PrintAndLog("proprietary non iso14443-4 card found, RATS not supported");
519 }
520
521
522 // try to see if card responses to "chinese magic backdoor" commands.
523 (void)mfCIdentify();
524
525 if (isMifareClassic) {
526 switch(DetectClassicPrng()) {
527 case 0:
528 PrintAndLog("Prng detection: HARDENED (hardnested)");
529 break;
530 case 1:
531 PrintAndLog("Prng detection: WEAK");
532 break;
533 default:
534 PrintAndLog("Prng detection error.");
535 }
536 }
537
538 return select_status;
539 }
540
541 // Collect ISO14443 Type A UIDs
542 int CmdHF14ACUIDs(const char *Cmd)
543 {
544 // requested number of UIDs
545 int n = atoi(Cmd);
546 // collect at least 1 (e.g. if no parameter was given)
547 n = n > 0 ? n : 1;
548
549 PrintAndLog("Collecting %d UIDs", n);
550 PrintAndLog("Start: %" PRIu64, msclock()/1000);
551 // repeat n times
552 for (int i = 0; i < n; i++) {
553 // execute anticollision procedure
554 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_RATS, 0, 0}};
555 SendCommand(&c);
556
557 UsbCommand resp;
558 WaitForResponse(CMD_ACK,&resp);
559
560 iso14a_card_select_t *card = (iso14a_card_select_t *) resp.d.asBytes;
561
562 // check if command failed
563 if (resp.arg[0] == 0) {
564 PrintAndLog("Card select failed.");
565 } else {
566 char uid_string[20];
567 for (uint16_t i = 0; i < card->uidlen; i++) {
568 sprintf(&uid_string[2*i], "%02X", card->uid[i]);
569 }
570 PrintAndLog("%s", uid_string);
571 }
572 }
573 PrintAndLog("End: %" PRIu64, msclock()/1000);
574
575 return 1;
576 }
577
578 // ## simulate iso14443a tag
579 // ## greg - added ability to specify tag UID
580 int CmdHF14ASim(const char *Cmd)
581 {
582 UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,{0,0,0}};
583
584 // Retrieve the tag type
585 uint8_t tagtype = param_get8ex(Cmd,0,0,10);
586
587 // When no argument was given, just print help message
588 if (tagtype == 0) {
589 PrintAndLog("");
590 PrintAndLog(" Emulating ISO/IEC 14443 type A tag with 4 or 7 byte UID");
591 PrintAndLog("");
592 PrintAndLog(" syntax: hf 14a sim <type> <uid>");
593 PrintAndLog(" types: 1 = MIFARE Classic");
594 PrintAndLog(" 2 = MIFARE Ultralight");
595 PrintAndLog(" 3 = MIFARE Desfire");
596 PrintAndLog(" 4 = ISO/IEC 14443-4");
597 PrintAndLog(" 5 = MIFARE Tnp3xxx");
598 PrintAndLog("");
599 return 1;
600 }
601
602 // Store the tag type
603 c.arg[0] = tagtype;
604
605 // Retrieve the full 4 or 7 byte long uid
606 uint64_t long_uid = param_get64ex(Cmd,1,0,16);
607
608 // Are we handling the (optional) second part uid?
609 if (long_uid > 0xffffffff) {
610 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 7 byte UID (%014" PRIx64 ")",long_uid);
611 // Store the second part
612 c.arg[2] = (long_uid & 0xffffffff);
613 long_uid >>= 32;
614 // Store the first part, ignore the first byte, it is replaced by cascade byte (0x88)
615 c.arg[1] = (long_uid & 0xffffff);
616 } else {
617 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 4 byte UID (%08x)",long_uid);
618 // Only store the first part
619 c.arg[1] = long_uid & 0xffffffff;
620 }
621 /*
622 // At lease save the mandatory first part of the UID
623 c.arg[0] = long_uid & 0xffffffff;
624
625 if (c.arg[1] == 0) {
626 PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
627 }
628
629 switch (c.arg[0]) {
630 case 1: {
631 PrintAndLog("Emulating ISO/IEC 14443-3 type A tag with 4 byte UID");
632 UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,param_get32ex(Cmd,0,0,10),param_get32ex(Cmd,1,0,16),param_get32ex(Cmd,2,0,16)};
633 } break;
634 case 2: {
635 PrintAndLog("Emulating ISO/IEC 14443-4 type A tag with 7 byte UID");
636 } break;
637 default: {
638 PrintAndLog("Error: unkown tag type (%d)",c.arg[0]);
639 PrintAndLog("syntax: hf 14a sim <uid>",c.arg[0]);
640 PrintAndLog(" type1: 4 ",c.arg[0]);
641
642 return 1;
643 } break;
644 }
645 */
646 /*
647 unsigned int hi = 0, lo = 0;
648 int n = 0, i = 0;
649 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
650 hi= (hi << 4) | (lo >> 28);
651 lo= (lo << 4) | (n & 0xf);
652 }
653 */
654 // UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,param_get32ex(Cmd,0,0,10),param_get32ex(Cmd,1,0,16),param_get32ex(Cmd,2,0,16)};
655 // PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
656 SendCommand(&c);
657 return 0;
658 }
659
660 int CmdHF14ASnoop(const char *Cmd) {
661 int param = 0;
662
663 uint8_t ctmp = param_getchar(Cmd, 0) ;
664 if (ctmp == 'h' || ctmp == 'H') {
665 PrintAndLog("It get data from the field and saves it into command buffer.");
666 PrintAndLog("Buffer accessible from command hf list 14a.");
667 PrintAndLog("Usage: hf 14a snoop [c][r]");
668 PrintAndLog("c - triggered by first data from card");
669 PrintAndLog("r - triggered by first 7-bit request from reader (REQ,WUP,...)");
670 PrintAndLog("sample: hf 14a snoop c r");
671 return 0;
672 }
673
674 for (int i = 0; i < 2; i++) {
675 ctmp = param_getchar(Cmd, i);
676 if (ctmp == 'c' || ctmp == 'C') param |= 0x01;
677 if (ctmp == 'r' || ctmp == 'R') param |= 0x02;
678 }
679
680 UsbCommand c = {CMD_SNOOP_ISO_14443a, {param, 0, 0}};
681 SendCommand(&c);
682 return 0;
683 }
684
685 void DropField() {
686 UsbCommand c = {CMD_READER_ISO_14443a, {0, 0, 0}};
687 SendCommand(&c);
688 }
689
690 int ExchangeRAW14a(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen) {
691 static bool responseNum = false;
692 uint16_t cmdc = 0;
693 *dataoutlen = 0;
694
695 if (activateField) {
696 responseNum = false;
697 UsbCommand resp;
698
699 // Anticollision + SELECT card
700 UsbCommand ca = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT | ISO14A_CLEAR_TRACE, 0, 0}};
701 SendCommand(&ca);
702 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
703 PrintAndLog("14aRAW ERROR: Proxmark connection timeout.");
704 return 1;
705 }
706
707 // check result
708 if (resp.arg[0] == 0) {
709 PrintAndLog("14aRAW ERROR: No card in field.");
710 return 1;
711 }
712
713 if (resp.arg[0] != 1 && resp.arg[0] != 2) {
714 PrintAndLog("14aRAW ERROR: card not in iso14443-4. res=%d.", resp.arg[0]);
715 return 1;
716 }
717
718 if (resp.arg[0] == 2) { // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
719 // get ATS
720 UsbCommand cr = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT, 2, 0}};
721 uint8_t rats[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0
722 memcpy(cr.d.asBytes, rats, 2);
723 SendCommand(&cr);
724 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
725 PrintAndLog("14aRAW ERROR: Proxmark connection timeout.");
726 return 1;
727 }
728
729 if (resp.arg[0] <= 0) { // ats_len
730 PrintAndLog("14aRAW ERROR: Can't get ATS.");
731 return 1;
732 }
733 }
734 }
735
736 if (leaveSignalON)
737 cmdc |= ISO14A_NO_DISCONNECT;
738
739 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_APPEND_CRC | cmdc, (datainlen & 0xFFFF) + 2, 0}};
740 uint8_t header[] = {0x0a | responseNum, 0x00};
741 responseNum ^= 1;
742 memcpy(c.d.asBytes, header, 2);
743 memcpy(&c.d.asBytes[2], datain, datainlen);
744 SendCommand(&c);
745
746 uint8_t *recv;
747 UsbCommand resp;
748
749 if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
750 recv = resp.d.asBytes;
751 int iLen = resp.arg[0];
752
753 if(!iLen) {
754 PrintAndLog("14aRAW ERROR: No card response.");
755 return 1;
756 }
757
758 *dataoutlen = iLen - 2;
759 if (*dataoutlen < 0)
760 *dataoutlen = 0;
761
762 if (maxdataoutlen && *dataoutlen > maxdataoutlen) {
763 PrintAndLog("14aRAW ERROR: Buffer too small(%d). Needs %d bytes", *dataoutlen, maxdataoutlen);
764 return 2;
765 }
766
767 if (recv[0] != header[0]) {
768 PrintAndLog("14aRAW ERROR: iso14443-4 framing error. Card send %2x must be %2x", dataout[0], header[0]);
769 return 2;
770 }
771
772 memcpy(dataout, &recv[2], *dataoutlen);
773
774 // CRC Check
775 if (iLen == -1) {
776 PrintAndLog("14aRAW ERROR: ISO 14443A CRC error.");
777 return 3;
778 }
779
780
781 } else {
782 PrintAndLog("14aRAW ERROR: Reply timeout.");
783 return 4;
784 }
785
786 return 0;
787 }
788
789 int CmdExchangeAPDU(uint8_t *datain, int datainlen, bool activateField, uint8_t *dataout, int maxdataoutlen, int *dataoutlen, bool *chaining) {
790 uint16_t cmdc = 0;
791
792 *chaining = false;
793
794 if (activateField) {
795 cmdc |= ISO14A_CONNECT | ISO14A_CLEAR_TRACE;
796 }
797
798 // "Command APDU" length should be 5+255+1, but javacard's APDU buffer might be smaller - 133 bytes
799 // https://stackoverflow.com/questions/32994936/safe-max-java-card-apdu-data-command-and-respond-size
800 // here length USB_CMD_DATA_SIZE=512
801 // timeout must be authomatically set by "get ATS"
802 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_APDU | ISO14A_NO_DISCONNECT | cmdc, (datainlen & 0xFFFF), 0}};
803 memcpy(c.d.asBytes, datain, datainlen);
804 SendCommand(&c);
805
806 uint8_t *recv;
807 UsbCommand resp;
808
809 if (activateField) {
810 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
811 PrintAndLog("APDU ERROR: Proxmark connection timeout.");
812 return 1;
813 }
814 if (resp.arg[0] != 1) {
815 PrintAndLog("APDU ERROR: Proxmark error %d.", resp.arg[0]);
816 DropField();
817 return 1;
818 }
819 }
820
821 if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
822 recv = resp.d.asBytes;
823 int iLen = resp.arg[0];
824 uint8_t res = resp.arg[1];
825
826 int dlen = iLen - 2;
827 if (dlen < 0)
828 dlen = 0;
829 *dataoutlen += dlen;
830
831 if (maxdataoutlen && *dataoutlen > maxdataoutlen) {
832 PrintAndLog("APDU ERROR: Buffer too small(%d). Needs %d bytes", *dataoutlen, maxdataoutlen);
833 return 2;
834 }
835
836 if(!iLen) {
837 PrintAndLog("APDU ERROR: No APDU response.");
838 return 1;
839 }
840
841 // check apdu length
842 if (iLen < 4 && iLen >= 0) {
843 PrintAndLog("APDU ERROR: Small APDU response. Len=%d", iLen);
844 return 2;
845 }
846
847 // check block TODO
848 if (iLen == -2) {
849 PrintAndLog("APDU ERROR: Block type mismatch.");
850 return 2;
851 }
852
853 memcpy(dataout, recv, dlen);
854
855 // chaining
856 if ((res & 0x10) != 0) {
857 *chaining = true;
858 }
859
860 // CRC Check
861 if (iLen == -1) {
862 PrintAndLog("APDU ERROR: ISO 14443A CRC error.");
863 return 3;
864 }
865 } else {
866 PrintAndLog("APDU ERROR: Reply timeout.");
867 return 4;
868 }
869
870 return 0;
871 }
872
873
874 int ExchangeAPDU14a(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen) {
875 *dataoutlen = 0;
876 bool chaining = false;
877
878 int res = CmdExchangeAPDU(datain, datainlen, activateField, dataout, maxdataoutlen, dataoutlen, &chaining);
879
880 while (chaining) {
881 // I-block with chaining
882 res = CmdExchangeAPDU(NULL, 0, false, &dataout[*dataoutlen], maxdataoutlen, dataoutlen, &chaining);
883
884 if (res) {
885 if (!leaveSignalON)
886 DropField();
887
888 return 100;
889 }
890 }
891
892 if (!leaveSignalON)
893 DropField();
894
895 return 0;
896 }
897
898 // ISO14443-4. 7. Half-duplex block transmission protocol
899 int CmdHF14AAPDU(const char *cmd) {
900 uint8_t data[USB_CMD_DATA_SIZE];
901 int datalen = 0;
902 bool activateField = false;
903 bool leaveSignalON = false;
904 bool decodeTLV = false;
905
906 CLIParserInit("hf 14a apdu",
907 "Sends an ISO 7816-4 APDU via ISO 14443-4 block transmission protocol (T=CL)",
908 "Sample:\n\thf 14a apdu -st 00A404000E325041592E5359532E444446303100\n");
909
910 void* argtable[] = {
911 arg_param_begin,
912 arg_lit0("sS", "select", "activate field and select card"),
913 arg_lit0("kK", "keep", "leave the signal field ON after receive response"),
914 arg_lit0("tT", "tlv", "executes TLV decoder if it possible"),
915 arg_strx1(NULL, NULL, "<APDU (hex)>", NULL),
916 arg_param_end
917 };
918 CLIExecWithReturn(cmd, argtable, false);
919
920 activateField = arg_get_lit(1);
921 leaveSignalON = arg_get_lit(2);
922 decodeTLV = arg_get_lit(3);
923 // len = data + PCB(1b) + CRC(2b)
924 CLIGetHexBLessWithReturn(4, data, &datalen, 1 + 2);
925
926
927 CLIParserFree();
928 // PrintAndLog("---str [%d] %s", arg_get_str(4)->count, arg_get_str(4)->sval[0]);
929 PrintAndLog(">>>>[%s%s%s] %s", activateField ? "sel ": "", leaveSignalON ? "keep ": "", decodeTLV ? "TLV": "", sprint_hex(data, datalen));
930
931 int res = ExchangeAPDU14a(data, datalen, activateField, leaveSignalON, data, USB_CMD_DATA_SIZE, &datalen);
932
933 if (res)
934 return res;
935
936 PrintAndLog("<<<< %s", sprint_hex(data, datalen));
937
938 PrintAndLog("APDU response: %02x %02x - %s", data[datalen - 2], data[datalen - 1], GetAPDUCodeDescription(data[datalen - 2], data[datalen - 1]));
939
940 // TLV decoder
941 if (decodeTLV && datalen > 4) {
942 TLVPrintFromBuffer(data, datalen - 2);
943 }
944
945 return 0;
946 }
947
948 int CmdHF14ACmdRaw(const char *cmd) {
949 UsbCommand c = {CMD_READER_ISO_14443a, {0, 0, 0}};
950 bool reply=1;
951 bool crc = false;
952 bool power = false;
953 bool active = false;
954 bool active_select = false;
955 bool no_rats = false;
956 uint16_t numbits = 0;
957 bool bTimeout = false;
958 uint32_t timeout = 0;
959 bool topazmode = false;
960 uint8_t data[USB_CMD_DATA_SIZE];
961 int datalen = 0;
962
963 // extract parameters
964 CLIParserInit("hf 14a raw", "Send raw hex data to tag",
965 "Sample:\n"\
966 "\thf 14a raw -pa -b7 -t1000 52 -- execute WUPA\n"\
967 "\thf 14a raw -p 9320 -- anticollision\n"\
968 "\thf 14a raw -psc 60 00 -- select and mifare AUTH\n");
969 void* argtable[] = {
970 arg_param_begin,
971 arg_lit0("rR", "nreply", "do not read response"),
972 arg_lit0("cC", "crc", "calculate and append CRC"),
973 arg_lit0("pP", "power", "leave the signal field ON after receive"),
974 arg_lit0("aA", "active", "active signal field ON without select"),
975 arg_lit0("sS", "actives", "active signal field ON with select"),
976 arg_int0("bB", "bits", NULL, "number of bits to send. Useful for send partial byte"),
977 arg_int0("t", "timeout", NULL, "timeout in ms"),
978 arg_lit0("T", "topaz", "use Topaz protocol to send command"),
979 arg_lit0("3", NULL, "ISO14443-3 select only (skip RATS)"),
980 arg_strx1(NULL, NULL, "<data (hex)>", NULL),
981 arg_param_end
982 };
983 // defaults
984 arg_get_int(6) = 0;
985 arg_get_int(7) = 0;
986
987 if (CLIParserParseString(cmd, argtable, arg_getsize(argtable), false)){
988 CLIParserFree();
989 return 0;
990 }
991
992 reply = !arg_get_lit(1);
993 crc = arg_get_lit(2);
994 power = arg_get_lit(3);
995 active = arg_get_lit(4);
996 active_select = arg_get_lit(5);
997 numbits = arg_get_int(6) & 0xFFFF;
998 timeout = arg_get_int(7);
999 bTimeout = (timeout > 0);
1000 topazmode = arg_get_lit(8);
1001 no_rats = arg_get_lit(9);
1002 // len = data + CRC(2b)
1003 if (CLIParamHexToBuf(arg_get_str(10), data, sizeof(data) -2, &datalen)) {
1004 CLIParserFree();
1005 return 1;
1006 }
1007
1008 CLIParserFree();
1009
1010 // logic
1011 if(crc && datalen>0 && datalen<sizeof(data)-2)
1012 {
1013 uint8_t first, second;
1014 if (topazmode) {
1015 ComputeCrc14443(CRC_14443_B, data, datalen, &first, &second);
1016 } else {
1017 ComputeCrc14443(CRC_14443_A, data, datalen, &first, &second);
1018 }
1019 data[datalen++] = first;
1020 data[datalen++] = second;
1021 }
1022
1023 if(active || active_select)
1024 {
1025 c.arg[0] |= ISO14A_CONNECT | ISO14A_CLEAR_TRACE;
1026 if(active)
1027 c.arg[0] |= ISO14A_NO_SELECT;
1028 }
1029
1030 if(bTimeout){
1031 #define MAX_TIMEOUT 40542464 // = (2^32-1) * (8*16) / 13560000Hz * 1000ms/s
1032 c.arg[0] |= ISO14A_SET_TIMEOUT;
1033 if(timeout > MAX_TIMEOUT) {
1034 timeout = MAX_TIMEOUT;
1035 PrintAndLog("Set timeout to 40542 seconds (11.26 hours). The max we can wait for response");
1036 }
1037 c.arg[2] = 13560000 / 1000 / (8*16) * timeout; // timeout in ETUs (time to transfer 1 bit, approx. 9.4 us)
1038 }
1039
1040 if(power) {
1041 c.arg[0] |= ISO14A_NO_DISCONNECT;
1042 }
1043
1044 if(datalen > 0) {
1045 c.arg[0] |= ISO14A_RAW;
1046 }
1047
1048 if(topazmode) {
1049 c.arg[0] |= ISO14A_TOPAZMODE;
1050 }
1051
1052 if(no_rats) {
1053 c.arg[0] |= ISO14A_NO_RATS;
1054 }
1055
1056 // Max buffer is USB_CMD_DATA_SIZE (512)
1057 c.arg[1] = (datalen & 0xFFFF) | ((uint32_t)numbits << 16);
1058 memcpy(c.d.asBytes,data,datalen);
1059
1060 SendCommand(&c);
1061
1062 if (reply) {
1063 int res = 0;
1064 if (active_select)
1065 res = waitCmd(1);
1066 if (!res && datalen > 0)
1067 waitCmd(0);
1068 } // if reply
1069 return 0;
1070 }
1071
1072
1073 static int waitCmd(uint8_t iSelect) {
1074 uint8_t *recv;
1075 UsbCommand resp;
1076 char *hexout;
1077
1078 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1079 recv = resp.d.asBytes;
1080 uint8_t iLen = resp.arg[0];
1081 if (iSelect){
1082 iLen = resp.arg[1];
1083 if (iLen){
1084 PrintAndLog("Card selected. UID[%i]:", iLen);
1085 } else {
1086 PrintAndLog("Can't select card.");
1087 }
1088 } else {
1089 PrintAndLog("received %i bytes:", iLen);
1090 }
1091 if(!iLen)
1092 return 1;
1093 hexout = (char *)malloc(iLen * 3 + 1);
1094 if (hexout != NULL) {
1095 for (int i = 0; i < iLen; i++) { // data in hex
1096 sprintf(&hexout[i * 3], "%02X ", recv[i]);
1097 }
1098 PrintAndLog("%s", hexout);
1099 free(hexout);
1100 } else {
1101 PrintAndLog("malloc failed your client has low memory?");
1102 return 2;
1103 }
1104 } else {
1105 PrintAndLog("timeout while waiting for reply.");
1106 return 3;
1107 }
1108 return 0;
1109 }
1110
1111 static command_t CommandTable[] =
1112 {
1113 {"help", CmdHelp, 1, "This help"},
1114 {"list", CmdHF14AList, 0, "[Deprecated] List ISO 14443a history"},
1115 {"reader", CmdHF14AReader, 0, "Start acting like an ISO14443 Type A reader"},
1116 {"info", CmdHF14AInfo, 0, "Reads card and shows information about it"},
1117 {"cuids", CmdHF14ACUIDs, 0, "<n> Collect n>0 ISO14443 Type A UIDs in one go"},
1118 {"sim", CmdHF14ASim, 0, "<UID> -- Simulate ISO 14443a tag"},
1119 {"snoop", CmdHF14ASnoop, 0, "Eavesdrop ISO 14443 Type A"},
1120 {"apdu", CmdHF14AAPDU, 0, "Send an ISO 7816-4 APDU via ISO 14443-4 block transmission protocol"},
1121 {"raw", CmdHF14ACmdRaw, 0, "Send raw hex data to tag"},
1122 {NULL, NULL, 0, NULL}
1123 };
1124
1125 int CmdHF14A(const char *Cmd) {
1126 (void)WaitForResponseTimeout(CMD_ACK,NULL,100);
1127 CmdsParse(CommandTable, Cmd);
1128 return 0;
1129 }
1130
1131 int CmdHelp(const char *Cmd)
1132 {
1133 CmdsHelp(CommandTable);
1134 return 0;
1135 }
Impressum, Datenschutz