]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
First check in.
[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 #include "../include/mifare.h"
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:%d, key type:%c, key:%s", blockNo, keyType?'B':'A', 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 /* dublett finns i CMDHFMFU.C
144 int CmdHF14AMfUWrBl(const char *Cmd)
145 {
146 uint8_t blockNo = 0;
147 bool chinese_card=0;
148 uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
149 UsbCommand resp;
150
151 if (strlen(Cmd)<3) {
152 PrintAndLog("Usage: hf mf uwrbl <block number> <block data (8 hex symbols)> <w>");
153 PrintAndLog(" sample: hf mf uwrbl 0 01020304");
154 return 0;
155 }
156
157 blockNo = param_get8(Cmd, 0);
158 if (param_gethex(Cmd, 1, bldata, 8)) {
159 PrintAndLog("Block data must include 8 HEX symbols");
160 return 1;
161 }
162
163 if (strchr(Cmd,'w') != 0) {
164 chinese_card=1;
165 }
166
167 switch(blockNo){
168 case 0:
169 if (!chinese_card){
170 PrintAndLog("Access Denied");
171 }else{
172 PrintAndLog("--specialblock no:%d", blockNo);
173 PrintAndLog("--data: %s", sprint_hex(bldata, 4));
174 UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};
175 memcpy(d.d.asBytes,bldata, 4);
176 SendCommand(&d);
177
178 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
179 uint8_t isOK = resp.arg[0] & 0xff;
180 PrintAndLog("isOk:%02x", isOK);
181 } else {
182 PrintAndLog("Command execute timeout");
183 }
184 }
185 break;
186 case 1:
187 if (!chinese_card){
188 PrintAndLog("Access Denied");
189 }else{
190 PrintAndLog("--specialblock no:%d", blockNo);
191 PrintAndLog("--data: %s", sprint_hex(bldata, 4));
192 UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};
193 memcpy(d.d.asBytes,bldata, 4);
194 SendCommand(&d);
195
196 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
197 uint8_t isOK = resp.arg[0] & 0xff;
198 PrintAndLog("isOk:%02x", isOK);
199 } else {
200 PrintAndLog("Command execute timeout");
201 }
202 }
203 break;
204 case 2:
205 if (!chinese_card){
206 PrintAndLog("Access Denied");
207 }else{
208 PrintAndLog("--specialblock no:%d", blockNo);
209 PrintAndLog("--data: %s", sprint_hex(bldata, 4));
210 UsbCommand c = {CMD_MIFAREU_WRITEBL, {blockNo}};
211 memcpy(c.d.asBytes, bldata, 4);
212 SendCommand(&c);
213
214 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
215 uint8_t isOK = resp.arg[0] & 0xff;
216 PrintAndLog("isOk:%02x", isOK);
217 } else {
218 PrintAndLog("Command execute timeout");
219 }
220 }
221 break;
222 case 3:
223 PrintAndLog("--specialblock no:%d", blockNo);
224 PrintAndLog("--data: %s", sprint_hex(bldata, 4));
225 UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};
226 memcpy(d.d.asBytes,bldata, 4);
227 SendCommand(&d);
228
229 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
230 uint8_t isOK = resp.arg[0] & 0xff;
231 PrintAndLog("isOk:%02x", isOK);
232 } else {
233 PrintAndLog("Command execute timeout");
234 }
235 break;
236 default:
237 PrintAndLog("--block no:%d", blockNo);
238 PrintAndLog("--data: %s", sprint_hex(bldata, 4));
239 UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}};
240 memcpy(e.d.asBytes,bldata, 4);
241 SendCommand(&e);
242
243 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
244 uint8_t isOK = resp.arg[0] & 0xff;
245 PrintAndLog("isOk:%02x", isOK);
246 } else {
247 PrintAndLog("Command execute timeout");
248 }
249 break;
250 }
251 return 0;
252 }
253 */
254 int CmdHF14AMfRdBl(const char *Cmd)
255 {
256 uint8_t blockNo = 0;
257 uint8_t keyType = 0;
258 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
259
260 char cmdp = 0x00;
261
262
263 if (strlen(Cmd)<3) {
264 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
265 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
266 return 0;
267 }
268
269 blockNo = param_get8(Cmd, 0);
270 cmdp = param_getchar(Cmd, 1);
271 if (cmdp == 0x00) {
272 PrintAndLog("Key type must be A or B");
273 return 1;
274 }
275 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
276 if (param_gethex(Cmd, 2, key, 12)) {
277 PrintAndLog("Key must include 12 HEX symbols");
278 return 1;
279 }
280 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo, keyType?'B':'A', sprint_hex(key, 6));
281
282 UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};
283 memcpy(c.d.asBytes, key, 6);
284 SendCommand(&c);
285
286 UsbCommand resp;
287 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
288 uint8_t isOK = resp.arg[0] & 0xff;
289 uint8_t *data = resp.d.asBytes;
290
291 if (isOK)
292 PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 16));
293 else
294 PrintAndLog("isOk:%02x", isOK);
295 } else {
296 PrintAndLog("Command execute timeout");
297 }
298
299 return 0;
300 }
301
302 /* dublett finns i CMDHFMFU.C
303 int CmdHF14AMfURdBl(const char *Cmd)
304 {
305 uint8_t blockNo = 0;
306
307 if (strlen(Cmd)<1) {
308 PrintAndLog("Usage: hf mf urdbl <block number>");
309 PrintAndLog(" sample: hf mf urdbl 0");
310 return 0;
311 }
312
313 blockNo = param_get8(Cmd, 0);
314 PrintAndLog("--block no:%d", blockNo);
315
316 UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}};
317 SendCommand(&c);
318
319 UsbCommand resp;
320 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
321 uint8_t isOK = resp.arg[0] & 0xff;
322 uint8_t *data = resp.d.asBytes;
323
324 if (isOK)
325 PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 4));
326 else
327 PrintAndLog("isOk:%02x", isOK);
328 } else {
329 PrintAndLog("Command execute timeout");
330 }
331
332 return 0;
333 }
334 */
335
336 /* dublett finns i CMDHFMFU.C
337 int CmdHF14AMfURdCard(const char *Cmd)
338 {
339 int i;
340 uint8_t sectorNo = 0;
341 uint8_t *lockbytes_t=NULL;
342 uint8_t lockbytes[2]={0,0};
343 bool bit[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
344
345 uint8_t isOK = 0;
346 uint8_t * data = NULL;
347
348 if (sectorNo > 15) {
349 PrintAndLog("Sector number must be less than 16");
350 return 1;
351 }
352 PrintAndLog("Attempting to Read Ultralight... ");
353
354 UsbCommand c = {CMD_MIFAREU_READCARD, {sectorNo}};
355 SendCommand(&c);
356
357 UsbCommand resp;
358 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
359 isOK = resp.arg[0] & 0xff;
360 data = resp.d.asBytes;
361
362 PrintAndLog("isOk:%02x", isOK);
363 if (isOK)
364 for (i = 0; i < 16; i++) {
365 switch(i){
366 case 2:
367 //process lock bytes
368 lockbytes_t=data+(i*4);
369 lockbytes[0]=lockbytes_t[2];
370 lockbytes[1]=lockbytes_t[3];
371 for(int j=0; j<16; j++){
372 bit[j]=lockbytes[j/8] & ( 1 <<(7-j%8));
373 }
374 //PrintAndLog("LB %02x %02x", lockbytes[0],lockbytes[1]);
375 //PrintAndLog("LB2b %02x %02x %02x %02x %02x %02x %02x %02x",bit[8],bit[9],bit[10],bit[11],bit[12],bit[13],bit[14],bit[15]);
376 PrintAndLog("Block %3d:%s ", i,sprint_hex(data + i * 4, 4));
377 break;
378 case 3:
379 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[4]);
380 break;
381 case 4:
382 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[3]);
383 break;
384 case 5:
385 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[2]);
386 break;
387 case 6:
388 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[1]);
389 break;
390 case 7:
391 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[0]);
392 break;
393 case 8:
394 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[15]);
395 break;
396 case 9:
397 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[14]);
398 break;
399 case 10:
400 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[13]);
401 break;
402 case 11:
403 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[12]);
404 break;
405 case 12:
406 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[11]);
407 break;
408 case 13:
409 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[10]);
410 break;
411 case 14:
412 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[9]);
413 break;
414 case 15:
415 PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[8]);
416 break;
417 default:
418 PrintAndLog("Block %3d:%s ", i,sprint_hex(data + i * 4, 4));
419 break;
420 }
421 }
422 } else {
423 PrintAndLog("Command execute timeout");
424 }
425 return 0;
426 }
427 */
428
429 int CmdHF14AMfRdSc(const char *Cmd)
430 {
431 int i;
432 uint8_t sectorNo = 0;
433 uint8_t keyType = 0;
434 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
435 uint8_t isOK = 0;
436 uint8_t *data = NULL;
437 char cmdp = 0x00;
438
439 if (strlen(Cmd)<3) {
440 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
441 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
442 return 0;
443 }
444
445 sectorNo = param_get8(Cmd, 0);
446 if (sectorNo > 39) {
447 PrintAndLog("Sector number must be less than 40");
448 return 1;
449 }
450 cmdp = param_getchar(Cmd, 1);
451 if (cmdp != 'a' && cmdp != 'A' && cmdp != 'b' && cmdp != 'B') {
452 PrintAndLog("Key type must be A or B");
453 return 1;
454 }
455 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
456 if (param_gethex(Cmd, 2, key, 12)) {
457 PrintAndLog("Key must include 12 HEX symbols");
458 return 1;
459 }
460 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo, keyType?'B':'A', sprint_hex(key, 6));
461
462 UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};
463 memcpy(c.d.asBytes, key, 6);
464 SendCommand(&c);
465 PrintAndLog(" ");
466
467 UsbCommand resp;
468 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
469 isOK = resp.arg[0] & 0xff;
470 data = resp.d.asBytes;
471
472 PrintAndLog("isOk:%02x", isOK);
473 if (isOK) {
474 for (i = 0; i < (sectorNo<32?3:15); i++) {
475 PrintAndLog("data : %s", sprint_hex(data + i * 16, 16));
476 }
477 PrintAndLog("trailer: %s", sprint_hex(data + (sectorNo<32?3:15) * 16, 16));
478 }
479 } else {
480 PrintAndLog("Command execute timeout");
481 }
482
483 return 0;
484 }
485
486
487 uint8_t FirstBlockOfSector(uint8_t sectorNo)
488 {
489 if (sectorNo < 32) {
490 return sectorNo * 4;
491 } else {
492 return 32 * 4 + (sectorNo - 32) * 16;
493 }
494 }
495
496
497 uint8_t NumBlocksPerSector(uint8_t sectorNo)
498 {
499 if (sectorNo < 32) {
500 return 4;
501 } else {
502 return 16;
503 }
504 }
505
506
507 int CmdHF14AMfDump(const char *Cmd)
508 {
509 uint8_t sectorNo, blockNo;
510
511 uint8_t keyA[40][6];
512 uint8_t keyB[40][6];
513 uint8_t rights[40][4];
514 uint8_t numSectors = 16;
515
516 FILE *fin;
517 FILE *fout;
518
519 UsbCommand resp;
520
521 int size = GetCardSize();
522 char cmdp = param_getchar(Cmd, 0);
523
524 PrintAndLog("Got %d",size);
525
526 return;
527
528 if ( size > -1)
529 cmdp = (char)48+size;
530
531 switch (cmdp) {
532 case '0' : numSectors = 5; break;
533 case '1' :
534 case '\0': numSectors = 16; break;
535 case '2' : numSectors = 32; break;
536 case '4' : numSectors = 40; break;
537 default: numSectors = 16;
538 }
539
540 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {
541 PrintAndLog("Usage: hf mf dump [card memory]");
542 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
543 PrintAndLog("");
544 PrintAndLog("Samples: hf mf dump");
545 PrintAndLog(" hf mf dump 4");
546 return 0;
547 }
548
549 if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {
550 PrintAndLog("Could not find file dumpkeys.bin");
551 return 1;
552 }
553
554 if ((fout = fopen("dumpdata.bin","wb")) == NULL) {
555 PrintAndLog("Could not create file name dumpdata.bin");
556 return 1;
557 }
558
559 // Read keys A from file
560 for (sectorNo=0; sectorNo<numSectors; sectorNo++) {
561 if (fread( keyA[sectorNo], 1, 6, fin ) == 0) {
562 PrintAndLog("File reading error.");
563 return 2;
564 }
565 }
566
567 // Read keys B from file
568 for (sectorNo=0; sectorNo<numSectors; sectorNo++) {
569 if (fread( keyB[sectorNo], 1, 6, fin ) == 0) {
570 PrintAndLog("File reading error.");
571 return 2;
572 }
573 }
574
575 PrintAndLog("|-----------------------------------------|");
576 PrintAndLog("|------ Reading sector access bits...-----|");
577 PrintAndLog("|-----------------------------------------|");
578
579 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
580 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 0, 0}};
581 memcpy(c.d.asBytes, keyA[sectorNo], 6);
582 SendCommand(&c);
583
584 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
585 uint8_t isOK = resp.arg[0] & 0xff;
586 uint8_t *data = resp.d.asBytes;
587 if (isOK){
588 rights[sectorNo][0] = ((data[7] & 0x10)>>4) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>2);
589 rights[sectorNo][1] = ((data[7] & 0x20)>>5) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>3);
590 rights[sectorNo][2] = ((data[7] & 0x40)>>6) | ((data[8] & 0x4)>>1) | ((data[8] & 0x40)>>4);
591 rights[sectorNo][3] = ((data[7] & 0x80)>>7) | ((data[8] & 0x8)>>2) | ((data[8] & 0x80)>>5);
592 } else {
593 PrintAndLog("Could not get access rights for sector %2d.", sectorNo);
594 }
595 } else {
596 PrintAndLog("Command execute timeout");
597 }
598 }
599
600 PrintAndLog("|-----------------------------------------|");
601 PrintAndLog("|----- Dumping all blocks to file... -----|");
602 PrintAndLog("|-----------------------------------------|");
603
604 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
605 for (blockNo = 0; blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
606 bool received = false;
607
608 if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
609 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 0, 0}};
610 memcpy(c.d.asBytes, keyA[sectorNo], 6);
611 SendCommand(&c);
612 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);
613 } else { // data block. Check if it can be read with key A or key B
614 uint8_t data_area = sectorNo<32?blockNo:blockNo/5;
615 if ((rights[sectorNo][data_area] == 3) || (rights[sectorNo][data_area] == 5)) { // only key B would work
616 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 1, 0}};
617 memcpy(c.d.asBytes, keyB[sectorNo], 6);
618 SendCommand(&c);
619 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);
620 } else if (rights[sectorNo][data_area] == 7) { // no key would work
621 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo, blockNo);
622 } else { // key A would work
623 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 0, 0}};
624 memcpy(c.d.asBytes, keyA[sectorNo], 6);
625 SendCommand(&c);
626 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);
627 }
628 }
629
630 if (received) {
631 uint8_t isOK = resp.arg[0] & 0xff;
632 uint8_t *data = resp.d.asBytes;
633 if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer. Fill in the keys.
634 data[0] = (keyA[sectorNo][0]);
635 data[1] = (keyA[sectorNo][1]);
636 data[2] = (keyA[sectorNo][2]);
637 data[3] = (keyA[sectorNo][3]);
638 data[4] = (keyA[sectorNo][4]);
639 data[5] = (keyA[sectorNo][5]);
640 data[10] = (keyB[sectorNo][0]);
641 data[11] = (keyB[sectorNo][1]);
642 data[12] = (keyB[sectorNo][2]);
643 data[13] = (keyB[sectorNo][3]);
644 data[14] = (keyB[sectorNo][4]);
645 data[15] = (keyB[sectorNo][5]);
646 }
647 if (isOK) {
648 fwrite ( data, 1, 16, fout );
649 PrintAndLog("Dumped block %2d of sector %2d into 'dumpdata.bin'", blockNo, sectorNo);
650 } else {
651 PrintAndLog("Could not read block %2d of sector %2d", blockNo, sectorNo);
652 }
653 }
654 else {
655 PrintAndLog("Command execute timeout");
656 }
657 }
658 }
659
660 fclose(fin);
661 fclose(fout);
662 return 0;
663 }
664
665
666 int CmdHF14AMfRestore(const char *Cmd)
667 {
668
669 uint8_t sectorNo,blockNo;
670 uint8_t keyType = 0;
671 uint8_t key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
672 uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
673 uint8_t keyA[40][6];
674 uint8_t keyB[40][6];
675 uint8_t numSectors;
676
677 FILE *fdump;
678 FILE *fkeys;
679
680 char cmdp = param_getchar(Cmd, 0);
681 switch (cmdp) {
682 case '0' : numSectors = 5; break;
683 case '1' :
684 case '\0': numSectors = 16; break;
685 case '2' : numSectors = 32; break;
686 case '4' : numSectors = 40; break;
687 default: numSectors = 16;
688 }
689
690 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {
691 PrintAndLog("Usage: hf mf restore [card memory]");
692 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
693 PrintAndLog("");
694 PrintAndLog("Samples: hf mf restore");
695 PrintAndLog(" hf mf restore 4");
696 return 0;
697 }
698
699 if ((fdump = fopen("dumpdata.bin","rb")) == NULL) {
700 PrintAndLog("Could not find file dumpdata.bin");
701 return 1;
702 }
703 if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) {
704 PrintAndLog("Could not find file dumpkeys.bin");
705 return 1;
706 }
707
708 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
709 if (fread(keyA[sectorNo], 1, 6, fkeys) == 0) {
710 PrintAndLog("File reading error (dumpkeys.bin).");
711 return 2;
712 }
713 }
714
715 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
716 if (fread(keyB[sectorNo], 1, 6, fkeys) == 0) {
717 PrintAndLog("File reading error (dumpkeys.bin).");
718 return 2;
719 }
720 }
721
722 PrintAndLog("Restoring dumpdata.bin to card");
723
724 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
725 for(blockNo = 0; blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
726 UsbCommand c = {CMD_MIFARE_WRITEBL, {FirstBlockOfSector(sectorNo) + blockNo, keyType, 0}};
727 memcpy(c.d.asBytes, key, 6);
728
729 if (fread(bldata, 1, 16, fdump) == 0) {
730 PrintAndLog("File reading error (dumpdata.bin).");
731 return 2;
732 }
733
734 if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer
735 bldata[0] = (keyA[sectorNo][0]);
736 bldata[1] = (keyA[sectorNo][1]);
737 bldata[2] = (keyA[sectorNo][2]);
738 bldata[3] = (keyA[sectorNo][3]);
739 bldata[4] = (keyA[sectorNo][4]);
740 bldata[5] = (keyA[sectorNo][5]);
741 bldata[10] = (keyB[sectorNo][0]);
742 bldata[11] = (keyB[sectorNo][1]);
743 bldata[12] = (keyB[sectorNo][2]);
744 bldata[13] = (keyB[sectorNo][3]);
745 bldata[14] = (keyB[sectorNo][4]);
746 bldata[15] = (keyB[sectorNo][5]);
747 }
748
749 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo) + blockNo, sprint_hex(bldata, 16));
750
751 memcpy(c.d.asBytes + 10, bldata, 16);
752 SendCommand(&c);
753
754 UsbCommand resp;
755 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
756 uint8_t isOK = resp.arg[0] & 0xff;
757 PrintAndLog("isOk:%02x", isOK);
758 } else {
759 PrintAndLog("Command execute timeout");
760 }
761 }
762 }
763
764 fclose(fdump);
765 fclose(fkeys);
766 return 0;
767 }
768
769
770 int CmdHF14AMfNested(const char *Cmd)
771 {
772 int i, j, res, iterations;
773 sector *e_sector = NULL;
774 uint8_t blockNo = 0;
775 uint8_t keyType = 0;
776 uint8_t trgBlockNo = 0;
777 uint8_t trgKeyType = 0;
778 uint8_t SectorsCnt = 0;
779 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
780 uint8_t keyBlock[13*6];
781 uint64_t key64 = 0;
782 bool transferToEml = false;
783
784 bool createDumpFile = false;
785 FILE *fkeys;
786 uint8_t standart[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
787 uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
788
789 char cmdp, ctmp;
790
791 if (strlen(Cmd)<3) {
792 PrintAndLog("Usage:");
793 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
794 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
795 PrintAndLog(" <target block number> <target key A/B> [t]");
796 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
797 PrintAndLog("t - transfer keys into emulator memory");
798 PrintAndLog("d - write keys to binary file");
799 PrintAndLog(" ");
800 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");
801 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");
802 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");
803 PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");
804 return 0;
805 }
806
807 cmdp = param_getchar(Cmd, 0);
808 blockNo = param_get8(Cmd, 1);
809 ctmp = param_getchar(Cmd, 2);
810
811 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
812 PrintAndLog("Key type must be A or B");
813 return 1;
814 }
815
816 if (ctmp != 'A' && ctmp != 'a')
817 keyType = 1;
818
819 if (param_gethex(Cmd, 3, key, 12)) {
820 PrintAndLog("Key must include 12 HEX symbols");
821 return 1;
822 }
823
824 if (cmdp == 'o' || cmdp == 'O') {
825 cmdp = 'o';
826 trgBlockNo = param_get8(Cmd, 4);
827 ctmp = param_getchar(Cmd, 5);
828 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
829 PrintAndLog("Target key type must be A or B");
830 return 1;
831 }
832 if (ctmp != 'A' && ctmp != 'a')
833 trgKeyType = 1;
834 } else {
835
836
837
838 switch (cmdp) {
839 case '0': SectorsCnt = 05; break;
840 case '1': SectorsCnt = 16; break;
841 case '2': SectorsCnt = 32; break;
842 case '4': SectorsCnt = 40; break;
843 default: SectorsCnt = 16;
844 }
845 }
846
847 ctmp = param_getchar(Cmd, 4);
848 if (ctmp == 't' || ctmp == 'T') transferToEml = true;
849 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = true;
850
851 ctmp = param_getchar(Cmd, 6);
852 transferToEml |= (ctmp == 't' || ctmp == 'T');
853 transferToEml |= (ctmp == 'd' || ctmp == 'D');
854
855 if (cmdp == 'o') {
856 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo, trgKeyType?'B':'A');
857 if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, true)) {
858 PrintAndLog("Nested error.");
859 return 2;
860 }
861 key64 = bytes_to_num(keyBlock, 6);
862 if (key64) {
863 PrintAndLog("Found valid key:%012"llx, key64);
864
865 // transfer key to the emulator
866 if (transferToEml) {
867 uint8_t sectortrailer;
868 if (trgBlockNo < 32*4) { // 4 block sector
869 sectortrailer = (trgBlockNo & 0x03) + 3;
870 } else { // 16 block sector
871 sectortrailer = (trgBlockNo & 0x0f) + 15;
872 }
873 mfEmlGetMem(keyBlock, sectortrailer, 1);
874
875 if (!trgKeyType)
876 num_to_bytes(key64, 6, keyBlock);
877 else
878 num_to_bytes(key64, 6, &keyBlock[10]);
879 mfEmlSetMem(keyBlock, sectortrailer, 1);
880 }
881 } else {
882 PrintAndLog("No valid key found");
883 }
884 }
885 else { // ------------------------------------ multiple sectors working
886 clock_t time1;
887 time1 = clock();
888
889 e_sector = calloc(SectorsCnt, sizeof(sector));
890 if (e_sector == NULL) return 1;
891
892 //test current key and additional standard keys first
893 memcpy(keyBlock, key, 6);
894 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 1 * 6));
895 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 2 * 6));
896 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 3 * 6));
897 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 4 * 6));
898 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));
899 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock + 6 * 6));
900 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock + 7 * 6));
901 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock + 8 * 6));
902 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock + 9 * 6));
903 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock + 10 * 6));
904 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock + 11 * 6));
905 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock + 12 * 6));
906 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock + 13 * 6));
907
908 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);
909 for (i = 0; i < SectorsCnt; i++) {
910 for (j = 0; j < 2; j++) {
911 if (e_sector[i].foundKey[j]) continue;
912
913 res = mfCheckKeys(FirstBlockOfSector(i), j, 6, keyBlock, &key64);
914
915 if (!res) {
916 e_sector[i].Key[j] = key64;
917 e_sector[i].foundKey[j] = 1;
918 }
919 }
920 }
921
922
923 // nested sectors
924 iterations = 0;
925 PrintAndLog("nested...");
926 bool calibrate = true;
927 for (i = 0; i < NESTED_SECTOR_RETRY; i++) {
928 for (uint8_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {
929 for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) {
930 if (e_sector[sectorNo].foundKey[trgKeyType]) continue;
931 PrintAndLog("-----------------------------------------------");
932 if(mfnested(blockNo, keyType, key, FirstBlockOfSector(sectorNo), trgKeyType, keyBlock, calibrate)) {
933 PrintAndLog("Nested error.\n");
934 return 2;
935 }
936 else {
937 calibrate = false;
938 }
939
940 iterations++;
941
942 key64 = bytes_to_num(keyBlock, 6);
943 if (key64) {
944 PrintAndLog("Found valid key:%012"llx, key64);
945 e_sector[sectorNo].foundKey[trgKeyType] = 1;
946 e_sector[sectorNo].Key[trgKeyType] = key64;
947 }
948 }
949 }
950 }
951
952 printf("Time in nested: %1.3f (%1.3f sec per key)\n\n", ((float)clock() - time1)/CLOCKS_PER_SEC, ((float)clock() - time1)/iterations/CLOCKS_PER_SEC);
953
954 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations);
955 //print them
956 PrintAndLog("|---|----------------|---|----------------|---|");
957 PrintAndLog("|sec|key A |res|key B |res|");
958 PrintAndLog("|---|----------------|---|----------------|---|");
959 for (i = 0; i < SectorsCnt; i++) {
960 PrintAndLog("|%03d| %012"llx" | %d | %012"llx" | %d |", i,
961 e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);
962 }
963 PrintAndLog("|---|----------------|---|----------------|---|");
964
965 // transfer them to the emulator
966 if (transferToEml) {
967 for (i = 0; i < SectorsCnt; i++) {
968 mfEmlGetMem(keyBlock, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);
969 if (e_sector[i].foundKey[0])
970 num_to_bytes(e_sector[i].Key[0], 6, keyBlock);
971 if (e_sector[i].foundKey[1])
972 num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);
973 mfEmlSetMem(keyBlock, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);
974 }
975 }
976
977 // Create dump file
978 if (createDumpFile) {
979 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
980 PrintAndLog("Could not create file dumpkeys.bin");
981 free(e_sector);
982 return 1;
983 }
984 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
985 for(i=0; i<SectorsCnt; i++) {
986 if (e_sector[i].foundKey[0]){
987 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
988 fwrite ( tempkey, 1, 6, fkeys );
989 }
990 else{
991 fwrite ( &standart, 1, 6, fkeys );
992 }
993 }
994 for(i=0; i<SectorsCnt; i++) {
995 if (e_sector[i].foundKey[1]){
996 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
997 fwrite ( tempkey, 1, 6, fkeys );
998 }
999 else{
1000 fwrite ( &standart, 1, 6, fkeys );
1001 }
1002 }
1003 fclose(fkeys);
1004 }
1005
1006 free(e_sector);
1007 }
1008
1009 return 0;
1010 }
1011
1012
1013 static uint32_t get_trailer_block (uint32_t uiBlock)
1014 {
1015 // Test if we are in the small or big sectors
1016 uint32_t trailer_block = 0;
1017 if (uiBlock < 128) {
1018 trailer_block = uiBlock + (3 - (uiBlock % 4));
1019 } else {
1020 trailer_block = uiBlock + (15 - (uiBlock % 16));
1021 }
1022 return trailer_block;
1023 }
1024
1025
1026 int CmdHF14AMfChk(const char *Cmd)
1027 {
1028 FILE * f;
1029 char filename[256]={0};
1030 char buf[13];
1031 uint8_t *keyBlock = NULL, *p;
1032 uint8_t stKeyBlock = 20;
1033
1034 int i, res;
1035 int keycnt = 0;
1036 char ctmp = 0x00;
1037 uint8_t blockNo = 0;
1038 uint8_t SectorsCnt = 1;
1039 uint8_t keyType = 0;
1040 uint64_t key64 = 0;
1041
1042 int transferToEml = 0;
1043 int createDumpFile = 0;
1044
1045 keyBlock = calloc(stKeyBlock, 6);
1046 if (keyBlock == NULL) return 1;
1047
1048 uint64_t defaultKeys[] =
1049 {
1050 0xffffffffffff, // Default key (first key used by program if no user defined key)
1051 0x000000000000, // Blank key
1052 0xa0a1a2a3a4a5, // NFCForum MAD key
1053 0xb0b1b2b3b4b5,
1054 0xaabbccddeeff,
1055 0x4d3a99c351dd,
1056 0x1a982c7e459a,
1057 0xd3f7d3f7d3f7,
1058 0x714c5c886e97,
1059 0x587ee5f9350f,
1060 0xa0478cc39091,
1061 0x533cb6c723f6,
1062 0x8fd0a4f256e9
1063 };
1064 int defaultKeysSize = sizeof(defaultKeys) / sizeof(uint64_t);
1065
1066 for (int defaultKeyCounter = 0; defaultKeyCounter < defaultKeysSize; defaultKeyCounter++)
1067 {
1068 num_to_bytes(defaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));
1069 }
1070
1071 if (strlen(Cmd)<3) {
1072 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");
1073 PrintAndLog(" * - all sectors");
1074 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
1075 // PrintAndLog("d - write keys to binary file\n");
1076
1077 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
1078 PrintAndLog(" hf mf chk *1 ? t");
1079 return 0;
1080 }
1081
1082 if (param_getchar(Cmd, 0)=='*') {
1083 blockNo = 3;
1084 switch(param_getchar(Cmd+1, 0)) {
1085 case '0': SectorsCnt = 5; break;
1086 case '1': SectorsCnt = 16; break;
1087 case '2': SectorsCnt = 32; break;
1088 case '4': SectorsCnt = 40; break;
1089 default: SectorsCnt = 16;
1090 }
1091 }
1092 else
1093 blockNo = param_get8(Cmd, 0);
1094
1095 ctmp = param_getchar(Cmd, 1);
1096 switch (ctmp) {
1097 case 'a': case 'A':
1098 keyType = !0;
1099 break;
1100 case 'b': case 'B':
1101 keyType = !1;
1102 break;
1103 case '?':
1104 keyType = 2;
1105 break;
1106 default:
1107 PrintAndLog("Key type must be A , B or ?");
1108 return 1;
1109 };
1110
1111 ctmp = param_getchar(Cmd, 2);
1112 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;
1113 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;
1114
1115 for (i = transferToEml || createDumpFile; param_getchar(Cmd, 2 + i); i++) {
1116 if (!param_gethex(Cmd, 2 + i, keyBlock + 6 * keycnt, 12)) {
1117 if ( stKeyBlock - keycnt < 2) {
1118 p = realloc(keyBlock, 6*(stKeyBlock+=10));
1119 if (!p) {
1120 PrintAndLog("Cannot allocate memory for Keys");
1121 free(keyBlock);
1122 return 2;
1123 }
1124 keyBlock = p;
1125 }
1126 PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,
1127 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],
1128 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);
1129 keycnt++;
1130 } else {
1131 // May be a dic file
1132 if ( param_getstr(Cmd, 2 + i,filename) > 255 ) {
1133 PrintAndLog("File name too long");
1134 free(keyBlock);
1135 return 2;
1136 }
1137
1138 if ( (f = fopen( filename , "r")) ) {
1139 while( fgets(buf, sizeof(buf), f) ){
1140 if (strlen(buf) < 12 || buf[11] == '\n')
1141 continue;
1142
1143 while (fgetc(f) != '\n' && !feof(f)) ; //goto next line
1144
1145 if( buf[0]=='#' ) continue; //The line start with # is comment, skip
1146
1147 if (!isxdigit(buf[0])){
1148 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);
1149 continue;
1150 }
1151
1152 buf[12] = 0;
1153
1154 if ( stKeyBlock - keycnt < 2) {
1155 p = realloc(keyBlock, 6*(stKeyBlock+=10));
1156 if (!p) {
1157 PrintAndLog("Cannot allocate memory for defKeys");
1158 free(keyBlock);
1159 return 2;
1160 }
1161 keyBlock = p;
1162 }
1163 memset(keyBlock + 6 * keycnt, 0, 6);
1164 num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);
1165 PrintAndLog("chk custom key[%2d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));
1166 keycnt++;
1167 memset(buf, 0, sizeof(buf));
1168 }
1169 } else {
1170 PrintAndLog("File: %s: not found or locked.", filename);
1171 free(keyBlock);
1172 return 1;
1173 fclose(f);
1174 }
1175 }
1176 }
1177
1178 if (keycnt == 0) {
1179 PrintAndLog("No key specified, trying default keys");
1180 for (;keycnt < defaultKeysSize; keycnt++)
1181 PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,
1182 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],
1183 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);
1184 }
1185
1186 for ( int t = !keyType; t < 2; keyType==2?(t++):(t=2) ) {
1187 int b=blockNo;
1188 for (int i = 0; i < SectorsCnt; ++i) {
1189 PrintAndLog("--SectorsCnt:%2d, block no:%3d, key type:%C, key count:%2d ", i, b, t?'B':'A', keycnt);
1190 uint32_t max_keys = keycnt>USB_CMD_DATA_SIZE/6?USB_CMD_DATA_SIZE/6:keycnt;
1191 for (uint32_t c = 0; c < keycnt; c+=max_keys) {
1192 uint32_t size = keycnt-c>max_keys?max_keys:keycnt-c;
1193 res = mfCheckKeys(b, t, size, &keyBlock[6*c], &key64);
1194 if (res != 1) {
1195 if (!res) {
1196 PrintAndLog("Found valid key:[%012"llx"]",key64);
1197 if (transferToEml) {
1198 uint8_t block[16];
1199 mfEmlGetMem(block, get_trailer_block(b), 1);
1200 num_to_bytes(key64, 6, block + t*10);
1201 mfEmlSetMem(block, get_trailer_block(b), 1);
1202 }
1203 }
1204 } else {
1205 PrintAndLog("Command execute timeout");
1206 }
1207 }
1208 b<127?(b+=4):(b+=16);
1209 }
1210 }
1211
1212 free(keyBlock);
1213
1214 /*
1215 // Create dump file
1216 if (createDumpFile) {
1217 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
1218 PrintAndLog("Could not create file dumpkeys.bin");
1219 free(e_sector);
1220 return 1;
1221 }
1222 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
1223 for(i=0; i<16; i++) {
1224 if (e_sector[i].foundKey[0]){
1225 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
1226 fwrite ( tempkey, 1, 6, fkeys );
1227 }
1228 else{
1229 fwrite ( &standart, 1, 6, fkeys );
1230 }
1231 }
1232 for(i=0; i<16; i++) {
1233 if (e_sector[i].foundKey[1]){
1234 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
1235 fwrite ( tempkey, 1, 6, fkeys );
1236 }
1237 else{
1238 fwrite ( &standart, 1, 6, fkeys );
1239 }
1240 }
1241 fclose(fkeys);
1242 }
1243 */
1244 return 0;
1245 }
1246
1247 int CmdHF14AMf1kSim(const char *Cmd)
1248 {
1249 uint8_t uid[7] = {0, 0, 0, 0, 0, 0, 0};
1250 uint8_t exitAfterNReads = 0;
1251 uint8_t flags = 0;
1252
1253 if (param_getchar(Cmd, 0) == 'h') {
1254 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");
1255 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");
1256 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
1257 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
1258 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
1259 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");
1260 return 0;
1261 }
1262 uint8_t pnr = 0;
1263 if (param_getchar(Cmd, pnr) == 'u') {
1264 if(param_gethex(Cmd, pnr+1, uid, 8) == 0)
1265 {
1266 flags |= FLAG_4B_UID_IN_DATA; // UID from packet
1267 } else if(param_gethex(Cmd,pnr+1,uid,14) == 0) {
1268 flags |= FLAG_7B_UID_IN_DATA;// UID from packet
1269 } else {
1270 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");
1271 return 1;
1272 }
1273 pnr +=2;
1274 }
1275 if (param_getchar(Cmd, pnr) == 'n') {
1276 exitAfterNReads = param_get8(Cmd,pnr+1);
1277 pnr += 2;
1278 }
1279 if (param_getchar(Cmd, pnr) == 'i' ) {
1280 //Using a flag to signal interactiveness, least significant bit
1281 flags |= FLAG_INTERACTIVE;
1282 pnr++;
1283 }
1284
1285 if (param_getchar(Cmd, pnr) == 'x' ) {
1286 //Using a flag to signal interactiveness, least significant bit
1287 flags |= FLAG_NR_AR_ATTACK;
1288 }
1289 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",
1290 flags & FLAG_4B_UID_IN_DATA ? sprint_hex(uid,4):
1291 flags & FLAG_7B_UID_IN_DATA ? sprint_hex(uid,7): "N/A"
1292 , exitAfterNReads, flags,flags);
1293
1294
1295 UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {flags, exitAfterNReads,0}};
1296 memcpy(c.d.asBytes, uid, sizeof(uid));
1297 SendCommand(&c);
1298
1299 if(flags & FLAG_INTERACTIVE)
1300 {
1301 UsbCommand resp;
1302 PrintAndLog("Press pm3-button to abort simulation");
1303 while(! WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1304 //We're waiting only 1.5 s at a time, otherwise we get the
1305 // annoying message about "Waiting for a response... "
1306 }
1307 }
1308
1309 return 0;
1310 }
1311
1312
1313 int CmdHF14AMfDbg(const char *Cmd)
1314 {
1315 int dbgMode = param_get32ex(Cmd, 0, 0, 10);
1316 if (dbgMode > 4) {
1317 PrintAndLog("Max debug mode parameter is 4 \n");
1318 }
1319
1320 if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {
1321 PrintAndLog("Usage: hf mf dbg <debug level>");
1322 PrintAndLog(" 0 - no debug messages");
1323 PrintAndLog(" 1 - error messages");
1324 PrintAndLog(" 2 - plus information messages");
1325 PrintAndLog(" 3 - plus debug messages");
1326 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1327 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1328 return 0;
1329 }
1330
1331 UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};
1332 SendCommand(&c);
1333
1334 return 0;
1335 }
1336
1337 int CmdHF14AMfEGet(const char *Cmd)
1338 {
1339 uint8_t blockNo = 0;
1340 uint8_t data[16];
1341
1342 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1343 PrintAndLog("Usage: hf mf eget <block number>");
1344 PrintAndLog(" sample: hf mf eget 0 ");
1345 return 0;
1346 }
1347
1348 blockNo = param_get8(Cmd, 0);
1349
1350 PrintAndLog(" ");
1351 if (!mfEmlGetMem(data, blockNo, 1)) {
1352 PrintAndLog("data[%3d]:%s", blockNo, sprint_hex(data, 16));
1353 } else {
1354 PrintAndLog("Command execute timeout");
1355 }
1356
1357 return 0;
1358 }
1359
1360
1361 int CmdHF14AMfEClear(const char *Cmd)
1362 {
1363 if (param_getchar(Cmd, 0) == 'h') {
1364 PrintAndLog("Usage: hf mf eclr");
1365 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1366 return 0;
1367 }
1368
1369 UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};
1370 SendCommand(&c);
1371 return 0;
1372 }
1373
1374
1375 int CmdHF14AMfESet(const char *Cmd)
1376 {
1377 uint8_t memBlock[16];
1378 uint8_t blockNo = 0;
1379
1380 memset(memBlock, 0x00, sizeof(memBlock));
1381
1382 if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {
1383 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1384 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1385 return 0;
1386 }
1387
1388 blockNo = param_get8(Cmd, 0);
1389
1390 if (param_gethex(Cmd, 1, memBlock, 32)) {
1391 PrintAndLog("block data must include 32 HEX symbols");
1392 return 1;
1393 }
1394
1395 // 1 - blocks count
1396 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};
1397 memcpy(c.d.asBytes, memBlock, 16);
1398 SendCommand(&c);
1399 return 0;
1400 }
1401
1402
1403 int CmdHF14AMfELoad(const char *Cmd)
1404 {
1405 FILE * f;
1406 char filename[20];
1407 char *fnameptr = filename;
1408 char buf[64];
1409 uint8_t buf8[64];
1410 int i, len, blockNum;
1411
1412 memset(filename, 0, sizeof(filename));
1413 memset(buf, 0, sizeof(buf));
1414
1415 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {
1416 PrintAndLog("It loads emul dump from the file `filename.eml`");
1417 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");
1418 PrintAndLog(" sample: hf mf eload filename");
1419 return 0;
1420 }
1421
1422 len = strlen(Cmd);
1423 if (len > 14) len = 14;
1424
1425 memcpy(filename, Cmd, len);
1426 fnameptr += len;
1427
1428 sprintf(fnameptr, ".eml");
1429
1430 // open file
1431 f = fopen(filename, "r");
1432 if (f == NULL) {
1433 PrintAndLog("File not found or locked.");
1434 return 1;
1435 }
1436
1437 blockNum = 0;
1438 while(!feof(f)){
1439 memset(buf, 0, sizeof(buf));
1440 if (fgets(buf, sizeof(buf), f) == NULL) {
1441 if((blockNum == 16*4) || (blockNum == 32*4 + 8*16)) { // supports both old (1K) and new (4K) .eml files)
1442 break;
1443 }
1444 PrintAndLog("File reading error.");
1445 return 2;
1446 }
1447 if (strlen(buf) < 32){
1448 if(strlen(buf) && feof(f))
1449 break;
1450 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1451 return 2;
1452 }
1453 for (i = 0; i < 32; i += 2) {
1454 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
1455 // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));
1456 }
1457 if (mfEmlSetMem(buf8, blockNum, 1)) {
1458 PrintAndLog("Cant set emul block: %3d", blockNum);
1459 return 3;
1460 }
1461 blockNum++;
1462
1463 if (blockNum >= 32*4 + 8*16) break;
1464 }
1465 fclose(f);
1466
1467 if ((blockNum != 16*4) && (blockNum != 32*4 + 8*16)) {
1468 PrintAndLog("File content error. There must be 64 or 256 blocks.");
1469 return 4;
1470 }
1471 PrintAndLog("Loaded %d blocks from file: %s", blockNum, filename);
1472 return 0;
1473 }
1474
1475
1476 int CmdHF14AMfESave(const char *Cmd)
1477 {
1478 FILE * f;
1479 char filename[20];
1480 char * fnameptr = filename;
1481 uint8_t buf[64];
1482 int i, j, len;
1483
1484 memset(filename, 0, sizeof(filename));
1485 memset(buf, 0, sizeof(buf));
1486
1487 if (param_getchar(Cmd, 0) == 'h') {
1488 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1489 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");
1490 PrintAndLog(" sample: hf mf esave ");
1491 PrintAndLog(" hf mf esave filename");
1492 return 0;
1493 }
1494
1495 len = strlen(Cmd);
1496 if (len > 14) len = 14;
1497
1498 if (len < 1) {
1499 // get filename
1500 if (mfEmlGetMem(buf, 0, 1)) {
1501 PrintAndLog("Cant get block: %d", 0);
1502 return 1;
1503 }
1504 for (j = 0; j < 7; j++, fnameptr += 2)
1505 sprintf(fnameptr, "%02x", buf[j]);
1506 } else {
1507 memcpy(filename, Cmd, len);
1508 fnameptr += len;
1509 }
1510
1511 sprintf(fnameptr, ".eml");
1512
1513 // open file
1514 f = fopen(filename, "w+");
1515
1516 // put hex
1517 for (i = 0; i < 32*4 + 8*16; i++) {
1518 if (mfEmlGetMem(buf, i, 1)) {
1519 PrintAndLog("Cant get block: %d", i);
1520 break;
1521 }
1522 for (j = 0; j < 16; j++)
1523 fprintf(f, "%02x", buf[j]);
1524 fprintf(f,"\n");
1525 }
1526 fclose(f);
1527
1528 PrintAndLog("Saved to file: %s", filename);
1529
1530 return 0;
1531 }
1532
1533
1534 int CmdHF14AMfECFill(const char *Cmd)
1535 {
1536 uint8_t keyType = 0;
1537 uint8_t numSectors = 16;
1538
1539 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1540 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1541 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1542 PrintAndLog("");
1543 PrintAndLog("samples: hf mf ecfill A");
1544 PrintAndLog(" hf mf ecfill A 4");
1545 PrintAndLog("Read card and transfer its data to emulator memory.");
1546 PrintAndLog("Keys must be laid in the emulator memory. \n");
1547 return 0;
1548 }
1549
1550 char ctmp = param_getchar(Cmd, 0);
1551 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
1552 PrintAndLog("Key type must be A or B");
1553 return 1;
1554 }
1555 if (ctmp != 'A' && ctmp != 'a') keyType = 1;
1556
1557 ctmp = param_getchar(Cmd, 1);
1558 switch (ctmp) {
1559 case '0' : numSectors = 5; break;
1560 case '1' :
1561 case '\0': numSectors = 16; break;
1562 case '2' : numSectors = 32; break;
1563 case '4' : numSectors = 40; break;
1564 default: numSectors = 16;
1565 }
1566
1567 printf("--params: numSectors: %d, keyType:%d", numSectors, keyType);
1568 UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {numSectors, keyType, 0}};
1569 SendCommand(&c);
1570 return 0;
1571 }
1572
1573
1574 int CmdHF14AMfEKeyPrn(const char *Cmd)
1575 {
1576 int i;
1577 uint8_t data[16];
1578 uint64_t keyA, keyB;
1579
1580 PrintAndLog("|---|----------------|----------------|");
1581 PrintAndLog("|sec|key A |key B |");
1582 PrintAndLog("|---|----------------|----------------|");
1583 for (i = 0; i < 40; i++) {
1584 if (mfEmlGetMem(data, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1)) {
1585 PrintAndLog("error get block %d", FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);
1586 break;
1587 }
1588 keyA = bytes_to_num(data, 6);
1589 keyB = bytes_to_num(data + 10, 6);
1590 PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB);
1591 }
1592 PrintAndLog("|---|----------------|----------------|");
1593
1594 return 0;
1595 }
1596
1597
1598 int CmdHF14AMfCSetUID(const char *Cmd)
1599 {
1600 uint8_t wipeCard = 0;
1601 uint8_t uid[8];
1602 uint8_t oldUid[8];
1603 int res;
1604
1605 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1606 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");
1607 PrintAndLog("sample: hf mf csetuid 01020304 w");
1608 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");
1609 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1610 return 0;
1611 }
1612
1613 if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {
1614 PrintAndLog("UID must include 8 HEX symbols");
1615 return 1;
1616 }
1617
1618 char ctmp = param_getchar(Cmd, 1);
1619 if (ctmp == 'w' || ctmp == 'W') wipeCard = 1;
1620
1621 PrintAndLog("--wipe card:%02x uid:%s", wipeCard, sprint_hex(uid, 4));
1622
1623 res = mfCSetUID(uid, oldUid, wipeCard);
1624 if (res) {
1625 PrintAndLog("Can't set UID. error=%d", res);
1626 return 1;
1627 }
1628
1629 PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));
1630 return 0;
1631 }
1632
1633 int CmdHF14AMfCSetBlk(const char *Cmd)
1634 {
1635 uint8_t uid[8];
1636 uint8_t memBlock[16];
1637 uint8_t blockNo = 0;
1638 int res;
1639 memset(memBlock, 0x00, sizeof(memBlock));
1640
1641 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1642 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");
1643 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1644 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");
1645 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1646 return 0;
1647 }
1648
1649 blockNo = param_get8(Cmd, 0);
1650
1651 if (param_gethex(Cmd, 1, memBlock, 32)) {
1652 PrintAndLog("block data must include 32 HEX symbols");
1653 return 1;
1654 }
1655
1656 PrintAndLog("--block number:%2d data:%s", blockNo, sprint_hex(memBlock, 16));
1657
1658 res = mfCSetBlock(blockNo, memBlock, uid, 0, CSETBLOCK_SINGLE_OPER);
1659 if (res) {
1660 PrintAndLog("Can't write block. error=%d", res);
1661 return 1;
1662 }
1663
1664 PrintAndLog("UID:%s", sprint_hex(uid, 4));
1665 return 0;
1666 }
1667
1668
1669 int CmdHF14AMfCLoad(const char *Cmd)
1670 {
1671 FILE * f;
1672 char filename[20];
1673 char * fnameptr = filename;
1674 char buf[64];
1675 uint8_t buf8[64];
1676 uint8_t fillFromEmulator = 0;
1677 int i, len, blockNum, flags;
1678
1679 memset(filename, 0, sizeof(filename));
1680 memset(buf, 0, sizeof(buf));
1681
1682 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {
1683 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");
1684 PrintAndLog("or from emulator memory (option `e`)");
1685 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1686 PrintAndLog(" or: hf mf cload e ");
1687 PrintAndLog(" sample: hf mf cload filename");
1688 return 0;
1689 }
1690
1691 char ctmp = param_getchar(Cmd, 0);
1692 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
1693
1694 if (fillFromEmulator) {
1695 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1696 for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {
1697 if (mfEmlGetMem(buf8, blockNum, 1)) {
1698 PrintAndLog("Cant get block: %d", blockNum);
1699 return 2;
1700 }
1701
1702 if (blockNum == 2) flags = 0;
1703 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1704
1705 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {
1706 PrintAndLog("Cant set magic card block: %d", blockNum);
1707 return 3;
1708 }
1709 }
1710 return 0;
1711 } else {
1712 len = strlen(Cmd);
1713 if (len > 14) len = 14;
1714
1715 memcpy(filename, Cmd, len);
1716 fnameptr += len;
1717
1718 sprintf(fnameptr, ".eml");
1719
1720 // open file
1721 f = fopen(filename, "r");
1722 if (f == NULL) {
1723 PrintAndLog("File not found or locked.");
1724 return 1;
1725 }
1726
1727 blockNum = 0;
1728 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1729 while(!feof(f)){
1730 memset(buf, 0, sizeof(buf));
1731 if (fgets(buf, sizeof(buf), f) == NULL) {
1732 PrintAndLog("File reading error.");
1733 return 2;
1734 }
1735
1736 if (strlen(buf) < 32){
1737 if(strlen(buf) && feof(f))
1738 break;
1739 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1740 return 2;
1741 }
1742 for (i = 0; i < 32; i += 2)
1743 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
1744
1745 if (blockNum == 2) flags = 0;
1746 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1747
1748 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {
1749 PrintAndLog("Can't set magic card block: %d", blockNum);
1750 return 3;
1751 }
1752 blockNum++;
1753
1754 if (blockNum >= 16 * 4) break; // magic card type - mifare 1K
1755 }
1756 fclose(f);
1757
1758 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
1759 PrintAndLog("File content error. There must be 64 blocks");
1760 return 4;
1761 }
1762 PrintAndLog("Loaded from file: %s", filename);
1763 return 0;
1764 }
1765 }
1766
1767 int CmdHF14AMfCGetBlk(const char *Cmd) {
1768 uint8_t memBlock[16];
1769 uint8_t blockNo = 0;
1770 int res;
1771 memset(memBlock, 0x00, sizeof(memBlock));
1772
1773 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1774 PrintAndLog("Usage: hf mf cgetblk <block number>");
1775 PrintAndLog("sample: hf mf cgetblk 1");
1776 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");
1777 return 0;
1778 }
1779
1780 blockNo = param_get8(Cmd, 0);
1781
1782 PrintAndLog("--block number:%2d ", blockNo);
1783
1784 res = mfCGetBlock(blockNo, memBlock, CSETBLOCK_SINGLE_OPER);
1785 if (res) {
1786 PrintAndLog("Can't read block. error=%d", res);
1787 return 1;
1788 }
1789
1790 PrintAndLog("block data:%s", sprint_hex(memBlock, 16));
1791 return 0;
1792 }
1793
1794
1795 int CmdHF14AMfCGetSc(const char *Cmd) {
1796 uint8_t memBlock[16];
1797 uint8_t sectorNo = 0;
1798 int i, res, flags;
1799 memset(memBlock, 0x00, sizeof(memBlock));
1800
1801 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1802 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1803 PrintAndLog("sample: hf mf cgetsc 0");
1804 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");
1805 return 0;
1806 }
1807
1808 sectorNo = param_get8(Cmd, 0);
1809 if (sectorNo > 15) {
1810 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1811 return 1;
1812 }
1813
1814 PrintAndLog("--sector number:%d ", sectorNo);
1815
1816 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1817 for (i = 0; i < 4; i++) {
1818 if (i == 1) flags = 0;
1819 if (i == 3) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1820
1821 res = mfCGetBlock(sectorNo * 4 + i, memBlock, flags);
1822 if (res) {
1823 PrintAndLog("Can't read block. %d error=%d", sectorNo * 4 + i, res);
1824 return 1;
1825 }
1826
1827 PrintAndLog("block %3d data:%s", sectorNo * 4 + i, sprint_hex(memBlock, 16));
1828 }
1829 return 0;
1830 }
1831
1832
1833 int CmdHF14AMfCSave(const char *Cmd) {
1834
1835 FILE * f;
1836 char filename[20];
1837 char * fnameptr = filename;
1838 uint8_t fillFromEmulator = 0;
1839 uint8_t buf[64];
1840 int i, j, len, flags;
1841
1842 memset(filename, 0, sizeof(filename));
1843 memset(buf, 0, sizeof(buf));
1844
1845 if (param_getchar(Cmd, 0) == 'h') {
1846 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1847 PrintAndLog("or into emulator memory (option `e`)");
1848 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1849 PrintAndLog(" sample: hf mf esave ");
1850 PrintAndLog(" hf mf esave filename");
1851 PrintAndLog(" hf mf esave e \n");
1852 return 0;
1853 }
1854
1855 char ctmp = param_getchar(Cmd, 0);
1856 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
1857
1858 if (fillFromEmulator) {
1859 // put into emulator
1860 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1861 for (i = 0; i < 16 * 4; i++) {
1862 if (i == 1) flags = 0;
1863 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1864
1865 if (mfCGetBlock(i, buf, flags)) {
1866 PrintAndLog("Cant get block: %d", i);
1867 break;
1868 }
1869
1870 if (mfEmlSetMem(buf, i, 1)) {
1871 PrintAndLog("Cant set emul block: %d", i);
1872 return 3;
1873 }
1874 }
1875 return 0;
1876 } else {
1877 len = strlen(Cmd);
1878 if (len > 14) len = 14;
1879
1880 if (len < 1) {
1881 // get filename
1882 if (mfCGetBlock(0, buf, CSETBLOCK_SINGLE_OPER)) {
1883 PrintAndLog("Cant get block: %d", 0);
1884 return 1;
1885 }
1886 for (j = 0; j < 7; j++, fnameptr += 2)
1887 sprintf(fnameptr, "%02x", buf[j]);
1888 } else {
1889 memcpy(filename, Cmd, len);
1890 fnameptr += len;
1891 }
1892
1893 sprintf(fnameptr, ".eml");
1894
1895 // open file
1896 f = fopen(filename, "w+");
1897
1898 // put hex
1899 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1900 for (i = 0; i < 16 * 4; i++) {
1901 if (i == 1) flags = 0;
1902 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1903
1904 if (mfCGetBlock(i, buf, flags)) {
1905 PrintAndLog("Cant get block: %d", i);
1906 break;
1907 }
1908 for (j = 0; j < 16; j++)
1909 fprintf(f, "%02x", buf[j]);
1910 fprintf(f,"\n");
1911 }
1912 fclose(f);
1913
1914 PrintAndLog("Saved to file: %s", filename);
1915
1916 return 0;
1917 }
1918 }
1919
1920
1921 int CmdHF14AMfSniff(const char *Cmd){
1922 // params
1923 bool wantLogToFile = 0;
1924 bool wantDecrypt = 0;
1925 //bool wantSaveToEml = 0; TODO
1926 bool wantSaveToEmlFile = 0;
1927
1928 //var
1929 int res = 0;
1930 int len = 0;
1931 int blockLen = 0;
1932 int num = 0;
1933 int pckNum = 0;
1934 uint8_t uid[7];
1935 uint8_t uid_len;
1936 uint8_t atqa[2];
1937 uint8_t sak;
1938 bool isTag;
1939 uint32_t parity;
1940 uint8_t buf[3000];
1941 uint8_t * bufPtr = buf;
1942 memset(buf, 0x00, 3000);
1943
1944 if (param_getchar(Cmd, 0) == 'h') {
1945 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
1946 PrintAndLog("You can specify:");
1947 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1948 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1949 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1950 PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1951 PrintAndLog("Usage: hf mf sniff [l][d][e][r]");
1952 PrintAndLog(" sample: hf mf sniff l d e");
1953 return 0;
1954 }
1955
1956 for (int i = 0; i < 4; i++) {
1957 char ctmp = param_getchar(Cmd, i);
1958 if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;
1959 if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;
1960 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1961 if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;
1962 }
1963
1964 printf("-------------------------------------------------------------------------\n");
1965 printf("Executing command. \n");
1966 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1967 printf("Press the key on pc keyboard to abort the client.\n");
1968 printf("-------------------------------------------------------------------------\n");
1969
1970 UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};
1971 clearCommandBuffer();
1972 SendCommand(&c);
1973
1974 // wait cycle
1975 while (true) {
1976 printf(".");
1977 fflush(stdout);
1978 if (ukbhit()) {
1979 getchar();
1980 printf("\naborted via keyboard!\n");
1981 break;
1982 }
1983
1984 UsbCommand resp;
1985 if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {
1986 res = resp.arg[0] & 0xff;
1987 len = resp.arg[1];
1988 num = resp.arg[2];
1989
1990 if (res == 0) return 0;
1991 if (res == 1) {
1992 if (num ==0) {
1993 bufPtr = buf;
1994 memset(buf, 0x00, 3000);
1995 }
1996 memcpy(bufPtr, resp.d.asBytes, len);
1997 bufPtr += len;
1998 pckNum++;
1999 }
2000 if (res == 2) {
2001 blockLen = bufPtr - buf;
2002 bufPtr = buf;
2003 printf(">\n");
2004 PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);
2005 num = 0;
2006 while (bufPtr - buf + 9 < blockLen) {
2007 isTag = bufPtr[3] & 0x80 ? true:false;
2008 bufPtr += 4;
2009 parity = *((uint32_t *)(bufPtr));
2010 bufPtr += 4;
2011 len = bufPtr[0];
2012 bufPtr++;
2013 if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff)) {
2014 memcpy(uid, bufPtr + 2, 7);
2015 memcpy(atqa, bufPtr + 2 + 7, 2);
2016 uid_len = (atqa[0] & 0xC0) == 0x40 ? 7 : 4;
2017 sak = bufPtr[11];
2018
2019 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
2020 sprint_hex(uid + (7 - uid_len), uid_len),
2021 atqa[1],
2022 atqa[0],
2023 sak);
2024 if (wantLogToFile || wantDecrypt) {
2025 FillFileNameByUID(logHexFileName, uid + (7 - uid_len), ".log", uid_len);
2026 AddLogCurrentDT(logHexFileName);
2027 }
2028 if (wantDecrypt)
2029 mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);
2030 } else {
2031 PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));
2032 if (wantLogToFile)
2033 AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);
2034 if (wantDecrypt)
2035 mfTraceDecode(bufPtr, len, parity, wantSaveToEmlFile);
2036 }
2037 bufPtr += len;
2038 num++;
2039 }
2040 }
2041 } // resp not NULL
2042 } // while (true)
2043
2044 return 0;
2045 }
2046
2047 // Tries to identify cardsize.
2048 // Returns <num> where num is:
2049 // -1 unidentified
2050 // 0 - MINI (320bytes)
2051 // 1 - 1K
2052 // 2 - 2K
2053 // 4 - 4K
2054 int GetCardSize()
2055 {
2056 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT, 0, 0}};
2057 SendCommand(&c);
2058
2059 UsbCommand resp;
2060 WaitForResponse(CMD_ACK,&resp);
2061
2062 if(resp.arg[0] == 0) {
2063 PrintAndLog("iso14443a card select failed");
2064 return -1;
2065 }
2066
2067 iso14a_card_select_t *card = (iso14a_card_select_t *)resp.d.asBytes;
2068
2069 PrintAndLog("Trying to detect card size.");
2070
2071 uint16_t atqa = 0;
2072 uint8_t sak = 0;
2073 atqa = (card->atqa[1] & 0xff) << 8;
2074 atqa += card->atqa[0] & 0xff;
2075 sak = card->sak;
2076
2077 // https://code.google.com/p/libnfc/source/browse/libnfc/target-subr.c
2078
2079 PrintAndLog("found ATAQ: %04X SAK: %02X", atqa, sak);
2080
2081
2082 // NXP MIFARE Mini 0.3k
2083 if ( (atqa && 0xff0f == 0x0004) && (sak == 0x09) ) return 0;
2084
2085 // MIFARE Classic 1K
2086 if ( (atqa && 0xff0f == 0x0004) && (sak == 0x08) ) return 1;
2087
2088 // MIFARE Classik 4K
2089 if ( (atqa && 0xff0f == 0x0002) && (sak == 0x18) ) return 4;
2090
2091 // SmartMX with MIFARE 1K emulation
2092 if ( (atqa && 0xf0ff == 0x0004) ) return 1;
2093
2094 // SmartMX with MIFARE 4K emulation
2095 if ( (atqa && 0xf0ff == 0x0002) ) return 4;
2096
2097 // Infineon MIFARE CLASSIC 1K
2098 if ( (atqa && 0xffff == 0x0004) && (sak == 0x88) ) return 1;
2099
2100 // MFC 4K emulated by Nokia 6212 Classic
2101 if ( (atqa && 0xffff == 0x0002) && (sak == 0x38) ) return 4;
2102
2103 // MFC 4K emulated by Nokia 6131 NFC
2104 if ( (atqa && 0xffff == 0x0008) && (sak == 0x38) ) return 4;
2105
2106 // MIFARE Plus (4 Byte UID or 4 Byte RID)
2107 // MIFARE Plus (7 Byte UID)
2108 if (
2109 (atqa && 0xffff == 0x0002) ||
2110 (atqa && 0xffff == 0x0004) ||
2111 (atqa && 0xffff == 0x0042) ||
2112 (atqa && 0xffff == 0x0044)
2113 )
2114 {
2115 switch(sak){
2116 case 0x08:
2117 case 0x10:
2118 //case 0x20:
2119 return 2;
2120 break;
2121 case 0x11:
2122 case 0x18:
2123 //case 0x20:
2124 return 4;
2125 break;
2126 }
2127 }
2128
2129 return -1;
2130 }
2131
2132
2133
2134
2135 static command_t CommandTable[] =
2136 {
2137 {"help", CmdHelp, 1, "This help"},
2138 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},
2139 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},
2140 //{"urdbl", CmdHF14AMfURdBl, 0, "Read MIFARE Ultralight block"},
2141 //{"urdcard", CmdHF14AMfURdCard, 0,"Read MIFARE Ultralight Card"},
2142 //{"uwrbl", CmdHF14AMfUWrBl, 0,"Write MIFARE Ultralight block"},
2143 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},
2144 {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},
2145 {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},
2146 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},
2147 {"chk", CmdHF14AMfChk, 0, "Test block keys"},
2148 {"mifare", CmdHF14AMifare, 0, "Read parity error messages."},
2149 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},
2150 {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},
2151 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},
2152 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},
2153 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},
2154 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},
2155 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},
2156 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},
2157 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},
2158 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},
2159 {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},
2160 {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block into magic Chinese card"},
2161 {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block from magic Chinese card"},
2162 {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector from magic Chinese card"},
2163 {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},
2164 {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},
2165 {NULL, NULL, 0, NULL}
2166 };
2167
2168 int CmdHFMF(const char *Cmd)
2169 {
2170 // flush
2171 WaitForResponseTimeout(CMD_ACK,NULL,100);
2172
2173 CmdsParse(CommandTable, Cmd);
2174 return 0;
2175 }
2176
2177 int CmdHelp(const char *Cmd)
2178 {
2179 CmdsHelp(CommandTable);
2180 return 0;
2181 }
Impressum, Datenschutz