]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfem4x.c
ff2ae5b0f527d3532167f3bf15e9106859bfded7
[proxmark3-svn] / client / cmdlfem4x.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Low frequency EM4x commands
9 //-----------------------------------------------------------------------------
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <inttypes.h>
14 #include "cmdlfem4x.h"
15 #include "proxmark3.h"
16 #include "ui.h"
17 #include "util.h"
18 #include "graph.h"
19 #include "cmdparser.h"
20 #include "cmddata.h"
21 #include "cmdlf.h"
22 #include "cmdmain.h"
23 #include "lfdemod.h"
24 #include "protocols.h"
25 #include "util_posix.h"
26
27 uint64_t g_em410xId=0;
28
29 static int CmdHelp(const char *Cmd);
30 void ConstructEM410xEmulGraph(const char *uid,const uint8_t clock);
31
32 int CmdEMdemodASK(const char *Cmd)
33 {
34 char cmdp = param_getchar(Cmd, 0);
35 int findone = (cmdp == '1') ? 1 : 0;
36 UsbCommand c={CMD_EM410X_DEMOD};
37 c.arg[0]=findone;
38 SendCommand(&c);
39 return 0;
40 }
41
42 //by marshmellow
43 //print 64 bit EM410x ID in multiple formats
44 void printEM410x(uint32_t hi, uint64_t id)
45 {
46 if (id || hi){
47 uint64_t iii=1;
48 uint64_t id2lo=0;
49 uint32_t ii=0;
50 uint32_t i=0;
51 for (ii=5; ii>0;ii--){
52 for (i=0;i<8;i++){
53 id2lo=(id2lo<<1LL) | ((id & (iii << (i+((ii-1)*8)))) >> (i+((ii-1)*8)));
54 }
55 }
56 if (hi){
57 //output 88 bit em id
58 PrintAndLog("\nEM TAG ID : %06X%016" PRIX64, hi, id);
59 } else{
60 //output 40 bit em id
61 PrintAndLog("\nEM TAG ID : %010" PRIX64, id);
62 PrintAndLog("\nPossible de-scramble patterns");
63 PrintAndLog("Unique TAG ID : %010" PRIX64, id2lo);
64 PrintAndLog("HoneyWell IdentKey {");
65 PrintAndLog("DEZ 8 : %08" PRIu64,id & 0xFFFFFF);
66 PrintAndLog("DEZ 10 : %010" PRIu64,id & 0xFFFFFFFF);
67 PrintAndLog("DEZ 5.5 : %05lld.%05" PRIu64,(id>>16LL) & 0xFFFF,(id & 0xFFFF));
68 PrintAndLog("DEZ 3.5A : %03lld.%05" PRIu64,(id>>32ll),(id & 0xFFFF));
69 PrintAndLog("DEZ 3.5B : %03lld.%05" PRIu64,(id & 0xFF000000) >> 24,(id & 0xFFFF));
70 PrintAndLog("DEZ 3.5C : %03lld.%05" PRIu64,(id & 0xFF0000) >> 16,(id & 0xFFFF));
71 PrintAndLog("DEZ 14/IK2 : %014" PRIu64,id);
72 PrintAndLog("DEZ 15/IK3 : %015" PRIu64,id2lo);
73 PrintAndLog("DEZ 20/ZK : %02" PRIu64 "%02" PRIu64 "%02" PRIu64 "%02" PRIu64 "%02" PRIu64 "%02" PRIu64 "%02" PRIu64 "%02" PRIu64 "%02" PRIu64 "%02" PRIu64,
74 (id2lo & 0xf000000000) >> 36,
75 (id2lo & 0x0f00000000) >> 32,
76 (id2lo & 0x00f0000000) >> 28,
77 (id2lo & 0x000f000000) >> 24,
78 (id2lo & 0x0000f00000) >> 20,
79 (id2lo & 0x00000f0000) >> 16,
80 (id2lo & 0x000000f000) >> 12,
81 (id2lo & 0x0000000f00) >> 8,
82 (id2lo & 0x00000000f0) >> 4,
83 (id2lo & 0x000000000f)
84 );
85 uint64_t paxton = (((id>>32) << 24) | (id & 0xffffff)) + 0x143e00;
86 PrintAndLog("}\nOther : %05" PRIu64 "_%03" PRIu64 "_%08" PRIu64 "",(id&0xFFFF),((id>>16LL) & 0xFF),(id & 0xFFFFFF));
87 PrintAndLog("Pattern Paxton : %" PRIu64 " [0x%" PRIX64 "]", paxton, paxton);
88
89 uint32_t p1id = (id & 0xFFFFFF);
90 uint8_t arr[32] = {0x00};
91 int i =0;
92 int j = 23;
93 for (; i < 24; ++i, --j ){
94 arr[i] = (p1id >> i) & 1;
95 }
96
97 uint32_t p1 = 0;
98
99 p1 |= arr[23] << 21;
100 p1 |= arr[22] << 23;
101 p1 |= arr[21] << 20;
102 p1 |= arr[20] << 22;
103
104 p1 |= arr[19] << 18;
105 p1 |= arr[18] << 16;
106 p1 |= arr[17] << 19;
107 p1 |= arr[16] << 17;
108
109 p1 |= arr[15] << 13;
110 p1 |= arr[14] << 15;
111 p1 |= arr[13] << 12;
112 p1 |= arr[12] << 14;
113
114 p1 |= arr[11] << 6;
115 p1 |= arr[10] << 2;
116 p1 |= arr[9] << 7;
117 p1 |= arr[8] << 1;
118
119 p1 |= arr[7] << 0;
120 p1 |= arr[6] << 8;
121 p1 |= arr[5] << 11;
122 p1 |= arr[4] << 3;
123
124 p1 |= arr[3] << 10;
125 p1 |= arr[2] << 4;
126 p1 |= arr[1] << 5;
127 p1 |= arr[0] << 9;
128 PrintAndLog("Pattern 1 : %d [0x%X]", p1, p1);
129
130 uint16_t sebury1 = id & 0xFFFF;
131 uint8_t sebury2 = (id >> 16) & 0x7F;
132 uint32_t sebury3 = id & 0x7FFFFF;
133 PrintAndLog("Pattern Sebury : %d %d %d [0x%X 0x%X 0x%X]", sebury1, sebury2, sebury3, sebury1, sebury2, sebury3);
134 }
135 }
136 return;
137 }
138
139 /* Read the ID of an EM410x tag.
140 * Format:
141 * 1111 1111 1 <-- standard non-repeatable header
142 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
143 * ....
144 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
145 * 0 <-- stop bit, end of tag
146 */
147 int AskEm410xDecode(bool verbose, uint32_t *hi, uint64_t *lo )
148 {
149 size_t idx = 0;
150 uint8_t BitStream[512]={0};
151 size_t BitLen = sizeof(BitStream);
152 if ( !getDemodBuf(BitStream, &BitLen) ) return 0;
153
154 if (Em410xDecode(BitStream, &BitLen, &idx, hi, lo)) {
155 //set GraphBuffer for clone or sim command
156 setDemodBuf(DemodBuffer, (BitLen==40) ? 64 : 128, idx+1);
157 setClockGrid(g_DemodClock, g_DemodStartIdx + ((idx+1)*g_DemodClock));
158
159 if (g_debugMode) {
160 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen);
161 printDemodBuff();
162 }
163 if (verbose) {
164 PrintAndLog("EM410x pattern found: ");
165 printEM410x(*hi, *lo);
166 g_em410xId = *lo;
167 }
168 return 1;
169 }
170 return 0;
171 }
172
173 //askdemod then call Em410xdecode
174 int AskEm410xDemod(const char *Cmd, uint32_t *hi, uint64_t *lo, bool verbose)
175 {
176 bool st = true;
177 if (!ASKDemod_ext(Cmd, false, false, 1, &st)) return 0;
178 return AskEm410xDecode(verbose, hi, lo);
179 }
180
181 //by marshmellow
182 //takes 3 arguments - clock, invert and maxErr as integers
183 //attempts to demodulate ask while decoding manchester
184 //prints binary found and saves in graphbuffer for further commands
185 int CmdAskEM410xDemod(const char *Cmd)
186 {
187 char cmdp = param_getchar(Cmd, 0);
188 if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') {
189 PrintAndLog("Usage: lf em 410xdemod [clock] <0|1> [maxError]");
190 PrintAndLog(" [set clock as integer] optional, if not set, autodetect.");
191 PrintAndLog(" <invert>, 1 for invert output");
192 PrintAndLog(" [set maximum allowed errors], default = 100.");
193 PrintAndLog("");
194 PrintAndLog(" sample: lf em 410xdemod = demod an EM410x Tag ID from GraphBuffer");
195 PrintAndLog(" : lf em 410xdemod 32 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32");
196 PrintAndLog(" : lf em 410xdemod 32 1 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32 and inverting data");
197 PrintAndLog(" : lf em 410xdemod 1 = demod an EM410x Tag ID from GraphBuffer while inverting data");
198 PrintAndLog(" : lf em 410xdemod 64 1 0 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/64 and inverting data and allowing 0 demod errors");
199 return 0;
200 }
201 uint64_t lo = 0;
202 uint32_t hi = 0;
203 return AskEm410xDemod(Cmd, &hi, &lo, true);
204 }
205
206 int usage_lf_em410x_sim(void) {
207 PrintAndLog("Simulating EM410x tag");
208 PrintAndLog("");
209 PrintAndLog("Usage: lf em 410xsim [h] <uid> <clock>");
210 PrintAndLog("Options:");
211 PrintAndLog(" h - this help");
212 PrintAndLog(" uid - uid (10 HEX symbols)");
213 PrintAndLog(" clock - clock (32|64) (optional)");
214 PrintAndLog("samples:");
215 PrintAndLog(" lf em 410xsim 0F0368568B");
216 PrintAndLog(" lf em 410xsim 0F0368568B 32");
217 return 0;
218 }
219
220 // Construct the graph for emulating an EM410X tag
221 void ConstructEM410xEmulGraph(const char *uid,const uint8_t clock)
222 {
223 int i, n, j, binary[4], parity[4];
224 /* clear our graph */
225 ClearGraph(0);
226
227 /* write 9 start bits */
228 for (i = 0; i < 9; i++)
229 AppendGraph(0, clock, 1);
230
231 /* for each hex char */
232 parity[0] = parity[1] = parity[2] = parity[3] = 0;
233 for (i = 0; i < 10; i++){
234 /* read each hex char */
235 sscanf(&uid[i], "%1x", &n);
236 for (j = 3; j >= 0; j--, n/= 2)
237 binary[j] = n % 2;
238
239 /* append each bit */
240 AppendGraph(0, clock, binary[0]);
241 AppendGraph(0, clock, binary[1]);
242 AppendGraph(0, clock, binary[2]);
243 AppendGraph(0, clock, binary[3]);
244
245 /* append parity bit */
246 AppendGraph(0, clock, binary[0] ^ binary[1] ^ binary[2] ^ binary[3]);
247
248 /* keep track of column parity */
249 parity[0] ^= binary[0];
250 parity[1] ^= binary[1];
251 parity[2] ^= binary[2];
252 parity[3] ^= binary[3];
253 }
254
255 /* parity columns */
256 AppendGraph(0, clock, parity[0]);
257 AppendGraph(0, clock, parity[1]);
258 AppendGraph(0, clock, parity[2]);
259 AppendGraph(0, clock, parity[3]);
260
261 /* stop bit */
262 AppendGraph(1, clock, 0);
263 }
264
265 // emulate an EM410X tag
266 int CmdEM410xSim(const char *Cmd)
267 {
268 char cmdp = param_getchar(Cmd, 0);
269 uint8_t uid[5] = {0x00};
270
271 if (cmdp == 'h' || cmdp == 'H') return usage_lf_em410x_sim();
272 /* clock is 64 in EM410x tags */
273 uint8_t clock = 64;
274
275 if (param_gethex(Cmd, 0, uid, 10)) {
276 PrintAndLog("UID must include 10 HEX symbols");
277 return 0;
278 }
279 param_getdec(Cmd,1, &clock);
280
281 PrintAndLog("Starting simulating UID %02X%02X%02X%02X%02X clock: %d", uid[0],uid[1],uid[2],uid[3],uid[4],clock);
282 PrintAndLog("Press pm3-button to abort simulation");
283
284 ConstructEM410xEmulGraph(Cmd, clock);
285
286 CmdLFSim("0"); //240 start_gap.
287 return 0;
288 }
289
290 int usage_lf_em410x_brute(void) {
291 PrintAndLog("Bruteforcing by emulating EM410x tag");
292 PrintAndLog("");
293 PrintAndLog("Usage: lf em 410xbrute [h] ids.txt [d 2000] [c clock]");
294 PrintAndLog("Options:");
295 PrintAndLog(" h - this help");
296 PrintAndLog(" ids.txt - file with UIDs in HEX format, one per line");
297 PrintAndLog(" d (2000) - pause delay in milliseconds between UIDs simulation, default 1000 ms (optional)");
298 PrintAndLog(" c (32) - clock (32|64), default 64 (optional)");
299 PrintAndLog("samples:");
300 PrintAndLog(" lf em 410xbrute ids.txt");
301 PrintAndLog(" lf em 410xbrute ids.txt c 32");
302 PrintAndLog(" lf em 410xbrute ids.txt d 3000");
303 PrintAndLog(" lf em 410xbrute ids.txt d 3000 c 32");
304 return 0;
305 }
306
307 int CmdEM410xBrute(const char *Cmd)
308 {
309 char filename[FILE_PATH_SIZE]={0};
310 FILE *f = NULL;
311 char buf[11];
312 uint32_t uidcnt = 0;
313 uint8_t stUidBlock = 20;
314 uint8_t *uidBlock = NULL, *p = NULL;
315 int ch;
316 uint8_t uid[5] = {0x00};
317 /* clock is 64 in EM410x tags */
318 uint8_t clock = 64;
319 /* default pause time: 1 second */
320 uint32_t delay = 1000;
321
322 char cmdp = param_getchar(Cmd, 0);
323
324 if (cmdp == 'h' || cmdp == 'H') return usage_lf_em410x_brute();
325
326
327 cmdp = param_getchar(Cmd, 1);
328
329 if (cmdp == 'd' || cmdp == 'D') {
330 delay = param_get32ex(Cmd, 2, 1000, 10);
331 param_getdec(Cmd, 4, &clock);
332 } else if (cmdp == 'c' || cmdp == 'C') {
333 param_getdec(Cmd, 2, &clock);
334 delay = param_get32ex(Cmd, 4, 1000, 10);
335 }
336
337 param_getstr(Cmd, 0, filename, sizeof(filename));
338
339 uidBlock = calloc(stUidBlock, 5);
340 if (uidBlock == NULL) return 1;
341
342 if (strlen(filename) > 0) {
343 if ((f = fopen(filename, "r")) == NULL) {
344 PrintAndLog("Error: Could not open UIDs file [%s]",filename);
345 free(uidBlock);
346 return 1;
347 }
348 } else {
349 PrintAndLog("Error: Please specify a filename");
350 free(uidBlock);
351 return 1;
352 }
353
354 while( fgets(buf, sizeof(buf), f) ) {
355 if (strlen(buf) < 10 || buf[9] == '\n') continue;
356 while (fgetc(f) != '\n' && !feof(f)); //goto next line
357
358 //The line start with # is comment, skip
359 if( buf[0]=='#' ) continue;
360
361 if (param_gethex(buf, 0, uid, 10)) {
362 PrintAndLog("UIDs must include 10 HEX symbols");
363 free(uidBlock);
364 fclose(f);
365 return 1;
366 }
367
368 buf[10] = 0;
369
370 if ( stUidBlock - uidcnt < 2) {
371 p = realloc(uidBlock, 5*(stUidBlock+=10));
372 if (!p) {
373 PrintAndLog("Cannot allocate memory for UIDs");
374 free(uidBlock);
375 fclose(f);
376 return 1;
377 }
378 uidBlock = p;
379 }
380 memset(uidBlock + 5 * uidcnt, 0, 5);
381 num_to_bytes(strtoll(buf, NULL, 16), 5, uidBlock + 5*uidcnt);
382 uidcnt++;
383 memset(buf, 0, sizeof(buf));
384 }
385 fclose(f);
386
387 if (uidcnt == 0) {
388 PrintAndLog("No UIDs found in file");
389 free(uidBlock);
390 return 1;
391 }
392 PrintAndLog("Loaded %d UIDs from %s, pause delay: %d ms", uidcnt, filename, delay);
393
394 // loop
395 for(uint32_t c = 0; c < uidcnt; ++c ) {
396 char testuid[11];
397 testuid[10] = 0;
398
399 if (ukbhit()) {
400 ch = getchar();
401 (void)ch;
402 printf("\nAborted via keyboard!\n");
403 free(uidBlock);
404 return 0;
405 }
406
407 sprintf(testuid, "%010" PRIX64, bytes_to_num(uidBlock + 5*c, 5));
408 PrintAndLog("Bruteforce %d / %d: simulating UID %s, clock %d", c + 1, uidcnt, testuid, clock);
409
410 ConstructEM410xEmulGraph(testuid, clock);
411
412 CmdLFSim("0"); //240 start_gap.
413
414 msleep(delay);
415 }
416
417 free(uidBlock);
418 return 0;
419 }
420
421
422 /* Function is equivalent of lf read + data samples + em410xread
423 * looped until an EM410x tag is detected
424 *
425 * Why is CmdSamples("16000")?
426 * TBD: Auto-grow sample size based on detected sample rate. IE: If the
427 * rate gets lower, then grow the number of samples
428 * Changed by martin, 4000 x 4 = 16000,
429 * see http://www.proxmark.org/forum/viewtopic.php?pid=7235#p7235
430 *
431 * EDIT -- capture enough to get 2 complete preambles at the slowest data rate known to be used (rf/64) (64*64*2+9 = 8201) marshmellow
432 */
433 int CmdEM410xWatch(const char *Cmd)
434 {
435 do {
436 if (ukbhit()) {
437 printf("\naborted via keyboard!\n");
438 break;
439 }
440 lf_read(true, 8201);
441 } while (!CmdAskEM410xDemod(""));
442
443 return 0;
444 }
445
446 //currently only supports manchester modulations
447 int CmdEM410xWatchnSpoof(const char *Cmd)
448 {
449 CmdEM410xWatch(Cmd);
450 PrintAndLog("# Replaying captured ID: %010"PRIx64, g_em410xId);
451 CmdLFaskSim("");
452 return 0;
453 }
454
455 int CmdEM410xWrite(const char *Cmd)
456 {
457 uint64_t id = 0xFFFFFFFFFFFFFFFF; // invalid id value
458 int card = 0xFF; // invalid card value
459 unsigned int clock = 0; // invalid clock value
460
461 sscanf(Cmd, "%" SCNx64 " %d %d", &id, &card, &clock);
462
463 // Check ID
464 if (id == 0xFFFFFFFFFFFFFFFF) {
465 PrintAndLog("Error! ID is required.\n");
466 return 0;
467 }
468 if (id >= 0x10000000000) {
469 PrintAndLog("Error! Given EM410x ID is longer than 40 bits.\n");
470 return 0;
471 }
472
473 // Check Card
474 if (card == 0xFF) {
475 PrintAndLog("Error! Card type required.\n");
476 return 0;
477 }
478 if (card < 0) {
479 PrintAndLog("Error! Bad card type selected.\n");
480 return 0;
481 }
482
483 // Check Clock
484 // Default: 64
485 if (clock == 0)
486 clock = 64;
487
488 // Allowed clock rates: 16, 32, 40 and 64
489 if ((clock != 16) && (clock != 32) && (clock != 64) && (clock != 40)) {
490 PrintAndLog("Error! Clock rate %d not valid. Supported clock rates are 16, 32, 40 and 64.\n", clock);
491 return 0;
492 }
493
494 if (card == 1) {
495 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64 " (clock rate: %d)", "T55x7", id, clock);
496 // NOTE: We really should pass the clock in as a separate argument, but to
497 // provide for backwards-compatibility for older firmware, and to avoid
498 // having to add another argument to CMD_EM410X_WRITE_TAG, we just store
499 // the clock rate in bits 8-15 of the card value
500 card = (card & 0xFF) | ((clock << 8) & 0xFF00);
501 } else if (card == 0) {
502 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64, "T5555", id, clock);
503 card = (card & 0xFF) | ((clock << 8) & 0xFF00);
504 } else {
505 PrintAndLog("Error! Bad card type selected.\n");
506 return 0;
507 }
508
509 UsbCommand c = {CMD_EM410X_WRITE_TAG, {card, (uint32_t)(id >> 32), (uint32_t)id}};
510 SendCommand(&c);
511
512 return 0;
513 }
514
515 //**************** Start of EM4x50 Code ************************
516 bool EM_EndParityTest(uint8_t *BitStream, size_t size, uint8_t rows, uint8_t cols, uint8_t pType)
517 {
518 if (rows*cols>size) return false;
519 uint8_t colP=0;
520 //assume last col is a parity and do not test
521 for (uint8_t colNum = 0; colNum < cols-1; colNum++) {
522 for (uint8_t rowNum = 0; rowNum < rows; rowNum++) {
523 colP ^= BitStream[(rowNum*cols)+colNum];
524 }
525 if (colP != pType) return false;
526 }
527 return true;
528 }
529
530 bool EM_ByteParityTest(uint8_t *BitStream, size_t size, uint8_t rows, uint8_t cols, uint8_t pType)
531 {
532 if (rows*cols>size) return false;
533 uint8_t rowP=0;
534 //assume last row is a parity row and do not test
535 for (uint8_t rowNum = 0; rowNum < rows-1; rowNum++) {
536 for (uint8_t colNum = 0; colNum < cols; colNum++) {
537 rowP ^= BitStream[(rowNum*cols)+colNum];
538 }
539 if (rowP != pType) return false;
540 }
541 return true;
542 }
543
544 uint32_t OutputEM4x50_Block(uint8_t *BitStream, size_t size, bool verbose, bool pTest)
545 {
546 if (size<45) return 0;
547 uint32_t code = bytebits_to_byte(BitStream,8);
548 code = code<<8 | bytebits_to_byte(BitStream+9,8);
549 code = code<<8 | bytebits_to_byte(BitStream+18,8);
550 code = code<<8 | bytebits_to_byte(BitStream+27,8);
551 if (verbose || g_debugMode){
552 for (uint8_t i = 0; i<5; i++){
553 if (i == 4) PrintAndLog(""); //parity byte spacer
554 PrintAndLog("%d%d%d%d%d%d%d%d %d -> 0x%02x",
555 BitStream[i*9],
556 BitStream[i*9+1],
557 BitStream[i*9+2],
558 BitStream[i*9+3],
559 BitStream[i*9+4],
560 BitStream[i*9+5],
561 BitStream[i*9+6],
562 BitStream[i*9+7],
563 BitStream[i*9+8],
564 bytebits_to_byte(BitStream+i*9,8)
565 );
566 }
567 if (pTest)
568 PrintAndLog("Parity Passed");
569 else
570 PrintAndLog("Parity Failed");
571 }
572 return code;
573 }
574 /* Read the transmitted data of an EM4x50 tag from the graphbuffer
575 * Format:
576 *
577 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
578 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
579 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
580 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
581 * CCCCCCCC <- column parity bits
582 * 0 <- stop bit
583 * LW <- Listen Window
584 *
585 * This pattern repeats for every block of data being transmitted.
586 * Transmission starts with two Listen Windows (LW - a modulated
587 * pattern of 320 cycles each (32/32/128/64/64)).
588 *
589 * Note that this data may or may not be the UID. It is whatever data
590 * is stored in the blocks defined in the control word First and Last
591 * Word Read values. UID is stored in block 32.
592 */
593 //completed by Marshmellow
594 int EM4x50Read(const char *Cmd, bool verbose)
595 {
596 uint8_t fndClk[] = {8,16,32,40,50,64,128};
597 int clk = 0;
598 int invert = 0;
599 int tol = 0;
600 int i, j, startblock, skip, block, start, end, low, high, minClk;
601 bool complete = false;
602 int tmpbuff[MAX_GRAPH_TRACE_LEN / 64];
603 uint32_t Code[6];
604 char tmp[6];
605 char tmp2[20];
606 int phaseoff;
607 high = low = 0;
608 memset(tmpbuff, 0, sizeof(tmpbuff));
609
610 // get user entry if any
611 sscanf(Cmd, "%i %i", &clk, &invert);
612
613 // first get high and low values
614 for (i = 0; i < GraphTraceLen; i++) {
615 if (GraphBuffer[i] > high)
616 high = GraphBuffer[i];
617 else if (GraphBuffer[i] < low)
618 low = GraphBuffer[i];
619 }
620
621 i = 0;
622 j = 0;
623 minClk = 255;
624 // get to first full low to prime loop and skip incomplete first pulse
625 while ((GraphBuffer[i] < high) && (i < GraphTraceLen))
626 ++i;
627 while ((GraphBuffer[i] > low) && (i < GraphTraceLen))
628 ++i;
629 skip = i;
630
631 // populate tmpbuff buffer with pulse lengths
632 while (i < GraphTraceLen) {
633 // measure from low to low
634 while ((GraphBuffer[i] > low) && (i < GraphTraceLen))
635 ++i;
636 start= i;
637 while ((GraphBuffer[i] < high) && (i < GraphTraceLen))
638 ++i;
639 while ((GraphBuffer[i] > low) && (i < GraphTraceLen))
640 ++i;
641 if (j>=(MAX_GRAPH_TRACE_LEN/64)) {
642 break;
643 }
644 tmpbuff[j++]= i - start;
645 if (i-start < minClk && i < GraphTraceLen) {
646 minClk = i - start;
647 }
648 }
649 // set clock
650 if (!clk) {
651 for (uint8_t clkCnt = 0; clkCnt<7; clkCnt++) {
652 tol = fndClk[clkCnt]/8;
653 if (minClk >= fndClk[clkCnt]-tol && minClk <= fndClk[clkCnt]+1) {
654 clk=fndClk[clkCnt];
655 break;
656 }
657 }
658 if (!clk) return 0;
659 } else tol = clk/8;
660
661 // look for data start - should be 2 pairs of LW (pulses of clk*3,clk*2)
662 start = -1;
663 for (i= 0; i < j - 4 ; ++i) {
664 skip += tmpbuff[i];
665 if (tmpbuff[i] >= clk*3-tol && tmpbuff[i] <= clk*3+tol) //3 clocks
666 if (tmpbuff[i+1] >= clk*2-tol && tmpbuff[i+1] <= clk*2+tol) //2 clocks
667 if (tmpbuff[i+2] >= clk*3-tol && tmpbuff[i+2] <= clk*3+tol) //3 clocks
668 if (tmpbuff[i+3] >= clk-tol) //1.5 to 2 clocks - depends on bit following
669 {
670 start= i + 4;
671 break;
672 }
673 }
674 startblock = i + 4;
675
676 // skip over the remainder of LW
677 skip += tmpbuff[i+1] + tmpbuff[i+2] + clk;
678 if (tmpbuff[i+3]>clk)
679 phaseoff = tmpbuff[i+3]-clk;
680 else
681 phaseoff = 0;
682 // now do it again to find the end
683 end = skip;
684 for (i += 3; i < j - 4 ; ++i) {
685 end += tmpbuff[i];
686 if (tmpbuff[i] >= clk*3-tol && tmpbuff[i] <= clk*3+tol) //3 clocks
687 if (tmpbuff[i+1] >= clk*2-tol && tmpbuff[i+1] <= clk*2+tol) //2 clocks
688 if (tmpbuff[i+2] >= clk*3-tol && tmpbuff[i+2] <= clk*3+tol) //3 clocks
689 if (tmpbuff[i+3] >= clk-tol) //1.5 to 2 clocks - depends on bit following
690 {
691 complete= true;
692 break;
693 }
694 }
695 end = i;
696 // report back
697 if (verbose || g_debugMode) {
698 if (start >= 0) {
699 PrintAndLog("\nNote: one block = 50 bits (32 data, 12 parity, 6 marker)");
700 } else {
701 PrintAndLog("No data found!, clock tried:%d",clk);
702 PrintAndLog("Try again with more samples.");
703 PrintAndLog(" or after a 'data askedge' command to clean up the read");
704 return 0;
705 }
706 } else if (start < 0) return 0;
707 start = skip;
708 snprintf(tmp2, sizeof(tmp2),"%d %d 1000 %d", clk, invert, clk*47);
709 // save GraphBuffer - to restore it later
710 save_restoreGB(GRAPH_SAVE);
711 // get rid of leading crap
712 snprintf(tmp, sizeof(tmp), "%i", skip);
713 CmdLtrim(tmp);
714 bool pTest;
715 bool AllPTest = true;
716 // now work through remaining buffer printing out data blocks
717 block = 0;
718 i = startblock;
719 while (block < 6) {
720 if (verbose || g_debugMode) PrintAndLog("\nBlock %i:", block);
721 skip = phaseoff;
722
723 // look for LW before start of next block
724 for ( ; i < j - 4 ; ++i) {
725 skip += tmpbuff[i];
726 if (tmpbuff[i] >= clk*3-tol && tmpbuff[i] <= clk*3+tol)
727 if (tmpbuff[i+1] >= clk-tol)
728 break;
729 }
730 if (i >= j-4) break; //next LW not found
731 skip += clk;
732 if (tmpbuff[i+1]>clk)
733 phaseoff = tmpbuff[i+1]-clk;
734 else
735 phaseoff = 0;
736 i += 2;
737 if (ASKDemod(tmp2, false, false, 1) < 1) {
738 save_restoreGB(GRAPH_RESTORE);
739 return 0;
740 }
741 //set DemodBufferLen to just one block
742 DemodBufferLen = skip/clk;
743 //test parities
744 pTest = EM_ByteParityTest(DemodBuffer,DemodBufferLen,5,9,0);
745 pTest &= EM_EndParityTest(DemodBuffer,DemodBufferLen,5,9,0);
746 AllPTest &= pTest;
747 //get output
748 Code[block] = OutputEM4x50_Block(DemodBuffer,DemodBufferLen,verbose, pTest);
749 if (g_debugMode) PrintAndLog("\nskipping %d samples, bits:%d", skip, skip/clk);
750 //skip to start of next block
751 snprintf(tmp,sizeof(tmp),"%i",skip);
752 CmdLtrim(tmp);
753 block++;
754 if (i >= end) break; //in case chip doesn't output 6 blocks
755 }
756 //print full code:
757 if (verbose || g_debugMode || AllPTest){
758 if (!complete) {
759 PrintAndLog("*** Warning!");
760 PrintAndLog("Partial data - no end found!");
761 PrintAndLog("Try again with more samples.");
762 }
763 PrintAndLog("Found data at sample: %i - using clock: %i", start, clk);
764 end = block;
765 for (block=0; block < end; block++){
766 PrintAndLog("Block %d: %08x",block,Code[block]);
767 }
768 if (AllPTest) {
769 PrintAndLog("Parities Passed");
770 } else {
771 PrintAndLog("Parities Failed");
772 PrintAndLog("Try cleaning the read samples with 'data askedge'");
773 }
774 }
775
776 //restore GraphBuffer
777 save_restoreGB(GRAPH_RESTORE);
778 return (int)AllPTest;
779 }
780
781 int CmdEM4x50Read(const char *Cmd)
782 {
783 return EM4x50Read(Cmd, true);
784 }
785
786 //**************** Start of EM4x05/EM4x69 Code ************************
787 int usage_lf_em_read(void) {
788 PrintAndLog("Read EM4x05/EM4x69. Tag must be on antenna. ");
789 PrintAndLog("");
790 PrintAndLog("Usage: lf em 4x05readword [h] <address> <pwd>");
791 PrintAndLog("Options:");
792 PrintAndLog(" h - this help");
793 PrintAndLog(" address - memory address to read. (0-15)");
794 PrintAndLog(" pwd - password (hex) (optional)");
795 PrintAndLog("samples:");
796 PrintAndLog(" lf em 4x05readword 1");
797 PrintAndLog(" lf em 4x05readword 1 11223344");
798 return 0;
799 }
800
801 // for command responses from em4x05 or em4x69
802 // download samples from device and copy them to the Graphbuffer
803 bool downloadSamplesEM() {
804 // 8 bit preamble + 32 bit word response (max clock (128) * 40bits = 5120 samples)
805 uint8_t got[6000];
806 if (!GetFromBigBuf(got, sizeof(got), 0, NULL, 4000, true)) {
807 PrintAndLog("command execution time out");
808 return false;
809 }
810 setGraphBuf(got, sizeof(got));
811 return true;
812 }
813
814 bool EM4x05testDemodReadData(uint32_t *word, bool readCmd) {
815 // em4x05/em4x69 command response preamble is 00001010
816 // skip first two 0 bits as they might have been missed in the demod
817 uint8_t preamble[] = {0,0,1,0,1,0};
818 size_t startIdx = 0;
819
820 // set size to 20 to only test first 14 positions for the preamble or less if not a read command
821 size_t size = (readCmd) ? 20 : 11;
822 // sanity check
823 size = (size > DemodBufferLen) ? DemodBufferLen : size;
824 // test preamble
825 if ( !preambleSearchEx(DemodBuffer, preamble, sizeof(preamble), &size, &startIdx, true) ) {
826 if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305 preamble not found :: %d", startIdx);
827 return false;
828 }
829 // if this is a readword command, get the read bytes and test the parities
830 if (readCmd) {
831 if (!EM_EndParityTest(DemodBuffer + startIdx + sizeof(preamble), 45, 5, 9, 0)) {
832 if (g_debugMode) PrintAndLog("DEBUG: Error - End Parity check failed");
833 return false;
834 }
835 // test for even parity bits and remove them. (leave out the end row of parities so 36 bits)
836 if ( removeParity(DemodBuffer, startIdx + sizeof(preamble),9,0,36) == 0 ) {
837 if (g_debugMode) PrintAndLog("DEBUG: Error - Parity not detected");
838 return false;
839 }
840
841 setDemodBuf(DemodBuffer, 32, 0);
842 //setClockGrid(0,0);
843
844 *word = bytebits_to_byteLSBF(DemodBuffer, 32);
845 }
846 return true;
847 }
848
849 // FSK, PSK, ASK/MANCHESTER, ASK/BIPHASE, ASK/DIPHASE
850 // should cover 90% of known used configs
851 // the rest will need to be manually demoded for now...
852 int demodEM4x05resp(uint32_t *word, bool readCmd) {
853 int ans = 0;
854
855 // test for FSK wave (easiest to 99% ID)
856 if (GetFskClock("", false, false)) {
857 //valid fsk clocks found
858 ans = FSKrawDemod("0 0", false);
859 if (!ans) {
860 if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305: FSK Demod failed, ans: %d", ans);
861 } else {
862 if (EM4x05testDemodReadData(word, readCmd)) {
863 return 1;
864 }
865 }
866 }
867 // PSK clocks should be easy to detect ( but difficult to demod a non-repeating pattern... )
868 ans = GetPskClock("", false, false);
869 if (ans>0) {
870 //try psk1
871 ans = PSKDemod("0 0 6", false);
872 if (!ans) {
873 if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305: PSK1 Demod failed, ans: %d", ans);
874 } else {
875 if (EM4x05testDemodReadData(word, readCmd)) {
876 return 1;
877 } else {
878 //try psk2
879 psk1TOpsk2(DemodBuffer, DemodBufferLen);
880 if (EM4x05testDemodReadData(word, readCmd)) {
881 return 1;
882 }
883 }
884 //try psk1 inverted
885 ans = PSKDemod("0 1 6", false);
886 if (!ans) {
887 if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305: PSK1 Demod failed, ans: %d", ans);
888 } else {
889 if (EM4x05testDemodReadData(word, readCmd)) {
890 return 1;
891 } else {
892 //try psk2
893 psk1TOpsk2(DemodBuffer, DemodBufferLen);
894 if (EM4x05testDemodReadData(word, readCmd)) {
895 return 1;
896 }
897 }
898 }
899 }
900 }
901
902 // manchester is more common than biphase... try first
903 bool stcheck = false;
904 // try manchester - NOTE: ST only applies to T55x7 tags.
905 ans = ASKDemod_ext("0,0,1", false, false, 1, &stcheck);
906 if (!ans) {
907 if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305: ASK/Manchester Demod failed, ans: %d", ans);
908 } else {
909 if (EM4x05testDemodReadData(word, readCmd)) {
910 return 1;
911 }
912 }
913
914 //try biphase
915 ans = ASKbiphaseDemod("0 0 1", false);
916 if (!ans) {
917 if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305: ASK/biphase Demod failed, ans: %d", ans);
918 } else {
919 if (EM4x05testDemodReadData(word, readCmd)) {
920 return 1;
921 }
922 }
923
924 //try diphase (differential biphase or inverted)
925 ans = ASKbiphaseDemod("0 1 1", false);
926 if (!ans) {
927 if (g_debugMode) PrintAndLog("DEBUG: Error - EM4305: ASK/biphase Demod failed, ans: %d", ans);
928 } else {
929 if (EM4x05testDemodReadData(word, readCmd)) {
930 return 1;
931 }
932 }
933
934 return -1;
935 }
936
937 int EM4x05ReadWord_ext(uint8_t addr, uint32_t pwd, bool usePwd, uint32_t *wordData) {
938 UsbCommand c = {CMD_EM4X_READ_WORD, {addr, pwd, usePwd}};
939 clearCommandBuffer();
940 SendCommand(&c);
941 UsbCommand resp;
942 if (!WaitForResponseTimeout(CMD_ACK, &resp, 2500)){
943 PrintAndLog("Command timed out");
944 return -1;
945 }
946 if ( !downloadSamplesEM() ) {
947 return -1;
948 }
949 int testLen = (GraphTraceLen < 1000) ? GraphTraceLen : 1000;
950 if (graphJustNoise(GraphBuffer, testLen)) {
951 return -1;
952 }
953 //attempt demod:
954 return demodEM4x05resp(wordData, true);
955 }
956
957 int EM4x05ReadWord(uint8_t addr, uint32_t pwd, bool usePwd) {
958 uint32_t wordData = 0;
959 int success = EM4x05ReadWord_ext(addr, pwd, usePwd, &wordData);
960 if (success == 1)
961 PrintAndLog("%s Address %02d | %08X", (addr>13) ? "Lock":" Got",addr,wordData);
962 else
963 PrintAndLog("Read Address %02d | failed",addr);
964
965 return success;
966 }
967
968 int CmdEM4x05ReadWord(const char *Cmd) {
969 uint8_t addr;
970 uint32_t pwd;
971 bool usePwd = false;
972 uint8_t ctmp = param_getchar(Cmd, 0);
973 if ( strlen(Cmd) == 0 || ctmp == 'H' || ctmp == 'h' ) return usage_lf_em_read();
974
975 addr = param_get8ex(Cmd, 0, 50, 10);
976 // for now use default input of 1 as invalid (unlikely 1 will be a valid password...)
977 pwd = param_get32ex(Cmd, 1, 1, 16);
978
979 if ( (addr > 15) ) {
980 PrintAndLog("Address must be between 0 and 15");
981 return 1;
982 }
983 if ( pwd == 1 ) {
984 PrintAndLog("Reading address %02u", addr);
985 } else {
986 usePwd = true;
987 PrintAndLog("Reading address %02u | password %08X", addr, pwd);
988 }
989
990 return EM4x05ReadWord(addr, pwd, usePwd);
991 }
992
993 int usage_lf_em_dump(void) {
994 PrintAndLog("Dump EM4x05/EM4x69. Tag must be on antenna. ");
995 PrintAndLog("");
996 PrintAndLog("Usage: lf em 4x05dump [h] <pwd>");
997 PrintAndLog("Options:");
998 PrintAndLog(" h - this help");
999 PrintAndLog(" pwd - password (hex) (optional)");
1000 PrintAndLog("samples:");
1001 PrintAndLog(" lf em 4x05dump");
1002 PrintAndLog(" lf em 4x05dump 11223344");
1003 return 0;
1004 }
1005
1006 int CmdEM4x05dump(const char *Cmd) {
1007 uint8_t addr = 0;
1008 uint32_t pwd;
1009 bool usePwd = false;
1010 uint8_t ctmp = param_getchar(Cmd, 0);
1011 if ( ctmp == 'H' || ctmp == 'h' ) return usage_lf_em_dump();
1012
1013 // for now use default input of 1 as invalid (unlikely 1 will be a valid password...)
1014 pwd = param_get32ex(Cmd, 0, 1, 16);
1015
1016 if ( pwd != 1 ) {
1017 usePwd = true;
1018 }
1019 int success = 1;
1020 for (; addr < 16; addr++) {
1021 if (addr == 2) {
1022 if (usePwd) {
1023 PrintAndLog(" PWD Address %02u | %08X",addr,pwd);
1024 } else {
1025 PrintAndLog(" PWD Address 02 | cannot read");
1026 }
1027 } else {
1028 success &= EM4x05ReadWord(addr, pwd, usePwd);
1029 }
1030 }
1031
1032 return success;
1033 }
1034
1035
1036 int usage_lf_em_write(void) {
1037 PrintAndLog("Write EM4x05/EM4x69. Tag must be on antenna. ");
1038 PrintAndLog("");
1039 PrintAndLog("Usage: lf em 4x05writeword [h] a <address> d <data> p <pwd> [s] [i]");
1040 PrintAndLog("Options:");
1041 PrintAndLog(" h - this help");
1042 PrintAndLog(" a <address> - memory address to write to. (0-15)");
1043 PrintAndLog(" d <data> - data to write (hex)");
1044 PrintAndLog(" p <pwd> - password (hex) (optional)");
1045 PrintAndLog(" s - swap the data bit order before write");
1046 PrintAndLog(" i - invert the data bits before write");
1047 PrintAndLog("samples:");
1048 PrintAndLog(" lf em 4x05writeword a 5 d 11223344");
1049 PrintAndLog(" lf em 4x05writeword a 5 p deadc0de d 11223344 s i");
1050 return 0;
1051 }
1052
1053 // note: em4x05 doesn't have a way to invert data output so we must invert the data prior to writing
1054 // it if invertion is needed. (example FSK2a vs FSK)
1055 // also em4x05 requires swapping word data when compared to the data used for t55xx chips.
1056 int EM4x05WriteWord(uint8_t addr, uint32_t data, uint32_t pwd, bool usePwd, bool swap, bool invert) {
1057 if (swap) data = SwapBits(data, 32);
1058
1059 if (invert) data ^= 0xFFFFFFFF;
1060
1061 if ( (addr > 15) ) {
1062 PrintAndLog("Address must be between 0 and 15");
1063 return -1;
1064 }
1065 if ( !usePwd ) {
1066 PrintAndLog("Writing address %d data %08X", addr, data);
1067 } else {
1068 PrintAndLog("Writing address %d data %08X using password %08X", addr, data, pwd);
1069 }
1070
1071 uint16_t flag = (addr << 8 ) | usePwd;
1072
1073 UsbCommand c = {CMD_EM4X_WRITE_WORD, {flag, data, pwd}};
1074 clearCommandBuffer();
1075 SendCommand(&c);
1076 UsbCommand resp;
1077 if (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)){
1078 PrintAndLog("Error occurred, device did not respond during write operation.");
1079 return -1;
1080 }
1081 if ( !downloadSamplesEM() ) {
1082 return -1;
1083 }
1084 //check response for 00001010 for write confirmation!
1085 //attempt demod:
1086 uint32_t dummy = 0;
1087 int result = demodEM4x05resp(&dummy,false);
1088 if (result == 1) {
1089 PrintAndLog("Write Verified");
1090 } else {
1091 PrintAndLog("Write could not be verified");
1092 }
1093 return result;
1094 }
1095
1096 int CmdEM4x05WriteWord(const char *Cmd) {
1097 bool errors = false;
1098 bool usePwd = false;
1099 uint32_t data = 0xFFFFFFFF;
1100 uint32_t pwd = 0xFFFFFFFF;
1101 bool swap = false;
1102 bool invert = false;
1103 uint8_t addr = 16; // default to invalid address
1104 bool gotData = false;
1105 char cmdp = 0;
1106 while(param_getchar(Cmd, cmdp) != 0x00)
1107 {
1108 switch(param_getchar(Cmd, cmdp))
1109 {
1110 case 'h':
1111 case 'H':
1112 return usage_lf_em_write();
1113 case 'a':
1114 case 'A':
1115 addr = param_get8ex(Cmd, cmdp+1, 16, 10);
1116 cmdp += 2;
1117 break;
1118 case 'd':
1119 case 'D':
1120 data = param_get32ex(Cmd, cmdp+1, 0, 16);
1121 gotData = true;
1122 cmdp += 2;
1123 break;
1124 case 'i':
1125 case 'I':
1126 invert = true;
1127 cmdp++;
1128 break;
1129 case 'p':
1130 case 'P':
1131 pwd = param_get32ex(Cmd, cmdp+1, 1, 16);
1132 if (pwd == 1) {
1133 PrintAndLog("invalid pwd");
1134 errors = true;
1135 }
1136 usePwd = true;
1137 cmdp += 2;
1138 break;
1139 case 's':
1140 case 'S':
1141 swap = true;
1142 cmdp++;
1143 break;
1144 default:
1145 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
1146 errors = true;
1147 break;
1148 }
1149 if(errors) break;
1150 }
1151 //Validations
1152 if(errors) return usage_lf_em_write();
1153
1154 if ( strlen(Cmd) == 0 ) return usage_lf_em_write();
1155
1156 if (!gotData) {
1157 PrintAndLog("You must enter the data you want to write");
1158 return usage_lf_em_write();
1159 }
1160 return EM4x05WriteWord(addr, data, pwd, usePwd, swap, invert);
1161 }
1162
1163 void printEM4x05config(uint32_t wordData) {
1164 uint16_t datarate = EM4x05_GET_BITRATE(wordData);
1165 uint8_t encoder = ((wordData >> 6) & 0xF);
1166 char enc[14];
1167 memset(enc,0,sizeof(enc));
1168
1169 uint8_t PSKcf = (wordData >> 10) & 0x3;
1170 char cf[10];
1171 memset(cf,0,sizeof(cf));
1172 uint8_t delay = (wordData >> 12) & 0x3;
1173 char cdelay[33];
1174 memset(cdelay,0,sizeof(cdelay));
1175 uint8_t numblks = EM4x05_GET_NUM_BLOCKS(wordData);
1176 uint8_t LWR = numblks+5-1; //last word read
1177 switch (encoder) {
1178 case 0: snprintf(enc,sizeof(enc),"NRZ"); break;
1179 case 1: snprintf(enc,sizeof(enc),"Manchester"); break;
1180 case 2: snprintf(enc,sizeof(enc),"Biphase"); break;
1181 case 3: snprintf(enc,sizeof(enc),"Miller"); break;
1182 case 4: snprintf(enc,sizeof(enc),"PSK1"); break;
1183 case 5: snprintf(enc,sizeof(enc),"PSK2"); break;
1184 case 6: snprintf(enc,sizeof(enc),"PSK3"); break;
1185 case 7: snprintf(enc,sizeof(enc),"Unknown"); break;
1186 case 8: snprintf(enc,sizeof(enc),"FSK1"); break;
1187 case 9: snprintf(enc,sizeof(enc),"FSK2"); break;
1188 default: snprintf(enc,sizeof(enc),"Unknown"); break;
1189 }
1190
1191 switch (PSKcf) {
1192 case 0: snprintf(cf,sizeof(cf),"RF/2"); break;
1193 case 1: snprintf(cf,sizeof(cf),"RF/8"); break;
1194 case 2: snprintf(cf,sizeof(cf),"RF/4"); break;
1195 case 3: snprintf(cf,sizeof(cf),"unknown"); break;
1196 }
1197
1198 switch (delay) {
1199 case 0: snprintf(cdelay, sizeof(cdelay),"no delay"); break;
1200 case 1: snprintf(cdelay, sizeof(cdelay),"BP/8 or 1/8th bit period delay"); break;
1201 case 2: snprintf(cdelay, sizeof(cdelay),"BP/4 or 1/4th bit period delay"); break;
1202 case 3: snprintf(cdelay, sizeof(cdelay),"no delay"); break;
1203 }
1204 uint8_t readLogin = (wordData & EM4x05_READ_LOGIN_REQ)>>18;
1205 uint8_t readHKL = (wordData & EM4x05_READ_HK_LOGIN_REQ)>>19;
1206 uint8_t writeLogin = (wordData & EM4x05_WRITE_LOGIN_REQ)>>20;
1207 uint8_t writeHKL = (wordData & EM4x05_WRITE_HK_LOGIN_REQ)>>21;
1208 uint8_t raw = (wordData & EM4x05_READ_AFTER_WRITE)>>22;
1209 uint8_t disable = (wordData & EM4x05_DISABLE_ALLOWED)>>23;
1210 uint8_t rtf = (wordData & EM4x05_READER_TALK_FIRST)>>24;
1211 uint8_t pigeon = (wordData & (1<<26))>>26;
1212 PrintAndLog("ConfigWord: %08X (Word 4)\n", wordData);
1213 PrintAndLog("Config Breakdown:");
1214 PrintAndLog(" Data Rate: %02u | RF/%u", wordData & 0x3F, datarate);
1215 PrintAndLog(" Encoder: %u | %s", encoder, enc);
1216 PrintAndLog(" PSK CF: %u | %s", PSKcf, cf);
1217 PrintAndLog(" Delay: %u | %s", delay, cdelay);
1218 PrintAndLog(" LastWordR: %02u | Address of last word for default read - meaning %u blocks are output", LWR, numblks);
1219 PrintAndLog(" ReadLogin: %u | Read Login is %s", readLogin, readLogin ? "Required" : "Not Required");
1220 PrintAndLog(" ReadHKL: %u | Read Housekeeping Words Login is %s", readHKL, readHKL ? "Required" : "Not Required");
1221 PrintAndLog("WriteLogin: %u | Write Login is %s", writeLogin, writeLogin ? "Required" : "Not Required");
1222 PrintAndLog(" WriteHKL: %u | Write Housekeeping Words Login is %s", writeHKL, writeHKL ? "Required" : "Not Required");
1223 PrintAndLog(" R.A.W.: %u | Read After Write is %s", raw, raw ? "On" : "Off");
1224 PrintAndLog(" Disable: %u | Disable Command is %s", disable, disable ? "Accepted" : "Not Accepted");
1225 PrintAndLog(" R.T.F.: %u | Reader Talk First is %s", rtf, rtf ? "Enabled" : "Disabled");
1226 PrintAndLog(" Pigeon: %u | Pigeon Mode is %s\n", pigeon, pigeon ? "Enabled" : "Disabled");
1227 }
1228
1229 void printEM4x05info(uint8_t chipType, uint8_t cap, uint16_t custCode, uint32_t serial) {
1230 switch (chipType) {
1231 case 9: PrintAndLog("\n Chip Type: %u | EM4305", chipType); break;
1232 case 4: PrintAndLog(" Chip Type: %u | Unknown", chipType); break;
1233 case 2: PrintAndLog(" Chip Type: %u | EM4469", chipType); break;
1234 //add more here when known
1235 default: PrintAndLog(" Chip Type: %u Unknown", chipType); break;
1236 }
1237
1238 switch (cap) {
1239 case 3: PrintAndLog(" Cap Type: %u | 330pF",cap); break;
1240 case 2: PrintAndLog(" Cap Type: %u | %spF",cap, (chipType==2)? "75":"210"); break;
1241 case 1: PrintAndLog(" Cap Type: %u | 250pF",cap); break;
1242 case 0: PrintAndLog(" Cap Type: %u | no resonant capacitor",cap); break;
1243 default: PrintAndLog(" Cap Type: %u | unknown",cap); break;
1244 }
1245
1246 PrintAndLog(" Cust Code: %03u | %s", custCode, (custCode == 0x200) ? "Default": "Unknown");
1247 if (serial != 0) {
1248 PrintAndLog("\n Serial #: %08X\n", serial);
1249 }
1250 }
1251
1252 void printEM4x05ProtectionBits(uint32_t wordData) {
1253 for (uint8_t i = 0; i < 15; i++) {
1254 PrintAndLog(" Word: %02u | %s", i, (((1 << i) & wordData ) || i < 2) ? "Is Write Locked" : "Is Not Write Locked");
1255 if (i==14) {
1256 PrintAndLog(" Word: %02u | %s", i+1, (((1 << i) & wordData ) || i < 2) ? "Is Write Locked" : "Is Not Write Locked");
1257 }
1258 }
1259 }
1260
1261 //quick test for EM4x05/EM4x69 tag
1262 bool EM4x05Block0Test(uint32_t *wordData) {
1263 if (EM4x05ReadWord_ext(0,0,false,wordData) == 1) {
1264 return true;
1265 }
1266 return false;
1267 }
1268
1269 int CmdEM4x05info(const char *Cmd) {
1270 //uint8_t addr = 0;
1271 uint32_t pwd;
1272 uint32_t wordData = 0;
1273 bool usePwd = false;
1274 uint8_t ctmp = param_getchar(Cmd, 0);
1275 if ( ctmp == 'H' || ctmp == 'h' ) return usage_lf_em_dump();
1276
1277 // for now use default input of 1 as invalid (unlikely 1 will be a valid password...)
1278 pwd = param_get32ex(Cmd, 0, 1, 16);
1279
1280 if ( pwd != 1 ) {
1281 usePwd = true;
1282 }
1283
1284 // read word 0 (chip info)
1285 // block 0 can be read even without a password.
1286 if ( !EM4x05Block0Test(&wordData) )
1287 return -1;
1288
1289 uint8_t chipType = (wordData >> 1) & 0xF;
1290 uint8_t cap = (wordData >> 5) & 3;
1291 uint16_t custCode = (wordData >> 9) & 0x3FF;
1292
1293 // read word 1 (serial #) doesn't need pwd
1294 wordData = 0;
1295 if (EM4x05ReadWord_ext(1, 0, false, &wordData) != 1) {
1296 //failed, but continue anyway...
1297 }
1298 printEM4x05info(chipType, cap, custCode, wordData);
1299
1300 // read word 4 (config block)
1301 // needs password if one is set
1302 wordData = 0;
1303 if ( EM4x05ReadWord_ext(4, pwd, usePwd, &wordData) != 1 ) {
1304 //failed
1305 PrintAndLog("Config block read failed - might be password protected.");
1306 return 0;
1307 }
1308 printEM4x05config(wordData);
1309
1310 // read word 14 and 15 to see which is being used for the protection bits
1311 wordData = 0;
1312 if ( EM4x05ReadWord_ext(14, pwd, usePwd, &wordData) != 1 ) {
1313 //failed
1314 return 0;
1315 }
1316 // if status bit says this is not the used protection word
1317 if (!(wordData & 0x8000)) {
1318 if ( EM4x05ReadWord_ext(15, pwd, usePwd, &wordData) != 1 ) {
1319 //failed
1320 return 0;
1321 }
1322 }
1323 if (!(wordData & 0x8000)) {
1324 //something went wrong
1325 return 0;
1326 }
1327 printEM4x05ProtectionBits(wordData);
1328
1329 return 1;
1330 }
1331
1332
1333 static command_t CommandTable[] =
1334 {
1335 {"help", CmdHelp, 1, "This help"},
1336 {"410xread", CmdEMdemodASK, 0, "[findone] -- Extract ID from EM410x tag (option 0 for continuous loop, 1 for only 1 tag)"},
1337 {"410xdemod", CmdAskEM410xDemod, 1, "[clock] [invert<0|1>] [maxErr] -- Demodulate an EM410x tag from GraphBuffer (args optional)"},
1338 {"410xsim", CmdEM410xSim, 0, "<UID> [clock rate] -- Simulate EM410x tag"},
1339 {"410xbrute", CmdEM410xBrute, 0, "ids.txt [d (delay in ms)] [c (clock rate)] -- Reader bruteforce attack by simulating EM410x tags"},
1340 {"410xwatch", CmdEM410xWatch, 0, "['h'] -- Watches for EM410x 125/134 kHz tags (option 'h' for 134)"},
1341 {"410xspoof", CmdEM410xWatchnSpoof, 0, "['h'] --- Watches for EM410x 125/134 kHz tags, and replays them. (option 'h' for 134)" },
1342 {"410xwrite", CmdEM410xWrite, 0, "<UID> <'0' T5555> <'1' T55x7> [clock rate] -- Write EM410x UID to T5555(Q5) or T55x7 tag, optionally setting clock rate"},
1343 {"4x05dump", CmdEM4x05dump, 0, "(pwd) -- Read EM4x05/EM4x69 all word data"},
1344 {"4x05info", CmdEM4x05info, 0, "(pwd) -- Get info from EM4x05/EM4x69 tag"},
1345 {"4x05readword", CmdEM4x05ReadWord, 0, "<Word> (pwd) -- Read EM4x05/EM4x69 word data"},
1346 {"4x05writeword", CmdEM4x05WriteWord, 0, "<Word> <data> (pwd) -- Write EM4x05/EM4x69 word data"},
1347 {"4x50read", CmdEM4x50Read, 1, "demod data from EM4x50 tag from the graph buffer"},
1348 {NULL, NULL, 0, NULL}
1349 };
1350
1351 int CmdLFEM4X(const char *Cmd)
1352 {
1353 CmdsParse(CommandTable, Cmd);
1354 return 0;
1355 }
1356
1357 int CmdHelp(const char *Cmd)
1358 {
1359 CmdsHelp(CommandTable);
1360 return 0;
1361 }
Impressum, Datenschutz