]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhf15.c
CHG: added possiblity to send <UID> into the "HF 15 SIM"
[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 char cmdp = param_getchar(Cmd, 0);
302 uint8_t uid[8] = {0x00};
303
304 //E0 16 24 00 00 00 00 00
305 if (cmdp == 'h' || cmdp == 'H') {
306 PrintAndLog("Usage: hf 15 sim <UID>");
307 PrintAndLog("");
308 PrintAndLog(" sample: hf 15 sim E016240000000000");
309 return 0;
310 }
311
312 if (param_gethex(Cmd, 0, uid, 16)) {
313 PrintAndLog("UID must include 16 HEX symbols");
314 return 0;
315 }
316
317 PrintAndLog("Starting simulating UID %02X %02X %02X %02X %02X %02X %02X %02X",
318 uid[0],uid[1],uid[2],uid[3],uid[4], uid[5], uid[6], uid[7]);
319
320 UsbCommand c = {CMD_SIMTAG_ISO_15693, {0, 0, 0}};
321 SendCommand(&c);
322 return 0;
323 }
324
325 // finds the AFI (Application Family Idendifier) of a card, by trying all values
326 // (There is no standard way of reading the AFI, allthough some tags support this)
327 int CmdHF15Afi(const char *Cmd)
328 {
329 UsbCommand c = {CMD_ISO_15693_FIND_AFI, {strtol(Cmd, NULL, 0), 0, 0}};
330 SendCommand(&c);
331 return 0;
332 }
333
334 // Reads all memory pages
335 int CmdHF15DumpMem(const char*Cmd) {
336 UsbCommand resp;
337 uint8_t uid[8];
338 uint8_t *recv=NULL;
339 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
340 uint8_t *req=c.d.asBytes;
341 int reqlen=0;
342 int blocknum=0;
343 char output[80];
344
345 if (!getUID(uid)) {
346 PrintAndLog("No Tag found.");
347 return 0;
348 }
349
350 PrintAndLog("Reading memory from tag UID=%s",sprintUID(NULL,uid));
351 PrintAndLog("Tag Info: %s",getTagInfo(uid));
352
353 for (int retry=0; retry<5; retry++) {
354
355 req[0]= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
356 ISO15_REQ_NONINVENTORY | ISO15_REQ_ADDRESS;
357 req[1]=ISO15_CMD_READ;
358 memcpy(&req[2],uid,8);
359 req[10]=blocknum;
360 reqlen=AddCrc(req,11);
361 c.arg[0]=reqlen;
362
363 SendCommand(&c);
364
365 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
366 recv = resp.d.asBytes;
367 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
368 if (!(recv[0] & ISO15_RES_ERROR)) {
369 retry=0;
370 *output=0; // reset outputstring
371 sprintf(output, "Block %02x ",blocknum);
372 for ( int i=1; i<resp.arg[0]-2; i++) { // data in hex
373 sprintf(output+strlen(output),"%02X ",recv[i]);
374 }
375 strcat(output," ");
376 for ( int i=1; i<resp.arg[0]-2; i++) { // data in cleaned ascii
377 sprintf(output+strlen(output),"%c",(recv[i]>31 && recv[i]<127)?recv[i]:'.');
378 }
379 PrintAndLog("%s",output);
380 blocknum++;
381 // PrintAndLog("bn=%i",blocknum);
382 } else {
383 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
384 return 0;
385 }
386 } // else PrintAndLog("crc");
387 } // else PrintAndLog("r null");
388 } // retry
389 // TODO: need fix
390 // if (resp.arg[0]<3)
391 // PrintAndLog("Lost Connection");
392 // else if (ISO15_CRC_CHECK!=Crc(resp.d.asBytes,resp.arg[0]))
393 // PrintAndLog("CRC Failed");
394 // else
395 // PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
396 return 0;
397 }
398
399
400 // "HF 15" interface
401
402 static command_t CommandTable15[] =
403 {
404 {"help", CmdHF15Help, 1, "This help"},
405 {"demod", CmdHF15Demod, 1, "Demodulate ISO15693 from tag"},
406 {"read", CmdHF15Read, 0, "Read HF tag (ISO 15693)"},
407 {"record", CmdHF15Record, 0, "Record Samples (ISO 15693)"}, // atrox
408 {"reader", CmdHF15Reader, 0, "Act like an ISO15693 reader"},
409 {"sim", CmdHF15Sim, 0, "Fake an ISO15693 tag"},
410 {"cmd", CmdHF15Cmd, 0, "Send direct commands to ISO15693 tag"},
411 {"findafi", CmdHF15Afi, 0, "Brute force AFI of an ISO15693 tag"},
412 {"dumpmemory", CmdHF15DumpMem, 0, "Read all memory pages of an ISO15693 tag"},
413 {NULL, NULL, 0, NULL}
414 };
415
416 int CmdHF15(const char *Cmd)
417 {
418 CmdsParse(CommandTable15, Cmd);
419 return 0;
420 }
421
422 int CmdHF15Help(const char *Cmd)
423 {
424 CmdsHelp(CommandTable15);
425 return 0;
426 }
427
428
429 // "HF 15 Cmd" Interface
430 // Allows direct communication with the tag on command level
431
432 int CmdHF15CmdInquiry(const char *Cmd)
433 {
434 UsbCommand resp;
435 uint8_t *recv;
436 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
437 uint8_t *req=c.d.asBytes;
438 int reqlen=0;
439
440 req[0]= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
441 ISO15_REQ_INVENTORY | ISO15_REQINV_SLOT1;
442 req[1]=ISO15_CMD_INVENTORY;
443 req[2]=0; // mask length
444 reqlen=AddCrc(req,3);
445 c.arg[0]=reqlen;
446
447 SendCommand(&c);
448
449 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
450 if (resp.arg[0]>=12) {
451 recv = resp.d.asBytes;
452 PrintAndLog("UID=%s",sprintUID(NULL,&recv[2]));
453 PrintAndLog("Tag Info: %s",getTagInfo(&recv[2]));
454 } else {
455 PrintAndLog("Response to short, just %i bytes. No tag?\n",resp.arg[0]);
456 }
457 } else {
458 PrintAndLog("timeout.");
459 }
460 return 0;
461 }
462
463
464 // Turns debugging on(1)/off(0)
465 int CmdHF15CmdDebug( const char *cmd) {
466 int debug=atoi(cmd);
467 if (strlen(cmd)<1) {
468 PrintAndLog("Usage: hf 15 cmd debug <0|1>");
469 PrintAndLog(" 0 no debugging");
470 PrintAndLog(" 1 turn debugging on");
471 return 0;
472 }
473
474 UsbCommand c = {CMD_ISO_15693_DEBUG, {debug, 0, 0}};
475 SendCommand(&c);
476 return 0;
477 }
478
479
480 int CmdHF15CmdRaw (const char *cmd) {
481 UsbCommand resp;
482 uint8_t *recv;
483 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
484 int reply=1;
485 int fast=1;
486 int crc=0;
487 char buf[5]="";
488 int i=0;
489 uint8_t data[100];
490 unsigned int datalen=0, temp;
491 char *hexout;
492
493
494 if (strlen(cmd)<3) {
495 PrintAndLog("Usage: hf 15 cmd raw [-r] [-2] [-c] <0A 0B 0C ... hex>");
496 PrintAndLog(" -r do not read response");
497 PrintAndLog(" -2 use slower '1 out of 256' mode");
498 PrintAndLog(" -c calculate and append CRC");
499 PrintAndLog(" Tip: turn on debugging for verbose output");
500 return 0;
501 }
502
503 // strip
504 while (*cmd==' ' || *cmd=='\t') cmd++;
505
506 while (cmd[i]!='\0') {
507 if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; }
508 if (cmd[i]=='-') {
509 switch (cmd[i+1]) {
510 case 'r':
511 case 'R':
512 reply=0;
513 break;
514 case '2':
515 fast=0;
516 break;
517 case 'c':
518 case 'C':
519 crc=1;
520 break;
521 default:
522 PrintAndLog("Invalid option");
523 return 0;
524 }
525 i+=2;
526 continue;
527 }
528 if ((cmd[i]>='0' && cmd[i]<='9') ||
529 (cmd[i]>='a' && cmd[i]<='f') ||
530 (cmd[i]>='A' && cmd[i]<='F') ) {
531 buf[strlen(buf)+1]=0;
532 buf[strlen(buf)]=cmd[i];
533 i++;
534
535 if (strlen(buf)>=2) {
536 sscanf(buf,"%x",&temp);
537 data[datalen]=(uint8_t)(temp & 0xff);
538 datalen++;
539 *buf=0;
540 }
541 continue;
542 }
543 PrintAndLog("Invalid char on input");
544 return 0;
545 }
546 if (crc) datalen=AddCrc(data,datalen);
547
548 c.arg[0]=datalen;
549 c.arg[1]=fast;
550 c.arg[2]=reply;
551 memcpy(c.d.asBytes,data,datalen);
552
553 SendCommand(&c);
554
555 if (reply) {
556 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
557 recv = resp.d.asBytes;
558 PrintAndLog("received %i octets",resp.arg[0]);
559 hexout = (char *)malloc(resp.arg[0] * 3 + 1);
560 if (hexout != NULL) {
561 for (int i = 0; i < resp.arg[0]; i++) { // data in hex
562 sprintf(&hexout[i * 3], "%02X ", recv[i]);
563 }
564 PrintAndLog("%s", hexout);
565 free(hexout);
566 }
567 } else {
568 PrintAndLog("timeout while waiting for reply.");
569 }
570
571 } // if reply
572 return 0;
573 }
574
575
576 /**
577 * parses common HF 15 CMD parameters and prepares some data structures
578 * Parameters:
579 * **cmd command line
580 */
581 int prepareHF15Cmd(char **cmd, UsbCommand *c, uint8_t iso15cmd[], int iso15cmdlen) {
582 int temp;
583 uint8_t *req = c->d.asBytes;
584 uint8_t uid[8] = {0x00};
585 uint32_t reqlen = 0;
586
587 // strip
588 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
589
590 if (strstr(*cmd,"-2")==*cmd) {
591 c->arg[1]=0; // use 1of256
592 (*cmd)+=2;
593 }
594
595 // strip
596 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
597
598 if (strstr(*cmd,"-o")==*cmd) {
599 req[reqlen]=ISO15_REQ_OPTION;
600 (*cmd)+=2;
601 }
602
603 // strip
604 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
605
606 switch (**cmd) {
607 case 0:
608 PrintAndLog("missing addr");
609 return 0;
610 break;
611 case 's':
612 case 'S':
613 // you must have selected the tag earlier
614 req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
615 ISO15_REQ_NONINVENTORY | ISO15_REQ_SELECT;
616 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
617 reqlen+=iso15cmdlen;
618 break;
619 case 'u':
620 case 'U':
621 // unaddressed mode may not be supported by all vendors
622 req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
623 ISO15_REQ_NONINVENTORY;
624 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
625 reqlen+=iso15cmdlen;
626 break;
627 case '*':
628 // we scan for the UID ourself
629 req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
630 ISO15_REQ_NONINVENTORY | ISO15_REQ_ADDRESS;
631 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
632 reqlen+=iso15cmdlen;
633 if (!getUID(uid)) {
634 PrintAndLog("No Tag found");
635 return 0;
636 }
637 memcpy(req+reqlen,uid,8);
638 PrintAndLog("Detected UID %s",sprintUID(NULL,uid));
639 reqlen+=8;
640 break;
641 default:
642 req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
643 ISO15_REQ_NONINVENTORY | ISO15_REQ_ADDRESS;
644 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
645 reqlen+=iso15cmdlen;
646
647 /* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX",
648 (short unsigned int *)&uid[7],(short unsigned int *)&uid[6],
649 (short unsigned int *)&uid[5],(short unsigned int *)&uid[4],
650 (short unsigned int *)&uid[3],(short unsigned int *)&uid[2],
651 (short unsigned int *)&uid[1],(short unsigned int *)&uid[0]); */
652 for (int i=0;i<8 && (*cmd)[i*2] && (*cmd)[i*2+1];i++) { // parse UID
653 sscanf((char[]){(*cmd)[i*2],(*cmd)[i*2+1],0},"%X",&temp);
654 uid[7-i]=temp&0xff;
655 }
656
657 PrintAndLog("Using UID %s",sprintUID(NULL,uid));
658 memcpy(&req[reqlen],&uid[0],8);
659 reqlen+=8;
660 }
661 // skip to next space
662 while (**cmd!=' ' && **cmd!='\t') (*cmd)++;
663 // skip over the space
664 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
665
666 c->arg[0]=reqlen;
667 return 1;
668 }
669
670 /**
671 * Commandline handling: HF15 CMD SYSINFO
672 * get system information from tag/VICC
673 */
674 int CmdHF15CmdSysinfo(const char *Cmd) {
675 UsbCommand resp;
676 uint8_t *recv;
677 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
678 uint8_t *req=c.d.asBytes;
679 int reqlen=0;
680 char cmdbuf[100];
681 char *cmd=cmdbuf;
682 char output[2048]="";
683 int i;
684
685 strncpy(cmd,Cmd,99);
686
687 // usage:
688 if (strlen(cmd)<1) {
689 PrintAndLog("Usage: hf 15 cmd sysinfo [options] <uid|s|u|*>");
690 PrintAndLog(" options:");
691 PrintAndLog(" -2 use slower '1 out of 256' mode");
692 PrintAndLog(" uid (either): ");
693 PrintAndLog(" <8B hex> full UID eg E011223344556677");
694 PrintAndLog(" s selected tag");
695 PrintAndLog(" u unaddressed mode");
696 PrintAndLog(" * scan for tag");
697 PrintAndLog(" start#: page number to start 0-255");
698 PrintAndLog(" count#: number of pages");
699 return 0;
700 }
701
702 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_SYSINFO},1);
703 reqlen=c.arg[0];
704
705 reqlen=AddCrc(req,reqlen);
706 c.arg[0]=reqlen;
707
708 SendCommand(&c);
709
710 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
711 recv = resp.d.asBytes;
712 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
713 if (!(recv[0] & ISO15_RES_ERROR)) {
714 *output=0; // reset outputstring
715 for ( i=1; i<resp.arg[0]-2; i++) {
716 sprintf(output+strlen(output),"%02X ",recv[i]);
717 }
718 strcat(output,"\n\r");
719 strcat(output,"UID = ");
720 strcat(output,sprintUID(NULL,recv+2));
721 strcat(output,"\n\r");
722 strcat(output,getTagInfo(recv+2)); //ABC
723 strcat(output,"\n\r");
724 i=10;
725 if (recv[1] & 0x01)
726 sprintf(output+strlen(output),"DSFID supported, set to %02X\n\r",recv[i++]);
727 else
728 strcat(output,"DSFID not supported\n\r");
729 if (recv[1] & 0x02)
730 sprintf(output+strlen(output),"AFI supported, set to %03X\n\r",recv[i++]);
731 else
732 strcat(output,"AFI not supported\n\r");
733 if (recv[1] & 0x04) {
734 strcat(output,"Tag provides info on memory layout (vendor dependent)\n\r");
735 sprintf(output+strlen(output)," %i (or %i) bytes/page x %i pages \n\r",
736 (recv[i+1]&0x1F)+1, (recv[i+1]&0x1F), recv[i]+1);
737 i+=2;
738 } else
739 strcat(output,"Tag does not provide information on memory layout\n\r");
740 if (recv[1] & 0x08) sprintf(output+strlen(output),"IC reference given: %02X\n\r",recv[i++]);
741 else strcat(output,"IC reference not given\n\r");
742
743
744 PrintAndLog("%s",output);
745 } else {
746 PrintAndLog("Tag returned Error %i: %s",recv[0],TagErrorStr(recv[0]));
747 }
748 } else {
749 PrintAndLog("CRC failed");
750 }
751 } else {
752 PrintAndLog("timeout: no answer");
753 }
754
755 return 0;
756 }
757
758 /**
759 * Commandline handling: HF15 CMD READMULTI
760 * Read multiple blocks at once (not all tags support this)
761 */
762 int CmdHF15CmdReadmulti(const char *Cmd) {
763 UsbCommand resp;
764 uint8_t *recv;
765 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
766 uint8_t *req=c.d.asBytes;
767 int reqlen=0, pagenum,pagecount;
768 char cmdbuf[100];
769 char *cmd=cmdbuf;
770 char output[2048]="";
771
772 strncpy(cmd,Cmd,99);
773
774 // usage:
775 if (strlen(cmd)<3) {
776 PrintAndLog("Usage: hf 15 cmd readmulti [options] <uid|s|u|*> <start#> <count#>");
777 PrintAndLog(" options:");
778 PrintAndLog(" -2 use slower '1 out of 256' mode");
779 PrintAndLog(" uid (either): ");
780 PrintAndLog(" <8B hex> full UID eg E011223344556677");
781 PrintAndLog(" s selected tag");
782 PrintAndLog(" u unaddressed mode");
783 PrintAndLog(" * scan for tag");
784 PrintAndLog(" start#: page number to start 0-255");
785 PrintAndLog(" count#: number of pages");
786 return 0;
787 }
788
789 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_READMULTI},1);
790 reqlen=c.arg[0];
791
792 pagenum=strtol(cmd,NULL,0);
793
794 // skip to next space
795 while (*cmd!=' ' && *cmd!='\t') cmd++;
796 // skip over the space
797 while (*cmd==' ' || *cmd=='\t') cmd++;
798
799 pagecount=strtol(cmd,NULL,0);
800 if (pagecount>0) pagecount--; // 0 means 1 page, 1 means 2 pages, ...
801
802 req[reqlen++]=(uint8_t)pagenum;
803 req[reqlen++]=(uint8_t)pagecount;
804
805 reqlen=AddCrc(req,reqlen);
806
807 c.arg[0]=reqlen;
808
809 SendCommand(&c);
810
811 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
812 recv = resp.d.asBytes;
813 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
814 if (!(recv[0] & ISO15_RES_ERROR)) {
815 *output=0; // reset outputstring
816 for ( int i=1; i<resp.arg[0]-2; i++) {
817 sprintf(output+strlen(output),"%02X ",recv[i]);
818 }
819 strcat(output," ");
820 for ( int i=1; i<resp.arg[0]-2; i++) {
821 sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.');
822 }
823 PrintAndLog("%s",output);
824 } else {
825 PrintAndLog("Tag returned Error %i: %s",recv[0],TagErrorStr(recv[0]));
826 }
827 } else {
828 PrintAndLog("CRC failed");
829 }
830 } else {
831 PrintAndLog("no answer");
832 }
833
834 return 0;
835 }
836
837 /**
838 * Commandline handling: HF15 CMD READ
839 * Reads a single Block
840 */
841 int CmdHF15CmdRead(const char *Cmd) {
842 UsbCommand resp;
843 uint8_t *recv;
844 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
845 uint8_t *req=c.d.asBytes;
846 int reqlen=0, pagenum;
847 char cmdbuf[100];
848 char *cmd=cmdbuf;
849 char output[100]="";
850
851 strncpy(cmd,Cmd,99);
852
853 // usage:
854 if (strlen(cmd)<3) {
855 PrintAndLog("Usage: hf 15 cmd read [options] <uid|s|u|*> <page#>");
856 PrintAndLog(" options:");
857 PrintAndLog(" -2 use slower '1 out of 256' mode");
858 PrintAndLog(" uid (either): ");
859 PrintAndLog(" <8B hex> full UID eg E011223344556677");
860 PrintAndLog(" s selected tag");
861 PrintAndLog(" u unaddressed mode");
862 PrintAndLog(" * scan for tag");
863 PrintAndLog(" page#: page number 0-255");
864 return 0;
865 }
866
867 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_READ},1);
868 reqlen=c.arg[0];
869
870 pagenum=strtol(cmd,NULL,0);
871 /*if (pagenum<0) {
872 PrintAndLog("invalid pagenum");
873 return 0;
874 } */
875
876 req[reqlen++]=(uint8_t)pagenum;
877
878 reqlen=AddCrc(req,reqlen);
879
880 c.arg[0]=reqlen;
881
882 SendCommand(&c);
883
884 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
885 recv = resp.d.asBytes;
886 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
887 if (!(recv[0] & ISO15_RES_ERROR)) {
888 *output=0; // reset outputstring
889 //sprintf(output, "Block %2i ",blocknum);
890 for ( int i=1; i<resp.arg[0]-2; i++) {
891 sprintf(output+strlen(output),"%02X ",recv[i]);
892 }
893 strcat(output," ");
894 for ( int i=1; i<resp.arg[0]-2; i++) {
895 sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.');
896 }
897 PrintAndLog("%s",output);
898 } else {
899 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
900 }
901 } else {
902 PrintAndLog("CRC failed");
903 }
904 } else {
905 PrintAndLog("no answer");
906 }
907
908 return 0;
909 }
910
911
912 /**
913 * Commandline handling: HF15 CMD WRITE
914 * Writes a single Block - might run into timeout, even when successful
915 */
916 int CmdHF15CmdWrite(const char *Cmd) {
917 UsbCommand resp;
918 uint8_t *recv;
919 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
920 uint8_t *req=c.d.asBytes;
921 int reqlen=0, pagenum, temp;
922 char cmdbuf[100];
923 char *cmd=cmdbuf;
924 char *cmd2;
925
926 strncpy(cmd,Cmd,99);
927
928 // usage:
929 if (strlen(cmd)<3) {
930 PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|u|*> <page#> <hexdata>");
931 PrintAndLog(" options:");
932 PrintAndLog(" -2 use slower '1 out of 256' mode");
933 PrintAndLog(" -o set OPTION Flag (needed for TI)");
934 PrintAndLog(" uid (either): ");
935 PrintAndLog(" <8B hex> full UID eg E011223344556677");
936 PrintAndLog(" s selected tag");
937 PrintAndLog(" u unaddressed mode");
938 PrintAndLog(" * scan for tag");
939 PrintAndLog(" page#: page number 0-255");
940 PrintAndLog(" hexdata: data to be written eg AA BB CC DD");
941 return 0;
942 }
943
944 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_WRITE},1);
945 reqlen=c.arg[0];
946
947 // *cmd -> page num ; *cmd2 -> data
948 cmd2=cmd;
949 while (*cmd2!=' ' && *cmd2!='\t' && *cmd2) cmd2++;
950 *cmd2=0;
951 cmd2++;
952
953 pagenum=strtol(cmd,NULL,0);
954 /*if (pagenum<0) {
955 PrintAndLog("invalid pagenum");
956 return 0;
957 } */
958 req[reqlen++]=(uint8_t)pagenum;
959
960
961 while (cmd2[0] && cmd2[1]) { // hexdata, read by 2 hexchars
962 if (*cmd2==' ') {
963 cmd2++;
964 continue;
965 }
966 sscanf((char[]){cmd2[0],cmd2[1],0},"%X",&temp);
967 req[reqlen++]=temp & 0xff;
968 cmd2+=2;
969 }
970
971 reqlen=AddCrc(req,reqlen);
972
973 c.arg[0]=reqlen;
974
975 SendCommand(&c);
976
977 if (WaitForResponseTimeout(CMD_ACK,&resp,2000) && resp.arg[0]>2) {
978 recv = resp.d.asBytes;
979 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
980 if (!(recv[0] & ISO15_RES_ERROR)) {
981 PrintAndLog("OK");
982 } else {
983 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
984 }
985 } else {
986 PrintAndLog("CRC failed");
987 }
988 } else {
989 PrintAndLog("timeout: no answer - data may be written anyway");
990 }
991
992 return 0;
993 }
994
995
996
997 static command_t CommandTable15Cmd[] =
998 {
999 {"help", CmdHF15CmdHelp, 1, "This Help"},
1000 {"inquiry", CmdHF15CmdInquiry, 0, "Search for tags in range"},
1001 /*
1002 {"select", CmdHF15CmdSelect, 0, "Select an tag with a specific UID for further commands"},
1003 */
1004 {"read", CmdHF15CmdRead, 0, "Read a block"},
1005 {"write", CmdHF15CmdWrite, 0, "Write a block"},
1006 {"readmulti",CmdHF15CmdReadmulti, 0, "Reads multiple Blocks"},
1007 {"sysinfo",CmdHF15CmdSysinfo, 0, "Get Card Information"},
1008 {"raw", CmdHF15CmdRaw, 0, "Send raw hex data to tag"},
1009 {"debug", CmdHF15CmdDebug, 0, "Turn debugging on/off"},
1010 {NULL, NULL, 0, NULL}
1011 };
1012
1013 int CmdHF15Cmd(const char *Cmd)
1014 {
1015 CmdsParse(CommandTable15Cmd, Cmd);
1016 return 0;
1017 }
1018
1019 int CmdHF15CmdHelp(const char *Cmd)
1020 {
1021 CmdsHelp(CommandTable15Cmd);
1022 return 0;
1023 }
1024
Impressum, Datenschutz