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