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"
19 #include "proxmark3.h"
21 #include "cmdparser.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");
44 ShowWaitCycles
= true;
48 GetFromBigBuf(got
,sizeof(got
),0);
49 WaitForResponse(CMD_ACK
,NULL
);
51 PrintAndLog("Recorded Activity");
53 PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
54 PrintAndLog("All times are in carrier periods (1/13.56Mhz)");
56 PrintAndLog(" Start | End | Src | Data");
57 PrintAndLog("-----------|-----------|-----|--------");
60 uint32_t first_timestamp
= 0;
62 uint32_t EndOfTransmissionTimestamp
= 0;
70 timestamp
= *((uint32_t *)(got
+i
));
71 if (timestamp
& 0x80000000) {
72 timestamp
&= 0x7fffffff;
79 first_timestamp
= timestamp
;
82 int parityBits
= *((uint32_t *)(got
+i
+4));
89 if (i
+ len
>= 1900) {
93 uint8_t *frame
= (got
+i
+9);
95 // Break and stick with current result if buffer was not completely full
96 if (frame
[0] == 0x44 && frame
[1] == 0x44 && frame
[2] == 0x44 && frame
[3] == 0x44) break;
101 for (j
= 0; j
< len
; j
++) {
102 int oddparity
= 0x01;
106 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
109 //if((parityBits >> (len - j - 1)) & 0x01) {
110 if (isResponse
&& (oddparity
!= ((parityBits
>> (len
- j
- 1)) & 0x01))) {
111 sprintf(line
+(j
*4), "%02x! ", frame
[j
]);
113 sprintf(line
+(j
*4), "%02x ", frame
[j
]);
117 if (ShowWaitCycles
) {
118 uint32_t next_timestamp
= (*((uint32_t *)(got
+i
+9))) & 0x7fffffff;
119 sprintf(line
, "fdt (Frame Delay Time): %d", (next_timestamp
- timestamp
));
127 for (j
= 0; j
< (len
- 1); j
++) {
128 // gives problems... search for the reason..
129 /*if(frame[j] == 0xAA) {
132 crc = "[1] Two drops close after each other";
135 crc = "[2] Potential SOC with a drop in second half of bitperiod";
138 crc = "[3] Segment Z after segment X is not possible";
141 crc = "[4] Parity bit of a fully received byte was wrong";
144 crc = "[?] Unknown error";
151 if (strlen(crc
)==0) {
152 ComputeCrc14443(CRC_14443_A
, frame
, len
-2, &b1
, &b2
);
153 if (b1
!= frame
[len
-2] || b2
!= frame
[len
-1]) {
154 crc
= (isResponse
& (len
< 6)) ? "" : " !crc";
165 EndOfTransmissionTimestamp
= (*((uint32_t *)(got
+i
))) & 0x7fffffff;
167 if (!ShowWaitCycles
) i
+= 9;
169 PrintAndLog(" %9d | %9d | %s | %s %s",
170 (timestamp
- first_timestamp
),
171 (EndOfTransmissionTimestamp
- first_timestamp
),
172 (len
?(isResponse
? "Tag" : "Rdr"):" "),
179 void iso14a_set_timeout(uint32_t timeout
) {
180 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_SET_TIMEOUT
, 0, timeout
}};
184 int CmdHF14AReader(const char *Cmd
)
186 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_NO_DISCONNECT
, 0, 0}};
190 WaitForResponse(CMD_ACK
,&resp
);
192 iso14a_card_select_t card
;
193 memcpy(&card
, (iso14a_card_select_t
*)resp
.d
.asBytes
, sizeof(iso14a_card_select_t
));
195 uint64_t select_status
= resp
.arg
[0];
197 if(select_status
== 0) {
198 PrintAndLog("iso14443a card select failed");
202 PrintAndLog("ATQA : %02x %02x", card
.atqa
[1], card
.atqa
[0]);
203 PrintAndLog(" UID : %s", sprint_hex(card
.uid
, card
.uidlen
));
204 PrintAndLog(" SAK : %02x [%d]", card
.sak
, resp
.arg
[0]);
207 case 0x00: PrintAndLog("TYPE : NXP MIFARE Ultralight | Ultralight C"); break;
208 case 0x04: PrintAndLog("TYPE : NXP MIFARE (various !DESFire !DESFire EV1)"); break;
209 case 0x08: PrintAndLog("TYPE : NXP MIFARE CLASSIC 1k | Plus 2k SL1"); break;
210 case 0x09: PrintAndLog("TYPE : NXP MIFARE Mini 0.3k"); break;
211 case 0x10: PrintAndLog("TYPE : NXP MIFARE Plus 2k SL2"); break;
212 case 0x11: PrintAndLog("TYPE : NXP MIFARE Plus 4k SL2"); break;
213 case 0x18: PrintAndLog("TYPE : NXP MIFARE Classic 4k | Plus 4k SL1"); break;
214 case 0x20: PrintAndLog("TYPE : NXP MIFARE DESFire 4k | DESFire EV1 2k/4k/8k | Plus 2k/4k SL3 | JCOP 31/41"); break;
215 case 0x24: PrintAndLog("TYPE : NXP MIFARE DESFire | DESFire EV1"); break;
216 case 0x28: PrintAndLog("TYPE : JCOP31 or JCOP41 v2.3.1"); break;
217 case 0x38: PrintAndLog("TYPE : Nokia 6212 or 6131 MIFARE CLASSIC 4K"); break;
218 case 0x88: PrintAndLog("TYPE : Infineon MIFARE CLASSIC 1K"); break;
219 case 0x98: PrintAndLog("TYPE : Gemplus MPCOS"); break;
224 // try to request ATS even if tag claims not to support it
225 if (select_status
== 2) {
226 uint8_t rats
[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0
227 c
.arg
[0] = ISO14A_RAW
| ISO14A_APPEND_CRC
| ISO14A_NO_DISCONNECT
;
230 memcpy(c
.d
.asBytes
, rats
, 2);
232 WaitForResponse(CMD_ACK
,&resp
);
234 memcpy(&card
.ats
, resp
.d
.asBytes
, resp
.arg
[0]);
235 card
.ats_len
= resp
.arg
[0];
245 if(card
.ats_len
>= 3) { // a valid ATS consists of at least the length byte TL and 2 CRC bytes
246 bool ta1
= 0, tb1
= 0, tc1
= 0;
249 if (!(card
.sak
& 0x02)) {
250 PrintAndLog("SAK incorrectly claims that card doesn't support RATS");
252 PrintAndLog(" ATS : %s", sprint_hex(card
.ats
, card
.ats_len
));
253 if (card
.ats_len
> 0) {
254 PrintAndLog(" - TL : length is %d bytes", card
.ats
[0]);
256 if (card
.ats_len
> 1) {
257 ta1
= (card
.ats
[1] & 0x10) == 0x10;
258 tb1
= (card
.ats
[1] & 0x20) == 0x20;
259 tc1
= (card
.ats
[1] & 0x40) == 0x40;
260 PrintAndLog(" - T0 : TA1 is%s present, TB1 is%s present, "
261 "TC1 is%s present, FSCI is %d",
262 (ta1
? "" : " NOT"), (tb1
? "" : " NOT"), (tc1
? "" : " NOT"),
263 (card
.ats
[1] & 0x0f));
266 if (ta1
&& card
.ats_len
> pos
) {
268 dr
[0] = ds
[0] = '\0';
269 if (card
.ats
[pos
] & 0x10) strcat(ds
, "2, ");
270 if (card
.ats
[pos
] & 0x20) strcat(ds
, "4, ");
271 if (card
.ats
[pos
] & 0x40) strcat(ds
, "8, ");
272 if (card
.ats
[pos
] & 0x01) strcat(dr
, "2, ");
273 if (card
.ats
[pos
] & 0x02) strcat(dr
, "4, ");
274 if (card
.ats
[pos
] & 0x04) strcat(dr
, "8, ");
275 if (strlen(ds
) != 0) ds
[strlen(ds
) - 2] = '\0';
276 if (strlen(dr
) != 0) dr
[strlen(dr
) - 2] = '\0';
277 PrintAndLog(" - TA1 : different divisors are%s supported, "
278 "DR: [%s], DS: [%s]",
279 (card
.ats
[pos
] & 0x80 ? " NOT" : ""), dr
, ds
);
282 if (tb1
&& card
.ats_len
> pos
) {
283 PrintAndLog(" - TB1 : SFGI = %d, FWI = %d",
284 (card
.ats
[pos
] & 0x08),
285 (card
.ats
[pos
] & 0x80) >> 4);
288 if (tc1
&& card
.ats_len
> pos
) {
289 PrintAndLog(" - TC1 : NAD is%s supported, CID is%s supported",
290 (card
.ats
[pos
] & 0x01) ? "" : " NOT",
291 (card
.ats
[pos
] & 0x02) ? "" : " NOT");
294 if (card
.ats_len
> pos
) {
296 if (card
.ats_len
- pos
> 7) {
297 if (memcmp(card
.ats
+ pos
, "\xC1\x05\x2F\x2F\x01\xBC\xD6", 7) == 0) {
298 tip
= "-> MIFARE Plus X 2K or 4K";
299 } else if (memcmp(card
.ats
+ pos
, "\xC1\x05\x2F\x2F\x00\x35\xC7", 7) == 0) {
300 tip
= "-> MIFARE Plus S 2K or 4K";
303 PrintAndLog(" - HB : %s%s", sprint_hex(card
.ats
+ pos
, card
.ats_len
- pos
- 2), tip
);
304 if (card
.ats
[pos
] == 0xC1) {
305 PrintAndLog(" c1 -> Mifare or (multiple) virtual cards of various type");
306 PrintAndLog(" %02x -> Length is %d bytes",
307 card
.ats
[pos
+ 1], card
.ats
[pos
+ 1]);
308 switch (card
.ats
[pos
+ 2] & 0xf0) {
310 PrintAndLog(" 1x -> MIFARE DESFire");
313 PrintAndLog(" 2x -> MIFARE Plus");
316 switch (card
.ats
[pos
+ 2] & 0x0f) {
318 PrintAndLog(" x0 -> <1 kByte");
321 PrintAndLog(" x0 -> 1 kByte");
324 PrintAndLog(" x0 -> 2 kByte");
327 PrintAndLog(" x0 -> 4 kByte");
330 PrintAndLog(" x0 -> 8 kByte");
333 switch (card
.ats
[pos
+ 3] & 0xf0) {
335 PrintAndLog(" 0x -> Engineering sample");
338 PrintAndLog(" 2x -> Released");
341 switch (card
.ats
[pos
+ 3] & 0x0f) {
343 PrintAndLog(" x0 -> Generation 1");
346 PrintAndLog(" x1 -> Generation 2");
349 PrintAndLog(" x2 -> Generation 3");
352 switch (card
.ats
[pos
+ 4] & 0x0f) {
354 PrintAndLog(" x0 -> Only VCSL supported");
357 PrintAndLog(" x1 -> VCS, VCSL, and SVC supported");
360 PrintAndLog(" xE -> no VCS command supported");
366 PrintAndLog("proprietary non iso14443-4 card found, RATS not supported");
369 return select_status
;
372 // Collect ISO14443 Type A UIDs
373 int CmdHF14ACUIDs(const char *Cmd
)
375 // requested number of UIDs
377 // collect at least 1 (e.g. if no parameter was given)
380 PrintAndLog("Collecting %d UIDs", n
);
381 PrintAndLog("Start: %u", time(NULL
));
383 for (int i
= 0; i
< n
; i
++) {
384 // execute anticollision procedure
385 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
, 0, 0}};
389 WaitForResponse(CMD_ACK
,&resp
);
391 iso14a_card_select_t
*card
= (iso14a_card_select_t
*) resp
.d
.asBytes
;
393 // check if command failed
394 if (resp
.arg
[0] == 0) {
395 PrintAndLog("Card select failed.");
398 for (uint16_t i
= 0; i
< card
->uidlen
; i
++) {
399 sprintf(&uid_string
[2*i
], "%02X", card
->uid
[i
]);
401 PrintAndLog("%s", uid_string
);
404 PrintAndLog("End: %u", time(NULL
));
409 // ## simulate iso14443a tag
410 // ## greg - added ability to specify tag UID
411 int CmdHF14ASim(const char *Cmd
)
413 UsbCommand c
= {CMD_SIMULATE_TAG_ISO_14443a
,{0,0,0}};
415 // Retrieve the tag type
416 uint8_t tagtype
= param_get8ex(Cmd
,0,0,10);
418 // When no argument was given, just print help message
421 PrintAndLog(" Emulating ISO/IEC 14443 type A tag with 4 or 7 byte UID");
423 PrintAndLog(" syntax: hf 14a sim <type> <uid>");
424 PrintAndLog(" types: 1 = MIFARE Classic");
425 PrintAndLog(" 2 = MIFARE Ultralight");
426 PrintAndLog(" 3 = MIFARE DESFIRE");
427 PrintAndLog(" 4 = ISO/IEC 14443-4");
432 // Store the tag type
435 // Retrieve the full 4 or 7 byte long uid
436 uint64_t long_uid
= param_get64ex(Cmd
,1,0,16);
438 // Are we handling the (optional) second part uid?
439 if (long_uid
> 0xffffffff) {
440 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 7 byte UID (%014"llx
")",long_uid
);
441 // Store the second part
442 c
.arg
[2] = (long_uid
& 0xffffffff);
444 // Store the first part, ignore the first byte, it is replaced by cascade byte (0x88)
445 c
.arg
[1] = (long_uid
& 0xffffff);
447 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 4 byte UID (%08x)",long_uid
);
448 // Only store the first part
449 c
.arg
[1] = long_uid
& 0xffffffff;
452 // At lease save the mandatory first part of the UID
453 c.arg[0] = long_uid & 0xffffffff;
456 // At lease save the mandatory first part of the UID
457 c.arg[0] = long_uid & 0xffffffff;
460 PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
465 PrintAndLog("Emulating ISO/IEC 14443-3 type A tag with 4 byte UID");
466 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)};
469 PrintAndLog("Emulating ISO/IEC 14443-4 type A tag with 7 byte UID");
472 PrintAndLog("Error: unkown tag type (%d)",c.arg[0]);
473 PrintAndLog("syntax: hf 14a sim <uid>",c.arg[0]);
474 PrintAndLog(" type1: 4 ",c.arg[0]);
481 unsigned int hi = 0, lo = 0;
483 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
484 hi= (hi << 4) | (lo >> 28);
485 lo= (lo << 4) | (n & 0xf);
488 // 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)};
489 // PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
494 int CmdHF14ASnoop(const char *Cmd
) {
497 if (param_getchar(Cmd
, 0) == 'h') {
498 PrintAndLog("It get data from the field and saves it into command buffer.");
499 PrintAndLog("Buffer accessible from command hf 14a list.");
500 PrintAndLog("Usage: hf 14a snoop [c][r]");
501 PrintAndLog("c - triggered by first data from card");
502 PrintAndLog("r - triggered by first 7-bit request from reader (REQ,WUP,...)");
503 PrintAndLog("sample: hf 14a snoop c r");
507 for (int i
= 0; i
< 2; i
++) {
508 char ctmp
= param_getchar(Cmd
, i
);
509 if (ctmp
== 'c' || ctmp
== 'C') param
|= 0x01;
510 if (ctmp
== 'r' || ctmp
== 'R') param
|= 0x02;
513 UsbCommand c
= {CMD_SNOOP_ISO_14443a
, {param
, 0, 0}};
518 int CmdHF14ACmdRaw(const char *cmd
) {
519 UsbCommand c
= {CMD_READER_ISO_14443a
, {0, 0, 0}};
524 uint8_t active_select
=0;
529 unsigned int datalen
=0, temp
;
532 PrintAndLog("Usage: hf 14a raw [-r] [-c] [-p] [-f] [-b] <number of bits> <0A 0B 0C ... hex>");
533 PrintAndLog(" -r do not read response");
534 PrintAndLog(" -c calculate and append CRC");
535 PrintAndLog(" -p leave the signal field ON after receive");
536 PrintAndLog(" -a active signal field ON without select");
537 PrintAndLog(" -s active signal field ON with select");
538 PrintAndLog(" -b number of bits to send. Useful for send partial byte");
543 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
545 while (cmd
[i
]!='\0') {
546 if (cmd
[i
]==' ' || cmd
[i
]=='\t') { i
++; continue; }
565 sscanf(cmd
+i
+2,"%d",&temp
);
566 numbits
= temp
& 0xFFFF;
568 while(cmd
[i
]!=' ' && cmd
[i
]!='\0') { i
++; }
572 PrintAndLog("Invalid option");
578 if ((cmd
[i
]>='0' && cmd
[i
]<='9') ||
579 (cmd
[i
]>='a' && cmd
[i
]<='f') ||
580 (cmd
[i
]>='A' && cmd
[i
]<='F') ) {
581 buf
[strlen(buf
)+1]=0;
582 buf
[strlen(buf
)]=cmd
[i
];
585 if (strlen(buf
)>=2) {
586 sscanf(buf
,"%x",&temp
);
587 data
[datalen
]=(uint8_t)(temp
& 0xff);
593 PrintAndLog("Invalid char on input");
598 uint8_t first
, second
;
599 ComputeCrc14443(CRC_14443_A
, data
, datalen
, &first
, &second
);
600 data
[datalen
++] = first
;
601 data
[datalen
++] = second
;
604 if(active
|| active_select
)
606 c
.arg
[0] |= ISO14A_CONNECT
;
608 c
.arg
[0] |= ISO14A_NO_SELECT
;
611 c
.arg
[0] |= ISO14A_NO_DISCONNECT
;
613 c
.arg
[0] |= ISO14A_RAW
;
617 memcpy(c
.d
.asBytes
,data
,datalen
);
630 static void waitCmd(uint8_t iSelect
)
636 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
637 recv
= resp
.d
.asBytes
;
638 uint8_t iLen
= iSelect
? resp
.arg
[1] : resp
.arg
[0];
639 PrintAndLog("received %i octets",iLen
);
642 hexout
= (char *)malloc(iLen
* 3 + 1);
643 if (hexout
!= NULL
) {
644 for (int i
= 0; i
< iLen
; i
++) { // data in hex
645 sprintf(&hexout
[i
* 3], "%02X ", recv
[i
]);
647 PrintAndLog("%s", hexout
);
650 PrintAndLog("malloc failed your client has low memory?");
653 PrintAndLog("timeout while waiting for reply.");
657 static command_t CommandTable
[] =
659 {"help", CmdHelp
, 1, "This help"},
660 {"list", CmdHF14AList
, 0, "List ISO 14443a history"},
661 {"reader", CmdHF14AReader
, 0, "Act like an ISO14443 Type A reader"},
662 {"cuids", CmdHF14ACUIDs
, 0, "<n> Collect n>0 ISO14443 Type A UIDs in one go"},
663 {"sim", CmdHF14ASim
, 0, "<UID> -- Fake ISO 14443a tag"},
664 {"snoop", CmdHF14ASnoop
, 0, "Eavesdrop ISO 14443 Type A"},
665 {"raw", CmdHF14ACmdRaw
, 0, "Send raw hex data to tag"},
666 {NULL
, NULL
, 0, NULL
}
669 int CmdHF14A(const char *Cmd
) {
671 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
674 CmdsParse(CommandTable
, Cmd
);
678 int CmdHelp(const char *Cmd
)
680 CmdsHelp(CommandTable
);