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