]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfem4x.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
7 //-----------------------------------------------------------------------------
8 // Low frequency EM4x commands
9 //-----------------------------------------------------------------------------
14 //#include "proxusb.h"
15 #include "proxmark3.h"
18 #include "cmdparser.h"
21 #include "cmdlfem4x.h"
23 static int CmdHelp(const char *Cmd
);
27 int CmdEMdemodASK(const char *Cmd
)
30 UsbCommand c
={CMD_EM410X_DEMOD
};
31 if(Cmd
[0]=='1') findone
=1;
39 /* Read the ID of an EM410x tag.
41 * 1111 1111 1 <-- standard non-repeatable header
42 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
44 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
45 * 0 <-- stop bit, end of tag
47 int CmdEM410xRead(const char *Cmd
)
49 int i
, j
, clock
, header
, rows
, bit
, hithigh
, hitlow
, first
, bit2idx
, high
, low
;
54 uint8_t BitStream
[MAX_GRAPH_TRACE_LEN
];
57 /* Detect high and lows and clock */
58 for (i
= 0; i
< GraphTraceLen
; i
++)
60 if (GraphBuffer
[i
] > high
)
61 high
= GraphBuffer
[i
];
62 else if (GraphBuffer
[i
] < low
)
67 clock
= GetClock(Cmd
, high
, 0);
69 /* parity for our 4 columns */
70 parity
[0] = parity
[1] = parity
[2] = parity
[3] = 0;
73 /* manchester demodulate */
75 for (i
= 0; i
< (int)(GraphTraceLen
/ clock
); i
++)
81 /* Find out if we hit both high and low peaks */
82 for (j
= 0; j
< clock
; j
++)
84 if (GraphBuffer
[(i
* clock
) + j
] == high
)
86 else if (GraphBuffer
[(i
* clock
) + j
] == low
)
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
))
96 if (hithigh
&& hitlow
)
100 /* If we didn't hit both high and low peaks, we had a bit transition */
101 if (!hithigh
|| !hitlow
)
104 BitStream
[bit2idx
++] = bit
;
108 /* We go till 5 before the graph ends because we'll get that far below */
109 for (i
= 1; i
< bit2idx
- 5; i
++)
111 /* Step 2: We have our header but need our tag ID */
112 if (header
== 9 && rows
< 10)
114 /* Confirm parity is correct */
115 if ((BitStream
[i
] ^ BitStream
[i
+1] ^ BitStream
[i
+2] ^ BitStream
[i
+3]) == BitStream
[i
+4])
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
]));
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];
128 /* Move 4 bits ahead */
132 /* Damn, something wrong! reset */
135 PrintAndLog("Thought we had a valid tag but failed at word %d (i=%d)", rows
+ 1, i
);
137 /* Start back rows * 5 + 9 header bits, -1 to not start at same place */
138 i
-= 9 + (5 * rows
) - 5;
144 /* Step 3: Got our 40 bits! confirm column parity */
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] &&
153 PrintAndLog("EM410x Tag ID: %s", id
);
154 PrintAndLog("Unique Tag ID: %s", id2
);
160 /* Crap! Incorrect parity or no stop bit, start all over */
165 /* Go back 59 bits (9 header bits + 10 rows at 4+1 parity) */
170 /* Step 1: get our header */
173 /* Need 9 consecutive 1's */
174 if (BitStream
[i
] == 1)
177 /* We don't have a header, not enough consecutive 1 bits */
183 /* if we've already retested after flipping bits, return */
187 /* if this didn't work, try flipping bits */
188 for (i
= 0; i
< bit2idx
; i
++)
194 /* emulate an EM410X tag
196 * 1111 1111 1 <-- standard non-repeatable header
197 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
199 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
200 * 0 <-- stop bit, end of tag
202 int CmdEM410xSim(const char *Cmd
)
204 int i
, n
, j
, h
, binary
[4], parity
[4];
206 /* clock is 64 in EM410x tags */
209 /* clear our graph */
212 /* write it out a few times */
213 for (h
= 0; h
< 4; h
++)
215 /* write 9 start bits */
216 for (i
= 0; i
< 9; i
++)
217 AppendGraph(0, clock
, 1);
219 /* for each hex char */
220 parity
[0] = parity
[1] = parity
[2] = parity
[3] = 0;
221 for (i
= 0; i
< 10; i
++)
223 /* read each hex char */
224 sscanf(&Cmd
[i
], "%1x", &n
);
225 for (j
= 3; j
>= 0; j
--, n
/= 2)
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]);
234 /* append parity bit */
235 AppendGraph(0, clock
, binary
[0] ^ binary
[1] ^ binary
[2] ^ binary
[3]);
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];
245 AppendGraph(0, clock
, parity
[0]);
246 AppendGraph(0, clock
, parity
[1]);
247 AppendGraph(0, clock
, parity
[2]);
248 AppendGraph(0, clock
, parity
[3]);
251 AppendGraph(0, clock
, 0);
254 /* modulate that biatch */
255 CmdManchesterMod("");
258 RepaintGraphWindow();
264 /* Function is equivalent of loread + losamples + em410xread
265 * looped until an EM410x tag is detected */
266 int CmdEM410xWatch(const char *Cmd
)
268 int read_h
= (*Cmd
== 'h');
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
275 // TBD: Auto-grow sample size based on detected sample rate. IE: If the
276 // rate gets lower, then grow the number of samples
278 // Changed by martin, 4000 x 4 = 16000,
279 // see http://www.proxmark.org/forum/viewtopic.php?pid=7235#p7235
281 } while ( ! CmdEM410xRead(""));
285 /* Read the transmitted data of an EM4x50 tag
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
294 * LW <- Listen Window
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)).
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.
304 int CmdEM4x50Read(const char *Cmd
)
306 int i
, j
, startblock
, skip
, block
, start
, end
, low
, high
;
307 bool complete
= false;
308 int tmpbuff
[MAX_GRAPH_TRACE_LEN
/ 64];
312 memset(tmpbuff
, 0, MAX_GRAPH_TRACE_LEN
/ 64);
314 /* first get high and low values */
315 for (i
= 0; i
< GraphTraceLen
; i
++)
317 if (GraphBuffer
[i
] > high
)
318 high
= GraphBuffer
[i
];
319 else if (GraphBuffer
[i
] < low
)
320 low
= GraphBuffer
[i
];
323 /* populate a buffer with pulse lengths */
326 while (i
< GraphTraceLen
)
328 // measure from low to low
329 while ((GraphBuffer
[i
] > low
) && (i
<GraphTraceLen
))
332 while ((GraphBuffer
[i
] < high
) && (i
<GraphTraceLen
))
334 while ((GraphBuffer
[i
] > low
) && (i
<GraphTraceLen
))
336 if (j
>=(MAX_GRAPH_TRACE_LEN
/64)) {
339 tmpbuff
[j
++]= i
- start
;
342 /* look for data start - should be 2 pairs of LW (pulses of 192,128) */
345 for (i
= 0; i
< j
- 4 ; ++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)
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
)
365 /* now do it again to find the end */
367 for (i
+= 3; i
< j
- 4 ; ++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)
381 PrintAndLog("Found data at sample: %i",skip
);
384 PrintAndLog("No data found!");
385 PrintAndLog("Try again with more samples.");
391 PrintAndLog("*** Warning!");
392 PrintAndLog("Partial data - no end found!");
393 PrintAndLog("Try again with more samples.");
396 /* get rid of leading crap */
397 sprintf(tmp
,"%i",skip
);
400 /* now work through remaining buffer printing out data blocks */
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");
410 /* look for LW before start of next block */
411 for ( ; i
< j
- 4 ; ++i
)
414 if (tmpbuff
[i
] >= 190 && tmpbuff
[i
] <= 194)
415 if (tmpbuff
[i
+1] >= 126 && tmpbuff
[i
+1] <= 130)
418 while (GraphBuffer
[skip
] > low
)
421 sprintf(tmp
,"%i",skip
);
429 int CmdEM410xWrite(const char *Cmd
)
431 uint64_t id
= 0xFFFFFFFFFFFFFFFF; // invalid id value
432 int card
= 0xFF; // invalid card value
433 unsigned int clock
= 0; // invalid clock value
435 sscanf(Cmd
, "%" PRIx64
" %d %d", &id
, &card
, &clock
);
438 if (id
== 0xFFFFFFFFFFFFFFFF) {
439 PrintAndLog("Error! ID is required.\n");
442 if (id
>= 0x10000000000) {
443 PrintAndLog("Error! Given EM410x ID is longer than 40 bits.\n");
449 PrintAndLog("Error! Card type required.\n");
453 PrintAndLog("Error! Bad card type selected.\n");
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
);
472 PrintAndLog("Error! Clock rate is only supported on T55x7 tags.\n");
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);
485 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64
, "T5555", id
, clock
);
487 PrintAndLog("Error! Bad card type selected.\n");
491 UsbCommand c
= {CMD_EM410X_WRITE_TAG
, {card
, (uint32_t)(id
>> 32), (uint32_t)id
}};
497 int CmdReadWord(const char *Cmd
)
499 int Word
= 16; //default to invalid word
502 sscanf(Cmd
, "%d", &Word
);
505 PrintAndLog("Word must be between 0 and 15");
509 PrintAndLog("Reading word %d", Word
);
511 c
.cmd
= CMD_EM4X_READ_WORD
;
512 c
.d
.asBytes
[0] = 0x0; //Normal mode
520 int CmdReadWordPWD(const char *Cmd
)
522 int Word
= 16; //default to invalid word
523 int Password
= 0xFFFFFFFF; //default to blank password
526 sscanf(Cmd
, "%d %x", &Word
, &Password
);
529 PrintAndLog("Word must be between 0 and 15");
533 PrintAndLog("Reading word %d with password %08X", Word
, Password
);
535 c
.cmd
= CMD_EM4X_READ_WORD
;
536 c
.d
.asBytes
[0] = 0x1; //Password mode
544 int CmdWriteWord(const char *Cmd
)
546 int Word
= 16; //default to invalid block
547 int Data
= 0xFFFFFFFF; //default to blank data
550 sscanf(Cmd
, "%x %d", &Data
, &Word
);
553 PrintAndLog("Word must be between 0 and 15");
557 PrintAndLog("Writting word %d with data %08X", Word
, Data
);
559 c
.cmd
= CMD_EM4X_WRITE_WORD
;
560 c
.d
.asBytes
[0] = 0x0; //Normal mode
568 int CmdWriteWordPWD(const char *Cmd
)
570 int Word
= 8; //default to invalid word
571 int Data
= 0xFFFFFFFF; //default to blank data
572 int Password
= 0xFFFFFFFF; //default to blank password
575 sscanf(Cmd
, "%x %d %x", &Data
, &Word
, &Password
);
578 PrintAndLog("Word must be between 0 and 15");
582 PrintAndLog("Writting word %d with data %08X and password %08X", Word
, Data
, Password
);
584 c
.cmd
= CMD_EM4X_WRITE_WORD
;
585 c
.d
.asBytes
[0] = 0x1; //Password mode
595 static command_t CommandTable
[] =
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
}
611 int CmdLFEM4X(const char *Cmd
)
613 CmdsParse(CommandTable
, Cmd
);
617 int CmdHelp(const char *Cmd
)
619 CmdsHelp(CommandTable
);