]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfhid.c
ca58d8abfb453f5caaff087a1713abc9f1565ad9
[proxmark3-svn] / client / cmdlfhid.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 HID commands (known)
9 //-----------------------------------------------------------------------------
10
11 #include "cmdlfhid.h"
12
13 #include <stdio.h>
14 #include <string.h>
15 #include "comms.h"
16 #include "ui.h"
17 #include "graph.h"
18 #include "cmdparser.h"
19 #include "cmddata.h" //for g_debugMode, demodbuff cmds
20 #include "lfdemod.h" // for HIDdemodFSK
21
22 static int CmdHelp(const char *Cmd);
23
24 //by marshmellow (based on existing demod + holiman's refactor)
25 //HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded)
26 //print full HID Prox ID and some bit format details if found
27 int CmdFSKdemodHID(const char *Cmd)
28 {
29 //raw fsk demod no manchester decoding no start bit finding just get binary from wave
30 uint32_t hi2=0, hi=0, lo=0;
31
32 uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
33 size_t BitLen = getFromGraphBuf(BitStream);
34 if (BitLen==0) return 0;
35 //get binary from fsk wave
36 int waveIdx = 0;
37 int idx = HIDdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo, &waveIdx);
38 if (idx<0){
39 if (g_debugMode){
40 if (idx==-1){
41 PrintAndLog("DEBUG: Just Noise Detected");
42 } else if (idx == -2) {
43 PrintAndLog("DEBUG: Error demoding fsk");
44 } else if (idx == -3) {
45 PrintAndLog("DEBUG: Preamble not found");
46 } else if (idx == -4) {
47 PrintAndLog("DEBUG: Error in Manchester data, SIZE: %d", BitLen);
48 } else {
49 PrintAndLog("DEBUG: Error demoding fsk %d", idx);
50 }
51 }
52 return 0;
53 }
54 if (hi2==0 && hi==0 && lo==0) {
55 if (g_debugMode) PrintAndLog("DEBUG: Error - no values found");
56 return 0;
57 }
58 if (hi2 != 0){ //extra large HID tags
59 PrintAndLog("HID Prox TAG ID: %x%08x%08x (%d)",
60 (unsigned int) hi2, (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF);
61 }
62 else { //standard HID tags <38 bits
63 uint8_t fmtLen = 0;
64 uint32_t fc = 0;
65 uint32_t cardnum = 0;
66 if (((hi>>5)&1)==1){//if bit 38 is set then < 37 bit format is used
67 uint32_t lo2=0;
68 lo2=(((hi & 31) << 12) | (lo>>20)); //get bits 21-37 to check for format len bit
69 uint8_t idx3 = 1;
70 while(lo2>1){ //find last bit set to 1 (format len bit)
71 lo2=lo2>>1;
72 idx3++;
73 }
74 fmtLen =idx3+19;
75 fc =0;
76 cardnum=0;
77 if(fmtLen==26){
78 cardnum = (lo>>1)&0xFFFF;
79 fc = (lo>>17)&0xFF;
80 }
81 if(fmtLen==34){
82 cardnum = (lo>>1)&0xFFFF;
83 fc= ((hi&1)<<15)|(lo>>17);
84 }
85 if(fmtLen==35){
86 cardnum = (lo>>1)&0xFFFFF;
87 fc = ((hi&1)<<11)|(lo>>21);
88 }
89 }
90 else { //if bit 38 is not set then 37 bit format is used
91 fmtLen = 37;
92 fc = 0;
93 cardnum = 0;
94 if(fmtLen == 37){
95 cardnum = (lo>>1)&0x7FFFF;
96 fc = ((hi&0xF)<<12)|(lo>>20);
97 }
98 }
99 PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %dbit - FC: %d - Card: %d",
100 (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
101 (unsigned int) fmtLen, (unsigned int) fc, (unsigned int) cardnum);
102 }
103 setDemodBuf(BitStream,BitLen,idx);
104 setClockGrid(50, waveIdx + (idx*50));
105 if (g_debugMode){
106 PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen);
107 printDemodBuff();
108 }
109 return 1;
110 }
111
112 int CmdHIDReadFSK(const char *Cmd)
113 {
114 int findone=0;
115 if(Cmd[0]=='1') findone=1;
116 UsbCommand c={CMD_HID_DEMOD_FSK};
117 c.arg[0]=findone;
118 SendCommand(&c);
119 return 0;
120 }
121
122 int CmdHIDSim(const char *Cmd)
123 {
124 uint32_t hi = 0, lo = 0;
125 int n = 0, i = 0;
126
127 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
128 hi = (hi << 4) | (lo >> 28);
129 lo = (lo << 4) | (n & 0xf);
130 }
131
132 PrintAndLog("Emulating tag with ID %x%08x", hi, lo);
133 PrintAndLog("Press pm3-button to abort simulation");
134
135 UsbCommand c = {CMD_HID_SIM_TAG, {hi, lo, 0}};
136 SendCommand(&c);
137 return 0;
138 }
139
140 int CmdHIDClone(const char *Cmd)
141 {
142 unsigned int hi2 = 0, hi = 0, lo = 0;
143 int n = 0, i = 0;
144 UsbCommand c;
145
146 if (strchr(Cmd,'l') != 0) {
147 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
148 hi2 = (hi2 << 4) | (hi >> 28);
149 hi = (hi << 4) | (lo >> 28);
150 lo = (lo << 4) | (n & 0xf);
151 }
152
153 PrintAndLog("Cloning tag with long ID %x%08x%08x", hi2, hi, lo);
154
155 c.d.asBytes[0] = 1;
156 }
157 else {
158 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
159 hi = (hi << 4) | (lo >> 28);
160 lo = (lo << 4) | (n & 0xf);
161 }
162
163 PrintAndLog("Cloning tag with ID %x%08x", hi, lo);
164
165 hi2 = 0;
166 c.d.asBytes[0] = 0;
167 }
168
169 c.cmd = CMD_HID_CLONE_TAG;
170 c.arg[0] = hi2;
171 c.arg[1] = hi;
172 c.arg[2] = lo;
173
174 SendCommand(&c);
175 return 0;
176 }
177
178 static command_t CommandTable[] =
179 {
180 {"help", CmdHelp, 1, "This help"},
181 {"demod", CmdFSKdemodHID, 1, "Demodulate HID Prox from GraphBuffer"},
182 {"read", CmdHIDReadFSK, 0, "['1'] Realtime HID FSK Read from antenna (option '1' for one tag only)"},
183 {"sim", CmdHIDSim, 0, "<ID> -- HID tag simulator"},
184 {"clone", CmdHIDClone, 0, "<ID> ['l'] -- Clone HID to T55x7 (tag must be in antenna)(option 'l' for 84bit ID)"},
185 {NULL, NULL, 0, NULL}
186 };
187
188 int CmdLFHID(const char *Cmd)
189 {
190 CmdsParse(CommandTable, Cmd);
191 return 0;
192 }
193
194 int CmdHelp(const char *Cmd)
195 {
196 CmdsHelp(CommandTable);
197 return 0;
198 }
Impressum, Datenschutz