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