]>
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 | ||
f6e01a34 | 16 | int usage_legic_calccrc(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."); |
f6e01a34 | 19 | PrintAndLog("Usage: hf legic crc [h] b <hexbytes> u <uidcrc> c <crc type>"); |
514ddaa2 | 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:"); |
f6e01a34 | 27 | PrintAndLog(" hf legic crc b deadbeef1122"); |
28 | PrintAndLog(" hf legic crc 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 | } | |
00271f77 | 42 | int usage_legic_rdmem(void){ |
cbdcc89a | 43 | PrintAndLog("Read data from a legic tag."); |
00271f77 | 44 | PrintAndLog("Usage: hf legic rdmem [h] <offset> <length> <IV>"); |
514ddaa2 | 45 | PrintAndLog("Options:"); |
ffa306de | 46 | PrintAndLog(" h : this help"); |
86087eba | 47 | PrintAndLog(" <offset> : offset in data array to start download from (hex)"); |
48 | PrintAndLog(" <length> : number of bytes to read (hex)"); | |
49 | PrintAndLog(" <IV> : (optional) Initialization vector to use (hex, odd and 7bits)"); | |
cbdcc89a | 50 | PrintAndLog(""); |
514ddaa2 | 51 | PrintAndLog("Samples:"); |
00271f77 | 52 | PrintAndLog(" hf legic rdmem 0 21 - reads from byte[0] 21 bytes(system header)"); |
53 | PrintAndLog(" hf legic rdmem 0 4 55 - reads from byte[0] 4 bytes with IV 0x55"); | |
54 | PrintAndLog(" hf legic rdmem 0 100 55 - reads 256bytes with IV 0x55"); | |
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)"); | |
f0fa6638 | 63 | PrintAndLog("Usage: hf legic write [h] o <offset> d <data (hex symbols)>"); |
ffa306de | 64 | PrintAndLog("Options:"); |
65 | PrintAndLog(" h : this help"); | |
f0fa6638 | 66 | PrintAndLog(" o <offset> : offset in data array to start writing"); |
67 | //PrintAndLog(" <IV> : (optional) Initialization vector to use (ODD and 7bits)"); | |
68 | PrintAndLog(" d <data> : bytes to write (hex symbols)"); | |
ffa306de | 69 | PrintAndLog(""); |
70 | PrintAndLog("Samples:"); | |
f0fa6638 | 71 | PrintAndLog(" hf legic write o 10 d 11223344 - Write 0x11223344 starting from offset 0x10"); |
e8fecd72 | 72 | return 0; |
73 | } | |
633d0686 | 74 | int usage_legic_reader(void){ |
75 | PrintAndLog("Read UID and type information from a legic tag."); | |
76 | PrintAndLog("Usage: hf legic reader [h]"); | |
77 | PrintAndLog("Options:"); | |
78 | PrintAndLog(" h : this help"); | |
79 | PrintAndLog(""); | |
80 | PrintAndLog("Samples:"); | |
81 | PrintAndLog(" hf legic reader"); | |
82 | return 0; | |
83 | } | |
3e750be3 | 84 | int usage_legic_info(void){ |
633d0686 | 85 | PrintAndLog("Reads information from a legic prime tag."); |
86 | PrintAndLog("Shows systemarea, user areas etc"); | |
3e750be3 | 87 | PrintAndLog("Usage: hf legic info [h]"); |
88 | PrintAndLog("Options:"); | |
89 | PrintAndLog(" h : this help"); | |
90 | PrintAndLog(""); | |
91 | PrintAndLog("Samples:"); | |
92 | PrintAndLog(" hf legic info"); | |
93 | return 0; | |
94 | } | |
633d0686 | 95 | int usage_legic_dump(void){ |
96 | PrintAndLog("Reads all pages from LEGIC MIM22, MIM256, MIM1024"); | |
97 | PrintAndLog("and saves binary dump into the file `filename.bin` or `cardUID.bin`"); | |
98 | PrintAndLog("It autodetects card type.\n"); | |
99 | PrintAndLog("Usage: hf legic dump [h] o <filename w/o .bin>"); | |
100 | PrintAndLog("Options:"); | |
101 | PrintAndLog(" h : this help"); | |
102 | PrintAndLog(" n <FN> : filename w/o .bin to save the dump as"); | |
103 | PrintAndLog(""); | |
104 | PrintAndLog("Samples:"); | |
105 | PrintAndLog(" hf legic dump"); | |
106 | PrintAndLog(" hf legic dump o myfile"); | |
107 | return 0; | |
108 | } | |
0e8cabed | 109 | int usage_legic_eload(void){ |
110 | PrintAndLog("It loads binary dump from the file `filename.bin`"); | |
111 | PrintAndLog("Usage: hf legic eload [h] [card memory] <file name w/o `.bin`>"); | |
112 | PrintAndLog("Options:"); | |
113 | PrintAndLog(" h : this help"); | |
114 | PrintAndLog(" [card memory] : 0 = MIM22"); | |
115 | PrintAndLog(" : 1 = MIM256 (default)"); | |
116 | PrintAndLog(" : 2 = MIM1024"); | |
117 | PrintAndLog(" <filename> : filename w/o .bin to load"); | |
118 | PrintAndLog(""); | |
119 | PrintAndLog("Samples:"); | |
120 | PrintAndLog(" hf legic eload filename"); | |
121 | PrintAndLog(" hf legic eload 2 filename"); | |
122 | return 0; | |
123 | } | |
124 | int usage_legic_esave(void){ | |
125 | PrintAndLog("It saves binary dump into the file `filename.bin` or `cardID.bin`"); | |
126 | PrintAndLog(" Usage: hf legic esave [h] [card memory] [file name w/o `.bin`]"); | |
127 | PrintAndLog("Options:"); | |
128 | PrintAndLog(" h : this help"); | |
129 | PrintAndLog(" [card memory] : 0 = MIM22"); | |
130 | PrintAndLog(" : 1 = MIM256 (default)"); | |
131 | PrintAndLog(" : 2 = MIM1024"); | |
132 | PrintAndLog(" <filename> : filename w/o .bin to load"); | |
133 | PrintAndLog(""); | |
134 | PrintAndLog("Samples:"); | |
135 | PrintAndLog(" hf legic esave "); | |
136 | PrintAndLog(" hf legic esave 2"); | |
137 | PrintAndLog(" hf legic esave 2 filename"); | |
138 | return 0; | |
139 | } | |
140 | ||
669c1b80 | 141 | /* |
142 | * Output BigBuf and deobfuscate LEGIC RF tag data. | |
cbdcc89a | 143 | * This is based on information given in the talk held |
669c1b80 | 144 | * by Henryk Ploetz and Karsten Nohl at 26c3 |
669c1b80 | 145 | */ |
633d0686 | 146 | int CmdLegicInfo(const char *Cmd) { |
147 | ||
148 | char cmdp = param_getchar(Cmd, 0); | |
149 | if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_info(); | |
e8fecd72 | 150 | |
151 | int i = 0, k = 0, segmentNum = 0, segment_len = 0, segment_flag = 0; | |
152 | int crc = 0, wrp = 0, wrc = 0; | |
60bb5ef7 | 153 | uint8_t stamp_len = 0; |
77a689db | 154 | uint8_t data[1024]; // receiver buffer |
e8fecd72 | 155 | char token_type[5] = {0,0,0,0,0}; |
156 | int dcf = 0; | |
3e134b4c | 157 | int bIsSegmented = 0; |
52cf34c1 | 158 | |
9015ae0f | 159 | CmdLegicRdmem("0 22 55"); |
633d0686 | 160 | |
77a689db | 161 | // copy data from device |
162 | GetEMLFromBigBuf(data, sizeof(data), 0); | |
f7f844d0 | 163 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000)){ |
164 | PrintAndLog("Command execute timeout"); | |
165 | return 1; | |
166 | } | |
167 | ||
52cf34c1 | 168 | // Output CDF System area (9 bytes) plus remaining header area (12 bytes) |
77a689db | 169 | crc = data[4]; |
170 | uint32_t calc_crc = CRC8Legic(data, 4); | |
3b920280 | 171 | |
52cf34c1 | 172 | PrintAndLog("\nCDF: System Area"); |
aacb96d7 | 173 | PrintAndLog("------------------------------------------------------"); |
3b920280 | 174 | PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x %s", |
77a689db | 175 | data[0], |
176 | data[1], | |
177 | data[2], | |
178 | data[3], | |
179 | data[4], | |
180 | (calc_crc == crc) ? "OK":"Fail" | |
52cf34c1 | 181 | ); |
669c1b80 | 182 | |
0e8cabed | 183 | // MCD = Manufacturer ID (should be list meaning something?) |
3e134b4c | 184 | |
185 | token_type[0] = 0; | |
77a689db | 186 | dcf = ((int)data[6] << 8) | (int)data[5]; |
3e134b4c | 187 | |
188 | // New unwritten media? | |
189 | if(dcf == 0xFFFF) { | |
190 | ||
191 | PrintAndLog("DCF: %d (%02x %02x), Token Type=NM (New Media)", | |
192 | dcf, | |
77a689db | 193 | data[5], |
194 | data[6] | |
3e134b4c | 195 | ); |
196 | ||
197 | } else if(dcf > 60000) { // Master token? | |
198 | ||
199 | int fl = 0; | |
200 | ||
77a689db | 201 | if(data[6] == 0xec) { |
3e134b4c | 202 | strncpy(token_type, "XAM", sizeof(token_type)); |
203 | fl = 1; | |
77a689db | 204 | stamp_len = 0x0c - (data[5] >> 4); |
3e134b4c | 205 | } else { |
77a689db | 206 | switch (data[5] & 0x7f) { |
e8fecd72 | 207 | case 0x00 ... 0x2f: |
208 | strncpy(token_type, "IAM", sizeof(token_type)); | |
77a689db | 209 | fl = (0x2f - (data[5] & 0x7f)) + 1; |
e8fecd72 | 210 | break; |
211 | case 0x30 ... 0x6f: | |
212 | strncpy(token_type, "SAM", sizeof(token_type)); | |
77a689db | 213 | fl = (0x6f - (data[5] & 0x7f)) + 1; |
e8fecd72 | 214 | break; |
215 | case 0x70 ... 0x7f: | |
216 | strncpy(token_type, "GAM", sizeof(token_type)); | |
77a689db | 217 | fl = (0x7f - (data[5] & 0x7f)) + 1; |
e8fecd72 | 218 | break; |
219 | } | |
52cf34c1 | 220 | |
77a689db | 221 | stamp_len = 0xfc - data[6]; |
3e134b4c | 222 | } |
52cf34c1 | 223 | |
3e134b4c | 224 | PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u), OL=%02u, FL=%02u", |
225 | dcf, | |
77a689db | 226 | data[5], |
227 | data[6], | |
e8fecd72 | 228 | token_type, |
77a689db | 229 | (data[5] & 0x80 )>> 7, |
3e134b4c | 230 | stamp_len, |
231 | fl | |
e8fecd72 | 232 | ); |
52cf34c1 | 233 | |
e8fecd72 | 234 | } else { // Is IM(-S) type of card... |
3e134b4c | 235 | |
77a689db | 236 | if(data[7] == 0x9F && data[8] == 0xFF) { |
3e134b4c | 237 | bIsSegmented = 1; |
238 | strncpy(token_type, "IM-S", sizeof(token_type)); | |
239 | } else { | |
240 | strncpy(token_type, "IM", sizeof(token_type)); | |
241 | } | |
242 | ||
243 | PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u)", | |
244 | dcf, | |
77a689db | 245 | data[5], |
246 | data[6], | |
3e134b4c | 247 | token_type, |
77a689db | 248 | (data[5]&0x80) >> 7 |
3e134b4c | 249 | ); |
250 | } | |
251 | ||
252 | // Makes no sence to show this on blank media... | |
253 | if(dcf != 0xFFFF) { | |
254 | ||
255 | if(bIsSegmented) { | |
256 | PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, SSC=%02x", | |
77a689db | 257 | data[7] & 0x0f, |
258 | (data[7] & 0x70) >> 4, | |
259 | (data[7] & 0x80) >> 7, | |
260 | data[8] | |
e8fecd72 | 261 | ); |
3e134b4c | 262 | } |
52cf34c1 | 263 | |
3e134b4c | 264 | // Header area is only available on IM-S cards, on master tokens this data is the master token data itself |
265 | if(bIsSegmented || dcf > 60000) { | |
266 | if(dcf > 60000) { | |
267 | PrintAndLog("Master token data"); | |
77a689db | 268 | PrintAndLog("%s", sprint_hex(data+8, 14)); |
3e134b4c | 269 | } else { |
e8fecd72 | 270 | PrintAndLog("Remaining Header Area"); |
77a689db | 271 | PrintAndLog("%s", sprint_hex(data+9, 13)); |
3e134b4c | 272 | } |
273 | } | |
274 | } | |
d7fd9084 | 275 | |
e8fecd72 | 276 | uint8_t segCrcBytes[8] = {0,0,0,0,0,0,0,0}; |
e579e768 | 277 | uint32_t segCalcCRC = 0; |
278 | uint32_t segCRC = 0; | |
d7fd9084 | 279 | |
3e134b4c | 280 | // Data card? |
281 | if(dcf <= 60000) { | |
cbdcc89a | 282 | |
e8fecd72 | 283 | PrintAndLog("\nADF: User Area"); |
284 | PrintAndLog("------------------------------------------------------"); | |
3e134b4c | 285 | |
286 | if(bIsSegmented) { | |
287 | ||
288 | // Data start point on segmented cards | |
e8fecd72 | 289 | i = 22; |
3e134b4c | 290 | |
291 | // decode segments | |
292 | for (segmentNum=1; segmentNum < 128; segmentNum++ ) | |
293 | { | |
77a689db | 294 | segment_len = ((data[i+1] ^ crc) & 0x0f) * 256 + (data[i] ^ crc); |
295 | segment_flag = ((data[i+1] ^ crc) & 0xf0) >> 4; | |
296 | wrp = (data[i+2] ^ crc); | |
297 | wrc = ((data[i+3] ^ crc) & 0x70) >> 4; | |
e8fecd72 | 298 | |
299 | bool hasWRC = (wrc > 0); | |
300 | bool hasWRP = (wrp > wrc); | |
301 | int wrp_len = (wrp - wrc); | |
302 | int remain_seg_payload_len = (segment_len - wrp - 5); | |
e579e768 | 303 | |
e8fecd72 | 304 | // validate segment-crc |
77a689db | 305 | segCrcBytes[0]=data[0]; //uid0 |
306 | segCrcBytes[1]=data[1]; //uid1 | |
307 | segCrcBytes[2]=data[2]; //uid2 | |
308 | segCrcBytes[3]=data[3]; //uid3 | |
309 | segCrcBytes[4]=(data[i] ^ crc); //hdr0 | |
310 | segCrcBytes[5]=(data[i+1] ^ crc); //hdr1 | |
311 | segCrcBytes[6]=(data[i+2] ^ crc); //hdr2 | |
312 | segCrcBytes[7]=(data[i+3] ^ crc); //hdr3 | |
e8fecd72 | 313 | |
314 | segCalcCRC = CRC8Legic(segCrcBytes, 8); | |
77a689db | 315 | segCRC = data[i+4] ^ crc; |
e8fecd72 | 316 | |
317 | 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)", | |
318 | segmentNum, | |
77a689db | 319 | data[i] ^ crc, |
320 | data[i+1] ^ crc, | |
321 | data[i+2] ^ crc, | |
322 | data[i+3] ^ crc, | |
e8fecd72 | 323 | segment_len, |
324 | segment_flag, | |
325 | (segment_flag & 0x4) >> 2, | |
326 | (segment_flag & 0x8) >> 3, | |
327 | wrp, | |
328 | wrc, | |
77a689db | 329 | ((data[i+3]^crc) & 0x80) >> 7, |
e8fecd72 | 330 | segCRC, |
331 | ( segCRC == segCalcCRC ) ? "OK" : "fail" | |
332 | ); | |
333 | ||
334 | i += 5; | |
669c1b80 | 335 | |
e8fecd72 | 336 | if ( hasWRC ) { |
337 | PrintAndLog("WRC protected area: (I %d | K %d| WRC %d)", i, k, wrc); | |
338 | PrintAndLog("\nrow | data"); | |
339 | PrintAndLog("-----+------------------------------------------------"); | |
3e134b4c | 340 | |
26778ea7 | 341 | for ( k=i; k < (i + wrc); ++k) |
77a689db | 342 | data[k] ^= crc; |
3e134b4c | 343 | |
77a689db | 344 | print_hex_break( data+i, wrc, 16); |
3b920280 | 345 | |
e8fecd72 | 346 | i += wrc; |
347 | } | |
669c1b80 | 348 | |
e8fecd72 | 349 | if ( hasWRP ) { |
350 | PrintAndLog("Remaining write protected area: (I %d | K %d | WRC %d | WRP %d WRP_LEN %d)",i, k, wrc, wrp, wrp_len); | |
351 | PrintAndLog("\nrow | data"); | |
352 | PrintAndLog("-----+------------------------------------------------"); | |
d7fd9084 | 353 | |
3e134b4c | 354 | for (k=i; k < (i+wrp_len); ++k) |
77a689db | 355 | data[k] ^= crc; |
3b920280 | 356 | |
77a689db | 357 | print_hex_break( data+i, wrp_len, 16); |
3b920280 | 358 | |
e8fecd72 | 359 | i += wrp_len; |
d7fd9084 | 360 | |
3e134b4c | 361 | // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...) |
e8fecd72 | 362 | if( wrp_len == 8 ) |
77a689db | 363 | PrintAndLog("Card ID: %2X%02X%02X", data[i-4]^crc, data[i-3]^crc, data[i-2]^crc); |
e8fecd72 | 364 | } |
669c1b80 | 365 | |
e8fecd72 | 366 | PrintAndLog("Remaining segment payload: (I %d | K %d | Remain LEN %d)", i, k, remain_seg_payload_len); |
367 | PrintAndLog("\nrow | data"); | |
368 | PrintAndLog("-----+------------------------------------------------"); | |
3e134b4c | 369 | |
370 | for ( k=i; k < (i+remain_seg_payload_len); ++k) | |
77a689db | 371 | data[k] ^= crc; |
3b920280 | 372 | |
77a689db | 373 | print_hex_break( data+i, remain_seg_payload_len, 16); |
669c1b80 | 374 | |
e8fecd72 | 375 | i += remain_seg_payload_len; |
d7fd9084 | 376 | |
e8fecd72 | 377 | PrintAndLog("-----+------------------------------------------------\n"); |
a182a680 | 378 | |
e8fecd72 | 379 | // end with last segment |
380 | if (segment_flag & 0x8) return 0; | |
52cf34c1 | 381 | |
e8fecd72 | 382 | } // end for loop |
3e134b4c | 383 | |
384 | } else { | |
385 | ||
386 | // Data start point on unsegmented cards | |
387 | i = 8; | |
388 | ||
77a689db | 389 | wrp = data[7] & 0x0F; |
390 | wrc = (data[7] & 0x70) >> 4; | |
3e134b4c | 391 | |
392 | bool hasWRC = (wrc > 0); | |
393 | bool hasWRP = (wrp > wrc); | |
394 | int wrp_len = (wrp - wrc); | |
395 | int remain_seg_payload_len = (1024 - 22 - wrp); // Any chance to get physical card size here!? | |
396 | ||
397 | PrintAndLog("Unsegmented card - WRP: %02u, WRC: %02u, RD: %01u", | |
398 | wrp, | |
399 | wrc, | |
77a689db | 400 | (data[7] & 0x80) >> 7 |
3e134b4c | 401 | ); |
402 | ||
403 | if ( hasWRC ) { | |
404 | PrintAndLog("WRC protected area: (I %d | WRC %d)", i, wrc); | |
405 | PrintAndLog("\nrow | data"); | |
406 | PrintAndLog("-----+------------------------------------------------"); | |
77a689db | 407 | print_hex_break( data+i, wrc, 16); |
3e134b4c | 408 | i += wrc; |
409 | } | |
410 | ||
411 | if ( hasWRP ) { | |
412 | PrintAndLog("Remaining write protected area: (I %d | WRC %d | WRP %d | WRP_LEN %d)", i, wrc, wrp, wrp_len); | |
413 | PrintAndLog("\nrow | data"); | |
414 | PrintAndLog("-----+------------------------------------------------"); | |
77a689db | 415 | print_hex_break( data + i, wrp_len, 16); |
3e134b4c | 416 | i += wrp_len; |
417 | ||
418 | // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...) | |
419 | if( wrp_len == 8 ) | |
77a689db | 420 | PrintAndLog("Card ID: %2X%02X%02X", data[i-4], data[i-3], data[i-2]); |
3e134b4c | 421 | } |
422 | ||
423 | PrintAndLog("Remaining segment payload: (I %d | Remain LEN %d)", i, remain_seg_payload_len); | |
424 | PrintAndLog("\nrow | data"); | |
425 | PrintAndLog("-----+------------------------------------------------"); | |
77a689db | 426 | print_hex_break( data + i, remain_seg_payload_len, 16); |
3e134b4c | 427 | i += remain_seg_payload_len; |
428 | ||
429 | PrintAndLog("-----+------------------------------------------------\n"); | |
430 | } | |
431 | } | |
52cf34c1 | 432 | return 0; |
669c1b80 | 433 | } |
41dab153 | 434 | |
77e1bab9 | 435 | // params: |
436 | // offset in data memory | |
437 | // number of bytes to read | |
00271f77 | 438 | int CmdLegicRdmem(const char *Cmd) { |
ffa306de | 439 | |
cbdcc89a | 440 | char cmdp = param_getchar(Cmd, 0); |
00271f77 | 441 | if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_rdmem(); |
cbdcc89a | 442 | |
b98827ff | 443 | uint32_t offset = 0, len = 0, IV = 1; |
ffa306de | 444 | sscanf(Cmd, "%x %x %x", &offset, &len, &IV); |
52cf34c1 | 445 | |
ffa306de | 446 | // OUT-OF-BOUNDS check |
a3994421 | 447 | if ( len + offset > MAX_LENGTH ) { |
448 | len = MAX_LENGTH - offset; | |
e1a0ed65 | 449 | PrintAndLog("Out-of-bound, shorten len to %d (0x%02X)", len, len); |
a3994421 | 450 | } |
ffa306de | 451 | |
0e8cabed | 452 | legic_chk_iv(&IV); |
ffa306de | 453 | |
ad5bc8cc | 454 | UsbCommand c = {CMD_READER_LEGIC_RF, {offset, len, IV}}; |
52cf34c1 | 455 | clearCommandBuffer(); |
456 | SendCommand(&c); | |
ad5bc8cc | 457 | UsbCommand resp; |
77e1bab9 | 458 | if ( !WaitForResponseTimeout(CMD_ACK, &resp, 3000) ) { |
459 | PrintAndLog("command execution time out"); | |
460 | return 1; | |
461 | } | |
c649c433 | 462 | |
77e1bab9 | 463 | uint8_t isOK = resp.arg[0] & 0xFF; |
464 | uint16_t readlen = resp.arg[1]; | |
465 | if ( !isOK ) { | |
466 | PrintAndLog("failed reading tag"); | |
467 | return 2; | |
468 | } | |
469 | ||
470 | uint8_t *data = malloc(readlen); | |
471 | if ( !data ){ | |
472 | PrintAndLog("Cannot allocate memory"); | |
473 | return 2; | |
474 | } | |
86087eba | 475 | |
77e1bab9 | 476 | if ( readlen != len ) |
477 | PrintAndLog("Fail, only managed to read 0x%02X bytes", readlen); | |
86087eba | 478 | |
77e1bab9 | 479 | // copy data from device |
480 | GetEMLFromBigBuf(data, readlen, 0); | |
481 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500)){ | |
482 | PrintAndLog("Command execute timeout"); | |
483 | free(data); | |
ad5bc8cc | 484 | return 1; |
485 | } | |
77e1bab9 | 486 | |
487 | PrintAndLog("\n ## | Data"); | |
488 | PrintAndLog("-----+-----"); | |
489 | print_hex_break( data, readlen, 32); | |
490 | free(data); | |
52cf34c1 | 491 | return 0; |
41dab153 | 492 | } |
3612a8a8 | 493 | |
0e8cabed | 494 | // load, filename (ascii hex textfile) |
495 | // uploads it to device mem | |
52cf34c1 | 496 | int CmdLegicLoad(const char *Cmd) { |
e8fecd72 | 497 | |
498 | // iceman: potential bug, where all filepaths or filename which starts with H or h will print the helptext :) | |
52cf34c1 | 499 | char cmdp = param_getchar(Cmd, 0); |
e579e768 | 500 | if ( cmdp == 'H' || cmdp == 'h' || cmdp == 0x00) return usage_legic_load(); |
b915fda3 | 501 | |
e579e768 | 502 | char filename[FILE_PATH_SIZE] = {0x00}; |
503 | int len = strlen(Cmd); | |
504 | ||
b915fda3 | 505 | if (len > FILE_PATH_SIZE) { |
506 | PrintAndLog("Filepath too long (was %s bytes), max allowed is %s ", len, FILE_PATH_SIZE); | |
507 | return 0; | |
508 | } | |
509 | memcpy(filename, Cmd, len); | |
510 | ||
511 | FILE *f = fopen(filename, "r"); | |
3612a8a8 | 512 | if(!f) { |
513 | PrintAndLog("couldn't open '%s'", Cmd); | |
514 | return -1; | |
515 | } | |
52cf34c1 | 516 | |
517 | char line[80]; | |
810f5379 | 518 | int offset = 0; |
c6e0a2eb | 519 | uint8_t data[USB_CMD_DATA_SIZE] = {0x00}; |
520 | int index = 0; | |
521 | int totalbytes = 0; | |
52cf34c1 | 522 | while ( fgets(line, sizeof(line), f) ) { |
3612a8a8 | 523 | int res = sscanf(line, "%x %x %x %x %x %x %x %x", |
c6e0a2eb | 524 | (unsigned int *)&data[index], |
525 | (unsigned int *)&data[index + 1], | |
526 | (unsigned int *)&data[index + 2], | |
527 | (unsigned int *)&data[index + 3], | |
528 | (unsigned int *)&data[index + 4], | |
529 | (unsigned int *)&data[index + 5], | |
530 | (unsigned int *)&data[index + 6], | |
531 | (unsigned int *)&data[index + 7]); | |
e7d099dc | 532 | |
3612a8a8 | 533 | if(res != 8) { |
534 | PrintAndLog("Error: could not read samples"); | |
535 | fclose(f); | |
536 | return -1; | |
537 | } | |
c6e0a2eb | 538 | index += res; |
539 | ||
540 | if ( index == USB_CMD_DATA_SIZE ){ | |
c6e0a2eb | 541 | UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}}; |
542 | memcpy(c.d.asBytes, data, sizeof(data)); | |
543 | clearCommandBuffer(); | |
544 | SendCommand(&c); | |
d7fd9084 | 545 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){ |
546 | PrintAndLog("Command execute timeout"); | |
547 | fclose(f); | |
548 | return 1; | |
549 | } | |
c6e0a2eb | 550 | offset += index; |
551 | totalbytes += index; | |
552 | index = 0; | |
553 | } | |
3612a8a8 | 554 | } |
555 | fclose(f); | |
c6e0a2eb | 556 | |
557 | // left over bytes? | |
558 | if ( index != 0 ) { | |
559 | UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}}; | |
560 | memcpy(c.d.asBytes, data, 8); | |
561 | clearCommandBuffer(); | |
562 | SendCommand(&c); | |
d7fd9084 | 563 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){ |
564 | PrintAndLog("Command execute timeout"); | |
565 | return 1; | |
566 | } | |
c6e0a2eb | 567 | totalbytes += index; |
568 | } | |
569 | ||
570 | PrintAndLog("loaded %u samples", totalbytes); | |
3612a8a8 | 571 | return 0; |
572 | } | |
573 | ||
0e8cabed | 574 | // Save, filename, num of bytes, starting offset. (in decimal) |
575 | // ie: "hf legic save nnn.txt 100 0 (saves the first 100bytes) | |
576 | // (ascii hex textfile) | |
52cf34c1 | 577 | int CmdLegicSave(const char *Cmd) { |
578 | int requested = 1024; | |
579 | int offset = 0; | |
580 | int delivered = 0; | |
e8fecd72 | 581 | char filename[FILE_PATH_SIZE] = {0x00}; |
52cf34c1 | 582 | uint8_t got[1024] = {0x00}; |
583 | ||
e8fecd72 | 584 | memset(filename, 0, FILE_PATH_SIZE); |
585 | ||
52cf34c1 | 586 | sscanf(Cmd, " %s %i %i", filename, &requested, &offset); |
587 | ||
588 | /* If no length given save entire legic read buffer */ | |
589 | /* round up to nearest 8 bytes so the saved data can be used with legicload */ | |
e7d099dc | 590 | if (requested == 0) |
52cf34c1 | 591 | requested = 1024; |
52cf34c1 | 592 | |
593 | if (requested % 8 != 0) { | |
594 | int remainder = requested % 8; | |
595 | requested = requested + 8 - remainder; | |
596 | } | |
597 | ||
598 | if (offset + requested > sizeof(got)) { | |
599 | PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024"); | |
600 | return 0; | |
601 | } | |
602 | ||
d7fd9084 | 603 | GetFromBigBuf(got, requested, offset); |
f7f844d0 | 604 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000)){ |
aacb96d7 | 605 | PrintAndLog("Command execute timeout"); |
f7f844d0 | 606 | return 1; |
607 | } | |
52cf34c1 | 608 | |
aacb96d7 | 609 | FILE *f = fopen(filename, "w"); |
610 | if(!f) { | |
611 | PrintAndLog("couldn't open '%s'", Cmd+1); | |
612 | return -1; | |
613 | } | |
614 | ||
52cf34c1 | 615 | for (int j = 0; j < requested; j += 8) { |
616 | fprintf(f, "%02x %02x %02x %02x %02x %02x %02x %02x\n", | |
c6e0a2eb | 617 | got[j+0], got[j+1], got[j+2], got[j+3], |
618 | got[j+4], got[j+5], got[j+6], got[j+7] | |
52cf34c1 | 619 | ); |
620 | delivered += 8; | |
621 | if (delivered >= requested) break; | |
622 | } | |
623 | ||
624 | fclose(f); | |
625 | PrintAndLog("saved %u samples", delivered); | |
626 | return 0; | |
3612a8a8 | 627 | } |
628 | ||
f7f844d0 | 629 | //TODO: write a help text (iceman) |
0e8cabed | 630 | // should say which tagtype |
631 | // should load a tag to device mem. | |
52cf34c1 | 632 | int CmdLegicRfSim(const char *Cmd) { |
3b920280 | 633 | UsbCommand c = {CMD_SIMULATE_TAG_LEGIC_RF, {6,3,0}}; |
52cf34c1 | 634 | sscanf(Cmd, " %"lli" %"lli" %"lli, &c.arg[0], &c.arg[1], &c.arg[2]); |
635 | clearCommandBuffer(); | |
636 | SendCommand(&c); | |
637 | return 0; | |
3612a8a8 | 638 | } |
639 | ||
52cf34c1 | 640 | int CmdLegicRfWrite(const char *Cmd) { |
ffa306de | 641 | |
f0fa6638 | 642 | uint8_t *data = NULL; |
643 | uint8_t cmdp = 0; | |
644 | bool errors = false; | |
645 | int len = 0, bg, en; | |
646 | uint32_t offset = 0, IV = 0x55; | |
0e8cabed | 647 | |
f0fa6638 | 648 | while(param_getchar(Cmd, cmdp) != 0x00) { |
649 | switch(param_getchar(Cmd, cmdp)) { | |
650 | case 'd': | |
651 | case 'D': | |
652 | // peek at length of the input string so we can | |
653 | // figure out how many elements to malloc in "data" | |
654 | bg=en=0; | |
655 | if (param_getptr(Cmd, &bg, &en, cmdp+1)) { | |
656 | errors = true; | |
657 | break; | |
658 | } | |
659 | len = (en - bg + 1); | |
660 | ||
661 | // check that user entered even number of characters | |
662 | // for hex data string | |
663 | if (len & 1) { | |
664 | errors = true; | |
665 | break; | |
666 | } | |
667 | ||
668 | // it's possible for user to accidentally enter "b" parameter | |
669 | // more than once - we have to clean previous malloc | |
670 | if (data) | |
671 | free(data); | |
672 | data = malloc(len >> 1); | |
673 | if ( data == NULL ) { | |
674 | PrintAndLog("Can't allocate memory. exiting"); | |
675 | errors = true; | |
676 | break; | |
677 | } | |
678 | ||
679 | if (param_gethex(Cmd, cmdp+1, data, len)) { | |
680 | errors = true; | |
681 | break; | |
682 | } | |
683 | ||
684 | len >>= 1; | |
685 | cmdp += 2; | |
686 | break; | |
687 | case 'o': | |
688 | case 'O': | |
689 | offset = param_get32ex(Cmd, cmdp+1, 4, 10); | |
690 | cmdp += 2; | |
691 | break; | |
692 | case 'h': | |
693 | case 'H': | |
694 | errors = true; | |
695 | break; | |
696 | default: | |
697 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
698 | errors = true; | |
699 | break; | |
700 | } | |
701 | if (errors) break; | |
702 | } | |
703 | //Validations | |
704 | if (errors){ | |
705 | if (data) | |
706 | free(data); | |
707 | return usage_legic_write(); | |
708 | } | |
709 | ||
0e8cabed | 710 | // tagtype |
711 | legic_card_select_t card; | |
712 | if (legic_get_type(&card)) { | |
713 | PrintAndLog("Failed to identify tagtype"); | |
714 | return -1; | |
715 | } | |
ffa306de | 716 | |
0e8cabed | 717 | legic_print_type(card.cardsize, 0); |
718 | ||
ffa306de | 719 | // OUT-OF-BOUNDS check |
f0fa6638 | 720 | // UID 4 bytes can't be written to. |
721 | if ( len + offset + 4 >= card.cardsize ) { | |
722 | PrintAndLog("Out-of-bounds, Cardsize = %d, [offset+len = %d ]", card.cardsize, len + offset + 4); | |
0e8cabed | 723 | return -2; |
b98827ff | 724 | } |
f0fa6638 | 725 | |
0e8cabed | 726 | legic_chk_iv(&IV); |
727 | ||
f0fa6638 | 728 | PrintAndLog("Writing to tag"); |
729 | UsbCommand c = {CMD_WRITER_LEGIC_RF, {offset, len, IV}}; | |
730 | memcpy(c.d.asBytes, data, len); | |
731 | ||
52cf34c1 | 732 | clearCommandBuffer(); |
3612a8a8 | 733 | SendCommand(&c); |
7bc3c99e | 734 | UsbCommand resp; |
b8168868 | 735 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 4000)) { |
7bc3c99e | 736 | PrintAndLog("command execution time out"); |
737 | return 1; | |
738 | } | |
f0fa6638 | 739 | uint8_t isOK = resp.arg[0] & 0xFF; |
740 | if ( !isOK ) { | |
741 | PrintAndLog("failed writing tag"); | |
742 | return 1; | |
743 | } | |
7bc3c99e | 744 | |
3612a8a8 | 745 | return 0; |
746 | } | |
747 | ||
f6e01a34 | 748 | /* |
3e134b4c | 749 | int CmdLegicRfRawWrite(const char *Cmd) { |
77e1bab9 | 750 | PrintAndLog("############# DANGER !! #############"); |
751 | PrintAndLog("# changing the DCF is irreversible #"); | |
752 | PrintAndLog("#####################################"); | |
753 | PrintAndLog("do youe really want to continue? y(es) n(o)"); | |
754 | // if (scanf(" %c", &answer) > 0 && (answer == 'y' || answer == 'Y')) { | |
755 | // return 0; | |
756 | // } | |
3e134b4c | 757 | return 0; |
758 | } | |
f6e01a34 | 759 | */ |
3e134b4c | 760 | |
f6e01a34 | 761 | int CmdLegicCalcCrc(const char *Cmd){ |
3b920280 | 762 | |
cc4c8fd6 | 763 | uint8_t *data = NULL; |
3e134b4c | 764 | uint8_t cmdp = 0, uidcrc = 0, type=0; |
765 | bool errors = false; | |
766 | int len = 0; | |
e31a0f73 | 767 | int bg, en; |
3b920280 | 768 | |
3e134b4c | 769 | while(param_getchar(Cmd, cmdp) != 0x00) { |
770 | switch(param_getchar(Cmd, cmdp)) { | |
771 | case 'b': | |
772 | case 'B': | |
e31a0f73 AG |
773 | // peek at length of the input string so we can |
774 | // figure out how many elements to malloc in "data" | |
775 | bg=en=0; | |
987c5984 AG |
776 | if (param_getptr(Cmd, &bg, &en, cmdp+1)) { |
777 | errors = true; | |
778 | break; | |
779 | } | |
e31a0f73 AG |
780 | len = (en - bg + 1); |
781 | ||
782 | // check that user entered even number of characters | |
783 | // for hex data string | |
784 | if (len & 1) { | |
785 | errors = true; | |
786 | break; | |
787 | } | |
788 | ||
08927081 AG |
789 | // it's possible for user to accidentally enter "b" parameter |
790 | // more than once - we have to clean previous malloc | |
791 | if (data) free(data); | |
e31a0f73 | 792 | data = malloc(len >> 1); |
3e134b4c | 793 | if ( data == NULL ) { |
794 | PrintAndLog("Can't allocate memory. exiting"); | |
795 | errors = true; | |
796 | break; | |
e31a0f73 AG |
797 | } |
798 | ||
987c5984 AG |
799 | if (param_gethex(Cmd, cmdp+1, data, len)) { |
800 | errors = true; | |
801 | break; | |
802 | } | |
3e134b4c | 803 | |
804 | len >>= 1; | |
805 | cmdp += 2; | |
806 | break; | |
807 | case 'u': | |
808 | case 'U': | |
809 | uidcrc = param_get8ex(Cmd, cmdp+1, 0, 16); | |
810 | cmdp += 2; | |
811 | break; | |
812 | case 'c': | |
813 | case 'C': | |
814 | type = param_get8ex(Cmd, cmdp+1, 0, 10); | |
815 | cmdp += 2; | |
816 | break; | |
817 | case 'h': | |
818 | case 'H': | |
819 | errors = true; | |
820 | break; | |
821 | default: | |
822 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
823 | errors = true; | |
824 | break; | |
825 | } | |
826 | if (errors) break; | |
827 | } | |
828 | //Validations | |
829 | if (errors){ | |
aeb128e2 | 830 | if (data) free(data); |
f6e01a34 | 831 | return usage_legic_calccrc(); |
3e134b4c | 832 | } |
833 | ||
834 | switch (type){ | |
835 | case 16: | |
87342aad | 836 | PrintAndLog("Legic crc16: %X", CRC16Legic(data, len, uidcrc)); |
837 | break; | |
3e134b4c | 838 | default: |
87342aad | 839 | PrintAndLog("Legic crc8: %X", CRC8Legic(data, len) ); |
3e134b4c | 840 | break; |
eb5206bd | 841 | } |
3b920280 | 842 | |
aeb128e2 | 843 | if (data) free(data); |
3b920280 | 844 | return 0; |
845 | } | |
3e750be3 | 846 | |
9015ae0f | 847 | int legic_print_type(uint32_t tagtype, uint8_t spaces){ |
848 | char spc[11] = " "; | |
849 | spc[10]=0x00; | |
850 | char *spacer = spc + (10-spaces); | |
851 | ||
852 | if ( tagtype == 22 ) | |
853 | PrintAndLog("%sTYPE : MIM%d card (outdated)", spacer, tagtype); | |
854 | else if ( tagtype == 256 ) | |
855 | PrintAndLog("%sTYPE : MIM%d card (234 bytes)", spacer, tagtype); | |
856 | else if ( tagtype == 1024 ) | |
857 | PrintAndLog("%sTYPE : MIM%d card (1002 bytes)", spacer, tagtype); | |
858 | else | |
859 | PrintAndLog("%sTYPE : Unknown %06x", spacer, tagtype); | |
860 | return 0; | |
861 | } | |
862 | int legic_get_type(legic_card_select_t *card){ | |
863 | ||
864 | if ( card == NULL ) return 1; | |
865 | ||
3e750be3 | 866 | UsbCommand c = {CMD_LEGIC_INFO, {0,0,0}}; |
867 | clearCommandBuffer(); | |
868 | SendCommand(&c); | |
869 | UsbCommand resp; | |
9015ae0f | 870 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 500)) |
871 | return 2; | |
a3994421 | 872 | |
873 | uint8_t isOK = resp.arg[0] & 0xFF; | |
9015ae0f | 874 | if ( !isOK ) |
875 | return 3; | |
876 | ||
877 | memcpy(card, (legic_card_select_t *)resp.d.asBytes, sizeof(legic_card_select_t)); | |
878 | return 0; | |
879 | } | |
0e8cabed | 880 | void legic_chk_iv(uint32_t *iv){ |
881 | if ( (*iv & 0x7F) != *iv ){ | |
882 | *iv &= 0x7F; | |
883 | PrintAndLog("Truncating IV to 7bits, %u", *iv); | |
884 | } | |
885 | // IV must be odd | |
886 | if ( (*iv & 1) == 0 ){ | |
887 | *iv |= 0x01; | |
888 | PrintAndLog("LSB of IV must be SET %u", *iv); | |
889 | } | |
890 | } | |
0e8cabed | 891 | void legic_seteml(uint8_t *src, uint32_t offset, uint32_t numofbytes) { |
0e8cabed | 892 | size_t len = 0; |
893 | UsbCommand c = {CMD_LEGIC_ESET, {0, 0, 0}}; | |
0e8cabed | 894 | for(size_t i = 0; i < numofbytes; i += USB_CMD_DATA_SIZE) { |
895 | ||
896 | len = MIN((numofbytes - i), USB_CMD_DATA_SIZE); | |
897 | c.arg[0] = i; // offset | |
898 | c.arg[1] = len; // number of bytes | |
77e1bab9 | 899 | memcpy(c.d.asBytes, src+i, len); |
0e8cabed | 900 | clearCommandBuffer(); |
901 | SendCommand(&c); | |
902 | PrintAndLog("ICE: offset %d | len %d", i, len); | |
903 | } | |
904 | } | |
9015ae0f | 905 | |
906 | int HFLegicReader(const char *Cmd, bool verbose) { | |
907 | ||
908 | char cmdp = param_getchar(Cmd, 0); | |
909 | if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_reader(); | |
a3994421 | 910 | |
911 | legic_card_select_t card; | |
9015ae0f | 912 | switch(legic_get_type(&card)){ |
913 | case 1: | |
914 | if ( verbose ) PrintAndLog("command execution time out"); | |
a3994421 | 915 | return 1; |
9015ae0f | 916 | case 2: |
917 | case 3: | |
918 | if ( verbose ) PrintAndLog("legic card select failed"); | |
919 | return 2; | |
920 | default: break; | |
921 | } | |
922 | PrintAndLog(" UID : %s", sprint_hex(card.uid, sizeof(card.uid))); | |
923 | legic_print_type(card.cardsize, 0); | |
3e750be3 | 924 | return 0; |
925 | } | |
633d0686 | 926 | int CmdLegicReader(const char *Cmd){ |
927 | return HFLegicReader(Cmd, TRUE); | |
3e750be3 | 928 | } |
633d0686 | 929 | |
930 | int CmdLegicDump(const char *Cmd){ | |
00271f77 | 931 | |
0e8cabed | 932 | FILE *f; |
00271f77 | 933 | char filename[FILE_PATH_SIZE] = {0x00}; |
934 | char *fnameptr = filename; | |
935 | size_t fileNlen = 0; | |
936 | bool errors = false; | |
9015ae0f | 937 | uint16_t dumplen; |
938 | uint8_t cmdp = 0; | |
00271f77 | 939 | |
0e8cabed | 940 | memset(filename, 0, sizeof(filename)); |
941 | ||
942 | while(param_getchar(Cmd, cmdp) != 0x00) { | |
943 | switch(param_getchar(Cmd, cmdp)) { | |
944 | case 'h': | |
945 | case 'H': | |
946 | return usage_legic_dump(); | |
947 | case 'o': | |
948 | case 'O': | |
949 | fileNlen = param_getstr(Cmd, cmdp+1, filename); | |
950 | if (!fileNlen) | |
951 | errors = true; | |
952 | if (fileNlen > FILE_PATH_SIZE-5) | |
953 | fileNlen = FILE_PATH_SIZE-5; | |
954 | cmdp += 2; | |
955 | break; | |
956 | default: | |
957 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
958 | errors = true; | |
959 | break; | |
00271f77 | 960 | } |
961 | if(errors) break; | |
962 | } | |
963 | ||
964 | //Validations | |
965 | if(errors) return usage_legic_dump(); | |
966 | ||
967 | // tagtype | |
9015ae0f | 968 | legic_card_select_t card; |
969 | if (legic_get_type(&card)) { | |
970 | PrintAndLog("Failed to identify tagtype"); | |
971 | return -1; | |
972 | } | |
973 | dumplen = card.cardsize; | |
00271f77 | 974 | |
9015ae0f | 975 | legic_print_type(dumplen, 0); |
976 | PrintAndLog("Reading tag memory..."); | |
977 | ||
00271f77 | 978 | UsbCommand c = {CMD_READER_LEGIC_RF, {0x00, dumplen, 0x55}}; |
979 | clearCommandBuffer(); | |
980 | SendCommand(&c); | |
981 | UsbCommand resp; | |
982 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 3000)) { | |
983 | PrintAndLog("Command execute time-out"); | |
984 | return 1; | |
985 | } | |
986 | ||
987 | uint8_t isOK = resp.arg[0] & 0xFF; | |
988 | if ( !isOK ) { | |
989 | PrintAndLog("Failed dumping tag data"); | |
990 | return 2; | |
991 | } | |
992 | ||
993 | uint16_t readlen = resp.arg[1]; | |
994 | uint8_t *data = malloc(readlen); | |
0e8cabed | 995 | if (!data) { |
00271f77 | 996 | PrintAndLog("Fail, cannot allocate memory"); |
997 | return 3; | |
998 | } | |
77e1bab9 | 999 | memset(data, 0, readlen); |
00271f77 | 1000 | |
1001 | if ( readlen != dumplen ) | |
1002 | PrintAndLog("Fail, only managed to read 0x%02X bytes of 0x%02X", readlen, dumplen); | |
1003 | ||
1004 | // copy data from device | |
1005 | GetEMLFromBigBuf(data, readlen, 0); | |
1006 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500)) { | |
1007 | PrintAndLog("Fail, transfer from device time-out"); | |
0e8cabed | 1008 | free(data); |
00271f77 | 1009 | return 4; |
1010 | } | |
1011 | ||
1012 | // user supplied filename? | |
1013 | if (fileNlen < 1) | |
1014 | sprintf(fnameptr,"%02X%02X%02X%02X.bin", data[0], data[1], data[2], data[3]); | |
1015 | else | |
1016 | sprintf(fnameptr + fileNlen,".bin"); | |
1017 | ||
0e8cabed | 1018 | if ((f = fopen(filename,"wb")) == NULL) { |
00271f77 | 1019 | PrintAndLog("Could not create file name %s", filename); |
0e8cabed | 1020 | if (data) |
1021 | free(data); | |
00271f77 | 1022 | return 5; |
1023 | } | |
0e8cabed | 1024 | fwrite(data, 1, readlen, f); |
1025 | fclose(f); | |
1026 | free(data); | |
00271f77 | 1027 | PrintAndLog("Wrote %d bytes to %s", readlen, filename); |
633d0686 | 1028 | return 0; |
1029 | } | |
0e8cabed | 1030 | |
1031 | int CmdLegicELoad(const char *Cmd) { | |
1032 | FILE * f; | |
1033 | char filename[FILE_PATH_SIZE]; | |
1034 | char *fnameptr = filename; | |
1035 | int len, numofbytes; | |
1036 | int nameParamNo = 1; | |
1037 | ||
1038 | char cmdp = param_getchar(Cmd, 0); | |
1039 | if ( cmdp == 'h' || cmdp == 'H' || cmdp == 0x00) | |
1040 | return usage_legic_eload(); | |
1041 | ||
1042 | switch (cmdp) { | |
1043 | case '0' : numofbytes = 22; break; | |
1044 | case '1' : | |
1045 | case '\0': numofbytes = 256; break; | |
1046 | case '2' : numofbytes = 1024; break; | |
1047 | default : numofbytes = 256; nameParamNo = 0;break; | |
1048 | } | |
1049 | ||
1050 | // set up buffer | |
1051 | uint8_t *data = malloc(numofbytes); | |
1052 | if (!data) { | |
1053 | PrintAndLog("Fail, cannot allocate memory"); | |
1054 | return 3; | |
1055 | } | |
1056 | memset(data, 0, numofbytes); | |
1057 | ||
1058 | // set up file | |
1059 | len = param_getstr(Cmd, nameParamNo, filename); | |
1060 | if (len > FILE_PATH_SIZE - 5) | |
1061 | len = FILE_PATH_SIZE - 5; | |
1062 | fnameptr += len; | |
1063 | sprintf(fnameptr, ".bin"); | |
1064 | ||
1065 | // open file | |
1066 | if ((f = fopen(filename,"rb")) == NULL) { | |
1067 | PrintAndLog("File %s not found or locked", filename); | |
1068 | free(data); | |
1069 | return 1; | |
1070 | } | |
1071 | ||
1072 | // load file | |
1073 | size_t bytes_read = fread(data, 1, numofbytes, f); | |
1074 | if ( bytes_read == 0){ | |
1075 | PrintAndLog("File reading error"); | |
1076 | free(data); | |
1077 | fclose(f); | |
1078 | return 2; | |
1079 | } | |
1080 | fclose(f); | |
1081 | ||
1082 | // transfer to device | |
1083 | legic_seteml(data, 0, numofbytes); | |
1084 | ||
1085 | free(data); | |
1086 | PrintAndLog("\nLoaded %d bytes from file: %s to emulator memory", numofbytes, filename); | |
1087 | return 0; | |
1088 | } | |
1089 | ||
1090 | int CmdLegicESave(const char *Cmd) { | |
1091 | FILE *f; | |
1092 | char filename[FILE_PATH_SIZE]; | |
1093 | char *fnameptr = filename; | |
1094 | int fileNlen, numofbytes, nameParamNo = 1; | |
1095 | ||
1096 | memset(filename, 0, sizeof(filename)); | |
1097 | ||
1098 | char cmdp = param_getchar(Cmd, 0); | |
1099 | ||
1100 | if ( cmdp == 'h' || cmdp == 'H' || cmdp == 0x00) | |
1101 | return usage_legic_esave(); | |
1102 | ||
1103 | switch (cmdp) { | |
1104 | case '0' : numofbytes = 22; break; | |
1105 | case '1' : | |
1106 | case '\0': numofbytes = 256; break; | |
1107 | case '2' : numofbytes = 1024; break; | |
1108 | default : numofbytes = 256; nameParamNo = 0; break; | |
1109 | } | |
1110 | ||
1111 | fileNlen = param_getstr(Cmd, nameParamNo, filename); | |
1112 | ||
1113 | if (fileNlen > FILE_PATH_SIZE - 5) | |
1114 | fileNlen = FILE_PATH_SIZE - 5; | |
1115 | ||
1116 | // set up buffer | |
1117 | uint8_t *data = malloc(numofbytes); | |
1118 | if (!data) { | |
1119 | PrintAndLog("Fail, cannot allocate memory"); | |
1120 | return 3; | |
1121 | } | |
1122 | memset(data, 0, numofbytes); | |
1123 | ||
1124 | // download emulator memory | |
77e1bab9 | 1125 | PrintAndLog("Reading emulator memory..."); |
0e8cabed | 1126 | GetEMLFromBigBuf(data, numofbytes, 0); |
1127 | if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500)) { | |
1128 | PrintAndLog("Fail, transfer from device time-out"); | |
1129 | free(data); | |
1130 | return 4; | |
1131 | } | |
1132 | ||
1133 | // user supplied filename? | |
1134 | if (fileNlen < 1) | |
1135 | sprintf(fnameptr,"%02X%02X%02X%02X.bin", data[0], data[1], data[2], data[3]); | |
1136 | else | |
1137 | sprintf(fnameptr + fileNlen,".bin"); | |
1138 | ||
1139 | // open file | |
1140 | if ((f = fopen(filename,"wb")) == NULL) { | |
1141 | PrintAndLog("Could not create file name %s", filename); | |
1142 | free(data); | |
1143 | return 1; | |
1144 | } | |
1145 | fwrite(data, 1, numofbytes, f); | |
1146 | fclose(f); | |
1147 | free(data); | |
1148 | PrintAndLog("\nSaved %d bytes from emulator memory to file: %s", numofbytes, filename); | |
1149 | return 0; | |
1150 | } | |
3e750be3 | 1151 | |
52cf34c1 | 1152 | static command_t CommandTable[] = { |
633d0686 | 1153 | {"help", CmdHelp, 1, "This help"}, |
1154 | {"reader", CmdLegicReader, 1, "LEGIC Prime Reader UID and Type tag info"}, | |
1155 | {"info", CmdLegicInfo, 0, "Display deobfuscated and decoded LEGIC Prime tag data"}, | |
1156 | {"dump", CmdLegicDump, 0, "Dump LEGIC Prime card to binary file"}, | |
00271f77 | 1157 | {"rdmem", CmdLegicRdmem, 0, "[offset][length] <iv> -- read bytes from a LEGIC card"}, |
633d0686 | 1158 | {"save", CmdLegicSave, 0, "<filename> [<length>] -- Store samples"}, |
1159 | {"load", CmdLegicLoad, 0, "<filename> -- Restore samples"}, | |
1160 | {"sim", CmdLegicRfSim, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"}, | |
1161 | {"write", CmdLegicRfWrite, 0, "<offset> <length> <iv> -- Write sample buffer (user after load or read)"}, | |
f6e01a34 | 1162 | {"crc", CmdLegicCalcCrc, 1, "Calculate Legic CRC over given hexbytes"}, |
0e8cabed | 1163 | {"eload", CmdLegicELoad, 1, "Load binary dump to emulator memory"}, |
1164 | {"esave", CmdLegicESave, 1, "Save emulator memory to binary file"}, | |
52cf34c1 | 1165 | {NULL, NULL, 0, NULL} |
1166 | }; | |
1167 | ||
1168 | int CmdHFLegic(const char *Cmd) { | |
1169 | clearCommandBuffer(); | |
1170 | CmdsParse(CommandTable, Cmd); | |
1171 | return 0; | |
1172 | } | |
1173 | ||
1174 | int CmdHelp(const char *Cmd) { | |
1175 | CmdsHelp(CommandTable); | |
1176 | return 0; | |
1177 | } |