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