1 //-----------------------------------------------------------------------------
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>, Hagen Fritsch
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
8 //-----------------------------------------------------------------------------
9 // High frequency ISO14443A commands
10 //-----------------------------------------------------------------------------
17 #include "iso14443crc.h"
21 #include "cmdparser.h"
27 static int CmdHelp(const char *Cmd
);
29 int CmdHF14AList(const char *Cmd
)
32 GetFromBigBuf(got
,sizeof(got
),0);
34 PrintAndLog("recorded activity:");
35 PrintAndLog(" ETU :rssi: who bytes");
36 PrintAndLog("---------+----+----+-----------");
47 int timestamp
= *((uint32_t *)(got
+i
));
48 if (timestamp
& 0x80000000) {
49 timestamp
&= 0x7fffffff;
56 int parityBits
= *((uint32_t *)(got
+i
+4));
57 // 4 bytes of additional information...
58 // maximum of 32 additional parity bit information
61 // at each quarter bit period we can send power level (16 levels)
62 // or each half bit period in 256 levels.
70 if (i
+ len
>= 1900) {
74 uint8_t *frame
= (got
+i
+9);
76 // Break and stick with current result if buffer was not completely full
77 if (frame
[0] == 0x44 && frame
[1] == 0x44 && frame
[3] == 0x44) { break; }
81 for (j
= 0; j
< len
; j
++) {
86 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
89 //if((parityBits >> (len - j - 1)) & 0x01) {
90 if (isResponse
&& (oddparity
!= ((parityBits
>> (len
- j
- 1)) & 0x01))) {
91 sprintf(line
+(j
*4), "%02x! ", frame
[j
]);
94 sprintf(line
+(j
*4), "%02x ", frame
[j
]);
102 for (j
= 0; j
< (len
- 1); j
++) {
103 // gives problems... search for the reason..
104 /*if(frame[j] == 0xAA) {
107 crc = "[1] Two drops close after each other";
110 crc = "[2] Potential SOC with a drop in second half of bitperiod";
113 crc = "[3] Segment Z after segment X is not possible";
116 crc = "[4] Parity bit of a fully received byte was wrong";
119 crc = "[?] Unknown error";
126 if (strlen(crc
)==0) {
127 ComputeCrc14443(CRC_14443_A
, frame
, len
-2, &b1
, &b2
);
128 if (b1
!= frame
[len
-2] || b2
!= frame
[len
-1]) {
129 crc
= (isResponse
& (len
< 6)) ? "" : " !crc";
138 char metricString
[100];
140 sprintf(metricString
, "%3d", metric
);
142 strcpy(metricString
, " ");
145 PrintAndLog(" +%7d: %s: %s %s %s",
146 (prev
< 0 ? 0 : (timestamp
- prev
)),
148 (isResponse
? "TAG" : " "), line
, crc
);
156 void iso14a_set_timeout(uint32_t timeout
) {
157 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_SET_TIMEOUT
, 0, timeout
}};
161 int CmdHF14AReader(const char *Cmd
)
163 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
, 0, 0}};
166 if( 3 == param_getstr(Cmd
,0,param
) && !strcmp("con",param
))
168 c
.arg
[0]|=ISO14A_NO_DISCONNECT
;
169 PrintAndLog("KEEP connected!\n");
172 if( 3 == param_getstr(Cmd
,0,param
) && !strcmp("dis",param
))
175 PrintAndLog("disconnected!\n");
181 UsbCommand
* resp
= WaitForResponse(CMD_ACK
);
182 uint8_t * uid
= resp
->d
.asBytes
;
183 iso14a_card_select_t
* card
= (iso14a_card_select_t
*)(uid
+ 12);
185 if(resp
->arg
[0] == 0) {
186 PrintAndLog("iso14443a card select failed");
190 PrintAndLog("ATQA : %02x %02x", card
->atqa
[0], card
->atqa
[1]);
191 PrintAndLog(" UID : %s", sprint_hex(uid
, 12));
192 PrintAndLog(" SAK : %02x [%d]", card
->sak
, resp
->arg
[0]);
195 case 0x00: PrintAndLog(" SAK : NXP MIFARE Ultralight | Ultralight C"); break;
196 case 0x04: PrintAndLog(" SAK : NXP MIFARE (various !DESFire !DESFire EV1)"); break;
198 case 0x08: PrintAndLog(" SAK : NXP MIFARE CLASSIC 1k | Plus 2k"); break;
199 case 0x09: PrintAndLog(" SAK : NXP MIFARE Mini 0.3k"); break;
200 case 0x10: PrintAndLog(" SAK : NXP MIFARE Plus 2k"); break;
201 case 0x11: PrintAndLog(" SAK : NXP MIFARE Plus 4k"); break;
202 case 0x18: PrintAndLog(" SAK : NXP MIFARE Classic 4k | Plus 4k"); break;
203 case 0x20: PrintAndLog(" SAK : NXP MIFARE DESFire 4k | DESFire EV1 2k/4k/8k | Plus 2k/4k | JCOP 31/41"); break;
204 case 0x24: PrintAndLog(" SAK : NXP MIFARE DESFire | DESFire EV1"); break;
205 case 0x28: PrintAndLog(" SAK : JCOP31 or JCOP41 v2.3.1"); break;
206 case 0x38: PrintAndLog(" SAK : Nokia 6212 or 6131 MIFARE CLASSIC 4K"); break;
207 case 0x88: PrintAndLog(" SAK : Infineon MIFARE CLASSIC 1K"); break;
208 case 0x98: PrintAndLog(" SAK : Gemplus MPCOS"); break;
211 if(resp
->arg
[0] == 1) {
212 bool ta1
= 0, tb1
= 0, tc1
= 0;
215 PrintAndLog(" ATS : %s", sprint_hex(card
->ats
, card
->ats_len
));
216 if (card
->ats_len
> 0) {
217 PrintAndLog(" - TL : length is %d bytes", card
->ats
[0]);
219 if (card
->ats_len
> 1) {
220 ta1
= (card
->ats
[1] & 0x10) == 0x10;
221 tb1
= (card
->ats
[1] & 0x20) == 0x20;
222 tc1
= (card
->ats
[1] & 0x40) == 0x40;
223 PrintAndLog(" - T0 : TA1 is%s present, TB1 is%s present, "
224 "TC1 is%s present, FSCI is %d",
225 (ta1
? "" : " NOT"), (tb1
? "" : " NOT"), (tc1
? "" : " NOT"),
226 (card
->ats
[1] & 0x0f));
229 if (ta1
&& card
->ats_len
> pos
) {
231 dr
[0] = ds
[0] = '\0';
232 if (card
->ats
[pos
] & 0x10) strcat(ds
, "2, ");
233 if (card
->ats
[pos
] & 0x20) strcat(ds
, "4, ");
234 if (card
->ats
[pos
] & 0x40) strcat(ds
, "8, ");
235 if (card
->ats
[pos
] & 0x01) strcat(dr
, "2, ");
236 if (card
->ats
[pos
] & 0x02) strcat(dr
, "4, ");
237 if (card
->ats
[pos
] & 0x04) strcat(dr
, "8, ");
238 if (strlen(ds
) != 0) ds
[strlen(ds
) - 2] = '\0';
239 if (strlen(dr
) != 0) dr
[strlen(dr
) - 2] = '\0';
240 PrintAndLog(" - TA1 : different divisors are%s supported, "
241 "DR: [%s], DS: [%s]",
242 (card
->ats
[pos
] & 0x80 ? " NOT" : ""), dr
, ds
);
245 if (tb1
&& card
->ats_len
> pos
) {
246 PrintAndLog(" - TB1 : SFGI = %d, FWI = %d",
247 (card
->ats
[pos
] & 0x08),
248 (card
->ats
[pos
] & 0x80) >> 4);
251 if (tc1
&& card
->ats_len
> pos
) {
252 PrintAndLog(" - TC1 : NAD is%s supported, CID is%s supported",
253 (card
->ats
[pos
] & 0x01) ? "" : " NOT",
254 (card
->ats
[pos
] & 0x02) ? "" : " NOT");
257 if (card
->ats_len
> pos
) {
259 if (card
->ats_len
- pos
> 7) {
260 if (memcmp(card
->ats
+ pos
, "\xC1\x05\x2F\x2F\x01\xBC\xD6", 7) == 0) {
261 tip
= "-> MIFARE Plus X 2K or 4K";
262 } else if (memcmp(card
->ats
+ pos
, "\xC1\x05\x2F\x2F\x00\x35\xC7", 7) == 0) {
263 tip
= "-> MIFARE Plus S 2K or 4K";
266 PrintAndLog(" - HB : %s%s", sprint_hex(card
->ats
+ pos
, card
->ats_len
- pos
- 2), tip
);
267 if (card
->ats
[pos
] == 0xC1) {
268 PrintAndLog(" c1 -> Mifare or (multiple) virtual cards of various type");
269 PrintAndLog(" %02x -> Length is %d bytes",
270 card
->ats
[pos
+ 1], card
->ats
[pos
+ 1]);
271 switch (card
->ats
[pos
+ 2] & 0xf0) {
273 PrintAndLog(" 1x -> MIFARE DESFire");
276 PrintAndLog(" 2x -> MIFARE Plus");
279 switch (card
->ats
[pos
+ 2] & 0x0f) {
281 PrintAndLog(" x0 -> <1 kByte");
284 PrintAndLog(" x0 -> 1 kByte");
287 PrintAndLog(" x0 -> 2 kByte");
290 PrintAndLog(" x0 -> 4 kByte");
293 PrintAndLog(" x0 -> 8 kByte");
296 switch (card
->ats
[pos
+ 3] & 0xf0) {
298 PrintAndLog(" 0x -> Engineering sample");
301 PrintAndLog(" 2x -> Released");
304 switch (card
->ats
[pos
+ 3] & 0x0f) {
306 PrintAndLog(" x0 -> Generation 1");
309 PrintAndLog(" x1 -> Generation 2");
312 PrintAndLog(" x2 -> Generation 3");
315 switch (card
->ats
[pos
+ 4] & 0x0f) {
317 PrintAndLog(" x0 -> Only VCSL supported");
320 PrintAndLog(" x1 -> VCS, VCSL, and SVC supported");
323 PrintAndLog(" xE -> no VCS command supported");
330 PrintAndLog("proprietary non-iso14443a card found, RATS not supported");
335 // Collect ISO14443 Type A UIDs
336 int CmdHF14ACUIDs(const char *Cmd
)
338 // requested number of UIDs
340 // collect at least 1 (e.g. if no parameter was given)
343 PrintAndLog("Collecting %d UIDs", n
);
344 PrintAndLog("Start: %u", time(NULL
));
346 for (int i
= 0; i
< n
; i
++) {
347 // execute anticollision procedure
348 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
, 0, 0}};
350 UsbCommand
*resp
= WaitForResponse(CMD_ACK
);
351 uint8_t *uid
= resp
->d
.asBytes
;
352 iso14a_card_select_t
*card
= (iso14a_card_select_t
*)(uid
+ 12);
354 // check if command failed
355 if (resp
->arg
[0] == 0) {
356 PrintAndLog("Card select failed.");
358 // check if UID is 4 bytes
359 if ((card
->atqa
[1] & 0xC0) == 0) {
360 PrintAndLog("%02X%02X%02X%02X",
361 *uid
, *(uid
+ 1), *(uid
+ 2), *(uid
+ 3));
363 PrintAndLog("UID longer than 4 bytes");
367 PrintAndLog("End: %u", time(NULL
));
372 // ## simulate iso14443a tag
373 // ## greg - added ability to specify tag UID
374 int CmdHF14ASim(const char *Cmd
)
376 UsbCommand c
= {CMD_SIMULATE_TAG_ISO_14443a
,{0,0,0}};
378 // Retrieve the tag type
379 uint8_t tagtype
= param_get8ex(Cmd
,0,0,10);
381 // When no argument was given, just print help message
384 PrintAndLog(" Emulating ISO/IEC 14443 type A tag with 4 or 7 byte UID");
386 PrintAndLog(" syntax: hf 14a sim <type> <uid>");
387 PrintAndLog(" types: 1 = MIFARE Classic");
388 PrintAndLog(" 2 = MIFARE Ultralight");
389 PrintAndLog(" 3 = MIFARE DESFIRE");
390 PrintAndLog(" 4 = ISO/IEC 14443-4");
395 // Store the tag type
398 // Retrieve the full 4 or 7 byte long uid
399 uint64_t long_uid
= param_get64ex(Cmd
,1,0,16);
401 // Are we handling the (optional) second part uid?
402 if (long_uid
> 0xffffffff) {
403 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 7 byte UID (%014llx)",long_uid
);
404 // Store the second part
405 c
.arg
[2] = (long_uid
& 0xffffffff);
407 // Store the first part, ignore the first byte, it is replaced by cascade byte (0x88)
408 c
.arg
[1] = (long_uid
& 0xffffff);
410 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 4 byte UID (%08x)",long_uid
);
411 // Only store the first part
412 c
.arg
[1] = long_uid
& 0xffffffff;
415 // At lease save the mandatory first part of the UID
416 c.arg[0] = long_uid & 0xffffffff;
419 // At lease save the mandatory first part of the UID
420 c.arg[0] = long_uid & 0xffffffff;
423 PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
428 PrintAndLog("Emulating ISO/IEC 14443-3 type A tag with 4 byte UID");
429 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)};
432 PrintAndLog("Emulating ISO/IEC 14443-4 type A tag with 7 byte UID");
435 PrintAndLog("Error: unkown tag type (%d)",c.arg[0]);
436 PrintAndLog("syntax: hf 14a sim <uid>",c.arg[0]);
437 PrintAndLog(" type1: 4 ",c.arg[0]);
444 unsigned int hi = 0, lo = 0;
446 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
447 hi= (hi << 4) | (lo >> 28);
448 lo= (lo << 4) | (n & 0xf);
451 // 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)};
452 // PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
457 int CmdHF14ASnoop(const char *Cmd
) {
460 if (param_getchar(Cmd
, 0) == 'h') {
461 PrintAndLog("It get data from the field and saves it into command buffer.");
462 PrintAndLog("Buffer accessible from command hf 14a list.");
463 PrintAndLog("Usage: hf 14a snoop [c][r]");
464 PrintAndLog("c - triggered by first data from card");
465 PrintAndLog("r - triggered by first 7-bit request from reader (REQ,WUP,...)");
466 PrintAndLog("sample: hf 14a snoop c r");
470 for (int i
= 0; i
< 2; i
++) {
471 char ctmp
= param_getchar(Cmd
, i
);
472 if (ctmp
== 'c' || ctmp
== 'C') param
|= 0x01;
473 if (ctmp
== 'r' || ctmp
== 'R') param
|= 0x02;
476 UsbCommand c
= {CMD_SNOOP_ISO_14443a
, {param
, 0, 0}};
481 int CmdHF14AFuzz(const char *Cmd
) {
482 char formatstr
[256] = {0},sendbuf
[256] = {0};
483 uint32_t start
=0,end
=0;
485 if (param_getchar(Cmd
, 0) == 0) {
486 PrintAndLog("fuzz raw hex data to the card and show response <ONLY for develepers>");
487 PrintAndLog("Usage: hf 14a fuzz <FORMAT> [<start index> <end index>]");
488 PrintAndLog("FORMAT controls the output as in C printf");
489 PrintAndLog("sample: hf 14a fuzz 909F");
490 PrintAndLog(" hf 14a fuzz 00%02x00000000 0 0xFF");
494 start
= param_get8ex(Cmd
, 1, 0,16);
495 end
= param_get8ex(Cmd
, 2, 0,16);
496 param_getstr(Cmd
, 0, formatstr
);
498 for( int i
=start
;i
<=end
;++i
)
500 snprintf(sendbuf
, sizeof(sendbuf
), formatstr
, i
);
502 int len
= strlen(sendbuf
)/2;
504 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_APDU
|ISO14A_NO_DISCONNECT
, len
, 0}};
505 param_gethex(sendbuf
, 0, c
.d
.asBytes
, len
*2);
506 PrintAndLog("len:%d raw:",len
);
507 PrintAndLog("%s",sprint_hex(c
.d
.asBytes
, len
));
510 UsbCommand
* resp
= WaitForResponse(CMD_ACK
);
511 PrintAndLog("res:%d",resp
->arg
[0]);
513 while(resp
->arg
[0] > sizeof(resp
->d
))
515 PrintAndLog("%s", sprint_hex(resp
->d
.asBytes
,sizeof(resp
->d
)));
517 resp
= WaitForResponse(CMD_ACK
);
519 PrintAndLog("%s", sprint_hex(resp
->d
.asBytes
,resp
->arg
[0]));
529 static command_t CommandTable
[] =
531 {"help", CmdHelp
, 1, "This help"},
532 {"list", CmdHF14AList
, 0, "List ISO 14443a history"},
533 {"reader", CmdHF14AReader
, 0, "Act like an ISO14443 Type A reader"},
534 {"cuids", CmdHF14ACUIDs
, 0, "<n> Collect n>0 ISO14443 Type A UIDs in one go"},
535 {"sim", CmdHF14ASim
, 0, "<UID> -- Fake ISO 14443a tag"},
536 {"snoop", CmdHF14ASnoop
, 0, "Eavesdrop ISO 14443 Type A"},
537 {"fuzz", CmdHF14AFuzz
, 0, "Fuzz"},
538 {NULL
, NULL
, 0, NULL
}
541 int CmdHF14A(const char *Cmd
)
544 while (WaitForResponseTimeout(CMD_ACK
, 500) != NULL
) ;
547 CmdsParse(CommandTable
, Cmd
);
551 int CmdHelp(const char *Cmd
)
553 CmdsHelp(CommandTable
);