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