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