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