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