]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhf.c
ADD: Print the key for ULEV1/NTAG authentication if sent in cmdhf.c "HF LIST 14A"
[proxmark3-svn] / client / cmdhf.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3 //
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
6 // the license.
7 //-----------------------------------------------------------------------------
8 // High frequency commands
9 //-----------------------------------------------------------------------------
10
11 #include <stdio.h>
12 #include <string.h>
13 #include "proxmark3.h"
14 #include "graph.h"
15 #include "ui.h"
16 #include "cmdparser.h"
17 #include "cmdhf.h"
18 #include "cmdhf14a.h"
19 #include "cmdhf14b.h"
20 #include "cmdhf15.h"
21 #include "cmdhfepa.h"
22 #include "cmdhflegic.h"
23 #include "cmdhficlass.h"
24 #include "cmdhfmf.h"
25 #include "cmdhfmfu.h"
26 #include "cmdhfmfdes.h"
27 #include "cmdhftopaz.h"
28 #include "protocols.h"
29
30 static int CmdHelp(const char *Cmd);
31
32 int CmdHFTune(const char *Cmd)
33 {
34 UsbCommand c={CMD_MEASURE_ANTENNA_TUNING_HF};
35 SendCommand(&c);
36 return 0;
37 }
38
39 //TODO:
40 //void annotateIso15693(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize){}
41
42 void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
43 {
44 switch(cmd[0])
45 {
46 case ISO14443A_CMD_WUPA: snprintf(exp,size,"WUPA"); break;
47 case ISO14443A_CMD_ANTICOLL_OR_SELECT:{
48 // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor)
49 // 93 70 = Select (usage: 9370+5bytes 9320 answer - answer: 1byte SAK)
50 if(cmd[1] == 0x70)
51 {
52 snprintf(exp,size,"SELECT_UID"); break;
53 }else
54 {
55 snprintf(exp,size,"ANTICOLL"); break;
56 }
57 }
58 case ISO14443A_CMD_ANTICOLL_OR_SELECT_2:{
59 //95 20 = Anticollision of cascade level2
60 //95 70 = Select of cascade level2
61 if(cmd[2] == 0x70)
62 {
63 snprintf(exp,size,"SELECT_UID-2"); break;
64 }else
65 {
66 snprintf(exp,size,"ANTICOLL-2"); break;
67 }
68 }
69 case ISO14443A_CMD_REQA: snprintf(exp,size,"REQA"); break;
70 case ISO14443A_CMD_READBLOCK: snprintf(exp,size,"READBLOCK(%d)",cmd[1]); break;
71 case ISO14443A_CMD_WRITEBLOCK: snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); break;
72 case ISO14443A_CMD_HALT: snprintf(exp,size,"HALT"); break;
73 case ISO14443A_CMD_RATS: snprintf(exp,size,"RATS"); break;
74 case MIFARE_CMD_INC: snprintf(exp,size,"INC(%d)",cmd[1]); break;
75 case MIFARE_CMD_DEC: snprintf(exp,size,"DEC(%d)",cmd[1]); break;
76 case MIFARE_CMD_RESTORE: snprintf(exp,size,"RESTORE(%d)",cmd[1]); break;
77 case MIFARE_CMD_TRANSFER: snprintf(exp,size,"TRANSFER(%d)",cmd[1]); break;
78 case MIFARE_AUTH_KEYA:{
79 if ( cmdsize > 3)
80 snprintf(exp,size,"AUTH-A(%d)",cmd[1]);
81 else
82 // case MIFARE_ULEV1_VERSION : both 0x60.
83 snprintf(exp,size,"EV1 VERSION");
84 break;
85 }
86 case MIFARE_AUTH_KEYB: snprintf(exp,size,"AUTH-B(%d)",cmd[1]); break;
87 case MIFARE_MAGICWUPC1: snprintf(exp,size,"MAGIC WUPC1"); break;
88 case MIFARE_MAGICWUPC2: snprintf(exp,size,"MAGIC WUPC2"); break;
89 case MIFARE_MAGICWIPEC: snprintf(exp,size,"MAGIC WIPEC"); break;
90 case MIFARE_ULC_AUTH_1 : snprintf(exp,size,"AUTH "); break;
91 case MIFARE_ULC_AUTH_2 : snprintf(exp,size,"AUTH_ANSW"); break;
92 case MIFARE_ULEV1_AUTH :
93 if ( cmdsize == 7 )
94 snprintf(exp,size,"PWD-AUTH KEY: 0x%02x%02x%02x%02x", cmd[1], cmd[2], cmd[3], cmd[4] );
95 else
96 snprintf(exp,size,"PWD-AUTH");
97 break;
98
99 case MIFARE_ULEV1_FASTREAD : {
100 if ( cmdsize >=3 && cmd[2] <= 0xE6)
101 snprintf(exp,size,"READ RANGE (%d-%d)",cmd[1],cmd[2]);
102 else
103 snprintf(exp,size,"?");
104 break;
105 }
106 case MIFARE_ULC_WRITE : {
107 if ( cmd[1] < 0x21 )
108 snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]);
109 else
110 snprintf(exp,size,"?");
111 break;
112 }
113 case MIFARE_ULEV1_READ_CNT :{
114 if ( cmd[1] < 5 )
115 snprintf(exp,size,"READ CNT(%d)",cmd[1]);
116 else
117 snprintf(exp,size,"?");
118 break;
119 }
120 case MIFARE_ULEV1_INCR_CNT : {
121 if ( cmd[1] < 5 )
122 snprintf(exp,size,"INCR(%d)",cmd[1]);
123 else
124 snprintf(exp,size,"?");
125 break;
126 }
127 case MIFARE_ULEV1_READSIG : snprintf(exp,size,"READ_SIG"); break;
128 case MIFARE_ULEV1_CHECKTEAR : snprintf(exp,size,"CHK_TEARING(%d)",cmd[1]); break;
129 case MIFARE_ULEV1_VCSL : snprintf(exp,size,"VCSL"); break;
130 default: snprintf(exp,size,"?"); break;
131 }
132 return;
133 }
134
135 void annotateIclass(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
136 {
137 switch(cmd[0])
138 {
139 case ICLASS_CMD_ACTALL: snprintf(exp,size,"ACTALL"); break;
140 case ICLASS_CMD_READ_OR_IDENTIFY:{
141 if(cmdsize > 1){
142 snprintf(exp,size,"READ(%d)",cmd[1]);
143 }else{
144 snprintf(exp,size,"IDENTIFY");
145 }
146 break;
147 }
148 case ICLASS_CMD_SELECT: snprintf(exp,size,"SELECT"); break;
149 case ICLASS_CMD_PAGESEL: snprintf(exp,size,"PAGESEL(%d)", cmd[1]); break;
150 case ICLASS_CMD_READCHECK_KC:snprintf(exp,size,"READCHECK[Kc](%d)", cmd[1]); break;
151 case ICLASS_CMD_READCHECK_KD:snprintf(exp,size,"READCHECK[Kd](%d)", cmd[1]); break;
152 case ICLASS_CMD_CHECK: snprintf(exp,size,"CHECK"); break;
153 case ICLASS_CMD_DETECT: snprintf(exp,size,"DETECT"); break;
154 case ICLASS_CMD_HALT: snprintf(exp,size,"HALT"); break;
155 case ICLASS_CMD_UPDATE: snprintf(exp,size,"UPDATE(%d)",cmd[1]); break;
156 case ICLASS_CMD_ACT: snprintf(exp,size,"ACT"); break;
157 case ICLASS_CMD_READ4: snprintf(exp,size,"READ4(%d)",cmd[1]); break;
158 default: snprintf(exp,size,"?"); break;
159 }
160 return;
161 }
162
163 void annotateIso15693(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
164 {
165
166 if(cmd[0] == 0x26)
167 {
168 switch(cmd[1]){
169 case ISO15693_INVENTORY :snprintf(exp, size, "INVENTORY");break;
170 case ISO15693_STAYQUIET :snprintf(exp, size, "STAY_QUIET");break;
171 default: snprintf(exp,size,"?"); break;
172
173 }
174 }else if(cmd[0] == 0x02)
175 {
176 switch(cmd[1])
177 {
178 case ISO15693_READBLOCK :snprintf(exp, size, "READBLOCK");break;
179 case ISO15693_WRITEBLOCK :snprintf(exp, size, "WRITEBLOCK");break;
180 case ISO15693_LOCKBLOCK :snprintf(exp, size, "LOCKBLOCK");break;
181 case ISO15693_READ_MULTI_BLOCK :snprintf(exp, size, "READ_MULTI_BLOCK");break;
182 case ISO15693_SELECT :snprintf(exp, size, "SELECT");break;
183 case ISO15693_RESET_TO_READY :snprintf(exp, size, "RESET_TO_READY");break;
184 case ISO15693_WRITE_AFI :snprintf(exp, size, "WRITE_AFI");break;
185 case ISO15693_LOCK_AFI :snprintf(exp, size, "LOCK_AFI");break;
186 case ISO15693_WRITE_DSFID :snprintf(exp, size, "WRITE_DSFID");break;
187 case ISO15693_LOCK_DSFID :snprintf(exp, size, "LOCK_DSFID");break;
188 case ISO15693_GET_SYSTEM_INFO :snprintf(exp, size, "GET_SYSTEM_INFO");break;
189 case ISO15693_READ_MULTI_SECSTATUS :snprintf(exp, size, "READ_MULTI_SECSTATUS");break;
190 default: snprintf(exp,size,"?"); break;
191 }
192 }
193 }
194
195
196 void annotateTopaz(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
197 {
198 switch(cmd[0]) {
199 case TOPAZ_REQA :snprintf(exp, size, "REQA");break;
200 case TOPAZ_WUPA :snprintf(exp, size, "WUPA");break;
201 case TOPAZ_RID :snprintf(exp, size, "RID");break;
202 case TOPAZ_RALL :snprintf(exp, size, "RALL");break;
203 case TOPAZ_READ :snprintf(exp, size, "READ");break;
204 case TOPAZ_WRITE_E :snprintf(exp, size, "WRITE-E");break;
205 case TOPAZ_WRITE_NE :snprintf(exp, size, "WRITE-NE");break;
206 case TOPAZ_RSEG :snprintf(exp, size, "RSEG");break;
207 case TOPAZ_READ8 :snprintf(exp, size, "READ8");break;
208 case TOPAZ_WRITE_E8 :snprintf(exp, size, "WRITE-E8");break;
209 case TOPAZ_WRITE_NE8 :snprintf(exp, size, "WRITE-NE8");break;
210 default: snprintf(exp,size,"?"); break;
211 }
212 }
213
214
215 /**
216 06 00 = INITIATE
217 0E xx = SELECT ID (xx = Chip-ID)
218 0B = Get UID
219 08 yy = Read Block (yy = block number)
220 09 yy dd dd dd dd = Write Block (yy = block number; dd dd dd dd = data to be written)
221 0C = Reset to Inventory
222 0F = Completion
223 0A 11 22 33 44 55 66 = Authenticate (11 22 33 44 55 66 = data to authenticate)
224 **/
225
226 void annotateIso14443b(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
227 {
228 switch(cmd[0]){
229 case ISO14443B_REQB : snprintf(exp,size,"REQB");break;
230 case ISO14443B_ATTRIB : snprintf(exp,size,"ATTRIB");break;
231 case ISO14443B_HALT : snprintf(exp,size,"HALT");break;
232 case ISO14443B_INITIATE : snprintf(exp,size,"INITIATE");break;
233 case ISO14443B_SELECT : snprintf(exp,size,"SELECT(%d)",cmd[1]);break;
234 case ISO14443B_GET_UID : snprintf(exp,size,"GET UID");break;
235 case ISO14443B_READ_BLK : snprintf(exp,size,"READ_BLK(%d)", cmd[1]);break;
236 case ISO14443B_WRITE_BLK : snprintf(exp,size,"WRITE_BLK(%d)",cmd[1]);break;
237 case ISO14443B_RESET : snprintf(exp,size,"RESET");break;
238 case ISO14443B_COMPLETION : snprintf(exp,size,"COMPLETION");break;
239 case ISO14443B_AUTHENTICATE : snprintf(exp,size,"AUTHENTICATE");break;
240 default : snprintf(exp,size ,"?");break;
241 }
242
243 }
244
245 /**
246 * @brief iso14443A_CRC_check Checks CRC in command or response
247 * @param isResponse
248 * @param data
249 * @param len
250 * @return 0 : CRC-command, CRC not ok
251 * 1 : CRC-command, CRC ok
252 * 2 : Not crc-command
253 */
254
255 uint8_t iso14443A_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
256 {
257 uint8_t b1,b2;
258
259 if(len <= 2) return 2;
260
261 if(isResponse & (len < 6)) return 2;
262
263 ComputeCrc14443(CRC_14443_A, data, len-2, &b1, &b2);
264 if (b1 != data[len-2] || b2 != data[len-1]) {
265 return 0;
266 } else {
267 return 1;
268 }
269 }
270
271
272 /**
273 * @brief iso14443B_CRC_check Checks CRC in command or response
274 * @param isResponse
275 * @param data
276 * @param len
277 * @return 0 : CRC-command, CRC not ok
278 * 1 : CRC-command, CRC ok
279 * 2 : Not crc-command
280 */
281
282 uint8_t iso14443B_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
283 {
284 uint8_t b1,b2;
285
286 if(len <= 2) return 2;
287
288 ComputeCrc14443(CRC_14443_B, data, len-2, &b1, &b2);
289 if(b1 != data[len-2] || b2 != data[len-1]) {
290 return 0;
291 }
292 return 1;
293 }
294
295 /**
296 * @brief iclass_CRC_Ok Checks CRC in command or response
297 * @param isResponse
298 * @param data
299 * @param len
300 * @return 0 : CRC-command, CRC not ok
301 * 1 : CRC-command, CRC ok
302 * 2 : Not crc-command
303 */
304 uint8_t iclass_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
305 {
306 if(len < 4) return 2;//CRC commands (and responses) are all at least 4 bytes
307
308 uint8_t b1, b2;
309
310 if(!isResponse)//Commands to tag
311 {
312 /**
313 These commands should have CRC. Total length leftmost
314 4 READ
315 4 READ4
316 12 UPDATE - unsecured, ends with CRC16
317 14 UPDATE - secured, ends with signature instead
318 4 PAGESEL
319 **/
320 if(len == 4 || len == 12)//Covers three of them
321 {
322 //Don't include the command byte
323 ComputeCrc14443(CRC_ICLASS, (data+1), len-3, &b1, &b2);
324 return b1 == data[len -2] && b2 == data[len-1];
325 }
326 return 2;
327 }else{
328 /**
329 These tag responses should have CRC. Total length leftmost
330
331 10 READ data[8] crc[2]
332 34 READ4 data[32]crc[2]
333 10 UPDATE data[8] crc[2]
334 10 SELECT csn[8] crc[2]
335 10 IDENTIFY asnb[8] crc[2]
336 10 PAGESEL block1[8] crc[2]
337 10 DETECT csn[8] crc[2]
338
339 These should not
340
341 4 CHECK chip_response[4]
342 8 READCHECK data[8]
343 1 ACTALL sof[1]
344 1 ACT sof[1]
345
346 In conclusion, without looking at the command; any response
347 of length 10 or 34 should have CRC
348 **/
349 if(len != 10 && len != 34) return true;
350
351 ComputeCrc14443(CRC_ICLASS, data, len-2, &b1, &b2);
352 return b1 == data[len -2] && b2 == data[len-1];
353 }
354 }
355
356
357 bool is_last_record(uint16_t tracepos, uint8_t *trace, uint16_t traceLen)
358 {
359 return(tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) >= traceLen);
360 }
361
362
363 bool next_record_is_response(uint16_t tracepos, uint8_t *trace)
364 {
365 uint16_t next_records_datalen = *((uint16_t *)(trace + tracepos + sizeof(uint32_t) + sizeof(uint16_t)));
366
367 return(next_records_datalen & 0x8000);
368 }
369
370
371 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)
372 {
373
374 #define MAX_TOPAZ_READER_CMD_LEN 16
375
376 uint32_t last_timestamp = timestamp + *duration;
377
378 if ((*data_len != 1) || (frame[0] == TOPAZ_WUPA) || (frame[0] == TOPAZ_REQA)) return false;
379
380 memcpy(topaz_reader_command, frame, *data_len);
381
382 while (!is_last_record(*tracepos, trace, traceLen) && !next_record_is_response(*tracepos, trace)) {
383 uint32_t next_timestamp = *((uint32_t *)(trace + *tracepos));
384 *tracepos += sizeof(uint32_t);
385 uint16_t next_duration = *((uint16_t *)(trace + *tracepos));
386 *tracepos += sizeof(uint16_t);
387 uint16_t next_data_len = *((uint16_t *)(trace + *tracepos)) & 0x7FFF;
388 *tracepos += sizeof(uint16_t);
389 uint8_t *next_frame = (trace + *tracepos);
390 *tracepos += next_data_len;
391 if ((next_data_len == 1) && (*data_len + next_data_len <= MAX_TOPAZ_READER_CMD_LEN)) {
392 memcpy(topaz_reader_command + *data_len, next_frame, next_data_len);
393 *data_len += next_data_len;
394 last_timestamp = next_timestamp + next_duration;
395 } else {
396 // rewind and exit
397 *tracepos = *tracepos - next_data_len - sizeof(uint16_t) - sizeof(uint16_t) - sizeof(uint32_t);
398 break;
399 }
400 uint16_t next_parity_len = (next_data_len-1)/8 + 1;
401 *tracepos += next_parity_len;
402 }
403
404 *duration = last_timestamp - timestamp;
405
406 return true;
407 }
408
409
410 uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace, uint8_t protocol, bool showWaitCycles, bool markCRCBytes)
411 {
412 bool isResponse;
413 uint16_t data_len, parity_len;
414 uint32_t duration;
415 uint8_t topaz_reader_command[9];
416 uint32_t timestamp, first_timestamp, EndOfTransmissionTimestamp;
417 char explanation[30] = {0};
418
419 if (tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen) return traceLen;
420
421 first_timestamp = *((uint32_t *)(trace));
422 timestamp = *((uint32_t *)(trace + tracepos));
423
424 tracepos += 4;
425 duration = *((uint16_t *)(trace + tracepos));
426 tracepos += 2;
427 data_len = *((uint16_t *)(trace + tracepos));
428 tracepos += 2;
429
430 if (data_len & 0x8000) {
431 data_len &= 0x7fff;
432 isResponse = true;
433 } else {
434 isResponse = false;
435 }
436 parity_len = (data_len-1)/8 + 1;
437
438 if (tracepos + data_len + parity_len > traceLen) {
439 return traceLen;
440 }
441 uint8_t *frame = trace + tracepos;
442 tracepos += data_len;
443 uint8_t *parityBytes = trace + tracepos;
444 tracepos += parity_len;
445
446 if (protocol == TOPAZ && !isResponse) {
447 // topaz reader commands come in 1 or 9 separate frames with 7 or 8 Bits each.
448 // merge them:
449 if (merge_topaz_reader_frames(timestamp, &duration, &tracepos, traceLen, trace, frame, topaz_reader_command, &data_len)) {
450 frame = topaz_reader_command;
451 }
452 }
453
454 //Check the CRC status
455 uint8_t crcStatus = 2;
456
457 if (data_len > 2) {
458 switch (protocol) {
459 case ICLASS:
460 crcStatus = iclass_CRC_check(isResponse, frame, data_len);
461 break;
462 case ISO_14443B:
463 case TOPAZ:
464 crcStatus = iso14443B_CRC_check(isResponse, frame, data_len);
465 break;
466 case ISO_14443A:
467 crcStatus = iso14443A_CRC_check(isResponse, frame, data_len);
468 break;
469 default:
470 break;
471 }
472 }
473 //0 CRC-command, CRC not ok
474 //1 CRC-command, CRC ok
475 //2 Not crc-command
476
477 //--- Draw the data column
478 //char line[16][110];
479 char line[16][110];
480
481 for (int j = 0; j < data_len && j/16 < 16; j++) {
482
483 int oddparity = 0x01;
484 int k;
485
486 for (k=0 ; k<8 ; k++) {
487 oddparity ^= (((frame[j] & 0xFF) >> k) & 0x01);
488 }
489 uint8_t parityBits = parityBytes[j>>3];
490 if (isResponse && (oddparity != ((parityBits >> (7-(j&0x0007))) & 0x01))) {
491 snprintf(line[j/16]+(( j % 16) * 4),110, "%02x! ", frame[j]);
492 } else {
493 snprintf(line[j/16]+(( j % 16) * 4),110, "%02x ", frame[j]);
494 }
495
496 }
497
498 if (markCRCBytes) {
499 if(crcStatus == 0 || crcStatus == 1)
500 {//CRC-command
501 char *pos1 = line[(data_len-2)/16]+(((data_len-2) % 16) * 4);
502 (*pos1) = '[';
503 char *pos2 = line[(data_len)/16]+(((data_len) % 16) * 4);
504 sprintf(pos2, "%c", ']');
505 }
506 }
507
508 if(data_len == 0)
509 {
510 if(data_len == 0){
511 sprintf(line[0],"<empty trace - possible error>");
512 }
513 }
514 //--- Draw the CRC column
515
516 char *crc = (crcStatus == 0 ? "!crc" : (crcStatus == 1 ? " ok " : " "));
517
518 EndOfTransmissionTimestamp = timestamp + duration;
519
520 if(!isResponse)
521 {
522 switch(protocol) {
523 case ICLASS: annotateIclass(explanation,sizeof(explanation),frame,data_len); break;
524 case ISO_14443A: annotateIso14443a(explanation,sizeof(explanation),frame,data_len); break;
525 case ISO_14443B: annotateIso14443b(explanation,sizeof(explanation),frame,data_len); break;
526 case TOPAZ: annotateTopaz(explanation,sizeof(explanation),frame,data_len); break;
527 default: break;
528 }
529 }
530
531 int num_lines = MIN((data_len - 1)/16 + 1, 16);
532 for (int j = 0; j < num_lines ; j++) {
533 if (j == 0) {
534 PrintAndLog(" %10d | %10d | %s |%-64s | %s| %s",
535 (timestamp - first_timestamp),
536 (EndOfTransmissionTimestamp - first_timestamp),
537 (isResponse ? "Tag" : "Rdr"),
538 line[j],
539 (j == num_lines-1) ? crc : " ",
540 (j == num_lines-1) ? explanation : "");
541 } else {
542 PrintAndLog(" | | | %-64s| %s| %s",
543 line[j],
544 (j == num_lines-1)?crc:" ",
545 (j == num_lines-1) ? explanation : "");
546 }
547 }
548
549 if (is_last_record(tracepos, trace, traceLen)) return traceLen;
550
551 if (showWaitCycles && !isResponse && next_record_is_response(tracepos, trace)) {
552 uint32_t next_timestamp = *((uint32_t *)(trace + tracepos));
553 PrintAndLog(" %9d | %9d | %s | fdt (Frame Delay Time): %d",
554 (EndOfTransmissionTimestamp - first_timestamp),
555 (next_timestamp - first_timestamp),
556 " ",
557 (next_timestamp - EndOfTransmissionTimestamp));
558 }
559
560 return tracepos;
561 }
562
563
564 int CmdHFList(const char *Cmd)
565 {
566 bool showWaitCycles = false;
567 bool markCRCBytes = false;
568 char type[40] = {0};
569 int tlen = param_getstr(Cmd,0,type);
570 char param1 = param_getchar(Cmd, 1);
571 char param2 = param_getchar(Cmd, 2);
572 bool errors = false;
573 uint8_t protocol = 0;
574 //Validate params
575
576 if(tlen == 0) {
577 errors = true;
578 }
579
580 if(param1 == 'h'
581 || (param1 != 0 && param1 != 'f' && param1 != 'c')
582 || (param2 != 0 && param2 != 'f' && param2 != 'c')) {
583 errors = true;
584 }
585
586 if(!errors) {
587 if(strcmp(type, "iclass") == 0) {
588 protocol = ICLASS;
589 } else if(strcmp(type, "14a") == 0) {
590 protocol = ISO_14443A;
591 } else if(strcmp(type, "14b") == 0) {
592 protocol = ISO_14443B;
593 } else if(strcmp(type,"topaz")== 0) {
594 protocol = TOPAZ;
595 } else if(strcmp(type,"raw")== 0) {
596 protocol = -1;//No crc, no annotations
597 }else{
598 errors = true;
599 }
600 }
601
602 if (errors) {
603 PrintAndLog("List protocol data in trace buffer.");
604 PrintAndLog("Usage: hf list <protocol> [f][c]");
605 PrintAndLog(" f - show frame delay times as well");
606 PrintAndLog(" c - mark CRC bytes");
607 PrintAndLog("Supported <protocol> values:");
608 PrintAndLog(" raw - just show raw data without annotations");
609 PrintAndLog(" 14a - interpret data as iso14443a communications");
610 PrintAndLog(" 14b - interpret data as iso14443b communications");
611 PrintAndLog(" iclass - interpret data as iclass communications");
612 PrintAndLog(" topaz - interpret data as topaz communications");
613 PrintAndLog("");
614 PrintAndLog("example: hf list 14a f");
615 PrintAndLog("example: hf list iclass");
616 return 0;
617 }
618
619
620 if (param1 == 'f' || param2 == 'f') {
621 showWaitCycles = true;
622 }
623
624 if (param1 == 'c' || param2 == 'c') {
625 markCRCBytes = true;
626 }
627
628 uint8_t *trace;
629 uint16_t tracepos = 0;
630 trace = malloc(USB_CMD_DATA_SIZE);
631
632 // Query for the size of the trace
633 UsbCommand response;
634 GetFromBigBuf(trace, USB_CMD_DATA_SIZE, 0);
635 WaitForResponse(CMD_ACK, &response);
636 uint16_t traceLen = response.arg[2];
637 if (traceLen > USB_CMD_DATA_SIZE) {
638 uint8_t *p = realloc(trace, traceLen);
639 if (p == NULL) {
640 PrintAndLog("Cannot allocate memory for trace");
641 free(trace);
642 return 2;
643 }
644 trace = p;
645 GetFromBigBuf(trace, traceLen, 0);
646 WaitForResponse(CMD_ACK, NULL);
647 }
648
649 PrintAndLog("Recorded Activity (TraceLen = %d bytes)", traceLen);
650 PrintAndLog("");
651 PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
652 PrintAndLog("iso14443a - All times are in carrier periods (1/13.56Mhz)");
653 PrintAndLog("iClass - Timings are not as accurate");
654 PrintAndLog("");
655 PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |");
656 PrintAndLog("------------|------------|-----|-----------------------------------------------------------------|-----|--------------------|");
657
658 while(tracepos < traceLen)
659 {
660 tracepos = printTraceLine(tracepos, traceLen, trace, protocol, showWaitCycles, markCRCBytes);
661 }
662
663 free(trace);
664 return 0;
665 }
666
667 int CmdHFSearch(const char *Cmd){
668 int ans = 0;
669 ans = CmdHF14AReader(Cmd);
670 if (ans > 0) return ans;
671
672 ans = CmdHF15Reader(Cmd);
673 //if (ans > 0) return ans;
674
675 ans = CmdHF14BRead(Cmd);
676 //if (ans > 0) return ans;
677
678 ans = CmdHFiClassReader(Cmd);
679 //if (ans > 0) return ans;
680 return 0;
681 }
682
683 static command_t CommandTable[] =
684 {
685 {"help", CmdHelp, 1, "This help"},
686 {"14a", CmdHF14A, 1, "{ ISO14443A RFIDs... }"},
687 {"14b", CmdHF14B, 1, "{ ISO14443B RFIDs... }"},
688 {"15", CmdHF15, 1, "{ ISO15693 RFIDs... }"},
689 {"epa", CmdHFEPA, 1, "{ German Identification Card... }"},
690 {"legic", CmdHFLegic, 0, "{ LEGIC RFIDs... }"},
691 {"iclass", CmdHFiClass, 1, "{ ICLASS RFIDs... }"},
692 {"mf", CmdHFMF, 1, "{ MIFARE RFIDs... }"},
693 {"mfu", CmdHFMFUltra, 1, "{ MIFARE Ultralight RFIDs... }"},
694 {"mfdes", CmdHFMFDes, 1, "{ MIFARE Desfire RFIDs... }"},
695 {"topaz", CmdHFTopaz, 1, "{ TOPAZ (NFC Type 1) RFIDs... }"},
696 {"tune", CmdHFTune, 0, "Continuously measure HF antenna tuning"},
697 {"list", CmdHFList, 1, "List protocol data in trace buffer"},
698 {"search", CmdHFSearch, 1, "Search for known HF tags"},
699 {NULL, NULL, 0, NULL}
700 };
701
702 int CmdHF(const char *Cmd)
703 {
704 CmdsParse(CommandTable, Cmd);
705 return 0;
706 }
707
708 int CmdHelp(const char *Cmd)
709 {
710 CmdsHelp(CommandTable);
711 return 0;
712 }
Impressum, Datenschutz