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