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