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