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