]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmf.c
hf 14a: Fix typos in manufacturer list
[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 int usage_hf14_mifare(void){
15 PrintAndLog("Usage: hf mf mifare [h] <block number> <A|B>");
16 PrintAndLog("options:");
17 PrintAndLog(" h this help");
18 PrintAndLog(" <block number> (Optional) target other block");
19 PrintAndLog(" <A|B> (optional) target key type");
20 PrintAndLog("samples:");
21 PrintAndLog(" hf mf mifare");
22 PrintAndLog(" hf mf mifare 16");
23 PrintAndLog(" hf mf mifare 16 B");
24 return 0;
25 }
26 int usage_hf14_mf1ksim(void){
27 PrintAndLog("Usage: hf mf sim [h] u <uid (8,14,20 hex symbols)> n <numreads> i x");
28 PrintAndLog("options:");
29 PrintAndLog(" h this help");
30 PrintAndLog(" u (Optional) UID 4,7 or 10bytes. If not specified, the UID 4b from emulator memory will be used");
31 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");
32 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");
33 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");
34 PrintAndLog("samples:");
35 PrintAndLog(" hf mf sim u 0a0a0a0a");
36 PrintAndLog(" hf mf sim u 11223344556677");
37 PrintAndLog(" hf mf sim u 112233445566778899AA");
38 return 0;
39 }
40 int usage_hf14_dbg(void){
41 PrintAndLog("Usage: hf mf dbg [h] <debug level>");
42 PrintAndLog("options:");
43 PrintAndLog(" h this help");
44 PrintAndLog(" <debug level> (Optional) see list for valid levels");
45 PrintAndLog(" 0 - no debug messages");
46 PrintAndLog(" 1 - error messages");
47 PrintAndLog(" 2 - plus information messages");
48 PrintAndLog(" 3 - plus debug messages");
49 PrintAndLog(" 4 - print even debug messages in timing critical functions");
50 PrintAndLog(" Note: this option therefore may cause malfunction itself");
51 PrintAndLog("samples:");
52 PrintAndLog(" hf mf dbg 3");
53 return 0;
54 }
55 int usage_hf14_sniff(void){
56 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");
57 PrintAndLog("Usage: hf mf sniff [h] [l] [d] [f]");
58 PrintAndLog("options:");
59 PrintAndLog(" h this help");
60 PrintAndLog(" l save encrypted sequence to logfile `uid.log`");
61 PrintAndLog(" d decrypt sequence and put it to log file `uid.log`");
62 // PrintAndLog(" n/a e decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");
63 PrintAndLog(" f decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");
64 PrintAndLog("sample:");
65 PrintAndLog(" hf mf sniff l d f");
66 return 0;
67 }
68 int usage_hf14_nested(void){
69 PrintAndLog("Usage:");
70 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");
71 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");
72 PrintAndLog(" <target block number> <target key A/B> [t]");
73 PrintAndLog("options:");
74 PrintAndLog(" h this help");
75 PrintAndLog(" card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
76 PrintAndLog(" t transfer keys into emulator memory");
77 PrintAndLog(" d write keys to binary file");
78 PrintAndLog(" ");
79 PrintAndLog("samples:");
80 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF ");
81 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF t ");
82 PrintAndLog(" hf mf nested 1 0 A FFFFFFFFFFFF d ");
83 PrintAndLog(" hf mf nested o 0 A FFFFFFFFFFFF 4 A");
84 return 0;
85 }
86 int usage_hf14_hardnested(void){
87 PrintAndLog("Usage:");
88 PrintAndLog(" hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>");
89 PrintAndLog(" <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]");
90 PrintAndLog(" or hf mf hardnested r [known target key]");
91 PrintAndLog(" ");
92 PrintAndLog("options:");
93 PrintAndLog(" h this help");
94 PrintAndLog(" w acquire nonces and write them to binary file nonces.bin");
95 PrintAndLog(" s slower acquisition (required by some non standard cards)");
96 PrintAndLog(" r read nonces.bin and start attack");
97 PrintAndLog(" t tests?");
98 PrintAndLog(" ");
99 PrintAndLog("samples:");
100 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A");
101 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");
102 PrintAndLog(" hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");
103 PrintAndLog(" hf mf hardnested r");
104 PrintAndLog(" hf mf hardnested r a0a1a2a3a4a5");
105 PrintAndLog(" ");
106 PrintAndLog("Add the known target key to check if it is present in the remaining key space:");
107 PrintAndLog(" sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF");
108 return 0;
109 }
110 int usage_hf14_chk(void){
111 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
112 PrintAndLog("options:");
113 PrintAndLog(" h this help");
114 PrintAndLog(" * all sectors based on card memory, other values then below defaults to 1k");
115 PrintAndLog(" 0 - MINI(320 bytes)");
116 PrintAndLog(" 1 - 1K");
117 PrintAndLog(" 2 - 2K");
118 PrintAndLog(" 4 - 4K");
119 PrintAndLog(" d write keys to binary file");
120 PrintAndLog(" t write keys to emulator memory\n");
121 PrintAndLog(" ");
122 PrintAndLog("samples:");
123 PrintAndLog(" hf mf chk 0 A 1234567890ab keys.dic -- target block 0, Key A");
124 PrintAndLog(" hf mf chk *1 ? t -- target all blocks, all keys, 1K, write to emul");
125 PrintAndLog(" hf mf chk *1 ? d -- target all blocks, all keys, 1K, write to file");
126 return 0;
127 }
128 int usage_hf14_keybrute(void){
129 PrintAndLog("J_Run's 2nd phase of multiple sector nested authentication key recovery");
130 PrintAndLog("You have a known 4 last bytes of a key recovered with mf_nonce_brute tool.");
131 PrintAndLog("First 2 bytes of key will be bruteforced");
132 PrintAndLog("");
133 PrintAndLog("Usage: hf mf keybrute [h] <block number> <A|B> <key>");
134 PrintAndLog("options:");
135 PrintAndLog(" h this help");
136 PrintAndLog(" <block number> target block number");
137 PrintAndLog(" <A|B> target key type");
138 PrintAndLog(" <key> candidate key from mf_nonce_brute tool");
139 PrintAndLog("samples:");
140 PrintAndLog(" hf mf keybrute 1 A 000011223344");
141 return 0;
142 }
143
144 int CmdHF14AMifare(const char *Cmd) {
145 uint32_t uid = 0;
146 uint32_t nt = 0, nr = 0;
147 uint64_t par_list = 0, ks_list = 0, r_key = 0;
148 int16_t isOK = 0;
149 int tmpchar;
150 uint8_t blockNo = 0, keytype = MIFARE_AUTH_KEYA;
151
152 char cmdp = param_getchar(Cmd, 0);
153 if ( cmdp == 'H' || cmdp == 'h') return usage_hf14_mifare();
154
155 blockNo = param_get8(Cmd, 0);
156
157 cmdp = param_getchar(Cmd, 1);
158 if (cmdp == 'B' || cmdp == 'b')
159 keytype = MIFARE_AUTH_KEYB;
160
161 UsbCommand c = {CMD_READER_MIFARE, {true, blockNo, keytype}};
162
163 // message
164 printf("-------------------------------------------------------------------------\n");
165 printf("Executing darkside attack. Expected execution time: 25sec on average :-)\n");
166 printf("Press button on the proxmark3 device to abort both proxmark3 and client.\n");
167 printf("-------------------------------------------------------------------------\n");
168 clock_t t1 = clock();
169 time_t start, end;
170 time(&start);
171
172 start:
173 clearCommandBuffer();
174 SendCommand(&c);
175
176 //flush queue
177 while (ukbhit()) {
178 tmpchar = getchar();
179 (void)tmpchar;
180 }
181
182 // wait cycle
183 while (true) {
184 printf(".");
185 fflush(stdout);
186 if (ukbhit()) {
187 tmpchar = getchar();
188 (void)tmpchar;
189 printf("\naborted via keyboard!\n");
190 break;
191 }
192
193 UsbCommand resp;
194 if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
195 isOK = resp.arg[0];
196 printf("\n");
197 uid = (uint32_t)bytes_to_num(resp.d.asBytes + 0, 4);
198 nt = (uint32_t)bytes_to_num(resp.d.asBytes + 4, 4);
199 par_list = bytes_to_num(resp.d.asBytes + 8, 8);
200 ks_list = bytes_to_num(resp.d.asBytes + 16, 8);
201 nr = bytes_to_num(resp.d.asBytes + 24, 4);
202
203 switch (isOK) {
204 case -1 : PrintAndLog("Button pressed. Aborted.\n"); break;
205 case -2 : PrintAndLog("Card isn't vulnerable to Darkside attack (doesn't send NACK on authentication requests).\n"); break;
206 case -3 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator is not predictable).\n"); break;
207 case -4 : PrintAndLog("Card isn't vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");
208 PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour.\n"); break;
209 default: ;
210 }
211 break;
212 }
213 }
214 printf("\n");
215
216 // par == 0, and -4
217 if (isOK == -4 && par_list == 0) {
218 // this special attack when parities is zero, uses checkkeys. Which now with block/keytype option also needs.
219 // but it uses 0|1 instead of 0x60|0x61...
220 if (nonce2key_ex(blockNo, keytype - 0x60 , uid, nt, nr, ks_list, &r_key) ){
221 PrintAndLog("Key not found (lfsr_common_prefix list is null).");
222 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
223 c.arg[0] = false;
224 goto start;
225 } else {
226 PrintAndLog("Found valid key: %012"llx" \n", r_key);
227 goto END;
228 }
229 }
230
231 // error
232 if (isOK != 1) return 1;
233
234 // execute original function from util nonce2key
235 if (nonce2key(uid, nt, nr, par_list, ks_list, &r_key)) {
236 isOK = 2;
237 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt);
238 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");
239 c.arg[0] = false;
240 goto start;
241 } else {
242
243 // nonce2key found a candidate key. Lets verify it.
244 uint8_t keyblock[] = {0,0,0,0,0,0};
245 num_to_bytes(r_key, 6, keyblock);
246 uint64_t key64 = 0;
247 int res = mfCheckKeys(blockNo, keytype - 0x60 , false, 1, keyblock, &key64);
248 if ( res > 0 ) {
249 PrintAndLog("Candidate Key found (%012"llx") - Test authentication failed. Starting over darkside attack", r_key);
250 goto start;
251 }
252 PrintAndLog("Found valid key: %012"llx" \n", r_key);
253 }
254 END:
255 t1 = clock() - t1;
256 time(&end);
257 unsigned long elapsed_time = difftime(end, start);
258 if ( t1 > 0 )
259 PrintAndLog("Time in darkside: %.0f ticks %u seconds\n", (float)t1, elapsed_time);
260 return 0;
261 }
262
263 int CmdHF14AMfWrBl(const char *Cmd) {
264 uint8_t blockNo = 0;
265 uint8_t keyType = 0;
266 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
267 uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
268
269 char cmdp = 0x00;
270
271 if (strlen(Cmd)<3) {
272 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");
273 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");
274 return 0;
275 }
276
277 blockNo = param_get8(Cmd, 0);
278 cmdp = param_getchar(Cmd, 1);
279 if (cmdp == 0x00) {
280 PrintAndLog("Key type must be A or B");
281 return 1;
282 }
283 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
284 if (param_gethex(Cmd, 2, key, 12)) {
285 PrintAndLog("Key must include 12 HEX symbols");
286 return 1;
287 }
288 if (param_gethex(Cmd, 3, bldata, 32)) {
289 PrintAndLog("Block data must include 32 HEX symbols");
290 return 1;
291 }
292 PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo, keyType?'B':'A', sprint_hex(key, 6));
293 PrintAndLog("--data: %s", sprint_hex(bldata, 16));
294
295 UsbCommand c = {CMD_MIFARE_WRITEBL, {blockNo, keyType, 0}};
296 memcpy(c.d.asBytes, key, 6);
297 memcpy(c.d.asBytes + 10, bldata, 16);
298 clearCommandBuffer();
299 SendCommand(&c);
300
301 UsbCommand resp;
302 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
303 uint8_t isOK = resp.arg[0] & 0xff;
304 PrintAndLog("isOk:%02x", isOK);
305 } else {
306 PrintAndLog("Command execute timeout");
307 }
308
309 return 0;
310 }
311
312 int CmdHF14AMfRdBl(const char *Cmd) {
313 uint8_t blockNo = 0;
314 uint8_t keyType = 0;
315 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
316
317 char cmdp = 0x00;
318
319
320 if (strlen(Cmd)<3) {
321 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");
322 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");
323 return 0;
324 }
325
326 blockNo = param_get8(Cmd, 0);
327 cmdp = param_getchar(Cmd, 1);
328 if (cmdp == 0x00) {
329 PrintAndLog("Key type must be A or B");
330 return 1;
331 }
332 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
333 if (param_gethex(Cmd, 2, key, 12)) {
334 PrintAndLog("Key must include 12 HEX symbols");
335 return 1;
336 }
337 PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo, keyType?'B':'A', sprint_hex(key, 6));
338
339 UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};
340 memcpy(c.d.asBytes, key, 6);
341 clearCommandBuffer();
342 SendCommand(&c);
343
344 UsbCommand resp;
345 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
346 uint8_t isOK = resp.arg[0] & 0xff;
347 uint8_t *data = resp.d.asBytes;
348
349 if (isOK)
350 PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 16));
351 else
352 PrintAndLog("isOk:%02x", isOK);
353 } else {
354 PrintAndLog("Command execute timeout");
355 }
356
357 return 0;
358 }
359
360 int CmdHF14AMfRdSc(const char *Cmd) {
361 int i;
362 uint8_t sectorNo = 0;
363 uint8_t keyType = 0;
364 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
365 uint8_t isOK = 0;
366 uint8_t *data = NULL;
367 char cmdp = 0x00;
368
369 if (strlen(Cmd)<3) {
370 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");
371 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");
372 return 0;
373 }
374
375 sectorNo = param_get8(Cmd, 0);
376 if (sectorNo > 39) {
377 PrintAndLog("Sector number must be less than 40");
378 return 1;
379 }
380 cmdp = param_getchar(Cmd, 1);
381 if (cmdp != 'a' && cmdp != 'A' && cmdp != 'b' && cmdp != 'B') {
382 PrintAndLog("Key type must be A or B");
383 return 1;
384 }
385 if (cmdp != 'A' && cmdp != 'a') keyType = 1;
386 if (param_gethex(Cmd, 2, key, 12)) {
387 PrintAndLog("Key must include 12 HEX symbols");
388 return 1;
389 }
390 PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo, keyType?'B':'A', sprint_hex(key, 6));
391
392 UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};
393 memcpy(c.d.asBytes, key, 6);
394 clearCommandBuffer();
395 SendCommand(&c);
396 PrintAndLog(" ");
397
398 UsbCommand resp;
399 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
400 isOK = resp.arg[0] & 0xff;
401 data = resp.d.asBytes;
402
403 PrintAndLog("isOk:%02x", isOK);
404 if (isOK) {
405 for (i = 0; i < (sectorNo<32?3:15); i++) {
406 PrintAndLog("data : %s", sprint_hex(data + i * 16, 16));
407 }
408 PrintAndLog("trailer: %s", sprint_hex(data + (sectorNo<32?3:15) * 16, 16));
409 }
410 } else {
411 PrintAndLog("Command execute timeout");
412 }
413
414 return 0;
415 }
416
417 uint8_t FirstBlockOfSector(uint8_t sectorNo) {
418 if (sectorNo < 32) {
419 return sectorNo * 4;
420 } else {
421 return 32 * 4 + (sectorNo - 32) * 16;
422 }
423 }
424
425 uint8_t NumBlocksPerSector(uint8_t sectorNo) {
426 if (sectorNo < 32) {
427 return 4;
428 } else {
429 return 16;
430 }
431 }
432
433 int CmdHF14AMfDump(const char *Cmd) {
434 uint8_t sectorNo, blockNo;
435
436 uint8_t keyA[40][6];
437 uint8_t keyB[40][6];
438 uint8_t rights[40][4];
439 uint8_t carddata[256][16];
440 uint8_t numSectors = 16;
441
442 FILE *fin;
443 FILE *fout;
444
445 UsbCommand resp;
446
447 char cmdp = param_getchar(Cmd, 0);
448 switch (cmdp) {
449 case '0' : numSectors = 5; break;
450 case '1' :
451 case '\0': numSectors = 16; break;
452 case '2' : numSectors = 32; break;
453 case '4' : numSectors = 40; break;
454 default: numSectors = 16;
455 }
456
457 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {
458 PrintAndLog("Usage: hf mf dump [card memory]");
459 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
460 PrintAndLog("");
461 PrintAndLog("Samples: hf mf dump");
462 PrintAndLog(" hf mf dump 4");
463 return 0;
464 }
465
466 if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {
467 PrintAndLog("Could not find file dumpkeys.bin");
468 return 1;
469 }
470
471 // Read keys A from file
472 size_t bytes_read;
473 for (sectorNo=0; sectorNo<numSectors; sectorNo++) {
474 bytes_read = fread( keyA[sectorNo], 1, 6, fin );
475 if ( bytes_read == 0) {
476 PrintAndLog("File reading error.");
477 fclose(fin);
478 fin = NULL;
479 return 2;
480 }
481 }
482
483 // Read keys B from file
484 for (sectorNo=0; sectorNo<numSectors; sectorNo++) {
485 bytes_read = fread( keyB[sectorNo], 1, 6, fin );
486 if ( bytes_read == 0) {
487 PrintAndLog("File reading error.");
488 fclose(fin);
489 fin = NULL;
490 return 2;
491 }
492 }
493
494 fclose(fin);
495 fin = NULL;
496
497 PrintAndLog("|-----------------------------------------|");
498 PrintAndLog("|------ Reading sector access bits...-----|");
499 PrintAndLog("|-----------------------------------------|");
500
501 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
502 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 0, 0}};
503 memcpy(c.d.asBytes, keyA[sectorNo], 6);
504 clearCommandBuffer();
505 SendCommand(&c);
506
507 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
508 uint8_t isOK = resp.arg[0] & 0xff;
509 uint8_t *data = resp.d.asBytes;
510 if (isOK){
511 rights[sectorNo][0] = ((data[7] & 0x10)>>2) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>4); // C1C2C3 for data area 0
512 rights[sectorNo][1] = ((data[7] & 0x20)>>3) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>5); // C1C2C3 for data area 1
513 rights[sectorNo][2] = ((data[7] & 0x40)>>4) | ((data[8] & 0x4)>>1) | ((data[8] & 0x40)>>6); // C1C2C3 for data area 2
514 rights[sectorNo][3] = ((data[7] & 0x80)>>5) | ((data[8] & 0x8)>>2) | ((data[8] & 0x80)>>7); // C1C2C3 for sector trailer
515 } else {
516 PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo);
517 rights[sectorNo][0] = rights[sectorNo][1] = rights[sectorNo][2] = 0x00;
518 rights[sectorNo][3] = 0x01;
519 }
520 } else {
521 PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo);
522 rights[sectorNo][0] = rights[sectorNo][1] = rights[sectorNo][2] = 0x00;
523 rights[sectorNo][3] = 0x01;
524 }
525 }
526
527 PrintAndLog("|-----------------------------------------|");
528 PrintAndLog("|----- Dumping all blocks to file... -----|");
529 PrintAndLog("|-----------------------------------------|");
530
531 bool isOK = true;
532 for (sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {
533 for (blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
534 bool received = false;
535
536 if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.
537 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 0, 0}};
538 memcpy(c.d.asBytes, keyA[sectorNo], 6);
539 clearCommandBuffer();
540 SendCommand(&c);
541 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);
542 } else { // data block. Check if it can be read with key A or key B
543 uint8_t data_area = sectorNo<32?blockNo:blockNo/5;
544 if ((rights[sectorNo][data_area] == 0x03) || (rights[sectorNo][data_area] == 0x05)) { // only key B would work
545 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 1, 0}};
546 memcpy(c.d.asBytes, keyB[sectorNo], 6);
547 SendCommand(&c);
548 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);
549 } else if (rights[sectorNo][data_area] == 0x07) { // no key would work
550 isOK = false;
551 PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo, blockNo);
552 } else { // key A would work
553 UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 0, 0}};
554 memcpy(c.d.asBytes, keyA[sectorNo], 6);
555 clearCommandBuffer();
556 SendCommand(&c);
557 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);
558 }
559 }
560
561 if (received) {
562 isOK = resp.arg[0] & 0xff;
563 uint8_t *data = resp.d.asBytes;
564 if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer. Fill in the keys.
565 data[0] = (keyA[sectorNo][0]);
566 data[1] = (keyA[sectorNo][1]);
567 data[2] = (keyA[sectorNo][2]);
568 data[3] = (keyA[sectorNo][3]);
569 data[4] = (keyA[sectorNo][4]);
570 data[5] = (keyA[sectorNo][5]);
571 data[10] = (keyB[sectorNo][0]);
572 data[11] = (keyB[sectorNo][1]);
573 data[12] = (keyB[sectorNo][2]);
574 data[13] = (keyB[sectorNo][3]);
575 data[14] = (keyB[sectorNo][4]);
576 data[15] = (keyB[sectorNo][5]);
577 }
578 if (isOK) {
579 memcpy(carddata[FirstBlockOfSector(sectorNo) + blockNo], data, 16);
580 PrintAndLog("Successfully read block %2d of sector %2d.", blockNo, sectorNo);
581 } else {
582 PrintAndLog("Could not read block %2d of sector %2d", blockNo, sectorNo);
583 break;
584 }
585 }
586 else {
587 isOK = false;
588 PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo, sectorNo);
589 break;
590 }
591 }
592 }
593
594 if (isOK) {
595 if ((fout = fopen("dumpdata.bin","wb")) == NULL) {
596 PrintAndLog("Could not create file name dumpdata.bin");
597 return 1;
598 }
599 uint16_t numblocks = FirstBlockOfSector(numSectors - 1) + NumBlocksPerSector(numSectors - 1);
600 fwrite(carddata, 1, 16*numblocks, fout);
601 fclose(fout);
602 fout = NULL;
603 PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks, 16*numblocks);
604 }
605
606 return 0;
607 }
608
609 int CmdHF14AMfRestore(const char *Cmd) {
610 uint8_t sectorNo,blockNo;
611 uint8_t keyType = 0;
612 uint8_t key[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
613 uint8_t bldata[16] = {0x00};
614 uint8_t keyA[40][6];
615 uint8_t keyB[40][6];
616 uint8_t numSectors;
617
618 FILE *fdump;
619 FILE *fkeys;
620
621 char cmdp = param_getchar(Cmd, 0);
622 switch (cmdp) {
623 case '0' : numSectors = 5; break;
624 case '1' :
625 case '\0': numSectors = 16; break;
626 case '2' : numSectors = 32; break;
627 case '4' : numSectors = 40; break;
628 default: numSectors = 16;
629 }
630
631 if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {
632 PrintAndLog("Usage: hf mf restore [card memory]");
633 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
634 PrintAndLog("");
635 PrintAndLog("Samples: hf mf restore");
636 PrintAndLog(" hf mf restore 4");
637 return 0;
638 }
639
640 if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) {
641 PrintAndLog("Could not find file dumpkeys.bin");
642 return 1;
643 }
644
645 size_t bytes_read;
646 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
647 bytes_read = fread( keyA[sectorNo], 1, 6, fkeys );
648 if ( bytes_read == 0) {
649 PrintAndLog("File reading error (dumpkeys.bin).");
650 fclose(fkeys);
651 fkeys = NULL;
652 return 2;
653 }
654 }
655
656 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
657 bytes_read = fread( keyB[sectorNo], 1, 6, fkeys );
658 if ( bytes_read == 0) {
659 PrintAndLog("File reading error (dumpkeys.bin).");
660 fclose(fkeys);
661 fkeys = NULL;
662 return 2;
663 }
664 }
665
666 fclose(fkeys);
667
668 if ((fdump = fopen("dumpdata.bin","rb")) == NULL) {
669 PrintAndLog("Could not find file dumpdata.bin");
670 return 1;
671 }
672 PrintAndLog("Restoring dumpdata.bin to card");
673
674 for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {
675 for(blockNo = 0; blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
676 UsbCommand c = {CMD_MIFARE_WRITEBL, {FirstBlockOfSector(sectorNo) + blockNo, keyType, 0}};
677 memcpy(c.d.asBytes, key, 6);
678 bytes_read = fread(bldata, 1, 16, fdump);
679 if ( bytes_read == 0) {
680 PrintAndLog("File reading error (dumpdata.bin).");
681 fclose(fdump);
682 fdump = NULL;
683 return 2;
684 }
685
686 if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer
687 bldata[0] = (keyA[sectorNo][0]);
688 bldata[1] = (keyA[sectorNo][1]);
689 bldata[2] = (keyA[sectorNo][2]);
690 bldata[3] = (keyA[sectorNo][3]);
691 bldata[4] = (keyA[sectorNo][4]);
692 bldata[5] = (keyA[sectorNo][5]);
693 bldata[10] = (keyB[sectorNo][0]);
694 bldata[11] = (keyB[sectorNo][1]);
695 bldata[12] = (keyB[sectorNo][2]);
696 bldata[13] = (keyB[sectorNo][3]);
697 bldata[14] = (keyB[sectorNo][4]);
698 bldata[15] = (keyB[sectorNo][5]);
699 }
700
701 PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo) + blockNo, sprint_hex(bldata, 16));
702
703 memcpy(c.d.asBytes + 10, bldata, 16);
704 clearCommandBuffer();
705 SendCommand(&c);
706
707 UsbCommand resp;
708 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
709 uint8_t isOK = resp.arg[0] & 0xff;
710 PrintAndLog("isOk:%02x", isOK);
711 } else {
712 PrintAndLog("Command execute timeout");
713 }
714 }
715 }
716
717 fclose(fdump);
718 fdump = NULL;
719 return 0;
720 }
721
722 int CmdHF14AMfNested(const char *Cmd) {
723 int i, j, res, iterations;
724 sector *e_sector = NULL;
725 uint8_t blockNo = 0;
726 uint8_t keyType = 0;
727 uint8_t trgBlockNo = 0;
728 uint8_t trgKeyType = 0;
729 uint8_t SectorsCnt = 0;
730 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
731 uint8_t keyBlock[6*6];
732 uint64_t key64 = 0;
733 bool transferToEml = false;
734
735 bool createDumpFile = false;
736 FILE *fkeys;
737 uint8_t standart[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
738 uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
739
740 if (strlen(Cmd)<3) return usage_hf14_nested();
741
742 char cmdp, ctmp;
743 cmdp = param_getchar(Cmd, 0);
744 blockNo = param_get8(Cmd, 1);
745 ctmp = param_getchar(Cmd, 2);
746
747 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
748 PrintAndLog("Key type must be A or B");
749 return 1;
750 }
751
752 if (ctmp != 'A' && ctmp != 'a')
753 keyType = 1;
754
755 if (param_gethex(Cmd, 3, key, 12)) {
756 PrintAndLog("Key must include 12 HEX symbols");
757 return 1;
758 }
759
760 if (cmdp == 'o' || cmdp == 'O') {
761 cmdp = 'o';
762 trgBlockNo = param_get8(Cmd, 4);
763 ctmp = param_getchar(Cmd, 5);
764 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
765 PrintAndLog("Target key type must be A or B");
766 return 1;
767 }
768 if (ctmp != 'A' && ctmp != 'a')
769 trgKeyType = 1;
770 } else {
771
772 switch (cmdp) {
773 case '0': SectorsCnt = 05; break;
774 case '1': SectorsCnt = 16; break;
775 case '2': SectorsCnt = 32; break;
776 case '4': SectorsCnt = 40; break;
777 default: SectorsCnt = 16;
778 }
779 }
780
781 ctmp = param_getchar(Cmd, 4);
782 if (ctmp == 't' || ctmp == 'T') transferToEml = true;
783 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = true;
784
785 ctmp = param_getchar(Cmd, 6);
786 transferToEml |= (ctmp == 't' || ctmp == 'T');
787 transferToEml |= (ctmp == 'd' || ctmp == 'D');
788
789 if (cmdp == 'o') {
790 int16_t isOK = mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, true);
791 switch (isOK) {
792 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
793 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
794 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
795 case -4 : PrintAndLog("No valid key found"); break;
796 case -5 :
797 key64 = bytes_to_num(keyBlock, 6);
798
799 // transfer key to the emulator
800 if (transferToEml) {
801 uint8_t sectortrailer;
802 if (trgBlockNo < 32*4) { // 4 block sector
803 sectortrailer = (trgBlockNo & 0x03) + 3;
804 } else { // 16 block sector
805 sectortrailer = (trgBlockNo & 0x0f) + 15;
806 }
807 mfEmlGetMem(keyBlock, sectortrailer, 1);
808
809 if (!trgKeyType)
810 num_to_bytes(key64, 6, keyBlock);
811 else
812 num_to_bytes(key64, 6, &keyBlock[10]);
813 mfEmlSetMem(keyBlock, sectortrailer, 1);
814 }
815 return 0;
816 default : PrintAndLog("Unknown Error.\n");
817 }
818 return 2;
819 }
820 else { // ------------------------------------ multiple sectors working
821 clock_t t1 = clock();
822 unsigned long elapsed_time;
823 time_t start, end;
824 time(&start);
825
826 e_sector = calloc(SectorsCnt, sizeof(sector));
827 if (e_sector == NULL) return 1;
828
829 //test current key and additional standard keys first
830 memcpy(keyBlock, key, 6);
831 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 1 * 6));
832 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 2 * 6));
833 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 3 * 6));
834 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 4 * 6));
835 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));
836
837 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);
838 for (i = 0; i < SectorsCnt; i++) {
839 for (j = 0; j < 2; j++) {
840 if (e_sector[i].foundKey[j]) continue;
841
842 res = mfCheckKeys(FirstBlockOfSector(i), j, true, 6, keyBlock, &key64);
843
844 if (!res) {
845 e_sector[i].Key[j] = key64;
846 e_sector[i].foundKey[j] = TRUE;
847 }
848 }
849 }
850 clock_t t2 = clock() - t1;
851 time(&end);
852 elapsed_time = difftime(end, start);
853 if ( t2 > 0 )
854 PrintAndLog("Time to check 6 known keys: %.0f ticks %u seconds\n", (float)t2 , elapsed_time);
855
856 PrintAndLog("enter nested...");
857
858 // nested sectors
859 iterations = 0;
860 bool calibrate = true;
861
862 for (i = 0; i < NESTED_SECTOR_RETRY; i++) {
863 for (uint8_t sectorNo = 0; sectorNo < SectorsCnt; ++sectorNo) {
864 for (trgKeyType = 0; trgKeyType < 2; ++trgKeyType) {
865
866 if (e_sector[sectorNo].foundKey[trgKeyType]) continue;
867
868 int16_t isOK = mfnested(blockNo, keyType, key, FirstBlockOfSector(sectorNo), trgKeyType, keyBlock, calibrate);
869 switch (isOK) {
870 case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
871 case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;
872 case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (its random number generator is not predictable).\n"); break;
873 case -4 : //key not found
874 calibrate = false;
875 iterations++;
876 continue;
877 case -5 :
878 calibrate = false;
879 iterations++;
880 e_sector[sectorNo].foundKey[trgKeyType] = 1;
881 e_sector[sectorNo].Key[trgKeyType] = bytes_to_num(keyBlock, 6);
882 continue;
883
884 default : PrintAndLog("Unknown Error.\n");
885 }
886 free(e_sector);
887 return 2;
888 }
889 }
890 }
891
892 t1 = clock() - t1;
893 time(&end);
894 elapsed_time = difftime(end, start);
895 if ( t1 > 0 )
896 PrintAndLog("Time in nested: %.0f ticks %u seconds\n", (float)t1, elapsed_time);
897
898
899 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
900 PrintAndLog("trying to read key B...");
901 for (i = 0; i < SectorsCnt; i++) {
902 // KEY A but not KEY B
903 if ( e_sector[i].foundKey[0] && !e_sector[i].foundKey[1] ) {
904
905 uint8_t sectrail = (FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);
906
907 PrintAndLog("Reading block %d", sectrail);
908
909 UsbCommand c = {CMD_MIFARE_READBL, {sectrail, 0, 0}};
910 num_to_bytes(e_sector[i].Key[0], 6, c.d.asBytes); // KEY A
911 clearCommandBuffer();
912 SendCommand(&c);
913
914 UsbCommand resp;
915 if ( !WaitForResponseTimeout(CMD_ACK,&resp,1500)) continue;
916
917 uint8_t isOK = resp.arg[0] & 0xff;
918 if (!isOK) continue;
919
920 uint8_t *data = resp.d.asBytes;
921 key64 = bytes_to_num(data+10, 6);
922 if (key64) {
923 PrintAndLog("Data:%s", sprint_hex(data+10, 6));
924 e_sector[i].foundKey[1] = TRUE;
925 e_sector[i].Key[1] = key64;
926 }
927 }
928 }
929
930
931 //print them
932 printKeyTable( SectorsCnt, e_sector );
933
934 // transfer them to the emulator
935 if (transferToEml) {
936 for (i = 0; i < SectorsCnt; i++) {
937 mfEmlGetMem(keyBlock, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);
938 if (e_sector[i].foundKey[0])
939 num_to_bytes(e_sector[i].Key[0], 6, keyBlock);
940 if (e_sector[i].foundKey[1])
941 num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);
942 mfEmlSetMem(keyBlock, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);
943 }
944 }
945
946 // Create dump file
947 if (createDumpFile) {
948 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {
949 PrintAndLog("Could not create file dumpkeys.bin");
950 free(e_sector);
951 return 1;
952 }
953 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
954 for(i=0; i<SectorsCnt; i++) {
955 if (e_sector[i].foundKey[0]){
956 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
957 fwrite ( tempkey, 1, 6, fkeys );
958 }
959 else{
960 fwrite ( &standart, 1, 6, fkeys );
961 }
962 }
963 for(i=0; i<SectorsCnt; i++) {
964 if (e_sector[i].foundKey[1]){
965 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
966 fwrite ( tempkey, 1, 6, fkeys );
967 }
968 else{
969 fwrite ( &standart, 1, 6, fkeys );
970 }
971 }
972 fclose(fkeys);
973 }
974
975 free(e_sector);
976 }
977 return 0;
978 }
979
980 int CmdHF14AMfNestedHard(const char *Cmd) {
981 uint8_t blockNo = 0;
982 uint8_t keyType = 0;
983 uint8_t trgBlockNo = 0;
984 uint8_t trgKeyType = 0;
985 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
986 uint8_t trgkey[6] = {0, 0, 0, 0, 0, 0};
987
988 char ctmp;
989 ctmp = param_getchar(Cmd, 0);
990 if (ctmp == 'H' || ctmp == 'h' ) return usage_hf14_hardnested();
991 if (ctmp != 'R' && ctmp != 'r' && ctmp != 'T' && ctmp != 't' && strlen(Cmd) < 20) return usage_hf14_hardnested();
992
993 bool know_target_key = false;
994 bool nonce_file_read = false;
995 bool nonce_file_write = false;
996 bool slow = false;
997 int tests = 0;
998
999 if (ctmp == 'R' || ctmp == 'r') {
1000 nonce_file_read = true;
1001 if (!param_gethex(Cmd, 1, trgkey, 12)) {
1002 know_target_key = true;
1003 }
1004 } else if (ctmp == 'T' || ctmp == 't') {
1005 tests = param_get32ex(Cmd, 1, 100, 10);
1006 } else {
1007 blockNo = param_get8(Cmd, 0);
1008 ctmp = param_getchar(Cmd, 1);
1009 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
1010 PrintAndLog("Key type must be A or B");
1011 return 1;
1012 }
1013 if (ctmp != 'A' && ctmp != 'a') {
1014 keyType = 1;
1015 }
1016
1017 if (param_gethex(Cmd, 2, key, 12)) {
1018 PrintAndLog("Key must include 12 HEX symbols");
1019 return 1;
1020 }
1021
1022 trgBlockNo = param_get8(Cmd, 3);
1023 ctmp = param_getchar(Cmd, 4);
1024 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
1025 PrintAndLog("Target key type must be A or B");
1026 return 1;
1027 }
1028 if (ctmp != 'A' && ctmp != 'a') {
1029 trgKeyType = 1;
1030 }
1031
1032 uint16_t i = 5;
1033
1034 if (!param_gethex(Cmd, 5, trgkey, 12)) {
1035 know_target_key = true;
1036 i++;
1037 }
1038
1039 while ((ctmp = param_getchar(Cmd, i))) {
1040 if (ctmp == 's' || ctmp == 'S') {
1041 slow = true;
1042 } else if (ctmp == 'w' || ctmp == 'W') {
1043 nonce_file_write = true;
1044 } else {
1045 PrintAndLog("Possible options are w and/or s");
1046 return 1;
1047 }
1048 i++;
1049 }
1050 }
1051
1052 PrintAndLog("--target block no:%3d, target key type:%c, known target key: 0x%02x%02x%02x%02x%02x%02x%s, file action: %s, Slow: %s, Tests: %d ",
1053 trgBlockNo,
1054 trgKeyType?'B':'A',
1055 trgkey[0], trgkey[1], trgkey[2], trgkey[3], trgkey[4], trgkey[5],
1056 know_target_key ? "" : " (not set)",
1057 nonce_file_write ? "write": nonce_file_read ? "read" : "none",
1058 slow ? "Yes" : "No",
1059 tests);
1060
1061 int16_t isOK = mfnestedhard(blockNo, keyType, key, trgBlockNo, trgKeyType, know_target_key ? trgkey : NULL, nonce_file_read, nonce_file_write, slow, tests);
1062
1063 if (isOK) {
1064 switch (isOK) {
1065 case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;
1066 case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;
1067 default : break;
1068 }
1069 return 2;
1070 }
1071
1072 return 0;
1073 }
1074
1075 int CmdHF14AMfChk(const char *Cmd) {
1076
1077 if (strlen(Cmd)<3) return usage_hf14_chk();
1078
1079 FILE * f;
1080 char filename[FILE_PATH_SIZE]={0};
1081 char buf[13];
1082 uint8_t *keyBlock = NULL, *p;
1083 uint8_t stKeyBlock = 20;
1084
1085 sector *e_sector = NULL;
1086
1087 int i, res;
1088 int keycnt = 0;
1089 char ctmp = 0x00;
1090 uint8_t blockNo = 0;
1091 uint8_t SectorsCnt = 1;
1092 uint8_t keyType = 0;
1093 uint64_t key64 = 0;
1094
1095 uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1096
1097 int transferToEml = 0;
1098 int createDumpFile = 0;
1099
1100 keyBlock = calloc(stKeyBlock, 6);
1101 if (keyBlock == NULL) return 1;
1102
1103 uint64_t defaultKeys[] = {
1104 0xffffffffffff, // Default key (first key used by program if no user defined key)
1105 0x000000000000, // Blank key
1106 0xa0a1a2a3a4a5, // NFCForum MAD key
1107 0xb0b1b2b3b4b5,
1108 0xaabbccddeeff,
1109 0x4d3a99c351dd,
1110 0x1a982c7e459a,
1111 0xd3f7d3f7d3f7,
1112 0x714c5c886e97,
1113 0x587ee5f9350f,
1114 0xa0478cc39091,
1115 0x533cb6c723f6,
1116 0x8fd0a4f256e9
1117 };
1118 int defaultKeysSize = sizeof(defaultKeys) / sizeof(uint64_t);
1119
1120 for (int defaultKeyCounter = 0; defaultKeyCounter < defaultKeysSize; defaultKeyCounter++)
1121 num_to_bytes(defaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));
1122
1123
1124 if (param_getchar(Cmd, 0)=='*') {
1125 blockNo = 3;
1126 switch(param_getchar(Cmd+1, 0)) {
1127 case '0': SectorsCnt = 5; break;
1128 case '1': SectorsCnt = 16; break;
1129 case '2': SectorsCnt = 32; break;
1130 case '4': SectorsCnt = 40; break;
1131 default: SectorsCnt = 16;
1132 }
1133 } else {
1134 blockNo = param_get8(Cmd, 0);
1135 }
1136
1137 ctmp = param_getchar(Cmd, 1);
1138 switch (ctmp) {
1139 case 'a': case 'A':
1140 keyType = !0;
1141 break;
1142 case 'b': case 'B':
1143 keyType = !1;
1144 break;
1145 case '?':
1146 keyType = 2;
1147 break;
1148 default:
1149 PrintAndLog("Key type must be A , B or ?");
1150 free(keyBlock);
1151 return 1;
1152 };
1153
1154 ctmp = param_getchar(Cmd, 2);
1155 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;
1156 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;
1157
1158 for (i = transferToEml || createDumpFile; param_getchar(Cmd, 2 + i); i++) {
1159 if (!param_gethex(Cmd, 2 + i, keyBlock + 6 * keycnt, 12)) {
1160 if ( stKeyBlock - keycnt < 2) {
1161 p = realloc(keyBlock, 6*(stKeyBlock+=10));
1162 if (!p) {
1163 PrintAndLog("Cannot allocate memory for Keys");
1164 free(keyBlock);
1165 return 2;
1166 }
1167 keyBlock = p;
1168 }
1169 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,
1170 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],
1171 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);
1172 keycnt++;
1173 } else {
1174 // May be a dic file
1175 if ( param_getstr(Cmd, 2 + i,filename) >= FILE_PATH_SIZE ) {
1176 PrintAndLog("File name too long");
1177 free(keyBlock);
1178 return 2;
1179 }
1180
1181 if ( (f = fopen( filename , "r")) ) {
1182 while( fgets(buf, sizeof(buf), f) ){
1183 if (strlen(buf) < 12 || buf[11] == '\n')
1184 continue;
1185
1186 while (fgetc(f) != '\n' && !feof(f)) ; //goto next line
1187
1188 if( buf[0]=='#' ) continue; //The line start with # is comment, skip
1189
1190 if (!isxdigit(buf[0])){
1191 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);
1192 continue;
1193 }
1194
1195 buf[12] = 0;
1196
1197 if ( stKeyBlock - keycnt < 2) {
1198 p = realloc(keyBlock, 6*(stKeyBlock+=10));
1199 if (!p) {
1200 PrintAndLog("Cannot allocate memory for defKeys");
1201 free(keyBlock);
1202 fclose(f);
1203 return 2;
1204 }
1205 keyBlock = p;
1206 }
1207 memset(keyBlock + 6 * keycnt, 0, 6);
1208 num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);
1209 PrintAndLog("check key[%2d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));
1210 keycnt++;
1211 memset(buf, 0, sizeof(buf));
1212 }
1213 fclose(f);
1214 } else {
1215 PrintAndLog("File: %s: not found or locked.", filename);
1216 free(keyBlock);
1217 return 1;
1218
1219 }
1220 }
1221 }
1222
1223 if (keycnt == 0) {
1224 PrintAndLog("No key specified, trying default keys");
1225 for (;keycnt < defaultKeysSize; keycnt++)
1226 PrintAndLog("key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,
1227 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],
1228 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);
1229 }
1230
1231 // initialize storage for found keys
1232 e_sector = calloc(SectorsCnt, sizeof(sector));
1233 if (e_sector == NULL) {
1234 free(keyBlock);
1235 return 1;
1236 }
1237
1238 // empty e_sector
1239 for(int i = 0; i < SectorsCnt; ++i){
1240 e_sector[i].Key[0] = 0xffffffffffff;
1241 e_sector[i].Key[1] = 0xffffffffffff;
1242 e_sector[i].foundKey[0] = FALSE;
1243 e_sector[i].foundKey[1] = FALSE;
1244 }
1245
1246
1247 uint8_t trgKeyType = 0;
1248 uint32_t max_keys = keycnt > (USB_CMD_DATA_SIZE/6) ? (USB_CMD_DATA_SIZE/6) : keycnt;
1249
1250 // time
1251 clock_t t1 = clock();
1252 time_t start, end;
1253 time(&start);
1254
1255 // check keys.
1256 for (trgKeyType = !keyType; trgKeyType < 2; (keyType==2) ? (++trgKeyType) : (trgKeyType=2) ) {
1257
1258 int b = blockNo;
1259 for (int i = 0; i < SectorsCnt; ++i) {
1260
1261 // skip already found keys.
1262 if (e_sector[i].foundKey[trgKeyType]) continue;
1263
1264 for (uint32_t c = 0; c < keycnt; c += max_keys) {
1265 printf(".");
1266 fflush(stdout);
1267 uint32_t size = keycnt-c > max_keys ? max_keys : keycnt-c;
1268
1269 res = mfCheckKeys(b, trgKeyType, true, size, &keyBlock[6*c], &key64);
1270 if (!res) {
1271 e_sector[i].Key[trgKeyType] = key64;
1272 e_sector[i].foundKey[trgKeyType] = TRUE;
1273 break;
1274 }
1275 }
1276 b < 127 ? ( b +=4 ) : ( b += 16 );
1277 }
1278 }
1279 t1 = clock() - t1;
1280 time(&end);
1281 unsigned long elapsed_time = difftime(end, start);
1282 if ( t1 > 0 )
1283 PrintAndLog("\nTime in checkkeys: %.0f ticks %u seconds\n", (float)t1, elapsed_time);
1284
1285
1286 // 20160116 If Sector A is found, but not Sector B, try just reading it of the tag?
1287 if ( keyType != 1 ) {
1288 PrintAndLog("testing to read key B...");
1289 for (i = 0; i < SectorsCnt; i++) {
1290 // KEY A but not KEY B
1291 if ( e_sector[i].foundKey[0] && !e_sector[i].foundKey[1] ) {
1292
1293 uint8_t sectrail = (FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);
1294
1295 PrintAndLog("Reading block %d", sectrail);
1296
1297 UsbCommand c = {CMD_MIFARE_READBL, {sectrail, 0, 0}};
1298 num_to_bytes(e_sector[i].Key[0], 6, c.d.asBytes); // KEY A
1299 clearCommandBuffer();
1300 SendCommand(&c);
1301
1302 UsbCommand resp;
1303 if ( !WaitForResponseTimeout(CMD_ACK,&resp,1500)) continue;
1304
1305 uint8_t isOK = resp.arg[0] & 0xff;
1306 if (!isOK) continue;
1307
1308 uint8_t *data = resp.d.asBytes;
1309 key64 = bytes_to_num(data+10, 6);
1310 if (key64) {
1311 PrintAndLog("Data:%s", sprint_hex(data+10, 6));
1312 e_sector[i].foundKey[1] = 1;
1313 e_sector[i].Key[1] = key64;
1314 }
1315 }
1316 }
1317 }
1318
1319
1320 //print them
1321 printKeyTable( SectorsCnt, e_sector );
1322
1323 if (transferToEml) {
1324 uint8_t block[16] = {0x00};
1325 for (uint8_t i = 0; i < SectorsCnt; ++i ) {
1326 mfEmlGetMem(block, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);
1327 if (e_sector[i].foundKey[0])
1328 num_to_bytes(e_sector[i].Key[0], 6, block);
1329 if (e_sector[i].foundKey[1])
1330 num_to_bytes(e_sector[i].Key[1], 6, block+10);
1331 mfEmlSetMem(block, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);
1332 }
1333 PrintAndLog("Found keys have been transferred to the emulator memory");
1334 }
1335
1336 if (createDumpFile) {
1337 FILE *fkeys = fopen("dumpkeys.bin","wb");
1338 if (fkeys == NULL) {
1339 PrintAndLog("Could not create file dumpkeys.bin");
1340 free(keyBlock);
1341 free(e_sector);
1342 return 1;
1343 }
1344 PrintAndLog("Printing keys to binary file dumpkeys.bin...");
1345
1346 for( i=0; i<SectorsCnt; i++) {
1347 num_to_bytes(e_sector[i].Key[0], 6, tempkey);
1348 fwrite ( tempkey, 1, 6, fkeys );
1349 }
1350 for(i=0; i<SectorsCnt; i++) {
1351 num_to_bytes(e_sector[i].Key[1], 6, tempkey);
1352 fwrite ( tempkey, 1, 6, fkeys );
1353 }
1354 fclose(fkeys);
1355 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");
1356 }
1357
1358 free(keyBlock);
1359 free(e_sector);
1360 PrintAndLog("");
1361 return 0;
1362 }
1363 #define ATTACK_KEY_COUNT 8
1364 sector *k_sector = NULL;
1365 uint8_t k_sectorsCount = 16;
1366 void readerAttack(nonces_t data[], bool setEmulatorMem) {
1367
1368 // initialize storage for found keys
1369 if (k_sector == NULL)
1370 k_sector = calloc(k_sectorsCount, sizeof(sector));
1371 if (k_sector == NULL)
1372 return;
1373
1374 uint64_t key = 0;
1375
1376 // empty e_sector
1377 for(int i = 0; i < k_sectorsCount; ++i){
1378 k_sector[i].Key[0] = 0xffffffffffff;
1379 k_sector[i].Key[1] = 0xffffffffffff;
1380 k_sector[i].foundKey[0] = FALSE;
1381 k_sector[i].foundKey[1] = FALSE;
1382 }
1383
1384 printf("enter reader attack\n");
1385 for (uint8_t i = 0; i < ATTACK_KEY_COUNT; ++i) {
1386 if (data[i].ar2 > 0) {
1387
1388 if (tryMfk32(data[i], &key)) {
1389 PrintAndLog("Found Key%s for sector %02d: [%012"llx"]"
1390 , (data[i].keytype) ? "B" : "A"
1391 , data[i].sector
1392 , key
1393 );
1394
1395 k_sector[i].Key[data[i].keytype] = key;
1396 k_sector[i].foundKey[data[i].keytype] = TRUE;
1397
1398 //set emulator memory for keys
1399 if (setEmulatorMem) {
1400 uint8_t memBlock[16] = {0,0,0,0,0,0, 0xff, 0x0F, 0x80, 0x69, 0,0,0,0,0,0};
1401 num_to_bytes( k_sector[i].Key[0], 6, memBlock);
1402 num_to_bytes( k_sector[i].Key[1], 6, memBlock+10);
1403 mfEmlSetMem( memBlock, i*4 + 3, 1);
1404 PrintAndLog("Setting Emulator Memory Block %02d: [%s]"
1405 , i*4 + 3
1406 , sprint_hex( memBlock, sizeof(memBlock))
1407 );
1408 }
1409 break;
1410 }
1411 //moebius attack
1412 // if (tryMfk32_moebius(data[i+ATTACK_KEY_COUNT], &key)) {
1413 // PrintAndLog("M-Found Key%s for sector %02d: [%012"llx"]"
1414 // ,(data[i+ATTACK_KEY_COUNT].keytype) ? "B" : "A"
1415 // , data[i+ATTACK_KEY_COUNT].sector
1416 // , key
1417 // );
1418 // }
1419 }
1420 }
1421 }
1422
1423 int CmdHF14AMf1kSim(const char *Cmd) {
1424
1425 uint8_t uid[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1426 uint8_t exitAfterNReads = 0;
1427 uint8_t flags = (FLAG_UID_IN_EMUL | FLAG_4B_UID_IN_DATA);
1428 int uidlen = 0;
1429 bool setEmulatorMem = false;
1430 uint8_t cmdp = 0;
1431 bool errors = false;
1432
1433 while(param_getchar(Cmd, cmdp) != 0x00) {
1434 switch(param_getchar(Cmd, cmdp)) {
1435 case 'e':
1436 case 'E':
1437 setEmulatorMem = true;
1438 cmdp++;
1439 break;
1440 case 'h':
1441 case 'H':
1442 return usage_hf14_mf1ksim();
1443 case 'i':
1444 case 'I':
1445 flags |= FLAG_INTERACTIVE;
1446 cmdp++;
1447 break;
1448 case 'n':
1449 case 'N':
1450 exitAfterNReads = param_get8(Cmd, cmdp+1);
1451 cmdp += 2;
1452 break;
1453 case 'u':
1454 case 'U':
1455 param_gethex_ex(Cmd, cmdp+1, uid, &uidlen);
1456 switch(uidlen) {
1457 case 20: flags = FLAG_10B_UID_IN_DATA; break;
1458 case 14: flags = FLAG_7B_UID_IN_DATA; break;
1459 case 8: flags = FLAG_4B_UID_IN_DATA; break;
1460 default: return usage_hf14_mf1ksim();
1461 }
1462 cmdp +=2;
1463 break;
1464 case 'x':
1465 case 'X':
1466 flags |= FLAG_NR_AR_ATTACK;
1467 cmdp++;
1468 break;
1469 default:
1470 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
1471 errors = true;
1472 break;
1473 }
1474 if(errors) break;
1475 }
1476 //Validations
1477 if(errors) return usage_hf14_mf1ksim();
1478
1479 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) "
1480 , (uidlen == 0 ) ? "N/A" : sprint_hex(uid, uidlen>>1)
1481 , exitAfterNReads
1482 , flags
1483 , flags);
1484
1485 UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {flags, exitAfterNReads, 0}};
1486 memcpy(c.d.asBytes, uid, sizeof(uid));
1487 clearCommandBuffer();
1488 SendCommand(&c);
1489
1490 if(flags & FLAG_INTERACTIVE) {
1491 PrintAndLog("Press pm3-button or send another cmd to abort simulation");
1492
1493 nonces_t data[ATTACK_KEY_COUNT*2];
1494 UsbCommand resp;
1495
1496 while( !ukbhit() ){
1497 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) continue;
1498
1499 if ( !(flags & FLAG_NR_AR_ATTACK) ) break;
1500 if ( (resp.arg[0] & 0xffff) != CMD_SIMULATE_MIFARE_CARD ) break;
1501
1502 memcpy( data, resp.d.asBytes, sizeof(data) );
1503 readerAttack(data, setEmulatorMem);
1504 }
1505
1506 if (k_sector != NULL) {
1507 printKeyTable(k_sectorsCount, k_sector );
1508 free(k_sector);
1509 }
1510 }
1511 return 0;
1512 }
1513
1514 int CmdHF14AMfSniff(const char *Cmd){
1515 bool wantLogToFile = FALSE;
1516 bool wantDecrypt = FALSE;
1517 //bool wantSaveToEml = FALSE; TODO
1518 bool wantSaveToEmlFile = FALSE;
1519
1520 //var
1521 int tmpchar;
1522 int res = 0;
1523 int len = 0;
1524 int blockLen = 0;
1525 int pckNum = 0;
1526 int num = 0;
1527 uint8_t uid[10];
1528 uint8_t uid_len = 0;
1529 uint8_t atqa[2] = {0x00, 0x00};
1530 uint8_t sak = 0;
1531 bool isTag = FALSE;
1532 uint8_t *buf = NULL;
1533 uint16_t bufsize = 0;
1534 uint8_t *bufPtr = NULL;
1535 uint16_t traceLen = 0;
1536
1537 memset(uid, 0x00, sizeof(uid));
1538
1539 char ctmp = param_getchar(Cmd, 0);
1540 if ( ctmp == 'h' || ctmp == 'H' ) return usage_hf14_sniff();
1541
1542 for (int i = 0; i < 4; i++) {
1543 ctmp = param_getchar(Cmd, i);
1544 if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;
1545 if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;
1546 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO
1547 if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;
1548 }
1549
1550 printf("-------------------------------------------------------------------------\n");
1551 printf("Executing mifare sniffing command. \n");
1552 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");
1553 printf("Press the key on pc keyboard to abort the client.\n");
1554 printf("-------------------------------------------------------------------------\n");
1555
1556 UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};
1557 clearCommandBuffer();
1558 SendCommand(&c);
1559
1560 // wait cycle
1561 while (true) {
1562 printf(".");
1563 fflush(stdout);
1564 if (ukbhit()) {
1565 tmpchar = getchar();
1566 (void)tmpchar;
1567 printf("\naborted via keyboard!\n");
1568 break;
1569 }
1570
1571 UsbCommand resp;
1572 if (WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {
1573 res = resp.arg[0] & 0xff;
1574 traceLen = resp.arg[1];
1575 len = resp.arg[2];
1576
1577 // we are done?
1578 if (res == 0) {
1579 free(buf);
1580 return 0;
1581 }
1582
1583 if (res == 1) { // there is (more) data to be transferred
1584 if (pckNum == 0) { // first packet, (re)allocate necessary buffer
1585 if (traceLen > bufsize) {
1586 uint8_t *p;
1587 if (buf == NULL) // not yet allocated
1588 p = malloc(traceLen);
1589 else // need more memory
1590 p = realloc(buf, traceLen);
1591
1592 if (p == NULL) {
1593 PrintAndLog("Cannot allocate memory for trace");
1594 free(buf);
1595 return 2;
1596 }
1597 buf = p;
1598 }
1599 bufPtr = buf;
1600 bufsize = traceLen;
1601 memset(buf, 0x00, traceLen);
1602 }
1603 if (bufPtr == NULL) {
1604 PrintAndLog("Cannot allocate memory for trace");
1605 free(buf);
1606 return 2;
1607 }
1608 // what happens if LEN is bigger then TRACELEN --iceman
1609 memcpy(bufPtr, resp.d.asBytes, len);
1610 bufPtr += len;
1611 pckNum++;
1612 }
1613
1614 if (res == 2) { // received all data, start displaying
1615 blockLen = bufPtr - buf;
1616 bufPtr = buf;
1617 printf(">\n");
1618 PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);
1619 while (bufPtr - buf < blockLen) {
1620 bufPtr += 6; // skip (void) timing information
1621 len = *((uint16_t *)bufPtr);
1622 if(len & 0x8000) {
1623 isTag = true;
1624 len &= 0x7fff;
1625 } else {
1626 isTag = false;
1627 }
1628 bufPtr += 2;
1629 if ((len == 17) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff) && (bufPtr[15] == 0xff) && (bufPtr[16] == 0xff)) {
1630 memcpy(uid, bufPtr + 2, 10);
1631 memcpy(atqa, bufPtr + 2 + 10, 2);
1632 switch (atqa[0] & 0xC0) {
1633 case 0x80: uid_len = 10; break;
1634 case 0x40: uid_len = 7; break;
1635 default: uid_len = 4; break;
1636 }
1637 sak = bufPtr[14];
1638 PrintAndLog("tag select uid| %s atqa:0x%02x%02x sak:0x%02x",
1639 sprint_hex(uid, uid_len),
1640 atqa[1],
1641 atqa[0],
1642 sak);
1643 if (wantLogToFile || wantDecrypt) {
1644 FillFileNameByUID(logHexFileName, uid, ".log", uid_len);
1645 AddLogCurrentDT(logHexFileName);
1646 }
1647 if (wantDecrypt)
1648 mfTraceInit(uid, uid_len, atqa, sak, wantSaveToEmlFile);
1649 } else {
1650 PrintAndLog("%03d| %s |%s", num, isTag ? "TAG" : "RDR", sprint_hex(bufPtr, len));
1651 if (wantLogToFile)
1652 AddLogHex(logHexFileName, isTag ? "TAG| ":"RDR| ", bufPtr, len);
1653 if (wantDecrypt)
1654 mfTraceDecode(bufPtr, len, wantSaveToEmlFile);
1655 num++;
1656 }
1657 bufPtr += len;
1658 bufPtr += ((len-1)/8+1); // ignore parity
1659 }
1660 pckNum = 0;
1661 }
1662 } // resp not NULL
1663 } // while (true)
1664
1665 free(buf);
1666 return 0;
1667 }
1668
1669 int CmdHF14AMfDbg(const char *Cmd) {
1670
1671 char ctmp = param_getchar(Cmd, 0);
1672 if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') return usage_hf14_dbg();
1673
1674 uint8_t dbgMode = param_get8ex(Cmd, 0, 0, 10);
1675 if (dbgMode > 4) return usage_hf14_dbg();
1676
1677 UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};
1678 SendCommand(&c);
1679 return 0;
1680 }
1681
1682 int CmdHF14AMfKeyBrute(const char *Cmd) {
1683
1684 uint8_t blockNo = 0, keytype = 0;
1685 uint8_t key[6] = {0, 0, 0, 0, 0, 0};
1686 uint64_t foundkey = 0;
1687
1688 char cmdp = param_getchar(Cmd, 0);
1689 if ( cmdp == 'H' || cmdp == 'h') return usage_hf14_keybrute();
1690
1691 // block number
1692 blockNo = param_get8(Cmd, 0);
1693
1694 // keytype
1695 cmdp = param_getchar(Cmd, 1);
1696 if (cmdp == 'B' || cmdp == 'b') keytype = 1;
1697
1698 // key
1699 if (param_gethex(Cmd, 2, key, 12)) return usage_hf14_keybrute();
1700
1701 clock_t t1 = clock();
1702 time_t start, end;
1703 time(&start);
1704
1705 if (mfKeyBrute( blockNo, keytype, key, &foundkey))
1706 PrintAndLog("Found valid key: %012"llx" \n", foundkey);
1707 else
1708 PrintAndLog("Key not found");
1709
1710 t1 = clock() - t1;
1711 time(&end);
1712 unsigned long elapsed_time = difftime(end, start);
1713 if ( t1 > 0 )
1714 PrintAndLog("\nTime in keybrute: %.0f ticks %u seconds\n", (float)t1, elapsed_time);
1715
1716 return 0;
1717 }
1718
1719 void printKeyTable( uint8_t sectorscnt, sector *e_sector ){
1720 PrintAndLog("|---|----------------|---|----------------|---|");
1721 PrintAndLog("|sec|key A |res|key B |res|");
1722 PrintAndLog("|---|----------------|---|----------------|---|");
1723 for (uint8_t i = 0; i < sectorscnt; ++i) {
1724 PrintAndLog("|%03d| %012"llx" | %d | %012"llx" | %d |", i,
1725 e_sector[i].Key[0], e_sector[i].foundKey[0],
1726 e_sector[i].Key[1], e_sector[i].foundKey[1]
1727 );
1728 }
1729 PrintAndLog("|---|----------------|---|----------------|---|");
1730 }
1731
1732 // EMULATOR COMMANDS
1733 int CmdHF14AMfEGet(const char *Cmd)
1734 {
1735 uint8_t blockNo = 0;
1736 uint8_t data[16] = {0x00};
1737
1738 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1739 PrintAndLog("Usage: hf mf eget <block number>");
1740 PrintAndLog(" sample: hf mf eget 0 ");
1741 return 0;
1742 }
1743
1744 blockNo = param_get8(Cmd, 0);
1745
1746 PrintAndLog("");
1747 if (!mfEmlGetMem(data, blockNo, 1)) {
1748 PrintAndLog("data[%3d]:%s", blockNo, sprint_hex(data, 16));
1749 } else {
1750 PrintAndLog("Command execute timeout");
1751 }
1752
1753 return 0;
1754 }
1755
1756 int CmdHF14AMfEClear(const char *Cmd)
1757 {
1758 if (param_getchar(Cmd, 0) == 'h') {
1759 PrintAndLog("Usage: hf mf eclr");
1760 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");
1761 return 0;
1762 }
1763
1764 UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};
1765 SendCommand(&c);
1766 return 0;
1767 }
1768
1769 int CmdHF14AMfESet(const char *Cmd)
1770 {
1771 uint8_t memBlock[16];
1772 uint8_t blockNo = 0;
1773
1774 memset(memBlock, 0x00, sizeof(memBlock));
1775
1776 if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {
1777 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");
1778 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");
1779 return 0;
1780 }
1781
1782 blockNo = param_get8(Cmd, 0);
1783
1784 if (param_gethex(Cmd, 1, memBlock, 32)) {
1785 PrintAndLog("block data must include 32 HEX symbols");
1786 return 1;
1787 }
1788
1789 // 1 - blocks count
1790 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};
1791 memcpy(c.d.asBytes, memBlock, 16);
1792 SendCommand(&c);
1793 return 0;
1794 }
1795
1796 int CmdHF14AMfELoad(const char *Cmd)
1797 {
1798 FILE * f;
1799 char filename[FILE_PATH_SIZE];
1800 char *fnameptr = filename;
1801 char buf[64] = {0x00};
1802 uint8_t buf8[64] = {0x00};
1803 int i, len, blockNum, numBlocks;
1804 int nameParamNo = 1;
1805 uint8_t blockWidth = 32;
1806 char ctmp = param_getchar(Cmd, 0);
1807
1808 if ( ctmp == 'h' || ctmp == 'H' || ctmp == 0x00) {
1809 PrintAndLog("It loads emul dump from the file `filename.eml`");
1810 PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`> [numblocks]");
1811 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K, u = UL");
1812 PrintAndLog("");
1813 PrintAndLog(" sample: hf mf eload filename");
1814 PrintAndLog(" hf mf eload 4 filename");
1815 return 0;
1816 }
1817
1818 switch (ctmp) {
1819 case '0' : numBlocks = 5*4; break;
1820 case '1' :
1821 case '\0': numBlocks = 16*4; break;
1822 case '2' : numBlocks = 32*4; break;
1823 case '4' : numBlocks = 256; break;
1824 case 'U' : // fall through
1825 case 'u' : numBlocks = 255; blockWidth = 8; break;
1826 default: {
1827 numBlocks = 16*4;
1828 nameParamNo = 0;
1829 }
1830 }
1831 uint32_t numblk2 = param_get32ex(Cmd,2,0,10);
1832 if (numblk2 > 0) numBlocks = numblk2;
1833
1834 len = param_getstr(Cmd,nameParamNo,filename);
1835
1836 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;
1837
1838 fnameptr += len;
1839
1840 sprintf(fnameptr, ".eml");
1841
1842 // open file
1843 f = fopen(filename, "r");
1844 if (f == NULL) {
1845 PrintAndLog("File %s not found or locked", filename);
1846 return 1;
1847 }
1848
1849 blockNum = 0;
1850 while(!feof(f)){
1851 memset(buf, 0, sizeof(buf));
1852
1853 if (fgets(buf, sizeof(buf), f) == NULL) {
1854
1855 if (blockNum >= numBlocks) break;
1856
1857 PrintAndLog("File reading error.");
1858 fclose(f);
1859 return 2;
1860 }
1861
1862 if (strlen(buf) < blockWidth){
1863 if(strlen(buf) && feof(f))
1864 break;
1865 PrintAndLog("File content error. Block data must include %d HEX symbols", blockWidth);
1866 fclose(f);
1867 return 2;
1868 }
1869
1870 for (i = 0; i < blockWidth; i += 2) {
1871 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
1872 }
1873 if (mfEmlSetMem_xt(buf8, blockNum, 1, blockWidth/2)) {
1874 PrintAndLog("Cant set emul block: %3d", blockNum);
1875 fclose(f);
1876 return 3;
1877 }
1878 printf(".");
1879 blockNum++;
1880
1881 if (blockNum >= numBlocks) break;
1882 }
1883 fclose(f);
1884 printf("\n");
1885
1886 if ((blockNum != numBlocks)) {
1887 PrintAndLog("File content error. Got %d must be %d blocks.",blockNum, numBlocks);
1888 return 4;
1889 }
1890 PrintAndLog("Loaded %d blocks from file: %s", blockNum, filename);
1891 return 0;
1892 }
1893
1894 int CmdHF14AMfESave(const char *Cmd)
1895 {
1896 FILE * f;
1897 char filename[FILE_PATH_SIZE];
1898 char * fnameptr = filename;
1899 uint8_t buf[64];
1900 int i, j, len, numBlocks;
1901 int nameParamNo = 1;
1902
1903 memset(filename, 0, sizeof(filename));
1904 memset(buf, 0, sizeof(buf));
1905
1906 char ctmp = param_getchar(Cmd, 0);
1907
1908 if ( ctmp == 'h' || ctmp == 'H') {
1909 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");
1910 PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");
1911 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1912 PrintAndLog("");
1913 PrintAndLog(" sample: hf mf esave ");
1914 PrintAndLog(" hf mf esave 4");
1915 PrintAndLog(" hf mf esave 4 filename");
1916 return 0;
1917 }
1918
1919 switch (ctmp) {
1920 case '0' : numBlocks = 5*4; break;
1921 case '1' :
1922 case '\0': numBlocks = 16*4; break;
1923 case '2' : numBlocks = 32*4; break;
1924 case '4' : numBlocks = 256; break;
1925 default: {
1926 numBlocks = 16*4;
1927 nameParamNo = 0;
1928 }
1929 }
1930
1931 len = param_getstr(Cmd,nameParamNo,filename);
1932
1933 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;
1934
1935 // user supplied filename?
1936 if (len < 1) {
1937 // get filename (UID from memory)
1938 if (mfEmlGetMem(buf, 0, 1)) {
1939 PrintAndLog("Can\'t get UID from block: %d", 0);
1940 len = sprintf(fnameptr, "dump");
1941 fnameptr += len;
1942 }
1943 else {
1944 for (j = 0; j < 7; j++, fnameptr += 2)
1945 sprintf(fnameptr, "%02X", buf[j]);
1946 }
1947 } else {
1948 fnameptr += len;
1949 }
1950
1951 // add file extension
1952 sprintf(fnameptr, ".eml");
1953
1954 // open file
1955 f = fopen(filename, "w+");
1956
1957 if ( !f ) {
1958 PrintAndLog("Can't open file %s ", filename);
1959 return 1;
1960 }
1961
1962 // put hex
1963 for (i = 0; i < numBlocks; i++) {
1964 if (mfEmlGetMem(buf, i, 1)) {
1965 PrintAndLog("Cant get block: %d", i);
1966 break;
1967 }
1968 for (j = 0; j < 16; j++)
1969 fprintf(f, "%02X", buf[j]);
1970 fprintf(f,"\n");
1971 }
1972 fclose(f);
1973
1974 PrintAndLog("Saved %d blocks to file: %s", numBlocks, filename);
1975
1976 return 0;
1977 }
1978
1979 int CmdHF14AMfECFill(const char *Cmd)
1980 {
1981 uint8_t keyType = 0;
1982 uint8_t numSectors = 16;
1983
1984 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
1985 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");
1986 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
1987 PrintAndLog("");
1988 PrintAndLog("samples: hf mf ecfill A");
1989 PrintAndLog(" hf mf ecfill A 4");
1990 PrintAndLog("Read card and transfer its data to emulator memory.");
1991 PrintAndLog("Keys must be laid in the emulator memory. \n");
1992 return 0;
1993 }
1994
1995 char ctmp = param_getchar(Cmd, 0);
1996 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {
1997 PrintAndLog("Key type must be A or B");
1998 return 1;
1999 }
2000 if (ctmp != 'A' && ctmp != 'a') keyType = 1;
2001
2002 ctmp = param_getchar(Cmd, 1);
2003 switch (ctmp) {
2004 case '0' : numSectors = 5; break;
2005 case '1' :
2006 case '\0': numSectors = 16; break;
2007 case '2' : numSectors = 32; break;
2008 case '4' : numSectors = 40; break;
2009 default: numSectors = 16;
2010 }
2011
2012 printf("--params: numSectors: %d, keyType:%d", numSectors, keyType);
2013 UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {numSectors, keyType, 0}};
2014 SendCommand(&c);
2015 return 0;
2016 }
2017
2018 int CmdHF14AMfEKeyPrn(const char *Cmd)
2019 {
2020 int i;
2021 uint8_t numSectors;
2022 uint8_t data[16];
2023 uint64_t keyA, keyB;
2024
2025 char cmdp = param_getchar(Cmd, 0);
2026
2027 if ( cmdp == 'h' || cmdp == 'H' ) {
2028 PrintAndLog("It prints the keys loaded in the emulator memory");
2029 PrintAndLog("Usage: hf mf ekeyprn [card memory]");
2030 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");
2031 PrintAndLog("");
2032 PrintAndLog(" sample: hf mf ekeyprn 1");
2033 return 0;
2034 }
2035
2036 switch (cmdp) {
2037 case '0' : numSectors = 5; break;
2038 case '1' :
2039 case '\0': numSectors = 16; break;
2040 case '2' : numSectors = 32; break;
2041 case '4' : numSectors = 40; break;
2042 default: numSectors = 16;
2043 }
2044
2045 PrintAndLog("|---|----------------|----------------|");
2046 PrintAndLog("|sec|key A |key B |");
2047 PrintAndLog("|---|----------------|----------------|");
2048 for (i = 0; i < numSectors; i++) {
2049 if (mfEmlGetMem(data, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1)) {
2050 PrintAndLog("error get block %d", FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);
2051 break;
2052 }
2053 keyA = bytes_to_num(data, 6);
2054 keyB = bytes_to_num(data + 10, 6);
2055 PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB);
2056 }
2057 PrintAndLog("|---|----------------|----------------|");
2058
2059 return 0;
2060 }
2061
2062 // CHINESE MAGIC COMMANDS
2063
2064 int CmdHF14AMfCSetUID(const char *Cmd) {
2065 uint8_t wipeCard = 0;
2066 uint8_t uid[8] = {0x00};
2067 uint8_t oldUid[8] = {0x00};
2068 uint8_t atqa[2] = {0x00};
2069 uint8_t sak[1] = {0x00};
2070 uint8_t atqaPresent = 1;
2071 int res;
2072 char ctmp;
2073 int argi=0;
2074
2075 if (strlen(Cmd) < 1 || param_getchar(Cmd, argi) == 'h') {
2076 PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");
2077 PrintAndLog("If you also want to wipe the card then add 'w' at the end of the command line.");
2078 PrintAndLog("");
2079 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols] [w]");
2080 PrintAndLog("");
2081 PrintAndLog("sample: hf mf csetuid 01020304");
2082 PrintAndLog(" hf mf csetuid 01020304 0004 08 w");
2083 return 0;
2084 }
2085
2086 if (param_getchar(Cmd, argi) && param_gethex(Cmd, argi, uid, 8)) {
2087 PrintAndLog("UID must include 8 HEX symbols");
2088 return 1;
2089 }
2090 argi++;
2091
2092 ctmp = param_getchar(Cmd, argi);
2093 if (ctmp == 'w' || ctmp == 'W') {
2094 wipeCard = 1;
2095 atqaPresent = 0;
2096 }
2097
2098 if (atqaPresent) {
2099 if (param_getchar(Cmd, argi)) {
2100 if (param_gethex(Cmd, argi, atqa, 4)) {
2101 PrintAndLog("ATQA must include 4 HEX symbols");
2102 return 1;
2103 }
2104 argi++;
2105 if (!param_getchar(Cmd, argi) || param_gethex(Cmd, argi, sak, 2)) {
2106 PrintAndLog("SAK must include 2 HEX symbols");
2107 return 1;
2108 }
2109 argi++;
2110 } else
2111 atqaPresent = 0;
2112 }
2113
2114 if(!wipeCard) {
2115 ctmp = param_getchar(Cmd, argi);
2116 if (ctmp == 'w' || ctmp == 'W') {
2117 wipeCard = 1;
2118 }
2119 }
2120
2121 PrintAndLog("--wipe card:%s uid:%s", (wipeCard)?"YES":"NO", sprint_hex(uid, 4));
2122
2123 res = mfCSetUID(uid, (atqaPresent) ? atqa : NULL, (atqaPresent) ? sak : NULL, oldUid, wipeCard);
2124 if (res) {
2125 PrintAndLog("Can't set UID. error=%d", res);
2126 return 1;
2127 }
2128
2129 PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));
2130 PrintAndLog("new UID:%s", sprint_hex(uid, 4));
2131 return 0;
2132 }
2133
2134 int CmdHF14AMfCSetBlk(const char *Cmd) {
2135 uint8_t block[16] = {0x00};
2136 uint8_t blockNo = 0;
2137 uint8_t params = MAGIC_SINGLE;
2138 int res;
2139
2140 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {
2141 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");
2142 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");
2143 PrintAndLog("Set block data for magic Chinese card (only works with such cards)");
2144 PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");
2145 return 0;
2146 }
2147
2148 blockNo = param_get8(Cmd, 0);
2149
2150 if (param_gethex(Cmd, 1, block, 32)) {
2151 PrintAndLog("block data must include 32 HEX symbols");
2152 return 1;
2153 }
2154
2155 char ctmp = param_getchar(Cmd, 2);
2156 if (ctmp == 'w' || ctmp == 'W')
2157 params |= MAGIC_WIPE;
2158
2159 PrintAndLog("--block number:%2d data:%s", blockNo, sprint_hex(block, 16));
2160
2161 res = mfCSetBlock(blockNo, block, NULL, params);
2162 if (res) {
2163 PrintAndLog("Can't write block. error=%d", res);
2164 return 1;
2165 }
2166 return 0;
2167 }
2168
2169 int CmdHF14AMfCLoad(const char *Cmd) {
2170 FILE * f;
2171 char filename[FILE_PATH_SIZE];
2172 char * fnameptr = filename;
2173 char buf[64] = {0x00};
2174 uint8_t buf8[64] = {0x00};
2175 uint8_t fillFromEmulator = 0;
2176 int i, len, blockNum, flags=0;
2177
2178 memset(filename, 0, sizeof(filename));
2179
2180 char ctmp = param_getchar(Cmd, 0);
2181
2182 if (ctmp == 'h' || ctmp == 'H' || ctmp == 0x00) {
2183 PrintAndLog("It loads magic Chinese card from the file `filename.eml`");
2184 PrintAndLog("or from emulator memory (option `e`)");
2185 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");
2186 PrintAndLog(" or: hf mf cload e ");
2187 PrintAndLog(" sample: hf mf cload filename");
2188 return 0;
2189 }
2190
2191 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
2192
2193 if (fillFromEmulator) {
2194 for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {
2195 if (mfEmlGetMem(buf8, blockNum, 1)) {
2196 PrintAndLog("Cant get block: %d", blockNum);
2197 return 2;
2198 }
2199 if (blockNum == 0) flags = MAGIC_INIT + MAGIC_WUPC; // switch on field and send magic sequence
2200 if (blockNum == 1) flags = 0; // just write
2201 if (blockNum == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF; // Done. Magic Halt and switch off field.
2202
2203 if (mfCSetBlock(blockNum, buf8, NULL, flags)) {
2204 PrintAndLog("Cant set magic card block: %d", blockNum);
2205 return 3;
2206 }
2207 }
2208 return 0;
2209 } else {
2210 len = strlen(Cmd);
2211 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;
2212
2213 memcpy(filename, Cmd, len);
2214 fnameptr += len;
2215
2216 sprintf(fnameptr, ".eml");
2217
2218 // open file
2219 f = fopen(filename, "r");
2220 if (f == NULL) {
2221 PrintAndLog("File not found or locked.");
2222 return 1;
2223 }
2224
2225 blockNum = 0;
2226 while(!feof(f)){
2227
2228 memset(buf, 0, sizeof(buf));
2229
2230 if (fgets(buf, sizeof(buf), f) == NULL) {
2231 fclose(f);
2232 PrintAndLog("File reading error.");
2233 return 2;
2234 }
2235
2236 if (strlen(buf) < 32) {
2237 if(strlen(buf) && feof(f))
2238 break;
2239 PrintAndLog("File content error. Block data must include 32 HEX symbols");
2240 fclose(f);
2241 return 2;
2242 }
2243 for (i = 0; i < 32; i += 2)
2244 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);
2245
2246 if (blockNum == 0) flags = MAGIC_INIT + MAGIC_WUPC; // switch on field and send magic sequence
2247 if (blockNum == 1) flags = 0; // just write
2248 if (blockNum == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF; // Done. Switch off field.
2249
2250 if (mfCSetBlock(blockNum, buf8, NULL, flags)) {
2251 PrintAndLog("Can't set magic card block: %d", blockNum);
2252 fclose(f);
2253 return 3;
2254 }
2255 blockNum++;
2256
2257 if (blockNum >= 16 * 4) break; // magic card type - mifare 1K
2258 }
2259 fclose(f);
2260
2261 // 64 or 256blocks.
2262 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){
2263 PrintAndLog("File content error. There must be 64 blocks");
2264 return 4;
2265 }
2266 PrintAndLog("Loaded from file: %s", filename);
2267 return 0;
2268 }
2269 return 0;
2270 }
2271
2272 int CmdHF14AMfCGetBlk(const char *Cmd) {
2273 uint8_t data[16];
2274 uint8_t blockNo = 0;
2275 int res;
2276 memset(data, 0x00, sizeof(data));
2277 char ctmp = param_getchar(Cmd, 0);
2278
2279 if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') {
2280 PrintAndLog("Usage: hf mf cgetblk <block number>");
2281 PrintAndLog("sample: hf mf cgetblk 1");
2282 PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");
2283 return 0;
2284 }
2285
2286 blockNo = param_get8(Cmd, 0);
2287
2288 PrintAndLog("--block number:%2d ", blockNo);
2289
2290 res = mfCGetBlock(blockNo, data, MAGIC_SINGLE);
2291 if (res) {
2292 PrintAndLog("Can't read block. error=%d", res);
2293 return 1;
2294 }
2295
2296 PrintAndLog("data: %s", sprint_hex(data, sizeof(data)));
2297 return 0;
2298 }
2299
2300 int CmdHF14AMfCGetSc(const char *Cmd) {
2301 uint8_t data[16];
2302 uint8_t sectorNo = 0;
2303 int i, res, flags;
2304 memset(data, 0x00, sizeof(data));
2305 char ctmp = param_getchar(Cmd, 0);
2306
2307 if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') {
2308 PrintAndLog("Usage: hf mf cgetsc <sector number>");
2309 PrintAndLog("sample: hf mf cgetsc 0");
2310 PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");
2311 return 0;
2312 }
2313
2314 sectorNo = param_get8(Cmd, 0);
2315 if (sectorNo > 15) {
2316 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");
2317 return 1;
2318 }
2319
2320 PrintAndLog("--sector number:%d ", sectorNo);
2321 PrintAndLog("block | data");
2322
2323 flags = MAGIC_INIT + MAGIC_WUPC;
2324 for (i = 0; i < 4; i++) {
2325 if (i == 1) flags = 0;
2326 if (i == 3) flags = MAGIC_HALT + MAGIC_OFF;
2327
2328 res = mfCGetBlock(sectorNo * 4 + i, data, flags);
2329 if (res) {
2330 PrintAndLog("Can't read block. %d error=%d", sectorNo * 4 + i, res);
2331 return 1;
2332 }
2333 PrintAndLog(" %3d | %s", sectorNo * 4 + i, sprint_hex(data, sizeof(data)));
2334 }
2335 return 0;
2336 }
2337
2338 int CmdHF14AMfCSave(const char *Cmd) {
2339
2340 FILE * f;
2341 char filename[FILE_PATH_SIZE];
2342 char * fnameptr = filename;
2343 uint8_t fillFromEmulator = 0;
2344 uint8_t buf[64];
2345 int i, j, len, flags;
2346
2347 memset(filename, 0, sizeof(filename));
2348 memset(buf, 0, sizeof(buf));
2349 char ctmp = param_getchar(Cmd, 0);
2350
2351 if ( ctmp == 'h' || ctmp == 'H' ) {
2352 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");
2353 PrintAndLog("or into emulator memory (option `e`)");
2354 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");
2355 PrintAndLog(" sample: hf mf esave ");
2356 PrintAndLog(" hf mf esave filename");
2357 PrintAndLog(" hf mf esave e \n");
2358 return 0;
2359 }
2360 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;
2361
2362 if (fillFromEmulator) {
2363 // put into emulator
2364 flags = MAGIC_INIT + MAGIC_WUPC;
2365 for (i = 0; i < 16 * 4; i++) {
2366 if (i == 1) flags = 0;
2367 if (i == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF;
2368
2369 if (mfCGetBlock(i, buf, flags)) {
2370 PrintAndLog("Cant get block: %d", i);
2371 break;
2372 }
2373
2374 if (mfEmlSetMem(buf, i, 1)) {
2375 PrintAndLog("Cant set emul block: %d", i);
2376 return 3;
2377 }
2378 }
2379 return 0;
2380 } else {
2381 len = strlen(Cmd);
2382 if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;
2383
2384 // get filename based on UID
2385 if (len < 1) {
2386
2387 if (mfCGetBlock(0, buf, MAGIC_SINGLE)) {
2388 PrintAndLog("Cant get block: %d", 0);
2389 len = sprintf(fnameptr, "dump");
2390 fnameptr += len;
2391 } else {
2392 for (j = 0; j < 7; j++, fnameptr += 2)
2393 sprintf(fnameptr, "%02x", buf[j]);
2394 }
2395 } else {
2396 memcpy(filename, Cmd, len);
2397 fnameptr += len;
2398 }
2399
2400 // add .eml extension
2401 sprintf(fnameptr, ".eml");
2402
2403 // open file
2404 f = fopen(filename, "w+");
2405
2406 if (f == NULL) {
2407 PrintAndLog("File not found or locked.");
2408 return 1;
2409 }
2410
2411 // put hex
2412 flags = MAGIC_INIT + MAGIC_WUPC;
2413 for (i = 0; i < 16 * 4; i++) {
2414 if (i == 1) flags = 0;
2415 if (i == 16 * 4 - 1) flags = MAGIC_HALT + MAGIC_OFF;
2416
2417 if (mfCGetBlock(i, buf, flags)) {
2418 PrintAndLog("Cant get block: %d", i);
2419 break;
2420 }
2421 for (j = 0; j < 16; j++)
2422 fprintf(f, "%02x", buf[j]);
2423 fprintf(f,"\n");
2424 }
2425 fflush(f);
2426 fclose(f);
2427 PrintAndLog("Saved to file: %s", filename);
2428 return 0;
2429 }
2430 }
2431
2432 //needs nt, ar, at, Data to decrypt
2433 int CmdHf14MfDecryptBytes(const char *Cmd){
2434 uint8_t data[50];
2435 uint32_t nt = param_get32ex(Cmd,0,0,16);
2436 uint32_t ar_enc = param_get32ex(Cmd,1,0,16);
2437 uint32_t at_enc = param_get32ex(Cmd,2,0,16);
2438
2439 int len = 0;
2440 param_gethex_ex(Cmd, 3, data, &len);
2441
2442 len /= 2;
2443 int limit = sizeof(data) / 2;
2444
2445 if ( len >= limit )
2446 len = limit;
2447
2448 return tryDecryptWord( nt, ar_enc, at_enc, data, len);
2449 }
2450
2451 static command_t CommandTable[] = {
2452 {"help", CmdHelp, 1, "This help"},
2453 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},
2454 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},
2455 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},
2456 {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},
2457 {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},
2458 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},
2459 {"chk", CmdHF14AMfChk, 0, "Check keys"},
2460 {"mifare", CmdHF14AMifare, 0, "Darkside attack. read parity error messages."},
2461 {"nested", CmdHF14AMfNested, 0, "Nested attack. Test nested authentication"},
2462 {"hardnested", CmdHF14AMfNestedHard, 0, "Nested attack for hardened Mifare cards"},
2463 {"keybrute", CmdHF14AMfKeyBrute, 0, "J_Run's 2nd phase of multiple sector nested authentication key recovery"},
2464 {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},
2465 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},
2466 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},
2467 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},
2468 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},
2469 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},
2470 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},
2471 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},
2472 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},
2473 {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},
2474 {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block - Magic Chinese card"},
2475 {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block - Magic Chinese card"},
2476 {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector - Magic Chinese card"},
2477 {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},
2478 {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},
2479 {"decrypt", CmdHf14MfDecryptBytes, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},
2480 {NULL, NULL, 0, NULL}
2481 };
2482
2483 int CmdHFMF(const char *Cmd) {
2484 clearCommandBuffer();
2485 CmdsParse(CommandTable, Cmd);
2486 return 0;
2487 }
2488
2489 int CmdHelp(const char *Cmd) {
2490 CmdsHelp(CommandTable);
2491 return 0;
2492 }
Impressum, Datenschutz