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