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