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