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