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