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