]>
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" | |
902cb3c0 | 15 | #include "proxmark3.h" |
db09cb3a | 16 | #include "ui.h" |
17 | #include "cmdparser.h" | |
f38a1528 | 18 | #include "../include/common.h" |
db09cb3a | 19 | #include "util.h" |
f38a1528 | 20 | #include "../include/hitag2.h" |
902cb3c0 | 21 | #include "sleep.h" |
22 | #include "cmdmain.h" | |
db09cb3a | 23 | |
24 | static int CmdHelp(const char *Cmd); | |
25 | ||
47e18126 | 26 | size_t nbytes(size_t nbits) { |
27 | return (nbits/8)+((nbits%8)>0); | |
28 | } | |
29 | ||
db09cb3a | 30 | int CmdLFHitagList(const char *Cmd) |
31 | { | |
32 | uint8_t got[3000]; | |
33 | GetFromBigBuf(got,sizeof(got),0); | |
902cb3c0 | 34 | WaitForResponse(CMD_ACK,NULL); |
35 | ||
db09cb3a | 36 | PrintAndLog("recorded activity:"); |
47e18126 | 37 | PrintAndLog(" ETU :nbits: who bytes"); |
38 | PrintAndLog("---------+-----+----+-----------"); | |
db09cb3a | 39 | |
40 | int i = 0; | |
41 | int prev = -1; | |
42 | ||
ab4da50d | 43 | char filename[256]; |
44 | FILE* pf = NULL; | |
45 | ||
db09cb3a | 46 | for (;;) { |
47 | if(i >= 1900) { | |
48 | break; | |
49 | } | |
50 | ||
51 | bool isResponse; | |
52 | int timestamp = *((uint32_t *)(got+i)); | |
53 | if (timestamp & 0x80000000) { | |
54 | timestamp &= 0x7fffffff; | |
55 | isResponse = 1; | |
56 | } else { | |
57 | isResponse = 0; | |
58 | } | |
59 | ||
db09cb3a | 60 | int parityBits = *((uint32_t *)(got+i+4)); |
61 | // 4 bytes of additional information... | |
62 | // maximum of 32 additional parity bit information | |
63 | // | |
64 | // TODO: | |
65 | // at each quarter bit period we can send power level (16 levels) | |
66 | // or each half bit period in 256 levels. | |
67 | ||
47e18126 | 68 | int bits = got[i+8]; |
69 | int len = nbytes(got[i+8]); | |
db09cb3a | 70 | |
71 | if (len > 100) { | |
72 | break; | |
73 | } | |
74 | if (i + len >= 1900) { | |
75 | break; | |
76 | } | |
77 | ||
78 | uint8_t *frame = (got+i+9); | |
79 | ||
80 | // Break and stick with current result if buffer was not completely full | |
81 | if (frame[0] == 0x44 && frame[1] == 0x44 && frame[3] == 0x44) { break; } | |
82 | ||
83 | char line[1000] = ""; | |
84 | int j; | |
85 | for (j = 0; j < len; j++) { | |
86 | int oddparity = 0x01; | |
87 | int k; | |
88 | ||
89 | for (k=0;k<8;k++) { | |
90 | oddparity ^= (((frame[j] & 0xFF) >> k) & 0x01); | |
91 | } | |
92 | ||
93 | //if((parityBits >> (len - j - 1)) & 0x01) { | |
94 | if (isResponse && (oddparity != ((parityBits >> (len - j - 1)) & 0x01))) { | |
95 | sprintf(line+(j*4), "%02x! ", frame[j]); | |
96 | } | |
97 | else { | |
98 | sprintf(line+(j*4), "%02x ", frame[j]); | |
99 | } | |
100 | } | |
101 | ||
47e18126 | 102 | PrintAndLog(" +%7d: %3d: %s %s", |
db09cb3a | 103 | (prev < 0 ? 0 : (timestamp - prev)), |
47e18126 | 104 | bits, |
db09cb3a | 105 | (isResponse ? "TAG" : " "), |
106 | line); | |
107 | ||
2d495a81 | 108 | |
902cb3c0 | 109 | if (pf) { |
47e18126 | 110 | fprintf(pf," +%7d: %3d: %s %s\n", |
2d495a81 | 111 | (prev < 0 ? 0 : (timestamp - prev)), |
47e18126 | 112 | bits, |
2d495a81 | 113 | (isResponse ? "TAG" : " "), |
47e18126 | 114 | line); |
2d495a81 | 115 | } |
116 | ||
db09cb3a | 117 | prev = timestamp; |
118 | i += (len + 9); | |
119 | } | |
2d495a81 | 120 | |
902cb3c0 | 121 | if (pf) { |
2d495a81 | 122 | PrintAndLog("Recorded activity succesfully written to file: %s", filename); |
123 | fclose(pf); | |
124 | } | |
125 | ||
126 | return 0; | |
db09cb3a | 127 | } |
128 | ||
129 | int CmdLFHitagSnoop(const char *Cmd) { | |
130 | UsbCommand c = {CMD_SNOOP_HITAG}; | |
131 | SendCommand(&c); | |
132 | return 0; | |
133 | } | |
134 | ||
135 | int CmdLFHitagSim(const char *Cmd) { | |
463ca973 | 136 | |
137 | UsbCommand c = {CMD_SIMULATE_HITAG}; | |
138 | char filename[FILE_PATH_SIZE] = { 0x00 }; | |
db09cb3a | 139 | FILE* pf; |
140 | bool tag_mem_supplied; | |
463ca973 | 141 | int len = 0; |
db09cb3a | 142 | |
463ca973 | 143 | len = strlen(Cmd); |
144 | if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE; | |
145 | memcpy(filename, Cmd, len); | |
146 | ||
db09cb3a | 147 | if (strlen(filename) > 0) { |
148 | if ((pf = fopen(filename,"rb+")) == NULL) { | |
149 | PrintAndLog("Error: Could not open file [%s]",filename); | |
150 | return 1; | |
151 | } | |
152 | tag_mem_supplied = true; | |
759c16b3 | 153 | if (fread(c.d.asBytes,48,1,pf) == 0) { |
463ca973 | 154 | PrintAndLog("Error: File reading error"); |
759c16b3 | 155 | return 1; |
463ca973 | 156 | } |
db09cb3a | 157 | fclose(pf); |
158 | } else { | |
159 | tag_mem_supplied = false; | |
160 | } | |
161 | ||
162 | // Does the tag comes with memory | |
163 | c.arg[0] = (uint32_t)tag_mem_supplied; | |
164 | ||
165 | SendCommand(&c); | |
166 | return 0; | |
167 | } | |
168 | ||
169 | int CmdLFHitagReader(const char *Cmd) { | |
170 | // UsbCommand c = {CMD_READER_HITAG}; | |
171 | ||
172 | // param_get32ex(Cmd,1,0,16); | |
173 | 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)}}; | |
174 | hitag_data* htd = (hitag_data*)c.d.asBytes; | |
175 | hitag_function htf = param_get32ex(Cmd,0,0,10); | |
176 | ||
177 | switch (htf) { | |
178 | case RHT2F_PASSWORD: { | |
179 | num_to_bytes(param_get32ex(Cmd,1,0,16),4,htd->pwd.password); | |
180 | } break; | |
181 | case RHT2F_AUTHENTICATE: { | |
182 | num_to_bytes(param_get32ex(Cmd,1,0,16),4,htd->auth.NrAr); | |
183 | num_to_bytes(param_get32ex(Cmd,2,0,16),4,htd->auth.NrAr+4); | |
184 | } break; | |
bde10a50 | 185 | case RHT2F_CRYPTO: { |
186 | num_to_bytes(param_get64ex(Cmd,1,0,16),6,htd->crypto.key); | |
187 | // num_to_bytes(param_get32ex(Cmd,2,0,16),4,htd->auth.NrAr+4); | |
188 | } break; | |
db09cb3a | 189 | case RHT2F_TEST_AUTH_ATTEMPTS: { |
190 | // No additional parameters needed | |
191 | } break; | |
192 | default: { | |
193 | PrintAndLog("Error: unkown reader function %d",htf); | |
ab4da50d | 194 | PrintAndLog("Hitag reader functions"); |
195 | PrintAndLog(" HitagS (0*)"); | |
196 | PrintAndLog(" Hitag1 (1*)"); | |
197 | PrintAndLog(" Hitag2 (2*)"); | |
198 | PrintAndLog(" 21 <password> (password mode)"); | |
199 | PrintAndLog(" 22 <nr> <ar> (authentication)"); | |
200 | PrintAndLog(" 23 <key> (authentication) key is in format: ISK high + ISK low"); | |
201 | PrintAndLog(" 25 (test recorded authentications)"); | |
db09cb3a | 202 | return 1; |
203 | } break; | |
204 | } | |
205 | ||
206 | // Copy the hitag2 function into the first argument | |
207 | c.arg[0] = htf; | |
208 | ||
ab4da50d | 209 | // Send the command to the proxmark |
db09cb3a | 210 | SendCommand(&c); |
ab4da50d | 211 | |
212 | UsbCommand resp; | |
213 | WaitForResponse(CMD_ACK,&resp); | |
214 | ||
215 | // Check the return status, stored in the first argument | |
216 | if (resp.arg[0] == false) return 1; | |
217 | ||
218 | uint32_t id = bytes_to_num(resp.d.asBytes,4); | |
219 | char filename[256]; | |
220 | FILE* pf = NULL; | |
221 | ||
222 | sprintf(filename,"%08x_%04x.ht2",id,(rand() & 0xffff)); | |
223 | if ((pf = fopen(filename,"wb")) == NULL) { | |
224 | PrintAndLog("Error: Could not open file [%s]",filename); | |
225 | return 1; | |
226 | } | |
227 | ||
228 | // Write the 48 tag memory bytes to file and finalize | |
229 | fwrite(resp.d.asBytes,1,48,pf); | |
230 | fclose(pf); | |
231 | ||
232 | PrintAndLog("Succesfully saved tag memory to [%s]",filename); | |
233 | ||
db09cb3a | 234 | return 0; |
235 | } | |
236 | ||
081151ea | 237 | static command_t CommandTable[] = |
db09cb3a | 238 | { |
239 | {"help", CmdHelp, 1, "This help"}, | |
240 | {"list", CmdLFHitagList, 1, "List Hitag trace history"}, | |
241 | {"reader", CmdLFHitagReader, 1, "Act like a Hitag Reader"}, | |
242 | {"sim", CmdLFHitagSim, 1, "Simulate Hitag transponder"}, | |
243 | {"snoop", CmdLFHitagSnoop, 1, "Eavesdrop Hitag communication"}, | |
244 | {NULL, NULL, 0, NULL} | |
245 | }; | |
246 | ||
247 | int CmdLFHitag(const char *Cmd) | |
248 | { | |
081151ea | 249 | CmdsParse(CommandTable, Cmd); |
db09cb3a | 250 | return 0; |
251 | } | |
252 | ||
253 | int CmdHelp(const char *Cmd) | |
254 | { | |
081151ea | 255 | CmdsHelp(CommandTable); |
db09cb3a | 256 | return 0; |
257 | } |