]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfem4x.c
Merge pull request #34 from marshmellow42/master
[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 "proxusb.h"
15 #include "proxmark3.h"
16 #include "ui.h"
17 #include "graph.h"
18 #include "cmdparser.h"
19 #include "cmddata.h"
20 #include "cmdlf.h"
21 #include "cmdlfem4x.h"
22
23 static int CmdHelp(const char *Cmd);
24
25
26
27 int CmdEMdemodASK(const char *Cmd)
28 {
29 int findone=0;
30 UsbCommand c={CMD_EM410X_DEMOD};
31 if(Cmd[0]=='1') findone=1;
32 c.arg[0]=findone;
33 SendCommand(&c);
34 return 0;
35 }
36
37
38
39 /* Read the ID of an EM410x tag.
40 * Format:
41 * 1111 1111 1 <-- standard non-repeatable header
42 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
43 * ....
44 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
45 * 0 <-- stop bit, end of tag
46 */
47 int CmdEM410xRead(const char *Cmd)
48 {
49 int i, j, clock, header, rows, bit, hithigh, hitlow, first, bit2idx, high, low;
50 int parity[4];
51 char id[11];
52 char id2[11];
53 int retested = 0;
54 uint8_t BitStream[MAX_GRAPH_TRACE_LEN];
55 high = low = 0;
56
57 /* Detect high and lows and clock */
58 for (i = 0; i < GraphTraceLen; i++)
59 {
60 if (GraphBuffer[i] > high)
61 high = GraphBuffer[i];
62 else if (GraphBuffer[i] < low)
63 low = GraphBuffer[i];
64 }
65
66 /* get clock */
67 clock = GetClock(Cmd, high, 0);
68
69 /* parity for our 4 columns */
70 parity[0] = parity[1] = parity[2] = parity[3] = 0;
71 header = rows = 0;
72
73 /* manchester demodulate */
74 bit = bit2idx = 0;
75 for (i = 0; i < (int)(GraphTraceLen / clock); i++)
76 {
77 hithigh = 0;
78 hitlow = 0;
79 first = 1;
80
81 /* Find out if we hit both high and low peaks */
82 for (j = 0; j < clock; j++)
83 {
84 if (GraphBuffer[(i * clock) + j] == high)
85 hithigh = 1;
86 else if (GraphBuffer[(i * clock) + j] == low)
87 hitlow = 1;
88
89 /* it doesn't count if it's the first part of our read
90 because it's really just trailing from the last sequence */
91 if (first && (hithigh || hitlow))
92 hithigh = hitlow = 0;
93 else
94 first = 0;
95
96 if (hithigh && hitlow)
97 break;
98 }
99
100 /* If we didn't hit both high and low peaks, we had a bit transition */
101 if (!hithigh || !hitlow)
102 bit ^= 1;
103
104 BitStream[bit2idx++] = bit;
105 }
106
107 retest:
108 /* We go till 5 before the graph ends because we'll get that far below */
109 for (i = 1; i < bit2idx - 5; i++)
110 {
111 /* Step 2: We have our header but need our tag ID */
112 if (header == 9 && rows < 10)
113 {
114 /* Confirm parity is correct */
115 if ((BitStream[i] ^ BitStream[i+1] ^ BitStream[i+2] ^ BitStream[i+3]) == BitStream[i+4])
116 {
117 /* Read another byte! */
118 sprintf(id+rows, "%x", (8 * BitStream[i]) + (4 * BitStream[i+1]) + (2 * BitStream[i+2]) + (1 * BitStream[i+3]));
119 sprintf(id2+rows, "%x", (8 * BitStream[i+3]) + (4 * BitStream[i+2]) + (2 * BitStream[i+1]) + (1 * BitStream[i]));
120 rows++;
121
122 /* Keep parity info */
123 parity[0] ^= BitStream[i];
124 parity[1] ^= BitStream[i+1];
125 parity[2] ^= BitStream[i+2];
126 parity[3] ^= BitStream[i+3];
127
128 /* Move 4 bits ahead */
129 i += 4;
130 }
131
132 /* Damn, something wrong! reset */
133 else
134 {
135 PrintAndLog("Thought we had a valid tag but failed at word %d (i=%d)", rows + 1, i);
136
137 /* Start back rows * 5 + 9 header bits, -1 to not start at same place */
138 i -= 9 + (5 * rows) - 5;
139
140 rows = header = 0;
141 }
142 }
143
144 /* Step 3: Got our 40 bits! confirm column parity */
145 else if (rows == 10)
146 {
147 /* We need to make sure our 4 bits of parity are correct and we have a stop bit */
148 if (BitStream[i] == parity[0] && BitStream[i+1] == parity[1] &&
149 BitStream[i+2] == parity[2] && BitStream[i+3] == parity[3] &&
150 BitStream[i+4] == 0)
151 {
152 /* Sweet! */
153 PrintAndLog("EM410x Tag ID: %s", id);
154 PrintAndLog("Unique Tag ID: %s", id2);
155
156 /* Stop any loops */
157 return 1;
158 }
159
160 /* Crap! Incorrect parity or no stop bit, start all over */
161 else
162 {
163 rows = header = 0;
164
165 /* Go back 59 bits (9 header bits + 10 rows at 4+1 parity) */
166 i -= 59;
167 }
168 }
169
170 /* Step 1: get our header */
171 else if (header < 9)
172 {
173 /* Need 9 consecutive 1's */
174 if (BitStream[i] == 1)
175 header++;
176
177 /* We don't have a header, not enough consecutive 1 bits */
178 else
179 header = 0;
180 }
181 }
182
183 /* if we've already retested after flipping bits, return */
184 if (retested++)
185 return 0;
186
187 /* if this didn't work, try flipping bits */
188 for (i = 0; i < bit2idx; i++)
189 BitStream[i] ^= 1;
190
191 goto retest;
192 }
193
194 /* emulate an EM410X tag
195 * Format:
196 * 1111 1111 1 <-- standard non-repeatable header
197 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
198 * ....
199 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
200 * 0 <-- stop bit, end of tag
201 */
202 int CmdEM410xSim(const char *Cmd)
203 {
204 int i, n, j, h, binary[4], parity[4];
205
206 /* clock is 64 in EM410x tags */
207 int clock = 64;
208
209 /* clear our graph */
210 ClearGraph(0);
211
212 /* write it out a few times */
213 for (h = 0; h < 4; h++)
214 {
215 /* write 9 start bits */
216 for (i = 0; i < 9; i++)
217 AppendGraph(0, clock, 1);
218
219 /* for each hex char */
220 parity[0] = parity[1] = parity[2] = parity[3] = 0;
221 for (i = 0; i < 10; i++)
222 {
223 /* read each hex char */
224 sscanf(&Cmd[i], "%1x", &n);
225 for (j = 3; j >= 0; j--, n/= 2)
226 binary[j] = n % 2;
227
228 /* append each bit */
229 AppendGraph(0, clock, binary[0]);
230 AppendGraph(0, clock, binary[1]);
231 AppendGraph(0, clock, binary[2]);
232 AppendGraph(0, clock, binary[3]);
233
234 /* append parity bit */
235 AppendGraph(0, clock, binary[0] ^ binary[1] ^ binary[2] ^ binary[3]);
236
237 /* keep track of column parity */
238 parity[0] ^= binary[0];
239 parity[1] ^= binary[1];
240 parity[2] ^= binary[2];
241 parity[3] ^= binary[3];
242 }
243
244 /* parity columns */
245 AppendGraph(0, clock, parity[0]);
246 AppendGraph(0, clock, parity[1]);
247 AppendGraph(0, clock, parity[2]);
248 AppendGraph(0, clock, parity[3]);
249
250 /* stop bit */
251 AppendGraph(0, clock, 0);
252 }
253
254 /* modulate that biatch */
255 CmdManchesterMod("");
256
257 /* booyah! */
258 RepaintGraphWindow();
259
260 CmdLFSim("");
261 return 0;
262 }
263
264 /* Function is equivalent of loread + losamples + em410xread
265 * looped until an EM410x tag is detected */
266 int CmdEM410xWatch(const char *Cmd)
267 {
268 int read_h = (*Cmd == 'h');
269 do
270 {
271 CmdLFRead(read_h ? "h" : "");
272 // 2000 samples is OK for clock=64, but not clock=32. Probably want
273 // 8000 for clock=16. Don't want to go too high since old HID driver
274 // is very slow
275 // TBD: Auto-grow sample size based on detected sample rate. IE: If the
276 // rate gets lower, then grow the number of samples
277
278 // Changed by martin, 4000 x 4 = 16000,
279 // see http://www.proxmark.org/forum/viewtopic.php?pid=7235#p7235
280 CmdSamples("16000");
281 } while ( ! CmdEM410xRead(""));
282 return 0;
283 }
284
285 /* Read the transmitted data of an EM4x50 tag
286 * Format:
287 *
288 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
289 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
290 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
291 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
292 * CCCCCCCC <- column parity bits
293 * 0 <- stop bit
294 * LW <- Listen Window
295 *
296 * This pattern repeats for every block of data being transmitted.
297 * Transmission starts with two Listen Windows (LW - a modulated
298 * pattern of 320 cycles each (32/32/128/64/64)).
299 *
300 * Note that this data may or may not be the UID. It is whatever data
301 * is stored in the blocks defined in the control word First and Last
302 * Word Read values. UID is stored in block 32.
303 */
304 int CmdEM4x50Read(const char *Cmd)
305 {
306 int i, j, startblock, skip, block, start, end, low, high;
307 bool complete= false;
308 int tmpbuff[MAX_GRAPH_TRACE_LEN / 64];
309 char tmp[6];
310
311 high= low= 0;
312 memset(tmpbuff, 0, MAX_GRAPH_TRACE_LEN / 64);
313
314 /* first get high and low values */
315 for (i = 0; i < GraphTraceLen; i++)
316 {
317 if (GraphBuffer[i] > high)
318 high = GraphBuffer[i];
319 else if (GraphBuffer[i] < low)
320 low = GraphBuffer[i];
321 }
322
323 /* populate a buffer with pulse lengths */
324 i= 0;
325 j= 0;
326 while (i < GraphTraceLen)
327 {
328 // measure from low to low
329 while ((GraphBuffer[i] > low) && (i<GraphTraceLen))
330 ++i;
331 start= i;
332 while ((GraphBuffer[i] < high) && (i<GraphTraceLen))
333 ++i;
334 while ((GraphBuffer[i] > low) && (i<GraphTraceLen))
335 ++i;
336 if (j>=(MAX_GRAPH_TRACE_LEN/64)) {
337 break;
338 }
339 tmpbuff[j++]= i - start;
340 }
341
342 /* look for data start - should be 2 pairs of LW (pulses of 192,128) */
343 start= -1;
344 skip= 0;
345 for (i= 0; i < j - 4 ; ++i)
346 {
347 skip += tmpbuff[i];
348 if (tmpbuff[i] >= 190 && tmpbuff[i] <= 194)
349 if (tmpbuff[i+1] >= 126 && tmpbuff[i+1] <= 130)
350 if (tmpbuff[i+2] >= 190 && tmpbuff[i+2] <= 194)
351 if (tmpbuff[i+3] >= 126 && tmpbuff[i+3] <= 130)
352 {
353 start= i + 3;
354 break;
355 }
356 }
357 startblock= i + 3;
358
359 /* skip over the remainder of the LW */
360 skip += tmpbuff[i+1]+tmpbuff[i+2];
361 while (skip < MAX_GRAPH_TRACE_LEN && GraphBuffer[skip] > low)
362 ++skip;
363 skip += 8;
364
365 /* now do it again to find the end */
366 end= start;
367 for (i += 3; i < j - 4 ; ++i)
368 {
369 end += tmpbuff[i];
370 if (tmpbuff[i] >= 190 && tmpbuff[i] <= 194)
371 if (tmpbuff[i+1] >= 126 && tmpbuff[i+1] <= 130)
372 if (tmpbuff[i+2] >= 190 && tmpbuff[i+2] <= 194)
373 if (tmpbuff[i+3] >= 126 && tmpbuff[i+3] <= 130)
374 {
375 complete= true;
376 break;
377 }
378 }
379
380 if (start >= 0)
381 PrintAndLog("Found data at sample: %i",skip);
382 else
383 {
384 PrintAndLog("No data found!");
385 PrintAndLog("Try again with more samples.");
386 return 0;
387 }
388
389 if (!complete)
390 {
391 PrintAndLog("*** Warning!");
392 PrintAndLog("Partial data - no end found!");
393 PrintAndLog("Try again with more samples.");
394 }
395
396 /* get rid of leading crap */
397 sprintf(tmp,"%i",skip);
398 CmdLtrim(tmp);
399
400 /* now work through remaining buffer printing out data blocks */
401 block= 0;
402 i= startblock;
403 while (block < 6)
404 {
405 PrintAndLog("Block %i:", block);
406 // mandemod routine needs to be split so we can call it for data
407 // just print for now for debugging
408 CmdManchesterDemod("i 64");
409 skip= 0;
410 /* look for LW before start of next block */
411 for ( ; i < j - 4 ; ++i)
412 {
413 skip += tmpbuff[i];
414 if (tmpbuff[i] >= 190 && tmpbuff[i] <= 194)
415 if (tmpbuff[i+1] >= 126 && tmpbuff[i+1] <= 130)
416 break;
417 }
418 while (GraphBuffer[skip] > low)
419 ++skip;
420 skip += 8;
421 sprintf(tmp,"%i",skip);
422 CmdLtrim(tmp);
423 start += skip;
424 block++;
425 }
426 return 0;
427 }
428
429 int CmdEM410xWrite(const char *Cmd)
430 {
431 uint64_t id = 0xFFFFFFFFFFFFFFFF; // invalid id value
432 int card = 0xFF; // invalid card value
433 unsigned int clock = 0; // invalid clock value
434
435 sscanf(Cmd, "%" PRIx64 " %d %d", &id, &card, &clock);
436
437 // Check ID
438 if (id == 0xFFFFFFFFFFFFFFFF) {
439 PrintAndLog("Error! ID is required.\n");
440 return 0;
441 }
442 if (id >= 0x10000000000) {
443 PrintAndLog("Error! Given EM410x ID is longer than 40 bits.\n");
444 return 0;
445 }
446
447 // Check Card
448 if (card == 0xFF) {
449 PrintAndLog("Error! Card type required.\n");
450 return 0;
451 }
452 if (card < 0) {
453 PrintAndLog("Error! Bad card type selected.\n");
454 return 0;
455 }
456
457 // Check Clock
458 if (card == 1)
459 {
460 // Default: 64
461 if (clock == 0)
462 clock = 64;
463
464 // Allowed clock rates: 16, 32 and 64
465 if ((clock != 16) && (clock != 32) && (clock != 64)) {
466 PrintAndLog("Error! Clock rate %d not valid. Supported clock rates are 16, 32 and 64.\n", clock);
467 return 0;
468 }
469 }
470 else if (clock != 0)
471 {
472 PrintAndLog("Error! Clock rate is only supported on T55x7 tags.\n");
473 return 0;
474 }
475
476 if (card == 1) {
477 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64 " (clock rate: %d)", "T55x7", id, clock);
478 // NOTE: We really should pass the clock in as a separate argument, but to
479 // provide for backwards-compatibility for older firmware, and to avoid
480 // having to add another argument to CMD_EM410X_WRITE_TAG, we just store
481 // the clock rate in bits 8-15 of the card value
482 card = (card & 0xFF) | (((uint64_t)clock << 8) & 0xFF00);
483 }
484 else if (card == 0)
485 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64, "T5555", id, clock);
486 else {
487 PrintAndLog("Error! Bad card type selected.\n");
488 return 0;
489 }
490
491 UsbCommand c = {CMD_EM410X_WRITE_TAG, {card, (uint32_t)(id >> 32), (uint32_t)id}};
492 SendCommand(&c);
493
494 return 0;
495 }
496
497 int CmdReadWord(const char *Cmd)
498 {
499 int Word = 16; //default to invalid word
500 UsbCommand c;
501
502 sscanf(Cmd, "%d", &Word);
503
504 if (Word > 15) {
505 PrintAndLog("Word must be between 0 and 15");
506 return 1;
507 }
508
509 PrintAndLog("Reading word %d", Word);
510
511 c.cmd = CMD_EM4X_READ_WORD;
512 c.d.asBytes[0] = 0x0; //Normal mode
513 c.arg[0] = 0;
514 c.arg[1] = Word;
515 c.arg[2] = 0;
516 SendCommand(&c);
517 return 0;
518 }
519
520 int CmdReadWordPWD(const char *Cmd)
521 {
522 int Word = 16; //default to invalid word
523 int Password = 0xFFFFFFFF; //default to blank password
524 UsbCommand c;
525
526 sscanf(Cmd, "%d %x", &Word, &Password);
527
528 if (Word > 15) {
529 PrintAndLog("Word must be between 0 and 15");
530 return 1;
531 }
532
533 PrintAndLog("Reading word %d with password %08X", Word, Password);
534
535 c.cmd = CMD_EM4X_READ_WORD;
536 c.d.asBytes[0] = 0x1; //Password mode
537 c.arg[0] = 0;
538 c.arg[1] = Word;
539 c.arg[2] = Password;
540 SendCommand(&c);
541 return 0;
542 }
543
544 int CmdWriteWord(const char *Cmd)
545 {
546 int Word = 16; //default to invalid block
547 int Data = 0xFFFFFFFF; //default to blank data
548 UsbCommand c;
549
550 sscanf(Cmd, "%x %d", &Data, &Word);
551
552 if (Word > 15) {
553 PrintAndLog("Word must be between 0 and 15");
554 return 1;
555 }
556
557 PrintAndLog("Writting word %d with data %08X", Word, Data);
558
559 c.cmd = CMD_EM4X_WRITE_WORD;
560 c.d.asBytes[0] = 0x0; //Normal mode
561 c.arg[0] = Data;
562 c.arg[1] = Word;
563 c.arg[2] = 0;
564 SendCommand(&c);
565 return 0;
566 }
567
568 int CmdWriteWordPWD(const char *Cmd)
569 {
570 int Word = 8; //default to invalid word
571 int Data = 0xFFFFFFFF; //default to blank data
572 int Password = 0xFFFFFFFF; //default to blank password
573 UsbCommand c;
574
575 sscanf(Cmd, "%x %d %x", &Data, &Word, &Password);
576
577 if (Word > 15) {
578 PrintAndLog("Word must be between 0 and 15");
579 return 1;
580 }
581
582 PrintAndLog("Writting word %d with data %08X and password %08X", Word, Data, Password);
583
584 c.cmd = CMD_EM4X_WRITE_WORD;
585 c.d.asBytes[0] = 0x1; //Password mode
586 c.arg[0] = Data;
587 c.arg[1] = Word;
588 c.arg[2] = Password;
589 SendCommand(&c);
590 return 0;
591 }
592
593
594
595 static command_t CommandTable[] =
596 {
597 {"help", CmdHelp, 1, "This help"},
598 {"em410xdemod", CmdEMdemodASK, 0, "[clock rate] -- Extract ID from EM410x tag"},
599 {"em410xread", CmdEM410xRead, 1, "[clock rate] -- Extract ID from EM410x tag"},
600 {"em410xsim", CmdEM410xSim, 0, "<UID> -- Simulate EM410x tag"},
601 {"em410xwatch", CmdEM410xWatch, 0, "['h'] -- Watches for EM410x 125/134 kHz tags (option 'h' for 134)"},
602 {"em410xwrite", CmdEM410xWrite, 1, "<UID> <'0' T5555> <'1' T55x7> [clock rate] -- Write EM410x UID to T5555(Q5) or T55x7 tag, optionally setting clock rate"},
603 {"em4x50read", CmdEM4x50Read, 1, "Extract data from EM4x50 tag"},
604 {"readword", CmdReadWord, 1, "<Word> -- Read EM4xxx word data"},
605 {"readwordPWD", CmdReadWordPWD, 1, "<Word> <Password> -- Read EM4xxx word data in password mode"},
606 {"writeword", CmdWriteWord, 1, "<Data> <Word> -- Write EM4xxx word data"},
607 {"writewordPWD", CmdWriteWordPWD, 1, "<Data> <Word> <Password> -- Write EM4xxx word data in password mode"},
608 {NULL, NULL, 0, NULL}
609 };
610
611 int CmdLFEM4X(const char *Cmd)
612 {
613 CmdsParse(CommandTable, Cmd);
614 return 0;
615 }
616
617 int CmdHelp(const char *Cmd)
618 {
619 CmdsHelp(CommandTable);
620 return 0;
621 }
Impressum, Datenschutz