]> git.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
6075db533092957b82ae9c4a789fd8d5f7a76859
[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 u 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 - plus information messages");
1264 PrintAndLog(" 3 - plus debug messages");
1265 PrintAndLog(" 4 - print even debug messages in timing critical functions");
1266 PrintAndLog(" Note: this option therefore may cause malfunction itself");
1267 return 0;
1268 }
1269
1270 UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};
1271 SendCommand(&c);
1272
1273 return 0;
1274 }
1275
1276 int CmdHF14AMfEGet(const char *Cmd)
1277 {
1278 uint8_t blockNo = 0;
1279 uint8_t data[3 * 16];
1280 int i;
1281
1282 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1283 PrintAndLog("Usage: hf mf eget <block number>");
1284 PrintAndLog(" sample: hf mf eget 0 ");
1285 return 0;
1286 }
1287
1288 blockNo = param_get8(Cmd, 0);
1289
1290 PrintAndLog(" ");
1291 if (!mfEmlGetMem(data, blockNo, 3)) {
1292 for (i = 0; i < 3; i++) {
1293 PrintAndLog("data[%d]:%s", blockNo + i, sprint_hex(data + i * 16, 16));
1294 }
1295 } else {
1296 PrintAndLog("Command execute timeout");
1297 }
1298
1299 return 0;
1300 }
1301
1302 int CmdHF14AMfEClear(const char *Cmd)
1303 {
1304 if (param_getchar(Cmd, 0) == 'h') {
1305 PrintAndLog("Usage: hf mf eclr");
1306 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1307 return 0;
1308 }
1309
1310 UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};
1311 SendCommand(&c);
1312 return 0;
1313 }
1314
1315 int CmdHF14AMfESet(const char *Cmd)
1316 {
1317 uint8_t memBlock[16];
1318 uint8_t blockNo = 0;
1319
1320 memset(memBlock, 0x00, sizeof(memBlock));
1321
1322 if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {
1323 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1324 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1325 return 0;
1326 }
1327
1328 blockNo = param_get8(Cmd, 0);
1329
1330 if (param_gethex(Cmd, 1, memBlock, 32)) {
1331 PrintAndLog("block data must include 32 HEX symbols");
1332 return 1;
1333 }
1334
1335 // 1 - blocks count
1336 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};
1337 memcpy(c.d.asBytes, memBlock, 16);
1338 SendCommand(&c);
1339 return 0;
1340 }
1341
1342 int CmdHF14AMfELoad(const char *Cmd)
1343 {
1344 FILE * f;
1345 char filename[20];
1346 char * fnameptr = filename;
1347 char buf[64];
1348 uint8_t buf8[64];
1349 int i, len, blockNum;
1350
1351 memset(filename, 0, sizeof(filename));
1352 memset(buf, 0, sizeof(buf));
1353
1354 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {
1355 PrintAndLog("It loads emul dump from the file `filename.eml`");
1356 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");
1357 PrintAndLog(" sample: hf mf eload filename");
1358 return 0;
1359 }
1360
1361 len = strlen(Cmd);
1362 if (len > 14) len = 14;
1363
1364 memcpy(filename, Cmd, len);
1365 fnameptr += len;
1366
1367 sprintf(fnameptr, ".eml");
1368
1369 // open file
1370 f = fopen(filename, "r");
1371 if (f == NULL) {
1372 PrintAndLog("File not found or locked.");
1373 return 1;
1374 }
1375
1376 blockNum = 0;
1377 while(!feof(f)){
1378 memset(buf, 0, sizeof(buf));
1379 if (fgets(buf, sizeof(buf), f) == NULL) {
1380 if(blockNum == 16 * 4)
1381 {
1382 break;
1383 }
1384 PrintAndLog("File reading error.");
1385 return 2;
1386 }
1387
1388 if (strlen(buf) < 32){
1389 if(strlen(buf) && feof(f))
1390 break;
1391 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1392 return 2;
1393 }
1394 for (i = 0; i < 32; i += 2)
1395 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
1396 // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));
1397
1398 if (mfEmlSetMem(buf8, blockNum, 1)) {
1399 PrintAndLog("Cant set emul block: %d", blockNum);
1400 return 3;
1401 }
1402 blockNum++;
1403
1404 if (blockNum >= 32 * 4 + 8 * 16) break;
1405 }
1406 fclose(f);
1407
1408 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
1409 PrintAndLog("File content error. There must be 64 blocks");
1410 return 4;
1411 }
1412 PrintAndLog("Loaded %d blocks from file: %s", blockNum, filename);
1413 return 0;
1414 }
1415
1416 int CmdHF14AMfESave(const char *Cmd)
1417 {
1418 FILE * f;
1419 char filename[20];
1420 char * fnameptr = filename;
1421 uint8_t buf[64];
1422 int i, j, len;
1423
1424 memset(filename, 0, sizeof(filename));
1425 memset(buf, 0, sizeof(buf));
1426
1427 if (param_getchar(Cmd, 0) == 'h') {
1428 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1429 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");
1430 PrintAndLog(" sample: hf mf esave ");
1431 PrintAndLog(" hf mf esave filename");
1432 return 0;
1433 }
1434
1435 len = strlen(Cmd);
1436 if (len > 14) len = 14;
1437
1438 if (len < 1) {
1439 // get filename
1440 if (mfEmlGetMem(buf, 0, 1)) {
1441 PrintAndLog("Cant get block: %d", 0);
1442 return 1;
1443 }
1444 for (j = 0; j < 7; j++, fnameptr += 2)
1445 sprintf(fnameptr, "%02x", buf[j]);
1446 } else {
1447 memcpy(filename, Cmd, len);
1448 fnameptr += len;
1449 }
1450
1451 sprintf(fnameptr, ".eml");
1452
1453 // open file
1454 f = fopen(filename, "w+");
1455
1456 // put hex
1457 for (i = 0; i < 32 * 4 + 8 * 16; i++) {
1458 if (mfEmlGetMem(buf, i, 1)) {
1459 PrintAndLog("Cant get block: %d", i);
1460 break;
1461 }
1462 for (j = 0; j < 16; j++)
1463 fprintf(f, "%02x", buf[j]);
1464 fprintf(f,"\n");
1465 }
1466 fclose(f);
1467
1468 PrintAndLog("Saved to file: %s", filename);
1469
1470 return 0;
1471 }
1472
1473 int CmdHF14AMfECFill(const char *Cmd)
1474 {
1475 uint8_t keyType = 0;
1476
1477 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1478 PrintAndLog("Usage: hf mf ecfill <key A/B>");
1479 PrintAndLog("sample: hf mf ecfill A");
1480 PrintAndLog("Card data blocks transfers to card emulator memory.");
1481 PrintAndLog("Keys must be laid in the simulator memory. \n");
1482 return 0;
1483 }
1484
1485 char ctmp = param_getchar(Cmd, 0);
1486 if (ctmp == 0x00) {
1487 PrintAndLog("Key type must be A or B");
1488 return 1;
1489 }
1490 if (ctmp != 'A' && ctmp != 'a') keyType = 1;
1491
1492 UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {0, keyType, 0}};
1493 SendCommand(&c);
1494 return 0;
1495 }
1496
1497 int CmdHF14AMfEKeyPrn(const char *Cmd)
1498 {
1499 int i,b=-1;
1500 uint8_t data[16];
1501 uint64_t keyA, keyB;
1502
1503 PrintAndLog("|---|----------------|----------------|");
1504 PrintAndLog("|sec|key A |key B |");
1505 PrintAndLog("|---|----------------|----------------|");
1506 for (i = 0; i < 40; i++) {
1507 b<127?(b+=4):(b+=16);
1508 if (mfEmlGetMem(data, b, 1)) {
1509 PrintAndLog("error get block %d", b);
1510 break;
1511 }
1512 keyA = bytes_to_num(data, 6);
1513 keyB = bytes_to_num(data + 10, 6);
1514 PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB);
1515 }
1516 PrintAndLog("|---|----------------|----------------|");
1517
1518 return 0;
1519 }
1520
1521 int CmdHF14AMfCSetUID(const char *Cmd)
1522 {
1523 uint8_t wipeCard = 0;
1524 uint8_t uid[8];
1525 uint8_t oldUid[8];
1526 int res;
1527
1528 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1529 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");
1530 PrintAndLog("sample: hf mf csetuid 01020304 w");
1531 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");
1532 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1533 return 0;
1534 }
1535
1536 if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {
1537 PrintAndLog("UID must include 8 HEX symbols");
1538 return 1;
1539 }
1540
1541 char ctmp = param_getchar(Cmd, 1);
1542 if (ctmp == 'w' || ctmp == 'W') wipeCard = 1;
1543
1544 PrintAndLog("--wipe card:%02x uid:%s", wipeCard, sprint_hex(uid, 4));
1545
1546 res = mfCSetUID(uid, oldUid, wipeCard);
1547 if (res) {
1548 PrintAndLog("Can't set UID. error=%d", res);
1549 return 1;
1550 }
1551
1552 PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));
1553 return 0;
1554 }
1555
1556 int CmdHF14AMfCSetBlk(const char *Cmd)
1557 {
1558 uint8_t uid[8];
1559 uint8_t memBlock[16];
1560 uint8_t blockNo = 0;
1561 int res;
1562 memset(memBlock, 0x00, sizeof(memBlock));
1563
1564 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1565 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");
1566 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
1567 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");
1568 PrintAndLog("If you want wipe card then add 'w' into command line. \n");
1569 return 0;
1570 }
1571
1572 blockNo = param_get8(Cmd, 0);
1573
1574 if (param_gethex(Cmd, 1, memBlock, 32)) {
1575 PrintAndLog("block data must include 32 HEX symbols");
1576 return 1;
1577 }
1578
1579 PrintAndLog("--block number:%02x data:%s", blockNo, sprint_hex(memBlock, 16));
1580
1581 res = mfCSetBlock(blockNo, memBlock, uid, 0, CSETBLOCK_SINGLE_OPER);
1582 if (res) {
1583 PrintAndLog("Can't write block. error=%d", res);
1584 return 1;
1585 }
1586
1587 PrintAndLog("UID:%s", sprint_hex(uid, 4));
1588 return 0;
1589 }
1590
1591 int CmdHF14AMfCLoad(const char *Cmd)
1592 {
1593 FILE * f;
1594 char filename[20];
1595 char * fnameptr = filename;
1596 char buf[64];
1597 uint8_t buf8[64];
1598 uint8_t fillFromEmulator = 0;
1599 int i, len, blockNum, flags;
1600
1601 memset(filename, 0, sizeof(filename));
1602 memset(buf, 0, sizeof(buf));
1603
1604 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {
1605 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");
1606 PrintAndLog("or from emulator memory (option `e`)");
1607 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
1608 PrintAndLog(" or: hf mf cload e ");
1609 PrintAndLog(" sample: hf mf cload filename");
1610 return 0;
1611 }
1612
1613 char ctmp = param_getchar(Cmd, 0);
1614 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
1615
1616 if (fillFromEmulator) {
1617 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1618 for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {
1619 if (mfEmlGetMem(buf8, blockNum, 1)) {
1620 PrintAndLog("Cant get block: %d", blockNum);
1621 return 2;
1622 }
1623
1624 if (blockNum == 2) flags = 0;
1625 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1626
1627 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {
1628 PrintAndLog("Cant set magic card block: %d", blockNum);
1629 return 3;
1630 }
1631 }
1632 return 0;
1633 } else {
1634 len = strlen(Cmd);
1635 if (len > 14) len = 14;
1636
1637 memcpy(filename, Cmd, len);
1638 fnameptr += len;
1639
1640 sprintf(fnameptr, ".eml");
1641
1642 // open file
1643 f = fopen(filename, "r");
1644 if (f == NULL) {
1645 PrintAndLog("File not found or locked.");
1646 return 1;
1647 }
1648
1649 blockNum = 0;
1650 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1651 while(!feof(f)){
1652 memset(buf, 0, sizeof(buf));
1653 if (fgets(buf, sizeof(buf), f) == NULL) {
1654 PrintAndLog("File reading error.");
1655 return 2;
1656 }
1657
1658 if (strlen(buf) < 32){
1659 if(strlen(buf) && feof(f))
1660 break;
1661 PrintAndLog("File content error. Block data must include 32 HEX symbols");
1662 return 2;
1663 }
1664 for (i = 0; i < 32; i += 2)
1665 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
1666
1667 if (blockNum == 2) flags = 0;
1668 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1669
1670 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {
1671 PrintAndLog("Cant set magic card block: %d", blockNum);
1672 return 3;
1673 }
1674 blockNum++;
1675
1676 if (blockNum >= 16 * 4) break; // magic card type - mifare 1K
1677 }
1678 fclose(f);
1679
1680 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
1681 PrintAndLog("File content error. There must be 64 blocks");
1682 return 4;
1683 }
1684 PrintAndLog("Loaded from file: %s", filename);
1685 return 0;
1686 }
1687 }
1688
1689 int CmdHF14AMfCGetBlk(const char *Cmd) {
1690 uint8_t memBlock[16];
1691 uint8_t blockNo = 0;
1692 int res;
1693 memset(memBlock, 0x00, sizeof(memBlock));
1694
1695 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1696 PrintAndLog("Usage: hf mf cgetblk <block number>");
1697 PrintAndLog("sample: hf mf cgetblk 1");
1698 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");
1699 return 0;
1700 }
1701
1702 blockNo = param_get8(Cmd, 0);
1703
1704 PrintAndLog("--block number:%02x ", blockNo);
1705
1706 res = mfCGetBlock(blockNo, memBlock, CSETBLOCK_SINGLE_OPER);
1707 if (res) {
1708 PrintAndLog("Can't read block. error=%d", res);
1709 return 1;
1710 }
1711
1712 PrintAndLog("block data:%s", sprint_hex(memBlock, 16));
1713 return 0;
1714 }
1715
1716 int CmdHF14AMfCGetSc(const char *Cmd) {
1717 uint8_t memBlock[16];
1718 uint8_t sectorNo = 0;
1719 int i, res, flags;
1720 memset(memBlock, 0x00, sizeof(memBlock));
1721
1722 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1723 PrintAndLog("Usage: hf mf cgetsc <sector number>");
1724 PrintAndLog("sample: hf mf cgetsc 0");
1725 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");
1726 return 0;
1727 }
1728
1729 sectorNo = param_get8(Cmd, 0);
1730 if (sectorNo > 15) {
1731 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
1732 return 1;
1733 }
1734
1735 PrintAndLog("--sector number:%02x ", sectorNo);
1736
1737 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1738 for (i = 0; i < 4; i++) {
1739 if (i == 1) flags = 0;
1740 if (i == 3) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1741
1742 res = mfCGetBlock(sectorNo * 4 + i, memBlock, flags);
1743 if (res) {
1744 PrintAndLog("Can't read block. %02x error=%d", sectorNo * 4 + i, res);
1745 return 1;
1746 }
1747
1748 PrintAndLog("block %02x data:%s", sectorNo * 4 + i, sprint_hex(memBlock, 16));
1749 }
1750 return 0;
1751 }
1752
1753 int CmdHF14AMfCSave(const char *Cmd) {
1754
1755 FILE * f;
1756 char filename[20];
1757 char * fnameptr = filename;
1758 uint8_t fillFromEmulator = 0;
1759 uint8_t buf[64];
1760 int i, j, len, flags;
1761
1762 memset(filename, 0, sizeof(filename));
1763 memset(buf, 0, sizeof(buf));
1764
1765 if (param_getchar(Cmd, 0) == 'h') {
1766 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
1767 PrintAndLog("or into emulator memory (option `e`)");
1768 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
1769 PrintAndLog(" sample: hf mf esave ");
1770 PrintAndLog(" hf mf esave filename");
1771 PrintAndLog(" hf mf esave e \n");
1772 return 0;
1773 }
1774
1775 char ctmp = param_getchar(Cmd, 0);
1776 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
1777
1778 if (fillFromEmulator) {
1779 // put into emulator
1780 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1781 for (i = 0; i < 16 * 4; i++) {
1782 if (i == 1) flags = 0;
1783 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1784
1785 if (mfCGetBlock(i, buf, flags)) {
1786 PrintAndLog("Cant get block: %d", i);
1787 break;
1788 }
1789
1790 if (mfEmlSetMem(buf, i, 1)) {
1791 PrintAndLog("Cant set emul block: %d", i);
1792 return 3;
1793 }
1794 }
1795 return 0;
1796 } else {
1797 len = strlen(Cmd);
1798 if (len > 14) len = 14;
1799
1800 if (len < 1) {
1801 // get filename
1802 if (mfCGetBlock(0, buf, CSETBLOCK_SINGLE_OPER)) {
1803 PrintAndLog("Cant get block: %d", 0);
1804 return 1;
1805 }
1806 for (j = 0; j < 7; j++, fnameptr += 2)
1807 sprintf(fnameptr, "%02x", buf[j]);
1808 } else {
1809 memcpy(filename, Cmd, len);
1810 fnameptr += len;
1811 }
1812
1813 sprintf(fnameptr, ".eml");
1814
1815 // open file
1816 f = fopen(filename, "w+");
1817
1818 // put hex
1819 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;
1820 for (i = 0; i < 16 * 4; i++) {
1821 if (i == 1) flags = 0;
1822 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;
1823
1824 if (mfCGetBlock(i, buf, flags)) {
1825 PrintAndLog("Cant get block: %d", i);
1826 break;
1827 }
1828 for (j = 0; j < 16; j++)
1829 fprintf(f, "%02x", buf[j]);
1830 fprintf(f,"\n");
1831 }
1832 fclose(f);
1833
1834 PrintAndLog("Saved to file: %s", filename);
1835
1836 return 0;
1837 }
1838 }
1839
1840 int CmdHF14AMfSniff(const char *Cmd){
1841 // params
1842 bool wantLogToFile = 0;
1843 bool wantDecrypt = 0;
1844 //bool wantSaveToEml = 0; TODO
1845 bool wantSaveToEmlFile = 0;
1846
1847 //var
1848 int res = 0;
1849 int len = 0;
1850 int blockLen = 0;
1851 int num = 0;
1852 int pckNum = 0;
1853 uint8_t uid[7];
1854 uint8_t uid_len;
1855 uint8_t atqa[2];
1856 uint8_t sak;
1857 bool isTag;
1858 uint32_t parity;
1859 uint8_t buf[3000];
1860 uint8_t * bufPtr = buf;
1861 memset(buf, 0x00, 3000);
1862
1863 if (param_getchar(Cmd, 0) == 'h') {
1864 PrintAndLog("It continuously get data from the field and saves it to: log, emulator, emulator file.");
1865 PrintAndLog("You can specify:");
1866 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");
1867 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");
1868 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
1869 PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
1870 PrintAndLog("Usage: hf mf sniff [l][d][e][r]");
1871 PrintAndLog(" sample: hf mf sniff l d e");
1872 return 0;
1873 }
1874
1875 for (int i = 0; i < 4; i++) {
1876 char ctmp = param_getchar(Cmd, i);
1877 if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;
1878 if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;
1879 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1880 if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;
1881 }
1882
1883 printf("-------------------------------------------------------------------------\n");
1884 printf("Executing command. \n");
1885 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1886 printf("Press the key on pc keyboard to abort the client.\n");
1887 printf("-------------------------------------------------------------------------\n");
1888
1889 UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};
1890 clearCommandBuffer();
1891 SendCommand(&c);
1892
1893 // wait cycle
1894 while (true) {
1895 printf(".");
1896 fflush(stdout);
1897 if (ukbhit()) {
1898 getchar();
1899 printf("\naborted via keyboard!\n");
1900 break;
1901 }
1902
1903 UsbCommand resp;
1904 if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {
1905 res = resp.arg[0] & 0xff;
1906 len = resp.arg[1];
1907 num = resp.arg[2];
1908
1909 if (res == 0) return 0;
1910 if (res == 1) {
1911 if (num ==0) {
1912 bufPtr = buf;
1913 memset(buf, 0x00, 3000);
1914 }
1915 memcpy(bufPtr, resp.d.asBytes, len);
1916 bufPtr += len;
1917 pckNum++;
1918 }
1919 if (res == 2) {
1920 blockLen = bufPtr - buf;
1921 bufPtr = buf;
1922 printf(">\n");
1923 PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);
1924 num = 0;
1925 while (bufPtr - buf + 9 < blockLen) {
1926 isTag = bufPtr[3] & 0x80 ? true:false;
1927 bufPtr += 4;
1928 parity = *((uint32_t *)(bufPtr));
1929 bufPtr += 4;
1930 len = bufPtr[0];
1931 bufPtr++;
1932 if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff)) {
1933 memcpy(uid, bufPtr + 2, 7);
1934 memcpy(atqa, bufPtr + 2 + 7, 2);
1935 uid_len = (atqa[0] & 0xC0) == 0x40 ? 7 : 4;
1936 sak = bufPtr[11];
1937
1938 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",
1939 sprint_hex(uid + (7 - uid_len), uid_len),
1940 atqa[1],
1941 atqa[0],
1942 sak);
1943 if (wantLogToFile || wantDecrypt) {
1944 FillFileNameByUID(logHexFileName, uid + (7 - uid_len), ".log", uid_len);
1945 AddLogCurrentDT(logHexFileName);
1946 }
1947 if (wantDecrypt) mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);
1948 } else {
1949 PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));
1950 if (wantLogToFile) AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);
1951 if (wantDecrypt) mfTraceDecode(bufPtr, len, parity, wantSaveToEmlFile);
1952 }
1953 bufPtr += len;
1954 num++;
1955 }
1956 }
1957 } // resp not NILL
1958 } // while (true)
1959
1960 return 0;
1961 }
1962
1963 static command_t CommandTable[] =
1964 {
1965 {"help", CmdHelp, 1, "This help"},
1966 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},
1967 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},
1968 {"urdbl", CmdHF14AMfURdBl, 0, "Read MIFARE Ultralight block"},
1969 {"urdcard", CmdHF14AMfURdCard, 0,"Read MIFARE Ultralight Card"},
1970 {"uwrbl", CmdHF14AMfUWrBl, 0,"Write MIFARE Ultralight block"},
1971 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},
1972 {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},
1973 {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},
1974 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},
1975 {"chk", CmdHF14AMfChk, 0, "Test block keys"},
1976 {"mifare", CmdHF14AMifare, 0, "Read parity error messages."},
1977 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},
1978 {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},
1979 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},
1980 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},
1981 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},
1982 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},
1983 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},
1984 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},
1985 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},
1986 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},
1987 {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},
1988 {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block into magic Chinese card"},
1989 {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block from magic Chinese card"},
1990 {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector from magic Chinese card"},
1991 {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},
1992 {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},
1993 {NULL, NULL, 0, NULL}
1994 };
1995
1996 int CmdHFMF(const char *Cmd)
1997 {
1998 // flush
1999 WaitForResponseTimeout(CMD_ACK,NULL,100);
2000
2001 CmdsParse(CommandTable, Cmd);
2002 return 0;
2003 }
2004
2005 int CmdHelp(const char *Cmd)
2006 {
2007 CmdsHelp(CommandTable);
2008 return 0;
2009 }
Impressum, Datenschutz