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
, "%010llx",lo
);
63 // emulate an EM410X tag
64 int CmdEM410xSim(const char *Cmd
)
66 int i
, n
, j
, binary
[4], parity
[4];
68 char cmdp
= param_getchar(Cmd
, 0);
69 uint8_t uid
[5] = {0x00};
71 if (cmdp
== 'h' || cmdp
== 'H') {
72 PrintAndLog("Usage: lf em4x 410xsim <UID>");
74 PrintAndLog(" sample: lf em4x 410xsim 0F0368568B");
78 if (param_gethex(Cmd
, 0, uid
, 10)) {
79 PrintAndLog("UID must include 10 HEX symbols");
83 PrintAndLog("Starting simulating UID %02X%02X%02X%02X%02X", uid
[0],uid
[1],uid
[2],uid
[3],uid
[4]);
84 PrintAndLog("Press pm3-button to about simulation");
86 /* clock is 64 in EM410x tags */
92 /* write 9 start bits */
93 for (i
= 0; i
< 9; i
++)
94 AppendGraph(0, clock
, 1);
96 /* for each hex char */
97 parity
[0] = parity
[1] = parity
[2] = parity
[3] = 0;
98 for (i
= 0; i
< 10; i
++)
100 /* read each hex char */
101 sscanf(&Cmd
[i
], "%1x", &n
);
102 for (j
= 3; j
>= 0; j
--, n
/= 2)
105 /* append each bit */
106 AppendGraph(0, clock
, binary
[0]);
107 AppendGraph(0, clock
, binary
[1]);
108 AppendGraph(0, clock
, binary
[2]);
109 AppendGraph(0, clock
, binary
[3]);
111 /* append parity bit */
112 AppendGraph(0, clock
, binary
[0] ^ binary
[1] ^ binary
[2] ^ binary
[3]);
114 /* keep track of column parity */
115 parity
[0] ^= binary
[0];
116 parity
[1] ^= binary
[1];
117 parity
[2] ^= binary
[2];
118 parity
[3] ^= binary
[3];
122 AppendGraph(0, clock
, parity
[0]);
123 AppendGraph(0, clock
, parity
[1]);
124 AppendGraph(0, clock
, parity
[2]);
125 AppendGraph(0, clock
, parity
[3]);
128 AppendGraph(1, clock
, 0);
130 CmdLFSim("0"); //240 start_gap.
134 /* Function is equivalent of lf read + data samples + em410xread
135 * looped until an EM410x tag is detected
137 * Why is CmdSamples("16000")?
138 * TBD: Auto-grow sample size based on detected sample rate. IE: If the
139 * rate gets lower, then grow the number of samples
140 * Changed by martin, 4000 x 4 = 16000,
141 * see http://www.proxmark.org/forum/viewtopic.php?pid=7235#p7235
144 int CmdEM410xWatch(const char *Cmd
)
148 printf("\naborted via keyboard!\n");
153 getSamples("8192",true); //capture enough to get 2 full messages
154 } while (!CmdEM410xRead(""));
159 int CmdEM410xWatchnSpoof(const char *Cmd
)
162 PrintAndLog("# Replaying captured ID: %s",global_em410xId
);
167 /* Read the transmitted data of an EM4x50 tag
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 * XXXXXXXX [row parity bit (even)] <- 8 bits plus parity
174 * CCCCCCCC <- column parity bits
176 * LW <- Listen Window
178 * This pattern repeats for every block of data being transmitted.
179 * Transmission starts with two Listen Windows (LW - a modulated
180 * pattern of 320 cycles each (32/32/128/64/64)).
182 * Note that this data may or may not be the UID. It is whatever data
183 * is stored in the blocks defined in the control word First and Last
184 * Word Read values. UID is stored in block 32.
186 int CmdEM4x50Read(const char *Cmd
)
188 int i
, j
, startblock
, skip
, block
, start
, end
, low
, high
;
189 bool complete
= false;
190 int tmpbuff
[MAX_GRAPH_TRACE_LEN
/ 64];
194 memset(tmpbuff
, 0, MAX_GRAPH_TRACE_LEN
/ 64);
196 /* first get high and low values */
197 for (i
= 0; i
< GraphTraceLen
; i
++)
199 if (GraphBuffer
[i
] > high
)
200 high
= GraphBuffer
[i
];
201 else if (GraphBuffer
[i
] < low
)
202 low
= GraphBuffer
[i
];
205 /* populate a buffer with pulse lengths */
208 while (i
< GraphTraceLen
)
210 // measure from low to low
211 while ((GraphBuffer
[i
] > low
) && (i
<GraphTraceLen
))
214 while ((GraphBuffer
[i
] < high
) && (i
<GraphTraceLen
))
216 while ((GraphBuffer
[i
] > low
) && (i
<GraphTraceLen
))
218 if (j
>=(MAX_GRAPH_TRACE_LEN
/64)) {
221 tmpbuff
[j
++]= i
- start
;
224 /* look for data start - should be 2 pairs of LW (pulses of 192,128) */
227 for (i
= 0; i
< j
- 4 ; ++i
)
230 if (tmpbuff
[i
] >= 190 && tmpbuff
[i
] <= 194)
231 if (tmpbuff
[i
+1] >= 126 && tmpbuff
[i
+1] <= 130)
232 if (tmpbuff
[i
+2] >= 190 && tmpbuff
[i
+2] <= 194)
233 if (tmpbuff
[i
+3] >= 126 && tmpbuff
[i
+3] <= 130)
241 /* skip over the remainder of the LW */
242 skip
+= tmpbuff
[i
+1]+tmpbuff
[i
+2];
243 while (skip
< MAX_GRAPH_TRACE_LEN
&& GraphBuffer
[skip
] > low
)
247 /* now do it again to find the end */
249 for (i
+= 3; i
< j
- 4 ; ++i
)
252 if (tmpbuff
[i
] >= 190 && tmpbuff
[i
] <= 194)
253 if (tmpbuff
[i
+1] >= 126 && tmpbuff
[i
+1] <= 130)
254 if (tmpbuff
[i
+2] >= 190 && tmpbuff
[i
+2] <= 194)
255 if (tmpbuff
[i
+3] >= 126 && tmpbuff
[i
+3] <= 130)
263 PrintAndLog("Found data at sample: %i",skip
);
266 PrintAndLog("No data found!");
267 PrintAndLog("Try again with more samples.");
273 PrintAndLog("*** Warning!");
274 PrintAndLog("Partial data - no end found!");
275 PrintAndLog("Try again with more samples.");
278 /* get rid of leading crap */
279 sprintf(tmp
,"%i",skip
);
282 /* now work through remaining buffer printing out data blocks */
287 PrintAndLog("Block %i:", block
);
288 // mandemod routine needs to be split so we can call it for data
289 // just print for now for debugging
290 CmdManchesterDemod("i 64");
292 /* look for LW before start of next block */
293 for ( ; i
< j
- 4 ; ++i
)
296 if (tmpbuff
[i
] >= 190 && tmpbuff
[i
] <= 194)
297 if (tmpbuff
[i
+1] >= 126 && tmpbuff
[i
+1] <= 130)
300 while (GraphBuffer
[skip
] > low
)
303 sprintf(tmp
,"%i",skip
);
311 int CmdEM410xWrite(const char *Cmd
)
313 uint64_t id
= 0xFFFFFFFFFFFFFFFF; // invalid id value
314 int card
= 0xFF; // invalid card value
315 unsigned int clock
= 0; // invalid clock value
317 sscanf(Cmd
, "%" PRIx64
" %d %d", &id
, &card
, &clock
);
320 if (id
== 0xFFFFFFFFFFFFFFFF) {
321 PrintAndLog("Error! ID is required.\n");
324 if (id
>= 0x10000000000) {
325 PrintAndLog("Error! Given EM410x ID is longer than 40 bits.\n");
331 PrintAndLog("Error! Card type required.\n");
335 PrintAndLog("Error! Bad card type selected.\n");
346 // Allowed clock rates: 16, 32 and 64
347 if ((clock
!= 16) && (clock
!= 32) && (clock
!= 64)) {
348 PrintAndLog("Error! Clock rate %d not valid. Supported clock rates are 16, 32 and 64.\n", clock
);
354 PrintAndLog("Error! Clock rate is only supported on T55x7 tags.\n");
359 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64
" (clock rate: %d)", "T55x7", id
, clock
);
360 // NOTE: We really should pass the clock in as a separate argument, but to
361 // provide for backwards-compatibility for older firmware, and to avoid
362 // having to add another argument to CMD_EM410X_WRITE_TAG, we just store
363 // the clock rate in bits 8-15 of the card value
364 card
= (card
& 0xFF) | (((uint64_t)clock
<< 8) & 0xFF00);
367 PrintAndLog("Writing %s tag with UID 0x%010" PRIx64
, "T5555", id
, clock
);
369 PrintAndLog("Error! Bad card type selected.\n");
373 UsbCommand c
= {CMD_EM410X_WRITE_TAG
, {card
, (uint32_t)(id
>> 32), (uint32_t)id
}};
379 int CmdReadWord(const char *Cmd
)
381 int Word
= -1; //default to invalid word
384 sscanf(Cmd
, "%d", &Word
);
386 if ( (Word
> 15) | (Word
< 0) ) {
387 PrintAndLog("Word must be between 0 and 15");
391 PrintAndLog("Reading word %d", Word
);
393 c
.cmd
= CMD_EM4X_READ_WORD
;
394 c
.d
.asBytes
[0] = 0x0; //Normal mode
402 int CmdReadWordPWD(const char *Cmd
)
404 int Word
= -1; //default to invalid word
405 int Password
= 0xFFFFFFFF; //default to blank password
408 sscanf(Cmd
, "%d %x", &Word
, &Password
);
410 if ( (Word
> 15) | (Word
< 0) ) {
411 PrintAndLog("Word must be between 0 and 15");
415 PrintAndLog("Reading word %d with password %08X", Word
, Password
);
417 c
.cmd
= CMD_EM4X_READ_WORD
;
418 c
.d
.asBytes
[0] = 0x1; //Password mode
426 int CmdWriteWord(const char *Cmd
)
428 int Word
= 16; //default to invalid block
429 int Data
= 0xFFFFFFFF; //default to blank data
432 sscanf(Cmd
, "%x %d", &Data
, &Word
);
435 PrintAndLog("Word must be between 0 and 15");
439 PrintAndLog("Writing word %d with data %08X", Word
, Data
);
441 c
.cmd
= CMD_EM4X_WRITE_WORD
;
442 c
.d
.asBytes
[0] = 0x0; //Normal mode
450 int CmdWriteWordPWD(const char *Cmd
)
452 int Word
= 16; //default to invalid word
453 int Data
= 0xFFFFFFFF; //default to blank data
454 int Password
= 0xFFFFFFFF; //default to blank password
457 sscanf(Cmd
, "%x %d %x", &Data
, &Word
, &Password
);
460 PrintAndLog("Word must be between 0 and 15");
464 PrintAndLog("Writing word %d with data %08X and password %08X", Word
, Data
, Password
);
466 c
.cmd
= CMD_EM4X_WRITE_WORD
;
467 c
.d
.asBytes
[0] = 0x1; //Password mode
475 static command_t CommandTable
[] =
477 {"help", CmdHelp
, 1, "This help"},
478 {"em410xdemod", CmdEMdemodASK
, 0, "[findone] -- Extract ID from EM410x tag (option 0 for continuous loop, 1 for only 1 tag)"},
479 {"em410xread", CmdEM410xRead
, 1, "[clock rate] -- Extract ID from EM410x tag"},
480 {"em410xsim", CmdEM410xSim
, 0, "<UID> -- Simulate EM410x tag"},
481 {"em410xwatch", CmdEM410xWatch
, 0, "['h'] -- Watches for EM410x 125/134 kHz tags (option 'h' for 134)"},
482 {"em410xspoof", CmdEM410xWatchnSpoof
, 0, "['h'] --- Watches for EM410x 125/134 kHz tags, and replays them. (option 'h' for 134)" },
483 {"em410xwrite", CmdEM410xWrite
, 1, "<UID> <'0' T5555> <'1' T55x7> [clock rate] -- Write EM410x UID to T5555(Q5) or T55x7 tag, optionally setting clock rate"},
484 {"em4x50read", CmdEM4x50Read
, 1, "Extract data from EM4x50 tag"},
485 {"readword", CmdReadWord
, 1, "<Word> -- Read EM4xxx word data"},
486 {"readwordPWD", CmdReadWordPWD
, 1, "<Word> <Password> -- Read EM4xxx word data in password mode"},
487 {"writeword", CmdWriteWord
, 1, "<Data> <Word> -- Write EM4xxx word data"},
488 {"writewordPWD", CmdWriteWordPWD
, 1, "<Data> <Word> <Password> -- Write EM4xxx word data in password mode"},
489 {NULL
, NULL
, 0, NULL
}
492 int CmdLFEM4X(const char *Cmd
)
494 CmdsParse(CommandTable
, Cmd
);
498 int CmdHelp(const char *Cmd
)
500 CmdsHelp(CommandTable
);