]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlft55xx.c
25df4c7617bfefc639558b20f13eb2f323cbbe9c
[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 "cmdlft55xx.h"
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <inttypes.h>
15 #include <ctype.h>
16 #include <time.h>
17 #include "comms.h"
18 #include "ui.h"
19 #include "graph.h"
20 #include "cmdmain.h"
21 #include "cmdparser.h"
22 #include "cmddata.h"
23 #include "cmdlf.h"
24 #include "util.h"
25 #include "lfdemod.h"
26 #include "protocols.h"
27 #include "taginfo.h"
28
29 #define T55x7_CONFIGURATION_BLOCK 0x00
30 #define T55x7_PAGE0 0x00
31 #define T55x7_PAGE1 0x01
32 //#define T55x7_PWD 0x00000010
33 #define REGULAR_READ_MODE_BLOCK 0xFF
34
35 // Default configuration
36 t55xx_conf_block_t config = { .modulation = DEMOD_ASK, .inverted = false, .offset = 0x00, .block0 = 0x00, .Q5 = false };
37
38 t55xx_conf_block_t Get_t55xx_Config(){
39 return config;
40 }
41 void Set_t55xx_Config(t55xx_conf_block_t conf){
42 config = conf;
43 }
44
45 int usage_t55xx_config(){
46 PrintAndLog("Usage: lf t55xx config [d <demodulation>] [i 1] [o <offset>] [Q5]");
47 PrintAndLog("Options:");
48 PrintAndLog(" h This help");
49 PrintAndLog(" b <8|16|32|40|50|64|100|128> Set bitrate");
50 PrintAndLog(" d <FSK|FSK1|FSK1a|FSK2|FSK2a|ASK|PSK1|PSK2|NRZ|BI|BIa> Set demodulation FSK / ASK / PSK / NRZ / Biphase / Biphase A");
51 PrintAndLog(" i [1] Invert data signal, defaults to normal");
52 PrintAndLog(" o [offset] Set offset, where data should start decode in bitstream");
53 PrintAndLog(" Q5 Set as Q5(T5555) chip instead of T55x7");
54 PrintAndLog(" ST Set Sequence Terminator on");
55 PrintAndLog("");
56 PrintAndLog("Examples:");
57 PrintAndLog(" lf t55xx config d FSK - FSK demodulation");
58 PrintAndLog(" lf t55xx config d FSK i 1 - FSK demodulation, inverse data");
59 PrintAndLog(" lf t55xx config d FSK i 1 o 3 - FSK demodulation, inverse data, offset=3,start from position 3 to decode data");
60 PrintAndLog("");
61 return 0;
62 }
63 int usage_t55xx_read(){
64 PrintAndLog("Usage: lf t55xx read [b <block>] [p <password>] <override_safety> <page1>");
65 PrintAndLog("Options:");
66 PrintAndLog(" b <block> - block number to read. Between 0-7");
67 PrintAndLog(" p <password> - OPTIONAL password (8 hex characters)");
68 PrintAndLog(" o - OPTIONAL override safety check");
69 PrintAndLog(" 1 - OPTIONAL read Page 1 instead of Page 0");
70 PrintAndLog(" e <mode> - OPTIONAL downlink encoding '0' fixed-bit-length (default), '1' Long Zero Reference");
71 PrintAndLog(" '2' Leading Zero, '3' 1 of 4 ");
72 PrintAndLog(" ****WARNING****");
73 PrintAndLog(" Use of read with password on a tag not configured for a pwd");
74 PrintAndLog(" can damage the tag");
75 PrintAndLog("");
76 PrintAndLog("Examples:");
77 PrintAndLog(" lf t55xx read b 0 - read data from block 0");
78 PrintAndLog(" lf t55xx read b 0 p feedbeef - read data from block 0 password feedbeef");
79 PrintAndLog(" lf t55xx read b 0 p feedbeef o - read data from block 0 password feedbeef safety check");
80 PrintAndLog("");
81 return 0;
82 }
83 int usage_t55xx_write(){
84 PrintAndLog("Usage: lf t55xx write [b <block>] [d <data>] [p <password>] [1] [t]");
85 PrintAndLog("Options:");
86 PrintAndLog(" b <block> - block number to write. Between 0-7");
87 PrintAndLog(" d <data> - 4 bytes of data to write (8 hex characters)");
88 PrintAndLog(" p <password> - OPTIONAL password 4bytes (8 hex characters)");
89 PrintAndLog(" 1 - OPTIONAL write Page 1 instead of Page 0");
90 PrintAndLog(" t - OPTIONAL test mode write - ****DANGER****");
91 PrintAndLog(" e <mode> - OPTIONAL downlink encoding '0' fixed-bit-length (default), '1' Long Zero Reference");
92 PrintAndLog(" '2' Leading Zero, '3' 1 of 4 ");
93 PrintAndLog("");
94 PrintAndLog("Examples:");
95 PrintAndLog(" lf t55xx write b 3 d 11223344 - write 11223344 to block 3");
96 PrintAndLog(" lf t55xx write b 3 d 11223344 p feedbeef - write 11223344 to block 3 password feedbeef");
97 PrintAndLog("");
98 return 0;
99 }
100 int usage_t55xx_trace() {
101 PrintAndLog("Usage: lf t55xx trace [1]");
102 PrintAndLog("Options:");
103 PrintAndLog(" [graph buffer data] - if set, use Graphbuffer otherwise read data from tag.");
104 PrintAndLog("");
105 PrintAndLog("Examples:");
106 PrintAndLog(" lf t55xx trace");
107 PrintAndLog(" lf t55xx trace 1");
108 PrintAndLog("");
109 return 0;
110 }
111 int usage_t55xx_info() {
112 PrintAndLog("Usage: lf t55xx info [1]");
113 PrintAndLog("Options:");
114 PrintAndLog(" [graph buffer data] - if set, use Graphbuffer otherwise read data from tag.");
115 PrintAndLog("");
116 PrintAndLog("Examples:");
117 PrintAndLog(" lf t55xx info");
118 PrintAndLog(" lf t55xx info 1");
119 PrintAndLog("");
120 return 0;
121 }
122 int usage_t55xx_dump(){
123 PrintAndLog("Usage: lf t55xx dump <password> [o]");
124 PrintAndLog("Options:");
125 PrintAndLog(" <password> - OPTIONAL password 4bytes (8 hex symbols)");
126 PrintAndLog(" o - OPTIONAL override, force pwd read despite danger to card");
127 PrintAndLog("");
128 PrintAndLog("Examples:");
129 PrintAndLog(" lf t55xx dump");
130 PrintAndLog(" lf t55xx dump feedbeef o");
131 PrintAndLog("");
132 return 0;
133 }
134 int usage_t55xx_detect(){
135 PrintAndLog("Usage: lf t55xx detect [1] [p <password>]");
136 PrintAndLog("Options:");
137 PrintAndLog(" 1 - if set, use Graphbuffer otherwise read data from tag.");
138 PrintAndLog(" p <password> - OPTIONAL password (8 hex characters)");
139 PrintAndLog(" e <mode> - OPTIONAL downlink encoding '0' fixed-bit-length (default), '1' Long Zero Reference");
140 PrintAndLog(" '2' Leading Zero, '3' 1 of 4 ");
141 PrintAndLog("");
142 PrintAndLog("Examples:");
143 PrintAndLog(" lf t55xx detect");
144 PrintAndLog(" lf t55xx detect 1");
145 PrintAndLog(" lf t55xx detect p 11223344");
146 PrintAndLog("");
147 return 0;
148 }
149 int usage_t55xx_detectP1(){
150 PrintAndLog("Command: Detect Page 1 of a t55xx chip");
151 PrintAndLog("Usage: lf t55xx p1detect [1] [p <password>]");
152 PrintAndLog("Options:");
153 PrintAndLog(" 1 - if set, use Graphbuffer otherwise read data from tag.");
154 PrintAndLog(" p <password> - OPTIONAL password (8 hex characters)");
155 PrintAndLog("");
156 PrintAndLog("Examples:");
157 PrintAndLog(" lf t55xx p1detect");
158 PrintAndLog(" lf t55xx p1detect 1");
159 PrintAndLog(" lf t55xx p1detect p 11223344");
160 PrintAndLog("");
161 return 0;
162 }
163 int usage_t55xx_wakup(){
164 PrintAndLog("Usage: lf t55xx wakeup [h] <password>");
165 PrintAndLog("This commands send the Answer-On-Request command and leaves the readerfield ON afterwards.");
166 PrintAndLog("Options:");
167 PrintAndLog(" h - this help");
168 PrintAndLog(" <password> - [required] password 4bytes (8 hex symbols)");
169 PrintAndLog("");
170 PrintAndLog("Examples:");
171 PrintAndLog(" lf t55xx wakeup 11223344 - send wakeup password");
172 return 0;
173 }
174 int usage_t55xx_bruteforce(){
175 PrintAndLog("This command uses A) bruteforce to scan a number range");
176 PrintAndLog(" B) a dictionary attack");
177 PrintAndLog("Usage: lf t55xx bruteforce <start password> <end password> [i <*.dic>]");
178 PrintAndLog(" password must be 4 bytes (8 hex symbols)");
179 PrintAndLog("Options:");
180 PrintAndLog(" h - this help");
181 PrintAndLog(" <start_pwd> - 4 byte hex value to start pwd search at");
182 PrintAndLog(" <end_pwd> - 4 byte hex value to end pwd search at");
183 PrintAndLog(" i <*.dic> - loads a default keys dictionary file <*.dic>");
184 PrintAndLog("");
185 PrintAndLog("Examples:");
186 PrintAndLog(" lf t55xx bruteforce aaaaaaaa bbbbbbbb");
187 PrintAndLog(" lf t55xx bruteforce i default_pwd.dic");
188 PrintAndLog("");
189 return 0;
190 }
191 int usage_t55xx_bruteforce_downlink(){
192 PrintAndLog("This command uses A) bruteforce to scan a number range");
193 PrintAndLog(" B) a dictionary attack");
194 PrintAndLog("Usage: lf t55xx bruteforce <start password> <end password> [i <*.dic>]");
195 PrintAndLog(" password must be 4 bytes (8 hex symbols)");
196 PrintAndLog("Options:");
197 PrintAndLog(" h - this help");
198 PrintAndLog(" r <start_pwd> <end_pwd> - 4 byte hex value to start and end pwd search at");
199 PrintAndLog(" i <*.dic> - loads a default keys dictionary file <*.dic>");
200 PrintAndLog(" e <mode> - OPTIONAL downlink encoding '0' fixed-bit-length (default), '1' Long Zero Reference");
201 PrintAndLog(" '2' Leading Zero, '3' 1 of 4 ");
202 PrintAndLog("");
203 PrintAndLog("Examples:");
204 PrintAndLog(" lf t55xx bruteforce aaaaaaaa bbbbbbbb");
205 PrintAndLog(" lf t55xx bruteforce i default_pwd.dic");
206 PrintAndLog("");
207 return 0;
208 }
209 int usage_t55xx_wipe(){
210 PrintAndLog("Usage: lf t55xx wipe [h] [Q5]");
211 PrintAndLog("This commands wipes a tag, fills blocks 1-7 with zeros and a default configuration block");
212 PrintAndLog("Options:");
213 PrintAndLog(" h - this help");
214 PrintAndLog(" Q5 - indicates to use the T5555 (Q5) default configuration block");
215 PrintAndLog("");
216 PrintAndLog("Examples:");
217 PrintAndLog(" lf t55xx wipe - wipes a t55x7 tag, config block 0x000880E0");
218 PrintAndLog(" lf t55xx wipe Q5 - wipes a t5555 Q5 tag, config block 0x6001F004");
219 return 0;
220 }
221
222
223 static int CmdHelp(const char *Cmd);
224
225 void printT5xxHeader(uint8_t page){
226 PrintAndLog("Reading Page %d:", page);
227 PrintAndLog("blk | hex data | binary");
228 PrintAndLog("----+----------+---------------------------------");
229 }
230
231 int CmdT55xxSetConfig(const char *Cmd) {
232
233 uint8_t offset = 0;
234 char modulation[6] = {0x00};
235 char tmp = 0x00;
236 uint8_t bitRate = 0;
237 uint8_t rates[9] = {8,16,32,40,50,64,100,128,0};
238 uint8_t cmdp = 0;
239 bool errors = false;
240 while(param_getchar(Cmd, cmdp) != 0x00 && !errors)
241 {
242 tmp = param_getchar(Cmd, cmdp);
243 switch(tmp)
244 {
245 case 'h':
246 case 'H':
247 return usage_t55xx_config();
248 case 'b':
249 errors |= param_getdec(Cmd, cmdp+1, &bitRate);
250 if ( !errors){
251 uint8_t i = 0;
252 for (; i < 9; i++){
253 if (rates[i]==bitRate) {
254 config.bitrate = i;
255 break;
256 }
257 }
258 if (i==9) errors = true;
259 }
260 cmdp+=2;
261 break;
262 case 'd':
263 param_getstr(Cmd, cmdp+1, modulation, sizeof(modulation));
264 cmdp += 2;
265
266 if ( strcmp(modulation, "FSK" ) == 0) {
267 config.modulation = DEMOD_FSK;
268 } else if ( strcmp(modulation, "FSK1" ) == 0) {
269 config.modulation = DEMOD_FSK1;
270 config.inverted=1;
271 } else if ( strcmp(modulation, "FSK1a" ) == 0) {
272 config.modulation = DEMOD_FSK1a;
273 config.inverted=0;
274 } else if ( strcmp(modulation, "FSK2" ) == 0) {
275 config.modulation = DEMOD_FSK2;
276 config.inverted=0;
277 } else if ( strcmp(modulation, "FSK2a" ) == 0) {
278 config.modulation = DEMOD_FSK2a;
279 config.inverted=1;
280 } else if ( strcmp(modulation, "ASK" ) == 0) {
281 config.modulation = DEMOD_ASK;
282 } else if ( strcmp(modulation, "NRZ" ) == 0) {
283 config.modulation = DEMOD_NRZ;
284 } else if ( strcmp(modulation, "PSK1" ) == 0) {
285 config.modulation = DEMOD_PSK1;
286 } else if ( strcmp(modulation, "PSK2" ) == 0) {
287 config.modulation = DEMOD_PSK2;
288 } else if ( strcmp(modulation, "PSK3" ) == 0) {
289 config.modulation = DEMOD_PSK3;
290 } else if ( strcmp(modulation, "BIa" ) == 0) {
291 config.modulation = DEMOD_BIa;
292 config.inverted=1;
293 } else if ( strcmp(modulation, "BI" ) == 0) {
294 config.modulation = DEMOD_BI;
295 config.inverted=0;
296 } else {
297 PrintAndLog("Unknown modulation '%s'", modulation);
298 errors = true;
299 }
300 break;
301 case 'i':
302 config.inverted = param_getchar(Cmd,cmdp+1) == '1';
303 cmdp+=2;
304 break;
305 case 'o':
306 errors |= param_getdec(Cmd, cmdp+1, &offset);
307 if ( !errors )
308 config.offset = offset;
309 cmdp+=2;
310 break;
311 case 'Q':
312 case 'q':
313 config.Q5 = true;
314 cmdp++;
315 break;
316 case 'S':
317 case 's':
318 config.ST = true;
319 cmdp++;
320 break;
321 default:
322 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
323 errors = true;
324 break;
325 }
326 }
327
328 // No args
329 if (cmdp == 0) return printConfiguration( config );
330
331 //Validations
332 if (errors) return usage_t55xx_config();
333
334 config.block0 = 0;
335 return printConfiguration ( config );
336 }
337
338 int T55xxReadBlock(uint8_t block, bool page1, bool usepwd, bool override, uint32_t password, uint8_t downlink_mode){
339 //Password mode
340 if ( usepwd ) {
341 // try reading the config block and verify that PWD bit is set before doing this!
342 if ( !override ) {
343 if ( !AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, false, 0,downlink_mode ) ) return 0;
344 if ( !tryDetectModulation() ) {
345 PrintAndLog("Safety Check: Could not detect if PWD bit is set in config block. Exits.");
346 return 0;
347 } else {
348 PrintAndLog("Safety Check: PWD bit is NOT set in config block. Reading without password...");
349 usepwd = false;
350 page1 = false;
351 }
352 } else {
353 PrintAndLog("Safety Check Overriden - proceeding despite risk");
354 }
355 }
356
357 if (!AquireData(page1, block, usepwd, password,downlink_mode) ) return 0;
358 if (!DecodeT55xxBlock()) return 0;
359
360 char blk[10]={0};
361 sprintf(blk,"%d", block);
362 printT55xxBlock(blk);
363 return 1;
364 }
365
366 int CmdT55xxReadBlock(const char *Cmd) {
367 uint8_t block = REGULAR_READ_MODE_BLOCK;
368 uint32_t password = 0; //default to blank Block 7
369 uint8_t downlink_mode = 0;
370
371 bool usepwd = false;
372 bool override = false;
373 bool page1 = false;
374 bool errors = false;
375 uint8_t cmdp = 0;
376 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {
377 switch(param_getchar(Cmd, cmdp)) {
378 case 'h':
379 case 'H':
380 return usage_t55xx_read();
381 case 'b':
382 case 'B':
383 errors |= param_getdec(Cmd, cmdp+1, &block);
384 cmdp += 2;
385 break;
386 case 'o':
387 case 'O':
388 override = true;
389 cmdp++;
390 break;
391 case 'p':
392 case 'P':
393 password = param_get32ex(Cmd, cmdp+1, 0, 16);
394 usepwd = true;
395 cmdp += 2;
396 break;
397 case '1':
398 page1 = true;
399 cmdp++;
400 break;
401 case 'e':
402 case 'E':
403 downlink_mode = param_getchar(Cmd, cmdp+1) - '0';
404 if (downlink_mode > 3) downlink_mode = 0;
405 cmdp +=2;
406 break;
407 default:
408 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
409 errors = true;
410 break;
411 }
412 }
413 if (errors) return usage_t55xx_read();
414
415 if (block > 7 && block != REGULAR_READ_MODE_BLOCK ) {
416 PrintAndLog("Block must be between 0 and 7");
417 return 0;
418 }
419
420 printT5xxHeader(page1);
421 return T55xxReadBlock(block, page1, usepwd, override, password, downlink_mode);
422 }
423
424 bool DecodeT55xxBlock(){
425
426 char buf[30] = {0x00};
427 char *cmdStr = buf;
428 int ans = 0;
429 bool ST = config.ST;
430 uint8_t bitRate[8] = {8,16,32,40,50,64,100,128};
431 DemodBufferLen = 0x00;
432
433 switch( config.modulation ){
434 case DEMOD_FSK:
435 snprintf(cmdStr, sizeof(buf),"%d %d", bitRate[config.bitrate], config.inverted );
436 ans = FSKrawDemod(cmdStr, false);
437 break;
438 case DEMOD_FSK1:
439 case DEMOD_FSK1a:
440 snprintf(cmdStr, sizeof(buf),"%d %d 8 5", bitRate[config.bitrate], config.inverted );
441 ans = FSKrawDemod(cmdStr, false);
442 break;
443 case DEMOD_FSK2:
444 case DEMOD_FSK2a:
445 snprintf(cmdStr, sizeof(buf),"%d %d 10 8", bitRate[config.bitrate], config.inverted );
446 ans = FSKrawDemod(cmdStr, false);
447 break;
448 case DEMOD_ASK:
449 snprintf(cmdStr, sizeof(buf),"%d %d 1", bitRate[config.bitrate], config.inverted );
450 ans = ASKDemod_ext(cmdStr, false, false, 1, &ST);
451 break;
452 case DEMOD_PSK1:
453 // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)
454 save_restoreGB(GRAPH_SAVE);
455 CmdLtrim("160");
456 snprintf(cmdStr, sizeof(buf),"%d %d 6", bitRate[config.bitrate], config.inverted );
457 ans = PSKDemod(cmdStr, false);
458 //undo trim samples
459 save_restoreGB(GRAPH_RESTORE);
460 break;
461 case DEMOD_PSK2: //inverted won't affect this
462 case DEMOD_PSK3: //not fully implemented
463 // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)
464 save_restoreGB(GRAPH_SAVE);
465 CmdLtrim("160");
466 snprintf(cmdStr, sizeof(buf),"%d 0 6", bitRate[config.bitrate] );
467 ans = PSKDemod(cmdStr, false);
468 psk1TOpsk2(DemodBuffer, DemodBufferLen);
469 //undo trim samples
470 save_restoreGB(GRAPH_RESTORE);
471 break;
472 case DEMOD_NRZ:
473 snprintf(cmdStr, sizeof(buf),"%d %d 1", bitRate[config.bitrate], config.inverted );
474 ans = NRZrawDemod(cmdStr, false);
475 break;
476 case DEMOD_BI:
477 case DEMOD_BIa:
478 snprintf(cmdStr, sizeof(buf),"0 %d %d 1", bitRate[config.bitrate], config.inverted );
479 ans = ASKbiphaseDemod(cmdStr, false);
480 break;
481 default:
482 return false;
483 }
484 return (bool) ans;
485 }
486
487 bool DecodeT5555TraceBlock() {
488 DemodBufferLen = 0x00;
489
490 // According to datasheet. Always: RF/64, not inverted, Manchester
491 return (bool) ASKDemod("64 0 1", false, false, 1);
492 }
493
494 int CmdT55xxDetect(const char *Cmd){
495 bool errors = false;
496 bool useGB = false;
497 bool usepwd = false;
498 uint32_t password = 0;
499 uint8_t cmdp = 0;
500 uint8_t downlink_mode = 0;
501
502 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {
503 switch(param_getchar(Cmd, cmdp)) {
504 case 'h':
505 case 'H':
506 return usage_t55xx_detect();
507 case 'p':
508 case 'P':
509 password = param_get32ex(Cmd, cmdp+1, 0, 16);
510 usepwd = true;
511 cmdp += 2;
512 break;
513 case '1':
514 // use Graphbuffer data
515 useGB = true;
516 cmdp++;
517 break;
518 case 'e':
519 case 'E':
520 downlink_mode = param_getchar(Cmd, cmdp+1) - '0';
521 if (downlink_mode > 3) downlink_mode = 0;
522 cmdp +=2;
523 break;
524 default:
525 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
526 errors = true;
527 break;
528 }
529 }
530 if (errors) return usage_t55xx_detect();
531
532 if ( !useGB) {
533 if ( !AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, usepwd, password,downlink_mode) )
534 return 0;
535 }
536
537 if ( !tryDetectModulation() )
538 PrintAndLog("Could not detect modulation automatically. Try setting it manually with \'lf t55xx config\'");
539 else {
540 // Add downlink mode to reference.
541 switch (downlink_mode) {
542 case 0 : PrintAndLog ("Downlink : e 0 - Default/Fixed Bit Length"); break;
543 case 1 : PrintAndLog ("Downlink : e 1 - Long Leading Reference"); break;
544 case 2 : PrintAndLog ("Downlink : e 2 - Leading Zero Reference"); break;
545 case 3 : PrintAndLog ("Downlink : e 3 - 1 of 4 Coding"); break;
546 // default:
547
548 // No default action
549 }
550 }
551 return 1;
552 }
553
554 // detect configuration?
555 bool tryDetectModulation(){
556 uint8_t hits = 0;
557 t55xx_conf_block_t tests[15];
558 int bitRate=0;
559 uint8_t fc1 = 0, fc2 = 0, ans = 0;
560 int clk = 0, firstClockEdge = 0;
561 ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false, &firstClockEdge);
562 if (ans && ((fc1==10 && fc2==8) || (fc1==8 && fc2==5))) {
563 if ( FSKrawDemod("0 0", false) && test(DEMOD_FSK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {
564 tests[hits].modulation = DEMOD_FSK;
565 if (fc1==8 && fc2 == 5)
566 tests[hits].modulation = DEMOD_FSK1a;
567 else if (fc1==10 && fc2 == 8)
568 tests[hits].modulation = DEMOD_FSK2;
569 tests[hits].bitrate = bitRate;
570 tests[hits].inverted = false;
571 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
572 tests[hits].ST = false;
573 ++hits;
574 }
575 if ( FSKrawDemod("0 1", false) && test(DEMOD_FSK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {
576 tests[hits].modulation = DEMOD_FSK;
577 if (fc1 == 8 && fc2 == 5)
578 tests[hits].modulation = DEMOD_FSK1;
579 else if (fc1 == 10 && fc2 == 8)
580 tests[hits].modulation = DEMOD_FSK2a;
581 tests[hits].bitrate = bitRate;
582 tests[hits].inverted = true;
583 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
584 tests[hits].ST = false;
585 ++hits;
586 }
587 } else {
588 clk = GetAskClock("", false, false);
589 if (clk>0) {
590 tests[hits].ST = true;
591 if ( ASKDemod_ext("0 0 1", false, false, 1, &tests[hits].ST) && test(DEMOD_ASK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {
592 tests[hits].modulation = DEMOD_ASK;
593 tests[hits].bitrate = bitRate;
594 tests[hits].inverted = false;
595 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
596 ++hits;
597 }
598 tests[hits].ST = true;
599 if ( ASKDemod_ext("0 1 1", false, false, 1, &tests[hits].ST) && test(DEMOD_ASK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {
600 tests[hits].modulation = DEMOD_ASK;
601 tests[hits].bitrate = bitRate;
602 tests[hits].inverted = true;
603 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
604 ++hits;
605 }
606 if ( ASKbiphaseDemod("0 0 0 2", false) && test(DEMOD_BI, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5) ) {
607 tests[hits].modulation = DEMOD_BI;
608 tests[hits].bitrate = bitRate;
609 tests[hits].inverted = false;
610 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
611 tests[hits].ST = false;
612 ++hits;
613 }
614 if ( ASKbiphaseDemod("0 0 1 2", false) && test(DEMOD_BIa, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5) ) {
615 tests[hits].modulation = DEMOD_BIa;
616 tests[hits].bitrate = bitRate;
617 tests[hits].inverted = true;
618 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
619 tests[hits].ST = false;
620 ++hits;
621 }
622 }
623 clk = GetNrzClock("", false, false);
624 if (clk>8) { //clock of rf/8 is likely a false positive, so don't use it.
625 if ( NRZrawDemod("0 0 1", false) && test(DEMOD_NRZ, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {
626 tests[hits].modulation = DEMOD_NRZ;
627 tests[hits].bitrate = bitRate;
628 tests[hits].inverted = false;
629 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
630 tests[hits].ST = false;
631 ++hits;
632 }
633
634 if ( NRZrawDemod("0 1 1", false) && test(DEMOD_NRZ, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {
635 tests[hits].modulation = DEMOD_NRZ;
636 tests[hits].bitrate = bitRate;
637 tests[hits].inverted = true;
638 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
639 tests[hits].ST = false;
640 ++hits;
641 }
642 }
643
644 clk = GetPskClock("", false, false);
645 if (clk>0) {
646 // allow undo
647 save_restoreGB(GRAPH_SAVE);
648 // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)
649 CmdLtrim("160");
650 if ( PSKDemod("0 0 6", false) && test(DEMOD_PSK1, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {
651 tests[hits].modulation = DEMOD_PSK1;
652 tests[hits].bitrate = bitRate;
653 tests[hits].inverted = false;
654 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
655 tests[hits].ST = false;
656 ++hits;
657 }
658 if ( PSKDemod("0 1 6", false) && test(DEMOD_PSK1, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)) {
659 tests[hits].modulation = DEMOD_PSK1;
660 tests[hits].bitrate = bitRate;
661 tests[hits].inverted = true;
662 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
663 tests[hits].ST = false;
664 ++hits;
665 }
666 // PSK2 - needs a call to psk1TOpsk2.
667 if ( PSKDemod("0 0 6", false)) {
668 psk1TOpsk2(DemodBuffer, DemodBufferLen);
669 if (test(DEMOD_PSK2, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)){
670 tests[hits].modulation = DEMOD_PSK2;
671 tests[hits].bitrate = bitRate;
672 tests[hits].inverted = false;
673 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
674 tests[hits].ST = false;
675 ++hits;
676 }
677 } // inverse waves does not affect this demod
678 // PSK3 - needs a call to psk1TOpsk2.
679 if ( PSKDemod("0 0 6", false)) {
680 psk1TOpsk2(DemodBuffer, DemodBufferLen);
681 if (test(DEMOD_PSK3, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)){
682 tests[hits].modulation = DEMOD_PSK3;
683 tests[hits].bitrate = bitRate;
684 tests[hits].inverted = false;
685 tests[hits].block0 = PackBits(tests[hits].offset, 32, DemodBuffer);
686 tests[hits].ST = false;
687 ++hits;
688 }
689 } // inverse waves does not affect this demod
690 //undo trim samples
691 save_restoreGB(GRAPH_RESTORE);
692 }
693 }
694 if ( hits == 1) {
695 config.modulation = tests[0].modulation;
696 config.bitrate = tests[0].bitrate;
697 config.inverted = tests[0].inverted;
698 config.offset = tests[0].offset;
699 config.block0 = tests[0].block0;
700 config.Q5 = tests[0].Q5;
701 config.ST = tests[0].ST;
702 printConfiguration( config );
703 return true;
704 }
705
706 if ( hits > 1) {
707 PrintAndLog("Found [%d] possible matches for modulation.",hits);
708 for(int i=0; i<hits; ++i){
709 PrintAndLog("--[%d]---------------", i+1);
710 printConfiguration( tests[i] );
711 }
712 }
713 return false;
714 }
715
716 bool testModulation(uint8_t mode, uint8_t modread){
717 switch( mode ){
718 case DEMOD_FSK:
719 if (modread >= DEMOD_FSK1 && modread <= DEMOD_FSK2a) return true;
720 break;
721 case DEMOD_ASK:
722 if (modread == DEMOD_ASK) return true;
723 break;
724 case DEMOD_PSK1:
725 if (modread == DEMOD_PSK1) return true;
726 break;
727 case DEMOD_PSK2:
728 if (modread == DEMOD_PSK2) return true;
729 break;
730 case DEMOD_PSK3:
731 if (modread == DEMOD_PSK3) return true;
732 break;
733 case DEMOD_NRZ:
734 if (modread == DEMOD_NRZ) return true;
735 break;
736 case DEMOD_BI:
737 if (modread == DEMOD_BI) return true;
738 break;
739 case DEMOD_BIa:
740 if (modread == DEMOD_BIa) return true;
741 break;
742 default:
743 return false;
744 }
745 return false;
746 }
747
748 bool testQ5Modulation(uint8_t mode, uint8_t modread){
749 switch( mode ){
750 case DEMOD_FSK:
751 if (modread >= 4 && modread <= 5) return true;
752 break;
753 case DEMOD_ASK:
754 if (modread == 0) return true;
755 break;
756 case DEMOD_PSK1:
757 if (modread == 1) return true;
758 break;
759 case DEMOD_PSK2:
760 if (modread == 2) return true;
761 break;
762 case DEMOD_PSK3:
763 if (modread == 3) return true;
764 break;
765 case DEMOD_NRZ:
766 if (modread == 7) return true;
767 break;
768 case DEMOD_BI:
769 if (modread == 6) return true;
770 break;
771 default:
772 return false;
773 }
774 return false;
775 }
776
777 int convertQ5bitRate(uint8_t bitRateRead) {
778 uint8_t expected[] = {8, 16, 32, 40, 50, 64, 100, 128};
779 for (int i=0; i<8; i++)
780 if (expected[i] == bitRateRead)
781 return i;
782
783 return -1;
784 }
785
786 bool testQ5(uint8_t mode, uint8_t *offset, int *fndBitRate, uint8_t clk){
787
788 if ( DemodBufferLen < 64 ) return false;
789 uint8_t si = 0;
790 for (uint8_t idx = 28; idx < 64; idx++){
791 si = idx;
792 if ( PackBits(si, 28, DemodBuffer) == 0x00 ) continue;
793
794 uint8_t safer = PackBits(si, 4, DemodBuffer); si += 4; //master key
795 uint8_t resv = PackBits(si, 8, DemodBuffer); si += 8;
796 // 2nibble must be zeroed.
797 if (safer != 0x6 && safer != 0x9) continue;
798 if ( resv > 0x00) continue;
799 //uint8_t pageSel = PackBits(si, 1, DemodBuffer); si += 1;
800 //uint8_t fastWrite = PackBits(si, 1, DemodBuffer); si += 1;
801 si += 1+1;
802 int bitRate = PackBits(si, 6, DemodBuffer)*2 + 2; si += 6; //bit rate
803 if (bitRate > 128 || bitRate < 8) continue;
804
805 //uint8_t AOR = PackBits(si, 1, DemodBuffer); si += 1;
806 //uint8_t PWD = PackBits(si, 1, DemodBuffer); si += 1;
807 //uint8_t pskcr = PackBits(si, 2, DemodBuffer); si += 2; //could check psk cr
808 //uint8_t inverse = PackBits(si, 1, DemodBuffer); si += 1;
809 si += 1+1+2+1;
810 uint8_t modread = PackBits(si, 3, DemodBuffer); si += 3;
811 uint8_t maxBlk = PackBits(si, 3, DemodBuffer); si += 3;
812 //uint8_t ST = PackBits(si, 1, DemodBuffer); si += 1;
813 if (maxBlk == 0) continue;
814 //test modulation
815 if (!testQ5Modulation(mode, modread)) continue;
816 if (bitRate != clk) continue;
817 *fndBitRate = convertQ5bitRate(bitRate);
818 if (*fndBitRate < 0) continue;
819 *offset = idx;
820
821 return true;
822 }
823 return false;
824 }
825
826 bool testBitRate(uint8_t readRate, uint8_t clk){
827 uint8_t expected[] = {8, 16, 32, 40, 50, 64, 100, 128};
828 if (expected[readRate] == clk)
829 return true;
830
831 return false;
832 }
833
834 bool test(uint8_t mode, uint8_t *offset, int *fndBitRate, uint8_t clk, bool *Q5){
835
836 if ( DemodBufferLen < 64 ) return false;
837 uint8_t si = 0;
838 for (uint8_t idx = 28; idx < 64; idx++){
839 si = idx;
840 if ( PackBits(si, 28, DemodBuffer) == 0x00 ) continue;
841
842 uint8_t safer = PackBits(si, 4, DemodBuffer); si += 4; //master key
843 uint8_t resv = PackBits(si, 4, DemodBuffer); si += 4; //was 7 & +=7+3 //should be only 4 bits if extended mode
844 // 2nibble must be zeroed.
845 // moved test to here, since this gets most faults first.
846 if ( resv > 0x00) continue;
847
848 int bitRate = PackBits(si, 6, DemodBuffer); si += 6; //bit rate (includes extended mode part of rate)
849 uint8_t extend = PackBits(si, 1, DemodBuffer); si += 1; //bit 15 extended mode
850 uint8_t modread = PackBits(si, 5, DemodBuffer); si += 5+2+1;
851 //uint8_t pskcr = PackBits(si, 2, DemodBuffer); si += 2+1; //could check psk cr
852 //uint8_t nml01 = PackBits(si, 1, DemodBuffer); si += 1+5; //bit 24, 30, 31 could be tested for 0 if not extended mode
853 //uint8_t nml02 = PackBits(si, 2, DemodBuffer); si += 2;
854
855 //if extended mode
856 bool extMode =( (safer == 0x6 || safer == 0x9) && extend) ? true : false;
857
858 if (!extMode) {
859 if (bitRate > 7) continue;
860 if (!testBitRate(bitRate, clk)) continue;
861 } else { //extended mode bitrate = same function to calc bitrate as em4x05
862 if (EM4x05_GET_BITRATE(bitRate) != clk) continue;
863
864 }
865 //test modulation
866 if (!testModulation(mode, modread)) continue;
867 *fndBitRate = bitRate;
868 *offset = idx;
869 *Q5 = false;
870 return true;
871 }
872 if (testQ5(mode, offset, fndBitRate, clk)) {
873 *Q5 = true;
874 return true;
875 }
876 return false;
877 }
878
879 void printT55xxBlock(const char *blockNum){
880
881 uint8_t i = config.offset;
882 uint8_t endpos = 32 + i;
883 uint32_t blockData = 0;
884 uint8_t bits[64] = {0x00};
885
886 if ( !DemodBufferLen) return;
887
888 if ( endpos > DemodBufferLen){
889 PrintAndLog("The configured offset %d is too big. Possible offset: %d)", i, DemodBufferLen-32);
890 return;
891 }
892
893 for (; i < endpos; ++i)
894 bits[i - config.offset]=DemodBuffer[i];
895
896 blockData = PackBits(0, 32, bits);
897
898 PrintAndLog(" %s | %08X | %s", blockNum, blockData, sprint_bin(bits,32));
899 }
900
901 int special(const char *Cmd) {
902 uint32_t blockData = 0;
903 uint8_t bits[32] = {0x00};
904
905 PrintAndLog("OFFSET | DATA | BINARY");
906 PrintAndLog("----------------------------------------------------");
907 int i,j = 0;
908 for (; j < 64; ++j){
909
910 for (i = 0; i < 32; ++i)
911 bits[i]=DemodBuffer[j+i];
912
913 blockData = PackBits(0, 32, bits);
914
915 PrintAndLog(" %02d | 0x%08X | %s",j , blockData, sprint_bin(bits,32));
916 }
917 return 0;
918 }
919
920 int printConfiguration( t55xx_conf_block_t b){
921 PrintAndLog("Chip Type : %s", (b.Q5) ? "T5555(Q5)" : "T55x7");
922 PrintAndLog("Modulation : %s", GetSelectedModulationStr(b.modulation) );
923 PrintAndLog("Bit Rate : %s", GetBitRateStr(b.bitrate, (b.block0 & T55x7_X_MODE && (b.block0>>28==6 || b.block0>>28==9))) );
924 PrintAndLog("Inverted : %s", (b.inverted) ? "Yes" : "No" );
925 PrintAndLog("Offset : %d", b.offset);
926 PrintAndLog("Seq. Term. : %s", (b.ST) ? "Yes" : "No" );
927 PrintAndLog("Block0 : 0x%08X", b.block0);
928 PrintAndLog("");
929 return 0;
930 }
931
932 int CmdT55xxWakeUp(const char *Cmd) {
933 uint32_t password = 0;
934 if ( strlen(Cmd) <= 0 ) return usage_t55xx_wakup();
935 char cmdp = param_getchar(Cmd, 0);
936 if ( cmdp == 'h' || cmdp == 'H') return usage_t55xx_wakup();
937
938 password = param_get32ex(Cmd, 0, 0, 16);
939
940 UsbCommand c = {CMD_T55XX_WAKEUP, {password, 0, 0}};
941 clearCommandBuffer();
942 SendCommand(&c);
943 PrintAndLog("Wake up command sent. Try read now");
944 return 0;
945 }
946
947 int CmdT55xxWriteBlock(const char *Cmd) {
948 uint8_t block = 0xFF; //default to invalid block
949 uint32_t data = 0; //default to blank Block
950 uint32_t password = 0; //default to blank Block 7
951 uint32_t downlink_mode = 0;
952
953 bool usepwd = false;
954 bool page1 = false;
955 bool gotdata = false;
956 bool testMode = false;
957 bool errors = false;
958 uint8_t cmdp = 0;
959 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {
960 switch(param_getchar(Cmd, cmdp)) {
961 case 'h':
962 case 'H':
963 return usage_t55xx_write();
964 case 'b':
965 case 'B':
966 errors |= param_getdec(Cmd, cmdp+1, &block);
967 cmdp += 2;
968 break;
969 case 'd':
970 case 'D':
971 data = param_get32ex(Cmd, cmdp+1, 0, 16);
972 gotdata = true;
973 cmdp += 2;
974 break;
975 case 'p':
976 case 'P':
977 password = param_get32ex(Cmd, cmdp+1, 0, 16);
978 usepwd = true;
979 cmdp += 2;
980 break;
981 case 't':
982 case 'T':
983 testMode = true;
984 cmdp++;
985 break;
986 case '1':
987 page1 = true;
988 cmdp++;
989 break;
990 case 'e':
991 case 'E':
992 downlink_mode = param_getchar(Cmd, cmdp+1) - '0';
993 if (downlink_mode > 3) downlink_mode = 0;
994 cmdp +=2;
995 break;
996 default:
997 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
998 errors = true;
999 break;
1000 }
1001 }
1002 if (errors || !gotdata) return usage_t55xx_write();
1003
1004 if (block > 7) {
1005 PrintAndLog("Block number must be between 0 and 7");
1006 return 0;
1007 }
1008
1009 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {data, block, 0}};
1010 UsbCommand resp;
1011 c.d.asBytes[0] = (page1) ? 0x2 : 0;
1012 c.d.asBytes[0] |= (testMode) ? 0x4 : 0;
1013 c.d.asBytes[0] |= (downlink_mode << 3);
1014
1015 char pwdStr[16] = {0};
1016 snprintf(pwdStr, sizeof(pwdStr), "pwd: 0x%08X", password);
1017
1018 PrintAndLog("Writing page %d block: %02d data: 0x%08X %s", page1, block, data, (usepwd) ? pwdStr : "" );
1019
1020 //Password mode
1021 if (usepwd) {
1022 c.arg[2] = password;
1023 c.d.asBytes[0] |= 0x1;
1024 }
1025
1026 clearCommandBuffer();
1027 SendCommand(&c);
1028 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){
1029 PrintAndLog("Error occurred, device did not ACK write operation. (May be due to old firmware)");
1030 return 0;
1031 }
1032 return 1;
1033 }
1034
1035 int CmdT55xxReadTrace(const char *Cmd) {
1036 char cmdp = param_getchar(Cmd, 0);
1037 bool pwdmode = false;
1038 uint32_t password = 0;
1039 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H')
1040 return usage_t55xx_trace();
1041
1042 if (strlen(Cmd)==0)
1043 if ( !AquireData( T55x7_PAGE1, REGULAR_READ_MODE_BLOCK, pwdmode, password,0 ) )
1044 return 0;
1045
1046 if ( config.Q5 ) {
1047 if (!DecodeT5555TraceBlock()) return 0;
1048 } else {
1049 if (!DecodeT55xxBlock()) return 0;
1050 }
1051
1052 if ( !DemodBufferLen ) return 0;
1053
1054 RepaintGraphWindow();
1055 uint8_t repeat = (config.offset > 5) ? 32 : 0;
1056
1057 uint8_t si = config.offset+repeat;
1058 uint32_t bl1 = PackBits(si, 32, DemodBuffer);
1059 uint32_t bl2 = PackBits(si+32, 32, DemodBuffer);
1060
1061 if (config.Q5) {
1062 uint32_t hdr = PackBits(si, 9, DemodBuffer); si += 9;
1063
1064 if (hdr != 0x1FF) {
1065 PrintAndLog("Invalid Q5 Trace data header (expected 0x1FF, found %X)", hdr);
1066 return 0;
1067 }
1068
1069 t5555_tracedata_t data = {.bl1 = bl1, .bl2 = bl2, .icr = 0, .lotidc = '?', .lotid = 0, .wafer = 0, .dw =0};
1070
1071 data.icr = PackBits(si, 2, DemodBuffer); si += 2;
1072 data.lotidc = 'Z' - PackBits(si, 2, DemodBuffer); si += 3;
1073
1074 data.lotid = PackBits(si, 4, DemodBuffer); si += 5;
1075 data.lotid <<= 4;
1076 data.lotid |= PackBits(si, 4, DemodBuffer); si += 5;
1077 data.lotid <<= 4;
1078 data.lotid |= PackBits(si, 4, DemodBuffer); si += 5;
1079 data.lotid <<= 4;
1080 data.lotid |= PackBits(si, 4, DemodBuffer); si += 5;
1081 data.lotid <<= 1;
1082 data.lotid |= PackBits(si, 1, DemodBuffer); si += 1;
1083
1084 data.wafer = PackBits(si, 3, DemodBuffer); si += 4;
1085 data.wafer <<= 2;
1086 data.wafer |= PackBits(si, 2, DemodBuffer); si += 2;
1087
1088 data.dw = PackBits(si, 2, DemodBuffer); si += 3;
1089 data.dw <<= 4;
1090 data.dw |= PackBits(si, 4, DemodBuffer); si += 5;
1091 data.dw <<= 4;
1092 data.dw |= PackBits(si, 4, DemodBuffer); si += 5;
1093 data.dw <<= 4;
1094 data.dw |= PackBits(si, 4, DemodBuffer); si += 5;
1095
1096 printT5555Trace(data, repeat);
1097
1098 } else {
1099
1100 t55x7_tracedata_t data = {.bl1 = bl1, .bl2 = bl2, .acl = 0, .mfc = 0, .cid = 0, .year = 0, .quarter = 0, .icr = 0, .lotid = 0, .wafer = 0, .dw = 0};
1101
1102 data.acl = PackBits(si, 8, DemodBuffer); si += 8;
1103 if ( data.acl != 0xE0 ) {
1104 PrintAndLog("The modulation is most likely wrong since the ACL is not 0xE0. ");
1105 return 0;
1106 }
1107
1108 data.mfc = PackBits(si, 8, DemodBuffer); si += 8;
1109 data.cid = PackBits(si, 5, DemodBuffer); si += 5;
1110 data.icr = PackBits(si, 3, DemodBuffer); si += 3;
1111 data.year = PackBits(si, 4, DemodBuffer); si += 4;
1112 data.quarter = PackBits(si, 2, DemodBuffer); si += 2;
1113 data.lotid = PackBits(si, 14, DemodBuffer); si += 14;
1114 data.wafer = PackBits(si, 5, DemodBuffer); si += 5;
1115 data.dw = PackBits(si, 15, DemodBuffer);
1116
1117 time_t t = time(NULL);
1118 struct tm tm = *localtime(&t);
1119 if ( data.year > tm.tm_year-110)
1120 data.year += 2000;
1121 else
1122 data.year += 2010;
1123
1124 printT55x7Trace(data, repeat);
1125 }
1126 return 0;
1127 }
1128
1129 void printT55x7Trace( t55x7_tracedata_t data, uint8_t repeat ){
1130 PrintAndLog("-- T55x7 Trace Information ----------------------------------");
1131 PrintAndLog("-------------------------------------------------------------");
1132 PrintAndLog(" ACL Allocation class (ISO/IEC 15963-1) : 0x%02X (%d)", data.acl, data.acl);
1133 PrintAndLog(" MFC Manufacturer ID (ISO/IEC 7816-6) : 0x%02X (%d) - %s", data.mfc, data.mfc, getManufacturerName(data.mfc));
1134 PrintAndLog(" CID : 0x%02X (%d) - %s", data.cid, data.cid, GetModelStrFromCID(data.cid));
1135 PrintAndLog(" ICR IC Revision : %d", data.icr );
1136 PrintAndLog(" Manufactured");
1137 PrintAndLog(" Year/Quarter : %d/%d", data.year, data.quarter);
1138 PrintAndLog(" Lot ID : %d", data.lotid );
1139 PrintAndLog(" Wafer number : %d", data.wafer);
1140 PrintAndLog(" Die Number : %d", data.dw);
1141 PrintAndLog("-------------------------------------------------------------");
1142 PrintAndLog(" Raw Data - Page 1");
1143 PrintAndLog(" Block 1 : 0x%08X %s", data.bl1, sprint_bin(DemodBuffer+config.offset+repeat,32) );
1144 PrintAndLog(" Block 2 : 0x%08X %s", data.bl2, sprint_bin(DemodBuffer+config.offset+repeat+32,32) );
1145 PrintAndLog("-------------------------------------------------------------");
1146
1147 /*
1148 TRACE - BLOCK O
1149 Bits Definition HEX
1150 1-8 ACL Allocation class (ISO/IEC 15963-1) 0xE0
1151 9-16 MFC Manufacturer ID (ISO/IEC 7816-6) 0x15 Atmel Corporation
1152 17-21 CID 0x1 = Atmel ATA5577M1 0x2 = Atmel ATA5577M2
1153 22-24 ICR IC revision
1154 25-28 YEAR (BCD encoded) 9 (= 2009)
1155 29-30 QUARTER 1,2,3,4
1156 31-32 LOT ID
1157
1158 TRACE - BLOCK 1
1159 1-12 LOT ID
1160 13-17 Wafer number
1161 18-32 DW, die number sequential
1162 */
1163 }
1164
1165 void printT5555Trace( t5555_tracedata_t data, uint8_t repeat ){
1166 PrintAndLog("-- T5555 (Q5) Trace Information -----------------------------");
1167 PrintAndLog("-------------------------------------------------------------");
1168 PrintAndLog(" ICR IC Revision : %d", data.icr );
1169 PrintAndLog(" Lot : %c%d", data.lotidc, data.lotid);
1170 PrintAndLog(" Wafer number : %d", data.wafer);
1171 PrintAndLog(" Die Number : %d", data.dw);
1172 PrintAndLog("-------------------------------------------------------------");
1173 PrintAndLog(" Raw Data - Page 1");
1174 PrintAndLog(" Block 1 : 0x%08X %s", data.bl1, sprint_bin(DemodBuffer+config.offset+repeat,32) );
1175 PrintAndLog(" Block 2 : 0x%08X %s", data.bl2, sprint_bin(DemodBuffer+config.offset+repeat+32,32) );
1176
1177 /*
1178 ** Q5 **
1179 TRACE - BLOCK O and BLOCK1
1180 Bits Definition HEX
1181 1-9 Header 0x1FF
1182 10-11 IC Revision
1183 12-13 Lot ID char
1184 15-35 Lot ID (NB parity)
1185 36-41 Wafer number (NB parity)
1186 42-58 DW, die number sequential (NB parity)
1187 60-63 Parity bits
1188 64 Always zero
1189 */
1190 }
1191
1192 //need to add Q5 info...
1193 int CmdT55xxInfo(const char *Cmd){
1194 /*
1195 Page 0 Block 0 Configuration data.
1196 Normal mode
1197 Extended mode
1198 */
1199 bool pwdmode = false;
1200 uint32_t password = 0;
1201 char cmdp = param_getchar(Cmd, 0);
1202
1203 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H')
1204 return usage_t55xx_info();
1205
1206 if (strlen(Cmd)==0)
1207 if ( !AquireData( T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, pwdmode, password,0 ) )
1208 return 1;
1209
1210 if (!DecodeT55xxBlock()) return 1;
1211
1212 // too little space to start with
1213 if ( DemodBufferLen < 32) return 1;
1214
1215 uint8_t si = config.offset;
1216 uint32_t bl0 = PackBits(si, 32, DemodBuffer);
1217
1218 uint32_t safer = PackBits(si, 4, DemodBuffer); si += 4;
1219 uint32_t resv = PackBits(si, 7, DemodBuffer); si += 7;
1220 uint32_t dbr = PackBits(si, 3, DemodBuffer); si += 3;
1221 uint32_t extend = PackBits(si, 1, DemodBuffer); si += 1;
1222 uint32_t datamod = PackBits(si, 5, DemodBuffer); si += 5;
1223 uint32_t pskcf = PackBits(si, 2, DemodBuffer); si += 2;
1224 uint32_t aor = PackBits(si, 1, DemodBuffer); si += 1;
1225 uint32_t otp = PackBits(si, 1, DemodBuffer); si += 1;
1226 uint32_t maxblk = PackBits(si, 3, DemodBuffer); si += 3;
1227 uint32_t pwd = PackBits(si, 1, DemodBuffer); si += 1;
1228 uint32_t sst = PackBits(si, 1, DemodBuffer); si += 1;
1229 uint32_t fw = PackBits(si, 1, DemodBuffer); si += 1;
1230 uint32_t inv = PackBits(si, 1, DemodBuffer); si += 1;
1231 uint32_t por = PackBits(si, 1, DemodBuffer); si += 1;
1232
1233 if (config.Q5) PrintAndLog("*** Warning *** Config Info read off a Q5 will not display as expected");
1234 PrintAndLog("");
1235 PrintAndLog("-- T55x7 Configuration & Tag Information --------------------");
1236 PrintAndLog("-------------------------------------------------------------");
1237 PrintAndLog(" Safer key : %s", GetSaferStr(safer));
1238 PrintAndLog(" reserved : %d", resv);
1239 PrintAndLog(" Data bit rate : %s", GetBitRateStr(dbr, extend));
1240 PrintAndLog(" eXtended mode : %s", (extend) ? "Yes - Warning":"No");
1241 PrintAndLog(" Modulation : %s", GetModulationStr(datamod));
1242 PrintAndLog(" PSK clock frequency : %d", pskcf);
1243 PrintAndLog(" AOR - Answer on Request : %s", (aor) ? "Yes":"No");
1244 PrintAndLog(" OTP - One Time Pad : %s", (otp) ? "Yes - Warning":"No" );
1245 PrintAndLog(" Max block : %d", maxblk);
1246 PrintAndLog(" Password mode : %s", (pwd) ? "Yes":"No");
1247 PrintAndLog(" Sequence Start Terminator : %s", (sst) ? "Yes":"No");
1248 PrintAndLog(" Fast Write : %s", (fw) ? "Yes":"No");
1249 PrintAndLog(" Inverse data : %s", (inv) ? "Yes":"No");
1250 PrintAndLog(" POR-Delay : %s", (por) ? "Yes":"No");
1251 PrintAndLog("-------------------------------------------------------------");
1252 PrintAndLog(" Raw Data - Page 0");
1253 PrintAndLog(" Block 0 : 0x%08X %s", bl0, sprint_bin(DemodBuffer+config.offset,32) );
1254 PrintAndLog("-------------------------------------------------------------");
1255
1256 return 0;
1257 }
1258
1259 int CmdT55xxDump(const char *Cmd){
1260
1261 uint32_t password = 0;
1262 char cmdp = param_getchar(Cmd, 0);
1263 bool override = false;
1264 if ( cmdp == 'h' || cmdp == 'H') return usage_t55xx_dump();
1265
1266 bool usepwd = ( strlen(Cmd) > 0);
1267 if ( usepwd ){
1268 password = param_get32ex(Cmd, 0, 0, 16);
1269 if (param_getchar(Cmd, 1) =='o' )
1270 override = true;
1271 }
1272
1273 printT5xxHeader(0);
1274 for ( uint8_t i = 0; i <8; ++i)
1275 T55xxReadBlock(i, 0, usepwd, override, password,0);
1276
1277 printT5xxHeader(1);
1278 for ( uint8_t i = 0; i<4; i++)
1279 T55xxReadBlock(i, 1, usepwd, override, password,0);
1280
1281 return 1;
1282 }
1283
1284 int AquireData( uint8_t page, uint8_t block, bool pwdmode, uint32_t password, uint8_t downlink_mode ){
1285 // arg0 bitmodes:
1286 // bit0 = pwdmode
1287 // bit1 = page to read from
1288 uint8_t arg0 = (page<<1) | pwdmode;
1289 arg0 |= (downlink_mode << 3);
1290 UsbCommand c = {CMD_T55XX_READ_BLOCK, {arg0, block, password}};
1291
1292 clearCommandBuffer();
1293 SendCommand(&c);
1294 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {
1295 PrintAndLog("command execution time out");
1296 return 0;
1297 }
1298 getSamples(12000,true);
1299 return 1;
1300 }
1301
1302 char * GetBitRateStr(uint32_t id, bool xmode) {
1303 static char buf[25];
1304
1305 char *retStr = buf;
1306 if (xmode) { //xmode bitrate calc is same as em4x05 calc
1307 snprintf(retStr,sizeof(buf),"%d - RF/%d", id, EM4x05_GET_BITRATE(id));
1308 } else {
1309 switch (id) {
1310 case 0: snprintf(retStr,sizeof(buf),"%d - RF/8",id); break;
1311 case 1: snprintf(retStr,sizeof(buf),"%d - RF/16",id); break;
1312 case 2: snprintf(retStr,sizeof(buf),"%d - RF/32",id); break;
1313 case 3: snprintf(retStr,sizeof(buf),"%d - RF/40",id); break;
1314 case 4: snprintf(retStr,sizeof(buf),"%d - RF/50",id); break;
1315 case 5: snprintf(retStr,sizeof(buf),"%d - RF/64",id); break;
1316 case 6: snprintf(retStr,sizeof(buf),"%d - RF/100",id); break;
1317 case 7: snprintf(retStr,sizeof(buf),"%d - RF/128",id); break;
1318 default: snprintf(retStr,sizeof(buf),"%d - (Unknown)",id); break;
1319 }
1320 }
1321 return buf;
1322 }
1323
1324 char * GetSaferStr(uint32_t id) {
1325 static char buf[40];
1326 char *retStr = buf;
1327
1328 snprintf(retStr,sizeof(buf),"%d",id);
1329 if (id == 6) {
1330 snprintf(retStr,sizeof(buf),"%d - passwd",id);
1331 }
1332 if (id == 9 ){
1333 snprintf(retStr,sizeof(buf),"%d - testmode",id);
1334 }
1335
1336 return buf;
1337 }
1338
1339 char * GetModulationStr( uint32_t id){
1340 static char buf[60];
1341 char *retStr = buf;
1342
1343 switch (id){
1344 case 0: snprintf(retStr,sizeof(buf),"%d - DIRECT (ASK/NRZ)",id); break;
1345 case 1: snprintf(retStr,sizeof(buf),"%d - PSK 1 phase change when input changes",id); break;
1346 case 2: snprintf(retStr,sizeof(buf),"%d - PSK 2 phase change on bitclk if input high",id); break;
1347 case 3: snprintf(retStr,sizeof(buf),"%d - PSK 3 phase change on rising edge of input",id); break;
1348 case 4: snprintf(retStr,sizeof(buf),"%d - FSK 1 RF/8 RF/5",id); break;
1349 case 5: snprintf(retStr,sizeof(buf),"%d - FSK 2 RF/8 RF/10",id); break;
1350 case 6: snprintf(retStr,sizeof(buf),"%d - FSK 1a RF/5 RF/8",id); break;
1351 case 7: snprintf(retStr,sizeof(buf),"%d - FSK 2a RF/10 RF/8",id); break;
1352 case 8: snprintf(retStr,sizeof(buf),"%d - Manchester",id); break;
1353 case 16: snprintf(retStr,sizeof(buf),"%d - Biphase",id); break;
1354 case 0x18: snprintf(retStr,sizeof(buf),"%d - Biphase a - AKA Conditional Dephase Encoding(CDP)",id); break;
1355 case 17: snprintf(retStr,sizeof(buf),"%d - Reserved",id); break;
1356 default: snprintf(retStr,sizeof(buf),"0x%02X (Unknown)",id); break;
1357 }
1358 return buf;
1359 }
1360
1361 char * GetModelStrFromCID(uint32_t cid){
1362
1363 static char buf[10];
1364 char *retStr = buf;
1365
1366 if (cid == 1) snprintf(retStr, sizeof(buf),"ATA5577M1");
1367 if (cid == 2) snprintf(retStr, sizeof(buf),"ATA5577M2");
1368 return buf;
1369 }
1370
1371 char * GetSelectedModulationStr( uint8_t id){
1372
1373 static char buf[20];
1374 char *retStr = buf;
1375
1376 switch (id) {
1377 case DEMOD_FSK: snprintf(retStr,sizeof(buf),"FSK"); break;
1378 case DEMOD_FSK1: snprintf(retStr,sizeof(buf),"FSK1"); break;
1379 case DEMOD_FSK1a: snprintf(retStr,sizeof(buf),"FSK1a"); break;
1380 case DEMOD_FSK2: snprintf(retStr,sizeof(buf),"FSK2"); break;
1381 case DEMOD_FSK2a: snprintf(retStr,sizeof(buf),"FSK2a"); break;
1382 case DEMOD_ASK: snprintf(retStr,sizeof(buf),"ASK"); break;
1383 case DEMOD_NRZ: snprintf(retStr,sizeof(buf),"DIRECT/NRZ"); break;
1384 case DEMOD_PSK1: snprintf(retStr,sizeof(buf),"PSK1"); break;
1385 case DEMOD_PSK2: snprintf(retStr,sizeof(buf),"PSK2"); break;
1386 case DEMOD_PSK3: snprintf(retStr,sizeof(buf),"PSK3"); break;
1387 case DEMOD_BI: snprintf(retStr,sizeof(buf),"BIPHASE"); break;
1388 case DEMOD_BIa: snprintf(retStr,sizeof(buf),"BIPHASEa - (CDP)"); break;
1389 default: snprintf(retStr,sizeof(buf),"(Unknown)"); break;
1390 }
1391 return buf;
1392 }
1393
1394 uint32_t PackBits(uint8_t start, uint8_t len, uint8_t* bits){
1395
1396 int i = start;
1397 int j = len-1;
1398
1399 if (len > 32) return 0;
1400
1401 uint32_t tmp = 0;
1402 for (; j >= 0; --j, ++i)
1403 tmp |= bits[i] << j;
1404
1405 return tmp;
1406 }
1407
1408 int CmdResetRead(const char *Cmd) {
1409 UsbCommand c = {CMD_T55XX_RESET_READ, {0,0,0}};
1410
1411 clearCommandBuffer();
1412 SendCommand(&c);
1413 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {
1414 PrintAndLog("command execution time out");
1415 return 0;
1416 }
1417
1418 uint8_t got[BIGBUF_SIZE-1];
1419 GetFromBigBuf(got, sizeof(got), 0, NULL, -1 , 0);
1420 setGraphBuf(got, sizeof(got));
1421 return 1;
1422 }
1423
1424 int CmdT55xxWipe(const char *Cmd) {
1425 char writeData[20] = {0};
1426 char *ptrData = writeData;
1427
1428 char cmdp = param_getchar(Cmd, 0);
1429 if ( cmdp == 'h' || cmdp == 'H') return usage_t55xx_wipe();
1430
1431 bool Q5 = (cmdp == 'q' || cmdp == 'Q');
1432
1433 // Try with the default password to reset block 0
1434 // With a pwd should work even if pwd bit not set
1435 PrintAndLog("\nBeginning Wipe of a T55xx tag (assuming the tag is not password protected)\n");
1436
1437 if ( Q5 ){
1438 snprintf(ptrData,sizeof(writeData),"b 0 d 6001F004 p 0");
1439 } else {
1440 snprintf(ptrData,sizeof(writeData),"b 0 d 00088040 p 0");
1441 }
1442
1443 if (!CmdT55xxWriteBlock(ptrData)) PrintAndLog("Error writing blk 0");
1444
1445 for (uint8_t blk = 1; blk<8; blk++) {
1446 snprintf(ptrData,sizeof(writeData),"b %d d 0", blk);
1447 if (!CmdT55xxWriteBlock(ptrData))
1448 PrintAndLog("Error writing blk %d", blk);
1449
1450 memset(writeData, 0x00, sizeof(writeData));
1451 }
1452 return 0;
1453 }
1454
1455 int CmdT55xxBruteForce(const char *Cmd) {
1456
1457 // load a default pwd file.
1458 char buf[9];
1459 char filename[FILE_PATH_SIZE]={0};
1460 int keycnt = 0;
1461 uint8_t downlink_mode = 0;
1462 int ch;
1463 uint8_t stKeyBlock = 20;
1464 uint8_t *keyBlock = NULL, *p = NULL;
1465 uint32_t start_password = 0x00000000; //start password
1466 uint32_t end_password = 0xFFFFFFFF; //end password
1467 bool found = false;
1468
1469 char cmdp = param_getchar(Cmd, 0);
1470 if (cmdp == 'h' || cmdp == 'H') return usage_t55xx_bruteforce();
1471
1472 keyBlock = calloc(stKeyBlock, 6);
1473 if (keyBlock == NULL) return 1;
1474
1475 if (cmdp == 'i' || cmdp == 'I') {
1476
1477 int len = strlen(Cmd+2);
1478 if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;
1479 memcpy(filename, Cmd+2, len);
1480
1481 FILE * f = fopen( filename , "r");
1482
1483 if ( !f ) {
1484 PrintAndLog("File: %s: not found or locked.", filename);
1485 free(keyBlock);
1486 return 1;
1487 }
1488
1489 while( fgets(buf, sizeof(buf), f) ) {
1490 if (strlen(buf) < 8 || buf[7] == '\n') continue;
1491
1492 while (fgetc(f) != '\n' && !feof(f)) ; //goto next line
1493
1494 //The line start with # is comment, skip
1495 if( buf[0]=='#' ) continue;
1496
1497 if (!isxdigit((unsigned char)buf[0])) {
1498 PrintAndLog("File content error. '%s' must include 8 HEX symbols", buf);
1499 continue;
1500 }
1501
1502 buf[8] = 0;
1503
1504 if ( stKeyBlock - keycnt < 2) {
1505 p = realloc(keyBlock, 6*(stKeyBlock+=10));
1506 if (!p) {
1507 PrintAndLog("Cannot allocate memory for defaultKeys");
1508 free(keyBlock);
1509 fclose(f);
1510 return 2;
1511 }
1512 keyBlock = p;
1513 }
1514 memset(keyBlock + 4 * keycnt, 0, 4);
1515 num_to_bytes(strtoll(buf, NULL, 16), 4, keyBlock + 4*keycnt);
1516 PrintAndLog("chk custom pwd[%2d] %08X", keycnt, bytes_to_num(keyBlock + 4*keycnt, 4));
1517 keycnt++;
1518 memset(buf, 0, sizeof(buf));
1519 }
1520 fclose(f);
1521
1522 if (keycnt == 0) {
1523 PrintAndLog("No keys found in file");
1524 free(keyBlock);
1525 return 1;
1526 }
1527 PrintAndLog("Loaded %d keys", keycnt);
1528
1529 // loop
1530 uint64_t testpwd = 0x00;
1531 for (uint16_t c = 0; c < keycnt; ++c ) {
1532
1533 if (ukbhit()) {
1534 ch = getchar();
1535 (void)ch;
1536 printf("\naborted via keyboard!\n");
1537 free(keyBlock);
1538 return 0;
1539 }
1540
1541 testpwd = bytes_to_num(keyBlock + 4*c, 4);
1542
1543 PrintAndLog("Testing %08X", testpwd);
1544
1545 if ( !AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, true, testpwd,downlink_mode)) {
1546 PrintAndLog("Aquireing data from device failed. Quitting");
1547 free(keyBlock);
1548 return 0;
1549 }
1550
1551 found = tryDetectModulation();
1552
1553 if ( found ) {
1554 PrintAndLog("Found valid password: [%08X]", testpwd);
1555 free(keyBlock);
1556 return 0;
1557 }
1558 }
1559 PrintAndLog("Password NOT found.");
1560 free(keyBlock);
1561 return 0;
1562 }
1563
1564 // Try to read Block 7, first :)
1565
1566 // incremental pwd range search
1567 start_password = param_get32ex(Cmd, 0, 0, 16);
1568 end_password = param_get32ex(Cmd, 1, 0, 16);
1569
1570 if ( start_password >= end_password ) {
1571 free(keyBlock);
1572 return usage_t55xx_bruteforce();
1573 }
1574 PrintAndLog("Search password range [%08X -> %08X]", start_password, end_password);
1575
1576 uint32_t i = start_password;
1577
1578 while ((!found) && (i <= end_password)) {
1579
1580 printf(".");
1581 fflush(stdout);
1582 if (ukbhit()) {
1583 ch = getchar();
1584 (void)ch;
1585 printf("\naborted via keyboard!\n");
1586 free(keyBlock);
1587 return 0;
1588 }
1589
1590 if (!AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, true, i,downlink_mode)) {
1591 PrintAndLog("Aquireing data from device failed. Quitting");
1592 free(keyBlock);
1593 return 0;
1594 }
1595 found = tryDetectModulation();
1596
1597 if (found) break;
1598 i++;
1599 }
1600
1601 PrintAndLog("");
1602
1603 if (found)
1604 PrintAndLog("Found valid password: [%08x]", i);
1605 else
1606 PrintAndLog("Password NOT found. Last tried: [%08x]", --i);
1607
1608 free(keyBlock);
1609 return 0;
1610 }
1611
1612 int CmdT55xxBruteForce_downlink(const char *Cmd) {
1613
1614 // load a default pwd file.
1615 char buf[9];
1616 char filename[FILE_PATH_SIZE]={0};
1617 int keycnt = 0;
1618 uint8_t downlink_mode = 0;
1619 int ch;
1620 uint8_t stKeyBlock = 20;
1621 uint8_t *keyBlock = NULL, *p = NULL;
1622 uint32_t start_password = 0x00000000; //start password
1623 uint32_t end_password = 0xFFFFFFFF; //end password
1624 bool found = false;
1625 uint8_t cmdp = 0;
1626 int cmd_offset = 0;
1627 int errors = 0;
1628 int len;
1629 bool use_file = false;
1630 bool use_range = false;
1631 bool try_all_dl_modes = false;
1632 uint8_t dl_mode = 0;
1633
1634 keyBlock = calloc(stKeyBlock, 6);
1635 if (keyBlock == NULL) return 1;
1636
1637 PrintAndLog("New Downlink Supprt");
1638
1639 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {
1640 switch(param_getchar(Cmd, cmdp)) {
1641 case 'h':
1642 case 'H':
1643 return usage_t55xx_bruteforce_downlink();
1644 case 'e':
1645 case 'E':
1646 downlink_mode = param_getchar(Cmd, cmdp+1) - '0';
1647 if (downlink_mode == 4) try_all_dl_modes = true;
1648 if (downlink_mode > 3) downlink_mode = 0;
1649 cmdp +=2;
1650 cmd_offset += 4;
1651 PrintAndLog ("DL Mode : %d",downlink_mode);
1652 break;
1653 case 'i':
1654 case 'I':
1655 if (use_range) {
1656 PrintAndLog ("use Range or File");
1657 return 0;
1658 }
1659 use_file = true;
1660 len = strlen(Cmd+2);
1661 if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;
1662 memcpy(filename, Cmd+cmd_offset+2, len);
1663 cmdp += 2;
1664 // PrintAndLog (" File : [%s]",filename);
1665 break;
1666 case 'r':
1667 case 'R':
1668 if (use_file) {
1669 PrintAndLog ("use Range or File");
1670 return 0;
1671 }
1672 use_range = true; // = param_get32ex(Cmd, cmdp+1, 0, 16);
1673 start_password = param_get32ex(Cmd, cmdp+1, 0, 16);
1674 end_password = param_get32ex(Cmd, cmdp+2, 0, 16);
1675 cmdp += 3;
1676 cmd_offset += 20; // 8 + 8 + 1 + 1 + 1
1677 // PrintAndLog (" Range : [%0X] - [%0X]",start_password,end_password);
1678 break;
1679 default:
1680 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
1681 errors = true;
1682 break;
1683 }
1684 }
1685
1686
1687 // if (cmdp == 'i' || cmdp == 'I') {
1688
1689 if (use_file)
1690 {
1691 FILE * f = fopen( filename , "r");
1692
1693 if ( !f ) {
1694 PrintAndLog("File: %s: not found or locked.", filename);
1695 free(keyBlock);
1696 return 1;
1697 }
1698
1699 while( fgets(buf, sizeof(buf), f) ) {
1700 if (strlen(buf) < 8 || buf[7] == '\n') continue;
1701
1702 while (fgetc(f) != '\n' && !feof(f)) ; //goto next line
1703
1704 //The line start with # is comment, skip
1705 if( buf[0]=='#' ) continue;
1706
1707 if (!isxdigit((unsigned char)buf[0])) {
1708 PrintAndLog("File content error. '%s' must include 8 HEX symbols", buf);
1709 continue;
1710 }
1711
1712 buf[8] = 0;
1713
1714 if ( stKeyBlock - keycnt < 2) {
1715 p = realloc(keyBlock, 6*(stKeyBlock+=10));
1716 if (!p) {
1717 PrintAndLog("Cannot allocate memory for defaultKeys");
1718 free(keyBlock);
1719 fclose(f);
1720 return 2;
1721 }
1722 keyBlock = p;
1723 }
1724 memset(keyBlock + 4 * keycnt, 0, 4);
1725 num_to_bytes(strtoll(buf, NULL, 16), 4, keyBlock + 4*keycnt);
1726 PrintAndLog("chk custom pwd[%2d] %08X", keycnt, bytes_to_num(keyBlock + 4*keycnt, 4));
1727 keycnt++;
1728 memset(buf, 0, sizeof(buf));
1729 }
1730 fclose(f);
1731
1732 if (keycnt == 0) {
1733 PrintAndLog("No keys found in file");
1734 free(keyBlock);
1735 return 1;
1736 }
1737 PrintAndLog("Loaded %d keys", keycnt);
1738
1739 // loop
1740 uint64_t testpwd = 0x00;
1741 for (uint16_t c = 0; c < keycnt; ++c ) {
1742
1743 if (ukbhit()) {
1744 ch = getchar();
1745 (void)ch;
1746 printf("\naborted via keyboard!\n");
1747 free(keyBlock);
1748 return 0;
1749 }
1750
1751 testpwd = bytes_to_num(keyBlock + 4*c, 4);
1752
1753 PrintAndLog("Testing %08X", testpwd);
1754
1755 // Try each downlink_mode of asked to
1756 // donwlink_mode will = 0 if > 3 or set to 0, so loop from 0 - 3
1757 for (dl_mode = downlink_mode; dl_mode <= 3; dl_mode++)
1758 {
1759 if ( !AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, true, testpwd,dl_mode)) {
1760 PrintAndLog("Aquireing data from device failed. Quitting");
1761 free(keyBlock);
1762 return 0;
1763 }
1764
1765 found = tryDetectModulation();
1766
1767 if ( found ) {
1768 PrintAndLog("Found valid password: [%08X]", testpwd);
1769 free(keyBlock);
1770 // Add downlink mode to reference.
1771 switch (dl_mode) {
1772 case 0 : PrintAndLog ("Downlink : e 0 - Default/Fixed Bit Length"); break;
1773 case 1 : PrintAndLog ("Downlink : e 1 - Long Leading Reference"); break;
1774 case 2 : PrintAndLog ("Downlink : e 2 - Leading Zero Reference"); break;
1775 case 3 : PrintAndLog ("Downlink : e 3 - 1 of 4 Coding"); break;
1776 }
1777 return 0;
1778 }
1779 if (!try_all_dl_modes) // Exit loop
1780 dl_mode = 4;
1781 }
1782 }
1783 PrintAndLog("Password NOT found.");
1784 free(keyBlock);
1785 return 0;
1786 }
1787
1788 if (use_range)
1789 {
1790 // incremental pwd range search
1791 // start_password = param_get32ex(Cmd, 0, 0, 16);
1792 // end_password = param_get32ex(Cmd, 1, 0, 16);
1793
1794 if ( start_password >= end_password ) {
1795 free(keyBlock);
1796 return usage_t55xx_bruteforce_downlink();
1797 }
1798 PrintAndLog("Search password range [%08X -> %08X]", start_password, end_password);
1799
1800 uint32_t i = start_password;
1801
1802 while ((!found) && (i <= end_password)) {
1803
1804 printf(".");
1805 fflush(stdout);
1806 if (ukbhit()) {
1807 ch = getchar();
1808 (void)ch;
1809 printf("\naborted via keyboard!\n");
1810 free(keyBlock);
1811 return 0;
1812 }
1813
1814 if (!AquireData(T55x7_PAGE0, T55x7_CONFIGURATION_BLOCK, true, i,downlink_mode)) {
1815 PrintAndLog("Aquireing data from device failed. Quitting");
1816 free(keyBlock);
1817 return 0;
1818 }
1819 found = tryDetectModulation();
1820
1821 if (found) break;
1822 i++;
1823 }
1824
1825 PrintAndLog("");
1826
1827 if (found) {
1828 PrintAndLog("Found valid password: [%08x]", i);
1829 // Add downlink mode to reference.
1830 switch (downlink_mode) {
1831 case 0 : PrintAndLog ("Downlink : e 0 - Default/Fixed Bit Length"); break;
1832 case 1 : PrintAndLog ("Downlink : e 1 - Long Leading Reference"); break;
1833 case 2 : PrintAndLog ("Downlink : e 2 - Leading Zero Reference"); break;
1834 case 3 : PrintAndLog ("Downlink : e 3 - 1 of 4 Coding"); break;
1835 }
1836 }
1837 else
1838 PrintAndLog("Password NOT found. Last tried: [%08x]", --i);
1839
1840 free(keyBlock);
1841 }
1842 return 0;
1843 }
1844
1845 // note length of data returned is different for different chips.
1846 // some return all page 1 (64 bits) and others return just that block (32 bits)
1847 // unfortunately the 64 bits makes this more likely to get a false positive...
1848 bool tryDetectP1(bool getData) {
1849 uint8_t preamble[] = {1,1,1,0,0,0,0,0,0,0,0,1,0,1,0,1};
1850 size_t startIdx = 0;
1851 uint8_t fc1 = 0, fc2 = 0, ans = 0;
1852 int clk = 0, firstClockEdge = 0;
1853 bool st = true;
1854
1855 if ( getData ) {
1856 if ( !AquireData(T55x7_PAGE1, 1, false, 0,0) )
1857 return false;
1858 }
1859
1860 // try fsk clock detect. if successful it cannot be any other type of modulation... (in theory...)
1861 ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false, &firstClockEdge);
1862 if (ans && ((fc1==10 && fc2==8) || (fc1==8 && fc2==5))) {
1863 if ( FSKrawDemod("0 0", false) &&
1864 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1865 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1866 return true;
1867 }
1868 if ( FSKrawDemod("0 1", false) &&
1869 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1870 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1871 return true;
1872 }
1873 return false;
1874 }
1875
1876 // try psk clock detect. if successful it cannot be any other type of modulation... (in theory...)
1877 clk = GetPskClock("", false, false);
1878 if (clk>0) {
1879 // allow undo
1880 // save_restoreGB(1);
1881 // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)
1882 //CmdLtrim("160");
1883 if ( PSKDemod("0 0 6", false) &&
1884 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1885 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1886 //save_restoreGB(0);
1887 return true;
1888 }
1889 if ( PSKDemod("0 1 6", false) &&
1890 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1891 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1892 //save_restoreGB(0);
1893 return true;
1894 }
1895 // PSK2 - needs a call to psk1TOpsk2.
1896 if ( PSKDemod("0 0 6", false)) {
1897 psk1TOpsk2(DemodBuffer, DemodBufferLen);
1898 if (preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1899 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1900 //save_restoreGB(0);
1901 return true;
1902 }
1903 } // inverse waves does not affect PSK2 demod
1904 //undo trim samples
1905 //save_restoreGB(0);
1906 // no other modulation clocks = 2 or 4 so quit searching
1907 if (fc1 != 8) return false;
1908 }
1909
1910 // try ask clock detect. it could be another type even if successful.
1911 clk = GetAskClock("", false, false);
1912 if (clk>0) {
1913 if ( ASKDemod_ext("0 0 1", false, false, 1, &st) &&
1914 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1915 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1916 return true;
1917 }
1918 st = true;
1919 if ( ASKDemod_ext("0 1 1", false, false, 1, &st) &&
1920 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1921 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1922 return true;
1923 }
1924 if ( ASKbiphaseDemod("0 0 0 2", false) &&
1925 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1926 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1927 return true;
1928 }
1929 if ( ASKbiphaseDemod("0 0 1 2", false) &&
1930 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1931 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1932 return true;
1933 }
1934 }
1935
1936 // try NRZ clock detect. it could be another type even if successful.
1937 clk = GetNrzClock("", false, false); //has the most false positives :(
1938 if (clk>0) {
1939 if ( NRZrawDemod("0 0 1", false) &&
1940 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1941 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1942 return true;
1943 }
1944 if ( NRZrawDemod("0 1 1", false) &&
1945 preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) &&
1946 (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
1947 return true;
1948 }
1949 }
1950 return false;
1951 }
1952 // does this need to be a callable command?
1953 int CmdT55xxDetectPage1(const char *Cmd){
1954 bool errors = false;
1955 bool useGB = false;
1956 bool usepwd = false;
1957 uint32_t password = 0;
1958 uint8_t cmdp = 0;
1959
1960 while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {
1961 switch(param_getchar(Cmd, cmdp)) {
1962 case 'h':
1963 case 'H':
1964 return usage_t55xx_detectP1();
1965 case 'p':
1966 case 'P':
1967 password = param_get32ex(Cmd, cmdp+1, 0, 16);
1968 usepwd = true;
1969 cmdp += 2;
1970 break;
1971 case '1':
1972 // use Graphbuffer data
1973 useGB = true;
1974 cmdp++;
1975 break;
1976 default:
1977 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
1978 errors = true;
1979 break;
1980 }
1981 }
1982 if (errors) return usage_t55xx_detectP1();
1983
1984 if ( !useGB ) {
1985 if ( !AquireData(T55x7_PAGE1, 1, usepwd, password,0) )
1986 return false;
1987 }
1988 bool success = tryDetectP1(false);
1989 if (success) PrintAndLog("T55xx chip found!");
1990 return success;
1991 }
1992
1993 static command_t CommandTable[] = {
1994 {"help", CmdHelp, 1, "This help"},
1995 {"bruteforce",CmdT55xxBruteForce,0, "<start password> <end password> [i <*.dic>] Simple bruteforce attack to find password"},
1996 {"bruteforcedl",CmdT55xxBruteForce_downlink,0, "r <start password> <end password> [i <*.dic>] [e <downlink>] Simple bruteforce attack to find password"},
1997 {"config", CmdT55xxSetConfig, 1, "Set/Get T55XX configuration (modulation, inverted, offset, rate)"},
1998 {"detect", CmdT55xxDetect, 1, "[1] Try detecting the tag modulation from reading the configuration block."},
1999 {"p1detect", CmdT55xxDetectPage1,1, "[1] Try detecting if this is a t55xx tag by reading page 1"},
2000 {"read", CmdT55xxReadBlock, 0, "b <block> p [password] [o] [1] -- Read T55xx block data. Optional [p password], [override], [page1]"},
2001 {"resetread", CmdResetRead, 0, "Send Reset Cmd then lf read the stream to attempt to identify the start of it (needs a demod and/or plot after)"},
2002 {"write", CmdT55xxWriteBlock,0, "b <block> d <data> p [password] [1] -- Write T55xx block data. Optional [p password], [page1]"},
2003 {"trace", CmdT55xxReadTrace, 1, "[1] Show T55x7 traceability data (page 1/ blk 0-1)"},
2004 {"info", CmdT55xxInfo, 1, "[1] Show T55x7 configuration data (page 0/ blk 0)"},
2005 {"dump", CmdT55xxDump, 0, "[password] [o] Dump T55xx card block 0-7. Optional [password], [override]"},
2006 {"special", special, 0, "Show block changes with 64 different offsets"},
2007 {"wakeup", CmdT55xxWakeUp, 0, "Send AOR wakeup command"},
2008 {"wipe", CmdT55xxWipe, 0, "[q] Wipe a T55xx tag and set defaults (will destroy any data on tag)"},
2009 {NULL, NULL, 0, NULL}
2010 };
2011
2012 int CmdLFT55XX(const char *Cmd) {
2013 CmdsParse(CommandTable, Cmd);
2014 return 0;
2015 }
2016
2017 int CmdHelp(const char *Cmd) {
2018 CmdsHelp(CommandTable);
2019 return 0;
2020 }
Impressum, Datenschutz