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