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