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