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