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