]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlft55xx.c
Merge branch 'master' of https://github.com/Proxmark/proxmark3
[proxmark3-svn] / client / cmdlft55xx.c
1 //-----------------------------------------------------------------------------
2 //
3 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
4 // at your option, any later version. See the LICENSE.txt file for the text of
5 // the license.
6 //-----------------------------------------------------------------------------
7 // Low frequency T55xx commands
8 //-----------------------------------------------------------------------------
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <inttypes.h>
13 #include "proxmark3.h"
14 #include "ui.h"
15 #include "graph.h"
16 #include "cmdmain.h"
17 #include "cmdparser.h"
18 #include "cmddata.h"
19 #include "cmdlf.h"
20 #include "cmdlft55xx.h"
21 #include "util.h"
22 #include "data.h"
23 #include "lfdemod.h"
24
25 #define LF_TRACE_BUFF_SIZE 20000 // 32 x 32 x 10 (32 bit times numofblock (7), times clock skip..)
26 #define LF_BITSSTREAM_LEN 1000 // more then 1000 bits shouldn't happend.. 8block * 4 bytes * 8bits =
27
28 int usage_t55xx_rd(){
29 PrintAndLog("Usage: lf t55xx rd <block> <password>");
30 PrintAndLog(" <block>, block number to read. Between 0-7");
31 PrintAndLog(" <password>, OPTIONAL password (8 hex characters)");
32 PrintAndLog("");
33 PrintAndLog(" sample: lf t55xx rd 0 = try reading data from block 0");
34 PrintAndLog(" : lf t55xx rd 0 feedbeef = try reading data from block 0 using password");
35 PrintAndLog("");
36 return 0;
37 }
38 int usage_t55xx_wr(){
39 PrintAndLog("Usage: lf t55xx wr <block> <data> [password]");
40 PrintAndLog(" <block>, block number to read. Between 0-7");
41 PrintAndLog(" <data>, 4 bytes of data to write (8 hex characters)");
42 PrintAndLog(" [password], OPTIONAL password 4bytes (8 hex characters)");
43 PrintAndLog("");
44 PrintAndLog(" sample: lf t55xx wd 3 11223344 = try writing data 11223344 to block 3");
45 PrintAndLog(" : lf t55xx wd 3 11223344 feedbeef = try writing data 11223344 to block 3 using password feedbeef");
46 PrintAndLog("");
47 return 0;
48 }
49 int usage_t55xx_trace() {
50 PrintAndLog("Usage: lf t55xx trace [graph buffer data]");
51 PrintAndLog(" [graph buffer data], if set, use Graphbuffer otherwise read data from tag.");
52 PrintAndLog("");
53 PrintAndLog(" sample: lf t55xx trace");
54 PrintAndLog(" : lf t55xx trace 1");
55 PrintAndLog("");
56 return 0;
57 }
58 int usage_t55xx_info() {
59 PrintAndLog("Usage: lf t55xx info [graph buffer data]");
60 PrintAndLog(" [graph buffer data], if set, use Graphbuffer otherwise read data from tag.");
61 PrintAndLog("");
62 PrintAndLog(" sample: lf t55xx info");
63 PrintAndLog(" : lf t55xx info 1");
64 PrintAndLog("");
65 return 0;
66 }
67
68 int usage_t55xx_dump(){
69 PrintAndLog("Usage: lf t55xx dump <password>");
70 PrintAndLog(" <password>, OPTIONAL password 4bytes (8 hex characters)");
71 PrintAndLog("");
72 PrintAndLog(" sample: lf t55xx dump");
73 PrintAndLog(" : lf t55xx dump feedbeef");
74 PrintAndLog("");
75 return 0;
76 }
77 static int CmdHelp(const char *Cmd);
78
79 int CmdReadBlk(const char *Cmd)
80 {
81 int invert = 0;
82 int clk = 0;
83 int block = -1;
84 int password = 0xFFFFFFFF; //default to blank Block 7
85 int errCnt;
86 size_t bitlen;
87 int maxErr = 100;
88 uint8_t askAmp = 0;
89 uint32_t blockData;
90 uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00};
91
92
93 char cmdp = param_getchar(Cmd, 0);
94 if (cmdp == 'h' || cmdp == 'H') {
95 usage_t55xx_rd();
96 return 0;
97 }
98
99 int res = sscanf(Cmd, "%d %x", &block, &password);
100
101 if ( res < 1 || res > 2 ){
102 usage_t55xx_rd();
103 return 1;
104 }
105
106 if ((block < 0) | (block > 7)) {
107 PrintAndLog("Block must be between 0 and 7");
108 return 1;
109 }
110
111 UsbCommand c = {CMD_T55XX_READ_BLOCK, {0, block, 0}};
112 c.d.asBytes[0] = 0x0;
113
114 //Password mode
115 if ( res == 2 ) {
116 c.arg[2] = password;
117 c.d.asBytes[0] = 0x1;
118 }
119
120 SendCommand(&c);
121 if ( !WaitForResponseTimeout(CMD_ACK,NULL,1500) ) {
122 PrintAndLog("command execution time out");
123 return 2;
124 }
125
126 CmdSamples("12000");
127
128 bitlen = getFromGraphBuf(bits);
129
130 errCnt = askrawdemod(bits, &bitlen, &clk, &invert, maxErr, askAmp);
131
132 //throw away static - allow 1 and -1 (in case of threshold command first)
133 if ( errCnt == -1 || bitlen < 16 ){
134 PrintAndLog("no data found");
135 if (g_debugMode)
136 PrintAndLog("errCnt: %d, bitlen: %d, clk: %d, invert: %d", errCnt, bitlen, clk, invert);
137 return 3;
138 }
139 if (g_debugMode)
140 PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d", clk, invert, bitlen);
141
142 //move bits back to DemodBuffer
143 setDemodBuf(bits, bitlen, 0);
144 printBitStream(bits,bitlen);
145
146 // bits has the manchester encoded data.
147 errCnt = manrawdecode(bits, &bitlen);
148 if ( errCnt == -1 || bitlen < 16 ){
149 PrintAndLog("no data found");
150 if (g_debugMode)
151 PrintAndLog("errCnt: %d, bitlen: %d, clk: %d, invert: %d", errCnt, bitlen, clk, invert);
152 return 4;
153 }
154
155 blockData = PackBits(0, 32, bits);
156
157 if ( block < 0)
158 PrintAndLog(" Decoded : 0x%08X %s", blockData, sprint_bin(bits,32) );
159 else
160 PrintAndLog(" Block %d : 0x%08X %s", block, blockData, sprint_bin(bits,32) );
161
162 return 0;
163 }
164
165 int CmdWriteBlk(const char *Cmd)
166 {
167 int block = 8; //default to invalid block
168 int data = 0xFFFFFFFF; //default to blank Block
169 int password = 0xFFFFFFFF; //default to blank Block 7
170
171 char cmdp = param_getchar(Cmd, 0);
172 if (cmdp == 'h' || cmdp == 'H') {
173 usage_t55xx_wr();
174 return 0;
175 }
176
177 int res = sscanf(Cmd, "%d %x %x",&block, &data, &password);
178
179 if ( res < 2 || res > 3) {
180 usage_t55xx_wr();
181 return 1;
182 }
183
184 if (block > 7) {
185 PrintAndLog("Block must be between 0 and 7");
186 return 1;
187 }
188
189 UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {data, block, 0}};
190 c.d.asBytes[0] = 0x0;
191
192 if (res == 2) {
193 PrintAndLog("Writing block %d data %08X", block, data);
194 } else {
195 //Password mode
196 c.arg[2] = password;
197 c.d.asBytes[0] = 0x1;
198 PrintAndLog("Writing block %d data %08X password %08X", block, data, password);
199 }
200
201 SendCommand(&c);
202 return 0;
203 }
204
205 int CmdReadTrace(const char *Cmd)
206 {
207 char cmdp = param_getchar(Cmd, 0);
208
209 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {
210 usage_t55xx_trace();
211 return 0;
212 }
213
214 if ( strlen(Cmd)==0){
215
216 UsbCommand c = {CMD_T55XX_READ_TRACE, {0, 0, 0}};
217 SendCommand(&c);
218 WaitForResponse(CMD_ACK, NULL);
219
220 uint8_t data[LF_TRACE_BUFF_SIZE] = {0x00};
221
222 GetFromBigBuf(data,LF_TRACE_BUFF_SIZE,0); //3560 -- should be offset..
223 WaitForResponseTimeout(CMD_ACK,NULL, 1500);
224
225 for (int j = 0; j < LF_TRACE_BUFF_SIZE; j++) {
226 GraphBuffer[j] = ((int)data[j]);
227 }
228 GraphTraceLen = LF_TRACE_BUFF_SIZE;
229 }
230
231 uint8_t bits[LF_BITSSTREAM_LEN] = {0x00};
232 uint8_t * bitstream = bits;
233
234 manchester_decode(GraphBuffer, LF_TRACE_BUFF_SIZE, bitstream, LF_BITSSTREAM_LEN);
235 RepaintGraphWindow();
236
237 uint8_t si = 5;
238 uint32_t bl0 = PackBits(si, 32, bitstream);
239 uint32_t bl1 = PackBits(si+32, 32, bitstream);
240
241 uint32_t acl = PackBits(si, 8, bitstream); si += 8;
242 uint32_t mfc = PackBits(si, 8, bitstream); si += 8;
243 uint32_t cid = PackBits(si, 5, bitstream); si += 5;
244 uint32_t icr = PackBits(si, 3, bitstream); si += 3;
245 uint32_t year = PackBits(si, 4, bitstream); si += 4;
246 uint32_t quarter = PackBits(si, 2, bitstream); si += 2;
247 uint32_t lotid = PackBits(si, 12, bitstream); si += 12;
248 uint32_t wafer = PackBits(si, 5, bitstream); si += 5;
249 uint32_t dw = PackBits(si, 15, bitstream);
250
251 PrintAndLog("");
252 PrintAndLog("-- T55xx Trace Information ----------------------------------");
253 PrintAndLog("-------------------------------------------------------------");
254 PrintAndLog(" ACL Allocation class (ISO/IEC 15963-1) : 0x%02X (%d)", acl, acl);
255 PrintAndLog(" MFC Manufacturer ID (ISO/IEC 7816-6) : 0x%02X (%d)", mfc, mfc);
256 PrintAndLog(" CID : 0x%02X (%d)", cid, cid);
257 PrintAndLog(" ICR IC Revision : %d",icr );
258 PrintAndLog(" Manufactured");
259 PrintAndLog(" Year/Quarter : %d/%d",2000+year, quarter );
260 PrintAndLog(" Lot ID : %d", lotid );
261 PrintAndLog(" Wafer number : %d", wafer);
262 PrintAndLog(" Die Number : %d", dw);
263 PrintAndLog("-------------------------------------------------------------");
264 PrintAndLog(" Raw Data - Page 1");
265 PrintAndLog(" Block 0 : 0x%08X %s", bl0, sprint_bin(bitstream+5,32) );
266 PrintAndLog(" Block 0 : 0x%08X %s", bl1, sprint_bin(bitstream+37,32) );
267 PrintAndLog("-------------------------------------------------------------");
268 /*
269 TRACE - BLOCK O
270 Bits Definition HEX
271 1-8 ACL Allocation class (ISO/IEC 15963-1) 0xE0
272 9-16 MFC Manufacturer ID (ISO/IEC 7816-6) 0x15 Atmel Corporation
273 17-21 CID 0x1 = Atmel ATA5577M1 0x2 = Atmel ATA5577M2
274 22-24 ICR IC revision
275 25-28 YEAR (BCD encoded) 9 (= 2009)
276 29-30 QUARTER 1,2,3,4
277 31-32 LOT ID
278
279 TRACE - BLOCK 1
280 1-12 LOT ID
281 13-17 Wafer number
282 18-32 DW, die number sequential
283 */
284
285 return 0;
286 }
287
288 int CmdInfo(const char *Cmd){
289 /*
290 Page 0 Block 0 Configuration data.
291 Normal mode
292 Extended mode
293 */
294 char cmdp = param_getchar(Cmd, 0);
295
296 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {
297 usage_t55xx_info();
298 return 0;
299 } else {
300 CmdReadBlk("0");
301 }
302
303 uint8_t bits[LF_BITSSTREAM_LEN] = {0x00};
304
305 manchester_decode(GraphBuffer, LF_TRACE_BUFF_SIZE, bits, LF_BITSSTREAM_LEN);
306
307 uint8_t si = 5;
308 uint32_t bl0 = PackBits(si, 32, bits);
309
310 uint32_t safer = PackBits(si, 4, bits); si += 4;
311 uint32_t resv = PackBits(si, 7, bits); si += 7;
312 uint32_t dbr = PackBits(si, 3, bits); si += 3;
313 uint32_t extend = PackBits(si, 1, bits); si += 1;
314 uint32_t datamodulation = PackBits(si, 5, bits); si += 5;
315 uint32_t pskcf = PackBits(si, 2, bits); si += 2;
316 uint32_t aor = PackBits(si, 1, bits); si += 1;
317 uint32_t otp = PackBits(si, 1, bits); si += 1;
318 uint32_t maxblk = PackBits(si, 3, bits); si += 3;
319 uint32_t pwd = PackBits(si, 1, bits); si += 1;
320 uint32_t sst = PackBits(si, 1, bits); si += 1;
321 uint32_t fw = PackBits(si, 1, bits); si += 1;
322 uint32_t inv = PackBits(si, 1, bits); si += 1;
323 uint32_t por = PackBits(si, 1, bits); si += 1;
324
325 PrintAndLog("");
326 PrintAndLog("-- T55xx Configuration & Tag Information --------------------");
327 PrintAndLog("-------------------------------------------------------------");
328 PrintAndLog(" Safer key : %s", GetSaferStr(safer));
329 PrintAndLog(" reserved : %d", resv);
330 PrintAndLog(" Data bit rate : %s", GetBitRateStr(dbr));
331 PrintAndLog(" eXtended mode : %s", (extend) ? "Yes - Warning":"No");
332 PrintAndLog(" Modulation : %s", GetModulationStr(datamodulation) );
333 PrintAndLog(" PSK clock freq : %d", pskcf);
334 PrintAndLog(" AOR - Answer on Request : %s", (aor) ? "Yes":"No");
335 PrintAndLog(" OTP - One Time Pad : %s", (otp) ? "Yes - Warning":"No" );
336 PrintAndLog(" Max block : %d", maxblk);
337 PrintAndLog(" Password mode : %s", (pwd) ? "Yes":"No");
338 PrintAndLog(" Sequence Start Terminator : %s", (sst) ? "Yes":"No");
339 PrintAndLog(" Fast Write : %s", (fw) ? "Yes":"No");
340 PrintAndLog(" Inverse data : %s", (inv) ? "Yes":"No");
341 PrintAndLog(" POR-Delay : %s", (por) ? "Yes":"No");
342 PrintAndLog("-------------------------------------------------------------");
343 PrintAndLog(" Raw Data - Page 0");
344 PrintAndLog(" Block 0 : 0x%08X %s", bl0, sprint_bin(bits+5,32) );
345 PrintAndLog("-------------------------------------------------------------");
346
347 return 0;
348 }
349
350 int CmdDump(const char *Cmd){
351
352 char s[20] = {0x00};
353 uint8_t pwd[4] = {0x00};
354
355 char cmdp = param_getchar(Cmd, 0);
356 if ( cmdp == 'h' || cmdp == 'H') {
357 usage_t55xx_dump();
358 return 0;
359 }
360
361 bool hasPwd = ( strlen(Cmd) > 0);
362 if ( hasPwd ){
363 if (param_gethex(Cmd, 0, pwd, 8)) {
364 PrintAndLog("password must include 8 HEX symbols");
365 return 1;
366 }
367 }
368
369 for ( int i = 0; i <8; ++i){
370 memset(s,0,sizeof(s));
371 if ( hasPwd ) {
372 sprintf(s,"%d %02x%02x%02x%02x", i, pwd[0],pwd[1],pwd[2],pwd[3]);
373 } else {
374 sprintf(s,"%d", i);
375 }
376 CmdReadBlk(s);
377 }
378 return 0;
379 }
380
381 int CmdIceFsk(const char *Cmd){
382
383 if (!HasGraphData()) return 0;
384
385 iceFsk3(GraphBuffer, LF_TRACE_BUFF_SIZE);
386 RepaintGraphWindow();
387 return 0;
388 }
389 int CmdIceManchester(const char *Cmd){
390 ManchesterDemod( -1);
391 return 0;
392 }
393 int ManchesterDemod(int blockNum){
394
395 if (!HasGraphData()) return 0;
396
397 uint8_t sizebyte = 32;
398 // the value 5 was selected during empirical studies of the decoded data. Some signal noise to skip.
399 uint8_t offset = 5;
400 uint32_t blockData;
401 uint8_t bits[LF_BITSSTREAM_LEN] = {0x00};
402 uint8_t * bitstream = bits;
403
404 manchester_decode(GraphBuffer, LF_TRACE_BUFF_SIZE, bits, LF_BITSSTREAM_LEN);
405 blockData = PackBits(offset, sizebyte, bits);
406
407 if ( blockNum < 0)
408 PrintAndLog(" Decoded : 0x%08X %s", blockData, sprint_bin(bitstream+offset,sizebyte) );
409 else
410 PrintAndLog(" Block %d : 0x%08X %s", blockNum, blockData, sprint_bin(bitstream+offset,sizebyte) );
411
412 return 0;
413 }
414
415 char * GetBitRateStr(uint32_t id){
416 static char buf[40];
417 char *retStr = buf;
418 switch (id){
419 case 0:
420 sprintf(retStr,"%d - RF/8",id);
421 break;
422 case 1:
423 sprintf(retStr,"%d - RF/16",id);
424 break;
425 case 2:
426 sprintf(retStr,"%d - RF/32",id);
427 break;
428 case 3:
429 sprintf(retStr,"%d - RF/40",id);
430 break;
431 case 4:
432 sprintf(retStr,"%d - RF/50",id);
433 break;
434 case 5:
435 sprintf(retStr,"%d - RF/64",id);
436 break;
437 case 6:
438 sprintf(retStr,"%d - RF/100",id);
439 break;
440 case 7:
441 sprintf(retStr,"%d - RF/128",id);
442 break;
443 default:
444 sprintf(retStr,"%d - (Unknown)",id);
445 break;
446 }
447
448 return buf;
449 }
450
451 char * GetSaferStr(uint32_t id){
452 static char buf[40];
453 char *retStr = buf;
454
455 sprintf(retStr,"%d",id);
456 if (id == 6) {
457 sprintf(retStr,"%d - pasdwd",id);
458 }
459 if (id == 9 ){
460 sprintf(retStr,"%d - testmode ",id);
461 }
462
463 return buf;
464 }
465 char * GetModulationStr( uint32_t id){
466 static char buf[40];
467 char *retStr = buf;
468
469 switch (id){
470 case 0:
471 sprintf(retStr,"%d - DIRECT (ASK/NRZ)",id);
472 break;
473 case 1:
474 sprintf(retStr,"%d - PSK 1 phase change when input changes",id);
475 break;
476 case 2:
477 sprintf(retStr,"%d - PSK 2 phase change on bitclk if input high",id);
478 break;
479 case 3:
480 sprintf(retStr,"%d - PSK 3 phase change on rising edge of input",id);
481 break;
482 case 4:
483 sprintf(retStr,"%d - FSK 1 RF/8 RF/5",id);
484 break;
485 case 5:
486 sprintf(retStr,"%d - FSK 2 RF/8 RF/10",id);
487 break;
488 case 6:
489 sprintf(retStr,"%d - FSK 1a RF/5 RF/8",id);
490 break;
491 case 7:
492 sprintf(retStr,"%d - FSK 2a RF/10 RF/8",id);
493 break;
494 case 8:
495 sprintf(retStr,"%d - Manschester",id);
496 break;
497 case 16:
498 sprintf(retStr,"%d - Biphase",id);
499 break;
500 case 17:
501 sprintf(retStr,"%d - Reserved",id);
502 break;
503 default:
504 sprintf(retStr,"0x%02X (Unknown)",id);
505 break;
506 }
507 return buf;
508 }
509
510
511 uint32_t PackBits(uint8_t start, uint8_t len, uint8_t* bits){
512
513 int i = start;
514 int j = len-1;
515 if (len > 32) {
516 return 0;
517 }
518 uint32_t tmp = 0;
519 for (; j >= 0; --j, ++i){
520 tmp |= bits[i] << j;
521 }
522 return tmp;
523 }
524
525 static command_t CommandTable[] =
526 {
527 {"help", CmdHelp, 1, "This help"},
528 {"rd", CmdReadBlk, 0, "<block> [password] -- Read T55xx block data (page 0) [optional password]"},
529 {"wr", CmdWriteBlk, 0, "<block> <data> [password] -- Write T55xx block data (page 0) [optional password]"},
530 {"trace", CmdReadTrace, 0, "[1] Read T55xx traceability data (page 1/ blk 0-1)"},
531 {"info", CmdInfo, 0, "[1] Read T55xx configuration data (page 0/ blk 0)"},
532 {"dump", CmdDump, 0, "[password] Dump T55xx card block 0-7. [optional password]"},
533 {"man", CmdIceManchester, 0, "Manchester demod (with SST)"},
534 {NULL, NULL, 0, NULL}
535 };
536
537 int CmdLFT55XX(const char *Cmd)
538 {
539 CmdsParse(CommandTable, Cmd);
540 return 0;
541 }
542
543 int CmdHelp(const char *Cmd)
544 {
545 CmdsHelp(CommandTable);
546 return 0;
547 }
Impressum, Datenschutz