]>
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 | ||
95 | PrintAndLog("DCF: %02x %02x, Token_Type=%s (OLE=%01u), Stamp_len=%02u", | |
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"); | |
119 | i = 22; | |
120 | // 64 potential segements | |
121 | for ( segmentNum=0; segmentNum<64; segmentNum++ ) { | |
52cf34c1 | 122 | segment_len = ((data_buf[i+1]^crc)&0x0f) * 256 + (data_buf[i]^crc); |
123 | segment_flag = ((data_buf[i+1]^crc)&0xf0)>>4; | |
124 | ||
125 | wrp = (data_buf[i+2]^crc); | |
126 | wrc = ((data_buf[i+3]^crc)&0x70)>>4; | |
127 | ||
d7fd9084 | 128 | bool hasWRC = (wrc > 0); |
129 | bool hasWRP = (wrp > wrc); | |
130 | int wrp_len = (wrp - wrc); | |
131 | int remain_seg_payload_len = (segment_len - wrp - 5); | |
e579e768 | 132 | |
d7fd9084 | 133 | // validate segment-crc |
134 | segCrcBytes[0]=data_buf[0]; //uid0 | |
135 | segCrcBytes[1]=data_buf[1]; //uid1 | |
136 | segCrcBytes[2]=data_buf[2]; //uid2 | |
137 | segCrcBytes[3]=data_buf[3]; //uid3 | |
138 | segCrcBytes[4]=(data_buf[i]^crc); //hdr0 | |
e579e768 | 139 | segCrcBytes[5]=(data_buf[i+1]^crc); //hdr1 |
140 | segCrcBytes[6]=(data_buf[i+2]^crc); //hdr2 | |
141 | segCrcBytes[7]=(data_buf[i+3]^crc); //hdr3 | |
d7fd9084 | 142 | |
e579e768 | 143 | segCalcCRC = CRC8Legic(segCrcBytes, 8); |
d7fd9084 | 144 | segCRC = data_buf[i+4]^crc; |
e579e768 | 145 | |
146 | PrintAndLog("Segment %02u: raw header=%02x %02x %02x %02x, flag=%01x (valid=%01u, last=%01u), len=%04u, WRP=%02u, WRC=%02u, RD=%01u, CRC=%02x (%s)", | |
d7fd9084 | 147 | segmentNum, |
52cf34c1 | 148 | data_buf[i]^crc, |
149 | data_buf[i+1]^crc, | |
150 | data_buf[i+2]^crc, | |
151 | data_buf[i+3]^crc, | |
152 | segment_flag, | |
d7fd9084 | 153 | (segment_flag & 0x4) >> 2, |
154 | (segment_flag & 0x8) >> 3, | |
52cf34c1 | 155 | segment_len, |
156 | wrp, | |
157 | wrc, | |
d7fd9084 | 158 | ((data_buf[i+3]^crc) & 0x80) >> 7, |
e579e768 | 159 | segCRC, |
160 | ( segCRC == segCalcCRC ) ? "OK" : "fail" | |
52cf34c1 | 161 | ); |
162 | ||
3b920280 | 163 | i += 5; |
669c1b80 | 164 | |
d7fd9084 | 165 | if ( hasWRC ) { |
166 | PrintAndLog("WRC protected area: (I %d | K %d| WRC %d)", i, k, wrc); | |
3b920280 | 167 | |
168 | for ( k=i; k < wrc; k++) | |
169 | data_buf[k] ^= crc; | |
170 | ||
d7fd9084 | 171 | //is WRC / 8? |
172 | ||
173 | // for ( k=i; k < wrc; k += 8) | |
174 | PrintAndLog("%s", sprint_hex( data_buf+i, wrc ) ); | |
3b920280 | 175 | |
176 | i += wrc; | |
52cf34c1 | 177 | } |
669c1b80 | 178 | |
d7fd9084 | 179 | if ( hasWRP ) { |
180 | PrintAndLog("Remaining write protected area: (I %d | K %d | WRC %d | WRP %d WRP_LEN %d)",i, k, wrc, wrp, wrp_len); | |
181 | ||
182 | // // de-xor? | |
183 | // if ( data_buf[k] > 0) { | |
184 | // for (k=i; k < wrp_len; k++) | |
185 | // data_buf[k] ^= crc; | |
186 | // } | |
3b920280 | 187 | |
d7fd9084 | 188 | // for (k=i; k < wrp_len; k += 16) { |
189 | ||
190 | PrintAndLog("%s", sprint_hex( data_buf+i, wrp_len)); | |
191 | // } | |
3b920280 | 192 | |
d7fd9084 | 193 | i += wrp_len; |
194 | ||
195 | // if( wrp_len == 8 ) | |
196 | // PrintAndLog("Card ID: %2X%02X%02X", data_buf[i-4]^crc, data_buf[i-3]^crc, data_buf[i-2]^crc); | |
52cf34c1 | 197 | } |
669c1b80 | 198 | |
52cf34c1 | 199 | PrintAndLog("Remaining segment payload:"); |
3b920280 | 200 | |
d7fd9084 | 201 | // if ( data_buf[k] > 0 ) { |
202 | // for ( k=i; k < remain_seg_payload_len; k++) | |
203 | // data_buf[k] ^= crc; | |
204 | // } | |
3b920280 | 205 | |
d7fd9084 | 206 | // for ( k=i; k < remain_seg_payload_len; k++) |
207 | PrintAndLog("%s", sprint_hex( data_buf+i, remain_seg_payload_len ) ); | |
669c1b80 | 208 | |
d7fd9084 | 209 | i += remain_seg_payload_len; |
210 | ||
52cf34c1 | 211 | // end with last segment |
212 | if (segment_flag & 0x8) return 0; | |
213 | ||
214 | } // end for loop | |
215 | return 0; | |
669c1b80 | 216 | } |
41dab153 | 217 | |
52cf34c1 | 218 | int CmdLegicRFRead(const char *Cmd) { |
219 | int byte_count=0, offset=0; | |
220 | sscanf(Cmd, "%i %i", &offset, &byte_count); | |
221 | if(byte_count == 0) byte_count = -1; | |
222 | if(byte_count + offset > 1024) byte_count = 1024 - offset; | |
223 | ||
224 | UsbCommand c= {CMD_READER_LEGIC_RF, {offset, byte_count, 0}}; | |
225 | clearCommandBuffer(); | |
226 | SendCommand(&c); | |
227 | return 0; | |
41dab153 | 228 | } |
3612a8a8 | 229 | |
52cf34c1 | 230 | int CmdLegicLoad(const char *Cmd) { |
b915fda3 | 231 | |
52cf34c1 | 232 | char cmdp = param_getchar(Cmd, 0); |
e579e768 | 233 | if ( cmdp == 'H' || cmdp == 'h' || cmdp == 0x00) return usage_legic_load(); |
b915fda3 | 234 | |
e579e768 | 235 | char filename[FILE_PATH_SIZE] = {0x00}; |
236 | int len = strlen(Cmd); | |
237 | ||
b915fda3 | 238 | if (len > FILE_PATH_SIZE) { |
239 | PrintAndLog("Filepath too long (was %s bytes), max allowed is %s ", len, FILE_PATH_SIZE); | |
240 | return 0; | |
241 | } | |
242 | memcpy(filename, Cmd, len); | |
243 | ||
244 | FILE *f = fopen(filename, "r"); | |
3612a8a8 | 245 | if(!f) { |
246 | PrintAndLog("couldn't open '%s'", Cmd); | |
247 | return -1; | |
248 | } | |
52cf34c1 | 249 | |
250 | char line[80]; | |
810f5379 | 251 | int offset = 0; |
c6e0a2eb | 252 | uint8_t data[USB_CMD_DATA_SIZE] = {0x00}; |
253 | int index = 0; | |
254 | int totalbytes = 0; | |
52cf34c1 | 255 | while ( fgets(line, sizeof(line), f) ) { |
3612a8a8 | 256 | int res = sscanf(line, "%x %x %x %x %x %x %x %x", |
c6e0a2eb | 257 | (unsigned int *)&data[index], |
258 | (unsigned int *)&data[index + 1], | |
259 | (unsigned int *)&data[index + 2], | |
260 | (unsigned int *)&data[index + 3], | |
261 | (unsigned int *)&data[index + 4], | |
262 | (unsigned int *)&data[index + 5], | |
263 | (unsigned int *)&data[index + 6], | |
264 | (unsigned int *)&data[index + 7]); | |
e7d099dc | 265 | |
3612a8a8 | 266 | if(res != 8) { |
267 | PrintAndLog("Error: could not read samples"); | |
268 | fclose(f); | |
269 | return -1; | |
270 | } | |
c6e0a2eb | 271 | index += res; |
272 | ||
273 | if ( index == USB_CMD_DATA_SIZE ){ | |
274 | // PrintAndLog("sent %d | %d | %d", index, offset, totalbytes); | |
275 | UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}}; | |
276 | memcpy(c.d.asBytes, data, sizeof(data)); | |
277 | clearCommandBuffer(); | |
278 | SendCommand(&c); | |
d7fd9084 | 279 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){ |
280 | PrintAndLog("Command execute timeout"); | |
281 | fclose(f); | |
282 | return 1; | |
283 | } | |
c6e0a2eb | 284 | offset += index; |
285 | totalbytes += index; | |
286 | index = 0; | |
287 | } | |
3612a8a8 | 288 | } |
289 | fclose(f); | |
c6e0a2eb | 290 | |
291 | // left over bytes? | |
292 | if ( index != 0 ) { | |
293 | UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}}; | |
294 | memcpy(c.d.asBytes, data, 8); | |
295 | clearCommandBuffer(); | |
296 | SendCommand(&c); | |
d7fd9084 | 297 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){ |
298 | PrintAndLog("Command execute timeout"); | |
299 | return 1; | |
300 | } | |
c6e0a2eb | 301 | totalbytes += index; |
302 | } | |
303 | ||
304 | PrintAndLog("loaded %u samples", totalbytes); | |
3612a8a8 | 305 | return 0; |
306 | } | |
307 | ||
52cf34c1 | 308 | int CmdLegicSave(const char *Cmd) { |
309 | int requested = 1024; | |
310 | int offset = 0; | |
311 | int delivered = 0; | |
312 | char filename[FILE_PATH_SIZE]; | |
313 | uint8_t got[1024] = {0x00}; | |
314 | ||
315 | sscanf(Cmd, " %s %i %i", filename, &requested, &offset); | |
316 | ||
317 | /* If no length given save entire legic read buffer */ | |
318 | /* round up to nearest 8 bytes so the saved data can be used with legicload */ | |
e7d099dc | 319 | if (requested == 0) |
52cf34c1 | 320 | requested = 1024; |
52cf34c1 | 321 | |
322 | if (requested % 8 != 0) { | |
323 | int remainder = requested % 8; | |
324 | requested = requested + 8 - remainder; | |
325 | } | |
326 | ||
327 | if (offset + requested > sizeof(got)) { | |
328 | PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024"); | |
329 | return 0; | |
330 | } | |
331 | ||
332 | FILE *f = fopen(filename, "w"); | |
333 | if(!f) { | |
334 | PrintAndLog("couldn't open '%s'", Cmd+1); | |
335 | return -1; | |
336 | } | |
337 | ||
d7fd9084 | 338 | GetFromBigBuf(got, requested, offset); |
339 | WaitForResponse(CMD_ACK, NULL); | |
52cf34c1 | 340 | |
341 | for (int j = 0; j < requested; j += 8) { | |
342 | fprintf(f, "%02x %02x %02x %02x %02x %02x %02x %02x\n", | |
c6e0a2eb | 343 | got[j+0], got[j+1], got[j+2], got[j+3], |
344 | got[j+4], got[j+5], got[j+6], got[j+7] | |
52cf34c1 | 345 | ); |
346 | delivered += 8; | |
347 | if (delivered >= requested) break; | |
348 | } | |
349 | ||
350 | fclose(f); | |
351 | PrintAndLog("saved %u samples", delivered); | |
352 | return 0; | |
3612a8a8 | 353 | } |
354 | ||
52cf34c1 | 355 | int CmdLegicRfSim(const char *Cmd) { |
3b920280 | 356 | UsbCommand c = {CMD_SIMULATE_TAG_LEGIC_RF, {6,3,0}}; |
52cf34c1 | 357 | sscanf(Cmd, " %"lli" %"lli" %"lli, &c.arg[0], &c.arg[1], &c.arg[2]); |
358 | clearCommandBuffer(); | |
359 | SendCommand(&c); | |
360 | return 0; | |
3612a8a8 | 361 | } |
362 | ||
52cf34c1 | 363 | int CmdLegicRfWrite(const char *Cmd) { |
364 | UsbCommand c = {CMD_WRITER_LEGIC_RF}; | |
125a98a1 | 365 | int res = sscanf(Cmd, " 0x%"llx" 0x%"llx, &c.arg[0], &c.arg[1]); |
3612a8a8 | 366 | if(res != 2) { |
367 | PrintAndLog("Please specify the offset and length as two hex strings"); | |
368 | return -1; | |
369 | } | |
52cf34c1 | 370 | clearCommandBuffer(); |
3612a8a8 | 371 | SendCommand(&c); |
372 | return 0; | |
373 | } | |
374 | ||
52cf34c1 | 375 | int CmdLegicRfFill(const char *Cmd) { |
376 | UsbCommand cmd = {CMD_WRITER_LEGIC_RF}; | |
1a07fd51 | 377 | int res = sscanf(Cmd, " 0x%"llx" 0x%"llx" 0x%"llx, &cmd.arg[0], &cmd.arg[1], &cmd.arg[2]); |
3612a8a8 | 378 | if(res != 3) { |
379 | PrintAndLog("Please specify the offset, length and value as two hex strings"); | |
380 | return -1; | |
381 | } | |
382 | ||
383 | int i; | |
52cf34c1 | 384 | UsbCommand c = {CMD_DOWNLOADED_SIM_SAMPLES_125K, {0, 0, 0}}; |
3612a8a8 | 385 | for(i = 0; i < 48; i++) { |
52cf34c1 | 386 | c.d.asBytes[i] = cmd.arg[2]; |
3612a8a8 | 387 | } |
52cf34c1 | 388 | |
389 | for(i = 0; i < 22; i++) { | |
390 | c.arg[0] = i*48; | |
391 | SendCommand(&c); | |
392 | WaitForResponse(CMD_ACK,NULL); | |
393 | } | |
394 | clearCommandBuffer(); | |
3612a8a8 | 395 | SendCommand(&cmd); |
396 | return 0; | |
397 | } | |
398 | ||
3b920280 | 399 | int CmdLegicCalcCrc8(const char *Cmd){ |
400 | ||
401 | int len = strlen(Cmd); | |
402 | if (len & 1 ) return usage_legic_calccrc8(); | |
403 | ||
404 | uint8_t *data = malloc(len); | |
405 | if ( data == NULL ) return 1; | |
406 | ||
407 | param_gethex(Cmd, 0, data, len ); | |
408 | ||
409 | uint32_t checksum = CRC8Legic(data, len/2); | |
410 | PrintAndLog("Bytes: %s || CRC8: %X", sprint_hex(data, len/2), checksum ); | |
411 | free(data); | |
412 | return 0; | |
413 | } | |
414 | ||
52cf34c1 | 415 | static command_t CommandTable[] = { |
3b920280 | 416 | {"help", CmdHelp, 1, "This help"}, |
417 | {"decode", CmdLegicDecode, 0, "Display deobfuscated and decoded LEGIC RF tag data (use after hf legic reader)"}, | |
418 | {"read", CmdLegicRFRead, 0, "[offset][length] -- read bytes from a LEGIC card"}, | |
419 | {"save", CmdLegicSave, 0, "<filename> [<length>] -- Store samples"}, | |
420 | {"load", CmdLegicLoad, 0, "<filename> -- Restore samples"}, | |
421 | {"sim", CmdLegicRfSim, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"}, | |
422 | {"write", CmdLegicRfWrite,0, "<offset> <length> -- Write sample buffer (user after load or read)"}, | |
423 | {"fill", CmdLegicRfFill, 0, "<offset> <length> <value> -- Fill/Write tag with constant value"}, | |
424 | {"crc8", CmdLegicCalcCrc8, 1, "Calculate Legic CRC8 over given hexbytes"}, | |
52cf34c1 | 425 | {NULL, NULL, 0, NULL} |
426 | }; | |
427 | ||
428 | int CmdHFLegic(const char *Cmd) { | |
429 | clearCommandBuffer(); | |
430 | CmdsParse(CommandTable, Cmd); | |
431 | return 0; | |
432 | } | |
433 | ||
434 | int CmdHelp(const char *Cmd) { | |
435 | CmdsHelp(CommandTable); | |
436 | return 0; | |
437 | } |