]>
Commit | Line | Data |
---|---|---|
db09cb3a | 1 | //----------------------------------------------------------------------------- |
2 | // Copyright (C) 2012 Roel Verdult | |
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 Hitag support | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include "data.h" | |
15 | #include "proxusb.h" | |
16 | #include "ui.h" | |
17 | #include "cmdparser.h" | |
18 | #include "common.h" | |
19 | #include "util.h" | |
20 | #include "hitag2.h" | |
21 | ||
22 | static int CmdHelp(const char *Cmd); | |
23 | ||
24 | int CmdLFHitagList(const char *Cmd) | |
25 | { | |
26 | uint8_t got[3000]; | |
27 | GetFromBigBuf(got,sizeof(got),0); | |
28 | ||
29 | PrintAndLog("recorded activity:"); | |
30 | PrintAndLog(" ETU :rssi: who bytes"); | |
31 | PrintAndLog("---------+----+----+-----------"); | |
32 | ||
33 | int i = 0; | |
34 | int prev = -1; | |
35 | ||
36 | for (;;) { | |
37 | if(i >= 1900) { | |
38 | break; | |
39 | } | |
40 | ||
41 | bool isResponse; | |
42 | int timestamp = *((uint32_t *)(got+i)); | |
43 | if (timestamp & 0x80000000) { | |
44 | timestamp &= 0x7fffffff; | |
45 | isResponse = 1; | |
46 | } else { | |
47 | isResponse = 0; | |
48 | } | |
49 | ||
50 | int metric = 0; | |
51 | int parityBits = *((uint32_t *)(got+i+4)); | |
52 | // 4 bytes of additional information... | |
53 | // maximum of 32 additional parity bit information | |
54 | // | |
55 | // TODO: | |
56 | // at each quarter bit period we can send power level (16 levels) | |
57 | // or each half bit period in 256 levels. | |
58 | ||
59 | int len = got[i+8]; | |
60 | ||
61 | if (len > 100) { | |
62 | break; | |
63 | } | |
64 | if (i + len >= 1900) { | |
65 | break; | |
66 | } | |
67 | ||
68 | uint8_t *frame = (got+i+9); | |
69 | ||
70 | // Break and stick with current result if buffer was not completely full | |
71 | if (frame[0] == 0x44 && frame[1] == 0x44 && frame[3] == 0x44) { break; } | |
72 | ||
73 | char line[1000] = ""; | |
74 | int j; | |
75 | for (j = 0; j < len; j++) { | |
76 | int oddparity = 0x01; | |
77 | int k; | |
78 | ||
79 | for (k=0;k<8;k++) { | |
80 | oddparity ^= (((frame[j] & 0xFF) >> k) & 0x01); | |
81 | } | |
82 | ||
83 | //if((parityBits >> (len - j - 1)) & 0x01) { | |
84 | if (isResponse && (oddparity != ((parityBits >> (len - j - 1)) & 0x01))) { | |
85 | sprintf(line+(j*4), "%02x! ", frame[j]); | |
86 | } | |
87 | else { | |
88 | sprintf(line+(j*4), "%02x ", frame[j]); | |
89 | } | |
90 | } | |
91 | ||
92 | char metricString[100]; | |
93 | if (isResponse) { | |
94 | sprintf(metricString, "%3d", metric); | |
95 | } else { | |
96 | strcpy(metricString, " "); | |
97 | } | |
98 | ||
99 | PrintAndLog(" +%7d: %s: %s %s", | |
100 | (prev < 0 ? 0 : (timestamp - prev)), | |
101 | metricString, | |
102 | (isResponse ? "TAG" : " "), | |
103 | line); | |
104 | ||
105 | prev = timestamp; | |
106 | i += (len + 9); | |
107 | } | |
108 | return 0; | |
109 | } | |
110 | ||
111 | int CmdLFHitagSnoop(const char *Cmd) { | |
112 | UsbCommand c = {CMD_SNOOP_HITAG}; | |
113 | SendCommand(&c); | |
114 | return 0; | |
115 | } | |
116 | ||
117 | int CmdLFHitagSim(const char *Cmd) { | |
118 | UsbCommand c = {CMD_SIMULATE_HITAG}; | |
119 | char filename[256]; | |
120 | FILE* pf; | |
121 | bool tag_mem_supplied; | |
122 | ||
123 | param_getstr(Cmd,0,filename); | |
124 | ||
125 | if (strlen(filename) > 0) { | |
126 | if ((pf = fopen(filename,"rb+")) == NULL) { | |
127 | PrintAndLog("Error: Could not open file [%s]",filename); | |
128 | return 1; | |
129 | } | |
130 | tag_mem_supplied = true; | |
131 | fread(c.d.asBytes,48,1,pf); | |
132 | fclose(pf); | |
133 | } else { | |
134 | tag_mem_supplied = false; | |
135 | } | |
136 | ||
137 | // Does the tag comes with memory | |
138 | c.arg[0] = (uint32_t)tag_mem_supplied; | |
139 | ||
140 | SendCommand(&c); | |
141 | return 0; | |
142 | } | |
143 | ||
144 | int CmdLFHitagReader(const char *Cmd) { | |
145 | // UsbCommand c = {CMD_READER_HITAG}; | |
146 | ||
147 | // param_get32ex(Cmd,1,0,16); | |
148 | UsbCommand c = {CMD_READER_HITAG};//, {param_get32ex(Cmd,0,0,10),param_get32ex(Cmd,1,0,16),param_get32ex(Cmd,2,0,16),param_get32ex(Cmd,3,0,16)}}; | |
149 | hitag_data* htd = (hitag_data*)c.d.asBytes; | |
150 | hitag_function htf = param_get32ex(Cmd,0,0,10); | |
151 | ||
152 | switch (htf) { | |
153 | case RHT2F_PASSWORD: { | |
154 | num_to_bytes(param_get32ex(Cmd,1,0,16),4,htd->pwd.password); | |
155 | } break; | |
156 | case RHT2F_AUTHENTICATE: { | |
157 | num_to_bytes(param_get32ex(Cmd,1,0,16),4,htd->auth.NrAr); | |
158 | num_to_bytes(param_get32ex(Cmd,2,0,16),4,htd->auth.NrAr+4); | |
159 | } break; | |
160 | case RHT2F_TEST_AUTH_ATTEMPTS: { | |
161 | // No additional parameters needed | |
162 | } break; | |
163 | default: { | |
164 | PrintAndLog("Error: unkown reader function %d",htf); | |
165 | PrintAndLog("Hitag reader functions",htf); | |
166 | PrintAndLog(" HitagS (0*)",htf); | |
167 | PrintAndLog(" Hitag1 (1*)",htf); | |
168 | PrintAndLog(" Hitag2 (2*)",htf); | |
169 | PrintAndLog(" 21 <password> (password mode)",htf); | |
170 | PrintAndLog(" 22 <nr> <ar> (authentication)",htf); | |
171 | PrintAndLog(" 25 (test recorded authentications)",htf); | |
172 | return 1; | |
173 | } break; | |
174 | } | |
175 | ||
176 | // Copy the hitag2 function into the first argument | |
177 | c.arg[0] = htf; | |
178 | ||
179 | SendCommand(&c); | |
180 | return 0; | |
181 | } | |
182 | ||
183 | static command_t CommandTableHitag[] = | |
184 | { | |
185 | {"help", CmdHelp, 1, "This help"}, | |
186 | {"list", CmdLFHitagList, 1, "List Hitag trace history"}, | |
187 | {"reader", CmdLFHitagReader, 1, "Act like a Hitag Reader"}, | |
188 | {"sim", CmdLFHitagSim, 1, "Simulate Hitag transponder"}, | |
189 | {"snoop", CmdLFHitagSnoop, 1, "Eavesdrop Hitag communication"}, | |
190 | {NULL, NULL, 0, NULL} | |
191 | }; | |
192 | ||
193 | int CmdLFHitag(const char *Cmd) | |
194 | { | |
195 | CmdsParse(CommandTableHitag, Cmd); | |
196 | return 0; | |
197 | } | |
198 | ||
199 | int CmdHelp(const char *Cmd) | |
200 | { | |
201 | CmdsHelp(CommandTableHitag); | |
202 | return 0; | |
203 | } |