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