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