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