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