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"
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;
84 for (bit
= 0; bit
< 64; bit
++) {
87 for (j
= 0; j
< 16; j
++) {
88 sum
+= GraphBuffer
[i
++];
95 PrintAndLog("bit %d sum %d", bit
, sum
);
98 for (bit
= 0; bit
< 64; bit
++) {
101 for (j
= 0; j
< 16; j
++) {
102 sum
+= GraphBuffer
[i
++];
104 if (sum
> 0 && bits
[bit
] != 1) {
105 PrintAndLog("oops1 at %d", bit
);
107 if (sum
< 0 && bits
[bit
] != 0) {
108 PrintAndLog("oops2 at %d", bit
);
112 GraphTraceLen
= 32*64;
115 for (bit
= 0; bit
< 64; bit
++) {
116 if (bits
[bit
] == 0) {
122 for (j
= 0; j
< 32; j
++) {
123 GraphBuffer
[i
++] = phase
;
128 RepaintGraphWindow();
132 int CmdIndalaDemod(const char *Cmd
)
134 // Usage: recover 64bit UID by default, specify "224" as arg to recover a 224bit UID
139 // worst case with GraphTraceLen=64000 is < 4096
140 // under normal conditions it's < 2048
141 uint8_t rawbits
[4096];
143 int worst
= 0, worstPos
= 0;
144 PrintAndLog("Expecting a bit less than %d raw bits", GraphTraceLen
/ 32);
145 for (i
= 0; i
< GraphTraceLen
-1; i
+= 2) {
147 if ((GraphBuffer
[i
] > GraphBuffer
[i
+ 1]) && (state
!= 1)) {
149 for (j
= 0; j
< count
- 8; j
+= 16) {
150 rawbits
[rawbit
++] = 0;
152 if ((abs(count
- j
)) > worst
) {
153 worst
= abs(count
- j
);
159 } else if ((GraphBuffer
[i
] < GraphBuffer
[i
+ 1]) && (state
!= 0)) {
161 for (j
= 0; j
< count
- 8; j
+= 16) {
162 rawbits
[rawbit
++] = 1;
164 if ((abs(count
- j
)) > worst
) {
165 worst
= abs(count
- j
);
173 PrintAndLog("Recovered %d raw bits", rawbit
);
174 PrintAndLog("worst metric (0=best..7=worst): %d at pos %d", worst
, worstPos
);
176 // Finding the start of a UID
177 int uidlen
, long_wait
;
178 if (strcmp(Cmd
, "224") == 0) {
187 for (start
= 0; start
<= rawbit
- uidlen
; start
++) {
188 first
= rawbits
[start
];
189 for (i
= start
; i
< start
+ long_wait
; i
++) {
190 if (rawbits
[i
] != first
) {
194 if (i
== (start
+ long_wait
)) {
198 if (start
== rawbit
- uidlen
+ 1) {
199 PrintAndLog("nothing to wait for");
203 // Inverting signal if needed
205 for (i
= start
; i
< rawbit
; i
++) {
206 rawbits
[i
] = !rawbits
[i
];
213 showbits
[uidlen
]='\0';
217 if (uidlen
> rawbit
) {
218 PrintAndLog("Warning: not enough raw bits to get a full UID");
219 for (bit
= 0; bit
< rawbit
; bit
++) {
220 bits
[bit
] = rawbits
[i
++];
221 // As we cannot know the parity, let's use "." and "/"
222 showbits
[bit
] = '.' + bits
[bit
];
224 showbits
[bit
+1]='\0';
225 PrintAndLog("Partial UID=%s", showbits
);
228 for (bit
= 0; bit
< uidlen
; bit
++) {
229 bits
[bit
] = rawbits
[i
++];
230 showbits
[bit
] = '0' + bits
[bit
];
236 uint32_t uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
241 for( idx
=0; idx
<64; idx
++) {
242 if (showbits
[idx
] == '0') {
243 uid1
=(uid1
<<1)|(uid2
>>31);
246 uid1
=(uid1
<<1)|(uid2
>>31);
250 PrintAndLog("UID=%s (%x%08x)", showbits
, uid1
, uid2
);
258 for( idx
=0; idx
<224; idx
++) {
259 uid1
=(uid1
<<1)|(uid2
>>31);
260 uid2
=(uid2
<<1)|(uid3
>>31);
261 uid3
=(uid3
<<1)|(uid4
>>31);
262 uid4
=(uid4
<<1)|(uid5
>>31);
263 uid5
=(uid5
<<1)|(uid6
>>31);
264 uid6
=(uid6
<<1)|(uid7
>>31);
265 if (showbits
[idx
] == '0') uid7
=(uid7
<<1)|0;
266 else uid7
=(uid7
<<1)|1;
268 PrintAndLog("UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits
, uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
271 // Checking UID against next occurences
272 for (; i
+ uidlen
<= rawbit
;) {
274 for (bit
= 0; bit
< uidlen
; bit
++) {
275 if (bits
[bit
] != rawbits
[i
++]) {
285 PrintAndLog("Occurences: %d (expected %d)", times
, (rawbit
- start
) / uidlen
);
287 // Remodulating for tag cloning
288 GraphTraceLen
= 32*uidlen
;
291 for (bit
= 0; bit
< uidlen
; bit
++) {
292 if (bits
[bit
] == 0) {
298 for (j
= 0; j
< 32; j
++) {
299 GraphBuffer
[i
++] = phase
;
304 RepaintGraphWindow();
308 int CmdIndalaClone(const char *Cmd
)
310 unsigned int uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
;
321 if (strchr(Cmd
,'l') != 0) {
322 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
323 uid1
= (uid1
<< 4) | (uid2
>> 28);
324 uid2
= (uid2
<< 4) | (uid3
>> 28);
325 uid3
= (uid3
<< 4) | (uid4
>> 28);
326 uid4
= (uid4
<< 4) | (uid5
>> 28);
327 uid5
= (uid5
<< 4) | (uid6
>> 28);
328 uid6
= (uid6
<< 4) | (uid7
>> 28);
329 uid7
= (uid7
<< 4) | (n
& 0xf);
331 PrintAndLog("Cloning 224bit tag with UID %x%08x%08x%08x%08x%08x%08x", uid1
, uid2
, uid3
, uid4
, uid5
, uid6
, uid7
);
332 c
.cmd
= CMD_INDALA_CLONE_TAG_L
;
333 c
.d
.asDwords
[0] = uid1
;
334 c
.d
.asDwords
[1] = uid2
;
335 c
.d
.asDwords
[2] = uid3
;
336 c
.d
.asDwords
[3] = uid4
;
337 c
.d
.asDwords
[4] = uid5
;
338 c
.d
.asDwords
[5] = uid6
;
339 c
.d
.asDwords
[6] = uid7
;
343 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
344 uid1
= (uid1
<< 4) | (uid2
>> 28);
345 uid2
= (uid2
<< 4) | (n
& 0xf);
347 PrintAndLog("Cloning 64bit tag with UID %x%08x", uid1
, uid2
);
348 c
.cmd
= CMD_INDALA_CLONE_TAG
;
357 int CmdLFRead(const char *Cmd
)
359 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_125K
};
360 // 'h' means higher-low-frequency, 134 kHz
363 } else if (*Cmd
== '\0') {
365 } else if (sscanf(Cmd
, "%i", &c
.arg
[0]) != 1) {
366 PrintAndLog("use 'read' or 'read h', or 'read <divisor>'");
370 WaitForResponse(CMD_ACK
,NULL
);
374 static void ChkBitstream(const char *str
)
378 /* convert to bitstream if necessary */
379 for (i
= 0; i
< (int)(GraphTraceLen
/ 2); i
++)
381 if (GraphBuffer
[i
] > 1 || GraphBuffer
[i
] < 0)
389 int CmdLFSim(const char *Cmd
)
394 sscanf(Cmd
, "%i", &gap
);
396 /* convert to bitstream if necessary */
399 PrintAndLog("Sending data, please wait...");
400 for (i
= 0; i
< GraphTraceLen
; i
+= 48) {
401 UsbCommand c
={CMD_DOWNLOADED_SIM_SAMPLES_125K
, {i
, 0, 0}};
403 for (j
= 0; j
< 48; j
++) {
404 c
.d
.asBytes
[j
] = GraphBuffer
[i
+j
];
407 WaitForResponse(CMD_ACK
,NULL
);
410 PrintAndLog("Starting simulator...");
411 UsbCommand c
= {CMD_SIMULATE_TAG_125K
, {GraphTraceLen
, gap
, 0}};
416 int CmdLFSimBidir(const char *Cmd
)
418 /* Set ADC to twice the carrier for a slight supersampling */
419 UsbCommand c
= {CMD_LF_SIMULATE_BIDIR
, {47, 384, 0}};
424 /* simulate an LF Manchester encoded tag with specified bitstream, clock rate and inter-id gap */
425 int CmdLFSimManchester(const char *Cmd
)
427 static int clock
, gap
;
428 static char data
[1024], gapstring
[8];
430 /* get settings/bits */
431 sscanf(Cmd
, "%i %s %i", &clock
, &data
[0], &gap
);
433 /* clear our graph */
436 /* fill it with our bitstream */
437 for (int i
= 0; i
< strlen(data
) ; ++i
)
438 AppendGraph(0, clock
, data
[i
]- '0');
441 CmdManchesterMod("");
443 /* show what we've done */
444 RepaintGraphWindow();
447 sprintf(&gapstring
[0], "%i", gap
);
452 int CmdVchDemod(const char *Cmd
)
454 // Is this the entire sync pattern, or does this also include some
455 // data bits that happen to be the same everywhere? That would be
457 static const int SyncPattern
[] = {
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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
467 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
470 // So first, we correlate for the sync pattern, and mark that.
471 int bestCorrel
= 0, bestPos
= 0;
473 // It does us no good to find the sync pattern, with fewer than
474 // 2048 samples after it...
475 for (i
= 0; i
< (GraphTraceLen
-2048); i
++) {
478 for (j
= 0; j
< arraylen(SyncPattern
); j
++) {
479 sum
+= GraphBuffer
[i
+j
]*SyncPattern
[j
];
481 if (sum
> bestCorrel
) {
486 PrintAndLog("best sync at %d [metric %d]", bestPos
, bestCorrel
);
494 for (i
= 0; i
< 2048; i
+= 8) {
497 for (j
= 0; j
< 8; j
++) {
498 sum
+= GraphBuffer
[bestPos
+i
+j
];
505 if(abs(sum
) < worst
) {
510 PrintAndLog("bits:");
511 PrintAndLog("%s", bits
);
512 PrintAndLog("worst metric: %d at pos %d", worst
, worstPos
);
514 if (strcmp(Cmd
, "clone")==0) {
517 for(s
= bits
; *s
; s
++) {
519 for(j
= 0; j
< 16; j
++) {
520 GraphBuffer
[GraphTraceLen
++] = (*s
== '1') ? 1 : 0;
523 RepaintGraphWindow();
528 static command_t CommandTable
[] =
530 {"help", CmdHelp
, 1, "This help"},
531 {"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)"},
532 {"em4x", CmdLFEM4X
, 1, "{ EM4X RFIDs... }"},
533 {"flexdemod", CmdFlexdemod
, 1, "Demodulate samples for FlexPass"},
534 {"hid", CmdLFHID
, 1, "{ HID RFIDs... }"},
535 {"indalademod", CmdIndalaDemod
, 1, "['224'] -- Demodulate samples for Indala 64 bit UID (option '224' for 224 bit)"},
536 {"indalaclone", CmdIndalaClone
, 1, "<UID> ['l']-- Clone Indala to T55x7 (tag must be in antenna)(UID in HEX)(option 'l' for 224 UID"},
537 {"read", CmdLFRead
, 0, "['h'|<divisor>] -- Read 125/134 kHz LF ID-only tag (option 'h' for 134, alternatively: f=12MHz/(divisor+1))"},
538 {"sim", CmdLFSim
, 0, "[GAP] -- Simulate LF tag from buffer with optional GAP (in microseconds)"},
539 {"simbidir", CmdLFSimBidir
, 0, "Simulate LF tag (with bidirectional data transmission between reader and tag)"},
540 {"simman", CmdLFSimManchester
, 0, "<Clock> <Bitstream> [GAP] Simulate arbitrary Manchester LF tag"},
541 {"ti", CmdLFTI
, 1, "{ TI RFIDs... }"},
542 {"hitag", CmdLFHitag
, 1, "{ Hitag tags and transponders... }"},
543 {"vchdemod", CmdVchDemod
, 1, "['clone'] -- Demodulate samples for VeriChip"},
544 {"t55xx", CmdLFT55XX
, 1, "{ T55xx RFIDs... }"},
545 {"pcf7931", CmdLFPCF7931
, 1, "{PCF7931 RFIDs...}"},
546 {NULL
, NULL
, 0, NULL
}
549 int CmdLF(const char *Cmd
)
551 CmdsParse(CommandTable
, Cmd
);
555 int CmdHelp(const char *Cmd
)
557 CmdsHelp(CommandTable
);