]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhfmf.c
Removed the -ldl switch from r763, so it only is added for Linux (it apparently cause...
[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
140 return 0;\r
141}\r
142\r
143int CmdHF14AMfRdBl(const char *Cmd)\r
144{\r
145 uint8_t blockNo = 0;\r
146 uint8_t keyType = 0;\r
147 uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
148 \r
149 char cmdp = 0x00;\r
150\r
151\r
152 if (strlen(Cmd)<3) {\r
153 PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");\r
154 PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");\r
155 return 0;\r
156 } \r
157 \r
158 blockNo = param_get8(Cmd, 0);\r
159 cmdp = param_getchar(Cmd, 1);\r
160 if (cmdp == 0x00) {\r
161 PrintAndLog("Key type must be A or B");\r
162 return 1;\r
163 }\r
164 if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r
165 if (param_gethex(Cmd, 2, key, 12)) {\r
166 PrintAndLog("Key must include 12 HEX symbols");\r
167 return 1;\r
168 }\r
169 PrintAndLog("--block no:%02x key type:%02x key:%s ", blockNo, keyType, sprint_hex(key, 6));\r
170 \r
171 UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};\r
172 memcpy(c.d.asBytes, key, 6);\r
173 SendCommand(&c);\r
9ca155ba 174\r
902cb3c0 175 UsbCommand resp;\r
176 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
177 uint8_t isOK = resp.arg[0] & 0xff;\r
178 uint8_t * data = resp.d.asBytes;\r
9ca155ba
M
179\r
180 if (isOK)\r
181 PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 16));\r
182 else\r
183 PrintAndLog("isOk:%02x", isOK);\r
184 } else {\r
185 PrintAndLog("Command execute timeout");\r
186 }\r
187\r
188 return 0;\r
189}\r
190\r
191int CmdHF14AMfRdSc(const char *Cmd)\r
192{\r
193 int i;\r
194 uint8_t sectorNo = 0;\r
195 uint8_t keyType = 0;\r
196 uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
197 \r
198 uint8_t isOK = 0;\r
199 uint8_t * data = NULL;\r
200\r
201 char cmdp = 0x00;\r
202\r
203 if (strlen(Cmd)<3) {\r
204 PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");\r
205 PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");\r
206 return 0;\r
207 } \r
208 \r
209 sectorNo = param_get8(Cmd, 0);\r
210 if (sectorNo > 63) {\r
211 PrintAndLog("Sector number must be less than 64");\r
212 return 1;\r
213 }\r
214 cmdp = param_getchar(Cmd, 1);\r
215 if (cmdp == 0x00) {\r
216 PrintAndLog("Key type must be A or B");\r
217 return 1;\r
218 }\r
219 if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r
220 if (param_gethex(Cmd, 2, key, 12)) {\r
221 PrintAndLog("Key must include 12 HEX symbols");\r
222 return 1;\r
223 }\r
224 PrintAndLog("--sector no:%02x key type:%02x key:%s ", sectorNo, keyType, sprint_hex(key, 6));\r
225 \r
226 UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};\r
227 memcpy(c.d.asBytes, key, 6);\r
228 SendCommand(&c);\r
9ca155ba
M
229 PrintAndLog(" ");\r
230\r
902cb3c0 231 UsbCommand resp;\r
232 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
233 isOK = resp.arg[0] & 0xff;\r
234 data = resp.d.asBytes;\r
9ca155ba
M
235\r
236 PrintAndLog("isOk:%02x", isOK);\r
237 if (isOK) \r
238 for (i = 0; i < 2; i++) {\r
239 PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));\r
240 }\r
241 } else {\r
242 PrintAndLog("Command1 execute timeout");\r
243 }\r
244\r
902cb3c0 245 // response2\r
9ca155ba 246 PrintAndLog(" ");\r
902cb3c0 247 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
248 isOK = resp.arg[0] & 0xff;\r
249 data = resp.d.asBytes;\r
9ca155ba
M
250\r
251 if (isOK) \r
252 for (i = 0; i < 2; i++) {\r
253 PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));\r
254 }\r
255 } else {\r
256 PrintAndLog("Command2 execute timeout");\r
257 }\r
258 \r
259 return 0;\r
260}\r
261\r
aea4d766 262int CmdHF14AMfDump(const char *Cmd)\r
26fdb4ab 263{\r
264 int i, j;\r
265 \r
aea4d766 266 uint8_t keyA[40][6];\r
267 uint8_t keyB[40][6];\r
268 uint8_t rights[40][4];\r
26fdb4ab 269 \r
26fdb4ab 270 FILE *fin;\r
271 FILE *fout;\r
272 \r
902cb3c0 273 UsbCommand resp;\r
26fdb4ab 274 \r
275 if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {\r
aea4d766 276 PrintAndLog("Could not find file dumpkeys.bin");\r
26fdb4ab 277 return 1;\r
278 }\r
279 \r
280 if ((fout = fopen("dumpdata.bin","wb")) == NULL) { \r
aea4d766 281 PrintAndLog("Could not create file name dumpdata.bin");\r
26fdb4ab 282 return 1;\r
283 }\r
284 \r
285 // Read key file\r
286 \r
287 for (i=0 ; i<16 ; i++) {\r
759c16b3 288 if (fread( keyA[i], 1, 6, fin ) == 0) {\r
289 PrintAndLog("File reading error.");\r
290 return 2;\r
291 }\r
26fdb4ab 292 }\r
293 for (i=0 ; i<16 ; i++) {\r
759c16b3 294 if (fread( keyB[i], 1, 6, fin ) == 0) {\r
295 PrintAndLog("File reading error.");\r
296 return 2;\r
297 }\r
26fdb4ab 298 }\r
299 \r
300 // Read access rights to sectors\r
301 \r
302 PrintAndLog("|-----------------------------------------|");\r
303 PrintAndLog("|------ Reading sector access bits...-----|");\r
304 PrintAndLog("|-----------------------------------------|");\r
305 \r
306 for (i = 0 ; i < 16 ; i++) {\r
307 UsbCommand c = {CMD_MIFARE_READBL, {4*i + 3, 0, 0}};\r
308 memcpy(c.d.asBytes, keyA[i], 6);\r
309 SendCommand(&c);\r
26fdb4ab 310\r
902cb3c0 311 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
312 uint8_t isOK = resp.arg[0] & 0xff;\r
313 uint8_t *data = resp.d.asBytes;\r
26fdb4ab 314 if (isOK){\r
315 rights[i][0] = ((data[7] & 0x10)>>4) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>2);\r
316 rights[i][1] = ((data[7] & 0x20)>>5) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>3);\r
317 rights[i][2] = ((data[7] & 0x40)>>6) | ((data[8] & 0x4)>>1) | ((data[8] & 0x40)>>4);\r
318 rights[i][3] = ((data[7] & 0x80)>>7) | ((data[8] & 0x8)>>2) | ((data[8] & 0x80)>>5);\r
319 }\r
320 else{\r
321 PrintAndLog("Could not get access rights for block %d", i);\r
322 }\r
323 }\r
324 else {\r
325 PrintAndLog("Command execute timeout");\r
326 }\r
327 }\r
328 \r
329 // Read blocks and print to file\r
330 \r
331 PrintAndLog("|-----------------------------------------|");\r
332 PrintAndLog("|----- Dumping all blocks to file... -----|");\r
333 PrintAndLog("|-----------------------------------------|");\r
334 \r
902cb3c0 335 \r
26fdb4ab 336 for (i=0 ; i<16 ; i++) {\r
337 for (j=0 ; j<4 ; j++) {\r
902cb3c0 338 bool received = false;\r
339 \r
340 if (j == 3){\r
26fdb4ab 341 UsbCommand c = {CMD_MIFARE_READBL, {i*4 + j, 0, 0}};\r
342 memcpy(c.d.asBytes, keyA[i], 6);\r
343 SendCommand(&c);\r
902cb3c0 344 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
26fdb4ab 345 }\r
346 else{\r
347 if ((rights[i][j] == 6) | (rights[i][j] == 5)) {\r
348 UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 1, 0}};\r
349 memcpy(c.d.asBytes, keyB[i], 6);\r
350 SendCommand(&c);\r
902cb3c0 351 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
26fdb4ab 352 }\r
353 else if (rights[i][j] == 7) {\r
354 PrintAndLog("Access rights do not allow reading of sector %d block %d",i,j);\r
355 }\r
356 else {\r
357 UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 0, 0}};\r
358 memcpy(c.d.asBytes, keyA[i], 6);\r
359 SendCommand(&c);\r
902cb3c0 360 received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
26fdb4ab 361 }\r
362 }\r
363\r
902cb3c0 364 if (received) {\r
365 uint8_t isOK = resp.arg[0] & 0xff;\r
366 uint8_t *data = resp.d.asBytes;\r
3d77fdfa 367 if (j == 3) {\r
368 data[0] = (keyA[i][0]);\r
369 data[1] = (keyA[i][1]);\r
370 data[2] = (keyA[i][2]);\r
371 data[3] = (keyA[i][3]);\r
372 data[4] = (keyA[i][4]);\r
373 data[5] = (keyA[i][5]);\r
374 data[10] = (keyB[i][0]);\r
375 data[11] = (keyB[i][1]);\r
376 data[12] = (keyB[i][2]);\r
377 data[13] = (keyB[i][3]);\r
378 data[14] = (keyB[i][4]);\r
379 data[15] = (keyB[i][5]);\r
380 }\r
26fdb4ab 381 if (isOK) {\r
382 fwrite ( data, 1, 16, fout );\r
383 }\r
384 else {\r
385 PrintAndLog("Could not get access rights for block %d", i);\r
386 }\r
387 }\r
388 else {\r
389 PrintAndLog("Command execute timeout");\r
390 }\r
391 }\r
392 }\r
393 \r
394 fclose(fin);\r
395 fclose(fout);\r
396 \r
397 return 0;\r
398}\r
399\r
aea4d766 400int CmdHF14AMfRestore(const char *Cmd)\r
26fdb4ab 401{\r
402\r
403 int i,j;\r
26fdb4ab 404 uint8_t keyType = 0;\r
405 uint8_t key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r
406 uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r
407 uint8_t keyA[16][6];\r
408 uint8_t keyB[16][6];\r
409 \r
26fdb4ab 410 FILE *fdump;\r
411 FILE *fkeys;\r
412 \r
26fdb4ab 413 if ((fdump = fopen("dumpdata.bin","rb")) == NULL) {\r
aea4d766 414 PrintAndLog("Could not find file dumpdata.bin");\r
26fdb4ab 415 return 1;\r
416 }\r
417 if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) {\r
aea4d766 418 PrintAndLog("Could not find file dumpkeys.bin");\r
26fdb4ab 419 return 1;\r
420 }\r
421 \r
422 for (i=0 ; i<16 ; i++) {\r
759c16b3 423 if (fread(keyA[i], 1, 6, fkeys) == 0) {\r
424 PrintAndLog("File reading error.");\r
425 return 2;\r
426 }\r
26fdb4ab 427 }\r
428 for (i=0 ; i<16 ; i++) {\r
759c16b3 429 if (fread(keyB[i], 1, 6, fkeys) == 0) {\r
430 PrintAndLog("File reading error.");\r
431 return 2;\r
432 }\r
26fdb4ab 433 }\r
434 \r
9d710943 435 PrintAndLog("Restoring dumpdata.bin to card");\r
26fdb4ab 436\r
437 for (i=0 ; i<16 ; i++) {\r
438 for( j=0 ; j<4 ; j++) {\r
439 UsbCommand c = {CMD_MIFARE_WRITEBL, {i*4 + j, keyType, 0}};\r
440 memcpy(c.d.asBytes, key, 6);\r
441 \r
759c16b3 442 if (fread(bldata, 1, 16, fdump) == 0) {\r
443 PrintAndLog("File reading error.");\r
444 return 2;\r
445 }\r
26fdb4ab 446 \r
447 if (j == 3) {\r
448 bldata[0] = (keyA[i][0]);\r
449 bldata[1] = (keyA[i][1]);\r
450 bldata[2] = (keyA[i][2]);\r
451 bldata[3] = (keyA[i][3]);\r
452 bldata[4] = (keyA[i][4]);\r
453 bldata[5] = (keyA[i][5]);\r
454 bldata[10] = (keyB[i][0]);\r
455 bldata[11] = (keyB[i][1]);\r
456 bldata[12] = (keyB[i][2]);\r
457 bldata[13] = (keyB[i][3]);\r
458 bldata[14] = (keyB[i][4]);\r
459 bldata[15] = (keyB[i][5]);\r
460 } \r
461 \r
6c064e16 462 PrintAndLog("Writing to block %2d: %s", i*4+j, sprint_hex(bldata, 16));\r
463 \r
464 /*\r
9d710943 465 PrintAndLog("Writing to block %2d: %s Confirm? [Y,N]", i*4+j, sprint_hex(bldata, 16));\r
466 \r
0f7f9edc 467 scanf("%c",&ch);\r
6c064e16 468 if ((ch != 'y') && (ch != 'Y')){\r
9d710943 469 PrintAndLog("Aborting !");\r
470 return 1;\r
471 }\r
6c064e16 472 */\r
26fdb4ab 473 \r
474 memcpy(c.d.asBytes + 10, bldata, 16);\r
475 SendCommand(&c);\r
26fdb4ab 476\r
902cb3c0 477 UsbCommand resp;\r
478 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
479 uint8_t isOK = resp.arg[0] & 0xff;\r
26fdb4ab 480 PrintAndLog("isOk:%02x", isOK);\r
481 } else {\r
482 PrintAndLog("Command execute timeout");\r
483 }\r
484 }\r
485 }\r
486 \r
487 fclose(fdump);\r
488 fclose(fkeys);\r
489 return 0;\r
490}\r
491\r
9ca155ba
M
492int CmdHF14AMfNested(const char *Cmd)\r
493{\r
494 int i, j, res, iterations;\r
495 sector * e_sector = NULL;\r
496 uint8_t blockNo = 0;\r
497 uint8_t keyType = 0;\r
498 uint8_t trgBlockNo = 0;\r
499 uint8_t trgKeyType = 0;\r
500 uint8_t blDiff = 0;\r
501 int SectorsCnt = 0;\r
502 uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r
9492e0b0 503 uint8_t keyBlock[6*6];\r
9ca155ba 504 uint64_t key64 = 0;\r
8556b852 505 int transferToEml = 0;\r
9ca155ba 506 \r
26fdb4ab 507 int createDumpFile = 0;\r
508 FILE *fkeys;\r
21156267 509 uint8_t standart[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r
510 uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r
26fdb4ab 511 \r
9ca155ba
M
512 char cmdp, ctmp;\r
513\r
514 if (strlen(Cmd)<3) {\r
515 PrintAndLog("Usage:");\r
26fdb4ab 516 PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");\r
0014cb46
M
517 PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");\r
518 PrintAndLog(" <target block number> <target key A/B> [t]");\r
9ca155ba 519 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r
8556b852 520 PrintAndLog("t - transfer keys into emulator memory");\r
26fdb4ab 521 PrintAndLog("d - write keys to binary file");\r
9ca155ba
M
522 PrintAndLog(" ");\r
523 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");\r
8556b852 524 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");\r
26fdb4ab 525 PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF d ");\r
9ca155ba
M
526 PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");\r
527 return 0;\r
528 } \r
529 \r
530 cmdp = param_getchar(Cmd, 0);\r
531 blockNo = param_get8(Cmd, 1);\r
532 ctmp = param_getchar(Cmd, 2);\r
533 if (ctmp == 0x00) {\r
534 PrintAndLog("Key type must be A or B");\r
535 return 1;\r
536 }\r
537 if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r
538 if (param_gethex(Cmd, 3, key, 12)) {\r
539 PrintAndLog("Key must include 12 HEX symbols");\r
540 return 1;\r
541 }\r
542 \r
8556b852 543 if (cmdp == 'o' || cmdp == 'O') {\r
9ca155ba
M
544 cmdp = 'o';\r
545 trgBlockNo = param_get8(Cmd, 4);\r
546 ctmp = param_getchar(Cmd, 5);\r
547 if (ctmp == 0x00) {\r
548 PrintAndLog("Target key type must be A or B");\r
549 return 1;\r
550 }\r
551 if (ctmp != 'A' && ctmp != 'a') trgKeyType = 1;\r
552 } else {\r
553 switch (cmdp) {\r
554 case '0': SectorsCnt = 05; break;\r
555 case '1': SectorsCnt = 16; break;\r
556 case '2': SectorsCnt = 32; break;\r
557 case '4': SectorsCnt = 64; break;\r
558 default: SectorsCnt = 16;\r
559 }\r
560 }\r
8556b852
M
561\r
562 ctmp = param_getchar(Cmd, 4);\r
21156267 563 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;\r
564 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;\r
565 \r
8556b852
M
566 ctmp = param_getchar(Cmd, 6);\r
567 transferToEml |= (ctmp == 't' || ctmp == 'T');\r
21156267 568 transferToEml |= (ctmp == 'd' || ctmp == 'D');\r
9ca155ba 569 \r
8556b852 570 PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo, keyType, sprint_hex(key, 6), transferToEml);\r
9ca155ba
M
571 if (cmdp == 'o')\r
572 PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo, trgKeyType);\r
573\r
574 if (cmdp == 'o') {\r
9492e0b0 575 if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, true)) {\r
9ca155ba
M
576 PrintAndLog("Nested error.");\r
577 return 2;\r
578 }\r
9492e0b0 579 key64 = bytes_to_num(keyBlock, 6);\r
580 if (key64) {\r
125a98a1 581 PrintAndLog("Found valid key:%012"llx, key64);\r
8556b852
M
582\r
583 // transfer key to the emulator\r
584 if (transferToEml) {\r
585 mfEmlGetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1);\r
586 \r
587 if (!trgKeyType)\r
588 num_to_bytes(key64, 6, keyBlock);\r
589 else\r
590 num_to_bytes(key64, 6, &keyBlock[10]);\r
591 mfEmlSetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1); \r
592 }\r
593 } else {\r
9ca155ba 594 PrintAndLog("No valid key found");\r
8556b852 595 }\r
21156267 596 }\r
597 else { // ------------------------------------ multiple sectors working\r
9492e0b0 598 clock_t time1;\r
599 time1 = clock();\r
600\r
9ca155ba
M
601 blDiff = blockNo % 4;\r
602 PrintAndLog("Block shift=%d", blDiff);\r
603 e_sector = calloc(SectorsCnt, sizeof(sector));\r
604 if (e_sector == NULL) return 1;\r
605 \r
606 //test current key 4 sectors\r
607 memcpy(keyBlock, key, 6);\r
9492e0b0 608 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 1 * 6));\r
609 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 2 * 6));\r
610 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 3 * 6));\r
611 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 4 * 6));\r
9ca155ba
M
612 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));\r
613\r
614 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);\r
615 for (i = 0; i < SectorsCnt; i++) {\r
616 for (j = 0; j < 2; j++) {\r
617 if (e_sector[i].foundKey[j]) continue;\r
618 \r
619 res = mfCheckKeys(i * 4 + blDiff, j, 6, keyBlock, &key64);\r
620 \r
621 if (!res) {\r
622 e_sector[i].Key[j] = key64;\r
623 e_sector[i].foundKey[j] = 1;\r
624 }\r
625 }\r
9492e0b0 626 }\r
627 \r
9ca155ba 628 \r
9ca155ba
M
629 // nested sectors\r
630 iterations = 0;\r
631 PrintAndLog("nested...");\r
9492e0b0 632 bool calibrate = true;\r
9ca155ba 633 for (i = 0; i < NESTED_SECTOR_RETRY; i++) {\r
9492e0b0 634 for (trgBlockNo = blDiff; trgBlockNo < SectorsCnt * 4; trgBlockNo = trgBlockNo + 4) {\r
9ca155ba
M
635 for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) { \r
636 if (e_sector[trgBlockNo / 4].foundKey[trgKeyType]) continue;\r
9492e0b0 637 PrintAndLog("-----------------------------------------------");\r
638 if(mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, calibrate)) {\r
639 PrintAndLog("Nested error.\n");\r
640 return 2;\r
641 }\r
642 else {\r
643 calibrate = false;\r
644 }\r
9ca155ba
M
645 \r
646 iterations++;\r
9492e0b0 647\r
648 key64 = bytes_to_num(keyBlock, 6);\r
649 if (key64) {\r
125a98a1 650 PrintAndLog("Found valid key:%012"llx, key64);\r
9ca155ba
M
651 e_sector[trgBlockNo / 4].foundKey[trgKeyType] = 1;\r
652 e_sector[trgBlockNo / 4].Key[trgKeyType] = key64;\r
653 }\r
654 }\r
9492e0b0 655 }\r
9ca155ba
M
656 }\r
657\r
9492e0b0 658 printf("Time in nested: %1.3f (%1.3f sec per key)\n\n", ((float)clock() - time1)/1000.0, ((float)clock() - time1)/iterations/1000.0);\r
659 \r
660 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations);\r
9ca155ba
M
661 //print them\r
662 PrintAndLog("|---|----------------|---|----------------|---|");\r
663 PrintAndLog("|sec|key A |res|key B |res|");\r
664 PrintAndLog("|---|----------------|---|----------------|---|");\r
665 for (i = 0; i < SectorsCnt; i++) {\r
125a98a1 666 PrintAndLog("|%03d| %012"llx" | %d | %012"llx" | %d |", i,\r
9ca155ba
M
667 e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);\r
668 }\r
669 PrintAndLog("|---|----------------|---|----------------|---|");\r
670 \r
8556b852
M
671 // transfer them to the emulator\r
672 if (transferToEml) {\r
673 for (i = 0; i < SectorsCnt; i++) {\r
674 mfEmlGetMem(keyBlock, i * 4 + 3, 1);\r
675 if (e_sector[i].foundKey[0])\r
ab8b654e 676 num_to_bytes(e_sector[i].Key[0], 6, keyBlock);\r
8556b852
M
677 if (e_sector[i].foundKey[1])\r
678 num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);\r
679 mfEmlSetMem(keyBlock, i * 4 + 3, 1);\r
680 } \r
681 }\r
682 \r
21156267 683 // Create dump file\r
26fdb4ab 684 if (createDumpFile) {\r
685 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) { \r
aea4d766 686 PrintAndLog("Could not create file dumpkeys.bin");\r
26fdb4ab 687 free(e_sector);\r
688 return 1;\r
689 }\r
21156267 690 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");\r
26fdb4ab 691 for(i=0; i<16; i++) {\r
21156267 692 if (e_sector[i].foundKey[0]){\r
693 num_to_bytes(e_sector[i].Key[0], 6, tempkey);\r
694 fwrite ( tempkey, 1, 6, fkeys );\r
695 }\r
696 else{\r
697 fwrite ( &standart, 1, 6, fkeys );\r
698 }\r
26fdb4ab 699 }\r
700 for(i=0; i<16; i++) {\r
21156267 701 if (e_sector[i].foundKey[1]){\r
702 num_to_bytes(e_sector[i].Key[1], 6, tempkey);\r
703 fwrite ( tempkey, 1, 6, fkeys );\r
704 }\r
705 else{\r
706 fwrite ( &standart, 1, 6, fkeys );\r
707 }\r
26fdb4ab 708 }\r
709 fclose(fkeys);\r
710 }\r
711 \r
9ca155ba
M
712 free(e_sector);\r
713 }\r
714\r
715 return 0;\r
716}\r
717\r
aea4d766 718static uint32_t\r
719get_trailer_block (uint32_t uiBlock)\r
720{\r
721 // Test if we are in the small or big sectors\r
722 uint32_t trailer_block = 0;\r
723 if (uiBlock < 128) {\r
724 trailer_block = uiBlock + (3 - (uiBlock % 4));\r
725 } else {\r
726 trailer_block = uiBlock + (15 - (uiBlock % 16));\r
727 }\r
728 return trailer_block;\r
729}\r
9ca155ba
M
730int CmdHF14AMfChk(const char *Cmd)\r
731{\r
aea4d766 732 FILE * f;\r
733 char filename[256]={0};\r
83613803 734 char buf[13];\r
aea4d766 735 uint8_t *keyBlock = NULL, *p;\r
736 uint8_t stKeyBlock = 20;\r
737 \r
9ca155ba
M
738 int i, res;\r
739 int keycnt = 0;\r
740 char ctmp = 0x00;\r
741 uint8_t blockNo = 0;\r
aea4d766 742 uint8_t SectorsCnt = 1;\r
9ca155ba 743 uint8_t keyType = 0;\r
9ca155ba 744 uint64_t key64 = 0;\r
aea4d766 745 \r
746 int transferToEml = 0;\r
747 int createDumpFile = 0;\r
9ca155ba 748\r
aea4d766 749 keyBlock = calloc(stKeyBlock, 6);\r
750 if (keyBlock == NULL) return 1;\r
751\r
752 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 0 * 6)); // Default key (first key used by program if no user defined key)\r
753 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 1 * 6)); // Blank key\r
754 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 2 * 6)); // NFCForum MAD key\r
755 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 3 * 6));\r
756 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 4 * 6));\r
757 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock + 5 * 6));\r
758 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock + 6 * 6));\r
759 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock + 7 * 6));\r
760 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock + 8 * 6));\r
761 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock + 9 * 6));\r
762 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock + 10 * 6));\r
763 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock + 11 * 6));\r
764 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock + 12 * 6));\r
765 \r
9ca155ba 766 if (strlen(Cmd)<3) {\r
aea4d766 767 PrintAndLog("Usage: hf mf chk <block number>/<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");\r
768 PrintAndLog(" * - all sectors");\r
769 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r
770// PrintAndLog("d - write keys to binary file\n");\r
771 \r
772 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");\r
773 PrintAndLog(" hf mf chk *1 ? t");\r
9ca155ba
M
774 return 0;\r
775 } \r
776 \r
aea4d766 777 if (param_getchar(Cmd, 0)=='*') {\r
778 blockNo = 3;\r
779 switch(param_getchar(Cmd+1, 0)) {\r
780 case '0': SectorsCnt = 5; break;\r
781 case '1': SectorsCnt = 16; break;\r
782 case '2': SectorsCnt = 32; break;\r
783 case '4': SectorsCnt = 40; break;\r
784 default: SectorsCnt = 16;\r
785 }\r
786 }\r
787 else\r
788 blockNo = param_get8(Cmd, 0);\r
789 \r
9ca155ba 790 ctmp = param_getchar(Cmd, 1);\r
aea4d766 791 switch (ctmp) { \r
792 case 'a': case 'A':\r
793 keyType = !0;\r
794 break;\r
795 case 'b': case 'B':\r
796 keyType = !1;\r
797 break;\r
798 case '?':\r
799 keyType = 2;\r
800 break;\r
801 default:\r
802 PrintAndLog("Key type must be A , B or ?");\r
9ca155ba 803 return 1;\r
aea4d766 804 };\r
9ca155ba 805 \r
aea4d766 806 ctmp = param_getchar(Cmd, 2);\r
807 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;\r
808 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;\r
809 \r
810 for (i = transferToEml || createDumpFile; param_getchar(Cmd, 2 + i); i++) {\r
811 if (!param_gethex(Cmd, 2 + i, keyBlock + 6 * keycnt, 12)) {\r
812 if ( stKeyBlock - keycnt < 2) {\r
813 p = realloc(keyBlock, 6*(stKeyBlock+=10));\r
814 if (!p) {\r
815 PrintAndLog("Cannot allocate memory for Keys");\r
816 free(keyBlock);\r
817 return 2;\r
818 }\r
819 keyBlock = p;\r
820 }\r
821 PrintAndLog("chk key[%d] %02x%02x%02x%02x%02x%02x", keycnt,\r
822 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r
823 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r
824 keycnt++;\r
825 } else {\r
826 // May be a dic file\r
827 if ( param_getstr(Cmd, 2 + i,filename) > 255 ) {\r
828 PrintAndLog("File name too long");\r
829 free(keyBlock);\r
830 return 2;\r
831 }\r
832 \r
aea4d766 833 if ( (f = fopen( filename , "r")) ) {\r
99a71a0d 834 while( !feof(f) ){\r
835 memset(buf, 0, sizeof(buf));\r
759c16b3 836 if (fgets(buf, sizeof(buf), f) == NULL) {\r
9492e0b0 837 PrintAndLog("File reading error.");\r
838 return 2;\r
839 }\r
759c16b3 840 \r
99a71a0d 841 if (strlen(buf) < 12 || buf[11] == '\n')\r
842 continue;\r
aea4d766 843 \r
99a71a0d 844 while (fgetc(f) != '\n' && !feof(f)) ; //goto next line\r
845 \r
9492e0b0 846 if( buf[0]=='#' ) continue; //The line start with # is comment, skip\r
9ca155ba 847\r
99a71a0d 848 if (!isxdigit(buf[0])){\r
849 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);\r
aea4d766 850 continue;\r
851 }\r
852 \r
99a71a0d 853 buf[12] = 0;\r
aea4d766 854\r
855 if ( stKeyBlock - keycnt < 2) {\r
856 p = realloc(keyBlock, 6*(stKeyBlock+=10));\r
857 if (!p) {\r
858 PrintAndLog("Cannot allocate memory for defKeys");\r
859 free(keyBlock);\r
860 return 2;\r
861 }\r
862 keyBlock = p;\r
863 }\r
864 memset(keyBlock + 6 * keycnt, 0, 6);\r
99a71a0d 865 num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);\r
125a98a1 866 PrintAndLog("chk custom key[%d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r
aea4d766 867 keycnt++;\r
868 }\r
aea4d766 869 } else {\r
870 PrintAndLog("File: %s: not found or locked.", filename);\r
871 free(keyBlock);\r
872 return 1;\r
873 fclose(f);\r
874 }\r
9ca155ba 875 }\r
9ca155ba
M
876 }\r
877 \r
878 if (keycnt == 0) {\r
aea4d766 879 PrintAndLog("No key specified,try default keys");\r
880 for (;keycnt <=12; keycnt++)\r
881 PrintAndLog("chk default key[%d] %02x%02x%02x%02x%02x%02x", keycnt,\r
882 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r
883 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r
9ca155ba 884 }\r
9ca155ba 885 \r
aea4d766 886 for ( int t = !keyType ; t < 2 ; keyType==2?(t++):(t=2) ) {\r
887 int b=blockNo;\r
888 for (int i=0; i<SectorsCnt; ++i) {\r
889 PrintAndLog("--SectorsCnt:%d block no:0x%02x key type:%C key count:%d ", i, b, t?'B':'A', keycnt);\r
9492e0b0 890 uint32_t max_keys = keycnt>USB_CMD_DATA_SIZE/6?USB_CMD_DATA_SIZE/6:keycnt;\r
891 for (uint32_t c = 0; c < keycnt; c+=max_keys) {\r
892 uint32_t size = keycnt-c>max_keys?max_keys:keycnt-c;\r
893 res = mfCheckKeys(b, t, size, &keyBlock[6*c], &key64);\r
aea4d766 894 if (res !=1) {\r
895 if (!res) {\r
125a98a1 896 PrintAndLog("Found valid key:[%012"llx"]",key64);\r
aea4d766 897 if (transferToEml) {\r
898 uint8_t block[16];\r
899 mfEmlGetMem(block, get_trailer_block(b), 1);\r
900 num_to_bytes(key64, 6, block + t*10);\r
901 mfEmlSetMem(block, get_trailer_block(b), 1);\r
902 }\r
aea4d766 903 }\r
904 } else {\r
905 PrintAndLog("Command execute timeout");\r
906 }\r
907 }\r
908 b<127?(b+=4):(b+=16); \r
909 }\r
9ca155ba 910 }\r
aea4d766 911 \r
912 free(keyBlock);\r
9ca155ba 913\r
aea4d766 914/*\r
915 // Create dump file\r
916 if (createDumpFile) {\r
917 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) { \r
918 PrintAndLog("Could not create file dumpkeys.bin");\r
919 free(e_sector);\r
920 return 1;\r
921 }\r
922 PrintAndLog("Printing keys to bynary file dumpkeys.bin...");\r
923 for(i=0; i<16; i++) {\r
924 if (e_sector[i].foundKey[0]){\r
925 num_to_bytes(e_sector[i].Key[0], 6, tempkey);\r
926 fwrite ( tempkey, 1, 6, fkeys );\r
927 }\r
928 else{\r
929 fwrite ( &standart, 1, 6, fkeys );\r
930 }\r
931 }\r
932 for(i=0; i<16; i++) {\r
933 if (e_sector[i].foundKey[1]){\r
934 num_to_bytes(e_sector[i].Key[1], 6, tempkey);\r
935 fwrite ( tempkey, 1, 6, fkeys );\r
936 }\r
937 else{\r
938 fwrite ( &standart, 1, 6, fkeys );\r
939 }\r
940 }\r
941 fclose(fkeys);\r
942 }\r
943*/\r
9ca155ba
M
944 return 0;\r
945}\r
946\r
947int CmdHF14AMf1kSim(const char *Cmd)\r
948{\r
949 uint8_t uid[4] = {0, 0, 0, 0};\r
950 \r
951 if (param_getchar(Cmd, 0) == 'h') {\r
952 PrintAndLog("Usage: hf mf sim <uid (8 hex symbols)>");\r
953 PrintAndLog(" sample: hf mf sim 0a0a0a0a ");\r
954 return 0;\r
955 } \r
956 \r
957 if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {\r
958 PrintAndLog("UID must include 8 HEX symbols");\r
959 return 1;\r
960 }\r
961 PrintAndLog(" uid:%s ", sprint_hex(uid, 4));\r
962 \r
963 UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {0, 0, 0}};\r
964 memcpy(c.d.asBytes, uid, 4);\r
965 SendCommand(&c);\r
966\r
967 return 0;\r
968}\r
969\r
970int CmdHF14AMfDbg(const char *Cmd)\r
971{\r
8556b852
M
972 int dbgMode = param_get32ex(Cmd, 0, 0, 10);\r
973 if (dbgMode > 4) {\r
974 PrintAndLog("Max debud mode parameter is 4 \n");\r
975 }\r
976\r
977 if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {\r
9ca155ba
M
978 PrintAndLog("Usage: hf mf dbg <debug level>");\r
979 PrintAndLog(" 0 - no debug messages");\r
980 PrintAndLog(" 1 - error messages");\r
981 PrintAndLog(" 2 - all messages");\r
982 PrintAndLog(" 4 - extended debug mode");\r
983 return 0;\r
984 } \r
985\r
8556b852
M
986 UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};\r
987 SendCommand(&c);\r
988\r
9ca155ba
M
989 return 0;\r
990}\r
991\r
992int CmdHF14AMfEGet(const char *Cmd)\r
993{\r
8556b852
M
994 uint8_t blockNo = 0;\r
995 uint8_t data[3 * 16];\r
996 int i;\r
997\r
998 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
999 PrintAndLog("Usage: hf mf eget <block number>");\r
1000 PrintAndLog(" sample: hf mf eget 0 ");\r
1001 return 0;\r
1002 } \r
1003 \r
1004 blockNo = param_get8(Cmd, 0);\r
8556b852
M
1005\r
1006 PrintAndLog(" ");\r
1007 if (!mfEmlGetMem(data, blockNo, 3)) {\r
1008 for (i = 0; i < 3; i++) {\r
1009 PrintAndLog("data[%d]:%s", blockNo + i, sprint_hex(data + i * 16, 16));\r
1010 }\r
1011 } else {\r
1012 PrintAndLog("Command execute timeout");\r
1013 }\r
1014\r
1015 return 0;\r
1016}\r
1017\r
1018int CmdHF14AMfEClear(const char *Cmd)\r
1019{\r
1020 if (param_getchar(Cmd, 0) == 'h') {\r
1021 PrintAndLog("Usage: hf mf eclr");\r
1022 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");\r
1023 return 0;\r
1024 } \r
1025\r
1026 UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};\r
1027 SendCommand(&c);\r
9ca155ba
M
1028 return 0;\r
1029}\r
1030\r
1031int CmdHF14AMfESet(const char *Cmd)\r
1032{\r
8556b852
M
1033 uint8_t memBlock[16];\r
1034 uint8_t blockNo = 0;\r
1035\r
1036 memset(memBlock, 0x00, sizeof(memBlock));\r
1037\r
1038 if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {\r
1039 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");\r
1040 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");\r
1041 return 0;\r
1042 } \r
1043 \r
1044 blockNo = param_get8(Cmd, 0);\r
8556b852
M
1045 \r
1046 if (param_gethex(Cmd, 1, memBlock, 32)) {\r
1047 PrintAndLog("block data must include 32 HEX symbols");\r
1048 return 1;\r
1049 }\r
1050 \r
1051 // 1 - blocks count\r
1052 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};\r
1053 memcpy(c.d.asBytes, memBlock, 16);\r
1054 SendCommand(&c);\r
9ca155ba
M
1055 return 0;\r
1056}\r
1057\r
1058int CmdHF14AMfELoad(const char *Cmd)\r
1059{\r
ab8b654e
M
1060 FILE * f;\r
1061 char filename[20];\r
1062 char * fnameptr = filename;\r
1063 char buf[64];\r
1064 uint8_t buf8[64];\r
1065 int i, len, blockNum;\r
1066 \r
1067 memset(filename, 0, sizeof(filename));\r
1068 memset(buf, 0, sizeof(buf));\r
1069\r
aea4d766 1070 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {\r
ab8b654e
M
1071 PrintAndLog("It loads emul dump from the file `filename.eml`");\r
1072 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");\r
1073 PrintAndLog(" sample: hf mf eload filename");\r
1074 return 0;\r
1075 } \r
1076\r
1077 len = strlen(Cmd);\r
1078 if (len > 14) len = 14;\r
ab8b654e
M
1079\r
1080 memcpy(filename, Cmd, len);\r
1081 fnameptr += len;\r
1082\r
1083 sprintf(fnameptr, ".eml"); \r
1084 \r
1085 // open file\r
1086 f = fopen(filename, "r");\r
1087 if (f == NULL) {\r
1088 PrintAndLog("File not found or locked.");\r
1089 return 1;\r
1090 }\r
1091 \r
1092 blockNum = 0;\r
1093 while(!feof(f)){\r
1094 memset(buf, 0, sizeof(buf));\r
759c16b3 1095 if (fgets(buf, sizeof(buf), f) == NULL) {\r
1096 PrintAndLog("File reading error.");\r
1097 return 2;\r
1098 }\r
aea4d766 1099\r
ab8b654e 1100 if (strlen(buf) < 32){\r
aea4d766 1101 if(strlen(buf) && feof(f))\r
1102 break;\r
ab8b654e
M
1103 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
1104 return 2;\r
1105 }\r
1106 for (i = 0; i < 32; i += 2)\r
1107 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
1108// PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));\r
1109\r
1110 if (mfEmlSetMem(buf8, blockNum, 1)) {\r
1111 PrintAndLog("Cant set emul block: %d", blockNum);\r
1112 return 3;\r
1113 }\r
1114 blockNum++;\r
1115 \r
aea4d766 1116 if (blockNum >= 32 * 4 + 8 * 16) break;\r
ab8b654e
M
1117 }\r
1118 fclose(f);\r
1119 \r
aea4d766 1120 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){\r
ab8b654e
M
1121 PrintAndLog("File content error. There must be 64 blocks");\r
1122 return 4;\r
1123 }\r
1124 PrintAndLog("Loaded from file: %s", filename);\r
9ca155ba
M
1125 return 0;\r
1126}\r
1127\r
1128int CmdHF14AMfESave(const char *Cmd)\r
1129{\r
ab8b654e
M
1130 FILE * f;\r
1131 char filename[20];\r
1132 char * fnameptr = filename;\r
1133 uint8_t buf[64];\r
1134 int i, j, len;\r
1135 \r
1136 memset(filename, 0, sizeof(filename));\r
1137 memset(buf, 0, sizeof(buf));\r
1138\r
1139 if (param_getchar(Cmd, 0) == 'h') {\r
1140 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");\r
1141 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");\r
1142 PrintAndLog(" sample: hf mf esave ");\r
1143 PrintAndLog(" hf mf esave filename");\r
1144 return 0;\r
1145 } \r
1146\r
1147 len = strlen(Cmd);\r
1148 if (len > 14) len = 14;\r
1149 \r
1150 if (len < 1) {\r
1151 // get filename\r
1152 if (mfEmlGetMem(buf, 0, 1)) {\r
1153 PrintAndLog("Cant get block: %d", 0);\r
1154 return 1;\r
1155 }\r
1156 for (j = 0; j < 7; j++, fnameptr += 2)\r
1157 sprintf(fnameptr, "%02x", buf[j]); \r
1158 } else {\r
1159 memcpy(filename, Cmd, len);\r
1160 fnameptr += len;\r
1161 }\r
1162\r
1163 sprintf(fnameptr, ".eml"); \r
1164 \r
1165 // open file\r
1166 f = fopen(filename, "w+");\r
1167\r
1168 // put hex\r
aea4d766 1169 for (i = 0; i < 32 * 4 + 8 * 16; i++) {\r
ab8b654e
M
1170 if (mfEmlGetMem(buf, i, 1)) {\r
1171 PrintAndLog("Cant get block: %d", i);\r
1172 break;\r
1173 }\r
1174 for (j = 0; j < 16; j++)\r
1175 fprintf(f, "%02x", buf[j]); \r
1176 fprintf(f,"\n");\r
1177 }\r
1178 fclose(f);\r
1179 \r
1180 PrintAndLog("Saved to file: %s", filename);\r
1181 \r
9ca155ba
M
1182 return 0;\r
1183}\r
1184\r
26fdb4ab 1185int CmdHF14AMfECFill(const char *Cmd)\r
1186{\r
8556b852
M
1187 uint8_t keyType = 0;\r
1188\r
1189 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1190 PrintAndLog("Usage: hf mf efill <key A/B>");\r
1191 PrintAndLog("sample: hf mf efill A");\r
1192 PrintAndLog("Card data blocks transfers to card emulator memory.");\r
1193 PrintAndLog("Keys must be laid in the simulator memory. \n");\r
1194 return 0;\r
1195 } \r
1196\r
1197 char ctmp = param_getchar(Cmd, 0);\r
1198 if (ctmp == 0x00) {\r
1199 PrintAndLog("Key type must be A or B");\r
1200 return 1;\r
1201 }\r
1202 if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r
1203\r
1204 UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {0, keyType, 0}};\r
1205 SendCommand(&c);\r
1206 return 0;\r
1207}\r
1208\r
26fdb4ab 1209int CmdHF14AMfEKeyPrn(const char *Cmd)\r
1210{\r
aea4d766 1211 int i,b=-1;\r
8556b852
M
1212 uint8_t data[16];\r
1213 uint64_t keyA, keyB;\r
1214 \r
1215 PrintAndLog("|---|----------------|----------------|");\r
1216 PrintAndLog("|sec|key A |key B |");\r
1217 PrintAndLog("|---|----------------|----------------|");\r
aea4d766 1218 for (i = 0; i < 40; i++) {\r
1219 b<127?(b+=4):(b+=16);\r
1220 if (mfEmlGetMem(data, b, 1)) {\r
1221 PrintAndLog("error get block %d", b);\r
8556b852
M
1222 break;\r
1223 }\r
1224 keyA = bytes_to_num(data, 6);\r
1225 keyB = bytes_to_num(data + 10, 6);\r
125a98a1 1226 PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB);\r
8556b852
M
1227 }\r
1228 PrintAndLog("|---|----------------|----------------|");\r
1229 \r
1230 return 0;\r
1231}\r
1232\r
0675f200
M
1233int CmdHF14AMfCSetUID(const char *Cmd)\r
1234{\r
1235 uint8_t wipeCard = 0;\r
1236 uint8_t uid[8];\r
1237 uint8_t oldUid[8];\r
1238 int res;\r
1239\r
1240 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1241 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");\r
1242 PrintAndLog("sample: hf mf csetuid 01020304 w");\r
1243 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");\r
1244 PrintAndLog("If you want wipe card then add 'w' into command line. \n");\r
1245 return 0;\r
1246 } \r
1247\r
1248 if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {\r
1249 PrintAndLog("UID must include 8 HEX symbols");\r
1250 return 1;\r
1251 }\r
1252\r
1253 char ctmp = param_getchar(Cmd, 1);\r
1254 if (ctmp == 'w' || ctmp == 'W') wipeCard = 1;\r
1255 \r
1256 PrintAndLog("--wipe card:%02x uid:%s", wipeCard, sprint_hex(uid, 4));\r
1257\r
1258 res = mfCSetUID(uid, oldUid, wipeCard);\r
1259 if (res) {\r
1260 PrintAndLog("Can't set UID. error=%d", res);\r
1261 return 1;\r
1262 }\r
1263 \r
1264 PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));\r
1265 return 0;\r
1266}\r
1267\r
1268int CmdHF14AMfCSetBlk(const char *Cmd)\r
1269{\r
f774db95
M
1270 uint8_t uid[8];\r
1271 uint8_t memBlock[16];\r
1272 uint8_t blockNo = 0;\r
1273 int res;\r
1274 memset(memBlock, 0x00, sizeof(memBlock));\r
1275\r
1276 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1277 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");\r
1278 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");\r
1279 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");\r
1280 PrintAndLog("If you want wipe card then add 'w' into command line. \n");\r
1281 return 0;\r
1282 } \r
1283\r
1284 blockNo = param_get8(Cmd, 0);\r
f774db95
M
1285\r
1286 if (param_gethex(Cmd, 1, memBlock, 32)) {\r
1287 PrintAndLog("block data must include 32 HEX symbols");\r
1288 return 1;\r
1289 }\r
1290\r
1291 PrintAndLog("--block number:%02x data:%s", blockNo, sprint_hex(memBlock, 16));\r
1292\r
208a0166 1293 res = mfCSetBlock(blockNo, memBlock, uid, 0, CSETBLOCK_SINGLE_OPER);\r
f774db95
M
1294 if (res) {\r
1295 PrintAndLog("Can't write block. error=%d", res);\r
1296 return 1;\r
1297 }\r
1298 \r
1299 PrintAndLog("UID:%s", sprint_hex(uid, 4));\r
0675f200
M
1300 return 0;\r
1301}\r
1302\r
1303int CmdHF14AMfCLoad(const char *Cmd)\r
1304{\r
208a0166
M
1305 FILE * f;\r
1306 char filename[20];\r
1307 char * fnameptr = filename;\r
1308 char buf[64];\r
1309 uint8_t buf8[64];\r
1310 uint8_t fillFromEmulator = 0;\r
1311 int i, len, blockNum, flags;\r
1312 \r
1313 memset(filename, 0, sizeof(filename));\r
1314 memset(buf, 0, sizeof(buf));\r
1315\r
1316 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {\r
1317 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");\r
1318 PrintAndLog("or from emulator memory (option `e`)");\r
1319 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");\r
1320 PrintAndLog(" or: hf mf cload e ");\r
1321 PrintAndLog(" sample: hf mf cload filename");\r
1322 return 0;\r
1323 } \r
1324\r
1325 char ctmp = param_getchar(Cmd, 0);\r
1326 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r
1327 \r
1328 if (fillFromEmulator) {\r
1329 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1330 for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {\r
1331 if (mfEmlGetMem(buf8, blockNum, 1)) {\r
1332 PrintAndLog("Cant get block: %d", blockNum);\r
1333 return 2;\r
1334 }\r
1335 \r
1336 if (blockNum == 2) flags = 0;\r
1337 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1338\r
1339 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r
1340 PrintAndLog("Cant set magic card block: %d", blockNum);\r
1341 return 3;\r
1342 }\r
1343 }\r
1344 return 0;\r
1345 } else {\r
1346 len = strlen(Cmd);\r
1347 if (len > 14) len = 14;\r
1348\r
1349 memcpy(filename, Cmd, len);\r
1350 fnameptr += len;\r
1351\r
1352 sprintf(fnameptr, ".eml"); \r
1353 \r
1354 // open file\r
1355 f = fopen(filename, "r");\r
1356 if (f == NULL) {\r
1357 PrintAndLog("File not found or locked.");\r
1358 return 1;\r
1359 }\r
1360 \r
1361 blockNum = 0;\r
1362 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1363 while(!feof(f)){\r
1364 memset(buf, 0, sizeof(buf));\r
759c16b3 1365 if (fgets(buf, sizeof(buf), f) == NULL) {\r
1366 PrintAndLog("File reading error.");\r
1367 return 2;\r
1368 }\r
208a0166
M
1369\r
1370 if (strlen(buf) < 32){\r
1371 if(strlen(buf) && feof(f))\r
1372 break;\r
1373 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
1374 return 2;\r
1375 }\r
1376 for (i = 0; i < 32; i += 2)\r
1377 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
1378\r
1379 if (blockNum == 2) flags = 0;\r
1380 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1381\r
1382 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r
1383 PrintAndLog("Cant set magic card block: %d", blockNum);\r
1384 return 3;\r
1385 }\r
1386 blockNum++;\r
1387 \r
1388 if (blockNum >= 16 * 4) break; // magic card type - mifare 1K\r
1389 }\r
1390 fclose(f);\r
1391 \r
1392 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){\r
1393 PrintAndLog("File content error. There must be 64 blocks");\r
1394 return 4;\r
1395 }\r
1396 PrintAndLog("Loaded from file: %s", filename);\r
1397 return 0;\r
1398 }\r
0675f200
M
1399}\r
1400\r
545a1f38
M
1401int CmdHF14AMfCGetBlk(const char *Cmd) {\r
1402 uint8_t memBlock[16];\r
1403 uint8_t blockNo = 0;\r
1404 int res;\r
1405 memset(memBlock, 0x00, sizeof(memBlock));\r
1406\r
1407 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1408 PrintAndLog("Usage: hf mf cgetblk <block number>");\r
1409 PrintAndLog("sample: hf mf cgetblk 1");\r
1410 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");\r
1411 return 0;\r
1412 } \r
1413\r
1414 blockNo = param_get8(Cmd, 0);\r
545a1f38
M
1415\r
1416 PrintAndLog("--block number:%02x ", blockNo);\r
1417\r
1418 res = mfCGetBlock(blockNo, memBlock, CSETBLOCK_SINGLE_OPER);\r
1419 if (res) {\r
1420 PrintAndLog("Can't read block. error=%d", res);\r
1421 return 1;\r
1422 }\r
1423 \r
1424 PrintAndLog("block data:%s", sprint_hex(memBlock, 16));\r
1425 return 0;\r
1426}\r
1427\r
1428int CmdHF14AMfCGetSc(const char *Cmd) {\r
1429 uint8_t memBlock[16];\r
1430 uint8_t sectorNo = 0;\r
1431 int i, res, flags;\r
1432 memset(memBlock, 0x00, sizeof(memBlock));\r
1433\r
1434 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1435 PrintAndLog("Usage: hf mf cgetsc <sector number>");\r
1436 PrintAndLog("sample: hf mf cgetsc 0");\r
1437 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");\r
1438 return 0;\r
1439 } \r
1440\r
1441 sectorNo = param_get8(Cmd, 0);\r
1442 if (sectorNo > 15) {\r
1443 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");\r
1444 return 1;\r
1445 }\r
1446\r
1447 PrintAndLog("--sector number:%02x ", sectorNo);\r
1448\r
1449 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1450 for (i = 0; i < 4; i++) {\r
1451 if (i == 1) flags = 0;\r
1452 if (i == 3) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1453\r
1454 res = mfCGetBlock(sectorNo * 4 + i, memBlock, flags);\r
1455 if (res) {\r
1456 PrintAndLog("Can't read block. %02x error=%d", sectorNo * 4 + i, res);\r
1457 return 1;\r
1458 }\r
1459 \r
1460 PrintAndLog("block %02x data:%s", sectorNo * 4 + i, sprint_hex(memBlock, 16));\r
1461 }\r
1462 return 0;\r
1463}\r
1464\r
1465int CmdHF14AMfCSave(const char *Cmd) {\r
1466\r
1467 FILE * f;\r
1468 char filename[20];\r
1469 char * fnameptr = filename;\r
1470 uint8_t fillFromEmulator = 0;\r
1471 uint8_t buf[64];\r
1472 int i, j, len, flags;\r
1473 \r
1474 memset(filename, 0, sizeof(filename));\r
1475 memset(buf, 0, sizeof(buf));\r
1476\r
1477 if (param_getchar(Cmd, 0) == 'h') {\r
1478 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");\r
1479 PrintAndLog("or into emulator memory (option `e`)");\r
1480 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");\r
1481 PrintAndLog(" sample: hf mf esave ");\r
1482 PrintAndLog(" hf mf esave filename");\r
1483 PrintAndLog(" hf mf esave e \n");\r
1484 return 0;\r
1485 } \r
1486\r
1487 char ctmp = param_getchar(Cmd, 0);\r
1488 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r
1489\r
1490 if (fillFromEmulator) {\r
1491 // put into emulator\r
1492 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1493 for (i = 0; i < 16 * 4; i++) {\r
1494 if (i == 1) flags = 0;\r
1495 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1496 \r
1497 if (mfCGetBlock(i, buf, flags)) {\r
1498 PrintAndLog("Cant get block: %d", i);\r
1499 break;\r
1500 }\r
1501 \r
1502 if (mfEmlSetMem(buf, i, 1)) {\r
1503 PrintAndLog("Cant set emul block: %d", i);\r
1504 return 3;\r
1505 }\r
1506 }\r
1507 return 0;\r
1508 } else {\r
1509 len = strlen(Cmd);\r
1510 if (len > 14) len = 14;\r
1511 \r
1512 if (len < 1) {\r
1513 // get filename\r
1514 if (mfCGetBlock(0, buf, CSETBLOCK_SINGLE_OPER)) {\r
1515 PrintAndLog("Cant get block: %d", 0);\r
1516 return 1;\r
1517 }\r
1518 for (j = 0; j < 7; j++, fnameptr += 2)\r
1519 sprintf(fnameptr, "%02x", buf[j]); \r
1520 } else {\r
1521 memcpy(filename, Cmd, len);\r
1522 fnameptr += len;\r
1523 }\r
1524\r
1525 sprintf(fnameptr, ".eml"); \r
1526 \r
1527 // open file\r
1528 f = fopen(filename, "w+");\r
1529\r
1530 // put hex\r
1531 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1532 for (i = 0; i < 16 * 4; i++) {\r
1533 if (i == 1) flags = 0;\r
1534 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1535 \r
1536 if (mfCGetBlock(i, buf, flags)) {\r
1537 PrintAndLog("Cant get block: %d", i);\r
1538 break;\r
1539 }\r
1540 for (j = 0; j < 16; j++)\r
1541 fprintf(f, "%02x", buf[j]); \r
1542 fprintf(f,"\n");\r
1543 }\r
1544 fclose(f);\r
1545 \r
1546 PrintAndLog("Saved to file: %s", filename);\r
1547 \r
1548 return 0;\r
1549 }\r
1550}\r
1551\r
b62a5a84 1552int CmdHF14AMfSniff(const char *Cmd){\r
55acbb2a 1553 // params\r
c948cbde
M
1554 bool wantLogToFile = 0;\r
1555 bool wantDecrypt = 0;\r
eede7162 1556 //bool wantSaveToEml = 0; TODO\r
55acbb2a
M
1557 bool wantSaveToEmlFile = 0;\r
1558\r
1559 //var \r
39864b0b
M
1560 int res = 0;\r
1561 int len = 0;\r
1562 int blockLen = 0;\r
1563 int num = 0;\r
1564 int pckNum = 0;\r
1565 uint8_t uid[8];\r
1566 uint8_t atqa[2];\r
1567 uint8_t sak;\r
1568 bool isTag;\r
71d90e54 1569 uint32_t parity;\r
39864b0b
M
1570 uint8_t buf[3000];\r
1571 uint8_t * bufPtr = buf;\r
1572 memset(buf, 0x00, 3000);\r
b62a5a84
M
1573 \r
1574 if (param_getchar(Cmd, 0) == 'h') {\r
55acbb2a
M
1575 PrintAndLog("It continuously get data from the field and saves it to: log, emulator, emulator file.");\r
1576 PrintAndLog("You can specify:");\r
1577 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");\r
1578 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");\r
1579 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");\r
e0c635d1 1580 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
1581 PrintAndLog("Usage: hf mf sniff [l][d][e][r]");\r
1582 PrintAndLog(" sample: hf mf sniff l d e");\r
b62a5a84
M
1583 return 0;\r
1584 } \r
1585 \r
55acbb2a
M
1586 for (int i = 0; i < 4; i++) {\r
1587 char ctmp = param_getchar(Cmd, i);\r
1588 if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;\r
1589 if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;\r
eede7162 1590 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO\r
55acbb2a
M
1591 if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;\r
1592 }\r
1593 \r
39864b0b
M
1594 printf("-------------------------------------------------------------------------\n");\r
1595 printf("Executing command. \n");\r
1596 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r
1597 printf("Press the key on pc keyboard to abort the client.\n");\r
1598 printf("-------------------------------------------------------------------------\n");\r
1599\r
b62a5a84
M
1600 UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};\r
1601 SendCommand(&c);\r
1602\r
39864b0b
M
1603 // wait cycle\r
1604 while (true) {\r
1605 printf(".");\r
1606 fflush(stdout);\r
1607 if (ukbhit()) {\r
1608 getchar();\r
1609 printf("\naborted via keyboard!\n");\r
1610 break;\r
1611 }\r
1612 \r
902cb3c0 1613 UsbCommand resp;\r
1614 if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {\r
1615 res = resp.arg[0] & 0xff;\r
1616 len = resp.arg[1];\r
1617 num = resp.arg[2];\r
39864b0b
M
1618 \r
1619 if (res == 0) return 0;\r
1620 if (res == 1) {\r
1621 if (num ==0) {\r
1622 bufPtr = buf;\r
1623 memset(buf, 0x00, 3000);\r
1624 }\r
902cb3c0 1625 memcpy(bufPtr, resp.d.asBytes, len);\r
39864b0b
M
1626 bufPtr += len;\r
1627 pckNum++;\r
1628 }\r
1629 if (res == 2) {\r
1630 blockLen = bufPtr - buf;\r
1631 bufPtr = buf;\r
1632 printf(">\n");\r
1633 PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);\r
1634 num = 0;\r
1635 while (bufPtr - buf + 9 < blockLen) {\r
1636 isTag = bufPtr[3] & 0x80 ? true:false;\r
71d90e54
M
1637 bufPtr += 4;\r
1638 parity = *((uint32_t *)(bufPtr));\r
1639 bufPtr += 4;\r
39864b0b
M
1640 len = bufPtr[0];\r
1641 bufPtr++;\r
1642 if ((len == 14) && (bufPtr[0] = 0xff) && (bufPtr[1] = 0xff)) {\r
1643 memcpy(uid, bufPtr + 2, 7);\r
1644 memcpy(atqa, bufPtr + 2 + 7, 2);\r
1645 sak = bufPtr[11];\r
55acbb2a 1646 \r
39864b0b 1647 PrintAndLog("tag select uid:%s atqa:%02x %02x sak:0x%02x", sprint_hex(uid, 7), atqa[0], atqa[1], sak);\r
55acbb2a 1648 if (wantLogToFile) {\r
e0c635d1 1649 FillFileNameByUID(logHexFileName, uid, ".log", 7);\r
55acbb2a
M
1650 AddLogCurrentDT(logHexFileName);\r
1651 } \r
e0c635d1 1652 if (wantDecrypt) mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);\r
39864b0b
M
1653 } else {\r
1654 PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));\r
55acbb2a 1655 if (wantLogToFile) AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);\r
71d90e54 1656 if (wantDecrypt) mfTraceDecode(bufPtr, len, parity, wantSaveToEmlFile);\r
39864b0b
M
1657 }\r
1658 bufPtr += len;\r
1659 num++;\r
1660 }\r
1661 }\r
1662 } // resp not NILL\r
1663 } // while (true)\r
b62a5a84
M
1664 return 0;\r
1665}\r
1666\r
26fdb4ab 1667static command_t CommandTable[] =\r
9ca155ba 1668{\r
545a1f38
M
1669 {"help", CmdHelp, 1, "This help"},\r
1670 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},\r
9ca155ba
M
1671 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},\r
1672 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},\r
aea4d766 1673 {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},\r
545a1f38 1674 {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},\r
9ca155ba 1675 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},\r
545a1f38 1676 {"chk", CmdHF14AMfChk, 0, "Test block keys"},\r
9ca155ba
M
1677 {"mifare", CmdHF14AMifare, 0, "Read parity error messages. param - <used card nonce>"},\r
1678 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},\r
b62a5a84 1679 {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},\r
545a1f38
M
1680 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},\r
1681 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},\r
51969283
M
1682 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},\r
1683 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},\r
9ca155ba
M
1684 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},\r
1685 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},\r
8556b852
M
1686 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},\r
1687 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},\r
0675f200 1688 {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},\r
f774db95 1689 {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block into magic Chinese card"},\r
545a1f38
M
1690 {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block from magic Chinese card"},\r
1691 {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector from magic Chinese card"},\r
208a0166 1692 {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},\r
545a1f38 1693 {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},\r
9ca155ba
M
1694 {NULL, NULL, 0, NULL}\r
1695};\r
1696\r
1697int CmdHFMF(const char *Cmd)\r
1698{\r
1699 // flush\r
7dd1908b 1700 WaitForResponseTimeout(CMD_ACK,NULL,100);\r
9ca155ba
M
1701\r
1702 CmdsParse(CommandTable, Cmd);\r
1703 return 0;\r
1704}\r
1705\r
1706int CmdHelp(const char *Cmd)\r
1707{\r
1708 CmdsHelp(CommandTable);\r
1709 return 0;\r
1710}\r
Impressum, Datenschutz