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