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