1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // High frequency commands
9 //-----------------------------------------------------------------------------
13 #include "proxmark3.h"
16 #include "cmdparser.h"
22 #include "cmdhflegic.h"
23 #include "cmdhficlass.h"
26 #include "cmdhftopaz.h"
27 #include "protocols.h"
29 static int CmdHelp(const char *Cmd
);
31 int CmdHFTune(const char *Cmd
)
33 UsbCommand c
={CMD_MEASURE_ANTENNA_TUNING_HF
};
39 void annotateIso14443a(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
43 case ISO14443A_CMD_WUPA
: snprintf(exp
,size
,"WUPA"); break;
44 case ISO14443A_CMD_ANTICOLL_OR_SELECT
:{
45 // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor)
46 // 93 70 = Select (usage: 9370+5bytes 9320 answer - answer: 1byte SAK)
49 snprintf(exp
,size
,"SELECT_UID"); break;
52 snprintf(exp
,size
,"ANTICOLL"); break;
55 case ISO14443A_CMD_ANTICOLL_OR_SELECT_2
:{
56 //95 20 = Anticollision of cascade level2
57 //95 70 = Select of cascade level2
60 snprintf(exp
,size
,"SELECT_UID-2"); break;
63 snprintf(exp
,size
,"ANTICOLL-2"); break;
66 case ISO14443A_CMD_REQA
: snprintf(exp
,size
,"REQA"); break;
67 case ISO14443A_CMD_READBLOCK
: snprintf(exp
,size
,"READBLOCK(%d)",cmd
[1]); break;
68 case ISO14443A_CMD_WRITEBLOCK
: snprintf(exp
,size
,"WRITEBLOCK(%d)",cmd
[1]); break;
69 case ISO14443A_CMD_HALT
: snprintf(exp
,size
,"HALT"); break;
70 case ISO14443A_CMD_RATS
: snprintf(exp
,size
,"RATS"); break;
71 case MIFARE_CMD_INC
: snprintf(exp
,size
,"INC(%d)",cmd
[1]); break;
72 case MIFARE_CMD_DEC
: snprintf(exp
,size
,"DEC(%d)",cmd
[1]); break;
73 case MIFARE_CMD_RESTORE
: snprintf(exp
,size
,"RESTORE(%d)",cmd
[1]); break;
74 case MIFARE_CMD_TRANSFER
: snprintf(exp
,size
,"TRANSFER(%d)",cmd
[1]); break;
75 case MIFARE_AUTH_KEYA
: snprintf(exp
,size
,"AUTH-A(%d)",cmd
[1]); break;
76 case MIFARE_AUTH_KEYB
: snprintf(exp
,size
,"AUTH-B(%d)",cmd
[1]); break;
77 case MIFARE_MAGICWUPC1
: snprintf(exp
,size
,"MAGIC WUPC1"); break;
78 case MIFARE_MAGICWUPC2
: snprintf(exp
,size
,"MAGIC WUPC2"); break;
79 case MIFARE_MAGICWIPEC
: snprintf(exp
,size
,"MAGIC WIPEC"); break;
80 default: snprintf(exp
,size
,"?"); break;
85 void annotateIclass(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
89 case ICLASS_CMD_ACTALL
: snprintf(exp
,size
,"ACTALL"); break;
90 case ICLASS_CMD_READ_OR_IDENTIFY
:{
92 snprintf(exp
,size
,"READ(%d)",cmd
[1]);
94 snprintf(exp
,size
,"IDENTIFY");
98 case ICLASS_CMD_SELECT
: snprintf(exp
,size
,"SELECT"); break;
99 case ICLASS_CMD_PAGESEL
: snprintf(exp
,size
,"PAGESEL(%d)", cmd
[1]); break;
100 case ICLASS_CMD_READCHECK_KC
:snprintf(exp
,size
,"READCHECK[Kc](%d)", cmd
[1]); break;
101 case ICLASS_CMD_READCHECK_KD
:snprintf(exp
,size
,"READCHECK[Kd](%d)", cmd
[1]); break;
102 case ICLASS_CMD_CHECK
: snprintf(exp
,size
,"CHECK"); break;
103 case ICLASS_CMD_DETECT
: snprintf(exp
,size
,"DETECT"); break;
104 case ICLASS_CMD_HALT
: snprintf(exp
,size
,"HALT"); break;
105 case ICLASS_CMD_UPDATE
: snprintf(exp
,size
,"UPDATE(%d)",cmd
[1]); break;
106 case ICLASS_CMD_ACT
: snprintf(exp
,size
,"ACT"); break;
107 case ICLASS_CMD_READ4
: snprintf(exp
,size
,"READ4(%d)",cmd
[1]); break;
108 default: snprintf(exp
,size
,"?"); break;
113 void annotateIso15693(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
119 case ISO15693_INVENTORY
:snprintf(exp
, size
, "INVENTORY");break;
120 case ISO15693_STAYQUIET
:snprintf(exp
, size
, "STAY_QUIET");break;
121 default: snprintf(exp
,size
,"?"); break;
124 }else if(cmd
[0] == 0x02)
128 case ISO15693_READBLOCK
:snprintf(exp
, size
, "READBLOCK");break;
129 case ISO15693_WRITEBLOCK
:snprintf(exp
, size
, "WRITEBLOCK");break;
130 case ISO15693_LOCKBLOCK
:snprintf(exp
, size
, "LOCKBLOCK");break;
131 case ISO15693_READ_MULTI_BLOCK
:snprintf(exp
, size
, "READ_MULTI_BLOCK");break;
132 case ISO15693_SELECT
:snprintf(exp
, size
, "SELECT");break;
133 case ISO15693_RESET_TO_READY
:snprintf(exp
, size
, "RESET_TO_READY");break;
134 case ISO15693_WRITE_AFI
:snprintf(exp
, size
, "WRITE_AFI");break;
135 case ISO15693_LOCK_AFI
:snprintf(exp
, size
, "LOCK_AFI");break;
136 case ISO15693_WRITE_DSFID
:snprintf(exp
, size
, "WRITE_DSFID");break;
137 case ISO15693_LOCK_DSFID
:snprintf(exp
, size
, "LOCK_DSFID");break;
138 case ISO15693_GET_SYSTEM_INFO
:snprintf(exp
, size
, "GET_SYSTEM_INFO");break;
139 case ISO15693_READ_MULTI_SECSTATUS
:snprintf(exp
, size
, "READ_MULTI_SECSTATUS");break;
140 default: snprintf(exp
,size
,"?"); break;
146 void annotateTopaz(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
150 case TOPAZ_REQA
:snprintf(exp
, size
, "REQA");break;
151 case TOPAZ_WUPA
:snprintf(exp
, size
, "WUPA");break;
152 case TOPAZ_RID
:snprintf(exp
, size
, "RID");break;
153 case TOPAZ_RALL
:snprintf(exp
, size
, "RALL");break;
154 case TOPAZ_READ
:snprintf(exp
, size
, "READ");break;
155 case TOPAZ_WRITE_E
:snprintf(exp
, size
, "WRITE-E");break;
156 case TOPAZ_WRITE_NE
:snprintf(exp
, size
, "WRITE-NE");break;
157 default: snprintf(exp
,size
,"?"); break;
164 0E xx = SELECT ID (xx = Chip-ID)
166 08 yy = Read Block (yy = block number)
167 09 yy dd dd dd dd = Write Block (yy = block number; dd dd dd dd = data to be written)
168 0C = Reset to Inventory
170 0A 11 22 33 44 55 66 = Authenticate (11 22 33 44 55 66 = data to authenticate)
173 void annotateIso14443b(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
176 case ISO14443B_REQB
: snprintf(exp
,size
,"REQB");break;
177 case ISO14443B_ATTRIB
: snprintf(exp
,size
,"ATTRIB");break;
178 case ISO14443B_HALT
: snprintf(exp
,size
,"HALT");break;
179 case ISO14443B_INITIATE
: snprintf(exp
,size
,"INITIATE");break;
180 case ISO14443B_SELECT
: snprintf(exp
,size
,"SELECT(%d)",cmd
[1]);break;
181 case ISO14443B_GET_UID
: snprintf(exp
,size
,"GET UID");break;
182 case ISO14443B_READ_BLK
: snprintf(exp
,size
,"READ_BLK(%d)", cmd
[1]);break;
183 case ISO14443B_WRITE_BLK
: snprintf(exp
,size
,"WRITE_BLK(%d)",cmd
[1]);break;
184 case ISO14443B_RESET
: snprintf(exp
,size
,"RESET");break;
185 case ISO14443B_COMPLETION
: snprintf(exp
,size
,"COMPLETION");break;
186 case ISO14443B_AUTHENTICATE
: snprintf(exp
,size
,"AUTHENTICATE");break;
187 default : snprintf(exp
,size
,"?");break;
193 * @brief iso14443A_CRC_check Checks CRC in command or response
197 * @return 0 : CRC-command, CRC not ok
198 * 1 : CRC-command, CRC ok
199 * 2 : Not crc-command
202 uint8_t iso14443A_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
206 if(len
<= 2) return 2;
208 if(isResponse
& (len
< 6)) return 2;
210 ComputeCrc14443(CRC_14443_A
, data
, len
-2, &b1
, &b2
);
211 if (b1
!= data
[len
-2] || b2
!= data
[len
-1]) {
220 * @brief iso14443B_CRC_check Checks CRC in command or response
224 * @return 0 : CRC-command, CRC not ok
225 * 1 : CRC-command, CRC ok
226 * 2 : Not crc-command
229 uint8_t iso14443B_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
233 if(len
<= 2) return 2;
235 ComputeCrc14443(CRC_14443_B
, data
, len
-2, &b1
, &b2
);
236 if(b1
!= data
[len
-2] || b2
!= data
[len
-1]) {
244 * @brief iclass_CRC_Ok Checks CRC in command or response
248 * @return 0 : CRC-command, CRC not ok
249 * 1 : CRC-command, CRC ok
250 * 2 : Not crc-command
252 uint8_t iclass_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
254 if(len
< 4) return 2;//CRC commands (and responses) are all at least 4 bytes
258 if(!isResponse
)//Commands to tag
261 These commands should have CRC. Total length leftmost
264 12 UPDATE - unsecured, ends with CRC16
265 14 UPDATE - secured, ends with signature instead
268 if(len
== 4 || len
== 12)//Covers three of them
270 //Don't include the command byte
271 ComputeCrc14443(CRC_ICLASS
, (data
+1), len
-3, &b1
, &b2
);
272 return b1
== data
[len
-2] && b2
== data
[len
-1];
277 These tag responses should have CRC. Total length leftmost
279 10 READ data[8] crc[2]
280 34 READ4 data[32]crc[2]
281 10 UPDATE data[8] crc[2]
282 10 SELECT csn[8] crc[2]
283 10 IDENTIFY asnb[8] crc[2]
284 10 PAGESEL block1[8] crc[2]
285 10 DETECT csn[8] crc[2]
289 4 CHECK chip_response[4]
294 In conclusion, without looking at the command; any response
295 of length 10 or 34 should have CRC
297 if(len
!= 10 && len
!= 34) return true;
299 ComputeCrc14443(CRC_ICLASS
, data
, len
-2, &b1
, &b2
);
300 return b1
== data
[len
-2] && b2
== data
[len
-1];
305 bool is_last_record(uint16_t tracepos
, uint8_t *trace
, uint16_t traceLen
)
307 return(tracepos
+ sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) >= traceLen
);
311 bool next_record_is_response(uint16_t tracepos
, uint8_t *trace
)
313 uint16_t next_records_datalen
= *((uint16_t *)(trace
+ tracepos
+ sizeof(uint32_t) + sizeof(uint16_t)));
315 return(next_records_datalen
& 0x8000);
319 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
)
322 #define MAX_TOPAZ_READER_CMD_LEN 9
324 uint32_t last_timestamp
= timestamp
+ *duration
;
326 if ((*data_len
!= 1) || (frame
[0] == TOPAZ_WUPA
) || (frame
[0] == TOPAZ_REQA
)) return false;
328 memcpy(topaz_reader_command
, frame
, *data_len
);
330 while (!is_last_record(*tracepos
, trace
, traceLen
) && !next_record_is_response(*tracepos
, trace
)) {
331 uint32_t next_timestamp
= *((uint32_t *)(trace
+ *tracepos
));
332 *tracepos
+= sizeof(uint32_t);
333 uint16_t next_duration
= *((uint16_t *)(trace
+ *tracepos
));
334 *tracepos
+= sizeof(uint16_t);
335 uint16_t next_data_len
= *((uint16_t *)(trace
+ *tracepos
)) & 0x7FFF;
336 *tracepos
+= sizeof(uint16_t);
337 uint8_t *next_frame
= (trace
+ *tracepos
);
338 *tracepos
+= next_data_len
;
339 if ((next_data_len
== 1) && (*data_len
+ next_data_len
<= MAX_TOPAZ_READER_CMD_LEN
)) {
340 memcpy(topaz_reader_command
+ *data_len
, next_frame
, next_data_len
);
341 *data_len
+= next_data_len
;
342 last_timestamp
= next_timestamp
+ next_duration
;
345 *tracepos
= *tracepos
- next_data_len
- sizeof(uint16_t) - sizeof(uint16_t) - sizeof(uint32_t);
348 uint16_t next_parity_len
= (next_data_len
-1)/8 + 1;
349 *tracepos
+= next_parity_len
;
352 *duration
= last_timestamp
- timestamp
;
358 uint16_t printTraceLine(uint16_t tracepos
, uint16_t traceLen
, uint8_t *trace
, uint8_t protocol
, bool showWaitCycles
, bool markCRCBytes
)
361 uint16_t data_len
, parity_len
;
363 uint8_t topaz_reader_command
[9];
364 uint32_t timestamp
, first_timestamp
, EndOfTransmissionTimestamp
;
365 char explanation
[30] = {0};
367 if (tracepos
+ sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen
) return traceLen
;
369 first_timestamp
= *((uint32_t *)(trace
));
370 timestamp
= *((uint32_t *)(trace
+ tracepos
));
373 duration
= *((uint16_t *)(trace
+ tracepos
));
375 data_len
= *((uint16_t *)(trace
+ tracepos
));
378 if (data_len
& 0x8000) {
384 parity_len
= (data_len
-1)/8 + 1;
386 if (tracepos
+ data_len
+ parity_len
> traceLen
) {
389 uint8_t *frame
= trace
+ tracepos
;
390 tracepos
+= data_len
;
391 uint8_t *parityBytes
= trace
+ tracepos
;
392 tracepos
+= parity_len
;
394 if (protocol
== TOPAZ
&& !isResponse
) {
395 // topaz reader commands come in 1 or 9 separate frames with 7 or 8 Bits each.
397 if (merge_topaz_reader_frames(timestamp
, &duration
, &tracepos
, traceLen
, trace
, frame
, topaz_reader_command
, &data_len
)) {
398 frame
= topaz_reader_command
;
402 //Check the CRC status
403 uint8_t crcStatus
= 2;
408 crcStatus
= iclass_CRC_check(isResponse
, frame
, data_len
);
412 crcStatus
= iso14443B_CRC_check(isResponse
, frame
, data_len
);
415 crcStatus
= iso14443A_CRC_check(isResponse
, frame
, data_len
);
421 //0 CRC-command, CRC not ok
422 //1 CRC-command, CRC ok
425 //--- Draw the data column
426 //char line[16][110];
429 for (int j
= 0; j
< data_len
&& j
/16 < 16; j
++) {
431 int oddparity
= 0x01;
434 for (k
=0 ; k
<8 ; k
++) {
435 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
437 uint8_t parityBits
= parityBytes
[j
>>3];
438 if (isResponse
&& (oddparity
!= ((parityBits
>> (7-(j
&0x0007))) & 0x01))) {
439 snprintf(line
[j
/16]+(( j
% 16) * 4), 110, " %02x!", frame
[j
]);
441 snprintf(line
[j
/16]+(( j
% 16) * 4), 110, " %02x ", frame
[j
]);
447 if(crcStatus
== 0 || crcStatus
== 1)
449 char *pos1
= line
[(data_len
-2)/16]+(((data_len
-2) % 16) * 4);
451 char *pos2
= line
[(data_len
)/16]+(((data_len
) % 16) * 4);
452 sprintf(pos2
, "%c", ']');
459 sprintf(line
[0],"<empty trace - possible error>");
462 //--- Draw the CRC column
464 char *crc
= (crcStatus
== 0 ? "!crc" : (crcStatus
== 1 ? " ok " : " "));
466 EndOfTransmissionTimestamp
= timestamp
+ duration
;
471 case ICLASS
: annotateIclass(explanation
,sizeof(explanation
),frame
,data_len
); break;
472 case ISO_14443A
: annotateIso14443a(explanation
,sizeof(explanation
),frame
,data_len
); break;
473 case ISO_14443B
: annotateIso14443b(explanation
,sizeof(explanation
),frame
,data_len
); break;
474 case TOPAZ
: annotateTopaz(explanation
,sizeof(explanation
),frame
,data_len
); break;
479 int num_lines
= MIN((data_len
- 1)/16 + 1, 16);
480 for (int j
= 0; j
< num_lines
; j
++) {
482 PrintAndLog(" %9d | %9d | %s |%-64s | %s| %s",
483 (timestamp
- first_timestamp
),
484 (EndOfTransmissionTimestamp
- first_timestamp
),
485 (isResponse
? "Tag" : "Rdr"),
487 (j
== num_lines
-1) ? crc
: " ",
488 (j
== num_lines
-1) ? explanation
: "");
490 PrintAndLog(" | | |%-64s | %s| %s",
492 (j
== num_lines
-1) ? crc
: " ",
493 (j
== num_lines
-1) ? explanation
: "");
497 if (is_last_record(tracepos
, trace
, traceLen
)) return traceLen
;
499 if (showWaitCycles
&& !isResponse
&& next_record_is_response(tracepos
, trace
)) {
500 uint32_t next_timestamp
= *((uint32_t *)(trace
+ tracepos
));
501 PrintAndLog(" %9d | %9d | %s | fdt (Frame Delay Time): %d",
502 (EndOfTransmissionTimestamp
- first_timestamp
),
503 (next_timestamp
- first_timestamp
),
505 (next_timestamp
- EndOfTransmissionTimestamp
));
512 int CmdHFList(const char *Cmd
)
514 bool showWaitCycles
= false;
515 bool markCRCBytes
= false;
517 int tlen
= param_getstr(Cmd
,0,type
);
518 char param1
= param_getchar(Cmd
, 1);
519 char param2
= param_getchar(Cmd
, 2);
521 uint8_t protocol
= 0;
529 || (param1
!= 0 && param1
!= 'f' && param1
!= 'c')
530 || (param2
!= 0 && param2
!= 'f' && param2
!= 'c')) {
535 if(strcmp(type
, "iclass") == 0) {
537 } else if(strcmp(type
, "14a") == 0) {
538 protocol
= ISO_14443A
;
539 } else if(strcmp(type
, "14b") == 0) {
540 protocol
= ISO_14443B
;
541 } else if(strcmp(type
,"topaz")== 0) {
543 } else if(strcmp(type
,"raw")== 0) {
544 protocol
= -1;//No crc, no annotations
551 PrintAndLog("List protocol data in trace buffer.");
552 PrintAndLog("Usage: hf list <protocol> [f][c]");
553 PrintAndLog(" f - show frame delay times as well");
554 PrintAndLog(" c - mark CRC bytes");
555 PrintAndLog("Supported <protocol> values:");
556 PrintAndLog(" raw - just show raw data without annotations");
557 PrintAndLog(" 14a - interpret data as iso14443a communications");
558 PrintAndLog(" 14b - interpret data as iso14443b communications");
559 PrintAndLog(" iclass - interpret data as iclass communications");
560 PrintAndLog(" topaz - interpret data as topaz communications");
562 PrintAndLog("example: hf list 14a f");
563 PrintAndLog("example: hf list iclass");
568 if (param1
== 'f' || param2
== 'f') {
569 showWaitCycles
= true;
572 if (param1
== 'c' || param2
== 'c') {
577 uint16_t tracepos
= 0;
578 trace
= malloc(USB_CMD_DATA_SIZE
);
580 // Query for the size of the trace
582 GetFromBigBuf(trace
, USB_CMD_DATA_SIZE
, 0);
583 WaitForResponse(CMD_ACK
, &response
);
584 uint16_t traceLen
= response
.arg
[2];
585 if (traceLen
> USB_CMD_DATA_SIZE
) {
586 uint8_t *p
= realloc(trace
, traceLen
);
588 PrintAndLog("Cannot allocate memory for trace");
593 GetFromBigBuf(trace
, traceLen
, 0);
594 WaitForResponse(CMD_ACK
, NULL
);
597 PrintAndLog("Recorded Activity (TraceLen = %d bytes)", traceLen
);
599 PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
600 PrintAndLog("iso14443a - All times are in carrier periods (1/13.56Mhz)");
601 PrintAndLog("iClass - Timings are not as accurate");
603 PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |");
604 PrintAndLog("-----------|-----------|-----|-----------------------------------------------------------------|-----|--------------------|");
606 while(tracepos
< traceLen
)
608 tracepos
= printTraceLine(tracepos
, traceLen
, trace
, protocol
, showWaitCycles
, markCRCBytes
);
616 static command_t CommandTable
[] =
618 {"help", CmdHelp
, 1, "This help"},
619 {"14a", CmdHF14A
, 1, "{ ISO14443A RFIDs... }"},
620 {"14b", CmdHF14B
, 1, "{ ISO14443B RFIDs... }"},
621 {"15", CmdHF15
, 1, "{ ISO15693 RFIDs... }"},
622 {"epa", CmdHFEPA
, 1, "{ German Identification Card... }"},
623 {"legic", CmdHFLegic
, 0, "{ LEGIC RFIDs... }"},
624 {"iclass", CmdHFiClass
, 1, "{ ICLASS RFIDs... }"},
625 {"mf", CmdHFMF
, 1, "{ MIFARE RFIDs... }"},
626 {"mfu", CmdHFMFUltra
, 1, "{ MIFARE Ultralight RFIDs... }"},
627 {"topaz", CmdHFTopaz
, 1, "{ TOPAZ (NFC Type 1) RFIDs... }"},
628 {"tune", CmdHFTune
, 0, "Continuously measure HF antenna tuning"},
629 {"list", CmdHFList
, 1, "List protocol data in trace buffer"},
630 {NULL
, NULL
, 0, NULL
}
633 int CmdHF(const char *Cmd
)
635 CmdsParse(CommandTable
, Cmd
);
639 int CmdHelp(const char *Cmd
)
641 CmdsHelp(CommandTable
);