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