]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhf.c
Fix CmdSmartUpgrade GCC8 strncpy specified bound depends on the length of the source...
[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 "cmdhf.h"
13
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include "comms.h"
18 #include "util.h"
19 #include "ui.h"
20 #include "iso14443crc.h"
21 #include "parity.h"
22 #include "cmdmain.h"
23 #include "cmdparser.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 "cmdhfmfp.h"
32 #include "cmdhfmfu.h"
33 #include "cmdhftopaz.h"
34 #include "protocols.h"
35 #include "emv/cmdemv.h"
36 #include "cmdhflist.h"
37 #include "cmdhffido.h"
38
39 static int CmdHelp(const char *Cmd);
40
41 int CmdHFTune(const char *Cmd)
42 {
43 UsbCommand c={CMD_MEASURE_ANTENNA_TUNING_HF};
44 SendCommand(&c);
45 return 0;
46 }
47
48 /**
49 * @brief iso14443B_CRC_check Checks CRC in command or response
50 * @param isResponse
51 * @param data
52 * @param len
53 * @return 0 : CRC-command, CRC not ok
54 * 1 : CRC-command, CRC ok
55 * 2 : Not crc-command
56 */
57
58 uint8_t iso14443B_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
59 {
60 uint8_t b1,b2;
61
62 if(len <= 2) return 2;
63
64 ComputeCrc14443(CRC_14443_B, data, len-2, &b1, &b2);
65 if(b1 != data[len-2] || b2 != data[len-1]) {
66 return 0;
67 } else {
68 return 1;
69 }
70 }
71
72 /**
73 * @brief iclass_CRC_Ok Checks CRC in command or response
74 * @param isResponse
75 * @param data
76 * @param len
77 * @return 0 : CRC-command, CRC not ok
78 * 1 : CRC-command, CRC ok
79 * 2 : Not crc-command
80 */
81 uint8_t iclass_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
82 {
83 if(len < 4) return 2;//CRC commands (and responses) are all at least 4 bytes
84
85 uint8_t b1, b2;
86
87 if(!isResponse)//Commands to tag
88 {
89 /**
90 These commands should have CRC. Total length leftmost
91 4 READ
92 4 READ4
93 12 UPDATE - unsecured, ends with CRC16
94 14 UPDATE - secured, ends with signature instead
95 4 PAGESEL
96 **/
97 if(len == 4 || len == 12)//Covers three of them
98 {
99 //Don't include the command byte
100 ComputeCrc14443(CRC_ICLASS, (data+1), len-3, &b1, &b2);
101 return b1 == data[len -2] && b2 == data[len-1];
102 }
103 return 2;
104 }else{
105 /**
106 These tag responses should have CRC. Total length leftmost
107
108 10 READ data[8] crc[2]
109 34 READ4 data[32]crc[2]
110 10 UPDATE data[8] crc[2]
111 10 SELECT csn[8] crc[2]
112 10 IDENTIFY asnb[8] crc[2]
113 10 PAGESEL block1[8] crc[2]
114 10 DETECT csn[8] crc[2]
115
116 These should not
117
118 4 CHECK chip_response[4]
119 8 READCHECK data[8]
120 1 ACTALL sof[1]
121 1 ACT sof[1]
122
123 In conclusion, without looking at the command; any response
124 of length 10 or 34 should have CRC
125 **/
126 if(len != 10 && len != 34) return true;
127
128 ComputeCrc14443(CRC_ICLASS, data, len-2, &b1, &b2);
129 return b1 == data[len -2] && b2 == data[len-1];
130 }
131 }
132
133
134 bool is_last_record(uint16_t tracepos, uint8_t *trace, uint16_t traceLen)
135 {
136 return(tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) >= traceLen);
137 }
138
139
140 bool next_record_is_response(uint16_t tracepos, uint8_t *trace)
141 {
142 uint16_t next_records_datalen = *((uint16_t *)(trace + tracepos + sizeof(uint32_t) + sizeof(uint16_t)));
143
144 return(next_records_datalen & 0x8000);
145 }
146
147
148 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)
149 {
150
151 #define MAX_TOPAZ_READER_CMD_LEN 16
152
153 uint32_t last_timestamp = timestamp + *duration;
154
155 if ((*data_len != 1) || (frame[0] == TOPAZ_WUPA) || (frame[0] == TOPAZ_REQA)) return false;
156
157 memcpy(topaz_reader_command, frame, *data_len);
158
159 while (!is_last_record(*tracepos, trace, traceLen) && !next_record_is_response(*tracepos, trace)) {
160 uint32_t next_timestamp = *((uint32_t *)(trace + *tracepos));
161 *tracepos += sizeof(uint32_t);
162 uint16_t next_duration = *((uint16_t *)(trace + *tracepos));
163 *tracepos += sizeof(uint16_t);
164 uint16_t next_data_len = *((uint16_t *)(trace + *tracepos)) & 0x7FFF;
165 *tracepos += sizeof(uint16_t);
166 uint8_t *next_frame = (trace + *tracepos);
167 *tracepos += next_data_len;
168 if ((next_data_len == 1) && (*data_len + next_data_len <= MAX_TOPAZ_READER_CMD_LEN)) {
169 memcpy(topaz_reader_command + *data_len, next_frame, next_data_len);
170 *data_len += next_data_len;
171 last_timestamp = next_timestamp + next_duration;
172 } else {
173 // rewind and exit
174 *tracepos = *tracepos - next_data_len - sizeof(uint16_t) - sizeof(uint16_t) - sizeof(uint32_t);
175 break;
176 }
177 uint16_t next_parity_len = (next_data_len-1)/8 + 1;
178 *tracepos += next_parity_len;
179 }
180
181 *duration = last_timestamp - timestamp;
182
183 return true;
184 }
185
186
187 uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace, uint8_t protocol, bool showWaitCycles, bool markCRCBytes)
188 {
189 bool isResponse;
190 uint16_t data_len, parity_len;
191 uint32_t duration;
192 uint8_t topaz_reader_command[9];
193 uint32_t timestamp, first_timestamp, EndOfTransmissionTimestamp;
194 char explanation[30] = {0};
195 uint8_t mfData[32] = {0};
196 size_t mfDataLen = 0;
197
198 if (tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen) return traceLen;
199
200 first_timestamp = *((uint32_t *)(trace));
201 timestamp = *((uint32_t *)(trace + tracepos));
202
203 tracepos += 4;
204 duration = *((uint16_t *)(trace + tracepos));
205 tracepos += 2;
206 data_len = *((uint16_t *)(trace + tracepos));
207 tracepos += 2;
208
209 if (data_len & 0x8000) {
210 data_len &= 0x7fff;
211 isResponse = true;
212 } else {
213 isResponse = false;
214 }
215 parity_len = (data_len-1)/8 + 1;
216
217 if (tracepos + data_len + parity_len > traceLen) {
218 return traceLen;
219 }
220 uint8_t *frame = trace + tracepos;
221 tracepos += data_len;
222 uint8_t *parityBytes = trace + tracepos;
223 tracepos += parity_len;
224
225 if (protocol == TOPAZ && !isResponse) {
226 // topaz reader commands come in 1 or 9 separate frames with 7 or 8 Bits each.
227 // merge them:
228 if (merge_topaz_reader_frames(timestamp, &duration, &tracepos, traceLen, trace, frame, topaz_reader_command, &data_len)) {
229 frame = topaz_reader_command;
230 }
231 }
232
233 //Check the CRC status
234 uint8_t crcStatus = 2;
235
236 if (data_len > 2) {
237 switch (protocol) {
238 case ICLASS:
239 crcStatus = iclass_CRC_check(isResponse, frame, data_len);
240 break;
241 case ISO_14443B:
242 case TOPAZ:
243 crcStatus = iso14443B_CRC_check(isResponse, frame, data_len);
244 break;
245 case PROTO_MIFARE:
246 crcStatus = mifare_CRC_check(isResponse, frame, data_len);
247 break;
248 case ISO_14443A:
249 crcStatus = iso14443A_CRC_check(isResponse, frame, data_len);
250 break;
251 default:
252 break;
253 }
254 }
255 //0 CRC-command, CRC not ok
256 //1 CRC-command, CRC ok
257 //2 Not crc-command
258
259 //--- Draw the data column
260 //char line[16][110];
261 char line[16][110];
262
263 for (int j = 0; j < data_len && j/16 < 16; j++) {
264
265 uint8_t parityBits = parityBytes[j>>3];
266 if (protocol != ISO_14443B && (isResponse || protocol == ISO_14443A) && (oddparity8(frame[j]) != ((parityBits >> (7-(j&0x0007))) & 0x01))) {
267 snprintf(line[j/16]+(( j % 16) * 4), 110, " %02x!", frame[j]);
268 } else {
269 snprintf(line[j/16]+(( j % 16) * 4), 110, " %02x ", frame[j]);
270 }
271
272 }
273
274 if (markCRCBytes) {
275 if(crcStatus == 0 || crcStatus == 1)
276 {//CRC-command
277 char *pos1 = line[(data_len-2)/16]+(((data_len-2) % 16) * 4);
278 (*pos1) = '[';
279 char *pos2 = line[(data_len)/16]+(((data_len) % 16) * 4);
280 sprintf(pos2, "%c", ']');
281 }
282 }
283
284 if (data_len == 0) {
285 sprintf(line[0]," <empty trace - possible error>");
286 }
287
288 //--- Draw the CRC column
289 char *crc = (crcStatus == 0 ? "!crc" : (crcStatus == 1 ? " ok " : " "));
290
291 EndOfTransmissionTimestamp = timestamp + duration;
292
293 if (protocol == PROTO_MIFARE)
294 annotateMifare(explanation, sizeof(explanation), frame, data_len, parityBytes, parity_len, isResponse);
295
296 if(!isResponse)
297 {
298 switch(protocol) {
299 case ICLASS: annotateIclass(explanation,sizeof(explanation),frame,data_len); break;
300 case ISO_14443A: annotateIso14443a(explanation,sizeof(explanation),frame,data_len); break;
301 case ISO_14443B: annotateIso14443b(explanation,sizeof(explanation),frame,data_len); break;
302 case TOPAZ: annotateTopaz(explanation,sizeof(explanation),frame,data_len); break;
303 default: break;
304 }
305 }
306
307 int num_lines = MIN((data_len - 1)/16 + 1, 16);
308 for (int j = 0; j < num_lines ; j++) {
309 if (j == 0) {
310 PrintAndLog(" %10d | %10d | %s |%-64s | %s| %s",
311 (timestamp - first_timestamp),
312 (EndOfTransmissionTimestamp - first_timestamp),
313 (isResponse ? "Tag" : "Rdr"),
314 line[j],
315 (j == num_lines-1) ? crc : " ",
316 (j == num_lines-1) ? explanation : "");
317 } else {
318 PrintAndLog(" | | |%-64s | %s| %s",
319 line[j],
320 (j == num_lines-1) ? crc : " ",
321 (j == num_lines-1) ? explanation : "");
322 }
323 }
324
325 if (DecodeMifareData(frame, data_len, parityBytes, isResponse, mfData, &mfDataLen)) {
326 memset(explanation, 0x00, sizeof(explanation));
327 if (!isResponse) {
328 explanation[0] = '>';
329 annotateIso14443a(&explanation[1], sizeof(explanation) - 1, mfData, mfDataLen);
330 }
331 uint8_t crcc = iso14443A_CRC_check(isResponse, mfData, mfDataLen);
332 PrintAndLog(" | * | dec |%-64s | %-4s| %s",
333 sprint_hex(mfData, mfDataLen),
334 (crcc == 0 ? "!crc" : (crcc == 1 ? " ok " : " ")),
335 (true) ? explanation : "");
336 };
337
338 if (is_last_record(tracepos, trace, traceLen)) return traceLen;
339
340 if (showWaitCycles && !isResponse && next_record_is_response(tracepos, trace)) {
341 uint32_t next_timestamp = *((uint32_t *)(trace + tracepos));
342 PrintAndLog(" %10d | %10d | %s | fdt (Frame Delay Time): %d",
343 (EndOfTransmissionTimestamp - first_timestamp),
344 (next_timestamp - first_timestamp),
345 " ",
346 (next_timestamp - EndOfTransmissionTimestamp));
347 }
348
349 return tracepos;
350 }
351
352
353 int CmdHFList(const char *Cmd)
354 {
355 #ifdef WITH_SMARTCARD
356 PrintAndLog("TEST_WITH_SMARTCARD");
357 #endif
358 #ifdef WITH_TEST
359 PrintAndLog("TEST_WITH_TEST");
360 #endif
361 bool showWaitCycles = false;
362 bool markCRCBytes = false;
363 bool loadFromFile = false;
364 bool saveToFile = false;
365 char param1 = '\0';
366 char param2 = '\0';
367 char param3 = '\0';
368 char type[40] = {0};
369 char filename[FILE_PATH_SIZE] = {0};
370 uint8_t protocol = 0;
371
372 // parse command line
373 int tlen = param_getstr(Cmd, 0, type, sizeof(type));
374 if (param_getlength(Cmd, 1) == 1) {
375 param1 = param_getchar(Cmd, 1);
376 } else {
377 param_getstr(Cmd, 1, filename, sizeof(filename));
378 }
379 if (param_getlength(Cmd, 2) == 1) {
380 param2 = param_getchar(Cmd, 2);
381 } else if (strlen(filename) == 0) {
382 param_getstr(Cmd, 2, filename, sizeof(filename));
383 }
384 if (param_getlength(Cmd, 3) == 1) {
385 param3 = param_getchar(Cmd, 3);
386 } else if (strlen(filename) == 0) {
387 param_getstr(Cmd, 3, filename, sizeof(filename));
388 }
389
390 // Validate param1
391 bool errors = false;
392
393 if(tlen == 0) {
394 errors = true;
395 }
396
397 if(param1 == 'h'
398 || (param1 != 0 && param1 != 'f' && param1 != 'c' && param1 != 'l')
399 || (param2 != 0 && param2 != 'f' && param2 != 'c' && param2 != 'l')
400 || (param3 != 0 && param3 != 'f' && param3 != 'c' && param3 != 'l')) {
401 errors = true;
402 }
403
404 if(!errors) {
405 if(strcmp(type, "iclass") == 0) {
406 protocol = ICLASS;
407 } else if(strcmp(type, "mf") == 0) {
408 protocol = PROTO_MIFARE;
409 } else if(strcmp(type, "14a") == 0) {
410 protocol = ISO_14443A;
411 } else if(strcmp(type, "14b") == 0) {
412 protocol = ISO_14443B;
413 } else if(strcmp(type,"topaz") == 0) {
414 protocol = TOPAZ;
415 } else if(strcmp(type, "7816") == 0) {
416 protocol = ISO_7816_4;
417 } else if(strcmp(type,"raw") == 0) {
418 protocol = -1; //No crc, no annotations
419 } else if (strcmp(type, "save") == 0) {
420 saveToFile = true;
421 } else {
422 errors = true;
423 }
424 }
425
426 if (param1 == 'f' || param2 == 'f' || param3 == 'f') {
427 showWaitCycles = true;
428 }
429
430 if (param1 == 'c' || param2 == 'c' || param3 == 'c') {
431 markCRCBytes = true;
432 }
433
434 if (param1 == 'l' || param2 == 'l' || param3 == 'l') {
435 loadFromFile = true;
436 }
437
438 if ((loadFromFile || saveToFile) && strlen(filename) == 0) {
439 errors = true;
440 }
441
442 if (loadFromFile && saveToFile) {
443 errors = true;
444 }
445
446 if (errors) {
447 PrintAndLog("List or save protocol data.");
448 PrintAndLog("Usage: hf list <protocol> [f] [c] [l <filename>]");
449 PrintAndLog(" hf list save <filename>");
450 PrintAndLog(" f - show frame delay times as well");
451 PrintAndLog(" c - mark CRC bytes");
452 PrintAndLog(" l - load data from file instead of trace buffer");
453 PrintAndLog(" save - save data to file");
454 PrintAndLog("Supported <protocol> values:");
455 PrintAndLog(" raw - just show raw data without annotations");
456 PrintAndLog(" 14a - interpret data as iso14443a communications");
457 PrintAndLog(" mf - interpret data as iso14443a communications and decrypt crypto1 stream");
458 PrintAndLog(" 14b - interpret data as iso14443b communications");
459 PrintAndLog(" iclass - interpret data as iclass communications");
460 PrintAndLog(" topaz - interpret data as topaz communications");
461 PrintAndLog("");
462 PrintAndLog("example: hf list 14a f");
463 PrintAndLog("example: hf list iclass");
464 PrintAndLog("example: hf list save myCardTrace.trc");
465 PrintAndLog("example: hf list 14a l myCardTrace.trc");
466 return 0;
467 }
468
469
470 uint8_t *trace;
471 uint32_t tracepos = 0;
472 uint32_t traceLen = 0;
473
474 if (loadFromFile) {
475 #define TRACE_CHUNK_SIZE (1<<16) // 64K to start with. Will be enough for BigBuf and some room for future extensions
476 FILE *tracefile = NULL;
477 size_t bytes_read;
478 trace = malloc(TRACE_CHUNK_SIZE);
479 if (trace == NULL) {
480 PrintAndLog("Cannot allocate memory for trace");
481 return 2;
482 }
483 if ((tracefile = fopen(filename,"rb")) == NULL) {
484 PrintAndLog("Could not open file %s", filename);
485 free(trace);
486 return 0;
487 }
488 while (!feof(tracefile)) {
489 bytes_read = fread(trace+traceLen, 1, TRACE_CHUNK_SIZE, tracefile);
490 traceLen += bytes_read;
491 if (!feof(tracefile)) {
492 uint8_t *p = realloc(trace, traceLen + TRACE_CHUNK_SIZE);
493 if (p == NULL) {
494 PrintAndLog("Cannot allocate memory for trace");
495 free(trace);
496 fclose(tracefile);
497 return 2;
498 }
499 trace = p;
500 }
501 }
502 fclose(tracefile);
503 } else {
504 trace = malloc(USB_CMD_DATA_SIZE);
505 // Query for the size of the trace
506 UsbCommand response;
507 GetFromBigBuf(trace, USB_CMD_DATA_SIZE, 0, &response, -1, false);
508 traceLen = response.arg[2];
509 if (traceLen > USB_CMD_DATA_SIZE) {
510 uint8_t *p = realloc(trace, traceLen);
511 if (p == NULL) {
512 PrintAndLog("Cannot allocate memory for trace");
513 free(trace);
514 return 2;
515 }
516 trace = p;
517 GetFromBigBuf(trace, traceLen, 0, NULL, -1, false);
518 }
519 }
520
521 if (saveToFile) {
522 FILE *tracefile = NULL;
523 if ((tracefile = fopen(filename,"wb")) == NULL) {
524 PrintAndLog("Could not create file %s", filename);
525 return 1;
526 }
527 fwrite(trace, 1, traceLen, tracefile);
528 PrintAndLog("Recorded Activity (TraceLen = %d bytes) written to file %s", traceLen, filename);
529 fclose(tracefile);
530 } else {
531 PrintAndLog("Recorded Activity (TraceLen = %d bytes)", traceLen);
532 PrintAndLog("");
533 PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
534 PrintAndLog("iso14443a - All times are in carrier periods (1/13.56Mhz)");
535 PrintAndLog("iClass - Timings are not as accurate");
536 PrintAndLog("");
537 PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |");
538 PrintAndLog("------------|------------|-----|-----------------------------------------------------------------|-----|--------------------|");
539
540 ClearAuthData();
541 while(tracepos < traceLen)
542 {
543 tracepos = printTraceLine(tracepos, traceLen, trace, protocol, showWaitCycles, markCRCBytes);
544 }
545 }
546
547 free(trace);
548 return 0;
549 }
550
551 int CmdHFSearch(const char *Cmd){
552 int ans = 0;
553 PrintAndLog("");
554 ans = CmdHF14AInfo("s");
555 if (ans > 0) {
556 PrintAndLog("\nValid ISO14443A Tag Found - Quiting Search\n");
557 return ans;
558 }
559 ans = HFiClassReader("", false, false);
560 if (ans) {
561 PrintAndLog("\nValid iClass Tag (or PicoPass Tag) Found - Quiting Search\n");
562 return ans;
563 }
564 ans = HF15Reader("", false);
565 if (ans) {
566 PrintAndLog("\nValid ISO15693 Tag Found - Quiting Search\n");
567 return ans;
568 }
569 //14b is longest test currently (and rarest chip type) ... put last
570 ans = HF14BInfo(false);
571 if (ans) {
572 PrintAndLog("\nValid ISO14443B Tag Found - Quiting Search\n");
573 return ans;
574 }
575 PrintAndLog("\nno known/supported 13.56 MHz tags found\n");
576 return 0;
577 }
578
579 int CmdHFSnoop(const char *Cmd)
580 {
581 char * pEnd;
582 UsbCommand c = {CMD_HF_SNIFFER, {strtol(Cmd, &pEnd,0),strtol(pEnd, &pEnd,0),0}};
583 SendCommand(&c);
584 return 0;
585 }
586
587 static command_t CommandTable[] =
588 {
589 {"help", CmdHelp, 1, "This help"},
590 {"14a", CmdHF14A, 1, "{ ISO14443A RFIDs... }"},
591 {"14b", CmdHF14B, 1, "{ ISO14443B RFIDs... }"},
592 {"15", CmdHF15, 1, "{ ISO15693 RFIDs... }"},
593 {"epa", CmdHFEPA, 1, "{ German Identification Card... }"},
594 {"legic", CmdHFLegic, 0, "{ LEGIC RFIDs... }"},
595 {"iclass", CmdHFiClass, 1, "{ ICLASS RFIDs... }"},
596 {"mf", CmdHFMF, 1, "{ MIFARE RFIDs... }"},
597 {"mfu", CmdHFMFUltra, 1, "{ MIFARE Ultralight RFIDs... }"},
598 {"mfp", CmdHFMFP, 1, "{ MIFARE Plus RFIDs... }"},
599 {"topaz", CmdHFTopaz, 1, "{ TOPAZ (NFC Type 1) RFIDs... }"},
600 {"fido", CmdHFFido, 1, "{ FIDO and FIDO2 authenticators... }"},
601 {"tune", CmdHFTune, 0, "Continuously measure HF antenna tuning"},
602 {"list", CmdHFList, 1, "List protocol data in trace buffer"},
603 {"search", CmdHFSearch, 1, "Search for known HF tags [preliminary]"},
604 {"snoop", CmdHFSnoop, 0, "<samples to skip (10000)> <triggers to skip (1)> Generic HF Snoop"},
605 {NULL, NULL, 0, NULL}
606 };
607
608 int CmdHF(const char *Cmd)
609 {
610 CmdsParse(CommandTable, Cmd);
611 return 0;
612 }
613
614 int CmdHelp(const char *Cmd)
615 {
616 CmdsHelp(CommandTable);
617 return 0;
618 }
Impressum, Datenschutz