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