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