]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhfmf.c
FIX: since the "hf mf ecfill" command supports 0,1,2,4 in card sizes, I consequently...
[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
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
785int 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
22f1c577 850 \r
9ca155ba
M
851 switch (cmdp) {\r
852 case '0': SectorsCnt = 05; break;\r
853 case '1': SectorsCnt = 16; break;\r
854 case '2': SectorsCnt = 32; break;\r
baeaf579 855 case '4': SectorsCnt = 40; break;\r
9ca155ba
M
856 default: SectorsCnt = 16;\r
857 }\r
858 }\r
8556b852
M
859\r
860 ctmp = param_getchar(Cmd, 4);\r
baeaf579 861 if (ctmp == 't' || ctmp == 'T') transferToEml = true;\r
862 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = true;\r
21156267 863 \r
8556b852
M
864 ctmp = param_getchar(Cmd, 6);\r
865 transferToEml |= (ctmp == 't' || ctmp == 'T');\r
21156267 866 transferToEml |= (ctmp == 'd' || ctmp == 'D');\r
9ca155ba 867 \r
9ca155ba 868 if (cmdp == 'o') {\r
baeaf579 869 PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo, trgKeyType?'B':'A');\r
9492e0b0 870 if (mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, true)) {\r
9ca155ba
M
871 PrintAndLog("Nested error.");\r
872 return 2;\r
873 }\r
9492e0b0 874 key64 = bytes_to_num(keyBlock, 6);\r
875 if (key64) {\r
125a98a1 876 PrintAndLog("Found valid key:%012"llx, key64);\r
8556b852
M
877\r
878 // transfer key to the emulator\r
879 if (transferToEml) {\r
baeaf579 880 uint8_t sectortrailer;\r
881 if (trgBlockNo < 32*4) { // 4 block sector\r
882 sectortrailer = (trgBlockNo & 0x03) + 3;\r
883 } else { // 16 block sector\r
884 sectortrailer = (trgBlockNo & 0x0f) + 15;\r
885 }\r
886 mfEmlGetMem(keyBlock, sectortrailer, 1);\r
8556b852
M
887 \r
888 if (!trgKeyType)\r
889 num_to_bytes(key64, 6, keyBlock);\r
890 else\r
891 num_to_bytes(key64, 6, &keyBlock[10]);\r
baeaf579 892 mfEmlSetMem(keyBlock, sectortrailer, 1); \r
8556b852
M
893 }\r
894 } else {\r
9ca155ba 895 PrintAndLog("No valid key found");\r
8556b852 896 }\r
21156267 897 }\r
898 else { // ------------------------------------ multiple sectors working\r
9492e0b0 899 clock_t time1;\r
900 time1 = clock();\r
901\r
9ca155ba
M
902 e_sector = calloc(SectorsCnt, sizeof(sector));\r
903 if (e_sector == NULL) return 1;\r
904 \r
baeaf579 905 //test current key and additional standard keys first\r
9ca155ba 906 memcpy(keyBlock, key, 6);\r
9492e0b0 907 num_to_bytes(0xffffffffffff, 6, (uint8_t*)(keyBlock + 1 * 6));\r
908 num_to_bytes(0x000000000000, 6, (uint8_t*)(keyBlock + 2 * 6));\r
909 num_to_bytes(0xa0a1a2a3a4a5, 6, (uint8_t*)(keyBlock + 3 * 6));\r
910 num_to_bytes(0xb0b1b2b3b4b5, 6, (uint8_t*)(keyBlock + 4 * 6));\r
9ca155ba 911 num_to_bytes(0xaabbccddeeff, 6, (uint8_t*)(keyBlock + 5 * 6));\r
4a79e52c 912 num_to_bytes(0x4d3a99c351dd, 6, (uint8_t*)(keyBlock + 6 * 6));\r
913 num_to_bytes(0x1a982c7e459a, 6, (uint8_t*)(keyBlock + 7 * 6));\r
914 num_to_bytes(0xd3f7d3f7d3f7, 6, (uint8_t*)(keyBlock + 8 * 6));\r
915 num_to_bytes(0x714c5c886e97, 6, (uint8_t*)(keyBlock + 9 * 6));\r
916 num_to_bytes(0x587ee5f9350f, 6, (uint8_t*)(keyBlock + 10 * 6));\r
917 num_to_bytes(0xa0478cc39091, 6, (uint8_t*)(keyBlock + 11 * 6));\r
918 num_to_bytes(0x533cb6c723f6, 6, (uint8_t*)(keyBlock + 12 * 6));\r
919 num_to_bytes(0x8fd0a4f256e9, 6, (uint8_t*)(keyBlock + 13 * 6));\r
9ca155ba
M
920\r
921 PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);\r
922 for (i = 0; i < SectorsCnt; i++) {\r
923 for (j = 0; j < 2; j++) {\r
924 if (e_sector[i].foundKey[j]) continue;\r
925 \r
baeaf579 926 res = mfCheckKeys(FirstBlockOfSector(i), j, 6, keyBlock, &key64);\r
9ca155ba
M
927 \r
928 if (!res) {\r
929 e_sector[i].Key[j] = key64;\r
930 e_sector[i].foundKey[j] = 1;\r
931 }\r
932 }\r
9492e0b0 933 }\r
934 \r
9ca155ba
M
935 // nested sectors\r
936 iterations = 0;\r
937 PrintAndLog("nested...");\r
9492e0b0 938 bool calibrate = true;\r
9ca155ba 939 for (i = 0; i < NESTED_SECTOR_RETRY; i++) {\r
baeaf579 940 for (uint8_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r
22f1c577 941\r
942 if (ukbhit()) {\r
943 printf("\naborted via keyboard!\n");\r
944 free(e_sector);\r
945 return 2;\r
946 } \r
947 \r
9ca155ba 948 for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) { \r
baeaf579 949 if (e_sector[sectorNo].foundKey[trgKeyType]) continue;\r
9492e0b0 950 PrintAndLog("-----------------------------------------------");\r
baeaf579 951 if(mfnested(blockNo, keyType, key, FirstBlockOfSector(sectorNo), trgKeyType, keyBlock, calibrate)) {\r
9492e0b0 952 PrintAndLog("Nested error.\n");\r
22f1c577 953 free(e_sector);\r
954 return 2; }\r
9492e0b0 955 else {\r
956 calibrate = false;\r
957 }\r
9ca155ba
M
958 \r
959 iterations++;\r
9492e0b0 960\r
961 key64 = bytes_to_num(keyBlock, 6);\r
962 if (key64) {\r
125a98a1 963 PrintAndLog("Found valid key:%012"llx, key64);\r
baeaf579 964 e_sector[sectorNo].foundKey[trgKeyType] = 1;\r
965 e_sector[sectorNo].Key[trgKeyType] = key64;\r
9ca155ba
M
966 }\r
967 }\r
9492e0b0 968 }\r
9ca155ba
M
969 }\r
970\r
a0041115 971 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 972 \r
973 PrintAndLog("-----------------------------------------------\nIterations count: %d\n\n", iterations);\r
9ca155ba
M
974 //print them\r
975 PrintAndLog("|---|----------------|---|----------------|---|");\r
976 PrintAndLog("|sec|key A |res|key B |res|");\r
977 PrintAndLog("|---|----------------|---|----------------|---|");\r
978 for (i = 0; i < SectorsCnt; i++) {\r
125a98a1 979 PrintAndLog("|%03d| %012"llx" | %d | %012"llx" | %d |", i,\r
9ca155ba
M
980 e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);\r
981 }\r
982 PrintAndLog("|---|----------------|---|----------------|---|");\r
983 \r
8556b852
M
984 // transfer them to the emulator\r
985 if (transferToEml) {\r
986 for (i = 0; i < SectorsCnt; i++) {\r
baeaf579 987 mfEmlGetMem(keyBlock, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);\r
8556b852 988 if (e_sector[i].foundKey[0])\r
ab8b654e 989 num_to_bytes(e_sector[i].Key[0], 6, keyBlock);\r
8556b852
M
990 if (e_sector[i].foundKey[1])\r
991 num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);\r
baeaf579 992 mfEmlSetMem(keyBlock, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);\r
8556b852
M
993 } \r
994 }\r
995 \r
21156267 996 // Create dump file\r
26fdb4ab 997 if (createDumpFile) {\r
998 if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) { \r
aea4d766 999 PrintAndLog("Could not create file dumpkeys.bin");\r
26fdb4ab 1000 free(e_sector);\r
1001 return 1;\r
1002 }\r
baeaf579 1003 PrintAndLog("Printing keys to binary file dumpkeys.bin...");\r
1004 for(i=0; i<SectorsCnt; i++) {\r
21156267 1005 if (e_sector[i].foundKey[0]){\r
1006 num_to_bytes(e_sector[i].Key[0], 6, tempkey);\r
1007 fwrite ( tempkey, 1, 6, fkeys );\r
1008 }\r
1009 else{\r
1010 fwrite ( &standart, 1, 6, fkeys );\r
1011 }\r
26fdb4ab 1012 }\r
baeaf579 1013 for(i=0; i<SectorsCnt; i++) {\r
21156267 1014 if (e_sector[i].foundKey[1]){\r
1015 num_to_bytes(e_sector[i].Key[1], 6, tempkey);\r
1016 fwrite ( tempkey, 1, 6, fkeys );\r
1017 }\r
1018 else{\r
1019 fwrite ( &standart, 1, 6, fkeys );\r
1020 }\r
26fdb4ab 1021 }\r
1022 fclose(fkeys);\r
1023 }\r
22f1c577 1024\r
9ca155ba
M
1025 free(e_sector);\r
1026 }\r
9ca155ba
M
1027 return 0;\r
1028}\r
1029\r
baeaf579 1030\r
9ca155ba
M
1031int CmdHF14AMfChk(const char *Cmd)\r
1032{\r
b44e5233 1033 if (strlen(Cmd)<3) {\r
1034 PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");\r
1035 PrintAndLog(" * - all sectors");\r
1036 PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r
1037 PrintAndLog("d - write keys to binary file\n");\r
1038 PrintAndLog("t - write keys to emulator memory");\r
1039 PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");\r
1040 PrintAndLog(" hf mf chk *1 ? t");\r
1041 PrintAndLog(" hf mf chk *1 ? d");\r
1042 return 0;\r
1043 }\r
1044 \r
aea4d766 1045 FILE * f;\r
1046 char filename[256]={0};\r
83613803 1047 char buf[13];\r
aea4d766 1048 uint8_t *keyBlock = NULL, *p;\r
1049 uint8_t stKeyBlock = 20;\r
1050 \r
9ca155ba
M
1051 int i, res;\r
1052 int keycnt = 0;\r
1053 char ctmp = 0x00;\r
1054 uint8_t blockNo = 0;\r
aea4d766 1055 uint8_t SectorsCnt = 1;\r
9ca155ba 1056 uint8_t keyType = 0;\r
9ca155ba 1057 uint64_t key64 = 0;\r
aea4d766 1058 \r
1059 int transferToEml = 0;\r
1060 int createDumpFile = 0;\r
9ca155ba 1061\r
aea4d766 1062 keyBlock = calloc(stKeyBlock, 6);\r
1063 if (keyBlock == NULL) return 1;\r
1064\r
0c12504a 1065 uint64_t defaultKeys[] =\r
1066 {\r
1067 0xffffffffffff, // Default key (first key used by program if no user defined key)\r
1068 0x000000000000, // Blank key\r
1069 0xa0a1a2a3a4a5, // NFCForum MAD key\r
1070 0xb0b1b2b3b4b5,\r
1071 0xaabbccddeeff,\r
1072 0x4d3a99c351dd,\r
1073 0x1a982c7e459a,\r
1074 0xd3f7d3f7d3f7,\r
1075 0x714c5c886e97,\r
1076 0x587ee5f9350f,\r
1077 0xa0478cc39091,\r
1078 0x533cb6c723f6,\r
1079 0x8fd0a4f256e9\r
1080 };\r
baeaf579 1081 int defaultKeysSize = sizeof(defaultKeys) / sizeof(uint64_t);\r
0c12504a 1082\r
1083 for (int defaultKeyCounter = 0; defaultKeyCounter < defaultKeysSize; defaultKeyCounter++)\r
1084 {\r
1085 num_to_bytes(defaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));\r
1086 }\r
aea4d766 1087 \r
aea4d766 1088 if (param_getchar(Cmd, 0)=='*') {\r
1089 blockNo = 3;\r
1090 switch(param_getchar(Cmd+1, 0)) {\r
1091 case '0': SectorsCnt = 5; break;\r
1092 case '1': SectorsCnt = 16; break;\r
1093 case '2': SectorsCnt = 32; break;\r
1094 case '4': SectorsCnt = 40; break;\r
1095 default: SectorsCnt = 16;\r
1096 }\r
1097 }\r
1098 else\r
1099 blockNo = param_get8(Cmd, 0);\r
1100 \r
9ca155ba 1101 ctmp = param_getchar(Cmd, 1);\r
aea4d766 1102 switch (ctmp) { \r
1103 case 'a': case 'A':\r
1104 keyType = !0;\r
1105 break;\r
1106 case 'b': case 'B':\r
1107 keyType = !1;\r
1108 break;\r
1109 case '?':\r
1110 keyType = 2;\r
1111 break;\r
1112 default:\r
1113 PrintAndLog("Key type must be A , B or ?");\r
9ca155ba 1114 return 1;\r
aea4d766 1115 };\r
9ca155ba 1116 \r
aea4d766 1117 ctmp = param_getchar(Cmd, 2);\r
1118 if (ctmp == 't' || ctmp == 'T') transferToEml = 1;\r
1119 else if (ctmp == 'd' || ctmp == 'D') createDumpFile = 1;\r
1120 \r
1121 for (i = transferToEml || createDumpFile; param_getchar(Cmd, 2 + i); i++) {\r
1122 if (!param_gethex(Cmd, 2 + i, keyBlock + 6 * keycnt, 12)) {\r
1123 if ( stKeyBlock - keycnt < 2) {\r
1124 p = realloc(keyBlock, 6*(stKeyBlock+=10));\r
1125 if (!p) {\r
1126 PrintAndLog("Cannot allocate memory for Keys");\r
1127 free(keyBlock);\r
1128 return 2;\r
1129 }\r
1130 keyBlock = p;\r
1131 }\r
baeaf579 1132 PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r
aea4d766 1133 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r
1134 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r
1135 keycnt++;\r
1136 } else {\r
1137 // May be a dic file\r
1138 if ( param_getstr(Cmd, 2 + i,filename) > 255 ) {\r
1139 PrintAndLog("File name too long");\r
1140 free(keyBlock);\r
1141 return 2;\r
1142 }\r
1143 \r
aea4d766 1144 if ( (f = fopen( filename , "r")) ) {\r
0c12504a 1145 while( fgets(buf, sizeof(buf), f) ){\r
99a71a0d 1146 if (strlen(buf) < 12 || buf[11] == '\n')\r
1147 continue;\r
aea4d766 1148 \r
99a71a0d 1149 while (fgetc(f) != '\n' && !feof(f)) ; //goto next line\r
1150 \r
9492e0b0 1151 if( buf[0]=='#' ) continue; //The line start with # is comment, skip\r
9ca155ba 1152\r
99a71a0d 1153 if (!isxdigit(buf[0])){\r
1154 PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);\r
aea4d766 1155 continue;\r
1156 }\r
1157 \r
99a71a0d 1158 buf[12] = 0;\r
aea4d766 1159\r
1160 if ( stKeyBlock - keycnt < 2) {\r
1161 p = realloc(keyBlock, 6*(stKeyBlock+=10));\r
1162 if (!p) {\r
1163 PrintAndLog("Cannot allocate memory for defKeys");\r
1164 free(keyBlock);\r
1165 return 2;\r
1166 }\r
1167 keyBlock = p;\r
1168 }\r
1169 memset(keyBlock + 6 * keycnt, 0, 6);\r
99a71a0d 1170 num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);\r
baeaf579 1171 PrintAndLog("chk custom key[%2d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r
aea4d766 1172 keycnt++;\r
0c12504a 1173 memset(buf, 0, sizeof(buf));\r
aea4d766 1174 }\r
a61b4976 1175 fclose(f);\r
aea4d766 1176 } else {\r
1177 PrintAndLog("File: %s: not found or locked.", filename);\r
1178 free(keyBlock);\r
1179 return 1;\r
a61b4976 1180 \r
aea4d766 1181 }\r
9ca155ba 1182 }\r
9ca155ba
M
1183 }\r
1184 \r
1185 if (keycnt == 0) {\r
baeaf579 1186 PrintAndLog("No key specified, trying default keys");\r
0c12504a 1187 for (;keycnt < defaultKeysSize; keycnt++)\r
baeaf579 1188 PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r
aea4d766 1189 (keyBlock + 6*keycnt)[0],(keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r
1190 (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r
9ca155ba 1191 }\r
9ca155ba 1192 \r
79db03ef 1193 // initialize storage for found keys\r
1194 bool validKey[2][40];\r
1195 uint8_t foundKey[2][40][6];\r
1196 for (uint16_t t = 0; t < 2; t++) {\r
1197 for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r
1198 validKey[t][sectorNo] = false;\r
1199 for (uint16_t i = 0; i < 6; i++) {\r
1200 foundKey[t][sectorNo][i] = 0xff;\r
1201 }\r
1202 }\r
9ca155ba 1203 }\r
9ca155ba 1204 \r
baeaf579 1205 for ( int t = !keyType; t < 2; keyType==2?(t++):(t=2) ) {\r
aea4d766 1206 int b=blockNo;\r
baeaf579 1207 for (int i = 0; i < SectorsCnt; ++i) {\r
79db03ef 1208 PrintAndLog("--sector:%2d, block:%3d, key type:%C, key count:%2d ", i, b, t?'B':'A', keycnt);\r
9492e0b0 1209 uint32_t max_keys = keycnt>USB_CMD_DATA_SIZE/6?USB_CMD_DATA_SIZE/6:keycnt;\r
1210 for (uint32_t c = 0; c < keycnt; c+=max_keys) {\r
1211 uint32_t size = keycnt-c>max_keys?max_keys:keycnt-c;\r
1212 res = mfCheckKeys(b, t, size, &keyBlock[6*c], &key64);\r
baeaf579 1213 if (res != 1) {\r
aea4d766 1214 if (!res) {\r
125a98a1 1215 PrintAndLog("Found valid key:[%012"llx"]",key64);\r
79db03ef 1216 num_to_bytes(key64, 6, foundKey[t][i]);\r
1217 validKey[t][i] = true;\r
1218 } \r
aea4d766 1219 } else {\r
1220 PrintAndLog("Command execute timeout");\r
1221 }\r
1222 }\r
1223 b<127?(b+=4):(b+=16); \r
1224 }\r
9ca155ba
M
1225 }\r
1226\r
79db03ef 1227 if (transferToEml) {\r
1228 uint8_t block[16];\r
1229 for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r
1230 if (validKey[0][sectorNo] || validKey[1][sectorNo]) {\r
1231 mfEmlGetMem(block, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1);\r
1232 for (uint16_t t = 0; t < 2; t++) {\r
1233 if (validKey[t][sectorNo]) {\r
1234 memcpy(block + t*10, foundKey[t][sectorNo], 6);\r
aea4d766 1235 }\r
aea4d766 1236 }\r
79db03ef 1237 mfEmlSetMem(block, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1);\r
aea4d766 1238 }\r
1239 }\r
79db03ef 1240 PrintAndLog("Found keys have been transferred to the emulator memory");\r
aea4d766 1241 }\r
79db03ef 1242\r
aea4d766 1243 if (createDumpFile) {\r
79db03ef 1244 FILE *fkeys = fopen("dumpkeys.bin","wb");\r
1245 if (fkeys == NULL) { \r
aea4d766 1246 PrintAndLog("Could not create file dumpkeys.bin");\r
79db03ef 1247 free(keyBlock);\r
aea4d766 1248 return 1;\r
aea4d766 1249 }\r
79db03ef 1250 for (uint16_t t = 0; t < 2; t++) {\r
1251 fwrite(foundKey[t], 1, 6*SectorsCnt, fkeys);\r
aea4d766 1252 }\r
1253 fclose(fkeys);\r
79db03ef 1254 PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");\r
aea4d766 1255 }\r
79db03ef 1256\r
1257 free(keyBlock);\r
1258\r
9ca155ba
M
1259 return 0;\r
1260}\r
1261\r
79db03ef 1262\r
9ca155ba
M
1263int CmdHF14AMf1kSim(const char *Cmd)\r
1264{\r
7bc95e2e 1265 uint8_t uid[7] = {0, 0, 0, 0, 0, 0, 0};\r
d2f487af 1266 uint8_t exitAfterNReads = 0;\r
1267 uint8_t flags = 0;\r
1268\r
9ca155ba 1269 if (param_getchar(Cmd, 0) == 'h') {\r
d2f487af 1270 PrintAndLog("Usage: hf mf sim u <uid (8 hex symbols)> n <numreads> i x");\r
1271 PrintAndLog(" u (Optional) UID. If not specified, the UID from emulator memory will be used");\r
1272 PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");\r
1273 PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");\r
1274 PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");\r
b03c0f2d 1275 PrintAndLog(" sample: hf mf sim u 0a0a0a0a ");\r
9ca155ba 1276 return 0;\r
9ca155ba 1277 }\r
d2f487af 1278 uint8_t pnr = 0;\r
1279 if (param_getchar(Cmd, pnr) == 'u') {\r
1280 if(param_gethex(Cmd, pnr+1, uid, 8) == 0)\r
1281 {\r
baeaf579 1282 flags |= FLAG_4B_UID_IN_DATA; // UID from packet\r
1283 } else if(param_gethex(Cmd,pnr+1,uid,14) == 0) {\r
d2f487af 1284 flags |= FLAG_7B_UID_IN_DATA;// UID from packet\r
baeaf579 1285 } else {\r
1286 PrintAndLog("UID, if specified, must include 8 or 14 HEX symbols");\r
1287 return 1;\r
d2f487af 1288 }\r
1289 pnr +=2;\r
1290 }\r
1291 if (param_getchar(Cmd, pnr) == 'n') {\r
1292 exitAfterNReads = param_get8(Cmd,pnr+1);\r
1293 pnr += 2;\r
1294 }\r
1295 if (param_getchar(Cmd, pnr) == 'i' ) {\r
1296 //Using a flag to signal interactiveness, least significant bit\r
1297 flags |= FLAG_INTERACTIVE;\r
1298 pnr++;\r
1299 }\r
1300\r
1301 if (param_getchar(Cmd, pnr) == 'x' ) {\r
1302 //Using a flag to signal interactiveness, least significant bit\r
1303 flags |= FLAG_NR_AR_ATTACK;\r
1304 }\r
1305 PrintAndLog(" uid:%s, numreads:%d, flags:%d (0x%02x) ",\r
1306 flags & FLAG_4B_UID_IN_DATA ? sprint_hex(uid,4):\r
1307 flags & FLAG_7B_UID_IN_DATA ? sprint_hex(uid,7): "N/A"\r
1308 , exitAfterNReads, flags,flags);\r
1309\r
1310\r
baeaf579 1311 UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {flags, exitAfterNReads,0}};\r
1312 memcpy(c.d.asBytes, uid, sizeof(uid));\r
1313 SendCommand(&c);\r
d2f487af 1314\r
baeaf579 1315 if(flags & FLAG_INTERACTIVE)\r
1316 {\r
1317 UsbCommand resp;\r
1318 PrintAndLog("Press pm3-button to abort simulation");\r
1319 while(! WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
1320 //We're waiting only 1.5 s at a time, otherwise we get the\r
1321 // annoying message about "Waiting for a response... "\r
1322 }\r
1323 }\r
1324 \r
1325 return 0;\r
9ca155ba
M
1326}\r
1327\r
baeaf579 1328\r
9ca155ba
M
1329int CmdHF14AMfDbg(const char *Cmd)\r
1330{\r
8556b852
M
1331 int dbgMode = param_get32ex(Cmd, 0, 0, 10);\r
1332 if (dbgMode > 4) {\r
baeaf579 1333 PrintAndLog("Max debug mode parameter is 4 \n");\r
8556b852
M
1334 }\r
1335\r
1336 if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {\r
9ca155ba
M
1337 PrintAndLog("Usage: hf mf dbg <debug level>");\r
1338 PrintAndLog(" 0 - no debug messages");\r
1339 PrintAndLog(" 1 - error messages");\r
b03c0f2d 1340 PrintAndLog(" 2 - plus information messages");\r
1341 PrintAndLog(" 3 - plus debug messages");\r
1342 PrintAndLog(" 4 - print even debug messages in timing critical functions");\r
1343 PrintAndLog(" Note: this option therefore may cause malfunction itself");\r
9ca155ba
M
1344 return 0;\r
1345 } \r
1346\r
8556b852
M
1347 UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};\r
1348 SendCommand(&c);\r
1349\r
9ca155ba
M
1350 return 0;\r
1351}\r
1352\r
1353int CmdHF14AMfEGet(const char *Cmd)\r
1354{\r
8556b852 1355 uint8_t blockNo = 0;\r
baeaf579 1356 uint8_t data[16];\r
8556b852
M
1357\r
1358 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1359 PrintAndLog("Usage: hf mf eget <block number>");\r
1360 PrintAndLog(" sample: hf mf eget 0 ");\r
1361 return 0;\r
1362 } \r
1363 \r
1364 blockNo = param_get8(Cmd, 0);\r
8556b852
M
1365\r
1366 PrintAndLog(" ");\r
baeaf579 1367 if (!mfEmlGetMem(data, blockNo, 1)) {\r
1368 PrintAndLog("data[%3d]:%s", blockNo, sprint_hex(data, 16));\r
8556b852
M
1369 } else {\r
1370 PrintAndLog("Command execute timeout");\r
1371 }\r
1372\r
1373 return 0;\r
1374}\r
1375\r
baeaf579 1376\r
8556b852
M
1377int CmdHF14AMfEClear(const char *Cmd)\r
1378{\r
1379 if (param_getchar(Cmd, 0) == 'h') {\r
1380 PrintAndLog("Usage: hf mf eclr");\r
1381 PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");\r
1382 return 0;\r
1383 } \r
1384\r
1385 UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};\r
1386 SendCommand(&c);\r
9ca155ba
M
1387 return 0;\r
1388}\r
1389\r
baeaf579 1390\r
9ca155ba
M
1391int CmdHF14AMfESet(const char *Cmd)\r
1392{\r
8556b852
M
1393 uint8_t memBlock[16];\r
1394 uint8_t blockNo = 0;\r
1395\r
1396 memset(memBlock, 0x00, sizeof(memBlock));\r
1397\r
1398 if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {\r
1399 PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");\r
1400 PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");\r
1401 return 0;\r
1402 } \r
1403 \r
1404 blockNo = param_get8(Cmd, 0);\r
8556b852
M
1405 \r
1406 if (param_gethex(Cmd, 1, memBlock, 32)) {\r
1407 PrintAndLog("block data must include 32 HEX symbols");\r
1408 return 1;\r
1409 }\r
1410 \r
1411 // 1 - blocks count\r
baeaf579 1412 UsbCommand c = {CMD_MIFARE_EML_MEMSET, {blockNo, 1, 0}};\r
8556b852 1413 memcpy(c.d.asBytes, memBlock, 16);\r
baeaf579 1414 SendCommand(&c);\r
1415 return 0;\r
9ca155ba
M
1416}\r
1417\r
baeaf579 1418\r
9ca155ba
M
1419int CmdHF14AMfELoad(const char *Cmd)\r
1420{\r
ab8b654e 1421 FILE * f;\r
5c065fa0 1422 char filename[255];\r
baeaf579 1423 char *fnameptr = filename;\r
ab8b654e
M
1424 char buf[64];\r
1425 uint8_t buf8[64];\r
1426 int i, len, blockNum;\r
1427 \r
1428 memset(filename, 0, sizeof(filename));\r
1429 memset(buf, 0, sizeof(buf));\r
1430\r
aea4d766 1431 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {\r
ab8b654e
M
1432 PrintAndLog("It loads emul dump from the file `filename.eml`");\r
1433 PrintAndLog("Usage: hf mf eload <file name w/o `.eml`>");\r
1434 PrintAndLog(" sample: hf mf eload filename");\r
1435 return 0;\r
1436 } \r
1437\r
1438 len = strlen(Cmd);\r
5c065fa0 1439 if (len > 250) len = 250;\r
ab8b654e
M
1440\r
1441 memcpy(filename, Cmd, len);\r
1442 fnameptr += len;\r
1443\r
1444 sprintf(fnameptr, ".eml"); \r
1445 \r
1446 // open file\r
1447 f = fopen(filename, "r");\r
1448 if (f == NULL) {\r
1449 PrintAndLog("File not found or locked.");\r
1450 return 1;\r
1451 }\r
1452 \r
1453 blockNum = 0;\r
1454 while(!feof(f)){\r
1455 memset(buf, 0, sizeof(buf));\r
759c16b3 1456 if (fgets(buf, sizeof(buf), f) == NULL) {\r
baeaf579 1457 if((blockNum == 16*4) || (blockNum == 32*4 + 8*16)) { // supports both old (1K) and new (4K) .eml files)\r
d2f487af 1458 break;\r
1459 }\r
1460 PrintAndLog("File reading error.");\r
a61b4976 1461 fclose(f);\r
759c16b3 1462 return 2;\r
baeaf579 1463 }\r
ab8b654e 1464 if (strlen(buf) < 32){\r
aea4d766 1465 if(strlen(buf) && feof(f))\r
1466 break;\r
ab8b654e
M
1467 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
1468 return 2;\r
1469 }\r
baeaf579 1470 for (i = 0; i < 32; i += 2) {\r
1471 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
ab8b654e 1472// PrintAndLog("data[%02d]:%s", blockNum, sprint_hex(buf8, 16));\r
baeaf579 1473 }\r
ab8b654e 1474 if (mfEmlSetMem(buf8, blockNum, 1)) {\r
baeaf579 1475 PrintAndLog("Cant set emul block: %3d", blockNum);\r
ab8b654e
M
1476 return 3;\r
1477 }\r
1478 blockNum++;\r
1479 \r
baeaf579 1480 if (blockNum >= 32*4 + 8*16) break;\r
ab8b654e
M
1481 }\r
1482 fclose(f);\r
1483 \r
baeaf579 1484 if ((blockNum != 16*4) && (blockNum != 32*4 + 8*16)) {\r
1485 PrintAndLog("File content error. There must be 64 or 256 blocks.");\r
a61b4976 1486 fclose(f);\r
ab8b654e
M
1487 return 4;\r
1488 }\r
d2f487af 1489 PrintAndLog("Loaded %d blocks from file: %s", blockNum, filename);\r
baeaf579 1490 return 0;\r
9ca155ba
M
1491}\r
1492\r
baeaf579 1493\r
9ca155ba
M
1494int CmdHF14AMfESave(const char *Cmd)\r
1495{\r
ab8b654e 1496 FILE * f;\r
5c065fa0 1497 char filename[255];\r
ab8b654e
M
1498 char * fnameptr = filename;\r
1499 uint8_t buf[64];\r
1500 int i, j, len;\r
1501 \r
1502 memset(filename, 0, sizeof(filename));\r
1503 memset(buf, 0, sizeof(buf));\r
1504\r
1505 if (param_getchar(Cmd, 0) == 'h') {\r
1506 PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");\r
1507 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`]");\r
1508 PrintAndLog(" sample: hf mf esave ");\r
1509 PrintAndLog(" hf mf esave filename");\r
1510 return 0;\r
1511 } \r
1512\r
1513 len = strlen(Cmd);\r
5c065fa0 1514 if (len > 250) len = 250;\r
ab8b654e
M
1515 \r
1516 if (len < 1) {\r
1517 // get filename\r
1518 if (mfEmlGetMem(buf, 0, 1)) {\r
1519 PrintAndLog("Cant get block: %d", 0);\r
1520 return 1;\r
1521 }\r
1522 for (j = 0; j < 7; j++, fnameptr += 2)\r
1523 sprintf(fnameptr, "%02x", buf[j]); \r
1524 } else {\r
1525 memcpy(filename, Cmd, len);\r
1526 fnameptr += len;\r
1527 }\r
1528\r
1529 sprintf(fnameptr, ".eml"); \r
1530 \r
1531 // open file\r
1532 f = fopen(filename, "w+");\r
1533\r
1534 // put hex\r
baeaf579 1535 for (i = 0; i < 32*4 + 8*16; i++) {\r
ab8b654e
M
1536 if (mfEmlGetMem(buf, i, 1)) {\r
1537 PrintAndLog("Cant get block: %d", i);\r
1538 break;\r
1539 }\r
1540 for (j = 0; j < 16; j++)\r
1541 fprintf(f, "%02x", buf[j]); \r
1542 fprintf(f,"\n");\r
1543 }\r
1544 fclose(f);\r
1545 \r
1546 PrintAndLog("Saved to file: %s", filename);\r
1547 \r
9ca155ba
M
1548 return 0;\r
1549}\r
1550\r
baeaf579 1551\r
26fdb4ab 1552int CmdHF14AMfECFill(const char *Cmd)\r
1553{\r
8556b852 1554 uint8_t keyType = 0;\r
baeaf579 1555 uint8_t numSectors = 16;\r
1556 \r
8556b852 1557 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
baeaf579 1558 PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");\r
1559 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
1560 PrintAndLog("");\r
1561 PrintAndLog("samples: hf mf ecfill A");\r
1562 PrintAndLog(" hf mf ecfill A 4");\r
1563 PrintAndLog("Read card and transfer its data to emulator memory.");\r
1564 PrintAndLog("Keys must be laid in the emulator memory. \n");\r
8556b852
M
1565 return 0;\r
1566 } \r
1567\r
1568 char ctmp = param_getchar(Cmd, 0);\r
baeaf579 1569 if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r
8556b852
M
1570 PrintAndLog("Key type must be A or B");\r
1571 return 1;\r
1572 }\r
1573 if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r
1574\r
baeaf579 1575 ctmp = param_getchar(Cmd, 1);\r
1576 switch (ctmp) {\r
1577 case '0' : numSectors = 5; break;\r
1578 case '1' : \r
1579 case '\0': numSectors = 16; break;\r
1580 case '2' : numSectors = 32; break;\r
1581 case '4' : numSectors = 40; break;\r
1582 default: numSectors = 16;\r
1583 } \r
1584\r
1585 printf("--params: numSectors: %d, keyType:%d", numSectors, keyType);\r
1586 UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {numSectors, keyType, 0}};\r
1587 SendCommand(&c);\r
1588 return 0;\r
8556b852
M
1589}\r
1590\r
baeaf579 1591\r
26fdb4ab 1592int CmdHF14AMfEKeyPrn(const char *Cmd)\r
1593{\r
baeaf579 1594 int i;\r
a0bf7ba7 1595 uint8_t numSectors;\r
8556b852
M
1596 uint8_t data[16];\r
1597 uint64_t keyA, keyB;\r
1598 \r
85578fcd 1599 if (param_getchar(Cmd, 0) == 'h') {\r
a0bf7ba7 1600 PrintAndLog("It prints the keys loaded in the emulator memory");\r
1601 PrintAndLog("Usage: hf mf ekeyprn [card memory]");\r
85578fcd 1602 PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r
a0bf7ba7 1603 PrintAndLog("");\r
1604 PrintAndLog(" sample: hf mf ekeyprn 1");\r
1605 return 0;\r
1606 } \r
1607\r
1608 char cmdp = param_getchar(Cmd, 0);\r
1609 \r
85578fcd 1610 switch (ctmp) {\r
1611 case '0' : numSectors = 5; break;\r
a0bf7ba7 1612 case '1' : \r
1613 case '\0': numSectors = 16; break;\r
85578fcd 1614 case '2' : numSectors = 32; break;\r
a0bf7ba7 1615 case '4' : numSectors = 40; break;\r
1616 default: numSectors = 16;\r
85578fcd 1617 } \r
a0bf7ba7 1618 \r
8556b852
M
1619 PrintAndLog("|---|----------------|----------------|");\r
1620 PrintAndLog("|sec|key A |key B |");\r
1621 PrintAndLog("|---|----------------|----------------|");\r
a0bf7ba7 1622 for (i = 0; i < numSectors; i++) {\r
baeaf579 1623 if (mfEmlGetMem(data, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1)) {\r
1624 PrintAndLog("error get block %d", FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);\r
8556b852
M
1625 break;\r
1626 }\r
1627 keyA = bytes_to_num(data, 6);\r
1628 keyB = bytes_to_num(data + 10, 6);\r
125a98a1 1629 PrintAndLog("|%03d| %012"llx" | %012"llx" |", i, keyA, keyB);\r
8556b852
M
1630 }\r
1631 PrintAndLog("|---|----------------|----------------|");\r
1632 \r
1633 return 0;\r
1634}\r
1635\r
baeaf579 1636\r
0675f200
M
1637int CmdHF14AMfCSetUID(const char *Cmd)\r
1638{\r
1639 uint8_t wipeCard = 0;\r
a61b4976 1640 uint8_t uid[8] = {0x00};\r
1641 uint8_t oldUid[8] = {0x00};\r
0675f200
M
1642 int res;\r
1643\r
1644 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1645 PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> <w>");\r
1646 PrintAndLog("sample: hf mf csetuid 01020304 w");\r
1647 PrintAndLog("Set UID for magic Chinese card (only works with!!!)");\r
1648 PrintAndLog("If you want wipe card then add 'w' into command line. \n");\r
1649 return 0;\r
1650 } \r
1651\r
1652 if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {\r
1653 PrintAndLog("UID must include 8 HEX symbols");\r
1654 return 1;\r
1655 }\r
1656\r
1657 char ctmp = param_getchar(Cmd, 1);\r
1658 if (ctmp == 'w' || ctmp == 'W') wipeCard = 1;\r
1659 \r
1660 PrintAndLog("--wipe card:%02x uid:%s", wipeCard, sprint_hex(uid, 4));\r
1661\r
1662 res = mfCSetUID(uid, oldUid, wipeCard);\r
1663 if (res) {\r
1664 PrintAndLog("Can't set UID. error=%d", res);\r
1665 return 1;\r
1666 }\r
1667 \r
1668 PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));\r
1669 return 0;\r
1670}\r
1671\r
1672int CmdHF14AMfCSetBlk(const char *Cmd)\r
1673{\r
f774db95
M
1674 uint8_t uid[8];\r
1675 uint8_t memBlock[16];\r
1676 uint8_t blockNo = 0;\r
1677 int res;\r
1678 memset(memBlock, 0x00, sizeof(memBlock));\r
1679\r
1680 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1681 PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)>");\r
1682 PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");\r
1683 PrintAndLog("Set block data for magic Chinese card (only works with!!!)");\r
1684 PrintAndLog("If you want wipe card then add 'w' into command line. \n");\r
1685 return 0;\r
1686 } \r
1687\r
1688 blockNo = param_get8(Cmd, 0);\r
f774db95
M
1689\r
1690 if (param_gethex(Cmd, 1, memBlock, 32)) {\r
1691 PrintAndLog("block data must include 32 HEX symbols");\r
1692 return 1;\r
1693 }\r
1694\r
baeaf579 1695 PrintAndLog("--block number:%2d data:%s", blockNo, sprint_hex(memBlock, 16));\r
f774db95 1696\r
208a0166 1697 res = mfCSetBlock(blockNo, memBlock, uid, 0, CSETBLOCK_SINGLE_OPER);\r
f774db95
M
1698 if (res) {\r
1699 PrintAndLog("Can't write block. error=%d", res);\r
1700 return 1;\r
1701 }\r
1702 \r
1703 PrintAndLog("UID:%s", sprint_hex(uid, 4));\r
0675f200
M
1704 return 0;\r
1705}\r
1706\r
baeaf579 1707\r
0675f200
M
1708int CmdHF14AMfCLoad(const char *Cmd)\r
1709{\r
208a0166 1710 FILE * f;\r
5c065fa0 1711 char filename[255];\r
208a0166
M
1712 char * fnameptr = filename;\r
1713 char buf[64];\r
1714 uint8_t buf8[64];\r
1715 uint8_t fillFromEmulator = 0;\r
1716 int i, len, blockNum, flags;\r
1717 \r
1718 memset(filename, 0, sizeof(filename));\r
1719 memset(buf, 0, sizeof(buf));\r
1720\r
1721 if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {\r
1722 PrintAndLog("It loads magic Chinese card (only works with!!!) from the file `filename.eml`");\r
1723 PrintAndLog("or from emulator memory (option `e`)");\r
1724 PrintAndLog("Usage: hf mf cload <file name w/o `.eml`>");\r
1725 PrintAndLog(" or: hf mf cload e ");\r
1726 PrintAndLog(" sample: hf mf cload filename");\r
1727 return 0;\r
1728 } \r
1729\r
1730 char ctmp = param_getchar(Cmd, 0);\r
1731 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r
1732 \r
1733 if (fillFromEmulator) {\r
1734 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1735 for (blockNum = 0; blockNum < 16 * 4; blockNum += 1) {\r
1736 if (mfEmlGetMem(buf8, blockNum, 1)) {\r
1737 PrintAndLog("Cant get block: %d", blockNum);\r
1738 return 2;\r
1739 }\r
1740 \r
1741 if (blockNum == 2) flags = 0;\r
1742 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1743\r
1744 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r
1745 PrintAndLog("Cant set magic card block: %d", blockNum);\r
1746 return 3;\r
1747 }\r
1748 }\r
1749 return 0;\r
1750 } else {\r
1751 len = strlen(Cmd);\r
5c065fa0 1752 if (len > 250) len = 250;\r
208a0166
M
1753\r
1754 memcpy(filename, Cmd, len);\r
1755 fnameptr += len;\r
1756\r
1757 sprintf(fnameptr, ".eml"); \r
1758 \r
1759 // open file\r
1760 f = fopen(filename, "r");\r
1761 if (f == NULL) {\r
1762 PrintAndLog("File not found or locked.");\r
1763 return 1;\r
1764 }\r
1765 \r
1766 blockNum = 0;\r
1767 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1768 while(!feof(f)){\r
1769 memset(buf, 0, sizeof(buf));\r
759c16b3 1770 if (fgets(buf, sizeof(buf), f) == NULL) {\r
baeaf579 1771 PrintAndLog("File reading error.");\r
1772 return 2;\r
1773 }\r
208a0166
M
1774\r
1775 if (strlen(buf) < 32){\r
1776 if(strlen(buf) && feof(f))\r
1777 break;\r
1778 PrintAndLog("File content error. Block data must include 32 HEX symbols");\r
1779 return 2;\r
1780 }\r
1781 for (i = 0; i < 32; i += 2)\r
1782 sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r
1783\r
1784 if (blockNum == 2) flags = 0;\r
1785 if (blockNum == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1786\r
1787 if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r
baeaf579 1788 PrintAndLog("Can't set magic card block: %d", blockNum);\r
208a0166
M
1789 return 3;\r
1790 }\r
1791 blockNum++;\r
1792 \r
1793 if (blockNum >= 16 * 4) break; // magic card type - mifare 1K\r
1794 }\r
1795 fclose(f);\r
1796 \r
4a79e52c 1797 if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){\r
208a0166
M
1798 PrintAndLog("File content error. There must be 64 blocks");\r
1799 return 4;\r
1800 }\r
1801 PrintAndLog("Loaded from file: %s", filename);\r
1802 return 0;\r
1803 }\r
0675f200
M
1804}\r
1805\r
545a1f38
M
1806int CmdHF14AMfCGetBlk(const char *Cmd) {\r
1807 uint8_t memBlock[16];\r
1808 uint8_t blockNo = 0;\r
1809 int res;\r
1810 memset(memBlock, 0x00, sizeof(memBlock));\r
1811\r
1812 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1813 PrintAndLog("Usage: hf mf cgetblk <block number>");\r
1814 PrintAndLog("sample: hf mf cgetblk 1");\r
1815 PrintAndLog("Get block data from magic Chinese card (only works with!!!)\n");\r
1816 return 0;\r
1817 } \r
1818\r
1819 blockNo = param_get8(Cmd, 0);\r
545a1f38 1820\r
baeaf579 1821 PrintAndLog("--block number:%2d ", blockNo);\r
545a1f38
M
1822\r
1823 res = mfCGetBlock(blockNo, memBlock, CSETBLOCK_SINGLE_OPER);\r
1824 if (res) {\r
1825 PrintAndLog("Can't read block. error=%d", res);\r
1826 return 1;\r
1827 }\r
1828 \r
1829 PrintAndLog("block data:%s", sprint_hex(memBlock, 16));\r
1830 return 0;\r
1831}\r
1832\r
baeaf579 1833\r
545a1f38
M
1834int CmdHF14AMfCGetSc(const char *Cmd) {\r
1835 uint8_t memBlock[16];\r
1836 uint8_t sectorNo = 0;\r
1837 int i, res, flags;\r
1838 memset(memBlock, 0x00, sizeof(memBlock));\r
1839\r
1840 if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r
1841 PrintAndLog("Usage: hf mf cgetsc <sector number>");\r
1842 PrintAndLog("sample: hf mf cgetsc 0");\r
1843 PrintAndLog("Get sector data from magic Chinese card (only works with!!!)\n");\r
1844 return 0;\r
1845 } \r
1846\r
1847 sectorNo = param_get8(Cmd, 0);\r
1848 if (sectorNo > 15) {\r
1849 PrintAndLog("Sector number must be in [0..15] as in MIFARE classic.");\r
1850 return 1;\r
1851 }\r
1852\r
baeaf579 1853 PrintAndLog("--sector number:%d ", sectorNo);\r
545a1f38
M
1854\r
1855 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1856 for (i = 0; i < 4; i++) {\r
1857 if (i == 1) flags = 0;\r
1858 if (i == 3) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1859\r
1860 res = mfCGetBlock(sectorNo * 4 + i, memBlock, flags);\r
1861 if (res) {\r
baeaf579 1862 PrintAndLog("Can't read block. %d error=%d", sectorNo * 4 + i, res);\r
545a1f38
M
1863 return 1;\r
1864 }\r
1865 \r
baeaf579 1866 PrintAndLog("block %3d data:%s", sectorNo * 4 + i, sprint_hex(memBlock, 16));\r
545a1f38
M
1867 }\r
1868 return 0;\r
1869}\r
1870\r
baeaf579 1871\r
545a1f38
M
1872int CmdHF14AMfCSave(const char *Cmd) {\r
1873\r
1874 FILE * f;\r
5c065fa0 1875 char filename[255];\r
545a1f38
M
1876 char * fnameptr = filename;\r
1877 uint8_t fillFromEmulator = 0;\r
1878 uint8_t buf[64];\r
1879 int i, j, len, flags;\r
1880 \r
1881 memset(filename, 0, sizeof(filename));\r
1882 memset(buf, 0, sizeof(buf));\r
1883\r
1884 if (param_getchar(Cmd, 0) == 'h') {\r
1885 PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");\r
1886 PrintAndLog("or into emulator memory (option `e`)");\r
1887 PrintAndLog("Usage: hf mf esave [file name w/o `.eml`][e]");\r
1888 PrintAndLog(" sample: hf mf esave ");\r
1889 PrintAndLog(" hf mf esave filename");\r
1890 PrintAndLog(" hf mf esave e \n");\r
1891 return 0;\r
1892 } \r
1893\r
1894 char ctmp = param_getchar(Cmd, 0);\r
1895 if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r
1896\r
1897 if (fillFromEmulator) {\r
1898 // put into emulator\r
1899 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1900 for (i = 0; i < 16 * 4; i++) {\r
1901 if (i == 1) flags = 0;\r
1902 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1903 \r
1904 if (mfCGetBlock(i, buf, flags)) {\r
1905 PrintAndLog("Cant get block: %d", i);\r
1906 break;\r
1907 }\r
1908 \r
1909 if (mfEmlSetMem(buf, i, 1)) {\r
1910 PrintAndLog("Cant set emul block: %d", i);\r
1911 return 3;\r
1912 }\r
1913 }\r
1914 return 0;\r
1915 } else {\r
1916 len = strlen(Cmd);\r
5c065fa0 1917 if (len > 250) len = 250;\r
545a1f38
M
1918 \r
1919 if (len < 1) {\r
1920 // get filename\r
1921 if (mfCGetBlock(0, buf, CSETBLOCK_SINGLE_OPER)) {\r
1922 PrintAndLog("Cant get block: %d", 0);\r
1923 return 1;\r
1924 }\r
1925 for (j = 0; j < 7; j++, fnameptr += 2)\r
1926 sprintf(fnameptr, "%02x", buf[j]); \r
1927 } else {\r
1928 memcpy(filename, Cmd, len);\r
1929 fnameptr += len;\r
1930 }\r
1931\r
1932 sprintf(fnameptr, ".eml"); \r
1933 \r
1934 // open file\r
1935 f = fopen(filename, "w+");\r
1936\r
1937 // put hex\r
1938 flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r
1939 for (i = 0; i < 16 * 4; i++) {\r
1940 if (i == 1) flags = 0;\r
1941 if (i == 16 * 4 - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r
1942 \r
1943 if (mfCGetBlock(i, buf, flags)) {\r
1944 PrintAndLog("Cant get block: %d", i);\r
1945 break;\r
1946 }\r
1947 for (j = 0; j < 16; j++)\r
1948 fprintf(f, "%02x", buf[j]); \r
1949 fprintf(f,"\n");\r
1950 }\r
1951 fclose(f);\r
1952 \r
1953 PrintAndLog("Saved to file: %s", filename);\r
1954 \r
1955 return 0;\r
1956 }\r
1957}\r
1958\r
baeaf579 1959\r
b62a5a84 1960int CmdHF14AMfSniff(const char *Cmd){\r
55acbb2a 1961 // params\r
c948cbde
M
1962 bool wantLogToFile = 0;\r
1963 bool wantDecrypt = 0;\r
eede7162 1964 //bool wantSaveToEml = 0; TODO\r
55acbb2a
M
1965 bool wantSaveToEmlFile = 0;\r
1966\r
1967 //var \r
39864b0b
M
1968 int res = 0;\r
1969 int len = 0;\r
1970 int blockLen = 0;\r
1971 int num = 0;\r
1972 int pckNum = 0;\r
991f13f2 1973 uint8_t uid[7];\r
1974 uint8_t uid_len;\r
39864b0b
M
1975 uint8_t atqa[2];\r
1976 uint8_t sak;\r
1977 bool isTag;\r
71d90e54 1978 uint32_t parity;\r
39864b0b
M
1979 uint8_t buf[3000];\r
1980 uint8_t * bufPtr = buf;\r
1981 memset(buf, 0x00, 3000);\r
b62a5a84
M
1982 \r
1983 if (param_getchar(Cmd, 0) == 'h') {\r
baeaf579 1984 PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");\r
55acbb2a
M
1985 PrintAndLog("You can specify:");\r
1986 PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");\r
1987 PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");\r
1988 PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");\r
e0c635d1 1989 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
1990 PrintAndLog("Usage: hf mf sniff [l][d][e][r]");\r
1991 PrintAndLog(" sample: hf mf sniff l d e");\r
b62a5a84
M
1992 return 0;\r
1993 } \r
1994 \r
55acbb2a
M
1995 for (int i = 0; i < 4; i++) {\r
1996 char ctmp = param_getchar(Cmd, i);\r
1997 if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;\r
1998 if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;\r
eede7162 1999 //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO\r
55acbb2a
M
2000 if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;\r
2001 }\r
2002 \r
39864b0b
M
2003 printf("-------------------------------------------------------------------------\n");\r
2004 printf("Executing command. \n");\r
2005 printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r
2006 printf("Press the key on pc keyboard to abort the client.\n");\r
2007 printf("-------------------------------------------------------------------------\n");\r
2008\r
d714d3ef 2009 UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};\r
2010 clearCommandBuffer();\r
2011 SendCommand(&c);\r
b62a5a84 2012\r
39864b0b
M
2013 // wait cycle\r
2014 while (true) {\r
2015 printf(".");\r
2016 fflush(stdout);\r
2017 if (ukbhit()) {\r
2018 getchar();\r
2019 printf("\naborted via keyboard!\n");\r
2020 break;\r
2021 }\r
2022 \r
902cb3c0 2023 UsbCommand resp;\r
2024 if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {\r
2025 res = resp.arg[0] & 0xff;\r
2026 len = resp.arg[1];\r
2027 num = resp.arg[2];\r
39864b0b
M
2028 \r
2029 if (res == 0) return 0;\r
2030 if (res == 1) {\r
2031 if (num ==0) {\r
2032 bufPtr = buf;\r
2033 memset(buf, 0x00, 3000);\r
2034 }\r
902cb3c0 2035 memcpy(bufPtr, resp.d.asBytes, len);\r
39864b0b
M
2036 bufPtr += len;\r
2037 pckNum++;\r
2038 }\r
2039 if (res == 2) {\r
2040 blockLen = bufPtr - buf;\r
2041 bufPtr = buf;\r
2042 printf(">\n");\r
2043 PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);\r
2044 num = 0;\r
2045 while (bufPtr - buf + 9 < blockLen) {\r
7bc95e2e 2046 isTag = bufPtr[3] & 0x80 ? true:false;\r
71d90e54
M
2047 bufPtr += 4;\r
2048 parity = *((uint32_t *)(bufPtr));\r
2049 bufPtr += 4;\r
39864b0b
M
2050 len = bufPtr[0];\r
2051 bufPtr++;\r
991f13f2 2052 if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff)) {\r
39864b0b
M
2053 memcpy(uid, bufPtr + 2, 7);\r
2054 memcpy(atqa, bufPtr + 2 + 7, 2);\r
991f13f2 2055 uid_len = (atqa[0] & 0xC0) == 0x40 ? 7 : 4;\r
39864b0b 2056 sak = bufPtr[11];\r
55acbb2a 2057 \r
991f13f2 2058 PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x", \r
2059 sprint_hex(uid + (7 - uid_len), uid_len),\r
2060 atqa[1], \r
2061 atqa[0], \r
2062 sak);\r
d714d3ef 2063 if (wantLogToFile || wantDecrypt) {\r
991f13f2 2064 FillFileNameByUID(logHexFileName, uid + (7 - uid_len), ".log", uid_len);\r
55acbb2a
M
2065 AddLogCurrentDT(logHexFileName);\r
2066 } \r
f38a1528 2067 if (wantDecrypt) \r
2068 mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);\r
39864b0b
M
2069 } else {\r
2070 PrintAndLog("%s(%d):%s", isTag ? "TAG":"RDR", num, sprint_hex(bufPtr, len));\r
f38a1528 2071 if (wantLogToFile) \r
2072 AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);\r
2073 if (wantDecrypt) \r
2074 mfTraceDecode(bufPtr, len, parity, wantSaveToEmlFile);\r
39864b0b
M
2075 }\r
2076 bufPtr += len;\r
2077 num++;\r
2078 }\r
2079 }\r
f38a1528 2080 } // resp not NULL\r
39864b0b 2081 } // while (true)\r
d714d3ef 2082 \r
2083 return 0;\r
b62a5a84
M
2084}\r
2085\r
f38a1528 2086// Tries to identify cardsize.\r
2087// Returns <num> where num is:\r
2088// -1 unidentified\r
2089// 0 - MINI (320bytes)\r
2090// 1 - 1K\r
2091// 2 - 2K\r
2092// 4 - 4K\r
2093int GetCardSize()\r
2094{\r
2095 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT, 0, 0}};\r
2096 SendCommand(&c);\r
2097\r
2098 UsbCommand resp;\r
2099 WaitForResponse(CMD_ACK,&resp);\r
2100\r
2101 if(resp.arg[0] == 0) {\r
2102 PrintAndLog("iso14443a card select failed");\r
2103 return -1;\r
2104 }\r
2105 \r
2106 iso14a_card_select_t *card = (iso14a_card_select_t *)resp.d.asBytes;\r
2107\r
2108 PrintAndLog("Trying to detect card size.");\r
2109 \r
2110 uint16_t atqa = 0;\r
2111 uint8_t sak = 0;\r
2112 atqa = (card->atqa[1] & 0xff) << 8;\r
2113 atqa += card->atqa[0] & 0xff;\r
2114 sak = card->sak;\r
2115 \r
2116 // https://code.google.com/p/libnfc/source/browse/libnfc/target-subr.c\r
2117 \r
2118 PrintAndLog("found ATAQ: %04X SAK: %02X", atqa, sak);\r
2119 \r
2120 \r
2121 // NXP MIFARE Mini 0.3k\r
b44e5233 2122 if ( ( (atqa & 0xff0f) == 0x0004) && (sak == 0x09) ) return 0;\r
f38a1528 2123 \r
2124 // MIFARE Classic 1K\r
b44e5233 2125 if ( ((atqa & 0xff0f) == 0x0004) && (sak == 0x08) ) return 1;\r
f38a1528 2126 \r
2127 // MIFARE Classik 4K\r
b44e5233 2128 if ( ((atqa & 0xff0f) == 0x0002) && (sak == 0x18) ) return 4;\r
f38a1528 2129 \r
2130 // SmartMX with MIFARE 1K emulation \r
b44e5233 2131 if ( ((atqa & 0xf0ff) == 0x0004) ) return 1;\r
f38a1528 2132\r
2133 // SmartMX with MIFARE 4K emulation \r
b44e5233 2134 if ( ((atqa & 0xf0ff) == 0x0002) ) return 4; \r
f38a1528 2135 \r
2136 // Infineon MIFARE CLASSIC 1K\r
b44e5233 2137 if ( ((atqa & 0xffff) == 0x0004) && (sak == 0x88) ) return 1;\r
f38a1528 2138 \r
2139 // MFC 4K emulated by Nokia 6212 Classic\r
b44e5233 2140 if ( ((atqa & 0xffff) == 0x0002) && (sak == 0x38) ) return 4;\r
f38a1528 2141\r
2142 // MFC 4K emulated by Nokia 6131 NFC\r
b44e5233 2143 if ( ((atqa & 0xffff) == 0x0008) && (sak == 0x38) ) return 4;\r
f38a1528 2144\r
b44e5233 2145 \r
2146 PrintAndLog("BEFOOO 1K %02X", (atqa & 0xff0f));\r
2147 \r
f38a1528 2148 // MIFARE Plus (4 Byte UID or 4 Byte RID)\r
2149 // MIFARE Plus (7 Byte UID)\r
2150 if (\r
b44e5233 2151 ((atqa & 0xffff) == 0x0002) |\r
2152 ((atqa & 0xffff) == 0x0004) |\r
2153 ((atqa & 0xffff) == 0x0042) | \r
2154 ((atqa & 0xffff) == 0x0044) \r
f38a1528 2155 )\r
2156 {\r
2157 switch(sak){\r
2158 case 0x08:\r
b44e5233 2159 case 0x10: {\r
f38a1528 2160 //case 0x20:\r
b44e5233 2161 PrintAndLog("2");\r
f38a1528 2162 return 2;\r
2163 break;\r
b44e5233 2164 }\r
f38a1528 2165 case 0x11:\r
b44e5233 2166 case 0x18:{\r
f38a1528 2167 //case 0x20:\r
b44e5233 2168 PrintAndLog("4");\r
f38a1528 2169 return 4;\r
2170 break;\r
b44e5233 2171 }\r
f38a1528 2172 }\r
2173 }\r
2174 \r
2175 return -1;\r
2176}\r
2177\r
2178\r
2179\r
2180\r
26fdb4ab 2181static command_t CommandTable[] =\r
9ca155ba 2182{\r
baeaf579 2183 {"help", CmdHelp, 1, "This help"},\r
2184 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},\r
d2f487af 2185 {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},\r
f38a1528 2186 //{"urdbl", CmdHF14AMfURdBl, 0, "Read MIFARE Ultralight block"},\r
2187 //{"urdcard", CmdHF14AMfURdCard, 0,"Read MIFARE Ultralight Card"},\r
2188 //{"uwrbl", CmdHF14AMfUWrBl, 0,"Write MIFARE Ultralight block"},\r
d2f487af 2189 {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},\r
2190 {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},\r
baeaf579 2191 {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},\r
9ca155ba 2192 {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},\r
baeaf579 2193 {"chk", CmdHF14AMfChk, 0, "Test block keys"},\r
7bc95e2e 2194 {"mifare", CmdHF14AMifare, 0, "Read parity error messages."},\r
9ca155ba 2195 {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},\r
b62a5a84 2196 {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},\r
baeaf579 2197 {"sim", CmdHF14AMf1kSim, 0, "Simulate MIFARE card"},\r
545a1f38 2198 {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory block"},\r
51969283
M
2199 {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},\r
2200 {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},\r
9ca155ba
M
2201 {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},\r
2202 {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},\r
8556b852 2203 {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},\r
baeaf579 2204 {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},\r
2205 {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},\r
2206 {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block into magic Chinese card"},\r
2207 {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block from magic Chinese card"},\r
545a1f38 2208 {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector from magic Chinese card"},\r
208a0166 2209 {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},\r
545a1f38 2210 {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},\r
9ca155ba
M
2211 {NULL, NULL, 0, NULL}\r
2212};\r
2213\r
2214int CmdHFMF(const char *Cmd)\r
2215{\r
2216 // flush\r
7dd1908b 2217 WaitForResponseTimeout(CMD_ACK,NULL,100);\r
9ca155ba
M
2218\r
2219 CmdsParse(CommandTable, Cmd);\r
2220 return 0;\r
2221}\r
2222\r
2223int CmdHelp(const char *Cmd)\r
2224{\r
2225 CmdsHelp(CommandTable);\r
2226 return 0;\r
2227}\r
Impressum, Datenschutz