]>
Commit | Line | Data |
---|---|---|
9ca155ba M |
1 | //-----------------------------------------------------------------------------\r |
2 | // Copyright (C) 2011 Merlok\r | |
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 | |
8556b852 | 12 | #include "proxmark3.h"\r |
9ca155ba M |
13 | \r |
14 | static int CmdHelp(const char *Cmd);\r | |
15 | \r | |
9ca155ba M |
16 | int CmdHF14AMifare(const char *Cmd)\r |
17 | {\r | |
18 | uint32_t uid = 0;\r | |
19 | uint32_t nt = 0;\r | |
20 | uint64_t par_list = 0, ks_list = 0, r_key = 0;\r | |
21 | uint8_t isOK = 0;\r | |
22 | uint8_t keyBlock[6] = {0,0,0,0,0,0};\r | |
23 | \r | |
24 | if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, keyBlock, 8)) {\r | |
25 | PrintAndLog("Nt must include 8 HEX symbols");\r | |
26 | return 1;\r | |
27 | }\r | |
28 | \r | |
29 | UsbCommand c = {CMD_READER_MIFARE, {(uint32_t)bytes_to_num(keyBlock, 4), 0, 0}};\r | |
30 | SendCommand(&c);\r | |
31 | \r | |
32 | //flush queue\r | |
33 | while (ukbhit()) getchar();\r | |
34 | \r | |
35 | // message\r | |
36 | printf("-------------------------------------------------------------------------\n");\r | |
37 | printf("Executing command. It may take up to 30 min.\n");\r | |
38 | printf("Press the key on proxmark3 device to abort proxmark3.\n");\r | |
39 | printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r | |
40 | printf("-------------------------------------------------------------------------\n");\r | |
41 | \r | |
42 | // wait cycle\r | |
43 | while (true) {\r | |
44 | printf(".");\r | |
45 | if (ukbhit()) {\r | |
46 | getchar();\r | |
47 | printf("\naborted via keyboard!\n");\r | |
48 | break;\r | |
49 | }\r | |
50 | \r | |
51 | UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 2000);\r | |
52 | if (resp != NULL) {\r | |
53 | isOK = resp->arg[0] & 0xff;\r | |
54 | \r | |
55 | uid = (uint32_t)bytes_to_num(resp->d.asBytes + 0, 4);\r | |
56 | nt = (uint32_t)bytes_to_num(resp->d.asBytes + 4, 4);\r | |
57 | par_list = bytes_to_num(resp->d.asBytes + 8, 8);\r | |
58 | ks_list = bytes_to_num(resp->d.asBytes + 16, 8);\r | |
59 | \r | |
60 | printf("\n\n");\r | |
61 | PrintAndLog("isOk:%02x", isOK);\r | |
62 | if (!isOK) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n");\r | |
63 | break;\r | |
64 | }\r | |
65 | } \r | |
66 | printf("\n");\r | |
67 | \r | |
68 | // error\r | |
69 | if (isOK != 1) return 1;\r | |
70 | \r | |
71 | // execute original function from util nonce2key\r | |
72 | if (nonce2key(uid, nt, par_list, ks_list, &r_key)) return 2;\r | |
73 | printf("------------------------------------------------------------------\n");\r | |
74 | PrintAndLog("Key found:%012llx \n", r_key);\r | |
75 | \r | |
76 | num_to_bytes(r_key, 6, keyBlock);\r | |
77 | isOK = mfCheckKeys(0, 0, 1, keyBlock, &r_key);\r | |
78 | if (!isOK) \r | |
79 | PrintAndLog("Found valid key:%012llx", r_key);\r | |
80 | else\r | |
81 | PrintAndLog("Found invalid key. ( Nt=%08x", nt); \r | |
82 | \r | |
83 | \r | |
84 | return 0;\r | |
85 | }\r | |
86 | \r | |
87 | int CmdHF14AMfWrBl(const char *Cmd)\r | |
88 | {\r | |
89 | uint8_t blockNo = 0;\r | |
90 | uint8_t keyType = 0;\r | |
91 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
92 | uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r | |
93 | \r | |
94 | char cmdp = 0x00;\r | |
95 | \r | |
96 | if (strlen(Cmd)<3) {\r | |
97 | PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");\r | |
98 | PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");\r | |
99 | return 0;\r | |
100 | } \r | |
101 | \r | |
102 | blockNo = param_get8(Cmd, 0);\r | |
103 | cmdp = param_getchar(Cmd, 1);\r | |
104 | if (cmdp == 0x00) {\r | |
105 | PrintAndLog("Key type must be A or B");\r | |
106 | return 1;\r | |
107 | }\r | |
108 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
109 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
110 | PrintAndLog("Key must include 12 HEX symbols");\r | |
111 | return 1;\r | |
112 | }\r | |
113 | if (param_gethex(Cmd, 3, bldata, 32)) {\r | |
114 | PrintAndLog("Block data must include 32 HEX symbols");\r | |
115 | return 1;\r | |
116 | }\r | |
117 | PrintAndLog("--block no:%02x key type:%02x key:%s", blockNo, keyType, sprint_hex(key, 6));\r | |
118 | PrintAndLog("--data: %s", sprint_hex(bldata, 16));\r | |
119 | \r | |
120 | UsbCommand c = {CMD_MIFARE_WRITEBL, {blockNo, keyType, 0}};\r | |
121 | memcpy(c.d.asBytes, key, 6);\r | |
122 | memcpy(c.d.asBytes + 10, bldata, 16);\r | |
123 | SendCommand(&c);\r | |
124 | UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);\r | |
125 | \r | |
126 | if (resp != NULL) {\r | |
127 | uint8_t isOK = resp->arg[0] & 0xff;\r | |
128 | \r | |
129 | PrintAndLog("isOk:%02x", isOK);\r | |
130 | } else {\r | |
131 | PrintAndLog("Command execute timeout");\r | |
132 | }\r | |
133 | \r | |
134 | return 0;\r | |
135 | }\r | |
136 | \r | |
137 | int CmdHF14AMfRdBl(const char *Cmd)\r | |
138 | {\r | |
139 | uint8_t blockNo = 0;\r | |
140 | uint8_t keyType = 0;\r | |
141 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
142 | \r | |
143 | char cmdp = 0x00;\r | |
144 | \r | |
145 | \r | |
146 | if (strlen(Cmd)<3) {\r | |
147 | PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");\r | |
148 | PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");\r | |
149 | return 0;\r | |
150 | } \r | |
151 | \r | |
152 | blockNo = param_get8(Cmd, 0);\r | |
153 | cmdp = param_getchar(Cmd, 1);\r | |
154 | if (cmdp == 0x00) {\r | |
155 | PrintAndLog("Key type must be A or B");\r | |
156 | return 1;\r | |
157 | }\r | |
158 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
159 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
160 | PrintAndLog("Key must include 12 HEX symbols");\r | |
161 | return 1;\r | |
162 | }\r | |
163 | PrintAndLog("--block no:%02x key type:%02x key:%s ", blockNo, keyType, sprint_hex(key, 6));\r | |
164 | \r | |
165 | UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};\r | |
166 | memcpy(c.d.asBytes, key, 6);\r | |
167 | SendCommand(&c);\r | |
168 | UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);\r | |
169 | \r | |
170 | if (resp != NULL) {\r | |
171 | uint8_t isOK = resp->arg[0] & 0xff;\r | |
172 | uint8_t * data = resp->d.asBytes;\r | |
173 | \r | |
174 | if (isOK)\r | |
175 | PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 16));\r | |
176 | else\r | |
177 | PrintAndLog("isOk:%02x", isOK);\r | |
178 | } else {\r | |
179 | PrintAndLog("Command execute timeout");\r | |
180 | }\r | |
181 | \r | |
182 | return 0;\r | |
183 | }\r | |
184 | \r | |
185 | int CmdHF14AMfRdSc(const char *Cmd)\r | |
186 | {\r | |
187 | int i;\r | |
188 | uint8_t sectorNo = 0;\r | |
189 | uint8_t keyType = 0;\r | |
190 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
191 | \r | |
192 | uint8_t isOK = 0;\r | |
193 | uint8_t * data = NULL;\r | |
194 | \r | |
195 | char cmdp = 0x00;\r | |
196 | \r | |
197 | if (strlen(Cmd)<3) {\r | |
198 | PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");\r | |
199 | PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");\r | |
200 | return 0;\r | |
201 | } \r | |
202 | \r | |
203 | sectorNo = param_get8(Cmd, 0);\r | |
204 | if (sectorNo > 63) {\r | |
205 | PrintAndLog("Sector number must be less than 64");\r | |
206 | return 1;\r | |
207 | }\r | |
208 | cmdp = param_getchar(Cmd, 1);\r | |
209 | if (cmdp == 0x00) {\r | |
210 | PrintAndLog("Key type must be A or B");\r | |
211 | return 1;\r | |
212 | }\r | |
213 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
214 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
215 | PrintAndLog("Key must include 12 HEX symbols");\r | |
216 | return 1;\r | |
217 | }\r | |
218 | PrintAndLog("--sector no:%02x key type:%02x key:%s ", sectorNo, keyType, sprint_hex(key, 6));\r | |
219 | \r | |
220 | UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};\r | |
221 | memcpy(c.d.asBytes, key, 6);\r | |
222 | SendCommand(&c);\r | |
223 | UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);\r | |
224 | PrintAndLog(" ");\r | |
225 | \r | |
226 | if (resp != NULL) {\r | |
227 | isOK = resp->arg[0] & 0xff;\r | |
228 | data = resp->d.asBytes;\r | |
229 | \r | |
230 | PrintAndLog("isOk:%02x", isOK);\r | |
231 | if (isOK) \r | |
232 | for (i = 0; i < 2; i++) {\r | |
233 | PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));\r | |
234 | }\r | |
235 | } else {\r | |
236 | PrintAndLog("Command1 execute timeout");\r | |
237 | }\r | |
238 | \r | |
239 | // response2\r | |
240 | resp = WaitForResponseTimeout(CMD_ACK, 500);\r | |
241 | PrintAndLog(" ");\r | |
242 | \r | |
243 | if (resp != NULL) {\r | |
244 | isOK = resp->arg[0] & 0xff;\r | |
245 | data = resp->d.asBytes;\r | |
246 | \r | |
247 | if (isOK) \r | |
248 | for (i = 0; i < 2; i++) {\r | |
249 | PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));\r | |
250 | }\r | |
251 | } else {\r | |
252 | PrintAndLog("Command2 execute timeout");\r | |
253 | }\r | |
254 | \r | |
255 | return 0;\r | |
256 | }\r | |
257 | \r | |
26fdb4ab | 258 | int CmdHF14AMfDump1k(const char *Cmd)\r |
259 | {\r | |
260 | int i, j;\r | |
261 | \r | |
26fdb4ab | 262 | uint8_t keyA[16][6];\r |
263 | uint8_t keyB[16][6];\r | |
264 | uint8_t rights[16][4];\r | |
265 | \r | |
26fdb4ab | 266 | FILE *fin;\r |
267 | FILE *fout;\r | |
268 | \r | |
269 | UsbCommand *resp;\r | |
270 | \r | |
271 | if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {\r | |
272 | PrintAndLog("Could not find file keys.bin");\r | |
273 | return 1;\r | |
274 | }\r | |
275 | \r | |
276 | if ((fout = fopen("dumpdata.bin","wb")) == NULL) { \r | |
277 | PrintAndLog("Could not create file name dump.bin");\r | |
278 | return 1;\r | |
279 | }\r | |
280 | \r | |
281 | // Read key file\r | |
282 | \r | |
283 | for (i=0 ; i<16 ; i++) {\r | |
284 | fread ( keyA[i], 1, 6, fin );\r | |
285 | }\r | |
286 | for (i=0 ; i<16 ; i++) {\r | |
287 | fread ( keyB[i], 1, 6, fin );\r | |
288 | }\r | |
289 | \r | |
290 | // Read access rights to sectors\r | |
291 | \r | |
292 | PrintAndLog("|-----------------------------------------|");\r | |
293 | PrintAndLog("|------ Reading sector access bits...-----|");\r | |
294 | PrintAndLog("|-----------------------------------------|");\r | |
295 | \r | |
296 | for (i = 0 ; i < 16 ; i++) {\r | |
297 | UsbCommand c = {CMD_MIFARE_READBL, {4*i + 3, 0, 0}};\r | |
298 | memcpy(c.d.asBytes, keyA[i], 6);\r | |
299 | SendCommand(&c);\r | |
300 | resp = WaitForResponseTimeout(CMD_ACK, 1500);\r | |
301 | \r | |
302 | if (resp != NULL) {\r | |
303 | uint8_t isOK = resp->arg[0] & 0xff;\r | |
304 | uint8_t *data = resp->d.asBytes;\r | |
305 | if (isOK){\r | |
306 | rights[i][0] = ((data[7] & 0x10)>>4) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>2);\r | |
307 | rights[i][1] = ((data[7] & 0x20)>>5) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>3);\r | |
308 | rights[i][2] = ((data[7] & 0x40)>>6) | ((data[8] & 0x4)>>1) | ((data[8] & 0x40)>>4);\r | |
309 | rights[i][3] = ((data[7] & 0x80)>>7) | ((data[8] & 0x8)>>2) | ((data[8] & 0x80)>>5);\r | |
310 | }\r | |
311 | else{\r | |
312 | PrintAndLog("Could not get access rights for block %d", i);\r | |
313 | }\r | |
314 | }\r | |
315 | else {\r | |
316 | PrintAndLog("Command execute timeout");\r | |
317 | }\r | |
318 | }\r | |
319 | \r | |
320 | // Read blocks and print to file\r | |
321 | \r | |
322 | PrintAndLog("|-----------------------------------------|");\r | |
323 | PrintAndLog("|----- Dumping all blocks to file... -----|");\r | |
324 | PrintAndLog("|-----------------------------------------|");\r | |
325 | \r | |
326 | for (i=0 ; i<16 ; i++) {\r | |
327 | for (j=0 ; j<4 ; j++) {\r | |
26fdb4ab | 328 | if (j == 3){\r |
329 | UsbCommand c = {CMD_MIFARE_READBL, {i*4 + j, 0, 0}};\r | |
330 | memcpy(c.d.asBytes, keyA[i], 6);\r | |
331 | SendCommand(&c);\r | |
332 | resp = WaitForResponseTimeout(CMD_ACK, 1500);\r | |
333 | }\r | |
334 | else{\r | |
335 | if ((rights[i][j] == 6) | (rights[i][j] == 5)) {\r | |
336 | UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 1, 0}};\r | |
337 | memcpy(c.d.asBytes, keyB[i], 6);\r | |
338 | SendCommand(&c);\r | |
339 | resp = WaitForResponseTimeout(CMD_ACK, 1500);\r | |
340 | }\r | |
341 | else if (rights[i][j] == 7) {\r | |
342 | PrintAndLog("Access rights do not allow reading of sector %d block %d",i,j);\r | |
343 | }\r | |
344 | else {\r | |
345 | UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 0, 0}};\r | |
346 | memcpy(c.d.asBytes, keyA[i], 6);\r | |
347 | SendCommand(&c);\r | |
348 | resp = WaitForResponseTimeout(CMD_ACK, 1500);\r | |
349 | }\r | |
350 | }\r | |
351 | \r | |
352 | if (resp != NULL) {\r | |
353 | uint8_t isOK = resp->arg[0] & 0xff;\r | |
354 | uint8_t *data = resp->d.asBytes;\r | |
3d77fdfa | 355 | if (j == 3) {\r |
356 | data[0] = (keyA[i][0]);\r | |
357 | data[1] = (keyA[i][1]);\r | |
358 | data[2] = (keyA[i][2]);\r | |
359 | data[3] = (keyA[i][3]);\r | |
360 | data[4] = (keyA[i][4]);\r | |
361 | data[5] = (keyA[i][5]);\r | |
362 | data[10] = (keyB[i][0]);\r | |
363 | data[11] = (keyB[i][1]);\r | |
364 | data[12] = (keyB[i][2]);\r | |
365 | data[13] = (keyB[i][3]);\r | |
366 | data[14] = (keyB[i][4]);\r | |
367 | data[15] = (keyB[i][5]);\r | |
368 | }\r | |
26fdb4ab | 369 | if (isOK) {\r |
370 | fwrite ( data, 1, 16, fout );\r | |
371 | }\r | |
372 | else {\r | |
373 | PrintAndLog("Could not get access rights for block %d", i);\r | |
374 | }\r | |
375 | }\r | |
376 | else {\r | |
377 | PrintAndLog("Command execute timeout");\r | |
378 | }\r | |
379 | }\r | |
380 | }\r | |
381 | \r | |
382 | fclose(fin);\r | |
383 | fclose(fout);\r | |
384 | \r | |
385 | return 0;\r | |
386 | }\r | |
387 | \r | |
388 | int CmdHF14AMfRestore1k(const char *Cmd)\r | |
389 | {\r | |
390 | \r | |
391 | int i,j;\r | |
26fdb4ab | 392 | uint8_t keyType = 0;\r |
393 | uint8_t key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r | |
394 | uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r | |
395 | uint8_t keyA[16][6];\r | |
396 | uint8_t keyB[16][6];\r | |
397 | \r | |
26fdb4ab | 398 | FILE *fdump;\r |
399 | FILE *fkeys;\r | |
400 | \r | |
26fdb4ab | 401 | if ((fdump = fopen("dumpdata.bin","rb")) == NULL) {\r |
402 | PrintAndLog("Could not find file dump.bin");\r | |
403 | return 1;\r | |
404 | }\r | |
405 | if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) {\r | |
406 | PrintAndLog("Could not find file keys.bin");\r | |
407 | return 1;\r | |
408 | }\r | |
409 | \r | |
410 | for (i=0 ; i<16 ; i++) {\r | |
411 | fread(keyA[i], 1, 6, fkeys);\r | |
412 | }\r | |
413 | for (i=0 ; i<16 ; i++) {\r | |
414 | fread(keyB[i], 1, 6, fkeys);\r | |
415 | }\r | |
416 | \r | |
9d710943 | 417 | PrintAndLog("Restoring dumpdata.bin to card");\r |
26fdb4ab | 418 | \r |
419 | for (i=0 ; i<16 ; i++) {\r | |
420 | for( j=0 ; j<4 ; j++) {\r | |
421 | UsbCommand c = {CMD_MIFARE_WRITEBL, {i*4 + j, keyType, 0}};\r | |
422 | memcpy(c.d.asBytes, key, 6);\r | |
423 | \r | |
424 | fread(bldata, 1, 16, fdump);\r | |
425 | \r | |
426 | if (j == 3) {\r | |
427 | bldata[0] = (keyA[i][0]);\r | |
428 | bldata[1] = (keyA[i][1]);\r | |
429 | bldata[2] = (keyA[i][2]);\r | |
430 | bldata[3] = (keyA[i][3]);\r | |
431 | bldata[4] = (keyA[i][4]);\r | |
432 | bldata[5] = (keyA[i][5]);\r | |
433 | bldata[10] = (keyB[i][0]);\r | |
434 | bldata[11] = (keyB[i][1]);\r | |
435 | bldata[12] = (keyB[i][2]);\r | |
436 | bldata[13] = (keyB[i][3]);\r | |
437 | bldata[14] = (keyB[i][4]);\r | |
438 | bldata[15] = (keyB[i][5]);\r | |
439 | } \r | |
440 | \r | |
6c064e16 | 441 | PrintAndLog("Writing to block %2d: %s", i*4+j, sprint_hex(bldata, 16));\r |
442 | \r | |
443 | /*\r | |
9d710943 | 444 | PrintAndLog("Writing to block %2d: %s Confirm? [Y,N]", i*4+j, sprint_hex(bldata, 16));\r |
445 | \r | |
0f7f9edc | 446 | scanf("%c",&ch);\r |
6c064e16 | 447 | if ((ch != 'y') && (ch != 'Y')){\r |
9d710943 | 448 | PrintAndLog("Aborting !");\r |
449 | return 1;\r | |
450 | }\r | |
6c064e16 | 451 | */\r |
26fdb4ab | 452 | \r |
453 | memcpy(c.d.asBytes + 10, bldata, 16);\r | |
454 | SendCommand(&c);\r | |
455 | UsbCommand *resp = WaitForResponseTimeout(CMD_ACK, 1500);\r | |
456 | \r | |
457 | if (resp != NULL) {\r | |
458 | uint8_t isOK = resp->arg[0] & 0xff;\r | |
459 | PrintAndLog("isOk:%02x", isOK);\r | |
460 | } else {\r | |
461 | PrintAndLog("Command execute timeout");\r | |
462 | }\r | |
463 | }\r | |
464 | }\r | |
465 | \r | |
466 | fclose(fdump);\r | |
467 | fclose(fkeys);\r | |
468 | return 0;\r | |
469 | }\r | |
470 | \r | |
9ca155ba M |
471 | int CmdHF14AMfNested(const char *Cmd)\r |
472 | {\r | |
473 | int i, j, res, iterations;\r | |
474 | sector * e_sector = NULL;\r | |
475 | uint8_t blockNo = 0;\r | |
476 | uint8_t keyType = 0;\r | |
477 | uint8_t trgBlockNo = 0;\r | |
478 | uint8_t trgKeyType = 0;\r | |
479 | uint8_t blDiff = 0;\r | |
480 | int SectorsCnt = 0;\r | |
481 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
482 | uint8_t keyBlock[16 * 6];\r | |
483 | uint64_t key64 = 0;\r | |
8556b852 | 484 | int transferToEml = 0;\r |
9ca155ba | 485 | \r |
26fdb4ab | 486 | int createDumpFile = 0;\r |
487 | FILE *fkeys;\r | |
21156267 | 488 | uint8_t standart[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r |
489 | uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r | |
26fdb4ab | 490 | \r |
9ca155ba M |
491 | char cmdp, ctmp;\r |
492 | \r | |
493 | if (strlen(Cmd)<3) {\r | |
494 | PrintAndLog("Usage:");\r | |
26fdb4ab | 495 | PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");\r |
0014cb46 M |
496 | PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");\r |
497 | PrintAndLog(" <target block number> <target key A/B> [t]");\r | |
9ca155ba | 498 | PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r |
8556b852 | 499 | PrintAndLog("t - transfer keys into emulator memory");\r |
26fdb4ab | 500 | PrintAndLog("d - write keys to binary file");\r |
9ca155ba M |
501 | PrintAndLog(" ");\r |
502 | PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");\r | |
8556b852 | 503 | PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");\r |
26fdb4ab | 504 | PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");\r |
9ca155ba M |
505 | PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");\r |
506 | return 0;\r | |
507 | } \r | |
508 | \r | |
509 | cmdp = param_getchar(Cmd, 0);\r | |
510 | blockNo = param_get8(Cmd, 1);\r | |
511 | ctmp = param_getchar(Cmd, 2);\r | |
512 | if (ctmp == 0x00) {\r | |
513 | PrintAndLog("Key type must be A or B");\r | |
514 | return 1;\r | |
515 | }\r | |
516 | if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r | |
517 | if (param_gethex(Cmd, 3, key, 12)) {\r | |
518 | PrintAndLog("Key must include 12 HEX symbols");\r | |
519 | return 1;\r | |
520 | }\r | |
521 | \r | |
8556b852 | 522 | if (cmdp == 'o' || cmdp == 'O') {\r |
9ca155ba M |
523 | cmdp = 'o';\r |
524 | trgBlockNo = param_get8(Cmd, 4);\r | |
525 | ctmp = param_getchar(Cmd, 5);\r | |
526 | if (ctmp == 0x00) {\r | |
527 | PrintAndLog("Target key type must be A or B");\r | |
528 | return 1;\r | |
529 | }\r | |
530 | if (ctmp != 'A' && ctmp != 'a') trgKeyType = 1;\r | |
531 | } else {\r | |
532 | switch (cmdp) {\r | |
533 | case '0': SectorsCnt = 05; break;\r | |
534 | case '1': SectorsCnt = 16; break;\r | |
535 | case '2': SectorsCnt = 32; break;\r | |
536 | case '4': SectorsCnt = 64; break;\r | |
537 | default: SectorsCnt = 16;\r | |
538 | }\r | |
539 | }\r | |
8556b852 M |
540 | \r |
541 | ctmp = param_getchar(Cmd, 4);\r | |
21156267 | 542 | if (ctmp == 't' || ctmp == 'T') transferToEml = 1;\r |
543 | else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;\r | |
544 | \r | |
8556b852 M |
545 | ctmp = param_getchar(Cmd, 6);\r |
546 | transferToEml |= (ctmp == 't' || ctmp == 'T');\r | |
21156267 | 547 | transferToEml |= (ctmp == 'd' || ctmp == 'D');\r |
9ca155ba | 548 | \r |
8556b852 | 549 | PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo, keyType, sprint_hex(key, 6), transferToEml);\r |
9ca155ba M |
550 | if (cmdp == 'o')\r |
551 | PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo, trgKeyType);\r | |
552 | \r | |
553 | if (cmdp == 'o') {\r | |
554 | if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock)) {\r | |
555 | PrintAndLog("Nested error.");\r | |
556 | return 2;\r | |
557 | }\r | |
558 | \r | |
559 | for (i = 0; i < 16; i++) {\r | |
560 | PrintAndLog("cnt=%d key= %s", i, sprint_hex(keyBlock + i * 6, 6));\r | |
561 | }\r | |
562 | \r | |
563 | // test keys\r | |
564 | res = mfCheckKeys(trgBlockNo, trgKeyType, 8, keyBlock, &key64);\r | |
565 | if (res)\r | |
566 | res = mfCheckKeys(trgBlockNo, trgKeyType, 8, &keyBlock[6 * 8], &key64);\r | |
8556b852 | 567 | if (!res) {\r |
9ca155ba | 568 | PrintAndLog("Found valid key:%012llx", key64);\r |
8556b852 M |
569 | \r |
570 | // transfer key to the emulator\r | |
571 | if (transferToEml) {\r | |
572 | mfEmlGetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1);\r | |
573 | \r | |
574 | if (!trgKeyType)\r | |
575 | num_to_bytes(key64, 6, keyBlock);\r | |
576 | else\r | |
577 | num_to_bytes(key64, 6, &keyBlock[10]);\r | |
578 | mfEmlSetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1); \r | |
579 | }\r | |
580 | } else {\r | |
9ca155ba | 581 | PrintAndLog("No valid key found");\r |
8556b852 | 582 | }\r |
21156267 | 583 | }\r |
584 | else { // ------------------------------------ multiple sectors working\r | |
9ca155ba M |
585 | blDiff = blockNo % 4;\r |
586 | PrintAndLog("Block shift=%d", blDiff);\r | |
587 | e_sector = calloc(SectorsCnt, sizeof(sector));\r | |
588 | if (e_sector == NULL) return 1;\r | |
589 | \r | |
590 | //test current key 4 sectors\r | |
591 | memcpy(keyBlock, key, 6);\r | |
592 | num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 1 * 6));\r | |
593 | num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 2 * 6));\r | |
594 | num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 3 * 6));\r | |
595 | num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 4 * 6));\r | |
596 | num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));\r | |
597 | \r | |
598 | PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);\r | |
599 | for (i = 0; i < SectorsCnt; i++) {\r | |
600 | for (j = 0; j < 2; j++) {\r | |
601 | if (e_sector[i].foundKey[j]) continue;\r | |
602 | \r | |
603 | res = mfCheckKeys(i * 4 + blDiff, j, 6, keyBlock, &key64);\r | |
604 | \r | |
605 | if (!res) {\r | |
606 | e_sector[i].Key[j] = key64;\r | |
607 | e_sector[i].foundKey[j] = 1;\r | |
608 | }\r | |
609 | }\r | |
610 | } \r | |
611 | \r | |
9ca155ba M |
612 | // nested sectors\r |
613 | iterations = 0;\r | |
614 | PrintAndLog("nested...");\r | |
615 | for (i = 0; i < NESTED_SECTOR_RETRY; i++) {\r | |
616 | for (trgBlockNo = blDiff; trgBlockNo < SectorsCnt * 4; trgBlockNo = trgBlockNo + 4) \r | |
617 | for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) { \r | |
618 | if (e_sector[trgBlockNo / 4].foundKey[trgKeyType]) continue;\r | |
619 | if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock)) continue;\r | |
620 | \r | |
621 | iterations++;\r | |
622 | \r | |
623 | //try keys from nested\r | |
624 | res = mfCheckKeys(trgBlockNo, trgKeyType, 8, keyBlock, &key64);\r | |
625 | if (res)\r | |
626 | res = mfCheckKeys(trgBlockNo, trgKeyType, 8, &keyBlock[6 * 8], &key64);\r | |
627 | if (!res) {\r | |
628 | PrintAndLog("Found valid key:%012llx", key64); \r | |
629 | e_sector[trgBlockNo / 4].foundKey[trgKeyType] = 1;\r | |
630 | e_sector[trgBlockNo / 4].Key[trgKeyType] = key64;\r | |
631 | }\r | |
632 | }\r | |
633 | }\r | |
634 | \r | |
635 | PrintAndLog("Iterations count: %d", iterations);\r | |
636 | //print them\r | |
637 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
638 | PrintAndLog("|sec|key A |res|key B |res|");\r | |
639 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
640 | for (i = 0; i < SectorsCnt; i++) {\r | |
641 | PrintAndLog("|%03d| %012llx | %d | %012llx | %d |", i, \r | |
642 | e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);\r | |
643 | }\r | |
644 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
645 | \r | |
8556b852 M |
646 | // transfer them to the emulator\r |
647 | if (transferToEml) {\r | |
648 | for (i = 0; i < SectorsCnt; i++) {\r | |
649 | mfEmlGetMem(keyBlock, i * 4 + 3, 1);\r | |
650 | if (e_sector[i].foundKey[0])\r | |
ab8b654e | 651 | num_to_bytes(e_sector[i].Key[0], 6, keyBlock);\r |
8556b852 M |
652 | if (e_sector[i].foundKey[1])\r |
653 | num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);\r | |
654 | mfEmlSetMem(keyBlock, i * 4 + 3, 1);\r | |
655 | } \r | |
656 | }\r | |
657 | \r | |
21156267 | 658 | // Create dump file\r |
26fdb4ab | 659 | if (createDumpFile) {\r |
660 | if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) { \r | |
5ff9150e | 661 | PrintAndLog("Could not create file keys.bin");\r |
26fdb4ab | 662 | free(e_sector);\r |
663 | return 1;\r | |
664 | }\r | |
21156267 | 665 | PrintAndLog("Printing keys to bynary file dumpkeys.bin...");\r |
26fdb4ab | 666 | for(i=0; i<16; i++) {\r |
21156267 | 667 | if (e_sector[i].foundKey[0]){\r |
668 | num_to_bytes(e_sector[i].Key[0], 6, tempkey);\r | |
669 | fwrite ( tempkey, 1, 6, fkeys );\r | |
670 | }\r | |
671 | else{\r | |
672 | fwrite ( &standart, 1, 6, fkeys );\r | |
673 | }\r | |
26fdb4ab | 674 | }\r |
675 | for(i=0; i<16; i++) {\r | |
21156267 | 676 | if (e_sector[i].foundKey[1]){\r |
677 | num_to_bytes(e_sector[i].Key[1], 6, tempkey);\r | |
678 | fwrite ( tempkey, 1, 6, fkeys );\r | |
679 | }\r | |
680 | else{\r | |
681 | fwrite ( &standart, 1, 6, fkeys );\r | |
682 | }\r | |
26fdb4ab | 683 | }\r |
684 | fclose(fkeys);\r | |
685 | }\r | |
686 | \r | |
9ca155ba M |
687 | free(e_sector);\r |
688 | }\r | |
689 | \r | |
690 | return 0;\r | |
691 | }\r | |
692 | \r | |
693 | int CmdHF14AMfChk(const char *Cmd)\r | |
694 | {\r | |
695 | int i, res;\r | |
696 | int keycnt = 0;\r | |
697 | char ctmp = 0x00;\r | |
698 | uint8_t blockNo = 0;\r | |
699 | uint8_t keyType = 0;\r | |
700 | uint8_t keyBlock[8 * 6];\r | |
701 | uint64_t key64 = 0;\r | |
702 | \r | |
703 | memset(keyBlock, 0x00, sizeof(keyBlock));\r | |
704 | \r | |
705 | if (strlen(Cmd)<3) {\r | |
706 | PrintAndLog("Usage: hf mf chk <block number> <key A/B> [<key (12 hex symbols)>]");\r | |
0014cb46 | 707 | PrintAndLog(" sample: hf mf chk 0 A FFFFFFFFFFFF a0a1a2a3a4a5 b0b1b2b3b4b5 ");\r |
9ca155ba M |
708 | return 0;\r |
709 | } \r | |
710 | \r | |
711 | blockNo = param_get8(Cmd, 0);\r | |
712 | ctmp = param_getchar(Cmd, 1);\r | |
713 | if (ctmp == 0x00) {\r | |
714 | PrintAndLog("Key type must be A or B");\r | |
715 | return 1;\r | |
716 | }\r | |
717 | if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r | |
718 | \r | |
719 | for (i = 0; i < 6; i++) {\r | |
720 | if (!isxdigit(param_getchar(Cmd, 2 + i))) break;\r | |
721 | \r | |
722 | if (param_gethex(Cmd, 2 + i, keyBlock + 6 * i, 12)) {\r | |
723 | PrintAndLog("Key[%d] must include 12 HEX symbols", i);\r | |
724 | return 1;\r | |
725 | }\r | |
726 | keycnt = i + 1;\r | |
727 | }\r | |
728 | \r | |
729 | if (keycnt == 0) {\r | |
730 | PrintAndLog("There is must be at least one key");\r | |
731 | return 1;\r | |
732 | }\r | |
733 | \r | |
734 | PrintAndLog("--block no:%02x key type:%02x key count:%d ", blockNo, keyType, keycnt);\r | |
735 | \r | |
736 | res = mfCheckKeys(blockNo, keyType, keycnt, keyBlock, &key64);\r | |
737 | if (res !=1) {\r | |
738 | if (!res)\r | |
739 | PrintAndLog("isOk:%02x valid key:%012llx", 1, key64);\r | |
740 | else\r | |
741 | PrintAndLog("isOk:%02x", 0);\r | |
742 | } else {\r | |
743 | PrintAndLog("Command execute timeout");\r | |
744 | }\r | |
745 | \r | |
746 | return 0;\r | |
747 | }\r | |
748 | \r | |
749 | int CmdHF14AMf1kSim(const char *Cmd)\r | |
750 | {\r | |
751 | uint8_t uid[4] = {0, 0, 0, 0};\r | |
752 | \r | |
753 | if (param_getchar(Cmd, 0) == 'h') {\r | |
754 | PrintAndLog("Usage: hf mf sim <uid (8 hex symbols)>");\r | |
755 | PrintAndLog(" sample: hf mf sim 0a0a0a0a ");\r | |
756 | return 0;\r | |
757 | } \r | |
758 | \r | |
759 | if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {\r | |
760 | PrintAndLog("UID must include 8 HEX symbols");\r | |
761 | return 1;\r | |
762 | }\r | |
763 | PrintAndLog(" uid:%s ", sprint_hex(uid, 4));\r | |
764 | \r | |
765 | UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {0, 0, 0}};\r | |
766 | memcpy(c.d.asBytes, uid, 4);\r | |
767 | SendCommand(&c);\r | |
768 | \r | |
769 | return 0;\r | |
770 | }\r | |
771 | \r | |
772 | int CmdHF14AMfDbg(const char *Cmd)\r | |
773 | {\r | |
8556b852 M |
774 | int dbgMode = param_get32ex(Cmd, 0, 0, 10);\r |
775 | if (dbgMode > 4) {\r | |
776 | PrintAndLog("Max debud mode parameter is 4 \n");\r | |
777 | }\r | |
778 | \r | |
779 | if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {\r | |
9ca155ba M |
780 | PrintAndLog("Usage: hf mf dbg <debug level>");\r |
781 | PrintAndLog(" 0 - no debug messages");\r | |
782 | PrintAndLog(" 1 - error messages");\r | |
783 | PrintAndLog(" 2 - all messages");\r | |
784 | PrintAndLog(" 4 - extended debug mode");\r | |
785 | return 0;\r | |
786 | } \r | |
787 | \r | |
8556b852 M |
788 | UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};\r |
789 | SendCommand(&c);\r | |
790 | \r | |
9ca155ba M |
791 | return 0;\r |
792 | }\r | |
793 | \r | |
794 | int CmdHF14AMfEGet(const char *Cmd)\r | |
795 | {\r | |
8556b852 M |
796 | uint8_t blockNo = 0;\r |
797 | uint8_t data[3 * 16];\r | |
798 | int i;\r | |
799 | \r | |
800 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
801 | PrintAndLog("Usage: hf mf eget <block number>");\r | |
802 | PrintAndLog(" sample: hf mf eget 0 ");\r | |
803 | return 0;\r | |
804 | } \r | |
805 | \r | |
806 | blockNo = param_get8(Cmd, 0);\r | |
807 | if (blockNo >= 16 * 4) {\r | |
808 | PrintAndLog("Block number must be in [0..63] as in MIFARE classic.");\r | |
809 | return 1;\r | |
810 | }\r | |
811 | \r | |
812 | PrintAndLog(" ");\r | |
813 | if (!mfEmlGetMem(data, blockNo, 3)) {\r | |
814 | for (i = 0; i < 3; i++) {\r | |
815 | PrintAndLog("data[%d]:%s", blockNo + i, sprint_hex(data + i * 16, 16));\r | |
816 | }\r | |
817 | } else {\r | |
818 | PrintAndLog("Command execute timeout");\r | |
819 | }\r | |
820 | \r | |
821 | return 0;\r | |
822 | }\r | |
823 | \r | |
824 | int CmdHF14AMfEClear(const char *Cmd)\r | |
825 | {\r | |
826 | if (param_getchar(Cmd, 0) == 'h') {\r | |
827 | PrintAndLog("Usage: hf mf eclr");\r | |
828 | PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");\r | |
829 | return 0;\r | |
830 | } \r | |
831 | \r | |
832 | UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};\r | |
833 | SendCommand(&c);\r | |
9ca155ba M |
834 | return 0;\r |
835 | }\r | |
836 | \r | |
837 | int CmdHF14AMfESet(const char *Cmd)\r | |
838 | {\r | |
8556b852 M |
839 | uint8_t memBlock[16];\r |
840 | uint8_t blockNo = 0;\r | |
841 | \r | |
842 | memset(memBlock, 0x00, sizeof(memBlock));\r | |
843 | \r | |
844 | if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {\r | |
845 | PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");\r | |
846 | PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");\r | |
847 | return 0;\r | |
848 | } \r | |
849 | \r | |
850 | blockNo = param_get8(Cmd, 0);\r | |
851 | if (blockNo >= 16 * 4) {\r | |
852 | PrintAndLog("Block number must be in [0..63] as in MIFARE classic.");\r | |
853 | return 1;\r | |
854 | }\r | |
855 | \r | |
856 | if (param_gethex(Cmd, 1, memBlock, 32)) {\r | |
857 | PrintAndLog("block data must include 32 HEX symbols");\r | |
858 | return 1;\r | |
859 | }\r | |
860 | \r | |
861 | // 1 - blocks count\r | |
862 | UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};\r | |
863 | memcpy(c.d.asBytes, memBlock, 16);\r | |
864 | SendCommand(&c);\r | |
9ca155ba M |
865 | return 0;\r |
866 | }\r | |
867 | \r | |
868 | int CmdHF14AMfELoad(const char *Cmd)\r | |
869 | {\r | |
ab8b654e M |
870 | FILE * f;\r |
871 | char filename[20];\r | |
872 | char * fnameptr = filename;\r | |
873 | char buf[64];\r | |
874 | uint8_t buf8[64];\r | |
875 | int i, len, blockNum;\r | |
876 | \r | |
877 | memset(filename, 0, sizeof(filename));\r | |
878 | memset(buf, 0, sizeof(buf));\r | |
879 | \r | |
880 | if (param_getchar(Cmd, 0) == 'h') {\r | |
881 | PrintAndLog("It loads emul dump from the file `filename.eml`");\r | |
882 | PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");\r | |
883 | PrintAndLog(" sample: hf mf eload filename");\r | |
884 | return 0;\r | |
885 | } \r | |
886 | \r | |
887 | len = strlen(Cmd);\r | |
888 | if (len > 14) len = 14;\r | |
889 | \r | |
890 | if (len < 1) {\r | |
891 | }\r | |
892 | \r | |
893 | memcpy(filename, Cmd, len);\r | |
894 | fnameptr += len;\r | |
895 | \r | |
896 | sprintf(fnameptr, ".eml"); \r | |
897 | \r | |
898 | // open file\r | |
899 | f = fopen(filename, "r");\r | |
900 | if (f == NULL) {\r | |
901 | PrintAndLog("File not found or locked.");\r | |
902 | return 1;\r | |
903 | }\r | |
904 | \r | |
905 | blockNum = 0;\r | |
906 | while(!feof(f)){\r | |
907 | memset(buf, 0, sizeof(buf));\r | |
908 | fgets(buf, sizeof(buf), f);\r | |
909 | if (strlen(buf) < 32){\r | |
910 | PrintAndLog("File content error. Block data must include 32 HEX symbols");\r | |
911 | return 2;\r | |
912 | }\r | |
913 | for (i = 0; i < 32; i += 2)\r | |
914 | sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r | |
915 | // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));\r | |
916 | \r | |
917 | if (mfEmlSetMem(buf8, blockNum, 1)) {\r | |
918 | PrintAndLog("Cant set emul block: %d", blockNum);\r | |
919 | return 3;\r | |
920 | }\r | |
921 | blockNum++;\r | |
922 | \r | |
923 | if (blockNum >= 16 * 4) break;\r | |
924 | }\r | |
925 | fclose(f);\r | |
926 | \r | |
927 | if (blockNum != 16 * 4){\r | |
928 | PrintAndLog("File content error. There must be 64 blocks");\r | |
929 | return 4;\r | |
930 | }\r | |
931 | PrintAndLog("Loaded from file: %s", filename);\r | |
9ca155ba M |
932 | return 0;\r |
933 | }\r | |
934 | \r | |
935 | int CmdHF14AMfESave(const char *Cmd)\r | |
936 | {\r | |
ab8b654e M |
937 | FILE * f;\r |
938 | char filename[20];\r | |
939 | char * fnameptr = filename;\r | |
940 | uint8_t buf[64];\r | |
941 | int i, j, len;\r | |
942 | \r | |
943 | memset(filename, 0, sizeof(filename));\r | |
944 | memset(buf, 0, sizeof(buf));\r | |
945 | \r | |
946 | if (param_getchar(Cmd, 0) == 'h') {\r | |
947 | PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");\r | |
948 | PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");\r | |
949 | PrintAndLog(" sample: hf mf esave ");\r | |
950 | PrintAndLog(" hf mf esave filename");\r | |
951 | return 0;\r | |
952 | } \r | |
953 | \r | |
954 | len = strlen(Cmd);\r | |
955 | if (len > 14) len = 14;\r | |
956 | \r | |
957 | if (len < 1) {\r | |
958 | // get filename\r | |
959 | if (mfEmlGetMem(buf, 0, 1)) {\r | |
960 | PrintAndLog("Cant get block: %d", 0);\r | |
961 | return 1;\r | |
962 | }\r | |
963 | for (j = 0; j < 7; j++, fnameptr += 2)\r | |
964 | sprintf(fnameptr, "%02x", buf[j]); \r | |
965 | } else {\r | |
966 | memcpy(filename, Cmd, len);\r | |
967 | fnameptr += len;\r | |
968 | }\r | |
969 | \r | |
970 | sprintf(fnameptr, ".eml"); \r | |
971 | \r | |
972 | // open file\r | |
973 | f = fopen(filename, "w+");\r | |
974 | \r | |
975 | // put hex\r | |
976 | for (i = 0; i < 16 * 4; i++) {\r | |
977 | if (mfEmlGetMem(buf, i, 1)) {\r | |
978 | PrintAndLog("Cant get block: %d", i);\r | |
979 | break;\r | |
980 | }\r | |
981 | for (j = 0; j < 16; j++)\r | |
982 | fprintf(f, "%02x", buf[j]); \r | |
983 | fprintf(f,"\n");\r | |
984 | }\r | |
985 | fclose(f);\r | |
986 | \r | |
987 | PrintAndLog("Saved to file: %s", filename);\r | |
988 | \r | |
9ca155ba M |
989 | return 0;\r |
990 | }\r | |
991 | \r | |
26fdb4ab | 992 | int CmdHF14AMfECFill(const char *Cmd)\r |
993 | {\r | |
8556b852 M |
994 | uint8_t keyType = 0;\r |
995 | \r | |
996 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
997 | PrintAndLog("Usage: hf mf efill <key A/B>");\r | |
998 | PrintAndLog("sample: hf mf efill A");\r | |
999 | PrintAndLog("Card data blocks transfers to card emulator memory.");\r | |
1000 | PrintAndLog("Keys must be laid in the simulator memory. \n");\r | |
1001 | return 0;\r | |
1002 | } \r | |
1003 | \r | |
1004 | char ctmp = param_getchar(Cmd, 0);\r | |
1005 | if (ctmp == 0x00) {\r | |
1006 | PrintAndLog("Key type must be A or B");\r | |
1007 | return 1;\r | |
1008 | }\r | |
1009 | if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r | |
1010 | \r | |
1011 | UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {0, keyType, 0}};\r | |
1012 | SendCommand(&c);\r | |
1013 | return 0;\r | |
1014 | }\r | |
1015 | \r | |
26fdb4ab | 1016 | int CmdHF14AMfEKeyPrn(const char *Cmd)\r |
1017 | {\r | |
8556b852 M |
1018 | int i;\r |
1019 | uint8_t data[16];\r | |
1020 | uint64_t keyA, keyB;\r | |
1021 | \r | |
1022 | PrintAndLog("|---|----------------|----------------|");\r | |
1023 | PrintAndLog("|sec|key A |key B |");\r | |
1024 | PrintAndLog("|---|----------------|----------------|");\r | |
1025 | for (i = 0; i < 16; i++) {\r | |
1026 | if (mfEmlGetMem(data, i * 4 + 3, 1)) {\r | |
1027 | PrintAndLog("error get block %d", i * 4 + 3);\r | |
1028 | break;\r | |
1029 | }\r | |
1030 | keyA = bytes_to_num(data, 6);\r | |
1031 | keyB = bytes_to_num(data + 10, 6);\r | |
1032 | PrintAndLog("|%03d| %012llx | %012llx |", i, keyA, keyB);\r | |
1033 | }\r | |
1034 | PrintAndLog("|---|----------------|----------------|");\r | |
1035 | \r | |
1036 | return 0;\r | |
1037 | }\r | |
1038 | \r | |
26fdb4ab | 1039 | static command_t CommandTable[] =\r |
9ca155ba | 1040 | {\r |
26fdb4ab | 1041 | {"help", CmdHelp, 1, "This help"},\r |
1042 | {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},\r | |
9ca155ba M |
1043 | {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},\r |
1044 | {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},\r | |
26fdb4ab | 1045 | {"dump1k", CmdHF14AMfDump1k, 0, "Dump MIFARE classic tag to binary file"},\r |
1046 | {"restore1k", CmdHF14AMfRestore1k, 0, "Restore MIFARE classic binary file to BLANK tag"},\r | |
9ca155ba | 1047 | {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},\r |
26fdb4ab | 1048 | {"chk", CmdHF14AMfChk, 0, "Test block up to 8 keys"},\r |
9ca155ba M |
1049 | {"mifare", CmdHF14AMifare, 0, "Read parity error messages. param - <used card nonce>"},\r |
1050 | {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},\r | |
26fdb4ab | 1051 | {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE 1k card"},\r |
8556b852 | 1052 | {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},\r |
51969283 M |
1053 | {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},\r |
1054 | {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},\r | |
9ca155ba M |
1055 | {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},\r |
1056 | {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},\r | |
8556b852 M |
1057 | {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},\r |
1058 | {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},\r | |
9ca155ba M |
1059 | {NULL, NULL, 0, NULL}\r |
1060 | };\r | |
1061 | \r | |
1062 | int CmdHFMF(const char *Cmd)\r | |
1063 | {\r | |
1064 | // flush\r | |
1065 | while (WaitForResponseTimeout(CMD_ACK, 500) != NULL) ;\r | |
1066 | \r | |
1067 | CmdsParse(CommandTable, Cmd);\r | |
1068 | return 0;\r | |
1069 | }\r | |
1070 | \r | |
1071 | int CmdHelp(const char *Cmd)\r | |
1072 | {\r | |
1073 | CmdsHelp(CommandTable);\r | |
1074 | return 0;\r | |
1075 | }\r |