]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhflegic.c
Fix description in help dump
[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>
28fdb04f 13//#include "proxusb.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"
7fe9b0b7 20
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;
4961e292 62 uint8_t data_buf[1024]; // 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{
222 FILE *f = fopen(Cmd, "r");
223 if(!f) {
224 PrintAndLog("couldn't open '%s'", Cmd);
225 return -1;
226 }
227 char line[80]; int offset = 0; unsigned int data[8];
228 while(fgets(line, sizeof(line), f)) {
229 int res = sscanf(line, "%x %x %x %x %x %x %x %x",
230 &data[0], &data[1], &data[2], &data[3],
231 &data[4], &data[5], &data[6], &data[7]);
232 if(res != 8) {
233 PrintAndLog("Error: could not read samples");
234 fclose(f);
235 return -1;
236 }
237 UsbCommand c={CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
238 int j; for(j = 0; j < 8; j++) {
239 c.d.asBytes[j] = data[j];
240 }
241 SendCommand(&c);
902cb3c0 242 WaitForResponse(CMD_ACK, NULL);
3612a8a8 243 offset += 8;
244 }
245 fclose(f);
246 PrintAndLog("loaded %u samples", offset);
247 return 0;
248}
249
250int CmdLegicSave(const char *Cmd)
251{
3612a8a8 252 int requested = 1024;
253 int offset = 0;
4961e292 254 int delivered = 0;
3612a8a8 255 char filename[1024];
4961e292 256 uint8_t got[1024];
257
3612a8a8 258 sscanf(Cmd, " %s %i %i", filename, &requested, &offset);
3612a8a8 259
4961e292 260 /* If no length given save entire legic read buffer */
261 /* round up to nearest 8 bytes so the saved data can be used with legicload */
3612a8a8 262 if (requested == 0) {
4961e292 263 requested = 1024;
3612a8a8 264 }
4961e292 265 if (requested % 8 != 0) {
266 int remainder = requested % 8;
267 requested = requested + 8 - remainder;
268 }
269
270 if (offset + requested > sizeof(got)) {
271 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024");
272 return 0;
273 }
274
3612a8a8 275 FILE *f = fopen(filename, "w");
276 if(!f) {
277 PrintAndLog("couldn't open '%s'", Cmd+1);
278 return -1;
279 }
280
4961e292 281 GetFromBigBuf(got,requested,offset);
282 WaitForResponse(CMD_ACK,NULL);
283
284 for (int j = 0; j < requested; j += 8) {
285 fprintf(f, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
286 got[j+0],
287 got[j+1],
288 got[j+2],
289 got[j+3],
290 got[j+4],
291 got[j+5],
292 got[j+6],
293 got[j+7]
294 );
295 delivered += 8;
3612a8a8 296 if (delivered >= requested)
297 break;
298 }
299
300 fclose(f);
301 PrintAndLog("saved %u samples", delivered);
302 return 0;
303}
304
305int CmdLegicRfSim(const char *Cmd)
306{
307 UsbCommand c={CMD_SIMULATE_TAG_LEGIC_RF};
308 c.arg[0] = 6;
309 c.arg[1] = 3;
310 c.arg[2] = 0;
1a07fd51 311 sscanf(Cmd, " %"lli" %"lli" %"lli, &c.arg[0], &c.arg[1], &c.arg[2]);
3612a8a8 312 SendCommand(&c);
313 return 0;
314}
315
316int CmdLegicRfWrite(const char *Cmd)
317{
318 UsbCommand c={CMD_WRITER_LEGIC_RF};
125a98a1 319 int res = sscanf(Cmd, " 0x%"llx" 0x%"llx, &c.arg[0], &c.arg[1]);
3612a8a8 320 if(res != 2) {
321 PrintAndLog("Please specify the offset and length as two hex strings");
322 return -1;
323 }
324 SendCommand(&c);
325 return 0;
326}
327
328int CmdLegicRfFill(const char *Cmd)
329{
330 UsbCommand cmd ={CMD_WRITER_LEGIC_RF};
1a07fd51 331 int res = sscanf(Cmd, " 0x%"llx" 0x%"llx" 0x%"llx, &cmd.arg[0], &cmd.arg[1], &cmd.arg[2]);
3612a8a8 332 if(res != 3) {
333 PrintAndLog("Please specify the offset, length and value as two hex strings");
334 return -1;
335 }
336
337 int i;
338 UsbCommand c={CMD_DOWNLOADED_SIM_SAMPLES_125K, {0, 0, 0}};
339 for(i = 0; i < 48; i++) {
340 c.d.asBytes[i] = cmd.arg[2];
341 }
342 for(i = 0; i < 22; i++) {
343 c.arg[0] = i*48;
344 SendCommand(&c);
902cb3c0 345 WaitForResponse(CMD_ACK,NULL);
3612a8a8 346 }
347 SendCommand(&cmd);
348 return 0;
349 }
350
Impressum, Datenschutz