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