]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhf15.c
Started iclass emulator support on device side. Not functional yet
[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
30 #include "proxmark3.h"
31 #include "data.h"
32 #include "graph.h"
33 #include "ui.h"
34 #include "util.h"
35 #include "cmdparser.h"
36 #include "cmdhf15.h"
37 #include "iso15693tools.h"
38 #include "cmdmain.h"
39
40 #define FrameSOF Iso15693FrameSOF
41 #define Logic0 Iso15693Logic0
42 #define Logic1 Iso15693Logic1
43 #define FrameEOF Iso15693FrameEOF
44
45 #define Crc(data,datalen) Iso15693Crc(data,datalen)
46 #define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
47 #define sprintUID(target,uid) Iso15693sprintUID(target,uid)
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
59 // UID, #significant Bits, "Vendor(+Product)"
60 { 0xE001000000000000LL, 16, "Motorola UK" },
61
62 // E0 02 xx
63 // 02 = ST Microelectronics
64 // XX = IC id (Chip ID Family)
65 { 0xE002000000000000LL, 16, "ST Microelectronics SA France" },
66 { 0xE002050000000000LL, 24, "ST Microelectronics; LRI64 [IC id = 05]"},
67 { 0xE002080000000000LL, 24, "ST Microelectronics; LRI2K [IC id = 08]"},
68 { 0xE0020A0000000000LL, 24, "ST Microelectronics; LRIS2K [IC id = 10]"},
69 { 0xE002440000000000LL, 24, "ST Microelectronics; LRIS64K [IC id = 68]"},
70
71 { 0xE003000000000000LL, 16, "Hitachi, Ltd Japan" },
72
73 // E0 04 xx
74 // 04 = Manufacturer code (Philips/NXP)
75 // XX = IC id (Chip ID Family)
76 //I-Code SLI SL2 ICS20 [IC id = 01]
77 //I-Code SLI-S [IC id = 02]
78 //I-Code SLI-L [IC id = 03]
79 //I-Code SLIX [IC id = 01 + bit36 set to 1 (starting from bit0 - different from normal SLI)]
80 //I-Code SLIX-S [IC id = 02 + bit36 set to 1]
81 //I-Code SLIX-L [IC id = 03 + bit36 set to 1]
82 { 0xE004000000000000LL, 16, "NXP Semiconductors Germany (Philips)" },
83 { 0xE004010000000000LL, 24, "NXP(Philips); IC SL2 ICS20/ICS21(SLI) ICS2002/ICS2102(SLIX)" },
84 { 0xE004020000000000LL, 24, "NXP(Philips); IC SL2 ICS53/ICS54(SLI-S) ICS5302/ICS5402(SLIX-S)" },
85 { 0xE004030000000000LL, 24, "NXP(Philips); IC SL2 ICS50/ICS51(SLI-L) ICS5002/ICS5102(SLIX-L)" },
86
87 // E0 05 XX .. .. ..
88 // 05 = Manufacturer code (Infineon)
89 // XX = IC id (Chip ID Family)
90 { 0xE005000000000000LL, 16, "Infineon Technologies AG Germany" },
91 { 0xE005A10000000000LL, 24, "Infineon; SRF55V01P [IC id = 161] plain mode 1kBit"},
92 { 0xE005A80000000000LL, 24, "Infineon; SRF55V01P [IC id = 168] pilot series 1kBit"},
93 { 0xE005400000000000LL, 24, "Infineon; SRF55V02P [IC id = 64] plain mode 2kBit"},
94 { 0xE005000000000000LL, 24, "Infineon; SRF55V10P [IC id = 00] plain mode 10KBit"},
95 { 0xE005500000000000LL, 24, "Infineon; SRF55V02S [IC id = 80] secure mode 2kBit"},
96 { 0xE005100000000000LL, 24, "Infineon; SRF55V10S [IC id = 16] secure mode 10KBit"},
97 { 0xE0051E0000000000LL, 23, "Infineon; SLE66r01P [IC id = 3x = My-d Move or My-d move NFC]"},
98 { 0xE005200000000000LL, 21, "Infineon; SLE66r01P [IC id = 3x = My-d Move or My-d move NFC]"},
99
100 { 0xE006000000000000LL, 16, "Cylink USA" },
101
102
103 // E0 07 xx
104 // 07 = Texas Instruments
105 // XX = from bit 41 to bit 43 = product configuration - from bit 44 to bit 47 IC id (Chip ID Family)
106 //Tag IT RFIDType-I Plus, 2kBit, TI Inlay
107 //Tag-it HF-I Plus Inlay [IC id = 00] -> b'0000 000 2kBit
108 //Tag-it HF-I Plus Chip [IC id = 64] -> b'1000 000 2kBit
109 //Tag-it HF-I Standard Chip / Inlays [IC id = 96] -> b'1100 000 256Bit
110 //Tag-it HF-I Pro Chip / Inlays [IC id = 98] -> b'1100 010 256Bit, Password protection
111 { 0xE007000000000000LL, 16, "Texas Instrument France" },
112 { 0xE007000000000000LL, 20, "Texas Instrument; Tag-it HF-I Plus Inlay; 64x32bit" },
113 { 0xE007100000000000LL, 20, "Texas Instrument; Tag-it HF-I Plus Chip; 64x32bit" },
114 { 0xE007800000000000LL, 23, "Texas Instrument; Tag-it HF-I Plus (RF-HDT-DVBB tag or Third Party Products)" },
115 { 0xE007C00000000000LL, 23, "Texas Instrument; Tag-it HF-I Standard; 8x32bit" },
116 { 0xE007C40000000000LL, 23, "Texas Instrument; Tag-it HF-I Pro; 8x23bit; password" },
117
118 { 0xE008000000000000LL, 16, "Fujitsu Limited Japan" },
119 { 0xE009000000000000LL, 16, "Matsushita Electronics Corporation, Semiconductor Company Japan" },
120 { 0xE00A000000000000LL, 16, "NEC Japan" },
121 { 0xE00B000000000000LL, 16, "Oki Electric Industry Co. Ltd Japan" },
122 { 0xE00C000000000000LL, 16, "Toshiba Corp. Japan" },
123 { 0xE00D000000000000LL, 16, "Mitsubishi Electric Corp. Japan" },
124 { 0xE00E000000000000LL, 16, "Samsung Electronics Co. Ltd Korea" },
125 { 0xE00F000000000000LL, 16, "Hynix / Hyundai, Korea" },
126 { 0xE010000000000000LL, 16, "LG-Semiconductors Co. Ltd Korea" },
127 { 0xE011000000000000LL, 16, "Emosyn-EM Microelectronics USA" },
128
129 { 0xE012000000000000LL, 16, "HID Corporation" },
130 { 0xE012000000000000LL, 16, "INSIDE Technology France" },
131 { 0xE013000000000000LL, 16, "ORGA Kartensysteme GmbH Germany" },
132 { 0xE014000000000000LL, 16, "SHARP Corporation Japan" },
133 { 0xE015000000000000LL, 16, "ATMEL France" },
134
135 { 0xE016000000000000LL, 16, "EM Microelectronic-Marin SA Switzerland (Skidata)" },
136 { 0xE016040000000000LL, 24, "EM-Marin SA (Skidata Keycard-eco); EM4034? no 'read', just 'readmulti'" },
137 { 0xE0160c0000000000LL, 24, "EM-Marin SA; EM4035?" },
138 { 0xE016100000000000LL, 24, "EM-Marin SA (Skidata); EM4135; 36x64bit start page 13" },
139 { 0xE016240000000000LL, 24, "EM-Marin SA (Skidata); EM4233;" },
140 { 0xE016940000000000LL, 24, "EM-Marin SA (Skidata); 51x64bit" },
141
142 { 0xE017000000000000LL, 16, "KSW Microtec GmbH Germany" },
143 { 0xE018000000000000LL, 16, "ZMD AG Germany" },
144 { 0xE019000000000000LL, 16, "XICOR, Inc. USA" },
145 { 0xE01A000000000000LL, 16, "Sony Corporation Japan Identifier Company Country" },
146 { 0xE01B000000000000LL, 16, "Malaysia Microelectronic Solutions Sdn. Bhd Malaysia" },
147 { 0xE01C000000000000LL, 16, "Emosyn USA" },
148 { 0xE01D000000000000LL, 16, "Shanghai Fudan Microelectronics Co. Ltd. P.R. China" },
149 { 0xE01E000000000000LL, 16, "Magellan Technology Pty Limited Australia" },
150 { 0xE01F000000000000LL, 16, "Melexis NV BO Switzerland" },
151 { 0xE020000000000000LL, 16, "Renesas Technology Corp. Japan" },
152 { 0xE021000000000000LL, 16, "TAGSYS France" },
153 { 0xE022000000000000LL, 16, "Transcore USA" },
154 { 0xE023000000000000LL, 16, "Shanghai belling corp., ltd. China" },
155 { 0xE024000000000000LL, 16, "Masktech Germany Gmbh Germany" },
156 { 0xE025000000000000LL, 16, "Innovision Research and Technology Plc UK" },
157 { 0xE026000000000000LL, 16, "Hitachi ULSI Systems Co., Ltd. Japan" },
158 { 0xE027000000000000LL, 16, "Cypak AB Sweden" },
159 { 0xE028000000000000LL, 16, "Ricoh Japan" },
160 { 0xE029000000000000LL, 16, "ASK France" },
161 { 0xE02A000000000000LL, 16, "Unicore Microsystems, LLC Russian Federation" },
162 { 0xE02B000000000000LL, 16, "Dallas Semiconductor/Maxim USA" },
163 { 0xE02C000000000000LL, 16, "Impinj, Inc. USA" },
164 { 0xE02D000000000000LL, 16, "RightPlug Alliance USA" },
165 { 0xE02E000000000000LL, 16, "Broadcom Corporation USA" },
166 { 0xE02F000000000000LL, 16, "MStar Semiconductor, Inc Taiwan, ROC" },
167 { 0xE030000000000000LL, 16, "BeeDar Technology Inc. USA" },
168 { 0xE031000000000000LL, 16, " RFIDsec Denmark" },
169 { 0xE032000000000000LL, 16, " Schweizer Electronic AG Germany" },
170 { 0xE033000000000000LL, 16, " AMIC Technology Corp Taiwan" },
171 { 0xE034000000000000LL, 16, "Mikron JSC Russia" },
172 { 0xE035000000000000LL, 16, "Fraunhofer Institute for Photonic Microsystems Germany" },
173 { 0xE036000000000000LL, 16, "IDS Microchip AG Switzerland" },
174 { 0xE037000000000000LL, 16, "Kovio USA" },
175 { 0xE038000000000000LL, 16, "HMT Microelectronic Ltd Switzerland Identifier Company Country" },
176 { 0xE039000000000000LL, 16, "Silicon Craft Technology Thailand" },
177 { 0xE03A000000000000LL, 16, "Advanced Film Device Inc. Japan" },
178 { 0xE03B000000000000LL, 16, "Nitecrest Ltd UK" },
179 { 0xE03C000000000000LL, 16, "Verayo Inc. USA" },
180 { 0xE03D000000000000LL, 16, "HID Global USA" },
181 { 0xE03E000000000000LL, 16, "Productivity Engineering Gmbh Germany" },
182 { 0xE03F000000000000LL, 16, "Austriamicrosystems AG (reserved) Austria" },
183 { 0xE040000000000000LL, 16, "Gemalto SA France" },
184 { 0xE041000000000000LL, 16, "Renesas Electronics Corporation Japan" },
185 { 0xE042000000000000LL, 16, "3Alogics Inc Korea" },
186 { 0xE043000000000000LL, 16, "Top TroniQ Asia Limited Hong Kong" },
187 { 0xE044000000000000LL, 16, "Gentag Inc (USA) USA" },
188 { 0,0,"no tag-info available" } // must be the last entry
189 };
190
191
192 // fast method to just read the UID of a tag (collission detection not supported)
193 // *buf should be large enough to fit the 64bit uid
194 // returns 1 if suceeded
195 int getUID(uint8_t *buf)
196 {
197 UsbCommand resp;
198 uint8_t *recv;
199 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
200 uint8_t *req=c.d.asBytes;
201 int reqlen=0;
202
203 for (int retry=0;retry<3; retry++) { // don't give up the at the first try
204
205 req[0]= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
206 ISO15_REQ_INVENTORY | ISO15_REQINV_SLOT1;
207 req[1]=ISO15_CMD_INVENTORY;
208 req[2]=0; // mask length
209 reqlen=AddCrc(req,3);
210 c.arg[0]=reqlen;
211
212 SendCommand(&c);
213
214 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
215 recv = resp.d.asBytes;
216 if (resp.arg[0]>=12 && ISO15_CRC_CHECK==Crc(recv,12)) {
217 memcpy(buf,&recv[2],8);
218 return 1;
219 }
220 }
221 } // retry
222 return 0;
223 }
224
225
226
227 // get a product description based on the UID
228 // uid[8] tag uid
229 // returns description of the best match
230 static char* getTagInfo(uint8_t *uid) {
231 uint64_t myuid,mask;
232 int i=0, best=-1;
233 memcpy(&myuid,uid,sizeof(uint64_t));
234 while (uidmapping[i].mask>0) {
235 mask=(~0LL) <<(64-uidmapping[i].mask);
236 if ((myuid & mask) == uidmapping[i].uid) {
237 if (best==-1) {
238 best=i;
239 } else {
240 if (uidmapping[i].mask>uidmapping[best].mask) {
241 best=i;
242 }
243 }
244 }
245 i++;
246 }
247
248 if (best>=0) return uidmapping[best].desc;
249
250 return uidmapping[i].desc;
251 }
252
253
254 // return a clear-text message to an errorcode
255 static char* TagErrorStr(uint8_t error) {
256 switch (error) {
257 case 0x01: return "The command is not supported";
258 case 0x02: return "The command is not recognised";
259 case 0x03: return "The option is not supported.";
260 case 0x0f: return "Unknown error.";
261 case 0x10: return "The specified block is not available (doesn’t exist).";
262 case 0x11: return "The specified block is already -locked and thus cannot be locked again";
263 case 0x12: return "The specified block is locked and its content cannot be changed.";
264 case 0x13: return "The specified block was not successfully programmed.";
265 case 0x14: return "The specified block was not successfully locked.";
266 default: return "Reserved for Future Use or Custom command error.";
267 }
268 }
269
270
271 // Mode 3
272 int CmdHF15Demod(const char *Cmd)
273 {
274 // The sampling rate is 106.353 ksps/s, for T = 18.8 us
275
276 int i, j;
277 int max = 0, maxPos = 0;
278
279 int skip = 4;
280
281 if (GraphTraceLen < 1000) return 0;
282
283 // First, correlate for SOF
284 for (i = 0; i < 100; i++) {
285 int corr = 0;
286 for (j = 0; j < arraylen(FrameSOF); j += skip) {
287 corr += FrameSOF[j] * GraphBuffer[i + (j / skip)];
288 }
289 if (corr > max) {
290 max = corr;
291 maxPos = i;
292 }
293 }
294 PrintAndLog("SOF at %d, correlation %d", maxPos,
295 max / (arraylen(FrameSOF) / skip));
296
297 i = maxPos + arraylen(FrameSOF) / skip;
298 int k = 0;
299 uint8_t outBuf[20];
300 memset(outBuf, 0, sizeof(outBuf));
301 uint8_t mask = 0x01;
302 for (;;) {
303 int corr0 = 0, corr1 = 0, corrEOF = 0;
304 for (j = 0; j < arraylen(Logic0); j += skip) {
305 corr0 += Logic0[j] * GraphBuffer[i + (j / skip)];
306 }
307 for (j = 0; j < arraylen(Logic1); j += skip) {
308 corr1 += Logic1[j] * GraphBuffer[i + (j / skip)];
309 }
310 for (j = 0; j < arraylen(FrameEOF); j += skip) {
311 corrEOF += FrameEOF[j] * GraphBuffer[i + (j / skip)];
312 }
313 // Even things out by the length of the target waveform.
314 corr0 *= 4;
315 corr1 *= 4;
316
317 if (corrEOF > corr1 && corrEOF > corr0) {
318 PrintAndLog("EOF at %d", i);
319 break;
320 } else if (corr1 > corr0) {
321 i += arraylen(Logic1) / skip;
322 outBuf[k] |= mask;
323 } else {
324 i += arraylen(Logic0) / skip;
325 }
326 mask <<= 1;
327 if (mask == 0) {
328 k++;
329 mask = 0x01;
330 }
331 if ((i + (int)arraylen(FrameEOF)) >= GraphTraceLen) {
332 PrintAndLog("ran off end!");
333 break;
334 }
335 }
336 if (mask != 0x01) {
337 PrintAndLog("error, uneven octet! (discard extra bits!)");
338 PrintAndLog(" mask=%02x", mask);
339 }
340 PrintAndLog("%d octets", k);
341
342 for (i = 0; i < k; i++) {
343 PrintAndLog("# %2d: %02x ", i, outBuf[i]);
344 }
345 PrintAndLog("CRC=%04x", Iso15693Crc(outBuf, k - 2));
346 return 0;
347 }
348
349
350
351 // * Acquire Samples as Reader (enables carrier, sends inquiry)
352 int CmdHF15Read(const char *Cmd)
353 {
354 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693};
355 SendCommand(&c);
356 return 0;
357 }
358
359 // Record Activity without enabeling carrier
360 int CmdHF15Record(const char *Cmd)
361 {
362 UsbCommand c = {CMD_RECORD_RAW_ADC_SAMPLES_ISO_15693};
363 SendCommand(&c);
364 return 0;
365 }
366
367 int CmdHF15Reader(const char *Cmd)
368 {
369 UsbCommand c = {CMD_READER_ISO_15693, {strtol(Cmd, NULL, 0), 0, 0}};
370 SendCommand(&c);
371 return 0;
372 }
373
374 // Simulation is still not working very good
375 int CmdHF15Sim(const char *Cmd)
376 {
377 char cmdp = param_getchar(Cmd, 0);
378 uint8_t uid[8] = {0x00};
379
380 //E0 16 24 00 00 00 00 00
381 if (cmdp == 'h' || cmdp == 'H') {
382 PrintAndLog("Usage: hf 15 sim <UID>");
383 PrintAndLog("");
384 PrintAndLog(" sample: hf 15 sim E016240000000000");
385 return 0;
386 }
387
388 if (param_gethex(Cmd, 0, uid, 16)) {
389 PrintAndLog("UID must include 16 HEX symbols");
390 return 0;
391 }
392
393 PrintAndLog("Starting simulating UID %02X %02X %02X %02X %02X %02X %02X %02X",
394 uid[0],uid[1],uid[2],uid[3],uid[4], uid[5], uid[6], uid[7]);
395
396 UsbCommand c = {CMD_SIMTAG_ISO_15693, {0, 0, 0}};
397 memcpy(c.d.asBytes,uid,8);
398
399 SendCommand(&c);
400 return 0;
401 }
402
403 // finds the AFI (Application Family Idendifier) of a card, by trying all values
404 // (There is no standard way of reading the AFI, allthough some tags support this)
405 int CmdHF15Afi(const char *Cmd)
406 {
407 UsbCommand c = {CMD_ISO_15693_FIND_AFI, {strtol(Cmd, NULL, 0), 0, 0}};
408 SendCommand(&c);
409 return 0;
410 }
411
412 // Reads all memory pages
413 int CmdHF15DumpMem(const char*Cmd) {
414 UsbCommand resp;
415 uint8_t uid[8];
416 uint8_t *recv=NULL;
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 int blocknum=0;
421 char output[80];
422
423 if (!getUID(uid)) {
424 PrintAndLog("No Tag found.");
425 return 0;
426 }
427
428 PrintAndLog("Reading memory from tag UID=%s",sprintUID(NULL,uid));
429 PrintAndLog("Tag Info: %s",getTagInfo(uid));
430
431 for (int retry=0; retry<5; retry++) {
432
433 req[0]= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
434 ISO15_REQ_NONINVENTORY | ISO15_REQ_ADDRESS;
435 req[1]=ISO15_CMD_READ;
436 memcpy(&req[2],uid,8);
437 req[10]=blocknum;
438 reqlen=AddCrc(req,11);
439 c.arg[0]=reqlen;
440
441 SendCommand(&c);
442
443 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
444 recv = resp.d.asBytes;
445 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
446 if (!(recv[0] & ISO15_RES_ERROR)) {
447 retry=0;
448 *output=0; // reset outputstring
449 sprintf(output, "Block %02x ",blocknum);
450 for ( int i=1; i<resp.arg[0]-2; i++) { // data in hex
451 sprintf(output+strlen(output),"%02X ",recv[i]);
452 }
453 strcat(output," ");
454 for ( int i=1; i<resp.arg[0]-2; i++) { // data in cleaned ascii
455 sprintf(output+strlen(output),"%c",(recv[i]>31 && recv[i]<127)?recv[i]:'.');
456 }
457 PrintAndLog("%s",output);
458 blocknum++;
459 // PrintAndLog("bn=%i",blocknum);
460 } else {
461 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
462 return 0;
463 }
464 } // else PrintAndLog("crc");
465 } // else PrintAndLog("r null");
466 } // retry
467 // TODO: need fix
468 // if (resp.arg[0]<3)
469 // PrintAndLog("Lost Connection");
470 // else if (ISO15_CRC_CHECK!=Crc(resp.d.asBytes,resp.arg[0]))
471 // PrintAndLog("CRC Failed");
472 // else
473 // PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
474 return 0;
475 }
476
477
478 // "HF 15" interface
479
480 static command_t CommandTable15[] =
481 {
482 {"help", CmdHF15Help, 1, "This help"},
483 {"demod", CmdHF15Demod, 1, "Demodulate ISO15693 from tag"},
484 {"read", CmdHF15Read, 0, "Read HF tag (ISO 15693)"},
485 {"record", CmdHF15Record, 0, "Record Samples (ISO 15693)"}, // atrox
486 {"reader", CmdHF15Reader, 0, "Act like an ISO15693 reader"},
487 {"sim", CmdHF15Sim, 0, "Fake an ISO15693 tag"},
488 {"cmd", CmdHF15Cmd, 0, "Send direct commands to ISO15693 tag"},
489 {"findafi", CmdHF15Afi, 0, "Brute force AFI of an ISO15693 tag"},
490 {"dumpmemory", CmdHF15DumpMem, 0, "Read all memory pages of an ISO15693 tag"},
491 {NULL, NULL, 0, NULL}
492 };
493
494 int CmdHF15(const char *Cmd)
495 {
496 CmdsParse(CommandTable15, Cmd);
497 return 0;
498 }
499
500 int CmdHF15Help(const char *Cmd)
501 {
502 CmdsHelp(CommandTable15);
503 return 0;
504 }
505
506
507 // "HF 15 Cmd" Interface
508 // Allows direct communication with the tag on command level
509
510 int CmdHF15CmdInquiry(const char *Cmd)
511 {
512 UsbCommand resp;
513 uint8_t *recv;
514 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
515 uint8_t *req=c.d.asBytes;
516 int reqlen=0;
517
518 req[0]= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
519 ISO15_REQ_INVENTORY | ISO15_REQINV_SLOT1;
520 req[1]=ISO15_CMD_INVENTORY;
521 req[2]=0; // mask length
522 reqlen=AddCrc(req,3);
523 c.arg[0]=reqlen;
524
525 SendCommand(&c);
526
527 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
528 if (resp.arg[0]>=12) {
529 recv = resp.d.asBytes;
530 PrintAndLog("UID=%s",sprintUID(NULL,&recv[2]));
531 PrintAndLog("Tag Info: %s",getTagInfo(&recv[2]));
532 } else {
533 PrintAndLog("Response to short, just %i bytes. No tag?\n",resp.arg[0]);
534 }
535 } else {
536 PrintAndLog("timeout.");
537 }
538 return 0;
539 }
540
541
542 // Turns debugging on(1)/off(0)
543 int CmdHF15CmdDebug( const char *cmd) {
544 int debug=atoi(cmd);
545 if (strlen(cmd)<1) {
546 PrintAndLog("Usage: hf 15 cmd debug <0|1>");
547 PrintAndLog(" 0 no debugging");
548 PrintAndLog(" 1 turn debugging on");
549 return 0;
550 }
551
552 UsbCommand c = {CMD_ISO_15693_DEBUG, {debug, 0, 0}};
553 SendCommand(&c);
554 return 0;
555 }
556
557
558 int CmdHF15CmdRaw (const char *cmd) {
559 UsbCommand resp;
560 uint8_t *recv;
561 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
562 int reply=1;
563 int fast=1;
564 int crc=0;
565 char buf[5]="";
566 int i=0;
567 uint8_t data[100];
568 unsigned int datalen=0, temp;
569 char *hexout;
570
571
572 if (strlen(cmd)<3) {
573 PrintAndLog("Usage: hf 15 cmd raw [-r] [-2] [-c] <0A 0B 0C ... hex>");
574 PrintAndLog(" -r do not read response");
575 PrintAndLog(" -2 use slower '1 out of 256' mode");
576 PrintAndLog(" -c calculate and append CRC");
577 PrintAndLog(" Tip: turn on debugging for verbose output");
578 return 0;
579 }
580
581 // strip
582 while (*cmd==' ' || *cmd=='\t') cmd++;
583
584 while (cmd[i]!='\0') {
585 if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; }
586 if (cmd[i]=='-') {
587 switch (cmd[i+1]) {
588 case 'r':
589 case 'R':
590 reply=0;
591 break;
592 case '2':
593 fast=0;
594 break;
595 case 'c':
596 case 'C':
597 crc=1;
598 break;
599 default:
600 PrintAndLog("Invalid option");
601 return 0;
602 }
603 i+=2;
604 continue;
605 }
606 if ((cmd[i]>='0' && cmd[i]<='9') ||
607 (cmd[i]>='a' && cmd[i]<='f') ||
608 (cmd[i]>='A' && cmd[i]<='F') ) {
609 buf[strlen(buf)+1]=0;
610 buf[strlen(buf)]=cmd[i];
611 i++;
612
613 if (strlen(buf)>=2) {
614 sscanf(buf,"%x",&temp);
615 data[datalen]=(uint8_t)(temp & 0xff);
616 datalen++;
617 *buf=0;
618 }
619 continue;
620 }
621 PrintAndLog("Invalid char on input");
622 return 0;
623 }
624 if (crc) datalen=AddCrc(data,datalen);
625
626 c.arg[0]=datalen;
627 c.arg[1]=fast;
628 c.arg[2]=reply;
629 memcpy(c.d.asBytes,data,datalen);
630
631 SendCommand(&c);
632
633 if (reply) {
634 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
635 recv = resp.d.asBytes;
636 PrintAndLog("received %i octets",resp.arg[0]);
637 hexout = (char *)malloc(resp.arg[0] * 3 + 1);
638 if (hexout != NULL) {
639 for (int i = 0; i < resp.arg[0]; i++) { // data in hex
640 sprintf(&hexout[i * 3], "%02X ", recv[i]);
641 }
642 PrintAndLog("%s", hexout);
643 free(hexout);
644 }
645 } else {
646 PrintAndLog("timeout while waiting for reply.");
647 }
648
649 } // if reply
650 return 0;
651 }
652
653
654 /**
655 * parses common HF 15 CMD parameters and prepares some data structures
656 * Parameters:
657 * **cmd command line
658 */
659 int prepareHF15Cmd(char **cmd, UsbCommand *c, uint8_t iso15cmd[], int iso15cmdlen) {
660 int temp;
661 uint8_t *req=c->d.asBytes;
662 uint8_t uid[8] = {0x00};
663 uint32_t reqlen=0;
664
665 // strip
666 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
667
668 if (strstr(*cmd,"-2")==*cmd) {
669 c->arg[1]=0; // use 1of256
670 (*cmd)+=2;
671 }
672
673 // strip
674 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
675
676 if (strstr(*cmd,"-o")==*cmd) {
677 req[reqlen]=ISO15_REQ_OPTION;
678 (*cmd)+=2;
679 }
680
681 // strip
682 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
683
684 switch (**cmd) {
685 case 0:
686 PrintAndLog("missing addr");
687 return 0;
688 break;
689 case 's':
690 case 'S':
691 // you must have selected the tag earlier
692 req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
693 ISO15_REQ_NONINVENTORY | ISO15_REQ_SELECT;
694 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
695 reqlen+=iso15cmdlen;
696 break;
697 case 'u':
698 case 'U':
699 // unaddressed mode may not be supported by all vendors
700 req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
701 ISO15_REQ_NONINVENTORY;
702 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
703 reqlen+=iso15cmdlen;
704 break;
705 case '*':
706 // we scan for the UID ourself
707 req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
708 ISO15_REQ_NONINVENTORY | ISO15_REQ_ADDRESS;
709 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
710 reqlen+=iso15cmdlen;
711 if (!getUID(uid)) {
712 PrintAndLog("No Tag found");
713 return 0;
714 }
715 memcpy(req+reqlen,uid,8);
716 PrintAndLog("Detected UID %s",sprintUID(NULL,uid));
717 reqlen+=8;
718 break;
719 default:
720 req[reqlen++]|= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
721 ISO15_REQ_NONINVENTORY | ISO15_REQ_ADDRESS;
722 memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen);
723 reqlen+=iso15cmdlen;
724
725 /* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX",
726 (short unsigned int *)&uid[7],(short unsigned int *)&uid[6],
727 (short unsigned int *)&uid[5],(short unsigned int *)&uid[4],
728 (short unsigned int *)&uid[3],(short unsigned int *)&uid[2],
729 (short unsigned int *)&uid[1],(short unsigned int *)&uid[0]); */
730 for (int i=0;i<8 && (*cmd)[i*2] && (*cmd)[i*2+1];i++) { // parse UID
731 sscanf((char[]){(*cmd)[i*2],(*cmd)[i*2+1],0},"%X",&temp);
732 uid[7-i]=temp&0xff;
733 }
734
735 PrintAndLog("Using UID %s",sprintUID(NULL,uid));
736 memcpy(&req[reqlen],&uid[0],8);
737 reqlen+=8;
738 }
739 // skip to next space
740 while (**cmd!=' ' && **cmd!='\t') (*cmd)++;
741 // skip over the space
742 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
743
744 c->arg[0]=reqlen;
745 return 1;
746 }
747
748 /**
749 * Commandline handling: HF15 CMD SYSINFO
750 * get system information from tag/VICC
751 */
752 int CmdHF15CmdSysinfo(const char *Cmd) {
753 UsbCommand resp;
754 uint8_t *recv;
755 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
756 uint8_t *req=c.d.asBytes;
757 int reqlen=0;
758 char cmdbuf[100];
759 char *cmd=cmdbuf;
760 char output[2048]="";
761 int i;
762
763 strncpy(cmd,Cmd,99);
764
765 // usage:
766 if (strlen(cmd)<1) {
767 PrintAndLog("Usage: hf 15 cmd sysinfo [options] <uid|s|u|*>");
768 PrintAndLog(" options:");
769 PrintAndLog(" -2 use slower '1 out of 256' mode");
770 PrintAndLog(" uid (either): ");
771 PrintAndLog(" <8B hex> full UID eg E011223344556677");
772 PrintAndLog(" s selected tag");
773 PrintAndLog(" u unaddressed mode");
774 PrintAndLog(" * scan for tag");
775 PrintAndLog(" start#: page number to start 0-255");
776 PrintAndLog(" count#: number of pages");
777 return 0;
778 }
779
780 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_SYSINFO},1);
781 reqlen=c.arg[0];
782
783 reqlen=AddCrc(req,reqlen);
784 c.arg[0]=reqlen;
785
786 SendCommand(&c);
787
788 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
789 recv = resp.d.asBytes;
790 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
791 if (!(recv[0] & ISO15_RES_ERROR)) {
792 *output=0; // reset outputstring
793 for ( i=1; i<resp.arg[0]-2; i++) {
794 sprintf(output+strlen(output),"%02X ",recv[i]);
795 }
796 strcat(output,"\n\r");
797 strcat(output,"UID = ");
798 strcat(output,sprintUID(NULL,recv+2));
799 strcat(output,"\n\r");
800 strcat(output,getTagInfo(recv+2)); //ABC
801 strcat(output,"\n\r");
802 i=10;
803 if (recv[1] & 0x01)
804 sprintf(output+strlen(output),"DSFID supported, set to %02X\n\r",recv[i++]);
805 else
806 strcat(output,"DSFID not supported\n\r");
807 if (recv[1] & 0x02)
808 sprintf(output+strlen(output),"AFI supported, set to %03X\n\r",recv[i++]);
809 else
810 strcat(output,"AFI not supported\n\r");
811 if (recv[1] & 0x04) {
812 strcat(output,"Tag provides info on memory layout (vendor dependent)\n\r");
813 sprintf(output+strlen(output)," %i (or %i) bytes/page x %i pages \n\r",
814 (recv[i+1]&0x1F)+1, (recv[i+1]&0x1F), recv[i]+1);
815 i+=2;
816 } else
817 strcat(output,"Tag does not provide information on memory layout\n\r");
818 if (recv[1] & 0x08) sprintf(output+strlen(output),"IC reference given: %02X\n\r",recv[i++]);
819 else strcat(output,"IC reference not given\n\r");
820
821
822 PrintAndLog("%s",output);
823 } else {
824 PrintAndLog("Tag returned Error %i: %s",recv[0],TagErrorStr(recv[0]));
825 }
826 } else {
827 PrintAndLog("CRC failed");
828 }
829 } else {
830 PrintAndLog("timeout: no answer");
831 }
832
833 return 0;
834 }
835
836 /**
837 * Commandline handling: HF15 CMD READMULTI
838 * Read multiple blocks at once (not all tags support this)
839 */
840 int CmdHF15CmdReadmulti(const char *Cmd) {
841 UsbCommand resp;
842 uint8_t *recv;
843 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
844 uint8_t *req=c.d.asBytes;
845 int reqlen=0, pagenum,pagecount;
846 char cmdbuf[100];
847 char *cmd=cmdbuf;
848 char output[2048]="";
849
850 strncpy(cmd,Cmd,99);
851
852 // usage:
853 if (strlen(cmd)<3) {
854 PrintAndLog("Usage: hf 15 cmd readmulti [options] <uid|s|u|*> <start#> <count#>");
855 PrintAndLog(" options:");
856 PrintAndLog(" -2 use slower '1 out of 256' mode");
857 PrintAndLog(" uid (either): ");
858 PrintAndLog(" <8B hex> full UID eg E011223344556677");
859 PrintAndLog(" s selected tag");
860 PrintAndLog(" u unaddressed mode");
861 PrintAndLog(" * scan for tag");
862 PrintAndLog(" start#: page number to start 0-255");
863 PrintAndLog(" count#: number of pages");
864 return 0;
865 }
866
867 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_READMULTI},1);
868 reqlen=c.arg[0];
869
870 pagenum=strtol(cmd,NULL,0);
871
872 // skip to next space
873 while (*cmd!=' ' && *cmd!='\t') cmd++;
874 // skip over the space
875 while (*cmd==' ' || *cmd=='\t') cmd++;
876
877 pagecount=strtol(cmd,NULL,0);
878 if (pagecount>0) pagecount--; // 0 means 1 page, 1 means 2 pages, ...
879
880 req[reqlen++]=(uint8_t)pagenum;
881 req[reqlen++]=(uint8_t)pagecount;
882
883 reqlen=AddCrc(req,reqlen);
884
885 c.arg[0]=reqlen;
886
887 SendCommand(&c);
888
889 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
890 recv = resp.d.asBytes;
891 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
892 if (!(recv[0] & ISO15_RES_ERROR)) {
893 *output=0; // reset outputstring
894 for ( int i=1; i<resp.arg[0]-2; i++) {
895 sprintf(output+strlen(output),"%02X ",recv[i]);
896 }
897 strcat(output," ");
898 for ( int i=1; i<resp.arg[0]-2; i++) {
899 sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.');
900 }
901 PrintAndLog("%s",output);
902 } else {
903 PrintAndLog("Tag returned Error %i: %s",recv[0],TagErrorStr(recv[0]));
904 }
905 } else {
906 PrintAndLog("CRC failed");
907 }
908 } else {
909 PrintAndLog("no answer");
910 }
911
912 return 0;
913 }
914
915 /**
916 * Commandline handling: HF15 CMD READ
917 * Reads a single Block
918 */
919 int CmdHF15CmdRead(const char *Cmd) {
920 UsbCommand resp;
921 uint8_t *recv;
922 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
923 uint8_t *req=c.d.asBytes;
924 int reqlen=0, pagenum;
925 char cmdbuf[100];
926 char *cmd=cmdbuf;
927 char output[100]="";
928
929 strncpy(cmd,Cmd,99);
930
931 // usage:
932 if (strlen(cmd)<3) {
933 PrintAndLog("Usage: hf 15 cmd read [options] <uid|s|u|*> <page#>");
934 PrintAndLog(" options:");
935 PrintAndLog(" -2 use slower '1 out of 256' mode");
936 PrintAndLog(" uid (either): ");
937 PrintAndLog(" <8B hex> full UID eg E011223344556677");
938 PrintAndLog(" s selected tag");
939 PrintAndLog(" u unaddressed mode");
940 PrintAndLog(" * scan for tag");
941 PrintAndLog(" page#: page number 0-255");
942 return 0;
943 }
944
945 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_READ},1);
946 reqlen=c.arg[0];
947
948 pagenum=strtol(cmd,NULL,0);
949 /*if (pagenum<0) {
950 PrintAndLog("invalid pagenum");
951 return 0;
952 } */
953
954 req[reqlen++]=(uint8_t)pagenum;
955
956 reqlen=AddCrc(req,reqlen);
957
958 c.arg[0]=reqlen;
959
960 SendCommand(&c);
961
962 if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
963 recv = resp.d.asBytes;
964 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
965 if (!(recv[0] & ISO15_RES_ERROR)) {
966 *output=0; // reset outputstring
967 //sprintf(output, "Block %2i ",blocknum);
968 for ( int i=1; i<resp.arg[0]-2; i++) {
969 sprintf(output+strlen(output),"%02X ",recv[i]);
970 }
971 strcat(output," ");
972 for ( int i=1; i<resp.arg[0]-2; i++) {
973 sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.');
974 }
975 PrintAndLog("%s",output);
976 } else {
977 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
978 }
979 } else {
980 PrintAndLog("CRC failed");
981 }
982 } else {
983 PrintAndLog("no answer");
984 }
985
986 return 0;
987 }
988
989
990 /**
991 * Commandline handling: HF15 CMD WRITE
992 * Writes a single Block - might run into timeout, even when successful
993 */
994 int CmdHF15CmdWrite(const char *Cmd) {
995 UsbCommand resp;
996 uint8_t *recv;
997 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
998 uint8_t *req=c.d.asBytes;
999 int reqlen=0, pagenum, temp;
1000 char cmdbuf[100];
1001 char *cmd=cmdbuf;
1002 char *cmd2;
1003
1004 strncpy(cmd,Cmd,99);
1005
1006 // usage:
1007 if (strlen(cmd)<3) {
1008 PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|u|*> <page#> <hexdata>");
1009 PrintAndLog(" options:");
1010 PrintAndLog(" -2 use slower '1 out of 256' mode");
1011 PrintAndLog(" -o set OPTION Flag (needed for TI)");
1012 PrintAndLog(" uid (either): ");
1013 PrintAndLog(" <8B hex> full UID eg E011223344556677");
1014 PrintAndLog(" s selected tag");
1015 PrintAndLog(" u unaddressed mode");
1016 PrintAndLog(" * scan for tag");
1017 PrintAndLog(" page#: page number 0-255");
1018 PrintAndLog(" hexdata: data to be written eg AA BB CC DD");
1019 return 0;
1020 }
1021
1022 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_WRITE},1);
1023 reqlen=c.arg[0];
1024
1025 // *cmd -> page num ; *cmd2 -> data
1026 cmd2=cmd;
1027 while (*cmd2!=' ' && *cmd2!='\t' && *cmd2) cmd2++;
1028 *cmd2=0;
1029 cmd2++;
1030
1031 pagenum=strtol(cmd,NULL,0);
1032 /*if (pagenum<0) {
1033 PrintAndLog("invalid pagenum");
1034 return 0;
1035 } */
1036 req[reqlen++]=(uint8_t)pagenum;
1037
1038
1039 while (cmd2[0] && cmd2[1]) { // hexdata, read by 2 hexchars
1040 if (*cmd2==' ') {
1041 cmd2++;
1042 continue;
1043 }
1044 sscanf((char[]){cmd2[0],cmd2[1],0},"%X",&temp);
1045 req[reqlen++]=temp & 0xff;
1046 cmd2+=2;
1047 }
1048
1049 reqlen=AddCrc(req,reqlen);
1050
1051 c.arg[0]=reqlen;
1052
1053 SendCommand(&c);
1054
1055 if (WaitForResponseTimeout(CMD_ACK,&resp,2000) && resp.arg[0]>2) {
1056 recv = resp.d.asBytes;
1057 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
1058 if (!(recv[0] & ISO15_RES_ERROR)) {
1059 PrintAndLog("OK");
1060 } else {
1061 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
1062 }
1063 } else {
1064 PrintAndLog("CRC failed");
1065 }
1066 } else {
1067 PrintAndLog("timeout: no answer - data may be written anyway");
1068 }
1069
1070 return 0;
1071 }
1072
1073
1074
1075 static command_t CommandTable15Cmd[] =
1076 {
1077 {"help", CmdHF15CmdHelp, 1, "This Help"},
1078 {"inquiry", CmdHF15CmdInquiry, 0, "Search for tags in range"},
1079 /*
1080 {"select", CmdHF15CmdSelect, 0, "Select an tag with a specific UID for further commands"},
1081 */
1082 {"read", CmdHF15CmdRead, 0, "Read a block"},
1083 {"write", CmdHF15CmdWrite, 0, "Write a block"},
1084 {"readmulti",CmdHF15CmdReadmulti, 0, "Reads multiple Blocks"},
1085 {"sysinfo",CmdHF15CmdSysinfo, 0, "Get Card Information"},
1086 {"raw", CmdHF15CmdRaw, 0, "Send raw hex data to tag"},
1087 {"debug", CmdHF15CmdDebug, 0, "Turn debugging on/off"},
1088 {NULL, NULL, 0, NULL}
1089 };
1090
1091 int CmdHF15Cmd(const char *Cmd)
1092 {
1093 CmdsParse(CommandTable15Cmd, Cmd);
1094 return 0;
1095 }
1096
1097 int CmdHF15CmdHelp(const char *Cmd)
1098 {
1099 CmdsHelp(CommandTable15Cmd);
1100 return 0;
1101 }
1102
Impressum, Datenschutz