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