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