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