]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/cmdlf.c
sync with master lf files to resolve conflicts
[proxmark3-svn] / client / cmdlf.c
... / ...
CommitLineData
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
31static int CmdHelp(const char *Cmd);
32
33/* send a command before reading */
34int 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
48int 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
127int 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
316int 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
358int 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
378static 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
393int CmdLFSim(const char *Cmd)
394{
395 int i;
396 static int gap;
397
398 sscanf(Cmd, "%i", &gap);
399
400 /* convert to bitstream if necessary */
401 ChkBitstream(Cmd);
402
403 PrintAndLog("Sending data, please wait...");
404 for (i = 0; i < GraphTraceLen; i += 48) {
405 UsbCommand c={CMD_DOWNLOADED_SIM_SAMPLES_125K, {i, 0, 0}};
406 int j;
407 for (j = 0; j < 48; j++) {
408 c.d.asBytes[j] = GraphBuffer[i+j];
409 }
410 SendCommand(&c);
411 WaitForResponse(CMD_ACK,NULL);
412 }
413
414 PrintAndLog("Starting simulator...");
415 UsbCommand c = {CMD_SIMULATE_TAG_125K, {GraphTraceLen, gap, 0}};
416 SendCommand(&c);
417 return 0;
418}
419
420int CmdLFSimBidir(const char *Cmd)
421{
422 // Set ADC to twice the carrier for a slight supersampling
423 // HACK: not implemented in ARMSRC.
424 PrintAndLog("Not implemented yet.");
425 UsbCommand c = {CMD_LF_SIMULATE_BIDIR, {47, 384, 0}};
426 SendCommand(&c);
427 return 0;
428}
429
430/* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */
431int CmdLFSimManchester(const char *Cmd)
432{
433 static int clock, gap;
434 static char data[1024], gapstring[8];
435
436 sscanf(Cmd, "%i %s %i", &clock, &data[0], &gap);
437
438 ClearGraph(0);
439
440 for (int i = 0; i < strlen(data) ; ++i)
441 AppendGraph(0, clock, data[i]- '0');
442
443 CmdManchesterMod("");
444
445 RepaintGraphWindow();
446
447 sprintf(&gapstring[0], "%i", gap);
448 CmdLFSim(gapstring);
449 return 0;
450}
451
452int CmdLFSnoop(const char *Cmd)
453{
454 UsbCommand c = {CMD_LF_SNOOP_RAW_ADC_SAMPLES};
455
456 // 'h' means higher-low-frequency, 134 kHz
457 c.arg[0] = 0;
458 c.arg[1] = -1;
459
460 if (*Cmd == 'l') {
461 sscanf(Cmd, "l %"lli, &c.arg[1]);
462 } else if(*Cmd == 'h') {
463 c.arg[0] = 1;
464 sscanf(Cmd, "h %"lli, &c.arg[1]);
465 } else if (sscanf(Cmd, "%"lli" %"lli, &c.arg[0], &c.arg[1]) < 1) {
466 PrintAndLog("usage 1: snoop");
467 PrintAndLog(" 2: snoop {l,h} [trigger threshold]");
468 PrintAndLog(" 3: snoop <divisor> [trigger threshold]");
469 return 0;
470 }
471
472 SendCommand(&c);
473 WaitForResponse(CMD_ACK,NULL);
474 return 0;
475}
476
477int CmdVchDemod(const char *Cmd)
478{
479 // Is this the entire sync pattern, or does this also include some
480 // data bits that happen to be the same everywhere? That would be
481 // lovely to know.
482 static const int SyncPattern[] = {
483 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
484 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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 };
494
495 // So first, we correlate for the sync pattern, and mark that.
496 int bestCorrel = 0, bestPos = 0;
497 int i;
498 // It does us no good to find the sync pattern, with fewer than
499 // 2048 samples after it...
500 for (i = 0; i < (GraphTraceLen-2048); i++) {
501 int sum = 0;
502 int j;
503 for (j = 0; j < arraylen(SyncPattern); j++) {
504 sum += GraphBuffer[i+j]*SyncPattern[j];
505 }
506 if (sum > bestCorrel) {
507 bestCorrel = sum;
508 bestPos = i;
509 }
510 }
511 PrintAndLog("best sync at %d [metric %d]", bestPos, bestCorrel);
512
513 char bits[257];
514 bits[256] = '\0';
515
516 int worst = INT_MAX;
517 int worstPos = 0;
518
519 for (i = 0; i < 2048; i += 8) {
520 int sum = 0;
521 int j;
522 for (j = 0; j < 8; j++) {
523 sum += GraphBuffer[bestPos+i+j];
524 }
525 if (sum < 0) {
526 bits[i/8] = '.';
527 } else {
528 bits[i/8] = '1';
529 }
530 if(abs(sum) < worst) {
531 worst = abs(sum);
532 worstPos = i;
533 }
534 }
535 PrintAndLog("bits:");
536 PrintAndLog("%s", bits);
537 PrintAndLog("worst metric: %d at pos %d", worst, worstPos);
538
539 if (strcmp(Cmd, "clone")==0) {
540 GraphTraceLen = 0;
541 char *s;
542 for(s = bits; *s; s++) {
543 int j;
544 for(j = 0; j < 16; j++) {
545 GraphBuffer[GraphTraceLen++] = (*s == '1') ? 1 : 0;
546 }
547 }
548 RepaintGraphWindow();
549 }
550 return 0;
551}
552
553//by marshmellow
554int CmdLFfind(const char *Cmd)
555{
556 int ans=0;
557 if (!offline){
558 ans=CmdLFRead("");
559 ans=CmdSamples("20000");
560 }
561 if (GraphTraceLen<1000) return 0;
562 PrintAndLog("NOTE: some demods output possible binary\n if it finds something that looks like a tag");
563 PrintAndLog("Checking for known tags:");
564
565 ans=Cmdaskmandemod("");
566 if (ans>0) {
567 PrintAndLog("Valid EM410x ID Found!");
568 return 1;
569 }
570 ans=CmdFSKdemodHID("");
571 if (ans>0) {
572 PrintAndLog("Valid HID Prox ID Found!");
573 return 1;
574 }
575 ans=CmdFSKdemodIO("");
576 if (ans>0) {
577 PrintAndLog("Valid IO Prox ID Found!");
578 return 1;
579 }
580 //add psk and indala
581 ans=CmdIndalaDecode("");
582 if (ans>0) {
583 PrintAndLog("Valid Indala ID Found!");
584 return 1;
585 }
586 // ans=CmdIndalaDemod("224");
587 // if (ans>0) return 1;
588 PrintAndLog("No Known Tags Found!\n");
589 return 0;
590}
591
592static command_t CommandTable[] =
593{
594 {"help", CmdHelp, 1, "This help"},
595 {"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)"},
596 {"em4x", CmdLFEM4X, 1, "{ EM4X RFIDs... }"},
597 {"flexdemod", CmdFlexdemod, 1, "Demodulate samples for FlexPass"},
598 {"hid", CmdLFHID, 1, "{ HID RFIDs... }"},
599 {"io", CmdLFIO, 1, "{ ioProx tags... }"},
600 {"indalademod", CmdIndalaDemod, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
601 {"indalaclone", CmdIndalaClone, 0, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"},
602 {"read", CmdLFRead, 0, "['h' or <divisor>] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134, alternatively: f=12MHz/(divisor+1))"},
603 {"search", CmdLFfind, 1, "Read and Search for valid known tag (in offline mode it you can load first then search)"},
604 {"sim", CmdLFSim, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
605 {"simbidir", CmdLFSimBidir, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
606 {"simman", CmdLFSimManchester, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"},
607 {"snoop", CmdLFSnoop, 0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"},
608 {"ti", CmdLFTI, 1, "{ TI RFIDs... }"},
609 {"hitag", CmdLFHitag, 1, "{ Hitag tags and transponders... }"},
610 {"vchdemod", CmdVchDemod, 1, "['clone'] -- Demodulate samples for VeriChip"},
611 {"t55xx", CmdLFT55XX, 1, "{ T55xx RFIDs... }"},
612 {"pcf7931", CmdLFPCF7931, 1, "{PCF7931 RFIDs...}"},
613 {NULL, NULL, 0, NULL}
614};
615
616int CmdLF(const char *Cmd)
617{
618 CmdsParse(CommandTable, Cmd);
619 return 0;
620}
621
622int CmdHelp(const char *Cmd)
623{
624 CmdsHelp(CommandTable);
625 return 0;
626}
Impressum, Datenschutz