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