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