]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfhitag.c
fixed windows compile
[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 "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 char filename[256];
29 FILE* pf;
30
31 param_getstr(Cmd,0,filename);
32
33 if (strlen(filename) > 0) {
34 if ((pf = fopen(filename,"w")) == NULL) {
35 PrintAndLog("Error: Could not open file [%s]",filename);
36 return 1;
37 }
38 }
39
40 PrintAndLog("recorded activity:");
41 PrintAndLog(" ETU :rssi: who bytes");
42 PrintAndLog("---------+----+----+-----------");
43
44 int i = 0;
45 int prev = -1;
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 metric = 0;
62 int parityBits = *((uint32_t *)(got+i+4));
63 // 4 bytes of additional information...
64 // maximum of 32 additional parity bit information
65 //
66 // TODO:
67 // at each quarter bit period we can send power level (16 levels)
68 // or each half bit period in 256 levels.
69
70 int len = 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 char metricString[100];
104 if (isResponse) {
105 sprintf(metricString, "%3d", metric);
106 } else {
107 strcpy(metricString, " ");
108 }
109
110 PrintAndLog(" +%7d: %s: %s %s",
111 (prev < 0 ? 0 : (timestamp - prev)),
112 metricString,
113 (isResponse ? "TAG" : " "),
114 line);
115
116
117 if (strlen(filename) > 0) {
118 fprintf(pf," +%7d: %s: %s %s %s",
119 (prev < 0 ? 0 : (timestamp - prev)),
120 metricString,
121 (isResponse ? "TAG" : " "),
122 line,
123 "\n");
124 }
125
126 prev = timestamp;
127 i += (len + 9);
128 }
129
130 if (strlen(filename) > 0) {
131 PrintAndLog("Recorded activity succesfully written to file: %s", filename);
132 fclose(pf);
133 }
134
135 return 0;
136 }
137
138 int CmdLFHitagSnoop(const char *Cmd) {
139 UsbCommand c = {CMD_SNOOP_HITAG};
140 SendCommand(&c);
141 return 0;
142 }
143
144 int CmdLFHitagSim(const char *Cmd) {
145 UsbCommand c = {CMD_SIMULATE_HITAG};
146 char filename[256] = { 0x00 };
147 FILE* pf;
148 bool tag_mem_supplied;
149
150 param_getstr(Cmd,0,filename);
151
152 if (strlen(filename) > 0) {
153 if ((pf = fopen(filename,"rb+")) == NULL) {
154 PrintAndLog("Error: Could not open file [%s]",filename);
155 return 1;
156 }
157 tag_mem_supplied = true;
158 fread(c.d.asBytes,48,1,pf);
159 fclose(pf);
160 } else {
161 tag_mem_supplied = false;
162 }
163
164 // Does the tag comes with memory
165 c.arg[0] = (uint32_t)tag_mem_supplied;
166
167 SendCommand(&c);
168 return 0;
169 }
170
171 int CmdLFHitagReader(const char *Cmd) {
172 // UsbCommand c = {CMD_READER_HITAG};
173
174 // param_get32ex(Cmd,1,0,16);
175 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)}};
176 hitag_data* htd = (hitag_data*)c.d.asBytes;
177 hitag_function htf = param_get32ex(Cmd,0,0,10);
178
179 switch (htf) {
180 case RHT2F_PASSWORD: {
181 num_to_bytes(param_get32ex(Cmd,1,0,16),4,htd->pwd.password);
182 } break;
183 case RHT2F_AUTHENTICATE: {
184 num_to_bytes(param_get32ex(Cmd,1,0,16),4,htd->auth.NrAr);
185 num_to_bytes(param_get32ex(Cmd,2,0,16),4,htd->auth.NrAr+4);
186 } break;
187 case RHT2F_CRYPTO: {
188 num_to_bytes(param_get64ex(Cmd,1,0,16),6,htd->crypto.key);
189 // num_to_bytes(param_get32ex(Cmd,2,0,16),4,htd->auth.NrAr+4);
190 } break;
191 case RHT2F_TEST_AUTH_ATTEMPTS: {
192 // No additional parameters needed
193 } break;
194 default: {
195 PrintAndLog("Error: unkown reader function %d",htf);
196 PrintAndLog("Hitag reader functions",htf);
197 PrintAndLog(" HitagS (0*)",htf);
198 PrintAndLog(" Hitag1 (1*)",htf);
199 PrintAndLog(" Hitag2 (2*)",htf);
200 PrintAndLog(" 21 <password> (password mode)",htf);
201 PrintAndLog(" 22 <nr> <ar> (authentication)",htf);
202 PrintAndLog(" 23 <key> (authentication) key is in format: ISK high + ISK low",htf);
203 PrintAndLog(" 25 (test recorded authentications)",htf);
204 return 1;
205 } break;
206 }
207
208 // Copy the hitag2 function into the first argument
209 c.arg[0] = htf;
210
211 SendCommand(&c);
212 return 0;
213 }
214
215 static command_t CommandTableHitag[] =
216 {
217 {"help", CmdHelp, 1, "This help"},
218 {"list", CmdLFHitagList, 1, "List Hitag trace history"},
219 {"reader", CmdLFHitagReader, 1, "Act like a Hitag Reader"},
220 {"sim", CmdLFHitagSim, 1, "Simulate Hitag transponder"},
221 {"snoop", CmdLFHitagSnoop, 1, "Eavesdrop Hitag communication"},
222 {NULL, NULL, 0, NULL}
223 };
224
225 int CmdLFHitag(const char *Cmd)
226 {
227 CmdsParse(CommandTableHitag, Cmd);
228 return 0;
229 }
230
231 int CmdHelp(const char *Cmd)
232 {
233 CmdsHelp(CommandTableHitag);
234 return 0;
235 }
Impressum, Datenschutz