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