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