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