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 "proxusb.h"
16 #include "proxmark3.h"
20 #include "cmdparser.h"
26 #include "cmdlfem4x.h"
27 #include "cmdlfhitag.h"
28 #include "cmdlft55xx.h"
29 #include "cmdlfpcf7931.h"
32 static int CmdHelp(const char *Cmd
);
34 /* send a command before reading */
35 int CmdLFCommandRead(const char *Cmd
)
41 UsbCommand c
= {CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K
};
42 sscanf(Cmd
, "%"lli
" %"lli
" %"lli
" %s %s", &c
.arg
[0], &c
.arg
[1], &c
.arg
[2],(char*)(&c
.d
.asBytes
),(char*)(&dummy
+1));
43 // in case they specified 'h'
44 strcpy((char *)&c
.d
.asBytes
+ strlen((char *)c
.d
.asBytes
), dummy
);
49 int CmdFlexdemod(const char *Cmd
)
52 for (i
= 0; i
< GraphTraceLen
; ++i
) {
53 if (GraphBuffer
[i
] < 0) {
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
) {
69 if (i
== (start
+ LONG_WAIT
)) {
73 if (start
== GraphTraceLen
- LONG_WAIT
) {
74 PrintAndLog("nothing to wait for");
78 GraphBuffer
[start
] = 2;
79 GraphBuffer
[start
+1] = -2;
85 for (bit
= 0; bit
< 64; bit
++) {
88 for (j
= 0; j
< 16; j
++) {
89 sum
+= GraphBuffer
[i
++];
96 PrintAndLog("bit %d sum %d", bit
, sum
);
99 for (bit
= 0; bit
< 64; bit
++) {
102 for (j
= 0; j
< 16; j
++) {
103 sum
+= GraphBuffer
[i
++];
105 if (sum
> 0 && bits
[bit
] != 1) {
106 PrintAndLog("oops1 at %d", bit
);
108 if (sum
< 0 && bits
[bit
] != 0) {
109 PrintAndLog("oops2 at %d", bit
);
113 GraphTraceLen
= 32*64;
116 for (bit
= 0; bit
< 64; bit
++) {
117 if (bits
[bit
] == 0) {
123 for (j
= 0; j
< 32; j
++) {
124 GraphBuffer
[i
++] = phase
;
129 RepaintGraphWindow();
133 int CmdIndalaDemod(const char *Cmd
)
135 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
140 // worst case with GraphTraceLen=64000 is < 4096
141 // under normal conditions it's < 2048
142 uint8_t rawbits
[4096];
144 int worst
= 0, worstPos
= 0;
145 PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen
/ 32);
146 for (i
= 0; i
< GraphTraceLen
-1; i
+= 2) {
148 if ((GraphBuffer
[i
] > GraphBuffer
[i
+ 1]) && (state
!= 1)) {
150 for (j
= 0; j
< count
- 8; j
+= 16) {
151 rawbits
[rawbit
++] = 0;
153 if ((abs(count
- j
)) > worst
) {
154 worst
= abs(count
- j
);
160 } else if ((GraphBuffer
[i
] < GraphBuffer
[i
+ 1]) && (state
!= 0)) {
162 for (j
= 0; j
< count
- 8; j
+= 16) {
163 rawbits
[rawbit
++] = 1;
165 if ((abs(count
- j
)) > worst
) {
166 worst
= abs(count
- j
);
174 PrintAndLog("Recovered %d raw bits", rawbit
);
175 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst
, worstPos
);
177 // Finding the start of a UID
178 int uidlen
, long_wait
;
179 if (strcmp(Cmd
, "224") == 0) {
188 for (start
= 0; start
<= rawbit
- uidlen
; start
++) {
189 first
= rawbits
[start
];
190 for (i
= start
; i
< start
+ long_wait
; i
++) {
191 if (rawbits
[i
] != first
) {
195 if (i
== (start
+ long_wait
)) {
199 if (start
== rawbit
- uidlen
+ 1) {
200 PrintAndLog("nothing to wait for");
204 // Inverting signal if needed
206 for (i
= start
; i
< rawbit
; i
++) {
207 rawbits
[i
] = !rawbits
[i
];
214 showbits
[uidlen
]='\0';
218 if (uidlen
> rawbit
) {
219 PrintAndLog("Warning: not enough raw bits to get a full UID");
220 for (bit
= 0; bit
< rawbit
; bit
++) {
221 bits
[bit
] = rawbits
[i
++];
222 // As we cannot know the parity, let's use "." and "/"
223 showbits
[bit
] = '.' + bits
[bit
];
225 showbits
[bit
+1]='\0';
226 PrintAndLog("Partial UID=%s", showbits
);
229 for (bit
= 0; bit
< uidlen
; bit
++) {
230 bits
[bit
] = rawbits
[i
++];
231 showbits
[bit
] = '0' + bits
[bit
];
237 uint32_t uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
242 for( idx
=0; idx
<64; idx
++) {
243 if (showbits
[idx
] == '0') {
244 uid1
=(uid1
<<1)|(uid2
>>31);
247 uid1
=(uid1
<<1)|(uid2
>>31);
251 PrintAndLog("UID=%s (%x%08x)", showbits
, uid1
, uid2
);
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 if (showbits
[idx
] == '0') uid7
=(uid7
<<1)|0;
267 else uid7
=(uid7
<<1)|1;
269 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits
, uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
272 // Checking UID against next occurences
273 for (; i
+ uidlen
<= rawbit
;) {
275 for (bit
= 0; bit
< uidlen
; bit
++) {
276 if (bits
[bit
] != rawbits
[i
++]) {
286 PrintAndLog("Occurences: %d (expected %d)", times
, (rawbit
- start
) / uidlen
);
288 // Remodulating for tag cloning
289 GraphTraceLen
= 32*uidlen
;
292 for (bit
= 0; bit
< uidlen
; bit
++) {
293 if (bits
[bit
] == 0) {
299 for (j
= 0; j
< 32; j
++) {
300 GraphBuffer
[i
++] = phase
;
305 RepaintGraphWindow();
309 int CmdIndalaClone(const char *Cmd
)
311 unsigned int uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
322 if (strchr(Cmd
,'l') != 0) {
323 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
324 uid1
= (uid1
<< 4) | (uid2
>> 28);
325 uid2
= (uid2
<< 4) | (uid3
>> 28);
326 uid3
= (uid3
<< 4) | (uid4
>> 28);
327 uid4
= (uid4
<< 4) | (uid5
>> 28);
328 uid5
= (uid5
<< 4) | (uid6
>> 28);
329 uid6
= (uid6
<< 4) | (uid7
>> 28);
330 uid7
= (uid7
<< 4) | (n
& 0xf);
332 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
333 c
.cmd
= CMD_INDALA_CLONE_TAG_L
;
334 c
.d
.asDwords
[0] = uid1
;
335 c
.d
.asDwords
[1] = uid2
;
336 c
.d
.asDwords
[2] = uid3
;
337 c
.d
.asDwords
[3] = uid4
;
338 c
.d
.asDwords
[4] = uid5
;
339 c
.d
.asDwords
[5] = uid6
;
340 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
};
361 // 'h' means higher-low-frequency, 134 kHz
364 } else if (*Cmd
== '\0') {
366 } else if (sscanf(Cmd
, "%"lli
, &c
.arg
[0]) != 1) {
367 PrintAndLog("use 'read' or 'read h', or 'read <divisor>'");
371 WaitForResponse(CMD_ACK
,NULL
);
375 static void ChkBitstream(const char *str
)
379 /* convert to bitstream if necessary */
380 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++)
382 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0)
390 int CmdLFSim(const char *Cmd
)
395 sscanf(Cmd
, "%i", &gap
);
397 /* convert to bitstream if necessary */
400 PrintAndLog("Sending data, please wait...");
401 for (i
= 0; i
< GraphTraceLen
; i
+= 48) {
402 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
404 for (j
= 0; j
< 48; j
++) {
405 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
408 WaitForResponse(CMD_ACK
,NULL
);
411 PrintAndLog("Starting simulator...");
412 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
417 int CmdLFSimBidir(const char *Cmd
)
419 /* Set ADC to twice the carrier for a slight supersampling */
420 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
425 /* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */
426 int CmdLFSimManchester(const char *Cmd
)
428 static int clock
, gap
;
429 static char data
[1024], gapstring
[8];
431 /* get settings/bits */
432 sscanf(Cmd
, "%i %s %i", &clock
, &data
[0], &gap
);
434 /* clear our graph */
437 /* fill it with our bitstream */
438 for (int i
= 0; i
< strlen(data
) ; ++i
)
439 AppendGraph(0, clock
, data
[i
]- '0');
442 CmdManchesterMod("");
444 /* show what we've done */
445 RepaintGraphWindow();
448 sprintf(&gapstring
[0], "%i", gap
);
453 int CmdLFSnoop(const char *Cmd
)
455 UsbCommand c
= {CMD_LF_SNOOP_RAW_ADC_SAMPLES
};
456 // 'h' means higher-low-frequency, 134 kHz
461 } else if (*Cmd
== 'l') {
462 sscanf(Cmd
, "l %"lli
, &c
.arg
[1]);
463 } else if(*Cmd
== 'h') {
465 sscanf(Cmd
, "h %"lli
, &c
.arg
[1]);
466 } else if (sscanf(Cmd
, "%"lli
" %"lli
, &c
.arg
[0], &c
.arg
[1]) < 1) {
467 PrintAndLog("use 'snoop' or 'snoop {l,h} [trigger threshold]', or 'snoop <divisor> [trigger threshold]'");
471 WaitForResponse(CMD_ACK
,NULL
);
475 int CmdVchDemod(const char *Cmd
)
477 // Is this the entire sync pattern, or does this also include some
478 // data bits that happen to be the same everywhere? That would be
480 static const int SyncPattern
[] = {
481 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
482 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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,
493 // So first, we correlate for the sync pattern, and mark that.
494 int bestCorrel
= 0, bestPos
= 0;
496 // It does us no good to find the sync pattern, with fewer than
497 // 2048 samples after it...
498 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
501 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
502 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
504 if (sum
> bestCorrel
) {
509 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
517 for (i
= 0; i
< 2048; i
+= 8) {
520 for (j
= 0; j
< 8; j
++) {
521 sum
+= GraphBuffer
[bestPos
+i
+j
];
528 if(abs(sum
) < worst
) {
533 PrintAndLog("bits:");
534 PrintAndLog("%s", bits
);
535 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
537 if (strcmp(Cmd
, "clone")==0) {
540 for(s
= bits
; *s
; s
++) {
542 for(j
= 0; j
< 16; j
++) {
543 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
546 RepaintGraphWindow();
551 static command_t CommandTable
[] =
553 {"help", CmdHelp
, 1, "This help"},
554 {"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)"},
555 {"em4x", CmdLFEM4X
, 1, "{ EM4X RFIDs... }"},
556 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
557 {"hid", CmdLFHID
, 1, "{ HID RFIDs... }"},
558 {"io", CmdLFIO
, 1, "{ ioProx tags... }"},
559 {"indalademod", CmdIndalaDemod
, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
560 {"indalaclone", CmdIndalaClone
, 1, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"},
561 {"read", CmdLFRead
, 0, "['h' or <divisor>] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134, alternatively: f=12MHz/(divisor+1))"},
562 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
563 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
564 {"simman", CmdLFSimManchester
, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"},
565 {"snoop", CmdLFSnoop
, 0, "['l'|'h'|<divisor>] [trigger threshold]-- Snoop LF (l:125khz, h:134khz)"},
566 {"ti", CmdLFTI
, 1, "{ TI RFIDs... }"},
567 {"hitag", CmdLFHitag
, 1, "{ Hitag tags and transponders... }"},
568 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
569 {"t55xx", CmdLFT55XX
, 1, "{ T55xx RFIDs... }"},
570 {"pcf7931", CmdLFPCF7931
, 1, "{PCF7931 RFIDs...}"},
571 {NULL
, NULL
, 0, NULL
}
574 int CmdLF(const char *Cmd
)
576 CmdsParse(CommandTable
, Cmd
);
580 int CmdHelp(const char *Cmd
)
582 CmdsHelp(CommandTable
);