]>
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 | //----------------------------------------------------------------------------- | |
10 | ||
8e220a91 | 11 | #include <stdio.h> |
12 | #include <string.h> | |
902cb3c0 | 13 | #include "proxmark3.h" |
41dab153 | 14 | #include "data.h" |
15 | #include "ui.h" | |
7fe9b0b7 | 16 | #include "cmdparser.h" |
17 | #include "cmdhflegic.h" | |
669c1b80 | 18 | #include "cmdmain.h" |
31d1caa5 | 19 | #include "util.h" |
3b920280 | 20 | #include "crc.h" |
7fe9b0b7 | 21 | static int CmdHelp(const char *Cmd); |
22 | ||
3b920280 | 23 | int usage_legic_calccrc8(void){ |
24 | PrintAndLog("Calculates the legic crc8 on the input hexbytes."); | |
25 | PrintAndLog("There must be an even number of hexsymbols as input."); | |
26 | PrintAndLog("Usage: hf legic crc8 <hexbytes>"); | |
27 | PrintAndLog("Options :"); | |
28 | PrintAndLog(" <hexbytes> : hex bytes in a string"); | |
29 | PrintAndLog(""); | |
30 | PrintAndLog("Sample : hf legic crc8 deadbeef1122"); | |
31 | return 0; | |
32 | } | |
33 | ||
e579e768 | 34 | int usage_legic_load(void){ |
35 | PrintAndLog("It loads datasamples from the file `filename` to device memory"); | |
36 | PrintAndLog("Usage: hf legic load <file name>"); | |
37 | PrintAndLog(" sample: hf legic load filename"); | |
38 | return 0; | |
39 | } | |
40 | ||
669c1b80 | 41 | /* |
42 | * Output BigBuf and deobfuscate LEGIC RF tag data. | |
43 | * This is based on information given in the talk held | |
44 | * by Henryk Ploetz and Karsten Nohl at 26c3 | |
669c1b80 | 45 | */ |
3b920280 | 46 | int CmdLegicDecode(const char *Cmd) { |
d7fd9084 | 47 | // Index for the bytearray. |
48 | int i = 0; | |
49 | int k = 0, segmentNum; | |
52cf34c1 | 50 | int segment_len = 0; |
51 | int segment_flag = 0; | |
60bb5ef7 | 52 | uint8_t stamp_len = 0; |
52cf34c1 | 53 | int crc = 0; |
54 | int wrp = 0; | |
55 | int wrc = 0; | |
d7fd9084 | 56 | uint8_t data_buf[1200]; // receiver buffer, should be 1024.. |
52cf34c1 | 57 | char token_type[4]; |
58 | ||
59 | // copy data from proxmark into buffer | |
60 | GetFromBigBuf(data_buf,sizeof(data_buf),0); | |
61 | WaitForResponse(CMD_ACK,NULL); | |
62 | ||
63 | // Output CDF System area (9 bytes) plus remaining header area (12 bytes) | |
3b920280 | 64 | crc = data_buf[4]; |
65 | uint32_t calc_crc = CRC8Legic(data_buf, 4); | |
66 | ||
52cf34c1 | 67 | PrintAndLog("\nCDF: System Area"); |
68 | ||
3b920280 | 69 | PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x %s", |
52cf34c1 | 70 | data_buf[0], |
71 | data_buf[1], | |
72 | data_buf[2], | |
73 | data_buf[3], | |
3b920280 | 74 | data_buf[4], |
e579e768 | 75 | (calc_crc == crc) ? "OK":"Fail" |
52cf34c1 | 76 | ); |
669c1b80 | 77 | |
d7fd9084 | 78 | switch (data_buf[5] & 0x7f) { |
52cf34c1 | 79 | case 0x00 ... 0x2f: |
80 | strncpy(token_type, "IAM",sizeof(token_type)); | |
81 | break; | |
82 | case 0x30 ... 0x6f: | |
d7fd9084 | 83 | strncpy(token_type, "SAM",sizeof(token_type)); |
52cf34c1 | 84 | break; |
85 | case 0x70 ... 0x7f: | |
d7fd9084 | 86 | strncpy(token_type, "GAM",sizeof(token_type)); |
52cf34c1 | 87 | break; |
88 | default: | |
d7fd9084 | 89 | strncpy(token_type, "???",sizeof(token_type)); |
52cf34c1 | 90 | break; |
91 | } | |
92 | ||
93 | stamp_len = 0xfc - data_buf[6]; | |
94 | ||
9827020a | 95 | PrintAndLog("DCF: %02x %02x, Token Type=%s (OLE=%01u), Stamp len=%02u", |
52cf34c1 | 96 | data_buf[5], |
97 | data_buf[6], | |
98 | token_type, | |
99 | (data_buf[5]&0x80)>>7, | |
100 | stamp_len | |
101 | ); | |
102 | ||
103 | PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, raw=%02x, SSC=%02x", | |
104 | data_buf[7]&0x0f, | |
105 | (data_buf[7]&0x70)>>4, | |
106 | (data_buf[7]&0x80)>>7, | |
107 | data_buf[7], | |
108 | data_buf[8] | |
109 | ); | |
110 | ||
111 | PrintAndLog("Remaining Header Area"); | |
112 | PrintAndLog("%s", sprint_hex(data_buf+9, 13)); | |
d7fd9084 | 113 | |
e579e768 | 114 | uint8_t segCrcBytes[8] = {0x00}; |
115 | uint32_t segCalcCRC = 0; | |
116 | uint32_t segCRC = 0; | |
d7fd9084 | 117 | |
118 | PrintAndLog("\nADF: User Area"); | |
9827020a | 119 | printf("-------------------------------------\n"); |
d7fd9084 | 120 | i = 22; |
121 | // 64 potential segements | |
122 | for ( segmentNum=0; segmentNum<64; segmentNum++ ) { | |
52cf34c1 | 123 | segment_len = ((data_buf[i+1]^crc)&0x0f) * 256 + (data_buf[i]^crc); |
124 | segment_flag = ((data_buf[i+1]^crc)&0xf0)>>4; | |
125 | ||
126 | wrp = (data_buf[i+2]^crc); | |
127 | wrc = ((data_buf[i+3]^crc)&0x70)>>4; | |
128 | ||
d7fd9084 | 129 | bool hasWRC = (wrc > 0); |
130 | bool hasWRP = (wrp > wrc); | |
131 | int wrp_len = (wrp - wrc); | |
132 | int remain_seg_payload_len = (segment_len - wrp - 5); | |
e579e768 | 133 | |
d7fd9084 | 134 | // validate segment-crc |
135 | segCrcBytes[0]=data_buf[0]; //uid0 | |
136 | segCrcBytes[1]=data_buf[1]; //uid1 | |
137 | segCrcBytes[2]=data_buf[2]; //uid2 | |
138 | segCrcBytes[3]=data_buf[3]; //uid3 | |
139 | segCrcBytes[4]=(data_buf[i]^crc); //hdr0 | |
e579e768 | 140 | segCrcBytes[5]=(data_buf[i+1]^crc); //hdr1 |
141 | segCrcBytes[6]=(data_buf[i+2]^crc); //hdr2 | |
142 | segCrcBytes[7]=(data_buf[i+3]^crc); //hdr3 | |
d7fd9084 | 143 | |
e579e768 | 144 | segCalcCRC = CRC8Legic(segCrcBytes, 8); |
d7fd9084 | 145 | segCRC = data_buf[i+4]^crc; |
e579e768 | 146 | |
9827020a | 147 | PrintAndLog("Segment %02u \nraw header=0x%02X 0x%02X 0x%02X 0x%02X \nSegment len: %u, Flag: 0x%X (valid:%01u, last:%01u), WRP: %02u, WRC: %02u, RD: %01u, CRC: 0x%02X (%s)", |
d7fd9084 | 148 | segmentNum, |
52cf34c1 | 149 | data_buf[i]^crc, |
150 | data_buf[i+1]^crc, | |
151 | data_buf[i+2]^crc, | |
152 | data_buf[i+3]^crc, | |
9827020a | 153 | segment_len, |
52cf34c1 | 154 | segment_flag, |
d7fd9084 | 155 | (segment_flag & 0x4) >> 2, |
156 | (segment_flag & 0x8) >> 3, | |
52cf34c1 | 157 | wrp, |
158 | wrc, | |
d7fd9084 | 159 | ((data_buf[i+3]^crc) & 0x80) >> 7, |
e579e768 | 160 | segCRC, |
161 | ( segCRC == segCalcCRC ) ? "OK" : "fail" | |
52cf34c1 | 162 | ); |
163 | ||
3b920280 | 164 | i += 5; |
669c1b80 | 165 | |
d7fd9084 | 166 | if ( hasWRC ) { |
167 | PrintAndLog("WRC protected area: (I %d | K %d| WRC %d)", i, k, wrc); | |
3b920280 | 168 | |
169 | for ( k=i; k < wrc; k++) | |
170 | data_buf[k] ^= crc; | |
171 | ||
d7fd9084 | 172 | //is WRC / 8? |
173 | ||
174 | // for ( k=i; k < wrc; k += 8) | |
9827020a | 175 | print_hex_break( data_buf+i, wrc, 16); |
3b920280 | 176 | |
177 | i += wrc; | |
52cf34c1 | 178 | } |
669c1b80 | 179 | |
d7fd9084 | 180 | if ( hasWRP ) { |
181 | PrintAndLog("Remaining write protected area: (I %d | K %d | WRC %d | WRP %d WRP_LEN %d)",i, k, wrc, wrp, wrp_len); | |
182 | ||
9827020a | 183 | // de-xor? if not zero, assume it needs xoring. |
184 | if ( data_buf[i] > 0) { | |
185 | for (k=i; k < wrp_len; k++) | |
186 | data_buf[k] ^= crc; | |
187 | } | |
3b920280 | 188 | |
d7fd9084 | 189 | // for (k=i; k < wrp_len; k += 16) { |
190 | ||
9827020a | 191 | print_hex_break( data_buf+i, wrp_len, 16); |
d7fd9084 | 192 | // } |
3b920280 | 193 | |
d7fd9084 | 194 | i += wrp_len; |
195 | ||
9827020a | 196 | if( wrp_len == 8 ) |
197 | PrintAndLog("Card ID: %2X%02X%02X", data_buf[i-4]^crc, data_buf[i-3]^crc, data_buf[i-2]^crc); | |
52cf34c1 | 198 | } |
669c1b80 | 199 | |
52cf34c1 | 200 | PrintAndLog("Remaining segment payload:"); |
3b920280 | 201 | |
9827020a | 202 | if ( data_buf[i] > 0 ) { |
203 | for ( k=i; k < remain_seg_payload_len; k++) | |
204 | data_buf[k] ^= crc; | |
205 | } | |
3b920280 | 206 | |
d7fd9084 | 207 | // for ( k=i; k < remain_seg_payload_len; k++) |
9827020a | 208 | print_hex_break( data_buf+i, remain_seg_payload_len, 16); |
669c1b80 | 209 | |
d7fd9084 | 210 | i += remain_seg_payload_len; |
211 | ||
9827020a | 212 | printf("\n-------------------------------------\n"); |
52cf34c1 | 213 | // end with last segment |
214 | if (segment_flag & 0x8) return 0; | |
215 | ||
216 | } // end for loop | |
217 | return 0; | |
669c1b80 | 218 | } |
41dab153 | 219 | |
52cf34c1 | 220 | int CmdLegicRFRead(const char *Cmd) { |
221 | int byte_count=0, offset=0; | |
222 | sscanf(Cmd, "%i %i", &offset, &byte_count); | |
223 | if(byte_count == 0) byte_count = -1; | |
224 | if(byte_count + offset > 1024) byte_count = 1024 - offset; | |
225 | ||
226 | UsbCommand c= {CMD_READER_LEGIC_RF, {offset, byte_count, 0}}; | |
227 | clearCommandBuffer(); | |
228 | SendCommand(&c); | |
229 | return 0; | |
41dab153 | 230 | } |
3612a8a8 | 231 | |
52cf34c1 | 232 | int CmdLegicLoad(const char *Cmd) { |
b915fda3 | 233 | |
52cf34c1 | 234 | char cmdp = param_getchar(Cmd, 0); |
e579e768 | 235 | if ( cmdp == 'H' || cmdp == 'h' || cmdp == 0x00) return usage_legic_load(); |
b915fda3 | 236 | |
e579e768 | 237 | char filename[FILE_PATH_SIZE] = {0x00}; |
238 | int len = strlen(Cmd); | |
239 | ||
b915fda3 | 240 | if (len > FILE_PATH_SIZE) { |
241 | PrintAndLog("Filepath too long (was %s bytes), max allowed is %s ", len, FILE_PATH_SIZE); | |
242 | return 0; | |
243 | } | |
244 | memcpy(filename, Cmd, len); | |
245 | ||
246 | FILE *f = fopen(filename, "r"); | |
3612a8a8 | 247 | if(!f) { |
248 | PrintAndLog("couldn't open '%s'", Cmd); | |
249 | return -1; | |
250 | } | |
52cf34c1 | 251 | |
252 | char line[80]; | |
810f5379 | 253 | int offset = 0; |
c6e0a2eb | 254 | uint8_t data[USB_CMD_DATA_SIZE] = {0x00}; |
255 | int index = 0; | |
256 | int totalbytes = 0; | |
52cf34c1 | 257 | while ( fgets(line, sizeof(line), f) ) { |
3612a8a8 | 258 | int res = sscanf(line, "%x %x %x %x %x %x %x %x", |
c6e0a2eb | 259 | (unsigned int *)&data[index], |
260 | (unsigned int *)&data[index + 1], | |
261 | (unsigned int *)&data[index + 2], | |
262 | (unsigned int *)&data[index + 3], | |
263 | (unsigned int *)&data[index + 4], | |
264 | (unsigned int *)&data[index + 5], | |
265 | (unsigned int *)&data[index + 6], | |
266 | (unsigned int *)&data[index + 7]); | |
e7d099dc | 267 | |
3612a8a8 | 268 | if(res != 8) { |
269 | PrintAndLog("Error: could not read samples"); | |
270 | fclose(f); | |
271 | return -1; | |
272 | } | |
c6e0a2eb | 273 | index += res; |
274 | ||
275 | if ( index == USB_CMD_DATA_SIZE ){ | |
276 | // PrintAndLog("sent %d | %d | %d", index, offset, totalbytes); | |
277 | UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}}; | |
278 | memcpy(c.d.asBytes, data, sizeof(data)); | |
279 | clearCommandBuffer(); | |
280 | SendCommand(&c); | |
d7fd9084 | 281 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){ |
282 | PrintAndLog("Command execute timeout"); | |
283 | fclose(f); | |
284 | return 1; | |
285 | } | |
c6e0a2eb | 286 | offset += index; |
287 | totalbytes += index; | |
288 | index = 0; | |
289 | } | |
3612a8a8 | 290 | } |
291 | fclose(f); | |
c6e0a2eb | 292 | |
293 | // left over bytes? | |
294 | if ( index != 0 ) { | |
295 | UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}}; | |
296 | memcpy(c.d.asBytes, data, 8); | |
297 | clearCommandBuffer(); | |
298 | SendCommand(&c); | |
d7fd9084 | 299 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){ |
300 | PrintAndLog("Command execute timeout"); | |
301 | return 1; | |
302 | } | |
c6e0a2eb | 303 | totalbytes += index; |
304 | } | |
305 | ||
306 | PrintAndLog("loaded %u samples", totalbytes); | |
3612a8a8 | 307 | return 0; |
308 | } | |
309 | ||
52cf34c1 | 310 | int CmdLegicSave(const char *Cmd) { |
311 | int requested = 1024; | |
312 | int offset = 0; | |
313 | int delivered = 0; | |
314 | char filename[FILE_PATH_SIZE]; | |
315 | uint8_t got[1024] = {0x00}; | |
316 | ||
317 | sscanf(Cmd, " %s %i %i", filename, &requested, &offset); | |
318 | ||
319 | /* If no length given save entire legic read buffer */ | |
320 | /* round up to nearest 8 bytes so the saved data can be used with legicload */ | |
e7d099dc | 321 | if (requested == 0) |
52cf34c1 | 322 | requested = 1024; |
52cf34c1 | 323 | |
324 | if (requested % 8 != 0) { | |
325 | int remainder = requested % 8; | |
326 | requested = requested + 8 - remainder; | |
327 | } | |
328 | ||
329 | if (offset + requested > sizeof(got)) { | |
330 | PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024"); | |
331 | return 0; | |
332 | } | |
333 | ||
334 | FILE *f = fopen(filename, "w"); | |
335 | if(!f) { | |
336 | PrintAndLog("couldn't open '%s'", Cmd+1); | |
337 | return -1; | |
338 | } | |
339 | ||
d7fd9084 | 340 | GetFromBigBuf(got, requested, offset); |
341 | WaitForResponse(CMD_ACK, NULL); | |
52cf34c1 | 342 | |
343 | for (int j = 0; j < requested; j += 8) { | |
344 | fprintf(f, "%02x %02x %02x %02x %02x %02x %02x %02x\n", | |
c6e0a2eb | 345 | got[j+0], got[j+1], got[j+2], got[j+3], |
346 | got[j+4], got[j+5], got[j+6], got[j+7] | |
52cf34c1 | 347 | ); |
348 | delivered += 8; | |
349 | if (delivered >= requested) break; | |
350 | } | |
351 | ||
352 | fclose(f); | |
353 | PrintAndLog("saved %u samples", delivered); | |
354 | return 0; | |
3612a8a8 | 355 | } |
356 | ||
52cf34c1 | 357 | int CmdLegicRfSim(const char *Cmd) { |
3b920280 | 358 | UsbCommand c = {CMD_SIMULATE_TAG_LEGIC_RF, {6,3,0}}; |
52cf34c1 | 359 | sscanf(Cmd, " %"lli" %"lli" %"lli, &c.arg[0], &c.arg[1], &c.arg[2]); |
360 | clearCommandBuffer(); | |
361 | SendCommand(&c); | |
362 | return 0; | |
3612a8a8 | 363 | } |
364 | ||
52cf34c1 | 365 | int CmdLegicRfWrite(const char *Cmd) { |
366 | UsbCommand c = {CMD_WRITER_LEGIC_RF}; | |
125a98a1 | 367 | int res = sscanf(Cmd, " 0x%"llx" 0x%"llx, &c.arg[0], &c.arg[1]); |
3612a8a8 | 368 | if(res != 2) { |
369 | PrintAndLog("Please specify the offset and length as two hex strings"); | |
370 | return -1; | |
371 | } | |
52cf34c1 | 372 | clearCommandBuffer(); |
3612a8a8 | 373 | SendCommand(&c); |
374 | return 0; | |
375 | } | |
376 | ||
52cf34c1 | 377 | int CmdLegicRfFill(const char *Cmd) { |
378 | UsbCommand cmd = {CMD_WRITER_LEGIC_RF}; | |
1a07fd51 | 379 | int res = sscanf(Cmd, " 0x%"llx" 0x%"llx" 0x%"llx, &cmd.arg[0], &cmd.arg[1], &cmd.arg[2]); |
3612a8a8 | 380 | if(res != 3) { |
381 | PrintAndLog("Please specify the offset, length and value as two hex strings"); | |
382 | return -1; | |
383 | } | |
384 | ||
385 | int i; | |
52cf34c1 | 386 | UsbCommand c = {CMD_DOWNLOADED_SIM_SAMPLES_125K, {0, 0, 0}}; |
3612a8a8 | 387 | for(i = 0; i < 48; i++) { |
52cf34c1 | 388 | c.d.asBytes[i] = cmd.arg[2]; |
3612a8a8 | 389 | } |
52cf34c1 | 390 | |
391 | for(i = 0; i < 22; i++) { | |
392 | c.arg[0] = i*48; | |
393 | SendCommand(&c); | |
394 | WaitForResponse(CMD_ACK,NULL); | |
395 | } | |
396 | clearCommandBuffer(); | |
3612a8a8 | 397 | SendCommand(&cmd); |
398 | return 0; | |
399 | } | |
400 | ||
3b920280 | 401 | int CmdLegicCalcCrc8(const char *Cmd){ |
402 | ||
403 | int len = strlen(Cmd); | |
404 | if (len & 1 ) return usage_legic_calccrc8(); | |
405 | ||
406 | uint8_t *data = malloc(len); | |
407 | if ( data == NULL ) return 1; | |
408 | ||
409 | param_gethex(Cmd, 0, data, len ); | |
410 | ||
411 | uint32_t checksum = CRC8Legic(data, len/2); | |
412 | PrintAndLog("Bytes: %s || CRC8: %X", sprint_hex(data, len/2), checksum ); | |
413 | free(data); | |
414 | return 0; | |
415 | } | |
416 | ||
52cf34c1 | 417 | static command_t CommandTable[] = { |
3b920280 | 418 | {"help", CmdHelp, 1, "This help"}, |
419 | {"decode", CmdLegicDecode, 0, "Display deobfuscated and decoded LEGIC RF tag data (use after hf legic reader)"}, | |
420 | {"read", CmdLegicRFRead, 0, "[offset][length] -- read bytes from a LEGIC card"}, | |
421 | {"save", CmdLegicSave, 0, "<filename> [<length>] -- Store samples"}, | |
422 | {"load", CmdLegicLoad, 0, "<filename> -- Restore samples"}, | |
423 | {"sim", CmdLegicRfSim, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"}, | |
424 | {"write", CmdLegicRfWrite,0, "<offset> <length> -- Write sample buffer (user after load or read)"}, | |
425 | {"fill", CmdLegicRfFill, 0, "<offset> <length> <value> -- Fill/Write tag with constant value"}, | |
426 | {"crc8", CmdLegicCalcCrc8, 1, "Calculate Legic CRC8 over given hexbytes"}, | |
52cf34c1 | 427 | {NULL, NULL, 0, NULL} |
428 | }; | |
429 | ||
430 | int CmdHFLegic(const char *Cmd) { | |
431 | clearCommandBuffer(); | |
432 | CmdsParse(CommandTable, Cmd); | |
433 | return 0; | |
434 | } | |
435 | ||
436 | int CmdHelp(const char *Cmd) { | |
437 | CmdsHelp(CommandTable); | |
438 | return 0; | |
439 | } |