]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhfmf.c
Some more lua-scripts and library fixes from iceman, including more default keys
[proxmark3-svn] / client / cmdhfmf.c
CommitLineData
9ca155ba 1//-----------------------------------------------------------------------------\r
b62a5a84 2// Copyright (C) 2011,2012 Merlok\r
9ca155ba
M
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\r
11#include "cmdhfmf.h"\r
12\r
13static int CmdHelp(const char *Cmd);\r
14\r
9ca155ba
M
15int CmdHF14AMifare(const char *Cmd)\r
16{\r
17 uint32_t uid = 0;\r
1c611bbd 18 uint32_t nt = 0, nr = 0;\r
9ca155ba
M
19 uint64_t par_list = 0, ks_list = 0, r_key = 0;\r
20 uint8_t isOK = 0;\r
ac14bee3 21 uint8_t keyBlock[8] = {0};\r
aea4d766 22\r
1c611bbd 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
aea4d766 30\r
9ca155ba 31 \r
aea4d766 32start:\r
e772353f 33 clearCommandBuffer();\r
34 SendCommand(&c);\r
9ca155ba
M
35 \r
36 //flush queue\r
37 while (ukbhit()) getchar();\r
38\r
9ca155ba
M
39 \r
40 // wait cycle\r
41 while (true) {\r
1c611bbd 42 printf(".");\r
aea4d766 43 fflush(stdout);\r
9ca155ba
M
44 if (ukbhit()) {\r
45 getchar();\r
46 printf("\naborted via keyboard!\n");\r
47 break;\r
48 }\r
49 \r
902cb3c0 50 UsbCommand resp;\r
1c611bbd 51 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {\r
902cb3c0 52 isOK = resp.arg[0] & 0xff;\r
902cb3c0 53 uid = (uint32_t)bytes_to_num(resp.d.asBytes + 0, 4);\r
54 nt = (uint32_t)bytes_to_num(resp.d.asBytes + 4, 4);\r
55 par_list = bytes_to_num(resp.d.asBytes + 8, 8);\r
56 ks_list = bytes_to_num(resp.d.asBytes + 16, 8);\r
1c611bbd 57 nr = bytes_to_num(resp.d.asBytes + 24, 4);\r
9ca155ba 58 printf("\n\n");\r
9ca155ba
M
59 if (!isOK) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n");\r
60 break;\r
61 }\r
62 } \r
1c611bbd 63\r
9ca155ba
M
64 printf("\n");\r
65 \r
66 // error\r
67 if (isOK != 1) return 1;\r
68 \r
69 // execute original function from util nonce2key\r
1c611bbd 70 if (nonce2key(uid, nt, nr, par_list, ks_list, &r_key))\r
bfaecce6
M
71 {\r
72 isOK = 2;\r
73 PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt); \r
74 } else {\r
75 printf("------------------------------------------------------------------\n");\r
125a98a1 76 PrintAndLog("Key found:%012"llx" \n", r_key);\r
9ca155ba 77\r
bfaecce6
M
78 num_to_bytes(r_key, 6, keyBlock);\r
79 isOK = mfCheckKeys(0, 0, 1, keyBlock, &r_key);\r
80 }\r
9ca155ba 81 if (!isOK) \r
125a98a1 82 PrintAndLog("Found valid key:%012"llx, r_key);\r
9ca155ba 83 else\r
aea4d766 84 {\r
1c611bbd 85 if (isOK != 2) PrintAndLog("Found invalid key. "); \r
86 PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");\r
87 c.arg[0] = false;\r
aea4d766 88 goto start;\r
89 }\r
9ca155ba
M
90 \r
91 return 0;\r
92}\r
93\r
94int CmdHF14AMfWrBl(const char *Cmd)\r
95{\r
96 uint8_t blockNo = 0;\r
97 uint8_t keyType = 0;\r
98 uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
99 uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r
100 \r
101 char cmdp = 0x00;\r
102\r
103 if (strlen(Cmd)<3) {\r
104 PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");\r
105 PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");\r
106 return 0;\r
107 } \r
108\r
109 blockNo = param_get8(Cmd, 0);\r
110 cmdp = param_getchar(Cmd, 1);\r
111 if (cmdp == 0x00) {\r
112 PrintAndLog("Key type must be A or B");\r
113 return 1;\r
114 }\r
115 if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r
116 if (param_gethex(Cmd, 2, key, 12)) {\r
117 PrintAndLog("Key must include 12 HEX symbols");\r
118 return 1;\r
119 }\r
120 if (param_gethex(Cmd, 3, bldata, 32)) {\r
121 PrintAndLog("Block data must include 32 HEX symbols");\r
122 return 1;\r
123 }\r
124 PrintAndLog("--block no:%02x key type:%02x key:%s", blockNo, keyType, sprint_hex(key, 6));\r
125 PrintAndLog("--data: %s", sprint_hex(bldata, 16));\r
126 \r
127 UsbCommand c = {CMD_MIFARE_WRITEBL, {blockNo, keyType, 0}};\r
128 memcpy(c.d.asBytes, key, 6);\r
129 memcpy(c.d.asBytes + 10, bldata, 16);\r
130 SendCommand(&c);\r
9ca155ba 131\r
902cb3c0 132 UsbCommand resp;\r
133 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
134 uint8_t isOK = resp.arg[0] & 0xff;\r
9ca155ba
M
135 PrintAndLog("isOk:%02x", isOK);\r
136 } else {\r
137 PrintAndLog("Command execute timeout");\r
138 }\r
139\r
d2f487af 140 return 0;\r
141}\r
142\r
143int CmdHF14AMfUWrBl(const char *Cmd)\r
144{\r
1801456e
P
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
d2f487af 148 UsbCommand resp;\r
1801456e
P
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:%02x", 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:%02x", 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:%02x", 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:%02x", 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:%02x", 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
d2f487af 249 }\r
1801456e 250 return 0;\r
d2f487af 251}\r
252\r
253int CmdHF14AMfRdBl(const char *Cmd)\r
254{\r
255 uint8_t blockNo = 0;\r
9ca155ba
M
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:%02x key type:%02x key:%s ", blockNo, keyType, 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
9ca155ba 284\r
902cb3c0 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
9ca155ba
M
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
d2f487af 298 return 0;\r
299}\r
300\r
301int CmdHF14AMfURdBl(const char *Cmd)\r
302{\r
303 uint8_t blockNo = 0;\r
304\r
305 if (strlen(Cmd)<1) {\r
306 PrintAndLog("Usage: hf mf urdbl <block number>");\r
307 PrintAndLog(" sample: hf mf urdbl 0");\r
308 return 0;\r
309 } \r
310 \r
311 blockNo = param_get8(Cmd, 0);\r
312 PrintAndLog("--block no:%02x", blockNo);\r
313 \r
314 UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}};\r
315 SendCommand(&c);\r
316\r
317 UsbCommand resp;\r
318 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
319 uint8_t isOK = resp.arg[0] & 0xff;\r
320 uint8_t * data = resp.d.asBytes;\r
321\r
322 if (isOK)\r
323 PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 4));\r
324 else\r
325 PrintAndLog("isOk:%02x", isOK);\r
326 } else {\r
327 PrintAndLog("Command execute timeout");\r
328 }\r
329\r
330 return 0;\r
331}\r
332\r
333int CmdHF14AMfURdCard(const char *Cmd)\r
334{\r
335 int i;\r
336 uint8_t sectorNo = 0;\r
337 uint8_t *lockbytes_t=NULL;\r
338 uint8_t lockbytes[2]={0,0};\r
339 bool bit[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};\r
340 \r
341 uint8_t isOK = 0;\r
342 uint8_t * data = NULL;\r
343\r
344 if (sectorNo > 15) {\r
345 PrintAndLog("Sector number must be less than 16");\r
346 return 1;\r
347 }\r
348 PrintAndLog("Attempting to Read Ultralight... ");\r
349 \r
350 UsbCommand c = {CMD_MIFAREU_READCARD, {sectorNo}};\r
351 SendCommand(&c);\r
352\r
353 UsbCommand resp;\r
354 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
355 isOK = resp.arg[0] & 0xff;\r
356 data = resp.d.asBytes;\r
357\r
358 PrintAndLog("isOk:%02x", isOK);\r
359 if (isOK) \r
360 for (i = 0; i < 16; i++) {\r
361 switch(i){\r
362 case 2:\r
363 //process lock bytes\r
364 lockbytes_t=data+(i*4);\r
365 lockbytes[0]=lockbytes_t[2];\r
366 lockbytes[1]=lockbytes_t[3];\r
367 for(int j=0; j<16; j++){\r
368 bit[j]=lockbytes[j/8] & ( 1 <<(7-j%8));\r
369 }\r
370 //PrintAndLog("LB %02x %02x", lockbytes[0],lockbytes[1]);\r
371 //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
372 PrintAndLog("Block %02x:%s ", i,sprint_hex(data + i * 4, 4));\r
373 break;\r
374 case 3: \r
375 PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[4]);\r
376 break;\r
377 case 4:\r
378 PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[3]);\r
379 break;\r
380 case 5:\r
381 PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[2]);\r
382 break;\r
383 case 6:\r
384 PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[1]);\r
385 break;\r
386 case 7:\r
387 PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[0]);\r
388 break;\r
389 case 8:\r
390 PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[15]);\r
391 break;\r
392 case 9:\r
393 PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[14]);\r
394 break;\r
395 case 10:\r
396 PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[13]);\r
397 break;\r
398 case 11:\r
399 PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[12]);\r
400 break;\r
401 case 12:\r
402 PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[11]);\r
403 break;\r
404 case 13:\r
405 PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[10]);\r
406 break;\r
407 case 14:\r
408 PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[9]);\r
409 break;\r
410 case 15:\r
411 PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[8]);\r
412 break;\r
413 default:\r
414 PrintAndLog("Block %02x:%s ", i,sprint_hex(data + i * 4, 4));\r
415 break;\r
416 }\r
417 }\r
418 } else {\r
419 PrintAndLog("Command1 execute timeout");\r
420 }\r
421 return 0;\r
422}\r
423\r
424int CmdHF14AMfRdSc(const char *Cmd)\r
425{\r
426 int i;\r
9ca155ba
M
427 uint8_t sectorNo = 0;\r
428 uint8_t keyType = 0;\r
429 uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
430 \r
431 uint8_t isOK = 0;\r
432 uint8_t * data = NULL;\r
433\r
434 char cmdp = 0x00;\r
435\r
436 if (strlen(Cmd)<3) {\r
437 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");\r
438 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");\r
439 return 0;\r
440 } \r
441 \r
442 sectorNo = param_get8(Cmd, 0);\r
443 if (sectorNo > 63) {\r
444 PrintAndLog("Sector number must be less than 64");\r
445 return 1;\r
446 }\r
447 cmdp = param_getchar(Cmd, 1);\r
448 if (cmdp == 0x00) {\r
449 PrintAndLog("Key type must be A or B");\r
450 return 1;\r
451 }\r
452 if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r
453 if (param_gethex(Cmd, 2, key, 12)) {\r
454 PrintAndLog("Key must include 12 HEX symbols");\r
455 return 1;\r
456 }\r
457 PrintAndLog("--sector no:%02x key type:%02x key:%s ", sectorNo, keyType, sprint_hex(key, 6));\r
458 \r
459 UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};\r
460 memcpy(c.d.asBytes, key, 6);\r
461 SendCommand(&c);\r
9ca155ba
M
462 PrintAndLog(" ");\r
463\r
902cb3c0 464 UsbCommand resp;\r
465 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
466 isOK = resp.arg[0] & 0xff;\r
467 data = resp.d.asBytes;\r
9ca155ba
M
468\r
469 PrintAndLog("isOk:%02x", isOK);\r
470 if (isOK) \r
471 for (i = 0; i < 2; i++) {\r
472 PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));\r
473 }\r
474 } else {\r
475 PrintAndLog("Command1 execute timeout");\r
476 }\r
477\r
902cb3c0 478 // response2\r
9ca155ba 479 PrintAndLog(" ");\r
902cb3c0 480 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
481 isOK = resp.arg[0] & 0xff;\r
482 data = resp.d.asBytes;\r
9ca155ba
M
483\r
484 if (isOK) \r
485 for (i = 0; i < 2; i++) {\r
486 PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));\r
487 }\r
488 } else {\r
489 PrintAndLog("Command2 execute timeout");\r
490 }\r
491 \r
492 return 0;\r
493}\r
494\r
aea4d766 495int CmdHF14AMfDump(const char *Cmd)\r
26fdb4ab 496{\r
497 int i, j;\r
498 \r
aea4d766 499 uint8_t keyA[40][6];\r
500 uint8_t keyB[40][6];\r
501 uint8_t rights[40][4];\r
26fdb4ab 502 \r
26fdb4ab 503 FILE *fin;\r
504 FILE *fout;\r
505 \r
902cb3c0 506 UsbCommand resp;\r
26fdb4ab 507 \r
508 if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {\r
aea4d766 509 PrintAndLog("Could not find file dumpkeys.bin");\r
26fdb4ab 510 return 1;\r
511 }\r
512 \r
513 if ((fout = fopen("dumpdata.bin","wb")) == NULL) { \r
aea4d766 514 PrintAndLog("Could not create file name dumpdata.bin");\r
26fdb4ab 515 return 1;\r
516 }\r
517 \r
518 // Read key file\r
519 \r
520 for (i=0 ; i<16 ; i++) {\r
759c16b3 521 if (fread( keyA[i], 1, 6, fin ) == 0) {\r
522 PrintAndLog("File reading error.");\r
523 return 2;\r
524 }\r
26fdb4ab 525 }\r
526 for (i=0 ; i<16 ; i++) {\r
759c16b3 527 if (fread( keyB[i], 1, 6, fin ) == 0) {\r
528 PrintAndLog("File reading error.");\r
529 return 2;\r
530 }\r
26fdb4ab 531 }\r
532 \r
533 // Read access rights to sectors\r
534 \r
535 PrintAndLog("|-----------------------------------------|");\r
536 PrintAndLog("|------ Reading sector access bits...-----|");\r
537 PrintAndLog("|-----------------------------------------|");\r
538 \r
539 for (i = 0 ; i < 16 ; i++) {\r
540 UsbCommand c = {CMD_MIFARE_READBL, {4*i + 3, 0, 0}};\r
541 memcpy(c.d.asBytes, keyA[i], 6);\r
542 SendCommand(&c);\r
26fdb4ab 543\r
902cb3c0 544 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
545 uint8_t isOK = resp.arg[0] & 0xff;\r
546 uint8_t *data = resp.d.asBytes;\r
26fdb4ab 547 if (isOK){\r
548 rights[i][0] = ((data[7] & 0x10)>>4) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>2);\r
549 rights[i][1] = ((data[7] & 0x20)>>5) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>3);\r
550 rights[i][2] = ((data[7] & 0x40)>>6) | ((data[8] & 0x4)>>1) | ((data[8] & 0x40)>>4);\r
551 rights[i][3] = ((data[7] & 0x80)>>7) | ((data[8] & 0x8)>>2) | ((data[8] & 0x80)>>5);\r
552 }\r
553 else{\r
554 PrintAndLog("Could not get access rights for block %d", i);\r
555 }\r
556 }\r
557 else {\r
558 PrintAndLog("Command execute timeout");\r
559 }\r
560 }\r
561 \r
562 // Read blocks and print to file\r
563 \r
564 PrintAndLog("|-----------------------------------------|");\r
565 PrintAndLog("|----- Dumping all blocks to file... -----|");\r
566 PrintAndLog("|-----------------------------------------|");\r
567 \r
902cb3c0 568 \r
26fdb4ab 569 for (i=0 ; i<16 ; i++) {\r
570 for (j=0 ; j<4 ; j++) {\r
902cb3c0 571 bool received = false;\r
572 \r
573 if (j == 3){\r
26fdb4ab 574 UsbCommand c = {CMD_MIFARE_READBL, {i*4 + j, 0, 0}};\r
575 memcpy(c.d.asBytes, keyA[i], 6);\r
576 SendCommand(&c);\r
902cb3c0 577 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
26fdb4ab 578 }\r
579 else{\r
580 if ((rights[i][j] == 6) | (rights[i][j] == 5)) {\r
581 UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 1, 0}};\r
582 memcpy(c.d.asBytes, keyB[i], 6);\r
583 SendCommand(&c);\r
902cb3c0 584 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
26fdb4ab 585 }\r
586 else if (rights[i][j] == 7) {\r
587 PrintAndLog("Access rights do not allow reading of sector %d block %d",i,j);\r
588 }\r
589 else {\r
590 UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 0, 0}};\r
591 memcpy(c.d.asBytes, keyA[i], 6);\r
592 SendCommand(&c);\r
902cb3c0 593 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
26fdb4ab 594 }\r
595 }\r
596\r
902cb3c0 597 if (received) {\r
598 uint8_t isOK = resp.arg[0] & 0xff;\r
599 uint8_t *data = resp.d.asBytes;\r
3d77fdfa 600 if (j == 3) {\r
601 data[0] = (keyA[i][0]);\r
602 data[1] = (keyA[i][1]);\r
603 data[2] = (keyA[i][2]);\r
604 data[3] = (keyA[i][3]);\r
605 data[4] = (keyA[i][4]);\r
606 data[5] = (keyA[i][5]);\r
607 data[10] = (keyB[i][0]);\r
608 data[11] = (keyB[i][1]);\r
609 data[12] = (keyB[i][2]);\r
610 data[13] = (keyB[i][3]);\r
611 data[14] = (keyB[i][4]);\r
612 data[15] = (keyB[i][5]);\r
613 }\r
26fdb4ab 614 if (isOK) {\r
615 fwrite ( data, 1, 16, fout );\r
6a2c1d8e 616 PrintAndLog("Dumped card data into 'dumpdata.bin'");\r
05ed5c49 617\r
26fdb4ab 618 }\r
619 else {\r
620 PrintAndLog("Could not get access rights for block %d", i);\r
621 }\r
622 }\r
623 else {\r
624 PrintAndLog("Command execute timeout");\r
625 }\r
626 }\r
627 }\r
628 \r
629 fclose(fin);\r
630 fclose(fout);\r
26fdb4ab 631 return 0;\r
632}\r
633\r
aea4d766 634int CmdHF14AMfRestore(const char *Cmd)\r
26fdb4ab 635{\r
636\r
637 int i,j;\r
26fdb4ab 638 uint8_t keyType = 0;\r
639 uint8_t key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r
640 uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r
641 uint8_t keyA[16][6];\r
642 uint8_t keyB[16][6];\r
643 \r
26fdb4ab 644 FILE *fdump;\r
645 FILE *fkeys;\r
646 \r
26fdb4ab 647 if ((fdump = fopen("dumpdata.bin","rb")) == NULL) {\r
aea4d766 648 PrintAndLog("Could not find file dumpdata.bin");\r
26fdb4ab 649 return 1;\r
650 }\r
651 if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) {\r
aea4d766 652 PrintAndLog("Could not find file dumpkeys.bin");\r
26fdb4ab 653 return 1;\r
654 }\r
655 \r
656 for (i=0 ; i<16 ; i++) {\r
759c16b3 657 if (fread(keyA[i], 1, 6, fkeys) == 0) {\r
658 PrintAndLog("File reading error.");\r
659 return 2;\r
660 }\r
26fdb4ab 661 }\r
662 for (i=0 ; i<16 ; i++) {\r
759c16b3 663 if (fread(keyB[i], 1, 6, fkeys) == 0) {\r
664 PrintAndLog("File reading error.");\r
665 return 2;\r
666 }\r
26fdb4ab 667 }\r
668 \r
9d710943 669 PrintAndLog("Restoring dumpdata.bin to card");\r
26fdb4ab 670\r
671 for (i=0 ; i<16 ; i++) {\r
672 for( j=0 ; j<4 ; j++) {\r
673 UsbCommand c = {CMD_MIFARE_WRITEBL, {i*4 + j, keyType, 0}};\r
674 memcpy(c.d.asBytes, key, 6);\r
675 \r
759c16b3 676 if (fread(bldata, 1, 16, fdump) == 0) {\r
677 PrintAndLog("File reading error.");\r
678 return 2;\r
679 }\r
26fdb4ab 680 \r
681 if (j == 3) {\r
682 bldata[0] = (keyA[i][0]);\r
683 bldata[1] = (keyA[i][1]);\r
684 bldata[2] = (keyA[i][2]);\r
685 bldata[3] = (keyA[i][3]);\r
686 bldata[4] = (keyA[i][4]);\r
687 bldata[5] = (keyA[i][5]);\r
688 bldata[10] = (keyB[i][0]);\r
689 bldata[11] = (keyB[i][1]);\r
690 bldata[12] = (keyB[i][2]);\r
691 bldata[13] = (keyB[i][3]);\r
692 bldata[14] = (keyB[i][4]);\r
693 bldata[15] = (keyB[i][5]);\r
694 } \r
695 \r
6c064e16 696 PrintAndLog("Writing to block %2d: %s", i*4+j, sprint_hex(bldata, 16));\r
697 \r
698 /*\r
9d710943 699 PrintAndLog("Writing to block %2d: %s Confirm? [Y,N]", i*4+j, sprint_hex(bldata, 16));\r
700 \r
0f7f9edc 701 scanf("%c",&ch);\r
6c064e16 702 if ((ch != 'y') && (ch != 'Y')){\r
9d710943 703 PrintAndLog("Aborting !");\r
704 return 1;\r
705 }\r
6c064e16 706 */\r
26fdb4ab 707 \r
708 memcpy(c.d.asBytes + 10, bldata, 16);\r
709 SendCommand(&c);\r
26fdb4ab 710\r
902cb3c0 711 UsbCommand resp;\r
712 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
713 uint8_t isOK = resp.arg[0] & 0xff;\r
26fdb4ab 714 PrintAndLog("isOk:%02x", isOK);\r
715 } else {\r
716 PrintAndLog("Command execute timeout");\r
717 }\r
718 }\r
719 }\r
720 \r
721 fclose(fdump);\r
722 fclose(fkeys);\r
723 return 0;\r
724}\r
725\r
9ca155ba
M
726int CmdHF14AMfNested(const char *Cmd)\r
727{\r
728 int i, j, res, iterations;\r
729 sector * e_sector = NULL;\r
730 uint8_t blockNo = 0;\r
731 uint8_t keyType = 0;\r
732 uint8_t trgBlockNo = 0;\r
733 uint8_t trgKeyType = 0;\r
734 uint8_t blDiff = 0;\r
735 int SectorsCnt = 0;\r
736 uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
9492e0b0 737 uint8_t keyBlock[6*6];\r
9ca155ba 738 uint64_t key64 = 0;\r
8556b852 739 int transferToEml = 0;\r
9ca155ba 740 \r
26fdb4ab 741 int createDumpFile = 0;\r
742 FILE *fkeys;\r
21156267 743 uint8_t standart[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r
744 uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r
26fdb4ab 745 \r
9ca155ba
M
746 char cmdp, ctmp;\r
747\r
748 if (strlen(Cmd)<3) {\r
749 PrintAndLog("Usage:");\r
26fdb4ab 750 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");\r
0014cb46
M
751 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");\r
752 PrintAndLog(" <target block number> <target key A/B> [t]");\r
9ca155ba 753 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r
8556b852 754 PrintAndLog("t - transfer keys into emulator memory");\r
26fdb4ab 755 PrintAndLog("d - write keys to binary file");\r
9ca155ba
M
756 PrintAndLog(" ");\r
757 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");\r
8556b852 758 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");\r
26fdb4ab 759 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");\r
9ca155ba
M
760 PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");\r
761 return 0;\r
762 } \r
763 \r
764 cmdp = param_getchar(Cmd, 0);\r
765 blockNo = param_get8(Cmd, 1);\r
766 ctmp = param_getchar(Cmd, 2);\r
767 if (ctmp == 0x00) {\r
768 PrintAndLog("Key type must be A or B");\r
769 return 1;\r
770 }\r
771 if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r
772 if (param_gethex(Cmd, 3, key, 12)) {\r
773 PrintAndLog("Key must include 12 HEX symbols");\r
774 return 1;\r
775 }\r
776 \r
8556b852 777 if (cmdp == 'o' || cmdp == 'O') {\r
9ca155ba
M
778 cmdp = 'o';\r
779 trgBlockNo = param_get8(Cmd, 4);\r
780 ctmp = param_getchar(Cmd, 5);\r
781 if (ctmp == 0x00) {\r
782 PrintAndLog("Target key type must be A or B");\r
783 return 1;\r
784 }\r
785 if (ctmp != 'A' && ctmp != 'a') trgKeyType = 1;\r
786 } else {\r
787 switch (cmdp) {\r
788 case '0': SectorsCnt = 05; break;\r
789 case '1': SectorsCnt = 16; break;\r
790 case '2': SectorsCnt = 32; break;\r
791 case '4': SectorsCnt = 64; break;\r
792 default: SectorsCnt = 16;\r
793 }\r
794 }\r
8556b852
M
795\r
796 ctmp = param_getchar(Cmd, 4);\r
21156267 797 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;\r
798 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;\r
799 \r
8556b852
M
800 ctmp = param_getchar(Cmd, 6);\r
801 transferToEml |= (ctmp == 't' || ctmp == 'T');\r
21156267 802 transferToEml |= (ctmp == 'd' || ctmp == 'D');\r
9ca155ba 803 \r
8556b852 804 PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo, keyType, sprint_hex(key, 6), transferToEml);\r
9ca155ba
M
805 if (cmdp == 'o')\r
806 PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo, trgKeyType);\r
807\r
808 if (cmdp == 'o') {\r
9492e0b0 809 if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, true)) {\r
9ca155ba
M
810 PrintAndLog("Nested error.");\r
811 return 2;\r
812 }\r
9492e0b0 813 key64 = bytes_to_num(keyBlock, 6);\r
814 if (key64) {\r
125a98a1 815 PrintAndLog("Found valid key:%012"llx, key64);\r
8556b852
M
816\r
817 // transfer key to the emulator\r
818 if (transferToEml) {\r
819 mfEmlGetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1);\r
820 \r
821 if (!trgKeyType)\r
822 num_to_bytes(key64, 6, keyBlock);\r
823 else\r
824 num_to_bytes(key64, 6, &keyBlock[10]);\r
825 mfEmlSetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1); \r
826 }\r
827 } else {\r
9ca155ba 828 PrintAndLog("No valid key found");\r
8556b852 829 }\r
21156267 830 }\r
831 else { // ------------------------------------ multiple sectors working\r
9492e0b0 832 clock_t time1;\r
833 time1 = clock();\r
834\r
9ca155ba
M
835 blDiff = blockNo % 4;\r
836 PrintAndLog("Block shift=%d", blDiff);\r
837 e_sector = calloc(SectorsCnt, sizeof(sector));\r
838 if (e_sector == NULL) return 1;\r
839 \r
840 //test current key 4 sectors\r
841 memcpy(keyBlock, key, 6);\r
9492e0b0 842 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 1 * 6));\r
843 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 2 * 6));\r
844 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 3 * 6));\r
845 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 4 * 6));\r
9ca155ba
M
846 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));\r
847\r
848 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);\r
849 for (i = 0; i < SectorsCnt; i++) {\r
850 for (j = 0; j < 2; j++) {\r
851 if (e_sector[i].foundKey[j]) continue;\r
852 \r
853 res = mfCheckKeys(i * 4 + blDiff, j, 6, keyBlock, &key64);\r
854 \r
855 if (!res) {\r
856 e_sector[i].Key[j] = key64;\r
857 e_sector[i].foundKey[j] = 1;\r
858 }\r
859 }\r
9492e0b0 860 }\r
861 \r
9ca155ba 862 \r
9ca155ba
M
863 // nested sectors\r
864 iterations = 0;\r
865 PrintAndLog("nested...");\r
9492e0b0 866 bool calibrate = true;\r
9ca155ba 867 for (i = 0; i < NESTED_SECTOR_RETRY; i++) {\r
9492e0b0 868 for (trgBlockNo = blDiff; trgBlockNo < SectorsCnt * 4; trgBlockNo = trgBlockNo + 4) {\r
9ca155ba
M
869 for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) { \r
870 if (e_sector[trgBlockNo / 4].foundKey[trgKeyType]) continue;\r
9492e0b0 871 PrintAndLog("-----------------------------------------------");\r
872 if(mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, calibrate)) {\r
873 PrintAndLog("Nested error.\n");\r
874 return 2;\r
875 }\r
876 else {\r
877 calibrate = false;\r
878 }\r
9ca155ba
M
879 \r
880 iterations++;\r
9492e0b0 881\r
882 key64 = bytes_to_num(keyBlock, 6);\r
883 if (key64) {\r
125a98a1 884 PrintAndLog("Found valid key:%012"llx, key64);\r
9ca155ba
M
885 e_sector[trgBlockNo / 4].foundKey[trgKeyType] = 1;\r
886 e_sector[trgBlockNo / 4].Key[trgKeyType] = key64;\r
887 }\r
888 }\r
9492e0b0 889 }\r
9ca155ba
M
890 }\r
891\r
a0041115 892 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
9492e0b0 893 \r
894 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations);\r
9ca155ba
M
895 //print them\r
896 PrintAndLog("|---|----------------|---|----------------|---|");\r
897 PrintAndLog("|sec|key A |res|key B |res|");\r
898 PrintAndLog("|---|----------------|---|----------------|---|");\r
899 for (i = 0; i < SectorsCnt; i++) {\r
125a98a1 900 PrintAndLog("|%03d| %012"llx" | %d | %012"llx" | %d |", i,\r
9ca155ba
M
901 e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);\r
902 }\r
903 PrintAndLog("|---|----------------|---|----------------|---|");\r
904 \r
8556b852
M
905 // transfer them to the emulator\r
906 if (transferToEml) {\r
907 for (i = 0; i < SectorsCnt; i++) {\r
908 mfEmlGetMem(keyBlock, i * 4 + 3, 1);\r
909 if (e_sector[i].foundKey[0])\r
ab8b654e 910 num_to_bytes(e_sector[i].Key[0], 6, keyBlock);\r
8556b852
M
911 if (e_sector[i].foundKey[1])\r
912 num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);\r
913 mfEmlSetMem(keyBlock, i * 4 + 3, 1);\r
914 } \r
915 }\r
916 \r
21156267 917 // Create dump file\r
26fdb4ab 918 if (createDumpFile) {\r
919 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) { \r
aea4d766 920 PrintAndLog("Could not create file dumpkeys.bin");\r
26fdb4ab 921 free(e_sector);\r
922 return 1;\r
923 }\r
21156267 924 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");\r
26fdb4ab 925 for(i=0; i<16; i++) {\r
21156267 926 if (e_sector[i].foundKey[0]){\r
927 num_to_bytes(e_sector[i].Key[0], 6, tempkey);\r
928 fwrite ( tempkey, 1, 6, fkeys );\r
929 }\r
930 else{\r
931 fwrite ( &standart, 1, 6, fkeys );\r
932 }\r
26fdb4ab 933 }\r
934 for(i=0; i<16; i++) {\r
21156267 935 if (e_sector[i].foundKey[1]){\r
936 num_to_bytes(e_sector[i].Key[1], 6, tempkey);\r
937 fwrite ( tempkey, 1, 6, fkeys );\r
938 }\r
939 else{\r
940 fwrite ( &standart, 1, 6, fkeys );\r
941 }\r
26fdb4ab 942 }\r
943 fclose(fkeys);\r
944 }\r
945 \r
9ca155ba
M
946 free(e_sector);\r
947 }\r
948\r
949 return 0;\r
950}\r
951\r
aea4d766 952static uint32_t\r
953get_trailer_block (uint32_t uiBlock)\r
954{\r
955 // Test if we are in the small or big sectors\r
956 uint32_t trailer_block = 0;\r
957 if (uiBlock < 128) {\r
958 trailer_block = uiBlock + (3 - (uiBlock % 4));\r
959 } else {\r
960 trailer_block = uiBlock + (15 - (uiBlock % 16));\r
961 }\r
962 return trailer_block;\r
963}\r
9ca155ba
M
964int CmdHF14AMfChk(const char *Cmd)\r
965{\r
aea4d766 966 FILE * f;\r
967 char filename[256]={0};\r
83613803 968 char buf[13];\r
aea4d766 969 uint8_t *keyBlock = NULL, *p;\r
970 uint8_t stKeyBlock = 20;\r
971 \r
9ca155ba
M
972 int i, res;\r
973 int keycnt = 0;\r
974 char ctmp = 0x00;\r
975 uint8_t blockNo = 0;\r
aea4d766 976 uint8_t SectorsCnt = 1;\r
9ca155ba 977 uint8_t keyType = 0;\r
9ca155ba 978 uint64_t key64 = 0;\r
aea4d766 979 \r
980 int transferToEml = 0;\r
981 int createDumpFile = 0;\r
9ca155ba 982\r
aea4d766 983 keyBlock = calloc(stKeyBlock, 6);\r
984 if (keyBlock == NULL) return 1;\r
985\r
0c12504a 986 uint64_t defaultKeys[] =\r
987 {\r
988 0xffffffffffff, // Default key (first key used by program if no user defined key)\r
989 0x000000000000, // Blank key\r
990 0xa0a1a2a3a4a5, // NFCForum MAD key\r
991 0xb0b1b2b3b4b5,\r
992 0xaabbccddeeff,\r
993 0x4d3a99c351dd,\r
994 0x1a982c7e459a,\r
995 0xd3f7d3f7d3f7,\r
996 0x714c5c886e97,\r
997 0x587ee5f9350f,\r
998 0xa0478cc39091,\r
999 0x533cb6c723f6,\r
1000 0x8fd0a4f256e9\r
1001 };\r
1002 int defaultKeysSize = (sizeof(defaultKeys) / 7) - 1;\r
1003\r
1004 for (int defaultKeyCounter = 0; defaultKeyCounter < defaultKeysSize; defaultKeyCounter++)\r
1005 {\r
1006 num_to_bytes(defaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));\r
1007 }\r
aea4d766 1008 \r
9ca155ba 1009 if (strlen(Cmd)<3) {\r
aea4d766 1010 PrintAndLog("Usage: hf mf chk <block number>/<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");\r
1011 PrintAndLog(" * - all sectors");\r
1012 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r
1013// PrintAndLog("d - write keys to binary file\n");\r
1014 \r
1015 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");\r
1016 PrintAndLog(" hf mf chk *1 ? t");\r
9ca155ba
M
1017 return 0;\r
1018 } \r
1019 \r
aea4d766 1020 if (param_getchar(Cmd, 0)=='*') {\r
1021 blockNo = 3;\r
1022 switch(param_getchar(Cmd+1, 0)) {\r
1023 case '0': SectorsCnt = 5; break;\r
1024 case '1': SectorsCnt = 16; break;\r
1025 case '2': SectorsCnt = 32; break;\r
1026 case '4': SectorsCnt = 40; break;\r
1027 default: SectorsCnt = 16;\r
1028 }\r
1029 }\r
1030 else\r
1031 blockNo = param_get8(Cmd, 0);\r
1032 \r
9ca155ba 1033 ctmp = param_getchar(Cmd, 1);\r
aea4d766 1034 switch (ctmp) { \r
1035 case 'a': case 'A':\r
1036 keyType = !0;\r
1037 break;\r
1038 case 'b': case 'B':\r
1039 keyType = !1;\r
1040 break;\r
1041 case '?':\r
1042 keyType = 2;\r
1043 break;\r
1044 default:\r
1045 PrintAndLog("Key type must be A , B or ?");\r
9ca155ba 1046 return 1;\r
aea4d766 1047 };\r
9ca155ba 1048 \r
aea4d766 1049 ctmp = param_getchar(Cmd, 2);\r
1050 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;\r
1051 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;\r
1052 \r
1053 for (i = transferToEml || createDumpFile; param_getchar(Cmd, 2 + i); i++) {\r
1054 if (!param_gethex(Cmd, 2 + i, keyBlock + 6 * keycnt, 12)) {\r
1055 if ( stKeyBlock - keycnt < 2) {\r
1056 p = realloc(keyBlock, 6*(stKeyBlock+=10));\r
1057 if (!p) {\r
1058 PrintAndLog("Cannot allocate memory for Keys");\r
1059 free(keyBlock);\r
1060 return 2;\r
1061 }\r
1062 keyBlock = p;\r
1063 }\r
1064 PrintAndLog("chk key[%d] %02x%02x%02x%02x%02x%02x", keycnt,\r
1065 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r
1066 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r
1067 keycnt++;\r
1068 } else {\r
1069 // May be a dic file\r
1070 if ( param_getstr(Cmd, 2 + i,filename) > 255 ) {\r
1071 PrintAndLog("File name too long");\r
1072 free(keyBlock);\r
1073 return 2;\r
1074 }\r
1075 \r
aea4d766 1076 if ( (f = fopen( filename , "r")) ) {\r
0c12504a 1077 while( fgets(buf, sizeof(buf), f) ){\r
99a71a0d 1078 if (strlen(buf) < 12 || buf[11] == '\n')\r
1079 continue;\r
aea4d766 1080 \r
99a71a0d 1081 while (fgetc(f) != '\n' && !feof(f)) ; //goto next line\r
1082 \r
9492e0b0 1083 if( buf[0]=='#' ) continue; //The line start with # is comment, skip\r
9ca155ba 1084\r
99a71a0d 1085 if (!isxdigit(buf[0])){\r
1086 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);\r
aea4d766 1087 continue;\r
1088 }\r
1089 \r
99a71a0d 1090 buf[12] = 0;\r
aea4d766 1091\r
1092 if ( stKeyBlock - keycnt < 2) {\r
1093 p = realloc(keyBlock, 6*(stKeyBlock+=10));\r
1094 if (!p) {\r
1095 PrintAndLog("Cannot allocate memory for defKeys");\r
1096 free(keyBlock);\r
1097 return 2;\r
1098 }\r
1099 keyBlock = p;\r
1100 }\r
1101 memset(keyBlock + 6 * keycnt, 0, 6);\r
99a71a0d 1102 num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);\r
125a98a1 1103 PrintAndLog("chk custom key[%d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r
aea4d766 1104 keycnt++;\r
0c12504a 1105 memset(buf, 0, sizeof(buf));\r
aea4d766 1106 }\r
aea4d766 1107 } else {\r
1108 PrintAndLog("File: %s: not found or locked.", filename);\r
1109 free(keyBlock);\r
1110 return 1;\r
1111 fclose(f);\r
1112 }\r
9ca155ba 1113 }\r
9ca155ba
M
1114 }\r
1115 \r
1116 if (keycnt == 0) {\r
aea4d766 1117 PrintAndLog("No key specified,try default keys");\r
0c12504a 1118 for (;keycnt < defaultKeysSize; keycnt++)\r
aea4d766 1119 PrintAndLog("chk default key[%d] %02x%02x%02x%02x%02x%02x", keycnt,\r
1120 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r
1121 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r
9ca155ba 1122 }\r
9ca155ba 1123 \r
aea4d766 1124 for ( int t = !keyType ; t < 2 ; keyType==2?(t++):(t=2) ) {\r
1125 int b=blockNo;\r
1126 for (int i=0; i<SectorsCnt; ++i) {\r
1127 PrintAndLog("--SectorsCnt:%d block no:0x%02x key type:%C key count:%d ", i, b, t?'B':'A', keycnt);\r
9492e0b0 1128 uint32_t max_keys = keycnt>USB_CMD_DATA_SIZE/6?USB_CMD_DATA_SIZE/6:keycnt;\r
1129 for (uint32_t c = 0; c < keycnt; c+=max_keys) {\r
1130 uint32_t size = keycnt-c>max_keys?max_keys:keycnt-c;\r
1131 res = mfCheckKeys(b, t, size, &keyBlock[6*c], &key64);\r
aea4d766 1132 if (res !=1) {\r
1133 if (!res) {\r
125a98a1 1134 PrintAndLog("Found valid key:[%012"llx"]",key64);\r
aea4d766 1135 if (transferToEml) {\r
1136 uint8_t block[16];\r
1137 mfEmlGetMem(block, get_trailer_block(b), 1);\r
1138 num_to_bytes(key64, 6, block + t*10);\r
1139 mfEmlSetMem(block, get_trailer_block(b), 1);\r
1140 }\r
aea4d766 1141 }\r
1142 } else {\r
1143 PrintAndLog("Command execute timeout");\r
1144 }\r
1145 }\r
1146 b<127?(b+=4):(b+=16); \r
1147 }\r
9ca155ba 1148 }\r
aea4d766 1149 \r
1150 free(keyBlock);\r
9ca155ba 1151\r
aea4d766 1152/*\r
1153 // Create dump file\r
1154 if (createDumpFile) {\r
1155 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) { \r
1156 PrintAndLog("Could not create file dumpkeys.bin");\r
1157 free(e_sector);\r
1158 return 1;\r
1159 }\r
1160 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");\r
1161 for(i=0; i<16; i++) {\r
1162 if (e_sector[i].foundKey[0]){\r
1163 num_to_bytes(e_sector[i].Key[0], 6, tempkey);\r
1164 fwrite ( tempkey, 1, 6, fkeys );\r
1165 }\r
1166 else{\r
1167 fwrite ( &standart, 1, 6, fkeys );\r
1168 }\r
1169 }\r
1170 for(i=0; i<16; i++) {\r
1171 if (e_sector[i].foundKey[1]){\r
1172 num_to_bytes(e_sector[i].Key[1], 6, tempkey);\r
1173 fwrite ( tempkey, 1, 6, fkeys );\r
1174 }\r
1175 else{\r
1176 fwrite ( &standart, 1, 6, fkeys );\r
1177 }\r
1178 }\r
1179 fclose(fkeys);\r
1180 }\r
1181*/\r
9ca155ba
M
1182 return 0;\r
1183}\r
1184\r
1185int CmdHF14AMf1kSim(const char *Cmd)\r
1186{\r
7bc95e2e 1187 uint8_t uid[7] = {0, 0, 0, 0, 0, 0, 0};\r
d2f487af 1188 uint8_t exitAfterNReads = 0;\r
1189 uint8_t flags = 0;\r
1190\r
9ca155ba 1191 if (param_getchar(Cmd, 0) == 'h') {\r
d2f487af 1192 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");\r
1193 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");\r
1194 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");\r
1195 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");\r
1196 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");\r
b03c0f2d 1197 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");\r
9ca155ba 1198 return 0;\r
9ca155ba 1199 }\r
d2f487af 1200 uint8_t pnr = 0;\r
1201 if (param_getchar(Cmd, pnr) == 'u') {\r
1202 if(param_gethex(Cmd, pnr+1, uid, 8) == 0)\r
1203 {\r
1204 flags |=FLAG_4B_UID_IN_DATA; // UID from packet\r
1205 }else if(param_gethex(Cmd,pnr+1,uid,14) == 0)\r
1206 {\r
1207 flags |= FLAG_7B_UID_IN_DATA;// UID from packet\r
1208 }else\r
1209 {\r
1210 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");\r
1211 return 1;\r
1212 }\r
1213 pnr +=2;\r
1214 }\r
1215 if (param_getchar(Cmd, pnr) == 'n') {\r
1216 exitAfterNReads = param_get8(Cmd,pnr+1);\r
1217 pnr += 2;\r
1218 }\r
1219 if (param_getchar(Cmd, pnr) == 'i' ) {\r
1220 //Using a flag to signal interactiveness, least significant bit\r
1221 flags |= FLAG_INTERACTIVE;\r
1222 pnr++;\r
1223 }\r
1224\r
1225 if (param_getchar(Cmd, pnr) == 'x' ) {\r
1226 //Using a flag to signal interactiveness, least significant bit\r
1227 flags |= FLAG_NR_AR_ATTACK;\r
1228 }\r
1229 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",\r
1230 flags & FLAG_4B_UID_IN_DATA ? sprint_hex(uid,4):\r
1231 flags & FLAG_7B_UID_IN_DATA ? sprint_hex(uid,7): "N/A"\r
1232 , exitAfterNReads, flags,flags);\r
1233\r
1234\r
1235 UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {flags, exitAfterNReads,0}};\r
1236 memcpy(c.d.asBytes, uid, sizeof(uid));\r
9ca155ba
M
1237 SendCommand(&c);\r
1238\r
7bc95e2e 1239 if(flags & FLAG_INTERACTIVE)\r
d2f487af 1240 {\r
1241 UsbCommand resp;\r
1242 PrintAndLog("Press pm3-button to abort simulation");\r
1243 while(! WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
1244 //We're waiting only 1.5 s at a time, otherwise we get the\r
1245 // annoying message about "Waiting for a response... "\r
1246 }\r
1247 }\r
1248\r
9ca155ba
M
1249 return 0;\r
1250}\r
1251\r
1252int CmdHF14AMfDbg(const char *Cmd)\r
1253{\r
8556b852
M
1254 int dbgMode = param_get32ex(Cmd, 0, 0, 10);\r
1255 if (dbgMode > 4) {\r
1256 PrintAndLog("Max debud mode parameter is 4 \n");\r
1257 }\r
1258\r
1259 if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {\r
9ca155ba
M
1260 PrintAndLog("Usage: hf mf dbg <debug level>");\r
1261 PrintAndLog(" 0 - no debug messages");\r
1262 PrintAndLog(" 1 - error messages");\r
b03c0f2d 1263 PrintAndLog(" 2 - plus information messages");\r
1264 PrintAndLog(" 3 - plus debug messages");\r
1265 PrintAndLog(" 4 - print even debug messages in timing critical functions");\r
1266 PrintAndLog(" Note: this option therefore may cause malfunction itself");\r
9ca155ba
M
1267 return 0;\r
1268 } \r
1269\r
8556b852
M
1270 UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};\r
1271 SendCommand(&c);\r
1272\r
9ca155ba
M
1273 return 0;\r
1274}\r
1275\r
1276int CmdHF14AMfEGet(const char *Cmd)\r
1277{\r
8556b852
M
1278 uint8_t blockNo = 0;\r
1279 uint8_t data[3 * 16];\r
1280 int i;\r
1281\r
1282 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1283 PrintAndLog("Usage: hf mf eget <block number>");\r
1284 PrintAndLog(" sample: hf mf eget 0 ");\r
1285 return 0;\r
1286 } \r
1287 \r
1288 blockNo = param_get8(Cmd, 0);\r
8556b852
M
1289\r
1290 PrintAndLog(" ");\r
1291 if (!mfEmlGetMem(data, blockNo, 3)) {\r
1292 for (i = 0; i < 3; i++) {\r
1293 PrintAndLog("data[%d]:%s", blockNo + i, sprint_hex(data + i * 16, 16));\r
1294 }\r
1295 } else {\r
1296 PrintAndLog("Command execute timeout");\r
1297 }\r
1298\r
1299 return 0;\r
1300}\r
1301\r
1302int CmdHF14AMfEClear(const char *Cmd)\r
1303{\r
1304 if (param_getchar(Cmd, 0) == 'h') {\r
1305 PrintAndLog("Usage: hf mf eclr");\r
1306 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");\r
1307 return 0;\r
1308 } \r
1309\r
1310 UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};\r
1311 SendCommand(&c);\r
9ca155ba
M
1312 return 0;\r
1313}\r
1314\r
1315int CmdHF14AMfESet(const char *Cmd)\r
1316{\r
8556b852
M
1317 uint8_t memBlock[16];\r
1318 uint8_t blockNo = 0;\r
1319\r
1320 memset(memBlock, 0x00, sizeof(memBlock));\r
1321\r
1322 if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {\r
1323 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");\r
1324 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");\r
1325 return 0;\r
1326 } \r
1327 \r
1328 blockNo = param_get8(Cmd, 0);\r
8556b852
M
1329 \r
1330 if (param_gethex(Cmd, 1, memBlock, 32)) {\r
1331 PrintAndLog("block data must include 32 HEX symbols");\r
1332 return 1;\r
1333 }\r
1334 \r
1335 // 1 - blocks count\r
1336 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};\r
1337 memcpy(c.d.asBytes, memBlock, 16);\r
1338 SendCommand(&c);\r
9ca155ba
M
1339 return 0;\r
1340}\r
1341\r
1342int CmdHF14AMfELoad(const char *Cmd)\r
1343{\r
ab8b654e
M
1344 FILE * f;\r
1345 char filename[20];\r
1346 char * fnameptr = filename;\r
1347 char buf[64];\r
1348 uint8_t buf8[64];\r
1349 int i, len, blockNum;\r
1350 \r
1351 memset(filename, 0, sizeof(filename));\r
1352 memset(buf, 0, sizeof(buf));\r
1353\r
aea4d766 1354 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {\r
ab8b654e
M
1355 PrintAndLog("It loads emul dump from the file `filename.eml`");\r
1356 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");\r
1357 PrintAndLog(" sample: hf mf eload filename");\r
1358 return 0;\r
1359 } \r
1360\r
1361 len = strlen(Cmd);\r
1362 if (len > 14) len = 14;\r
ab8b654e
M
1363\r
1364 memcpy(filename, Cmd, len);\r
1365 fnameptr += len;\r
1366\r
1367 sprintf(fnameptr, ".eml"); \r
1368 \r
1369 // open file\r
1370 f = fopen(filename, "r");\r
1371 if (f == NULL) {\r
1372 PrintAndLog("File not found or locked.");\r
1373 return 1;\r
1374 }\r
1375 \r
1376 blockNum = 0;\r
1377 while(!feof(f)){\r
1378 memset(buf, 0, sizeof(buf));\r
759c16b3 1379 if (fgets(buf, sizeof(buf), f) == NULL) {\r
d2f487af 1380 if(blockNum == 16 * 4)\r
1381 {\r
1382 break;\r
1383 }\r
1384 PrintAndLog("File reading error.");\r
759c16b3 1385 return 2;\r
1386 }\r
aea4d766 1387\r
ab8b654e 1388 if (strlen(buf) < 32){\r
aea4d766 1389 if(strlen(buf) && feof(f))\r
1390 break;\r
ab8b654e
M
1391 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
1392 return 2;\r
1393 }\r
1394 for (i = 0; i < 32; i += 2)\r
1395 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
1396// PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));\r
1397\r
1398 if (mfEmlSetMem(buf8, blockNum, 1)) {\r
1399 PrintAndLog("Cant set emul block: %d", blockNum);\r
1400 return 3;\r
1401 }\r
1402 blockNum++;\r
1403 \r
aea4d766 1404 if (blockNum >= 32 * 4 + 8 * 16) break;\r
ab8b654e
M
1405 }\r
1406 fclose(f);\r
1407 \r
aea4d766 1408 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){\r
ab8b654e
M
1409 PrintAndLog("File content error. There must be 64 blocks");\r
1410 return 4;\r
1411 }\r
d2f487af 1412 PrintAndLog("Loaded %d blocks from file: %s", blockNum, filename);\r
9ca155ba
M
1413 return 0;\r
1414}\r
1415\r
1416int CmdHF14AMfESave(const char *Cmd)\r
1417{\r
ab8b654e
M
1418 FILE * f;\r
1419 char filename[20];\r
1420 char * fnameptr = filename;\r
1421 uint8_t buf[64];\r
1422 int i, j, len;\r
1423 \r
1424 memset(filename, 0, sizeof(filename));\r
1425 memset(buf, 0, sizeof(buf));\r
1426\r
1427 if (param_getchar(Cmd, 0) == 'h') {\r
1428 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");\r
1429 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");\r
1430 PrintAndLog(" sample: hf mf esave ");\r
1431 PrintAndLog(" hf mf esave filename");\r
1432 return 0;\r
1433 } \r
1434\r
1435 len = strlen(Cmd);\r
1436 if (len > 14) len = 14;\r
1437 \r
1438 if (len < 1) {\r
1439 // get filename\r
1440 if (mfEmlGetMem(buf, 0, 1)) {\r
1441 PrintAndLog("Cant get block: %d", 0);\r
1442 return 1;\r
1443 }\r
1444 for (j = 0; j < 7; j++, fnameptr += 2)\r
1445 sprintf(fnameptr, "%02x", buf[j]); \r
1446 } else {\r
1447 memcpy(filename, Cmd, len);\r
1448 fnameptr += len;\r
1449 }\r
1450\r
1451 sprintf(fnameptr, ".eml"); \r
1452 \r
1453 // open file\r
1454 f = fopen(filename, "w+");\r
1455\r
1456 // put hex\r
aea4d766 1457 for (i = 0; i < 32 * 4 + 8 * 16; i++) {\r
ab8b654e
M
1458 if (mfEmlGetMem(buf, i, 1)) {\r
1459 PrintAndLog("Cant get block: %d", i);\r
1460 break;\r
1461 }\r
1462 for (j = 0; j < 16; j++)\r
1463 fprintf(f, "%02x", buf[j]); \r
1464 fprintf(f,"\n");\r
1465 }\r
1466 fclose(f);\r
1467 \r
1468 PrintAndLog("Saved to file: %s", filename);\r
1469 \r
9ca155ba
M
1470 return 0;\r
1471}\r
1472\r
26fdb4ab 1473int CmdHF14AMfECFill(const char *Cmd)\r
1474{\r
8556b852
M
1475 uint8_t keyType = 0;\r
1476\r
1477 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
b03c0f2d 1478 PrintAndLog("Usage: hf mf ecfill <key A/B>");\r
1479 PrintAndLog("sample: hf mf ecfill A");\r
8556b852
M
1480 PrintAndLog("Card data blocks transfers to card emulator memory.");\r
1481 PrintAndLog("Keys must be laid in the simulator memory. \n");\r
1482 return 0;\r
1483 } \r
1484\r
1485 char ctmp = param_getchar(Cmd, 0);\r
1486 if (ctmp == 0x00) {\r
1487 PrintAndLog("Key type must be A or B");\r
1488 return 1;\r
1489 }\r
1490 if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r
1491\r
1492 UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {0, keyType, 0}};\r
1493 SendCommand(&c);\r
1494 return 0;\r
1495}\r
1496\r
26fdb4ab 1497int CmdHF14AMfEKeyPrn(const char *Cmd)\r
1498{\r
aea4d766 1499 int i,b=-1;\r
8556b852
M
1500 uint8_t data[16];\r
1501 uint64_t keyA, keyB;\r
1502 \r
1503 PrintAndLog("|---|----------------|----------------|");\r
1504 PrintAndLog("|sec|key A |key B |");\r
1505 PrintAndLog("|---|----------------|----------------|");\r
aea4d766 1506 for (i = 0; i < 40; i++) {\r
1507 b<127?(b+=4):(b+=16);\r
1508 if (mfEmlGetMem(data, b, 1)) {\r
1509 PrintAndLog("error get block %d", b);\r
8556b852
M
1510 break;\r
1511 }\r
1512 keyA = bytes_to_num(data, 6);\r
1513 keyB = bytes_to_num(data + 10, 6);\r
125a98a1 1514 PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB);\r
8556b852
M
1515 }\r
1516 PrintAndLog("|---|----------------|----------------|");\r
1517 \r
1518 return 0;\r
1519}\r
1520\r
0675f200
M
1521int CmdHF14AMfCSetUID(const char *Cmd)\r
1522{\r
1523 uint8_t wipeCard = 0;\r
1524 uint8_t uid[8];\r
1525 uint8_t oldUid[8];\r
1526 int res;\r
1527\r
1528 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1529 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");\r
1530 PrintAndLog("sample: hf mf csetuid 01020304 w");\r
1531 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");\r
1532 PrintAndLog("If you want wipe card then add 'w' into command line. \n");\r
1533 return 0;\r
1534 } \r
1535\r
1536 if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {\r
1537 PrintAndLog("UID must include 8 HEX symbols");\r
1538 return 1;\r
1539 }\r
1540\r
1541 char ctmp = param_getchar(Cmd, 1);\r
1542 if (ctmp == 'w' || ctmp == 'W') wipeCard = 1;\r
1543 \r
1544 PrintAndLog("--wipe card:%02x uid:%s", wipeCard, sprint_hex(uid, 4));\r
1545\r
1546 res = mfCSetUID(uid, oldUid, wipeCard);\r
1547 if (res) {\r
1548 PrintAndLog("Can't set UID. error=%d", res);\r
1549 return 1;\r
1550 }\r
1551 \r
1552 PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));\r
1553 return 0;\r
1554}\r
1555\r
1556int CmdHF14AMfCSetBlk(const char *Cmd)\r
1557{\r
f774db95
M
1558 uint8_t uid[8];\r
1559 uint8_t memBlock[16];\r
1560 uint8_t blockNo = 0;\r
1561 int res;\r
1562 memset(memBlock, 0x00, sizeof(memBlock));\r
1563\r
1564 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1565 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");\r
1566 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");\r
1567 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");\r
1568 PrintAndLog("If you want wipe card then add 'w' into command line. \n");\r
1569 return 0;\r
1570 } \r
1571\r
1572 blockNo = param_get8(Cmd, 0);\r
f774db95
M
1573\r
1574 if (param_gethex(Cmd, 1, memBlock, 32)) {\r
1575 PrintAndLog("block data must include 32 HEX symbols");\r
1576 return 1;\r
1577 }\r
1578\r
1579 PrintAndLog("--block number:%02x data:%s", blockNo, sprint_hex(memBlock, 16));\r
1580\r
208a0166 1581 res = mfCSetBlock(blockNo, memBlock, uid, 0, CSETBLOCK_SINGLE_OPER);\r
f774db95
M
1582 if (res) {\r
1583 PrintAndLog("Can't write block. error=%d", res);\r
1584 return 1;\r
1585 }\r
1586 \r
1587 PrintAndLog("UID:%s", sprint_hex(uid, 4));\r
0675f200
M
1588 return 0;\r
1589}\r
1590\r
1591int CmdHF14AMfCLoad(const char *Cmd)\r
1592{\r
208a0166
M
1593 FILE * f;\r
1594 char filename[20];\r
1595 char * fnameptr = filename;\r
1596 char buf[64];\r
1597 uint8_t buf8[64];\r
1598 uint8_t fillFromEmulator = 0;\r
1599 int i, len, blockNum, flags;\r
1600 \r
1601 memset(filename, 0, sizeof(filename));\r
1602 memset(buf, 0, sizeof(buf));\r
1603\r
1604 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {\r
1605 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");\r
1606 PrintAndLog("or from emulator memory (option `e`)");\r
1607 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");\r
1608 PrintAndLog(" or: hf mf cload e ");\r
1609 PrintAndLog(" sample: hf mf cload filename");\r
1610 return 0;\r
1611 } \r
1612\r
1613 char ctmp = param_getchar(Cmd, 0);\r
1614 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r
1615 \r
1616 if (fillFromEmulator) {\r
1617 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1618 for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {\r
1619 if (mfEmlGetMem(buf8, blockNum, 1)) {\r
1620 PrintAndLog("Cant get block: %d", blockNum);\r
1621 return 2;\r
1622 }\r
1623 \r
1624 if (blockNum == 2) flags = 0;\r
1625 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1626\r
1627 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r
1628 PrintAndLog("Cant set magic card block: %d", blockNum);\r
1629 return 3;\r
1630 }\r
1631 }\r
1632 return 0;\r
1633 } else {\r
1634 len = strlen(Cmd);\r
1635 if (len > 14) len = 14;\r
1636\r
1637 memcpy(filename, Cmd, len);\r
1638 fnameptr += len;\r
1639\r
1640 sprintf(fnameptr, ".eml"); \r
1641 \r
1642 // open file\r
1643 f = fopen(filename, "r");\r
1644 if (f == NULL) {\r
1645 PrintAndLog("File not found or locked.");\r
1646 return 1;\r
1647 }\r
1648 \r
1649 blockNum = 0;\r
1650 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1651 while(!feof(f)){\r
1652 memset(buf, 0, sizeof(buf));\r
759c16b3 1653 if (fgets(buf, sizeof(buf), f) == NULL) {\r
1654 PrintAndLog("File reading error.");\r
1655 return 2;\r
1656 }\r
208a0166
M
1657\r
1658 if (strlen(buf) < 32){\r
1659 if(strlen(buf) && feof(f))\r
1660 break;\r
1661 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
1662 return 2;\r
1663 }\r
1664 for (i = 0; i < 32; i += 2)\r
1665 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
1666\r
1667 if (blockNum == 2) flags = 0;\r
1668 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1669\r
1670 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r
1671 PrintAndLog("Cant set magic card block: %d", blockNum);\r
1672 return 3;\r
1673 }\r
1674 blockNum++;\r
1675 \r
1676 if (blockNum >= 16 * 4) break; // magic card type - mifare 1K\r
1677 }\r
1678 fclose(f);\r
1679 \r
1680 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){\r
1681 PrintAndLog("File content error. There must be 64 blocks");\r
1682 return 4;\r
1683 }\r
1684 PrintAndLog("Loaded from file: %s", filename);\r
1685 return 0;\r
1686 }\r
0675f200
M
1687}\r
1688\r
545a1f38
M
1689int CmdHF14AMfCGetBlk(const char *Cmd) {\r
1690 uint8_t memBlock[16];\r
1691 uint8_t blockNo = 0;\r
1692 int res;\r
1693 memset(memBlock, 0x00, sizeof(memBlock));\r
1694\r
1695 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1696 PrintAndLog("Usage: hf mf cgetblk <block number>");\r
1697 PrintAndLog("sample: hf mf cgetblk 1");\r
1698 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");\r
1699 return 0;\r
1700 } \r
1701\r
1702 blockNo = param_get8(Cmd, 0);\r
545a1f38
M
1703\r
1704 PrintAndLog("--block number:%02x ", blockNo);\r
1705\r
1706 res = mfCGetBlock(blockNo, memBlock, CSETBLOCK_SINGLE_OPER);\r
1707 if (res) {\r
1708 PrintAndLog("Can't read block. error=%d", res);\r
1709 return 1;\r
1710 }\r
1711 \r
1712 PrintAndLog("block data:%s", sprint_hex(memBlock, 16));\r
1713 return 0;\r
1714}\r
1715\r
1716int CmdHF14AMfCGetSc(const char *Cmd) {\r
1717 uint8_t memBlock[16];\r
1718 uint8_t sectorNo = 0;\r
1719 int i, res, flags;\r
1720 memset(memBlock, 0x00, sizeof(memBlock));\r
1721\r
1722 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1723 PrintAndLog("Usage: hf mf cgetsc <sector number>");\r
1724 PrintAndLog("sample: hf mf cgetsc 0");\r
1725 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");\r
1726 return 0;\r
1727 } \r
1728\r
1729 sectorNo = param_get8(Cmd, 0);\r
1730 if (sectorNo > 15) {\r
1731 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");\r
1732 return 1;\r
1733 }\r
1734\r
1735 PrintAndLog("--sector number:%02x ", sectorNo);\r
1736\r
1737 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1738 for (i = 0; i < 4; i++) {\r
1739 if (i == 1) flags = 0;\r
1740 if (i == 3) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1741\r
1742 res = mfCGetBlock(sectorNo * 4 + i, memBlock, flags);\r
1743 if (res) {\r
1744 PrintAndLog("Can't read block. %02x error=%d", sectorNo * 4 + i, res);\r
1745 return 1;\r
1746 }\r
1747 \r
1748 PrintAndLog("block %02x data:%s", sectorNo * 4 + i, sprint_hex(memBlock, 16));\r
1749 }\r
1750 return 0;\r
1751}\r
1752\r
1753int CmdHF14AMfCSave(const char *Cmd) {\r
1754\r
1755 FILE * f;\r
1756 char filename[20];\r
1757 char * fnameptr = filename;\r
1758 uint8_t fillFromEmulator = 0;\r
1759 uint8_t buf[64];\r
1760 int i, j, len, flags;\r
1761 \r
1762 memset(filename, 0, sizeof(filename));\r
1763 memset(buf, 0, sizeof(buf));\r
1764\r
1765 if (param_getchar(Cmd, 0) == 'h') {\r
1766 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");\r
1767 PrintAndLog("or into emulator memory (option `e`)");\r
1768 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");\r
1769 PrintAndLog(" sample: hf mf esave ");\r
1770 PrintAndLog(" hf mf esave filename");\r
1771 PrintAndLog(" hf mf esave e \n");\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 // put into emulator\r
1780 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1781 for (i = 0; i < 16 * 4; i++) {\r
1782 if (i == 1) flags = 0;\r
1783 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1784 \r
1785 if (mfCGetBlock(i, buf, flags)) {\r
1786 PrintAndLog("Cant get block: %d", i);\r
1787 break;\r
1788 }\r
1789 \r
1790 if (mfEmlSetMem(buf, i, 1)) {\r
1791 PrintAndLog("Cant set emul block: %d", i);\r
1792 return 3;\r
1793 }\r
1794 }\r
1795 return 0;\r
1796 } else {\r
1797 len = strlen(Cmd);\r
1798 if (len > 14) len = 14;\r
1799 \r
1800 if (len < 1) {\r
1801 // get filename\r
1802 if (mfCGetBlock(0, buf, CSETBLOCK_SINGLE_OPER)) {\r
1803 PrintAndLog("Cant get block: %d", 0);\r
1804 return 1;\r
1805 }\r
1806 for (j = 0; j < 7; j++, fnameptr += 2)\r
1807 sprintf(fnameptr, "%02x", buf[j]); \r
1808 } else {\r
1809 memcpy(filename, Cmd, len);\r
1810 fnameptr += len;\r
1811 }\r
1812\r
1813 sprintf(fnameptr, ".eml"); \r
1814 \r
1815 // open file\r
1816 f = fopen(filename, "w+");\r
1817\r
1818 // put hex\r
1819 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1820 for (i = 0; i < 16 * 4; i++) {\r
1821 if (i == 1) flags = 0;\r
1822 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1823 \r
1824 if (mfCGetBlock(i, buf, flags)) {\r
1825 PrintAndLog("Cant get block: %d", i);\r
1826 break;\r
1827 }\r
1828 for (j = 0; j < 16; j++)\r
1829 fprintf(f, "%02x", buf[j]); \r
1830 fprintf(f,"\n");\r
1831 }\r
1832 fclose(f);\r
1833 \r
1834 PrintAndLog("Saved to file: %s", filename);\r
1835 \r
1836 return 0;\r
1837 }\r
1838}\r
1839\r
b62a5a84 1840int CmdHF14AMfSniff(const char *Cmd){\r
55acbb2a 1841 // params\r
c948cbde
M
1842 bool wantLogToFile = 0;\r
1843 bool wantDecrypt = 0;\r
eede7162 1844 //bool wantSaveToEml = 0; TODO\r
55acbb2a
M
1845 bool wantSaveToEmlFile = 0;\r
1846\r
1847 //var \r
39864b0b
M
1848 int res = 0;\r
1849 int len = 0;\r
1850 int blockLen = 0;\r
1851 int num = 0;\r
1852 int pckNum = 0;\r
991f13f2 1853 uint8_t uid[7];\r
1854 uint8_t uid_len;\r
39864b0b
M
1855 uint8_t atqa[2];\r
1856 uint8_t sak;\r
1857 bool isTag;\r
71d90e54 1858 uint32_t parity;\r
39864b0b
M
1859 uint8_t buf[3000];\r
1860 uint8_t * bufPtr = buf;\r
1861 memset(buf, 0x00, 3000);\r
b62a5a84
M
1862 \r
1863 if (param_getchar(Cmd, 0) == 'h') {\r
55acbb2a
M
1864 PrintAndLog("It continuously get data from the field and saves it to: log, emulator, emulator file.");\r
1865 PrintAndLog("You can specify:");\r
1866 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");\r
1867 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");\r
1868 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");\r
e0c635d1 1869 PrintAndLog(" r - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");\r
55acbb2a
M
1870 PrintAndLog("Usage: hf mf sniff [l][d][e][r]");\r
1871 PrintAndLog(" sample: hf mf sniff l d e");\r
b62a5a84
M
1872 return 0;\r
1873 } \r
1874 \r
55acbb2a
M
1875 for (int i = 0; i < 4; i++) {\r
1876 char ctmp = param_getchar(Cmd, i);\r
1877 if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;\r
1878 if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;\r
eede7162 1879 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO\r
55acbb2a
M
1880 if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;\r
1881 }\r
1882 \r
39864b0b
M
1883 printf("-------------------------------------------------------------------------\n");\r
1884 printf("Executing command. \n");\r
1885 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r
1886 printf("Press the key on pc keyboard to abort the client.\n");\r
1887 printf("-------------------------------------------------------------------------\n");\r
1888\r
d714d3ef 1889 UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};\r
1890 clearCommandBuffer();\r
1891 SendCommand(&c);\r
b62a5a84 1892\r
39864b0b
M
1893 // wait cycle\r
1894 while (true) {\r
1895 printf(".");\r
1896 fflush(stdout);\r
1897 if (ukbhit()) {\r
1898 getchar();\r
1899 printf("\naborted via keyboard!\n");\r
1900 break;\r
1901 }\r
1902 \r
902cb3c0 1903 UsbCommand resp;\r
1904 if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {\r
1905 res = resp.arg[0] & 0xff;\r
1906 len = resp.arg[1];\r
1907 num = resp.arg[2];\r
39864b0b
M
1908 \r
1909 if (res == 0) return 0;\r
1910 if (res == 1) {\r
1911 if (num ==0) {\r
1912 bufPtr = buf;\r
1913 memset(buf, 0x00, 3000);\r
1914 }\r
902cb3c0 1915 memcpy(bufPtr, resp.d.asBytes, len);\r
39864b0b
M
1916 bufPtr += len;\r
1917 pckNum++;\r
1918 }\r
1919 if (res == 2) {\r
1920 blockLen = bufPtr - buf;\r
1921 bufPtr = buf;\r
1922 printf(">\n");\r
1923 PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);\r
1924 num = 0;\r
1925 while (bufPtr - buf + 9 < blockLen) {\r
7bc95e2e 1926 isTag = bufPtr[3] & 0x80 ? true:false;\r
71d90e54
M
1927 bufPtr += 4;\r
1928 parity = *((uint32_t *)(bufPtr));\r
1929 bufPtr += 4;\r
39864b0b
M
1930 len = bufPtr[0];\r
1931 bufPtr++;\r
991f13f2 1932 if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff)) {\r
39864b0b
M
1933 memcpy(uid, bufPtr + 2, 7);\r
1934 memcpy(atqa, bufPtr + 2 + 7, 2);\r
991f13f2 1935 uid_len = (atqa[0] & 0xC0) == 0x40 ? 7 : 4;\r
39864b0b 1936 sak = bufPtr[11];\r
55acbb2a 1937 \r
991f13f2 1938 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x", \r
1939 sprint_hex(uid + (7 - uid_len), uid_len),\r
1940 atqa[1], \r
1941 atqa[0], \r
1942 sak);\r
d714d3ef 1943 if (wantLogToFile || wantDecrypt) {\r
991f13f2 1944 FillFileNameByUID(logHexFileName, uid + (7 - uid_len), ".log", uid_len);\r
55acbb2a
M
1945 AddLogCurrentDT(logHexFileName);\r
1946 } \r
e0c635d1 1947 if (wantDecrypt) mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);\r
39864b0b
M
1948 } else {\r
1949 PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));\r
55acbb2a 1950 if (wantLogToFile) AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);\r
71d90e54 1951 if (wantDecrypt) mfTraceDecode(bufPtr, len, parity, wantSaveToEmlFile);\r
39864b0b
M
1952 }\r
1953 bufPtr += len;\r
1954 num++;\r
1955 }\r
1956 }\r
1957 } // resp not NILL\r
1958 } // while (true)\r
d714d3ef 1959 \r
1960 return 0;\r
b62a5a84
M
1961}\r
1962\r
26fdb4ab 1963static command_t CommandTable[] =\r
9ca155ba 1964{\r
d2f487af 1965 {"help", CmdHelp, 1, "This help"},\r
1966 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},\r
1967 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},\r
1968 {"urdbl", CmdHF14AMfURdBl, 0, "Read MIFARE Ultralight block"},\r
1969 {"urdcard", CmdHF14AMfURdCard, 0,"Read MIFARE Ultralight Card"},\r
1970 {"uwrbl", CmdHF14AMfUWrBl, 0,"Write MIFARE Ultralight block"},\r
1971 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},\r
1972 {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},\r
1973 {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},\r
9ca155ba 1974 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},\r
545a1f38 1975 {"chk", CmdHF14AMfChk, 0, "Test block keys"},\r
7bc95e2e 1976 {"mifare", CmdHF14AMifare, 0, "Read parity error messages."},\r
9ca155ba 1977 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},\r
b62a5a84 1978 {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},\r
545a1f38
M
1979 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},\r
1980 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},\r
51969283
M
1981 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},\r
1982 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},\r
9ca155ba
M
1983 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},\r
1984 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},\r
8556b852
M
1985 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},\r
1986 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},\r
0675f200 1987 {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},\r
f774db95 1988 {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block into magic Chinese card"},\r
545a1f38
M
1989 {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block from magic Chinese card"},\r
1990 {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector from magic Chinese card"},\r
208a0166 1991 {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},\r
545a1f38 1992 {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},\r
9ca155ba
M
1993 {NULL, NULL, 0, NULL}\r
1994};\r
1995\r
1996int CmdHFMF(const char *Cmd)\r
1997{\r
1998 // flush\r
7dd1908b 1999 WaitForResponseTimeout(CMD_ACK,NULL,100);\r
9ca155ba
M
2000\r
2001 CmdsParse(CommandTable, Cmd);\r
2002 return 0;\r
2003}\r
2004\r
2005int CmdHelp(const char *Cmd)\r
2006{\r
2007 CmdsHelp(CommandTable);\r
2008 return 0;\r
2009}\r
Impressum, Datenschutz