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