]>
Commit | Line | Data |
---|---|---|
1 | //-----------------------------------------------------------------------------\r | |
2 | // Copyright (C) 2011 Merlok\r | |
3 | //\r | |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or,\r | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of\r | |
6 | // the license.\r | |
7 | //-----------------------------------------------------------------------------\r | |
8 | // High frequency MIFARE commands\r | |
9 | //-----------------------------------------------------------------------------\r | |
10 | \r | |
11 | #include "cmdhfmf.h"\r | |
12 | #include "proxmark3.h"\r | |
13 | \r | |
14 | static int CmdHelp(const char *Cmd);\r | |
15 | \r | |
16 | \r | |
17 | int CmdHF14AMifare(const char *Cmd)\r | |
18 | {\r | |
19 | uint32_t uid = 0;\r | |
20 | uint32_t nt = 0;\r | |
21 | uint64_t par_list = 0, ks_list = 0, r_key = 0;\r | |
22 | uint8_t isOK = 0;\r | |
23 | uint8_t keyBlock[6] = {0,0,0,0,0,0};\r | |
24 | \r | |
25 | if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, keyBlock, 8)) {\r | |
26 | PrintAndLog("Nt must include 8 HEX symbols");\r | |
27 | return 1;\r | |
28 | }\r | |
29 | \r | |
30 | UsbCommand c = {CMD_READER_MIFARE, {(uint32_t)bytes_to_num(keyBlock, 4), 0, 0}};\r | |
31 | SendCommand(&c);\r | |
32 | \r | |
33 | //flush queue\r | |
34 | while (ukbhit()) getchar();\r | |
35 | \r | |
36 | // message\r | |
37 | printf("-------------------------------------------------------------------------\n");\r | |
38 | printf("Executing command. It may take up to 30 min.\n");\r | |
39 | printf("Press the key on proxmark3 device to abort proxmark3.\n");\r | |
40 | printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r | |
41 | printf("-------------------------------------------------------------------------\n");\r | |
42 | \r | |
43 | // wait cycle\r | |
44 | while (true) {\r | |
45 | printf(".");\r | |
46 | if (ukbhit()) {\r | |
47 | getchar();\r | |
48 | printf("\naborted via keyboard!\n");\r | |
49 | break;\r | |
50 | }\r | |
51 | \r | |
52 | UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 2000);\r | |
53 | if (resp != NULL) {\r | |
54 | isOK = resp->arg[0] & 0xff;\r | |
55 | \r | |
56 | uid = (uint32_t)bytes_to_num(resp->d.asBytes + 0, 4);\r | |
57 | nt = (uint32_t)bytes_to_num(resp->d.asBytes + 4, 4);\r | |
58 | par_list = bytes_to_num(resp->d.asBytes + 8, 8);\r | |
59 | ks_list = bytes_to_num(resp->d.asBytes + 16, 8);\r | |
60 | \r | |
61 | printf("\n\n");\r | |
62 | PrintAndLog("isOk:%02x", isOK);\r | |
63 | if (!isOK) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n");\r | |
64 | break;\r | |
65 | }\r | |
66 | } \r | |
67 | printf("\n");\r | |
68 | \r | |
69 | // error\r | |
70 | if (isOK != 1) return 1;\r | |
71 | \r | |
72 | // execute original function from util nonce2key\r | |
73 | if (nonce2key(uid, nt, par_list, ks_list, &r_key)) return 2;\r | |
74 | printf("------------------------------------------------------------------\n");\r | |
75 | PrintAndLog("Key found:%012llx \n", r_key);\r | |
76 | \r | |
77 | num_to_bytes(r_key, 6, keyBlock);\r | |
78 | isOK = mfCheckKeys(0, 0, 1, keyBlock, &r_key);\r | |
79 | if (!isOK) \r | |
80 | PrintAndLog("Found valid key:%012llx", r_key);\r | |
81 | else\r | |
82 | PrintAndLog("Found invalid key. ( Nt=%08x", nt); \r | |
83 | \r | |
84 | \r | |
85 | return 0;\r | |
86 | }\r | |
87 | \r | |
88 | int CmdHF14AMfWrBl(const char *Cmd)\r | |
89 | {\r | |
90 | uint8_t blockNo = 0;\r | |
91 | uint8_t keyType = 0;\r | |
92 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
93 | uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r | |
94 | \r | |
95 | char cmdp = 0x00;\r | |
96 | \r | |
97 | if (strlen(Cmd)<3) {\r | |
98 | PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");\r | |
99 | PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");\r | |
100 | return 0;\r | |
101 | } \r | |
102 | \r | |
103 | blockNo = param_get8(Cmd, 0);\r | |
104 | cmdp = param_getchar(Cmd, 1);\r | |
105 | if (cmdp == 0x00) {\r | |
106 | PrintAndLog("Key type must be A or B");\r | |
107 | return 1;\r | |
108 | }\r | |
109 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
110 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
111 | PrintAndLog("Key must include 12 HEX symbols");\r | |
112 | return 1;\r | |
113 | }\r | |
114 | if (param_gethex(Cmd, 3, bldata, 32)) {\r | |
115 | PrintAndLog("Block data must include 32 HEX symbols");\r | |
116 | return 1;\r | |
117 | }\r | |
118 | PrintAndLog("--block no:%02x key type:%02x key:%s", blockNo, keyType, sprint_hex(key, 6));\r | |
119 | PrintAndLog("--data: %s", sprint_hex(bldata, 16));\r | |
120 | \r | |
121 | UsbCommand c = {CMD_MIFARE_WRITEBL, {blockNo, keyType, 0}};\r | |
122 | memcpy(c.d.asBytes, key, 6);\r | |
123 | memcpy(c.d.asBytes + 10, bldata, 16);\r | |
124 | SendCommand(&c);\r | |
125 | UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);\r | |
126 | \r | |
127 | if (resp != NULL) {\r | |
128 | uint8_t isOK = resp->arg[0] & 0xff;\r | |
129 | \r | |
130 | PrintAndLog("isOk:%02x", isOK);\r | |
131 | } else {\r | |
132 | PrintAndLog("Command execute timeout");\r | |
133 | }\r | |
134 | \r | |
135 | return 0;\r | |
136 | }\r | |
137 | \r | |
138 | int CmdHF14AMfRdBl(const char *Cmd)\r | |
139 | {\r | |
140 | uint8_t blockNo = 0;\r | |
141 | uint8_t keyType = 0;\r | |
142 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
143 | \r | |
144 | char cmdp = 0x00;\r | |
145 | \r | |
146 | \r | |
147 | if (strlen(Cmd)<3) {\r | |
148 | PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");\r | |
149 | PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");\r | |
150 | return 0;\r | |
151 | } \r | |
152 | \r | |
153 | blockNo = param_get8(Cmd, 0);\r | |
154 | cmdp = param_getchar(Cmd, 1);\r | |
155 | if (cmdp == 0x00) {\r | |
156 | PrintAndLog("Key type must be A or B");\r | |
157 | return 1;\r | |
158 | }\r | |
159 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
160 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
161 | PrintAndLog("Key must include 12 HEX symbols");\r | |
162 | return 1;\r | |
163 | }\r | |
164 | PrintAndLog("--block no:%02x key type:%02x key:%s ", blockNo, keyType, sprint_hex(key, 6));\r | |
165 | \r | |
166 | UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};\r | |
167 | memcpy(c.d.asBytes, key, 6);\r | |
168 | SendCommand(&c);\r | |
169 | UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);\r | |
170 | \r | |
171 | if (resp != NULL) {\r | |
172 | uint8_t isOK = resp->arg[0] & 0xff;\r | |
173 | uint8_t * data = resp->d.asBytes;\r | |
174 | \r | |
175 | if (isOK)\r | |
176 | PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 16));\r | |
177 | else\r | |
178 | PrintAndLog("isOk:%02x", isOK);\r | |
179 | } else {\r | |
180 | PrintAndLog("Command execute timeout");\r | |
181 | }\r | |
182 | \r | |
183 | return 0;\r | |
184 | }\r | |
185 | \r | |
186 | int CmdHF14AMfRdSc(const char *Cmd)\r | |
187 | {\r | |
188 | int i;\r | |
189 | uint8_t sectorNo = 0;\r | |
190 | uint8_t keyType = 0;\r | |
191 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
192 | \r | |
193 | uint8_t isOK = 0;\r | |
194 | uint8_t * data = NULL;\r | |
195 | \r | |
196 | char cmdp = 0x00;\r | |
197 | \r | |
198 | if (strlen(Cmd)<3) {\r | |
199 | PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");\r | |
200 | PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");\r | |
201 | return 0;\r | |
202 | } \r | |
203 | \r | |
204 | sectorNo = param_get8(Cmd, 0);\r | |
205 | if (sectorNo > 63) {\r | |
206 | PrintAndLog("Sector number must be less than 64");\r | |
207 | return 1;\r | |
208 | }\r | |
209 | cmdp = param_getchar(Cmd, 1);\r | |
210 | if (cmdp == 0x00) {\r | |
211 | PrintAndLog("Key type must be A or B");\r | |
212 | return 1;\r | |
213 | }\r | |
214 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
215 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
216 | PrintAndLog("Key must include 12 HEX symbols");\r | |
217 | return 1;\r | |
218 | }\r | |
219 | PrintAndLog("--sector no:%02x key type:%02x key:%s ", sectorNo, keyType, sprint_hex(key, 6));\r | |
220 | \r | |
221 | UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};\r | |
222 | memcpy(c.d.asBytes, key, 6);\r | |
223 | SendCommand(&c);\r | |
224 | UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);\r | |
225 | PrintAndLog(" ");\r | |
226 | \r | |
227 | if (resp != NULL) {\r | |
228 | isOK = resp->arg[0] & 0xff;\r | |
229 | data = resp->d.asBytes;\r | |
230 | \r | |
231 | PrintAndLog("isOk:%02x", isOK);\r | |
232 | if (isOK) \r | |
233 | for (i = 0; i < 2; i++) {\r | |
234 | PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));\r | |
235 | }\r | |
236 | } else {\r | |
237 | PrintAndLog("Command1 execute timeout");\r | |
238 | }\r | |
239 | \r | |
240 | // response2\r | |
241 | resp = WaitForResponseTimeout(CMD_ACK, 500);\r | |
242 | PrintAndLog(" ");\r | |
243 | \r | |
244 | if (resp != NULL) {\r | |
245 | isOK = resp->arg[0] & 0xff;\r | |
246 | data = resp->d.asBytes;\r | |
247 | \r | |
248 | if (isOK) \r | |
249 | for (i = 0; i < 2; i++) {\r | |
250 | PrintAndLog("data:%s", sprint_hex(data + i * 16, 16));\r | |
251 | }\r | |
252 | } else {\r | |
253 | PrintAndLog("Command2 execute timeout");\r | |
254 | }\r | |
255 | \r | |
256 | return 0;\r | |
257 | }\r | |
258 | \r | |
259 | int CmdHF14AMfNested(const char *Cmd)\r | |
260 | {\r | |
261 | int i, j, res, iterations;\r | |
262 | sector * e_sector = NULL;\r | |
263 | uint8_t blockNo = 0;\r | |
264 | uint8_t keyType = 0;\r | |
265 | uint8_t trgBlockNo = 0;\r | |
266 | uint8_t trgKeyType = 0;\r | |
267 | uint8_t blDiff = 0;\r | |
268 | int SectorsCnt = 0;\r | |
269 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
270 | uint8_t keyBlock[16 * 6];\r | |
271 | uint64_t key64 = 0;\r | |
272 | int transferToEml = 0;\r | |
273 | \r | |
274 | char cmdp, ctmp;\r | |
275 | \r | |
276 | if (strlen(Cmd)<3) {\r | |
277 | PrintAndLog("Usage:");\r | |
278 | PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t]");\r | |
279 | PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");\r | |
280 | PrintAndLog(" <target block number> <target key A/B> [t]");\r | |
281 | PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r | |
282 | PrintAndLog("t - transfer keys into emulator memory");\r | |
283 | PrintAndLog(" ");\r | |
284 | PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");\r | |
285 | PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF t ");\r | |
286 | PrintAndLog(" sample2: hf mf nested o 0 A FFFFFFFFFFFF 4 A");\r | |
287 | return 0;\r | |
288 | } \r | |
289 | \r | |
290 | cmdp = param_getchar(Cmd, 0);\r | |
291 | blockNo = param_get8(Cmd, 1);\r | |
292 | ctmp = param_getchar(Cmd, 2);\r | |
293 | if (ctmp == 0x00) {\r | |
294 | PrintAndLog("Key type must be A or B");\r | |
295 | return 1;\r | |
296 | }\r | |
297 | if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r | |
298 | if (param_gethex(Cmd, 3, key, 12)) {\r | |
299 | PrintAndLog("Key must include 12 HEX symbols");\r | |
300 | return 1;\r | |
301 | }\r | |
302 | \r | |
303 | if (cmdp == 'o' || cmdp == 'O') {\r | |
304 | cmdp = 'o';\r | |
305 | trgBlockNo = param_get8(Cmd, 4);\r | |
306 | ctmp = param_getchar(Cmd, 5);\r | |
307 | if (ctmp == 0x00) {\r | |
308 | PrintAndLog("Target key type must be A or B");\r | |
309 | return 1;\r | |
310 | }\r | |
311 | if (ctmp != 'A' && ctmp != 'a') trgKeyType = 1;\r | |
312 | } else {\r | |
313 | switch (cmdp) {\r | |
314 | case '0': SectorsCnt = 05; break;\r | |
315 | case '1': SectorsCnt = 16; break;\r | |
316 | case '2': SectorsCnt = 32; break;\r | |
317 | case '4': SectorsCnt = 64; break;\r | |
318 | default: SectorsCnt = 16;\r | |
319 | }\r | |
320 | }\r | |
321 | \r | |
322 | ctmp = param_getchar(Cmd, 4);\r | |
323 | if (ctmp == 't' || ctmp == 'T') transferToEml = 1;\r | |
324 | ctmp = param_getchar(Cmd, 6);\r | |
325 | transferToEml |= (ctmp == 't' || ctmp == 'T');\r | |
326 | \r | |
327 | PrintAndLog("--block no:%02x key type:%02x key:%s etrans:%d", blockNo, keyType, sprint_hex(key, 6), transferToEml);\r | |
328 | if (cmdp == 'o')\r | |
329 | PrintAndLog("--target block no:%02x target key type:%02x ", trgBlockNo, trgKeyType);\r | |
330 | \r | |
331 | if (cmdp == 'o') {\r | |
332 | if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock)) {\r | |
333 | PrintAndLog("Nested error.");\r | |
334 | return 2;\r | |
335 | }\r | |
336 | \r | |
337 | for (i = 0; i < 16; i++) {\r | |
338 | PrintAndLog("cnt=%d key= %s", i, sprint_hex(keyBlock + i * 6, 6));\r | |
339 | }\r | |
340 | \r | |
341 | // test keys\r | |
342 | res = mfCheckKeys(trgBlockNo, trgKeyType, 8, keyBlock, &key64);\r | |
343 | if (res)\r | |
344 | res = mfCheckKeys(trgBlockNo, trgKeyType, 8, &keyBlock[6 * 8], &key64);\r | |
345 | if (!res) {\r | |
346 | PrintAndLog("Found valid key:%012llx", key64);\r | |
347 | \r | |
348 | // transfer key to the emulator\r | |
349 | if (transferToEml) {\r | |
350 | mfEmlGetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1);\r | |
351 | \r | |
352 | if (!trgKeyType)\r | |
353 | num_to_bytes(key64, 6, keyBlock);\r | |
354 | else\r | |
355 | num_to_bytes(key64, 6, &keyBlock[10]);\r | |
356 | mfEmlSetMem(keyBlock, (trgBlockNo / 4) * 4 + 3, 1); \r | |
357 | }\r | |
358 | } else {\r | |
359 | PrintAndLog("No valid key found");\r | |
360 | }\r | |
361 | } else // ------------------------------------ multiple sectors working\r | |
362 | {\r | |
363 | blDiff = blockNo % 4;\r | |
364 | PrintAndLog("Block shift=%d", blDiff);\r | |
365 | e_sector = calloc(SectorsCnt, sizeof(sector));\r | |
366 | if (e_sector == NULL) return 1;\r | |
367 | \r | |
368 | //test current key 4 sectors\r | |
369 | memcpy(keyBlock, key, 6);\r | |
370 | num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 1 * 6));\r | |
371 | num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 2 * 6));\r | |
372 | num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 3 * 6));\r | |
373 | num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 4 * 6));\r | |
374 | num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));\r | |
375 | \r | |
376 | PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);\r | |
377 | for (i = 0; i < SectorsCnt; i++) {\r | |
378 | for (j = 0; j < 2; j++) {\r | |
379 | if (e_sector[i].foundKey[j]) continue;\r | |
380 | \r | |
381 | res = mfCheckKeys(i * 4 + blDiff, j, 6, keyBlock, &key64);\r | |
382 | \r | |
383 | if (!res) {\r | |
384 | e_sector[i].Key[j] = key64;\r | |
385 | e_sector[i].foundKey[j] = 1;\r | |
386 | }\r | |
387 | }\r | |
388 | } \r | |
389 | \r | |
390 | \r | |
391 | // nested sectors\r | |
392 | iterations = 0;\r | |
393 | PrintAndLog("nested...");\r | |
394 | for (i = 0; i < NESTED_SECTOR_RETRY; i++) {\r | |
395 | for (trgBlockNo = blDiff; trgBlockNo < SectorsCnt * 4; trgBlockNo = trgBlockNo + 4) \r | |
396 | for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) { \r | |
397 | if (e_sector[trgBlockNo / 4].foundKey[trgKeyType]) continue;\r | |
398 | if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock)) continue;\r | |
399 | \r | |
400 | iterations++;\r | |
401 | \r | |
402 | //try keys from nested\r | |
403 | res = mfCheckKeys(trgBlockNo, trgKeyType, 8, keyBlock, &key64);\r | |
404 | if (res)\r | |
405 | res = mfCheckKeys(trgBlockNo, trgKeyType, 8, &keyBlock[6 * 8], &key64);\r | |
406 | if (!res) {\r | |
407 | PrintAndLog("Found valid key:%012llx", key64); \r | |
408 | e_sector[trgBlockNo / 4].foundKey[trgKeyType] = 1;\r | |
409 | e_sector[trgBlockNo / 4].Key[trgKeyType] = key64;\r | |
410 | }\r | |
411 | }\r | |
412 | }\r | |
413 | \r | |
414 | PrintAndLog("Iterations count: %d", iterations);\r | |
415 | //print them\r | |
416 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
417 | PrintAndLog("|sec|key A |res|key B |res|");\r | |
418 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
419 | for (i = 0; i < SectorsCnt; i++) {\r | |
420 | PrintAndLog("|%03d| %012llx | %d | %012llx | %d |", i, \r | |
421 | e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);\r | |
422 | }\r | |
423 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
424 | \r | |
425 | // transfer them to the emulator\r | |
426 | if (transferToEml) {\r | |
427 | for (i = 0; i < SectorsCnt; i++) {\r | |
428 | mfEmlGetMem(keyBlock, i * 4 + 3, 1);\r | |
429 | if (e_sector[i].foundKey[0])\r | |
430 | num_to_bytes(e_sector[i].Key[0], 6, keyBlock);\r | |
431 | if (e_sector[i].foundKey[1])\r | |
432 | num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);\r | |
433 | mfEmlSetMem(keyBlock, i * 4 + 3, 1);\r | |
434 | } \r | |
435 | }\r | |
436 | \r | |
437 | free(e_sector);\r | |
438 | }\r | |
439 | \r | |
440 | return 0;\r | |
441 | }\r | |
442 | \r | |
443 | int CmdHF14AMfChk(const char *Cmd)\r | |
444 | {\r | |
445 | int i, res;\r | |
446 | int keycnt = 0;\r | |
447 | char ctmp = 0x00;\r | |
448 | uint8_t blockNo = 0;\r | |
449 | uint8_t keyType = 0;\r | |
450 | uint8_t keyBlock[8 * 6];\r | |
451 | uint64_t key64 = 0;\r | |
452 | \r | |
453 | memset(keyBlock, 0x00, sizeof(keyBlock));\r | |
454 | \r | |
455 | if (strlen(Cmd)<3) {\r | |
456 | PrintAndLog("Usage: hf mf chk <block number> <key A/B> [<key (12 hex symbols)>]");\r | |
457 | PrintAndLog(" sample: hf mf chk 0 A FFFFFFFFFFFF a0a1a2a3a4a5 b0b1b2b3b4b5 ");\r | |
458 | return 0;\r | |
459 | } \r | |
460 | \r | |
461 | blockNo = param_get8(Cmd, 0);\r | |
462 | ctmp = param_getchar(Cmd, 1);\r | |
463 | if (ctmp == 0x00) {\r | |
464 | PrintAndLog("Key type must be A or B");\r | |
465 | return 1;\r | |
466 | }\r | |
467 | if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r | |
468 | \r | |
469 | for (i = 0; i < 6; i++) {\r | |
470 | if (!isxdigit(param_getchar(Cmd, 2 + i))) break;\r | |
471 | \r | |
472 | if (param_gethex(Cmd, 2 + i, keyBlock + 6 * i, 12)) {\r | |
473 | PrintAndLog("Key[%d] must include 12 HEX symbols", i);\r | |
474 | return 1;\r | |
475 | }\r | |
476 | keycnt = i + 1;\r | |
477 | }\r | |
478 | \r | |
479 | if (keycnt == 0) {\r | |
480 | PrintAndLog("There is must be at least one key");\r | |
481 | return 1;\r | |
482 | }\r | |
483 | \r | |
484 | PrintAndLog("--block no:%02x key type:%02x key count:%d ", blockNo, keyType, keycnt);\r | |
485 | \r | |
486 | res = mfCheckKeys(blockNo, keyType, keycnt, keyBlock, &key64);\r | |
487 | if (res !=1) {\r | |
488 | if (!res)\r | |
489 | PrintAndLog("isOk:%02x valid key:%012llx", 1, key64);\r | |
490 | else\r | |
491 | PrintAndLog("isOk:%02x", 0);\r | |
492 | } else {\r | |
493 | PrintAndLog("Command execute timeout");\r | |
494 | }\r | |
495 | \r | |
496 | return 0;\r | |
497 | }\r | |
498 | \r | |
499 | int CmdHF14AMf1kSim(const char *Cmd)\r | |
500 | {\r | |
501 | uint8_t uid[4] = {0, 0, 0, 0};\r | |
502 | \r | |
503 | if (param_getchar(Cmd, 0) == 'h') {\r | |
504 | PrintAndLog("Usage: hf mf sim <uid (8 hex symbols)>");\r | |
505 | PrintAndLog(" sample: hf mf sim 0a0a0a0a ");\r | |
506 | return 0;\r | |
507 | } \r | |
508 | \r | |
509 | if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {\r | |
510 | PrintAndLog("UID must include 8 HEX symbols");\r | |
511 | return 1;\r | |
512 | }\r | |
513 | PrintAndLog(" uid:%s ", sprint_hex(uid, 4));\r | |
514 | \r | |
515 | UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {0, 0, 0}};\r | |
516 | memcpy(c.d.asBytes, uid, 4);\r | |
517 | SendCommand(&c);\r | |
518 | \r | |
519 | return 0;\r | |
520 | }\r | |
521 | \r | |
522 | int CmdHF14AMfDbg(const char *Cmd)\r | |
523 | {\r | |
524 | int dbgMode = param_get32ex(Cmd, 0, 0, 10);\r | |
525 | if (dbgMode > 4) {\r | |
526 | PrintAndLog("Max debud mode parameter is 4 \n");\r | |
527 | }\r | |
528 | \r | |
529 | if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {\r | |
530 | PrintAndLog("Usage: hf mf dbg <debug level>");\r | |
531 | PrintAndLog(" 0 - no debug messages");\r | |
532 | PrintAndLog(" 1 - error messages");\r | |
533 | PrintAndLog(" 2 - all messages");\r | |
534 | PrintAndLog(" 4 - extended debug mode");\r | |
535 | return 0;\r | |
536 | } \r | |
537 | \r | |
538 | UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};\r | |
539 | SendCommand(&c);\r | |
540 | \r | |
541 | return 0;\r | |
542 | }\r | |
543 | \r | |
544 | int CmdHF14AMfEGet(const char *Cmd)\r | |
545 | {\r | |
546 | uint8_t blockNo = 0;\r | |
547 | uint8_t data[3 * 16];\r | |
548 | int i;\r | |
549 | \r | |
550 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
551 | PrintAndLog("Usage: hf mf eget <block number>");\r | |
552 | PrintAndLog(" sample: hf mf eget 0 ");\r | |
553 | return 0;\r | |
554 | } \r | |
555 | \r | |
556 | blockNo = param_get8(Cmd, 0);\r | |
557 | if (blockNo >= 16 * 4) {\r | |
558 | PrintAndLog("Block number must be in [0..63] as in MIFARE classic.");\r | |
559 | return 1;\r | |
560 | }\r | |
561 | \r | |
562 | PrintAndLog(" ");\r | |
563 | if (!mfEmlGetMem(data, blockNo, 3)) {\r | |
564 | for (i = 0; i < 3; i++) {\r | |
565 | PrintAndLog("data[%d]:%s", blockNo + i, sprint_hex(data + i * 16, 16));\r | |
566 | }\r | |
567 | } else {\r | |
568 | PrintAndLog("Command execute timeout");\r | |
569 | }\r | |
570 | \r | |
571 | return 0;\r | |
572 | }\r | |
573 | \r | |
574 | int CmdHF14AMfEClear(const char *Cmd)\r | |
575 | {\r | |
576 | if (param_getchar(Cmd, 0) == 'h') {\r | |
577 | PrintAndLog("Usage: hf mf eclr");\r | |
578 | PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");\r | |
579 | return 0;\r | |
580 | } \r | |
581 | \r | |
582 | UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};\r | |
583 | SendCommand(&c);\r | |
584 | return 0;\r | |
585 | }\r | |
586 | \r | |
587 | int CmdHF14AMfESet(const char *Cmd)\r | |
588 | {\r | |
589 | uint8_t memBlock[16];\r | |
590 | uint8_t blockNo = 0;\r | |
591 | \r | |
592 | memset(memBlock, 0x00, sizeof(memBlock));\r | |
593 | \r | |
594 | if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {\r | |
595 | PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");\r | |
596 | PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");\r | |
597 | return 0;\r | |
598 | } \r | |
599 | \r | |
600 | blockNo = param_get8(Cmd, 0);\r | |
601 | if (blockNo >= 16 * 4) {\r | |
602 | PrintAndLog("Block number must be in [0..63] as in MIFARE classic.");\r | |
603 | return 1;\r | |
604 | }\r | |
605 | \r | |
606 | if (param_gethex(Cmd, 1, memBlock, 32)) {\r | |
607 | PrintAndLog("block data must include 32 HEX symbols");\r | |
608 | return 1;\r | |
609 | }\r | |
610 | \r | |
611 | // 1 - blocks count\r | |
612 | UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};\r | |
613 | memcpy(c.d.asBytes, memBlock, 16);\r | |
614 | SendCommand(&c);\r | |
615 | return 0;\r | |
616 | }\r | |
617 | \r | |
618 | int CmdHF14AMfELoad(const char *Cmd)\r | |
619 | {\r | |
620 | FILE * f;\r | |
621 | char filename[20];\r | |
622 | char * fnameptr = filename;\r | |
623 | char buf[64];\r | |
624 | uint8_t buf8[64];\r | |
625 | int i, len, blockNum;\r | |
626 | \r | |
627 | memset(filename, 0, sizeof(filename));\r | |
628 | memset(buf, 0, sizeof(buf));\r | |
629 | \r | |
630 | if (param_getchar(Cmd, 0) == 'h') {\r | |
631 | PrintAndLog("It loads emul dump from the file `filename.eml`");\r | |
632 | PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");\r | |
633 | PrintAndLog(" sample: hf mf eload filename");\r | |
634 | return 0;\r | |
635 | } \r | |
636 | \r | |
637 | len = strlen(Cmd);\r | |
638 | if (len > 14) len = 14;\r | |
639 | \r | |
640 | if (len < 1) {\r | |
641 | }\r | |
642 | \r | |
643 | memcpy(filename, Cmd, len);\r | |
644 | fnameptr += len;\r | |
645 | \r | |
646 | sprintf(fnameptr, ".eml"); \r | |
647 | \r | |
648 | // open file\r | |
649 | f = fopen(filename, "r");\r | |
650 | if (f == NULL) {\r | |
651 | PrintAndLog("File not found or locked.");\r | |
652 | return 1;\r | |
653 | }\r | |
654 | \r | |
655 | blockNum = 0;\r | |
656 | while(!feof(f)){\r | |
657 | memset(buf, 0, sizeof(buf));\r | |
658 | fgets(buf, sizeof(buf), f);\r | |
659 | if (strlen(buf) < 32){\r | |
660 | PrintAndLog("File content error. Block data must include 32 HEX symbols");\r | |
661 | return 2;\r | |
662 | }\r | |
663 | for (i = 0; i < 32; i += 2)\r | |
664 | sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r | |
665 | // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));\r | |
666 | \r | |
667 | if (mfEmlSetMem(buf8, blockNum, 1)) {\r | |
668 | PrintAndLog("Cant set emul block: %d", blockNum);\r | |
669 | return 3;\r | |
670 | }\r | |
671 | blockNum++;\r | |
672 | \r | |
673 | if (blockNum >= 16 * 4) break;\r | |
674 | }\r | |
675 | fclose(f);\r | |
676 | \r | |
677 | if (blockNum != 16 * 4){\r | |
678 | PrintAndLog("File content error. There must be 64 blocks");\r | |
679 | return 4;\r | |
680 | }\r | |
681 | PrintAndLog("Loaded from file: %s", filename);\r | |
682 | return 0;\r | |
683 | }\r | |
684 | \r | |
685 | int CmdHF14AMfESave(const char *Cmd)\r | |
686 | {\r | |
687 | FILE * f;\r | |
688 | char filename[20];\r | |
689 | char * fnameptr = filename;\r | |
690 | uint8_t buf[64];\r | |
691 | int i, j, len;\r | |
692 | \r | |
693 | memset(filename, 0, sizeof(filename));\r | |
694 | memset(buf, 0, sizeof(buf));\r | |
695 | \r | |
696 | if (param_getchar(Cmd, 0) == 'h') {\r | |
697 | PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");\r | |
698 | PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");\r | |
699 | PrintAndLog(" sample: hf mf esave ");\r | |
700 | PrintAndLog(" hf mf esave filename");\r | |
701 | return 0;\r | |
702 | } \r | |
703 | \r | |
704 | len = strlen(Cmd);\r | |
705 | if (len > 14) len = 14;\r | |
706 | \r | |
707 | if (len < 1) {\r | |
708 | // get filename\r | |
709 | if (mfEmlGetMem(buf, 0, 1)) {\r | |
710 | PrintAndLog("Cant get block: %d", 0);\r | |
711 | return 1;\r | |
712 | }\r | |
713 | for (j = 0; j < 7; j++, fnameptr += 2)\r | |
714 | sprintf(fnameptr, "%02x", buf[j]); \r | |
715 | } else {\r | |
716 | memcpy(filename, Cmd, len);\r | |
717 | fnameptr += len;\r | |
718 | }\r | |
719 | \r | |
720 | sprintf(fnameptr, ".eml"); \r | |
721 | \r | |
722 | // open file\r | |
723 | f = fopen(filename, "w+");\r | |
724 | \r | |
725 | // put hex\r | |
726 | for (i = 0; i < 16 * 4; i++) {\r | |
727 | if (mfEmlGetMem(buf, i, 1)) {\r | |
728 | PrintAndLog("Cant get block: %d", i);\r | |
729 | break;\r | |
730 | }\r | |
731 | for (j = 0; j < 16; j++)\r | |
732 | fprintf(f, "%02x", buf[j]); \r | |
733 | fprintf(f,"\n");\r | |
734 | }\r | |
735 | fclose(f);\r | |
736 | \r | |
737 | PrintAndLog("Saved to file: %s", filename);\r | |
738 | \r | |
739 | return 0;\r | |
740 | }\r | |
741 | \r | |
742 | int CmdHF14AMfECFill(const char *Cmd) {\r | |
743 | uint8_t keyType = 0;\r | |
744 | \r | |
745 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
746 | PrintAndLog("Usage: hf mf efill <key A/B>");\r | |
747 | PrintAndLog("sample: hf mf efill A");\r | |
748 | PrintAndLog("Card data blocks transfers to card emulator memory.");\r | |
749 | PrintAndLog("Keys must be laid in the simulator memory. \n");\r | |
750 | return 0;\r | |
751 | } \r | |
752 | \r | |
753 | char ctmp = param_getchar(Cmd, 0);\r | |
754 | if (ctmp == 0x00) {\r | |
755 | PrintAndLog("Key type must be A or B");\r | |
756 | return 1;\r | |
757 | }\r | |
758 | if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r | |
759 | \r | |
760 | UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {0, keyType, 0}};\r | |
761 | SendCommand(&c);\r | |
762 | return 0;\r | |
763 | }\r | |
764 | \r | |
765 | int CmdHF14AMfEKeyPrn(const char *Cmd) {\r | |
766 | int i;\r | |
767 | uint8_t data[16];\r | |
768 | uint64_t keyA, keyB;\r | |
769 | \r | |
770 | PrintAndLog("|---|----------------|----------------|");\r | |
771 | PrintAndLog("|sec|key A |key B |");\r | |
772 | PrintAndLog("|---|----------------|----------------|");\r | |
773 | for (i = 0; i < 16; i++) {\r | |
774 | if (mfEmlGetMem(data, i * 4 + 3, 1)) {\r | |
775 | PrintAndLog("error get block %d", i * 4 + 3);\r | |
776 | break;\r | |
777 | }\r | |
778 | keyA = bytes_to_num(data, 6);\r | |
779 | keyB = bytes_to_num(data + 10, 6);\r | |
780 | PrintAndLog("|%03d| %012llx | %012llx |", i, keyA, keyB);\r | |
781 | }\r | |
782 | PrintAndLog("|---|----------------|----------------|");\r | |
783 | \r | |
784 | return 0;\r | |
785 | }\r | |
786 | \r | |
787 | static command_t CommandTable[] = \r | |
788 | {\r | |
789 | {"help", CmdHelp, 1, "This help"},\r | |
790 | {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},\r | |
791 | {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},\r | |
792 | {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},\r | |
793 | {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},\r | |
794 | {"chk", CmdHF14AMfChk, 0, "Test block up to 8 keys"},\r | |
795 | {"mifare", CmdHF14AMifare, 0, "Read parity error messages. param - <used card nonce>"},\r | |
796 | {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},\r | |
797 | {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE 1k card"},\r | |
798 | {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},\r | |
799 | {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},\r | |
800 | {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},\r | |
801 | {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},\r | |
802 | {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},\r | |
803 | {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},\r | |
804 | {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},\r | |
805 | {NULL, NULL, 0, NULL}\r | |
806 | };\r | |
807 | \r | |
808 | int CmdHFMF(const char *Cmd)\r | |
809 | {\r | |
810 | // flush\r | |
811 | while (WaitForResponseTimeout(CMD_ACK, 500) != NULL) ;\r | |
812 | \r | |
813 | CmdsParse(CommandTable, Cmd);\r | |
814 | return 0;\r | |
815 | }\r | |
816 | \r | |
817 | int CmdHelp(const char *Cmd)\r | |
818 | {\r | |
819 | CmdsHelp(CommandTable);\r | |
820 | return 0;\r | |
821 | }\r |