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