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