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