]>
Commit | Line | Data |
---|---|---|
9ca155ba | 1 | //-----------------------------------------------------------------------------\r |
b62a5a84 | 2 | // Copyright (C) 2011,2012 Merlok\r |
9ca155ba M |
3 | //\r |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or,\r | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of\r | |
6 | // the license.\r | |
7 | //-----------------------------------------------------------------------------\r | |
8 | // High frequency MIFARE commands\r | |
9 | //-----------------------------------------------------------------------------\r | |
10 | \r | |
11 | #include "cmdhfmf.h"\r | |
12 | \r | |
13 | static int CmdHelp(const char *Cmd);\r | |
14 | \r | |
9ca155ba M |
15 | int CmdHF14AMifare(const char *Cmd)\r |
16 | {\r | |
17 | uint32_t uid = 0;\r | |
1c611bbd | 18 | uint32_t nt = 0, nr = 0;\r |
9ca155ba M |
19 | uint64_t par_list = 0, ks_list = 0, r_key = 0;\r |
20 | uint8_t isOK = 0;\r | |
ac14bee3 | 21 | uint8_t keyBlock[8] = {0};\r |
aea4d766 | 22 | \r |
1c611bbd | 23 | UsbCommand c = {CMD_READER_MIFARE, {true, 0, 0}};\r |
24 | \r | |
25 | // message\r | |
26 | printf("-------------------------------------------------------------------------\n");\r | |
27 | printf("Executing command. Expected execution time: 25sec on average :-)\n");\r | |
28 | printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r | |
29 | printf("-------------------------------------------------------------------------\n");\r | |
aea4d766 | 30 | \r |
9ca155ba | 31 | \r |
aea4d766 | 32 | start:\r |
e772353f | 33 | clearCommandBuffer();\r |
34 | SendCommand(&c);\r | |
9ca155ba M |
35 | \r |
36 | //flush queue\r | |
37 | while (ukbhit()) getchar();\r | |
38 | \r | |
9ca155ba M |
39 | \r |
40 | // wait cycle\r | |
41 | while (true) {\r | |
1c611bbd | 42 | printf(".");\r |
aea4d766 | 43 | fflush(stdout);\r |
9ca155ba M |
44 | if (ukbhit()) {\r |
45 | getchar();\r | |
46 | printf("\naborted via keyboard!\n");\r | |
47 | break;\r | |
48 | }\r | |
49 | \r | |
902cb3c0 | 50 | UsbCommand resp;\r |
1c611bbd | 51 | if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {\r |
902cb3c0 | 52 | isOK = resp.arg[0] & 0xff;\r |
902cb3c0 | 53 | uid = (uint32_t)bytes_to_num(resp.d.asBytes + 0, 4);\r |
54 | nt = (uint32_t)bytes_to_num(resp.d.asBytes + 4, 4);\r | |
55 | par_list = bytes_to_num(resp.d.asBytes + 8, 8);\r | |
56 | ks_list = bytes_to_num(resp.d.asBytes + 16, 8);\r | |
1c611bbd | 57 | nr = bytes_to_num(resp.d.asBytes + 24, 4);\r |
9ca155ba | 58 | printf("\n\n");\r |
9ca155ba M |
59 | if (!isOK) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n");\r |
60 | break;\r | |
61 | }\r | |
62 | } \r | |
1c611bbd | 63 | \r |
9ca155ba M |
64 | printf("\n");\r |
65 | \r | |
66 | // error\r | |
67 | if (isOK != 1) return 1;\r | |
68 | \r | |
69 | // execute original function from util nonce2key\r | |
1c611bbd | 70 | if (nonce2key(uid, nt, nr, par_list, ks_list, &r_key))\r |
bfaecce6 M |
71 | {\r |
72 | isOK = 2;\r | |
73 | PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt); \r | |
74 | } else {\r | |
75 | printf("------------------------------------------------------------------\n");\r | |
125a98a1 | 76 | PrintAndLog("Key found:%012"llx" \n", r_key);\r |
9ca155ba | 77 | \r |
bfaecce6 M |
78 | num_to_bytes(r_key, 6, keyBlock);\r |
79 | isOK = mfCheckKeys(0, 0, 1, keyBlock, &r_key);\r | |
80 | }\r | |
9ca155ba | 81 | if (!isOK) \r |
125a98a1 | 82 | PrintAndLog("Found valid key:%012"llx, r_key);\r |
9ca155ba | 83 | else\r |
aea4d766 | 84 | {\r |
1c611bbd | 85 | if (isOK != 2) PrintAndLog("Found invalid key. "); \r |
86 | PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");\r | |
87 | c.arg[0] = false;\r | |
aea4d766 | 88 | goto start;\r |
89 | }\r | |
9ca155ba M |
90 | \r |
91 | return 0;\r | |
92 | }\r | |
93 | \r | |
94 | int CmdHF14AMfWrBl(const char *Cmd)\r | |
95 | {\r | |
96 | uint8_t blockNo = 0;\r | |
97 | uint8_t keyType = 0;\r | |
98 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
99 | uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r | |
100 | \r | |
101 | char cmdp = 0x00;\r | |
102 | \r | |
103 | if (strlen(Cmd)<3) {\r | |
104 | PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");\r | |
105 | PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");\r | |
106 | return 0;\r | |
107 | } \r | |
108 | \r | |
109 | blockNo = param_get8(Cmd, 0);\r | |
110 | cmdp = param_getchar(Cmd, 1);\r | |
111 | if (cmdp == 0x00) {\r | |
112 | PrintAndLog("Key type must be A or B");\r | |
113 | return 1;\r | |
114 | }\r | |
115 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
116 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
117 | PrintAndLog("Key must include 12 HEX symbols");\r | |
118 | return 1;\r | |
119 | }\r | |
120 | if (param_gethex(Cmd, 3, bldata, 32)) {\r | |
121 | PrintAndLog("Block data must include 32 HEX symbols");\r | |
122 | return 1;\r | |
123 | }\r | |
124 | PrintAndLog("--block no:%02x key type:%02x key:%s", blockNo, keyType, sprint_hex(key, 6));\r | |
125 | PrintAndLog("--data: %s", sprint_hex(bldata, 16));\r | |
126 | \r | |
127 | UsbCommand c = {CMD_MIFARE_WRITEBL, {blockNo, keyType, 0}};\r | |
128 | memcpy(c.d.asBytes, key, 6);\r | |
129 | memcpy(c.d.asBytes + 10, bldata, 16);\r | |
130 | SendCommand(&c);\r | |
9ca155ba | 131 | \r |
902cb3c0 | 132 | UsbCommand resp;\r |
133 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
134 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
9ca155ba M |
135 | PrintAndLog("isOk:%02x", isOK);\r |
136 | } else {\r | |
137 | PrintAndLog("Command execute timeout");\r | |
138 | }\r | |
139 | \r | |
981bd429 | 140 | return 0; |
141 | } | |
142 | ||
143 | int CmdHF14AMfUWrBl(const char *Cmd) | |
144 | { | |
145 | uint8_t blockNo = 0; | |
146 | uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | |
147 | UsbCommand resp; | |
148 | ||
149 | if (strlen(Cmd)<3) { | |
150 | PrintAndLog("Usage: hf mf uwrbl <block number> <block data (8 hex symbols)>"); | |
151 | PrintAndLog(" sample: hf mf uwrbl 0 01020304"); | |
152 | return 0; | |
153 | } | |
154 | ||
155 | blockNo = param_get8(Cmd, 0); | |
156 | if (param_gethex(Cmd, 1, bldata, 8)) { | |
157 | PrintAndLog("Block data must include 8 HEX symbols"); | |
158 | return 1; | |
159 | } | |
160 | ||
161 | switch(blockNo) | |
162 | { | |
163 | case 0: | |
164 | PrintAndLog("Access Denied"); | |
165 | break; | |
166 | case 1: | |
167 | PrintAndLog("Access Denied"); | |
168 | break; | |
169 | case 2: | |
170 | PrintAndLog("--specialblock no:%02x", blockNo); | |
171 | PrintAndLog("--data: %s", sprint_hex(bldata, 4)); | |
172 | UsbCommand c = {CMD_MIFAREU_WRITEBL, {blockNo}}; | |
173 | memcpy(c.d.asBytes, bldata, 4); | |
174 | SendCommand(&c); | |
175 | ||
176 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
177 | uint8_t isOK = resp.arg[0] & 0xff; | |
178 | PrintAndLog("isOk:%02x", isOK); | |
179 | } else { | |
180 | PrintAndLog("Command execute timeout"); | |
181 | } | |
182 | break; | |
183 | case 3: | |
184 | PrintAndLog("--specialblock no:%02x", blockNo); | |
185 | PrintAndLog("--data: %s", sprint_hex(bldata, 4)); | |
186 | UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}}; | |
187 | memcpy(d.d.asBytes,bldata, 4); | |
188 | SendCommand(&d); | |
189 | ||
190 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
191 | uint8_t isOK = resp.arg[0] & 0xff; | |
192 | PrintAndLog("isOk:%02x", isOK); | |
193 | } else { | |
194 | PrintAndLog("Command execute timeout"); | |
195 | } | |
196 | break; | |
197 | default: | |
198 | PrintAndLog("--block no:%02x", blockNo); | |
199 | PrintAndLog("--data: %s", sprint_hex(bldata, 4)); | |
200 | //UsbCommand e = {CMD_MIFAREU_WRITEBL_COMPAT, {blockNo}}; | |
201 | //memcpy(e.d.asBytes,bldata, 16); | |
202 | UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}}; | |
203 | memcpy(e.d.asBytes,bldata, 4); | |
204 | SendCommand(&e); | |
205 | ||
206 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
207 | uint8_t isOK = resp.arg[0] & 0xff; | |
208 | PrintAndLog("isOk:%02x", isOK); | |
209 | } else { | |
210 | PrintAndLog("Command execute timeout"); | |
211 | } | |
212 | break; | |
213 | } | |
214 | return 0; | |
215 | } | |
216 | ||
217 | int CmdHF14AMfRdBl(const char *Cmd) | |
218 | { | |
219 | uint8_t blockNo = 0; | |
9ca155ba M |
220 | uint8_t keyType = 0;\r |
221 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
222 | \r | |
223 | char cmdp = 0x00;\r | |
224 | \r | |
225 | \r | |
226 | if (strlen(Cmd)<3) {\r | |
227 | PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");\r | |
228 | PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");\r | |
229 | return 0;\r | |
230 | } \r | |
231 | \r | |
232 | blockNo = param_get8(Cmd, 0);\r | |
233 | cmdp = param_getchar(Cmd, 1);\r | |
234 | if (cmdp == 0x00) {\r | |
235 | PrintAndLog("Key type must be A or B");\r | |
236 | return 1;\r | |
237 | }\r | |
238 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
239 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
240 | PrintAndLog("Key must include 12 HEX symbols");\r | |
241 | return 1;\r | |
242 | }\r | |
243 | PrintAndLog("--block no:%02x key type:%02x key:%s ", blockNo, keyType, sprint_hex(key, 6));\r | |
244 | \r | |
245 | UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};\r | |
246 | memcpy(c.d.asBytes, key, 6);\r | |
247 | SendCommand(&c);\r | |
9ca155ba | 248 | \r |
902cb3c0 | 249 | UsbCommand resp;\r |
250 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
251 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
252 | uint8_t * data = resp.d.asBytes;\r | |
9ca155ba M |
253 | \r |
254 | if (isOK)\r | |
255 | PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 16));\r | |
256 | else\r | |
257 | PrintAndLog("isOk:%02x", isOK);\r | |
258 | } else {\r | |
259 | PrintAndLog("Command execute timeout");\r | |
260 | }\r | |
261 | \r | |
981bd429 | 262 | return 0; |
263 | } | |
264 | ||
265 | int CmdHF14AMfURdBl(const char *Cmd) | |
266 | { | |
267 | uint8_t blockNo = 0; | |
268 | ||
269 | if (strlen(Cmd)<1) { | |
270 | PrintAndLog("Usage: hf mf urdbl <block number>"); | |
271 | PrintAndLog(" sample: hf mf urdbl 0"); | |
272 | return 0; | |
273 | } | |
274 | ||
275 | blockNo = param_get8(Cmd, 0); | |
276 | PrintAndLog("--block no:%02x", blockNo); | |
277 | ||
278 | UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}}; | |
279 | SendCommand(&c); | |
280 | ||
281 | UsbCommand resp; | |
282 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
283 | uint8_t isOK = resp.arg[0] & 0xff; | |
284 | uint8_t * data = resp.d.asBytes; | |
285 | ||
286 | if (isOK) | |
287 | PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 4)); | |
288 | else | |
289 | PrintAndLog("isOk:%02x", isOK); | |
290 | } else { | |
291 | PrintAndLog("Command execute timeout"); | |
292 | } | |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
297 | int CmdHF14AMfURdCard(const char *Cmd) | |
298 | { | |
299 | int i; | |
300 | uint8_t sectorNo = 0; | |
301 | uint8_t *lockbytes_t=NULL; | |
302 | uint8_t lockbytes[2]={0,0}; | |
303 | bool bit[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
304 | ||
305 | uint8_t isOK = 0; | |
306 | uint8_t * data = NULL; | |
307 | ||
308 | if (sectorNo > 15) { | |
309 | PrintAndLog("Sector number must be less than 16"); | |
310 | return 1; | |
311 | } | |
312 | PrintAndLog("Attempting to Read Ultralight... "); | |
313 | ||
314 | UsbCommand c = {CMD_MIFAREU_READCARD, {sectorNo}}; | |
315 | SendCommand(&c); | |
316 | ||
317 | UsbCommand resp; | |
318 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
319 | isOK = resp.arg[0] & 0xff; | |
320 | data = resp.d.asBytes; | |
321 | ||
322 | PrintAndLog("isOk:%02x", isOK); | |
323 | if (isOK) | |
324 | for (i = 0; i < 16; i++) { | |
325 | switch(i){ | |
326 | case 2: | |
327 | //process lock bytes | |
328 | lockbytes_t=data+(i*4); | |
329 | lockbytes[0]=lockbytes_t[2]; | |
330 | lockbytes[1]=lockbytes_t[3]; | |
331 | for(int j=0; j<16; j++){ | |
332 | bit[j]=lockbytes[j/8] & ( 1 <<(7-j%8)); | |
333 | } | |
334 | //PrintAndLog("LB %02x %02x", lockbytes[0],lockbytes[1]); | |
335 | //PrintAndLog("LB2b %02x %02x %02x %02x %02x %02x %02x %02x",bit[8],bit[9],bit[10],bit[11],bit[12],bit[13],bit[14],bit[15]); | |
336 | PrintAndLog("Block %02x:%s ", i,sprint_hex(data + i * 4, 4)); | |
337 | break; | |
338 | case 3: | |
339 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[4]); | |
340 | break; | |
341 | case 4: | |
342 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[3]); | |
343 | break; | |
344 | case 5: | |
345 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[2]); | |
346 | break; | |
347 | case 6: | |
348 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[1]); | |
349 | break; | |
350 | case 7: | |
351 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[0]); | |
352 | break; | |
353 | case 8: | |
354 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[15]); | |
355 | break; | |
356 | case 9: | |
357 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[14]); | |
358 | break; | |
359 | case 10: | |
360 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[13]); | |
361 | break; | |
362 | case 11: | |
363 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[12]); | |
364 | break; | |
365 | case 12: | |
366 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[11]); | |
367 | break; | |
368 | case 13: | |
369 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[10]); | |
370 | break; | |
371 | case 14: | |
372 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[9]); | |
373 | break; | |
374 | case 15: | |
375 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[8]); | |
376 | break; | |
377 | default: | |
378 | PrintAndLog("Block %02x:%s ", i,sprint_hex(data + i * 4, 4)); | |
379 | break; | |
380 | } | |
381 | } | |
382 | } else { | |
383 | PrintAndLog("Command1 execute timeout"); | |
384 | } | |
385 | return 0; | |
386 | } | |
387 | ||
388 | int CmdHF14AMfRdSc(const char *Cmd) | |
389 | { | |
390 | int i; | |
9ca155ba M |
391 | uint8_t sectorNo = 0;\r |
392 | uint8_t keyType = 0;\r | |
393 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
394 | \r | |
395 | uint8_t isOK = 0;\r | |
396 | uint8_t * data = NULL;\r | |
397 | \r | |
398 | char cmdp = 0x00;\r | |
399 | \r | |
400 | if (strlen(Cmd)<3) {\r | |
401 | PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");\r | |
402 | PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");\r | |
403 | return 0;\r | |
404 | } \r | |
405 | \r | |
406 | sectorNo = param_get8(Cmd, 0);\r | |
407 | if (sectorNo > 63) {\r | |
408 | PrintAndLog("Sector number must be less than 64");\r | |
409 | return 1;\r | |
410 | }\r | |
411 | cmdp = param_getchar(Cmd, 1);\r | |
412 | if (cmdp == 0x00) {\r | |
413 | PrintAndLog("Key type must be A or B");\r | |
414 | return 1;\r | |
415 | }\r | |
416 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
417 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
418 | PrintAndLog("Key must include 12 HEX symbols");\r | |
419 | return 1;\r | |
420 | }\r | |
421 | PrintAndLog("--sector no:%02x key type:%02x key:%s ", sectorNo, keyType, sprint_hex(key, 6));\r | |
422 | \r | |
423 | UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};\r | |
424 | memcpy(c.d.asBytes, key, 6);\r | |
425 | SendCommand(&c);\r | |
9ca155ba M |
426 | PrintAndLog(" ");\r |
427 | \r | |
902cb3c0 | 428 | UsbCommand resp;\r |
429 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
430 | isOK = resp.arg[0] & 0xff;\r | |
431 | data = resp.d.asBytes;\r | |
9ca155ba M |
432 | \r |
433 | PrintAndLog("isOk:%02x", isOK);\r | |
434 | if (isOK) \r | |
435 | for (i = 0; i < 2; i++) {\r | |
436 | PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));\r | |
437 | }\r | |
438 | } else {\r | |
439 | PrintAndLog("Command1 execute timeout");\r | |
440 | }\r | |
441 | \r | |
902cb3c0 | 442 | // response2\r |
9ca155ba | 443 | PrintAndLog(" ");\r |
902cb3c0 | 444 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r |
445 | isOK = resp.arg[0] & 0xff;\r | |
446 | data = resp.d.asBytes;\r | |
9ca155ba M |
447 | \r |
448 | if (isOK) \r | |
449 | for (i = 0; i < 2; i++) {\r | |
450 | PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));\r | |
451 | }\r | |
452 | } else {\r | |
453 | PrintAndLog("Command2 execute timeout");\r | |
454 | }\r | |
455 | \r | |
456 | return 0;\r | |
457 | }\r | |
458 | \r | |
aea4d766 | 459 | int CmdHF14AMfDump(const char *Cmd)\r |
26fdb4ab | 460 | {\r |
461 | int i, j;\r | |
462 | \r | |
aea4d766 | 463 | uint8_t keyA[40][6];\r |
464 | uint8_t keyB[40][6];\r | |
465 | uint8_t rights[40][4];\r | |
26fdb4ab | 466 | \r |
26fdb4ab | 467 | FILE *fin;\r |
468 | FILE *fout;\r | |
469 | \r | |
902cb3c0 | 470 | UsbCommand resp;\r |
26fdb4ab | 471 | \r |
472 | if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {\r | |
aea4d766 | 473 | PrintAndLog("Could not find file dumpkeys.bin");\r |
26fdb4ab | 474 | return 1;\r |
475 | }\r | |
476 | \r | |
477 | if ((fout = fopen("dumpdata.bin","wb")) == NULL) { \r | |
aea4d766 | 478 | PrintAndLog("Could not create file name dumpdata.bin");\r |
26fdb4ab | 479 | return 1;\r |
480 | }\r | |
481 | \r | |
482 | // Read key file\r | |
483 | \r | |
484 | for (i=0 ; i<16 ; i++) {\r | |
759c16b3 | 485 | if (fread( keyA[i], 1, 6, fin ) == 0) {\r |
486 | PrintAndLog("File reading error.");\r | |
487 | return 2;\r | |
488 | }\r | |
26fdb4ab | 489 | }\r |
490 | for (i=0 ; i<16 ; i++) {\r | |
759c16b3 | 491 | if (fread( keyB[i], 1, 6, fin ) == 0) {\r |
492 | PrintAndLog("File reading error.");\r | |
493 | return 2;\r | |
494 | }\r | |
26fdb4ab | 495 | }\r |
496 | \r | |
497 | // Read access rights to sectors\r | |
498 | \r | |
499 | PrintAndLog("|-----------------------------------------|");\r | |
500 | PrintAndLog("|------ Reading sector access bits...-----|");\r | |
501 | PrintAndLog("|-----------------------------------------|");\r | |
502 | \r | |
503 | for (i = 0 ; i < 16 ; i++) {\r | |
504 | UsbCommand c = {CMD_MIFARE_READBL, {4*i + 3, 0, 0}};\r | |
505 | memcpy(c.d.asBytes, keyA[i], 6);\r | |
506 | SendCommand(&c);\r | |
26fdb4ab | 507 | \r |
902cb3c0 | 508 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r |
509 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
510 | uint8_t *data = resp.d.asBytes;\r | |
26fdb4ab | 511 | if (isOK){\r |
512 | rights[i][0] = ((data[7] & 0x10)>>4) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>2);\r | |
513 | rights[i][1] = ((data[7] & 0x20)>>5) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>3);\r | |
514 | rights[i][2] = ((data[7] & 0x40)>>6) | ((data[8] & 0x4)>>1) | ((data[8] & 0x40)>>4);\r | |
515 | rights[i][3] = ((data[7] & 0x80)>>7) | ((data[8] & 0x8)>>2) | ((data[8] & 0x80)>>5);\r | |
516 | }\r | |
517 | else{\r | |
518 | PrintAndLog("Could not get access rights for block %d", i);\r | |
519 | }\r | |
520 | }\r | |
521 | else {\r | |
522 | PrintAndLog("Command execute timeout");\r | |
523 | }\r | |
524 | }\r | |
525 | \r | |
526 | // Read blocks and print to file\r | |
527 | \r | |
528 | PrintAndLog("|-----------------------------------------|");\r | |
529 | PrintAndLog("|----- Dumping all blocks to file... -----|");\r | |
530 | PrintAndLog("|-----------------------------------------|");\r | |
531 | \r | |
902cb3c0 | 532 | \r |
26fdb4ab | 533 | for (i=0 ; i<16 ; i++) {\r |
534 | for (j=0 ; j<4 ; j++) {\r | |
902cb3c0 | 535 | bool received = false;\r |
536 | \r | |
537 | if (j == 3){\r | |
26fdb4ab | 538 | UsbCommand c = {CMD_MIFARE_READBL, {i*4 + j, 0, 0}};\r |
539 | memcpy(c.d.asBytes, keyA[i], 6);\r | |
540 | SendCommand(&c);\r | |
902cb3c0 | 541 | received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r |
26fdb4ab | 542 | }\r |
543 | else{\r | |
544 | if ((rights[i][j] == 6) | (rights[i][j] == 5)) {\r | |
545 | UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 1, 0}};\r | |
546 | memcpy(c.d.asBytes, keyB[i], 6);\r | |
547 | SendCommand(&c);\r | |
902cb3c0 | 548 | received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r |
26fdb4ab | 549 | }\r |
550 | else if (rights[i][j] == 7) {\r | |
551 | PrintAndLog("Access rights do not allow reading of sector %d block %d",i,j);\r | |
552 | }\r | |
553 | else {\r | |
554 | UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 0, 0}};\r | |
555 | memcpy(c.d.asBytes, keyA[i], 6);\r | |
556 | SendCommand(&c);\r | |
902cb3c0 | 557 | received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r |
26fdb4ab | 558 | }\r |
559 | }\r | |
560 | \r | |
902cb3c0 | 561 | if (received) {\r |
562 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
563 | uint8_t *data = resp.d.asBytes;\r | |
3d77fdfa | 564 | if (j == 3) {\r |
565 | data[0] = (keyA[i][0]);\r | |
566 | data[1] = (keyA[i][1]);\r | |
567 | data[2] = (keyA[i][2]);\r | |
568 | data[3] = (keyA[i][3]);\r | |
569 | data[4] = (keyA[i][4]);\r | |
570 | data[5] = (keyA[i][5]);\r | |
571 | data[10] = (keyB[i][0]);\r | |
572 | data[11] = (keyB[i][1]);\r | |
573 | data[12] = (keyB[i][2]);\r | |
574 | data[13] = (keyB[i][3]);\r | |
575 | data[14] = (keyB[i][4]);\r | |
576 | data[15] = (keyB[i][5]);\r | |
577 | }\r | |
26fdb4ab | 578 | if (isOK) {\r |
579 | fwrite ( data, 1, 16, fout );\r | |
6a2c1d8e | 580 | PrintAndLog("Dumped card data into 'dumpdata.bin'");\r |
05ed5c49 | 581 | \r |
26fdb4ab | 582 | }\r |
583 | else {\r | |
584 | PrintAndLog("Could not get access rights for block %d", i);\r | |
585 | }\r | |
586 | }\r | |
587 | else {\r | |
588 | PrintAndLog("Command execute timeout");\r | |
589 | }\r | |
590 | }\r | |
591 | }\r | |
592 | \r | |
593 | fclose(fin);\r | |
594 | fclose(fout);\r | |
26fdb4ab | 595 | return 0;\r |
596 | }\r | |
597 | \r | |
aea4d766 | 598 | int CmdHF14AMfRestore(const char *Cmd)\r |
26fdb4ab | 599 | {\r |
600 | \r | |
601 | int i,j;\r | |
26fdb4ab | 602 | uint8_t keyType = 0;\r |
603 | uint8_t key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r | |
604 | uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r | |
605 | uint8_t keyA[16][6];\r | |
606 | uint8_t keyB[16][6];\r | |
607 | \r | |
26fdb4ab | 608 | FILE *fdump;\r |
609 | FILE *fkeys;\r | |
610 | \r | |
26fdb4ab | 611 | if ((fdump = fopen("dumpdata.bin","rb")) == NULL) {\r |
aea4d766 | 612 | PrintAndLog("Could not find file dumpdata.bin");\r |
26fdb4ab | 613 | return 1;\r |
614 | }\r | |
615 | if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) {\r | |
aea4d766 | 616 | PrintAndLog("Could not find file dumpkeys.bin");\r |
26fdb4ab | 617 | return 1;\r |
618 | }\r | |
619 | \r | |
620 | for (i=0 ; i<16 ; i++) {\r | |
759c16b3 | 621 | if (fread(keyA[i], 1, 6, fkeys) == 0) {\r |
622 | PrintAndLog("File reading error.");\r | |
623 | return 2;\r | |
624 | }\r | |
26fdb4ab | 625 | }\r |
626 | for (i=0 ; i<16 ; i++) {\r | |
759c16b3 | 627 | if (fread(keyB[i], 1, 6, fkeys) == 0) {\r |
628 | PrintAndLog("File reading error.");\r | |
629 | return 2;\r | |
630 | }\r | |
26fdb4ab | 631 | }\r |
632 | \r | |
9d710943 | 633 | PrintAndLog("Restoring dumpdata.bin to card");\r |
26fdb4ab | 634 | \r |
635 | for (i=0 ; i<16 ; i++) {\r | |
636 | for( j=0 ; j<4 ; j++) {\r | |
637 | UsbCommand c = {CMD_MIFARE_WRITEBL, {i*4 + j, keyType, 0}};\r | |
638 | memcpy(c.d.asBytes, key, 6);\r | |
639 | \r | |
759c16b3 | 640 | if (fread(bldata, 1, 16, fdump) == 0) {\r |
641 | PrintAndLog("File reading error.");\r | |
642 | return 2;\r | |
643 | }\r | |
26fdb4ab | 644 | \r |
645 | if (j == 3) {\r | |
646 | bldata[0] = (keyA[i][0]);\r | |
647 | bldata[1] = (keyA[i][1]);\r | |
648 | bldata[2] = (keyA[i][2]);\r | |
649 | bldata[3] = (keyA[i][3]);\r | |
650 | bldata[4] = (keyA[i][4]);\r | |
651 | bldata[5] = (keyA[i][5]);\r | |
652 | bldata[10] = (keyB[i][0]);\r | |
653 | bldata[11] = (keyB[i][1]);\r | |
654 | bldata[12] = (keyB[i][2]);\r | |
655 | bldata[13] = (keyB[i][3]);\r | |
656 | bldata[14] = (keyB[i][4]);\r | |
657 | bldata[15] = (keyB[i][5]);\r | |
658 | } \r | |
659 | \r | |
6c064e16 | 660 | PrintAndLog("Writing to block %2d: %s", i*4+j, sprint_hex(bldata, 16));\r |
661 | \r | |
662 | /*\r | |
9d710943 | 663 | PrintAndLog("Writing to block %2d: %s Confirm? [Y,N]", i*4+j, sprint_hex(bldata, 16));\r |
664 | \r | |
0f7f9edc | 665 | scanf("%c",&ch);\r |
6c064e16 | 666 | if ((ch != 'y') && (ch != 'Y')){\r |
9d710943 | 667 | PrintAndLog("Aborting !");\r |
668 | return 1;\r | |
669 | }\r | |
6c064e16 | 670 | */\r |
26fdb4ab | 671 | \r |
672 | memcpy(c.d.asBytes + 10, bldata, 16);\r | |
673 | SendCommand(&c);\r | |
26fdb4ab | 674 | \r |
902cb3c0 | 675 | UsbCommand resp;\r |
676 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
677 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
26fdb4ab | 678 | PrintAndLog("isOk:%02x", isOK);\r |
679 | } else {\r | |
680 | PrintAndLog("Command execute timeout");\r | |
681 | }\r | |
682 | }\r | |
683 | }\r | |
684 | \r | |
685 | fclose(fdump);\r | |
686 | fclose(fkeys);\r | |
687 | return 0;\r | |
688 | }\r | |
689 | \r | |
9ca155ba M |
690 | int CmdHF14AMfNested(const char *Cmd)\r |
691 | {\r | |
692 | int i, j, res, iterations;\r | |
693 | sector * e_sector = NULL;\r | |
694 | uint8_t blockNo = 0;\r | |
695 | uint8_t keyType = 0;\r | |
696 | uint8_t trgBlockNo = 0;\r | |
697 | uint8_t trgKeyType = 0;\r | |
698 | uint8_t blDiff = 0;\r | |
699 | int SectorsCnt = 0;\r | |
700 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
9492e0b0 | 701 | uint8_t keyBlock[6*6];\r |
9ca155ba | 702 | uint64_t key64 = 0;\r |
8556b852 | 703 | int transferToEml = 0;\r |
9ca155ba | 704 | \r |
26fdb4ab | 705 | int createDumpFile = 0;\r |
706 | FILE *fkeys;\r | |
21156267 | 707 | uint8_t standart[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r |
708 | uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r | |
26fdb4ab | 709 | \r |
9ca155ba M |
710 | char cmdp, ctmp;\r |
711 | \r | |
712 | if (strlen(Cmd)<3) {\r | |
713 | PrintAndLog("Usage:");\r | |
26fdb4ab | 714 | PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");\r |
0014cb46 M |
715 | PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");\r |
716 | PrintAndLog(" <target block number> <target key A/B> [t]");\r | |
9ca155ba | 717 | PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r |
8556b852 | 718 | PrintAndLog("t - transfer keys into emulator memory");\r |
26fdb4ab | 719 | PrintAndLog("d - write keys to binary file");\r |
9ca155ba M |
720 | PrintAndLog(" ");\r |
721 | PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");\r | |
8556b852 | 722 | PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");\r |
26fdb4ab | 723 | PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");\r |
9ca155ba M |
724 | PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");\r |
725 | return 0;\r | |
726 | } \r | |
727 | \r | |
728 | cmdp = param_getchar(Cmd, 0);\r | |
729 | blockNo = param_get8(Cmd, 1);\r | |
730 | ctmp = param_getchar(Cmd, 2);\r | |
731 | if (ctmp == 0x00) {\r | |
732 | PrintAndLog("Key type must be A or B");\r | |
733 | return 1;\r | |
734 | }\r | |
735 | if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r | |
736 | if (param_gethex(Cmd, 3, key, 12)) {\r | |
737 | PrintAndLog("Key must include 12 HEX symbols");\r | |
738 | return 1;\r | |
739 | }\r | |
740 | \r | |
8556b852 | 741 | if (cmdp == 'o' || cmdp == 'O') {\r |
9ca155ba M |
742 | cmdp = 'o';\r |
743 | trgBlockNo = param_get8(Cmd, 4);\r | |
744 | ctmp = param_getchar(Cmd, 5);\r | |
745 | if (ctmp == 0x00) {\r | |
746 | PrintAndLog("Target key type must be A or B");\r | |
747 | return 1;\r | |
748 | }\r | |
749 | if (ctmp != 'A' && ctmp != 'a') trgKeyType = 1;\r | |
750 | } else {\r | |
751 | switch (cmdp) {\r | |
752 | case '0': SectorsCnt = 05; break;\r | |
753 | case '1': SectorsCnt = 16; break;\r | |
754 | case '2': SectorsCnt = 32; break;\r | |
755 | case '4': SectorsCnt = 64; break;\r | |
756 | default: SectorsCnt = 16;\r | |
757 | }\r | |
758 | }\r | |
8556b852 M |
759 | \r |
760 | ctmp = param_getchar(Cmd, 4);\r | |
21156267 | 761 | if (ctmp == 't' || ctmp == 'T') transferToEml = 1;\r |
762 | else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;\r | |
763 | \r | |
8556b852 M |
764 | ctmp = param_getchar(Cmd, 6);\r |
765 | transferToEml |= (ctmp == 't' || ctmp == 'T');\r | |
21156267 | 766 | transferToEml |= (ctmp == 'd' || ctmp == 'D');\r |
9ca155ba | 767 | \r |
8556b852 | 768 | PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo, keyType, sprint_hex(key, 6), transferToEml);\r |
9ca155ba M |
769 | if (cmdp == 'o')\r |
770 | PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo, trgKeyType);\r | |
771 | \r | |
772 | if (cmdp == 'o') {\r | |
9492e0b0 | 773 | if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, true)) {\r |
9ca155ba M |
774 | PrintAndLog("Nested error.");\r |
775 | return 2;\r | |
776 | }\r | |
9492e0b0 | 777 | key64 = bytes_to_num(keyBlock, 6);\r |
778 | if (key64) {\r | |
125a98a1 | 779 | PrintAndLog("Found valid key:%012"llx, key64);\r |
8556b852 M |
780 | \r |
781 | // transfer key to the emulator\r | |
782 | if (transferToEml) {\r | |
783 | mfEmlGetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1);\r | |
784 | \r | |
785 | if (!trgKeyType)\r | |
786 | num_to_bytes(key64, 6, keyBlock);\r | |
787 | else\r | |
788 | num_to_bytes(key64, 6, &keyBlock[10]);\r | |
789 | mfEmlSetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1); \r | |
790 | }\r | |
791 | } else {\r | |
9ca155ba | 792 | PrintAndLog("No valid key found");\r |
8556b852 | 793 | }\r |
21156267 | 794 | }\r |
795 | else { // ------------------------------------ multiple sectors working\r | |
9492e0b0 | 796 | clock_t time1;\r |
797 | time1 = clock();\r | |
798 | \r | |
9ca155ba M |
799 | blDiff = blockNo % 4;\r |
800 | PrintAndLog("Block shift=%d", blDiff);\r | |
801 | e_sector = calloc(SectorsCnt, sizeof(sector));\r | |
802 | if (e_sector == NULL) return 1;\r | |
803 | \r | |
804 | //test current key 4 sectors\r | |
805 | memcpy(keyBlock, key, 6);\r | |
9492e0b0 | 806 | num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 1 * 6));\r |
807 | num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 2 * 6));\r | |
808 | num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 3 * 6));\r | |
809 | num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 4 * 6));\r | |
9ca155ba M |
810 | num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));\r |
811 | \r | |
812 | PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);\r | |
813 | for (i = 0; i < SectorsCnt; i++) {\r | |
814 | for (j = 0; j < 2; j++) {\r | |
815 | if (e_sector[i].foundKey[j]) continue;\r | |
816 | \r | |
817 | res = mfCheckKeys(i * 4 + blDiff, j, 6, keyBlock, &key64);\r | |
818 | \r | |
819 | if (!res) {\r | |
820 | e_sector[i].Key[j] = key64;\r | |
821 | e_sector[i].foundKey[j] = 1;\r | |
822 | }\r | |
823 | }\r | |
9492e0b0 | 824 | }\r |
825 | \r | |
9ca155ba | 826 | \r |
9ca155ba M |
827 | // nested sectors\r |
828 | iterations = 0;\r | |
829 | PrintAndLog("nested...");\r | |
9492e0b0 | 830 | bool calibrate = true;\r |
9ca155ba | 831 | for (i = 0; i < NESTED_SECTOR_RETRY; i++) {\r |
9492e0b0 | 832 | for (trgBlockNo = blDiff; trgBlockNo < SectorsCnt * 4; trgBlockNo = trgBlockNo + 4) {\r |
9ca155ba M |
833 | for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) { \r |
834 | if (e_sector[trgBlockNo / 4].foundKey[trgKeyType]) continue;\r | |
9492e0b0 | 835 | PrintAndLog("-----------------------------------------------");\r |
836 | if(mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, calibrate)) {\r | |
837 | PrintAndLog("Nested error.\n");\r | |
838 | return 2;\r | |
839 | }\r | |
840 | else {\r | |
841 | calibrate = false;\r | |
842 | }\r | |
9ca155ba M |
843 | \r |
844 | iterations++;\r | |
9492e0b0 | 845 | \r |
846 | key64 = bytes_to_num(keyBlock, 6);\r | |
847 | if (key64) {\r | |
125a98a1 | 848 | PrintAndLog("Found valid key:%012"llx, key64);\r |
9ca155ba M |
849 | e_sector[trgBlockNo / 4].foundKey[trgKeyType] = 1;\r |
850 | e_sector[trgBlockNo / 4].Key[trgKeyType] = key64;\r | |
851 | }\r | |
852 | }\r | |
9492e0b0 | 853 | }\r |
9ca155ba M |
854 | }\r |
855 | \r | |
a0041115 | 856 | printf("Time in nested: %1.3f (%1.3f sec per key)\n\n", ((float)clock() - time1)/CLOCKS_PER_SEC, ((float)clock() - time1)/iterations/CLOCKS_PER_SEC);\r |
9492e0b0 | 857 | \r |
858 | PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations);\r | |
9ca155ba M |
859 | //print them\r |
860 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
861 | PrintAndLog("|sec|key A |res|key B |res|");\r | |
862 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
863 | for (i = 0; i < SectorsCnt; i++) {\r | |
125a98a1 | 864 | PrintAndLog("|%03d| %012"llx" | %d | %012"llx" | %d |", i,\r |
9ca155ba M |
865 | e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);\r |
866 | }\r | |
867 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
868 | \r | |
8556b852 M |
869 | // transfer them to the emulator\r |
870 | if (transferToEml) {\r | |
871 | for (i = 0; i < SectorsCnt; i++) {\r | |
872 | mfEmlGetMem(keyBlock, i * 4 + 3, 1);\r | |
873 | if (e_sector[i].foundKey[0])\r | |
ab8b654e | 874 | num_to_bytes(e_sector[i].Key[0], 6, keyBlock);\r |
8556b852 M |
875 | if (e_sector[i].foundKey[1])\r |
876 | num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);\r | |
877 | mfEmlSetMem(keyBlock, i * 4 + 3, 1);\r | |
878 | } \r | |
879 | }\r | |
880 | \r | |
21156267 | 881 | // Create dump file\r |
26fdb4ab | 882 | if (createDumpFile) {\r |
883 | if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) { \r | |
aea4d766 | 884 | PrintAndLog("Could not create file dumpkeys.bin");\r |
26fdb4ab | 885 | free(e_sector);\r |
886 | return 1;\r | |
887 | }\r | |
21156267 | 888 | PrintAndLog("Printing keys to bynary file dumpkeys.bin...");\r |
26fdb4ab | 889 | for(i=0; i<16; i++) {\r |
21156267 | 890 | if (e_sector[i].foundKey[0]){\r |
891 | num_to_bytes(e_sector[i].Key[0], 6, tempkey);\r | |
892 | fwrite ( tempkey, 1, 6, fkeys );\r | |
893 | }\r | |
894 | else{\r | |
895 | fwrite ( &standart, 1, 6, fkeys );\r | |
896 | }\r | |
26fdb4ab | 897 | }\r |
898 | for(i=0; i<16; i++) {\r | |
21156267 | 899 | if (e_sector[i].foundKey[1]){\r |
900 | num_to_bytes(e_sector[i].Key[1], 6, tempkey);\r | |
901 | fwrite ( tempkey, 1, 6, fkeys );\r | |
902 | }\r | |
903 | else{\r | |
904 | fwrite ( &standart, 1, 6, fkeys );\r | |
905 | }\r | |
26fdb4ab | 906 | }\r |
907 | fclose(fkeys);\r | |
908 | }\r | |
909 | \r | |
9ca155ba M |
910 | free(e_sector);\r |
911 | }\r | |
912 | \r | |
913 | return 0;\r | |
914 | }\r | |
915 | \r | |
aea4d766 | 916 | static uint32_t\r |
917 | get_trailer_block (uint32_t uiBlock)\r | |
918 | {\r | |
919 | // Test if we are in the small or big sectors\r | |
920 | uint32_t trailer_block = 0;\r | |
921 | if (uiBlock < 128) {\r | |
922 | trailer_block = uiBlock + (3 - (uiBlock % 4));\r | |
923 | } else {\r | |
924 | trailer_block = uiBlock + (15 - (uiBlock % 16));\r | |
925 | }\r | |
926 | return trailer_block;\r | |
927 | }\r | |
9ca155ba M |
928 | int CmdHF14AMfChk(const char *Cmd)\r |
929 | {\r | |
aea4d766 | 930 | FILE * f;\r |
931 | char filename[256]={0};\r | |
83613803 | 932 | char buf[13];\r |
aea4d766 | 933 | uint8_t *keyBlock = NULL, *p;\r |
934 | uint8_t stKeyBlock = 20;\r | |
935 | \r | |
9ca155ba M |
936 | int i, res;\r |
937 | int keycnt = 0;\r | |
938 | char ctmp = 0x00;\r | |
939 | uint8_t blockNo = 0;\r | |
aea4d766 | 940 | uint8_t SectorsCnt = 1;\r |
9ca155ba | 941 | uint8_t keyType = 0;\r |
9ca155ba | 942 | uint64_t key64 = 0;\r |
aea4d766 | 943 | \r |
944 | int transferToEml = 0;\r | |
945 | int createDumpFile = 0;\r | |
9ca155ba | 946 | \r |
aea4d766 | 947 | keyBlock = calloc(stKeyBlock, 6);\r |
948 | if (keyBlock == NULL) return 1;\r | |
949 | \r | |
950 | num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 0 * 6)); // Default key (first key used by program if no user defined key)\r | |
951 | num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 1 * 6)); // Blank key\r | |
952 | num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 2 * 6)); // NFCForum MAD key\r | |
953 | num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 3 * 6));\r | |
954 | num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 4 * 6));\r | |
955 | num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock + 5 * 6));\r | |
956 | num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock + 6 * 6));\r | |
957 | num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock + 7 * 6));\r | |
958 | num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock + 8 * 6));\r | |
959 | num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock + 9 * 6));\r | |
960 | num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock + 10 * 6));\r | |
961 | num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock + 11 * 6));\r | |
962 | num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock + 12 * 6));\r | |
963 | \r | |
9ca155ba | 964 | if (strlen(Cmd)<3) {\r |
aea4d766 | 965 | PrintAndLog("Usage: hf mf chk <block number>/<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");\r |
966 | PrintAndLog(" * - all sectors");\r | |
967 | PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r | |
968 | // PrintAndLog("d - write keys to binary file\n");\r | |
969 | \r | |
970 | PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");\r | |
971 | PrintAndLog(" hf mf chk *1 ? t");\r | |
9ca155ba M |
972 | return 0;\r |
973 | } \r | |
974 | \r | |
aea4d766 | 975 | if (param_getchar(Cmd, 0)=='*') {\r |
976 | blockNo = 3;\r | |
977 | switch(param_getchar(Cmd+1, 0)) {\r | |
978 | case '0': SectorsCnt = 5; break;\r | |
979 | case '1': SectorsCnt = 16; break;\r | |
980 | case '2': SectorsCnt = 32; break;\r | |
981 | case '4': SectorsCnt = 40; break;\r | |
982 | default: SectorsCnt = 16;\r | |
983 | }\r | |
984 | }\r | |
985 | else\r | |
986 | blockNo = param_get8(Cmd, 0);\r | |
987 | \r | |
9ca155ba | 988 | ctmp = param_getchar(Cmd, 1);\r |
aea4d766 | 989 | switch (ctmp) { \r |
990 | case 'a': case 'A':\r | |
991 | keyType = !0;\r | |
992 | break;\r | |
993 | case 'b': case 'B':\r | |
994 | keyType = !1;\r | |
995 | break;\r | |
996 | case '?':\r | |
997 | keyType = 2;\r | |
998 | break;\r | |
999 | default:\r | |
1000 | PrintAndLog("Key type must be A , B or ?");\r | |
9ca155ba | 1001 | return 1;\r |
aea4d766 | 1002 | };\r |
9ca155ba | 1003 | \r |
aea4d766 | 1004 | ctmp = param_getchar(Cmd, 2);\r |
1005 | if (ctmp == 't' || ctmp == 'T') transferToEml = 1;\r | |
1006 | else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;\r | |
1007 | \r | |
1008 | for (i = transferToEml || createDumpFile; param_getchar(Cmd, 2 + i); i++) {\r | |
1009 | if (!param_gethex(Cmd, 2 + i, keyBlock + 6 * keycnt, 12)) {\r | |
1010 | if ( stKeyBlock - keycnt < 2) {\r | |
1011 | p = realloc(keyBlock, 6*(stKeyBlock+=10));\r | |
1012 | if (!p) {\r | |
1013 | PrintAndLog("Cannot allocate memory for Keys");\r | |
1014 | free(keyBlock);\r | |
1015 | return 2;\r | |
1016 | }\r | |
1017 | keyBlock = p;\r | |
1018 | }\r | |
1019 | PrintAndLog("chk key[%d] %02x%02x%02x%02x%02x%02x", keycnt,\r | |
1020 | (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r | |
1021 | (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r | |
1022 | keycnt++;\r | |
1023 | } else {\r | |
1024 | // May be a dic file\r | |
1025 | if ( param_getstr(Cmd, 2 + i,filename) > 255 ) {\r | |
1026 | PrintAndLog("File name too long");\r | |
1027 | free(keyBlock);\r | |
1028 | return 2;\r | |
1029 | }\r | |
1030 | \r | |
aea4d766 | 1031 | if ( (f = fopen( filename , "r")) ) {\r |
99a71a0d | 1032 | while( !feof(f) ){\r |
1033 | memset(buf, 0, sizeof(buf));\r | |
759c16b3 | 1034 | if (fgets(buf, sizeof(buf), f) == NULL) {\r |
9492e0b0 | 1035 | PrintAndLog("File reading error.");\r |
1036 | return 2;\r | |
1037 | }\r | |
759c16b3 | 1038 | \r |
99a71a0d | 1039 | if (strlen(buf) < 12 || buf[11] == '\n')\r |
1040 | continue;\r | |
aea4d766 | 1041 | \r |
99a71a0d | 1042 | while (fgetc(f) != '\n' && !feof(f)) ; //goto next line\r |
1043 | \r | |
9492e0b0 | 1044 | if( buf[0]=='#' ) continue; //The line start with # is comment, skip\r |
9ca155ba | 1045 | \r |
99a71a0d | 1046 | if (!isxdigit(buf[0])){\r |
1047 | PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);\r | |
aea4d766 | 1048 | continue;\r |
1049 | }\r | |
1050 | \r | |
99a71a0d | 1051 | buf[12] = 0;\r |
aea4d766 | 1052 | \r |
1053 | if ( stKeyBlock - keycnt < 2) {\r | |
1054 | p = realloc(keyBlock, 6*(stKeyBlock+=10));\r | |
1055 | if (!p) {\r | |
1056 | PrintAndLog("Cannot allocate memory for defKeys");\r | |
1057 | free(keyBlock);\r | |
1058 | return 2;\r | |
1059 | }\r | |
1060 | keyBlock = p;\r | |
1061 | }\r | |
1062 | memset(keyBlock + 6 * keycnt, 0, 6);\r | |
99a71a0d | 1063 | num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);\r |
125a98a1 | 1064 | PrintAndLog("chk custom key[%d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r |
aea4d766 | 1065 | keycnt++;\r |
1066 | }\r | |
aea4d766 | 1067 | } else {\r |
1068 | PrintAndLog("File: %s: not found or locked.", filename);\r | |
1069 | free(keyBlock);\r | |
1070 | return 1;\r | |
1071 | fclose(f);\r | |
1072 | }\r | |
9ca155ba | 1073 | }\r |
9ca155ba M |
1074 | }\r |
1075 | \r | |
1076 | if (keycnt == 0) {\r | |
aea4d766 | 1077 | PrintAndLog("No key specified,try default keys");\r |
1078 | for (;keycnt <=12; keycnt++)\r | |
1079 | PrintAndLog("chk default key[%d] %02x%02x%02x%02x%02x%02x", keycnt,\r | |
1080 | (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r | |
1081 | (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r | |
9ca155ba | 1082 | }\r |
9ca155ba | 1083 | \r |
aea4d766 | 1084 | for ( int t = !keyType ; t < 2 ; keyType==2?(t++):(t=2) ) {\r |
1085 | int b=blockNo;\r | |
1086 | for (int i=0; i<SectorsCnt; ++i) {\r | |
1087 | PrintAndLog("--SectorsCnt:%d block no:0x%02x key type:%C key count:%d ", i, b, t?'B':'A', keycnt);\r | |
9492e0b0 | 1088 | uint32_t max_keys = keycnt>USB_CMD_DATA_SIZE/6?USB_CMD_DATA_SIZE/6:keycnt;\r |
1089 | for (uint32_t c = 0; c < keycnt; c+=max_keys) {\r | |
1090 | uint32_t size = keycnt-c>max_keys?max_keys:keycnt-c;\r | |
1091 | res = mfCheckKeys(b, t, size, &keyBlock[6*c], &key64);\r | |
aea4d766 | 1092 | if (res !=1) {\r |
1093 | if (!res) {\r | |
125a98a1 | 1094 | PrintAndLog("Found valid key:[%012"llx"]",key64);\r |
aea4d766 | 1095 | if (transferToEml) {\r |
1096 | uint8_t block[16];\r | |
1097 | mfEmlGetMem(block, get_trailer_block(b), 1);\r | |
1098 | num_to_bytes(key64, 6, block + t*10);\r | |
1099 | mfEmlSetMem(block, get_trailer_block(b), 1);\r | |
1100 | }\r | |
aea4d766 | 1101 | }\r |
1102 | } else {\r | |
1103 | PrintAndLog("Command execute timeout");\r | |
1104 | }\r | |
1105 | }\r | |
1106 | b<127?(b+=4):(b+=16); \r | |
1107 | }\r | |
9ca155ba | 1108 | }\r |
aea4d766 | 1109 | \r |
1110 | free(keyBlock);\r | |
9ca155ba | 1111 | \r |
aea4d766 | 1112 | /*\r |
1113 | // Create dump file\r | |
1114 | if (createDumpFile) {\r | |
1115 | if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) { \r | |
1116 | PrintAndLog("Could not create file dumpkeys.bin");\r | |
1117 | free(e_sector);\r | |
1118 | return 1;\r | |
1119 | }\r | |
1120 | PrintAndLog("Printing keys to bynary file dumpkeys.bin...");\r | |
1121 | for(i=0; i<16; i++) {\r | |
1122 | if (e_sector[i].foundKey[0]){\r | |
1123 | num_to_bytes(e_sector[i].Key[0], 6, tempkey);\r | |
1124 | fwrite ( tempkey, 1, 6, fkeys );\r | |
1125 | }\r | |
1126 | else{\r | |
1127 | fwrite ( &standart, 1, 6, fkeys );\r | |
1128 | }\r | |
1129 | }\r | |
1130 | for(i=0; i<16; i++) {\r | |
1131 | if (e_sector[i].foundKey[1]){\r | |
1132 | num_to_bytes(e_sector[i].Key[1], 6, tempkey);\r | |
1133 | fwrite ( tempkey, 1, 6, fkeys );\r | |
1134 | }\r | |
1135 | else{\r | |
1136 | fwrite ( &standart, 1, 6, fkeys );\r | |
1137 | }\r | |
1138 | }\r | |
1139 | fclose(fkeys);\r | |
1140 | }\r | |
1141 | */\r | |
9ca155ba M |
1142 | return 0;\r |
1143 | }\r | |
1144 | \r | |
1145 | int CmdHF14AMf1kSim(const char *Cmd)\r | |
1146 | {\r | |
1147 | uint8_t uid[4] = {0, 0, 0, 0};\r | |
1148 | \r | |
1149 | if (param_getchar(Cmd, 0) == 'h') {\r | |
1150 | PrintAndLog("Usage: hf mf sim <uid (8 hex symbols)>");\r | |
1151 | PrintAndLog(" sample: hf mf sim 0a0a0a0a ");\r | |
1152 | return 0;\r | |
1153 | } \r | |
1154 | \r | |
1155 | if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {\r | |
1156 | PrintAndLog("UID must include 8 HEX symbols");\r | |
1157 | return 1;\r | |
1158 | }\r | |
1159 | PrintAndLog(" uid:%s ", sprint_hex(uid, 4));\r | |
1160 | \r | |
1161 | UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {0, 0, 0}};\r | |
1162 | memcpy(c.d.asBytes, uid, 4);\r | |
1163 | SendCommand(&c);\r | |
1164 | \r | |
1165 | return 0;\r | |
1166 | }\r | |
1167 | \r | |
1168 | int CmdHF14AMfDbg(const char *Cmd)\r | |
1169 | {\r | |
8556b852 M |
1170 | int dbgMode = param_get32ex(Cmd, 0, 0, 10);\r |
1171 | if (dbgMode > 4) {\r | |
1172 | PrintAndLog("Max debud mode parameter is 4 \n");\r | |
1173 | }\r | |
1174 | \r | |
1175 | if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {\r | |
9ca155ba M |
1176 | PrintAndLog("Usage: hf mf dbg <debug level>");\r |
1177 | PrintAndLog(" 0 - no debug messages");\r | |
1178 | PrintAndLog(" 1 - error messages");\r | |
1179 | PrintAndLog(" 2 - all messages");\r | |
1180 | PrintAndLog(" 4 - extended debug mode");\r | |
1181 | return 0;\r | |
1182 | } \r | |
1183 | \r | |
8556b852 M |
1184 | UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};\r |
1185 | SendCommand(&c);\r | |
1186 | \r | |
9ca155ba M |
1187 | return 0;\r |
1188 | }\r | |
1189 | \r | |
1190 | int CmdHF14AMfEGet(const char *Cmd)\r | |
1191 | {\r | |
8556b852 M |
1192 | uint8_t blockNo = 0;\r |
1193 | uint8_t data[3 * 16];\r | |
1194 | int i;\r | |
1195 | \r | |
1196 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1197 | PrintAndLog("Usage: hf mf eget <block number>");\r | |
1198 | PrintAndLog(" sample: hf mf eget 0 ");\r | |
1199 | return 0;\r | |
1200 | } \r | |
1201 | \r | |
1202 | blockNo = param_get8(Cmd, 0);\r | |
8556b852 M |
1203 | \r |
1204 | PrintAndLog(" ");\r | |
1205 | if (!mfEmlGetMem(data, blockNo, 3)) {\r | |
1206 | for (i = 0; i < 3; i++) {\r | |
1207 | PrintAndLog("data[%d]:%s", blockNo + i, sprint_hex(data + i * 16, 16));\r | |
1208 | }\r | |
1209 | } else {\r | |
1210 | PrintAndLog("Command execute timeout");\r | |
1211 | }\r | |
1212 | \r | |
1213 | return 0;\r | |
1214 | }\r | |
1215 | \r | |
1216 | int CmdHF14AMfEClear(const char *Cmd)\r | |
1217 | {\r | |
1218 | if (param_getchar(Cmd, 0) == 'h') {\r | |
1219 | PrintAndLog("Usage: hf mf eclr");\r | |
1220 | PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");\r | |
1221 | return 0;\r | |
1222 | } \r | |
1223 | \r | |
1224 | UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};\r | |
1225 | SendCommand(&c);\r | |
9ca155ba M |
1226 | return 0;\r |
1227 | }\r | |
1228 | \r | |
1229 | int CmdHF14AMfESet(const char *Cmd)\r | |
1230 | {\r | |
8556b852 M |
1231 | uint8_t memBlock[16];\r |
1232 | uint8_t blockNo = 0;\r | |
1233 | \r | |
1234 | memset(memBlock, 0x00, sizeof(memBlock));\r | |
1235 | \r | |
1236 | if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {\r | |
1237 | PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");\r | |
1238 | PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");\r | |
1239 | return 0;\r | |
1240 | } \r | |
1241 | \r | |
1242 | blockNo = param_get8(Cmd, 0);\r | |
8556b852 M |
1243 | \r |
1244 | if (param_gethex(Cmd, 1, memBlock, 32)) {\r | |
1245 | PrintAndLog("block data must include 32 HEX symbols");\r | |
1246 | return 1;\r | |
1247 | }\r | |
1248 | \r | |
1249 | // 1 - blocks count\r | |
1250 | UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};\r | |
1251 | memcpy(c.d.asBytes, memBlock, 16);\r | |
1252 | SendCommand(&c);\r | |
9ca155ba M |
1253 | return 0;\r |
1254 | }\r | |
1255 | \r | |
1256 | int CmdHF14AMfELoad(const char *Cmd)\r | |
1257 | {\r | |
ab8b654e M |
1258 | FILE * f;\r |
1259 | char filename[20];\r | |
1260 | char * fnameptr = filename;\r | |
1261 | char buf[64];\r | |
1262 | uint8_t buf8[64];\r | |
1263 | int i, len, blockNum;\r | |
1264 | \r | |
1265 | memset(filename, 0, sizeof(filename));\r | |
1266 | memset(buf, 0, sizeof(buf));\r | |
1267 | \r | |
aea4d766 | 1268 | if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {\r |
ab8b654e M |
1269 | PrintAndLog("It loads emul dump from the file `filename.eml`");\r |
1270 | PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");\r | |
1271 | PrintAndLog(" sample: hf mf eload filename");\r | |
1272 | return 0;\r | |
1273 | } \r | |
1274 | \r | |
1275 | len = strlen(Cmd);\r | |
1276 | if (len > 14) len = 14;\r | |
ab8b654e M |
1277 | \r |
1278 | memcpy(filename, Cmd, len);\r | |
1279 | fnameptr += len;\r | |
1280 | \r | |
1281 | sprintf(fnameptr, ".eml"); \r | |
1282 | \r | |
1283 | // open file\r | |
1284 | f = fopen(filename, "r");\r | |
1285 | if (f == NULL) {\r | |
1286 | PrintAndLog("File not found or locked.");\r | |
1287 | return 1;\r | |
1288 | }\r | |
1289 | \r | |
1290 | blockNum = 0;\r | |
1291 | while(!feof(f)){\r | |
1292 | memset(buf, 0, sizeof(buf));\r | |
759c16b3 | 1293 | if (fgets(buf, sizeof(buf), f) == NULL) {\r |
1294 | PrintAndLog("File reading error.");\r | |
1295 | return 2;\r | |
1296 | }\r | |
aea4d766 | 1297 | \r |
ab8b654e | 1298 | if (strlen(buf) < 32){\r |
aea4d766 | 1299 | if(strlen(buf) && feof(f))\r |
1300 | break;\r | |
ab8b654e M |
1301 | PrintAndLog("File content error. Block data must include 32 HEX symbols");\r |
1302 | return 2;\r | |
1303 | }\r | |
1304 | for (i = 0; i < 32; i += 2)\r | |
1305 | sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r | |
1306 | // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));\r | |
1307 | \r | |
1308 | if (mfEmlSetMem(buf8, blockNum, 1)) {\r | |
1309 | PrintAndLog("Cant set emul block: %d", blockNum);\r | |
1310 | return 3;\r | |
1311 | }\r | |
1312 | blockNum++;\r | |
1313 | \r | |
aea4d766 | 1314 | if (blockNum >= 32 * 4 + 8 * 16) break;\r |
ab8b654e M |
1315 | }\r |
1316 | fclose(f);\r | |
1317 | \r | |
aea4d766 | 1318 | if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){\r |
ab8b654e M |
1319 | PrintAndLog("File content error. There must be 64 blocks");\r |
1320 | return 4;\r | |
1321 | }\r | |
1322 | PrintAndLog("Loaded from file: %s", filename);\r | |
9ca155ba M |
1323 | return 0;\r |
1324 | }\r | |
1325 | \r | |
1326 | int CmdHF14AMfESave(const char *Cmd)\r | |
1327 | {\r | |
ab8b654e M |
1328 | FILE * f;\r |
1329 | char filename[20];\r | |
1330 | char * fnameptr = filename;\r | |
1331 | uint8_t buf[64];\r | |
1332 | int i, j, len;\r | |
1333 | \r | |
1334 | memset(filename, 0, sizeof(filename));\r | |
1335 | memset(buf, 0, sizeof(buf));\r | |
1336 | \r | |
1337 | if (param_getchar(Cmd, 0) == 'h') {\r | |
1338 | PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");\r | |
1339 | PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");\r | |
1340 | PrintAndLog(" sample: hf mf esave ");\r | |
1341 | PrintAndLog(" hf mf esave filename");\r | |
1342 | return 0;\r | |
1343 | } \r | |
1344 | \r | |
1345 | len = strlen(Cmd);\r | |
1346 | if (len > 14) len = 14;\r | |
1347 | \r | |
1348 | if (len < 1) {\r | |
1349 | // get filename\r | |
1350 | if (mfEmlGetMem(buf, 0, 1)) {\r | |
1351 | PrintAndLog("Cant get block: %d", 0);\r | |
1352 | return 1;\r | |
1353 | }\r | |
1354 | for (j = 0; j < 7; j++, fnameptr += 2)\r | |
1355 | sprintf(fnameptr, "%02x", buf[j]); \r | |
1356 | } else {\r | |
1357 | memcpy(filename, Cmd, len);\r | |
1358 | fnameptr += len;\r | |
1359 | }\r | |
1360 | \r | |
1361 | sprintf(fnameptr, ".eml"); \r | |
1362 | \r | |
1363 | // open file\r | |
1364 | f = fopen(filename, "w+");\r | |
1365 | \r | |
1366 | // put hex\r | |
aea4d766 | 1367 | for (i = 0; i < 32 * 4 + 8 * 16; i++) {\r |
ab8b654e M |
1368 | if (mfEmlGetMem(buf, i, 1)) {\r |
1369 | PrintAndLog("Cant get block: %d", i);\r | |
1370 | break;\r | |
1371 | }\r | |
1372 | for (j = 0; j < 16; j++)\r | |
1373 | fprintf(f, "%02x", buf[j]); \r | |
1374 | fprintf(f,"\n");\r | |
1375 | }\r | |
1376 | fclose(f);\r | |
1377 | \r | |
1378 | PrintAndLog("Saved to file: %s", filename);\r | |
1379 | \r | |
9ca155ba M |
1380 | return 0;\r |
1381 | }\r | |
1382 | \r | |
26fdb4ab | 1383 | int CmdHF14AMfECFill(const char *Cmd)\r |
1384 | {\r | |
8556b852 M |
1385 | uint8_t keyType = 0;\r |
1386 | \r | |
1387 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1388 | PrintAndLog("Usage: hf mf efill <key A/B>");\r | |
1389 | PrintAndLog("sample: hf mf efill A");\r | |
1390 | PrintAndLog("Card data blocks transfers to card emulator memory.");\r | |
1391 | PrintAndLog("Keys must be laid in the simulator memory. \n");\r | |
1392 | return 0;\r | |
1393 | } \r | |
1394 | \r | |
1395 | char ctmp = param_getchar(Cmd, 0);\r | |
1396 | if (ctmp == 0x00) {\r | |
1397 | PrintAndLog("Key type must be A or B");\r | |
1398 | return 1;\r | |
1399 | }\r | |
1400 | if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r | |
1401 | \r | |
1402 | UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {0, keyType, 0}};\r | |
1403 | SendCommand(&c);\r | |
1404 | return 0;\r | |
1405 | }\r | |
1406 | \r | |
26fdb4ab | 1407 | int CmdHF14AMfEKeyPrn(const char *Cmd)\r |
1408 | {\r | |
aea4d766 | 1409 | int i,b=-1;\r |
8556b852 M |
1410 | uint8_t data[16];\r |
1411 | uint64_t keyA, keyB;\r | |
1412 | \r | |
1413 | PrintAndLog("|---|----------------|----------------|");\r | |
1414 | PrintAndLog("|sec|key A |key B |");\r | |
1415 | PrintAndLog("|---|----------------|----------------|");\r | |
aea4d766 | 1416 | for (i = 0; i < 40; i++) {\r |
1417 | b<127?(b+=4):(b+=16);\r | |
1418 | if (mfEmlGetMem(data, b, 1)) {\r | |
1419 | PrintAndLog("error get block %d", b);\r | |
8556b852 M |
1420 | break;\r |
1421 | }\r | |
1422 | keyA = bytes_to_num(data, 6);\r | |
1423 | keyB = bytes_to_num(data + 10, 6);\r | |
125a98a1 | 1424 | PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB);\r |
8556b852 M |
1425 | }\r |
1426 | PrintAndLog("|---|----------------|----------------|");\r | |
1427 | \r | |
1428 | return 0;\r | |
1429 | }\r | |
1430 | \r | |
0675f200 M |
1431 | int CmdHF14AMfCSetUID(const char *Cmd)\r |
1432 | {\r | |
1433 | uint8_t wipeCard = 0;\r | |
1434 | uint8_t uid[8];\r | |
1435 | uint8_t oldUid[8];\r | |
1436 | int res;\r | |
1437 | \r | |
1438 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1439 | PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");\r | |
1440 | PrintAndLog("sample: hf mf csetuid 01020304 w");\r | |
1441 | PrintAndLog("Set UID for magic Chinese card (only works with!!!)");\r | |
1442 | PrintAndLog("If you want wipe card then add 'w' into command line. \n");\r | |
1443 | return 0;\r | |
1444 | } \r | |
1445 | \r | |
1446 | if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {\r | |
1447 | PrintAndLog("UID must include 8 HEX symbols");\r | |
1448 | return 1;\r | |
1449 | }\r | |
1450 | \r | |
1451 | char ctmp = param_getchar(Cmd, 1);\r | |
1452 | if (ctmp == 'w' || ctmp == 'W') wipeCard = 1;\r | |
1453 | \r | |
1454 | PrintAndLog("--wipe card:%02x uid:%s", wipeCard, sprint_hex(uid, 4));\r | |
1455 | \r | |
1456 | res = mfCSetUID(uid, oldUid, wipeCard);\r | |
1457 | if (res) {\r | |
1458 | PrintAndLog("Can't set UID. error=%d", res);\r | |
1459 | return 1;\r | |
1460 | }\r | |
1461 | \r | |
1462 | PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));\r | |
1463 | return 0;\r | |
1464 | }\r | |
1465 | \r | |
1466 | int CmdHF14AMfCSetBlk(const char *Cmd)\r | |
1467 | {\r | |
f774db95 M |
1468 | uint8_t uid[8];\r |
1469 | uint8_t memBlock[16];\r | |
1470 | uint8_t blockNo = 0;\r | |
1471 | int res;\r | |
1472 | memset(memBlock, 0x00, sizeof(memBlock));\r | |
1473 | \r | |
1474 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1475 | PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");\r | |
1476 | PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");\r | |
1477 | PrintAndLog("Set block data for magic Chinese card (only works with!!!)");\r | |
1478 | PrintAndLog("If you want wipe card then add 'w' into command line. \n");\r | |
1479 | return 0;\r | |
1480 | } \r | |
1481 | \r | |
1482 | blockNo = param_get8(Cmd, 0);\r | |
f774db95 M |
1483 | \r |
1484 | if (param_gethex(Cmd, 1, memBlock, 32)) {\r | |
1485 | PrintAndLog("block data must include 32 HEX symbols");\r | |
1486 | return 1;\r | |
1487 | }\r | |
1488 | \r | |
1489 | PrintAndLog("--block number:%02x data:%s", blockNo, sprint_hex(memBlock, 16));\r | |
1490 | \r | |
208a0166 | 1491 | res = mfCSetBlock(blockNo, memBlock, uid, 0, CSETBLOCK_SINGLE_OPER);\r |
f774db95 M |
1492 | if (res) {\r |
1493 | PrintAndLog("Can't write block. error=%d", res);\r | |
1494 | return 1;\r | |
1495 | }\r | |
1496 | \r | |
1497 | PrintAndLog("UID:%s", sprint_hex(uid, 4));\r | |
0675f200 M |
1498 | return 0;\r |
1499 | }\r | |
1500 | \r | |
1501 | int CmdHF14AMfCLoad(const char *Cmd)\r | |
1502 | {\r | |
208a0166 M |
1503 | FILE * f;\r |
1504 | char filename[20];\r | |
1505 | char * fnameptr = filename;\r | |
1506 | char buf[64];\r | |
1507 | uint8_t buf8[64];\r | |
1508 | uint8_t fillFromEmulator = 0;\r | |
1509 | int i, len, blockNum, flags;\r | |
1510 | \r | |
1511 | memset(filename, 0, sizeof(filename));\r | |
1512 | memset(buf, 0, sizeof(buf));\r | |
1513 | \r | |
1514 | if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {\r | |
1515 | PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");\r | |
1516 | PrintAndLog("or from emulator memory (option `e`)");\r | |
1517 | PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");\r | |
1518 | PrintAndLog(" or: hf mf cload e ");\r | |
1519 | PrintAndLog(" sample: hf mf cload filename");\r | |
1520 | return 0;\r | |
1521 | } \r | |
1522 | \r | |
1523 | char ctmp = param_getchar(Cmd, 0);\r | |
1524 | if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r | |
1525 | \r | |
1526 | if (fillFromEmulator) {\r | |
1527 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
1528 | for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {\r | |
1529 | if (mfEmlGetMem(buf8, blockNum, 1)) {\r | |
1530 | PrintAndLog("Cant get block: %d", blockNum);\r | |
1531 | return 2;\r | |
1532 | }\r | |
1533 | \r | |
1534 | if (blockNum == 2) flags = 0;\r | |
1535 | if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r | |
1536 | \r | |
1537 | if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r | |
1538 | PrintAndLog("Cant set magic card block: %d", blockNum);\r | |
1539 | return 3;\r | |
1540 | }\r | |
1541 | }\r | |
1542 | return 0;\r | |
1543 | } else {\r | |
1544 | len = strlen(Cmd);\r | |
1545 | if (len > 14) len = 14;\r | |
1546 | \r | |
1547 | memcpy(filename, Cmd, len);\r | |
1548 | fnameptr += len;\r | |
1549 | \r | |
1550 | sprintf(fnameptr, ".eml"); \r | |
1551 | \r | |
1552 | // open file\r | |
1553 | f = fopen(filename, "r");\r | |
1554 | if (f == NULL) {\r | |
1555 | PrintAndLog("File not found or locked.");\r | |
1556 | return 1;\r | |
1557 | }\r | |
1558 | \r | |
1559 | blockNum = 0;\r | |
1560 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
1561 | while(!feof(f)){\r | |
1562 | memset(buf, 0, sizeof(buf));\r | |
759c16b3 | 1563 | if (fgets(buf, sizeof(buf), f) == NULL) {\r |
1564 | PrintAndLog("File reading error.");\r | |
1565 | return 2;\r | |
1566 | }\r | |
208a0166 M |
1567 | \r |
1568 | if (strlen(buf) < 32){\r | |
1569 | if(strlen(buf) && feof(f))\r | |
1570 | break;\r | |
1571 | PrintAndLog("File content error. Block data must include 32 HEX symbols");\r | |
1572 | return 2;\r | |
1573 | }\r | |
1574 | for (i = 0; i < 32; i += 2)\r | |
1575 | sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r | |
1576 | \r | |
1577 | if (blockNum == 2) flags = 0;\r | |
1578 | if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r | |
1579 | \r | |
1580 | if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r | |
1581 | PrintAndLog("Cant set magic card block: %d", blockNum);\r | |
1582 | return 3;\r | |
1583 | }\r | |
1584 | blockNum++;\r | |
1585 | \r | |
1586 | if (blockNum >= 16 * 4) break; // magic card type - mifare 1K\r | |
1587 | }\r | |
1588 | fclose(f);\r | |
1589 | \r | |
1590 | if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){\r | |
1591 | PrintAndLog("File content error. There must be 64 blocks");\r | |
1592 | return 4;\r | |
1593 | }\r | |
1594 | PrintAndLog("Loaded from file: %s", filename);\r | |
1595 | return 0;\r | |
1596 | }\r | |
0675f200 M |
1597 | }\r |
1598 | \r | |
545a1f38 M |
1599 | int CmdHF14AMfCGetBlk(const char *Cmd) {\r |
1600 | uint8_t memBlock[16];\r | |
1601 | uint8_t blockNo = 0;\r | |
1602 | int res;\r | |
1603 | memset(memBlock, 0x00, sizeof(memBlock));\r | |
1604 | \r | |
1605 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1606 | PrintAndLog("Usage: hf mf cgetblk <block number>");\r | |
1607 | PrintAndLog("sample: hf mf cgetblk 1");\r | |
1608 | PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");\r | |
1609 | return 0;\r | |
1610 | } \r | |
1611 | \r | |
1612 | blockNo = param_get8(Cmd, 0);\r | |
545a1f38 M |
1613 | \r |
1614 | PrintAndLog("--block number:%02x ", blockNo);\r | |
1615 | \r | |
1616 | res = mfCGetBlock(blockNo, memBlock, CSETBLOCK_SINGLE_OPER);\r | |
1617 | if (res) {\r | |
1618 | PrintAndLog("Can't read block. error=%d", res);\r | |
1619 | return 1;\r | |
1620 | }\r | |
1621 | \r | |
1622 | PrintAndLog("block data:%s", sprint_hex(memBlock, 16));\r | |
1623 | return 0;\r | |
1624 | }\r | |
1625 | \r | |
1626 | int CmdHF14AMfCGetSc(const char *Cmd) {\r | |
1627 | uint8_t memBlock[16];\r | |
1628 | uint8_t sectorNo = 0;\r | |
1629 | int i, res, flags;\r | |
1630 | memset(memBlock, 0x00, sizeof(memBlock));\r | |
1631 | \r | |
1632 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1633 | PrintAndLog("Usage: hf mf cgetsc <sector number>");\r | |
1634 | PrintAndLog("sample: hf mf cgetsc 0");\r | |
1635 | PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");\r | |
1636 | return 0;\r | |
1637 | } \r | |
1638 | \r | |
1639 | sectorNo = param_get8(Cmd, 0);\r | |
1640 | if (sectorNo > 15) {\r | |
1641 | PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");\r | |
1642 | return 1;\r | |
1643 | }\r | |
1644 | \r | |
1645 | PrintAndLog("--sector number:%02x ", sectorNo);\r | |
1646 | \r | |
1647 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
1648 | for (i = 0; i < 4; i++) {\r | |
1649 | if (i == 1) flags = 0;\r | |
1650 | if (i == 3) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r | |
1651 | \r | |
1652 | res = mfCGetBlock(sectorNo * 4 + i, memBlock, flags);\r | |
1653 | if (res) {\r | |
1654 | PrintAndLog("Can't read block. %02x error=%d", sectorNo * 4 + i, res);\r | |
1655 | return 1;\r | |
1656 | }\r | |
1657 | \r | |
1658 | PrintAndLog("block %02x data:%s", sectorNo * 4 + i, sprint_hex(memBlock, 16));\r | |
1659 | }\r | |
1660 | return 0;\r | |
1661 | }\r | |
1662 | \r | |
1663 | int CmdHF14AMfCSave(const char *Cmd) {\r | |
1664 | \r | |
1665 | FILE * f;\r | |
1666 | char filename[20];\r | |
1667 | char * fnameptr = filename;\r | |
1668 | uint8_t fillFromEmulator = 0;\r | |
1669 | uint8_t buf[64];\r | |
1670 | int i, j, len, flags;\r | |
1671 | \r | |
1672 | memset(filename, 0, sizeof(filename));\r | |
1673 | memset(buf, 0, sizeof(buf));\r | |
1674 | \r | |
1675 | if (param_getchar(Cmd, 0) == 'h') {\r | |
1676 | PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");\r | |
1677 | PrintAndLog("or into emulator memory (option `e`)");\r | |
1678 | PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");\r | |
1679 | PrintAndLog(" sample: hf mf esave ");\r | |
1680 | PrintAndLog(" hf mf esave filename");\r | |
1681 | PrintAndLog(" hf mf esave e \n");\r | |
1682 | return 0;\r | |
1683 | } \r | |
1684 | \r | |
1685 | char ctmp = param_getchar(Cmd, 0);\r | |
1686 | if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r | |
1687 | \r | |
1688 | if (fillFromEmulator) {\r | |
1689 | // put into emulator\r | |
1690 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
1691 | for (i = 0; i < 16 * 4; i++) {\r | |
1692 | if (i == 1) flags = 0;\r | |
1693 | if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r | |
1694 | \r | |
1695 | if (mfCGetBlock(i, buf, flags)) {\r | |
1696 | PrintAndLog("Cant get block: %d", i);\r | |
1697 | break;\r | |
1698 | }\r | |
1699 | \r | |
1700 | if (mfEmlSetMem(buf, i, 1)) {\r | |
1701 | PrintAndLog("Cant set emul block: %d", i);\r | |
1702 | return 3;\r | |
1703 | }\r | |
1704 | }\r | |
1705 | return 0;\r | |
1706 | } else {\r | |
1707 | len = strlen(Cmd);\r | |
1708 | if (len > 14) len = 14;\r | |
1709 | \r | |
1710 | if (len < 1) {\r | |
1711 | // get filename\r | |
1712 | if (mfCGetBlock(0, buf, CSETBLOCK_SINGLE_OPER)) {\r | |
1713 | PrintAndLog("Cant get block: %d", 0);\r | |
1714 | return 1;\r | |
1715 | }\r | |
1716 | for (j = 0; j < 7; j++, fnameptr += 2)\r | |
1717 | sprintf(fnameptr, "%02x", buf[j]); \r | |
1718 | } else {\r | |
1719 | memcpy(filename, Cmd, len);\r | |
1720 | fnameptr += len;\r | |
1721 | }\r | |
1722 | \r | |
1723 | sprintf(fnameptr, ".eml"); \r | |
1724 | \r | |
1725 | // open file\r | |
1726 | f = fopen(filename, "w+");\r | |
1727 | \r | |
1728 | // put hex\r | |
1729 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
1730 | for (i = 0; i < 16 * 4; i++) {\r | |
1731 | if (i == 1) flags = 0;\r | |
1732 | if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r | |
1733 | \r | |
1734 | if (mfCGetBlock(i, buf, flags)) {\r | |
1735 | PrintAndLog("Cant get block: %d", i);\r | |
1736 | break;\r | |
1737 | }\r | |
1738 | for (j = 0; j < 16; j++)\r | |
1739 | fprintf(f, "%02x", buf[j]); \r | |
1740 | fprintf(f,"\n");\r | |
1741 | }\r | |
1742 | fclose(f);\r | |
1743 | \r | |
1744 | PrintAndLog("Saved to file: %s", filename);\r | |
1745 | \r | |
1746 | return 0;\r | |
1747 | }\r | |
1748 | }\r | |
1749 | \r | |
b62a5a84 | 1750 | int CmdHF14AMfSniff(const char *Cmd){\r |
55acbb2a | 1751 | // params\r |
c948cbde M |
1752 | bool wantLogToFile = 0;\r |
1753 | bool wantDecrypt = 0;\r | |
eede7162 | 1754 | //bool wantSaveToEml = 0; TODO\r |
55acbb2a M |
1755 | bool wantSaveToEmlFile = 0;\r |
1756 | \r | |
1757 | //var \r | |
39864b0b M |
1758 | int res = 0;\r |
1759 | int len = 0;\r | |
1760 | int blockLen = 0;\r | |
1761 | int num = 0;\r | |
1762 | int pckNum = 0;\r | |
1763 | uint8_t uid[8];\r | |
1764 | uint8_t atqa[2];\r | |
1765 | uint8_t sak;\r | |
1766 | bool isTag;\r | |
71d90e54 | 1767 | uint32_t parity;\r |
39864b0b M |
1768 | uint8_t buf[3000];\r |
1769 | uint8_t * bufPtr = buf;\r | |
1770 | memset(buf, 0x00, 3000);\r | |
b62a5a84 M |
1771 | \r |
1772 | if (param_getchar(Cmd, 0) == 'h') {\r | |
55acbb2a M |
1773 | PrintAndLog("It continuously get data from the field and saves it to: log, emulator, emulator file.");\r |
1774 | PrintAndLog("You can specify:");\r | |
1775 | PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");\r | |
1776 | PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");\r | |
1777 | PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");\r | |
e0c635d1 | 1778 | PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");\r |
55acbb2a M |
1779 | PrintAndLog("Usage: hf mf sniff [l][d][e][r]");\r |
1780 | PrintAndLog(" sample: hf mf sniff l d e");\r | |
b62a5a84 M |
1781 | return 0;\r |
1782 | } \r | |
1783 | \r | |
55acbb2a M |
1784 | for (int i = 0; i < 4; i++) {\r |
1785 | char ctmp = param_getchar(Cmd, i);\r | |
1786 | if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;\r | |
1787 | if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;\r | |
eede7162 | 1788 | //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO\r |
55acbb2a M |
1789 | if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;\r |
1790 | }\r | |
1791 | \r | |
39864b0b M |
1792 | printf("-------------------------------------------------------------------------\n");\r |
1793 | printf("Executing command. \n");\r | |
1794 | printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r | |
1795 | printf("Press the key on pc keyboard to abort the client.\n");\r | |
1796 | printf("-------------------------------------------------------------------------\n");\r | |
1797 | \r | |
b62a5a84 M |
1798 | UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};\r |
1799 | SendCommand(&c);\r | |
1800 | \r | |
39864b0b M |
1801 | // wait cycle\r |
1802 | while (true) {\r | |
1803 | printf(".");\r | |
1804 | fflush(stdout);\r | |
1805 | if (ukbhit()) {\r | |
1806 | getchar();\r | |
1807 | printf("\naborted via keyboard!\n");\r | |
1808 | break;\r | |
1809 | }\r | |
1810 | \r | |
902cb3c0 | 1811 | UsbCommand resp;\r |
1812 | if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {\r | |
1813 | res = resp.arg[0] & 0xff;\r | |
1814 | len = resp.arg[1];\r | |
1815 | num = resp.arg[2];\r | |
39864b0b M |
1816 | \r |
1817 | if (res == 0) return 0;\r | |
1818 | if (res == 1) {\r | |
1819 | if (num ==0) {\r | |
1820 | bufPtr = buf;\r | |
1821 | memset(buf, 0x00, 3000);\r | |
1822 | }\r | |
902cb3c0 | 1823 | memcpy(bufPtr, resp.d.asBytes, len);\r |
39864b0b M |
1824 | bufPtr += len;\r |
1825 | pckNum++;\r | |
1826 | }\r | |
1827 | if (res == 2) {\r | |
1828 | blockLen = bufPtr - buf;\r | |
1829 | bufPtr = buf;\r | |
1830 | printf(">\n");\r | |
1831 | PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);\r | |
1832 | num = 0;\r | |
1833 | while (bufPtr - buf + 9 < blockLen) {\r | |
1834 | isTag = bufPtr[3] & 0x80 ? true:false;\r | |
71d90e54 M |
1835 | bufPtr += 4;\r |
1836 | parity = *((uint32_t *)(bufPtr));\r | |
1837 | bufPtr += 4;\r | |
39864b0b M |
1838 | len = bufPtr[0];\r |
1839 | bufPtr++;\r | |
1840 | if ((len == 14) && (bufPtr[0] = 0xff) && (bufPtr[1] = 0xff)) {\r | |
1841 | memcpy(uid, bufPtr + 2, 7);\r | |
1842 | memcpy(atqa, bufPtr + 2 + 7, 2);\r | |
1843 | sak = bufPtr[11];\r | |
55acbb2a | 1844 | \r |
39864b0b | 1845 | PrintAndLog("tag select uid:%s atqa:%02x %02x sak:0x%02x", sprint_hex(uid, 7), atqa[0], atqa[1], sak);\r |
55acbb2a | 1846 | if (wantLogToFile) {\r |
e0c635d1 | 1847 | FillFileNameByUID(logHexFileName, uid, ".log", 7);\r |
55acbb2a M |
1848 | AddLogCurrentDT(logHexFileName);\r |
1849 | } \r | |
e0c635d1 | 1850 | if (wantDecrypt) mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);\r |
39864b0b M |
1851 | } else {\r |
1852 | PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));\r | |
55acbb2a | 1853 | if (wantLogToFile) AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);\r |
71d90e54 | 1854 | if (wantDecrypt) mfTraceDecode(bufPtr, len, parity, wantSaveToEmlFile);\r |
39864b0b M |
1855 | }\r |
1856 | bufPtr += len;\r | |
1857 | num++;\r | |
1858 | }\r | |
1859 | }\r | |
1860 | } // resp not NILL\r | |
1861 | } // while (true)\r | |
b62a5a84 M |
1862 | return 0;\r |
1863 | }\r | |
1864 | \r | |
26fdb4ab | 1865 | static command_t CommandTable[] =\r |
9ca155ba | 1866 | {\r |
981bd429 | 1867 | {"help", CmdHelp, 1, "This help"}, |
1868 | {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"}, | |
1869 | {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"}, | |
1870 | {"urdbl", CmdHF14AMfURdBl, 0, "Read MIFARE Ultralight block"}, | |
1871 | {"urdcard", CmdHF14AMfURdCard, 0,"Read MIFARE Ultralight Card"}, | |
1872 | {"uwrbl", CmdHF14AMfUWrBl, 0,"Write MIFARE Ultralight block"}, | |
1873 | {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"}, | |
1874 | {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"}, | |
1875 | {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"}, | |
9ca155ba | 1876 | {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},\r |
545a1f38 | 1877 | {"chk", CmdHF14AMfChk, 0, "Test block keys"},\r |
9ca155ba M |
1878 | {"mifare", CmdHF14AMifare, 0, "Read parity error messages. param - <used card nonce>"},\r |
1879 | {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},\r | |
b62a5a84 | 1880 | {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},\r |
545a1f38 M |
1881 | {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},\r |
1882 | {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},\r | |
51969283 M |
1883 | {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},\r |
1884 | {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},\r | |
9ca155ba M |
1885 | {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},\r |
1886 | {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},\r | |
8556b852 M |
1887 | {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},\r |
1888 | {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},\r | |
0675f200 | 1889 | {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},\r |
f774db95 | 1890 | {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block into magic Chinese card"},\r |
545a1f38 M |
1891 | {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block from magic Chinese card"},\r |
1892 | {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector from magic Chinese card"},\r | |
208a0166 | 1893 | {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},\r |
545a1f38 | 1894 | {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},\r |
9ca155ba M |
1895 | {NULL, NULL, 0, NULL}\r |
1896 | };\r | |
1897 | \r | |
1898 | int CmdHFMF(const char *Cmd)\r | |
1899 | {\r | |
1900 | // flush\r | |
7dd1908b | 1901 | WaitForResponseTimeout(CMD_ACK,NULL,100);\r |
9ca155ba M |
1902 | \r |
1903 | CmdsParse(CommandTable, Cmd);\r | |
1904 | return 0;\r | |
1905 | }\r | |
1906 | \r | |
1907 | int CmdHelp(const char *Cmd)\r | |
1908 | {\r | |
1909 | CmdsHelp(CommandTable);\r | |
1910 | return 0;\r | |
1911 | }\r |