]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlf.c
4cf9c815c94a00c2ac6627b962ae72e502be807e
[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 "util.h"
23 #include "cmdlf.h"
24 #include "cmdlfhid.h"
25 #include "cmdlfawid.h"
26 #include "cmdlfti.h"
27 #include "cmdlfem4x.h"
28 #include "cmdlfhitag.h"
29 #include "cmdlft55xx.h"
30 #include "cmdlfpcf7931.h"
31 #include "cmdlfio.h"
32 #include "lfdemod.h"
33 #include "cmdlfviking.h"
34 static int CmdHelp(const char *Cmd);
35
36 int usage_lf_cmdread(void) {
37 PrintAndLog("Usage: lf cmdread d <delay period> z <zero period> o <one period> c <cmdbytes> [H]");
38 PrintAndLog("Options: ");
39 PrintAndLog(" h This help");
40 PrintAndLog(" H Freqency High (134 KHz), default is 'Low (125KHz)'");
41 PrintAndLog(" d <delay> delay OFF period, (dec)");
42 PrintAndLog(" z <zero> time period ZERO, (dec)");
43 PrintAndLog(" o <one> time period ONE, (dec)");
44 PrintAndLog(" c <cmd> Command bytes");
45 PrintAndLog(" ************* All periods in microseconds (ms)");
46 PrintAndLog("Examples:");
47 PrintAndLog(" lf cmdread d 80 z 100 o 200 c 11000");
48 PrintAndLog(" lf cmdread d 80 z 100 o 100 c 11000 H");
49 return 0;
50 }
51 int usage_lf_read(void){
52 PrintAndLog("Usage: lf read [h] [s]");
53 PrintAndLog("Options: ");
54 PrintAndLog(" h This help");
55 PrintAndLog(" s silent run no printout");
56 PrintAndLog("This function takes no arguments. ");
57 PrintAndLog("Use 'lf config' to set parameters.");
58 return 0;
59 }
60 int usage_lf_snoop(void) {
61 PrintAndLog("Usage: lf snoop");
62 PrintAndLog("Options: ");
63 PrintAndLog(" h This help");
64 PrintAndLog("This function takes no arguments. ");
65 PrintAndLog("Use 'lf config' to set parameters.");
66 return 0;
67 }
68 int usage_lf_config(void) {
69 PrintAndLog("Usage: lf config [H|<divisor>] [b <bps>] [d <decim>] [a 0|1]");
70 PrintAndLog("Options: ");
71 PrintAndLog(" h This help");
72 PrintAndLog(" L Low frequency (125 KHz)");
73 PrintAndLog(" H High frequency (134 KHz)");
74 PrintAndLog(" q <divisor> Manually set divisor. 88-> 134KHz, 95-> 125 Hz");
75 PrintAndLog(" b <bps> Sets resolution of bits per sample. Default (max): 8");
76 PrintAndLog(" d <decim> Sets decimation. A value of N saves only 1 in N samples. Default: 1");
77 PrintAndLog(" a [0|1] Averaging - if set, will average the stored sample value when decimating. Default: 1");
78 PrintAndLog(" t <threshold> Sets trigger threshold. 0 means no threshold (range: 0-128)");
79 PrintAndLog("Examples:");
80 PrintAndLog(" lf config b 8 L");
81 PrintAndLog(" Samples at 125KHz, 8bps.");
82 PrintAndLog(" lf config H b 4 d 3");
83 PrintAndLog(" Samples at 134KHz, averages three samples into one, stored with ");
84 PrintAndLog(" a resolution of 4 bits per sample.");
85 PrintAndLog(" lf read");
86 PrintAndLog(" Performs a read (active field)");
87 PrintAndLog(" lf snoop");
88 PrintAndLog(" Performs a snoop (no active field)");
89 return 0;
90 }
91 int usage_lf_simfsk(void) {
92 PrintAndLog("Usage: lf simfsk [c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>]");
93 PrintAndLog("Options: ");
94 PrintAndLog(" h This help");
95 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
96 PrintAndLog(" i invert data");
97 PrintAndLog(" H <fcHigh> Manually set the larger Field Clock");
98 PrintAndLog(" L <fcLow> Manually set the smaller Field Clock");
99 //PrintAndLog(" s TBD- -to enable a gap between playback repetitions - default: no gap");
100 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
101 PrintAndLog("\n NOTE: if you set one clock manually set them all manually");
102 return 0;
103 }
104 int usage_lf_simask(void) {
105 PrintAndLog("Usage: lf simask [c <clock>] [i] [b|m|r] [s] [d <raw hex to sim>]");
106 PrintAndLog("Options: ");
107 PrintAndLog(" h This help");
108 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
109 PrintAndLog(" i invert data");
110 PrintAndLog(" b sim ask/biphase");
111 PrintAndLog(" m sim ask/manchester - Default");
112 PrintAndLog(" r sim ask/raw");
113 PrintAndLog(" s TBD- -to enable a gap between playback repetitions - default: no gap");
114 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
115 return 0;
116 }
117 int usage_lf_simpsk(void) {
118 PrintAndLog("Usage: lf simpsk [1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>]");
119 PrintAndLog("Options: ");
120 PrintAndLog(" h This help");
121 PrintAndLog(" c <clock> Manually set clock - can autodetect if using DemodBuffer");
122 PrintAndLog(" i invert data");
123 PrintAndLog(" 1 set PSK1 (default)");
124 PrintAndLog(" 2 set PSK2");
125 PrintAndLog(" 3 set PSK3");
126 PrintAndLog(" r <carrier> 2|4|8 are valid carriers: default = 2");
127 PrintAndLog(" d <hexdata> Data to sim as hex - omit to sim from DemodBuffer");
128 return 0;
129 }
130 int usage_lf_find(void){
131 PrintAndLog("Usage: lf search <0|1> [u]");
132 PrintAndLog(" <use data from Graphbuffer> , if not set, try reading data from tag.");
133 PrintAndLog(" [Search for Unknown tags] , if not set, reads only known tags.");
134 PrintAndLog("");
135 PrintAndLog(" sample: lf search = try reading data from tag & search for known tags");
136 PrintAndLog(" : lf search 1 = use data from GraphBuffer & search for known tags");
137 PrintAndLog(" : lf search u = try reading data from tag & search for known and unknown tags");
138 PrintAndLog(" : lf search 1 u = use data from GraphBuffer & search for known and unknown tags");
139 return 0;
140 }
141
142
143 /* send a LF command before reading */
144 int CmdLFCommandRead(const char *Cmd)
145 {
146 static char dummy[3] = {0x20,0x00,0x00};
147 UsbCommand c = {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K};
148 bool errors = FALSE;
149
150 uint8_t cmdp = 0;
151 int strLength = 0;
152
153 while(param_getchar(Cmd, cmdp) != 0x00) {
154 switch(param_getchar(Cmd, cmdp))
155 {
156 case 'h':
157 return usage_lf_cmdread();
158 case 'H':
159 dummy[1]='h';
160 cmdp++;
161 break;
162 case 'L':
163 cmdp++;
164 break;
165 case 'c':
166 strLength = param_getstr(Cmd, cmdp+1, (char *)&c.d.asBytes);
167 cmdp+=2;
168 break;
169 case 'd':
170 c.arg[0] = param_get32ex(Cmd, cmdp+1, 0, 10);
171 cmdp+=2;
172 break;
173 case 'z':
174 c.arg[1] = param_get32ex(Cmd, cmdp+1, 0, 10);
175 cmdp+=2;
176 break;
177 case 'o':
178 c.arg[2] = param_get32ex(Cmd, cmdp+1, 0, 10);
179 cmdp+=2;
180 break;
181 default:
182 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
183 errors = 1;
184 break;
185 }
186 if(errors) break;
187 }
188 // No args
189 if (cmdp == 0) errors = 1;
190
191 //Validations
192 if (errors) return usage_lf_cmdread();
193
194 // in case they specified 'H'
195 // added to the end..
196 strcpy((char *)&c.d.asBytes + strLength, dummy);
197
198 clearCommandBuffer();
199 SendCommand(&c);
200 return 0;
201 }
202
203 int CmdFlexdemod(const char *Cmd)
204 {
205 int i;
206 for (i = 0; i < GraphTraceLen; ++i) {
207 if (GraphBuffer[i] < 0) {
208 GraphBuffer[i] = -1;
209 } else {
210 GraphBuffer[i] = 1;
211 }
212 }
213
214 #define LONG_WAIT 100
215 int start;
216 for (start = 0; start < GraphTraceLen - LONG_WAIT; start++) {
217 int first = GraphBuffer[start];
218 for (i = start; i < start + LONG_WAIT; i++) {
219 if (GraphBuffer[i] != first) {
220 break;
221 }
222 }
223 if (i == (start + LONG_WAIT)) {
224 break;
225 }
226 }
227 if (start == GraphTraceLen - LONG_WAIT) {
228 PrintAndLog("nothing to wait for");
229 return 0;
230 }
231
232 GraphBuffer[start] = 2;
233 GraphBuffer[start+1] = -2;
234 uint8_t bits[64] = {0x00};
235
236 int bit, sum;
237 i = start;
238 for (bit = 0; bit < 64; bit++) {
239 sum = 0;
240 for (int j = 0; j < 16; j++) {
241 sum += GraphBuffer[i++];
242 }
243
244 bits[bit] = (sum > 0) ? 1 : 0;
245
246 PrintAndLog("bit %d sum %d", bit, sum);
247 }
248
249 for (bit = 0; bit < 64; bit++) {
250 int j;
251 int sum = 0;
252 for (j = 0; j < 16; j++) {
253 sum += GraphBuffer[i++];
254 }
255 if (sum > 0 && bits[bit] != 1) {
256 PrintAndLog("oops1 at %d", bit);
257 }
258 if (sum < 0 && bits[bit] != 0) {
259 PrintAndLog("oops2 at %d", bit);
260 }
261 }
262
263 // HACK writing back to graphbuffer.
264 GraphTraceLen = 32*64;
265 i = 0;
266 int phase = 0;
267 for (bit = 0; bit < 64; bit++) {
268
269 phase = (bits[bit] == 0) ? 0 : 1;
270
271 int j;
272 for (j = 0; j < 32; j++) {
273 GraphBuffer[i++] = phase;
274 phase = !phase;
275 }
276 }
277
278 RepaintGraphWindow();
279 return 0;
280 }
281
282 int CmdIndalaDemod(const char *Cmd)
283 {
284 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
285
286 int state = -1;
287 int count = 0;
288 int i, j;
289
290 // worst case with GraphTraceLen=64000 is < 4096
291 // under normal conditions it's < 2048
292
293 uint8_t rawbits[4096];
294 int rawbit = 0;
295 int worst = 0, worstPos = 0;
296 // PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen / 32);
297 for (i = 0; i < GraphTraceLen-1; i += 2) {
298 count += 1;
299 if ((GraphBuffer[i] > GraphBuffer[i + 1]) && (state != 1)) {
300 if (state == 0) {
301 for (j = 0; j < count - 8; j += 16) {
302 rawbits[rawbit++] = 0;
303 }
304 if ((abs(count - j)) > worst) {
305 worst = abs(count - j);
306 worstPos = i;
307 }
308 }
309 state = 1;
310 count = 0;
311 } else if ((GraphBuffer[i] < GraphBuffer[i + 1]) && (state != 0)) {
312 if (state == 1) {
313 for (j = 0; j < count - 8; j += 16) {
314 rawbits[rawbit++] = 1;
315 }
316 if ((abs(count - j)) > worst) {
317 worst = abs(count - j);
318 worstPos = i;
319 }
320 }
321 state = 0;
322 count = 0;
323 }
324 }
325
326 if ( rawbit>0 ){
327 PrintAndLog("Recovered %d raw bits, expected: %d", rawbit, GraphTraceLen/32);
328 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst, worstPos);
329 } else {
330 return 0;
331 }
332
333 // Finding the start of a UID
334 int uidlen, long_wait;
335 if (strcmp(Cmd, "224") == 0) {
336 uidlen = 224;
337 long_wait = 30;
338 } else {
339 uidlen = 64;
340 long_wait = 29;
341 }
342
343 int start;
344 int first = 0;
345 for (start = 0; start <= rawbit - uidlen; start++) {
346 first = rawbits[start];
347 for (i = start; i < start + long_wait; i++) {
348 if (rawbits[i] != first) {
349 break;
350 }
351 }
352 if (i == (start + long_wait)) {
353 break;
354 }
355 }
356
357 if (start == rawbit - uidlen + 1) {
358 PrintAndLog("nothing to wait for");
359 return 0;
360 }
361
362 // Inverting signal if needed
363 if (first == 1) {
364 for (i = start; i < rawbit; i++) {
365 rawbits[i] = !rawbits[i];
366 }
367 }
368
369 // Dumping UID
370 uint8_t bits[224] = {0x00};
371 char showbits[225] = {0x00};
372 int bit;
373 i = start;
374 int times = 0;
375
376 if (uidlen > rawbit) {
377 PrintAndLog("Warning: not enough raw bits to get a full UID");
378 for (bit = 0; bit < rawbit; bit++) {
379 bits[bit] = rawbits[i++];
380 // As we cannot know the parity, let's use "." and "/"
381 showbits[bit] = '.' + bits[bit];
382 }
383 showbits[bit+1]='\0';
384 PrintAndLog("Partial UID=%s", showbits);
385 return 0;
386 } else {
387 for (bit = 0; bit < uidlen; bit++) {
388 bits[bit] = rawbits[i++];
389 showbits[bit] = '0' + bits[bit];
390 }
391 times = 1;
392 }
393
394 //convert UID to HEX
395 uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7;
396 int idx;
397 uid1 = uid2 = 0;
398
399 if (uidlen==64){
400 for( idx=0; idx<64; idx++) {
401 if (showbits[idx] == '0') {
402 uid1 = (uid1<<1) | (uid2>>31);
403 uid2 = (uid2<<1) | 0;
404 } else {
405 uid1 = (uid1<<1) | (uid2>>31);
406 uid2 = (uid2<<1) | 1;
407 }
408 }
409 PrintAndLog("UID=%s (%x%08x)", showbits, uid1, uid2);
410 } else {
411 uid3 = uid4 = uid5 = uid6 = uid7 = 0;
412
413 for( idx=0; idx<224; idx++) {
414 uid1 = (uid1<<1) | (uid2>>31);
415 uid2 = (uid2<<1) | (uid3>>31);
416 uid3 = (uid3<<1) | (uid4>>31);
417 uid4 = (uid4<<1) | (uid5>>31);
418 uid5 = (uid5<<1) | (uid6>>31);
419 uid6 = (uid6<<1) | (uid7>>31);
420
421 if (showbits[idx] == '0')
422 uid7 = (uid7<<1) | 0;
423 else
424 uid7 = (uid7<<1) | 1;
425 }
426 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits, uid1, uid2, uid3, uid4, uid5, uid6, uid7);
427 }
428
429 // Checking UID against next occurrences
430 int failed = 0;
431 for (; i + uidlen <= rawbit;) {
432 failed = 0;
433 for (bit = 0; bit < uidlen; bit++) {
434 if (bits[bit] != rawbits[i++]) {
435 failed = 1;
436 break;
437 }
438 }
439 if (failed == 1) {
440 break;
441 }
442 times += 1;
443 }
444
445 PrintAndLog("Occurrences: %d (expected %d)", times, (rawbit - start) / uidlen);
446
447 // Remodulating for tag cloning
448 // HACK: 2015-01-04 this will have an impact on our new way of seening lf commands (demod)
449 // since this changes graphbuffer data.
450 GraphTraceLen = 32 * uidlen;
451 i = 0;
452 int phase = 0;
453 for (bit = 0; bit < uidlen; bit++) {
454 phase = (bits[bit] == 0) ? 0 : 1;
455 int j;
456 for (j = 0; j < 32; j++) {
457 GraphBuffer[i++] = phase;
458 phase = !phase;
459 }
460 }
461
462 RepaintGraphWindow();
463 return 1;
464 }
465
466 int CmdIndalaClone(const char *Cmd)
467 {
468 UsbCommand c;
469 unsigned int uid1, uid2, uid3, uid4, uid5, uid6, uid7;
470
471 uid1 = uid2 = uid3 = uid4 = uid5 = uid6 = uid7 = 0;
472 int n = 0, i = 0;
473
474 if (strchr(Cmd,'l') != 0) {
475 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
476 uid1 = (uid1 << 4) | (uid2 >> 28);
477 uid2 = (uid2 << 4) | (uid3 >> 28);
478 uid3 = (uid3 << 4) | (uid4 >> 28);
479 uid4 = (uid4 << 4) | (uid5 >> 28);
480 uid5 = (uid5 << 4) | (uid6 >> 28);
481 uid6 = (uid6 << 4) | (uid7 >> 28);
482 uid7 = (uid7 << 4) | (n & 0xf);
483 }
484 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1, uid2, uid3, uid4, uid5, uid6, uid7);
485 c.cmd = CMD_INDALA_CLONE_TAG_L;
486 c.d.asDwords[0] = uid1;
487 c.d.asDwords[1] = uid2;
488 c.d.asDwords[2] = uid3;
489 c.d.asDwords[3] = uid4;
490 c.d.asDwords[4] = uid5;
491 c.d.asDwords[5] = uid6;
492 c.d.asDwords[6] = uid7;
493 } else {
494 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
495 uid1 = (uid1 << 4) | (uid2 >> 28);
496 uid2 = (uid2 << 4) | (n & 0xf);
497 }
498 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1, uid2);
499 c.cmd = CMD_INDALA_CLONE_TAG;
500 c.arg[0] = uid1;
501 c.arg[1] = uid2;
502 }
503
504 SendCommand(&c);
505 return 0;
506 }
507
508 int CmdLFSetConfig(const char *Cmd)
509 {
510
511 uint8_t divisor = 0;//Frequency divisor
512 uint8_t bps = 0; // Bits per sample
513 uint8_t decimation = 0; //How many to keep
514 bool averaging = 1; // Defaults to true
515 bool errors = FALSE;
516 int trigger_threshold =-1;//Means no change
517 uint8_t unsigned_trigg = 0;
518
519 uint8_t cmdp =0;
520 while(param_getchar(Cmd, cmdp) != 0x00)
521 {
522 switch(param_getchar(Cmd, cmdp))
523 {
524 case 'h':
525 return usage_lf_config();
526 case 'H':
527 divisor = 88;
528 cmdp++;
529 break;
530 case 'L':
531 divisor = 95;
532 cmdp++;
533 break;
534 case 'q':
535 errors |= param_getdec(Cmd,cmdp+1,&divisor);
536 cmdp+=2;
537 break;
538 case 't':
539 errors |= param_getdec(Cmd,cmdp+1,&unsigned_trigg);
540 cmdp+=2;
541 if(!errors) trigger_threshold = unsigned_trigg;
542 break;
543 case 'b':
544 errors |= param_getdec(Cmd,cmdp+1,&bps);
545 cmdp+=2;
546 break;
547 case 'd':
548 errors |= param_getdec(Cmd,cmdp+1,&decimation);
549 cmdp+=2;
550 break;
551 case 'a':
552 averaging = param_getchar(Cmd,cmdp+1) == '1';
553 cmdp+=2;
554 break;
555 default:
556 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
557 errors = 1;
558 break;
559 }
560 if(errors) break;
561 }
562 if(cmdp == 0)
563 {
564 errors = 1;// No args
565 }
566
567 //Validations
568 if(errors)
569 {
570 return usage_lf_config();
571 }
572 //Bps is limited to 8, so fits in lower half of arg1
573 if(bps >> 8) bps = 8;
574
575 sample_config config = {
576 decimation,bps,averaging,divisor,trigger_threshold
577 };
578 //Averaging is a flag on high-bit of arg[1]
579 UsbCommand c = {CMD_SET_LF_SAMPLING_CONFIG};
580 memcpy(c.d.asBytes,&config,sizeof(sample_config));
581 SendCommand(&c);
582 return 0;
583 }
584
585 int CmdLFRead(const char *Cmd)
586 {
587 bool arg1 = false;
588 uint8_t cmdp = param_getchar(Cmd, 0);
589
590 if ( cmdp == 'h' || cmdp == 'H') return usage_lf_read();
591
592 //suppress print
593 if ( cmdp == 's' || cmdp == 'S') arg1 = true;
594
595 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K, {arg1,0,0}};
596 clearCommandBuffer();
597 SendCommand(&c);
598 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {
599 PrintAndLog("command execution time out");
600 return 1;
601 }
602 return 0;
603 }
604
605 int CmdLFSnoop(const char *Cmd)
606 {
607 uint8_t cmdp = param_getchar(Cmd, 0);
608 if(cmdp == 'h' || cmdp == 'H') return usage_lf_snoop();
609
610 UsbCommand c = {CMD_LF_SNOOP_RAW_ADC_SAMPLES};
611 clearCommandBuffer();
612 SendCommand(&c);
613 WaitForResponse(CMD_ACK,NULL);
614 return 0;
615 }
616
617 static void ChkBitstream(const char *str)
618 {
619 int i;
620
621 /* convert to bitstream if necessary */
622 for (i = 0; i < (int)(GraphTraceLen / 2); i++){
623 if (GraphBuffer[i] > 1 || GraphBuffer[i] < 0) {
624 CmdGetBitStream("");
625 break;
626 }
627 }
628 }
629 //Attempt to simulate any wave in buffer (one bit per output sample)
630 // converts GraphBuffer to bitstream (based on zero crossings) if needed.
631 int CmdLFSim(const char *Cmd)
632 {
633 int i,j;
634 static int gap;
635
636 sscanf(Cmd, "%i", &gap);
637
638 // convert to bitstream if necessary
639 ChkBitstream(Cmd);
640
641 //can send only 512 bits at a time (1 byte sent per bit...)
642 printf("Sending [%d bytes]", GraphTraceLen);
643 for (i = 0; i < GraphTraceLen; i += USB_CMD_DATA_SIZE) {
644 UsbCommand c = {CMD_DOWNLOADED_SIM_SAMPLES_125K, {i, 0, 0}};
645
646 for (j = 0; j < USB_CMD_DATA_SIZE; j++) {
647 c.d.asBytes[j] = GraphBuffer[i+j];
648 }
649 clearCommandBuffer();
650 SendCommand(&c);
651 WaitForResponse(CMD_ACK,NULL);
652 printf(".");
653 }
654
655 PrintAndLog("\nStarting to simulate");
656 UsbCommand c = {CMD_SIMULATE_TAG_125K, {GraphTraceLen, gap, 0}};
657 clearCommandBuffer();
658 SendCommand(&c);
659 return 0;
660 }
661
662 // by marshmellow - sim ask data given clock, fcHigh, fcLow, invert
663 // - allow pull data from DemodBuffer
664 int CmdLFfskSim(const char *Cmd)
665 {
666 //might be able to autodetect FCs and clock from Graphbuffer if using demod buffer
667 // otherwise will need FChigh, FClow, Clock, and bitstream
668 uint8_t fcHigh=0, fcLow=0, clk=0;
669 uint8_t invert=0;
670 bool errors = FALSE;
671 char hexData[32] = {0x00}; // store entered hex data
672 uint8_t data[255] = {0x00};
673 int dataLen = 0;
674 uint8_t cmdp = 0;
675 while(param_getchar(Cmd, cmdp) != 0x00)
676 {
677 switch(param_getchar(Cmd, cmdp))
678 {
679 case 'h':
680 return usage_lf_simfsk();
681 case 'i':
682 invert = 1;
683 cmdp++;
684 break;
685 case 'c':
686 errors |= param_getdec(Cmd,cmdp+1,&clk);
687 cmdp+=2;
688 break;
689 case 'H':
690 errors |= param_getdec(Cmd,cmdp+1,&fcHigh);
691 cmdp+=2;
692 break;
693 case 'L':
694 errors |= param_getdec(Cmd,cmdp+1,&fcLow);
695 cmdp+=2;
696 break;
697 //case 's':
698 // separator=1;
699 // cmdp++;
700 // break;
701 case 'd':
702 dataLen = param_getstr(Cmd, cmdp+1, hexData);
703 if (dataLen==0) {
704 errors=TRUE;
705 } else {
706 dataLen = hextobinarray((char *)data, hexData);
707 }
708 if (dataLen==0) errors=TRUE;
709 if (errors) PrintAndLog ("Error getting hex data");
710 cmdp+=2;
711 break;
712 default:
713 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
714 errors = TRUE;
715 break;
716 }
717 if(errors) break;
718 }
719 if(cmdp == 0 && DemodBufferLen == 0)
720 {
721 errors = TRUE;// No args
722 }
723
724 //Validations
725 if(errors)
726 {
727 return usage_lf_simfsk();
728 }
729
730 if (dataLen == 0){ //using DemodBuffer
731 if (clk==0 || fcHigh==0 || fcLow==0){ //manual settings must set them all
732 uint8_t ans = fskClocks(&fcHigh, &fcLow, &clk, 0);
733 if (ans==0){
734 if (!fcHigh) fcHigh=10;
735 if (!fcLow) fcLow=8;
736 if (!clk) clk=50;
737 }
738 }
739 } else {
740 setDemodBuf(data, dataLen, 0);
741 }
742
743 //default if not found
744 if (clk == 0) clk = 50;
745 if (fcHigh == 0) fcHigh = 10;
746 if (fcLow == 0) fcLow = 8;
747
748 uint16_t arg1, arg2;
749 arg1 = fcHigh << 8 | fcLow;
750 arg2 = invert << 8 | clk;
751 size_t size = DemodBufferLen;
752 if (size > USB_CMD_DATA_SIZE) {
753 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size, USB_CMD_DATA_SIZE);
754 size = USB_CMD_DATA_SIZE;
755 }
756 UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, size}};
757
758 memcpy(c.d.asBytes, DemodBuffer, size);
759 SendCommand(&c);
760 return 0;
761 }
762
763 // by marshmellow - sim ask data given clock, invert, manchester or raw, separator
764 // - allow pull data from DemodBuffer
765 int CmdLFaskSim(const char *Cmd)
766 {
767 //autodetect clock from Graphbuffer if using demod buffer
768 // needs clock, invert, manchester/raw as m or r, separator as s, and bitstream
769 uint8_t encoding = 1, separator = 0;
770 uint8_t clk=0, invert=0;
771 bool errors = FALSE;
772 char hexData[32] = {0x00};
773 uint8_t data[255]= {0x00}; // store entered hex data
774 int dataLen = 0;
775 uint8_t cmdp = 0;
776 while(param_getchar(Cmd, cmdp) != 0x00)
777 {
778 switch(param_getchar(Cmd, cmdp))
779 {
780 case 'h':
781 return usage_lf_simask();
782 case 'i':
783 invert = 1;
784 cmdp++;
785 break;
786 case 'c':
787 errors |= param_getdec(Cmd,cmdp+1,&clk);
788 cmdp+=2;
789 break;
790 case 'b':
791 encoding=2; //biphase
792 cmdp++;
793 break;
794 case 'm':
795 encoding=1;
796 cmdp++;
797 break;
798 case 'r':
799 encoding=0;
800 cmdp++;
801 break;
802 case 's':
803 separator=1;
804 cmdp++;
805 break;
806 case 'd':
807 dataLen = param_getstr(Cmd, cmdp+1, hexData);
808 if (dataLen==0) {
809 errors=TRUE;
810 } else {
811 dataLen = hextobinarray((char *)data, hexData);
812 }
813 if (dataLen==0) errors=TRUE;
814 if (errors) PrintAndLog ("Error getting hex data, datalen: %d",dataLen);
815 cmdp+=2;
816 break;
817 default:
818 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
819 errors = TRUE;
820 break;
821 }
822 if(errors) break;
823 }
824 if(cmdp == 0 && DemodBufferLen == 0)
825 {
826 errors = TRUE;// No args
827 }
828
829 //Validations
830 if(errors)
831 {
832 return usage_lf_simask();
833 }
834 if (dataLen == 0){ //using DemodBuffer
835 if (clk == 0) clk = GetAskClock("0", false, false);
836 } else {
837 setDemodBuf(data, dataLen, 0);
838 }
839 if (clk == 0) clk = 64;
840 if (encoding == 0) clk = clk/2; //askraw needs to double the clock speed
841 uint16_t arg1, arg2;
842 size_t size=DemodBufferLen;
843 arg1 = clk << 8 | encoding;
844 arg2 = invert << 8 | separator;
845 if (size > USB_CMD_DATA_SIZE) {
846 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size, USB_CMD_DATA_SIZE);
847 size = USB_CMD_DATA_SIZE;
848 }
849 UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
850 PrintAndLog("preparing to sim ask data: %d bits", size);
851 memcpy(c.d.asBytes, DemodBuffer, size);
852 SendCommand(&c);
853 return 0;
854 }
855
856 // by marshmellow - sim psk data given carrier, clock, invert
857 // - allow pull data from DemodBuffer or parameters
858 int CmdLFpskSim(const char *Cmd)
859 {
860 //might be able to autodetect FC and clock from Graphbuffer if using demod buffer
861 //will need carrier, Clock, and bitstream
862 uint8_t carrier=0, clk=0;
863 uint8_t invert=0;
864 bool errors = FALSE;
865 char hexData[32] = {0x00}; // store entered hex data
866 uint8_t data[255] = {0x00};
867 int dataLen = 0;
868 uint8_t cmdp = 0;
869 uint8_t pskType = 1;
870 while(param_getchar(Cmd, cmdp) != 0x00)
871 {
872 switch(param_getchar(Cmd, cmdp))
873 {
874 case 'h':
875 return usage_lf_simpsk();
876 case 'i':
877 invert = 1;
878 cmdp++;
879 break;
880 case 'c':
881 errors |= param_getdec(Cmd,cmdp+1,&clk);
882 cmdp+=2;
883 break;
884 case 'r':
885 errors |= param_getdec(Cmd,cmdp+1,&carrier);
886 cmdp+=2;
887 break;
888 case '1':
889 pskType=1;
890 cmdp++;
891 break;
892 case '2':
893 pskType=2;
894 cmdp++;
895 break;
896 case '3':
897 pskType=3;
898 cmdp++;
899 break;
900 case 'd':
901 dataLen = param_getstr(Cmd, cmdp+1, hexData);
902 if (dataLen==0) {
903 errors=TRUE;
904 } else {
905 dataLen = hextobinarray((char *)data, hexData);
906 }
907 if (dataLen==0) errors=TRUE;
908 if (errors) PrintAndLog ("Error getting hex data");
909 cmdp+=2;
910 break;
911 default:
912 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
913 errors = TRUE;
914 break;
915 }
916 if (errors) break;
917 }
918 if (cmdp == 0 && DemodBufferLen == 0)
919 {
920 errors = TRUE;// No args
921 }
922
923 //Validations
924 if (errors)
925 {
926 return usage_lf_simpsk();
927 }
928 if (dataLen == 0){ //using DemodBuffer
929 PrintAndLog("Getting Clocks");
930 if (clk==0) clk = GetPskClock("", FALSE, FALSE);
931 PrintAndLog("clk: %d",clk);
932 if (!carrier) carrier = GetPskCarrier("", FALSE, FALSE);
933 PrintAndLog("carrier: %d", carrier);
934 } else {
935 setDemodBuf(data, dataLen, 0);
936 }
937
938 if (clk <= 0) clk = 32;
939 if (carrier == 0) carrier = 2;
940 if (pskType != 1){
941 if (pskType == 2){
942 //need to convert psk2 to psk1 data before sim
943 psk2TOpsk1(DemodBuffer, DemodBufferLen);
944 } else {
945 PrintAndLog("Sorry, PSK3 not yet available");
946 }
947 }
948 uint16_t arg1, arg2;
949 arg1 = clk << 8 | carrier;
950 arg2 = invert;
951 size_t size=DemodBufferLen;
952 if (size > USB_CMD_DATA_SIZE) {
953 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size, USB_CMD_DATA_SIZE);
954 size=USB_CMD_DATA_SIZE;
955 }
956 UsbCommand c = {CMD_PSK_SIM_TAG, {arg1, arg2, size}};
957 PrintAndLog("DEBUG: Sending DemodBuffer Length: %d", size);
958 memcpy(c.d.asBytes, DemodBuffer, size);
959 SendCommand(&c);
960
961 return 0;
962 }
963
964 int CmdLFSimBidir(const char *Cmd)
965 {
966 // Set ADC to twice the carrier for a slight supersampling
967 // HACK: not implemented in ARMSRC.
968 PrintAndLog("Not implemented yet.");
969 UsbCommand c = {CMD_LF_SIMULATE_BIDIR, {47, 384, 0}};
970 SendCommand(&c);
971 return 0;
972 }
973
974 int CmdVchDemod(const char *Cmd)
975 {
976 // Is this the entire sync pattern, or does this also include some
977 // data bits that happen to be the same everywhere? That would be
978 // lovely to know.
979 static const int SyncPattern[] = {
980 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
981 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
982 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
983 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
984 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
985 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
986 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
987 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
988 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
989 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
990 };
991
992 // So first, we correlate for the sync pattern, and mark that.
993 int bestCorrel = 0, bestPos = 0;
994 int i;
995 // It does us no good to find the sync pattern, with fewer than
996 // 2048 samples after it...
997 for (i = 0; i < (GraphTraceLen-2048); i++) {
998 int sum = 0;
999 int j;
1000 for (j = 0; j < arraylen(SyncPattern); j++) {
1001 sum += GraphBuffer[i+j]*SyncPattern[j];
1002 }
1003 if (sum > bestCorrel) {
1004 bestCorrel = sum;
1005 bestPos = i;
1006 }
1007 }
1008 PrintAndLog("best sync at %d [metric %d]", bestPos, bestCorrel);
1009
1010 char bits[257];
1011 bits[256] = '\0';
1012
1013 int worst = INT_MAX;
1014 int worstPos = 0;
1015
1016 for (i = 0; i < 2048; i += 8) {
1017 int sum = 0;
1018 int j;
1019 for (j = 0; j < 8; j++) {
1020 sum += GraphBuffer[bestPos+i+j];
1021 }
1022 if (sum < 0) {
1023 bits[i/8] = '.';
1024 } else {
1025 bits[i/8] = '1';
1026 }
1027 if(abs(sum) < worst) {
1028 worst = abs(sum);
1029 worstPos = i;
1030 }
1031 }
1032 PrintAndLog("bits:");
1033 PrintAndLog("%s", bits);
1034 PrintAndLog("worst metric: %d at pos %d", worst, worstPos);
1035
1036 if (strcmp(Cmd, "clone")==0) {
1037 GraphTraceLen = 0;
1038 char *s;
1039 for(s = bits; *s; s++) {
1040 int j;
1041 for(j = 0; j < 16; j++) {
1042 GraphBuffer[GraphTraceLen++] = (*s == '1') ? 1 : 0;
1043 }
1044 }
1045 RepaintGraphWindow();
1046 }
1047 return 0;
1048 }
1049
1050 //by marshmellow
1051 int CmdLFfind(const char *Cmd)
1052 {
1053 int ans = 0;
1054 char cmdp = param_getchar(Cmd, 0);
1055 char testRaw = param_getchar(Cmd, 1);
1056 if (strlen(Cmd) > 3 || cmdp == 'h' || cmdp == 'H') return usage_lf_find();
1057
1058 if (!offline && (cmdp != '1')){
1059 CmdLFRead("s");
1060 getSamples("30000",false);
1061 } else if (GraphTraceLen < 1000) {
1062 PrintAndLog("Data in Graphbuffer was too small.");
1063 return 0;
1064 }
1065 if (cmdp == 'u' || cmdp == 'U') testRaw = 'u';
1066
1067 PrintAndLog("NOTE: some demods output possible binary\n if it finds something that looks like a tag");
1068 PrintAndLog("False Positives ARE possible\n");
1069 PrintAndLog("\nChecking for known tags:\n");
1070
1071 ans=CmdFSKdemodIO("");
1072 if (ans>0) {
1073 PrintAndLog("\nValid IO Prox ID Found!");
1074 return 1;
1075 }
1076
1077 ans=CmdFSKdemodPyramid("");
1078 if (ans>0) {
1079 PrintAndLog("\nValid Pyramid ID Found!");
1080 return 1;
1081 }
1082
1083 ans=CmdFSKdemodParadox("");
1084 if (ans>0) {
1085 PrintAndLog("\nValid Paradox ID Found!");
1086 return 1;
1087 }
1088
1089 ans=CmdFSKdemodAWID("");
1090 if (ans>0) {
1091 PrintAndLog("\nValid AWID ID Found!");
1092 return 1;
1093 }
1094
1095 ans=CmdFSKdemodHID("");
1096 if (ans>0) {
1097 PrintAndLog("\nValid HID Prox ID Found!");
1098 return 1;
1099 }
1100
1101 //add psk and indala
1102 ans=CmdIndalaDecode("");
1103 if (ans>0) {
1104 PrintAndLog("\nValid Indala ID Found!");
1105 return 1;
1106 }
1107
1108 ans=CmdAskEM410xDemod("");
1109 if (ans>0) {
1110 PrintAndLog("\nValid EM410x ID Found!");
1111 return 1;
1112 }
1113
1114 ans=CmdG_Prox_II_Demod("");
1115 if (ans>0) {
1116 PrintAndLog("\nValid Guardall G-Prox II ID Found!");
1117 return 1;
1118 }
1119
1120 ans=CmdFDXBdemodBI("");
1121 if (ans>0) {
1122 PrintAndLog("\nValid FDX-B ID Found!");
1123 return 1;
1124 }
1125
1126 ans=EM4x50Read("", false);
1127 if (ans>0) {
1128 PrintAndLog("\nValid EM4x50 ID Found!");
1129 return 1;
1130 }
1131
1132 ans=CmdVikingDemod("");
1133 if (ans>0) {
1134 PrintAndLog("\nValid Viking ID Found!");
1135 return 1;
1136 }
1137
1138 ans=CmdPSKNexWatch("");
1139 if (ans>0) {
1140 PrintAndLog("\nValid NexWatch ID Found!");
1141 return 1;
1142 }
1143
1144 PrintAndLog("\nNo Known Tags Found!\n");
1145 if (testRaw=='u' || testRaw=='U'){
1146 //test unknown tag formats (raw mode)
1147 PrintAndLog("\nChecking for Unknown tags:\n");
1148 ans=AutoCorrelate(4000, FALSE, FALSE);
1149
1150 if (ans > 0) {
1151
1152 PrintAndLog("Possible Auto Correlation of %d repeating samples",ans);
1153
1154 if ( ans % 8 == 0) {
1155 int bytes = (ans / 8);
1156 PrintAndLog("Possible %d bytes", bytes);
1157 int blocks = 0;
1158 if ( bytes % 2 == 0) {
1159 blocks = (bytes / 2);
1160 PrintAndLog("Possible 2 blocks, width %d", blocks);
1161 }
1162 if ( bytes % 4 == 0) {
1163 blocks = (bytes / 4);
1164 PrintAndLog("Possible 4 blocks, width %d", blocks);
1165 }
1166 if ( bytes % 8 == 0) {
1167 blocks = (bytes / 8);
1168 PrintAndLog("Possible 8 blocks, width %d", blocks);
1169 }
1170 if ( bytes % 16 == 0) {
1171 blocks = (bytes / 16);
1172 PrintAndLog("Possible 16 blocks, width %d", blocks);
1173 }
1174 }
1175 }
1176 ans=GetFskClock("",FALSE,FALSE);
1177 if (ans != 0){ //fsk
1178 ans=FSKrawDemod("",TRUE);
1179 if (ans>0) {
1180 PrintAndLog("\nUnknown FSK Modulated Tag Found!");
1181 return 1;
1182 }
1183 }
1184 ans=ASKDemod("0 0 0",TRUE,FALSE,1);
1185 if (ans>0) {
1186 PrintAndLog("\nUnknown ASK Modulated and Manchester encoded Tag Found!");
1187 PrintAndLog("\nif it does not look right it could instead be ASK/Biphase - try 'data rawdemod ab'");
1188 return 1;
1189 }
1190 ans=CmdPSK1rawDemod("");
1191 if (ans>0) {
1192 PrintAndLog("Possible unknown PSK1 Modulated Tag Found above!\n\nCould also be PSK2 - try 'data rawdemod p2'");
1193 PrintAndLog("\nCould also be PSK3 - [currently not supported]");
1194 PrintAndLog("\nCould also be NRZ - try 'data nrzrawdemod");
1195 return 1;
1196 }
1197 PrintAndLog("\nNo Data Found!\n");
1198 }
1199 return 0;
1200 }
1201
1202 static command_t CommandTable[] =
1203 {
1204 {"help", CmdHelp, 1, "This help"},
1205 {"awid", CmdLFAWID, 1, "{ AWID RFIDs... }"},
1206 {"em4x", CmdLFEM4X, 1, "{ EM4X RFIDs... }"},
1207 {"hid", CmdLFHID, 1, "{ HID RFIDs... }"},
1208 {"hitag", CmdLFHitag, 1, "{ HITAG RFIDs... }"},
1209 {"io", CmdLFIO, 1, "{ IOPROX RFIDs... }"},
1210 {"pcf7931", CmdLFPCF7931, 1, "{ PCF7931 RFIDs... }"},
1211 {"ti", CmdLFTI, 1, "{ TI RFIDs... }"},
1212 {"t55xx", CmdLFT55XX, 1, "{ T55X7 RFIDs... }"},
1213 {"viking", CmdLFViking, 1, "{ Viking RFIDs... }"},
1214 {"config", CmdLFSetConfig, 0, "Set config for LF sampling, bit/sample, decimation, frequency"},
1215
1216 {"cmdread", CmdLFCommandRead, 0, "<off period> <'0' period> <'1' period> <command> ['h' 134] \n\t\t-- Modulate LF reader field to send command before read (all periods in microseconds)"},
1217 {"flexdemod", CmdFlexdemod, 1, "Demodulate samples for FlexPass"},
1218 {"indalademod", CmdIndalaDemod, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
1219 {"indalaclone", CmdIndalaClone, 0, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"},
1220 {"read", CmdLFRead, 0, "['s' silent] Read 125/134 kHz LF ID-only tag. Do 'lf read h' for help"},
1221 {"search", CmdLFfind, 1, "[offline] ['u'] Read and Search for valid known tag (in offline mode it you can load first then search) \n\t\t-- 'u' to search for unknown tags"},
1222 {"sim", CmdLFSim, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
1223 {"simask", CmdLFaskSim, 0, "[clock] [invert <1|0>] [biphase/manchester/raw <'b'|'m'|'r'>] [msg separator 's'] [d <hexdata>] \n\t\t-- Simulate LF ASK tag from demodbuffer or input"},
1224 {"simfsk", CmdLFfskSim, 0, "[c <clock>] [i] [H <fcHigh>] [L <fcLow>] [d <hexdata>] \n\t\t-- Simulate LF FSK tag from demodbuffer or input"},
1225 {"simpsk", CmdLFpskSim, 0, "[1|2|3] [c <clock>] [i] [r <carrier>] [d <raw hex to sim>] \n\t\t-- Simulate LF PSK tag from demodbuffer or input"},
1226 {"simbidir", CmdLFSimBidir, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
1227 {"snoop", CmdLFSnoop, 0, "Snoop LF"},
1228 {"vchdemod", CmdVchDemod, 1, "['clone'] -- Demodulate samples for VeriChip"},
1229 {NULL, NULL, 0, NULL}
1230 };
1231
1232 int CmdLF(const char *Cmd)
1233 {
1234 CmdsParse(CommandTable, Cmd);
1235 return 0;
1236 }
1237
1238 int CmdHelp(const char *Cmd)
1239 {
1240 CmdsHelp(CommandTable);
1241 return 0;
1242 }
Impressum, Datenschutz