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