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