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