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