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