1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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 commands
10 //-----------------------------------------------------------------------------
15 #include "proxmark3.h"
19 #include "iso14443crc.h"
22 #include "cmdparser.h"
28 #include "cmdhflegic.h"
29 #include "cmdhficlass.h"
32 #include "cmdhftopaz.h"
33 #include "protocols.h"
34 #include "emv/cmdemv.h"
35 #include "cmdhflist.h"
37 static int CmdHelp(const char *Cmd
);
39 int CmdHFTune(const char *Cmd
)
41 UsbCommand c
={CMD_MEASURE_ANTENNA_TUNING_HF
};
46 void annotateIclass(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
50 case ICLASS_CMD_ACTALL
: snprintf(exp
,size
,"ACTALL"); break;
51 case ICLASS_CMD_READ_OR_IDENTIFY
:{
53 snprintf(exp
,size
,"READ(%d)",cmd
[1]);
55 snprintf(exp
,size
,"IDENTIFY");
59 case ICLASS_CMD_SELECT
: snprintf(exp
,size
,"SELECT"); break;
60 case ICLASS_CMD_PAGESEL
: snprintf(exp
,size
,"PAGESEL(%d)", cmd
[1]); break;
61 case ICLASS_CMD_READCHECK_KC
:snprintf(exp
,size
,"READCHECK[Kc](%d)", cmd
[1]); break;
62 case ICLASS_CMD_READCHECK_KD
:snprintf(exp
,size
,"READCHECK[Kd](%d)", cmd
[1]); break;
63 case ICLASS_CMD_CHECK
: snprintf(exp
,size
,"CHECK"); break;
64 case ICLASS_CMD_DETECT
: snprintf(exp
,size
,"DETECT"); break;
65 case ICLASS_CMD_HALT
: snprintf(exp
,size
,"HALT"); break;
66 case ICLASS_CMD_UPDATE
: snprintf(exp
,size
,"UPDATE(%d)",cmd
[1]); break;
67 case ICLASS_CMD_ACT
: snprintf(exp
,size
,"ACT"); break;
68 case ICLASS_CMD_READ4
: snprintf(exp
,size
,"READ4(%d)",cmd
[1]); break;
69 default: snprintf(exp
,size
,"?"); break;
74 void annotateIso15693(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
80 case ISO15693_INVENTORY
:snprintf(exp
, size
, "INVENTORY");break;
81 case ISO15693_STAYQUIET
:snprintf(exp
, size
, "STAY_QUIET");break;
82 default: snprintf(exp
,size
,"?"); break;
85 }else if(cmd
[0] == 0x02)
89 case ISO15693_READBLOCK
:snprintf(exp
, size
, "READBLOCK");break;
90 case ISO15693_WRITEBLOCK
:snprintf(exp
, size
, "WRITEBLOCK");break;
91 case ISO15693_LOCKBLOCK
:snprintf(exp
, size
, "LOCKBLOCK");break;
92 case ISO15693_READ_MULTI_BLOCK
:snprintf(exp
, size
, "READ_MULTI_BLOCK");break;
93 case ISO15693_SELECT
:snprintf(exp
, size
, "SELECT");break;
94 case ISO15693_RESET_TO_READY
:snprintf(exp
, size
, "RESET_TO_READY");break;
95 case ISO15693_WRITE_AFI
:snprintf(exp
, size
, "WRITE_AFI");break;
96 case ISO15693_LOCK_AFI
:snprintf(exp
, size
, "LOCK_AFI");break;
97 case ISO15693_WRITE_DSFID
:snprintf(exp
, size
, "WRITE_DSFID");break;
98 case ISO15693_LOCK_DSFID
:snprintf(exp
, size
, "LOCK_DSFID");break;
99 case ISO15693_GET_SYSTEM_INFO
:snprintf(exp
, size
, "GET_SYSTEM_INFO");break;
100 case ISO15693_READ_MULTI_SECSTATUS
:snprintf(exp
, size
, "READ_MULTI_SECSTATUS");break;
101 default: snprintf(exp
,size
,"?"); break;
107 void annotateTopaz(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
110 case TOPAZ_REQA
:snprintf(exp
, size
, "REQA");break;
111 case TOPAZ_WUPA
:snprintf(exp
, size
, "WUPA");break;
112 case TOPAZ_RID
:snprintf(exp
, size
, "RID");break;
113 case TOPAZ_RALL
:snprintf(exp
, size
, "RALL");break;
114 case TOPAZ_READ
:snprintf(exp
, size
, "READ");break;
115 case TOPAZ_WRITE_E
:snprintf(exp
, size
, "WRITE-E");break;
116 case TOPAZ_WRITE_NE
:snprintf(exp
, size
, "WRITE-NE");break;
117 case TOPAZ_RSEG
:snprintf(exp
, size
, "RSEG");break;
118 case TOPAZ_READ8
:snprintf(exp
, size
, "READ8");break;
119 case TOPAZ_WRITE_E8
:snprintf(exp
, size
, "WRITE-E8");break;
120 case TOPAZ_WRITE_NE8
:snprintf(exp
, size
, "WRITE-NE8");break;
121 default: snprintf(exp
,size
,"?"); break;
128 0E xx = SELECT ID (xx = Chip-ID)
130 08 yy = Read Block (yy = block number)
131 09 yy dd dd dd dd = Write Block (yy = block number; dd dd dd dd = data to be written)
132 0C = Reset to Inventory
134 0A 11 22 33 44 55 66 = Authenticate (11 22 33 44 55 66 = data to authenticate)
137 void annotateIso14443b(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
140 case ISO14443B_REQB
: snprintf(exp
,size
,"REQB");break;
141 case ISO14443B_ATTRIB
: snprintf(exp
,size
,"ATTRIB");break;
142 case ISO14443B_HALT
: snprintf(exp
,size
,"HALT");break;
143 case ISO14443B_INITIATE
: snprintf(exp
,size
,"INITIATE");break;
144 case ISO14443B_SELECT
: snprintf(exp
,size
,"SELECT(%d)",cmd
[1]);break;
145 case ISO14443B_GET_UID
: snprintf(exp
,size
,"GET UID");break;
146 case ISO14443B_READ_BLK
: snprintf(exp
,size
,"READ_BLK(%d)", cmd
[1]);break;
147 case ISO14443B_WRITE_BLK
: snprintf(exp
,size
,"WRITE_BLK(%d)",cmd
[1]);break;
148 case ISO14443B_RESET
: snprintf(exp
,size
,"RESET");break;
149 case ISO14443B_COMPLETION
: snprintf(exp
,size
,"COMPLETION");break;
150 case ISO14443B_AUTHENTICATE
: snprintf(exp
,size
,"AUTHENTICATE");break;
151 default : snprintf(exp
,size
,"?");break;
157 * @brief iso14443B_CRC_check Checks CRC in command or response
161 * @return 0 : CRC-command, CRC not ok
162 * 1 : CRC-command, CRC ok
163 * 2 : Not crc-command
166 uint8_t iso14443B_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
170 if(len
<= 2) return 2;
172 ComputeCrc14443(CRC_14443_B
, data
, len
-2, &b1
, &b2
);
173 if(b1
!= data
[len
-2] || b2
!= data
[len
-1]) {
181 * @brief iclass_CRC_Ok Checks CRC in command or response
185 * @return 0 : CRC-command, CRC not ok
186 * 1 : CRC-command, CRC ok
187 * 2 : Not crc-command
189 uint8_t iclass_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
191 if(len
< 4) return 2;//CRC commands (and responses) are all at least 4 bytes
195 if(!isResponse
)//Commands to tag
198 These commands should have CRC. Total length leftmost
201 12 UPDATE - unsecured, ends with CRC16
202 14 UPDATE - secured, ends with signature instead
205 if(len
== 4 || len
== 12)//Covers three of them
207 //Don't include the command byte
208 ComputeCrc14443(CRC_ICLASS
, (data
+1), len
-3, &b1
, &b2
);
209 return b1
== data
[len
-2] && b2
== data
[len
-1];
214 These tag responses should have CRC. Total length leftmost
216 10 READ data[8] crc[2]
217 34 READ4 data[32]crc[2]
218 10 UPDATE data[8] crc[2]
219 10 SELECT csn[8] crc[2]
220 10 IDENTIFY asnb[8] crc[2]
221 10 PAGESEL block1[8] crc[2]
222 10 DETECT csn[8] crc[2]
226 4 CHECK chip_response[4]
231 In conclusion, without looking at the command; any response
232 of length 10 or 34 should have CRC
234 if(len
!= 10 && len
!= 34) return true;
236 ComputeCrc14443(CRC_ICLASS
, data
, len
-2, &b1
, &b2
);
237 return b1
== data
[len
-2] && b2
== data
[len
-1];
242 bool is_last_record(uint16_t tracepos
, uint8_t *trace
, uint16_t traceLen
)
244 return(tracepos
+ sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) >= traceLen
);
248 bool next_record_is_response(uint16_t tracepos
, uint8_t *trace
)
250 uint16_t next_records_datalen
= *((uint16_t *)(trace
+ tracepos
+ sizeof(uint32_t) + sizeof(uint16_t)));
252 return(next_records_datalen
& 0x8000);
256 bool merge_topaz_reader_frames(uint32_t timestamp
, uint32_t *duration
, uint16_t *tracepos
, uint16_t traceLen
, uint8_t *trace
, uint8_t *frame
, uint8_t *topaz_reader_command
, uint16_t *data_len
)
259 #define MAX_TOPAZ_READER_CMD_LEN 16
261 uint32_t last_timestamp
= timestamp
+ *duration
;
263 if ((*data_len
!= 1) || (frame
[0] == TOPAZ_WUPA
) || (frame
[0] == TOPAZ_REQA
)) return false;
265 memcpy(topaz_reader_command
, frame
, *data_len
);
267 while (!is_last_record(*tracepos
, trace
, traceLen
) && !next_record_is_response(*tracepos
, trace
)) {
268 uint32_t next_timestamp
= *((uint32_t *)(trace
+ *tracepos
));
269 *tracepos
+= sizeof(uint32_t);
270 uint16_t next_duration
= *((uint16_t *)(trace
+ *tracepos
));
271 *tracepos
+= sizeof(uint16_t);
272 uint16_t next_data_len
= *((uint16_t *)(trace
+ *tracepos
)) & 0x7FFF;
273 *tracepos
+= sizeof(uint16_t);
274 uint8_t *next_frame
= (trace
+ *tracepos
);
275 *tracepos
+= next_data_len
;
276 if ((next_data_len
== 1) && (*data_len
+ next_data_len
<= MAX_TOPAZ_READER_CMD_LEN
)) {
277 memcpy(topaz_reader_command
+ *data_len
, next_frame
, next_data_len
);
278 *data_len
+= next_data_len
;
279 last_timestamp
= next_timestamp
+ next_duration
;
282 *tracepos
= *tracepos
- next_data_len
- sizeof(uint16_t) - sizeof(uint16_t) - sizeof(uint32_t);
285 uint16_t next_parity_len
= (next_data_len
-1)/8 + 1;
286 *tracepos
+= next_parity_len
;
289 *duration
= last_timestamp
- timestamp
;
295 uint16_t printTraceLine(uint16_t tracepos
, uint16_t traceLen
, uint8_t *trace
, uint8_t protocol
, bool showWaitCycles
, bool markCRCBytes
)
298 uint16_t data_len
, parity_len
;
300 uint8_t topaz_reader_command
[9];
301 uint32_t timestamp
, first_timestamp
, EndOfTransmissionTimestamp
;
302 char explanation
[30] = {0};
303 uint8_t mfData
[32] = {0};
304 size_t mfDataLen
= 0;
306 if (tracepos
+ sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen
) return traceLen
;
308 first_timestamp
= *((uint32_t *)(trace
));
309 timestamp
= *((uint32_t *)(trace
+ tracepos
));
312 duration
= *((uint16_t *)(trace
+ tracepos
));
314 data_len
= *((uint16_t *)(trace
+ tracepos
));
317 if (data_len
& 0x8000) {
323 parity_len
= (data_len
-1)/8 + 1;
325 if (tracepos
+ data_len
+ parity_len
> traceLen
) {
328 uint8_t *frame
= trace
+ tracepos
;
329 tracepos
+= data_len
;
330 uint8_t *parityBytes
= trace
+ tracepos
;
331 tracepos
+= parity_len
;
333 if (protocol
== TOPAZ
&& !isResponse
) {
334 // topaz reader commands come in 1 or 9 separate frames with 7 or 8 Bits each.
336 if (merge_topaz_reader_frames(timestamp
, &duration
, &tracepos
, traceLen
, trace
, frame
, topaz_reader_command
, &data_len
)) {
337 frame
= topaz_reader_command
;
341 //Check the CRC status
342 uint8_t crcStatus
= 2;
347 crcStatus
= iclass_CRC_check(isResponse
, frame
, data_len
);
351 crcStatus
= iso14443B_CRC_check(isResponse
, frame
, data_len
);
354 crcStatus
= mifare_CRC_check(isResponse
, frame
, data_len
);
357 crcStatus
= iso14443A_CRC_check(isResponse
, frame
, data_len
);
363 //0 CRC-command, CRC not ok
364 //1 CRC-command, CRC ok
367 //--- Draw the data column
368 //char line[16][110];
371 for (int j
= 0; j
< data_len
&& j
/16 < 16; j
++) {
373 uint8_t parityBits
= parityBytes
[j
>>3];
374 if (protocol
!= ISO_14443B
&& (isResponse
|| protocol
== ISO_14443A
) && (oddparity8(frame
[j
]) != ((parityBits
>> (7-(j
&0x0007))) & 0x01))) {
375 snprintf(line
[j
/16]+(( j
% 16) * 4),110, "%02x! ", frame
[j
]);
377 snprintf(line
[j
/16]+(( j
% 16) * 4), 110, " %02x ", frame
[j
]);
383 if(crcStatus
== 0 || crcStatus
== 1)
385 char *pos1
= line
[(data_len
-2)/16]+(((data_len
-2) % 16) * 4);
387 char *pos2
= line
[(data_len
)/16]+(((data_len
) % 16) * 4);
388 sprintf(pos2
, "%c", ']');
395 sprintf(line
[0],"<empty trace - possible error>");
398 //--- Draw the CRC column
400 char *crc
= (crcStatus
== 0 ? "!crc" : (crcStatus
== 1 ? " ok " : " "));
402 EndOfTransmissionTimestamp
= timestamp
+ duration
;
404 if (protocol
== PROTO_MIFARE
)
405 annotateMifare(explanation
, sizeof(explanation
), frame
, data_len
, parityBytes
, parity_len
, isResponse
);
410 case ICLASS
: annotateIclass(explanation
,sizeof(explanation
),frame
,data_len
); break;
411 case ISO_14443A
: annotateIso14443a(explanation
,sizeof(explanation
),frame
,data_len
); break;
412 case ISO_14443B
: annotateIso14443b(explanation
,sizeof(explanation
),frame
,data_len
); break;
413 case TOPAZ
: annotateTopaz(explanation
,sizeof(explanation
),frame
,data_len
); break;
418 int num_lines
= MIN((data_len
- 1)/16 + 1, 16);
419 for (int j
= 0; j
< num_lines
; j
++) {
421 PrintAndLog(" %10d | %10d | %s |%-64s | %s| %s",
422 (timestamp
- first_timestamp
),
423 (EndOfTransmissionTimestamp
- first_timestamp
),
424 (isResponse
? "Tag" : "Rdr"),
426 (j
== num_lines
-1) ? crc
: " ",
427 (j
== num_lines
-1) ? explanation
: "");
429 PrintAndLog(" | | |%-64s | %s| %s",
431 (j
== num_lines
-1) ? crc
: " ",
432 (j
== num_lines
-1) ? explanation
: "");
436 if (DecodeMifareData(frame
, data_len
, isResponse
, mfData
, &mfDataLen
)) {
437 PrintAndLog(" | | |%-64s | %s| %s",
438 sprint_hex(mfData
, mfDataLen
),
440 (false) ? explanation
: "");
443 if (is_last_record(tracepos
, trace
, traceLen
)) return traceLen
;
445 if (showWaitCycles
&& !isResponse
&& next_record_is_response(tracepos
, trace
)) {
446 uint32_t next_timestamp
= *((uint32_t *)(trace
+ tracepos
));
447 PrintAndLog(" %10d | %10d | %s | fdt (Frame Delay Time): %d",
448 (EndOfTransmissionTimestamp
- first_timestamp
),
449 (next_timestamp
- first_timestamp
),
451 (next_timestamp
- EndOfTransmissionTimestamp
));
458 int CmdHFList(const char *Cmd
)
460 bool showWaitCycles
= false;
461 bool markCRCBytes
= false;
463 int tlen
= param_getstr(Cmd
,0,type
, sizeof(type
));
464 char param1
= param_getchar(Cmd
, 1);
465 char param2
= param_getchar(Cmd
, 2);
467 uint8_t protocol
= 0;
475 || (param1
!= 0 && param1
!= 'f' && param1
!= 'c')
476 || (param2
!= 0 && param2
!= 'f' && param2
!= 'c')) {
481 if(strcmp(type
, "iclass") == 0) {
483 } else if(strcmp(type
, "mf") == 0) {
484 protocol
= PROTO_MIFARE
;
485 } else if(strcmp(type
, "14a") == 0) {
486 protocol
= ISO_14443A
;
487 } else if(strcmp(type
, "14b") == 0) {
488 protocol
= ISO_14443B
;
489 } else if(strcmp(type
,"topaz")== 0) {
491 } else if(strcmp(type
,"raw")== 0) {
492 protocol
= -1;//No crc, no annotations
499 PrintAndLog("List protocol data in trace buffer.");
500 PrintAndLog("Usage: hf list <protocol> [f][c]");
501 PrintAndLog(" f - show frame delay times as well");
502 PrintAndLog(" c - mark CRC bytes");
503 PrintAndLog("Supported <protocol> values:");
504 PrintAndLog(" raw - just show raw data without annotations");
505 PrintAndLog(" 14a - interpret data as iso14443a communications");
506 PrintAndLog(" mf - interpret data as iso14443a communications and decrypt crypto1 stream");
507 PrintAndLog(" 14b - interpret data as iso14443b communications");
508 PrintAndLog(" iclass - interpret data as iclass communications");
509 PrintAndLog(" topaz - interpret data as topaz communications");
511 PrintAndLog("example: hf list 14a f");
512 PrintAndLog("example: hf list iclass");
517 if (param1
== 'f' || param2
== 'f') {
518 showWaitCycles
= true;
521 if (param1
== 'c' || param2
== 'c') {
526 uint16_t tracepos
= 0;
527 trace
= malloc(USB_CMD_DATA_SIZE
);
529 // Query for the size of the trace
531 GetFromBigBuf(trace
, USB_CMD_DATA_SIZE
, 0);
532 WaitForResponse(CMD_ACK
, &response
);
533 uint16_t traceLen
= response
.arg
[2];
534 if (traceLen
> USB_CMD_DATA_SIZE
) {
535 uint8_t *p
= realloc(trace
, traceLen
);
537 PrintAndLog("Cannot allocate memory for trace");
542 GetFromBigBuf(trace
, traceLen
, 0);
543 WaitForResponse(CMD_ACK
, NULL
);
546 PrintAndLog("Recorded Activity (TraceLen = %d bytes)", traceLen
);
548 PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
549 PrintAndLog("iso14443a - All times are in carrier periods (1/13.56Mhz)");
550 PrintAndLog("iClass - Timings are not as accurate");
552 PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |");
553 PrintAndLog("------------|------------|-----|-----------------------------------------------------------------|-----|--------------------|");
556 while(tracepos
< traceLen
)
558 tracepos
= printTraceLine(tracepos
, traceLen
, trace
, protocol
, showWaitCycles
, markCRCBytes
);
565 int CmdHFSearch(const char *Cmd
){
568 ans
= CmdHF14AInfo("s");
570 PrintAndLog("\nValid ISO14443A Tag Found - Quiting Search\n");
573 ans
= HFiClassReader("", false, false);
575 PrintAndLog("\nValid iClass Tag (or PicoPass Tag) Found - Quiting Search\n");
578 ans
= HF15Reader("", false);
580 PrintAndLog("\nValid ISO15693 Tag Found - Quiting Search\n");
583 //14b is longest test currently (and rarest chip type) ... put last
584 ans
= HF14BInfo(false);
586 PrintAndLog("\nValid ISO14443B Tag Found - Quiting Search\n");
589 PrintAndLog("\nno known/supported 13.56 MHz tags found\n");
593 int CmdHFSnoop(const char *Cmd
)
596 UsbCommand c
= {CMD_HF_SNIFFER
, {strtol(Cmd
, &pEnd
,0),strtol(pEnd
, &pEnd
,0),0}};
601 static command_t CommandTable
[] =
603 {"help", CmdHelp
, 1, "This help"},
604 {"14a", CmdHF14A
, 1, "{ ISO14443A RFIDs... }"},
605 {"14b", CmdHF14B
, 1, "{ ISO14443B RFIDs... }"},
606 {"15", CmdHF15
, 1, "{ ISO15693 RFIDs... }"},
607 {"epa", CmdHFEPA
, 1, "{ German Identification Card... }"},
608 {"emv", CmdHFEMV
, 1, "{ EMV cards... }"},
609 {"legic", CmdHFLegic
, 0, "{ LEGIC RFIDs... }"},
610 {"iclass", CmdHFiClass
, 1, "{ ICLASS RFIDs... }"},
611 {"mf", CmdHFMF
, 1, "{ MIFARE RFIDs... }"},
612 {"mfu", CmdHFMFUltra
, 1, "{ MIFARE Ultralight RFIDs... }"},
613 {"topaz", CmdHFTopaz
, 1, "{ TOPAZ (NFC Type 1) RFIDs... }"},
614 {"tune", CmdHFTune
, 0, "Continuously measure HF antenna tuning"},
615 {"list", CmdHFList
, 1, "List protocol data in trace buffer"},
616 {"search", CmdHFSearch
, 1, "Search for known HF tags [preliminary]"},
617 {"snoop", CmdHFSnoop
, 0, "<samples to skip (10000)> <triggers to skip (1)> Generic HF Snoop"},
618 {NULL
, NULL
, 0, NULL
}
621 int CmdHF(const char *Cmd
)
623 CmdsParse(CommandTable
, Cmd
);
627 int CmdHelp(const char *Cmd
)
629 CmdsHelp(CommandTable
);