]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdlfem4x.c
chg: updated the README.txt
[proxmark3-svn] / client / cmdlfem4x.c
CommitLineData
2ae8a312 1 //-----------------------------------------------------------------------------
a553f267 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
7fe9b0b7 11#include <stdio.h>
9e13f875 12#include <string.h>
ec564290 13#include <inttypes.h>
902cb3c0 14#include "proxmark3.h"
7fe9b0b7 15#include "ui.h"
16#include "graph.h"
f38a1528 17#include "cmdmain.h"
7fe9b0b7 18#include "cmdparser.h"
19#include "cmddata.h"
20#include "cmdlf.h"
21#include "cmdlfem4x.h"
f38a1528 22#include "util.h"
23#include "data.h"
f6c18637 24#define LF_TRACE_BUFF_SIZE 12000
c6be64da 25#define LF_BITSSTREAM_LEN 1000
f38a1528 26
27char *global_em410xId;
7fe9b0b7 28
29static int CmdHelp(const char *Cmd);
30
6ff6ade2 31
32
33int CmdEMdemodASK(const char *Cmd)
34{
35 int findone=0;
36 UsbCommand c={CMD_EM410X_DEMOD};
37 if(Cmd[0]=='1') findone=1;
38 c.arg[0]=findone;
39 SendCommand(&c);
40 return 0;
41}
42
43
44
7fe9b0b7 45/* Read the ID of an EM410x tag.
46 * Format:
47 * 1111 1111 1 <-- standard non-repeatable header
48 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
49 * ....
50 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
51 * 0 <-- stop bit, end of tag
52 */
53int CmdEM410xRead(const char *Cmd)
54{
55 int i, j, clock, header, rows, bit, hithigh, hitlow, first, bit2idx, high, low;
56 int parity[4];
57 char id[11];
080ff30a 58 char id2[11];
7fe9b0b7 59 int retested = 0;
15cdabd4 60 uint8_t BitStream[MAX_GRAPH_TRACE_LEN];
7fe9b0b7 61 high = low = 0;
62
63 /* Detect high and lows and clock */
64 for (i = 0; i < GraphTraceLen; i++)
65 {
66 if (GraphBuffer[i] > high)
67 high = GraphBuffer[i];
68 else if (GraphBuffer[i] < low)
69 low = GraphBuffer[i];
70 }
71
72 /* get clock */
73 clock = GetClock(Cmd, high, 0);
3649b640 74
7fe9b0b7 75 /* parity for our 4 columns */
76 parity[0] = parity[1] = parity[2] = parity[3] = 0;
77 header = rows = 0;
78
79 /* manchester demodulate */
80 bit = bit2idx = 0;
81 for (i = 0; i < (int)(GraphTraceLen / clock); i++)
82 {
83 hithigh = 0;
84 hitlow = 0;
85 first = 1;
86
87 /* Find out if we hit both high and low peaks */
88 for (j = 0; j < clock; j++)
89 {
90 if (GraphBuffer[(i * clock) + j] == high)
91 hithigh = 1;
92 else if (GraphBuffer[(i * clock) + j] == low)
93 hitlow = 1;
94
95 /* it doesn't count if it's the first part of our read
96 because it's really just trailing from the last sequence */
97 if (first && (hithigh || hitlow))
98 hithigh = hitlow = 0;
99 else
100 first = 0;
101
102 if (hithigh && hitlow)
103 break;
104 }
105
106 /* If we didn't hit both high and low peaks, we had a bit transition */
107 if (!hithigh || !hitlow)
108 bit ^= 1;
109
110 BitStream[bit2idx++] = bit;
111 }
112
113retest:
114 /* We go till 5 before the graph ends because we'll get that far below */
3649b640 115 for (i = 0; i < bit2idx - 5; i++)
7fe9b0b7 116 {
117 /* Step 2: We have our header but need our tag ID */
118 if (header == 9 && rows < 10)
119 {
120 /* Confirm parity is correct */
121 if ((BitStream[i] ^ BitStream[i+1] ^ BitStream[i+2] ^ BitStream[i+3]) == BitStream[i+4])
122 {
123 /* Read another byte! */
124 sprintf(id+rows, "%x", (8 * BitStream[i]) + (4 * BitStream[i+1]) + (2 * BitStream[i+2]) + (1 * BitStream[i+3]));
cb967ea9 125 sprintf(id2+rows, "%x", (8 * BitStream[i+3]) + (4 * BitStream[i+2]) + (2 * BitStream[i+1]) + (1 * BitStream[i]));
7fe9b0b7 126 rows++;
127
128 /* Keep parity info */
129 parity[0] ^= BitStream[i];
130 parity[1] ^= BitStream[i+1];
131 parity[2] ^= BitStream[i+2];
132 parity[3] ^= BitStream[i+3];
133
134 /* Move 4 bits ahead */
135 i += 4;
136 }
137
138 /* Damn, something wrong! reset */
139 else
140 {
141 PrintAndLog("Thought we had a valid tag but failed at word %d (i=%d)", rows + 1, i);
142
143 /* Start back rows * 5 + 9 header bits, -1 to not start at same place */
c15d2bdc 144 i -= 9 + (5 * rows) -5;
7fe9b0b7 145
146 rows = header = 0;
147 }
148 }
149
150 /* Step 3: Got our 40 bits! confirm column parity */
151 else if (rows == 10)
152 {
153 /* We need to make sure our 4 bits of parity are correct and we have a stop bit */
154 if (BitStream[i] == parity[0] && BitStream[i+1] == parity[1] &&
155 BitStream[i+2] == parity[2] && BitStream[i+3] == parity[3] &&
156 BitStream[i+4] == 0)
157 {
158 /* Sweet! */
159 PrintAndLog("EM410x Tag ID: %s", id);
cb967ea9 160 PrintAndLog("Unique Tag ID: %s", id2);
7fe9b0b7 161
f38a1528 162 global_em410xId = id;
163
7fe9b0b7 164 /* Stop any loops */
165 return 1;
166 }
167
168 /* Crap! Incorrect parity or no stop bit, start all over */
169 else
170 {
171 rows = header = 0;
172
173 /* Go back 59 bits (9 header bits + 10 rows at 4+1 parity) */
174 i -= 59;
175 }
176 }
177
178 /* Step 1: get our header */
179 else if (header < 9)
180 {
181 /* Need 9 consecutive 1's */
182 if (BitStream[i] == 1)
183 header++;
184
185 /* We don't have a header, not enough consecutive 1 bits */
186 else
187 header = 0;
188 }
189 }
190
191 /* if we've already retested after flipping bits, return */
f38a1528 192 if (retested++){
7fe9b0b7 193 return 0;
f38a1528 194 }
7fe9b0b7 195
196 /* if this didn't work, try flipping bits */
197 for (i = 0; i < bit2idx; i++)
198 BitStream[i] ^= 1;
199
200 goto retest;
201}
202
203/* emulate an EM410X tag
204 * Format:
205 * 1111 1111 1 <-- standard non-repeatable header
206 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
207 * ....
208 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
209 * 0 <-- stop bit, end of tag
210 */
211int CmdEM410xSim(const char *Cmd)
2ae8a312 212{
06b58a94 213 int i, n, j, binary[4], parity[4];
2ae8a312 214
215 char cmdp = param_getchar(Cmd, 0);
216 uint8_t uid[5] = {0x00};
217
218 if (cmdp == 'h' || cmdp == 'H') {
a501c82b 219 PrintAndLog("Usage: lf em4x 410xsim <UID>");
2ae8a312 220 PrintAndLog("");
a501c82b 221 PrintAndLog(" sample: lf em4x 410xsim 0F0368568B");
2ae8a312 222 return 0;
223 }
7fe9b0b7 224
2ae8a312 225 if (param_gethex(Cmd, 0, uid, 10)) {
226 PrintAndLog("UID must include 10 HEX symbols");
227 return 0;
228 }
229
3649b640 230 PrintAndLog("Starting simulating UID %02X%02X%02X%02X%02X", uid[0],uid[1],uid[2],uid[3],uid[4]);
231 PrintAndLog("Press pm3-button to about simulation");
2ae8a312 232
7fe9b0b7 233 /* clock is 64 in EM410x tags */
234 int clock = 64;
235
236 /* clear our graph */
c15d2bdc 237 ClearGraph(0);
7c756d68 238
7fe9b0b7 239 /* write 9 start bits */
240 for (i = 0; i < 9; i++)
241 AppendGraph(0, clock, 1);
242
243 /* for each hex char */
244 parity[0] = parity[1] = parity[2] = parity[3] = 0;
245 for (i = 0; i < 10; i++)
246 {
247 /* read each hex char */
248 sscanf(&Cmd[i], "%1x", &n);
249 for (j = 3; j >= 0; j--, n/= 2)
250 binary[j] = n % 2;
251
252 /* append each bit */
253 AppendGraph(0, clock, binary[0]);
254 AppendGraph(0, clock, binary[1]);
255 AppendGraph(0, clock, binary[2]);
256 AppendGraph(0, clock, binary[3]);
257
258 /* append parity bit */
259 AppendGraph(0, clock, binary[0] ^ binary[1] ^ binary[2] ^ binary[3]);
260
261 /* keep track of column parity */
262 parity[0] ^= binary[0];
263 parity[1] ^= binary[1];
264 parity[2] ^= binary[2];
265 parity[3] ^= binary[3];
266 }
267
268 /* parity columns */
269 AppendGraph(0, clock, parity[0]);
270 AppendGraph(0, clock, parity[1]);
271 AppendGraph(0, clock, parity[2]);
272 AppendGraph(0, clock, parity[3]);
273
06b58a94 274 /* stop bit */
a501c82b 275 AppendGraph(1, clock, 0);
06b58a94 276
a501c82b 277 CmdLFSim("240"); //240 start_gap.
7fe9b0b7 278 return 0;
279}
280
c2d25819 281/* Function is equivalent of lf read + data samples + em410xread
282 * looped until an EM410x tag is detected
283 *
284 * Why is CmdSamples("16000")?
285 * TBD: Auto-grow sample size based on detected sample rate. IE: If the
286 * rate gets lower, then grow the number of samples
287 * Changed by martin, 4000 x 4 = 16000,
288 * see http://www.proxmark.org/forum/viewtopic.php?pid=7235#p7235
289
290*/
7fe9b0b7 291int CmdEM410xWatch(const char *Cmd)
292{
c2d25819 293 int read_h = (*Cmd == 'h');
294 do
295 {
2ae8a312 296 if (ukbhit()) {
297 printf("\naborted via keyboard!\n");
298 break;
299 }
300
c2d25819 301 CmdLFRead(read_h ? "h" : "");
c15d2bdc 302 CmdSamples("6000");
2ae8a312 303
c2d25819 304 } while (
a61b4976 305 !CmdEM410xRead("")
f38a1528 306 );
c2d25819 307 return 0;
f38a1528 308}
309
310int CmdEM410xWatchnSpoof(const char *Cmd)
311{
c2d25819 312 CmdEM410xWatch(Cmd);
f38a1528 313 PrintAndLog("# Replaying : %s",global_em410xId);
314 CmdEM410xSim(global_em410xId);
7fe9b0b7 315 return 0;
316}
317
318/* Read the transmitted data of an EM4x50 tag
319 * Format:
320 *
321 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
322 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
323 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
324 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
325 * CCCCCCCC <- column parity bits
326 * 0 <- stop bit
327 * LW <- Listen Window
328 *
329 * This pattern repeats for every block of data being transmitted.
330 * Transmission starts with two Listen Windows (LW - a modulated
331 * pattern of 320 cycles each (32/32/128/64/64)).
332 *
333 * Note that this data may or may not be the UID. It is whatever data
334 * is stored in the blocks defined in the control word First and Last
335 * Word Read values. UID is stored in block 32.
336 */
337int CmdEM4x50Read(const char *Cmd)
338{
31b6e9af 339 int i, j, startblock, skip, block, start, end, low, high;
7fe9b0b7 340 bool complete= false;
341 int tmpbuff[MAX_GRAPH_TRACE_LEN / 64];
342 char tmp[6];
343
344 high= low= 0;
913d23c6 345 memset(tmpbuff, 0, MAX_GRAPH_TRACE_LEN / 64);
7fe9b0b7 346
347 /* first get high and low values */
348 for (i = 0; i < GraphTraceLen; i++)
349 {
350 if (GraphBuffer[i] > high)
351 high = GraphBuffer[i];
352 else if (GraphBuffer[i] < low)
353 low = GraphBuffer[i];
354 }
355
356 /* populate a buffer with pulse lengths */
357 i= 0;
358 j= 0;
359 while (i < GraphTraceLen)
360 {
361 // measure from low to low
362 while ((GraphBuffer[i] > low) && (i<GraphTraceLen))
363 ++i;
364 start= i;
365 while ((GraphBuffer[i] < high) && (i<GraphTraceLen))
366 ++i;
367 while ((GraphBuffer[i] > low) && (i<GraphTraceLen))
368 ++i;
a61b4976 369 if (j>=(MAX_GRAPH_TRACE_LEN/64)) {
7fe9b0b7 370 break;
371 }
372 tmpbuff[j++]= i - start;
373 }
374
375 /* look for data start - should be 2 pairs of LW (pulses of 192,128) */
376 start= -1;
377 skip= 0;
378 for (i= 0; i < j - 4 ; ++i)
379 {
380 skip += tmpbuff[i];
381 if (tmpbuff[i] >= 190 && tmpbuff[i] <= 194)
382 if (tmpbuff[i+1] >= 126 && tmpbuff[i+1] <= 130)
383 if (tmpbuff[i+2] >= 190 && tmpbuff[i+2] <= 194)
384 if (tmpbuff[i+3] >= 126 && tmpbuff[i+3] <= 130)
385 {
386 start= i + 3;
387 break;
388 }
389 }
390 startblock= i + 3;
391
392 /* skip over the remainder of the LW */
393 skip += tmpbuff[i+1]+tmpbuff[i+2];
394 while (skip < MAX_GRAPH_TRACE_LEN && GraphBuffer[skip] > low)
395 ++skip;
396 skip += 8;
397
398 /* now do it again to find the end */
399 end= start;
400 for (i += 3; i < j - 4 ; ++i)
401 {
402 end += tmpbuff[i];
403 if (tmpbuff[i] >= 190 && tmpbuff[i] <= 194)
404 if (tmpbuff[i+1] >= 126 && tmpbuff[i+1] <= 130)
405 if (tmpbuff[i+2] >= 190 && tmpbuff[i+2] <= 194)
406 if (tmpbuff[i+3] >= 126 && tmpbuff[i+3] <= 130)
407 {
408 complete= true;
409 break;
410 }
411 }
412
413 if (start >= 0)
414 PrintAndLog("Found data at sample: %i",skip);
415 else
416 {
417 PrintAndLog("No data found!");
418 PrintAndLog("Try again with more samples.");
419 return 0;
420 }
421
422 if (!complete)
423 {
424 PrintAndLog("*** Warning!");
425 PrintAndLog("Partial data - no end found!");
426 PrintAndLog("Try again with more samples.");
427 }
428
429 /* get rid of leading crap */
430 sprintf(tmp,"%i",skip);
431 CmdLtrim(tmp);
432
433 /* now work through remaining buffer printing out data blocks */
434 block= 0;
435 i= startblock;
436 while (block < 6)
437 {
438 PrintAndLog("Block %i:", block);
439 // mandemod routine needs to be split so we can call it for data
440 // just print for now for debugging
441 CmdManchesterDemod("i 64");
442 skip= 0;
443 /* look for LW before start of next block */
444 for ( ; i < j - 4 ; ++i)
445 {
446 skip += tmpbuff[i];
447 if (tmpbuff[i] >= 190 && tmpbuff[i] <= 194)
448 if (tmpbuff[i+1] >= 126 && tmpbuff[i+1] <= 130)
449 break;
450 }
451 while (GraphBuffer[skip] > low)
452 ++skip;
453 skip += 8;
454 sprintf(tmp,"%i",skip);
455 CmdLtrim(tmp);
456 start += skip;
457 block++;
458 }
459 return 0;
460}
461
2d4eae76 462int CmdEM410xWrite(const char *Cmd)
463{
e67b06b7 464 uint64_t id = 0xFFFFFFFFFFFFFFFF; // invalid id value
7bb9d33e 465 int card = 0xFF; // invalid card value
e67b06b7 466 unsigned int clock = 0; // invalid clock value
467
468 sscanf(Cmd, "%" PRIx64 " %d %d", &id, &card, &clock);
469
470 // Check ID
471 if (id == 0xFFFFFFFFFFFFFFFF) {
472 PrintAndLog("Error! ID is required.\n");
473 return 0;
474 }
475 if (id >= 0x10000000000) {
476 PrintAndLog("Error! Given EM410x ID is longer than 40 bits.\n");
477 return 0;
478 }
479
480 // Check Card
481 if (card == 0xFF) {
482 PrintAndLog("Error! Card type required.\n");
483 return 0;
484 }
485 if (card < 0) {
486 PrintAndLog("Error! Bad card type selected.\n");
487 return 0;
488 }
489
490 // Check Clock
491 if (card == 1)
492 {
493 // Default: 64
494 if (clock == 0)
495 clock = 64;
496
497 // Allowed clock rates: 16, 32 and 64
498 if ((clock != 16) && (clock != 32) && (clock != 64)) {
499 PrintAndLog("Error! Clock rate %d not valid. Supported clock rates are 16, 32 and 64.\n", clock);
500 return 0;
501 }
502 }
503 else if (clock != 0)
504 {
505 PrintAndLog("Error! Clock rate is only supported on T55x7 tags.\n");
506 return 0;
507 }
508
509 if (card == 1) {
510 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64 " (clock rate: %d)", "T55x7", id, clock);
511 // NOTE: We really should pass the clock in as a separate argument, but to
512 // provide for backwards-compatibility for older firmware, and to avoid
513 // having to add another argument to CMD_EM410X_WRITE_TAG, we just store
514 // the clock rate in bits 8-15 of the card value
515 card = (card & 0xFF) | (((uint64_t)clock << 8) & 0xFF00);
516 }
517 else if (card == 0)
518 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64, "T5555", id, clock);
519 else {
520 PrintAndLog("Error! Bad card type selected.\n");
521 return 0;
522 }
2d4eae76 523
2d4eae76 524 UsbCommand c = {CMD_EM410X_WRITE_TAG, {card, (uint32_t)(id >> 32), (uint32_t)id}};
525 SendCommand(&c);
526
527 return 0;
528}
529
54a942b0 530int CmdReadWord(const char *Cmd)
531{
f38a1528 532 int Word = -1; //default to invalid word
b44e5233 533 UsbCommand c;
54a942b0 534
b44e5233 535 sscanf(Cmd, "%d", &Word);
54a942b0 536
f38a1528 537 if ( (Word > 15) | (Word < 0) ) {
b44e5233 538 PrintAndLog("Word must be between 0 and 15");
539 return 1;
540 }
54a942b0 541
b44e5233 542 PrintAndLog("Reading word %d", Word);
54a942b0 543
b44e5233 544 c.cmd = CMD_EM4X_READ_WORD;
545 c.d.asBytes[0] = 0x0; //Normal mode
546 c.arg[0] = 0;
547 c.arg[1] = Word;
548 c.arg[2] = 0;
549 SendCommand(&c);
f38a1528 550 WaitForResponse(CMD_ACK, NULL);
551
f6c18637 552 uint8_t data[LF_TRACE_BUFF_SIZE] = {0x00};
f38a1528 553
b44e5233 554 GetFromBigBuf(data,LF_TRACE_BUFF_SIZE,3560); //3560 -- should be offset..
f38a1528 555 WaitForResponseTimeout(CMD_ACK,NULL, 1500);
556
b44e5233 557 for (int j = 0; j < LF_TRACE_BUFF_SIZE; j++) {
f6c18637 558 GraphBuffer[j] = ((int)data[j]);
f38a1528 559 }
b44e5233 560 GraphTraceLen = LF_TRACE_BUFF_SIZE;
b44e5233 561
c6be64da 562 uint8_t bits[LF_BITSSTREAM_LEN] = {0x00};
b44e5233 563 uint8_t * bitstream = bits;
c6be64da 564 manchester_decode(GraphBuffer, LF_TRACE_BUFF_SIZE, bitstream,LF_BITSSTREAM_LEN);
f6c18637 565 RepaintGraphWindow();
54a942b0 566 return 0;
567}
568
569int CmdReadWordPWD(const char *Cmd)
570{
f38a1528 571 int Word = -1; //default to invalid word
b44e5233 572 int Password = 0xFFFFFFFF; //default to blank password
573 UsbCommand c;
574
575 sscanf(Cmd, "%d %x", &Word, &Password);
576
f38a1528 577 if ( (Word > 15) | (Word < 0) ) {
b44e5233 578 PrintAndLog("Word must be between 0 and 15");
579 return 1;
580 }
54a942b0 581
b44e5233 582 PrintAndLog("Reading word %d with password %08X", Word, Password);
583
584 c.cmd = CMD_EM4X_READ_WORD;
585 c.d.asBytes[0] = 0x1; //Password mode
586 c.arg[0] = 0;
587 c.arg[1] = Word;
588 c.arg[2] = Password;
589 SendCommand(&c);
f38a1528 590 WaitForResponse(CMD_ACK, NULL);
b44e5233 591
f6c18637 592 uint8_t data[LF_TRACE_BUFF_SIZE] = {0x00};
f38a1528 593
b44e5233 594 GetFromBigBuf(data,LF_TRACE_BUFF_SIZE,3560); //3560 -- should be offset..
f38a1528 595 WaitForResponseTimeout(CMD_ACK,NULL, 1500);
596
b44e5233 597 for (int j = 0; j < LF_TRACE_BUFF_SIZE; j++) {
f6c18637 598 GraphBuffer[j] = ((int)data[j]);
f38a1528 599 }
b44e5233 600 GraphTraceLen = LF_TRACE_BUFF_SIZE;
b44e5233 601
c6be64da 602 uint8_t bits[LF_BITSSTREAM_LEN] = {0x00};
603 uint8_t * bitstream = bits;
604 manchester_decode(GraphBuffer, LF_TRACE_BUFF_SIZE, bitstream, LF_BITSSTREAM_LEN);
f6c18637 605 RepaintGraphWindow();
54a942b0 606 return 0;
607}
608
609int CmdWriteWord(const char *Cmd)
610{
611 int Word = 16; //default to invalid block
612 int Data = 0xFFFFFFFF; //default to blank data
613 UsbCommand c;
614
615 sscanf(Cmd, "%x %d", &Data, &Word);
616
617 if (Word > 15) {
618 PrintAndLog("Word must be between 0 and 15");
619 return 1;
620 }
621
a61b4976 622 PrintAndLog("Writing word %d with data %08X", Word, Data);
54a942b0 623
624 c.cmd = CMD_EM4X_WRITE_WORD;
625 c.d.asBytes[0] = 0x0; //Normal mode
626 c.arg[0] = Data;
627 c.arg[1] = Word;
628 c.arg[2] = 0;
629 SendCommand(&c);
630 return 0;
631}
632
633int CmdWriteWordPWD(const char *Cmd)
634{
a61b4976 635 int Word = 16; //default to invalid word
54a942b0 636 int Data = 0xFFFFFFFF; //default to blank data
637 int Password = 0xFFFFFFFF; //default to blank password
638 UsbCommand c;
639
640 sscanf(Cmd, "%x %d %x", &Data, &Word, &Password);
641
642 if (Word > 15) {
643 PrintAndLog("Word must be between 0 and 15");
644 return 1;
645 }
646
a61b4976 647 PrintAndLog("Writing word %d with data %08X and password %08X", Word, Data, Password);
54a942b0 648
649 c.cmd = CMD_EM4X_WRITE_WORD;
650 c.d.asBytes[0] = 0x1; //Password mode
651 c.arg[0] = Data;
652 c.arg[1] = Word;
653 c.arg[2] = Password;
654 SendCommand(&c);
655 return 0;
656}
657
2d4eae76 658static command_t CommandTable[] =
7fe9b0b7 659{
54a942b0 660 {"help", CmdHelp, 1, "This help"},
6ff6ade2 661 {"410xdemod", CmdEMdemodASK, 0, "[clock rate] -- Extract ID from EM410x tag"},
c2d25819 662 {"410xread", CmdEM410xRead, 1, "[clock rate] -- Extract ID from EM410x tag"},
663 {"410xsim", CmdEM410xSim, 0, "<UID> -- Simulate EM410x tag"},
c15d2bdc 664 {"replay", MWRem4xReplay, 0, "Watches for tag and simulates manchester encoded em4x tag"},
c2d25819 665 {"410xwatch", CmdEM410xWatch, 0, "['h'] -- Watches for EM410x 125/134 kHz tags (option 'h' for 134)"},
666 {"410xspoof", CmdEM410xWatchnSpoof, 0, "['h'] --- Watches for EM410x 125/134 kHz tags, and replays them. (option 'h' for 134)" },
667 {"410xwrite", CmdEM410xWrite, 1, "<UID> <'0' T5555> <'1' T55x7> [clock rate] -- Write EM410x UID to T5555(Q5) or T55x7 tag, optionally setting clock rate"},
668 {"4x50read", CmdEM4x50Read, 1, "Extract data from EM4x50 tag"},
f38a1528 669 {"rd", CmdReadWord, 1, "<Word 1-15> -- Read EM4xxx word data"},
670 {"rdpwd", CmdReadWordPWD, 1, "<Word 1-15> <Password> -- Read EM4xxx word data in password mode "},
671 {"wr", CmdWriteWord, 1, "<Data> <Word 1-15> -- Write EM4xxx word data"},
672 {"wrpwd", CmdWriteWordPWD, 1, "<Data> <Word 1-15> <Password> -- Write EM4xxx word data in password mode"},
7fe9b0b7 673 {NULL, NULL, 0, NULL}
674};
675
c15d2bdc 676
677//Confirms the parity of a bitstream as well as obtaining the data (TagID) from within the appropriate memory space.
678//Arguments:
679// Pointer to a string containing the desired bitsream
680// Pointer to a string that will receive the decoded tag ID
681// Length of the bitsream pointed at in the first argument, char* _strBitStream
682//Retuns:
683//1 Parity confirmed
684//0 Parity not confirmed
685int ConfirmEm410xTagParity( char* _strBitStream, char* pID, int LengthOfBitstream )
686{
687 int i = 0;
688 int rows = 0;
689 int Parity[4] = {0x00};
690 char ID[11] = {0x00};
691 int k = 0;
692 int BitStream[70] = {0x00};
693 int counter = 0;
694 //prepare variables
695 for ( i = 0; i <= LengthOfBitstream; i++)
696 {
697 if (_strBitStream[i] == '1')
698 {
699 k =1;
700 memcpy(&BitStream[i], &k,4);
701 }
702 else if (_strBitStream[i] == '0')
703 {
704 k = 0;
705 memcpy(&BitStream[i], &k,4);
706 }
707 }
708 while ( counter < 2 )
709 {
710 //set/reset variables and counters
711 memset(ID,0x00,sizeof(ID));
712 memset(Parity,0x00,sizeof(Parity));
713 rows = 0;
714 for ( i = 9; i <= LengthOfBitstream; i++)
715 {
716 if ( rows < 10 )
717 {
718 if ((BitStream[i] ^ BitStream[i+1] ^ BitStream[i+2] ^ BitStream[i+3]) == BitStream[i+4])
719 {
720 sprintf(ID+rows, "%x", (8 * BitStream[i]) + (4 * BitStream[i+1]) + (2 * BitStream[i+2]) + (1 * BitStream[i+3]));
721 rows++;
722 /* Keep parity info and move four bits ahead*/
723 Parity[0] ^= BitStream[i];
724 Parity[1] ^= BitStream[i+1];
725 Parity[2] ^= BitStream[i+2];
726 Parity[3] ^= BitStream[i+3];
727 i += 4;
728 }
729 }
730 if ( rows == 10 )
731 {
732 if ( BitStream[i] == Parity[0] && BitStream[i+1] == Parity[1] &&
733 BitStream[i+2] == Parity[2] && BitStream[i+3] == Parity[3] &&
734 BitStream[i+4] == 0)
735 {
736 memcpy(pID,ID,strlen(ID));
737 return 1;
738 }
739 }
740 }
741 printf("[PARITY ->]Failed. Flipping Bits, and rechecking parity for bitstream:\n[PARITY ->]");
742 for (k = 0; k < LengthOfBitstream; k++)
743 {
744 BitStream[k] ^= 1;
745 printf("%i", BitStream[k]);
746 }
747 puts(" ");
748 counter++;
749 }
750 return 0;
751}
752//Reads and demodulates an em410x RFID tag. It further allows slight modification to the decoded bitstream
753//Once a suitable bitstream has been identified, and if needed, modified, it is replayed. Allowing emulation of the
754//"stolen" rfid tag.
755//No meaningful returns or arguments.
756int MWRem4xReplay(const char* Cmd)
757{
758 // //header traces
759 // static char ArrayTraceZero[] = { '0','0','0','0','0','0','0','0','0' };
760 // static char ArrayTraceOne[] = { '1','1','1','1','1','1','1','1','1' };
761 // //local string variables
762 // char strClockRate[10] = {0x00};
763 // char strAnswer[4] = {0x00};
764 // char strTempBufferMini[2] = {0x00};
765 // //our outbound bit-stream
766 // char strSimulateBitStream[65] = {0x00};
767 // //integers
768 // int iClockRate = 0;
769 // int needle = 0;
770 // int j = 0;
771 // int iFirstHeaderOffset = 0x00000000;
772 // int numManchesterDemodBits=0;
773 // //boolean values
774 // bool bInverted = false;
775 // //pointers to strings. memory will be allocated.
776 // char* pstrInvertBitStream = 0x00000000;
777 // char* pTempBuffer = 0x00000000;
778 // char* pID = 0x00000000;
779 // char* strBitStreamBuffer = 0x00000000;
780
781
782 // puts("###################################");
783 // puts("#### Em4x Replay ##");
784 // puts("#### R.A.M. June 2013 ##");
785 // puts("###################################");
786 // //initialize
787 // CmdLFRead("");
788 // //Collect ourselves 10,000 samples
789 // CmdSamples("10000");
790 // puts("[->]preforming ASK demodulation\n");
791 // //demodulate ask
792 // Cmdaskdemod("0");
793 // iClockRate = DetectClock(0);
794 // sprintf(strClockRate, "%i\n",iClockRate);
795 // printf("[->]Detected ClockRate: %s\n", strClockRate);
796
797 // //If detected clock rate is something completely unreasonable, dont go ahead
798 // if ( iClockRate < 0xFFFE )
799 // {
800 // pTempBuffer = (char*)malloc(MAX_GRAPH_TRACE_LEN);
801 // if (pTempBuffer == 0x00000000)
802 // return 0;
803 // memset(pTempBuffer,0x00,MAX_GRAPH_TRACE_LEN);
804 // //Preform manchester de-modulation and display in a single line.
805 // numManchesterDemodBits = CmdManchesterDemod( strClockRate );
806 // //note: numManchesterDemodBits is set above in CmdManchesterDemod()
807 // if ( numManchesterDemodBits == 0 )
808 // return 0;
809 // strBitStreamBuffer = malloc(numManchesterDemodBits+1);
810 // if ( strBitStreamBuffer == 0x00000000 )
811 // return 0;
812 // memset(strBitStreamBuffer, 0x00, (numManchesterDemodBits+1));
813 // //fill strBitStreamBuffer with demodulated, string formatted bits.
814 // for ( j = 0; j <= numManchesterDemodBits; j++ )
815 // {
816 // sprintf(strTempBufferMini, "%i",BitStream[j]);
817 // strcat(strBitStreamBuffer,strTempBufferMini);
818 // }
819 // printf("[->]Demodulated Bitstream: \n%s\n", strBitStreamBuffer);
820 // //Reset counter and select most probable bit stream
821 // j = 0;
822 // while ( j < numManchesterDemodBits )
823 // {
824 // memset(strSimulateBitStream,0x00,64);
825 // //search for header of nine (9) 0's : 000000000 or nine (9) 1's : 1111 1111 1
826 // if ( ( strncmp(strBitStreamBuffer+j, ArrayTraceZero, sizeof(ArrayTraceZero)) == 0 ) ||
827 // ( strncmp(strBitStreamBuffer+j, ArrayTraceOne, sizeof(ArrayTraceOne)) == 0 ) )
828 // {
829 // iFirstHeaderOffset = j;
830 // memcpy(strSimulateBitStream, strBitStreamBuffer+j,64);
831 // printf("[->]Offset of Header");
832 // if ( strncmp(strBitStreamBuffer+iFirstHeaderOffset, "0", 1) == 0 )
833 // printf("'%s'", ArrayTraceZero );
834 // else
835 // printf("'%s'", ArrayTraceOne );
836 // printf(": %i\nHighlighted string : %s\n",iFirstHeaderOffset,strSimulateBitStream);
837 // //allow us to escape loop or choose another frame
838 // puts("[<-]Are we happy with this sample? [Y]es/[N]o");
839 // gets(strAnswer);
840 // if ( ( strncmp(strAnswer,"y",1) == 0 ) || ( strncmp(strAnswer,"Y",1) == 0 ) )
841 // {
842 // j = numManchesterDemodBits+1;
843 // break;
844 // }
845 // }
846 // j++;
847 // }
848 // }
849 // else return 0;
850
851 // //Do we want the buffer inverted?
852 // memset(strAnswer, 0x00, sizeof(strAnswer));
853 // printf("[<-]Do you wish to invert the highlighted bitstream? [Y]es/[N]o\n");
854 // gets(strAnswer);
855 // if ( ( strncmp("y", strAnswer,1) == 0 ) || ( strncmp("Y", strAnswer, 1 ) == 0 ) )
856 // {
857 // //allocate heap memory
858 // pstrInvertBitStream = (char*)malloc(numManchesterDemodBits);
859 // if ( pstrInvertBitStream != 0x00000000 )
860 // {
861 // memset(pstrInvertBitStream,0x00,numManchesterDemodBits);
862 // bInverted = true;
863 // //Invert Bitstream
864 // for ( needle = 0; needle <= numManchesterDemodBits; needle++ )
865 // {
866 // if (strSimulateBitStream[needle] == '0')
867 // strcat(pstrInvertBitStream,"1");
868 // else if (strSimulateBitStream[needle] == '1')
869 // strcat(pstrInvertBitStream,"0");
870 // }
871 // printf("[->]Inverted bitstream: %s\n", pstrInvertBitStream);
872 // }
873 // }
874 // //Confirm parity of selected string
875 // pID = (char*)malloc(11);
876 // if (pID != 0x00000000)
877 // {
878 // memset(pID, 0x00, 11);
879 // if (ConfirmEm410xTagParity(strSimulateBitStream,pID, 64) == 1)
880 // {
881 // printf("[->]Parity confirmed for selected bitstream!\n");
882 // printf("[->]Tag ID was detected as: [hex]:%s\n",pID );
883 // }
884 // else
885 // printf("[->]Parity check failed for the selected bitstream!\n");
886 // }
887
888 // //Spoof
889 // memset(strAnswer, 0x00, sizeof(strAnswer));
890 // printf("[<-]Do you wish to continue with the EM4x simulation? [Y]es/[N]o\n");
891 // gets(strAnswer);
892 // if ( ( strncmp(strAnswer,"y",1) == 0 ) || ( strncmp(strAnswer,"Y",1) == 0 ) )
893 // {
894 // strcat(pTempBuffer, strClockRate);
895 // strcat(pTempBuffer, " ");
896 // if (bInverted == true)
897 // strcat(pTempBuffer,pstrInvertBitStream);
898 // if (bInverted == false)
899 // strcat(pTempBuffer,strSimulateBitStream);
900 // //inform the user
901 // puts("[->]Starting simulation now: \n");
902 // //Simulate tag with prepared buffer.
903 // CmdLFSimManchester(pTempBuffer);
904 // }
905 // else if ( ( strcmp("n", strAnswer) == 0 ) || ( strcmp("N", strAnswer ) == 0 ) )
906 // printf("[->]Exiting procedure now...\n");
907 // else
908 // printf("[->]Erroneous selection\nExiting procedure now....\n");
909
910 // //Clean up -- Exit function
911 // //clear memory, then release pointer.
912 // if ( pstrInvertBitStream != 0x00000000 )
913 // {
914 // memset(pstrInvertBitStream,0x00,numManchesterDemodBits);
915 // free(pstrInvertBitStream);
916 // }
917 // if ( pTempBuffer != 0x00000000 )
918 // {
919 // memset(pTempBuffer,0x00,MAX_GRAPH_TRACE_LEN);
920 // free(pTempBuffer);
921 // }
922 // if ( pID != 0x00000000 )
923 // {
924 // memset(pID,0x00,11);
925 // free(pID);
926 // }
927 // if ( strBitStreamBuffer != 0x00000000 )
928 // {
929 // memset(strBitStreamBuffer,0x00,numManchesterDemodBits);
930 // free(strBitStreamBuffer);
931 // }
932 return 0;
933}
934
7fe9b0b7 935int CmdLFEM4X(const char *Cmd)
936{
937 CmdsParse(CommandTable, Cmd);
938 return 0;
939}
940
941int CmdHelp(const char *Cmd)
942{
943 CmdsHelp(CommandTable);
944 return 0;
945}
Impressum, Datenschutz