]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhf.c
add: start to support Topaz tags
[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 "protocols.h"
27
28 static int CmdHelp(const char *Cmd);
29
30 int CmdHFTune(const char *Cmd)
31 {
32 UsbCommand c={CMD_MEASURE_ANTENNA_TUNING_HF};
33 SendCommand(&c);
34 return 0;
35 }
36
37
38 void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
39 {
40 switch(cmd[0])
41 {
42 case ISO14443A_CMD_WUPA: snprintf(exp,size,"WUPA"); break;
43 case ISO14443A_CMD_ANTICOLL_OR_SELECT:{
44 // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor)
45 // 93 70 = Select (usage: 9370+5bytes 9320 answer - answer: 1byte SAK)
46 if(cmd[1] == 0x70)
47 {
48 snprintf(exp,size,"SELECT_UID"); break;
49 }else
50 {
51 snprintf(exp,size,"ANTICOLL"); break;
52 }
53 }
54 case ISO14443A_CMD_ANTICOLL_OR_SELECT_2:{
55 //95 20 = Anticollision of cascade level2
56 //95 70 = Select of cascade level2
57 if(cmd[2] == 0x70)
58 {
59 snprintf(exp,size,"SELECT_UID-2"); break;
60 }else
61 {
62 snprintf(exp,size,"ANTICOLL-2"); break;
63 }
64 }
65 case ISO14443A_CMD_REQA: snprintf(exp,size,"REQA"); break;
66 case ISO14443A_CMD_READBLOCK: snprintf(exp,size,"READBLOCK(%d)",cmd[1]); break;
67 case ISO14443A_CMD_WRITEBLOCK: snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); break;
68 case ISO14443A_CMD_HALT: snprintf(exp,size,"HALT"); break;
69 case ISO14443A_CMD_RATS: snprintf(exp,size,"RATS"); break;
70 case MIFARE_CMD_INC: snprintf(exp,size,"INC(%d)",cmd[1]); break;
71 case MIFARE_CMD_DEC: snprintf(exp,size,"DEC(%d)",cmd[1]); break;
72 case MIFARE_CMD_RESTORE: snprintf(exp,size,"RESTORE(%d)",cmd[1]); break;
73 case MIFARE_CMD_TRANSFER: snprintf(exp,size,"TRANSFER(%d)",cmd[1]); break;
74 case MIFARE_AUTH_KEYA: snprintf(exp,size,"AUTH-A(%d)",cmd[1]); break;
75 case MIFARE_AUTH_KEYB: snprintf(exp,size,"AUTH-B(%d)",cmd[1]); break;
76 case MIFARE_MAGICWUPC1: snprintf(exp,size,"MAGIC WUPC1"); break;
77 case MIFARE_MAGICWUPC2: snprintf(exp,size,"MAGIC WUPC2"); break;
78 case MIFARE_MAGICWIPEC: snprintf(exp,size,"MAGIC WIPEC"); break;
79 default: snprintf(exp,size,"?"); break;
80 }
81 return;
82 }
83
84 void annotateIclass(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
85 {
86 switch(cmd[0])
87 {
88 case ICLASS_CMD_ACTALL: snprintf(exp,size,"ACTALL"); break;
89 case ICLASS_CMD_READ_OR_IDENTIFY:{
90 if(cmdsize > 1){
91 snprintf(exp,size,"READ(%d)",cmd[1]);
92 }else{
93 snprintf(exp,size,"IDENTIFY");
94 }
95 break;
96 }
97 case ICLASS_CMD_SELECT: snprintf(exp,size,"SELECT"); break;
98 case ICLASS_CMD_PAGESEL: snprintf(exp,size,"PAGESEL(%d)", cmd[1]); break;
99 case ICLASS_CMD_READCHECK_KC:snprintf(exp,size,"READCHECK[Kc](%d)", cmd[1]); break;
100 case ICLASS_CMD_READCHECK_KD:snprintf(exp,size,"READCHECK[Kd](%d)", cmd[1]); break;
101 case ICLASS_CMD_CHECK: snprintf(exp,size,"CHECK"); break;
102 case ICLASS_CMD_DETECT: snprintf(exp,size,"DETECT"); break;
103 case ICLASS_CMD_HALT: snprintf(exp,size,"HALT"); break;
104 case ICLASS_CMD_UPDATE: snprintf(exp,size,"UPDATE(%d)",cmd[1]); break;
105 case ICLASS_CMD_ACT: snprintf(exp,size,"ACT"); break;
106 case ICLASS_CMD_READ4: snprintf(exp,size,"READ4(%d)",cmd[1]); break;
107 default: snprintf(exp,size,"?"); break;
108 }
109 return;
110 }
111
112 void annotateIso15693(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
113 {
114
115 if(cmd[0] == 0x26)
116 {
117 switch(cmd[1]){
118 case ISO15693_INVENTORY :snprintf(exp, size, "INVENTORY");break;
119 case ISO15693_STAYQUIET :snprintf(exp, size, "STAY_QUIET");break;
120 default: snprintf(exp,size,"?"); break;
121
122 }
123 }else if(cmd[0] == 0x02)
124 {
125 switch(cmd[1])
126 {
127 case ISO15693_READBLOCK :snprintf(exp, size, "READBLOCK");break;
128 case ISO15693_WRITEBLOCK :snprintf(exp, size, "WRITEBLOCK");break;
129 case ISO15693_LOCKBLOCK :snprintf(exp, size, "LOCKBLOCK");break;
130 case ISO15693_READ_MULTI_BLOCK :snprintf(exp, size, "READ_MULTI_BLOCK");break;
131 case ISO15693_SELECT :snprintf(exp, size, "SELECT");break;
132 case ISO15693_RESET_TO_READY :snprintf(exp, size, "RESET_TO_READY");break;
133 case ISO15693_WRITE_AFI :snprintf(exp, size, "WRITE_AFI");break;
134 case ISO15693_LOCK_AFI :snprintf(exp, size, "LOCK_AFI");break;
135 case ISO15693_WRITE_DSFID :snprintf(exp, size, "WRITE_DSFID");break;
136 case ISO15693_LOCK_DSFID :snprintf(exp, size, "LOCK_DSFID");break;
137 case ISO15693_GET_SYSTEM_INFO :snprintf(exp, size, "GET_SYSTEM_INFO");break;
138 case ISO15693_READ_MULTI_SECSTATUS :snprintf(exp, size, "READ_MULTI_SECSTATUS");break;
139 default: snprintf(exp,size,"?"); break;
140 }
141 }
142 }
143
144
145 void annotateTopaz(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
146 {
147
148 switch(cmd[0]) {
149 case TOPAZ_REQA :snprintf(exp, size, "REQA");break;
150 case TOPAZ_WUPA :snprintf(exp, size, "WUPA");break;
151 case TOPAZ_RID :snprintf(exp, size, "RID");break;
152 case TOPAZ_RALL :snprintf(exp, size, "RALL");break;
153 case TOPAZ_READ :snprintf(exp, size, "READ");break;
154 case TOPAZ_WRITE_E :snprintf(exp, size, "WRITE-E");break;
155 case TOPAZ_WRITE_NE :snprintf(exp, size, "WRITE-NE");break;
156 default: snprintf(exp,size,"?"); break;
157 }
158 }
159
160
161 /**
162 06 00 = INITIATE
163 0E xx = SELECT ID (xx = Chip-ID)
164 0B = Get UID
165 08 yy = Read Block (yy = block number)
166 09 yy dd dd dd dd = Write Block (yy = block number; dd dd dd dd = data to be written)
167 0C = Reset to Inventory
168 0F = Completion
169 0A 11 22 33 44 55 66 = Authenticate (11 22 33 44 55 66 = data to authenticate)
170 **/
171
172 void annotateIso14443b(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
173 {
174 switch(cmd[0]){
175 case ISO14443B_REQB : snprintf(exp,size,"REQB");break;
176 case ISO14443B_ATTRIB : snprintf(exp,size,"ATTRIB");break;
177 case ISO14443B_HALT : snprintf(exp,size,"HALT");break;
178 case ISO14443B_INITIATE : snprintf(exp,size,"INITIATE");break;
179 case ISO14443B_SELECT : snprintf(exp,size,"SELECT(%d)",cmd[1]);break;
180 case ISO14443B_GET_UID : snprintf(exp,size,"GET UID");break;
181 case ISO14443B_READ_BLK : snprintf(exp,size,"READ_BLK(%d)", cmd[1]);break;
182 case ISO14443B_WRITE_BLK : snprintf(exp,size,"WRITE_BLK(%d)",cmd[1]);break;
183 case ISO14443B_RESET : snprintf(exp,size,"RESET");break;
184 case ISO14443B_COMPLETION : snprintf(exp,size,"COMPLETION");break;
185 case ISO14443B_AUTHENTICATE : snprintf(exp,size,"AUTHENTICATE");break;
186 default : snprintf(exp,size ,"?");break;
187 }
188
189 }
190
191 /**
192 * @brief iso14443B_CRC_Ok Checks CRC in command or response
193 * @param isResponse
194 * @param data
195 * @param len
196 * @return 0 : CRC-command, CRC not ok
197 * 1 : CRC-command, CRC ok
198 * 2 : Not crc-command
199 */
200
201 uint8_t iso14443B_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
202 {
203 uint8_t b1,b2;
204
205 if(len <= 2) return 2;
206
207 ComputeCrc14443(CRC_14443_B, data, len-2, &b1, &b2);
208 if(b1 != data[len-2] || b2 != data[len-1]) {
209 return 0;
210 }
211 return 1;
212 }
213
214 /**
215 * @brief iclass_CRC_Ok Checks CRC in command or response
216 * @param isResponse
217 * @param data
218 * @param len
219 * @return 0 : CRC-command, CRC not ok
220 * 1 : CRC-command, CRC ok
221 * 2 : Not crc-command
222 */
223 uint8_t iclass_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
224 {
225 if(len < 4) return 2;//CRC commands (and responses) are all at least 4 bytes
226
227 uint8_t b1, b2;
228
229 if(!isResponse)//Commands to tag
230 {
231 /**
232 These commands should have CRC. Total length leftmost
233 4 READ
234 4 READ4
235 12 UPDATE - unsecured, ends with CRC16
236 14 UPDATE - secured, ends with signature instead
237 4 PAGESEL
238 **/
239 if(len == 4 || len == 12)//Covers three of them
240 {
241 //Don't include the command byte
242 ComputeCrc14443(CRC_ICLASS, (data+1), len-3, &b1, &b2);
243 return b1 == data[len -2] && b2 == data[len-1];
244 }
245 return 2;
246 }else{
247 /**
248 These tag responses should have CRC. Total length leftmost
249
250 10 READ data[8] crc[2]
251 34 READ4 data[32]crc[2]
252 10 UPDATE data[8] crc[2]
253 10 SELECT csn[8] crc[2]
254 10 IDENTIFY asnb[8] crc[2]
255 10 PAGESEL block1[8] crc[2]
256 10 DETECT csn[8] crc[2]
257
258 These should not
259
260 4 CHECK chip_response[4]
261 8 READCHECK data[8]
262 1 ACTALL sof[1]
263 1 ACT sof[1]
264
265 In conclusion, without looking at the command; any response
266 of length 10 or 34 should have CRC
267 **/
268 if(len != 10 && len != 34) return true;
269
270 ComputeCrc14443(CRC_ICLASS, data, len-2, &b1, &b2);
271 return b1 == data[len -2] && b2 == data[len-1];
272 }
273 }
274
275
276 uint16_t merge_topaz_reader_frames(uint16_t tracepos, uint16_t traceLen, uint8_t *trace, uint8_t *topaz_reader_command, uint16_t *data_len)
277 {
278 return tracepos;
279 }
280
281
282 uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace, uint8_t protocol, bool showWaitCycles)
283 {
284 bool isResponse;
285 uint16_t duration, data_len, parity_len;
286 uint8_t topaz_reader_command[9];
287 uint32_t timestamp, first_timestamp, EndOfTransmissionTimestamp;
288 char explanation[30] = {0};
289
290 if (tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen) return traceLen;
291
292 first_timestamp = *((uint32_t *)(trace));
293 timestamp = *((uint32_t *)(trace + tracepos));
294
295 tracepos += 4;
296 duration = *((uint16_t *)(trace + tracepos));
297 tracepos += 2;
298 data_len = *((uint16_t *)(trace + tracepos));
299 tracepos += 2;
300
301 if (data_len & 0x8000) {
302 data_len &= 0x7fff;
303 isResponse = true;
304 } else {
305 isResponse = false;
306 }
307 parity_len = (data_len-1)/8 + 1;
308
309 if (tracepos + data_len + parity_len > traceLen) {
310 return traceLen;
311 }
312 uint8_t *frame = trace + tracepos;
313 tracepos += data_len;
314 uint8_t *parityBytes = trace + tracepos;
315 tracepos += parity_len;
316
317 if (protocol == TOPAZ && !isResponse) {
318 // topaz reader commands come in 1 or 9 separate frames with 8 Bits each.
319 // merge them:
320 tracepos = merge_topaz_reader_frames(tracepos, traceLen, trace, topaz_reader_command, &data_len);
321 }
322
323 //Check the CRC status
324 uint8_t crcStatus = 2;
325
326 if (data_len > 2) {
327 uint8_t b1, b2;
328 switch (protocol) {
329 case ICLASS:
330 crcStatus = iclass_CRC_check(isResponse, frame, data_len);
331 break;
332 case ISO_14443B:
333 case TOPAZ:
334 crcStatus = iso14443B_CRC_check(isResponse, topaz_reader_command, data_len);
335 break;
336 case ISO_14443A:
337 ComputeCrc14443(CRC_14443_A, frame, data_len-2, &b1, &b2);
338 if (b1 != frame[data_len-2] || b2 != frame[data_len-1]) {
339 if(!(isResponse & (data_len < 6))) {
340 crcStatus = 0;
341 }
342 }
343 break;
344 default:
345 break;
346 }
347 }
348 //0 CRC-command, CRC not ok
349 //1 CRC-command, CRC ok
350 //2 Not crc-command
351
352 //--- Draw the data column
353 //char line[16][110];
354 char line[16][110];
355
356 for (int j = 0; j < data_len && j/16 < 16; j++) {
357
358 int oddparity = 0x01;
359 int k;
360
361 for (k=0 ; k<8 ; k++) {
362 oddparity ^= (((frame[j] & 0xFF) >> k) & 0x01);
363 }
364 uint8_t parityBits = parityBytes[j>>3];
365 if (isResponse && (oddparity != ((parityBits >> (7-(j&0x0007))) & 0x01))) {
366 snprintf(line[j/16]+(( j % 16) * 4),110, "%02x! ", frame[j]);
367
368 } else {
369 snprintf(line[j/16]+(( j % 16) * 4),110, "%02x ", frame[j]);
370 }
371
372 }
373 if(crcStatus == 1)
374 {//CRC-command
375 char *pos1 = line[(data_len-2)/16]+(((data_len-2) % 16) * 4)-1;
376 (*pos1) = '[';
377 char *pos2 = line[(data_len)/16]+(((data_len) % 16) * 4)-2;
378 (*pos2) = ']';
379 }
380 if(data_len == 0)
381 {
382 if(data_len == 0){
383 sprintf(line[0],"<empty trace - possible error>");
384 }
385 }
386 //--- Draw the CRC column
387
388 char *crc = (crcStatus == 0 ? "!crc" : (crcStatus == 1 ? " ok " : " "));
389
390 EndOfTransmissionTimestamp = timestamp + duration;
391
392 if(!isResponse)
393 {
394 switch(protocol) {
395 case ICLASS: annotateIclass(explanation,sizeof(explanation),frame,data_len); break;
396 case ISO_14443A: annotateIso14443a(explanation,sizeof(explanation),frame,data_len); break;
397 case ISO_14443B: annotateIso14443b(explanation,sizeof(explanation),frame,data_len); break;
398 case TOPAZ: annotateTopaz(explanation,sizeof(explanation),frame,data_len); break;
399 default: break;
400 }
401 }
402
403 int num_lines = MIN((data_len - 1)/16 + 1, 16);
404 for (int j = 0; j < num_lines ; j++) {
405 if (j == 0) {
406 PrintAndLog(" %9d | %9d | %s | %-64s| %s| %s",
407 (timestamp - first_timestamp),
408 (EndOfTransmissionTimestamp - first_timestamp),
409 (isResponse ? "Tag" : "Rdr"),
410 line[j],
411 (j == num_lines-1) ? crc : " ",
412 (j == num_lines-1) ? explanation : "");
413 } else {
414 PrintAndLog(" | | | %-64s| %s| %s",
415 line[j],
416 (j == num_lines-1) ? crc : " ",
417 (j == num_lines-1) ? explanation : "");
418 }
419 }
420
421 if (tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen) return traceLen;
422
423 bool next_isResponse = *((uint16_t *)(trace + tracepos + 6)) & 0x8000;
424
425 if (showWaitCycles && !isResponse && next_isResponse) {
426 uint32_t next_timestamp = *((uint32_t *)(trace + tracepos));
427 if (next_timestamp != 0x44444444) {
428 PrintAndLog(" %9d | %9d | %s | fdt (Frame Delay Time): %d",
429 (EndOfTransmissionTimestamp - first_timestamp),
430 (next_timestamp - first_timestamp),
431 " ",
432 (next_timestamp - EndOfTransmissionTimestamp));
433 }
434 }
435
436 return tracepos;
437 }
438
439
440 int CmdHFList(const char *Cmd)
441 {
442 bool showWaitCycles = false;
443 char type[40] = {0};
444 int tlen = param_getstr(Cmd,0,type);
445 char param = param_getchar(Cmd, 1);
446 bool errors = false;
447 uint8_t protocol = 0;
448 //Validate params
449 if(tlen == 0)
450 {
451 errors = true;
452 }
453 if(param == 'h' || (param !=0 && param != 'f'))
454 {
455 errors = true;
456 }
457 if(!errors)
458 {
459 if(strcmp(type, "iclass") == 0) {
460 protocol = ICLASS;
461 } else if(strcmp(type, "14a") == 0) {
462 protocol = ISO_14443A;
463 } else if(strcmp(type, "14b") == 0) {
464 protocol = ISO_14443B;
465 } else if(strcmp(type,"topaz")== 0) {
466 protocol = TOPAZ;
467 } else if(strcmp(type,"raw")== 0) {
468 protocol = -1;//No crc, no annotations
469 } else {
470 errors = true;
471 }
472 }
473
474 if (errors) {
475 PrintAndLog("List protocol data in trace buffer.");
476 PrintAndLog("Usage: hf list <protocol> [f]");
477 PrintAndLog(" f - show frame delay times as well");
478 PrintAndLog("Supported <protocol> values:");
479 PrintAndLog(" raw - just show raw data without annotations");
480 PrintAndLog(" 14a - interpret data as iso14443a communications");
481 PrintAndLog(" 14b - interpret data as iso14443b communications");
482 PrintAndLog(" iclass - interpret data as iclass communications");
483 PrintAndLog(" topaz - interpret data as topaz communications");
484 PrintAndLog("");
485 PrintAndLog("example: hf list 14a f");
486 PrintAndLog("example: hf list iclass");
487 return 0;
488 }
489
490
491 if (param == 'f') {
492 showWaitCycles = true;
493 }
494
495
496 uint8_t *trace;
497 uint16_t tracepos = 0;
498 trace = malloc(USB_CMD_DATA_SIZE);
499
500 // Query for the size of the trace
501 UsbCommand response;
502 GetFromBigBuf(trace, USB_CMD_DATA_SIZE, 0);
503 WaitForResponse(CMD_ACK, &response);
504 uint16_t traceLen = response.arg[2];
505 if (traceLen > USB_CMD_DATA_SIZE) {
506 uint8_t *p = realloc(trace, traceLen);
507 if (p == NULL) {
508 PrintAndLog("Cannot allocate memory for trace");
509 free(trace);
510 return 2;
511 }
512 trace = p;
513 GetFromBigBuf(trace, traceLen, 0);
514 WaitForResponse(CMD_ACK, NULL);
515 }
516
517 PrintAndLog("Recorded Activity (TraceLen = %d bytes)", traceLen);
518 PrintAndLog("");
519 PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
520 PrintAndLog("iso14443a - All times are in carrier periods (1/13.56Mhz)");
521 PrintAndLog("iClass - Timings are not as accurate");
522 PrintAndLog("");
523 PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |");
524 PrintAndLog("-----------|-----------|-----|-----------------------------------------------------------------|-----|--------------------|");
525
526 while(tracepos < traceLen)
527 {
528 tracepos = printTraceLine(tracepos, traceLen, trace, protocol, showWaitCycles);
529 }
530
531 free(trace);
532 return 0;
533 }
534
535
536 static command_t CommandTable[] =
537 {
538 {"help", CmdHelp, 1, "This help"},
539 {"14a", CmdHF14A, 1, "{ ISO14443A RFIDs... }"},
540 {"14b", CmdHF14B, 1, "{ ISO14443B RFIDs... }"},
541 {"15", CmdHF15, 1, "{ ISO15693 RFIDs... }"},
542 {"epa", CmdHFEPA, 1, "{ German Identification Card... }"},
543 {"legic", CmdHFLegic, 0, "{ LEGIC RFIDs... }"},
544 {"iclass", CmdHFiClass, 1, "{ ICLASS RFIDs... }"},
545 {"mf", CmdHFMF, 1, "{ MIFARE RFIDs... }"},
546 {"mfu", CmdHFMFUltra, 1, "{ MIFARE Ultralight RFIDs... }"},
547 {"tune", CmdHFTune, 0, "Continuously measure HF antenna tuning"},
548 {"list", CmdHFList, 1, "List protocol data in trace buffer"},
549 {NULL, NULL, 0, NULL}
550 };
551
552 int CmdHF(const char *Cmd)
553 {
554 CmdsParse(CommandTable, Cmd);
555 return 0;
556 }
557
558 int CmdHelp(const char *Cmd)
559 {
560 CmdsHelp(CommandTable);
561 return 0;
562 }
Impressum, Datenschutz