]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfem4x.c
5faf48012b6f5ad35567ef91915c8c161181fded
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 "proxmark3.h"
18 #include "cmdparser.h"
21 #include "cmdlfem4x.h"
22 char *global_em410xId
;
24 static int CmdHelp(const char *Cmd
);
26 int CmdEMdemodASK(const char *Cmd
)
28 char cmdp
= param_getchar(Cmd
, 0);
29 int findone
= (cmdp
== '1') ? 1 : 0;
30 UsbCommand c
={CMD_EM410X_DEMOD
};
36 /* Read the ID of an EM410x tag.
38 * 1111 1111 1 <-- standard non-repeatable header
39 * XXXX [row parity bit] <-- 10 rows of 5 bits for our 40 bit tag ID
41 * CCCC <-- each bit here is parity for the 10 bits above in corresponding column
42 * 0 <-- stop bit, end of tag
44 int CmdEM410xRead(const char *Cmd
)
49 if(!AskEm410xDemod("", &hi
, &lo
)) return 0;
50 PrintAndLog("EM410x pattern found: ");
53 PrintAndLog ("EM410x XL pattern found");
57 sprintf(id
, "%010x", lo
);
62 // emulate an EM410X tag
63 int CmdEM410xSim(const char *Cmd
)
65 int i
, n
, j
, binary
[4], parity
[4];
67 char cmdp
= param_getchar(Cmd
, 0);
68 uint8_t uid
[5] = {0x00};
70 if (cmdp
== 'h' || cmdp
== 'H') {
71 PrintAndLog("Usage: lf em4x 410xsim <UID>");
73 PrintAndLog(" sample: lf em4x 410xsim 0F0368568B");
77 if (param_gethex(Cmd
, 0, uid
, 10)) {
78 PrintAndLog("UID must include 10 HEX symbols");
82 PrintAndLog("Starting simulating UID %02X%02X%02X%02X%02X", uid
[0],uid
[1],uid
[2],uid
[3],uid
[4]);
83 PrintAndLog("Press pm3-button to about simulation");
85 /* clock is 64 in EM410x tags */
91 /* write 9 start bits */
92 for (i
= 0; i
< 9; i
++)
93 AppendGraph(0, clock
, 1);
95 /* for each hex char */
96 parity
[0] = parity
[1] = parity
[2] = parity
[3] = 0;
97 for (i
= 0; i
< 10; i
++)
99 /* read each hex char */
100 sscanf(&Cmd
[i
], "%1x", &n
);
101 for (j
= 3; j
>= 0; j
--, n
/= 2)
104 /* append each bit */
105 AppendGraph(0, clock
, binary
[0]);
106 AppendGraph(0, clock
, binary
[1]);
107 AppendGraph(0, clock
, binary
[2]);
108 AppendGraph(0, clock
, binary
[3]);
110 /* append parity bit */
111 AppendGraph(0, clock
, binary
[0] ^ binary
[1] ^ binary
[2] ^ binary
[3]);
113 /* keep track of column parity */
114 parity
[0] ^= binary
[0];
115 parity
[1] ^= binary
[1];
116 parity
[2] ^= binary
[2];
117 parity
[3] ^= binary
[3];
121 AppendGraph(0, clock
, parity
[0]);
122 AppendGraph(0, clock
, parity
[1]);
123 AppendGraph(0, clock
, parity
[2]);
124 AppendGraph(0, clock
, parity
[3]);
127 AppendGraph(1, clock
, 0);
129 CmdLFSim("0"); //240 start_gap.
133 /* Function is equivalent of lf read + data samples + em410xread
134 * looped until an EM410x tag is detected
136 * Why is CmdSamples("16000")?
137 * TBD: Auto-grow sample size based on detected sample rate. IE: If the
138 * rate gets lower, then grow the number of samples
139 * Changed by martin, 4000 x 4 = 16000,
140 * see http://www.proxmark.org/forum/viewtopic.php?pid=7235#p7235
143 int CmdEM410xWatch(const char *Cmd
)
147 printf("\naborted via keyboard!\n");
153 } while (!CmdEM410xRead(""));
158 int CmdEM410xWatchnSpoof(const char *Cmd
)
161 PrintAndLog("# Replaying captured ID: %s",global_em410xId
);
166 /* Read the transmitted data of an EM4x50 tag
169 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
170 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
171 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
172 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
173 * CCCCCCCC <- column parity bits
175 * LW <- Listen Window
177 * This pattern repeats for every block of data being transmitted.
178 * Transmission starts with two Listen Windows (LW - a modulated
179 * pattern of 320 cycles each (32/32/128/64/64)).
181 * Note that this data may or may not be the UID. It is whatever data
182 * is stored in the blocks defined in the control word First and Last
183 * Word Read values. UID is stored in block 32.
185 int CmdEM4x50Read(const char *Cmd
)
187 int i
, j
, startblock
, skip
, block
, start
, end
, low
, high
;
188 bool complete
= false;
189 int tmpbuff
[MAX_GRAPH_TRACE_LEN
/ 64];
193 memset(tmpbuff
, 0, MAX_GRAPH_TRACE_LEN
/ 64);
195 /* first get high and low values */
196 for (i
= 0; i
< GraphTraceLen
; i
++)
198 if (GraphBuffer
[i
] > high
)
199 high
= GraphBuffer
[i
];
200 else if (GraphBuffer
[i
] < low
)
201 low
= GraphBuffer
[i
];
204 /* populate a buffer with pulse lengths */
207 while (i
< GraphTraceLen
)
209 // measure from low to low
210 while ((GraphBuffer
[i
] > low
) && (i
<GraphTraceLen
))
213 while ((GraphBuffer
[i
] < high
) && (i
<GraphTraceLen
))
215 while ((GraphBuffer
[i
] > low
) && (i
<GraphTraceLen
))
217 if (j
>=(MAX_GRAPH_TRACE_LEN
/64)) {
220 tmpbuff
[j
++]= i
- start
;
223 /* look for data start - should be 2 pairs of LW (pulses of 192,128) */
226 for (i
= 0; i
< j
- 4 ; ++i
)
229 if (tmpbuff
[i
] >= 190 && tmpbuff
[i
] <= 194)
230 if (tmpbuff
[i
+1] >= 126 && tmpbuff
[i
+1] <= 130)
231 if (tmpbuff
[i
+2] >= 190 && tmpbuff
[i
+2] <= 194)
232 if (tmpbuff
[i
+3] >= 126 && tmpbuff
[i
+3] <= 130)
240 /* skip over the remainder of the LW */
241 skip
+= tmpbuff
[i
+1]+tmpbuff
[i
+2];
242 while (skip
< MAX_GRAPH_TRACE_LEN
&& GraphBuffer
[skip
] > low
)
246 /* now do it again to find the end */
248 for (i
+= 3; i
< j
- 4 ; ++i
)
251 if (tmpbuff
[i
] >= 190 && tmpbuff
[i
] <= 194)
252 if (tmpbuff
[i
+1] >= 126 && tmpbuff
[i
+1] <= 130)
253 if (tmpbuff
[i
+2] >= 190 && tmpbuff
[i
+2] <= 194)
254 if (tmpbuff
[i
+3] >= 126 && tmpbuff
[i
+3] <= 130)
262 PrintAndLog("Found data at sample: %i",skip
);
265 PrintAndLog("No data found!");
266 PrintAndLog("Try again with more samples.");
272 PrintAndLog("*** Warning!");
273 PrintAndLog("Partial data - no end found!");
274 PrintAndLog("Try again with more samples.");
277 /* get rid of leading crap */
278 sprintf(tmp
,"%i",skip
);
281 /* now work through remaining buffer printing out data blocks */
286 PrintAndLog("Block %i:", block
);
287 // mandemod routine needs to be split so we can call it for data
288 // just print for now for debugging
289 CmdManchesterDemod("i 64");
291 /* look for LW before start of next block */
292 for ( ; i
< j
- 4 ; ++i
)
295 if (tmpbuff
[i
] >= 190 && tmpbuff
[i
] <= 194)
296 if (tmpbuff
[i
+1] >= 126 && tmpbuff
[i
+1] <= 130)
299 while (GraphBuffer
[skip
] > low
)
302 sprintf(tmp
,"%i",skip
);
310 int CmdEM410xWrite(const char *Cmd
)
312 uint64_t id
= 0xFFFFFFFFFFFFFFFF; // invalid id value
313 int card
= 0xFF; // invalid card value
314 unsigned int clock
= 0; // invalid clock value
316 sscanf(Cmd
, "%" PRIx64
" %d %d", &id
, &card
, &clock
);
319 if (id
== 0xFFFFFFFFFFFFFFFF) {
320 PrintAndLog("Error! ID is required.\n");
323 if (id
>= 0x10000000000) {
324 PrintAndLog("Error! Given EM410x ID is longer than 40 bits.\n");
330 PrintAndLog("Error! Card type required.\n");
334 PrintAndLog("Error! Bad card type selected.\n");
345 // Allowed clock rates: 16, 32 and 64
346 if ((clock
!= 16) && (clock
!= 32) && (clock
!= 64)) {
347 PrintAndLog("Error! Clock rate %d not valid. Supported clock rates are 16, 32 and 64.\n", clock
);
353 PrintAndLog("Error! Clock rate is only supported on T55x7 tags.\n");
358 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64
" (clock rate: %d)", "T55x7", id
, clock
);
359 // NOTE: We really should pass the clock in as a separate argument, but to
360 // provide for backwards-compatibility for older firmware, and to avoid
361 // having to add another argument to CMD_EM410X_WRITE_TAG, we just store
362 // the clock rate in bits 8-15 of the card value
363 card
= (card
& 0xFF) | (((uint64_t)clock
<< 8) & 0xFF00);
366 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64
, "T5555", id
, clock
);
368 PrintAndLog("Error! Bad card type selected.\n");
372 UsbCommand c
= {CMD_EM410X_WRITE_TAG
, {card
, (uint32_t)(id
>> 32), (uint32_t)id
}};
378 int CmdReadWord(const char *Cmd
)
380 int Word
= -1; //default to invalid word
383 sscanf(Cmd
, "%d", &Word
);
385 if ( (Word
> 15) | (Word
< 0) ) {
386 PrintAndLog("Word must be between 0 and 15");
390 PrintAndLog("Reading word %d", Word
);
392 c
.cmd
= CMD_EM4X_READ_WORD
;
393 c
.d
.asBytes
[0] = 0x0; //Normal mode
401 int CmdReadWordPWD(const char *Cmd
)
403 int Word
= -1; //default to invalid word
404 int Password
= 0xFFFFFFFF; //default to blank password
407 sscanf(Cmd
, "%d %x", &Word
, &Password
);
409 if ( (Word
> 15) | (Word
< 0) ) {
410 PrintAndLog("Word must be between 0 and 15");
414 PrintAndLog("Reading word %d with password %08X", Word
, Password
);
416 c
.cmd
= CMD_EM4X_READ_WORD
;
417 c
.d
.asBytes
[0] = 0x1; //Password mode
425 int CmdWriteWord(const char *Cmd
)
427 int Word
= 16; //default to invalid block
428 int Data
= 0xFFFFFFFF; //default to blank data
431 sscanf(Cmd
, "%x %d", &Data
, &Word
);
434 PrintAndLog("Word must be between 0 and 15");
438 PrintAndLog("Writing word %d with data %08X", Word
, Data
);
440 c
.cmd
= CMD_EM4X_WRITE_WORD
;
441 c
.d
.asBytes
[0] = 0x0; //Normal mode
449 int CmdWriteWordPWD(const char *Cmd
)
451 int Word
= 16; //default to invalid word
452 int Data
= 0xFFFFFFFF; //default to blank data
453 int Password
= 0xFFFFFFFF; //default to blank password
456 sscanf(Cmd
, "%x %d %x", &Data
, &Word
, &Password
);
459 PrintAndLog("Word must be between 0 and 15");
463 PrintAndLog("Writing word %d with data %08X and password %08X", Word
, Data
, Password
);
465 c
.cmd
= CMD_EM4X_WRITE_WORD
;
466 c
.d
.asBytes
[0] = 0x1; //Password mode
474 static command_t CommandTable
[] =
476 {"help", CmdHelp
, 1, "This help"},
477 {"em410xdemod", CmdEMdemodASK
, 0, "[findone] -- Extract ID from EM410x tag (option 0 for continuous loop, 1 for only 1 tag)"},
478 {"em410xread", CmdEM410xRead
, 1, "[clock rate] -- Extract ID from EM410x tag"},
479 {"em410xsim", CmdEM410xSim
, 0, "<UID> -- Simulate EM410x tag"},
480 {"em410xwatch", CmdEM410xWatch
, 0, "['h'] -- Watches for EM410x 125/134 kHz tags (option 'h' for 134)"},
481 {"em410xspoof", CmdEM410xWatchnSpoof
, 0, "['h'] --- Watches for EM410x 125/134 kHz tags, and replays them. (option 'h' for 134)" },
482 {"em410xwrite", CmdEM410xWrite
, 1, "<UID> <'0' T5555> <'1' T55x7> [clock rate] -- Write EM410x UID to T5555(Q5) or T55x7 tag, optionally setting clock rate"},
483 {"em4x50read", CmdEM4x50Read
, 1, "Extract data from EM4x50 tag"},
484 {"readword", CmdReadWord
, 1, "<Word> -- Read EM4xxx word data"},
485 {"readwordPWD", CmdReadWordPWD
, 1, "<Word> <Password> -- Read EM4xxx word data in password mode"},
486 {"writeword", CmdWriteWord
, 1, "<Data> <Word> -- Write EM4xxx word data"},
487 {"writewordPWD", CmdWriteWordPWD
, 1, "<Data> <Word> <Password> -- Write EM4xxx word data in password mode"},
488 {NULL
, NULL
, 0, NULL
}
491 int CmdLFEM4X(const char *Cmd
)
493 CmdsParse(CommandTable
, Cmd
);
497 int CmdHelp(const char *Cmd
)
499 CmdsHelp(CommandTable
);