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