]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhf15.c
6bcad20124ac54f452d392e2435173c467a99afd
[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 {"csetuid", CmdHF15CSetUID, 0, "Set UID for magic Chinese card"},
386 {NULL, NULL, 0, NULL}
387 };
388
389 int CmdHF15(const char *Cmd)
390 {
391 CmdsParse(CommandTable15, Cmd);
392 return 0;
393 }
394
395 int CmdHF15Help(const char *Cmd)
396 {
397 CmdsHelp(CommandTable15);
398 return 0;
399 }
400
401
402 // "HF 15 Cmd" Interface
403 // Allows direct communication with the tag on command level
404
405 int CmdHF15CmdInquiry(const char *Cmd)
406 {
407 UsbCommand resp;
408 uint8_t *recv;
409 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
410 uint8_t *req=c.d.asBytes;
411 int reqlen=0;
412
413 req[0] = ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_INVENTORY | ISO15693_REQINV_SLOT1;
414 req[1] = ISO15693_INVENTORY;
415 req[2] = 0; // mask length
416 reqlen=AddCrc(req,3);
417 c.arg[0] = reqlen;
418
419 SendCommand(&c);
420
421 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
422 if (resp.arg[0]>=12) {
423 recv = resp.d.asBytes;
424 PrintAndLog("UID: %s", sprintUID(NULL,recv+2));
425 PrintAndLog("Manufacturer byte: %02X, %s", recv[8], getManufacturerName(recv[8]));
426 PrintAndLog("Chip ID: %02X, %s", recv[7], getChipInfo(recv[8], recv[7]));
427 } else {
428 PrintAndLog("Response to short, just %i bytes. No tag?\n",resp.arg[0]);
429 }
430 } else {
431 PrintAndLog("timeout.");
432 }
433 return 0;
434 }
435
436
437 // Turns debugging on(1)/off(0)
438 int CmdHF15CmdDebug( const char *cmd) {
439 int debug = atoi(cmd);
440 if (strlen(cmd) < 1) {
441 PrintAndLog("Usage: hf 15 debug <0|1>");
442 PrintAndLog(" 0 no debugging");
443 PrintAndLog(" 1 turn debugging on");
444 return 0;
445 }
446
447 UsbCommand c = {CMD_ISO_15693_DEBUG, {debug, 0, 0}};
448 SendCommand(&c);
449 return 0;
450 }
451
452
453 int CmdHF15CmdRaw (const char *cmd) {
454 UsbCommand resp;
455 uint8_t *recv;
456 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
457 int reply=1;
458 int fast=1;
459 int crc=0;
460 char buf[5]="";
461 int i=0;
462 uint8_t data[100];
463 unsigned int datalen=0, temp;
464 char *hexout;
465
466
467 if (strlen(cmd)<2) {
468 PrintAndLog("Usage: hf 15 cmd raw [-r] [-2] [-c] <0A 0B 0C ... hex>");
469 PrintAndLog(" -r do not read response");
470 PrintAndLog(" -2 use slower '1 out of 256' mode");
471 PrintAndLog(" -c calculate and append CRC");
472 PrintAndLog(" Tip: turn on debugging for verbose output");
473 return 0;
474 }
475
476 // strip
477 while (*cmd==' ' || *cmd=='\t') cmd++;
478
479 while (cmd[i]!='\0') {
480 if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; }
481 if (cmd[i]=='-') {
482 switch (cmd[i+1]) {
483 case 'r':
484 case 'R':
485 reply=0;
486 break;
487 case '2':
488 fast=0;
489 break;
490 case 'c':
491 case 'C':
492 crc=1;
493 break;
494 default:
495 PrintAndLog("Invalid option");
496 return 0;
497 }
498 i+=2;
499 continue;
500 }
501 if ((cmd[i]>='0' && cmd[i]<='9') ||
502 (cmd[i]>='a' && cmd[i]<='f') ||
503 (cmd[i]>='A' && cmd[i]<='F') ) {
504 buf[strlen(buf)+1]=0;
505 buf[strlen(buf)]=cmd[i];
506 i++;
507
508 if (strlen(buf)>=2) {
509 sscanf(buf,"%x",&temp);
510 data[datalen]=(uint8_t)(temp & 0xff);
511 datalen++;
512 *buf=0;
513 }
514 continue;
515 }
516 PrintAndLog("Invalid char on input");
517 return 0;
518 }
519 if (crc) datalen=AddCrc(data,datalen);
520
521 c.arg[0]=datalen;
522 c.arg[1]=fast;
523 c.arg[2]=reply;
524 memcpy(c.d.asBytes,data,datalen);
525
526 SendCommand(&c);
527
528 if (reply) {
529 if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {
530 recv = resp.d.asBytes;
531 int recv_len = resp.arg[0];
532 if (recv_len == 0) {
533 PrintAndLog("received SOF only. Maybe Picopass/iCLASS?");
534 } else if (recv_len > 0) {
535 PrintAndLog("received %i octets", recv_len);
536 hexout = (char *)malloc(resp.arg[0] * 3 + 1);
537 if (hexout != NULL) {
538 for (int i = 0; i < resp.arg[0]; i++) { // data in hex
539 sprintf(&hexout[i * 3], "%02X ", recv[i]);
540 }
541 PrintAndLog("%s", hexout);
542 free(hexout);
543 }
544 } else if (recv_len == -1) {
545 PrintAndLog("card didn't respond");
546 } else if (recv_len == -2) {
547 PrintAndLog("receive buffer overflow");
548 }
549 } else {
550 PrintAndLog("timeout while waiting for reply.");
551 }
552 }
553
554 return 0;
555 }
556
557
558 /**
559 * parses common HF 15 CMD parameters and prepares some data structures
560 * Parameters:
561 * **cmd command line
562 */
563 int prepareHF15Cmd(char **cmd, UsbCommand *c, uint8_t iso15cmd[], int iso15cmdlen) {
564 int temp;
565 uint8_t *req=c->d.asBytes;
566 uint8_t uid[8] = {0x00};
567 uint32_t reqlen=0;
568
569 // strip
570 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
571
572 if (strstr(*cmd,"-2")==*cmd) {
573 c->arg[1]=0; // use 1of256
574 (*cmd)+=2;
575 }
576
577 // strip
578 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
579
580 if (strstr(*cmd,"-o")==*cmd) {
581 req[reqlen]=ISO15693_REQ_OPTION;
582 (*cmd)+=2;
583 }
584
585 // strip
586 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
587
588 switch (**cmd) {
589 case 0:
590 PrintAndLog("missing addr");
591 return 0;
592 break;
593 case 's':
594 case 'S':
595 // you must have selected the tag earlier
596 req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_SELECT;
597 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
598 reqlen += iso15cmdlen;
599 break;
600 case 'u':
601 case 'U':
602 // unaddressed mode may not be supported by all vendors
603 req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH;
604 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
605 reqlen += iso15cmdlen;
606 break;
607 case '*':
608 // we scan for the UID ourself
609 req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_ADDRESS;
610 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
611 reqlen+=iso15cmdlen;
612 if (!getUID(uid)) {
613 PrintAndLog("No Tag found");
614 return 0;
615 }
616 memcpy(req+reqlen,uid,8);
617 PrintAndLog("Detected UID %s",sprintUID(NULL,uid));
618 reqlen+=8;
619 break;
620 default:
621 req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_ADDRESS;
622 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
623 reqlen+=iso15cmdlen;
624
625 /* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX",
626 (short unsigned int *)&uid[7],(short unsigned int *)&uid[6],
627 (short unsigned int *)&uid[5],(short unsigned int *)&uid[4],
628 (short unsigned int *)&uid[3],(short unsigned int *)&uid[2],
629 (short unsigned int *)&uid[1],(short unsigned int *)&uid[0]); */
630 for (int i=0;i<8 && (*cmd)[i*2] && (*cmd)[i*2+1];i++) { // parse UID
631 sscanf((char[]){(*cmd)[i*2],(*cmd)[i*2+1],0},"%X",&temp);
632 uid[7-i]=temp&0xff;
633 }
634
635 PrintAndLog("Using UID %s",sprintUID(NULL,uid));
636 memcpy(&req[reqlen],&uid[0],8);
637 reqlen+=8;
638 }
639 // skip to next space
640 while (**cmd!=' ' && **cmd!='\t') (*cmd)++;
641 // skip over the space
642 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
643
644 c->arg[0]=reqlen;
645 return 1;
646 }
647
648 /**
649 * Commandline handling: HF15 CMD SYSINFO
650 * get system information from tag/VICC
651 */
652 int CmdHF15CmdSysinfo(const char *Cmd) {
653 UsbCommand resp;
654 uint8_t *recv;
655 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
656 uint8_t *req=c.d.asBytes;
657 int reqlen=0;
658 char cmdbuf[100];
659 char *cmd=cmdbuf;
660 char output[2048]="";
661 int i;
662
663 strncpy(cmd,Cmd,99);
664
665 // usage:
666 if (strlen(cmd)<1) {
667 PrintAndLog("Usage: hf 15 cmd sysinfo [options] <uid|s|u|*>");
668 PrintAndLog(" options:");
669 PrintAndLog(" -2 use slower '1 out of 256' mode");
670 PrintAndLog(" uid (either): ");
671 PrintAndLog(" <8B hex> full UID eg E011223344556677");
672 PrintAndLog(" s selected tag");
673 PrintAndLog(" u unaddressed mode");
674 PrintAndLog(" * scan for tag");
675 PrintAndLog(" start#: page number to start 0-255");
676 PrintAndLog(" count#: number of pages");
677 return 0;
678 }
679
680 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15693_GET_SYSTEM_INFO},1);
681 reqlen=c.arg[0];
682
683 reqlen=AddCrc(req,reqlen);
684 c.arg[0]=reqlen;
685
686 SendCommand(&c);
687
688 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
689 recv = resp.d.asBytes;
690 if (ISO15693_CRC_CHECK==Crc(recv,resp.arg[0])) {
691 if (!(recv[0] & ISO15693_RES_ERROR)) {
692 *output=0; // reset outputstring
693 PrintAndLog("UID: %s", sprintUID(NULL,recv+2));
694 PrintAndLog("Manufacturer byte: %02X, %s", recv[8], getManufacturerName(recv[8]));
695 PrintAndLog("Chip ID: %02X, %s", recv[7], getChipInfo(recv[8], recv[7]));
696 i=10;
697 if (recv[1] & 0x01)
698 sprintf(output+strlen(output),"DSFID supported, set to %02X\n\r",recv[i++]);
699 else
700 strcat(output,"DSFID not supported\n\r");
701 if (recv[1] & 0x02)
702 sprintf(output+strlen(output),"AFI supported, set to %03X\n\r",recv[i++]);
703 else
704 strcat(output,"AFI not supported\n\r");
705 if (recv[1] & 0x04) {
706 strcat(output,"Tag provides info on memory layout (vendor dependent)\n\r");
707 sprintf(output+strlen(output)," %i (or %i) bytes/page x %i pages \n\r",
708 (recv[i+1]&0x1F)+1, (recv[i+1]&0x1F), recv[i]+1);
709 i+=2;
710 } else
711 strcat(output,"Tag does not provide information on memory layout\n\r");
712 if (recv[1] & 0x08) sprintf(output+strlen(output),"IC reference given: %02X\n\r",recv[i++]);
713 else strcat(output,"IC reference not given\n\r");
714
715
716 PrintAndLog("%s",output);
717 } else {
718 PrintAndLog("Tag returned Error %i: %s",recv[0],TagErrorStr(recv[0]));
719 }
720 } else {
721 PrintAndLog("CRC failed");
722 }
723 } else {
724 PrintAndLog("timeout: no answer");
725 }
726
727 return 0;
728 }
729
730 /**
731 * Commandline handling: HF15 CMD READMULTI
732 * Read multiple blocks at once (not all tags support this)
733 */
734 int CmdHF15CmdReadmulti(const char *Cmd) {
735 UsbCommand resp;
736 uint8_t *recv;
737 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
738 uint8_t *req=c.d.asBytes;
739 int reqlen=0, pagenum,pagecount;
740 char cmdbuf[100];
741 char *cmd=cmdbuf;
742 char output[2048]="";
743
744 strncpy(cmd,Cmd,99);
745
746 // usage:
747 if (strlen(cmd)<3) {
748 PrintAndLog("Usage: hf 15 cmd readmulti [options] <uid|s|u|*> <start#> <count#>");
749 PrintAndLog(" options:");
750 PrintAndLog(" -2 use slower '1 out of 256' mode");
751 PrintAndLog(" uid (either): ");
752 PrintAndLog(" <8B hex> full UID eg E011223344556677");
753 PrintAndLog(" s selected tag");
754 PrintAndLog(" u unaddressed mode");
755 PrintAndLog(" * scan for tag");
756 PrintAndLog(" start#: page number to start 0-255");
757 PrintAndLog(" count#: number of pages");
758 return 0;
759 }
760
761 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15693_READ_MULTI_BLOCK},1);
762 reqlen=c.arg[0];
763
764 pagenum=strtol(cmd,NULL,0);
765
766 // skip to next space
767 while (*cmd!=' ' && *cmd!='\t') cmd++;
768 // skip over the space
769 while (*cmd==' ' || *cmd=='\t') cmd++;
770
771 pagecount=strtol(cmd,NULL,0);
772 if (pagecount>0) pagecount--; // 0 means 1 page, 1 means 2 pages, ...
773
774 req[reqlen++]=(uint8_t)pagenum;
775 req[reqlen++]=(uint8_t)pagecount;
776
777 reqlen=AddCrc(req,reqlen);
778
779 c.arg[0]=reqlen;
780
781 SendCommand(&c);
782
783 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
784 recv = resp.d.asBytes;
785 if (ISO15693_CRC_CHECK==Crc(recv,resp.arg[0])) {
786 if (!(recv[0] & ISO15693_RES_ERROR)) {
787 *output=0; // reset outputstring
788 for ( int i=1; i<resp.arg[0]-2; i++) {
789 sprintf(output+strlen(output),"%02X ",recv[i]);
790 }
791 strcat(output," ");
792 for ( int i=1; i<resp.arg[0]-2; i++) {
793 sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.');
794 }
795 PrintAndLog("%s",output);
796 } else {
797 PrintAndLog("Tag returned Error %i: %s",recv[0],TagErrorStr(recv[0]));
798 }
799 } else {
800 PrintAndLog("CRC failed");
801 }
802 } else {
803 PrintAndLog("no answer");
804 }
805
806 return 0;
807 }
808
809 /**
810 * Commandline handling: HF15 CMD READ
811 * Reads a single Block
812 */
813 int CmdHF15CmdRead(const char *Cmd) {
814 UsbCommand resp;
815 uint8_t *recv;
816 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
817 uint8_t *req=c.d.asBytes;
818 int reqlen=0, pagenum;
819 char cmdbuf[100];
820 char *cmd=cmdbuf;
821 char output[100]="";
822
823 strncpy(cmd,Cmd,99);
824
825 // usage:
826 if (strlen(cmd)<3) {
827 PrintAndLog("Usage: hf 15 cmd read [options] <uid|s|u|*> <page#>");
828 PrintAndLog(" options:");
829 PrintAndLog(" -2 use slower '1 out of 256' mode");
830 PrintAndLog(" uid (either): ");
831 PrintAndLog(" <8B hex> full UID eg E011223344556677");
832 PrintAndLog(" s selected tag");
833 PrintAndLog(" u unaddressed mode");
834 PrintAndLog(" * scan for tag");
835 PrintAndLog(" page#: page number 0-255");
836 return 0;
837 }
838
839 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15693_READBLOCK},1);
840 reqlen=c.arg[0];
841
842 pagenum=strtol(cmd,NULL,0);
843 /*if (pagenum<0) {
844 PrintAndLog("invalid pagenum");
845 return 0;
846 } */
847
848 req[reqlen++]=(uint8_t)pagenum;
849
850 reqlen=AddCrc(req,reqlen);
851
852 c.arg[0]=reqlen;
853
854 SendCommand(&c);
855
856 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
857 recv = resp.d.asBytes;
858 if (ISO15693_CRC_CHECK==Crc(recv,resp.arg[0])) {
859 if (!(recv[0] & ISO15693_RES_ERROR)) {
860 *output=0; // reset outputstring
861 //sprintf(output, "Block %2i ",blocknum);
862 for ( int i=1; i<resp.arg[0]-2; i++) {
863 sprintf(output+strlen(output),"%02X ",recv[i]);
864 }
865 strcat(output," ");
866 for ( int i=1; i<resp.arg[0]-2; i++) {
867 sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.');
868 }
869 PrintAndLog("%s",output);
870 } else {
871 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
872 }
873 } else {
874 PrintAndLog("CRC failed");
875 }
876 } else {
877 PrintAndLog("no answer");
878 }
879
880 return 0;
881 }
882
883
884 /**
885 * Commandline handling: HF15 CMD WRITE
886 * Writes a single Block - might run into timeout, even when successful
887 */
888 int CmdHF15CmdWrite(const char *Cmd) {
889 UsbCommand resp;
890 uint8_t *recv;
891 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
892 uint8_t *req=c.d.asBytes;
893 int reqlen=0, pagenum, temp;
894 char cmdbuf[100];
895 char *cmd=cmdbuf;
896 char *cmd2;
897
898 strncpy(cmd,Cmd,99);
899
900 // usage:
901 if (strlen(cmd)<3) {
902 PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|u|*> <page#> <hexdata>");
903 PrintAndLog(" options:");
904 PrintAndLog(" -2 use slower '1 out of 256' mode");
905 PrintAndLog(" -o set OPTION Flag (needed for TI)");
906 PrintAndLog(" uid (either): ");
907 PrintAndLog(" <8B hex> full UID eg E011223344556677");
908 PrintAndLog(" s selected tag");
909 PrintAndLog(" u unaddressed mode");
910 PrintAndLog(" * scan for tag");
911 PrintAndLog(" page#: page number 0-255");
912 PrintAndLog(" hexdata: data to be written eg AA BB CC DD");
913 return 0;
914 }
915
916 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15693_WRITEBLOCK},1);
917 reqlen=c.arg[0];
918
919 // *cmd -> page num ; *cmd2 -> data
920 cmd2=cmd;
921 while (*cmd2!=' ' && *cmd2!='\t' && *cmd2) cmd2++;
922 *cmd2=0;
923 cmd2++;
924
925 pagenum=strtol(cmd,NULL,0);
926 /*if (pagenum<0) {
927 PrintAndLog("invalid pagenum");
928 return 0;
929 } */
930 req[reqlen++]=(uint8_t)pagenum;
931
932
933 while (cmd2[0] && cmd2[1]) { // hexdata, read by 2 hexchars
934 if (*cmd2==' ') {
935 cmd2++;
936 continue;
937 }
938 sscanf((char[]){cmd2[0],cmd2[1],0},"%X",&temp);
939 req[reqlen++]=temp & 0xff;
940 cmd2+=2;
941 }
942
943 reqlen=AddCrc(req,reqlen);
944
945 c.arg[0]=reqlen;
946
947 SendCommand(&c);
948
949 if (WaitForResponseTimeout(CMD_ACK,&resp,2000) && resp.arg[0]>2) {
950 recv = resp.d.asBytes;
951 if (ISO15693_CRC_CHECK==Crc(recv,resp.arg[0])) {
952 if (!(recv[0] & ISO15693_RES_ERROR)) {
953 PrintAndLog("OK");
954 } else {
955 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
956 }
957 } else {
958 PrintAndLog("CRC failed");
959 }
960 } else {
961 PrintAndLog("timeout: no answer - data may be written anyway");
962 }
963
964 return 0;
965 }
966
967 int CmdHF15CSetUID(const char *Cmd)
968 {
969 uint8_t uid[8] = {0x00};
970 uint8_t oldUid[8], newUid[8] = {0x00};
971
972 uint8_t needHelp = 0;
973 char cmdp = 1;
974
975 if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 16)) {
976 PrintAndLog("UID must include 16 HEX symbols");
977 return 1;
978 }
979
980 if (uid[0] != 0xe0) {
981 PrintAndLog("UID must begin with the byte 'E0'");
982 return 1;
983 }
984
985 while(param_getchar(Cmd, cmdp) != 0x00)
986 {
987 switch(param_getchar(Cmd, cmdp))
988 {
989 case 'h':
990 case 'H':
991 needHelp = 1;
992 break;
993 default:
994 PrintAndLog("ERROR: Unknown parameter '%c'", param_getchar(Cmd, cmdp));
995 needHelp = 1;
996 break;
997 }
998 cmdp++;
999 }
1000
1001 if (strlen(Cmd) < 1 || needHelp) {
1002 PrintAndLog("");
1003 PrintAndLog("Usage: hf 15 csetuid <UID 16 hex symbols>");
1004 PrintAndLog("sample: hf 15 csetuid E004013344556677");
1005 PrintAndLog("Set UID for magic Chinese card (only works with such cards)");
1006 return 0;
1007 }
1008
1009 PrintAndLog("");
1010 PrintAndLog("new UID | %s", sprint_hex(uid, 8));
1011 PrintAndLog("Using backdoor Magic tag function");
1012
1013 if (!getUID(oldUid)) {
1014 PrintAndLog("Can't get old UID.");
1015 return 1;
1016 }
1017
1018 UsbCommand resp;
1019 uint8_t *recv;
1020 char *hexout;
1021 UsbCommand c = {CMD_CSETUID_ISO_15693, {0, 0, 0}};
1022 memcpy(c.d.asBytes, uid, 8);
1023
1024 SendCommand(&c);
1025
1026 for (int i=0; i<4; i++) {
1027 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
1028 recv = resp.d.asBytes;
1029 PrintAndLog("received %i octets",resp.arg[0]);
1030 hexout = (char *)malloc(resp.arg[0] * 3 + 1);
1031 if (hexout != NULL) {
1032 for (int i = 0; i < resp.arg[0]; i++) { // data in hex
1033 sprintf(&hexout[i * 3], "%02X ", recv[i]);
1034 }
1035 PrintAndLog("%s", hexout);
1036 free(hexout);
1037 }
1038 } else {
1039 PrintAndLog("timeout while waiting for reply.");
1040 }
1041 }
1042
1043 if (!getUID(newUid)) {
1044 PrintAndLog("Can't get new UID.");
1045 return 1;
1046 }
1047
1048 PrintAndLog("");
1049 PrintAndLog("old UID : %02X %02X %02X %02X %02X %02X %02X %02X", oldUid[7], oldUid[6], oldUid[5], oldUid[4], oldUid[3], oldUid[2], oldUid[1], oldUid[0]);
1050 PrintAndLog("new UID : %02X %02X %02X %02X %02X %02X %02X %02X", newUid[7], newUid[6], newUid[5], newUid[4], newUid[3], newUid[2], newUid[1], newUid[0]);
1051 return 0;
1052 }
1053
1054
1055 static command_t CommandTable15Cmd[] =
1056 {
1057 {"help", CmdHF15CmdHelp, 1, "This Help"},
1058 {"inquiry", CmdHF15CmdInquiry, 0, "Search for tags in range"},
1059 /*
1060 {"select", CmdHF15CmdSelect, 0, "Select an tag with a specific UID for further commands"},
1061 */
1062 {"read", CmdHF15CmdRead, 0, "Read a block"},
1063 {"write", CmdHF15CmdWrite, 0, "Write a block"},
1064 {"readmulti",CmdHF15CmdReadmulti, 0, "Reads multiple Blocks"},
1065 {"sysinfo",CmdHF15CmdSysinfo, 0, "Get Card Information"},
1066 {"raw", CmdHF15CmdRaw, 0, "Send raw hex data to tag"},
1067 {"csetuid", CmdHF15CSetUID, 0, "Set UID for magic Chinese card"},
1068 {"debug", CmdHF15CmdDebug, 0, "Turn debugging on/off"},
1069 {NULL, NULL, 0, NULL}
1070 };
1071
1072 int CmdHF15Cmd(const char *Cmd)
1073 {
1074 CmdsParse(CommandTable15Cmd, Cmd);
1075 return 0;
1076 }
1077
1078 int CmdHF15CmdHelp(const char *Cmd)
1079 {
1080 CmdsHelp(CommandTable15Cmd);
1081 return 0;
1082 }
1083
Impressum, Datenschutz