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