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