]>
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 | |
f38a1528 | 10 | #include "../include/mifare.h"\r |
9ca155ba M |
11 | #include "cmdhfmf.h"\r |
12 | \r | |
13 | static int CmdHelp(const char *Cmd);\r | |
14 | \r | |
9ca155ba M |
15 | int CmdHF14AMifare(const char *Cmd)\r |
16 | {\r | |
17 | uint32_t uid = 0;\r | |
1c611bbd | 18 | uint32_t nt = 0, nr = 0;\r |
9ca155ba M |
19 | uint64_t par_list = 0, ks_list = 0, r_key = 0;\r |
20 | uint8_t isOK = 0;\r | |
ac14bee3 | 21 | uint8_t keyBlock[8] = {0};\r |
aea4d766 | 22 | \r |
1c611bbd | 23 | UsbCommand c = {CMD_READER_MIFARE, {true, 0, 0}};\r |
24 | \r | |
25 | // message\r | |
26 | printf("-------------------------------------------------------------------------\n");\r | |
27 | printf("Executing command. Expected execution time: 25sec on average :-)\n");\r | |
28 | printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r | |
29 | printf("-------------------------------------------------------------------------\n");\r | |
aea4d766 | 30 | \r |
9ca155ba | 31 | \r |
aea4d766 | 32 | start:\r |
e772353f | 33 | clearCommandBuffer();\r |
34 | SendCommand(&c);\r | |
9ca155ba M |
35 | \r |
36 | //flush queue\r | |
37 | while (ukbhit()) getchar();\r | |
38 | \r | |
9ca155ba M |
39 | \r |
40 | // wait cycle\r | |
41 | while (true) {\r | |
1c611bbd | 42 | printf(".");\r |
aea4d766 | 43 | fflush(stdout);\r |
9ca155ba M |
44 | if (ukbhit()) {\r |
45 | getchar();\r | |
46 | printf("\naborted via keyboard!\n");\r | |
47 | break;\r | |
48 | }\r | |
49 | \r | |
902cb3c0 | 50 | UsbCommand resp;\r |
1c611bbd | 51 | if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {\r |
902cb3c0 | 52 | isOK = resp.arg[0] & 0xff;\r |
902cb3c0 | 53 | uid = (uint32_t)bytes_to_num(resp.d.asBytes + 0, 4);\r |
54 | nt = (uint32_t)bytes_to_num(resp.d.asBytes + 4, 4);\r | |
55 | par_list = bytes_to_num(resp.d.asBytes + 8, 8);\r | |
56 | ks_list = bytes_to_num(resp.d.asBytes + 16, 8);\r | |
1c611bbd | 57 | nr = bytes_to_num(resp.d.asBytes + 24, 4);\r |
9ca155ba | 58 | printf("\n\n");\r |
9ca155ba M |
59 | if (!isOK) PrintAndLog("Proxmark can't get statistic info. Execution aborted.\n");\r |
60 | break;\r | |
61 | }\r | |
62 | } \r | |
1c611bbd | 63 | \r |
9ca155ba M |
64 | printf("\n");\r |
65 | \r | |
66 | // error\r | |
67 | if (isOK != 1) return 1;\r | |
68 | \r | |
69 | // execute original function from util nonce2key\r | |
1c611bbd | 70 | if (nonce2key(uid, nt, nr, par_list, ks_list, &r_key))\r |
bfaecce6 M |
71 | {\r |
72 | isOK = 2;\r | |
73 | PrintAndLog("Key not found (lfsr_common_prefix list is null). Nt=%08x", nt); \r | |
74 | } else {\r | |
75 | printf("------------------------------------------------------------------\n");\r | |
125a98a1 | 76 | PrintAndLog("Key found:%012"llx" \n", r_key);\r |
9ca155ba | 77 | \r |
bfaecce6 M |
78 | num_to_bytes(r_key, 6, keyBlock);\r |
79 | isOK = mfCheckKeys(0, 0, 1, keyBlock, &r_key);\r | |
80 | }\r | |
9ca155ba | 81 | if (!isOK) \r |
125a98a1 | 82 | PrintAndLog("Found valid key:%012"llx, r_key);\r |
9ca155ba | 83 | else\r |
aea4d766 | 84 | {\r |
1c611bbd | 85 | if (isOK != 2) PrintAndLog("Found invalid key. "); \r |
86 | PrintAndLog("Failing is expected to happen in 25%% of all cases. Trying again with a different reader nonce...");\r | |
87 | c.arg[0] = false;\r | |
aea4d766 | 88 | goto start;\r |
89 | }\r | |
9ca155ba M |
90 | \r |
91 | return 0;\r | |
92 | }\r | |
93 | \r | |
94 | int CmdHF14AMfWrBl(const char *Cmd)\r | |
95 | {\r | |
96 | uint8_t blockNo = 0;\r | |
97 | uint8_t keyType = 0;\r | |
98 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
99 | uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r | |
100 | \r | |
101 | char cmdp = 0x00;\r | |
102 | \r | |
103 | if (strlen(Cmd)<3) {\r | |
104 | PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");\r | |
105 | PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");\r | |
106 | return 0;\r | |
107 | } \r | |
108 | \r | |
109 | blockNo = param_get8(Cmd, 0);\r | |
110 | cmdp = param_getchar(Cmd, 1);\r | |
111 | if (cmdp == 0x00) {\r | |
112 | PrintAndLog("Key type must be A or B");\r | |
113 | return 1;\r | |
114 | }\r | |
115 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
116 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
117 | PrintAndLog("Key must include 12 HEX symbols");\r | |
118 | return 1;\r | |
119 | }\r | |
120 | if (param_gethex(Cmd, 3, bldata, 32)) {\r | |
121 | PrintAndLog("Block data must include 32 HEX symbols");\r | |
122 | return 1;\r | |
123 | }\r | |
baeaf579 | 124 | PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo, keyType?'B':'A', sprint_hex(key, 6));\r |
9ca155ba M |
125 | PrintAndLog("--data: %s", sprint_hex(bldata, 16));\r |
126 | \r | |
127 | UsbCommand c = {CMD_MIFARE_WRITEBL, {blockNo, keyType, 0}};\r | |
128 | memcpy(c.d.asBytes, key, 6);\r | |
129 | memcpy(c.d.asBytes + 10, bldata, 16);\r | |
130 | SendCommand(&c);\r | |
9ca155ba | 131 | \r |
902cb3c0 | 132 | UsbCommand resp;\r |
133 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
134 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
9ca155ba M |
135 | PrintAndLog("isOk:%02x", isOK);\r |
136 | } else {\r | |
137 | PrintAndLog("Command execute timeout");\r | |
138 | }\r | |
139 | \r | |
d2f487af | 140 | return 0;\r |
141 | }\r | |
142 | \r | |
f38a1528 | 143 | /* dublett finns i CMDHFMFU.C \r |
d2f487af | 144 | int CmdHF14AMfUWrBl(const char *Cmd)\r |
145 | {\r | |
1801456e P |
146 | uint8_t blockNo = 0;\r |
147 | bool chinese_card=0;\r | |
148 | uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r | |
d2f487af | 149 | UsbCommand resp;\r |
1801456e P |
150 | \r |
151 | if (strlen(Cmd)<3) {\r | |
152 | PrintAndLog("Usage: hf mf uwrbl <block number> <block data (8 hex symbols)> <w>");\r | |
153 | PrintAndLog(" sample: hf mf uwrbl 0 01020304");\r | |
154 | return 0;\r | |
155 | } \r | |
156 | \r | |
157 | blockNo = param_get8(Cmd, 0);\r | |
158 | if (param_gethex(Cmd, 1, bldata, 8)) {\r | |
159 | PrintAndLog("Block data must include 8 HEX symbols");\r | |
160 | return 1;\r | |
161 | }\r | |
162 | \r | |
163 | if (strchr(Cmd,'w') != 0) {\r | |
164 | chinese_card=1;\r | |
165 | }\r | |
166 | \r | |
167 | switch(blockNo){\r | |
168 | case 0:\r | |
169 | if (!chinese_card){\r | |
170 | PrintAndLog("Access Denied");\r | |
171 | }else{\r | |
baeaf579 | 172 | PrintAndLog("--specialblock no:%d", blockNo);\r |
1801456e P |
173 | PrintAndLog("--data: %s", sprint_hex(bldata, 4));\r |
174 | UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};\r | |
175 | memcpy(d.d.asBytes,bldata, 4);\r | |
176 | SendCommand(&d);\r | |
177 | \r | |
178 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
179 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
180 | PrintAndLog("isOk:%02x", isOK);\r | |
181 | } else {\r | |
182 | PrintAndLog("Command execute timeout");\r | |
183 | }\r | |
184 | }\r | |
185 | break;\r | |
186 | case 1:\r | |
187 | if (!chinese_card){\r | |
188 | PrintAndLog("Access Denied");\r | |
189 | }else{\r | |
baeaf579 | 190 | PrintAndLog("--specialblock no:%d", blockNo);\r |
1801456e P |
191 | PrintAndLog("--data: %s", sprint_hex(bldata, 4));\r |
192 | UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};\r | |
193 | memcpy(d.d.asBytes,bldata, 4);\r | |
194 | SendCommand(&d);\r | |
195 | \r | |
196 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
197 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
198 | PrintAndLog("isOk:%02x", isOK);\r | |
199 | } else {\r | |
200 | PrintAndLog("Command execute timeout");\r | |
201 | }\r | |
202 | }\r | |
203 | break;\r | |
204 | case 2:\r | |
205 | if (!chinese_card){\r | |
206 | PrintAndLog("Access Denied");\r | |
207 | }else{\r | |
baeaf579 | 208 | PrintAndLog("--specialblock no:%d", blockNo);\r |
1801456e P |
209 | PrintAndLog("--data: %s", sprint_hex(bldata, 4));\r |
210 | UsbCommand c = {CMD_MIFAREU_WRITEBL, {blockNo}};\r | |
211 | memcpy(c.d.asBytes, bldata, 4);\r | |
212 | SendCommand(&c);\r | |
213 | \r | |
214 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
215 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
216 | PrintAndLog("isOk:%02x", isOK);\r | |
217 | } else {\r | |
218 | PrintAndLog("Command execute timeout");\r | |
219 | }\r | |
220 | }\r | |
221 | break;\r | |
222 | case 3:\r | |
baeaf579 | 223 | PrintAndLog("--specialblock no:%d", blockNo);\r |
1801456e P |
224 | PrintAndLog("--data: %s", sprint_hex(bldata, 4));\r |
225 | UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};\r | |
226 | memcpy(d.d.asBytes,bldata, 4);\r | |
227 | SendCommand(&d);\r | |
228 | \r | |
229 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
230 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
231 | PrintAndLog("isOk:%02x", isOK);\r | |
232 | } else {\r | |
233 | PrintAndLog("Command execute timeout");\r | |
234 | }\r | |
235 | break;\r | |
236 | default: \r | |
baeaf579 | 237 | PrintAndLog("--block no:%d", blockNo);\r |
1801456e P |
238 | PrintAndLog("--data: %s", sprint_hex(bldata, 4)); \r |
239 | UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}};\r | |
240 | memcpy(e.d.asBytes,bldata, 4);\r | |
241 | SendCommand(&e);\r | |
242 | \r | |
243 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
244 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
245 | PrintAndLog("isOk:%02x", isOK);\r | |
246 | } else {\r | |
247 | PrintAndLog("Command execute timeout");\r | |
248 | }\r | |
249 | break;\r | |
d2f487af | 250 | }\r |
1801456e | 251 | return 0;\r |
d2f487af | 252 | }\r |
f38a1528 | 253 | */\r |
d2f487af | 254 | int CmdHF14AMfRdBl(const char *Cmd)\r |
255 | {\r | |
256 | uint8_t blockNo = 0;\r | |
9ca155ba M |
257 | uint8_t keyType = 0;\r |
258 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
259 | \r | |
260 | char cmdp = 0x00;\r | |
261 | \r | |
262 | \r | |
263 | if (strlen(Cmd)<3) {\r | |
264 | PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");\r | |
265 | PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");\r | |
266 | return 0;\r | |
267 | } \r | |
268 | \r | |
269 | blockNo = param_get8(Cmd, 0);\r | |
270 | cmdp = param_getchar(Cmd, 1);\r | |
271 | if (cmdp == 0x00) {\r | |
272 | PrintAndLog("Key type must be A or B");\r | |
273 | return 1;\r | |
274 | }\r | |
275 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
276 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
277 | PrintAndLog("Key must include 12 HEX symbols");\r | |
278 | return 1;\r | |
279 | }\r | |
baeaf579 | 280 | PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo, keyType?'B':'A', sprint_hex(key, 6));\r |
9ca155ba M |
281 | \r |
282 | UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};\r | |
283 | memcpy(c.d.asBytes, key, 6);\r | |
284 | SendCommand(&c);\r | |
9ca155ba | 285 | \r |
902cb3c0 | 286 | UsbCommand resp;\r |
287 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
baeaf579 | 288 | uint8_t isOK = resp.arg[0] & 0xff;\r |
289 | uint8_t *data = resp.d.asBytes;\r | |
9ca155ba M |
290 | \r |
291 | if (isOK)\r | |
292 | PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 16));\r | |
293 | else\r | |
294 | PrintAndLog("isOk:%02x", isOK);\r | |
295 | } else {\r | |
296 | PrintAndLog("Command execute timeout");\r | |
297 | }\r | |
298 | \r | |
d2f487af | 299 | return 0;\r |
300 | }\r | |
301 | \r | |
f38a1528 | 302 | /* dublett finns i CMDHFMFU.C \r |
d2f487af | 303 | int CmdHF14AMfURdBl(const char *Cmd)\r |
304 | {\r | |
baeaf579 | 305 | uint8_t blockNo = 0;\r |
d2f487af | 306 | \r |
baeaf579 | 307 | if (strlen(Cmd)<1) {\r |
308 | PrintAndLog("Usage: hf mf urdbl <block number>");\r | |
309 | PrintAndLog(" sample: hf mf urdbl 0");\r | |
310 | return 0;\r | |
311 | } \r | |
d2f487af | 312 | \r |
baeaf579 | 313 | blockNo = param_get8(Cmd, 0);\r |
314 | PrintAndLog("--block no:%d", blockNo);\r | |
d2f487af | 315 | \r |
baeaf579 | 316 | UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}};\r |
317 | SendCommand(&c);\r | |
d2f487af | 318 | \r |
baeaf579 | 319 | UsbCommand resp;\r |
320 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
321 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
322 | uint8_t *data = resp.d.asBytes;\r | |
323 | \r | |
324 | if (isOK)\r | |
325 | PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 4));\r | |
326 | else\r | |
327 | PrintAndLog("isOk:%02x", isOK);\r | |
328 | } else {\r | |
329 | PrintAndLog("Command execute timeout");\r | |
330 | }\r | |
d2f487af | 331 | \r |
baeaf579 | 332 | return 0;\r |
d2f487af | 333 | }\r |
f38a1528 | 334 | */\r |
d2f487af | 335 | \r |
f38a1528 | 336 | /* dublett finns i CMDHFMFU.C \r |
d2f487af | 337 | int CmdHF14AMfURdCard(const char *Cmd)\r |
338 | {\r | |
baeaf579 | 339 | int i;\r |
340 | uint8_t sectorNo = 0;\r | |
d2f487af | 341 | uint8_t *lockbytes_t=NULL;\r |
342 | uint8_t lockbytes[2]={0,0};\r | |
343 | bool bit[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};\r | |
344 | \r | |
baeaf579 | 345 | uint8_t isOK = 0;\r |
346 | uint8_t * data = NULL;\r | |
d2f487af | 347 | \r |
baeaf579 | 348 | if (sectorNo > 15) {\r |
349 | PrintAndLog("Sector number must be less than 16");\r | |
350 | return 1;\r | |
351 | }\r | |
352 | PrintAndLog("Attempting to Read Ultralight... ");\r | |
d2f487af | 353 | \r |
354 | UsbCommand c = {CMD_MIFAREU_READCARD, {sectorNo}};\r | |
355 | SendCommand(&c);\r | |
356 | \r | |
baeaf579 | 357 | UsbCommand resp;\r |
358 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
359 | isOK = resp.arg[0] & 0xff;\r | |
360 | data = resp.d.asBytes;\r | |
361 | \r | |
362 | PrintAndLog("isOk:%02x", isOK);\r | |
363 | if (isOK) \r | |
364 | for (i = 0; i < 16; i++) {\r | |
365 | switch(i){\r | |
366 | case 2:\r | |
d2f487af | 367 | //process lock bytes\r |
368 | lockbytes_t=data+(i*4);\r | |
369 | lockbytes[0]=lockbytes_t[2];\r | |
370 | lockbytes[1]=lockbytes_t[3];\r | |
371 | for(int j=0; j<16; j++){\r | |
372 | bit[j]=lockbytes[j/8] & ( 1 <<(7-j%8));\r | |
373 | }\r | |
374 | //PrintAndLog("LB %02x %02x", lockbytes[0],lockbytes[1]);\r | |
375 | //PrintAndLog("LB2b %02x %02x %02x %02x %02x %02x %02x %02x",bit[8],bit[9],bit[10],bit[11],bit[12],bit[13],bit[14],bit[15]); \r | |
baeaf579 | 376 | PrintAndLog("Block %3d:%s ", i,sprint_hex(data + i * 4, 4));\r |
d2f487af | 377 | break;\r |
baeaf579 | 378 | case 3: \r |
379 | PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[4]);\r | |
d2f487af | 380 | break;\r |
baeaf579 | 381 | case 4:\r |
382 | PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[3]);\r | |
d2f487af | 383 | break;\r |
baeaf579 | 384 | case 5:\r |
385 | PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[2]);\r | |
d2f487af | 386 | break;\r |
baeaf579 | 387 | case 6:\r |
388 | PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[1]);\r | |
d2f487af | 389 | break;\r |
baeaf579 | 390 | case 7:\r |
391 | PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[0]);\r | |
d2f487af | 392 | break;\r |
baeaf579 | 393 | case 8:\r |
394 | PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[15]);\r | |
d2f487af | 395 | break;\r |
baeaf579 | 396 | case 9:\r |
397 | PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[14]);\r | |
d2f487af | 398 | break;\r |
baeaf579 | 399 | case 10:\r |
400 | PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[13]);\r | |
d2f487af | 401 | break;\r |
baeaf579 | 402 | case 11:\r |
403 | PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[12]);\r | |
d2f487af | 404 | break;\r |
baeaf579 | 405 | case 12:\r |
406 | PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[11]);\r | |
d2f487af | 407 | break;\r |
baeaf579 | 408 | case 13:\r |
409 | PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[10]);\r | |
d2f487af | 410 | break;\r |
baeaf579 | 411 | case 14:\r |
412 | PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[9]);\r | |
d2f487af | 413 | break;\r |
baeaf579 | 414 | case 15:\r |
415 | PrintAndLog("Block %3d:%s [%d]", i,sprint_hex(data + i * 4, 4),bit[8]);\r | |
d2f487af | 416 | break;\r |
baeaf579 | 417 | default:\r |
418 | PrintAndLog("Block %3d:%s ", i,sprint_hex(data + i * 4, 4));\r | |
d2f487af | 419 | break;\r |
420 | }\r | |
421 | }\r | |
422 | } else {\r | |
baeaf579 | 423 | PrintAndLog("Command execute timeout");\r |
d2f487af | 424 | }\r |
425 | return 0;\r | |
426 | }\r | |
f38a1528 | 427 | */\r |
baeaf579 | 428 | \r |
d2f487af | 429 | int CmdHF14AMfRdSc(const char *Cmd)\r |
430 | {\r | |
431 | int i;\r | |
9ca155ba M |
432 | uint8_t sectorNo = 0;\r |
433 | uint8_t keyType = 0;\r | |
434 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
9ca155ba | 435 | uint8_t isOK = 0;\r |
baeaf579 | 436 | uint8_t *data = NULL;\r |
9ca155ba M |
437 | char cmdp = 0x00;\r |
438 | \r | |
439 | if (strlen(Cmd)<3) {\r | |
440 | PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");\r | |
441 | PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");\r | |
442 | return 0;\r | |
443 | } \r | |
444 | \r | |
445 | sectorNo = param_get8(Cmd, 0);\r | |
baeaf579 | 446 | if (sectorNo > 39) {\r |
447 | PrintAndLog("Sector number must be less than 40");\r | |
9ca155ba M |
448 | return 1;\r |
449 | }\r | |
450 | cmdp = param_getchar(Cmd, 1);\r | |
baeaf579 | 451 | if (cmdp != 'a' && cmdp != 'A' && cmdp != 'b' && cmdp != 'B') {\r |
9ca155ba M |
452 | PrintAndLog("Key type must be A or B");\r |
453 | return 1;\r | |
454 | }\r | |
455 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
456 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
457 | PrintAndLog("Key must include 12 HEX symbols");\r | |
458 | return 1;\r | |
459 | }\r | |
baeaf579 | 460 | PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo, keyType?'B':'A', sprint_hex(key, 6));\r |
9ca155ba | 461 | \r |
baeaf579 | 462 | UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};\r |
9ca155ba | 463 | memcpy(c.d.asBytes, key, 6);\r |
baeaf579 | 464 | SendCommand(&c);\r |
9ca155ba M |
465 | PrintAndLog(" ");\r |
466 | \r | |
902cb3c0 | 467 | UsbCommand resp;\r |
468 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
469 | isOK = resp.arg[0] & 0xff;\r | |
470 | data = resp.d.asBytes;\r | |
9ca155ba M |
471 | \r |
472 | PrintAndLog("isOk:%02x", isOK);\r | |
baeaf579 | 473 | if (isOK) {\r |
474 | for (i = 0; i < (sectorNo<32?3:15); i++) {\r | |
475 | PrintAndLog("data : %s", sprint_hex(data + i * 16, 16));\r | |
9ca155ba | 476 | }\r |
baeaf579 | 477 | PrintAndLog("trailer: %s", sprint_hex(data + (sectorNo<32?3:15) * 16, 16));\r |
478 | }\r | |
9ca155ba | 479 | } else {\r |
baeaf579 | 480 | PrintAndLog("Command execute timeout");\r |
9ca155ba M |
481 | }\r |
482 | \r | |
baeaf579 | 483 | return 0;\r |
484 | }\r | |
9ca155ba | 485 | \r |
baeaf579 | 486 | \r |
487 | uint8_t FirstBlockOfSector(uint8_t sectorNo)\r | |
488 | {\r | |
489 | if (sectorNo < 32) {\r | |
490 | return sectorNo * 4;\r | |
9ca155ba | 491 | } else {\r |
baeaf579 | 492 | return 32 * 4 + (sectorNo - 32) * 16;\r |
9ca155ba | 493 | }\r |
9ca155ba M |
494 | }\r |
495 | \r | |
baeaf579 | 496 | \r |
497 | uint8_t NumBlocksPerSector(uint8_t sectorNo)\r | |
498 | {\r | |
499 | if (sectorNo < 32) {\r | |
500 | return 4;\r | |
501 | } else {\r | |
502 | return 16;\r | |
503 | }\r | |
504 | }\r | |
505 | \r | |
506 | \r | |
aea4d766 | 507 | int CmdHF14AMfDump(const char *Cmd)\r |
26fdb4ab | 508 | {\r |
baeaf579 | 509 | uint8_t sectorNo, blockNo;\r |
26fdb4ab | 510 | \r |
aea4d766 | 511 | uint8_t keyA[40][6];\r |
512 | uint8_t keyB[40][6];\r | |
513 | uint8_t rights[40][4];\r | |
79db03ef | 514 | uint8_t carddata[256][16];\r |
baeaf579 | 515 | uint8_t numSectors = 16;\r |
26fdb4ab | 516 | \r |
26fdb4ab | 517 | FILE *fin;\r |
518 | FILE *fout;\r | |
519 | \r | |
902cb3c0 | 520 | UsbCommand resp;\r |
baeaf579 | 521 | \r |
f38a1528 | 522 | int size = GetCardSize(); \r |
baeaf579 | 523 | char cmdp = param_getchar(Cmd, 0);\r |
f38a1528 | 524 | \r |
525 | PrintAndLog("Got %d",size);\r | |
526 | \r | |
527 | return;\r | |
528 | \r | |
529 | if ( size > -1) \r | |
530 | cmdp = (char)48+size;\r | |
531 | \r | |
baeaf579 | 532 | switch (cmdp) {\r |
533 | case '0' : numSectors = 5; break;\r | |
534 | case '1' : \r | |
535 | case '\0': numSectors = 16; break;\r | |
536 | case '2' : numSectors = 32; break;\r | |
537 | case '4' : numSectors = 40; break;\r | |
538 | default: numSectors = 16;\r | |
539 | } \r | |
540 | \r | |
541 | if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {\r | |
542 | PrintAndLog("Usage: hf mf dump [card memory]");\r | |
543 | PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r | |
544 | PrintAndLog("");\r | |
545 | PrintAndLog("Samples: hf mf dump");\r | |
546 | PrintAndLog(" hf mf dump 4");\r | |
547 | return 0;\r | |
548 | }\r | |
26fdb4ab | 549 | \r |
550 | if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {\r | |
aea4d766 | 551 | PrintAndLog("Could not find file dumpkeys.bin");\r |
26fdb4ab | 552 | return 1;\r |
553 | }\r | |
554 | \r | |
f38a1528 | 555 | // Read keys A from file\r |
baeaf579 | 556 | for (sectorNo=0; sectorNo<numSectors; sectorNo++) {\r |
557 | if (fread( keyA[sectorNo], 1, 6, fin ) == 0) {\r | |
558 | PrintAndLog("File reading error.");\r | |
759c16b3 | 559 | return 2;\r |
baeaf579 | 560 | }\r |
26fdb4ab | 561 | }\r |
baeaf579 | 562 | \r |
f38a1528 | 563 | // Read keys B from file\r |
baeaf579 | 564 | for (sectorNo=0; sectorNo<numSectors; sectorNo++) {\r |
565 | if (fread( keyB[sectorNo], 1, 6, fin ) == 0) {\r | |
566 | PrintAndLog("File reading error.");\r | |
759c16b3 | 567 | return 2;\r |
baeaf579 | 568 | }\r |
26fdb4ab | 569 | }\r |
570 | \r | |
26fdb4ab | 571 | PrintAndLog("|-----------------------------------------|");\r |
572 | PrintAndLog("|------ Reading sector access bits...-----|");\r | |
573 | PrintAndLog("|-----------------------------------------|");\r | |
574 | \r | |
baeaf579 | 575 | for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r |
576 | UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 0, 0}};\r | |
577 | memcpy(c.d.asBytes, keyA[sectorNo], 6);\r | |
26fdb4ab | 578 | SendCommand(&c);\r |
26fdb4ab | 579 | \r |
baeaf579 | 580 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r |
902cb3c0 | 581 | uint8_t isOK = resp.arg[0] & 0xff;\r |
582 | uint8_t *data = resp.d.asBytes;\r | |
26fdb4ab | 583 | if (isOK){\r |
c626c56e | 584 | rights[sectorNo][0] = ((data[7] & 0x10)>>2) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>4); // C1C2C3 for data area 0\r |
585 | rights[sectorNo][1] = ((data[7] & 0x20)>>3) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>5); // C1C2C3 for data area 1\r | |
586 | rights[sectorNo][2] = ((data[7] & 0x40)>>4) | ((data[8] & 0x4)>>1) | ((data[8] & 0x40)>>6); // C1C2C3 for data area 2\r | |
587 | rights[sectorNo][3] = ((data[7] & 0x80)>>5) | ((data[8] & 0x8)>>2) | ((data[8] & 0x80)>>7); // C1C2C3 for sector trailer\r | |
baeaf579 | 588 | } else {\r |
79db03ef | 589 | PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo);\r |
c626c56e | 590 | rights[sectorNo][0] = rights[sectorNo][1] = rights[sectorNo][2] = 0x00;\r |
591 | rights[sectorNo][3] = 0x01;\r | |
26fdb4ab | 592 | }\r |
baeaf579 | 593 | } else {\r |
79db03ef | 594 | PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo);\r |
c626c56e | 595 | rights[sectorNo][0] = rights[sectorNo][1] = rights[sectorNo][2] = 0x00;\r |
596 | rights[sectorNo][3] = 0x01;\r | |
26fdb4ab | 597 | }\r |
598 | }\r | |
599 | \r | |
26fdb4ab | 600 | PrintAndLog("|-----------------------------------------|");\r |
601 | PrintAndLog("|----- Dumping all blocks to file... -----|");\r | |
602 | PrintAndLog("|-----------------------------------------|");\r | |
603 | \r | |
79db03ef | 604 | bool isOK = true;\r |
605 | for (sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {\r | |
606 | for (blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r | |
baeaf579 | 607 | bool received = false;\r |
f38a1528 | 608 | \r |
baeaf579 | 609 | if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer. At least the Access Conditions can always be read with key A. \r |
610 | UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 0, 0}};\r | |
611 | memcpy(c.d.asBytes, keyA[sectorNo], 6);\r | |
26fdb4ab | 612 | SendCommand(&c);\r |
baeaf579 | 613 | received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r |
614 | } else { // data block. Check if it can be read with key A or key B\r | |
615 | uint8_t data_area = sectorNo<32?blockNo:blockNo/5;\r | |
c626c56e | 616 | if ((rights[sectorNo][data_area] == 0x03) || (rights[sectorNo][data_area] == 0x05)) { // only key B would work\r |
baeaf579 | 617 | UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 1, 0}};\r |
618 | memcpy(c.d.asBytes, keyB[sectorNo], 6);\r | |
26fdb4ab | 619 | SendCommand(&c);\r |
baeaf579 | 620 | received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r |
c626c56e | 621 | } else if (rights[sectorNo][data_area] == 0x07) { // no key would work\r |
79db03ef | 622 | isOK = false;\r |
baeaf579 | 623 | PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo, blockNo);\r |
624 | } else { // key A would work\r | |
625 | UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 0, 0}};\r | |
626 | memcpy(c.d.asBytes, keyA[sectorNo], 6);\r | |
627 | SendCommand(&c);\r | |
628 | received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r | |
26fdb4ab | 629 | }\r |
630 | }\r | |
631 | \r | |
902cb3c0 | 632 | if (received) {\r |
79db03ef | 633 | isOK = resp.arg[0] & 0xff;\r |
902cb3c0 | 634 | uint8_t *data = resp.d.asBytes;\r |
baeaf579 | 635 | if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer. Fill in the keys.\r |
636 | data[0] = (keyA[sectorNo][0]);\r | |
637 | data[1] = (keyA[sectorNo][1]);\r | |
638 | data[2] = (keyA[sectorNo][2]);\r | |
639 | data[3] = (keyA[sectorNo][3]);\r | |
640 | data[4] = (keyA[sectorNo][4]);\r | |
641 | data[5] = (keyA[sectorNo][5]);\r | |
642 | data[10] = (keyB[sectorNo][0]);\r | |
643 | data[11] = (keyB[sectorNo][1]);\r | |
644 | data[12] = (keyB[sectorNo][2]);\r | |
645 | data[13] = (keyB[sectorNo][3]);\r | |
646 | data[14] = (keyB[sectorNo][4]);\r | |
647 | data[15] = (keyB[sectorNo][5]);\r | |
3d77fdfa | 648 | }\r |
26fdb4ab | 649 | if (isOK) {\r |
79db03ef | 650 | memcpy(carddata[FirstBlockOfSector(sectorNo) + blockNo], data, 16);\r |
651 | PrintAndLog("Successfully read block %2d of sector %2d.", blockNo, sectorNo);\r | |
baeaf579 | 652 | } else {\r |
653 | PrintAndLog("Could not read block %2d of sector %2d", blockNo, sectorNo);\r | |
79db03ef | 654 | break;\r |
26fdb4ab | 655 | }\r |
656 | }\r | |
657 | else {\r | |
79db03ef | 658 | isOK = false;\r |
659 | PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo, sectorNo);\r | |
660 | break;\r | |
26fdb4ab | 661 | }\r |
662 | }\r | |
663 | }\r | |
79db03ef | 664 | \r |
665 | if (isOK) {\r | |
666 | if ((fout = fopen("dumpdata.bin","wb")) == NULL) { \r | |
667 | PrintAndLog("Could not create file name dumpdata.bin");\r | |
668 | return 1;\r | |
669 | }\r | |
670 | uint16_t numblocks = FirstBlockOfSector(numSectors - 1) + NumBlocksPerSector(numSectors - 1);\r | |
671 | fwrite(carddata, 1, 16*numblocks, fout);\r | |
672 | fclose(fout);\r | |
673 | PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks, 16*numblocks);\r | |
674 | }\r | |
26fdb4ab | 675 | \r |
676 | fclose(fin);\r | |
baeaf579 | 677 | return 0;\r |
26fdb4ab | 678 | }\r |
679 | \r | |
baeaf579 | 680 | \r |
aea4d766 | 681 | int CmdHF14AMfRestore(const char *Cmd)\r |
26fdb4ab | 682 | {\r |
683 | \r | |
baeaf579 | 684 | uint8_t sectorNo,blockNo;\r |
26fdb4ab | 685 | uint8_t keyType = 0;\r |
686 | uint8_t key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r | |
687 | uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r | |
baeaf579 | 688 | uint8_t keyA[40][6];\r |
689 | uint8_t keyB[40][6];\r | |
690 | uint8_t numSectors;\r | |
26fdb4ab | 691 | \r |
26fdb4ab | 692 | FILE *fdump;\r |
693 | FILE *fkeys;\r | |
baeaf579 | 694 | \r |
695 | char cmdp = param_getchar(Cmd, 0);\r | |
696 | switch (cmdp) {\r | |
697 | case '0' : numSectors = 5; break;\r | |
698 | case '1' : \r | |
699 | case '\0': numSectors = 16; break;\r | |
700 | case '2' : numSectors = 32; break;\r | |
701 | case '4' : numSectors = 40; break;\r | |
702 | default: numSectors = 16;\r | |
703 | } \r | |
704 | \r | |
705 | if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {\r | |
706 | PrintAndLog("Usage: hf mf restore [card memory]");\r | |
707 | PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r | |
708 | PrintAndLog("");\r | |
709 | PrintAndLog("Samples: hf mf restore");\r | |
710 | PrintAndLog(" hf mf restore 4");\r | |
711 | return 0;\r | |
712 | }\r | |
713 | \r | |
26fdb4ab | 714 | if ((fdump = fopen("dumpdata.bin","rb")) == NULL) {\r |
aea4d766 | 715 | PrintAndLog("Could not find file dumpdata.bin");\r |
26fdb4ab | 716 | return 1;\r |
717 | }\r | |
718 | if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) {\r | |
aea4d766 | 719 | PrintAndLog("Could not find file dumpkeys.bin");\r |
26fdb4ab | 720 | return 1;\r |
721 | }\r | |
722 | \r | |
baeaf579 | 723 | for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r |
724 | if (fread(keyA[sectorNo], 1, 6, fkeys) == 0) {\r | |
725 | PrintAndLog("File reading error (dumpkeys.bin).");\r | |
759c16b3 | 726 | return 2;\r |
baeaf579 | 727 | }\r |
26fdb4ab | 728 | }\r |
baeaf579 | 729 | \r |
730 | for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r | |
731 | if (fread(keyB[sectorNo], 1, 6, fkeys) == 0) {\r | |
732 | PrintAndLog("File reading error (dumpkeys.bin).");\r | |
759c16b3 | 733 | return 2;\r |
baeaf579 | 734 | }\r |
26fdb4ab | 735 | }\r |
79db03ef | 736 | \r |
9d710943 | 737 | PrintAndLog("Restoring dumpdata.bin to card");\r |
26fdb4ab | 738 | \r |
baeaf579 | 739 | for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r |
740 | for(blockNo = 0; blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r | |
741 | UsbCommand c = {CMD_MIFARE_WRITEBL, {FirstBlockOfSector(sectorNo) + blockNo, keyType, 0}};\r | |
26fdb4ab | 742 | memcpy(c.d.asBytes, key, 6);\r |
743 | \r | |
759c16b3 | 744 | if (fread(bldata, 1, 16, fdump) == 0) {\r |
baeaf579 | 745 | PrintAndLog("File reading error (dumpdata.bin).");\r |
746 | return 2;\r | |
747 | }\r | |
26fdb4ab | 748 | \r |
baeaf579 | 749 | if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer\r |
750 | bldata[0] = (keyA[sectorNo][0]);\r | |
751 | bldata[1] = (keyA[sectorNo][1]);\r | |
752 | bldata[2] = (keyA[sectorNo][2]);\r | |
753 | bldata[3] = (keyA[sectorNo][3]);\r | |
754 | bldata[4] = (keyA[sectorNo][4]);\r | |
755 | bldata[5] = (keyA[sectorNo][5]);\r | |
756 | bldata[10] = (keyB[sectorNo][0]);\r | |
757 | bldata[11] = (keyB[sectorNo][1]);\r | |
758 | bldata[12] = (keyB[sectorNo][2]);\r | |
759 | bldata[13] = (keyB[sectorNo][3]);\r | |
760 | bldata[14] = (keyB[sectorNo][4]);\r | |
761 | bldata[15] = (keyB[sectorNo][5]);\r | |
26fdb4ab | 762 | } \r |
763 | \r | |
baeaf579 | 764 | PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo) + blockNo, sprint_hex(bldata, 16));\r |
26fdb4ab | 765 | \r |
766 | memcpy(c.d.asBytes + 10, bldata, 16);\r | |
767 | SendCommand(&c);\r | |
26fdb4ab | 768 | \r |
902cb3c0 | 769 | UsbCommand resp;\r |
baeaf579 | 770 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r |
902cb3c0 | 771 | uint8_t isOK = resp.arg[0] & 0xff;\r |
26fdb4ab | 772 | PrintAndLog("isOk:%02x", isOK);\r |
773 | } else {\r | |
774 | PrintAndLog("Command execute timeout");\r | |
775 | }\r | |
776 | }\r | |
777 | }\r | |
778 | \r | |
779 | fclose(fdump);\r | |
780 | fclose(fkeys);\r | |
781 | return 0;\r | |
782 | }\r | |
783 | \r | |
baeaf579 | 784 | \r |
9ca155ba M |
785 | int CmdHF14AMfNested(const char *Cmd)\r |
786 | {\r | |
787 | int i, j, res, iterations;\r | |
baeaf579 | 788 | sector *e_sector = NULL;\r |
9ca155ba M |
789 | uint8_t blockNo = 0;\r |
790 | uint8_t keyType = 0;\r | |
791 | uint8_t trgBlockNo = 0;\r | |
792 | uint8_t trgKeyType = 0;\r | |
baeaf579 | 793 | uint8_t SectorsCnt = 0;\r |
9ca155ba | 794 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r |
4a79e52c | 795 | uint8_t keyBlock[13*6];\r |
9ca155ba | 796 | uint64_t key64 = 0;\r |
baeaf579 | 797 | bool transferToEml = false;\r |
9ca155ba | 798 | \r |
baeaf579 | 799 | bool createDumpFile = false;\r |
26fdb4ab | 800 | FILE *fkeys;\r |
21156267 | 801 | uint8_t standart[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r |
802 | uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r | |
26fdb4ab | 803 | \r |
9ca155ba M |
804 | char cmdp, ctmp;\r |
805 | \r | |
806 | if (strlen(Cmd)<3) {\r | |
807 | PrintAndLog("Usage:");\r | |
26fdb4ab | 808 | PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t,d]");\r |
0014cb46 M |
809 | PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");\r |
810 | PrintAndLog(" <target block number> <target key A/B> [t]");\r | |
9ca155ba | 811 | PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r |
8556b852 | 812 | PrintAndLog("t - transfer keys into emulator memory");\r |
26fdb4ab | 813 | PrintAndLog("d - write keys to binary file");\r |
9ca155ba M |
814 | PrintAndLog(" ");\r |
815 | PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");\r | |
79db03ef | 816 | PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");\r |
817 | PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");\r | |
818 | PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");\r | |
9ca155ba M |
819 | return 0;\r |
820 | } \r | |
821 | \r | |
822 | cmdp = param_getchar(Cmd, 0);\r | |
823 | blockNo = param_get8(Cmd, 1);\r | |
824 | ctmp = param_getchar(Cmd, 2);\r | |
f38a1528 | 825 | \r |
baeaf579 | 826 | if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r |
9ca155ba M |
827 | PrintAndLog("Key type must be A or B");\r |
828 | return 1;\r | |
829 | }\r | |
f38a1528 | 830 | \r |
831 | if (ctmp != 'A' && ctmp != 'a') \r | |
832 | keyType = 1;\r | |
833 | \r | |
9ca155ba M |
834 | if (param_gethex(Cmd, 3, key, 12)) {\r |
835 | PrintAndLog("Key must include 12 HEX symbols");\r | |
836 | return 1;\r | |
837 | }\r | |
838 | \r | |
8556b852 | 839 | if (cmdp == 'o' || cmdp == 'O') {\r |
9ca155ba M |
840 | cmdp = 'o';\r |
841 | trgBlockNo = param_get8(Cmd, 4);\r | |
842 | ctmp = param_getchar(Cmd, 5);\r | |
baeaf579 | 843 | if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r |
9ca155ba M |
844 | PrintAndLog("Target key type must be A or B");\r |
845 | return 1;\r | |
846 | }\r | |
f38a1528 | 847 | if (ctmp != 'A' && ctmp != 'a') \r |
848 | trgKeyType = 1;\r | |
9ca155ba | 849 | } else {\r |
f38a1528 | 850 | \r |
851 | \r | |
852 | \r | |
9ca155ba M |
853 | switch (cmdp) {\r |
854 | case '0': SectorsCnt = 05; break;\r | |
855 | case '1': SectorsCnt = 16; break;\r | |
856 | case '2': SectorsCnt = 32; break;\r | |
baeaf579 | 857 | case '4': SectorsCnt = 40; break;\r |
9ca155ba M |
858 | default: SectorsCnt = 16;\r |
859 | }\r | |
860 | }\r | |
8556b852 M |
861 | \r |
862 | ctmp = param_getchar(Cmd, 4);\r | |
baeaf579 | 863 | if (ctmp == 't' || ctmp == 'T') transferToEml = true;\r |
864 | else if (ctmp == 'd' || ctmp == 'D') createDumpFile = true;\r | |
21156267 | 865 | \r |
8556b852 M |
866 | ctmp = param_getchar(Cmd, 6);\r |
867 | transferToEml |= (ctmp == 't' || ctmp == 'T');\r | |
21156267 | 868 | transferToEml |= (ctmp == 'd' || ctmp == 'D');\r |
9ca155ba | 869 | \r |
9ca155ba | 870 | if (cmdp == 'o') {\r |
baeaf579 | 871 | PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo, trgKeyType?'B':'A');\r |
9492e0b0 | 872 | if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, true)) {\r |
9ca155ba M |
873 | PrintAndLog("Nested error.");\r |
874 | return 2;\r | |
875 | }\r | |
9492e0b0 | 876 | key64 = bytes_to_num(keyBlock, 6);\r |
877 | if (key64) {\r | |
125a98a1 | 878 | PrintAndLog("Found valid key:%012"llx, key64);\r |
8556b852 M |
879 | \r |
880 | // transfer key to the emulator\r | |
881 | if (transferToEml) {\r | |
baeaf579 | 882 | uint8_t sectortrailer;\r |
883 | if (trgBlockNo < 32*4) { // 4 block sector\r | |
884 | sectortrailer = (trgBlockNo & 0x03) + 3;\r | |
885 | } else { // 16 block sector\r | |
886 | sectortrailer = (trgBlockNo & 0x0f) + 15;\r | |
887 | }\r | |
888 | mfEmlGetMem(keyBlock, sectortrailer, 1);\r | |
8556b852 M |
889 | \r |
890 | if (!trgKeyType)\r | |
891 | num_to_bytes(key64, 6, keyBlock);\r | |
892 | else\r | |
893 | num_to_bytes(key64, 6, &keyBlock[10]);\r | |
baeaf579 | 894 | mfEmlSetMem(keyBlock, sectortrailer, 1); \r |
8556b852 M |
895 | }\r |
896 | } else {\r | |
9ca155ba | 897 | PrintAndLog("No valid key found");\r |
8556b852 | 898 | }\r |
21156267 | 899 | }\r |
900 | else { // ------------------------------------ multiple sectors working\r | |
9492e0b0 | 901 | clock_t time1;\r |
902 | time1 = clock();\r | |
903 | \r | |
9ca155ba M |
904 | e_sector = calloc(SectorsCnt, sizeof(sector));\r |
905 | if (e_sector == NULL) return 1;\r | |
906 | \r | |
baeaf579 | 907 | //test current key and additional standard keys first\r |
9ca155ba | 908 | memcpy(keyBlock, key, 6);\r |
9492e0b0 | 909 | num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 1 * 6));\r |
910 | num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 2 * 6));\r | |
911 | num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 3 * 6));\r | |
912 | num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 4 * 6));\r | |
9ca155ba | 913 | num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));\r |
4a79e52c | 914 | num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock + 6 * 6));\r |
915 | num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock + 7 * 6));\r | |
916 | num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock + 8 * 6));\r | |
917 | num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock + 9 * 6));\r | |
918 | num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock + 10 * 6));\r | |
919 | num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock + 11 * 6));\r | |
920 | num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock + 12 * 6));\r | |
921 | num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock + 13 * 6));\r | |
9ca155ba M |
922 | \r |
923 | PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);\r | |
924 | for (i = 0; i < SectorsCnt; i++) {\r | |
925 | for (j = 0; j < 2; j++) {\r | |
926 | if (e_sector[i].foundKey[j]) continue;\r | |
927 | \r | |
baeaf579 | 928 | res = mfCheckKeys(FirstBlockOfSector(i), j, 6, keyBlock, &key64);\r |
9ca155ba M |
929 | \r |
930 | if (!res) {\r | |
931 | e_sector[i].Key[j] = key64;\r | |
932 | e_sector[i].foundKey[j] = 1;\r | |
933 | }\r | |
934 | }\r | |
9492e0b0 | 935 | }\r |
936 | \r | |
9ca155ba | 937 | \r |
9ca155ba M |
938 | // nested sectors\r |
939 | iterations = 0;\r | |
940 | PrintAndLog("nested...");\r | |
9492e0b0 | 941 | bool calibrate = true;\r |
9ca155ba | 942 | for (i = 0; i < NESTED_SECTOR_RETRY; i++) {\r |
baeaf579 | 943 | for (uint8_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r |
9ca155ba | 944 | for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) { \r |
baeaf579 | 945 | if (e_sector[sectorNo].foundKey[trgKeyType]) continue;\r |
9492e0b0 | 946 | PrintAndLog("-----------------------------------------------");\r |
baeaf579 | 947 | if(mfnested(blockNo, keyType, key, FirstBlockOfSector(sectorNo), trgKeyType, keyBlock, calibrate)) {\r |
9492e0b0 | 948 | PrintAndLog("Nested error.\n");\r |
949 | return 2;\r | |
950 | }\r | |
951 | else {\r | |
952 | calibrate = false;\r | |
953 | }\r | |
9ca155ba M |
954 | \r |
955 | iterations++;\r | |
9492e0b0 | 956 | \r |
957 | key64 = bytes_to_num(keyBlock, 6);\r | |
958 | if (key64) {\r | |
125a98a1 | 959 | PrintAndLog("Found valid key:%012"llx, key64);\r |
baeaf579 | 960 | e_sector[sectorNo].foundKey[trgKeyType] = 1;\r |
961 | e_sector[sectorNo].Key[trgKeyType] = key64;\r | |
9ca155ba M |
962 | }\r |
963 | }\r | |
9492e0b0 | 964 | }\r |
9ca155ba M |
965 | }\r |
966 | \r | |
a0041115 | 967 | printf("Time in nested: %1.3f (%1.3f sec per key)\n\n", ((float)clock() - time1)/CLOCKS_PER_SEC, ((float)clock() - time1)/iterations/CLOCKS_PER_SEC);\r |
9492e0b0 | 968 | \r |
969 | PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations);\r | |
9ca155ba M |
970 | //print them\r |
971 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
972 | PrintAndLog("|sec|key A |res|key B |res|");\r | |
973 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
974 | for (i = 0; i < SectorsCnt; i++) {\r | |
125a98a1 | 975 | PrintAndLog("|%03d| %012"llx" | %d | %012"llx" | %d |", i,\r |
9ca155ba M |
976 | e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);\r |
977 | }\r | |
978 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
979 | \r | |
8556b852 M |
980 | // transfer them to the emulator\r |
981 | if (transferToEml) {\r | |
982 | for (i = 0; i < SectorsCnt; i++) {\r | |
baeaf579 | 983 | mfEmlGetMem(keyBlock, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);\r |
8556b852 | 984 | if (e_sector[i].foundKey[0])\r |
ab8b654e | 985 | num_to_bytes(e_sector[i].Key[0], 6, keyBlock);\r |
8556b852 M |
986 | if (e_sector[i].foundKey[1])\r |
987 | num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);\r | |
baeaf579 | 988 | mfEmlSetMem(keyBlock, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);\r |
8556b852 M |
989 | } \r |
990 | }\r | |
991 | \r | |
21156267 | 992 | // Create dump file\r |
26fdb4ab | 993 | if (createDumpFile) {\r |
994 | if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) { \r | |
aea4d766 | 995 | PrintAndLog("Could not create file dumpkeys.bin");\r |
26fdb4ab | 996 | free(e_sector);\r |
997 | return 1;\r | |
998 | }\r | |
baeaf579 | 999 | PrintAndLog("Printing keys to binary file dumpkeys.bin...");\r |
1000 | for(i=0; i<SectorsCnt; i++) {\r | |
21156267 | 1001 | if (e_sector[i].foundKey[0]){\r |
1002 | num_to_bytes(e_sector[i].Key[0], 6, tempkey);\r | |
1003 | fwrite ( tempkey, 1, 6, fkeys );\r | |
1004 | }\r | |
1005 | else{\r | |
1006 | fwrite ( &standart, 1, 6, fkeys );\r | |
1007 | }\r | |
26fdb4ab | 1008 | }\r |
baeaf579 | 1009 | for(i=0; i<SectorsCnt; i++) {\r |
21156267 | 1010 | if (e_sector[i].foundKey[1]){\r |
1011 | num_to_bytes(e_sector[i].Key[1], 6, tempkey);\r | |
1012 | fwrite ( tempkey, 1, 6, fkeys );\r | |
1013 | }\r | |
1014 | else{\r | |
1015 | fwrite ( &standart, 1, 6, fkeys );\r | |
1016 | }\r | |
26fdb4ab | 1017 | }\r |
1018 | fclose(fkeys);\r | |
1019 | }\r | |
1020 | \r | |
9ca155ba M |
1021 | free(e_sector);\r |
1022 | }\r | |
1023 | \r | |
1024 | return 0;\r | |
1025 | }\r | |
1026 | \r | |
baeaf579 | 1027 | \r |
9ca155ba M |
1028 | int CmdHF14AMfChk(const char *Cmd)\r |
1029 | {\r | |
aea4d766 | 1030 | FILE * f;\r |
1031 | char filename[256]={0};\r | |
83613803 | 1032 | char buf[13];\r |
aea4d766 | 1033 | uint8_t *keyBlock = NULL, *p;\r |
1034 | uint8_t stKeyBlock = 20;\r | |
1035 | \r | |
9ca155ba M |
1036 | int i, res;\r |
1037 | int keycnt = 0;\r | |
1038 | char ctmp = 0x00;\r | |
1039 | uint8_t blockNo = 0;\r | |
aea4d766 | 1040 | uint8_t SectorsCnt = 1;\r |
9ca155ba | 1041 | uint8_t keyType = 0;\r |
9ca155ba | 1042 | uint64_t key64 = 0;\r |
aea4d766 | 1043 | \r |
1044 | int transferToEml = 0;\r | |
1045 | int createDumpFile = 0;\r | |
9ca155ba | 1046 | \r |
aea4d766 | 1047 | keyBlock = calloc(stKeyBlock, 6);\r |
1048 | if (keyBlock == NULL) return 1;\r | |
1049 | \r | |
0c12504a | 1050 | uint64_t defaultKeys[] =\r |
1051 | {\r | |
1052 | 0xffffffffffff, // Default key (first key used by program if no user defined key)\r | |
1053 | 0x000000000000, // Blank key\r | |
1054 | 0xa0a1a2a3a4a5, // NFCForum MAD key\r | |
1055 | 0xb0b1b2b3b4b5,\r | |
1056 | 0xaabbccddeeff,\r | |
1057 | 0x4d3a99c351dd,\r | |
1058 | 0x1a982c7e459a,\r | |
1059 | 0xd3f7d3f7d3f7,\r | |
1060 | 0x714c5c886e97,\r | |
1061 | 0x587ee5f9350f,\r | |
1062 | 0xa0478cc39091,\r | |
1063 | 0x533cb6c723f6,\r | |
1064 | 0x8fd0a4f256e9\r | |
1065 | };\r | |
baeaf579 | 1066 | int defaultKeysSize = sizeof(defaultKeys) / sizeof(uint64_t);\r |
0c12504a | 1067 | \r |
1068 | for (int defaultKeyCounter = 0; defaultKeyCounter < defaultKeysSize; defaultKeyCounter++)\r | |
1069 | {\r | |
1070 | num_to_bytes(defaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));\r | |
1071 | }\r | |
aea4d766 | 1072 | \r |
9ca155ba | 1073 | if (strlen(Cmd)<3) {\r |
baeaf579 | 1074 | PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");\r |
aea4d766 | 1075 | PrintAndLog(" * - all sectors");\r |
1076 | PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r | |
79db03ef | 1077 | PrintAndLog("d - write keys to binary file\n");\r |
aea4d766 | 1078 | PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");\r |
1079 | PrintAndLog(" hf mf chk *1 ? t");\r | |
9ca155ba M |
1080 | return 0;\r |
1081 | } \r | |
1082 | \r | |
aea4d766 | 1083 | if (param_getchar(Cmd, 0)=='*') {\r |
1084 | blockNo = 3;\r | |
1085 | switch(param_getchar(Cmd+1, 0)) {\r | |
1086 | case '0': SectorsCnt = 5; break;\r | |
1087 | case '1': SectorsCnt = 16; break;\r | |
1088 | case '2': SectorsCnt = 32; break;\r | |
1089 | case '4': SectorsCnt = 40; break;\r | |
1090 | default: SectorsCnt = 16;\r | |
1091 | }\r | |
1092 | }\r | |
1093 | else\r | |
1094 | blockNo = param_get8(Cmd, 0);\r | |
1095 | \r | |
9ca155ba | 1096 | ctmp = param_getchar(Cmd, 1);\r |
aea4d766 | 1097 | switch (ctmp) { \r |
1098 | case 'a': case 'A':\r | |
1099 | keyType = !0;\r | |
1100 | break;\r | |
1101 | case 'b': case 'B':\r | |
1102 | keyType = !1;\r | |
1103 | break;\r | |
1104 | case '?':\r | |
1105 | keyType = 2;\r | |
1106 | break;\r | |
1107 | default:\r | |
1108 | PrintAndLog("Key type must be A , B or ?");\r | |
9ca155ba | 1109 | return 1;\r |
aea4d766 | 1110 | };\r |
9ca155ba | 1111 | \r |
aea4d766 | 1112 | ctmp = param_getchar(Cmd, 2);\r |
1113 | if (ctmp == 't' || ctmp == 'T') transferToEml = 1;\r | |
1114 | else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;\r | |
1115 | \r | |
1116 | for (i = transferToEml || createDumpFile; param_getchar(Cmd, 2 + i); i++) {\r | |
1117 | if (!param_gethex(Cmd, 2 + i, keyBlock + 6 * keycnt, 12)) {\r | |
1118 | if ( stKeyBlock - keycnt < 2) {\r | |
1119 | p = realloc(keyBlock, 6*(stKeyBlock+=10));\r | |
1120 | if (!p) {\r | |
1121 | PrintAndLog("Cannot allocate memory for Keys");\r | |
1122 | free(keyBlock);\r | |
1123 | return 2;\r | |
1124 | }\r | |
1125 | keyBlock = p;\r | |
1126 | }\r | |
baeaf579 | 1127 | PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r |
aea4d766 | 1128 | (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r |
1129 | (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r | |
1130 | keycnt++;\r | |
1131 | } else {\r | |
1132 | // May be a dic file\r | |
1133 | if ( param_getstr(Cmd, 2 + i,filename) > 255 ) {\r | |
1134 | PrintAndLog("File name too long");\r | |
1135 | free(keyBlock);\r | |
1136 | return 2;\r | |
1137 | }\r | |
1138 | \r | |
aea4d766 | 1139 | if ( (f = fopen( filename , "r")) ) {\r |
0c12504a | 1140 | while( fgets(buf, sizeof(buf), f) ){\r |
99a71a0d | 1141 | if (strlen(buf) < 12 || buf[11] == '\n')\r |
1142 | continue;\r | |
aea4d766 | 1143 | \r |
99a71a0d | 1144 | while (fgetc(f) != '\n' && !feof(f)) ; //goto next line\r |
1145 | \r | |
9492e0b0 | 1146 | if( buf[0]=='#' ) continue; //The line start with # is comment, skip\r |
9ca155ba | 1147 | \r |
99a71a0d | 1148 | if (!isxdigit(buf[0])){\r |
1149 | PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);\r | |
aea4d766 | 1150 | continue;\r |
1151 | }\r | |
1152 | \r | |
99a71a0d | 1153 | buf[12] = 0;\r |
aea4d766 | 1154 | \r |
1155 | if ( stKeyBlock - keycnt < 2) {\r | |
1156 | p = realloc(keyBlock, 6*(stKeyBlock+=10));\r | |
1157 | if (!p) {\r | |
1158 | PrintAndLog("Cannot allocate memory for defKeys");\r | |
1159 | free(keyBlock);\r | |
1160 | return 2;\r | |
1161 | }\r | |
1162 | keyBlock = p;\r | |
1163 | }\r | |
1164 | memset(keyBlock + 6 * keycnt, 0, 6);\r | |
99a71a0d | 1165 | num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);\r |
baeaf579 | 1166 | PrintAndLog("chk custom key[%2d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r |
aea4d766 | 1167 | keycnt++;\r |
0c12504a | 1168 | memset(buf, 0, sizeof(buf));\r |
aea4d766 | 1169 | }\r |
aea4d766 | 1170 | } else {\r |
1171 | PrintAndLog("File: %s: not found or locked.", filename);\r | |
1172 | free(keyBlock);\r | |
1173 | return 1;\r | |
1174 | fclose(f);\r | |
1175 | }\r | |
9ca155ba | 1176 | }\r |
9ca155ba M |
1177 | }\r |
1178 | \r | |
1179 | if (keycnt == 0) {\r | |
baeaf579 | 1180 | PrintAndLog("No key specified, trying default keys");\r |
0c12504a | 1181 | for (;keycnt < defaultKeysSize; keycnt++)\r |
baeaf579 | 1182 | PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r |
aea4d766 | 1183 | (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r |
1184 | (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r | |
9ca155ba | 1185 | }\r |
9ca155ba | 1186 | \r |
79db03ef | 1187 | // initialize storage for found keys\r |
1188 | bool validKey[2][40];\r | |
1189 | uint8_t foundKey[2][40][6];\r | |
1190 | for (uint16_t t = 0; t < 2; t++) {\r | |
1191 | for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r | |
1192 | validKey[t][sectorNo] = false;\r | |
1193 | for (uint16_t i = 0; i < 6; i++) {\r | |
1194 | foundKey[t][sectorNo][i] = 0xff;\r | |
1195 | }\r | |
1196 | }\r | |
9ca155ba | 1197 | }\r |
9ca155ba | 1198 | \r |
baeaf579 | 1199 | for ( int t = !keyType; t < 2; keyType==2?(t++):(t=2) ) {\r |
aea4d766 | 1200 | int b=blockNo;\r |
baeaf579 | 1201 | for (int i = 0; i < SectorsCnt; ++i) {\r |
79db03ef | 1202 | PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i, b, t?'B':'A', keycnt);\r |
9492e0b0 | 1203 | uint32_t max_keys = keycnt>USB_CMD_DATA_SIZE/6?USB_CMD_DATA_SIZE/6:keycnt;\r |
1204 | for (uint32_t c = 0; c < keycnt; c+=max_keys) {\r | |
1205 | uint32_t size = keycnt-c>max_keys?max_keys:keycnt-c;\r | |
1206 | res = mfCheckKeys(b, t, size, &keyBlock[6*c], &key64);\r | |
baeaf579 | 1207 | if (res != 1) {\r |
aea4d766 | 1208 | if (!res) {\r |
125a98a1 | 1209 | PrintAndLog("Found valid key:[%012"llx"]",key64);\r |
79db03ef | 1210 | num_to_bytes(key64, 6, foundKey[t][i]);\r |
1211 | validKey[t][i] = true;\r | |
1212 | } \r | |
aea4d766 | 1213 | } else {\r |
1214 | PrintAndLog("Command execute timeout");\r | |
1215 | }\r | |
1216 | }\r | |
1217 | b<127?(b+=4):(b+=16); \r | |
1218 | }\r | |
9ca155ba M |
1219 | }\r |
1220 | \r | |
79db03ef | 1221 | if (transferToEml) {\r |
1222 | uint8_t block[16];\r | |
1223 | for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r | |
1224 | if (validKey[0][sectorNo] || validKey[1][sectorNo]) {\r | |
1225 | mfEmlGetMem(block, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1);\r | |
1226 | for (uint16_t t = 0; t < 2; t++) {\r | |
1227 | if (validKey[t][sectorNo]) {\r | |
1228 | memcpy(block + t*10, foundKey[t][sectorNo], 6);\r | |
aea4d766 | 1229 | }\r |
aea4d766 | 1230 | }\r |
79db03ef | 1231 | mfEmlSetMem(block, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1);\r |
aea4d766 | 1232 | }\r |
1233 | }\r | |
79db03ef | 1234 | PrintAndLog("Found keys have been transferred to the emulator memory");\r |
aea4d766 | 1235 | }\r |
79db03ef | 1236 | \r |
aea4d766 | 1237 | if (createDumpFile) {\r |
79db03ef | 1238 | FILE *fkeys = fopen("dumpkeys.bin","wb");\r |
1239 | if (fkeys == NULL) { \r | |
aea4d766 | 1240 | PrintAndLog("Could not create file dumpkeys.bin");\r |
79db03ef | 1241 | free(keyBlock);\r |
aea4d766 | 1242 | return 1;\r |
aea4d766 | 1243 | }\r |
79db03ef | 1244 | for (uint16_t t = 0; t < 2; t++) {\r |
1245 | fwrite(foundKey[t], 1, 6*SectorsCnt, fkeys);\r | |
aea4d766 | 1246 | }\r |
1247 | fclose(fkeys);\r | |
79db03ef | 1248 | PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");\r |
aea4d766 | 1249 | }\r |
79db03ef | 1250 | \r |
1251 | free(keyBlock);\r | |
1252 | \r | |
9ca155ba M |
1253 | return 0;\r |
1254 | }\r | |
1255 | \r | |
79db03ef | 1256 | \r |
9ca155ba M |
1257 | int CmdHF14AMf1kSim(const char *Cmd)\r |
1258 | {\r | |
7bc95e2e | 1259 | uint8_t uid[7] = {0, 0, 0, 0, 0, 0, 0};\r |
d2f487af | 1260 | uint8_t exitAfterNReads = 0;\r |
1261 | uint8_t flags = 0;\r | |
1262 | \r | |
9ca155ba | 1263 | if (param_getchar(Cmd, 0) == 'h') {\r |
d2f487af | 1264 | PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");\r |
1265 | PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");\r | |
1266 | PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");\r | |
1267 | PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");\r | |
1268 | PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");\r | |
b03c0f2d | 1269 | PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");\r |
9ca155ba | 1270 | return 0;\r |
9ca155ba | 1271 | }\r |
d2f487af | 1272 | uint8_t pnr = 0;\r |
1273 | if (param_getchar(Cmd, pnr) == 'u') {\r | |
1274 | if(param_gethex(Cmd, pnr+1, uid, 8) == 0)\r | |
1275 | {\r | |
baeaf579 | 1276 | flags |= FLAG_4B_UID_IN_DATA; // UID from packet\r |
1277 | } else if(param_gethex(Cmd,pnr+1,uid,14) == 0) {\r | |
d2f487af | 1278 | flags |= FLAG_7B_UID_IN_DATA;// UID from packet\r |
baeaf579 | 1279 | } else {\r |
1280 | PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");\r | |
1281 | return 1;\r | |
d2f487af | 1282 | }\r |
1283 | pnr +=2;\r | |
1284 | }\r | |
1285 | if (param_getchar(Cmd, pnr) == 'n') {\r | |
1286 | exitAfterNReads = param_get8(Cmd,pnr+1);\r | |
1287 | pnr += 2;\r | |
1288 | }\r | |
1289 | if (param_getchar(Cmd, pnr) == 'i' ) {\r | |
1290 | //Using a flag to signal interactiveness, least significant bit\r | |
1291 | flags |= FLAG_INTERACTIVE;\r | |
1292 | pnr++;\r | |
1293 | }\r | |
1294 | \r | |
1295 | if (param_getchar(Cmd, pnr) == 'x' ) {\r | |
1296 | //Using a flag to signal interactiveness, least significant bit\r | |
1297 | flags |= FLAG_NR_AR_ATTACK;\r | |
1298 | }\r | |
1299 | PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",\r | |
1300 | flags & FLAG_4B_UID_IN_DATA ? sprint_hex(uid,4):\r | |
1301 | flags & FLAG_7B_UID_IN_DATA ? sprint_hex(uid,7): "N/A"\r | |
1302 | , exitAfterNReads, flags,flags);\r | |
1303 | \r | |
1304 | \r | |
baeaf579 | 1305 | UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {flags, exitAfterNReads,0}};\r |
1306 | memcpy(c.d.asBytes, uid, sizeof(uid));\r | |
1307 | SendCommand(&c);\r | |
d2f487af | 1308 | \r |
baeaf579 | 1309 | if(flags & FLAG_INTERACTIVE)\r |
1310 | {\r | |
1311 | UsbCommand resp;\r | |
1312 | PrintAndLog("Press pm3-button to abort simulation");\r | |
1313 | while(! WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
1314 | //We're waiting only 1.5 s at a time, otherwise we get the\r | |
1315 | // annoying message about "Waiting for a response... "\r | |
1316 | }\r | |
1317 | }\r | |
1318 | \r | |
1319 | return 0;\r | |
9ca155ba M |
1320 | }\r |
1321 | \r | |
baeaf579 | 1322 | \r |
9ca155ba M |
1323 | int CmdHF14AMfDbg(const char *Cmd)\r |
1324 | {\r | |
8556b852 M |
1325 | int dbgMode = param_get32ex(Cmd, 0, 0, 10);\r |
1326 | if (dbgMode > 4) {\r | |
baeaf579 | 1327 | PrintAndLog("Max debug mode parameter is 4 \n");\r |
8556b852 M |
1328 | }\r |
1329 | \r | |
1330 | if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {\r | |
9ca155ba M |
1331 | PrintAndLog("Usage: hf mf dbg <debug level>");\r |
1332 | PrintAndLog(" 0 - no debug messages");\r | |
1333 | PrintAndLog(" 1 - error messages");\r | |
b03c0f2d | 1334 | PrintAndLog(" 2 - plus information messages");\r |
1335 | PrintAndLog(" 3 - plus debug messages");\r | |
1336 | PrintAndLog(" 4 - print even debug messages in timing critical functions");\r | |
1337 | PrintAndLog(" Note: this option therefore may cause malfunction itself");\r | |
9ca155ba M |
1338 | return 0;\r |
1339 | } \r | |
1340 | \r | |
8556b852 M |
1341 | UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};\r |
1342 | SendCommand(&c);\r | |
1343 | \r | |
9ca155ba M |
1344 | return 0;\r |
1345 | }\r | |
1346 | \r | |
1347 | int CmdHF14AMfEGet(const char *Cmd)\r | |
1348 | {\r | |
8556b852 | 1349 | uint8_t blockNo = 0;\r |
baeaf579 | 1350 | uint8_t data[16];\r |
8556b852 M |
1351 | \r |
1352 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1353 | PrintAndLog("Usage: hf mf eget <block number>");\r | |
1354 | PrintAndLog(" sample: hf mf eget 0 ");\r | |
1355 | return 0;\r | |
1356 | } \r | |
1357 | \r | |
1358 | blockNo = param_get8(Cmd, 0);\r | |
8556b852 M |
1359 | \r |
1360 | PrintAndLog(" ");\r | |
baeaf579 | 1361 | if (!mfEmlGetMem(data, blockNo, 1)) {\r |
1362 | PrintAndLog("data[%3d]:%s", blockNo, sprint_hex(data, 16));\r | |
8556b852 M |
1363 | } else {\r |
1364 | PrintAndLog("Command execute timeout");\r | |
1365 | }\r | |
1366 | \r | |
1367 | return 0;\r | |
1368 | }\r | |
1369 | \r | |
baeaf579 | 1370 | \r |
8556b852 M |
1371 | int CmdHF14AMfEClear(const char *Cmd)\r |
1372 | {\r | |
1373 | if (param_getchar(Cmd, 0) == 'h') {\r | |
1374 | PrintAndLog("Usage: hf mf eclr");\r | |
1375 | PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");\r | |
1376 | return 0;\r | |
1377 | } \r | |
1378 | \r | |
1379 | UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};\r | |
1380 | SendCommand(&c);\r | |
9ca155ba M |
1381 | return 0;\r |
1382 | }\r | |
1383 | \r | |
baeaf579 | 1384 | \r |
9ca155ba M |
1385 | int CmdHF14AMfESet(const char *Cmd)\r |
1386 | {\r | |
8556b852 M |
1387 | uint8_t memBlock[16];\r |
1388 | uint8_t blockNo = 0;\r | |
1389 | \r | |
1390 | memset(memBlock, 0x00, sizeof(memBlock));\r | |
1391 | \r | |
1392 | if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {\r | |
1393 | PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");\r | |
1394 | PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");\r | |
1395 | return 0;\r | |
1396 | } \r | |
1397 | \r | |
1398 | blockNo = param_get8(Cmd, 0);\r | |
8556b852 M |
1399 | \r |
1400 | if (param_gethex(Cmd, 1, memBlock, 32)) {\r | |
1401 | PrintAndLog("block data must include 32 HEX symbols");\r | |
1402 | return 1;\r | |
1403 | }\r | |
1404 | \r | |
1405 | // 1 - blocks count\r | |
baeaf579 | 1406 | UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};\r |
8556b852 | 1407 | memcpy(c.d.asBytes, memBlock, 16);\r |
baeaf579 | 1408 | SendCommand(&c);\r |
1409 | return 0;\r | |
9ca155ba M |
1410 | }\r |
1411 | \r | |
baeaf579 | 1412 | \r |
9ca155ba M |
1413 | int CmdHF14AMfELoad(const char *Cmd)\r |
1414 | {\r | |
ab8b654e M |
1415 | FILE * f;\r |
1416 | char filename[20];\r | |
baeaf579 | 1417 | char *fnameptr = filename;\r |
ab8b654e M |
1418 | char buf[64];\r |
1419 | uint8_t buf8[64];\r | |
1420 | int i, len, blockNum;\r | |
1421 | \r | |
1422 | memset(filename, 0, sizeof(filename));\r | |
1423 | memset(buf, 0, sizeof(buf));\r | |
1424 | \r | |
aea4d766 | 1425 | if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {\r |
ab8b654e M |
1426 | PrintAndLog("It loads emul dump from the file `filename.eml`");\r |
1427 | PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");\r | |
1428 | PrintAndLog(" sample: hf mf eload filename");\r | |
1429 | return 0;\r | |
1430 | } \r | |
1431 | \r | |
1432 | len = strlen(Cmd);\r | |
1433 | if (len > 14) len = 14;\r | |
ab8b654e M |
1434 | \r |
1435 | memcpy(filename, Cmd, len);\r | |
1436 | fnameptr += len;\r | |
1437 | \r | |
1438 | sprintf(fnameptr, ".eml"); \r | |
1439 | \r | |
1440 | // open file\r | |
1441 | f = fopen(filename, "r");\r | |
1442 | if (f == NULL) {\r | |
1443 | PrintAndLog("File not found or locked.");\r | |
1444 | return 1;\r | |
1445 | }\r | |
1446 | \r | |
1447 | blockNum = 0;\r | |
1448 | while(!feof(f)){\r | |
1449 | memset(buf, 0, sizeof(buf));\r | |
759c16b3 | 1450 | if (fgets(buf, sizeof(buf), f) == NULL) {\r |
baeaf579 | 1451 | if((blockNum == 16*4) || (blockNum == 32*4 + 8*16)) { // supports both old (1K) and new (4K) .eml files)\r |
d2f487af | 1452 | break;\r |
1453 | }\r | |
1454 | PrintAndLog("File reading error.");\r | |
759c16b3 | 1455 | return 2;\r |
baeaf579 | 1456 | }\r |
ab8b654e | 1457 | if (strlen(buf) < 32){\r |
aea4d766 | 1458 | if(strlen(buf) && feof(f))\r |
1459 | break;\r | |
ab8b654e M |
1460 | PrintAndLog("File content error. Block data must include 32 HEX symbols");\r |
1461 | return 2;\r | |
1462 | }\r | |
baeaf579 | 1463 | for (i = 0; i < 32; i += 2) {\r |
1464 | sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r | |
ab8b654e | 1465 | // PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));\r |
baeaf579 | 1466 | }\r |
ab8b654e | 1467 | if (mfEmlSetMem(buf8, blockNum, 1)) {\r |
baeaf579 | 1468 | PrintAndLog("Cant set emul block: %3d", blockNum);\r |
ab8b654e M |
1469 | return 3;\r |
1470 | }\r | |
1471 | blockNum++;\r | |
1472 | \r | |
baeaf579 | 1473 | if (blockNum >= 32*4 + 8*16) break;\r |
ab8b654e M |
1474 | }\r |
1475 | fclose(f);\r | |
1476 | \r | |
baeaf579 | 1477 | if ((blockNum != 16*4) && (blockNum != 32*4 + 8*16)) {\r |
1478 | PrintAndLog("File content error. There must be 64 or 256 blocks.");\r | |
ab8b654e M |
1479 | return 4;\r |
1480 | }\r | |
d2f487af | 1481 | PrintAndLog("Loaded %d blocks from file: %s", blockNum, filename);\r |
baeaf579 | 1482 | return 0;\r |
9ca155ba M |
1483 | }\r |
1484 | \r | |
baeaf579 | 1485 | \r |
9ca155ba M |
1486 | int CmdHF14AMfESave(const char *Cmd)\r |
1487 | {\r | |
ab8b654e M |
1488 | FILE * f;\r |
1489 | char filename[20];\r | |
1490 | char * fnameptr = filename;\r | |
1491 | uint8_t buf[64];\r | |
1492 | int i, j, len;\r | |
1493 | \r | |
1494 | memset(filename, 0, sizeof(filename));\r | |
1495 | memset(buf, 0, sizeof(buf));\r | |
1496 | \r | |
1497 | if (param_getchar(Cmd, 0) == 'h') {\r | |
1498 | PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");\r | |
1499 | PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");\r | |
1500 | PrintAndLog(" sample: hf mf esave ");\r | |
1501 | PrintAndLog(" hf mf esave filename");\r | |
1502 | return 0;\r | |
1503 | } \r | |
1504 | \r | |
1505 | len = strlen(Cmd);\r | |
1506 | if (len > 14) len = 14;\r | |
1507 | \r | |
1508 | if (len < 1) {\r | |
1509 | // get filename\r | |
1510 | if (mfEmlGetMem(buf, 0, 1)) {\r | |
1511 | PrintAndLog("Cant get block: %d", 0);\r | |
1512 | return 1;\r | |
1513 | }\r | |
1514 | for (j = 0; j < 7; j++, fnameptr += 2)\r | |
1515 | sprintf(fnameptr, "%02x", buf[j]); \r | |
1516 | } else {\r | |
1517 | memcpy(filename, Cmd, len);\r | |
1518 | fnameptr += len;\r | |
1519 | }\r | |
1520 | \r | |
1521 | sprintf(fnameptr, ".eml"); \r | |
1522 | \r | |
1523 | // open file\r | |
1524 | f = fopen(filename, "w+");\r | |
1525 | \r | |
1526 | // put hex\r | |
baeaf579 | 1527 | for (i = 0; i < 32*4 + 8*16; i++) {\r |
ab8b654e M |
1528 | if (mfEmlGetMem(buf, i, 1)) {\r |
1529 | PrintAndLog("Cant get block: %d", i);\r | |
1530 | break;\r | |
1531 | }\r | |
1532 | for (j = 0; j < 16; j++)\r | |
1533 | fprintf(f, "%02x", buf[j]); \r | |
1534 | fprintf(f,"\n");\r | |
1535 | }\r | |
1536 | fclose(f);\r | |
1537 | \r | |
1538 | PrintAndLog("Saved to file: %s", filename);\r | |
1539 | \r | |
9ca155ba M |
1540 | return 0;\r |
1541 | }\r | |
1542 | \r | |
baeaf579 | 1543 | \r |
26fdb4ab | 1544 | int CmdHF14AMfECFill(const char *Cmd)\r |
1545 | {\r | |
8556b852 | 1546 | uint8_t keyType = 0;\r |
baeaf579 | 1547 | uint8_t numSectors = 16;\r |
1548 | \r | |
8556b852 | 1549 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r |
baeaf579 | 1550 | PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");\r |
1551 | PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r | |
1552 | PrintAndLog("");\r | |
1553 | PrintAndLog("samples: hf mf ecfill A");\r | |
1554 | PrintAndLog(" hf mf ecfill A 4");\r | |
1555 | PrintAndLog("Read card and transfer its data to emulator memory.");\r | |
1556 | PrintAndLog("Keys must be laid in the emulator memory. \n");\r | |
8556b852 M |
1557 | return 0;\r |
1558 | } \r | |
1559 | \r | |
1560 | char ctmp = param_getchar(Cmd, 0);\r | |
baeaf579 | 1561 | if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r |
8556b852 M |
1562 | PrintAndLog("Key type must be A or B");\r |
1563 | return 1;\r | |
1564 | }\r | |
1565 | if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r | |
1566 | \r | |
baeaf579 | 1567 | ctmp = param_getchar(Cmd, 1);\r |
1568 | switch (ctmp) {\r | |
1569 | case '0' : numSectors = 5; break;\r | |
1570 | case '1' : \r | |
1571 | case '\0': numSectors = 16; break;\r | |
1572 | case '2' : numSectors = 32; break;\r | |
1573 | case '4' : numSectors = 40; break;\r | |
1574 | default: numSectors = 16;\r | |
1575 | } \r | |
1576 | \r | |
1577 | printf("--params: numSectors: %d, keyType:%d", numSectors, keyType);\r | |
1578 | UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {numSectors, keyType, 0}};\r | |
1579 | SendCommand(&c);\r | |
1580 | return 0;\r | |
8556b852 M |
1581 | }\r |
1582 | \r | |
baeaf579 | 1583 | \r |
26fdb4ab | 1584 | int CmdHF14AMfEKeyPrn(const char *Cmd)\r |
1585 | {\r | |
baeaf579 | 1586 | int i;\r |
8556b852 M |
1587 | uint8_t data[16];\r |
1588 | uint64_t keyA, keyB;\r | |
1589 | \r | |
1590 | PrintAndLog("|---|----------------|----------------|");\r | |
1591 | PrintAndLog("|sec|key A |key B |");\r | |
1592 | PrintAndLog("|---|----------------|----------------|");\r | |
aea4d766 | 1593 | for (i = 0; i < 40; i++) {\r |
baeaf579 | 1594 | if (mfEmlGetMem(data, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1)) {\r |
1595 | PrintAndLog("error get block %d", FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);\r | |
8556b852 M |
1596 | break;\r |
1597 | }\r | |
1598 | keyA = bytes_to_num(data, 6);\r | |
1599 | keyB = bytes_to_num(data + 10, 6);\r | |
125a98a1 | 1600 | PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB);\r |
8556b852 M |
1601 | }\r |
1602 | PrintAndLog("|---|----------------|----------------|");\r | |
1603 | \r | |
1604 | return 0;\r | |
1605 | }\r | |
1606 | \r | |
baeaf579 | 1607 | \r |
0675f200 M |
1608 | int CmdHF14AMfCSetUID(const char *Cmd)\r |
1609 | {\r | |
1610 | uint8_t wipeCard = 0;\r | |
1611 | uint8_t uid[8];\r | |
1612 | uint8_t oldUid[8];\r | |
1613 | int res;\r | |
1614 | \r | |
1615 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1616 | PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");\r | |
1617 | PrintAndLog("sample: hf mf csetuid 01020304 w");\r | |
1618 | PrintAndLog("Set UID for magic Chinese card (only works with!!!)");\r | |
1619 | PrintAndLog("If you want wipe card then add 'w' into command line. \n");\r | |
1620 | return 0;\r | |
1621 | } \r | |
1622 | \r | |
1623 | if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {\r | |
1624 | PrintAndLog("UID must include 8 HEX symbols");\r | |
1625 | return 1;\r | |
1626 | }\r | |
1627 | \r | |
1628 | char ctmp = param_getchar(Cmd, 1);\r | |
1629 | if (ctmp == 'w' || ctmp == 'W') wipeCard = 1;\r | |
1630 | \r | |
1631 | PrintAndLog("--wipe card:%02x uid:%s", wipeCard, sprint_hex(uid, 4));\r | |
1632 | \r | |
1633 | res = mfCSetUID(uid, oldUid, wipeCard);\r | |
1634 | if (res) {\r | |
1635 | PrintAndLog("Can't set UID. error=%d", res);\r | |
1636 | return 1;\r | |
1637 | }\r | |
1638 | \r | |
1639 | PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));\r | |
1640 | return 0;\r | |
1641 | }\r | |
1642 | \r | |
1643 | int CmdHF14AMfCSetBlk(const char *Cmd)\r | |
1644 | {\r | |
f774db95 M |
1645 | uint8_t uid[8];\r |
1646 | uint8_t memBlock[16];\r | |
1647 | uint8_t blockNo = 0;\r | |
1648 | int res;\r | |
1649 | memset(memBlock, 0x00, sizeof(memBlock));\r | |
1650 | \r | |
1651 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1652 | PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");\r | |
1653 | PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");\r | |
1654 | PrintAndLog("Set block data for magic Chinese card (only works with!!!)");\r | |
1655 | PrintAndLog("If you want wipe card then add 'w' into command line. \n");\r | |
1656 | return 0;\r | |
1657 | } \r | |
1658 | \r | |
1659 | blockNo = param_get8(Cmd, 0);\r | |
f774db95 M |
1660 | \r |
1661 | if (param_gethex(Cmd, 1, memBlock, 32)) {\r | |
1662 | PrintAndLog("block data must include 32 HEX symbols");\r | |
1663 | return 1;\r | |
1664 | }\r | |
1665 | \r | |
baeaf579 | 1666 | PrintAndLog("--block number:%2d data:%s", blockNo, sprint_hex(memBlock, 16));\r |
f774db95 | 1667 | \r |
208a0166 | 1668 | res = mfCSetBlock(blockNo, memBlock, uid, 0, CSETBLOCK_SINGLE_OPER);\r |
f774db95 M |
1669 | if (res) {\r |
1670 | PrintAndLog("Can't write block. error=%d", res);\r | |
1671 | return 1;\r | |
1672 | }\r | |
1673 | \r | |
1674 | PrintAndLog("UID:%s", sprint_hex(uid, 4));\r | |
0675f200 M |
1675 | return 0;\r |
1676 | }\r | |
1677 | \r | |
baeaf579 | 1678 | \r |
0675f200 M |
1679 | int CmdHF14AMfCLoad(const char *Cmd)\r |
1680 | {\r | |
208a0166 M |
1681 | FILE * f;\r |
1682 | char filename[20];\r | |
1683 | char * fnameptr = filename;\r | |
1684 | char buf[64];\r | |
1685 | uint8_t buf8[64];\r | |
1686 | uint8_t fillFromEmulator = 0;\r | |
1687 | int i, len, blockNum, flags;\r | |
1688 | \r | |
1689 | memset(filename, 0, sizeof(filename));\r | |
1690 | memset(buf, 0, sizeof(buf));\r | |
1691 | \r | |
1692 | if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {\r | |
1693 | PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");\r | |
1694 | PrintAndLog("or from emulator memory (option `e`)");\r | |
1695 | PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");\r | |
1696 | PrintAndLog(" or: hf mf cload e ");\r | |
1697 | PrintAndLog(" sample: hf mf cload filename");\r | |
1698 | return 0;\r | |
1699 | } \r | |
1700 | \r | |
1701 | char ctmp = param_getchar(Cmd, 0);\r | |
1702 | if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r | |
1703 | \r | |
1704 | if (fillFromEmulator) {\r | |
1705 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
1706 | for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {\r | |
1707 | if (mfEmlGetMem(buf8, blockNum, 1)) {\r | |
1708 | PrintAndLog("Cant get block: %d", blockNum);\r | |
1709 | return 2;\r | |
1710 | }\r | |
1711 | \r | |
1712 | if (blockNum == 2) flags = 0;\r | |
1713 | if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r | |
1714 | \r | |
1715 | if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r | |
1716 | PrintAndLog("Cant set magic card block: %d", blockNum);\r | |
1717 | return 3;\r | |
1718 | }\r | |
1719 | }\r | |
1720 | return 0;\r | |
1721 | } else {\r | |
1722 | len = strlen(Cmd);\r | |
1723 | if (len > 14) len = 14;\r | |
1724 | \r | |
1725 | memcpy(filename, Cmd, len);\r | |
1726 | fnameptr += len;\r | |
1727 | \r | |
1728 | sprintf(fnameptr, ".eml"); \r | |
1729 | \r | |
1730 | // open file\r | |
1731 | f = fopen(filename, "r");\r | |
1732 | if (f == NULL) {\r | |
1733 | PrintAndLog("File not found or locked.");\r | |
1734 | return 1;\r | |
1735 | }\r | |
1736 | \r | |
1737 | blockNum = 0;\r | |
1738 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
1739 | while(!feof(f)){\r | |
1740 | memset(buf, 0, sizeof(buf));\r | |
759c16b3 | 1741 | if (fgets(buf, sizeof(buf), f) == NULL) {\r |
baeaf579 | 1742 | PrintAndLog("File reading error.");\r |
1743 | return 2;\r | |
1744 | }\r | |
208a0166 M |
1745 | \r |
1746 | if (strlen(buf) < 32){\r | |
1747 | if(strlen(buf) && feof(f))\r | |
1748 | break;\r | |
1749 | PrintAndLog("File content error. Block data must include 32 HEX symbols");\r | |
1750 | return 2;\r | |
1751 | }\r | |
1752 | for (i = 0; i < 32; i += 2)\r | |
1753 | sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r | |
1754 | \r | |
1755 | if (blockNum == 2) flags = 0;\r | |
1756 | if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r | |
1757 | \r | |
1758 | if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r | |
baeaf579 | 1759 | PrintAndLog("Can't set magic card block: %d", blockNum);\r |
208a0166 M |
1760 | return 3;\r |
1761 | }\r | |
1762 | blockNum++;\r | |
1763 | \r | |
1764 | if (blockNum >= 16 * 4) break; // magic card type - mifare 1K\r | |
1765 | }\r | |
1766 | fclose(f);\r | |
1767 | \r | |
4a79e52c | 1768 | if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){\r |
208a0166 M |
1769 | PrintAndLog("File content error. There must be 64 blocks");\r |
1770 | return 4;\r | |
1771 | }\r | |
1772 | PrintAndLog("Loaded from file: %s", filename);\r | |
1773 | return 0;\r | |
1774 | }\r | |
0675f200 M |
1775 | }\r |
1776 | \r | |
545a1f38 M |
1777 | int CmdHF14AMfCGetBlk(const char *Cmd) {\r |
1778 | uint8_t memBlock[16];\r | |
1779 | uint8_t blockNo = 0;\r | |
1780 | int res;\r | |
1781 | memset(memBlock, 0x00, sizeof(memBlock));\r | |
1782 | \r | |
1783 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1784 | PrintAndLog("Usage: hf mf cgetblk <block number>");\r | |
1785 | PrintAndLog("sample: hf mf cgetblk 1");\r | |
1786 | PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");\r | |
1787 | return 0;\r | |
1788 | } \r | |
1789 | \r | |
1790 | blockNo = param_get8(Cmd, 0);\r | |
545a1f38 | 1791 | \r |
baeaf579 | 1792 | PrintAndLog("--block number:%2d ", blockNo);\r |
545a1f38 M |
1793 | \r |
1794 | res = mfCGetBlock(blockNo, memBlock, CSETBLOCK_SINGLE_OPER);\r | |
1795 | if (res) {\r | |
1796 | PrintAndLog("Can't read block. error=%d", res);\r | |
1797 | return 1;\r | |
1798 | }\r | |
1799 | \r | |
1800 | PrintAndLog("block data:%s", sprint_hex(memBlock, 16));\r | |
1801 | return 0;\r | |
1802 | }\r | |
1803 | \r | |
baeaf579 | 1804 | \r |
545a1f38 M |
1805 | int CmdHF14AMfCGetSc(const char *Cmd) {\r |
1806 | uint8_t memBlock[16];\r | |
1807 | uint8_t sectorNo = 0;\r | |
1808 | int i, res, flags;\r | |
1809 | memset(memBlock, 0x00, sizeof(memBlock));\r | |
1810 | \r | |
1811 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1812 | PrintAndLog("Usage: hf mf cgetsc <sector number>");\r | |
1813 | PrintAndLog("sample: hf mf cgetsc 0");\r | |
1814 | PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");\r | |
1815 | return 0;\r | |
1816 | } \r | |
1817 | \r | |
1818 | sectorNo = param_get8(Cmd, 0);\r | |
1819 | if (sectorNo > 15) {\r | |
1820 | PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");\r | |
1821 | return 1;\r | |
1822 | }\r | |
1823 | \r | |
baeaf579 | 1824 | PrintAndLog("--sector number:%d ", sectorNo);\r |
545a1f38 M |
1825 | \r |
1826 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
1827 | for (i = 0; i < 4; i++) {\r | |
1828 | if (i == 1) flags = 0;\r | |
1829 | if (i == 3) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r | |
1830 | \r | |
1831 | res = mfCGetBlock(sectorNo * 4 + i, memBlock, flags);\r | |
1832 | if (res) {\r | |
baeaf579 | 1833 | PrintAndLog("Can't read block. %d error=%d", sectorNo * 4 + i, res);\r |
545a1f38 M |
1834 | return 1;\r |
1835 | }\r | |
1836 | \r | |
baeaf579 | 1837 | PrintAndLog("block %3d data:%s", sectorNo * 4 + i, sprint_hex(memBlock, 16));\r |
545a1f38 M |
1838 | }\r |
1839 | return 0;\r | |
1840 | }\r | |
1841 | \r | |
baeaf579 | 1842 | \r |
545a1f38 M |
1843 | int CmdHF14AMfCSave(const char *Cmd) {\r |
1844 | \r | |
1845 | FILE * f;\r | |
1846 | char filename[20];\r | |
1847 | char * fnameptr = filename;\r | |
1848 | uint8_t fillFromEmulator = 0;\r | |
1849 | uint8_t buf[64];\r | |
1850 | int i, j, len, flags;\r | |
1851 | \r | |
1852 | memset(filename, 0, sizeof(filename));\r | |
1853 | memset(buf, 0, sizeof(buf));\r | |
1854 | \r | |
1855 | if (param_getchar(Cmd, 0) == 'h') {\r | |
1856 | PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");\r | |
1857 | PrintAndLog("or into emulator memory (option `e`)");\r | |
1858 | PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");\r | |
1859 | PrintAndLog(" sample: hf mf esave ");\r | |
1860 | PrintAndLog(" hf mf esave filename");\r | |
1861 | PrintAndLog(" hf mf esave e \n");\r | |
1862 | return 0;\r | |
1863 | } \r | |
1864 | \r | |
1865 | char ctmp = param_getchar(Cmd, 0);\r | |
1866 | if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r | |
1867 | \r | |
1868 | if (fillFromEmulator) {\r | |
1869 | // put into emulator\r | |
1870 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
1871 | for (i = 0; i < 16 * 4; i++) {\r | |
1872 | if (i == 1) flags = 0;\r | |
1873 | if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r | |
1874 | \r | |
1875 | if (mfCGetBlock(i, buf, flags)) {\r | |
1876 | PrintAndLog("Cant get block: %d", i);\r | |
1877 | break;\r | |
1878 | }\r | |
1879 | \r | |
1880 | if (mfEmlSetMem(buf, i, 1)) {\r | |
1881 | PrintAndLog("Cant set emul block: %d", i);\r | |
1882 | return 3;\r | |
1883 | }\r | |
1884 | }\r | |
1885 | return 0;\r | |
1886 | } else {\r | |
1887 | len = strlen(Cmd);\r | |
1888 | if (len > 14) len = 14;\r | |
1889 | \r | |
1890 | if (len < 1) {\r | |
1891 | // get filename\r | |
1892 | if (mfCGetBlock(0, buf, CSETBLOCK_SINGLE_OPER)) {\r | |
1893 | PrintAndLog("Cant get block: %d", 0);\r | |
1894 | return 1;\r | |
1895 | }\r | |
1896 | for (j = 0; j < 7; j++, fnameptr += 2)\r | |
1897 | sprintf(fnameptr, "%02x", buf[j]); \r | |
1898 | } else {\r | |
1899 | memcpy(filename, Cmd, len);\r | |
1900 | fnameptr += len;\r | |
1901 | }\r | |
1902 | \r | |
1903 | sprintf(fnameptr, ".eml"); \r | |
1904 | \r | |
1905 | // open file\r | |
1906 | f = fopen(filename, "w+");\r | |
1907 | \r | |
1908 | // put hex\r | |
1909 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
1910 | for (i = 0; i < 16 * 4; i++) {\r | |
1911 | if (i == 1) flags = 0;\r | |
1912 | if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r | |
1913 | \r | |
1914 | if (mfCGetBlock(i, buf, flags)) {\r | |
1915 | PrintAndLog("Cant get block: %d", i);\r | |
1916 | break;\r | |
1917 | }\r | |
1918 | for (j = 0; j < 16; j++)\r | |
1919 | fprintf(f, "%02x", buf[j]); \r | |
1920 | fprintf(f,"\n");\r | |
1921 | }\r | |
1922 | fclose(f);\r | |
1923 | \r | |
1924 | PrintAndLog("Saved to file: %s", filename);\r | |
1925 | \r | |
1926 | return 0;\r | |
1927 | }\r | |
1928 | }\r | |
1929 | \r | |
baeaf579 | 1930 | \r |
b62a5a84 | 1931 | int CmdHF14AMfSniff(const char *Cmd){\r |
55acbb2a | 1932 | // params\r |
c948cbde M |
1933 | bool wantLogToFile = 0;\r |
1934 | bool wantDecrypt = 0;\r | |
eede7162 | 1935 | //bool wantSaveToEml = 0; TODO\r |
55acbb2a M |
1936 | bool wantSaveToEmlFile = 0;\r |
1937 | \r | |
1938 | //var \r | |
39864b0b M |
1939 | int res = 0;\r |
1940 | int len = 0;\r | |
1941 | int blockLen = 0;\r | |
1942 | int num = 0;\r | |
1943 | int pckNum = 0;\r | |
991f13f2 | 1944 | uint8_t uid[7];\r |
1945 | uint8_t uid_len;\r | |
39864b0b M |
1946 | uint8_t atqa[2];\r |
1947 | uint8_t sak;\r | |
1948 | bool isTag;\r | |
71d90e54 | 1949 | uint32_t parity;\r |
39864b0b M |
1950 | uint8_t buf[3000];\r |
1951 | uint8_t * bufPtr = buf;\r | |
1952 | memset(buf, 0x00, 3000);\r | |
b62a5a84 M |
1953 | \r |
1954 | if (param_getchar(Cmd, 0) == 'h') {\r | |
baeaf579 | 1955 | PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");\r |
55acbb2a M |
1956 | PrintAndLog("You can specify:");\r |
1957 | PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");\r | |
1958 | PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");\r | |
1959 | PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");\r | |
e0c635d1 | 1960 | 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 |
1961 | PrintAndLog("Usage: hf mf sniff [l][d][e][r]");\r |
1962 | PrintAndLog(" sample: hf mf sniff l d e");\r | |
b62a5a84 M |
1963 | return 0;\r |
1964 | } \r | |
1965 | \r | |
55acbb2a M |
1966 | for (int i = 0; i < 4; i++) {\r |
1967 | char ctmp = param_getchar(Cmd, i);\r | |
1968 | if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;\r | |
1969 | if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;\r | |
eede7162 | 1970 | //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO\r |
55acbb2a M |
1971 | if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;\r |
1972 | }\r | |
1973 | \r | |
39864b0b M |
1974 | printf("-------------------------------------------------------------------------\n");\r |
1975 | printf("Executing command. \n");\r | |
1976 | printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r | |
1977 | printf("Press the key on pc keyboard to abort the client.\n");\r | |
1978 | printf("-------------------------------------------------------------------------\n");\r | |
1979 | \r | |
d714d3ef | 1980 | UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};\r |
1981 | clearCommandBuffer();\r | |
1982 | SendCommand(&c);\r | |
b62a5a84 | 1983 | \r |
39864b0b M |
1984 | // wait cycle\r |
1985 | while (true) {\r | |
1986 | printf(".");\r | |
1987 | fflush(stdout);\r | |
1988 | if (ukbhit()) {\r | |
1989 | getchar();\r | |
1990 | printf("\naborted via keyboard!\n");\r | |
1991 | break;\r | |
1992 | }\r | |
1993 | \r | |
902cb3c0 | 1994 | UsbCommand resp;\r |
1995 | if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {\r | |
1996 | res = resp.arg[0] & 0xff;\r | |
1997 | len = resp.arg[1];\r | |
1998 | num = resp.arg[2];\r | |
39864b0b M |
1999 | \r |
2000 | if (res == 0) return 0;\r | |
2001 | if (res == 1) {\r | |
2002 | if (num ==0) {\r | |
2003 | bufPtr = buf;\r | |
2004 | memset(buf, 0x00, 3000);\r | |
2005 | }\r | |
902cb3c0 | 2006 | memcpy(bufPtr, resp.d.asBytes, len);\r |
39864b0b M |
2007 | bufPtr += len;\r |
2008 | pckNum++;\r | |
2009 | }\r | |
2010 | if (res == 2) {\r | |
2011 | blockLen = bufPtr - buf;\r | |
2012 | bufPtr = buf;\r | |
2013 | printf(">\n");\r | |
2014 | PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);\r | |
2015 | num = 0;\r | |
2016 | while (bufPtr - buf + 9 < blockLen) {\r | |
7bc95e2e | 2017 | isTag = bufPtr[3] & 0x80 ? true:false;\r |
71d90e54 M |
2018 | bufPtr += 4;\r |
2019 | parity = *((uint32_t *)(bufPtr));\r | |
2020 | bufPtr += 4;\r | |
39864b0b M |
2021 | len = bufPtr[0];\r |
2022 | bufPtr++;\r | |
991f13f2 | 2023 | if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff)) {\r |
39864b0b M |
2024 | memcpy(uid, bufPtr + 2, 7);\r |
2025 | memcpy(atqa, bufPtr + 2 + 7, 2);\r | |
991f13f2 | 2026 | uid_len = (atqa[0] & 0xC0) == 0x40 ? 7 : 4;\r |
39864b0b | 2027 | sak = bufPtr[11];\r |
55acbb2a | 2028 | \r |
991f13f2 | 2029 | PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x", \r |
2030 | sprint_hex(uid + (7 - uid_len), uid_len),\r | |
2031 | atqa[1], \r | |
2032 | atqa[0], \r | |
2033 | sak);\r | |
d714d3ef | 2034 | if (wantLogToFile || wantDecrypt) {\r |
991f13f2 | 2035 | FillFileNameByUID(logHexFileName, uid + (7 - uid_len), ".log", uid_len);\r |
55acbb2a M |
2036 | AddLogCurrentDT(logHexFileName);\r |
2037 | } \r | |
f38a1528 | 2038 | if (wantDecrypt) \r |
2039 | mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);\r | |
39864b0b M |
2040 | } else {\r |
2041 | PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));\r | |
f38a1528 | 2042 | if (wantLogToFile) \r |
2043 | AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);\r | |
2044 | if (wantDecrypt) \r | |
2045 | mfTraceDecode(bufPtr, len, parity, wantSaveToEmlFile);\r | |
39864b0b M |
2046 | }\r |
2047 | bufPtr += len;\r | |
2048 | num++;\r | |
2049 | }\r | |
2050 | }\r | |
f38a1528 | 2051 | } // resp not NULL\r |
39864b0b | 2052 | } // while (true)\r |
d714d3ef | 2053 | \r |
2054 | return 0;\r | |
b62a5a84 M |
2055 | }\r |
2056 | \r | |
f38a1528 | 2057 | // Tries to identify cardsize.\r |
2058 | // Returns <num> where num is:\r | |
2059 | // -1 unidentified\r | |
2060 | // 0 - MINI (320bytes)\r | |
2061 | // 1 - 1K\r | |
2062 | // 2 - 2K\r | |
2063 | // 4 - 4K\r | |
2064 | int GetCardSize()\r | |
2065 | {\r | |
2066 | UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT, 0, 0}};\r | |
2067 | SendCommand(&c);\r | |
2068 | \r | |
2069 | UsbCommand resp;\r | |
2070 | WaitForResponse(CMD_ACK,&resp);\r | |
2071 | \r | |
2072 | if(resp.arg[0] == 0) {\r | |
2073 | PrintAndLog("iso14443a card select failed");\r | |
2074 | return -1;\r | |
2075 | }\r | |
2076 | \r | |
2077 | iso14a_card_select_t *card = (iso14a_card_select_t *)resp.d.asBytes;\r | |
2078 | \r | |
2079 | PrintAndLog("Trying to detect card size.");\r | |
2080 | \r | |
2081 | uint16_t atqa = 0;\r | |
2082 | uint8_t sak = 0;\r | |
2083 | atqa = (card->atqa[1] & 0xff) << 8;\r | |
2084 | atqa += card->atqa[0] & 0xff;\r | |
2085 | sak = card->sak;\r | |
2086 | \r | |
2087 | // https://code.google.com/p/libnfc/source/browse/libnfc/target-subr.c\r | |
2088 | \r | |
2089 | PrintAndLog("found ATAQ: %04X SAK: %02X", atqa, sak);\r | |
2090 | \r | |
2091 | \r | |
2092 | // NXP MIFARE Mini 0.3k\r | |
2093 | if ( (atqa && 0xff0f == 0x0004) && (sak == 0x09) ) return 0;\r | |
2094 | \r | |
2095 | // MIFARE Classic 1K\r | |
2096 | if ( (atqa && 0xff0f == 0x0004) && (sak == 0x08) ) return 1;\r | |
2097 | \r | |
2098 | // MIFARE Classik 4K\r | |
2099 | if ( (atqa && 0xff0f == 0x0002) && (sak == 0x18) ) return 4;\r | |
2100 | \r | |
2101 | // SmartMX with MIFARE 1K emulation \r | |
2102 | if ( (atqa && 0xf0ff == 0x0004) ) return 1;\r | |
2103 | \r | |
2104 | // SmartMX with MIFARE 4K emulation \r | |
2105 | if ( (atqa && 0xf0ff == 0x0002) ) return 4; \r | |
2106 | \r | |
2107 | // Infineon MIFARE CLASSIC 1K\r | |
2108 | if ( (atqa && 0xffff == 0x0004) && (sak == 0x88) ) return 1;\r | |
2109 | \r | |
2110 | // MFC 4K emulated by Nokia 6212 Classic\r | |
2111 | if ( (atqa && 0xffff == 0x0002) && (sak == 0x38) ) return 4;\r | |
2112 | \r | |
2113 | // MFC 4K emulated by Nokia 6131 NFC\r | |
2114 | if ( (atqa && 0xffff == 0x0008) && (sak == 0x38) ) return 4;\r | |
2115 | \r | |
2116 | // MIFARE Plus (4 Byte UID or 4 Byte RID)\r | |
2117 | // MIFARE Plus (7 Byte UID)\r | |
2118 | if (\r | |
2119 | (atqa && 0xffff == 0x0002) ||\r | |
2120 | (atqa && 0xffff == 0x0004) ||\r | |
2121 | (atqa && 0xffff == 0x0042) || \r | |
2122 | (atqa && 0xffff == 0x0044) \r | |
2123 | )\r | |
2124 | {\r | |
2125 | switch(sak){\r | |
2126 | case 0x08:\r | |
2127 | case 0x10:\r | |
2128 | //case 0x20:\r | |
2129 | return 2;\r | |
2130 | break;\r | |
2131 | case 0x11:\r | |
2132 | case 0x18:\r | |
2133 | //case 0x20:\r | |
2134 | return 4;\r | |
2135 | break;\r | |
2136 | }\r | |
2137 | }\r | |
2138 | \r | |
2139 | return -1;\r | |
2140 | }\r | |
2141 | \r | |
2142 | \r | |
2143 | \r | |
2144 | \r | |
26fdb4ab | 2145 | static command_t CommandTable[] =\r |
9ca155ba | 2146 | {\r |
baeaf579 | 2147 | {"help", CmdHelp, 1, "This help"},\r |
2148 | {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},\r | |
d2f487af | 2149 | {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},\r |
f38a1528 | 2150 | //{"urdbl", CmdHF14AMfURdBl, 0, "Read MIFARE Ultralight block"},\r |
2151 | //{"urdcard", CmdHF14AMfURdCard, 0,"Read MIFARE Ultralight Card"},\r | |
2152 | //{"uwrbl", CmdHF14AMfUWrBl, 0,"Write MIFARE Ultralight block"},\r | |
d2f487af | 2153 | {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},\r |
2154 | {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},\r | |
baeaf579 | 2155 | {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},\r |
9ca155ba | 2156 | {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},\r |
baeaf579 | 2157 | {"chk", CmdHF14AMfChk, 0, "Test block keys"},\r |
7bc95e2e | 2158 | {"mifare", CmdHF14AMifare, 0, "Read parity error messages."},\r |
9ca155ba | 2159 | {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},\r |
b62a5a84 | 2160 | {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},\r |
baeaf579 | 2161 | {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},\r |
545a1f38 | 2162 | {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},\r |
51969283 M |
2163 | {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},\r |
2164 | {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},\r | |
9ca155ba M |
2165 | {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},\r |
2166 | {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},\r | |
8556b852 | 2167 | {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},\r |
baeaf579 | 2168 | {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},\r |
2169 | {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},\r | |
2170 | {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block into magic Chinese card"},\r | |
2171 | {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block from magic Chinese card"},\r | |
545a1f38 | 2172 | {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector from magic Chinese card"},\r |
208a0166 | 2173 | {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},\r |
545a1f38 | 2174 | {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},\r |
9ca155ba M |
2175 | {NULL, NULL, 0, NULL}\r |
2176 | };\r | |
2177 | \r | |
2178 | int CmdHFMF(const char *Cmd)\r | |
2179 | {\r | |
2180 | // flush\r | |
7dd1908b | 2181 | WaitForResponseTimeout(CMD_ACK,NULL,100);\r |
9ca155ba M |
2182 | \r |
2183 | CmdsParse(CommandTable, Cmd);\r | |
2184 | return 0;\r | |
2185 | }\r | |
2186 | \r | |
2187 | int CmdHelp(const char *Cmd)\r | |
2188 | {\r | |
2189 | CmdsHelp(CommandTable);\r | |
2190 | return 0;\r | |
2191 | }\r |