]>
Commit | Line | Data |
---|---|---|
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 | #include "cmdhflegic.h" | |
11 | ||
12 | static int CmdHelp(const char *Cmd); | |
13 | ||
14 | #define SESSION_IV 0x55 | |
15 | #define MAX_LENGTH 1024 | |
16 | ||
17 | int usage_legic_calccrc8(void){ | |
18 | PrintAndLog("Calculates the legic crc8/crc16 on the input hexbytes."); | |
19 | PrintAndLog("There must be an even number of hexsymbols as input."); | |
20 | PrintAndLog("Usage: hf legic crc8 [h] b <hexbytes> u <uidcrc> c <crc type>"); | |
21 | PrintAndLog("Options:"); | |
22 | PrintAndLog(" h : this help"); | |
23 | PrintAndLog(" b <hexbytes> : hex bytes"); | |
24 | PrintAndLog(" u <uidcrc> : MCC hexbyte"); | |
25 | PrintAndLog(" c <crc type> : 8|16 bit crc size"); | |
26 | PrintAndLog(""); | |
27 | PrintAndLog("Samples:"); | |
28 | PrintAndLog(" hf legic crc8 b deadbeef1122"); | |
29 | PrintAndLog(" hf legic crc8 b deadbeef1122 u 9A c 16"); | |
30 | return 0; | |
31 | } | |
32 | int usage_legic_load(void){ | |
33 | PrintAndLog("It loads datasamples from the file `filename` to device memory"); | |
34 | PrintAndLog("Usage: hf legic load [h] <file name>"); | |
35 | PrintAndLog("Options:"); | |
36 | PrintAndLog(" h : this help"); | |
37 | PrintAndLog(" <filename> : Name of file to load"); | |
38 | PrintAndLog(""); | |
39 | PrintAndLog("Samples:"); | |
40 | PrintAndLog(" hf legic load filename"); | |
41 | return 0; | |
42 | } | |
43 | int usage_legic_read(void){ | |
44 | PrintAndLog("Read data from a legic tag."); | |
45 | PrintAndLog("Usage: hf legic read [h] <offset> <length> <IV>"); | |
46 | PrintAndLog("Options:"); | |
47 | PrintAndLog(" h : this help"); | |
48 | PrintAndLog(" <offset> : offset in data array to start download from"); | |
49 | PrintAndLog(" <length> : number of bytes to download"); | |
50 | PrintAndLog(" <IV> : (optional) Initialization vector to use"); | |
51 | PrintAndLog(""); | |
52 | PrintAndLog("Samples:"); | |
53 | PrintAndLog(" hf legic read"); | |
54 | PrintAndLog(" hf legic read 10 4"); | |
55 | return 0; | |
56 | } | |
57 | int usage_legic_sim(void){ | |
58 | PrintAndLog("Missing help text."); | |
59 | return 0; | |
60 | } | |
61 | int usage_legic_write(void){ | |
62 | PrintAndLog(" Write sample buffer to a legic tag. (use after load or read)"); | |
63 | PrintAndLog("Usage: hf legic write [h] <offset> <length> <IV>"); | |
64 | PrintAndLog("Options:"); | |
65 | PrintAndLog(" h : this help"); | |
66 | PrintAndLog(" <offset> : offset in data array to start writing from"); | |
67 | PrintAndLog(" <length> : number of bytes to write"); | |
68 | PrintAndLog(" <IV> : (optional) Initialization vector to use"); | |
69 | PrintAndLog(""); | |
70 | PrintAndLog("Samples:"); | |
71 | PrintAndLog(" hf legic write"); | |
72 | PrintAndLog(" hf legic write 10 4"); | |
73 | return 0; | |
74 | } | |
75 | int usage_legic_rawwrite(void){ | |
76 | PrintAndLog("Write raw data direct to a specific address on legic tag."); | |
77 | PrintAndLog("Usage: hf legic writeraw [h] <address> <value> <IV>"); | |
78 | PrintAndLog("Options:"); | |
79 | PrintAndLog(" h : this help"); | |
80 | PrintAndLog(" <address> : address to write to"); | |
81 | PrintAndLog(" <value> : value to write"); | |
82 | PrintAndLog(" <IV> : (optional) Initialization vector to use"); | |
83 | PrintAndLog(""); | |
84 | PrintAndLog("Samples:"); | |
85 | PrintAndLog(" hf legic writeraw"); | |
86 | PrintAndLog(" hf legic writeraw 10 4"); | |
87 | return 0; | |
88 | } | |
89 | int usage_legic_fill(void){ | |
90 | PrintAndLog("Missing help text."); | |
91 | return 0; | |
92 | } | |
93 | ||
94 | /* | |
95 | * Output BigBuf and deobfuscate LEGIC RF tag data. | |
96 | * This is based on information given in the talk held | |
97 | * by Henryk Ploetz and Karsten Nohl at 26c3 | |
98 | */ | |
99 | int CmdLegicDecode(const char *Cmd) { | |
100 | ||
101 | int i = 0, k = 0, segmentNum = 0, segment_len = 0, segment_flag = 0; | |
102 | int crc = 0, wrp = 0, wrc = 0; | |
103 | uint8_t stamp_len = 0; | |
104 | uint8_t data_buf[1052]; // receiver buffer | |
105 | char token_type[5] = {0,0,0,0,0}; | |
106 | int dcf = 0; | |
107 | int bIsSegmented = 0; | |
108 | ||
109 | // download EML memory, where the "legic read" command puts the data. | |
110 | // copy data from proxmark into buffer | |
111 | GetFromBigBuf(data_buf,sizeof(data_buf),0); | |
112 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000)){ | |
113 | PrintAndLog("Command execute timeout"); | |
114 | return 1; | |
115 | } | |
116 | ||
117 | // Output CDF System area (9 bytes) plus remaining header area (12 bytes) | |
118 | crc = data_buf[4]; | |
119 | uint32_t calc_crc = CRC8Legic(data_buf, 4); | |
120 | ||
121 | PrintAndLog("\nCDF: System Area"); | |
122 | PrintAndLog("------------------------------------------------------"); | |
123 | PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x %s", | |
124 | data_buf[0], | |
125 | data_buf[1], | |
126 | data_buf[2], | |
127 | data_buf[3], | |
128 | data_buf[4], | |
129 | (calc_crc == crc) ? "OK":"Fail" | |
130 | ); | |
131 | ||
132 | ||
133 | token_type[0] = 0; | |
134 | dcf = ((int)data_buf[6] << 8) | (int)data_buf[5]; | |
135 | ||
136 | // New unwritten media? | |
137 | if(dcf == 0xFFFF) { | |
138 | ||
139 | PrintAndLog("DCF: %d (%02x %02x), Token Type=NM (New Media)", | |
140 | dcf, | |
141 | data_buf[5], | |
142 | data_buf[6] | |
143 | ); | |
144 | ||
145 | } else if(dcf > 60000) { // Master token? | |
146 | ||
147 | int fl = 0; | |
148 | ||
149 | if(data_buf[6] == 0xec) { | |
150 | strncpy(token_type, "XAM", sizeof(token_type)); | |
151 | fl = 1; | |
152 | stamp_len = 0x0c - (data_buf[5] >> 4); | |
153 | } else { | |
154 | switch (data_buf[5] & 0x7f) { | |
155 | case 0x00 ... 0x2f: | |
156 | strncpy(token_type, "IAM", sizeof(token_type)); | |
157 | fl = (0x2f - (data_buf[5] & 0x7f)) + 1; | |
158 | break; | |
159 | case 0x30 ... 0x6f: | |
160 | strncpy(token_type, "SAM", sizeof(token_type)); | |
161 | fl = (0x6f - (data_buf[5] & 0x7f)) + 1; | |
162 | break; | |
163 | case 0x70 ... 0x7f: | |
164 | strncpy(token_type, "GAM", sizeof(token_type)); | |
165 | fl = (0x7f - (data_buf[5] & 0x7f)) + 1; | |
166 | break; | |
167 | } | |
168 | ||
169 | stamp_len = 0xfc - data_buf[6]; | |
170 | } | |
171 | ||
172 | PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u), OL=%02u, FL=%02u", | |
173 | dcf, | |
174 | data_buf[5], | |
175 | data_buf[6], | |
176 | token_type, | |
177 | (data_buf[5] & 0x80 )>> 7, | |
178 | stamp_len, | |
179 | fl | |
180 | ); | |
181 | ||
182 | } else { // Is IM(-S) type of card... | |
183 | ||
184 | if(data_buf[7] == 0x9F && data_buf[8] == 0xFF) { | |
185 | bIsSegmented = 1; | |
186 | strncpy(token_type, "IM-S", sizeof(token_type)); | |
187 | } else { | |
188 | strncpy(token_type, "IM", sizeof(token_type)); | |
189 | } | |
190 | ||
191 | PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u)", | |
192 | dcf, | |
193 | data_buf[5], | |
194 | data_buf[6], | |
195 | token_type, | |
196 | (data_buf[5]&0x80) >> 7 | |
197 | ); | |
198 | } | |
199 | ||
200 | // Makes no sence to show this on blank media... | |
201 | if(dcf != 0xFFFF) { | |
202 | ||
203 | if(bIsSegmented) { | |
204 | PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, SSC=%02x", | |
205 | data_buf[7] & 0x0f, | |
206 | (data_buf[7] & 0x70) >> 4, | |
207 | (data_buf[7] & 0x80) >> 7, | |
208 | data_buf[8] | |
209 | ); | |
210 | } | |
211 | ||
212 | // Header area is only available on IM-S cards, on master tokens this data is the master token data itself | |
213 | if(bIsSegmented || dcf > 60000) { | |
214 | if(dcf > 60000) { | |
215 | PrintAndLog("Master token data"); | |
216 | PrintAndLog("%s", sprint_hex(data_buf+8, 14)); | |
217 | } else { | |
218 | PrintAndLog("Remaining Header Area"); | |
219 | PrintAndLog("%s", sprint_hex(data_buf+9, 13)); | |
220 | } | |
221 | } | |
222 | } | |
223 | ||
224 | uint8_t segCrcBytes[8] = {0,0,0,0,0,0,0,0}; | |
225 | uint32_t segCalcCRC = 0; | |
226 | uint32_t segCRC = 0; | |
227 | ||
228 | // Data card? | |
229 | if(dcf <= 60000) { | |
230 | ||
231 | PrintAndLog("\nADF: User Area"); | |
232 | PrintAndLog("------------------------------------------------------"); | |
233 | ||
234 | if(bIsSegmented) { | |
235 | ||
236 | // Data start point on segmented cards | |
237 | i = 22; | |
238 | ||
239 | // decode segments | |
240 | for (segmentNum=1; segmentNum < 128; segmentNum++ ) | |
241 | { | |
242 | segment_len = ((data_buf[i+1] ^ crc) & 0x0f) * 256 + (data_buf[i] ^ crc); | |
243 | segment_flag = ((data_buf[i+1] ^ crc) & 0xf0) >> 4; | |
244 | wrp = (data_buf[i+2] ^ crc); | |
245 | wrc = ((data_buf[i+3] ^ crc) & 0x70) >> 4; | |
246 | ||
247 | bool hasWRC = (wrc > 0); | |
248 | bool hasWRP = (wrp > wrc); | |
249 | int wrp_len = (wrp - wrc); | |
250 | int remain_seg_payload_len = (segment_len - wrp - 5); | |
251 | ||
252 | // validate segment-crc | |
253 | segCrcBytes[0]=data_buf[0]; //uid0 | |
254 | segCrcBytes[1]=data_buf[1]; //uid1 | |
255 | segCrcBytes[2]=data_buf[2]; //uid2 | |
256 | segCrcBytes[3]=data_buf[3]; //uid3 | |
257 | segCrcBytes[4]=(data_buf[i] ^ crc); //hdr0 | |
258 | segCrcBytes[5]=(data_buf[i+1] ^ crc); //hdr1 | |
259 | segCrcBytes[6]=(data_buf[i+2] ^ crc); //hdr2 | |
260 | segCrcBytes[7]=(data_buf[i+3] ^ crc); //hdr3 | |
261 | ||
262 | segCalcCRC = CRC8Legic(segCrcBytes, 8); | |
263 | segCRC = data_buf[i+4] ^ crc; | |
264 | ||
265 | PrintAndLog("Segment %02u \nraw header | 0x%02X 0x%02X 0x%02X 0x%02X \nSegment len: %u, Flag: 0x%X (valid:%01u, last:%01u), WRP: %02u, WRC: %02u, RD: %01u, CRC: 0x%02X (%s)", | |
266 | segmentNum, | |
267 | data_buf[i] ^ crc, | |
268 | data_buf[i+1] ^ crc, | |
269 | data_buf[i+2] ^ crc, | |
270 | data_buf[i+3] ^ crc, | |
271 | segment_len, | |
272 | segment_flag, | |
273 | (segment_flag & 0x4) >> 2, | |
274 | (segment_flag & 0x8) >> 3, | |
275 | wrp, | |
276 | wrc, | |
277 | ((data_buf[i+3]^crc) & 0x80) >> 7, | |
278 | segCRC, | |
279 | ( segCRC == segCalcCRC ) ? "OK" : "fail" | |
280 | ); | |
281 | ||
282 | i += 5; | |
283 | ||
284 | if ( hasWRC ) { | |
285 | PrintAndLog("WRC protected area: (I %d | K %d| WRC %d)", i, k, wrc); | |
286 | PrintAndLog("\nrow | data"); | |
287 | PrintAndLog("-----+------------------------------------------------"); | |
288 | ||
289 | for ( k=i; k < (i + wrc); ++k) | |
290 | data_buf[k] ^= crc; | |
291 | ||
292 | print_hex_break( data_buf+i, wrc, 16); | |
293 | ||
294 | i += wrc; | |
295 | } | |
296 | ||
297 | if ( hasWRP ) { | |
298 | PrintAndLog("Remaining write protected area: (I %d | K %d | WRC %d | WRP %d WRP_LEN %d)",i, k, wrc, wrp, wrp_len); | |
299 | PrintAndLog("\nrow | data"); | |
300 | PrintAndLog("-----+------------------------------------------------"); | |
301 | ||
302 | for (k=i; k < (i+wrp_len); ++k) | |
303 | data_buf[k] ^= crc; | |
304 | ||
305 | print_hex_break( data_buf+i, wrp_len, 16); | |
306 | ||
307 | i += wrp_len; | |
308 | ||
309 | // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...) | |
310 | if( wrp_len == 8 ) | |
311 | PrintAndLog("Card ID: %2X%02X%02X", data_buf[i-4]^crc, data_buf[i-3]^crc, data_buf[i-2]^crc); | |
312 | } | |
313 | ||
314 | PrintAndLog("Remaining segment payload: (I %d | K %d | Remain LEN %d)", i, k, remain_seg_payload_len); | |
315 | PrintAndLog("\nrow | data"); | |
316 | PrintAndLog("-----+------------------------------------------------"); | |
317 | ||
318 | for ( k=i; k < (i+remain_seg_payload_len); ++k) | |
319 | data_buf[k] ^= crc; | |
320 | ||
321 | print_hex_break( data_buf+i, remain_seg_payload_len, 16); | |
322 | ||
323 | i += remain_seg_payload_len; | |
324 | ||
325 | PrintAndLog("-----+------------------------------------------------\n"); | |
326 | ||
327 | // end with last segment | |
328 | if (segment_flag & 0x8) return 0; | |
329 | ||
330 | } // end for loop | |
331 | ||
332 | } else { | |
333 | ||
334 | // Data start point on unsegmented cards | |
335 | i = 8; | |
336 | ||
337 | wrp = data_buf[7] & 0x0F; | |
338 | wrc = (data_buf[7] & 0x70) >> 4; | |
339 | ||
340 | bool hasWRC = (wrc > 0); | |
341 | bool hasWRP = (wrp > wrc); | |
342 | int wrp_len = (wrp - wrc); | |
343 | int remain_seg_payload_len = (1024 - 22 - wrp); // Any chance to get physical card size here!? | |
344 | ||
345 | PrintAndLog("Unsegmented card - WRP: %02u, WRC: %02u, RD: %01u", | |
346 | wrp, | |
347 | wrc, | |
348 | (data_buf[7] & 0x80) >> 7 | |
349 | ); | |
350 | ||
351 | if ( hasWRC ) { | |
352 | PrintAndLog("WRC protected area: (I %d | WRC %d)", i, wrc); | |
353 | PrintAndLog("\nrow | data"); | |
354 | PrintAndLog("-----+------------------------------------------------"); | |
355 | print_hex_break( data_buf+i, wrc, 16); | |
356 | i += wrc; | |
357 | } | |
358 | ||
359 | if ( hasWRP ) { | |
360 | PrintAndLog("Remaining write protected area: (I %d | WRC %d | WRP %d | WRP_LEN %d)", i, wrc, wrp, wrp_len); | |
361 | PrintAndLog("\nrow | data"); | |
362 | PrintAndLog("-----+------------------------------------------------"); | |
363 | print_hex_break( data_buf + i, wrp_len, 16); | |
364 | i += wrp_len; | |
365 | ||
366 | // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...) | |
367 | if( wrp_len == 8 ) | |
368 | PrintAndLog("Card ID: %2X%02X%02X", data_buf[i-4], data_buf[i-3], data_buf[i-2]); | |
369 | } | |
370 | ||
371 | PrintAndLog("Remaining segment payload: (I %d | Remain LEN %d)", i, remain_seg_payload_len); | |
372 | PrintAndLog("\nrow | data"); | |
373 | PrintAndLog("-----+------------------------------------------------"); | |
374 | print_hex_break( data_buf + i, remain_seg_payload_len, 16); | |
375 | i += remain_seg_payload_len; | |
376 | ||
377 | PrintAndLog("-----+------------------------------------------------\n"); | |
378 | } | |
379 | } | |
380 | return 0; | |
381 | } | |
382 | ||
383 | int CmdLegicRFRead(const char *Cmd) { | |
384 | ||
385 | // params: | |
386 | // offset in data | |
387 | // number of bytes. | |
388 | char cmdp = param_getchar(Cmd, 0); | |
389 | if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_read(); | |
390 | ||
391 | uint32_t offset = 0, len = 0, IV = 0; | |
392 | sscanf(Cmd, "%x %x %x", &offset, &len, &IV); | |
393 | ||
394 | // OUT-OF-BOUNDS check | |
395 | if(len + offset > MAX_LENGTH) len = MAX_LENGTH - offset; | |
396 | ||
397 | IV &= 0x7F; | |
398 | PrintAndLog("Current IV: 0x%02x", IV); | |
399 | ||
400 | UsbCommand c= {CMD_READER_LEGIC_RF, {offset, len, IV}}; | |
401 | clearCommandBuffer(); | |
402 | SendCommand(&c); | |
403 | return 0; | |
404 | } | |
405 | ||
406 | int CmdLegicLoad(const char *Cmd) { | |
407 | ||
408 | // iceman: potential bug, where all filepaths or filename which starts with H or h will print the helptext :) | |
409 | char cmdp = param_getchar(Cmd, 0); | |
410 | if ( cmdp == 'H' || cmdp == 'h' || cmdp == 0x00) return usage_legic_load(); | |
411 | ||
412 | char filename[FILE_PATH_SIZE] = {0x00}; | |
413 | int len = strlen(Cmd); | |
414 | ||
415 | if (len > FILE_PATH_SIZE) { | |
416 | PrintAndLog("Filepath too long (was %s bytes), max allowed is %s ", len, FILE_PATH_SIZE); | |
417 | return 0; | |
418 | } | |
419 | memcpy(filename, Cmd, len); | |
420 | ||
421 | FILE *f = fopen(filename, "r"); | |
422 | if(!f) { | |
423 | PrintAndLog("couldn't open '%s'", Cmd); | |
424 | return -1; | |
425 | } | |
426 | ||
427 | char line[80]; | |
428 | int offset = 0; | |
429 | uint8_t data[USB_CMD_DATA_SIZE] = {0x00}; | |
430 | int index = 0; | |
431 | int totalbytes = 0; | |
432 | while ( fgets(line, sizeof(line), f) ) { | |
433 | int res = sscanf(line, "%x %x %x %x %x %x %x %x", | |
434 | (unsigned int *)&data[index], | |
435 | (unsigned int *)&data[index + 1], | |
436 | (unsigned int *)&data[index + 2], | |
437 | (unsigned int *)&data[index + 3], | |
438 | (unsigned int *)&data[index + 4], | |
439 | (unsigned int *)&data[index + 5], | |
440 | (unsigned int *)&data[index + 6], | |
441 | (unsigned int *)&data[index + 7]); | |
442 | ||
443 | if(res != 8) { | |
444 | PrintAndLog("Error: could not read samples"); | |
445 | fclose(f); | |
446 | return -1; | |
447 | } | |
448 | index += res; | |
449 | ||
450 | if ( index == USB_CMD_DATA_SIZE ){ | |
451 | // PrintAndLog("sent %d | %d | %d", index, offset, totalbytes); | |
452 | UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}}; | |
453 | memcpy(c.d.asBytes, data, sizeof(data)); | |
454 | clearCommandBuffer(); | |
455 | SendCommand(&c); | |
456 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){ | |
457 | PrintAndLog("Command execute timeout"); | |
458 | fclose(f); | |
459 | return 1; | |
460 | } | |
461 | offset += index; | |
462 | totalbytes += index; | |
463 | index = 0; | |
464 | } | |
465 | } | |
466 | fclose(f); | |
467 | ||
468 | // left over bytes? | |
469 | if ( index != 0 ) { | |
470 | UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}}; | |
471 | memcpy(c.d.asBytes, data, 8); | |
472 | clearCommandBuffer(); | |
473 | SendCommand(&c); | |
474 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){ | |
475 | PrintAndLog("Command execute timeout"); | |
476 | return 1; | |
477 | } | |
478 | totalbytes += index; | |
479 | } | |
480 | ||
481 | PrintAndLog("loaded %u samples", totalbytes); | |
482 | return 0; | |
483 | } | |
484 | ||
485 | int CmdLegicSave(const char *Cmd) { | |
486 | int requested = 1024; | |
487 | int offset = 0; | |
488 | int delivered = 0; | |
489 | char filename[FILE_PATH_SIZE] = {0x00}; | |
490 | uint8_t got[1024] = {0x00}; | |
491 | ||
492 | memset(filename, 0, FILE_PATH_SIZE); | |
493 | ||
494 | sscanf(Cmd, " %s %i %i", filename, &requested, &offset); | |
495 | ||
496 | /* If no length given save entire legic read buffer */ | |
497 | /* round up to nearest 8 bytes so the saved data can be used with legicload */ | |
498 | if (requested == 0) | |
499 | requested = 1024; | |
500 | ||
501 | if (requested % 8 != 0) { | |
502 | int remainder = requested % 8; | |
503 | requested = requested + 8 - remainder; | |
504 | } | |
505 | ||
506 | if (offset + requested > sizeof(got)) { | |
507 | PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024"); | |
508 | return 0; | |
509 | } | |
510 | ||
511 | GetFromBigBuf(got, requested, offset); | |
512 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000)){ | |
513 | PrintAndLog("Command execute timeout"); | |
514 | return 1; | |
515 | } | |
516 | ||
517 | FILE *f = fopen(filename, "w"); | |
518 | if(!f) { | |
519 | PrintAndLog("couldn't open '%s'", Cmd+1); | |
520 | return -1; | |
521 | } | |
522 | ||
523 | for (int j = 0; j < requested; j += 8) { | |
524 | fprintf(f, "%02x %02x %02x %02x %02x %02x %02x %02x\n", | |
525 | got[j+0], got[j+1], got[j+2], got[j+3], | |
526 | got[j+4], got[j+5], got[j+6], got[j+7] | |
527 | ); | |
528 | delivered += 8; | |
529 | if (delivered >= requested) break; | |
530 | } | |
531 | ||
532 | fclose(f); | |
533 | PrintAndLog("saved %u samples", delivered); | |
534 | return 0; | |
535 | } | |
536 | ||
537 | //TODO: write a help text (iceman) | |
538 | int CmdLegicRfSim(const char *Cmd) { | |
539 | UsbCommand c = {CMD_SIMULATE_TAG_LEGIC_RF, {6,3,0}}; | |
540 | sscanf(Cmd, " %"lli" %"lli" %"lli, &c.arg[0], &c.arg[1], &c.arg[2]); | |
541 | clearCommandBuffer(); | |
542 | SendCommand(&c); | |
543 | return 0; | |
544 | } | |
545 | ||
546 | int CmdLegicRfWrite(const char *Cmd) { | |
547 | ||
548 | // params: | |
549 | // offset - in tag memory | |
550 | // length - num of bytes to be written | |
551 | // IV - initialisation vector | |
552 | ||
553 | char cmdp = param_getchar(Cmd, 0); | |
554 | if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_write(); | |
555 | ||
556 | uint32_t offset = 0, len = 0, IV = SESSION_IV; | |
557 | ||
558 | UsbCommand c = {CMD_WRITER_LEGIC_RF, {0,0,0}}; | |
559 | int res = sscanf(Cmd, "%x %x %x", &offset, &len, &IV); | |
560 | if(res < 2) { | |
561 | PrintAndLog("Please specify the offset and length as two hex strings and, optionally, the IV also as an hex string"); | |
562 | return -1; | |
563 | } | |
564 | ||
565 | // OUT-OF-BOUNDS check | |
566 | if(len + offset > MAX_LENGTH) len = MAX_LENGTH - offset; | |
567 | ||
568 | ||
569 | IV &= 0x7F; | |
570 | PrintAndLog("Current IV: 0x%02x", IV); | |
571 | ||
572 | c.arg[0] = offset; | |
573 | c.arg[1] = len; | |
574 | c.arg[2] = IV; | |
575 | ||
576 | clearCommandBuffer(); | |
577 | SendCommand(&c); | |
578 | return 0; | |
579 | } | |
580 | ||
581 | int CmdLegicRfRawWrite(const char *Cmd) { | |
582 | ||
583 | char cmdp = param_getchar(Cmd, 0); | |
584 | if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_rawwrite(); | |
585 | ||
586 | uint32_t address = 0, data = 0, IV = SESSION_IV; | |
587 | char answer; | |
588 | ||
589 | UsbCommand c = { CMD_RAW_WRITER_LEGIC_RF, {0,0,0} }; | |
590 | int res = sscanf(Cmd, "%x %x %x", &address, &data, &IV); | |
591 | if(res < 2) | |
592 | return usage_legic_rawwrite(); | |
593 | ||
594 | // OUT-OF-BOUNDS check | |
595 | if(address > MAX_LENGTH) | |
596 | return usage_legic_rawwrite(); | |
597 | ||
598 | IV &= 0x7F; | |
599 | PrintAndLog("Current IV: 0x%02x", IV); | |
600 | ||
601 | c.arg[0] = address; | |
602 | c.arg[1] = data; | |
603 | c.arg[2] = IV; | |
604 | ||
605 | if (c.arg[0] == 0x05 || c.arg[0] == 0x06) { | |
606 | PrintAndLog("############# DANGER !! #############"); | |
607 | PrintAndLog("# changing the DCF is irreversible #"); | |
608 | PrintAndLog("#####################################"); | |
609 | PrintAndLog("do youe really want to continue? y(es) n(o)"); | |
610 | if (scanf(" %c", &answer) > 0 && (answer == 'y' || answer == 'Y')) { | |
611 | SendCommand(&c); | |
612 | return 0; | |
613 | } | |
614 | return -1; | |
615 | } | |
616 | ||
617 | clearCommandBuffer(); | |
618 | SendCommand(&c); | |
619 | return 0; | |
620 | } | |
621 | ||
622 | //TODO: write a help text (iceman) | |
623 | int CmdLegicRfFill(const char *Cmd) { | |
624 | UsbCommand cmd = {CMD_WRITER_LEGIC_RF, {0,0,0} }; | |
625 | int res = sscanf(Cmd, " 0x%"llx" 0x%"llx" 0x%"llx, &cmd.arg[0], &cmd.arg[1], &cmd.arg[2]); | |
626 | if(res != 3) { | |
627 | PrintAndLog("Please specify the offset, length and value as two hex strings"); | |
628 | return -1; | |
629 | } | |
630 | ||
631 | int i; | |
632 | UsbCommand c = {CMD_DOWNLOADED_SIM_SAMPLES_125K, {0, 0, 0}}; | |
633 | memset(c.d.asBytes, cmd.arg[2], 48); | |
634 | ||
635 | for(i = 0; i < 22; i++) { | |
636 | c.arg[0] = i*48; | |
637 | ||
638 | clearCommandBuffer(); | |
639 | SendCommand(&c); | |
640 | WaitForResponse(CMD_ACK, NULL); | |
641 | } | |
642 | clearCommandBuffer(); | |
643 | SendCommand(&cmd); | |
644 | return 0; | |
645 | } | |
646 | ||
647 | int CmdLegicCalcCrc8(const char *Cmd){ | |
648 | ||
649 | uint8_t *data = NULL; | |
650 | uint8_t cmdp = 0, uidcrc = 0, type=0; | |
651 | bool errors = false; | |
652 | int len = 0; | |
653 | int bg, en; | |
654 | ||
655 | while(param_getchar(Cmd, cmdp) != 0x00) { | |
656 | switch(param_getchar(Cmd, cmdp)) { | |
657 | case 'b': | |
658 | case 'B': | |
659 | // peek at length of the input string so we can | |
660 | // figure out how many elements to malloc in "data" | |
661 | bg=en=0; | |
662 | if (param_getptr(Cmd, &bg, &en, cmdp+1)) { | |
663 | errors = true; | |
664 | break; | |
665 | } | |
666 | len = (en - bg + 1); | |
667 | ||
668 | // check that user entered even number of characters | |
669 | // for hex data string | |
670 | if (len & 1) { | |
671 | errors = true; | |
672 | break; | |
673 | } | |
674 | ||
675 | // it's possible for user to accidentally enter "b" parameter | |
676 | // more than once - we have to clean previous malloc | |
677 | if (data) free(data); | |
678 | data = malloc(len >> 1); | |
679 | if ( data == NULL ) { | |
680 | PrintAndLog("Can't allocate memory. exiting"); | |
681 | errors = true; | |
682 | break; | |
683 | } | |
684 | ||
685 | if (param_gethex(Cmd, cmdp+1, data, len)) { | |
686 | errors = true; | |
687 | break; | |
688 | } | |
689 | ||
690 | len >>= 1; | |
691 | cmdp += 2; | |
692 | break; | |
693 | case 'u': | |
694 | case 'U': | |
695 | uidcrc = param_get8ex(Cmd, cmdp+1, 0, 16); | |
696 | cmdp += 2; | |
697 | break; | |
698 | case 'c': | |
699 | case 'C': | |
700 | type = param_get8ex(Cmd, cmdp+1, 0, 10); | |
701 | cmdp += 2; | |
702 | break; | |
703 | case 'h': | |
704 | case 'H': | |
705 | errors = true; | |
706 | break; | |
707 | default: | |
708 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
709 | errors = true; | |
710 | break; | |
711 | } | |
712 | if (errors) break; | |
713 | } | |
714 | //Validations | |
715 | if (errors){ | |
716 | if (data) free(data); | |
717 | return usage_legic_calccrc8(); | |
718 | } | |
719 | ||
720 | switch (type){ | |
721 | case 16: | |
722 | PrintAndLog("LEGIC CRC16: %X", CRC16Legic(data, len, uidcrc)); | |
723 | break; | |
724 | default: | |
725 | PrintAndLog("LEGIC CRC8: %X", CRC8Legic(data, len) ); | |
726 | break; | |
727 | } | |
728 | ||
729 | if (data) free(data); | |
730 | return 0; | |
731 | } | |
732 | ||
733 | static command_t CommandTable[] = { | |
734 | {"help", CmdHelp, 1, "This help"}, | |
735 | {"decode", CmdLegicDecode, 0, "Display deobfuscated and decoded LEGIC RF tag data (use after hf legic reader)"}, | |
736 | {"read", CmdLegicRFRead, 0, "[offset][length] <iv> -- read bytes from a LEGIC card"}, | |
737 | {"save", CmdLegicSave, 0, "<filename> [<length>] -- Store samples"}, | |
738 | {"load", CmdLegicLoad, 0, "<filename> -- Restore samples"}, | |
739 | {"sim", CmdLegicRfSim, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"}, | |
740 | {"write", CmdLegicRfWrite,0, "<offset> <length> <iv> -- Write sample buffer (user after load or read)"}, | |
741 | {"writeraw",CmdLegicRfRawWrite, 0, "<address> <value> <iv> -- Write direct to address"}, | |
742 | {"fill", CmdLegicRfFill, 0, "<offset> <length> <value> -- Fill/Write tag with constant value"}, | |
743 | {"crc8", CmdLegicCalcCrc8, 1, "Calculate Legic CRC8 over given hexbytes"}, | |
744 | {NULL, NULL, 0, NULL} | |
745 | }; | |
746 | ||
747 | int CmdHFLegic(const char *Cmd) { | |
748 | clearCommandBuffer(); | |
749 | CmdsParse(CommandTable, Cmd); | |
750 | return 0; | |
751 | } | |
752 | ||
753 | int CmdHelp(const char *Cmd) { | |
754 | CmdsHelp(CommandTable); | |
755 | return 0; | |
756 | } |