]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhflegic.c
adjust em4x05/em4x69 command timings to...
[proxmark3-svn] / client / cmdhflegic.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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// High frequency Legic commands
9//-----------------------------------------------------------------------------
10
8e220a91 11#include <stdio.h>
12#include <string.h>
43534cba 13#include <inttypes.h>
902cb3c0 14#include "proxmark3.h"
41dab153 15#include "data.h"
16#include "ui.h"
7fe9b0b7 17#include "cmdparser.h"
18#include "cmdhflegic.h"
669c1b80 19#include "cmdmain.h"
31d1caa5 20#include "util.h"
7fe9b0b7 21static int CmdHelp(const char *Cmd);
22
7fe9b0b7 23static command_t CommandTable[] =
24{
25 {"help", CmdHelp, 1, "This help"},
669c1b80 26 {"decode", CmdLegicDecode, 0, "Display deobfuscated and decoded LEGIC RF tag data (use after hf legic reader)"},
41dab153 27 {"reader", CmdLegicRFRead, 0, "[offset [length]] -- read bytes from a LEGIC card"},
3612a8a8 28 {"save", CmdLegicSave, 0, "<filename> [<length>] -- Store samples"},
29 {"load", CmdLegicLoad, 0, "<filename> -- Restore samples"},
30 {"sim", CmdLegicRfSim, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"},
31 {"write", CmdLegicRfWrite,0, "<offset> <length> -- Write sample buffer (user after load or read)"},
32 {"fill", CmdLegicRfFill, 0, "<offset> <length> <value> -- Fill/Write tag with constant value"},
7fe9b0b7 33 {NULL, NULL, 0, NULL}
34};
35
36int CmdHFLegic(const char *Cmd)
37{
38 CmdsParse(CommandTable, Cmd);
39 return 0;
40}
41
42int CmdHelp(const char *Cmd)
43{
44 CmdsHelp(CommandTable);
45 return 0;
46}
669c1b80 47
48/*
49 * Output BigBuf and deobfuscate LEGIC RF tag data.
50 * This is based on information given in the talk held
51 * by Henryk Ploetz and Karsten Nohl at 26c3
669c1b80 52 */
53int CmdLegicDecode(const char *Cmd)
54{
4961e292 55 int i, j, k, n;
669c1b80 56 int segment_len = 0;
57 int segment_flag = 0;
58 int stamp_len = 0;
59 int crc = 0;
60 int wrp = 0;
61 int wrc = 0;
56837794 62 uint8_t data_buf[1052]; // receiver buffer
669c1b80 63 char out_string[3076]; // just use big buffer - bad practice
64 char token_type[4];
669c1b80 65
66 // copy data from proxmark into buffer
4961e292 67 GetFromBigBuf(data_buf,sizeof(data_buf),0);
68 WaitForResponse(CMD_ACK,NULL);
669c1b80 69
70 // Output CDF System area (9 bytes) plus remaining header area (12 bytes)
71
72 PrintAndLog("\nCDF: System Area");
73
74 PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x",
75 data_buf[0],
76 data_buf[1],
77 data_buf[2],
78 data_buf[3],
79 data_buf[4]
80 );
81
82 crc = data_buf[4];
83
84 switch (data_buf[5]&0x7f) {
85 case 0x00 ... 0x2f:
86 strncpy(token_type, "IAM",sizeof(token_type));
87 break;
88 case 0x30 ... 0x6f:
89 strcpy(token_type, "SAM");
90 break;
91 case 0x70 ... 0x7f:
92 strcpy(token_type, "GAM");
93 break;
94 default:
95 strcpy(token_type, "???");
96 break;
97 }
98
99 stamp_len = 0xfc - data_buf[6];
100
101 PrintAndLog("DCF: %02x %02x, Token_Type=%s (OLE=%01u), Stamp_len=%02u",
102 data_buf[5],
103 data_buf[6],
104 token_type,
105 (data_buf[5]&0x80)>>7,
106 stamp_len
107 );
108
109 PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, raw=%02x, SSC=%02x",
110 data_buf[7]&0x0f,
111 (data_buf[7]&0x70)>>4,
112 (data_buf[7]&0x80)>>7,
113 data_buf[7],
114 data_buf[8]
115 );
116
117 PrintAndLog("Remaining Header Area");
118
119 PrintAndLog("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
120 data_buf[9],
121 data_buf[10],
122 data_buf[11],
123 data_buf[12],
124 data_buf[13],
125 data_buf[14],
126 data_buf[15],
127 data_buf[16],
128 data_buf[17],
129 data_buf[18],
130 data_buf[19],
131 data_buf[20],
132 data_buf[21]
133 );
134
135 PrintAndLog("\nADF: User Area");
136
41dab153 137 i = 22;
669c1b80 138 for (n=0; n<64; n++) {
139 segment_len = ((data_buf[i+1]^crc)&0x0f) * 256 + (data_buf[i]^crc);
140 segment_flag = ((data_buf[i+1]^crc)&0xf0)>>4;
141
142 wrp = (data_buf[i+2]^crc);
143 wrc = ((data_buf[i+3]^crc)&0x70)>>4;
144
145 PrintAndLog("Segment %02u: raw header=%02x %02x %02x %02x, flag=%01x (valid=%01u, last=%01u), len=%04u, WRP=%02u, WRC=%02u, RD=%01u, CRC=%02x",
146 n,
147 data_buf[i]^crc,
148 data_buf[i+1]^crc,
149 data_buf[i+2]^crc,
150 data_buf[i+3]^crc,
151 segment_flag,
152 (segment_flag&0x4)>>2,
153 (segment_flag&0x8)>>3,
154 segment_len,
155 wrp,
156 wrc,
157 ((data_buf[i+3]^crc)&0x80)>>7,
158 (data_buf[i+4]^crc)
159 );
160
161 i+=5;
162
163 if (wrc>0) {
164 PrintAndLog("WRC protected area:");
31332265 165 for (k=0, j=0; k < wrc && j<(sizeof(out_string)-3); k++, i++, j += 3) {
669c1b80 166 sprintf(&out_string[j], "%02x", (data_buf[i]^crc));
167 out_string[j+2] = ' ';
168 };
169
170 out_string[j] = '\0';
171
172 PrintAndLog("%s", out_string);
173 }
174
175 if (wrp>wrc) {
176 PrintAndLog("Remaining write protected area:");
177
31332265 178 for (k=0, j=0; k < (wrp-wrc) && j<(sizeof(out_string)-3); k++, i++, j += 3) {
669c1b80 179 sprintf(&out_string[j], "%02x", (data_buf[i]^crc));
180 out_string[j+2] = ' ';
181 };
182
183 out_string[j] = '\0';
184
185 PrintAndLog("%s", out_string);
186 if((wrp-wrc) == 8) {
187 sprintf(out_string,"Card ID: %2X%02X%02X",data_buf[i-4]^crc,data_buf[i-3]^crc,data_buf[i-2]^crc);
188 PrintAndLog("%s", out_string);
189 }
190 }
191
192 PrintAndLog("Remaining segment payload:");
31332265 193 for (k=0, j=0; k < (segment_len - wrp - 5) && j<(sizeof(out_string)-3); k++, i++, j += 3) {
669c1b80 194 sprintf(&out_string[j], "%02x", (data_buf[i]^crc));
195 out_string[j+2] = ' ';
196 };
197
198 out_string[j] = '\0';
199
200 PrintAndLog("%s", out_string);
201
202 // end with last segment
203 if (segment_flag & 0x8)
204 return 0;
205 };
206 return 0;
207}
41dab153 208
209int CmdLegicRFRead(const char *Cmd)
210{
211 int byte_count=0,offset=0;
212 sscanf(Cmd, "%i %i", &offset, &byte_count);
a2b1414f 213 if(byte_count == 0) byte_count = -1;
214 if(byte_count + offset > 1024) byte_count = 1024 - offset;
41dab153 215 UsbCommand c={CMD_READER_LEGIC_RF, {offset, byte_count, 0}};
216 SendCommand(&c);
217 return 0;
218}
3612a8a8 219
220int CmdLegicLoad(const char *Cmd)
221{
b915fda3 222 char filename[FILE_PATH_SIZE] = {0x00};
223 int len = 0;
224
225 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {
226 PrintAndLog("It loads datasamples from the file `filename`");
227 PrintAndLog("Usage: hf legic load <file name>");
228 PrintAndLog(" sample: hf legic load filename");
229 return 0;
230 }
231
232 len = strlen(Cmd);
233 if (len > FILE_PATH_SIZE) {
234 PrintAndLog("Filepath too long (was %s bytes), max allowed is %s ", len, FILE_PATH_SIZE);
235 return 0;
236 }
237 memcpy(filename, Cmd, len);
238
239 FILE *f = fopen(filename, "r");
3612a8a8 240 if(!f) {
241 PrintAndLog("couldn't open '%s'", Cmd);
242 return -1;
243 }
244 char line[80]; int offset = 0; unsigned int data[8];
245 while(fgets(line, sizeof(line), f)) {
246 int res = sscanf(line, "%x %x %x %x %x %x %x %x",
247 &data[0], &data[1], &data[2], &data[3],
248 &data[4], &data[5], &data[6], &data[7]);
249 if(res != 8) {
250 PrintAndLog("Error: could not read samples");
251 fclose(f);
252 return -1;
253 }
254 UsbCommand c={CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
255 int j; for(j = 0; j < 8; j++) {
256 c.d.asBytes[j] = data[j];
257 }
258 SendCommand(&c);
902cb3c0 259 WaitForResponse(CMD_ACK, NULL);
3612a8a8 260 offset += 8;
261 }
262 fclose(f);
263 PrintAndLog("loaded %u samples", offset);
264 return 0;
265}
266
267int CmdLegicSave(const char *Cmd)
268{
3612a8a8 269 int requested = 1024;
270 int offset = 0;
4961e292 271 int delivered = 0;
b915fda3 272 char filename[FILE_PATH_SIZE];
4961e292 273 uint8_t got[1024];
274
3612a8a8 275 sscanf(Cmd, " %s %i %i", filename, &requested, &offset);
3612a8a8 276
4961e292 277 /* If no length given save entire legic read buffer */
278 /* round up to nearest 8 bytes so the saved data can be used with legicload */
3612a8a8 279 if (requested == 0) {
4961e292 280 requested = 1024;
3612a8a8 281 }
4961e292 282 if (requested % 8 != 0) {
283 int remainder = requested % 8;
284 requested = requested + 8 - remainder;
285 }
4961e292 286 if (offset + requested > sizeof(got)) {
287 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024");
288 return 0;
289 }
290
3612a8a8 291 FILE *f = fopen(filename, "w");
292 if(!f) {
293 PrintAndLog("couldn't open '%s'", Cmd+1);
294 return -1;
295 }
296
4961e292 297 GetFromBigBuf(got,requested,offset);
298 WaitForResponse(CMD_ACK,NULL);
299
300 for (int j = 0; j < requested; j += 8) {
301 fprintf(f, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
302 got[j+0],
303 got[j+1],
304 got[j+2],
305 got[j+3],
306 got[j+4],
307 got[j+5],
308 got[j+6],
309 got[j+7]
310 );
311 delivered += 8;
3612a8a8 312 if (delivered >= requested)
313 break;
314 }
315
316 fclose(f);
317 PrintAndLog("saved %u samples", delivered);
318 return 0;
319}
320
321int CmdLegicRfSim(const char *Cmd)
322{
323 UsbCommand c={CMD_SIMULATE_TAG_LEGIC_RF};
324 c.arg[0] = 6;
325 c.arg[1] = 3;
326 c.arg[2] = 0;
43534cba 327 sscanf(Cmd, " %" SCNi64 " %" SCNi64 " %" SCNi64, &c.arg[0], &c.arg[1], &c.arg[2]);
3612a8a8 328 SendCommand(&c);
329 return 0;
330}
331
332int CmdLegicRfWrite(const char *Cmd)
333{
334 UsbCommand c={CMD_WRITER_LEGIC_RF};
43534cba 335 int res = sscanf(Cmd, " 0x%" SCNx64 " 0x%" SCNx64, &c.arg[0], &c.arg[1]);
3612a8a8 336 if(res != 2) {
337 PrintAndLog("Please specify the offset and length as two hex strings");
338 return -1;
339 }
340 SendCommand(&c);
341 return 0;
342}
343
344int CmdLegicRfFill(const char *Cmd)
345{
346 UsbCommand cmd ={CMD_WRITER_LEGIC_RF};
43534cba 347 int res = sscanf(Cmd, " 0x%" SCNx64 " 0x%" SCNx64 " 0x%" SCNx64, &cmd.arg[0], &cmd.arg[1], &cmd.arg[2]);
3612a8a8 348 if(res != 3) {
349 PrintAndLog("Please specify the offset, length and value as two hex strings");
350 return -1;
351 }
352
353 int i;
354 UsbCommand c={CMD_DOWNLOADED_SIM_SAMPLES_125K, {0, 0, 0}};
355 for(i = 0; i < 48; i++) {
356 c.d.asBytes[i] = cmd.arg[2];
357 }
358 for(i = 0; i < 22; i++) {
359 c.arg[0] = i*48;
360 SendCommand(&c);
902cb3c0 361 WaitForResponse(CMD_ACK,NULL);
3612a8a8 362 }
363 SendCommand(&cmd);
364 return 0;
365 }
366
Impressum, Datenschutz