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 "cmdhfmfdes.h"
29 static int CmdHelp(const char *Cmd
);
31 int CmdHFTune(const char *Cmd
)
33 UsbCommand c
={CMD_MEASURE_ANTENNA_TUNING_HF
};
37 // for the time being. Need better Bigbuf handling.
38 #define TRACE_SIZE 3000
40 #define ICLASS_CMD_ACTALL 0x0A
41 #define ICLASS_CMD_IDENTIFY 0x0C
42 #define ICLASS_CMD_READ 0x0C
43 #define ICLASS_CMD_SELECT 0x81
44 #define ICLASS_CMD_PAGESEL 0x84
45 #define ICLASS_CMD_READCHECK 0x88
46 #define ICLASS_CMD_CHECK 0x05
47 #define ICLASS_CMD_SOF 0x0F
48 #define ICLASS_CMD_HALT 0x00
50 #define iso14443_CMD_WUPA 0x52
51 #define iso14443_CMD_SELECT 0x93
52 #define iso14443_CMD_SELECT_2 0x95
53 #define iso14443_CMD_REQ 0x26
54 #define iso14443_CMD_READBLOCK 0x30
55 #define iso14443_CMD_WRITEBLOCK 0xA0
56 #define iso14443_CMD_INC 0xC0
57 #define iso14443_CMD_DEC 0xC1
58 #define iso14443_CMD_RESTORE 0xC2
59 #define iso14443_CMD_TRANSFER 0xB0
60 #define iso14443_CMD_HALT 0x50
61 #define iso14443_CMD_RATS 0xE0
64 void annotateIso14443a(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
68 case iso14443_CMD_WUPA
: snprintf(exp
,size
,"WUPA"); break;
69 case iso14443_CMD_SELECT
:{
72 snprintf(exp
,size
,"SELECT_UID"); break;
75 snprintf(exp
,size
,"SELECT_ALL"); break;
78 case iso14443_CMD_SELECT_2
: snprintf(exp
,size
,"SELECT_2"); break;
79 case iso14443_CMD_REQ
: snprintf(exp
,size
,"REW"); break;
80 case iso14443_CMD_READBLOCK
: snprintf(exp
,size
,"READBLOCK(%d)",cmd
[1]); break;
81 case iso14443_CMD_WRITEBLOCK
: snprintf(exp
,size
,"WRITEBLOCK(%d)",cmd
[1]); break;
82 case iso14443_CMD_INC
: snprintf(exp
,size
,"INC(%d)",cmd
[1]); break;
83 case iso14443_CMD_DEC
: snprintf(exp
,size
,"DEC(%d)",cmd
[1]); break;
84 case iso14443_CMD_RESTORE
: snprintf(exp
,size
,"RESTORE(%d)",cmd
[1]); break;
85 case iso14443_CMD_TRANSFER
: snprintf(exp
,size
,"TRANSFER(%d)",cmd
[1]); break;
86 case iso14443_CMD_HALT
: snprintf(exp
,size
,"HALT"); break;
87 case iso14443_CMD_RATS
: snprintf(exp
,size
,"RATS"); break;
88 default: snprintf(exp
,size
,"?"); break;
93 void annotateIclass(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
96 if(cmdsize
> 1 && cmd
[0] == ICLASS_CMD_READ
)
98 snprintf(exp
,size
,"READ(%d)",cmd
[1]);
104 case ICLASS_CMD_ACTALL
: snprintf(exp
,size
,"ACTALL"); break;
105 case ICLASS_CMD_IDENTIFY
: snprintf(exp
,size
,"IDENTIFY"); break;
106 case ICLASS_CMD_SELECT
: snprintf(exp
,size
,"SELECT"); break;
107 case ICLASS_CMD_PAGESEL
: snprintf(exp
,size
,"PAGESEL"); break;
108 case ICLASS_CMD_READCHECK
: snprintf(exp
,size
,"READCHECK"); break;
109 case ICLASS_CMD_CHECK
: snprintf(exp
,size
,"CHECK"); break;
110 case ICLASS_CMD_SOF
: snprintf(exp
,size
,"SOF"); break;
111 case ICLASS_CMD_HALT
: snprintf(exp
,size
,"HALT"); break;
112 default: snprintf(exp
,size
,"?"); break;
119 uint16_t printTraceLine(uint16_t tracepos
, uint8_t* trace
, bool iclass
, bool showWaitCycles
)
122 uint16_t duration
, data_len
,parity_len
;
124 uint32_t timestamp
, first_timestamp
, EndOfTransmissionTimestamp
;
125 char explanation
[30] = {0};
127 first_timestamp
= *((uint32_t *)(trace
));
128 timestamp
= *((uint32_t *)(trace
+ tracepos
));
129 // Break and stick with current result if buffer was not completely full
130 if (timestamp
== 0x44444444) return TRACE_SIZE
;
133 duration
= *((uint16_t *)(trace
+ tracepos
));
135 data_len
= *((uint16_t *)(trace
+ tracepos
));
138 if (data_len
& 0x8000) {
144 parity_len
= (data_len
-1)/8 + 1;
146 if (tracepos
+ data_len
+ parity_len
>= TRACE_SIZE
) {
150 uint8_t *frame
= trace
+ tracepos
;
151 tracepos
+= data_len
;
152 uint8_t *parityBytes
= trace
+ tracepos
;
153 tracepos
+= parity_len
;
155 //--- Draw the data column
157 for (int j
= 0; j
< data_len
; j
++) {
158 int oddparity
= 0x01;
161 for (k
=0 ; k
<8 ; k
++) {
162 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
165 uint8_t parityBits
= parityBytes
[j
>>3];
167 if (isResponse
&& (oddparity
!= ((parityBits
>> (7-(j
&0x0007))) & 0x01))) {
168 sprintf(line
[j
/16]+((j
%16)*4), "%02x! ", frame
[j
]);
170 sprintf(line
[j
/16]+((j
%16)*4), "%02x ", frame
[j
]);
173 //--- Draw the CRC column
174 bool crcError
= false;
180 if(!isResponse
&& data_len
== 4 ) {
181 // Rough guess that this is a command from the reader
182 // For iClass the command byte is not part of the CRC
183 ComputeCrc14443(CRC_ICLASS
, &frame
[1], data_len
-3, &b1
, &b2
);
186 // For other data.. CRC might not be applicable (UPDATE commands etc.)
187 ComputeCrc14443(CRC_ICLASS
, frame
, data_len
-2, &b1
, &b2
);
190 if (b1
!= frame
[data_len
-2] || b2
!= frame
[data_len
-1]) {
196 ComputeCrc14443(CRC_14443_A
, frame
, data_len
-2, &b1
, &b2
);
198 if (b1
!= frame
[data_len
-2] || b2
!= frame
[data_len
-1]) {
199 if(!(isResponse
& (data_len
< 6)))
207 char *crc
= crcError
? "!crc" :" ";
209 EndOfTransmissionTimestamp
= timestamp
+ duration
;
213 if(iclass
) annotateIclass(explanation
,sizeof(explanation
),frame
,data_len
);
214 else annotateIso14443a(explanation
,sizeof(explanation
),frame
,data_len
);
217 int num_lines
= (data_len
- 1)/16 + 1;
218 for (int j
= 0; j
< num_lines
; j
++) {
220 PrintAndLog(" %9d | %9d | %s | %-64s| %s| %s",
221 (timestamp
- first_timestamp
),
222 (EndOfTransmissionTimestamp
- first_timestamp
),
223 (isResponse
? "Tag" : "Rdr"),
225 (j
== num_lines
-1) ? crc
: " ",
226 (j
== num_lines
-1) ? explanation
: "");
228 PrintAndLog(" | | | %-64s| %s| %s",
230 (j
== num_lines
-1)?crc
:" ",
231 (j
== num_lines
-1) ? explanation
: "");
235 bool next_isResponse
= *((uint16_t *)(trace
+ tracepos
+ 6)) & 0x8000;
237 if (showWaitCycles
&& !isResponse
&& next_isResponse
) {
238 uint32_t next_timestamp
= *((uint32_t *)(trace
+ tracepos
));
239 if (next_timestamp
!= 0x44444444) {
240 PrintAndLog(" %9d | %9d | %s | fdt (Frame Delay Time): %d",
241 (EndOfTransmissionTimestamp
- first_timestamp
),
242 (next_timestamp
- first_timestamp
),
244 (next_timestamp
- EndOfTransmissionTimestamp
));
250 int CmdHFList(const char *Cmd
)
252 bool showWaitCycles
= false;
254 int tlen
= param_getstr(Cmd
,0,type
);
255 char param
= param_getchar(Cmd
, 1);
259 if(tlen
== 0 || (strcmp(type
, "iclass") != 0 && strcmp(type
,"14a") != 0))
263 if(param
== 'h' || (param
!=0 && param
!= 'f'))
269 PrintAndLog("List protocol data in trace buffer.");
270 PrintAndLog("Usage: hf list [14a|iclass] [f]");
271 PrintAndLog(" 14a - interpret data as iso14443a communications");
272 PrintAndLog(" iclass - interpret data as iclass communications");
273 PrintAndLog(" f - show frame delay times as well");
275 PrintAndLog("example: hf list 14a f");
276 PrintAndLog("example: hf list iclass");
279 if(strcmp(type
, "iclass") == 0)
285 showWaitCycles
= true;
289 uint8_t trace
[TRACE_SIZE
];
290 uint16_t tracepos
= 0;
291 GetFromBigBuf(trace
, TRACE_SIZE
, 0);
292 WaitForResponse(CMD_ACK
, NULL
);
294 PrintAndLog("Recorded Activity");
296 PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
297 PrintAndLog("iso14443a - All times are in carrier periods (1/13.56Mhz)");
298 PrintAndLog("iClass - Timings are not as accurate");
300 PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |");
301 PrintAndLog("-----------|-----------|-----|-----------------------------------------------------------------|-----|--------------------|");
303 while(tracepos
< TRACE_SIZE
)
305 tracepos
= printTraceLine(tracepos
, trace
, iclass
, showWaitCycles
);
311 static command_t CommandTable
[] =
313 {"help", CmdHelp
, 1, "This help"},
314 {"14a", CmdHF14A
, 1, "{ ISO14443A RFIDs... }"},
315 {"14b", CmdHF14B
, 1, "{ ISO14443B RFIDs... }"},
316 {"15", CmdHF15
, 1, "{ ISO15693 RFIDs... }"},
317 {"epa", CmdHFEPA
, 1, "{ German Identification Card... }"},
318 {"legic", CmdHFLegic
, 0, "{ LEGIC RFIDs... }"},
319 {"iclass", CmdHFiClass
, 1, "{ ICLASS RFIDs... }"},
320 {"mf", CmdHFMF
, 1, "{ MIFARE RFIDs... }"},
321 {"mfu", CmdHFMFUltra
, 1, "{ MIFARE Ultralight RFIDs... }"},
322 {"mfdes", CmdHFMFDes
, 1, "{ MIFARE Desfire RFIDs... }"},
323 {"des", CmdHFDES
, 0, "{ MIFARE DESfire}"},
324 {"tune", CmdHFTune
, 0, "Continuously measure HF antenna tuning"},
325 {"list", CmdHFList
, 1, "List protocol data in trace buffer"},
326 {NULL
, NULL
, 0, NULL
}
329 int CmdHF(const char *Cmd
)
331 CmdsParse(CommandTable
, Cmd
);
335 int CmdHelp(const char *Cmd
)
337 CmdsHelp(CommandTable
);