]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlf.c
ADD: marshmellows new lf command and DetectClock. (works great!)
[proxmark3-svn] / client / cmdlf.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 commands
9 //-----------------------------------------------------------------------------
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <limits.h>
15 #include "proxmark3.h"
16 #include "data.h"
17 #include "graph.h"
18 #include "ui.h"
19 #include "cmdparser.h"
20 #include "cmdmain.h"
21 #include "cmddata.h"
22 #include "cmdlf.h"
23 #include "cmdlfawid26.h"
24 #include "cmdlfhid.h"
25 #include "cmdlfti.h"
26 #include "cmdlfem4x.h"
27 #include "cmdlfhitag.h"
28 #include "cmdlft55xx.h"
29 #include "cmdlfpcf7931.h"
30 #include "cmdlfio.h"
31
32 static int CmdHelp(const char *Cmd);
33
34 /* send a command before reading */
35 int CmdLFCommandRead(const char *Cmd)
36 {
37 static char dummy[3];
38
39 dummy[0]= ' ';
40
41 UsbCommand c = {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K};
42 sscanf(Cmd, "%"lli" %"lli" %"lli" %s %s", &c.arg[0], &c.arg[1], &c.arg[2],(char*)(&c.d.asBytes),(char*)(&dummy+1));
43 // in case they specified 'h'
44 strcpy((char *)&c.d.asBytes + strlen((char *)c.d.asBytes), dummy);
45 SendCommand(&c);
46 return 0;
47 }
48
49 int CmdFlexdemod(const char *Cmd)
50 {
51 int i;
52 for (i = 0; i < GraphTraceLen; ++i) {
53 if (GraphBuffer[i] < 0) {
54 GraphBuffer[i] = -1;
55 } else {
56 GraphBuffer[i] = 1;
57 }
58 }
59
60 #define LONG_WAIT 100
61 int start;
62 for (start = 0; start < GraphTraceLen - LONG_WAIT; start++) {
63 int first = GraphBuffer[start];
64 for (i = start; i < start + LONG_WAIT; i++) {
65 if (GraphBuffer[i] != first) {
66 break;
67 }
68 }
69 if (i == (start + LONG_WAIT)) {
70 break;
71 }
72 }
73 if (start == GraphTraceLen - LONG_WAIT) {
74 PrintAndLog("nothing to wait for");
75 return 0;
76 }
77
78 GraphBuffer[start] = 2;
79 GraphBuffer[start+1] = -2;
80
81 uint8_t bits[64];
82
83 int bit;
84 i = start;
85 for (bit = 0; bit < 64; bit++) {
86 int j;
87 int sum = 0;
88 for (j = 0; j < 16; j++) {
89 sum += GraphBuffer[i++];
90 }
91 if (sum > 0) {
92 bits[bit] = 1;
93 } else {
94 bits[bit] = 0;
95 }
96 PrintAndLog("bit %d sum %d", bit, sum);
97 }
98
99 for (bit = 0; bit < 64; bit++) {
100 int j;
101 int sum = 0;
102 for (j = 0; j < 16; j++) {
103 sum += GraphBuffer[i++];
104 }
105 if (sum > 0 && bits[bit] != 1) {
106 PrintAndLog("oops1 at %d", bit);
107 }
108 if (sum < 0 && bits[bit] != 0) {
109 PrintAndLog("oops2 at %d", bit);
110 }
111 }
112
113 GraphTraceLen = 32*64;
114 i = 0;
115 int phase = 0;
116 for (bit = 0; bit < 64; bit++) {
117 if (bits[bit] == 0) {
118 phase = 0;
119 } else {
120 phase = 1;
121 }
122 int j;
123 for (j = 0; j < 32; j++) {
124 GraphBuffer[i++] = phase;
125 phase = !phase;
126 }
127 }
128
129 RepaintGraphWindow();
130 return 0;
131 }
132
133 int CmdIndalaDemod(const char *Cmd)
134 {
135 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
136
137 int state = -1;
138 int count = 0;
139 int i, j;
140 // worst case with GraphTraceLen=64000 is < 4096
141 // under normal conditions it's < 2048
142 uint8_t rawbits[4096];
143 int rawbit = 0;
144 int worst = 0, worstPos = 0;
145 PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen / 32);
146 for (i = 0; i < GraphTraceLen-1; i += 2) {
147 count += 1;
148 if ((GraphBuffer[i] > GraphBuffer[i + 1]) && (state != 1)) {
149 if (state == 0) {
150 for (j = 0; j < count - 8; j += 16) {
151 rawbits[rawbit++] = 0;
152 }
153 if ((abs(count - j)) > worst) {
154 worst = abs(count - j);
155 worstPos = i;
156 }
157 }
158 state = 1;
159 count = 0;
160 } else if ((GraphBuffer[i] < GraphBuffer[i + 1]) && (state != 0)) {
161 if (state == 1) {
162 for (j = 0; j < count - 8; j += 16) {
163 rawbits[rawbit++] = 1;
164 }
165 if ((abs(count - j)) > worst) {
166 worst = abs(count - j);
167 worstPos = i;
168 }
169 }
170 state = 0;
171 count = 0;
172 }
173 }
174 PrintAndLog("Recovered %d raw bits", rawbit);
175 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst, worstPos);
176
177 // Finding the start of a UID
178 int uidlen, long_wait;
179 if (strcmp(Cmd, "224") == 0) {
180 uidlen = 224;
181 long_wait = 30;
182 } else {
183 uidlen = 64;
184 long_wait = 29;
185 }
186 int start;
187 int first = 0;
188 for (start = 0; start <= rawbit - uidlen; start++) {
189 first = rawbits[start];
190 for (i = start; i < start + long_wait; i++) {
191 if (rawbits[i] != first) {
192 break;
193 }
194 }
195 if (i == (start + long_wait)) {
196 break;
197 }
198 }
199 if (start == rawbit - uidlen + 1) {
200 PrintAndLog("nothing to wait for");
201 return 0;
202 }
203
204 // Inverting signal if needed
205 if (first == 1) {
206 for (i = start; i < rawbit; i++) {
207 rawbits[i] = !rawbits[i];
208 }
209 }
210
211 // Dumping UID
212 uint8_t bits[224];
213 char showbits[225];
214 showbits[uidlen]='\0';
215 int bit;
216 i = start;
217 int times = 0;
218 if (uidlen > rawbit) {
219 PrintAndLog("Warning: not enough raw bits to get a full UID");
220 for (bit = 0; bit < rawbit; bit++) {
221 bits[bit] = rawbits[i++];
222 // As we cannot know the parity, let's use "." and "/"
223 showbits[bit] = '.' + bits[bit];
224 }
225 showbits[bit+1]='\0';
226 PrintAndLog("Partial UID=%s", showbits);
227 return 0;
228 } else {
229 for (bit = 0; bit < uidlen; bit++) {
230 bits[bit] = rawbits[i++];
231 showbits[bit] = '0' + bits[bit];
232 }
233 times = 1;
234 }
235
236 //convert UID to HEX
237 uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7;
238 int idx;
239 uid1=0;
240 uid2=0;
241 if (uidlen==64){
242 for( idx=0; idx<64; idx++) {
243 if (showbits[idx] == '0') {
244 uid1=(uid1<<1)|(uid2>>31);
245 uid2=(uid2<<1)|0;
246 } else {
247 uid1=(uid1<<1)|(uid2>>31);
248 uid2=(uid2<<1)|1;
249 }
250 }
251 PrintAndLog("UID=%s (%x%08x)", showbits, uid1, uid2);
252 }
253 else {
254 uid3=0;
255 uid4=0;
256 uid5=0;
257 uid6=0;
258 uid7=0;
259 for( idx=0; idx<224; idx++) {
260 uid1=(uid1<<1)|(uid2>>31);
261 uid2=(uid2<<1)|(uid3>>31);
262 uid3=(uid3<<1)|(uid4>>31);
263 uid4=(uid4<<1)|(uid5>>31);
264 uid5=(uid5<<1)|(uid6>>31);
265 uid6=(uid6<<1)|(uid7>>31);
266 if (showbits[idx] == '0') uid7=(uid7<<1)|0;
267 else uid7=(uid7<<1)|1;
268 }
269 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits, uid1, uid2, uid3, uid4, uid5, uid6, uid7);
270 }
271
272 // Checking UID against next occurrences
273 for (; i + uidlen <= rawbit;) {
274 int failed = 0;
275 for (bit = 0; bit < uidlen; bit++) {
276 if (bits[bit] != rawbits[i++]) {
277 failed = 1;
278 break;
279 }
280 }
281 if (failed == 1) {
282 break;
283 }
284 times += 1;
285 }
286 PrintAndLog("Occurrences: %d (expected %d)", times, (rawbit - start) / uidlen);
287
288 // Remodulating for tag cloning
289 GraphTraceLen = 32*uidlen;
290 i = 0;
291 int phase = 0;
292 for (bit = 0; bit < uidlen; bit++) {
293 if (bits[bit] == 0) {
294 phase = 0;
295 } else {
296 phase = 1;
297 }
298 int j;
299 for (j = 0; j < 32; j++) {
300 GraphBuffer[i++] = phase;
301 phase = !phase;
302 }
303 }
304
305 RepaintGraphWindow();
306 return 0;
307 }
308
309 int CmdIndalaClone(const char *Cmd)
310 {
311 unsigned int uid1, uid2, uid3, uid4, uid5, uid6, uid7;
312 UsbCommand c;
313 uid1=0;
314 uid2=0;
315 uid3=0;
316 uid4=0;
317 uid5=0;
318 uid6=0;
319 uid7=0;
320 int n = 0, i = 0;
321
322 if (strchr(Cmd,'l') != 0) {
323 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
324 uid1 = (uid1 << 4) | (uid2 >> 28);
325 uid2 = (uid2 << 4) | (uid3 >> 28);
326 uid3 = (uid3 << 4) | (uid4 >> 28);
327 uid4 = (uid4 << 4) | (uid5 >> 28);
328 uid5 = (uid5 << 4) | (uid6 >> 28);
329 uid6 = (uid6 << 4) | (uid7 >> 28);
330 uid7 = (uid7 << 4) | (n & 0xf);
331 }
332 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1, uid2, uid3, uid4, uid5, uid6, uid7);
333 c.cmd = CMD_INDALA_CLONE_TAG_L;
334 c.d.asDwords[0] = uid1;
335 c.d.asDwords[1] = uid2;
336 c.d.asDwords[2] = uid3;
337 c.d.asDwords[3] = uid4;
338 c.d.asDwords[4] = uid5;
339 c.d.asDwords[5] = uid6;
340 c.d.asDwords[6] = uid7;
341 }
342 else
343 {
344 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
345 uid1 = (uid1 << 4) | (uid2 >> 28);
346 uid2 = (uid2 << 4) | (n & 0xf);
347 }
348 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1, uid2);
349 c.cmd = CMD_INDALA_CLONE_TAG;
350 c.arg[0] = uid1;
351 c.arg[1] = uid2;
352 }
353
354 SendCommand(&c);
355 return 0;
356 }
357
358 int CmdLFRead(const char *Cmd)
359 {
360 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K};
361 // 'h' means higher-low-frequency, 134 kHz
362 if(*Cmd == 'h') {
363 c.arg[0] = 1;
364 } else if (*Cmd == '\0') {
365 c.arg[0] = 0;
366 } else if (sscanf(Cmd, "%"lli, &c.arg[0]) != 1) {
367 PrintAndLog("Samples 1: 'lf read'");
368 PrintAndLog(" 2: 'lf read h'");
369 PrintAndLog(" 3: 'lf read <divisor>'");
370 return 0;
371 }
372 SendCommand(&c);
373 WaitForResponse(CMD_ACK,NULL);
374
375 // load samples
376 CmdSamples("");
377 return 0;
378 }
379
380 static void ChkBitstream(const char *str)
381 {
382 int i;
383
384 /* convert to bitstream if necessary */
385 for (i = 0; i < (int)(GraphTraceLen / 2); i++)
386 {
387 if (GraphBuffer[i] > 1 || GraphBuffer[i] < 0)
388 {
389 CmdBitstream(str);
390 break;
391 }
392 }
393 }
394
395 int CmdLFSim(const char *Cmd)
396 {
397 int i,j;
398
399 static int gap;
400
401 sscanf(Cmd, "%i", &gap);
402
403 /* convert to bitstream if necessary */
404 ChkBitstream(Cmd);
405
406 printf("Sending [%d bytes]", GraphTraceLen);
407 for (i = 0; i < GraphTraceLen; i += USB_CMD_DATA_SIZE) {
408 UsbCommand c={CMD_DOWNLOADED_SIM_SAMPLES_125K, {i, 0, 0}};
409
410 for (j = 0; j < USB_CMD_DATA_SIZE; j++) {
411 c.d.asBytes[j] = GraphBuffer[i+j];
412 }
413 SendCommand(&c);
414 WaitForResponse(CMD_ACK,NULL);
415 printf(".");
416 }
417 printf("\n");
418 PrintAndLog("Starting to simulate");
419 UsbCommand c = {CMD_SIMULATE_TAG_125K, {GraphTraceLen, gap, 0}};
420 SendCommand(&c);
421 return 0;
422 }
423
424 int CmdLFSimBidir(const char *Cmd)
425 {
426 /* Set ADC to twice the carrier for a slight supersampling */
427 UsbCommand c = {CMD_LF_SIMULATE_BIDIR, {47, 384, 0}};
428 SendCommand(&c);
429 return 0;
430 }
431
432 /* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */
433 int CmdLFSimManchester(const char *Cmd)
434 {
435 static int clock, gap;
436 static char data[1024], gapstring[8];
437
438 /* get settings/bits */
439 sscanf(Cmd, "%i %s %i", &clock, &data[0], &gap);
440
441 /* clear our graph */
442 ClearGraph(0);
443
444 /* fill it with our bitstream */
445 for (int i = 0; i < strlen(data) ; ++i)
446 AppendGraph(0, clock, data[i]- '0');
447
448 /* modulate */
449 CmdManchesterMod("");
450
451 /* show what we've done */
452 RepaintGraphWindow();
453
454 /* simulate */
455 sprintf(&gapstring[0], "%i", gap);
456 CmdLFSim(gapstring);
457 return 0;
458 }
459
460 int CmdLFSnoop(const char *Cmd)
461 {
462 UsbCommand c = {CMD_LF_SNOOP_RAW_ADC_SAMPLES};
463 // 'h' means higher-low-frequency, 134 kHz
464 c.arg[0] = 0;
465 c.arg[1] = -1;
466 if (*Cmd == 0) {
467 // empty
468 } else if (*Cmd == 'l') {
469 sscanf(Cmd, "l %"lli, &c.arg[1]);
470 } else if(*Cmd == 'h') {
471 c.arg[0] = 1;
472 sscanf(Cmd, "h %"lli, &c.arg[1]);
473 } else if (sscanf(Cmd, "%"lli" %"lli, &c.arg[0], &c.arg[1]) < 1) {
474 PrintAndLog("use 'snoop' or 'snoop {l,h} [trigger threshold]', or 'snoop <divisor> [trigger threshold]'");
475 return 0;
476 }
477 SendCommand(&c);
478 WaitForResponse(CMD_ACK,NULL);
479
480 size_t BUFF_SIZE = 8000;
481 uint8_t data[BUFF_SIZE];
482
483 GetFromBigBuf(data,BUFF_SIZE,3560); //3560 -- should be offset..
484 WaitForResponseTimeout(CMD_ACK,NULL, 1500);
485
486 for (int j = 0; j < BUFF_SIZE; j++) {
487 GraphBuffer[j] = ((int)data[j]);
488 }
489 GraphTraceLen = BUFF_SIZE;
490
491 return 0;
492 }
493
494 int CmdVchDemod(const char *Cmd)
495 {
496 // Is this the entire sync pattern, or does this also include some
497 // data bits that happen to be the same everywhere? That would be
498 // lovely to know.
499 static const int SyncPattern[] = {
500 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
501 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
502 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
503 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
504 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
505 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
506 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
507 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
508 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
509 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
510 };
511
512 // So first, we correlate for the sync pattern, and mark that.
513 int bestCorrel = 0, bestPos = 0;
514 int i;
515 // It does us no good to find the sync pattern, with fewer than
516 // 2048 samples after it...
517 for (i = 0; i < (GraphTraceLen-2048); i++) {
518 int sum = 0;
519 int j;
520 for (j = 0; j < arraylen(SyncPattern); j++) {
521 sum += GraphBuffer[i+j]*SyncPattern[j];
522 }
523 if (sum > bestCorrel) {
524 bestCorrel = sum;
525 bestPos = i;
526 }
527 }
528 PrintAndLog("best sync at %d [metric %d]", bestPos, bestCorrel);
529
530 char bits[257];
531 bits[256] = '\0';
532
533 int worst = INT_MAX;
534 int worstPos = 0;
535
536 for (i = 0; i < 2048; i += 8) {
537 int sum = 0;
538 int j;
539 for (j = 0; j < 8; j++) {
540 sum += GraphBuffer[bestPos+i+j];
541 }
542 if (sum < 0) {
543 bits[i/8] = '.';
544 } else {
545 bits[i/8] = '1';
546 }
547 if(abs(sum) < worst) {
548 worst = abs(sum);
549 worstPos = i;
550 }
551 }
552 PrintAndLog("bits:");
553 PrintAndLog("%s", bits);
554 PrintAndLog("worst metric: %d at pos %d", worst, worstPos);
555
556 if (strcmp(Cmd, "clone")==0) {
557 GraphTraceLen = 0;
558 char *s;
559 for(s = bits; *s; s++) {
560 int j;
561 for(j = 0; j < 16; j++) {
562 GraphBuffer[GraphTraceLen++] = (*s == '1') ? 1 : 0;
563 }
564 }
565 RepaintGraphWindow();
566 }
567 return 0;
568 }
569
570 static command_t CommandTable[] =
571 {
572 {"help", CmdHelp, 1, "This help"},
573 {"cmdread", CmdLFCommandRead, 0, "<off period> <'0' period> <'1' period> <command> ['h'] -- Modulate LF reader field to send command before read (all periods in microseconds) (option 'h' for 134)"},
574
575 {"flexdemod", CmdFlexdemod, 1, "Demodulate samples for FlexPass"},
576 {"indalademod", CmdIndalaDemod, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
577 {"indalaclone", CmdIndalaClone, 0, "<UID> ['l']-- Clone Indala to T55x7 (UID in HEX)(option 'l' for 224 UID"},
578 {"vchdemod", CmdVchDemod, 1, "['clone'] -- Demodulate samples for VeriChip"},
579
580
581 {"read", CmdLFRead, 0, "['h' or <divisor>] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134, alternatively: f=12MHz/(divisor+1))"},
582 {"sim", CmdLFSim, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
583 {"simbidir", CmdLFSimBidir, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
584 {"simman", CmdLFSimManchester, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"},
585 {"snoop", CmdLFSnoop, 0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"},
586
587 {"awid26", CmdLFAWID26, 1, "{ AWID26 tags }"},
588 {"em4x", CmdLFEM4X, 1, "{ EM4X tags }"},
589 {"hid", CmdLFHID, 1, "{ HID tags }"},
590 {"hitag", CmdLFHitag, 1, "{ Hitag tags and transponders }"},
591 {"io", CmdLFIO, 1, "{ ioProx tags }"},
592 {"pcf7931", CmdLFPCF7931, 1, "{ PCF7931 tags }"},
593 {"ti", CmdLFTI, 1, "{ TI tags }"},
594 {"t55xx", CmdLFT55XX, 1, "{ T55xx tags }"},
595
596 {NULL, NULL, 0, NULL}
597 };
598
599 int CmdLF(const char *Cmd)
600 {
601 CmdsParse(CommandTable, Cmd);
602 return 0;
603 }
604
605 int CmdHelp(const char *Cmd)
606 {
607 CmdsHelp(CommandTable);
608 return 0;
609 }
Impressum, Datenschutz