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