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