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 "../common/iso14443crc.h"
19 #include "proxmark3.h"
21 #include "cmdparser.h"
23 #include "../include/common.h"
25 #include "../include/mifare.h"
27 static int CmdHelp(const char *Cmd
);
28 static void waitCmd(uint8_t iLen
);
30 int CmdHF14AList(const char *Cmd
)
32 bool ShowWaitCycles
= false;
33 char param
= param_getchar(Cmd
, 0);
35 if (param
== 'h' || (param
!= 0 && param
!= 'f')) {
36 PrintAndLog("List data in trace buffer.");
37 PrintAndLog("Usage: hf 14a list [f]");
38 PrintAndLog("f - show frame delay times as well");
39 PrintAndLog("sample: hf 14a list f");
43 ShowWaitCycles
= (param
== 'f');
45 // for the time being. Need better Bigbuf handling.
46 #define TRACE_SIZE 3000
48 uint8_t trace
[TRACE_SIZE
];
49 GetFromBigBuf(trace
, TRACE_SIZE
, 0);
50 WaitForResponse(CMD_ACK
,NULL
);
52 PrintAndLog("Recorded Activity");
54 PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
55 PrintAndLog("All times are in carrier periods (1/13.56Mhz)");
57 PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC ");
58 PrintAndLog("-----------|-----------|-----|-----------------------------------------------------------------------");
60 uint16_t tracepos
= 0;
66 uint32_t first_timestamp
;
67 uint32_t EndOfTransmissionTimestamp
;
71 if(tracepos
>= TRACE_SIZE
) break;
73 timestamp
= *((uint32_t *)(trace
+ tracepos
));
75 // Break and stick with current result if buffer was not completely full
76 if (timestamp
== 0x44444444) break;
79 first_timestamp
= timestamp
;
83 duration
= *((uint16_t *)(trace
+ tracepos
));
85 data_len
= *((uint16_t *)(trace
+ tracepos
));
89 if (data_len
& 0x8000) {
94 parity_len
= (data_len
-1)/8 + 1;
96 if (tracepos
+ data_len
+ parity_len
>= TRACE_SIZE
) break;
98 uint8_t *frame
= trace
+ tracepos
;
100 uint8_t *parityBytes
= trace
+ tracepos
;
101 tracepos
+= parity_len
;
104 for (int j
= 0; j
< data_len
; j
++) {
105 int oddparity
= 0x01;
109 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
112 uint8_t parityBits
= parityBytes
[j
>>3];
113 if (isResponse
&& (oddparity
!= ((parityBits
>> (7-(j
&0x0007))) & 0x01))) {
114 sprintf(line
[j
/16]+((j
%16)*4), "%02x! ", frame
[j
]);
116 sprintf(line
[j
/16]+((j
%16)*4), "%02x ", frame
[j
]);
120 char crc
[5] = {0x00};
123 ComputeCrc14443(CRC_14443_A
, frame
, data_len
-2, &b1
, &b2
);
124 if (b1
!= frame
[data_len
-2] || b2
!= frame
[data_len
-1]) {
125 sprintf(crc
, (isResponse
& (data_len
< 6)) ? "" : "!crc");
129 EndOfTransmissionTimestamp
= timestamp
+ duration
;
130 int num_lines
= (data_len
- 1)/16 + 1;
132 for (int j
= 0; j
< num_lines
; j
++) {
134 PrintAndLog(" %9d | %9d | %s | %-64s| %s",
135 (timestamp
- first_timestamp
),
136 (EndOfTransmissionTimestamp
- first_timestamp
),
137 (isResponse
? "Tag" : "Rdr"),
139 (j
== num_lines
-1)?crc
:""
142 PrintAndLog(" | | | %-64s| %s",
144 (j
== num_lines
-1)?crc
:"");
148 bool next_isResponse
= *((uint16_t *)(trace
+ tracepos
+ 6)) & 0x8000;
150 if (ShowWaitCycles
&& !isResponse
&& next_isResponse
) {
151 uint32_t next_timestamp
= *((uint32_t *)(trace
+ tracepos
));
152 if (next_timestamp
!= 0x44444444) {
153 PrintAndLog(" %9d | %9d | %s | fdt (Frame Delay Time): %d",
154 (EndOfTransmissionTimestamp
- first_timestamp
),
155 (next_timestamp
- first_timestamp
),
157 (next_timestamp
- EndOfTransmissionTimestamp
));
164 void iso14a_set_timeout(uint32_t timeout
) {
165 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_SET_TIMEOUT
, 0, timeout
}};
169 int CmdHF14AReader(const char *Cmd
)
171 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_NO_DISCONNECT
, 0, 0}};
175 WaitForResponse(CMD_ACK
,&resp
);
177 iso14a_card_select_t card
;
178 memcpy(&card
, (iso14a_card_select_t
*)resp
.d
.asBytes
, sizeof(iso14a_card_select_t
));
180 uint64_t select_status
= resp
.arg
[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS
182 if(select_status
== 0) {
183 PrintAndLog("iso14443a card select failed");
192 PrintAndLog("ATQA : %02x %02x", card
.atqa
[1], card
.atqa
[0]);
193 PrintAndLog(" UID : %s", sprint_hex(card
.uid
, card
.uidlen
));
194 PrintAndLog(" SAK : %02x [%d]", card
.sak
, resp
.arg
[0]);
197 case 0x00: PrintAndLog("TYPE : NXP MIFARE Ultralight | Ultralight C"); break;
198 case 0x01: PrintAndLog("TYPE : NXP TNP3xxx Activision Game Appliance"); break;
199 case 0x04: PrintAndLog("TYPE : NXP MIFARE (various !DESFire !DESFire EV1)"); break;
200 case 0x08: PrintAndLog("TYPE : NXP MIFARE CLASSIC 1k | Plus 2k SL1"); break;
201 case 0x09: PrintAndLog("TYPE : NXP MIFARE Mini 0.3k"); break;
202 case 0x10: PrintAndLog("TYPE : NXP MIFARE Plus 2k SL2"); break;
203 case 0x11: PrintAndLog("TYPE : NXP MIFARE Plus 4k SL2"); break;
204 case 0x18: PrintAndLog("TYPE : NXP MIFARE Classic 4k | Plus 4k SL1"); break;
205 case 0x20: PrintAndLog("TYPE : NXP MIFARE DESFire 4k | DESFire EV1 2k/4k/8k | Plus 2k/4k SL3 | JCOP 31/41"); break;
206 case 0x24: PrintAndLog("TYPE : NXP MIFARE DESFire | DESFire EV1"); break;
207 case 0x28: PrintAndLog("TYPE : JCOP31 or JCOP41 v2.3.1"); break;
208 case 0x38: PrintAndLog("TYPE : Nokia 6212 or 6131 MIFARE CLASSIC 4K"); break;
209 case 0x88: PrintAndLog("TYPE : Infineon MIFARE CLASSIC 1K"); break;
210 case 0x98: PrintAndLog("TYPE : Gemplus MPCOS"); break;
215 // try to request ATS even if tag claims not to support it
216 if (select_status
== 2) {
217 uint8_t rats
[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0
218 c
.arg
[0] = ISO14A_RAW
| ISO14A_APPEND_CRC
| ISO14A_NO_DISCONNECT
;
221 memcpy(c
.d
.asBytes
, rats
, 2);
223 WaitForResponse(CMD_ACK
,&resp
);
225 memcpy(&card
.ats
, resp
.d
.asBytes
, resp
.arg
[0]);
226 card
.ats_len
= resp
.arg
[0]; // note: ats_len includes CRC Bytes
229 if(card
.ats_len
>= 3) { // a valid ATS consists of at least the length byte (TL) and 2 CRC bytes
230 bool ta1
= 0, tb1
= 0, tc1
= 0;
233 if (select_status
== 2) {
234 PrintAndLog("SAK incorrectly claims that card doesn't support RATS");
236 PrintAndLog(" ATS : %s", sprint_hex(card
.ats
, card
.ats_len
));
237 PrintAndLog(" - TL : length is %d bytes", card
.ats
[0]);
238 if (card
.ats
[0] != card
.ats_len
- 2) {
239 PrintAndLog("ATS may be corrupted. Length of ATS (%d bytes incl. 2 Bytes CRC) doesn't match TL", card
.ats_len
);
242 if (card
.ats
[0] > 1) { // there is a format byte (T0)
243 ta1
= (card
.ats
[1] & 0x10) == 0x10;
244 tb1
= (card
.ats
[1] & 0x20) == 0x20;
245 tc1
= (card
.ats
[1] & 0x40) == 0x40;
246 int16_t fsci
= card
.ats
[1] & 0x0f;
247 PrintAndLog(" - T0 : TA1 is%s present, TB1 is%s present, "
248 "TC1 is%s present, FSCI is %d (FSC = %ld)",
249 (ta1
? "" : " NOT"), (tb1
? "" : " NOT"), (tc1
? "" : " NOT"),
251 fsci
< 5 ? (fsci
- 2) * 8 :
252 fsci
< 8 ? (fsci
- 3) * 32 :
260 dr
[0] = ds
[0] = '\0';
261 if (card
.ats
[pos
] & 0x10) strcat(ds
, "2, ");
262 if (card
.ats
[pos
] & 0x20) strcat(ds
, "4, ");
263 if (card
.ats
[pos
] & 0x40) strcat(ds
, "8, ");
264 if (card
.ats
[pos
] & 0x01) strcat(dr
, "2, ");
265 if (card
.ats
[pos
] & 0x02) strcat(dr
, "4, ");
266 if (card
.ats
[pos
] & 0x04) strcat(dr
, "8, ");
267 if (strlen(ds
) != 0) ds
[strlen(ds
) - 2] = '\0';
268 if (strlen(dr
) != 0) dr
[strlen(dr
) - 2] = '\0';
269 PrintAndLog(" - TA1 : different divisors are%s supported, "
270 "DR: [%s], DS: [%s]",
271 (card
.ats
[pos
] & 0x80 ? " NOT" : ""), dr
, ds
);
275 uint32_t sfgi
= card
.ats
[pos
] & 0x0F;
276 uint32_t fwi
= card
.ats
[pos
] >> 4;
277 PrintAndLog(" - TB1 : SFGI = %d (SFGT = %s%ld/fc), FWI = %d (FWT = %ld/fc)",
279 sfgi
? "" : "(not needed) ",
280 sfgi
? (1 << 12) << sfgi
: 0,
287 PrintAndLog(" - TC1 : NAD is%s supported, CID is%s supported",
288 (card
.ats
[pos
] & 0x01) ? "" : " NOT",
289 (card
.ats
[pos
] & 0x02) ? "" : " NOT");
292 if (card
.ats
[0] > pos
) {
294 if (card
.ats
[0] - pos
>= 7) {
295 if (memcmp(card
.ats
+ pos
, "\xC1\x05\x2F\x2F\x01\xBC\xD6", 7) == 0) {
296 tip
= "-> MIFARE Plus X 2K or 4K";
297 } else if (memcmp(card
.ats
+ pos
, "\xC1\x05\x2F\x2F\x00\x35\xC7", 7) == 0) {
298 tip
= "-> MIFARE Plus S 2K or 4K";
301 PrintAndLog(" - HB : %s%s", sprint_hex(card
.ats
+ pos
, card
.ats
[0] - pos
), tip
);
302 if (card
.ats
[pos
] == 0xC1) {
303 PrintAndLog(" c1 -> Mifare or (multiple) virtual cards of various type");
304 PrintAndLog(" %02x -> Length is %d bytes",
305 card
.ats
[pos
+ 1], card
.ats
[pos
+ 1]);
306 switch (card
.ats
[pos
+ 2] & 0xf0) {
308 PrintAndLog(" 1x -> MIFARE DESFire");
311 PrintAndLog(" 2x -> MIFARE Plus");
314 switch (card
.ats
[pos
+ 2] & 0x0f) {
316 PrintAndLog(" x0 -> <1 kByte");
319 PrintAndLog(" x0 -> 1 kByte");
322 PrintAndLog(" x0 -> 2 kByte");
325 PrintAndLog(" x0 -> 4 kByte");
328 PrintAndLog(" x0 -> 8 kByte");
331 switch (card
.ats
[pos
+ 3] & 0xf0) {
333 PrintAndLog(" 0x -> Engineering sample");
336 PrintAndLog(" 2x -> Released");
339 switch (card
.ats
[pos
+ 3] & 0x0f) {
341 PrintAndLog(" x0 -> Generation 1");
344 PrintAndLog(" x1 -> Generation 2");
347 PrintAndLog(" x2 -> Generation 3");
350 switch (card
.ats
[pos
+ 4] & 0x0f) {
352 PrintAndLog(" x0 -> Only VCSL supported");
355 PrintAndLog(" x1 -> VCS, VCSL, and SVC supported");
358 PrintAndLog(" xE -> no VCS command supported");
364 PrintAndLog("proprietary non iso14443-4 card found, RATS not supported");
368 // try to see if card responses to "chinese magic backdoor" commands.
369 c
.cmd
= CMD_MIFARE_CIDENT
;
374 WaitForResponse(CMD_ACK
,&resp
);
375 uint8_t isOK
= resp
.arg
[0] & 0xff;
376 PrintAndLog(" Answers to chinese magic backdoor commands: %s", (isOK
? "YES" : "NO") );
379 c
.cmd
= CMD_READER_ISO_14443a
;
385 return select_status
;
388 // Collect ISO14443 Type A UIDs
389 int CmdHF14ACUIDs(const char *Cmd
)
391 // requested number of UIDs
393 // collect at least 1 (e.g. if no parameter was given)
396 PrintAndLog("Collecting %d UIDs", n
);
397 PrintAndLog("Start: %u", time(NULL
));
399 for (int i
= 0; i
< n
; i
++) {
400 // execute anticollision procedure
401 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
, 0, 0}};
405 WaitForResponse(CMD_ACK
,&resp
);
407 iso14a_card_select_t
*card
= (iso14a_card_select_t
*) resp
.d
.asBytes
;
409 // check if command failed
410 if (resp
.arg
[0] == 0) {
411 PrintAndLog("Card select failed.");
414 for (uint16_t i
= 0; i
< card
->uidlen
; i
++) {
415 sprintf(&uid_string
[2*i
], "%02X", card
->uid
[i
]);
417 PrintAndLog("%s", uid_string
);
420 PrintAndLog("End: %u", time(NULL
));
425 // ## simulate iso14443a tag
426 // ## greg - added ability to specify tag UID
427 int CmdHF14ASim(const char *Cmd
)
429 UsbCommand c
= {CMD_SIMULATE_TAG_ISO_14443a
,{0,0,0}};
431 // Retrieve the tag type
432 uint8_t tagtype
= param_get8ex(Cmd
,0,0,10);
434 // When no argument was given, just print help message
437 PrintAndLog(" Emulating ISO/IEC 14443 type A tag with 4 or 7 byte UID");
439 PrintAndLog(" syntax: hf 14a sim <type> <uid>");
440 PrintAndLog(" types: 1 = MIFARE Classic");
441 PrintAndLog(" 2 = MIFARE Ultralight");
442 PrintAndLog(" 3 = MIFARE DESFIRE");
443 PrintAndLog(" 4 = ISO/IEC 14443-4");
444 PrintAndLog(" 5 = MIFARE TNP3XXX");
449 // Store the tag type
452 // Retrieve the full 4 or 7 byte long uid
453 uint64_t long_uid
= param_get64ex(Cmd
,1,0,16);
455 // Are we handling the (optional) second part uid?
456 if (long_uid
> 0xffffffff) {
457 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 7 byte UID (%014"llx
")",long_uid
);
458 // Store the second part
459 c
.arg
[2] = (long_uid
& 0xffffffff);
461 // Store the first part, ignore the first byte, it is replaced by cascade byte (0x88)
462 c
.arg
[1] = (long_uid
& 0xffffff);
464 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 4 byte UID (%08x)",long_uid
);
465 // Only store the first part
466 c
.arg
[1] = long_uid
& 0xffffffff;
469 // At lease save the mandatory first part of the UID
470 c.arg[0] = long_uid & 0xffffffff;
473 PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
478 PrintAndLog("Emulating ISO/IEC 14443-3 type A tag with 4 byte UID");
479 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)};
482 PrintAndLog("Emulating ISO/IEC 14443-4 type A tag with 7 byte UID");
485 PrintAndLog("Error: unkown tag type (%d)",c.arg[0]);
486 PrintAndLog("syntax: hf 14a sim <uid>",c.arg[0]);
487 PrintAndLog(" type1: 4 ",c.arg[0]);
494 unsigned int hi = 0, lo = 0;
496 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
497 hi= (hi << 4) | (lo >> 28);
498 lo= (lo << 4) | (n & 0xf);
501 // 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)};
502 // PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
507 int CmdHF14ASnoop(const char *Cmd
) {
510 if (param_getchar(Cmd
, 0) == 'h') {
511 PrintAndLog("It get data from the field and saves it into command buffer.");
512 PrintAndLog("Buffer accessible from command hf 14a list.");
513 PrintAndLog("Usage: hf 14a snoop [c][r]");
514 PrintAndLog("c - triggered by first data from card");
515 PrintAndLog("r - triggered by first 7-bit request from reader (REQ,WUP,...)");
516 PrintAndLog("sample: hf 14a snoop c r");
520 for (int i
= 0; i
< 2; i
++) {
521 char ctmp
= param_getchar(Cmd
, i
);
522 if (ctmp
== 'c' || ctmp
== 'C') param
|= 0x01;
523 if (ctmp
== 'r' || ctmp
== 'R') param
|= 0x02;
526 UsbCommand c
= {CMD_SNOOP_ISO_14443a
, {param
, 0, 0}};
531 int CmdHF14ACmdRaw(const char *cmd
) {
532 UsbCommand c
= {CMD_READER_ISO_14443a
, {0, 0, 0}};
537 uint8_t active_select
=0;
543 uint8_t data
[USB_CMD_DATA_SIZE
];
544 unsigned int datalen
=0, temp
;
547 PrintAndLog("Usage: hf 14a raw [-r] [-c] [-p] [-f] [-b] [-t] <number of bits> <0A 0B 0C ... hex>");
548 PrintAndLog(" -r do not read response");
549 PrintAndLog(" -c calculate and append CRC");
550 PrintAndLog(" -p leave the signal field ON after receive");
551 PrintAndLog(" -a active signal field ON without select");
552 PrintAndLog(" -s active signal field ON with select");
553 PrintAndLog(" -b number of bits to send. Useful for send partial byte");
554 PrintAndLog(" -t timeout");
559 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
561 while (cmd
[i
]!='\0') {
562 if (cmd
[i
]==' ' || cmd
[i
]=='\t') { i
++; continue; }
581 sscanf(cmd
+i
+2,"%d",&temp
);
582 numbits
= temp
& 0xFFFF;
584 while(cmd
[i
]!=' ' && cmd
[i
]!='\0') { i
++; }
589 sscanf(cmd
+i
+2,"%d",&temp
);
590 timeout
= temp
& 0xFFFF;
592 while(cmd
[i
]!=' ' && cmd
[i
]!='\0') { i
++; }
596 PrintAndLog("Invalid option");
602 if ((cmd
[i
]>='0' && cmd
[i
]<='9') ||
603 (cmd
[i
]>='a' && cmd
[i
]<='f') ||
604 (cmd
[i
]>='A' && cmd
[i
]<='F') ) {
605 buf
[strlen(buf
)+1]=0;
606 buf
[strlen(buf
)]=cmd
[i
];
609 if (strlen(buf
)>=2) {
610 sscanf(buf
,"%x",&temp
);
611 data
[datalen
]=(uint8_t)(temp
& 0xff);
613 if (++datalen
>sizeof(data
)){
615 PrintAndLog("Buffer is full, we can't add CRC to your data");
621 PrintAndLog("Invalid char on input");
624 if(crc
&& datalen
>0 && datalen
<sizeof(data
)-2)
626 uint8_t first
, second
;
627 ComputeCrc14443(CRC_14443_A
, data
, datalen
, &first
, &second
);
628 data
[datalen
++] = first
;
629 data
[datalen
++] = second
;
632 if(active
|| active_select
)
634 c
.arg
[0] |= ISO14A_CONNECT
;
636 c
.arg
[0] |= ISO14A_NO_SELECT
;
639 #define MAX_TIMEOUT 624*105 // max timeout is 624 ms
640 c
.arg
[0] |= ISO14A_SET_TIMEOUT
;
641 c
.arg
[2] = timeout
* 105; // each bit is about 9.4 us
642 if(c
.arg
[2]>MAX_TIMEOUT
) {
643 c
.arg
[2] = MAX_TIMEOUT
;
644 PrintAndLog("Set timeout to 624 ms. The max we can wait for response");
648 c
.arg
[0] |= ISO14A_NO_DISCONNECT
;
650 c
.arg
[0] |= ISO14A_RAW
;
652 // Max buffer is USB_CMD_DATA_SIZE
653 c
.arg
[1] = (datalen
& 0xFFFF) | (numbits
<< 16);
654 memcpy(c
.d
.asBytes
,data
,datalen
);
667 static void waitCmd(uint8_t iSelect
)
673 if (WaitForResponseTimeout(CMD_ACK
,&resp
,10000)) {
674 recv
= resp
.d
.asBytes
;
675 uint8_t iLen
= iSelect
? resp
.arg
[1] : resp
.arg
[0];
676 PrintAndLog("received %i octets",iLen
);
679 hexout
= (char *)malloc(iLen
* 3 + 1);
680 if (hexout
!= NULL
) {
681 for (int i
= 0; i
< iLen
; i
++) { // data in hex
682 sprintf(&hexout
[i
* 3], "%02X ", recv
[i
]);
684 PrintAndLog("%s", hexout
);
687 PrintAndLog("malloc failed your client has low memory?");
690 PrintAndLog("timeout while waiting for reply.");
694 static command_t CommandTable
[] =
696 {"help", CmdHelp
, 1, "This help"},
697 {"list", CmdHF14AList
, 0, "List ISO 14443a history"},
698 {"reader", CmdHF14AReader
, 0, "Act like an ISO14443 Type A reader"},
699 {"cuids", CmdHF14ACUIDs
, 0, "<n> Collect n>0 ISO14443 Type A UIDs in one go"},
700 {"sim", CmdHF14ASim
, 0, "<UID> -- Fake ISO 14443a tag"},
701 {"snoop", CmdHF14ASnoop
, 0, "Eavesdrop ISO 14443 Type A"},
702 {"raw", CmdHF14ACmdRaw
, 0, "Send raw hex data to tag"},
703 {NULL
, NULL
, 0, NULL
}
706 int CmdHF14A(const char *Cmd
) {
708 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
711 CmdsParse(CommandTable
, Cmd
);
715 int CmdHelp(const char *Cmd
)
717 CmdsHelp(CommandTable
);