]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
FIX: Minor correction of variablename. don't even ask.
[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[255];
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 > 250) len = 250;
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[255];
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 > 250) len = 250;
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 numSectors;
1596 uint8_t data[16];
1597 uint64_t keyA, keyB;
1598
1599 if (param_getchar(Cmd, 0) == 'h') {
1600 PrintAndLog("It prints the keys loaded in the emulator memory");
1601 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
1602 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1603 PrintAndLog("");
1604 PrintAndLog(" sample: hf mf ekeyprn 1");
1605 return 0;
1606 }
1607
1608 char cmdp = param_getchar(Cmd, 0);
1609
1610 switch (cmdp) {
1611 case '0' : numSectors = 5; break;
1612 case '1' :
1613 case '\0': numSectors = 16; break;
1614 case '2' : numSectors = 32; break;
1615 case '4' : numSectors = 40; break;
1616 default: numSectors = 16;
1617 }
1618
1619 PrintAndLog("|---|----------------|----------------|");
1620 PrintAndLog("|sec|key A |key B |");
1621 PrintAndLog("|---|----------------|----------------|");
1622 for (i = 0; i < numSectors; i++) {
1623 if (mfEmlGetMem(data, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1)) {
1624 PrintAndLog("error get block %d", FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);
1625 break;
1626 }
1627 keyA = bytes_to_num(data, 6);
1628 keyB = bytes_to_num(data + 10, 6);
1629 PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB);
1630 }
1631 PrintAndLog("|---|----------------|----------------|");
1632
1633 return 0;
1634 }
1635
1636
1637 int CmdHF14AMfCSetUID(const char *Cmd)
1638 {
1639 uint8_t wipeCard = 0;
1640 uint8_t uid[8] = {0x00};
1641 uint8_t oldUid[8] = {0x00};
1642 int res;
1643
1644 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1645 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");
1646 PrintAndLog("sample: hf mf csetuid 01020304 w");
1647 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");
1648 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1649 return 0;
1650 }
1651
1652 if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {
1653 PrintAndLog("UID must include 8 HEX symbols");
1654 return 1;
1655 }
1656
1657 char ctmp = param_getchar(Cmd, 1);
1658 if (ctmp == 'w' || ctmp == 'W') wipeCard = 1;
1659
1660 PrintAndLog("--wipe card:%02x uid:%s", wipeCard, sprint_hex(uid, 4));
1661
1662 res = mfCSetUID(uid, oldUid, wipeCard);
1663 if (res) {
1664 PrintAndLog("Can't set UID. error=%d", res);
1665 return 1;
1666 }
1667
1668 PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));
1669 return 0;
1670 }
1671
1672 int CmdHF14AMfCSetBlk(const char *Cmd)
1673 {
1674 uint8_t uid[8];
1675 uint8_t memBlock[16];
1676 uint8_t blockNo = 0;
1677 int res;
1678 memset(memBlock, 0x00, sizeof(memBlock));
1679
1680 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1681 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");
1682 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1683 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");
1684 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1685 return 0;
1686 }
1687
1688 blockNo = param_get8(Cmd, 0);
1689
1690 if (param_gethex(Cmd, 1, memBlock, 32)) {
1691 PrintAndLog("block data must include 32 HEX symbols");
1692 return 1;
1693 }
1694
1695 PrintAndLog("--block number:%2d data:%s", blockNo, sprint_hex(memBlock, 16));
1696
1697 res = mfCSetBlock(blockNo, memBlock, uid, 0, CSETBLOCK_SINGLE_OPER);
1698 if (res) {
1699 PrintAndLog("Can't write block. error=%d", res);
1700 return 1;
1701 }
1702
1703 PrintAndLog("UID:%s", sprint_hex(uid, 4));
1704 return 0;
1705 }
1706
1707
1708 int CmdHF14AMfCLoad(const char *Cmd)
1709 {
1710 FILE * f;
1711 char filename[255];
1712 char * fnameptr = filename;
1713 char buf[64];
1714 uint8_t buf8[64];
1715 uint8_t fillFromEmulator = 0;
1716 int i, len, blockNum, flags;
1717
1718 memset(filename, 0, sizeof(filename));
1719 memset(buf, 0, sizeof(buf));
1720
1721 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {
1722 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");
1723 PrintAndLog("or from emulator memory (option `e`)");
1724 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1725 PrintAndLog(" or: hf mf cload e ");
1726 PrintAndLog(" sample: hf mf cload filename");
1727 return 0;
1728 }
1729
1730 char ctmp = param_getchar(Cmd, 0);
1731 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
1732
1733 if (fillFromEmulator) {
1734 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1735 for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {
1736 if (mfEmlGetMem(buf8, blockNum, 1)) {
1737 PrintAndLog("Cant get block: %d", blockNum);
1738 return 2;
1739 }
1740
1741 if (blockNum == 2) flags = 0;
1742 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1743
1744 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {
1745 PrintAndLog("Cant set magic card block: %d", blockNum);
1746 return 3;
1747 }
1748 }
1749 return 0;
1750 } else {
1751 len = strlen(Cmd);
1752 if (len > 250) len = 250;
1753
1754 memcpy(filename, Cmd, len);
1755 fnameptr += len;
1756
1757 sprintf(fnameptr, ".eml");
1758
1759 // open file
1760 f = fopen(filename, "r");
1761 if (f == NULL) {
1762 PrintAndLog("File not found or locked.");
1763 return 1;
1764 }
1765
1766 blockNum = 0;
1767 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1768 while(!feof(f)){
1769 memset(buf, 0, sizeof(buf));
1770 if (fgets(buf, sizeof(buf), f) == NULL) {
1771 PrintAndLog("File reading error.");
1772 return 2;
1773 }
1774
1775 if (strlen(buf) < 32){
1776 if(strlen(buf) && feof(f))
1777 break;
1778 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1779 return 2;
1780 }
1781 for (i = 0; i < 32; i += 2)
1782 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
1783
1784 if (blockNum == 2) flags = 0;
1785 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1786
1787 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {
1788 PrintAndLog("Can't set magic card block: %d", blockNum);
1789 return 3;
1790 }
1791 blockNum++;
1792
1793 if (blockNum >= 16 * 4) break; // magic card type - mifare 1K
1794 }
1795 fclose(f);
1796
1797 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
1798 PrintAndLog("File content error. There must be 64 blocks");
1799 return 4;
1800 }
1801 PrintAndLog("Loaded from file: %s", filename);
1802 return 0;
1803 }
1804 }
1805
1806 int CmdHF14AMfCGetBlk(const char *Cmd) {
1807 uint8_t memBlock[16];
1808 uint8_t blockNo = 0;
1809 int res;
1810 memset(memBlock, 0x00, sizeof(memBlock));
1811
1812 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1813 PrintAndLog("Usage: hf mf cgetblk <block number>");
1814 PrintAndLog("sample: hf mf cgetblk 1");
1815 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");
1816 return 0;
1817 }
1818
1819 blockNo = param_get8(Cmd, 0);
1820
1821 PrintAndLog("--block number:%2d ", blockNo);
1822
1823 res = mfCGetBlock(blockNo, memBlock, CSETBLOCK_SINGLE_OPER);
1824 if (res) {
1825 PrintAndLog("Can't read block. error=%d", res);
1826 return 1;
1827 }
1828
1829 PrintAndLog("block data:%s", sprint_hex(memBlock, 16));
1830 return 0;
1831 }
1832
1833
1834 int CmdHF14AMfCGetSc(const char *Cmd) {
1835 uint8_t memBlock[16];
1836 uint8_t sectorNo = 0;
1837 int i, res, flags;
1838 memset(memBlock, 0x00, sizeof(memBlock));
1839
1840 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1841 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1842 PrintAndLog("sample: hf mf cgetsc 0");
1843 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");
1844 return 0;
1845 }
1846
1847 sectorNo = param_get8(Cmd, 0);
1848 if (sectorNo > 15) {
1849 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1850 return 1;
1851 }
1852
1853 PrintAndLog("--sector number:%d ", sectorNo);
1854
1855 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1856 for (i = 0; i < 4; i++) {
1857 if (i == 1) flags = 0;
1858 if (i == 3) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1859
1860 res = mfCGetBlock(sectorNo * 4 + i, memBlock, flags);
1861 if (res) {
1862 PrintAndLog("Can't read block. %d error=%d", sectorNo * 4 + i, res);
1863 return 1;
1864 }
1865
1866 PrintAndLog("block %3d data:%s", sectorNo * 4 + i, sprint_hex(memBlock, 16));
1867 }
1868 return 0;
1869 }
1870
1871
1872 int CmdHF14AMfCSave(const char *Cmd) {
1873
1874 FILE * f;
1875 char filename[255];
1876 char * fnameptr = filename;
1877 uint8_t fillFromEmulator = 0;
1878 uint8_t buf[64];
1879 int i, j, len, flags;
1880
1881 memset(filename, 0, sizeof(filename));
1882 memset(buf, 0, sizeof(buf));
1883
1884 if (param_getchar(Cmd, 0) == 'h') {
1885 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1886 PrintAndLog("or into emulator memory (option `e`)");
1887 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1888 PrintAndLog(" sample: hf mf esave ");
1889 PrintAndLog(" hf mf esave filename");
1890 PrintAndLog(" hf mf esave e \n");
1891 return 0;
1892 }
1893
1894 char ctmp = param_getchar(Cmd, 0);
1895 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
1896
1897 if (fillFromEmulator) {
1898 // put into emulator
1899 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1900 for (i = 0; i < 16 * 4; i++) {
1901 if (i == 1) flags = 0;
1902 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1903
1904 if (mfCGetBlock(i, buf, flags)) {
1905 PrintAndLog("Cant get block: %d", i);
1906 break;
1907 }
1908
1909 if (mfEmlSetMem(buf, i, 1)) {
1910 PrintAndLog("Cant set emul block: %d", i);
1911 return 3;
1912 }
1913 }
1914 return 0;
1915 } else {
1916 len = strlen(Cmd);
1917 if (len > 250) len = 250;
1918
1919 if (len < 1) {
1920 // get filename
1921 if (mfCGetBlock(0, buf, CSETBLOCK_SINGLE_OPER)) {
1922 PrintAndLog("Cant get block: %d", 0);
1923 return 1;
1924 }
1925 for (j = 0; j < 7; j++, fnameptr += 2)
1926 sprintf(fnameptr, "%02x", buf[j]);
1927 } else {
1928 memcpy(filename, Cmd, len);
1929 fnameptr += len;
1930 }
1931
1932 sprintf(fnameptr, ".eml");
1933
1934 // open file
1935 f = fopen(filename, "w+");
1936
1937 // put hex
1938 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1939 for (i = 0; i < 16 * 4; i++) {
1940 if (i == 1) flags = 0;
1941 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1942
1943 if (mfCGetBlock(i, buf, flags)) {
1944 PrintAndLog("Cant get block: %d", i);
1945 break;
1946 }
1947 for (j = 0; j < 16; j++)
1948 fprintf(f, "%02x", buf[j]);
1949 fprintf(f,"\n");
1950 }
1951 fclose(f);
1952
1953 PrintAndLog("Saved to file: %s", filename);
1954
1955 return 0;
1956 }
1957 }
1958
1959
1960 int CmdHF14AMfSniff(const char *Cmd){
1961 // params
1962 bool wantLogToFile = 0;
1963 bool wantDecrypt = 0;
1964 //bool wantSaveToEml = 0; TODO
1965 bool wantSaveToEmlFile = 0;
1966
1967 //var
1968 int res = 0;
1969 int len = 0;
1970 int blockLen = 0;
1971 int num = 0;
1972 int pckNum = 0;
1973 uint8_t uid[7];
1974 uint8_t uid_len;
1975 uint8_t atqa[2];
1976 uint8_t sak;
1977 bool isTag;
1978 uint32_t parity;
1979 uint8_t buf[3000];
1980 uint8_t * bufPtr = buf;
1981 memset(buf, 0x00, 3000);
1982
1983 if (param_getchar(Cmd, 0) == 'h') {
1984 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
1985 PrintAndLog("You can specify:");
1986 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1987 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1988 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1989 PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1990 PrintAndLog("Usage: hf mf sniff [l][d][e][r]");
1991 PrintAndLog(" sample: hf mf sniff l d e");
1992 return 0;
1993 }
1994
1995 for (int i = 0; i < 4; i++) {
1996 char ctmp = param_getchar(Cmd, i);
1997 if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;
1998 if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;
1999 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
2000 if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;
2001 }
2002
2003 printf("-------------------------------------------------------------------------\n");
2004 printf("Executing command. \n");
2005 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
2006 printf("Press the key on pc keyboard to abort the client.\n");
2007 printf("-------------------------------------------------------------------------\n");
2008
2009 UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};
2010 clearCommandBuffer();
2011 SendCommand(&c);
2012
2013 // wait cycle
2014 while (true) {
2015 printf(".");
2016 fflush(stdout);
2017 if (ukbhit()) {
2018 getchar();
2019 printf("\naborted via keyboard!\n");
2020 break;
2021 }
2022
2023 UsbCommand resp;
2024 if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {
2025 res = resp.arg[0] & 0xff;
2026 len = resp.arg[1];
2027 num = resp.arg[2];
2028
2029 if (res == 0) return 0;
2030 if (res == 1) {
2031 if (num ==0) {
2032 bufPtr = buf;
2033 memset(buf, 0x00, 3000);
2034 }
2035 memcpy(bufPtr, resp.d.asBytes, len);
2036 bufPtr += len;
2037 pckNum++;
2038 }
2039 if (res == 2) {
2040 blockLen = bufPtr - buf;
2041 bufPtr = buf;
2042 printf(">\n");
2043 PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);
2044 num = 0;
2045 while (bufPtr - buf + 9 < blockLen) {
2046 isTag = bufPtr[3] & 0x80 ? true:false;
2047 bufPtr += 4;
2048 parity = *((uint32_t *)(bufPtr));
2049 bufPtr += 4;
2050 len = bufPtr[0];
2051 bufPtr++;
2052 if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff)) {
2053 memcpy(uid, bufPtr + 2, 7);
2054 memcpy(atqa, bufPtr + 2 + 7, 2);
2055 uid_len = (atqa[0] & 0xC0) == 0x40 ? 7 : 4;
2056 sak = bufPtr[11];
2057
2058 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
2059 sprint_hex(uid + (7 - uid_len), uid_len),
2060 atqa[1],
2061 atqa[0],
2062 sak);
2063 if (wantLogToFile || wantDecrypt) {
2064 FillFileNameByUID(logHexFileName, uid + (7 - uid_len), ".log", uid_len);
2065 AddLogCurrentDT(logHexFileName);
2066 }
2067 if (wantDecrypt)
2068 mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);
2069 } else {
2070 PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));
2071 if (wantLogToFile)
2072 AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);
2073 if (wantDecrypt)
2074 mfTraceDecode(bufPtr, len, parity, wantSaveToEmlFile);
2075 }
2076 bufPtr += len;
2077 num++;
2078 }
2079 }
2080 } // resp not NULL
2081 } // while (true)
2082
2083 return 0;
2084 }
2085
2086 // Tries to identify cardsize.
2087 // Returns <num> where num is:
2088 // -1 unidentified
2089 // 0 - MINI (320bytes)
2090 // 1 - 1K
2091 // 2 - 2K
2092 // 4 - 4K
2093 int GetCardSize()
2094 {
2095 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT, 0, 0}};
2096 SendCommand(&c);
2097
2098 UsbCommand resp;
2099 WaitForResponse(CMD_ACK,&resp);
2100
2101 if(resp.arg[0] == 0) {
2102 PrintAndLog("iso14443a card select failed");
2103 return -1;
2104 }
2105
2106 iso14a_card_select_t *card = (iso14a_card_select_t *)resp.d.asBytes;
2107
2108 PrintAndLog("Trying to detect card size.");
2109
2110 uint16_t atqa = 0;
2111 uint8_t sak = 0;
2112 atqa = (card->atqa[1] & 0xff) << 8;
2113 atqa += card->atqa[0] & 0xff;
2114 sak = card->sak;
2115
2116 // https://code.google.com/p/libnfc/source/browse/libnfc/target-subr.c
2117
2118 PrintAndLog("found ATAQ: %04X SAK: %02X", atqa, sak);
2119
2120
2121 // NXP MIFARE Mini 0.3k
2122 if ( ( (atqa & 0xff0f) == 0x0004) && (sak == 0x09) ) return 0;
2123
2124 // MIFARE Classic 1K
2125 if ( ((atqa & 0xff0f) == 0x0004) && (sak == 0x08) ) return 1;
2126
2127 // MIFARE Classik 4K
2128 if ( ((atqa & 0xff0f) == 0x0002) && (sak == 0x18) ) return 4;
2129
2130 // SmartMX with MIFARE 1K emulation
2131 if ( ((atqa & 0xf0ff) == 0x0004) ) return 1;
2132
2133 // SmartMX with MIFARE 4K emulation
2134 if ( ((atqa & 0xf0ff) == 0x0002) ) return 4;
2135
2136 // Infineon MIFARE CLASSIC 1K
2137 if ( ((atqa & 0xffff) == 0x0004) && (sak == 0x88) ) return 1;
2138
2139 // MFC 4K emulated by Nokia 6212 Classic
2140 if ( ((atqa & 0xffff) == 0x0002) && (sak == 0x38) ) return 4;
2141
2142 // MFC 4K emulated by Nokia 6131 NFC
2143 if ( ((atqa & 0xffff) == 0x0008) && (sak == 0x38) ) return 4;
2144
2145
2146 PrintAndLog("BEFOOO 1K %02X", (atqa & 0xff0f));
2147
2148 // MIFARE Plus (4 Byte UID or 4 Byte RID)
2149 // MIFARE Plus (7 Byte UID)
2150 if (
2151 ((atqa & 0xffff) == 0x0002) |
2152 ((atqa & 0xffff) == 0x0004) |
2153 ((atqa & 0xffff) == 0x0042) |
2154 ((atqa & 0xffff) == 0x0044)
2155 )
2156 {
2157 switch(sak){
2158 case 0x08:
2159 case 0x10: {
2160 //case 0x20:
2161 PrintAndLog("2");
2162 return 2;
2163 break;
2164 }
2165 case 0x11:
2166 case 0x18:{
2167 //case 0x20:
2168 PrintAndLog("4");
2169 return 4;
2170 break;
2171 }
2172 }
2173 }
2174
2175 return -1;
2176 }
2177
2178
2179
2180
2181 static command_t CommandTable[] =
2182 {
2183 {"help", CmdHelp, 1, "This help"},
2184 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},
2185 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},
2186 //{"urdbl", CmdHF14AMfURdBl, 0, "Read MIFARE Ultralight block"},
2187 //{"urdcard", CmdHF14AMfURdCard, 0,"Read MIFARE Ultralight Card"},
2188 //{"uwrbl", CmdHF14AMfUWrBl, 0,"Write MIFARE Ultralight block"},
2189 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},
2190 {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},
2191 {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},
2192 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},
2193 {"chk", CmdHF14AMfChk, 0, "Test block keys"},
2194 {"mifare", CmdHF14AMifare, 0, "Read parity error messages."},
2195 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},
2196 {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},
2197 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},
2198 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},
2199 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},
2200 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},
2201 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},
2202 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},
2203 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},
2204 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},
2205 {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},
2206 {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block into magic Chinese card"},
2207 {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block from magic Chinese card"},
2208 {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector from magic Chinese card"},
2209 {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},
2210 {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},
2211 {NULL, NULL, 0, NULL}
2212 };
2213
2214 int CmdHFMF(const char *Cmd)
2215 {
2216 // flush
2217 WaitForResponseTimeout(CMD_ACK,NULL,100);
2218
2219 CmdsParse(CommandTable, Cmd);
2220 return 0;
2221 }
2222
2223 int CmdHelp(const char *Cmd)
2224 {
2225 CmdsHelp(CommandTable);
2226 return 0;
2227 }
Impressum, Datenschutz