]>
Commit | Line | Data |
---|---|---|
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 | ||
ad939de5 | 11 | #include "cmdlfhid.h" |
12 | ||
7fe9b0b7 | 13 | #include <stdio.h> |
54a942b0 | 14 | #include <string.h> |
ad939de5 | 15 | #include "comms.h" |
7fe9b0b7 | 16 | #include "ui.h" |
17 | #include "graph.h" | |
18 | #include "cmdparser.h" | |
6cd2eef4 | 19 | #include "cmddata.h" //for g_debugMode, demodbuff cmds |
20 | #include "lfdemod.h" // for HIDdemodFSK | |
7fe9b0b7 | 21 | |
22 | static int CmdHelp(const char *Cmd); | |
6cd2eef4 | 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) | |
7fe9b0b7 | 28 | { |
6cd2eef4 | 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 | |
1c70664a | 36 | int waveIdx = 0; |
37 | int idx = HIDdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo, &waveIdx); | |
6cd2eef4 | 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 | } | |
7fe9b0b7 | 52 | return 0; |
53 | } | |
6cd2eef4 | 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 | } | |
7fe9b0b7 | 98 | } |
6cd2eef4 | 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); | |
7fe9b0b7 | 102 | } |
6cd2eef4 | 103 | setDemodBuf(BitStream,BitLen,idx); |
1c70664a | 104 | setClockGrid(50, waveIdx + (idx*50)); |
6cd2eef4 | 105 | if (g_debugMode){ |
106 | PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen); | |
107 | printDemodBuff(); | |
108 | } | |
109 | return 1; | |
7fe9b0b7 | 110 | } |
6cd2eef4 | 111 | |
112 | int CmdHIDReadFSK(const char *Cmd) | |
7fe9b0b7 | 113 | { |
083ca3de | 114 | int findone=0; |
3fe4ff4f | 115 | if(Cmd[0]=='1') findone=1; |
7fe9b0b7 | 116 | UsbCommand c={CMD_HID_DEMOD_FSK}; |
083ca3de | 117 | c.arg[0]=findone; |
7fe9b0b7 | 118 | SendCommand(&c); |
119 | return 0; | |
120 | } | |
121 | ||
122 | int CmdHIDSim(const char *Cmd) | |
38b20f75 | 123 | { |
8cf533fd | 124 | uint32_t hi = 0, lo = 0; |
125 | int n = 0, i = 0; | |
38b20f75 | 126 | |
8cf533fd | 127 | while (sscanf(&Cmd[i++], "%1x", &n ) == 1) { |
128 | hi = (hi << 4) | (lo >> 28); | |
129 | lo = (lo << 4) | (n & 0xf); | |
130 | } | |
38b20f75 | 131 | |
8cf533fd | 132 | PrintAndLog("Emulating tag with ID %x%08x", hi, lo); |
133 | PrintAndLog("Press pm3-button to abort simulation"); | |
38b20f75 | 134 | |
8cf533fd | 135 | UsbCommand c = {CMD_HID_SIM_TAG, {hi, lo, 0}}; |
136 | SendCommand(&c); | |
137 | return 0; | |
38b20f75 | 138 | } |
139 | ||
140 | int CmdHIDClone(const char *Cmd) | |
7fe9b0b7 | 141 | { |
54a942b0 | 142 | unsigned int hi2 = 0, hi = 0, lo = 0; |
035303ac | 143 | int n = 0, i = 0; |
54a942b0 | 144 | UsbCommand c; |
7fe9b0b7 | 145 | |
54a942b0 | 146 | if (strchr(Cmd,'l') != 0) { |
38b20f75 | 147 | while (sscanf(&Cmd[i++], "%1x", &n ) == 1) { |
54a942b0 | 148 | hi2 = (hi2 << 4) | (hi >> 28); |
149 | hi = (hi << 4) | (lo >> 28); | |
150 | lo = (lo << 4) | (n & 0xf); | |
151 | } | |
38b20f75 | 152 | |
54a942b0 | 153 | PrintAndLog("Cloning tag with long ID %x%08x%08x", hi2, hi, lo); |
38b20f75 | 154 | |
54a942b0 | 155 | c.d.asBytes[0] = 1; |
38b20f75 | 156 | } |
157 | else { | |
158 | while (sscanf(&Cmd[i++], "%1x", &n ) == 1) { | |
54a942b0 | 159 | hi = (hi << 4) | (lo >> 28); |
160 | lo = (lo << 4) | (n & 0xf); | |
161 | } | |
38b20f75 | 162 | |
54a942b0 | 163 | PrintAndLog("Cloning tag with ID %x%08x", hi, lo); |
38b20f75 | 164 | |
54a942b0 | 165 | hi2 = 0; |
166 | c.d.asBytes[0] = 0; | |
7fe9b0b7 | 167 | } |
38b20f75 | 168 | |
169 | c.cmd = CMD_HID_CLONE_TAG; | |
d16d20b1 | 170 | c.arg[0] = hi2; |
171 | c.arg[1] = hi; | |
172 | c.arg[2] = lo; | |
ec09b62d | 173 | |
ec09b62d | 174 | SendCommand(&c); |
175 | return 0; | |
176 | } | |
177 | ||
7fe9b0b7 | 178 | static command_t CommandTable[] = |
179 | { | |
180 | {"help", CmdHelp, 1, "This help"}, | |
6cd2eef4 | 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)"}, | |
083ca3de | 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)"}, | |
7fe9b0b7 | 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 | } |