]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlf.c
@marshmellows last LF changes.
[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
298 // loop through raw signal - since we know it is psk1 rf/32 fc/2 skip every other value (+=2)
299 for (i = 0; i < GraphTraceLen-1; i += 2) {
300 count += 1;
301 if ((GraphBuffer[i] > GraphBuffer[i + 1]) && (state != 1)) {
302 // appears redundant - marshmellow
303 if (state == 0) {
304 for (j = 0; j < count - 8; j += 16) {
305 rawbits[rawbit++] = 0;
306 }
307 if ((abs(count - j)) > worst) {
308 worst = abs(count - j);
309 worstPos = i;
310 }
311 }
312 state = 1;
313 count = 0;
314 } else if ((GraphBuffer[i] < GraphBuffer[i + 1]) && (state != 0)) {
315 //appears redundant
316 if (state == 1) {
317 for (j = 0; j < count - 8; j += 16) {
318 rawbits[rawbit++] = 1;
319 }
320 if ((abs(count - j)) > worst) {
321 worst = abs(count - j);
322 worstPos = i;
323 }
324 }
325 state = 0;
326 count = 0;
327 }
328 }
329
330 if ( rawbit>0 ){
331 PrintAndLog("Recovered %d raw bits, expected: %d", rawbit, GraphTraceLen/32);
332 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst, worstPos);
333 } else {
334 return 0;
335 }
336
337 // Finding the start of a UID
338 int uidlen, long_wait;
339 if (strcmp(Cmd, "224") == 0) {
340 uidlen = 224;
341 long_wait = 30;
342 } else {
343 uidlen = 64;
344 long_wait = 29;
345 }
346
347 int start;
348 int first = 0;
349 for (start = 0; start <= rawbit - uidlen; start++) {
350 first = rawbits[start];
351 for (i = start; i < start + long_wait; i++) {
352 if (rawbits[i] != first) {
353 break;
354 }
355 }
356 if (i == (start + long_wait)) {
357 break;
358 }
359 }
360
361 if (start == rawbit - uidlen + 1) {
362 PrintAndLog("nothing to wait for");
363 return 0;
364 }
365
366 // Inverting signal if needed
367 if (first == 1) {
368 for (i = start; i < rawbit; i++) {
369 rawbits[i] = !rawbits[i];
370 }
371 }
372
373 // Dumping UID
374 uint8_t bits[224] = {0x00};
375 char showbits[225] = {0x00};
376 int bit;
377 i = start;
378 int times = 0;
379
380 if (uidlen > rawbit) {
381 PrintAndLog("Warning: not enough raw bits to get a full UID");
382 for (bit = 0; bit < rawbit; bit++) {
383 bits[bit] = rawbits[i++];
384 // As we cannot know the parity, let's use "." and "/"
385 showbits[bit] = '.' + bits[bit];
386 }
387 showbits[bit+1]='\0';
388 PrintAndLog("Partial UID=%s", showbits);
389 return 0;
390 } else {
391 for (bit = 0; bit < uidlen; bit++) {
392 bits[bit] = rawbits[i++];
393 showbits[bit] = '0' + bits[bit];
394 }
395 times = 1;
396 }
397
398 //convert UID to HEX
399 uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7;
400 int idx;
401 uid1 = uid2 = 0;
402
403 if (uidlen==64){
404 for( idx=0; idx<64; idx++) {
405 if (showbits[idx] == '0') {
406 uid1 = (uid1<<1) | (uid2>>31);
407 uid2 = (uid2<<1) | 0;
408 } else {
409 uid1 = (uid1<<1) | (uid2>>31);
410 uid2 = (uid2<<1) | 1;
411 }
412 }
413 PrintAndLog("UID=%s (%x%08x)", showbits, uid1, uid2);
414 } else {
415 uid3 = uid4 = uid5 = uid6 = uid7 = 0;
416
417 for( idx=0; idx<224; idx++) {
418 uid1 = (uid1<<1) | (uid2>>31);
419 uid2 = (uid2<<1) | (uid3>>31);
420 uid3 = (uid3<<1) | (uid4>>31);
421 uid4 = (uid4<<1) | (uid5>>31);
422 uid5 = (uid5<<1) | (uid6>>31);
423 uid6 = (uid6<<1) | (uid7>>31);
424
425 if (showbits[idx] == '0')
426 uid7 = (uid7<<1) | 0;
427 else
428 uid7 = (uid7<<1) | 1;
429 }
430 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits, uid1, uid2, uid3, uid4, uid5, uid6, uid7);
431 }
432
433 // Checking UID against next occurrences
434 int failed = 0;
435 for (; i + uidlen <= rawbit;) {
436 failed = 0;
437 for (bit = 0; bit < uidlen; bit++) {
438 if (bits[bit] != rawbits[i++]) {
439 failed = 1;
440 break;
441 }
442 }
443 if (failed == 1) {
444 break;
445 }
446 times += 1;
447 }
448
449 PrintAndLog("Occurrences: %d (expected %d)", times, (rawbit - start) / uidlen);
450
451 // Remodulating for tag cloning
452 // HACK: 2015-01-04 this will have an impact on our new way of seening lf commands (demod)
453 // since this changes graphbuffer data.
454 GraphTraceLen = 32 * uidlen;
455 i = 0;
456 int phase = 0;
457 for (bit = 0; bit < uidlen; bit++) {
458 phase = (bits[bit] == 0) ? 0 : 1;
459 int j;
460 for (j = 0; j < 32; j++) {
461 GraphBuffer[i++] = phase;
462 phase = !phase;
463 }
464 }
465
466 RepaintGraphWindow();
467 return 1;
468 }
469
470 int CmdIndalaClone(const char *Cmd)
471 {
472 UsbCommand c;
473 unsigned int uid1, uid2, uid3, uid4, uid5, uid6, uid7;
474
475 uid1 = uid2 = uid3 = uid4 = uid5 = uid6 = uid7 = 0;
476 int n = 0, i = 0;
477
478 if (strchr(Cmd,'l') != 0) {
479 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
480 uid1 = (uid1 << 4) | (uid2 >> 28);
481 uid2 = (uid2 << 4) | (uid3 >> 28);
482 uid3 = (uid3 << 4) | (uid4 >> 28);
483 uid4 = (uid4 << 4) | (uid5 >> 28);
484 uid5 = (uid5 << 4) | (uid6 >> 28);
485 uid6 = (uid6 << 4) | (uid7 >> 28);
486 uid7 = (uid7 << 4) | (n & 0xf);
487 }
488 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1, uid2, uid3, uid4, uid5, uid6, uid7);
489 c.cmd = CMD_INDALA_CLONE_TAG_L;
490 c.d.asDwords[0] = uid1;
491 c.d.asDwords[1] = uid2;
492 c.d.asDwords[2] = uid3;
493 c.d.asDwords[3] = uid4;
494 c.d.asDwords[4] = uid5;
495 c.d.asDwords[5] = uid6;
496 c.d.asDwords[6] = uid7;
497 } else {
498 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
499 uid1 = (uid1 << 4) | (uid2 >> 28);
500 uid2 = (uid2 << 4) | (n & 0xf);
501 }
502 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1, uid2);
503 c.cmd = CMD_INDALA_CLONE_TAG;
504 c.arg[0] = uid1;
505 c.arg[1] = uid2;
506 }
507
508 SendCommand(&c);
509 return 0;
510 }
511
512 int CmdLFSetConfig(const char *Cmd)
513 {
514
515 uint8_t divisor = 0;//Frequency divisor
516 uint8_t bps = 0; // Bits per sample
517 uint8_t decimation = 0; //How many to keep
518 bool averaging = 1; // Defaults to true
519 bool errors = FALSE;
520 int trigger_threshold =-1;//Means no change
521 uint8_t unsigned_trigg = 0;
522
523 uint8_t cmdp =0;
524 while(param_getchar(Cmd, cmdp) != 0x00)
525 {
526 switch(param_getchar(Cmd, cmdp))
527 {
528 case 'h':
529 return usage_lf_config();
530 case 'H':
531 divisor = 88;
532 cmdp++;
533 break;
534 case 'L':
535 divisor = 95;
536 cmdp++;
537 break;
538 case 'q':
539 errors |= param_getdec(Cmd,cmdp+1,&divisor);
540 cmdp+=2;
541 break;
542 case 't':
543 errors |= param_getdec(Cmd,cmdp+1,&unsigned_trigg);
544 cmdp+=2;
545 if(!errors) trigger_threshold = unsigned_trigg;
546 break;
547 case 'b':
548 errors |= param_getdec(Cmd,cmdp+1,&bps);
549 cmdp+=2;
550 break;
551 case 'd':
552 errors |= param_getdec(Cmd,cmdp+1,&decimation);
553 cmdp+=2;
554 break;
555 case 'a':
556 averaging = param_getchar(Cmd,cmdp+1) == '1';
557 cmdp+=2;
558 break;
559 default:
560 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
561 errors = 1;
562 break;
563 }
564 if(errors) break;
565 }
566 if(cmdp == 0)
567 {
568 errors = 1;// No args
569 }
570
571 //Validations
572 if(errors)
573 {
574 return usage_lf_config();
575 }
576 //Bps is limited to 8, so fits in lower half of arg1
577 if(bps >> 8) bps = 8;
578
579 sample_config config = {
580 decimation,bps,averaging,divisor,trigger_threshold
581 };
582 //Averaging is a flag on high-bit of arg[1]
583 UsbCommand c = {CMD_SET_LF_SAMPLING_CONFIG};
584 memcpy(c.d.asBytes,&config,sizeof(sample_config));
585 SendCommand(&c);
586 return 0;
587 }
588
589 int CmdLFRead(const char *Cmd)
590 {
591 bool arg1 = false;
592 uint8_t cmdp = param_getchar(Cmd, 0);
593
594 if ( cmdp == 'h' || cmdp == 'H') return usage_lf_read();
595
596 //suppress print
597 if ( cmdp == 's' || cmdp == 'S') arg1 = true;
598
599 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K, {arg1,0,0}};
600 clearCommandBuffer();
601 SendCommand(&c);
602 if ( !WaitForResponseTimeout(CMD_ACK,NULL,2500) ) {
603 PrintAndLog("command execution time out");
604 return 1;
605 }
606 return 0;
607 }
608
609 int CmdLFSnoop(const char *Cmd)
610 {
611 uint8_t cmdp = param_getchar(Cmd, 0);
612 if(cmdp == 'h' || cmdp == 'H') return usage_lf_snoop();
613
614 UsbCommand c = {CMD_LF_SNOOP_RAW_ADC_SAMPLES};
615 clearCommandBuffer();
616 SendCommand(&c);
617 WaitForResponse(CMD_ACK,NULL);
618 return 0;
619 }
620
621 static void ChkBitstream(const char *str)
622 {
623 int i;
624
625 /* convert to bitstream if necessary */
626 for (i = 0; i < (int)(GraphTraceLen / 2); i++){
627 if (GraphBuffer[i] > 1 || GraphBuffer[i] < 0) {
628 CmdGetBitStream("");
629 break;
630 }
631 }
632 }
633 //Attempt to simulate any wave in buffer (one bit per output sample)
634 // converts GraphBuffer to bitstream (based on zero crossings) if needed.
635 int CmdLFSim(const char *Cmd)
636 {
637 int i,j;
638 static int gap;
639
640 sscanf(Cmd, "%i", &gap);
641
642 // convert to bitstream if necessary
643 ChkBitstream(Cmd);
644
645 //can send only 512 bits at a time (1 byte sent per bit...)
646 printf("Sending [%d bytes]", GraphTraceLen);
647 for (i = 0; i < GraphTraceLen; i += USB_CMD_DATA_SIZE) {
648 UsbCommand c = {CMD_DOWNLOADED_SIM_SAMPLES_125K, {i, 0, 0}};
649
650 for (j = 0; j < USB_CMD_DATA_SIZE; j++) {
651 c.d.asBytes[j] = GraphBuffer[i+j];
652 }
653 clearCommandBuffer();
654 SendCommand(&c);
655 WaitForResponse(CMD_ACK,NULL);
656 printf(".");
657 }
658
659 PrintAndLog("\nStarting to simulate");
660 UsbCommand c = {CMD_SIMULATE_TAG_125K, {GraphTraceLen, gap, 0}};
661 clearCommandBuffer();
662 SendCommand(&c);
663 return 0;
664 }
665
666 // by marshmellow - sim ask data given clock, fcHigh, fcLow, invert
667 // - allow pull data from DemodBuffer
668 int CmdLFfskSim(const char *Cmd)
669 {
670 //might be able to autodetect FCs and clock from Graphbuffer if using demod buffer
671 // otherwise will need FChigh, FClow, Clock, and bitstream
672 uint8_t fcHigh=0, fcLow=0, clk=0;
673 uint8_t invert=0;
674 bool errors = FALSE;
675 char hexData[32] = {0x00}; // store entered hex data
676 uint8_t data[255] = {0x00};
677 int dataLen = 0;
678 uint8_t cmdp = 0;
679 while(param_getchar(Cmd, cmdp) != 0x00)
680 {
681 switch(param_getchar(Cmd, cmdp))
682 {
683 case 'h':
684 return usage_lf_simfsk();
685 case 'i':
686 invert = 1;
687 cmdp++;
688 break;
689 case 'c':
690 errors |= param_getdec(Cmd,cmdp+1,&clk);
691 cmdp+=2;
692 break;
693 case 'H':
694 errors |= param_getdec(Cmd,cmdp+1,&fcHigh);
695 cmdp+=2;
696 break;
697 case 'L':
698 errors |= param_getdec(Cmd,cmdp+1,&fcLow);
699 cmdp+=2;
700 break;
701 //case 's':
702 // separator=1;
703 // cmdp++;
704 // break;
705 case 'd':
706 dataLen = param_getstr(Cmd, cmdp+1, hexData);
707 if (dataLen==0) {
708 errors=TRUE;
709 } else {
710 dataLen = hextobinarray((char *)data, hexData);
711 }
712 if (dataLen==0) errors=TRUE;
713 if (errors) PrintAndLog ("Error getting hex data");
714 cmdp+=2;
715 break;
716 default:
717 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
718 errors = TRUE;
719 break;
720 }
721 if(errors) break;
722 }
723 if(cmdp == 0 && DemodBufferLen == 0)
724 {
725 errors = TRUE;// No args
726 }
727
728 //Validations
729 if(errors)
730 {
731 return usage_lf_simfsk();
732 }
733
734 if (dataLen == 0){ //using DemodBuffer
735 if (clk==0 || fcHigh==0 || fcLow==0){ //manual settings must set them all
736 uint8_t ans = fskClocks(&fcHigh, &fcLow, &clk, 0);
737 if (ans==0){
738 if (!fcHigh) fcHigh=10;
739 if (!fcLow) fcLow=8;
740 if (!clk) clk=50;
741 }
742 }
743 } else {
744 setDemodBuf(data, dataLen, 0);
745 }
746
747 //default if not found
748 if (clk == 0) clk = 50;
749 if (fcHigh == 0) fcHigh = 10;
750 if (fcLow == 0) fcLow = 8;
751
752 uint16_t arg1, arg2;
753 arg1 = fcHigh << 8 | fcLow;
754 arg2 = invert << 8 | clk;
755 size_t size = DemodBufferLen;
756 if (size > USB_CMD_DATA_SIZE) {
757 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size, USB_CMD_DATA_SIZE);
758 size = USB_CMD_DATA_SIZE;
759 }
760 UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, size}};
761
762 memcpy(c.d.asBytes, DemodBuffer, size);
763 SendCommand(&c);
764 return 0;
765 }
766
767 // by marshmellow - sim ask data given clock, invert, manchester or raw, separator
768 // - allow pull data from DemodBuffer
769 int CmdLFaskSim(const char *Cmd)
770 {
771 //autodetect clock from Graphbuffer if using demod buffer
772 // needs clock, invert, manchester/raw as m or r, separator as s, and bitstream
773 uint8_t encoding = 1, separator = 0;
774 uint8_t clk=0, invert=0;
775 bool errors = FALSE;
776 char hexData[32] = {0x00};
777 uint8_t data[255]= {0x00}; // store entered hex data
778 int dataLen = 0;
779 uint8_t cmdp = 0;
780 while(param_getchar(Cmd, cmdp) != 0x00)
781 {
782 switch(param_getchar(Cmd, cmdp))
783 {
784 case 'h':
785 return usage_lf_simask();
786 case 'i':
787 invert = 1;
788 cmdp++;
789 break;
790 case 'c':
791 errors |= param_getdec(Cmd,cmdp+1,&clk);
792 cmdp+=2;
793 break;
794 case 'b':
795 encoding=2; //biphase
796 cmdp++;
797 break;
798 case 'm':
799 encoding=1;
800 cmdp++;
801 break;
802 case 'r':
803 encoding=0;
804 cmdp++;
805 break;
806 case 's':
807 separator=1;
808 cmdp++;
809 break;
810 case 'd':
811 dataLen = param_getstr(Cmd, cmdp+1, hexData);
812 if (dataLen==0) {
813 errors=TRUE;
814 } else {
815 dataLen = hextobinarray((char *)data, hexData);
816 }
817 if (dataLen==0) errors=TRUE;
818 if (errors) PrintAndLog ("Error getting hex data, datalen: %d",dataLen);
819 cmdp+=2;
820 break;
821 default:
822 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
823 errors = TRUE;
824 break;
825 }
826 if(errors) break;
827 }
828 if(cmdp == 0 && DemodBufferLen == 0)
829 {
830 errors = TRUE;// No args
831 }
832
833 //Validations
834 if(errors)
835 {
836 return usage_lf_simask();
837 }
838 if (dataLen == 0){ //using DemodBuffer
839 if (clk == 0) clk = GetAskClock("0", false, false);
840 } else {
841 setDemodBuf(data, dataLen, 0);
842 }
843 if (clk == 0) clk = 64;
844 if (encoding == 0) clk = clk/2; //askraw needs to double the clock speed
845 uint16_t arg1, arg2;
846 size_t size=DemodBufferLen;
847 arg1 = clk << 8 | encoding;
848 arg2 = invert << 8 | separator;
849 if (size > USB_CMD_DATA_SIZE) {
850 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size, USB_CMD_DATA_SIZE);
851 size = USB_CMD_DATA_SIZE;
852 }
853 UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
854 PrintAndLog("preparing to sim ask data: %d bits", size);
855 memcpy(c.d.asBytes, DemodBuffer, size);
856 SendCommand(&c);
857 return 0;
858 }
859
860 // by marshmellow - sim psk data given carrier, clock, invert
861 // - allow pull data from DemodBuffer or parameters
862 int CmdLFpskSim(const char *Cmd)
863 {
864 //might be able to autodetect FC and clock from Graphbuffer if using demod buffer
865 //will need carrier, Clock, and bitstream
866 uint8_t carrier=0, clk=0;
867 uint8_t invert=0;
868 bool errors = FALSE;
869 char hexData[32] = {0x00}; // store entered hex data
870 uint8_t data[255] = {0x00};
871 int dataLen = 0;
872 uint8_t cmdp = 0;
873 uint8_t pskType = 1;
874 while(param_getchar(Cmd, cmdp) != 0x00)
875 {
876 switch(param_getchar(Cmd, cmdp))
877 {
878 case 'h':
879 return usage_lf_simpsk();
880 case 'i':
881 invert = 1;
882 cmdp++;
883 break;
884 case 'c':
885 errors |= param_getdec(Cmd,cmdp+1,&clk);
886 cmdp+=2;
887 break;
888 case 'r':
889 errors |= param_getdec(Cmd,cmdp+1,&carrier);
890 cmdp+=2;
891 break;
892 case '1':
893 pskType=1;
894 cmdp++;
895 break;
896 case '2':
897 pskType=2;
898 cmdp++;
899 break;
900 case '3':
901 pskType=3;
902 cmdp++;
903 break;
904 case 'd':
905 dataLen = param_getstr(Cmd, cmdp+1, hexData);
906 if (dataLen==0) {
907 errors=TRUE;
908 } else {
909 dataLen = hextobinarray((char *)data, hexData);
910 }
911 if (dataLen==0) errors=TRUE;
912 if (errors) PrintAndLog ("Error getting hex data");
913 cmdp+=2;
914 break;
915 default:
916 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
917 errors = TRUE;
918 break;
919 }
920 if (errors) break;
921 }
922 if (cmdp == 0 && DemodBufferLen == 0)
923 {
924 errors = TRUE;// No args
925 }
926
927 //Validations
928 if (errors)
929 {
930 return usage_lf_simpsk();
931 }
932 if (dataLen == 0){ //using DemodBuffer
933 PrintAndLog("Getting Clocks");
934 if (clk==0) clk = GetPskClock("", FALSE, FALSE);
935 PrintAndLog("clk: %d",clk);
936 if (!carrier) carrier = GetPskCarrier("", FALSE, FALSE);
937 PrintAndLog("carrier: %d", carrier);
938 } else {
939 setDemodBuf(data, dataLen, 0);
940 }
941
942 if (clk <= 0) clk = 32;
943 if (carrier == 0) carrier = 2;
944 if (pskType != 1){
945 if (pskType == 2){
946 //need to convert psk2 to psk1 data before sim
947 psk2TOpsk1(DemodBuffer, DemodBufferLen);
948 } else {
949 PrintAndLog("Sorry, PSK3 not yet available");
950 }
951 }
952 uint16_t arg1, arg2;
953 arg1 = clk << 8 | carrier;
954 arg2 = invert;
955 size_t size=DemodBufferLen;
956 if (size > USB_CMD_DATA_SIZE) {
957 PrintAndLog("DemodBuffer too long for current implementation - length: %d - max: %d", size, USB_CMD_DATA_SIZE);
958 size=USB_CMD_DATA_SIZE;
959 }
960 UsbCommand c = {CMD_PSK_SIM_TAG, {arg1, arg2, size}};
961 PrintAndLog("DEBUG: Sending DemodBuffer Length: %d", size);
962 memcpy(c.d.asBytes, DemodBuffer, size);
963 SendCommand(&c);
964
965 return 0;
966 }
967
968 int CmdLFSimBidir(const char *Cmd)
969 {
970 // Set ADC to twice the carrier for a slight supersampling
971 // HACK: not implemented in ARMSRC.
972 PrintAndLog("Not implemented yet.");
973 UsbCommand c = {CMD_LF_SIMULATE_BIDIR, {47, 384, 0}};
974 SendCommand(&c);
975 return 0;
976 }
977
978 int CmdVchDemod(const char *Cmd)
979 {
980 // Is this the entire sync pattern, or does this also include some
981 // data bits that happen to be the same everywhere? That would be
982 // lovely to know.
983 static const int SyncPattern[] = {
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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
991 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
992 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
993 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
994 };
995
996 // So first, we correlate for the sync pattern, and mark that.
997 int bestCorrel = 0, bestPos = 0;
998 int i;
999 // It does us no good to find the sync pattern, with fewer than
1000 // 2048 samples after it...
1001 for (i = 0; i < (GraphTraceLen-2048); i++) {
1002 int sum = 0;
1003 int j;
1004 for (j = 0; j < arraylen(SyncPattern); j++) {
1005 sum += GraphBuffer[i+j]*SyncPattern[j];
1006 }
1007 if (sum > bestCorrel) {
1008 bestCorrel = sum;
1009 bestPos = i;
1010 }
1011 }
1012 PrintAndLog("best sync at %d [metric %d]", bestPos, bestCorrel);
1013
1014 char bits[257];
1015 bits[256] = '\0';
1016
1017 int worst = INT_MAX;
1018 int worstPos = 0;
1019
1020 for (i = 0; i < 2048; i += 8) {
1021 int sum = 0;
1022 int j;
1023 for (j = 0; j < 8; j++) {
1024 sum += GraphBuffer[bestPos+i+j];
1025 }
1026 if (sum < 0) {
1027 bits[i/8] = '.';
1028 } else {
1029 bits[i/8] = '1';
1030 }
1031 if(abs(sum) < worst) {
1032 worst = abs(sum);
1033 worstPos = i;
1034 }
1035 }
1036 PrintAndLog("bits:");
1037 PrintAndLog("%s", bits);
1038 PrintAndLog("worst metric: %d at pos %d", worst, worstPos);
1039
1040 if (strcmp(Cmd, "clone")==0) {
1041 GraphTraceLen = 0;
1042 char *s;
1043 for(s = bits; *s; s++) {
1044 int j;
1045 for(j = 0; j < 16; j++) {
1046 GraphBuffer[GraphTraceLen++] = (*s == '1') ? 1 : 0;
1047 }
1048 }
1049 RepaintGraphWindow();
1050 }
1051 return 0;
1052 }
1053
1054 //by marshmellow
1055 int CmdLFfind(const char *Cmd)
1056 {
1057 int ans = 0;
1058 char cmdp = param_getchar(Cmd, 0);
1059 char testRaw = param_getchar(Cmd, 1);
1060 if (strlen(Cmd) > 3 || cmdp == 'h' || cmdp == 'H') return usage_lf_find();
1061
1062 if (!offline && (cmdp != '1')){
1063 CmdLFRead("s");
1064 getSamples("30000",false);
1065 } else if (GraphTraceLen < 1000) {
1066 PrintAndLog("Data in Graphbuffer was too small.");
1067 return 0;
1068 }
1069 if (cmdp == 'u' || cmdp == 'U') testRaw = 'u';
1070
1071 PrintAndLog("NOTE: some demods output possible binary\n if it finds something that looks like a tag");
1072 PrintAndLog("False Positives ARE possible\n");
1073 PrintAndLog("\nChecking for known tags:\n");
1074
1075 ans=CmdFSKdemodIO("");
1076 if (ans>0) {
1077 PrintAndLog("\nValid IO Prox ID Found!");
1078 return 1;
1079 }
1080
1081 ans=CmdFSKdemodPyramid("");
1082 if (ans>0) {
1083 PrintAndLog("\nValid Pyramid ID Found!");
1084 return 1;
1085 }
1086
1087 ans=CmdFSKdemodParadox("");
1088 if (ans>0) {
1089 PrintAndLog("\nValid Paradox ID Found!");
1090 return 1;
1091 }
1092
1093 ans=CmdFSKdemodAWID("");
1094 if (ans>0) {
1095 PrintAndLog("\nValid AWID ID Found!");
1096 return 1;
1097 }
1098
1099 ans=CmdFSKdemodHID("");
1100 if (ans>0) {
1101 PrintAndLog("\nValid HID Prox ID Found!");
1102 return 1;
1103 }
1104
1105 ans=CmdAskEM410xDemod("");
1106 if (ans>0) {
1107 PrintAndLog("\nValid EM410x ID Found!");
1108 return 1;
1109 }
1110
1111 ans=CmdG_Prox_II_Demod("");
1112 if (ans>0) {
1113 PrintAndLog("\nValid Guardall G-Prox II ID Found!");
1114 return 1;
1115 }
1116
1117 ans=CmdFDXBdemodBI("");
1118 if (ans>0) {
1119 PrintAndLog("\nValid FDX-B ID Found!");
1120 return 1;
1121 }
1122
1123 ans=EM4x50Read("", false);
1124 if (ans>0) {
1125 PrintAndLog("\nValid EM4x50 ID Found!");
1126 return 1;
1127 }
1128
1129 ans=CmdVikingDemod("");
1130 if (ans>0) {
1131 PrintAndLog("\nValid Viking ID Found!");
1132 return 1;
1133 }
1134
1135 ans=CmdIndalaDecode("");
1136 if (ans>0) {
1137 PrintAndLog("\nValid Indala 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 {"awid", CmdLFAWID, 1, "{ AWID RFIDs... }"},
1209 {"em4x", CmdLFEM4X, 1, "{ EM4X 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, "Snoop LF"},
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