]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhf.c
added cmdhflist file
[proxmark3-svn] / client / cmdhf.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4f131b53 3// Merlok - 2017
a553f267 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
acf0582d 12#include <stdlib.h>
7fe9b0b7 13#include <stdio.h>
4c3de57a 14#include <string.h>
902cb3c0 15#include "proxmark3.h"
7cb8516c 16#include "util.h"
acf0582d 17#include "data.h"
7fe9b0b7 18#include "ui.h"
7cb8516c 19#include "iso14443crc.h"
1f065e1d 20#include "parity.h"
7cb8516c 21#include "cmdmain.h"
7fe9b0b7 22#include "cmdparser.h"
23#include "cmdhf.h"
24#include "cmdhf14a.h"
25#include "cmdhf14b.h"
26#include "cmdhf15.h"
5acd09bd 27#include "cmdhfepa.h"
7fe9b0b7 28#include "cmdhflegic.h"
cee5a30d 29#include "cmdhficlass.h"
9ca155ba 30#include "cmdhfmf.h"
5ee70129 31#include "cmdhfmfu.h"
05ddb52c 32#include "cmdhftopaz.h"
b67f7ec3 33#include "protocols.h"
3c5fce2b 34#include "emv/cmdemv.h"
4f131b53 35#include "cmdhflist.h"
7fe9b0b7 36
37static int CmdHelp(const char *Cmd);
38
39int CmdHFTune(const char *Cmd)
40{
41 UsbCommand c={CMD_MEASURE_ANTENNA_TUNING_HF};
42 SendCommand(&c);
43 return 0;
44}
4c3de57a 45
4c3de57a
MHS
46
47void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
48{
49 switch(cmd[0])
50 {
41fdd0f0
MHS
51 case ISO14443A_CMD_WUPA: snprintf(exp,size,"WUPA"); break;
52 case ISO14443A_CMD_ANTICOLL_OR_SELECT:{
4df54240
MHS
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)
383608a6 55 if(cmd[1] == 0x70)
4c3de57a
MHS
56 {
57 snprintf(exp,size,"SELECT_UID"); break;
58 }else
59 {
4df54240
MHS
60 snprintf(exp,size,"ANTICOLL"); break;
61 }
62 }
41fdd0f0 63 case ISO14443A_CMD_ANTICOLL_OR_SELECT_2:{
4df54240
MHS
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;
4c3de57a
MHS
72 }
73 }
16a95d76 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;
75377d29 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 }
16a95d76 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;
75377d29 95 case MIFARE_ULC_AUTH_1: snprintf(exp,size,"AUTH "); break;
96 case MIFARE_ULC_AUTH_2: snprintf(exp,size,"AUTH_ANSW"); break;
a2e2bb8a 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;
75377d29 103 case MIFARE_ULEV1_FASTREAD:{
6ce0e538 104 if ( cmdsize >=3 && cmd[2] <= 0xE6)
75377d29 105 snprintf(exp,size,"READ RANGE (%d-%d)",cmd[1],cmd[2]);
106 else
107 snprintf(exp,size,"?");
108 break;
109 }
c585a5cf 110 case MIFARE_ULC_WRITE:{
75377d29 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;
16a95d76 134 default: snprintf(exp,size,"?"); break;
4c3de57a
MHS
135 }
136 return;
137}
138
139void annotateIclass(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
140{
4c3de57a
MHS
141 switch(cmd[0])
142 {
337818f7 143 case ICLASS_CMD_ACTALL: snprintf(exp,size,"ACTALL"); break;
4df54240
MHS
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 }
337818f7 152 case ICLASS_CMD_SELECT: snprintf(exp,size,"SELECT"); break;
49726b40
MHS
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;
337818f7 156 case ICLASS_CMD_CHECK: snprintf(exp,size,"CHECK"); break;
49726b40 157 case ICLASS_CMD_DETECT: snprintf(exp,size,"DETECT"); break;
337818f7 158 case ICLASS_CMD_HALT: snprintf(exp,size,"HALT"); break;
49726b40
MHS
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;
337818f7 162 default: snprintf(exp,size,"?"); break;
4c3de57a
MHS
163 }
164 return;
165}
166
4df54240
MHS
167void 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;
4c3de57a 176
4df54240
MHS
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;
41fdd0f0 194 default: snprintf(exp,size,"?"); break;
4df54240
MHS
195 }
196 }
197}
08e8317c 198
ee1eadee 199
200void annotateTopaz(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
201{
ee1eadee 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;
48ece4a7 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;
ee1eadee 214 default: snprintf(exp,size,"?"); break;
215 }
216}
217
218
08e8317c
MHS
219/**
22006 00 = INITIATE
2210E xx = SELECT ID (xx = Chip-ID)
2220B = Get UID
22308 yy = Read Block (yy = block number)
22409 yy dd dd dd dd = Write Block (yy = block number; dd dd dd dd = data to be written)
2250C = Reset to Inventory
2260F = Completion
2270A 11 22 33 44 55 66 = Authenticate (11 22 33 44 55 66 = data to authenticate)
228**/
229
41fdd0f0
MHS
230void 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;
08e8317c
MHS
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;
41fdd0f0
MHS
245 }
246
247}
248
249/**
ef00343c 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
259uint8_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
41fdd0f0
MHS
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
286uint8_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]) {
ef00343c 294 return 0;
295 } else {
296 return 1;
41fdd0f0 297 }
41fdd0f0
MHS
298}
299
49726b40
MHS
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 */
41fdd0f0 309uint8_t iclass_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
49726b40
MHS
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}
4c3de57a 360
ee1eadee 361
a8904ebd 362bool is_last_record(uint16_t tracepos, uint8_t *trace, uint16_t traceLen)
4c3de57a 363{
a8904ebd 364 return(tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) >= traceLen);
365}
366
367
368bool 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
ef00343c 376bool 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)
a8904ebd 377{
378
48ece4a7 379#define MAX_TOPAZ_READER_CMD_LEN 16
a8904ebd 380
381 uint32_t last_timestamp = timestamp + *duration;
382
ef00343c 383 if ((*data_len != 1) || (frame[0] == TOPAZ_WUPA) || (frame[0] == TOPAZ_REQA)) return false;
4c3de57a 384
ef00343c 385 memcpy(topaz_reader_command, frame, *data_len);
a8904ebd 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);
ef00343c 390 uint16_t next_duration = *((uint16_t *)(trace + *tracepos));
a8904ebd 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;
ef00343c 396 if ((next_data_len == 1) && (*data_len + next_data_len <= MAX_TOPAZ_READER_CMD_LEN)) {
a8904ebd 397 memcpy(topaz_reader_command + *data_len, next_frame, next_data_len);
398 *data_len += next_data_len;
ef00343c 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 }
a8904ebd 405 uint16_t next_parity_len = (next_data_len-1)/8 + 1;
406 *tracepos += next_parity_len;
407 }
408
409 *duration = last_timestamp - timestamp;
ef00343c 410
411 return true;
ee1eadee 412}
413
414
05ddb52c 415uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace, uint8_t protocol, bool showWaitCycles, bool markCRCBytes)
4c3de57a
MHS
416{
417 bool isResponse;
a8904ebd 418 uint16_t data_len, parity_len;
419 uint32_t duration;
ee1eadee 420 uint8_t topaz_reader_command[9];
4c3de57a
MHS
421 uint32_t timestamp, first_timestamp, EndOfTransmissionTimestamp;
422 char explanation[30] = {0};
423
f71f4deb 424 if (tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen) return traceLen;
425
4c3de57a
MHS
426 first_timestamp = *((uint32_t *)(trace));
427 timestamp = *((uint32_t *)(trace + tracepos));
4c3de57a
MHS
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
f71f4deb 443 if (tracepos + data_len + parity_len > traceLen) {
444 return traceLen;
4c3de57a 445 }
4c3de57a
MHS
446 uint8_t *frame = trace + tracepos;
447 tracepos += data_len;
448 uint8_t *parityBytes = trace + tracepos;
449 tracepos += parity_len;
450
ee1eadee 451 if (protocol == TOPAZ && !isResponse) {
ef00343c 452 // topaz reader commands come in 1 or 9 separate frames with 7 or 8 Bits each.
ee1eadee 453 // merge them:
ef00343c 454 if (merge_topaz_reader_frames(timestamp, &duration, &tracepos, traceLen, trace, frame, topaz_reader_command, &data_len)) {
455 frame = topaz_reader_command;
456 }
ee1eadee 457 }
458
27eabcdc
MHS
459 //Check the CRC status
460 uint8_t crcStatus = 2;
461
462 if (data_len > 2) {
ee1eadee 463 switch (protocol) {
464 case ICLASS:
465 crcStatus = iclass_CRC_check(isResponse, frame, data_len);
466 break;
467 case ISO_14443B:
ef00343c 468 case TOPAZ:
a8904ebd 469 crcStatus = iso14443B_CRC_check(isResponse, frame, data_len);
ee1eadee 470 break;
4f131b53 471 case PROTO_MIFARE:
ee1eadee 472 case ISO_14443A:
ef00343c 473 crcStatus = iso14443A_CRC_check(isResponse, frame, data_len);
ee1eadee 474 break;
475 default:
476 break;
27eabcdc
MHS
477 }
478 }
479 //0 CRC-command, CRC not ok
480 //1 CRC-command, CRC ok
481 //2 Not crc-command
9e8255d4 482
4c3de57a 483 //--- Draw the data column
9e8255d4 484 //char line[16][110];
4c3de57a 485 char line[16][110];
9e8255d4
MHS
486
487 for (int j = 0; j < data_len && j/16 < 16; j++) {
488
4c3de57a 489 uint8_t parityBits = parityBytes[j>>3];
1f065e1d 490 if (protocol != ISO_14443B && (isResponse || protocol == ISO_14443A) && (oddparity8(frame[j]) != ((parityBits >> (7-(j&0x0007))) & 0x01))) {
9e8255d4 491 snprintf(line[j/16]+(( j % 16) * 4),110, "%02x! ", frame[j]);
4c3de57a 492 } else {
ef00343c 493 snprintf(line[j/16]+(( j % 16) * 4), 110, " %02x ", frame[j]);
9e8255d4 494 }
27eabcdc
MHS
495
496 }
05ddb52c 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 }
9e8255d4 506 }
05ddb52c 507
9e8255d4
MHS
508 if(data_len == 0)
509 {
510 if(data_len == 0){
511 sprintf(line[0],"<empty trace - possible error>");
4c3de57a
MHS
512 }
513 }
514 //--- Draw the CRC column
4c3de57a 515
49726b40 516 char *crc = (crcStatus == 0 ? "!crc" : (crcStatus == 1 ? " ok " : " "));
4c3de57a
MHS
517
518 EndOfTransmissionTimestamp = timestamp + duration;
519
520 if(!isResponse)
521 {
ee1eadee 522 switch(protocol) {
523 case ICLASS: annotateIclass(explanation,sizeof(explanation),frame,data_len); break;
4f131b53 524 case PROTO_MIFARE:
ee1eadee 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 }
4c3de57a
MHS
530 }
531
9e8255d4
MHS
532 int num_lines = MIN((data_len - 1)/16 + 1, 16);
533 for (int j = 0; j < num_lines ; j++) {
4c3de57a 534 if (j == 0) {
48ece4a7 535 PrintAndLog(" %10d | %10d | %s |%-64s | %s| %s",
4c3de57a
MHS
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 {
48ece4a7 543 PrintAndLog(" | | |%-64s | %s| %s",
4c3de57a 544 line[j],
ee1eadee 545 (j == num_lines-1) ? crc : " ",
4c3de57a
MHS
546 (j == num_lines-1) ? explanation : "");
547 }
548 }
549
a8904ebd 550 if (is_last_record(tracepos, trace, traceLen)) return traceLen;
f71f4deb 551
a8904ebd 552 if (showWaitCycles && !isResponse && next_record_is_response(tracepos, trace)) {
4c3de57a 553 uint32_t next_timestamp = *((uint32_t *)(trace + tracepos));
275d9e61 554 PrintAndLog(" %10d | %10d | %s | fdt (Frame Delay Time): %d",
a8904ebd 555 (EndOfTransmissionTimestamp - first_timestamp),
556 (next_timestamp - first_timestamp),
557 " ",
558 (next_timestamp - EndOfTransmissionTimestamp));
4c3de57a 559 }
f71f4deb 560
4c3de57a
MHS
561 return tracepos;
562}
563
f71f4deb 564
4c3de57a
MHS
565int CmdHFList(const char *Cmd)
566{
567 bool showWaitCycles = false;
05ddb52c 568 bool markCRCBytes = false;
4c3de57a 569 char type[40] = {0};
874572d4 570 int tlen = param_getstr(Cmd,0,type, sizeof(type));
05ddb52c 571 char param1 = param_getchar(Cmd, 1);
572 char param2 = param_getchar(Cmd, 2);
4c3de57a 573 bool errors = false;
b689b842 574 uint8_t protocol = 0;
4c3de57a 575 //Validate params
05ddb52c 576
577 if(tlen == 0) {
4c3de57a
MHS
578 errors = true;
579 }
05ddb52c 580
581 if(param1 == 'h'
582 || (param1 != 0 && param1 != 'f' && param1 != 'c')
583 || (param2 != 0 && param2 != 'f' && param2 != 'c')) {
4c3de57a
MHS
584 errors = true;
585 }
05ddb52c 586
587 if(!errors) {
ee1eadee 588 if(strcmp(type, "iclass") == 0) {
b689b842 589 protocol = ICLASS;
4f131b53 590 } else if(strcmp(type, "mf") == 0) {
591 protocol = PROTO_MIFARE;
ee1eadee 592 } else if(strcmp(type, "14a") == 0) {
b689b842 593 protocol = ISO_14443A;
ee1eadee 594 } else if(strcmp(type, "14b") == 0) {
b689b842 595 protocol = ISO_14443B;
ef00343c 596 } else if(strcmp(type,"topaz")== 0) {
ee1eadee 597 protocol = TOPAZ;
598 } else if(strcmp(type,"raw")== 0) {
b689b842 599 protocol = -1;//No crc, no annotations
ee1eadee 600 } else {
b689b842
MHS
601 errors = true;
602 }
603 }
4c3de57a
MHS
604
605 if (errors) {
606 PrintAndLog("List protocol data in trace buffer.");
05ddb52c 607 PrintAndLog("Usage: hf list <protocol> [f][c]");
92623113 608 PrintAndLog(" f - show frame delay times as well");
05ddb52c 609 PrintAndLog(" c - mark CRC bytes");
92623113
MHS
610 PrintAndLog("Supported <protocol> values:");
611 PrintAndLog(" raw - just show raw data without annotations");
337818f7 612 PrintAndLog(" 14a - interpret data as iso14443a communications");
4f131b53 613 PrintAndLog(" mf - interpret data as iso14443a communications and decrypt crypto1 stream");
41fdd0f0 614 PrintAndLog(" 14b - interpret data as iso14443b communications");
4c3de57a 615 PrintAndLog(" iclass - interpret data as iclass communications");
ee1eadee 616 PrintAndLog(" topaz - interpret data as topaz communications");
4c3de57a
MHS
617 PrintAndLog("");
618 PrintAndLog("example: hf list 14a f");
619 PrintAndLog("example: hf list iclass");
620 return 0;
621 }
b689b842 622
4c3de57a 623
05ddb52c 624 if (param1 == 'f' || param2 == 'f') {
4c3de57a
MHS
625 showWaitCycles = true;
626 }
627
05ddb52c 628 if (param1 == 'c' || param2 == 'c') {
629 markCRCBytes = true;
630 }
4c3de57a 631
f71f4deb 632 uint8_t *trace;
4c3de57a 633 uint16_t tracepos = 0;
f71f4deb 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);
4c3de57a
MHS
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("");
de15fc5f 659 PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |");
660 PrintAndLog("------------|------------|-----|-----------------------------------------------------------------|-----|--------------------|");
4c3de57a 661
f71f4deb 662 while(tracepos < traceLen)
4c3de57a 663 {
05ddb52c 664 tracepos = printTraceLine(tracepos, traceLen, trace, protocol, showWaitCycles, markCRCBytes);
4c3de57a 665 }
f71f4deb 666
667 free(trace);
4c3de57a
MHS
668 return 0;
669}
670
8ceb6b03 671int CmdHFSearch(const char *Cmd){
672 int ans = 0;
6ce0e538 673 PrintAndLog("");
fe842bed 674 ans = CmdHF14AInfo("s");
6ce0e538 675 if (ans > 0) {
676 PrintAndLog("\nValid ISO14443A Tag Found - Quiting Search\n");
677 return ans;
ff4fdb32 678 }
aa53efc3 679 ans = HFiClassReader("", false, false);
ff4fdb32 680 if (ans) {
aa53efc3 681 PrintAndLog("\nValid iClass Tag (or PicoPass Tag) Found - Quiting Search\n");
ff4fdb32 682 return ans;
683 }
979c7655 684 ans = HF15Reader("", false);
6ce0e538 685 if (ans) {
979c7655 686 PrintAndLog("\nValid ISO15693 Tag Found - Quiting Search\n");
6ce0e538 687 return ans;
688 }
979c7655 689 //14b is longest test currently (and rarest chip type) ... put last
690 ans = HF14BInfo(false);
6ce0e538 691 if (ans) {
979c7655 692 PrintAndLog("\nValid ISO14443B Tag Found - Quiting Search\n");
6ce0e538 693 return ans;
694 }
ff4fdb32 695 PrintAndLog("\nno known/supported 13.56 MHz tags found\n");
8ceb6b03 696 return 0;
697}
7fe9b0b7 698
0472d76d 699int 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
7fe9b0b7 707static command_t CommandTable[] =
708{
05ddb52c 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... }"},
3c5fce2b 714 {"emv", CmdHFEMV, 1, "{ EMV cards... }"},
05ddb52c 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"},
7624e8b2 722 {"search", CmdHFSearch, 1, "Search for known HF tags [preliminary]"},
b2fe0e77 723 {"snoop", CmdHFSnoop, 0, "<samples to skip (10000)> <triggers to skip (1)> Generic HF Snoop"},
05ddb52c 724 {NULL, NULL, 0, NULL}
7fe9b0b7 725};
726
727int CmdHF(const char *Cmd)
728{
729 CmdsParse(CommandTable, Cmd);
730 return 0;
731}
732
733int CmdHelp(const char *Cmd)
734{
735 CmdsHelp(CommandTable);
736 return 0;
737}
Impressum, Datenschutz