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