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 "protocols.h"
28 static int CmdHelp(const char *Cmd
);
30 int CmdHFTune(const char *Cmd
)
32 UsbCommand c
={CMD_MEASURE_ANTENNA_TUNING_HF
};
38 void annotateIso14443a(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
42 case ISO14443A_CMD_WUPA
: snprintf(exp
,size
,"WUPA"); break;
43 case ISO14443A_CMD_ANTICOLL_OR_SELECT
:{
44 // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor)
45 // 93 70 = Select (usage: 9370+5bytes 9320 answer - answer: 1byte SAK)
48 snprintf(exp
,size
,"SELECT_UID"); break;
51 snprintf(exp
,size
,"ANTICOLL"); break;
54 case ISO14443A_CMD_ANTICOLL_OR_SELECT_2
:{
55 //95 20 = Anticollision of cascade level2
56 //95 70 = Select of cascade level2
59 snprintf(exp
,size
,"SELECT_UID-2"); break;
62 snprintf(exp
,size
,"ANTICOLL-2"); break;
65 case ISO14443A_CMD_REQA
: snprintf(exp
,size
,"REQA"); break;
66 case ISO14443A_CMD_READBLOCK
: snprintf(exp
,size
,"READBLOCK(%d)",cmd
[1]); break;
67 case ISO14443A_CMD_WRITEBLOCK
: snprintf(exp
,size
,"WRITEBLOCK(%d)",cmd
[1]); break;
68 case ISO14443A_CMD_HALT
: snprintf(exp
,size
,"HALT"); break;
69 case ISO14443A_CMD_RATS
: snprintf(exp
,size
,"RATS"); break;
70 case MIFARE_CMD_INC
: snprintf(exp
,size
,"INC(%d)",cmd
[1]); break;
71 case MIFARE_CMD_DEC
: snprintf(exp
,size
,"DEC(%d)",cmd
[1]); break;
72 case MIFARE_CMD_RESTORE
: snprintf(exp
,size
,"RESTORE(%d)",cmd
[1]); break;
73 case MIFARE_CMD_TRANSFER
: snprintf(exp
,size
,"TRANSFER(%d)",cmd
[1]); break;
74 case MIFARE_AUTH_KEYA
: snprintf(exp
,size
,"AUTH-A(%d)",cmd
[1]); break;
75 case MIFARE_AUTH_KEYB
: snprintf(exp
,size
,"AUTH-B(%d)",cmd
[1]); break;
76 case MIFARE_MAGICMODE
: snprintf(exp
,size
,"MAGIC"); break;
77 default: snprintf(exp
,size
,"?"); break;
82 void annotateIclass(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
86 case ICLASS_CMD_ACTALL
: snprintf(exp
,size
,"ACTALL"); break;
87 case ICLASS_CMD_READ_OR_IDENTIFY
:{
89 snprintf(exp
,size
,"READ(%d)",cmd
[1]);
91 snprintf(exp
,size
,"IDENTIFY");
95 case ICLASS_CMD_SELECT
: snprintf(exp
,size
,"SELECT"); break;
96 case ICLASS_CMD_PAGESEL
: snprintf(exp
,size
,"PAGESEL(%d)", cmd
[1]); break;
97 case ICLASS_CMD_READCHECK_KC
:snprintf(exp
,size
,"READCHECK[Kc](%d)", cmd
[1]); break;
98 case ICLASS_CMD_READCHECK_KD
:snprintf(exp
,size
,"READCHECK[Kd](%d)", cmd
[1]); break;
99 case ICLASS_CMD_CHECK
: snprintf(exp
,size
,"CHECK"); break;
100 case ICLASS_CMD_DETECT
: snprintf(exp
,size
,"DETECT"); break;
101 case ICLASS_CMD_HALT
: snprintf(exp
,size
,"HALT"); break;
102 case ICLASS_CMD_UPDATE
: snprintf(exp
,size
,"UPDATE(%d)",cmd
[1]); break;
103 case ICLASS_CMD_ACT
: snprintf(exp
,size
,"ACT"); break;
104 case ICLASS_CMD_READ4
: snprintf(exp
,size
,"READ4(%d)",cmd
[1]); break;
105 default: snprintf(exp
,size
,"?"); break;
110 void annotateIso15693(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
116 case ISO15693_INVENTORY
:snprintf(exp
, size
, "INVENTORY");break;
117 case ISO15693_STAYQUIET
:snprintf(exp
, size
, "STAY_QUIET");break;
118 default: snprintf(exp
,size
,"?"); break;
121 }else if(cmd
[0] == 0x02)
125 case ISO15693_READBLOCK
:snprintf(exp
, size
, "READBLOCK");break;
126 case ISO15693_WRITEBLOCK
:snprintf(exp
, size
, "WRITEBLOCK");break;
127 case ISO15693_LOCKBLOCK
:snprintf(exp
, size
, "LOCKBLOCK");break;
128 case ISO15693_READ_MULTI_BLOCK
:snprintf(exp
, size
, "READ_MULTI_BLOCK");break;
129 case ISO15693_SELECT
:snprintf(exp
, size
, "SELECT");break;
130 case ISO15693_RESET_TO_READY
:snprintf(exp
, size
, "RESET_TO_READY");break;
131 case ISO15693_WRITE_AFI
:snprintf(exp
, size
, "WRITE_AFI");break;
132 case ISO15693_LOCK_AFI
:snprintf(exp
, size
, "LOCK_AFI");break;
133 case ISO15693_WRITE_DSFID
:snprintf(exp
, size
, "WRITE_DSFID");break;
134 case ISO15693_LOCK_DSFID
:snprintf(exp
, size
, "LOCK_DSFID");break;
135 case ISO15693_GET_SYSTEM_INFO
:snprintf(exp
, size
, "GET_SYSTEM_INFO");break;
136 case ISO15693_READ_MULTI_SECSTATUS
:snprintf(exp
, size
, "READ_MULTI_SECSTATUS");break;
137 default: snprintf(exp
,size
,"?"); break;
144 0E xx = SELECT ID (xx = Chip-ID)
146 08 yy = Read Block (yy = block number)
147 09 yy dd dd dd dd = Write Block (yy = block number; dd dd dd dd = data to be written)
148 0C = Reset to Inventory
150 0A 11 22 33 44 55 66 = Authenticate (11 22 33 44 55 66 = data to authenticate)
153 void annotateIso14443b(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
156 case ISO14443B_REQB
: snprintf(exp
,size
,"REQB");break;
157 case ISO14443B_ATTRIB
: snprintf(exp
,size
,"ATTRIB");break;
158 case ISO14443B_HALT
: snprintf(exp
,size
,"HALT");break;
159 case ISO14443B_INITIATE
: snprintf(exp
,size
,"INITIATE");break;
160 case ISO14443B_SELECT
: snprintf(exp
,size
,"SELECT(%d)",cmd
[1]);break;
161 case ISO14443B_GET_UID
: snprintf(exp
,size
,"GET UID");break;
162 case ISO14443B_READ_BLK
: snprintf(exp
,size
,"READ_BLK(%d)", cmd
[1]);break;
163 case ISO14443B_WRITE_BLK
: snprintf(exp
,size
,"WRITE_BLK(%d)",cmd
[1]);break;
164 case ISO14443B_RESET
: snprintf(exp
,size
,"RESET");break;
165 case ISO14443B_COMPLETION
: snprintf(exp
,size
,"COMPLETION");break;
166 case ISO14443B_AUTHENTICATE
: snprintf(exp
,size
,"AUTHENTICATE");break;
167 default : snprintf(exp
,size
,"?");break;
173 * @brief iso14443B_CRC_Ok Checks CRC in command or response
177 * @return 0 : CRC-command, CRC not ok
178 * 1 : CRC-command, CRC ok
179 * 2 : Not crc-command
182 uint8_t iso14443B_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
186 if(len
<= 2) return 2;
188 ComputeCrc14443(CRC_14443_B
, data
, len
-2, &b1
, &b2
);
189 if(b1
!= data
[len
-2] || b2
!= data
[len
-1]) {
196 * @brief iclass_CRC_Ok Checks CRC in command or response
200 * @return 0 : CRC-command, CRC not ok
201 * 1 : CRC-command, CRC ok
202 * 2 : Not crc-command
204 uint8_t iclass_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
206 if(len
< 4) return 2;//CRC commands (and responses) are all at least 4 bytes
210 if(!isResponse
)//Commands to tag
213 These commands should have CRC. Total length leftmost
216 12 UPDATE - unsecured, ends with CRC16
217 14 UPDATE - secured, ends with signature instead
220 if(len
== 4 || len
== 12)//Covers three of them
222 //Don't include the command byte
223 ComputeCrc14443(CRC_ICLASS
, (data
+1), len
-3, &b1
, &b2
);
224 return b1
== data
[len
-2] && b2
== data
[len
-1];
229 These tag responses should have CRC. Total length leftmost
231 10 READ data[8] crc[2]
232 34 READ4 data[32]crc[2]
233 10 UPDATE data[8] crc[2]
234 10 SELECT csn[8] crc[2]
235 10 IDENTIFY asnb[8] crc[2]
236 10 PAGESEL block1[8] crc[2]
237 10 DETECT csn[8] crc[2]
241 4 CHECK chip_response[4]
246 In conclusion, without looking at the command; any response
247 of length 10 or 34 should have CRC
249 if(len
!= 10 && len
!= 34) return true;
251 ComputeCrc14443(CRC_ICLASS
, data
, len
-2, &b1
, &b2
);
252 return b1
== data
[len
-2] && b2
== data
[len
-1];
256 uint16_t printTraceLine(uint16_t tracepos
, uint16_t traceLen
, uint8_t *trace
, uint8_t protocol
, bool showWaitCycles
)
259 uint16_t duration
, data_len
, parity_len
;
261 uint32_t timestamp
, first_timestamp
, EndOfTransmissionTimestamp
;
262 char explanation
[30] = {0};
264 if (tracepos
+ sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen
) return traceLen
;
266 first_timestamp
= *((uint32_t *)(trace
));
267 timestamp
= *((uint32_t *)(trace
+ tracepos
));
270 duration
= *((uint16_t *)(trace
+ tracepos
));
272 data_len
= *((uint16_t *)(trace
+ tracepos
));
275 if (data_len
& 0x8000) {
281 parity_len
= (data_len
-1)/8 + 1;
283 if (tracepos
+ data_len
+ parity_len
> traceLen
) {
286 uint8_t *frame
= trace
+ tracepos
;
287 tracepos
+= data_len
;
288 uint8_t *parityBytes
= trace
+ tracepos
;
289 tracepos
+= parity_len
;
292 //--- Draw the data column
293 //char line[16][110];
296 for (int j
= 0; j
< data_len
&& j
/16 < 16; j
++) {
298 int oddparity
= 0x01;
301 for (k
=0 ; k
<8 ; k
++) {
302 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
305 uint8_t parityBits
= parityBytes
[j
>>3];
306 if (isResponse
&& (oddparity
!= ((parityBits
>> (7-(j
&0x0007))) & 0x01))) {
307 snprintf(line
[j
/16]+(( j
% 16) * 4),110, "%02x! ", frame
[j
]);
310 snprintf(line
[j
/16]+(( j
% 16) * 4),110, "%02x ", frame
[j
]);
316 sprintf(line
[0],"<empty trace - possible error>");
319 //--- Draw the CRC column
320 uint8_t crcStatus
= 2;
324 if(protocol
== ICLASS
)
326 crcStatus
= iclass_CRC_check(isResponse
, frame
, data_len
);
328 }else if (protocol
== ISO_14443B
)
330 crcStatus
= iso14443B_CRC_check(isResponse
, frame
, data_len
);
332 else if (protocol
== ISO_14443A
){//Iso 14443a
334 ComputeCrc14443(CRC_14443_A
, frame
, data_len
-2, &b1
, &b2
);
336 if (b1
!= frame
[data_len
-2] || b2
!= frame
[data_len
-1]) {
337 if(!(isResponse
& (data_len
< 6)))
344 //0 CRC-command, CRC not ok
345 //1 CRC-command, CRC ok
347 char *crc
= (crcStatus
== 0 ? "!crc" : (crcStatus
== 1 ? " ok " : " "));
349 EndOfTransmissionTimestamp
= timestamp
+ duration
;
353 if(protocol
== ICLASS
)
354 annotateIclass(explanation
,sizeof(explanation
),frame
,data_len
);
355 else if (protocol
== ISO_14443A
)
356 annotateIso14443a(explanation
,sizeof(explanation
),frame
,data_len
);
357 else if(protocol
== ISO_14443B
)
358 annotateIso14443b(explanation
,sizeof(explanation
),frame
,data_len
);
361 int num_lines
= MIN((data_len
- 1)/16 + 1, 16);
362 for (int j
= 0; j
< num_lines
; j
++) {
364 PrintAndLog(" %9d | %9d | %s | %-64s| %s| %s",
365 (timestamp
- first_timestamp
),
366 (EndOfTransmissionTimestamp
- first_timestamp
),
367 (isResponse
? "Tag" : "Rdr"),
369 (j
== num_lines
-1) ? crc
: " ",
370 (j
== num_lines
-1) ? explanation
: "");
372 PrintAndLog(" | | | %-64s| %s| %s",
374 (j
== num_lines
-1)?crc
:" ",
375 (j
== num_lines
-1) ? explanation
: "");
379 if (tracepos
+ sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen
) return traceLen
;
381 bool next_isResponse
= *((uint16_t *)(trace
+ tracepos
+ 6)) & 0x8000;
383 if (showWaitCycles
&& !isResponse
&& next_isResponse
) {
384 uint32_t next_timestamp
= *((uint32_t *)(trace
+ tracepos
));
385 if (next_timestamp
!= 0x44444444) {
386 PrintAndLog(" %9d | %9d | %s | fdt (Frame Delay Time): %d",
387 (EndOfTransmissionTimestamp
- first_timestamp
),
388 (next_timestamp
- first_timestamp
),
390 (next_timestamp
- EndOfTransmissionTimestamp
));
398 int CmdHFList(const char *Cmd
)
400 bool showWaitCycles
= false;
402 int tlen
= param_getstr(Cmd
,0,type
);
403 char param
= param_getchar(Cmd
, 1);
405 uint8_t protocol
= 0;
411 if(param
== 'h' || (param
!=0 && param
!= 'f'))
417 if(strcmp(type
, "iclass") == 0)
420 }else if(strcmp(type
, "14a") == 0)
422 protocol
= ISO_14443A
;
424 else if(strcmp(type
, "14b") == 0)
426 protocol
= ISO_14443B
;
427 }else if(strcmp(type
,"raw")== 0)
429 protocol
= -1;//No crc, no annotations
436 PrintAndLog("List protocol data in trace buffer.");
437 PrintAndLog("Usage: hf list <protocol> [f]");
438 PrintAndLog(" f - show frame delay times as well");
439 PrintAndLog("Supported <protocol> values:");
440 PrintAndLog(" raw - just show raw data without annotations");
441 PrintAndLog(" 14a - interpret data as iso14443a communications");
442 PrintAndLog(" 14b - interpret data as iso14443b communications");
443 PrintAndLog(" iclass - interpret data as iclass communications");
445 PrintAndLog("example: hf list 14a f");
446 PrintAndLog("example: hf list iclass");
452 showWaitCycles
= true;
457 uint16_t tracepos
= 0;
458 trace
= malloc(USB_CMD_DATA_SIZE
);
460 // Query for the size of the trace
462 GetFromBigBuf(trace
, USB_CMD_DATA_SIZE
, 0);
463 WaitForResponse(CMD_ACK
, &response
);
464 uint16_t traceLen
= response
.arg
[2];
465 if (traceLen
> USB_CMD_DATA_SIZE
) {
466 uint8_t *p
= realloc(trace
, traceLen
);
468 PrintAndLog("Cannot allocate memory for trace");
473 GetFromBigBuf(trace
, traceLen
, 0);
474 WaitForResponse(CMD_ACK
, NULL
);
477 PrintAndLog("Recorded Activity (TraceLen = %d bytes)", traceLen
);
479 PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
480 PrintAndLog("iso14443a - All times are in carrier periods (1/13.56Mhz)");
481 PrintAndLog("iClass - Timings are not as accurate");
483 PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |");
484 PrintAndLog("-----------|-----------|-----|-----------------------------------------------------------------|-----|--------------------|");
486 while(tracepos
< traceLen
)
488 tracepos
= printTraceLine(tracepos
, traceLen
, trace
, protocol
, showWaitCycles
);
496 static command_t CommandTable
[] =
498 {"help", CmdHelp
, 1, "This help"},
499 {"14a", CmdHF14A
, 1, "{ ISO14443A RFIDs... }"},
500 {"14b", CmdHF14B
, 1, "{ ISO14443B RFIDs... }"},
501 {"15", CmdHF15
, 1, "{ ISO15693 RFIDs... }"},
502 {"epa", CmdHFEPA
, 1, "{ German Identification Card... }"},
503 {"legic", CmdHFLegic
, 0, "{ LEGIC RFIDs... }"},
504 {"iclass", CmdHFiClass
, 1, "{ ICLASS RFIDs... }"},
505 {"mf", CmdHFMF
, 1, "{ MIFARE RFIDs... }"},
506 {"mfu", CmdHFMFUltra
, 1, "{ MIFARE Ultralight RFIDs... }"},
507 {"tune", CmdHFTune
, 0, "Continuously measure HF antenna tuning"},
508 {"list", CmdHFList
, 1, "List protocol data in trace buffer"},
509 {NULL
, NULL
, 0, NULL
}
512 int CmdHF(const char *Cmd
)
514 CmdsParse(CommandTable
, Cmd
);
518 int CmdHelp(const char *Cmd
)
520 CmdsHelp(CommandTable
);