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