1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
7 //-----------------------------------------------------------------------------
8 // Low frequency commands
9 //-----------------------------------------------------------------------------
15 #include "proxmark3.h"
19 #include "cmdparser.h"
25 #include "cmdlfem4x.h"
26 #include "cmdlfhitag.h"
27 #include "cmdlft55xx.h"
28 #include "cmdlfpcf7931.h"
31 static int CmdHelp(const char *Cmd
);
33 /* send a command before reading */
34 int CmdLFCommandRead(const char *Cmd
)
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
);
48 int CmdFlexdemod(const char *Cmd
)
51 for (i
= 0; i
< GraphTraceLen
; ++i
) {
52 if (GraphBuffer
[i
] < 0) {
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
) {
68 if (i
== (start
+ LONG_WAIT
)) {
72 if (start
== GraphTraceLen
- LONG_WAIT
) {
73 PrintAndLog("nothing to wait for");
77 GraphBuffer
[start
] = 2;
78 GraphBuffer
[start
+1] = -2;
79 uint8_t bits
[64] = {0x00};
83 for (bit
= 0; bit
< 64; bit
++) {
85 for (int j
= 0; j
< 16; j
++) {
86 sum
+= GraphBuffer
[i
++];
89 bits
[bit
] = (sum
> 0) ? 1 : 0;
91 PrintAndLog("bit %d sum %d", bit
, sum
);
94 for (bit
= 0; bit
< 64; bit
++) {
97 for (j
= 0; j
< 16; j
++) {
98 sum
+= GraphBuffer
[i
++];
100 if (sum
> 0 && bits
[bit
] != 1) {
101 PrintAndLog("oops1 at %d", bit
);
103 if (sum
< 0 && bits
[bit
] != 0) {
104 PrintAndLog("oops2 at %d", bit
);
108 // HACK writing back to graphbuffer.
109 GraphTraceLen
= 32*64;
112 for (bit
= 0; bit
< 64; bit
++) {
114 phase
= (bits
[bit
] == 0) ? 0 : 1;
117 for (j
= 0; j
< 32; j
++) {
118 GraphBuffer
[i
++] = phase
;
123 RepaintGraphWindow();
127 int CmdIndalaDemod(const char *Cmd
)
129 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
135 // worst case with GraphTraceLen=64000 is < 4096
136 // under normal conditions it's < 2048
138 uint8_t rawbits
[4096];
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) {
144 if ((GraphBuffer
[i
] > GraphBuffer
[i
+ 1]) && (state
!= 1)) {
146 for (j
= 0; j
< count
- 8; j
+= 16) {
147 rawbits
[rawbit
++] = 0;
149 if ((abs(count
- j
)) > worst
) {
150 worst
= abs(count
- j
);
156 } else if ((GraphBuffer
[i
] < GraphBuffer
[i
+ 1]) && (state
!= 0)) {
158 for (j
= 0; j
< count
- 8; j
+= 16) {
159 rawbits
[rawbit
++] = 1;
161 if ((abs(count
- j
)) > worst
) {
162 worst
= abs(count
- j
);
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
);
178 // Finding the start of a UID
179 int uidlen
, long_wait
;
180 if (strcmp(Cmd
, "224") == 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
) {
197 if (i
== (start
+ long_wait
)) {
202 if (start
== rawbit
- uidlen
+ 1) {
203 PrintAndLog("nothing to wait for");
207 // Inverting signal if needed
209 for (i
= start
; i
< rawbit
; i
++) {
210 rawbits
[i
] = !rawbits
[i
];
215 uint8_t bits
[224] = {0x00};
216 char showbits
[225] = {0x00};
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
];
228 showbits
[bit
+1]='\0';
229 PrintAndLog("Partial UID=%s", showbits
);
232 for (bit
= 0; bit
< uidlen
; bit
++) {
233 bits
[bit
] = rawbits
[i
++];
234 showbits
[bit
] = '0' + bits
[bit
];
240 uint32_t uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
245 for( idx
=0; idx
<64; idx
++) {
246 if (showbits
[idx
] == '0') {
247 uid1
=(uid1
<<1)|(uid2
>>31);
250 uid1
=(uid1
<<1)|(uid2
>>31);
254 PrintAndLog("UID=%s (%x%08x)", showbits
, uid1
, uid2
);
257 uid3
= uid4
= uid5
= uid6
= uid7
= 0;
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);
267 if (showbits
[idx
] == '0')
268 uid7
= (uid7
<<1) | 0;
270 uid7
= (uid7
<<1) | 1;
272 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits
, uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
275 // Checking UID against next occurrences
277 for (; i
+ uidlen
<= rawbit
;) {
279 for (bit
= 0; bit
< uidlen
; bit
++) {
280 if (bits
[bit
] != rawbits
[i
++]) {
291 PrintAndLog("Occurrences: %d (expected %d)", times
, (rawbit
- start
) / uidlen
);
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
;
299 for (bit
= 0; bit
< uidlen
; bit
++) {
300 if (bits
[bit
] == 0) {
306 for (j
= 0; j
< 32; j
++) {
307 GraphBuffer
[i
++] = phase
;
312 RepaintGraphWindow();
316 int CmdIndalaClone(const char *Cmd
)
319 unsigned int uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
321 uid1
= uid2
= uid3
= uid4
= uid5
= uid6
= uid7
= 0;
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);
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
;
344 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
345 uid1
= (uid1
<< 4) | (uid2
>> 28);
346 uid2
= (uid2
<< 4) | (n
& 0xf);
348 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1
, uid2
);
349 c
.cmd
= CMD_INDALA_CLONE_TAG
;
358 int CmdLFRead(const char *Cmd
)
360 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
};
362 // 'h' means higher-low-frequency, 134 kHz
365 } else if (*Cmd
== '\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>'");
374 WaitForResponse(CMD_ACK
,NULL
);
378 static void ChkBitstream(const char *str
)
382 /* convert to bitstream if necessary */
383 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++)
385 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0)
393 int CmdLFSim(const char *Cmd
)
398 sscanf(Cmd
, "%i", &gap
);
400 /* convert to bitstream if necessary */
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}};
407 for (j
= 0; j
< 48; j
++) {
408 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
411 WaitForResponse(CMD_ACK
,NULL
);
414 PrintAndLog("Starting simulator...");
415 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
420 int CmdLFSimBidir(const char *Cmd
)
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}};
430 /* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */
431 int CmdLFSimManchester(const char *Cmd
)
433 static int clock
, gap
;
434 static char data
[1024], gapstring
[8];
436 sscanf(Cmd
, "%i %s %i", &clock
, &data
[0], &gap
);
440 for (int i
= 0; i
< strlen(data
) ; ++i
)
441 AppendGraph(0, clock
, data
[i
]- '0');
443 CmdManchesterMod("");
445 RepaintGraphWindow();
447 sprintf(&gapstring
[0], "%i", gap
);
452 int CmdLFSnoop(const char *Cmd
)
454 UsbCommand c
= {CMD_LF_SNOOP_RAW_ADC_SAMPLES
};
456 // 'h' means higher-low-frequency, 134 kHz
461 sscanf(Cmd
, "l %"lli
, &c
.arg
[1]);
462 } else if(*Cmd
== 'h') {
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]");
473 WaitForResponse(CMD_ACK
,NULL
);
477 int CmdVchDemod(const char *Cmd
)
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
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,
495 // So first, we correlate for the sync pattern, and mark that.
496 int bestCorrel
= 0, bestPos
= 0;
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
++) {
503 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
504 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
506 if (sum
> bestCorrel
) {
511 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
519 for (i
= 0; i
< 2048; i
+= 8) {
522 for (j
= 0; j
< 8; j
++) {
523 sum
+= GraphBuffer
[bestPos
+i
+j
];
530 if(abs(sum
) < worst
) {
535 PrintAndLog("bits:");
536 PrintAndLog("%s", bits
);
537 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
539 if (strcmp(Cmd
, "clone")==0) {
542 for(s
= bits
; *s
; s
++) {
544 for(j
= 0; j
< 16; j
++) {
545 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
548 RepaintGraphWindow();
554 int CmdLFfind(const char *Cmd
)
559 ans
=CmdSamples("20000");
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:");
565 ans
=Cmdaskmandemod("");
567 PrintAndLog("Valid EM410x ID Found!");
570 ans
=CmdFSKdemodHID("");
572 PrintAndLog("Valid HID Prox ID Found!");
575 ans
=CmdFSKdemodIO("");
577 PrintAndLog("Valid IO Prox ID Found!");
581 ans
=CmdIndalaDecode("");
583 PrintAndLog("Valid Indala ID Found!");
586 // ans=CmdIndalaDemod("224");
587 // if (ans>0) return 1;
588 PrintAndLog("No Known Tags Found!\n");
592 static command_t CommandTable
[] =
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
}
616 int CmdLF(const char *Cmd
)
618 CmdsParse(CommandTable
, Cmd
);
622 int CmdHelp(const char *Cmd
)
624 CmdsHelp(CommandTable
);