]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhf15.c
5a7973f6a61d17b8552c1f5c8ffe1321c5e26bbd
[proxmark3-svn] / client / cmdhf15.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3 // Modified 2010-2012 by <adrian -at- atrox.at>
4 // Modified 2012 by <vsza at vsza.hu>
5 //
6 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
7 // at your option, any later version. See the LICENSE.txt file for the text of
8 // the license.
9 //-----------------------------------------------------------------------------
10 // High frequency ISO15693 commands
11 //-----------------------------------------------------------------------------
12 // There are three basic operation modes, depending on which device (proxmark/pc)
13 // the signal processing, (de)modulation, transmission protocol and logic is done.
14 // Mode 1:
15 // All steps are done on the proxmark, the output of the commands is returned via
16 // USB-debug-print commands.
17 // Mode 2:
18 // The protocol is done on the PC, passing only Iso15693 data frames via USB. This
19 // allows direct communication with a tag on command level
20 // Mode 3:
21 // The proxmark just samples the antenna and passes this "analog" data via USB to
22 // the client. Signal Processing & decoding is done on the pc. This is the slowest
23 // variant, but offers the possibility to analyze the waveforms directly.
24
25 #include "cmdhf15.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31
32 #include "comms.h"
33 #include "graph.h"
34 #include "ui.h"
35 #include "util.h"
36 #include "cmdparser.h"
37 #include "iso15693tools.h"
38 #include "protocols.h"
39 #include "cmdmain.h"
40 #include "taginfo.h"
41
42 #define Crc(data,datalen) Iso15693Crc(data,datalen)
43 #define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
44 #define sprintUID(target,uid) Iso15693sprintUID(target,uid)
45
46 // SOF defined as
47 // 1) Unmodulated time of 56.64us
48 // 2) 24 pulses of 423.75khz
49 // 3) logic '1' (unmodulated for 18.88us followed by 8 pulses of 423.75khz)
50
51 static const int Iso15693FrameSOF[] = {
52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
55 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
56 -1, -1, -1, -1,
57 -1, -1, -1, -1,
58 1, 1, 1, 1,
59 1, 1, 1, 1
60 };
61 static const int Iso15693Logic0[] = {
62 1, 1, 1, 1,
63 1, 1, 1, 1,
64 -1, -1, -1, -1,
65 -1, -1, -1, -1
66 };
67 static const int Iso15693Logic1[] = {
68 -1, -1, -1, -1,
69 -1, -1, -1, -1,
70 1, 1, 1, 1,
71 1, 1, 1, 1
72 };
73
74 // EOF defined as
75 // 1) logic '0' (8 pulses of 423.75khz followed by unmodulated for 18.88us)
76 // 2) 24 pulses of 423.75khz
77 // 3) Unmodulated time of 56.64us
78
79 static const int Iso15693FrameEOF[] = {
80 1, 1, 1, 1,
81 1, 1, 1, 1,
82 -1, -1, -1, -1,
83 -1, -1, -1, -1,
84 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
85 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
86 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
87 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
88 };
89
90
91 // fast method to just read the UID of a tag (collission detection not supported)
92 // *buf should be large enough to fit the 64bit uid
93 // returns 1 if suceeded
94 int getUID(uint8_t *buf)
95 {
96 UsbCommand resp;
97 uint8_t *recv;
98 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
99 uint8_t *req=c.d.asBytes;
100 int reqlen=0;
101
102 for (int retry=0;retry<3; retry++) { // don't give up the at the first try
103
104 req[0] = ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_INVENTORY | ISO15693_REQINV_SLOT1;
105 req[1] = ISO15693_INVENTORY;
106 req[2] = 0; // mask length
107 reqlen = AddCrc(req,3);
108 c.arg[0] = reqlen;
109
110 SendCommand(&c);
111
112 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
113 recv = resp.d.asBytes;
114 if (resp.arg[0]>=12 && ISO15693_CRC_CHECK==Crc(recv,12)) {
115 memcpy(buf,&recv[2],8);
116 return 1;
117 }
118 }
119 } // retry
120 return 0;
121 }
122
123
124 // return a clear-text message to an errorcode
125 static char* TagErrorStr(uint8_t error) {
126 switch (error) {
127 case 0x01: return "The command is not supported";
128 case 0x02: return "The command is not recognised";
129 case 0x03: return "The option is not supported.";
130 case 0x0f: return "Unknown error.";
131 case 0x10: return "The specified block is not available (doesn't exist).";
132 case 0x11: return "The specified block is already -locked and thus cannot be locked again";
133 case 0x12: return "The specified block is locked and its content cannot be changed.";
134 case 0x13: return "The specified block was not successfully programmed.";
135 case 0x14: return "The specified block was not successfully locked.";
136 default: return "Reserved for Future Use or Custom command error.";
137 }
138 }
139
140
141 // Mode 3
142 int CmdHF15Demod(const char *Cmd)
143 {
144 // The sampling rate is 106.353 ksps/s, for T = 18.8 us
145
146 int i, j;
147 int max = 0, maxPos = 0;
148
149 int skip = 2;
150
151 if (GraphTraceLen < 2000) return 0;
152
153 // First, correlate for SOF
154 for (i = 0; i < 200; i++) {
155 int corr = 0;
156 for (j = 0; j < arraylen(Iso15693FrameSOF); j += skip) {
157 corr += Iso15693FrameSOF[j] * GraphBuffer[i + (j / skip)];
158 }
159 if (corr > max) {
160 max = corr;
161 maxPos = i;
162 }
163 }
164 PrintAndLog("SOF at %d, correlation %d", maxPos,
165 max / (arraylen(Iso15693FrameSOF) / skip));
166
167 i = maxPos + arraylen(Iso15693FrameSOF) / skip;
168 int k = 0;
169 uint8_t outBuf[20];
170 memset(outBuf, 0, sizeof(outBuf));
171 uint8_t mask = 0x01;
172 for (;;) {
173 int corr0 = 0, corr00 = 0, corr01 = 0, corr1 = 0, corrEOF = 0;
174 for(j = 0; j < arraylen(Iso15693Logic0); j += skip) {
175 corr0 += Iso15693Logic0[j]*GraphBuffer[i+(j/skip)];
176 }
177 corr01 = corr00 = corr0;
178 for(j = 0; j < arraylen(Iso15693Logic0); j += skip) {
179 corr00 += Iso15693Logic0[j]*GraphBuffer[i+arraylen(Iso15693Logic0)/skip+(j/skip)];
180 corr01 += Iso15693Logic1[j]*GraphBuffer[i+arraylen(Iso15693Logic0)/skip+(j/skip)];
181 }
182 for(j = 0; j < arraylen(Iso15693Logic1); j += skip) {
183 corr1 += Iso15693Logic1[j]*GraphBuffer[i+(j/skip)];
184 }
185 for(j = 0; j < arraylen(Iso15693FrameEOF); j += skip) {
186 corrEOF += Iso15693FrameEOF[j]*GraphBuffer[i+(j/skip)];
187 }
188 // Even things out by the length of the target waveform.
189 corr00 *= 2;
190 corr01 *= 2;
191 corr0 *= 4;
192 corr1 *= 4;
193
194 if(corrEOF > corr1 && corrEOF > corr00 && corrEOF > corr01) {
195 PrintAndLog("EOF at %d", i);
196 break;
197 } else if (corr1 > corr0) {
198 i += arraylen(Iso15693Logic1) / skip;
199 outBuf[k] |= mask;
200 } else {
201 i += arraylen(Iso15693Logic0) / skip;
202 }
203 mask <<= 1;
204 if (mask == 0) {
205 k++;
206 mask = 0x01;
207 }
208 if ((i + (int)arraylen(Iso15693FrameEOF)) >= GraphTraceLen) {
209 PrintAndLog("ran off end!");
210 break;
211 }
212 }
213 if (mask != 0x01) {
214 PrintAndLog("error, uneven octet! (discard extra bits!)");
215 PrintAndLog(" mask=%02x", mask);
216 }
217 PrintAndLog("%d octets", k);
218
219 for (i = 0; i < k; i++) {
220 PrintAndLog("# %2d: %02x ", i, outBuf[i]);
221 }
222 PrintAndLog("CRC=%04x", Iso15693Crc(outBuf, k - 2));
223 return 0;
224 }
225
226
227
228 // * Acquire Samples as Reader (enables carrier, sends inquiry)
229 int CmdHF15Read(const char *Cmd)
230 {
231 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693};
232 SendCommand(&c);
233 return 0;
234 }
235
236 // Record Activity without enabling carrier
237 int CmdHF15Snoop(const char *Cmd)
238 {
239 UsbCommand c = {CMD_SNOOP_ISO_15693};
240 SendCommand(&c);
241 return 0;
242 }
243
244 int HF15Reader(const char *Cmd, bool verbose)
245 {
246 uint8_t uid[8];
247
248 if (!getUID(uid)) {
249 if (verbose) PrintAndLog("No Tag found.");
250 return 0;
251 }
252
253 PrintAndLog("UID: %s", sprintUID(NULL,uid));
254 PrintAndLog("Manufacturer byte: %02X, %s", uid[6], getManufacturerName(uid[6]));
255 PrintAndLog("Chip ID: %02X, %s", uid[5], getChipInfo(uid[6], uid[5]));
256 return 1;
257 }
258
259 int CmdHF15Reader(const char *Cmd)
260 {
261 UsbCommand c = {CMD_READER_ISO_15693, {strtol(Cmd, NULL, 0), 0, 0}};
262 SendCommand(&c);
263 return 0;
264 }
265
266 // Simulation is still not working very good
267 int CmdHF15Sim(const char *Cmd)
268 {
269 char cmdp = param_getchar(Cmd, 0);
270 uint8_t uid[8] = {0x00};
271
272 //E0 16 24 00 00 00 00 00
273 if (cmdp == 'h' || cmdp == 'H') {
274 PrintAndLog("Usage: hf 15 sim <UID>");
275 PrintAndLog("");
276 PrintAndLog(" sample: hf 15 sim E016240000000000");
277 return 0;
278 }
279
280 if (param_gethex(Cmd, 0, uid, 16)) {
281 PrintAndLog("UID must include 16 HEX symbols");
282 return 0;
283 }
284
285 PrintAndLog("Starting simulating UID %02X %02X %02X %02X %02X %02X %02X %02X",
286 uid[0],uid[1],uid[2],uid[3],uid[4], uid[5], uid[6], uid[7]);
287 PrintAndLog("Press the button to stop simulation");
288
289 UsbCommand c = {CMD_SIMTAG_ISO_15693, {0, 0, 0}};
290 memcpy(c.d.asBytes,uid,8);
291
292 SendCommand(&c);
293 return 0;
294 }
295
296 // finds the AFI (Application Family Idendifier) of a card, by trying all values
297 // (There is no standard way of reading the AFI, allthough some tags support this)
298 int CmdHF15Afi(const char *Cmd)
299 {
300 UsbCommand c = {CMD_ISO_15693_FIND_AFI, {strtol(Cmd, NULL, 0), 0, 0}};
301 SendCommand(&c);
302 return 0;
303 }
304
305 // Reads all memory pages
306 int CmdHF15DumpMem(const char*Cmd) {
307 UsbCommand resp;
308 uint8_t uid[8];
309 uint8_t *recv=NULL;
310 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
311 uint8_t *req=c.d.asBytes;
312 int reqlen=0;
313 int blocknum=0;
314 char output[80];
315
316 if (!getUID(uid)) {
317 PrintAndLog("No Tag found.");
318 return 0;
319 }
320
321 PrintAndLog("Reading memory from tag");
322 PrintAndLog("UID: %s", sprintUID(NULL,uid));
323 PrintAndLog("Manufacturer byte: %02X, %s", uid[6], getManufacturerName(uid[6]));
324 PrintAndLog("Chip ID: %02X, %s", uid[5], getChipInfo(uid[6], uid[5]));
325
326 for (int retry=0; retry<5; retry++) {
327
328 req[0]= ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_ADDRESS;
329 req[1] = ISO15693_READBLOCK;
330 memcpy(&req[2],uid,8);
331 req[10] = blocknum;
332 reqlen = AddCrc(req,11);
333 c.arg[0] = reqlen;
334
335 SendCommand(&c);
336
337 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
338 recv = resp.d.asBytes;
339 if (ISO15693_CRC_CHECK==Crc(recv,resp.arg[0])) {
340 if (!(recv[0] & ISO15693_RES_ERROR)) {
341 retry=0;
342 *output=0; // reset outputstring
343 sprintf(output, "Block %02x ",blocknum);
344 for ( int i=1; i<resp.arg[0]-2; i++) { // data in hex
345 sprintf(output+strlen(output),"%02X ",recv[i]);
346 }
347 strcat(output," ");
348 for ( int i=1; i<resp.arg[0]-2; i++) { // data in cleaned ascii
349 sprintf(output+strlen(output),"%c",(recv[i]>31 && recv[i]<127)?recv[i]:'.');
350 }
351 PrintAndLog("%s",output);
352 blocknum++;
353 // PrintAndLog("bn=%i",blocknum);
354 } else {
355 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
356 return 1;
357 }
358 } // else PrintAndLog("crc");
359 } // else PrintAndLog("r null");
360 } // retry
361 // TODO: need fix
362 // if (resp.arg[0]<3)
363 // PrintAndLog("Lost Connection");
364 // else if (ISO15693_CRC_CHECK!=Crc(resp.d.asBytes,resp.arg[0]))
365 // PrintAndLog("CRC Failed");
366 // else
367 // PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
368 return 1;
369 }
370
371
372 // "HF 15" interface
373
374 static command_t CommandTable15[] =
375 {
376 {"help", CmdHF15Help, 1, "This help"},
377 {"demod", CmdHF15Demod, 1, "Demodulate ISO15693 from tag"},
378 {"read", CmdHF15Read, 0, "Read HF tag (ISO 15693)"},
379 {"snoop", CmdHF15Snoop, 0, "Eavesdrop ISO 15693 communications"},
380 {"reader", CmdHF15Reader, 0, "Act like an ISO15693 reader"},
381 {"sim", CmdHF15Sim, 0, "Fake an ISO15693 tag"},
382 {"cmd", CmdHF15Cmd, 0, "Send direct commands to ISO15693 tag"},
383 {"findafi", CmdHF15Afi, 0, "Brute force AFI of an ISO15693 tag"},
384 {"dumpmemory", CmdHF15DumpMem, 0, "Read all memory pages of an ISO15693 tag"},
385 {NULL, NULL, 0, NULL}
386 };
387
388 int CmdHF15(const char *Cmd)
389 {
390 CmdsParse(CommandTable15, Cmd);
391 return 0;
392 }
393
394 int CmdHF15Help(const char *Cmd)
395 {
396 CmdsHelp(CommandTable15);
397 return 0;
398 }
399
400
401 // "HF 15 Cmd" Interface
402 // Allows direct communication with the tag on command level
403
404 int CmdHF15CmdInquiry(const char *Cmd)
405 {
406 UsbCommand resp;
407 uint8_t *recv;
408 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
409 uint8_t *req=c.d.asBytes;
410 int reqlen=0;
411
412 req[0] = ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_INVENTORY | ISO15693_REQINV_SLOT1;
413 req[1] = ISO15693_INVENTORY;
414 req[2] = 0; // mask length
415 reqlen=AddCrc(req,3);
416 c.arg[0] = reqlen;
417
418 SendCommand(&c);
419
420 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
421 if (resp.arg[0]>=12) {
422 recv = resp.d.asBytes;
423 PrintAndLog("UID: %s", sprintUID(NULL,recv+2));
424 PrintAndLog("Manufacturer byte: %02X, %s", recv[8], getManufacturerName(recv[8]));
425 PrintAndLog("Chip ID: %02X, %s", recv[7], getChipInfo(recv[8], recv[7]));
426 } else {
427 PrintAndLog("Response to short, just %i bytes. No tag?\n",resp.arg[0]);
428 }
429 } else {
430 PrintAndLog("timeout.");
431 }
432 return 0;
433 }
434
435
436 // Turns debugging on(1)/off(0)
437 int CmdHF15CmdDebug( const char *cmd) {
438 int debug = atoi(cmd);
439 if (strlen(cmd) < 1) {
440 PrintAndLog("Usage: hf 15 debug <0|1>");
441 PrintAndLog(" 0 no debugging");
442 PrintAndLog(" 1 turn debugging on");
443 return 0;
444 }
445
446 UsbCommand c = {CMD_ISO_15693_DEBUG, {debug, 0, 0}};
447 SendCommand(&c);
448 return 0;
449 }
450
451
452 int CmdHF15CmdRaw (const char *cmd) {
453 UsbCommand resp;
454 uint8_t *recv;
455 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
456 int reply=1;
457 int fast=1;
458 int crc=0;
459 char buf[5]="";
460 int i=0;
461 uint8_t data[100];
462 unsigned int datalen=0, temp;
463 char *hexout;
464
465
466 if (strlen(cmd)<3) {
467 PrintAndLog("Usage: hf 15 cmd raw [-r] [-2] [-c] <0A 0B 0C ... hex>");
468 PrintAndLog(" -r do not read response");
469 PrintAndLog(" -2 use slower '1 out of 256' mode");
470 PrintAndLog(" -c calculate and append CRC");
471 PrintAndLog(" Tip: turn on debugging for verbose output");
472 return 0;
473 }
474
475 // strip
476 while (*cmd==' ' || *cmd=='\t') cmd++;
477
478 while (cmd[i]!='\0') {
479 if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; }
480 if (cmd[i]=='-') {
481 switch (cmd[i+1]) {
482 case 'r':
483 case 'R':
484 reply=0;
485 break;
486 case '2':
487 fast=0;
488 break;
489 case 'c':
490 case 'C':
491 crc=1;
492 break;
493 default:
494 PrintAndLog("Invalid option");
495 return 0;
496 }
497 i+=2;
498 continue;
499 }
500 if ((cmd[i]>='0' && cmd[i]<='9') ||
501 (cmd[i]>='a' && cmd[i]<='f') ||
502 (cmd[i]>='A' && cmd[i]<='F') ) {
503 buf[strlen(buf)+1]=0;
504 buf[strlen(buf)]=cmd[i];
505 i++;
506
507 if (strlen(buf)>=2) {
508 sscanf(buf,"%x",&temp);
509 data[datalen]=(uint8_t)(temp & 0xff);
510 datalen++;
511 *buf=0;
512 }
513 continue;
514 }
515 PrintAndLog("Invalid char on input");
516 return 0;
517 }
518 if (crc) datalen=AddCrc(data,datalen);
519
520 c.arg[0]=datalen;
521 c.arg[1]=fast;
522 c.arg[2]=reply;
523 memcpy(c.d.asBytes,data,datalen);
524
525 SendCommand(&c);
526
527 if (reply) {
528 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
529 recv = resp.d.asBytes;
530 PrintAndLog("received %i octets",resp.arg[0]);
531 hexout = (char *)malloc(resp.arg[0] * 3 + 1);
532 if (hexout != NULL) {
533 for (int i = 0; i < resp.arg[0]; i++) { // data in hex
534 sprintf(&hexout[i * 3], "%02X ", recv[i]);
535 }
536 PrintAndLog("%s", hexout);
537 free(hexout);
538 }
539 } else {
540 PrintAndLog("timeout while waiting for reply.");
541 }
542
543 } // if reply
544 return 0;
545 }
546
547
548 /**
549 * parses common HF 15 CMD parameters and prepares some data structures
550 * Parameters:
551 * **cmd command line
552 */
553 int prepareHF15Cmd(char **cmd, UsbCommand *c, uint8_t iso15cmd[], int iso15cmdlen) {
554 int temp;
555 uint8_t *req=c->d.asBytes;
556 uint8_t uid[8] = {0x00};
557 uint32_t reqlen=0;
558
559 // strip
560 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
561
562 if (strstr(*cmd,"-2")==*cmd) {
563 c->arg[1]=0; // use 1of256
564 (*cmd)+=2;
565 }
566
567 // strip
568 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
569
570 if (strstr(*cmd,"-o")==*cmd) {
571 req[reqlen]=ISO15693_REQ_OPTION;
572 (*cmd)+=2;
573 }
574
575 // strip
576 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
577
578 switch (**cmd) {
579 case 0:
580 PrintAndLog("missing addr");
581 return 0;
582 break;
583 case 's':
584 case 'S':
585 // you must have selected the tag earlier
586 req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_SELECT;
587 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
588 reqlen += iso15cmdlen;
589 break;
590 case 'u':
591 case 'U':
592 // unaddressed mode may not be supported by all vendors
593 req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH;
594 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
595 reqlen += iso15cmdlen;
596 break;
597 case '*':
598 // we scan for the UID ourself
599 req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_ADDRESS;
600 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
601 reqlen+=iso15cmdlen;
602 if (!getUID(uid)) {
603 PrintAndLog("No Tag found");
604 return 0;
605 }
606 memcpy(req+reqlen,uid,8);
607 PrintAndLog("Detected UID %s",sprintUID(NULL,uid));
608 reqlen+=8;
609 break;
610 default:
611 req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_ADDRESS;
612 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
613 reqlen+=iso15cmdlen;
614
615 /* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX",
616 (short unsigned int *)&uid[7],(short unsigned int *)&uid[6],
617 (short unsigned int *)&uid[5],(short unsigned int *)&uid[4],
618 (short unsigned int *)&uid[3],(short unsigned int *)&uid[2],
619 (short unsigned int *)&uid[1],(short unsigned int *)&uid[0]); */
620 for (int i=0;i<8 && (*cmd)[i*2] && (*cmd)[i*2+1];i++) { // parse UID
621 sscanf((char[]){(*cmd)[i*2],(*cmd)[i*2+1],0},"%X",&temp);
622 uid[7-i]=temp&0xff;
623 }
624
625 PrintAndLog("Using UID %s",sprintUID(NULL,uid));
626 memcpy(&req[reqlen],&uid[0],8);
627 reqlen+=8;
628 }
629 // skip to next space
630 while (**cmd!=' ' && **cmd!='\t') (*cmd)++;
631 // skip over the space
632 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
633
634 c->arg[0]=reqlen;
635 return 1;
636 }
637
638 /**
639 * Commandline handling: HF15 CMD SYSINFO
640 * get system information from tag/VICC
641 */
642 int CmdHF15CmdSysinfo(const char *Cmd) {
643 UsbCommand resp;
644 uint8_t *recv;
645 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
646 uint8_t *req=c.d.asBytes;
647 int reqlen=0;
648 char cmdbuf[100];
649 char *cmd=cmdbuf;
650 char output[2048]="";
651 int i;
652
653 strncpy(cmd,Cmd,99);
654
655 // usage:
656 if (strlen(cmd)<1) {
657 PrintAndLog("Usage: hf 15 cmd sysinfo [options] <uid|s|u|*>");
658 PrintAndLog(" options:");
659 PrintAndLog(" -2 use slower '1 out of 256' mode");
660 PrintAndLog(" uid (either): ");
661 PrintAndLog(" <8B hex> full UID eg E011223344556677");
662 PrintAndLog(" s selected tag");
663 PrintAndLog(" u unaddressed mode");
664 PrintAndLog(" * scan for tag");
665 PrintAndLog(" start#: page number to start 0-255");
666 PrintAndLog(" count#: number of pages");
667 return 0;
668 }
669
670 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15693_GET_SYSTEM_INFO},1);
671 reqlen=c.arg[0];
672
673 reqlen=AddCrc(req,reqlen);
674 c.arg[0]=reqlen;
675
676 SendCommand(&c);
677
678 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
679 recv = resp.d.asBytes;
680 if (ISO15693_CRC_CHECK==Crc(recv,resp.arg[0])) {
681 if (!(recv[0] & ISO15693_RES_ERROR)) {
682 *output=0; // reset outputstring
683 PrintAndLog("UID: %s", sprintUID(NULL,recv+2));
684 PrintAndLog("Manufacturer byte: %02X, %s", recv[8], getManufacturerName(recv[8]));
685 PrintAndLog("Chip ID: %02X, %s", recv[7], getChipInfo(recv[8], recv[7]));
686 i=10;
687 if (recv[1] & 0x01)
688 sprintf(output+strlen(output),"DSFID supported, set to %02X\n\r",recv[i++]);
689 else
690 strcat(output,"DSFID not supported\n\r");
691 if (recv[1] & 0x02)
692 sprintf(output+strlen(output),"AFI supported, set to %03X\n\r",recv[i++]);
693 else
694 strcat(output,"AFI not supported\n\r");
695 if (recv[1] & 0x04) {
696 strcat(output,"Tag provides info on memory layout (vendor dependent)\n\r");
697 sprintf(output+strlen(output)," %i (or %i) bytes/page x %i pages \n\r",
698 (recv[i+1]&0x1F)+1, (recv[i+1]&0x1F), recv[i]+1);
699 i+=2;
700 } else
701 strcat(output,"Tag does not provide information on memory layout\n\r");
702 if (recv[1] & 0x08) sprintf(output+strlen(output),"IC reference given: %02X\n\r",recv[i++]);
703 else strcat(output,"IC reference not given\n\r");
704
705
706 PrintAndLog("%s",output);
707 } else {
708 PrintAndLog("Tag returned Error %i: %s",recv[0],TagErrorStr(recv[0]));
709 }
710 } else {
711 PrintAndLog("CRC failed");
712 }
713 } else {
714 PrintAndLog("timeout: no answer");
715 }
716
717 return 0;
718 }
719
720 /**
721 * Commandline handling: HF15 CMD READMULTI
722 * Read multiple blocks at once (not all tags support this)
723 */
724 int CmdHF15CmdReadmulti(const char *Cmd) {
725 UsbCommand resp;
726 uint8_t *recv;
727 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
728 uint8_t *req=c.d.asBytes;
729 int reqlen=0, pagenum,pagecount;
730 char cmdbuf[100];
731 char *cmd=cmdbuf;
732 char output[2048]="";
733
734 strncpy(cmd,Cmd,99);
735
736 // usage:
737 if (strlen(cmd)<3) {
738 PrintAndLog("Usage: hf 15 cmd readmulti [options] <uid|s|u|*> <start#> <count#>");
739 PrintAndLog(" options:");
740 PrintAndLog(" -2 use slower '1 out of 256' mode");
741 PrintAndLog(" uid (either): ");
742 PrintAndLog(" <8B hex> full UID eg E011223344556677");
743 PrintAndLog(" s selected tag");
744 PrintAndLog(" u unaddressed mode");
745 PrintAndLog(" * scan for tag");
746 PrintAndLog(" start#: page number to start 0-255");
747 PrintAndLog(" count#: number of pages");
748 return 0;
749 }
750
751 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15693_READ_MULTI_BLOCK},1);
752 reqlen=c.arg[0];
753
754 pagenum=strtol(cmd,NULL,0);
755
756 // skip to next space
757 while (*cmd!=' ' && *cmd!='\t') cmd++;
758 // skip over the space
759 while (*cmd==' ' || *cmd=='\t') cmd++;
760
761 pagecount=strtol(cmd,NULL,0);
762 if (pagecount>0) pagecount--; // 0 means 1 page, 1 means 2 pages, ...
763
764 req[reqlen++]=(uint8_t)pagenum;
765 req[reqlen++]=(uint8_t)pagecount;
766
767 reqlen=AddCrc(req,reqlen);
768
769 c.arg[0]=reqlen;
770
771 SendCommand(&c);
772
773 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
774 recv = resp.d.asBytes;
775 if (ISO15693_CRC_CHECK==Crc(recv,resp.arg[0])) {
776 if (!(recv[0] & ISO15693_RES_ERROR)) {
777 *output=0; // reset outputstring
778 for ( int i=1; i<resp.arg[0]-2; i++) {
779 sprintf(output+strlen(output),"%02X ",recv[i]);
780 }
781 strcat(output," ");
782 for ( int i=1; i<resp.arg[0]-2; i++) {
783 sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.');
784 }
785 PrintAndLog("%s",output);
786 } else {
787 PrintAndLog("Tag returned Error %i: %s",recv[0],TagErrorStr(recv[0]));
788 }
789 } else {
790 PrintAndLog("CRC failed");
791 }
792 } else {
793 PrintAndLog("no answer");
794 }
795
796 return 0;
797 }
798
799 /**
800 * Commandline handling: HF15 CMD READ
801 * Reads a single Block
802 */
803 int CmdHF15CmdRead(const char *Cmd) {
804 UsbCommand resp;
805 uint8_t *recv;
806 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
807 uint8_t *req=c.d.asBytes;
808 int reqlen=0, pagenum;
809 char cmdbuf[100];
810 char *cmd=cmdbuf;
811 char output[100]="";
812
813 strncpy(cmd,Cmd,99);
814
815 // usage:
816 if (strlen(cmd)<3) {
817 PrintAndLog("Usage: hf 15 cmd read [options] <uid|s|u|*> <page#>");
818 PrintAndLog(" options:");
819 PrintAndLog(" -2 use slower '1 out of 256' mode");
820 PrintAndLog(" uid (either): ");
821 PrintAndLog(" <8B hex> full UID eg E011223344556677");
822 PrintAndLog(" s selected tag");
823 PrintAndLog(" u unaddressed mode");
824 PrintAndLog(" * scan for tag");
825 PrintAndLog(" page#: page number 0-255");
826 return 0;
827 }
828
829 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15693_READBLOCK},1);
830 reqlen=c.arg[0];
831
832 pagenum=strtol(cmd,NULL,0);
833 /*if (pagenum<0) {
834 PrintAndLog("invalid pagenum");
835 return 0;
836 } */
837
838 req[reqlen++]=(uint8_t)pagenum;
839
840 reqlen=AddCrc(req,reqlen);
841
842 c.arg[0]=reqlen;
843
844 SendCommand(&c);
845
846 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
847 recv = resp.d.asBytes;
848 if (ISO15693_CRC_CHECK==Crc(recv,resp.arg[0])) {
849 if (!(recv[0] & ISO15693_RES_ERROR)) {
850 *output=0; // reset outputstring
851 //sprintf(output, "Block %2i ",blocknum);
852 for ( int i=1; i<resp.arg[0]-2; i++) {
853 sprintf(output+strlen(output),"%02X ",recv[i]);
854 }
855 strcat(output," ");
856 for ( int i=1; i<resp.arg[0]-2; i++) {
857 sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.');
858 }
859 PrintAndLog("%s",output);
860 } else {
861 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
862 }
863 } else {
864 PrintAndLog("CRC failed");
865 }
866 } else {
867 PrintAndLog("no answer");
868 }
869
870 return 0;
871 }
872
873
874 /**
875 * Commandline handling: HF15 CMD WRITE
876 * Writes a single Block - might run into timeout, even when successful
877 */
878 int CmdHF15CmdWrite(const char *Cmd) {
879 UsbCommand resp;
880 uint8_t *recv;
881 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
882 uint8_t *req=c.d.asBytes;
883 int reqlen=0, pagenum, temp;
884 char cmdbuf[100];
885 char *cmd=cmdbuf;
886 char *cmd2;
887
888 strncpy(cmd,Cmd,99);
889
890 // usage:
891 if (strlen(cmd)<3) {
892 PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|u|*> <page#> <hexdata>");
893 PrintAndLog(" options:");
894 PrintAndLog(" -2 use slower '1 out of 256' mode");
895 PrintAndLog(" -o set OPTION Flag (needed for TI)");
896 PrintAndLog(" uid (either): ");
897 PrintAndLog(" <8B hex> full UID eg E011223344556677");
898 PrintAndLog(" s selected tag");
899 PrintAndLog(" u unaddressed mode");
900 PrintAndLog(" * scan for tag");
901 PrintAndLog(" page#: page number 0-255");
902 PrintAndLog(" hexdata: data to be written eg AA BB CC DD");
903 return 0;
904 }
905
906 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15693_WRITEBLOCK},1);
907 reqlen=c.arg[0];
908
909 // *cmd -> page num ; *cmd2 -> data
910 cmd2=cmd;
911 while (*cmd2!=' ' && *cmd2!='\t' && *cmd2) cmd2++;
912 *cmd2=0;
913 cmd2++;
914
915 pagenum=strtol(cmd,NULL,0);
916 /*if (pagenum<0) {
917 PrintAndLog("invalid pagenum");
918 return 0;
919 } */
920 req[reqlen++]=(uint8_t)pagenum;
921
922
923 while (cmd2[0] && cmd2[1]) { // hexdata, read by 2 hexchars
924 if (*cmd2==' ') {
925 cmd2++;
926 continue;
927 }
928 sscanf((char[]){cmd2[0],cmd2[1],0},"%X",&temp);
929 req[reqlen++]=temp & 0xff;
930 cmd2+=2;
931 }
932
933 reqlen=AddCrc(req,reqlen);
934
935 c.arg[0]=reqlen;
936
937 SendCommand(&c);
938
939 if (WaitForResponseTimeout(CMD_ACK,&resp,2000) && resp.arg[0]>2) {
940 recv = resp.d.asBytes;
941 if (ISO15693_CRC_CHECK==Crc(recv,resp.arg[0])) {
942 if (!(recv[0] & ISO15693_RES_ERROR)) {
943 PrintAndLog("OK");
944 } else {
945 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
946 }
947 } else {
948 PrintAndLog("CRC failed");
949 }
950 } else {
951 PrintAndLog("timeout: no answer - data may be written anyway");
952 }
953
954 return 0;
955 }
956
957
958
959 static command_t CommandTable15Cmd[] =
960 {
961 {"help", CmdHF15CmdHelp, 1, "This Help"},
962 {"inquiry", CmdHF15CmdInquiry, 0, "Search for tags in range"},
963 /*
964 {"select", CmdHF15CmdSelect, 0, "Select an tag with a specific UID for further commands"},
965 */
966 {"read", CmdHF15CmdRead, 0, "Read a block"},
967 {"write", CmdHF15CmdWrite, 0, "Write a block"},
968 {"readmulti",CmdHF15CmdReadmulti, 0, "Reads multiple Blocks"},
969 {"sysinfo",CmdHF15CmdSysinfo, 0, "Get Card Information"},
970 {"raw", CmdHF15CmdRaw, 0, "Send raw hex data to tag"},
971 {"debug", CmdHF15CmdDebug, 0, "Turn debugging on/off"},
972 {NULL, NULL, 0, NULL}
973 };
974
975 int CmdHF15Cmd(const char *Cmd)
976 {
977 CmdsParse(CommandTable15Cmd, Cmd);
978 return 0;
979 }
980
981 int CmdHF15CmdHelp(const char *Cmd)
982 {
983 CmdsHelp(CommandTable15Cmd);
984 return 0;
985 }
986
Impressum, Datenschutz