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