]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlft55xx.c
Merge branch 'master' of https://github.com/Proxmark/proxmark3
[proxmark3-svn] / client / cmdlft55xx.c
1 //-----------------------------------------------------------------------------
2 //
3 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
4 // at your option, any later version. See the LICENSE.txt file for the text of
5 // the license.
6 //-----------------------------------------------------------------------------
7 // Low frequency T55xx commands
8 //-----------------------------------------------------------------------------
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <inttypes.h>
13 #include "proxmark3.h"
14 #include "ui.h"
15 #include "graph.h"
16 #include "cmdmain.h"
17 #include "cmdparser.h"
18 #include "cmddata.h"
19 #include "cmdlf.h"
20 #include "cmdlft55xx.h"
21 #include "util.h"
22 #include "data.h"
23 #include "lfdemod.h"
24 #include "../common/crc.h"
25
26 #define LF_TRACE_BUFF_SIZE 20000 // 32 x 32 x 10 (32 bit times numofblock (7), times clock skip..)
27 #define LF_BITSSTREAM_LEN 1000 // more then 1000 bits shouldn't happend.. 8block * 4 bytes * 8bits =
28
29 // 0 = FSK
30 // 1 = ASK
31 // 2 = PSK
32 // 4 = NZR (direct)
33 typedef struct {
34 uint8_t modulation;
35 bool inversed;
36 uint32_t block0;
37 } t55xx_conf_block;
38
39 // Default configuration: FSK, not inversed.
40 t55xx_conf_block config = {0x00, FALSE, 0x00};
41
42 // FSK
43 // FSK inverted
44 //FSKrawDemod("", FALSE)
45 //FSKrawDemod("1", FALSE)
46
47 // ASK/MAN
48 // ASK/MAN inverted
49 //ASKmanDemod("", FALSE, FALSE)
50
51 // NZR (autoclock, normal, maxerrors 1)
52 // NZR (autoclock, inverse, maxerrors 1)
53 //NRZrawDemod("0 0 1", FALSE) ) {
54
55 // PSK (autoclock, normal, maxerrors 1)
56 // PSK (autoclock, inverse, maxerrors 1)
57 //PSKDemod("0 0 1", FALSE)
58
59 int usage_t55xx_config(){
60 PrintAndLog("Usage: lf t55xx config [d <demodulation>] [i 0|1]");
61 PrintAndLog("Options: ");
62 PrintAndLog(" h This help");
63 PrintAndLog(" d <> Set demodulation FSK / ASK / PSK / NZR");
64 PrintAndLog(" i [0|1] Inverse data signal, Default: 0");
65 PrintAndLog("Examples:");
66 PrintAndLog(" lf t55xx config d FSK ");
67 PrintAndLog(" FSK demodulation");
68 PrintAndLog(" lf t55xx config d FSK i 1");
69 PrintAndLog(" FSK demodulation, inverse data");
70 PrintAndLog(" lf dump");
71 PrintAndLog(" Dumps all block from tag");
72 PrintAndLog(" lf trace");
73 PrintAndLog(" Read trace block and decode it");
74 PrintAndLog(" lf info");
75 PrintAndLog(" Read configuration and decode it");
76 return 0;
77 }
78 int usage_t55xx_read(){
79 PrintAndLog("Usage: lf t55xx read <block> <password>");
80 PrintAndLog(" <block>, block number to read. Between 0-7");
81 PrintAndLog(" <password>, OPTIONAL password (8 hex characters)");
82 PrintAndLog("");
83 PrintAndLog(" sample: lf t55xx read 0 = try reading data from block 0");
84 PrintAndLog(" : lf t55xx read 0 feedbeef = try reading data from block 0 using password");
85 PrintAndLog("");
86 return 0;
87 }
88 int usage_t55xx_write(){
89 PrintAndLog("Usage: lf t55xx wr <block> <data> [password]");
90 PrintAndLog(" <block>, block number to read. Between 0-7");
91 PrintAndLog(" <data>, 4 bytes of data to write (8 hex characters)");
92 PrintAndLog(" [password], OPTIONAL password 4bytes (8 hex characters)");
93 PrintAndLog("");
94 PrintAndLog(" sample: lf t55xx wd 3 11223344 = try writing data 11223344 to block 3");
95 PrintAndLog(" : lf t55xx wd 3 11223344 feedbeef = try writing data 11223344 to block 3 using password feedbeef");
96 PrintAndLog("");
97 return 0;
98 }
99 int usage_t55xx_trace() {
100 PrintAndLog("Usage: lf t55xx trace [graph buffer data]");
101 PrintAndLog(" [graph buffer data], if set, use Graphbuffer otherwise read data from tag.");
102 PrintAndLog("");
103 PrintAndLog(" sample: lf t55xx trace");
104 PrintAndLog(" : lf t55xx trace 1");
105 PrintAndLog("");
106 return 0;
107 }
108 int usage_t55xx_info() {
109 PrintAndLog("Usage: lf t55xx info [graph buffer data]");
110 PrintAndLog(" [graph buffer data], if set, use Graphbuffer otherwise read data from tag.");
111 PrintAndLog("");
112 PrintAndLog(" sample: lf t55xx info");
113 PrintAndLog(" : lf t55xx info 1");
114 PrintAndLog("");
115 return 0;
116 }
117 int usage_t55xx_dump(){
118 PrintAndLog("Usage: lf t55xx dump <password>");
119 PrintAndLog(" <password>, OPTIONAL password 4bytes (8 hex characters)");
120 PrintAndLog("");
121 PrintAndLog(" sample: lf t55xx dump");
122 PrintAndLog(" : lf t55xx dump feedbeef");
123 PrintAndLog("");
124 return 0;
125 }
126
127 static int CmdHelp(const char *Cmd);
128
129 int CmdT55xxSetConfig(const char *Cmd){
130
131 uint8_t paramNum =0;
132 if(param_getchar(Cmd, paramNum) == 'h')
133 {
134 return usage_t55xx_config();
135 }
136
137 uint8_t buff[] = { 0x01, 0x01, 0x01, 0x01,
138 0x01, 0x01, 0x01, 0x01,
139 0x01, 0x40, 0x01, 0x01, 0x04 };
140 PrintAndLog("CRC-8: %x",CRC8Maxim(buff, 13));
141
142 //config = { 0, FALSE};
143 return 0;
144 }
145
146 // detect configuration?
147
148 int CmdReadBlk(const char *Cmd)
149 {
150 int block = -1;
151 int password = 0xFFFFFFFF; //default to blank Block 7
152
153 char cmdp = param_getchar(Cmd, 0);
154 if (cmdp == 'h' || cmdp == 'H')
155 return usage_t55xx_read();
156
157 int res = sscanf(Cmd, "%d %x", &block, &password);
158
159 if ( res < 1 || res > 2 ){
160 usage_t55xx_read();
161 return 1;
162 }
163
164 if ((block < 0) | (block > 7)) {
165 PrintAndLog("Block must be between 0 and 7");
166 return 1;
167 }
168
169 UsbCommand c = {CMD_T55XX_READ_BLOCK, {0, block, 0}};
170 c.d.asBytes[0] = 0x0;
171
172 //Password mode
173 if ( res == 2 ) {
174 c.arg[2] = password;
175 c.d.asBytes[0] = 0x1;
176 }
177
178 SendCommand(&c);
179 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {
180 PrintAndLog("command execution time out");
181 return 2;
182 }
183
184 uint8_t got[12000];
185 GetFromBigBuf(got,sizeof(got),0);
186 WaitForResponse(CMD_ACK,NULL);
187
188 setGraphBuf(got, 12000);
189
190 if (block == 0){
191 // try a detection.
192
193 }
194
195 if (CmdDetectClockRate("f")){ //wave is almost certainly FSK
196 //call FSK DEMOD
197 // FSK
198 if ( FSKrawDemod("", FALSE))
199 printT55xx("FSK");
200 // FSK inverted
201 if ( FSKrawDemod("1", FALSE))
202 printT55xx("FSK inv");
203 } else {
204 // ASK/MAN (autoclock, normal, maxerrors 1)
205 if ( ASKmanDemod("0 0 1", FALSE, FALSE) )
206 printT55xx("ASK/MAN");
207
208 // ASK/MAN (autoclock, inverted, maxerrors 1)
209 if ( ASKmanDemod("0 1 1", FALSE, FALSE) )
210 printT55xx("ASK/MAN Inv");
211
212 // NZR (autoclock, normal, maxerrors 1)
213 if ( NRZrawDemod("0 0 1", FALSE) )
214 printT55xx("NZR");
215 // NZR (autoclock, inverted, maxerrors 1)
216 if ( NRZrawDemod("0 1 1", FALSE) )
217 printT55xx("NZR inv");
218
219 // PSK (autoclock, normal, maxerrors 1)
220 if (!PSKDemod("0 0 1", FALSE))
221 printT55xx("PSK");
222
223 // PSK (autoclock, inverted, maxerrors 1)
224 if (!PSKDemod("0 1 1", FALSE))
225 printT55xx("PSK inv");
226 }
227 return 0;
228 }
229
230 void printT55xx(const char *demodStr){
231
232 uint32_t blockData = 0;
233 uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00};
234
235 if ( !DemodBufferLen)
236 return;
237
238 int i =0;
239 for (;i<DemodBufferLen;++i)
240 bits[i]=DemodBuffer[i];
241
242 blockData = PackBits(1, 32, bits);
243 PrintAndLog("0x%08X %s [%s]", blockData, sprint_bin(bits+1,32), demodStr );
244 }
245
246 /*
247 FSK1 / FSK1a
248 size = fskdemod(dest, size, 32, 0, 8, 10); // fsk1 RF/32
249 size = fskdemod(dest, size, 32, 1, 8, 10); // fsk1a RF/32
250
251 FSK2 / FSK2a
252 size = fskdemod(dest, size, 32, 0, 10, 8); // fsk2 RF/32
253 size = fskdemod(dest, size, 32, 1, 10, 8); // fsk2a RF/32
254 size = fskdemod(dest, size, 50, 1, 10, 8); // fsk2a RF/50
255 size = fskdemod(dest, size, 64, 1, 10, 8); // FSK2a RF/64
256
257 PSK1
258 errCnt = pskRawDemod(bits, &bitlen, 32, 0);
259 */
260 int CmdWriteBlk(const char *Cmd)
261 {
262 int block = 8; //default to invalid block
263 int data = 0xFFFFFFFF; //default to blank Block
264 int password = 0xFFFFFFFF; //default to blank Block 7
265
266 char cmdp = param_getchar(Cmd, 0);
267 if (cmdp == 'h' || cmdp == 'H') {
268 usage_t55xx_write();
269 return 0;
270 }
271
272 int res = sscanf(Cmd, "%d %x %x",&block, &data, &password);
273
274 if ( res < 2 || res > 3) {
275 usage_t55xx_write();
276 return 1;
277 }
278
279 if (block > 7) {
280 PrintAndLog("Block must be between 0 and 7");
281 return 1;
282 }
283
284 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {data, block, 0}};
285 c.d.asBytes[0] = 0x0;
286
287 PrintAndLog("Writing to T55x7");
288 PrintAndLog("block : %d", block);
289 PrintAndLog("data : 0x%08X", data);
290
291 //Password mode
292 if (res == 3) {
293 c.arg[2] = password;
294 c.d.asBytes[0] = 0x1;
295 PrintAndLog("pwd : 0x%08X", password);
296 }
297 SendCommand(&c);
298 return 0;
299 }
300
301 int CmdReadTrace(const char *Cmd)
302 {
303 uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00};
304
305 char cmdp = param_getchar(Cmd, 0);
306
307 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {
308 usage_t55xx_trace();
309 return 0;
310 }
311
312 if ( strlen(Cmd)==0){
313
314 UsbCommand c = {CMD_T55XX_READ_TRACE, {0, 0, 0}};
315 SendCommand(&c);
316 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {
317 PrintAndLog("command execution time out");
318 return 1;
319 }
320 //darn
321 //CmdSamples("12000");
322 }
323
324 size_t bitlen = getFromGraphBuf(bits);
325 if ( bitlen == 0 )
326 return 2;
327
328 RepaintGraphWindow();
329
330 uint8_t si = 5;
331 uint32_t bl0 = PackBits(si, 32, bits);
332 uint32_t bl1 = PackBits(si+32, 32, bits);
333
334 uint32_t acl = PackBits(si, 8, bits); si += 8;
335 uint32_t mfc = PackBits(si, 8, bits); si += 8;
336 uint32_t cid = PackBits(si, 5, bits); si += 5;
337 uint32_t icr = PackBits(si, 3, bits); si += 3;
338 uint32_t year = PackBits(si, 4, bits); si += 4;
339 uint32_t quarter = PackBits(si, 2, bits); si += 2;
340 uint32_t lotid = PackBits(si, 12, bits); si += 12;
341 uint32_t wafer = PackBits(si, 5, bits); si += 5;
342 uint32_t dw = PackBits(si, 15, bits);
343
344 PrintAndLog("");
345 PrintAndLog("-- T55xx Trace Information ----------------------------------");
346 PrintAndLog("-------------------------------------------------------------");
347 PrintAndLog(" ACL Allocation class (ISO/IEC 15963-1) : 0x%02X (%d)", acl, acl);
348 PrintAndLog(" MFC Manufacturer ID (ISO/IEC 7816-6) : 0x%02X (%d)", mfc, mfc);
349 PrintAndLog(" CID : 0x%02X (%d)", cid, cid);
350 PrintAndLog(" ICR IC Revision : %d",icr );
351 PrintAndLog(" Manufactured");
352 PrintAndLog(" Year/Quarter : %d/%d",2000+year, quarter );
353 PrintAndLog(" Lot ID : %d", lotid );
354 PrintAndLog(" Wafer number : %d", wafer);
355 PrintAndLog(" Die Number : %d", dw);
356 PrintAndLog("-------------------------------------------------------------");
357 PrintAndLog(" Raw Data - Page 1");
358 PrintAndLog(" Block 0 : 0x%08X %s", bl0, sprint_bin(bits+5,32) );
359 PrintAndLog(" Block 0 : 0x%08X %s", bl1, sprint_bin(bits+37,32) );
360 PrintAndLog("-------------------------------------------------------------");
361 /*
362 TRACE - BLOCK O
363 Bits Definition HEX
364 1-8 ACL Allocation class (ISO/IEC 15963-1) 0xE0
365 9-16 MFC Manufacturer ID (ISO/IEC 7816-6) 0x15 Atmel Corporation
366 17-21 CID 0x1 = Atmel ATA5577M1 0x2 = Atmel ATA5577M2
367 22-24 ICR IC revision
368 25-28 YEAR (BCD encoded) 9 (= 2009)
369 29-30 QUARTER 1,2,3,4
370 31-32 LOT ID
371
372 TRACE - BLOCK 1
373 1-12 LOT ID
374 13-17 Wafer number
375 18-32 DW, die number sequential
376 */
377
378 return 0;
379 }
380
381 int CmdInfo(const char *Cmd){
382 /*
383 Page 0 Block 0 Configuration data.
384 Normal mode
385 Extended mode
386 */
387 char cmdp = param_getchar(Cmd, 0);
388
389 if (cmdp == 'h' || cmdp == 'H') {
390 return usage_t55xx_info();
391 } else {
392 CmdReadBlk("0");
393 }
394
395 // config
396
397 uint8_t bits[LF_BITSSTREAM_LEN] = {0x00};
398
399 uint8_t si = 1;
400 uint32_t bl0 = PackBits(si, 32, bits);
401
402 uint32_t safer = PackBits(si, 4, bits); si += 4;
403 uint32_t resv = PackBits(si, 7, bits); si += 7;
404 uint32_t dbr = PackBits(si, 3, bits); si += 3;
405 uint32_t extend = PackBits(si, 1, bits); si += 1;
406 uint32_t datamodulation = PackBits(si, 5, bits); si += 5;
407 uint32_t pskcf = PackBits(si, 2, bits); si += 2;
408 uint32_t aor = PackBits(si, 1, bits); si += 1;
409 uint32_t otp = PackBits(si, 1, bits); si += 1;
410 uint32_t maxblk = PackBits(si, 3, bits); si += 3;
411 uint32_t pwd = PackBits(si, 1, bits); si += 1;
412 uint32_t sst = PackBits(si, 1, bits); si += 1;
413 uint32_t fw = PackBits(si, 1, bits); si += 1;
414 uint32_t inv = PackBits(si, 1, bits); si += 1;
415 uint32_t por = PackBits(si, 1, bits); si += 1;
416
417 PrintAndLog("");
418 PrintAndLog("-- T55xx Configuration & Tag Information --------------------");
419 PrintAndLog("-------------------------------------------------------------");
420 PrintAndLog(" Safer key : %s", GetSaferStr(safer));
421 PrintAndLog(" reserved : %d", resv);
422 PrintAndLog(" Data bit rate : %s", GetBitRateStr(dbr));
423 PrintAndLog(" eXtended mode : %s", (extend) ? "Yes - Warning":"No");
424 PrintAndLog(" Modulation : %s", GetModulationStr(datamodulation) );
425 PrintAndLog(" PSK clock freq : %d", pskcf);
426 PrintAndLog(" AOR - Answer on Request : %s", (aor) ? "Yes":"No");
427 PrintAndLog(" OTP - One Time Pad : %s", (otp) ? "Yes - Warning":"No" );
428 PrintAndLog(" Max block : %d", maxblk);
429 PrintAndLog(" Password mode : %s", (pwd) ? "Yes":"No");
430 PrintAndLog(" Sequence Start Terminator : %s", (sst) ? "Yes":"No");
431 PrintAndLog(" Fast Write : %s", (fw) ? "Yes":"No");
432 PrintAndLog(" Inverse data : %s", (inv) ? "Yes":"No");
433 PrintAndLog(" POR-Delay : %s", (por) ? "Yes":"No");
434 PrintAndLog("-------------------------------------------------------------");
435 PrintAndLog(" Raw Data - Page 0");
436 PrintAndLog(" Block 0 : 0x%08X %s", bl0, sprint_bin(bits+5,32) );
437 PrintAndLog("-------------------------------------------------------------");
438
439 return 0;
440 }
441
442 int CmdDump(const char *Cmd){
443
444 char s[20] = {0x00};
445 uint8_t pwd[4] = {0x00};
446
447 char cmdp = param_getchar(Cmd, 0);
448 if ( cmdp == 'h' || cmdp == 'H') {
449 usage_t55xx_dump();
450 return 0;
451 }
452
453 bool hasPwd = ( strlen(Cmd) > 0);
454 if ( hasPwd ){
455 if (param_gethex(Cmd, 0, pwd, 8)) {
456 PrintAndLog("password must include 8 HEX symbols");
457 return 1;
458 }
459 }
460
461 for ( int i = 0; i <8; ++i){
462 memset(s,0,sizeof(s));
463 if ( hasPwd ) {
464 sprintf(s,"%d %02x%02x%02x%02x", i, pwd[0],pwd[1],pwd[2],pwd[3]);
465 } else {
466 sprintf(s,"%d", i);
467 }
468 CmdReadBlk(s);
469 }
470 return 0;
471 }
472
473 char * GetBitRateStr(uint32_t id){
474 static char buf[40];
475 char *retStr = buf;
476 switch (id){
477 case 0:
478 sprintf(retStr,"%d - RF/8",id);
479 break;
480 case 1:
481 sprintf(retStr,"%d - RF/16",id);
482 break;
483 case 2:
484 sprintf(retStr,"%d - RF/32",id);
485 break;
486 case 3:
487 sprintf(retStr,"%d - RF/40",id);
488 break;
489 case 4:
490 sprintf(retStr,"%d - RF/50",id);
491 break;
492 case 5:
493 sprintf(retStr,"%d - RF/64",id);
494 break;
495 case 6:
496 sprintf(retStr,"%d - RF/100",id);
497 break;
498 case 7:
499 sprintf(retStr,"%d - RF/128",id);
500 break;
501 default:
502 sprintf(retStr,"%d - (Unknown)",id);
503 break;
504 }
505
506 return buf;
507 }
508
509 char * GetSaferStr(uint32_t id){
510 static char buf[40];
511 char *retStr = buf;
512
513 sprintf(retStr,"%d",id);
514 if (id == 6) {
515 sprintf(retStr,"%d - pasdwd",id);
516 }
517 if (id == 9 ){
518 sprintf(retStr,"%d - testmode ",id);
519 }
520
521 return buf;
522 }
523 char * GetModulationStr( uint32_t id){
524 static char buf[40];
525 char *retStr = buf;
526
527 switch (id){
528 case 0:
529 sprintf(retStr,"%d - DIRECT (ASK/NRZ)",id);
530 break;
531 case 1:
532 sprintf(retStr,"%d - PSK 1 phase change when input changes",id);
533 break;
534 case 2:
535 sprintf(retStr,"%d - PSK 2 phase change on bitclk if input high",id);
536 break;
537 case 3:
538 sprintf(retStr,"%d - PSK 3 phase change on rising edge of input",id);
539 break;
540 case 4:
541 sprintf(retStr,"%d - FSK 1 RF/8 RF/5",id);
542 break;
543 case 5:
544 sprintf(retStr,"%d - FSK 2 RF/8 RF/10",id);
545 break;
546 case 6:
547 sprintf(retStr,"%d - FSK 1a RF/5 RF/8",id);
548 break;
549 case 7:
550 sprintf(retStr,"%d - FSK 2a RF/10 RF/8",id);
551 break;
552 case 8:
553 sprintf(retStr,"%d - Manschester",id);
554 break;
555 case 16:
556 sprintf(retStr,"%d - Biphase",id);
557 break;
558 case 17:
559 sprintf(retStr,"%d - Reserved",id);
560 break;
561 default:
562 sprintf(retStr,"0x%02X (Unknown)",id);
563 break;
564 }
565 return buf;
566 }
567
568 uint32_t PackBits(uint8_t start, uint8_t len, uint8_t* bits){
569
570 int i = start;
571 int j = len-1;
572 if (len > 32) {
573 return 0;
574 }
575 uint32_t tmp = 0;
576 for (; j >= 0; --j, ++i){
577 tmp |= bits[i] << j;
578 }
579 return tmp;
580 }
581
582 static command_t CommandTable[] =
583 {
584 {"help", CmdHelp, 1, "This help"},
585 {"config", CmdT55xxSetConfig, 1, "Set T55XX config for modulation, inversed data"},
586 {"read", CmdReadBlk, 0, "<block> [password] -- Read T55xx block data (page 0) [optional password]"},
587 {"write", CmdWriteBlk, 0, "<block> <data> [password] -- Write T55xx block data (page 0) [optional password]"},
588 {"trace", CmdReadTrace, 0, "[1] Read T55xx traceability data (page 1/ blk 0-1)"},
589 {"info", CmdInfo, 0, "[1] Read T55xx configuration data (page 0/ blk 0)"},
590 {"dump", CmdDump, 0, "[password] Dump T55xx card block 0-7. [optional password]"},
591 {NULL, NULL, 0, NULL}
592 };
593
594 int CmdLFT55XX(const char *Cmd)
595 {
596 CmdsParse(CommandTable, Cmd);
597 return 0;
598 }
599
600 int CmdHelp(const char *Cmd)
601 {
602 CmdsHelp(CommandTable);
603 return 0;
604 }
Impressum, Datenschutz