]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
b4450d54906ffb8493a754f22db174f30c8e00c1
[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[8] = {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
30 UsbCommand c = {CMD_READER_MIFARE, {(uint32_t)bytes_to_num(keyBlock, 4), 0, 0}};
31 start:
32 SendCommand(&c);
33
34 //flush queue
35 while (ukbhit()) getchar();
36
37 // message
38 printf("-------------------------------------------------------------------------\n");
39 printf("Executing command. It may take up to 30 min.\n");
40 printf("Press the key on proxmark3 device to abort proxmark3.\n");
41 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
42 printf("-------------------------------------------------------------------------\n");
43
44 // wait cycle
45 while (true) {
46 printf(".");
47 fflush(stdout);
48 if (ukbhit()) {
49 getchar();
50 printf("\naborted via keyboard!\n");
51 break;
52 }
53
54 UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 2000);
55 if (resp != NULL) {
56 isOK = resp->arg[0] & 0xff;
57
58 uid = (uint32_t)bytes_to_num(resp->d.asBytes + 0, 4);
59 nt = (uint32_t)bytes_to_num(resp->d.asBytes + 4, 4);
60 par_list = bytes_to_num(resp->d.asBytes + 8, 8);
61 ks_list = bytes_to_num(resp->d.asBytes + 16, 8);
62
63 printf("\n\n");
64 PrintAndLog("isOk:%02x", isOK);
65 if (!isOK) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n");
66 break;
67 }
68 }
69 printf("\n");
70
71 // error
72 if (isOK != 1) return 1;
73
74 // execute original function from util nonce2key
75 if (nonce2key(uid, nt, par_list, ks_list, &r_key)) return 2;
76 printf("------------------------------------------------------------------\n");
77 PrintAndLog("Key found:%012llx \n", r_key);
78
79 num_to_bytes(r_key, 6, keyBlock);
80 isOK = mfCheckKeys(0, 0, 1, keyBlock, &r_key);
81 if (!isOK)
82 PrintAndLog("Found valid key:%012llx", r_key);
83 else
84 {
85 PrintAndLog("Found invalid key. ( Nt=%08x ,Trying use it to run again...", nt);
86 c.arg[0] = nt;
87 goto start;
88 }
89
90 return 0;
91 }
92
93 int CmdHF14AMfWrBl(const char *Cmd)
94 {
95 uint8_t blockNo = 0;
96 uint8_t keyType = 0;
97 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
98 uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
99
100 char cmdp = 0x00;
101
102 if (strlen(Cmd)<3) {
103 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
104 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
105 return 0;
106 }
107
108 blockNo = param_get8(Cmd, 0);
109 cmdp = param_getchar(Cmd, 1);
110 if (cmdp == 0x00) {
111 PrintAndLog("Key type must be A or B");
112 return 1;
113 }
114 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
115 if (param_gethex(Cmd, 2, key, 12)) {
116 PrintAndLog("Key must include 12 HEX symbols");
117 return 1;
118 }
119 if (param_gethex(Cmd, 3, bldata, 32)) {
120 PrintAndLog("Block data must include 32 HEX symbols");
121 return 1;
122 }
123 PrintAndLog("--block no:%02x key type:%02x key:%s", blockNo, keyType, sprint_hex(key, 6));
124 PrintAndLog("--data: %s", sprint_hex(bldata, 16));
125
126 UsbCommand c = {CMD_MIFARE_WRITEBL, {blockNo, keyType, 0}};
127 memcpy(c.d.asBytes, key, 6);
128 memcpy(c.d.asBytes + 10, bldata, 16);
129 SendCommand(&c);
130 UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);
131
132 if (resp != NULL) {
133 uint8_t isOK = resp->arg[0] & 0xff;
134
135 PrintAndLog("isOk:%02x", isOK);
136 } else {
137 PrintAndLog("Command execute timeout");
138 }
139
140 return 0;
141 }
142
143 int CmdHF14AMfRdBl(const char *Cmd)
144 {
145 uint8_t blockNo = 0;
146 uint8_t keyType = 0;
147 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
148
149 char cmdp = 0x00;
150
151
152 if (strlen(Cmd)<3) {
153 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
154 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
155 return 0;
156 }
157
158 blockNo = param_get8(Cmd, 0);
159 cmdp = param_getchar(Cmd, 1);
160 if (cmdp == 0x00) {
161 PrintAndLog("Key type must be A or B");
162 return 1;
163 }
164 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
165 if (param_gethex(Cmd, 2, key, 12)) {
166 PrintAndLog("Key must include 12 HEX symbols");
167 return 1;
168 }
169 PrintAndLog("--block no:%02x key type:%02x key:%s ", blockNo, keyType, sprint_hex(key, 6));
170
171 UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};
172 memcpy(c.d.asBytes, key, 6);
173 SendCommand(&c);
174 UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);
175
176 if (resp != NULL) {
177 uint8_t isOK = resp->arg[0] & 0xff;
178 uint8_t * data = resp->d.asBytes;
179
180 if (isOK)
181 PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 16));
182 else
183 PrintAndLog("isOk:%02x", isOK);
184 } else {
185 PrintAndLog("Command execute timeout");
186 }
187
188 return 0;
189 }
190
191 int CmdHF14AMfRdSc(const char *Cmd)
192 {
193 int i;
194 uint8_t sectorNo = 0;
195 uint8_t keyType = 0;
196 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
197
198 uint8_t isOK = 0;
199 uint8_t * data = NULL;
200
201 char cmdp = 0x00;
202
203 if (strlen(Cmd)<3) {
204 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
205 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
206 return 0;
207 }
208
209 sectorNo = param_get8(Cmd, 0);
210 if (sectorNo > 63) {
211 PrintAndLog("Sector number must be less than 64");
212 return 1;
213 }
214 cmdp = param_getchar(Cmd, 1);
215 if (cmdp == 0x00) {
216 PrintAndLog("Key type must be A or B");
217 return 1;
218 }
219 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
220 if (param_gethex(Cmd, 2, key, 12)) {
221 PrintAndLog("Key must include 12 HEX symbols");
222 return 1;
223 }
224 PrintAndLog("--sector no:%02x key type:%02x key:%s ", sectorNo, keyType, sprint_hex(key, 6));
225
226 UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};
227 memcpy(c.d.asBytes, key, 6);
228 SendCommand(&c);
229 UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);
230 PrintAndLog(" ");
231
232 if (resp != NULL) {
233 isOK = resp->arg[0] & 0xff;
234 data = resp->d.asBytes;
235
236 PrintAndLog("isOk:%02x", isOK);
237 if (isOK)
238 for (i = 0; i < 2; i++) {
239 PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));
240 }
241 } else {
242 PrintAndLog("Command1 execute timeout");
243 }
244
245 // response2
246 resp = WaitForResponseTimeout(CMD_ACK, 500);
247 PrintAndLog(" ");
248
249 if (resp != NULL) {
250 isOK = resp->arg[0] & 0xff;
251 data = resp->d.asBytes;
252
253 if (isOK)
254 for (i = 0; i < 2; i++) {
255 PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));
256 }
257 } else {
258 PrintAndLog("Command2 execute timeout");
259 }
260
261 return 0;
262 }
263
264 int CmdHF14AMfDump(const char *Cmd)
265 {
266 int i, j;
267
268 uint8_t keyA[40][6];
269 uint8_t keyB[40][6];
270 uint8_t rights[40][4];
271
272 FILE *fin;
273 FILE *fout;
274
275 UsbCommand *resp;
276
277 if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {
278 PrintAndLog("Could not find file dumpkeys.bin");
279 return 1;
280 }
281
282 if ((fout = fopen("dumpdata.bin","wb")) == NULL) {
283 PrintAndLog("Could not create file name dumpdata.bin");
284 return 1;
285 }
286
287 // Read key file
288
289 for (i=0 ; i<16 ; i++) {
290 fread ( keyA[i], 1, 6, fin );
291 }
292 for (i=0 ; i<16 ; i++) {
293 fread ( keyB[i], 1, 6, fin );
294 }
295
296 // Read access rights to sectors
297
298 PrintAndLog("|-----------------------------------------|");
299 PrintAndLog("|------ Reading sector access bits...-----|");
300 PrintAndLog("|-----------------------------------------|");
301
302 for (i = 0 ; i < 16 ; i++) {
303 UsbCommand c = {CMD_MIFARE_READBL, {4*i + 3, 0, 0}};
304 memcpy(c.d.asBytes, keyA[i], 6);
305 SendCommand(&c);
306 resp = WaitForResponseTimeout(CMD_ACK, 1500);
307
308 if (resp != NULL) {
309 uint8_t isOK = resp->arg[0] & 0xff;
310 uint8_t *data = resp->d.asBytes;
311 if (isOK){
312 rights[i][0] = ((data[7] & 0x10)>>4) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>2);
313 rights[i][1] = ((data[7] & 0x20)>>5) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>3);
314 rights[i][2] = ((data[7] & 0x40)>>6) | ((data[8] & 0x4)>>1) | ((data[8] & 0x40)>>4);
315 rights[i][3] = ((data[7] & 0x80)>>7) | ((data[8] & 0x8)>>2) | ((data[8] & 0x80)>>5);
316 }
317 else{
318 PrintAndLog("Could not get access rights for block %d", i);
319 }
320 }
321 else {
322 PrintAndLog("Command execute timeout");
323 }
324 }
325
326 // Read blocks and print to file
327
328 PrintAndLog("|-----------------------------------------|");
329 PrintAndLog("|----- Dumping all blocks to file... -----|");
330 PrintAndLog("|-----------------------------------------|");
331
332 for (i=0 ; i<16 ; i++) {
333 for (j=0 ; j<4 ; j++) {
334 if (j == 3){
335 UsbCommand c = {CMD_MIFARE_READBL, {i*4 + j, 0, 0}};
336 memcpy(c.d.asBytes, keyA[i], 6);
337 SendCommand(&c);
338 resp = WaitForResponseTimeout(CMD_ACK, 1500);
339 }
340 else{
341 if ((rights[i][j] == 6) | (rights[i][j] == 5)) {
342 UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 1, 0}};
343 memcpy(c.d.asBytes, keyB[i], 6);
344 SendCommand(&c);
345 resp = WaitForResponseTimeout(CMD_ACK, 1500);
346 }
347 else if (rights[i][j] == 7) {
348 PrintAndLog("Access rights do not allow reading of sector %d block %d",i,j);
349 }
350 else {
351 UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 0, 0}};
352 memcpy(c.d.asBytes, keyA[i], 6);
353 SendCommand(&c);
354 resp = WaitForResponseTimeout(CMD_ACK, 1500);
355 }
356 }
357
358 if (resp != NULL) {
359 uint8_t isOK = resp->arg[0] & 0xff;
360 uint8_t *data = resp->d.asBytes;
361 if (j == 3) {
362 data[0] = (keyA[i][0]);
363 data[1] = (keyA[i][1]);
364 data[2] = (keyA[i][2]);
365 data[3] = (keyA[i][3]);
366 data[4] = (keyA[i][4]);
367 data[5] = (keyA[i][5]);
368 data[10] = (keyB[i][0]);
369 data[11] = (keyB[i][1]);
370 data[12] = (keyB[i][2]);
371 data[13] = (keyB[i][3]);
372 data[14] = (keyB[i][4]);
373 data[15] = (keyB[i][5]);
374 }
375 if (isOK) {
376 fwrite ( data, 1, 16, fout );
377 }
378 else {
379 PrintAndLog("Could not get access rights for block %d", i);
380 }
381 }
382 else {
383 PrintAndLog("Command execute timeout");
384 }
385 }
386 }
387
388 fclose(fin);
389 fclose(fout);
390
391 return 0;
392 }
393
394 int CmdHF14AMfRestore(const char *Cmd)
395 {
396
397 int i,j;
398 uint8_t keyType = 0;
399 uint8_t key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
400 uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
401 uint8_t keyA[16][6];
402 uint8_t keyB[16][6];
403
404 FILE *fdump;
405 FILE *fkeys;
406
407 if ((fdump = fopen("dumpdata.bin","rb")) == NULL) {
408 PrintAndLog("Could not find file dumpdata.bin");
409 return 1;
410 }
411 if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) {
412 PrintAndLog("Could not find file dumpkeys.bin");
413 return 1;
414 }
415
416 for (i=0 ; i<16 ; i++) {
417 fread(keyA[i], 1, 6, fkeys);
418 }
419 for (i=0 ; i<16 ; i++) {
420 fread(keyB[i], 1, 6, fkeys);
421 }
422
423 PrintAndLog("Restoring dumpdata.bin to card");
424
425 for (i=0 ; i<16 ; i++) {
426 for( j=0 ; j<4 ; j++) {
427 UsbCommand c = {CMD_MIFARE_WRITEBL, {i*4 + j, keyType, 0}};
428 memcpy(c.d.asBytes, key, 6);
429
430 fread(bldata, 1, 16, fdump);
431
432 if (j == 3) {
433 bldata[0] = (keyA[i][0]);
434 bldata[1] = (keyA[i][1]);
435 bldata[2] = (keyA[i][2]);
436 bldata[3] = (keyA[i][3]);
437 bldata[4] = (keyA[i][4]);
438 bldata[5] = (keyA[i][5]);
439 bldata[10] = (keyB[i][0]);
440 bldata[11] = (keyB[i][1]);
441 bldata[12] = (keyB[i][2]);
442 bldata[13] = (keyB[i][3]);
443 bldata[14] = (keyB[i][4]);
444 bldata[15] = (keyB[i][5]);
445 }
446
447 PrintAndLog("Writing to block %2d: %s", i*4+j, sprint_hex(bldata, 16));
448
449 /*
450 PrintAndLog("Writing to block %2d: %s Confirm? [Y,N]", i*4+j, sprint_hex(bldata, 16));
451
452 scanf("%c",&ch);
453 if ((ch != 'y') && (ch != 'Y')){
454 PrintAndLog("Aborting !");
455 return 1;
456 }
457 */
458
459 memcpy(c.d.asBytes + 10, bldata, 16);
460 SendCommand(&c);
461 UsbCommand *resp = WaitForResponseTimeout(CMD_ACK, 1500);
462
463 if (resp != NULL) {
464 uint8_t isOK = resp->arg[0] & 0xff;
465 PrintAndLog("isOk:%02x", isOK);
466 } else {
467 PrintAndLog("Command execute timeout");
468 }
469 }
470 }
471
472 fclose(fdump);
473 fclose(fkeys);
474 return 0;
475 }
476
477 int CmdHF14AMfNested(const char *Cmd)
478 {
479 int i, j, res, iterations;
480 sector * e_sector = NULL;
481 uint8_t blockNo = 0;
482 uint8_t keyType = 0;
483 uint8_t trgBlockNo = 0;
484 uint8_t trgKeyType = 0;
485 uint8_t blDiff = 0;
486 int SectorsCnt = 0;
487 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
488 uint8_t keyBlock[16 * 6];
489 uint64_t key64 = 0;
490 int transferToEml = 0;
491
492 int createDumpFile = 0;
493 FILE *fkeys;
494 uint8_t standart[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
495 uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
496
497 char cmdp, ctmp;
498
499 if (strlen(Cmd)<3) {
500 PrintAndLog("Usage:");
501 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
502 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
503 PrintAndLog(" <target block number> <target key A/B> [t]");
504 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
505 PrintAndLog("t - transfer keys into emulator memory");
506 PrintAndLog("d - write keys to binary file");
507 PrintAndLog(" ");
508 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
509 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");
510 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");
511 PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
512 return 0;
513 }
514
515 cmdp = param_getchar(Cmd, 0);
516 blockNo = param_get8(Cmd, 1);
517 ctmp = param_getchar(Cmd, 2);
518 if (ctmp == 0x00) {
519 PrintAndLog("Key type must be A or B");
520 return 1;
521 }
522 if (ctmp != 'A' && ctmp != 'a') keyType = 1;
523 if (param_gethex(Cmd, 3, key, 12)) {
524 PrintAndLog("Key must include 12 HEX symbols");
525 return 1;
526 }
527
528 if (cmdp == 'o' || cmdp == 'O') {
529 cmdp = 'o';
530 trgBlockNo = param_get8(Cmd, 4);
531 ctmp = param_getchar(Cmd, 5);
532 if (ctmp == 0x00) {
533 PrintAndLog("Target key type must be A or B");
534 return 1;
535 }
536 if (ctmp != 'A' && ctmp != 'a') trgKeyType = 1;
537 } else {
538 switch (cmdp) {
539 case '0': SectorsCnt = 05; break;
540 case '1': SectorsCnt = 16; break;
541 case '2': SectorsCnt = 32; break;
542 case '4': SectorsCnt = 64; break;
543 default: SectorsCnt = 16;
544 }
545 }
546
547 ctmp = param_getchar(Cmd, 4);
548 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;
549 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;
550
551 ctmp = param_getchar(Cmd, 6);
552 transferToEml |= (ctmp == 't' || ctmp == 'T');
553 transferToEml |= (ctmp == 'd' || ctmp == 'D');
554
555 PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo, keyType, sprint_hex(key, 6), transferToEml);
556 if (cmdp == 'o')
557 PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo, trgKeyType);
558
559 if (cmdp == 'o') {
560 if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock)) {
561 PrintAndLog("Nested error.");
562 return 2;
563 }
564
565 for (i = 0; i < 16; i++) {
566 PrintAndLog("count=%d key= %s", i, sprint_hex(keyBlock + i * 6, 6));
567 }
568
569 // test keys
570 res = mfCheckKeys(trgBlockNo, trgKeyType, 8, keyBlock, &key64);
571 if (res)
572 res = mfCheckKeys(trgBlockNo, trgKeyType, 8, &keyBlock[6 * 8], &key64);
573 if (!res) {
574 PrintAndLog("Found valid key:%012llx", key64);
575
576 // transfer key to the emulator
577 if (transferToEml) {
578 mfEmlGetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1);
579
580 if (!trgKeyType)
581 num_to_bytes(key64, 6, keyBlock);
582 else
583 num_to_bytes(key64, 6, &keyBlock[10]);
584 mfEmlSetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1);
585 }
586 } else {
587 PrintAndLog("No valid key found");
588 }
589 }
590 else { // ------------------------------------ multiple sectors working
591 blDiff = blockNo % 4;
592 PrintAndLog("Block shift=%d", blDiff);
593 e_sector = calloc(SectorsCnt, sizeof(sector));
594 if (e_sector == NULL) return 1;
595
596 //test current key 4 sectors
597 memcpy(keyBlock, key, 6);
598 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 1 * 6));
599 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 2 * 6));
600 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 3 * 6));
601 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 4 * 6));
602 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));
603
604 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);
605 for (i = 0; i < SectorsCnt; i++) {
606 for (j = 0; j < 2; j++) {
607 if (e_sector[i].foundKey[j]) continue;
608
609 res = mfCheckKeys(i * 4 + blDiff, j, 6, keyBlock, &key64);
610
611 if (!res) {
612 e_sector[i].Key[j] = key64;
613 e_sector[i].foundKey[j] = 1;
614 }
615 }
616 }
617
618 // nested sectors
619 iterations = 0;
620 PrintAndLog("nested...");
621 for (i = 0; i < NESTED_SECTOR_RETRY; i++) {
622 for (trgBlockNo = blDiff; trgBlockNo < SectorsCnt * 4; trgBlockNo = trgBlockNo + 4)
623 for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) {
624 if (e_sector[trgBlockNo / 4].foundKey[trgKeyType]) continue;
625 if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock)) continue;
626
627 iterations++;
628
629 //try keys from nested
630 res = mfCheckKeys(trgBlockNo, trgKeyType, 8, keyBlock, &key64);
631 if (res)
632 res = mfCheckKeys(trgBlockNo, trgKeyType, 8, &keyBlock[6 * 8], &key64);
633 if (!res) {
634 PrintAndLog("Found valid key:%012llx", key64);
635 e_sector[trgBlockNo / 4].foundKey[trgKeyType] = 1;
636 e_sector[trgBlockNo / 4].Key[trgKeyType] = key64;
637 }
638 }
639 }
640
641 PrintAndLog("Iterations count: %d", iterations);
642 //print them
643 PrintAndLog("|---|----------------|---|----------------|---|");
644 PrintAndLog("|sec|key A |res|key B |res|");
645 PrintAndLog("|---|----------------|---|----------------|---|");
646 for (i = 0; i < SectorsCnt; i++) {
647 PrintAndLog("|%03d| %012llx | %d | %012llx | %d |", i,
648 e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);
649 }
650 PrintAndLog("|---|----------------|---|----------------|---|");
651
652 // transfer them to the emulator
653 if (transferToEml) {
654 for (i = 0; i < SectorsCnt; i++) {
655 mfEmlGetMem(keyBlock, i * 4 + 3, 1);
656 if (e_sector[i].foundKey[0])
657 num_to_bytes(e_sector[i].Key[0], 6, keyBlock);
658 if (e_sector[i].foundKey[1])
659 num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);
660 mfEmlSetMem(keyBlock, i * 4 + 3, 1);
661 }
662 }
663
664 // Create dump file
665 if (createDumpFile) {
666 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
667 PrintAndLog("Could not create file dumpkeys.bin");
668 free(e_sector);
669 return 1;
670 }
671 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
672 for(i=0; i<16; i++) {
673 if (e_sector[i].foundKey[0]){
674 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
675 fwrite ( tempkey, 1, 6, fkeys );
676 }
677 else{
678 fwrite ( &standart, 1, 6, fkeys );
679 }
680 }
681 for(i=0; i<16; i++) {
682 if (e_sector[i].foundKey[1]){
683 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
684 fwrite ( tempkey, 1, 6, fkeys );
685 }
686 else{
687 fwrite ( &standart, 1, 6, fkeys );
688 }
689 }
690 fclose(fkeys);
691 }
692
693 free(e_sector);
694 }
695
696 return 0;
697 }
698
699 static uint32_t
700 get_trailer_block (uint32_t uiBlock)
701 {
702 // Test if we are in the small or big sectors
703 uint32_t trailer_block = 0;
704 if (uiBlock < 128) {
705 trailer_block = uiBlock + (3 - (uiBlock % 4));
706 } else {
707 trailer_block = uiBlock + (15 - (uiBlock % 16));
708 }
709 return trailer_block;
710 }
711 int CmdHF14AMfChk(const char *Cmd)
712 {
713 FILE * f;
714 char filename[256]={0};
715 char buf[13];
716 uint8_t *keyBlock = NULL, *p;
717 uint8_t stKeyBlock = 20;
718
719 int i, res;
720 int keycnt = 0;
721 char ctmp = 0x00;
722 uint8_t blockNo = 0;
723 uint8_t SectorsCnt = 1;
724 uint8_t keyType = 0;
725 uint64_t key64 = 0;
726
727 int transferToEml = 0;
728 int createDumpFile = 0;
729
730 keyBlock = calloc(stKeyBlock, 6);
731 if (keyBlock == NULL) return 1;
732
733 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 0 * 6)); // Default key (first key used by program if no user defined key)
734 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 1 * 6)); // Blank key
735 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 2 * 6)); // NFCForum MAD key
736 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 3 * 6));
737 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 4 * 6));
738 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock + 5 * 6));
739 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock + 6 * 6));
740 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock + 7 * 6));
741 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock + 8 * 6));
742 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock + 9 * 6));
743 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock + 10 * 6));
744 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock + 11 * 6));
745 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock + 12 * 6));
746
747 if (strlen(Cmd)<3) {
748 PrintAndLog("Usage: hf mf chk <block number>/<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");
749 PrintAndLog(" * - all sectors");
750 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
751 // PrintAndLog("d - write keys to binary file\n");
752
753 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
754 PrintAndLog(" hf mf chk *1 ? t");
755 return 0;
756 }
757
758 if (param_getchar(Cmd, 0)=='*') {
759 blockNo = 3;
760 switch(param_getchar(Cmd+1, 0)) {
761 case '0': SectorsCnt = 5; break;
762 case '1': SectorsCnt = 16; break;
763 case '2': SectorsCnt = 32; break;
764 case '4': SectorsCnt = 40; break;
765 default: SectorsCnt = 16;
766 }
767 }
768 else
769 blockNo = param_get8(Cmd, 0);
770
771 ctmp = param_getchar(Cmd, 1);
772 switch (ctmp) {
773 case 'a': case 'A':
774 keyType = !0;
775 break;
776 case 'b': case 'B':
777 keyType = !1;
778 break;
779 case '?':
780 keyType = 2;
781 break;
782 default:
783 PrintAndLog("Key type must be A , B or ?");
784 return 1;
785 };
786
787 ctmp = param_getchar(Cmd, 2);
788 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;
789 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;
790
791 for (i = transferToEml || createDumpFile; param_getchar(Cmd, 2 + i); i++) {
792 if (!param_gethex(Cmd, 2 + i, keyBlock + 6 * keycnt, 12)) {
793 if ( stKeyBlock - keycnt < 2) {
794 p = realloc(keyBlock, 6*(stKeyBlock+=10));
795 if (!p) {
796 PrintAndLog("Cannot allocate memory for Keys");
797 free(keyBlock);
798 return 2;
799 }
800 keyBlock = p;
801 }
802 PrintAndLog("chk key[%d] %02x%02x%02x%02x%02x%02x", keycnt,
803 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],
804 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);
805 keycnt++;
806 } else {
807 // May be a dic file
808 if ( param_getstr(Cmd, 2 + i,filename) > 255 ) {
809 PrintAndLog("File name too long");
810 free(keyBlock);
811 return 2;
812 }
813
814 if ( (f = fopen( filename , "r")) ) {
815 while( !feof(f) ){
816 memset(buf, 0, sizeof(buf));
817 fgets(buf, sizeof(buf), f);
818
819 if (strlen(buf) < 12 || buf[11] == '\n')
820 continue;
821
822 while (fgetc(f) != '\n' && !feof(f)) ; //goto next line
823
824 if( buf[0]=='#' ) continue; //The line start with # is remcommnet,skip
825
826 if (!isxdigit(buf[0])){
827 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);
828 continue;
829 }
830
831 buf[12] = 0;
832
833 if ( stKeyBlock - keycnt < 2) {
834 p = realloc(keyBlock, 6*(stKeyBlock+=10));
835 if (!p) {
836 PrintAndLog("Cannot allocate memory for defKeys");
837 free(keyBlock);
838 return 2;
839 }
840 keyBlock = p;
841 }
842 memset(keyBlock + 6 * keycnt, 0, 6);
843 num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);
844 PrintAndLog("chk custom key[%d] %012llx", keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));
845 keycnt++;
846 }
847 } else {
848 PrintAndLog("File: %s: not found or locked.", filename);
849 free(keyBlock);
850 return 1;
851 fclose(f);
852 }
853 }
854 }
855
856 if (keycnt == 0) {
857 PrintAndLog("No key specified,try default keys");
858 for (;keycnt <=12; keycnt++)
859 PrintAndLog("chk default key[%d] %02x%02x%02x%02x%02x%02x", keycnt,
860 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],
861 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);
862 }
863
864 for ( int t = !keyType ; t < 2 ; keyType==2?(t++):(t=2) ) {
865 int b=blockNo;
866 for (int i=0; i<SectorsCnt; ++i) {
867 PrintAndLog("--SectorsCnt:%d block no:0x%02x key type:%C key count:%d ", i, b, t?'B':'A', keycnt);
868 int size = keycnt>8?8:keycnt;
869 for (int c = 0; c < keycnt; c+=size) {
870 size=keycnt-c>8?8:keycnt-c;
871 res = mfCheckKeys(b, t, size, keyBlock +6*c, &key64);
872 if (res !=1) {
873 if (!res) {
874 PrintAndLog("Found valid key:[%012llx]",key64);
875 if (transferToEml) {
876 uint8_t block[16];
877 mfEmlGetMem(block, get_trailer_block(b), 1);
878 num_to_bytes(key64, 6, block + t*10);
879 mfEmlSetMem(block, get_trailer_block(b), 1);
880 }
881 break;
882 }
883 else {
884 printf("Not found yet, keycnt:%d\r", c+size);
885 fflush(stdout);
886 }
887 } else {
888 PrintAndLog("Command execute timeout");
889 }
890 }
891 b<127?(b+=4):(b+=16);
892 }
893 }
894
895 free(keyBlock);
896
897 /*
898 // Create dump file
899 if (createDumpFile) {
900 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
901 PrintAndLog("Could not create file dumpkeys.bin");
902 free(e_sector);
903 return 1;
904 }
905 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");
906 for(i=0; i<16; i++) {
907 if (e_sector[i].foundKey[0]){
908 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
909 fwrite ( tempkey, 1, 6, fkeys );
910 }
911 else{
912 fwrite ( &standart, 1, 6, fkeys );
913 }
914 }
915 for(i=0; i<16; i++) {
916 if (e_sector[i].foundKey[1]){
917 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
918 fwrite ( tempkey, 1, 6, fkeys );
919 }
920 else{
921 fwrite ( &standart, 1, 6, fkeys );
922 }
923 }
924 fclose(fkeys);
925 }
926 */
927 return 0;
928 }
929
930 int CmdHF14AMf1kSim(const char *Cmd)
931 {
932 uint8_t uid[4] = {0, 0, 0, 0};
933
934 if (param_getchar(Cmd, 0) == 'h') {
935 PrintAndLog("Usage: hf mf sim <uid (8 hex symbols)>");
936 PrintAndLog(" sample: hf mf sim 0a0a0a0a ");
937 return 0;
938 }
939
940 if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {
941 PrintAndLog("UID must include 8 HEX symbols");
942 return 1;
943 }
944 PrintAndLog(" uid:%s ", sprint_hex(uid, 4));
945
946 UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {0, 0, 0}};
947 memcpy(c.d.asBytes, uid, 4);
948 SendCommand(&c);
949
950 return 0;
951 }
952
953 int CmdHF14AMfDbg(const char *Cmd)
954 {
955 int dbgMode = param_get32ex(Cmd, 0, 0, 10);
956 if (dbgMode > 4) {
957 PrintAndLog("Max debud mode parameter is 4 \n");
958 }
959
960 if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {
961 PrintAndLog("Usage: hf mf dbg <debug level>");
962 PrintAndLog(" 0 - no debug messages");
963 PrintAndLog(" 1 - error messages");
964 PrintAndLog(" 2 - all messages");
965 PrintAndLog(" 4 - extended debug mode");
966 return 0;
967 }
968
969 UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};
970 SendCommand(&c);
971
972 return 0;
973 }
974
975 int CmdHF14AMfEGet(const char *Cmd)
976 {
977 uint8_t blockNo = 0;
978 uint8_t data[3 * 16];
979 int i;
980
981 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
982 PrintAndLog("Usage: hf mf eget <block number>");
983 PrintAndLog(" sample: hf mf eget 0 ");
984 return 0;
985 }
986
987 blockNo = param_get8(Cmd, 0);
988 if (blockNo >= 32 * 4 + 8 * 16) {
989 PrintAndLog("Block number must be in [0..255] as in MIFARE classic.");
990 return 1;
991 }
992
993 PrintAndLog(" ");
994 if (!mfEmlGetMem(data, blockNo, 3)) {
995 for (i = 0; i < 3; i++) {
996 PrintAndLog("data[%d]:%s", blockNo + i, sprint_hex(data + i * 16, 16));
997 }
998 } else {
999 PrintAndLog("Command execute timeout");
1000 }
1001
1002 return 0;
1003 }
1004
1005 int CmdHF14AMfEClear(const char *Cmd)
1006 {
1007 if (param_getchar(Cmd, 0) == 'h') {
1008 PrintAndLog("Usage: hf mf eclr");
1009 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1010 return 0;
1011 }
1012
1013 UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};
1014 SendCommand(&c);
1015 return 0;
1016 }
1017
1018 int CmdHF14AMfESet(const char *Cmd)
1019 {
1020 uint8_t memBlock[16];
1021 uint8_t blockNo = 0;
1022
1023 memset(memBlock, 0x00, sizeof(memBlock));
1024
1025 if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {
1026 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1027 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1028 return 0;
1029 }
1030
1031 blockNo = param_get8(Cmd, 0);
1032 if (blockNo >= 32 * 4 + 8 * 16) {
1033 PrintAndLog("Block number must be in [0..255] as in MIFARE classic.");
1034 return 1;
1035 }
1036
1037 if (param_gethex(Cmd, 1, memBlock, 32)) {
1038 PrintAndLog("block data must include 32 HEX symbols");
1039 return 1;
1040 }
1041
1042 // 1 - blocks count
1043 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};
1044 memcpy(c.d.asBytes, memBlock, 16);
1045 SendCommand(&c);
1046 return 0;
1047 }
1048
1049 int CmdHF14AMfELoad(const char *Cmd)
1050 {
1051 FILE * f;
1052 char filename[20];
1053 char * fnameptr = filename;
1054 char buf[64];
1055 uint8_t buf8[64];
1056 int i, len, blockNum;
1057
1058 memset(filename, 0, sizeof(filename));
1059 memset(buf, 0, sizeof(buf));
1060
1061 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {
1062 PrintAndLog("It loads emul dump from the file `filename.eml`");
1063 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");
1064 PrintAndLog(" sample: hf mf eload filename");
1065 return 0;
1066 }
1067
1068 len = strlen(Cmd);
1069 if (len > 14) len = 14;
1070
1071 memcpy(filename, Cmd, len);
1072 fnameptr += len;
1073
1074 sprintf(fnameptr, ".eml");
1075
1076 // open file
1077 f = fopen(filename, "r");
1078 if (f == NULL) {
1079 PrintAndLog("File not found or locked.");
1080 return 1;
1081 }
1082
1083 blockNum = 0;
1084 while(!feof(f)){
1085 memset(buf, 0, sizeof(buf));
1086 fgets(buf, sizeof(buf), f);
1087
1088 if (strlen(buf) < 32){
1089 if(strlen(buf) && feof(f))
1090 break;
1091 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1092 return 2;
1093 }
1094 for (i = 0; i < 32; i += 2)
1095 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
1096 // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));
1097
1098 if (mfEmlSetMem(buf8, blockNum, 1)) {
1099 PrintAndLog("Cant set emul block: %d", blockNum);
1100 return 3;
1101 }
1102 blockNum++;
1103
1104 if (blockNum >= 32 * 4 + 8 * 16) break;
1105 }
1106 fclose(f);
1107
1108 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
1109 PrintAndLog("File content error. There must be 64 blocks");
1110 return 4;
1111 }
1112 PrintAndLog("Loaded from file: %s", filename);
1113 return 0;
1114 }
1115
1116 int CmdHF14AMfESave(const char *Cmd)
1117 {
1118 FILE * f;
1119 char filename[20];
1120 char * fnameptr = filename;
1121 uint8_t buf[64];
1122 int i, j, len;
1123
1124 memset(filename, 0, sizeof(filename));
1125 memset(buf, 0, sizeof(buf));
1126
1127 if (param_getchar(Cmd, 0) == 'h') {
1128 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1129 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");
1130 PrintAndLog(" sample: hf mf esave ");
1131 PrintAndLog(" hf mf esave filename");
1132 return 0;
1133 }
1134
1135 len = strlen(Cmd);
1136 if (len > 14) len = 14;
1137
1138 if (len < 1) {
1139 // get filename
1140 if (mfEmlGetMem(buf, 0, 1)) {
1141 PrintAndLog("Cant get block: %d", 0);
1142 return 1;
1143 }
1144 for (j = 0; j < 7; j++, fnameptr += 2)
1145 sprintf(fnameptr, "%02x", buf[j]);
1146 } else {
1147 memcpy(filename, Cmd, len);
1148 fnameptr += len;
1149 }
1150
1151 sprintf(fnameptr, ".eml");
1152
1153 // open file
1154 f = fopen(filename, "w+");
1155
1156 // put hex
1157 for (i = 0; i < 32 * 4 + 8 * 16; i++) {
1158 if (mfEmlGetMem(buf, i, 1)) {
1159 PrintAndLog("Cant get block: %d", i);
1160 break;
1161 }
1162 for (j = 0; j < 16; j++)
1163 fprintf(f, "%02x", buf[j]);
1164 fprintf(f,"\n");
1165 }
1166 fclose(f);
1167
1168 PrintAndLog("Saved to file: %s", filename);
1169
1170 return 0;
1171 }
1172
1173 int CmdHF14AMfECFill(const char *Cmd)
1174 {
1175 uint8_t keyType = 0;
1176
1177 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1178 PrintAndLog("Usage: hf mf efill <key A/B>");
1179 PrintAndLog("sample: hf mf efill A");
1180 PrintAndLog("Card data blocks transfers to card emulator memory.");
1181 PrintAndLog("Keys must be laid in the simulator memory. \n");
1182 return 0;
1183 }
1184
1185 char ctmp = param_getchar(Cmd, 0);
1186 if (ctmp == 0x00) {
1187 PrintAndLog("Key type must be A or B");
1188 return 1;
1189 }
1190 if (ctmp != 'A' && ctmp != 'a') keyType = 1;
1191
1192 UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {0, keyType, 0}};
1193 SendCommand(&c);
1194 return 0;
1195 }
1196
1197 int CmdHF14AMfEKeyPrn(const char *Cmd)
1198 {
1199 int i,b=-1;
1200 uint8_t data[16];
1201 uint64_t keyA, keyB;
1202
1203 PrintAndLog("|---|----------------|----------------|");
1204 PrintAndLog("|sec|key A |key B |");
1205 PrintAndLog("|---|----------------|----------------|");
1206 for (i = 0; i < 40; i++) {
1207 b<127?(b+=4):(b+=16);
1208 if (mfEmlGetMem(data, b, 1)) {
1209 PrintAndLog("error get block %d", b);
1210 break;
1211 }
1212 keyA = bytes_to_num(data, 6);
1213 keyB = bytes_to_num(data + 10, 6);
1214 PrintAndLog("|%03d| %012llx | %012llx |", i, keyA, keyB);
1215 }
1216 PrintAndLog("|---|----------------|----------------|");
1217
1218 return 0;
1219 }
1220
1221 int CmdHF14AMfCSetUID(const char *Cmd)
1222 {
1223 uint8_t wipeCard = 0;
1224 uint8_t uid[8];
1225 uint8_t oldUid[8];
1226 int res;
1227
1228 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1229 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");
1230 PrintAndLog("sample: hf mf csetuid 01020304 w");
1231 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");
1232 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1233 return 0;
1234 }
1235
1236 if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {
1237 PrintAndLog("UID must include 8 HEX symbols");
1238 return 1;
1239 }
1240
1241 char ctmp = param_getchar(Cmd, 1);
1242 if (ctmp == 'w' || ctmp == 'W') wipeCard = 1;
1243
1244 PrintAndLog("--wipe card:%02x uid:%s", wipeCard, sprint_hex(uid, 4));
1245
1246 res = mfCSetUID(uid, oldUid, wipeCard);
1247 if (res) {
1248 PrintAndLog("Can't set UID. error=%d", res);
1249 return 1;
1250 }
1251
1252 PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));
1253 return 0;
1254 }
1255
1256 int CmdHF14AMfCSetBlk(const char *Cmd)
1257 {
1258 uint8_t uid[8];
1259 uint8_t memBlock[16];
1260 uint8_t blockNo = 0;
1261 int res;
1262 memset(memBlock, 0x00, sizeof(memBlock));
1263
1264 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1265 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");
1266 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1267 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");
1268 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1269 return 0;
1270 }
1271
1272 blockNo = param_get8(Cmd, 0);
1273 if (blockNo >= 32 * 4 + 8 * 16) {
1274 PrintAndLog("Block number must be in [0..255] as in MIFARE classic.");
1275 return 1;
1276 }
1277
1278 if (param_gethex(Cmd, 1, memBlock, 32)) {
1279 PrintAndLog("block data must include 32 HEX symbols");
1280 return 1;
1281 }
1282
1283 PrintAndLog("--block number:%02x data:%s", blockNo, sprint_hex(memBlock, 16));
1284
1285 res = mfCSetBlock(blockNo, memBlock, uid, 0, CSETBLOCK_SINGLE_OPER);
1286 if (res) {
1287 PrintAndLog("Can't write block. error=%d", res);
1288 return 1;
1289 }
1290
1291 PrintAndLog("UID:%s", sprint_hex(uid, 4));
1292 return 0;
1293 }
1294
1295 int CmdHF14AMfCLoad(const char *Cmd)
1296 {
1297 FILE * f;
1298 char filename[20];
1299 char * fnameptr = filename;
1300 char buf[64];
1301 uint8_t buf8[64];
1302 uint8_t fillFromEmulator = 0;
1303 int i, len, blockNum, flags;
1304
1305 memset(filename, 0, sizeof(filename));
1306 memset(buf, 0, sizeof(buf));
1307
1308 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {
1309 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");
1310 PrintAndLog("or from emulator memory (option `e`)");
1311 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1312 PrintAndLog(" or: hf mf cload e ");
1313 PrintAndLog(" sample: hf mf cload filename");
1314 return 0;
1315 }
1316
1317 char ctmp = param_getchar(Cmd, 0);
1318 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
1319
1320 if (fillFromEmulator) {
1321 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1322 for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {
1323 if (mfEmlGetMem(buf8, blockNum, 1)) {
1324 PrintAndLog("Cant get block: %d", blockNum);
1325 return 2;
1326 }
1327
1328 if (blockNum == 2) flags = 0;
1329 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1330
1331 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {
1332 PrintAndLog("Cant set magic card block: %d", blockNum);
1333 return 3;
1334 }
1335 }
1336 return 0;
1337 } else {
1338 len = strlen(Cmd);
1339 if (len > 14) len = 14;
1340
1341 memcpy(filename, Cmd, len);
1342 fnameptr += len;
1343
1344 sprintf(fnameptr, ".eml");
1345
1346 // open file
1347 f = fopen(filename, "r");
1348 if (f == NULL) {
1349 PrintAndLog("File not found or locked.");
1350 return 1;
1351 }
1352
1353 blockNum = 0;
1354 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1355 while(!feof(f)){
1356 memset(buf, 0, sizeof(buf));
1357 fgets(buf, sizeof(buf), f);
1358
1359 if (strlen(buf) < 32){
1360 if(strlen(buf) && feof(f))
1361 break;
1362 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1363 return 2;
1364 }
1365 for (i = 0; i < 32; i += 2)
1366 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
1367
1368 if (blockNum == 2) flags = 0;
1369 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1370
1371 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {
1372 PrintAndLog("Cant set magic card block: %d", blockNum);
1373 return 3;
1374 }
1375 blockNum++;
1376
1377 if (blockNum >= 16 * 4) break; // magic card type - mifare 1K
1378 }
1379 fclose(f);
1380
1381 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
1382 PrintAndLog("File content error. There must be 64 blocks");
1383 return 4;
1384 }
1385 PrintAndLog("Loaded from file: %s", filename);
1386 return 0;
1387 }
1388 }
1389
1390 int CmdHF14AMfCGetBlk(const char *Cmd) {
1391 uint8_t memBlock[16];
1392 uint8_t blockNo = 0;
1393 int res;
1394 memset(memBlock, 0x00, sizeof(memBlock));
1395
1396 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1397 PrintAndLog("Usage: hf mf cgetblk <block number>");
1398 PrintAndLog("sample: hf mf cgetblk 1");
1399 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");
1400 return 0;
1401 }
1402
1403 blockNo = param_get8(Cmd, 0);
1404 if (blockNo >= 32 * 4 + 8 * 16) {
1405 PrintAndLog("Block number must be in [0..255] as in MIFARE classic.");
1406 return 1;
1407 }
1408
1409 PrintAndLog("--block number:%02x ", blockNo);
1410
1411 res = mfCGetBlock(blockNo, memBlock, CSETBLOCK_SINGLE_OPER);
1412 if (res) {
1413 PrintAndLog("Can't read block. error=%d", res);
1414 return 1;
1415 }
1416
1417 PrintAndLog("block data:%s", sprint_hex(memBlock, 16));
1418 return 0;
1419 }
1420
1421 int CmdHF14AMfCGetSc(const char *Cmd) {
1422 uint8_t memBlock[16];
1423 uint8_t sectorNo = 0;
1424 int i, res, flags;
1425 memset(memBlock, 0x00, sizeof(memBlock));
1426
1427 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1428 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1429 PrintAndLog("sample: hf mf cgetsc 0");
1430 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");
1431 return 0;
1432 }
1433
1434 sectorNo = param_get8(Cmd, 0);
1435 if (sectorNo > 15) {
1436 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1437 return 1;
1438 }
1439
1440 PrintAndLog("--sector number:%02x ", sectorNo);
1441
1442 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1443 for (i = 0; i < 4; i++) {
1444 if (i == 1) flags = 0;
1445 if (i == 3) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1446
1447 res = mfCGetBlock(sectorNo * 4 + i, memBlock, flags);
1448 if (res) {
1449 PrintAndLog("Can't read block. %02x error=%d", sectorNo * 4 + i, res);
1450 return 1;
1451 }
1452
1453 PrintAndLog("block %02x data:%s", sectorNo * 4 + i, sprint_hex(memBlock, 16));
1454 }
1455 return 0;
1456 }
1457
1458 int CmdHF14AMfCSave(const char *Cmd) {
1459
1460 FILE * f;
1461 char filename[20];
1462 char * fnameptr = filename;
1463 uint8_t fillFromEmulator = 0;
1464 uint8_t buf[64];
1465 int i, j, len, flags;
1466
1467 memset(filename, 0, sizeof(filename));
1468 memset(buf, 0, sizeof(buf));
1469
1470 if (param_getchar(Cmd, 0) == 'h') {
1471 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1472 PrintAndLog("or into emulator memory (option `e`)");
1473 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1474 PrintAndLog(" sample: hf mf esave ");
1475 PrintAndLog(" hf mf esave filename");
1476 PrintAndLog(" hf mf esave e \n");
1477 return 0;
1478 }
1479
1480 char ctmp = param_getchar(Cmd, 0);
1481 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
1482
1483 if (fillFromEmulator) {
1484 // put into emulator
1485 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1486 for (i = 0; i < 16 * 4; i++) {
1487 if (i == 1) flags = 0;
1488 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1489
1490 if (mfCGetBlock(i, buf, flags)) {
1491 PrintAndLog("Cant get block: %d", i);
1492 break;
1493 }
1494
1495 if (mfEmlSetMem(buf, i, 1)) {
1496 PrintAndLog("Cant set emul block: %d", i);
1497 return 3;
1498 }
1499 }
1500 return 0;
1501 } else {
1502 len = strlen(Cmd);
1503 if (len > 14) len = 14;
1504
1505 if (len < 1) {
1506 // get filename
1507 if (mfCGetBlock(0, buf, CSETBLOCK_SINGLE_OPER)) {
1508 PrintAndLog("Cant get block: %d", 0);
1509 return 1;
1510 }
1511 for (j = 0; j < 7; j++, fnameptr += 2)
1512 sprintf(fnameptr, "%02x", buf[j]);
1513 } else {
1514 memcpy(filename, Cmd, len);
1515 fnameptr += len;
1516 }
1517
1518 sprintf(fnameptr, ".eml");
1519
1520 // open file
1521 f = fopen(filename, "w+");
1522
1523 // put hex
1524 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1525 for (i = 0; i < 16 * 4; i++) {
1526 if (i == 1) flags = 0;
1527 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1528
1529 if (mfCGetBlock(i, buf, flags)) {
1530 PrintAndLog("Cant get block: %d", i);
1531 break;
1532 }
1533 for (j = 0; j < 16; j++)
1534 fprintf(f, "%02x", buf[j]);
1535 fprintf(f,"\n");
1536 }
1537 fclose(f);
1538
1539 PrintAndLog("Saved to file: %s", filename);
1540
1541 return 0;
1542 }
1543 }
1544
1545 static command_t CommandTable[] =
1546 {
1547 {"help", CmdHelp, 1, "This help"},
1548 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},
1549 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},
1550 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},
1551 {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},
1552 {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},
1553 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},
1554 {"chk", CmdHF14AMfChk, 0, "Test block keys"},
1555 {"mifare", CmdHF14AMifare, 0, "Read parity error messages. param - <used card nonce>"},
1556 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},
1557 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},
1558 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},
1559 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},
1560 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},
1561 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},
1562 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},
1563 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},
1564 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},
1565 {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},
1566 {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block into magic Chinese card"},
1567 {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block from magic Chinese card"},
1568 {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector from magic Chinese card"},
1569 {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},
1570 {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},
1571 {NULL, NULL, 0, NULL}
1572 };
1573
1574 int CmdHFMF(const char *Cmd)
1575 {
1576 // flush
1577 while (WaitForResponseTimeout(CMD_ACK, 500) != NULL) ;
1578
1579 CmdsParse(CommandTable, Cmd);
1580 return 0;
1581 }
1582
1583 int CmdHelp(const char *Cmd)
1584 {
1585 CmdsHelp(CommandTable);
1586 return 0;
1587 }
Impressum, Datenschutz