]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlf.c
00f10088ead9055b9c8a978bee4397201c2ff8e4
[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 "proxusb.h"
16 #include "proxmark3.h"
17 #include "data.h"
18 #include "graph.h"
19 #include "ui.h"
20 #include "cmdparser.h"
21 #include "cmdmain.h"
22 #include "cmddata.h"
23 #include "cmdlf.h"
24 #include "cmdlfhid.h"
25 #include "cmdlfti.h"
26 #include "cmdlfem4x.h"
27 #include "cmdlfhitag.h"
28
29 static int CmdHelp(const char *Cmd);
30
31 /* send a command before reading */
32 int CmdLFCommandRead(const char *Cmd)
33 {
34 static char dummy[3];
35
36 dummy[0]= ' ';
37
38 UsbCommand c = {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K};
39 sscanf(Cmd, "%lli %lli %lli %s %s", &c.arg[0], &c.arg[1], &c.arg[2], (char *) &c.d.asBytes,(char *) &dummy+1);
40 // in case they specified 'h'
41 strcpy((char *)&c.d.asBytes + strlen((char *)c.d.asBytes), dummy);
42 SendCommand(&c);
43 return 0;
44 }
45
46 int CmdFlexdemod(const char *Cmd)
47 {
48 int i;
49 for (i = 0; i < GraphTraceLen; ++i) {
50 if (GraphBuffer[i] < 0) {
51 GraphBuffer[i] = -1;
52 } else {
53 GraphBuffer[i] = 1;
54 }
55 }
56
57 #define LONG_WAIT 100
58 int start;
59 for (start = 0; start < GraphTraceLen - LONG_WAIT; start++) {
60 int first = GraphBuffer[start];
61 for (i = start; i < start + LONG_WAIT; i++) {
62 if (GraphBuffer[i] != first) {
63 break;
64 }
65 }
66 if (i == (start + LONG_WAIT)) {
67 break;
68 }
69 }
70 if (start == GraphTraceLen - LONG_WAIT) {
71 PrintAndLog("nothing to wait for");
72 return 0;
73 }
74
75 GraphBuffer[start] = 2;
76 GraphBuffer[start+1] = -2;
77
78 uint8_t bits[64];
79
80 int bit;
81 i = start;
82 for (bit = 0; bit < 64; bit++) {
83 int j;
84 int sum = 0;
85 for (j = 0; j < 16; j++) {
86 sum += GraphBuffer[i++];
87 }
88 if (sum > 0) {
89 bits[bit] = 1;
90 } else {
91 bits[bit] = 0;
92 }
93 PrintAndLog("bit %d sum %d", bit, sum);
94 }
95
96 for (bit = 0; bit < 64; bit++) {
97 int j;
98 int sum = 0;
99 for (j = 0; j < 16; j++) {
100 sum += GraphBuffer[i++];
101 }
102 if (sum > 0 && bits[bit] != 1) {
103 PrintAndLog("oops1 at %d", bit);
104 }
105 if (sum < 0 && bits[bit] != 0) {
106 PrintAndLog("oops2 at %d", bit);
107 }
108 }
109
110 GraphTraceLen = 32*64;
111 i = 0;
112 int phase = 0;
113 for (bit = 0; bit < 64; bit++) {
114 if (bits[bit] == 0) {
115 phase = 0;
116 } else {
117 phase = 1;
118 }
119 int j;
120 for (j = 0; j < 32; j++) {
121 GraphBuffer[i++] = phase;
122 phase = !phase;
123 }
124 }
125
126 RepaintGraphWindow();
127 return 0;
128 }
129
130 int CmdIndalaDemod(const char *Cmd)
131 {
132 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
133
134 int state = -1;
135 int count = 0;
136 int i, j;
137 // worst case with GraphTraceLen=64000 is < 4096
138 // under normal conditions it's < 2048
139 uint8_t rawbits[4096];
140 int rawbit = 0;
141 int worst = 0, worstPos = 0;
142 PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen / 32);
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 }
171 PrintAndLog("Recovered %d raw bits", rawbit);
172 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst, worstPos);
173
174 // Finding the start of a UID
175 int uidlen, long_wait;
176 if (strcmp(Cmd, "224") == 0) {
177 uidlen = 224;
178 long_wait = 30;
179 } else {
180 uidlen = 64;
181 long_wait = 29;
182 }
183 int start;
184 int first = 0;
185 for (start = 0; start <= rawbit - uidlen; start++) {
186 first = rawbits[start];
187 for (i = start; i < start + long_wait; i++) {
188 if (rawbits[i] != first) {
189 break;
190 }
191 }
192 if (i == (start + long_wait)) {
193 break;
194 }
195 }
196 if (start == rawbit - uidlen + 1) {
197 PrintAndLog("nothing to wait for");
198 return 0;
199 }
200
201 // Inverting signal if needed
202 if (first == 1) {
203 for (i = start; i < rawbit; i++) {
204 rawbits[i] = !rawbits[i];
205 }
206 }
207
208 // Dumping UID
209 uint8_t bits[224];
210 char showbits[225];
211 showbits[uidlen]='\0';
212 int bit;
213 i = start;
214 int times = 0;
215 if (uidlen > rawbit) {
216 PrintAndLog("Warning: not enough raw bits to get a full UID");
217 for (bit = 0; bit < rawbit; bit++) {
218 bits[bit] = rawbits[i++];
219 // As we cannot know the parity, let's use "." and "/"
220 showbits[bit] = '.' + bits[bit];
221 }
222 showbits[bit+1]='\0';
223 PrintAndLog("Partial UID=%s", showbits);
224 return 0;
225 } else {
226 for (bit = 0; bit < uidlen; bit++) {
227 bits[bit] = rawbits[i++];
228 showbits[bit] = '0' + bits[bit];
229 }
230 times = 1;
231 }
232
233 //convert UID to HEX
234 uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7;
235 int idx;
236 uid1=0;
237 uid2=0;
238 if (uidlen==64){
239 for( idx=0; idx<64; idx++) {
240 if (showbits[idx] == '0') {
241 uid1=(uid1<<1)|(uid2>>31);
242 uid2=(uid2<<1)|0;
243 } else {
244 uid1=(uid1<<1)|(uid2>>31);
245 uid2=(uid2<<1)|1;
246 }
247 }
248 PrintAndLog("UID=%s (%x%08x)", showbits, uid1, uid2);
249 }
250 else {
251 uid3=0;
252 uid4=0;
253 uid5=0;
254 uid6=0;
255 uid7=0;
256 for( idx=0; idx<224; idx++) {
257 uid1=(uid1<<1)|(uid2>>31);
258 uid2=(uid2<<1)|(uid3>>31);
259 uid3=(uid3<<1)|(uid4>>31);
260 uid4=(uid4<<1)|(uid5>>31);
261 uid5=(uid5<<1)|(uid6>>31);
262 uid6=(uid6<<1)|(uid7>>31);
263 if (showbits[idx] == '0') uid7=(uid7<<1)|0;
264 else uid7=(uid7<<1)|1;
265 }
266 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits, uid1, uid2, uid3, uid4, uid5, uid6, uid7);
267 }
268
269 // Checking UID against next occurences
270 for (; i + uidlen <= rawbit;) {
271 int failed = 0;
272 for (bit = 0; bit < uidlen; bit++) {
273 if (bits[bit] != rawbits[i++]) {
274 failed = 1;
275 break;
276 }
277 }
278 if (failed == 1) {
279 break;
280 }
281 times += 1;
282 }
283 PrintAndLog("Occurences: %d (expected %d)", times, (rawbit - start) / uidlen);
284
285 // Remodulating for tag cloning
286 GraphTraceLen = 32*uidlen;
287 i = 0;
288 int phase = 0;
289 for (bit = 0; bit < uidlen; bit++) {
290 if (bits[bit] == 0) {
291 phase = 0;
292 } else {
293 phase = 1;
294 }
295 int j;
296 for (j = 0; j < 32; j++) {
297 GraphBuffer[i++] = phase;
298 phase = !phase;
299 }
300 }
301
302 RepaintGraphWindow();
303 return 0;
304 }
305
306 int CmdIndalaClone(const char *Cmd)
307 {
308 unsigned int uid1, uid2, uid3, uid4, uid5, uid6, uid7;
309 UsbCommand c;
310 uid1=0;
311 uid2=0;
312 uid3=0;
313 uid4=0;
314 uid5=0;
315 uid6=0;
316 uid7=0;
317 int n = 0, i = 0;
318
319 if (strchr(Cmd,'l') != 0) {
320 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
321 uid1 = (uid1 << 4) | (uid2 >> 28);
322 uid2 = (uid2 << 4) | (uid3 >> 28);
323 uid3 = (uid3 << 4) | (uid4 >> 28);
324 uid4 = (uid4 << 4) | (uid5 >> 28);
325 uid5 = (uid5 << 4) | (uid6 >> 28);
326 uid6 = (uid6 << 4) | (uid7 >> 28);
327 uid7 = (uid7 << 4) | (n & 0xf);
328 }
329 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1, uid2, uid3, uid4, uid5, uid6, uid7);
330 c.cmd = CMD_INDALA_CLONE_TAG_L;
331 c.d.asDwords[0] = uid1;
332 c.d.asDwords[1] = uid2;
333 c.d.asDwords[2] = uid3;
334 c.d.asDwords[3] = uid4;
335 c.d.asDwords[4] = uid5;
336 c.d.asDwords[5] = uid6;
337 c.d.asDwords[6] = uid7;
338 }
339 else
340 {
341 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
342 uid1 = (uid1 << 4) | (uid2 >> 28);
343 uid2 = (uid2 << 4) | (n & 0xf);
344 }
345 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1, uid2);
346 c.cmd = CMD_INDALA_CLONE_TAG;
347 c.arg[0] = uid1;
348 c.arg[1] = uid2;
349 }
350
351 SendCommand(&c);
352 return 0;
353 }
354
355 int CmdLFRead(const char *Cmd)
356 {
357 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K};
358 // 'h' means higher-low-frequency, 134 kHz
359 if(*Cmd == 'h') {
360 c.arg[0] = 1;
361 } else if (*Cmd == '\0') {
362 c.arg[0] = 0;
363 } else {
364 PrintAndLog("use 'read' or 'read h'");
365 return 0;
366 }
367 SendCommand(&c);
368 WaitForResponse(CMD_ACK,NULL);
369 return 0;
370 }
371
372 static void ChkBitstream(const char *str)
373 {
374 int i;
375
376 /* convert to bitstream if necessary */
377 for (i = 0; i < (int)(GraphTraceLen / 2); i++)
378 {
379 if (GraphBuffer[i] > 1 || GraphBuffer[i] < 0)
380 {
381 CmdBitstream(str);
382 break;
383 }
384 }
385 }
386
387 int CmdLFSim(const char *Cmd)
388 {
389 int i;
390 static int gap;
391
392 sscanf(Cmd, "%i", &gap);
393
394 /* convert to bitstream if necessary */
395 ChkBitstream(Cmd);
396
397 PrintAndLog("Sending data, please wait...");
398 for (i = 0; i < GraphTraceLen; i += 48) {
399 UsbCommand c={CMD_DOWNLOADED_SIM_SAMPLES_125K, {i, 0, 0}};
400 int j;
401 for (j = 0; j < 48; j++) {
402 c.d.asBytes[j] = GraphBuffer[i+j];
403 }
404 SendCommand(&c);
405 WaitForResponse(CMD_ACK,NULL);
406 }
407
408 PrintAndLog("Starting simulator...");
409 UsbCommand c = {CMD_SIMULATE_TAG_125K, {GraphTraceLen, gap, 0}};
410 SendCommand(&c);
411 return 0;
412 }
413
414 int CmdLFSimBidir(const char *Cmd)
415 {
416 /* Set ADC to twice the carrier for a slight supersampling */
417 UsbCommand c = {CMD_LF_SIMULATE_BIDIR, {47, 384, 0}};
418 SendCommand(&c);
419 return 0;
420 }
421
422 /* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */
423 int CmdLFSimManchester(const char *Cmd)
424 {
425 static int clock, gap;
426 static char data[1024], gapstring[8];
427
428 /* get settings/bits */
429 sscanf(Cmd, "%i %s %i", &clock, &data[0], &gap);
430
431 /* clear our graph */
432 ClearGraph(0);
433
434 /* fill it with our bitstream */
435 for (int i = 0; i < strlen(data) ; ++i)
436 AppendGraph(0, clock, data[i]- '0');
437
438 /* modulate */
439 CmdManchesterMod("");
440
441 /* show what we've done */
442 RepaintGraphWindow();
443
444 /* simulate */
445 sprintf(&gapstring[0], "%i", gap);
446 CmdLFSim(gapstring);
447 return 0;
448 }
449
450 int CmdVchDemod(const char *Cmd)
451 {
452 // Is this the entire sync pattern, or does this also include some
453 // data bits that happen to be the same everywhere? That would be
454 // lovely to know.
455 static const int SyncPattern[] = {
456 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
457 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
458 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
459 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
460 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
461 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
462 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
463 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
464 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
465 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
466 };
467
468 // So first, we correlate for the sync pattern, and mark that.
469 int bestCorrel = 0, bestPos = 0;
470 int i;
471 // It does us no good to find the sync pattern, with fewer than
472 // 2048 samples after it...
473 for (i = 0; i < (GraphTraceLen-2048); i++) {
474 int sum = 0;
475 int j;
476 for (j = 0; j < arraylen(SyncPattern); j++) {
477 sum += GraphBuffer[i+j]*SyncPattern[j];
478 }
479 if (sum > bestCorrel) {
480 bestCorrel = sum;
481 bestPos = i;
482 }
483 }
484 PrintAndLog("best sync at %d [metric %d]", bestPos, bestCorrel);
485
486 char bits[257];
487 bits[256] = '\0';
488
489 int worst = INT_MAX;
490 int worstPos = 0;
491
492 for (i = 0; i < 2048; i += 8) {
493 int sum = 0;
494 int j;
495 for (j = 0; j < 8; j++) {
496 sum += GraphBuffer[bestPos+i+j];
497 }
498 if (sum < 0) {
499 bits[i/8] = '.';
500 } else {
501 bits[i/8] = '1';
502 }
503 if(abs(sum) < worst) {
504 worst = abs(sum);
505 worstPos = i;
506 }
507 }
508 PrintAndLog("bits:");
509 PrintAndLog("%s", bits);
510 PrintAndLog("worst metric: %d at pos %d", worst, worstPos);
511
512 if (strcmp(Cmd, "clone")==0) {
513 GraphTraceLen = 0;
514 char *s;
515 for(s = bits; *s; s++) {
516 int j;
517 for(j = 0; j < 16; j++) {
518 GraphBuffer[GraphTraceLen++] = (*s == '1') ? 1 : 0;
519 }
520 }
521 RepaintGraphWindow();
522 }
523 return 0;
524 }
525
526 static command_t CommandTable[] =
527 {
528 {"help", CmdHelp, 1, "This help"},
529 {"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)"},
530 {"em4x", CmdLFEM4X, 1, "{ EM4X RFIDs... }"},
531 {"flexdemod", CmdFlexdemod, 1, "Demodulate samples for FlexPass"},
532 {"hid", CmdLFHID, 1, "{ HID RFIDs... }"},
533 {"indalademod", CmdIndalaDemod, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
534 {"indalaclone", CmdIndalaClone, 1, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"},
535 {"read", CmdLFRead, 0, "['h'] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134)"},
536 {"sim", CmdLFSim, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
537 {"simbidir", CmdLFSimBidir, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
538 {"simman", CmdLFSimManchester, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"},
539 {"ti", CmdLFTI, 1, "{ TI RFIDs... }"},
540 {"hitag", CmdLFHitag, 1, "{ Hitag tags and transponders... }"},
541 {"vchdemod", CmdVchDemod, 1, "['clone'] -- Demodulate samples for VeriChip"},
542 {NULL, NULL, 0, NULL}
543 };
544
545 int CmdLF(const char *Cmd)
546 {
547 CmdsParse(CommandTable, Cmd);
548 return 0;
549 }
550
551 int CmdHelp(const char *Cmd)
552 {
553 CmdsHelp(CommandTable);
554 return 0;
555 }
Impressum, Datenschutz