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