]>
Commit | Line | Data |
---|---|---|
9ca155ba | 1 | //-----------------------------------------------------------------------------\r |
b62a5a84 | 2 | // Copyright (C) 2011,2012 Merlok\r |
9ca155ba M |
3 | //\r |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or,\r | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of\r | |
6 | // the license.\r | |
7 | //-----------------------------------------------------------------------------\r | |
8 | // High frequency MIFARE commands\r | |
9 | //-----------------------------------------------------------------------------\r | |
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 |
ad939de5 | 18 | #include "comms.h"\r |
7cb8516c | 19 | #include "cmdmain.h"\r |
c48c4d78 | 20 | #include "cmdhfmfhard.h"\r |
a37725fa | 21 | #include "parity.h"\r |
7cb8516c | 22 | #include "util.h"\r |
ec9c7112 | 23 | #include "util_posix.h"\r |
c48c4d78 | 24 | #include "usb_cmd.h"\r |
7cb8516c | 25 | #include "ui.h"\r |
fdd9395d | 26 | #include "mifare/mifarehost.h"\r |
7cb8516c | 27 | #include "mifare.h"\r |
fdd9395d | 28 | #include "mifare/mfkey.h"\r |
362d2039 | 29 | #include "hardnested/hardnested_bf_core.h"\r |
7dadcc95 OM |
30 | #include "cliparser/cliparser.h"\r |
31 | #include "cmdhf14a.h"\r | |
4be9f36e | 32 | #include "mifare/mifaredefault.h"\r |
fdd9395d OM |
33 | #include "mifare/mifare4.h"\r |
34 | #include "mifare/mad.h"\r | |
35 | #include "mifare/ndef.h"\r | |
36 | #include "emv/dump.h"\r | |
7cb8516c | 37 | \r |
a39af1cb | 38 | #define NESTED_SECTOR_RETRY 10 // how often we try mfested() until we give up\r |
7cb8516c | 39 | \r |
9ca155ba M |
40 | static int CmdHelp(const char *Cmd);\r |
41 | \r | |
9ca155ba M |
42 | int CmdHF14AMifare(const char *Cmd)\r |
43 | {\r | |
7779d73c | 44 | int isOK = 0;\r |
45 | uint64_t key = 0;\r | |
7779d73c | 46 | isOK = mfDarkside(&key);\r |
47 | switch (isOK) {\r | |
48 | case -1 : PrintAndLog("Button pressed. Aborted."); return 1;\r | |
49 | case -2 : PrintAndLog("Card is not vulnerable to Darkside attack (doesn't send NACK on authentication requests)."); return 1;\r | |
50 | case -3 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator is not predictable)."); return 1;\r | |
51 | case -4 : PrintAndLog("Card is not vulnerable to Darkside attack (its random number generator seems to be based on the wellknown");\r | |
c48c4d78 | 52 | PrintAndLog("generating polynomial with 16 effective bits only, but shows unexpected behaviour."); return 1;\r |
7779d73c | 53 | case -5 : PrintAndLog("Aborted via keyboard."); return 1;\r |
54 | default : PrintAndLog("Found valid key:%012" PRIx64 "\n", key);\r | |
aea4d766 | 55 | }\r |
7906cb41 | 56 | \r |
3fe4ff4f | 57 | PrintAndLog("");\r |
9ca155ba M |
58 | return 0;\r |
59 | }\r | |
60 | \r | |
7779d73c | 61 | \r |
9ca155ba M |
62 | int CmdHF14AMfWrBl(const char *Cmd)\r |
63 | {\r | |
64 | uint8_t blockNo = 0;\r | |
65 | uint8_t keyType = 0;\r | |
66 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
67 | uint8_t bldata[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\r | |
7906cb41 | 68 | \r |
a39af1cb | 69 | char cmdp = 0x00;\r |
9ca155ba M |
70 | \r |
71 | if (strlen(Cmd)<3) {\r | |
72 | PrintAndLog("Usage: hf mf wrbl <block number> <key A/B> <key (12 hex symbols)> <block data (32 hex symbols)>");\r | |
73 | PrintAndLog(" sample: hf mf wrbl 0 A FFFFFFFFFFFF 000102030405060708090A0B0C0D0E0F");\r | |
74 | return 0;\r | |
7906cb41 | 75 | }\r |
9ca155ba M |
76 | \r |
77 | blockNo = param_get8(Cmd, 0);\r | |
78 | cmdp = param_getchar(Cmd, 1);\r | |
79 | if (cmdp == 0x00) {\r | |
80 | PrintAndLog("Key type must be A or B");\r | |
81 | return 1;\r | |
82 | }\r | |
83 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
84 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
85 | PrintAndLog("Key must include 12 HEX symbols");\r | |
86 | return 1;\r | |
87 | }\r | |
88 | if (param_gethex(Cmd, 3, bldata, 32)) {\r | |
89 | PrintAndLog("Block data must include 32 HEX symbols");\r | |
90 | return 1;\r | |
91 | }\r | |
baeaf579 | 92 | PrintAndLog("--block no:%d, key type:%c, key:%s", blockNo, keyType?'B':'A', sprint_hex(key, 6));\r |
9ca155ba | 93 | PrintAndLog("--data: %s", sprint_hex(bldata, 16));\r |
7906cb41 | 94 | \r |
9ca155ba M |
95 | UsbCommand c = {CMD_MIFARE_WRITEBL, {blockNo, keyType, 0}};\r |
96 | memcpy(c.d.asBytes, key, 6);\r | |
97 | memcpy(c.d.asBytes + 10, bldata, 16);\r | |
98 | SendCommand(&c);\r | |
9ca155ba | 99 | \r |
902cb3c0 | 100 | UsbCommand resp;\r |
101 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
102 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
9ca155ba M |
103 | PrintAndLog("isOk:%02x", isOK);\r |
104 | } else {\r | |
105 | PrintAndLog("Command execute timeout");\r | |
106 | }\r | |
107 | \r | |
d2f487af | 108 | return 0;\r |
109 | }\r | |
110 | \r | |
d2f487af | 111 | int CmdHF14AMfRdBl(const char *Cmd)\r |
112 | {\r | |
113 | uint8_t blockNo = 0;\r | |
9ca155ba M |
114 | uint8_t keyType = 0;\r |
115 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
7906cb41 | 116 | \r |
a39af1cb | 117 | char cmdp = 0x00;\r |
9ca155ba M |
118 | \r |
119 | \r | |
120 | if (strlen(Cmd)<3) {\r | |
121 | PrintAndLog("Usage: hf mf rdbl <block number> <key A/B> <key (12 hex symbols)>");\r | |
122 | PrintAndLog(" sample: hf mf rdbl 0 A FFFFFFFFFFFF ");\r | |
123 | return 0;\r | |
7906cb41 F |
124 | }\r |
125 | \r | |
9ca155ba M |
126 | blockNo = param_get8(Cmd, 0);\r |
127 | cmdp = param_getchar(Cmd, 1);\r | |
128 | if (cmdp == 0x00) {\r | |
129 | PrintAndLog("Key type must be A or B");\r | |
130 | return 1;\r | |
131 | }\r | |
132 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
133 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
134 | PrintAndLog("Key must include 12 HEX symbols");\r | |
135 | return 1;\r | |
136 | }\r | |
baeaf579 | 137 | PrintAndLog("--block no:%d, key type:%c, key:%s ", blockNo, keyType?'B':'A', sprint_hex(key, 6));\r |
7906cb41 | 138 | \r |
9ca155ba M |
139 | UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};\r |
140 | memcpy(c.d.asBytes, key, 6);\r | |
141 | SendCommand(&c);\r | |
9ca155ba | 142 | \r |
902cb3c0 | 143 | UsbCommand resp;\r |
144 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
baeaf579 | 145 | uint8_t isOK = resp.arg[0] & 0xff;\r |
146 | uint8_t *data = resp.d.asBytes;\r | |
9ca155ba | 147 | \r |
ac4ecfe3 | 148 | if (isOK) {\r |
9ca155ba | 149 | PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 16));\r |
ac4ecfe3 | 150 | } else {\r |
9ca155ba | 151 | PrintAndLog("isOk:%02x", isOK);\r |
ac4ecfe3 OM |
152 | return 1;\r |
153 | }\r | |
154 | \r | |
155 | if (mfIsSectorTrailer(blockNo) && (data[6] || data[7] || data[8])) {\r | |
156 | PrintAndLogEx(NORMAL, "Trailer decoded:");\r | |
157 | int bln = mfFirstBlockOfSector(mfSectorNum(blockNo));\r | |
158 | int blinc = (mfNumBlocksPerSector(mfSectorNum(blockNo)) > 4) ? 5 : 1;\r | |
159 | for (int i = 0; i < 4; i++) {\r | |
160 | PrintAndLogEx(NORMAL, "Access block %d%s: %s", bln, ((blinc > 1) && (i < 3) ? "+" : "") , mfGetAccessConditionsDesc(i, &data[6]));\r | |
161 | bln += blinc;\r | |
162 | }\r | |
163 | PrintAndLogEx(NORMAL, "UserData: %s", sprint_hex_inrow(&data[9], 1));\r | |
164 | }\r | |
9ca155ba M |
165 | } else {\r |
166 | PrintAndLog("Command execute timeout");\r | |
ac4ecfe3 | 167 | return 2;\r |
9ca155ba M |
168 | }\r |
169 | \r | |
d2f487af | 170 | return 0;\r |
171 | }\r | |
172 | \r | |
d2f487af | 173 | int CmdHF14AMfRdSc(const char *Cmd)\r |
174 | {\r | |
175 | int i;\r | |
9ca155ba M |
176 | uint8_t sectorNo = 0;\r |
177 | uint8_t keyType = 0;\r | |
178 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
9ca155ba | 179 | uint8_t isOK = 0;\r |
baeaf579 | 180 | uint8_t *data = NULL;\r |
a39af1cb | 181 | char cmdp = 0x00;\r |
9ca155ba M |
182 | \r |
183 | if (strlen(Cmd)<3) {\r | |
184 | PrintAndLog("Usage: hf mf rdsc <sector number> <key A/B> <key (12 hex symbols)>");\r | |
185 | PrintAndLog(" sample: hf mf rdsc 0 A FFFFFFFFFFFF ");\r | |
186 | return 0;\r | |
7906cb41 F |
187 | }\r |
188 | \r | |
9ca155ba | 189 | sectorNo = param_get8(Cmd, 0);\r |
baeaf579 | 190 | if (sectorNo > 39) {\r |
191 | PrintAndLog("Sector number must be less than 40");\r | |
9ca155ba M |
192 | return 1;\r |
193 | }\r | |
194 | cmdp = param_getchar(Cmd, 1);\r | |
baeaf579 | 195 | if (cmdp != 'a' && cmdp != 'A' && cmdp != 'b' && cmdp != 'B') {\r |
9ca155ba M |
196 | PrintAndLog("Key type must be A or B");\r |
197 | return 1;\r | |
198 | }\r | |
199 | if (cmdp != 'A' && cmdp != 'a') keyType = 1;\r | |
200 | if (param_gethex(Cmd, 2, key, 12)) {\r | |
201 | PrintAndLog("Key must include 12 HEX symbols");\r | |
202 | return 1;\r | |
203 | }\r | |
baeaf579 | 204 | PrintAndLog("--sector no:%d key type:%c key:%s ", sectorNo, keyType?'B':'A', sprint_hex(key, 6));\r |
7906cb41 | 205 | \r |
baeaf579 | 206 | UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};\r |
9ca155ba | 207 | memcpy(c.d.asBytes, key, 6);\r |
baeaf579 | 208 | SendCommand(&c);\r |
9ca155ba M |
209 | PrintAndLog(" ");\r |
210 | \r | |
902cb3c0 | 211 | UsbCommand resp;\r |
212 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r | |
213 | isOK = resp.arg[0] & 0xff;\r | |
214 | data = resp.d.asBytes;\r | |
9ca155ba M |
215 | \r |
216 | PrintAndLog("isOk:%02x", isOK);\r | |
baeaf579 | 217 | if (isOK) {\r |
218 | for (i = 0; i < (sectorNo<32?3:15); i++) {\r | |
219 | PrintAndLog("data : %s", sprint_hex(data + i * 16, 16));\r | |
9ca155ba | 220 | }\r |
baeaf579 | 221 | PrintAndLog("trailer: %s", sprint_hex(data + (sectorNo<32?3:15) * 16, 16));\r |
a39af1cb | 222 | \r |
daccbcdc | 223 | PrintAndLogEx(NORMAL, "Trailer decoded:");\r |
a39af1cb | 224 | int bln = mfFirstBlockOfSector(sectorNo);\r |
225 | int blinc = (mfNumBlocksPerSector(sectorNo) > 4) ? 5 : 1;\r | |
226 | for (i = 0; i < 4; i++) {\r | |
227 | PrintAndLogEx(NORMAL, "Access block %d%s: %s", bln, ((blinc > 1) && (i < 3) ? "+" : "") , mfGetAccessConditionsDesc(i, &(data + (sectorNo<32?3:15) * 16)[6]));\r | |
228 | bln += blinc;\r | |
229 | }\r | |
230 | PrintAndLogEx(NORMAL, "UserData: %s", sprint_hex_inrow(&(data + (sectorNo<32?3:15) * 16)[9], 1));\r | |
baeaf579 | 231 | }\r |
9ca155ba | 232 | } else {\r |
baeaf579 | 233 | PrintAndLog("Command execute timeout");\r |
9ca155ba M |
234 | }\r |
235 | \r | |
baeaf579 | 236 | return 0;\r |
237 | }\r | |
9ca155ba | 238 | \r |
baeaf579 | 239 | uint8_t FirstBlockOfSector(uint8_t sectorNo)\r |
240 | {\r | |
241 | if (sectorNo < 32) {\r | |
242 | return sectorNo * 4;\r | |
9ca155ba | 243 | } else {\r |
baeaf579 | 244 | return 32 * 4 + (sectorNo - 32) * 16;\r |
9ca155ba | 245 | }\r |
9ca155ba M |
246 | }\r |
247 | \r | |
baeaf579 | 248 | uint8_t NumBlocksPerSector(uint8_t sectorNo)\r |
249 | {\r | |
250 | if (sectorNo < 32) {\r | |
251 | return 4;\r | |
252 | } else {\r | |
253 | return 16;\r | |
254 | }\r | |
255 | }\r | |
256 | \r | |
adf023ff | 257 | static int ParamCardSizeSectors(const char c) {\r |
a8561e35 | 258 | int numSectors = 16;\r |
adf023ff | 259 | switch (c) {\r |
a8561e35 | 260 | case '0' : numSectors = 5; break;\r |
261 | case '2' : numSectors = 32; break;\r | |
262 | case '4' : numSectors = 40; break;\r | |
263 | default: numSectors = 16;\r | |
adf023ff | 264 | }\r |
a8561e35 | 265 | return numSectors;\r |
adf023ff OM |
266 | }\r |
267 | \r | |
268 | static int ParamCardSizeBlocks(const char c) {\r | |
269 | int numBlocks = 16 * 4;\r | |
270 | switch (c) {\r | |
271 | case '0' : numBlocks = 5 * 4; break;\r | |
272 | case '2' : numBlocks = 32 * 4; break;\r | |
273 | case '4' : numBlocks = 32 * 4 + 8 * 16; break;\r | |
274 | default: numBlocks = 16 * 4;\r | |
275 | }\r | |
276 | return numBlocks;\r | |
277 | }\r | |
278 | \r | |
aea4d766 | 279 | int CmdHF14AMfDump(const char *Cmd)\r |
26fdb4ab | 280 | {\r |
baeaf579 | 281 | uint8_t sectorNo, blockNo;\r |
7906cb41 | 282 | \r |
4309ef8f | 283 | uint8_t keys[2][40][6];\r |
aea4d766 | 284 | uint8_t rights[40][4];\r |
79db03ef | 285 | uint8_t carddata[256][16];\r |
baeaf579 | 286 | uint8_t numSectors = 16;\r |
7906cb41 | 287 | \r |
26fdb4ab | 288 | FILE *fin;\r |
289 | FILE *fout;\r | |
7906cb41 | 290 | \r |
902cb3c0 | 291 | UsbCommand resp;\r |
baeaf579 | 292 | \r |
293 | char cmdp = param_getchar(Cmd, 0);\r | |
adf023ff | 294 | numSectors = ParamCardSizeSectors(cmdp);\r |
7906cb41 | 295 | \r |
4309ef8f MF |
296 | if (strlen(Cmd) > 3 || cmdp == 'h' || cmdp == 'H') {\r |
297 | PrintAndLog("Usage: hf mf dump [card memory] [k|m]");\r | |
baeaf579 | 298 | PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r |
4309ef8f MF |
299 | PrintAndLog(" k: Always try using both Key A and Key B for each sector, even if access bits would prohibit it");\r |
300 | PrintAndLog(" m: When missing access bits or keys, replace that block with NULL");\r | |
baeaf579 | 301 | PrintAndLog("");\r |
302 | PrintAndLog("Samples: hf mf dump");\r | |
303 | PrintAndLog(" hf mf dump 4");\r | |
4309ef8f | 304 | PrintAndLog(" hf mf dump 4 m");\r |
baeaf579 | 305 | return 0;\r |
306 | }\r | |
7906cb41 | 307 | \r |
4309ef8f MF |
308 | char opts = param_getchar(Cmd, 1);\r |
309 | bool useBothKeysAlways = false;\r | |
310 | if (opts == 'k' || opts == 'K') useBothKeysAlways = true;\r | |
311 | bool nullMissingKeys = false;\r | |
312 | if (opts == 'm' || opts == 'M') nullMissingKeys = true;\r | |
313 | \r | |
26fdb4ab | 314 | if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {\r |
aea4d766 | 315 | PrintAndLog("Could not find file dumpkeys.bin");\r |
26fdb4ab | 316 | return 1;\r |
317 | }\r | |
7906cb41 | 318 | \r |
4309ef8f MF |
319 | // Read keys from file\r |
320 | for (int group=0; group<=1; group++) {\r | |
321 | for (sectorNo=0; sectorNo<numSectors; sectorNo++) {\r | |
322 | size_t bytes_read = fread(keys[group][sectorNo], 1, 6, fin);\r | |
323 | if (bytes_read != 6) {\r | |
324 | PrintAndLog("File reading error.");\r | |
325 | fclose(fin);\r | |
326 | return 2;\r | |
327 | }\r | |
a39af1cb | 328 | }\r |
26fdb4ab | 329 | }\r |
7906cb41 | 330 | \r |
97d582a6 | 331 | fclose(fin);\r |
baeaf579 | 332 | \r |
26fdb4ab | 333 | PrintAndLog("|-----------------------------------------|");\r |
334 | PrintAndLog("|------ Reading sector access bits...-----|");\r | |
335 | PrintAndLog("|-----------------------------------------|");\r | |
40c6a02b | 336 | uint8_t tries = 0;\r |
baeaf579 | 337 | for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r |
7906cb41 | 338 | for (tries = 0; tries < 3; tries++) {\r |
40c6a02b | 339 | UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 0, 0}};\r |
4309ef8f MF |
340 | // At least the Access Conditions can always be read with key A.\r |
341 | memcpy(c.d.asBytes, keys[0][sectorNo], 6);\r | |
40c6a02b | 342 | SendCommand(&c);\r |
26fdb4ab | 343 | \r |
40c6a02b | 344 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r |
345 | uint8_t isOK = resp.arg[0] & 0xff;\r | |
346 | uint8_t *data = resp.d.asBytes;\r | |
347 | if (isOK){\r | |
348 | rights[sectorNo][0] = ((data[7] & 0x10)>>2) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>4); // C1C2C3 for data area 0\r | |
349 | rights[sectorNo][1] = ((data[7] & 0x20)>>3) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>5); // C1C2C3 for data area 1\r | |
350 | rights[sectorNo][2] = ((data[7] & 0x40)>>4) | ((data[8] & 0x4)>>1) | ((data[8] & 0x40)>>6); // C1C2C3 for data area 2\r | |
351 | rights[sectorNo][3] = ((data[7] & 0x80)>>5) | ((data[8] & 0x8)>>2) | ((data[8] & 0x80)>>7); // C1C2C3 for sector trailer\r | |
352 | break;\r | |
353 | } else if (tries == 2) { // on last try set defaults\r | |
354 | PrintAndLog("Could not get access rights for sector %2d. Trying with defaults...", sectorNo);\r | |
355 | rights[sectorNo][0] = rights[sectorNo][1] = rights[sectorNo][2] = 0x00;\r | |
356 | rights[sectorNo][3] = 0x01;\r | |
357 | }\r | |
baeaf579 | 358 | } else {\r |
40c6a02b | 359 | PrintAndLog("Command execute timeout when trying to read access rights for sector %2d. Trying with defaults...", sectorNo);\r |
c626c56e | 360 | rights[sectorNo][0] = rights[sectorNo][1] = rights[sectorNo][2] = 0x00;\r |
361 | rights[sectorNo][3] = 0x01;\r | |
26fdb4ab | 362 | }\r |
26fdb4ab | 363 | }\r |
364 | }\r | |
7906cb41 | 365 | \r |
26fdb4ab | 366 | PrintAndLog("|-----------------------------------------|");\r |
367 | PrintAndLog("|----- Dumping all blocks to file... -----|");\r | |
368 | PrintAndLog("|-----------------------------------------|");\r | |
7906cb41 | 369 | \r |
79db03ef | 370 | bool isOK = true;\r |
371 | for (sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {\r | |
372 | for (blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r | |
baeaf579 | 373 | bool received = false;\r |
7906cb41 | 374 | for (tries = 0; tries < 3; tries++) {\r |
a39af1cb | 375 | if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer. At least the Access Conditions can always be read with key A.\r |
79db03ef | 376 | UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 0, 0}};\r |
4309ef8f | 377 | memcpy(c.d.asBytes, keys[0][sectorNo], 6);\r |
79db03ef | 378 | SendCommand(&c);\r |
379 | received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r | |
4309ef8f MF |
380 | } else if (useBothKeysAlways) {\r |
381 | // Always try both keys, even if access conditions wouldn't work.\r | |
382 | for (int k=0; k<=1; k++) {\r | |
383 | UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 1, 0}};\r | |
384 | memcpy(c.d.asBytes, keys[k][sectorNo], 6);\r | |
385 | SendCommand(&c);\r | |
386 | received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r | |
387 | \r | |
388 | // Don't try the other one on success.\r | |
389 | if (resp.arg[0] & 0xff) break;\r | |
390 | }\r | |
a39af1cb | 391 | } else { // data block. Check if it can be read with key A or key B\r |
40c6a02b | 392 | uint8_t data_area = sectorNo<32?blockNo:blockNo/5;\r |
a39af1cb | 393 | if ((rights[sectorNo][data_area] == 0x03) || (rights[sectorNo][data_area] == 0x05)) { // only key B would work\r |
40c6a02b | 394 | UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 1, 0}};\r |
4309ef8f | 395 | memcpy(c.d.asBytes, keys[1][sectorNo], 6);\r |
40c6a02b | 396 | SendCommand(&c);\r |
397 | received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r | |
a39af1cb | 398 | } else if (rights[sectorNo][data_area] == 0x07) { // no key would work\r |
40c6a02b | 399 | PrintAndLog("Access rights do not allow reading of sector %2d block %3d", sectorNo, blockNo);\r |
4309ef8f MF |
400 | if (nullMissingKeys) {\r |
401 | memset(resp.d.asBytes, 0, 16);\r | |
402 | resp.arg[0] = 1;\r | |
403 | PrintAndLog(" ... filling the block with NULL");\r | |
404 | received = true;\r | |
405 | } else {\r | |
406 | isOK = false;\r | |
407 | tries = 2;\r | |
408 | }\r | |
a39af1cb | 409 | } else { // key A would work\r |
40c6a02b | 410 | UsbCommand c = {CMD_MIFARE_READBL, {FirstBlockOfSector(sectorNo) + blockNo, 0, 0}};\r |
4309ef8f | 411 | memcpy(c.d.asBytes, keys[0][sectorNo], 6);\r |
40c6a02b | 412 | SendCommand(&c);\r |
413 | received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r | |
414 | }\r | |
415 | }\r | |
416 | if (received) {\r | |
417 | isOK = resp.arg[0] & 0xff;\r | |
418 | if (isOK) break;\r | |
26fdb4ab | 419 | }\r |
420 | }\r | |
421 | \r | |
902cb3c0 | 422 | if (received) {\r |
79db03ef | 423 | isOK = resp.arg[0] & 0xff;\r |
902cb3c0 | 424 | uint8_t *data = resp.d.asBytes;\r |
a39af1cb | 425 | if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer. Fill in the keys.\r |
4309ef8f MF |
426 | memcpy(data, keys[0][sectorNo], 6);\r |
427 | memcpy(data + 10, keys[1][sectorNo], 6);\r | |
3d77fdfa | 428 | }\r |
26fdb4ab | 429 | if (isOK) {\r |
79db03ef | 430 | memcpy(carddata[FirstBlockOfSector(sectorNo) + blockNo], data, 16);\r |
a39af1cb | 431 | PrintAndLog("Successfully read block %2d of sector %2d.", blockNo, sectorNo);\r |
baeaf579 | 432 | } else {\r |
433 | PrintAndLog("Could not read block %2d of sector %2d", blockNo, sectorNo);\r | |
79db03ef | 434 | break;\r |
26fdb4ab | 435 | }\r |
436 | }\r | |
437 | else {\r | |
79db03ef | 438 | isOK = false;\r |
439 | PrintAndLog("Command execute timeout when trying to read block %2d of sector %2d.", blockNo, sectorNo);\r | |
440 | break;\r | |
26fdb4ab | 441 | }\r |
442 | }\r | |
443 | }\r | |
79db03ef | 444 | \r |
445 | if (isOK) {\r | |
7906cb41 | 446 | if ((fout = fopen("dumpdata.bin","wb")) == NULL) {\r |
79db03ef | 447 | PrintAndLog("Could not create file name dumpdata.bin");\r |
448 | return 1;\r | |
449 | }\r | |
450 | uint16_t numblocks = FirstBlockOfSector(numSectors - 1) + NumBlocksPerSector(numSectors - 1);\r | |
451 | fwrite(carddata, 1, 16*numblocks, fout);\r | |
452 | fclose(fout);\r | |
453 | PrintAndLog("Dumped %d blocks (%d bytes) to file dumpdata.bin", numblocks, 16*numblocks);\r | |
454 | }\r | |
7906cb41 | 455 | \r |
baeaf579 | 456 | return 0;\r |
26fdb4ab | 457 | }\r |
458 | \r | |
aea4d766 | 459 | int CmdHF14AMfRestore(const char *Cmd)\r |
26fdb4ab | 460 | {\r |
baeaf579 | 461 | uint8_t sectorNo,blockNo;\r |
26fdb4ab | 462 | uint8_t keyType = 0;\r |
83602aff | 463 | uint8_t key[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};\r |
b915fda3 | 464 | uint8_t bldata[16] = {0x00};\r |
baeaf579 | 465 | uint8_t keyA[40][6];\r |
466 | uint8_t keyB[40][6];\r | |
467 | uint8_t numSectors;\r | |
7906cb41 | 468 | \r |
26fdb4ab | 469 | FILE *fdump;\r |
470 | FILE *fkeys;\r | |
baeaf579 | 471 | \r |
472 | char cmdp = param_getchar(Cmd, 0);\r | |
473 | switch (cmdp) {\r | |
474 | case '0' : numSectors = 5; break;\r | |
7906cb41 | 475 | case '1' :\r |
baeaf579 | 476 | case '\0': numSectors = 16; break;\r |
477 | case '2' : numSectors = 32; break;\r | |
478 | case '4' : numSectors = 40; break;\r | |
479 | default: numSectors = 16;\r | |
7906cb41 | 480 | }\r |
baeaf579 | 481 | \r |
482 | if (strlen(Cmd) > 1 || cmdp == 'h' || cmdp == 'H') {\r | |
483 | PrintAndLog("Usage: hf mf restore [card memory]");\r | |
484 | PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r | |
485 | PrintAndLog("");\r | |
486 | PrintAndLog("Samples: hf mf restore");\r | |
487 | PrintAndLog(" hf mf restore 4");\r | |
488 | return 0;\r | |
489 | }\r | |
490 | \r | |
26fdb4ab | 491 | if ((fkeys = fopen("dumpkeys.bin","rb")) == NULL) {\r |
aea4d766 | 492 | PrintAndLog("Could not find file dumpkeys.bin");\r |
26fdb4ab | 493 | return 1;\r |
494 | }\r | |
7906cb41 | 495 | \r |
baeaf579 | 496 | for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r |
4c16ae80 | 497 | size_t bytes_read = fread(keyA[sectorNo], 1, 6, fkeys);\r |
498 | if (bytes_read != 6) {\r | |
baeaf579 | 499 | PrintAndLog("File reading error (dumpkeys.bin).");\r |
31d1caa5 | 500 | fclose(fkeys);\r |
759c16b3 | 501 | return 2;\r |
baeaf579 | 502 | }\r |
26fdb4ab | 503 | }\r |
baeaf579 | 504 | \r |
505 | for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r | |
4c16ae80 | 506 | size_t bytes_read = fread(keyB[sectorNo], 1, 6, fkeys);\r |
507 | if (bytes_read != 6) {\r | |
baeaf579 | 508 | PrintAndLog("File reading error (dumpkeys.bin).");\r |
31d1caa5 | 509 | fclose(fkeys);\r |
759c16b3 | 510 | return 2;\r |
baeaf579 | 511 | }\r |
26fdb4ab | 512 | }\r |
b915fda3 | 513 | \r |
ca4714cd | 514 | fclose(fkeys);\r |
79db03ef | 515 | \r |
b915fda3 | 516 | if ((fdump = fopen("dumpdata.bin","rb")) == NULL) {\r |
517 | PrintAndLog("Could not find file dumpdata.bin");\r | |
518 | return 1;\r | |
7906cb41 | 519 | }\r |
9d710943 | 520 | PrintAndLog("Restoring dumpdata.bin to card");\r |
26fdb4ab | 521 | \r |
baeaf579 | 522 | for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r |
523 | for(blockNo = 0; blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r | |
524 | UsbCommand c = {CMD_MIFARE_WRITEBL, {FirstBlockOfSector(sectorNo) + blockNo, keyType, 0}};\r | |
26fdb4ab | 525 | memcpy(c.d.asBytes, key, 6);\r |
7906cb41 | 526 | \r |
4c16ae80 | 527 | size_t bytes_read = fread(bldata, 1, 16, fdump);\r |
528 | if (bytes_read != 16) {\r | |
baeaf579 | 529 | PrintAndLog("File reading error (dumpdata.bin).");\r |
ca4714cd | 530 | fclose(fdump);\r |
baeaf579 | 531 | return 2;\r |
532 | }\r | |
7906cb41 | 533 | \r |
a39af1cb | 534 | if (blockNo == NumBlocksPerSector(sectorNo) - 1) { // sector trailer\r |
baeaf579 | 535 | bldata[0] = (keyA[sectorNo][0]);\r |
536 | bldata[1] = (keyA[sectorNo][1]);\r | |
537 | bldata[2] = (keyA[sectorNo][2]);\r | |
538 | bldata[3] = (keyA[sectorNo][3]);\r | |
539 | bldata[4] = (keyA[sectorNo][4]);\r | |
540 | bldata[5] = (keyA[sectorNo][5]);\r | |
541 | bldata[10] = (keyB[sectorNo][0]);\r | |
542 | bldata[11] = (keyB[sectorNo][1]);\r | |
543 | bldata[12] = (keyB[sectorNo][2]);\r | |
544 | bldata[13] = (keyB[sectorNo][3]);\r | |
545 | bldata[14] = (keyB[sectorNo][4]);\r | |
546 | bldata[15] = (keyB[sectorNo][5]);\r | |
7906cb41 F |
547 | }\r |
548 | \r | |
baeaf579 | 549 | PrintAndLog("Writing to block %3d: %s", FirstBlockOfSector(sectorNo) + blockNo, sprint_hex(bldata, 16));\r |
7906cb41 | 550 | \r |
26fdb4ab | 551 | memcpy(c.d.asBytes + 10, bldata, 16);\r |
552 | SendCommand(&c);\r | |
26fdb4ab | 553 | \r |
902cb3c0 | 554 | UsbCommand resp;\r |
baeaf579 | 555 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r |
902cb3c0 | 556 | uint8_t isOK = resp.arg[0] & 0xff;\r |
26fdb4ab | 557 | PrintAndLog("isOk:%02x", isOK);\r |
558 | } else {\r | |
559 | PrintAndLog("Command execute timeout");\r | |
560 | }\r | |
561 | }\r | |
562 | }\r | |
7906cb41 | 563 | \r |
26fdb4ab | 564 | fclose(fdump);\r |
26fdb4ab | 565 | return 0;\r |
566 | }\r | |
567 | \r | |
0c86cb01 OM |
568 | //----------------------------------------------\r |
569 | // Nested\r | |
570 | //----------------------------------------------\r | |
0c86cb01 OM |
571 | \r |
572 | static void parseParamTDS(const char *Cmd, const uint8_t indx, bool *paramT, bool *paramD, uint8_t *timeout) {\r | |
5594c621 | 573 | char ctmp3[4] = {0};\r |
0c86cb01 OM |
574 | int len = param_getlength(Cmd, indx);\r |
575 | if (len > 0 && len < 4){\r | |
874572d4 | 576 | param_getstr(Cmd, indx, ctmp3, sizeof(ctmp3));\r |
a39af1cb | 577 | \r |
0c86cb01 OM |
578 | *paramT |= (ctmp3[0] == 't' || ctmp3[0] == 'T');\r |
579 | *paramD |= (ctmp3[0] == 'd' || ctmp3[0] == 'D');\r | |
580 | bool paramS1 = *paramT || *paramD;\r | |
581 | \r | |
582 | // slow and very slow\r | |
583 | if (ctmp3[0] == 's' || ctmp3[0] == 'S' || ctmp3[1] == 's' || ctmp3[1] == 'S') {\r | |
584 | *timeout = 11; // slow\r | |
a39af1cb | 585 | \r |
0c86cb01 OM |
586 | if (!paramS1 && (ctmp3[1] == 's' || ctmp3[1] == 'S')) {\r |
587 | *timeout = 53; // very slow\r | |
588 | }\r | |
589 | if (paramS1 && (ctmp3[2] == 's' || ctmp3[2] == 'S')) {\r | |
590 | *timeout = 53; // very slow\r | |
591 | }\r | |
592 | }\r | |
593 | }\r | |
594 | }\r | |
595 | \r | |
9ca155ba M |
596 | int CmdHF14AMfNested(const char *Cmd)\r |
597 | {\r | |
598 | int i, j, res, iterations;\r | |
7cb8516c | 599 | sector_t *e_sector = NULL;\r |
9ca155ba M |
600 | uint8_t blockNo = 0;\r |
601 | uint8_t keyType = 0;\r | |
602 | uint8_t trgBlockNo = 0;\r | |
603 | uint8_t trgKeyType = 0;\r | |
baeaf579 | 604 | uint8_t SectorsCnt = 0;\r |
9ca155ba | 605 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r |
e537c3e8 | 606 | uint8_t keyBlock[MifareDefaultKeysSize * 6];\r |
9ca155ba | 607 | uint64_t key64 = 0;\r |
0c86cb01 OM |
608 | // timeout in units. (ms * 106)/10 or us*0.0106\r |
609 | uint8_t btimeout14a = MF_CHKKEYS_DEFTIMEOUT; // fast by default\r | |
a39af1cb | 610 | \r |
adf023ff | 611 | bool autosearchKey = false;\r |
7906cb41 | 612 | \r |
adf023ff | 613 | bool transferToEml = false;\r |
baeaf579 | 614 | bool createDumpFile = false;\r |
26fdb4ab | 615 | FILE *fkeys;\r |
21156267 | 616 | uint8_t standart[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r |
617 | uint8_t tempkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r | |
7906cb41 | 618 | \r |
9ca155ba M |
619 | char cmdp, ctmp;\r |
620 | \r | |
621 | if (strlen(Cmd)<3) {\r | |
622 | PrintAndLog("Usage:");\r | |
0c86cb01 OM |
623 | PrintAndLog(" all sectors: hf mf nested <card memory> <block number> <key A/B> <key (12 hex symbols)> [t|d|s|ss]");\r |
624 | PrintAndLog(" all sectors autosearch key: hf mf nested <card memory> * [t|d|s|ss]");\r | |
0014cb46 M |
625 | PrintAndLog(" one sector: hf mf nested o <block number> <key A/B> <key (12 hex symbols)>");\r |
626 | PrintAndLog(" <target block number> <target key A/B> [t]");\r | |
adf023ff | 627 | PrintAndLog(" ");\r |
9ca155ba | 628 | PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r |
adf023ff OM |
629 | PrintAndLog("t - transfer keys to emulator memory");\r |
630 | PrintAndLog("d - write keys to binary file dumpkeys.bin");\r | |
0c86cb01 OM |
631 | PrintAndLog("s - Slow (1ms) check keys (required by some non standard cards)");\r |
632 | PrintAndLog("ss - Very slow (5ms) check keys");\r | |
9ca155ba M |
633 | PrintAndLog(" ");\r |
634 | PrintAndLog(" sample1: hf mf nested 1 0 A FFFFFFFFFFFF ");\r | |
79db03ef | 635 | PrintAndLog(" sample2: hf mf nested 1 0 A FFFFFFFFFFFF t ");\r |
636 | PrintAndLog(" sample3: hf mf nested 1 0 A FFFFFFFFFFFF d ");\r | |
637 | PrintAndLog(" sample4: hf mf nested o 0 A FFFFFFFFFFFF 4 A");\r | |
adf023ff | 638 | PrintAndLog(" sample5: hf mf nested 1 * t");\r |
0c86cb01 | 639 | PrintAndLog(" sample6: hf mf nested 1 * ss");\r |
9ca155ba | 640 | return 0;\r |
7906cb41 F |
641 | }\r |
642 | \r | |
adf023ff | 643 | // <card memory>\r |
9ca155ba | 644 | cmdp = param_getchar(Cmd, 0);\r |
adf023ff OM |
645 | if (cmdp == 'o' || cmdp == 'O') {\r |
646 | cmdp = 'o';\r | |
647 | SectorsCnt = 1;\r | |
648 | } else {\r | |
649 | SectorsCnt = ParamCardSizeSectors(cmdp);\r | |
9ca155ba | 650 | }\r |
a39af1cb | 651 | \r |
adf023ff OM |
652 | // <block number>. number or autosearch key (*)\r |
653 | if (param_getchar(Cmd, 1) == '*') {\r | |
654 | autosearchKey = true;\r | |
7906cb41 | 655 | \r |
0c86cb01 | 656 | parseParamTDS(Cmd, 2, &transferToEml, &createDumpFile, &btimeout14a);\r |
7906cb41 | 657 | \r |
a39af1cb | 658 | PrintAndLog("--nested. sectors:%2d, block no:*, eml:%c, dmp=%c checktimeout=%d us",\r |
0c86cb01 | 659 | SectorsCnt, transferToEml?'y':'n', createDumpFile?'y':'n', ((int)btimeout14a * 10000) / 106);\r |
adf023ff OM |
660 | } else {\r |
661 | blockNo = param_get8(Cmd, 1);\r | |
7906cb41 | 662 | \r |
adf023ff | 663 | ctmp = param_getchar(Cmd, 2);\r |
baeaf579 | 664 | if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r |
adf023ff | 665 | PrintAndLog("Key type must be A or B");\r |
9ca155ba M |
666 | return 1;\r |
667 | }\r | |
adf023ff | 668 | \r |
7906cb41 | 669 | if (ctmp != 'A' && ctmp != 'a')\r |
adf023ff OM |
670 | keyType = 1;\r |
671 | \r | |
672 | if (param_gethex(Cmd, 3, key, 12)) {\r | |
673 | PrintAndLog("Key must include 12 HEX symbols");\r | |
674 | return 1;\r | |
675 | }\r | |
7906cb41 | 676 | \r |
adf023ff OM |
677 | // check if we can authenticate to sector\r |
678 | res = mfCheckKeys(blockNo, keyType, true, 1, key, &key64);\r | |
679 | if (res) {\r | |
680 | PrintAndLog("Can't authenticate to block:%3d key type:%c key:%s", blockNo, keyType?'B':'A', sprint_hex(key, 6));\r | |
681 | return 3;\r | |
9ca155ba | 682 | }\r |
8556b852 | 683 | \r |
adf023ff | 684 | // one sector nested\r |
a39af1cb | 685 | if (cmdp == 'o') {\r |
adf023ff | 686 | trgBlockNo = param_get8(Cmd, 4);\r |
7906cb41 | 687 | \r |
adf023ff OM |
688 | ctmp = param_getchar(Cmd, 5);\r |
689 | if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r | |
690 | PrintAndLog("Target key type must be A or B");\r | |
691 | return 1;\r | |
692 | }\r | |
693 | if (ctmp != 'A' && ctmp != 'a')\r | |
694 | trgKeyType = 1;\r | |
7906cb41 | 695 | \r |
0c86cb01 | 696 | parseParamTDS(Cmd, 6, &transferToEml, &createDumpFile, &btimeout14a);\r |
adf023ff | 697 | } else {\r |
0c86cb01 | 698 | parseParamTDS(Cmd, 4, &transferToEml, &createDumpFile, &btimeout14a);\r |
adf023ff OM |
699 | }\r |
700 | \r | |
a39af1cb | 701 | PrintAndLog("--nested. sectors:%2d, block no:%3d, key type:%c, eml:%c, dmp=%c checktimeout=%d us",\r |
0c86cb01 | 702 | SectorsCnt, blockNo, keyType?'B':'A', transferToEml?'y':'n', createDumpFile?'y':'n', ((int)btimeout14a * 10000) / 106);\r |
adf023ff OM |
703 | }\r |
704 | \r | |
705 | // one-sector nested\r | |
706 | if (cmdp == 'o') { // ------------------------------------ one sector working\r | |
baeaf579 | 707 | PrintAndLog("--target block no:%3d, target key type:%c ", trgBlockNo, trgKeyType?'B':'A');\r |
dc8ba239 | 708 | int16_t isOK = mfnested(blockNo, keyType, key, trgBlockNo, trgKeyType, keyBlock, true);\r |
709 | if (isOK) {\r | |
710 | switch (isOK) {\r | |
711 | case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;\r | |
712 | case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;\r | |
713 | case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;\r | |
714 | default : PrintAndLog("Unknown Error.\n");\r | |
715 | }\r | |
9ca155ba M |
716 | return 2;\r |
717 | }\r | |
9492e0b0 | 718 | key64 = bytes_to_num(keyBlock, 6);\r |
719 | if (key64) {\r | |
43534cba | 720 | PrintAndLog("Found valid key:%012" PRIx64, key64);\r |
8556b852 M |
721 | \r |
722 | // transfer key to the emulator\r | |
723 | if (transferToEml) {\r | |
baeaf579 | 724 | uint8_t sectortrailer;\r |
a39af1cb | 725 | if (trgBlockNo < 32*4) { // 4 block sector\r |
32e6891a | 726 | sectortrailer = trgBlockNo | 0x03;\r |
a39af1cb | 727 | } else { // 16 block sector\r |
32e6891a | 728 | sectortrailer = trgBlockNo | 0x0f;\r |
baeaf579 | 729 | }\r |
730 | mfEmlGetMem(keyBlock, sectortrailer, 1);\r | |
7906cb41 | 731 | \r |
8556b852 M |
732 | if (!trgKeyType)\r |
733 | num_to_bytes(key64, 6, keyBlock);\r | |
734 | else\r | |
735 | num_to_bytes(key64, 6, &keyBlock[10]);\r | |
7906cb41 | 736 | mfEmlSetMem(keyBlock, sectortrailer, 1);\r |
adf023ff | 737 | PrintAndLog("Key transferred to emulator memory.");\r |
8556b852 M |
738 | }\r |
739 | } else {\r | |
9ca155ba | 740 | PrintAndLog("No valid key found");\r |
8556b852 | 741 | }\r |
21156267 | 742 | }\r |
743 | else { // ------------------------------------ multiple sectors working\r | |
acf0582d | 744 | uint64_t msclock1;\r |
745 | msclock1 = msclock();\r | |
9492e0b0 | 746 | \r |
7cb8516c | 747 | e_sector = calloc(SectorsCnt, sizeof(sector_t));\r |
9ca155ba | 748 | if (e_sector == NULL) return 1;\r |
7906cb41 | 749 | \r |
baeaf579 | 750 | //test current key and additional standard keys first\r |
275d9e61 OM |
751 | for (int defaultKeyCounter = 0; defaultKeyCounter < MifareDefaultKeysSize; defaultKeyCounter++){\r |
752 | num_to_bytes(MifareDefaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));\r | |
753 | }\r | |
9ca155ba M |
754 | \r |
755 | PrintAndLog("Testing known keys. Sector count=%d", SectorsCnt);\r | |
e537c3e8 | 756 | mfCheckKeysSec(SectorsCnt, 2, btimeout14a, true, MifareDefaultKeysSize, keyBlock, e_sector);\r |
a39af1cb | 757 | \r |
adf023ff OM |
758 | // get known key from array\r |
759 | bool keyFound = false;\r | |
760 | if (autosearchKey) {\r | |
761 | for (i = 0; i < SectorsCnt; i++) {\r | |
762 | for (j = 0; j < 2; j++) {\r | |
763 | if (e_sector[i].foundKey[j]) {\r | |
764 | // get known key\r | |
765 | blockNo = i * 4;\r | |
766 | keyType = j;\r | |
767 | num_to_bytes(e_sector[i].Key[j], 6, key);\r | |
adf023ff OM |
768 | keyFound = true;\r |
769 | break;\r | |
770 | }\r | |
771 | }\r | |
772 | if (keyFound) break;\r | |
a39af1cb | 773 | }\r |
adf023ff OM |
774 | \r |
775 | // Can't found a key....\r | |
776 | if (!keyFound) {\r | |
777 | PrintAndLog("Can't found any of the known keys.");\r | |
44964fd1 | 778 | free(e_sector);\r |
adf023ff OM |
779 | return 4;\r |
780 | }\r | |
781 | PrintAndLog("--auto key. block no:%3d, key type:%c key:%s", blockNo, keyType?'B':'A', sprint_hex(key, 6));\r | |
782 | }\r | |
7906cb41 | 783 | \r |
9ca155ba M |
784 | // nested sectors\r |
785 | iterations = 0;\r | |
786 | PrintAndLog("nested...");\r | |
9492e0b0 | 787 | bool calibrate = true;\r |
9ca155ba | 788 | for (i = 0; i < NESTED_SECTOR_RETRY; i++) {\r |
baeaf579 | 789 | for (uint8_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r |
7906cb41 | 790 | for (trgKeyType = 0; trgKeyType < 2; trgKeyType++) {\r |
baeaf579 | 791 | if (e_sector[sectorNo].foundKey[trgKeyType]) continue;\r |
9492e0b0 | 792 | PrintAndLog("-----------------------------------------------");\r |
dc8ba239 | 793 | int16_t isOK = mfnested(blockNo, keyType, key, FirstBlockOfSector(sectorNo), trgKeyType, keyBlock, calibrate);\r |
794 | if(isOK) {\r | |
795 | switch (isOK) {\r | |
796 | case -1 : PrintAndLog("Error: No response from Proxmark.\n"); break;\r | |
797 | case -2 : PrintAndLog("Button pressed. Aborted.\n"); break;\r | |
798 | case -3 : PrintAndLog("Tag isn't vulnerable to Nested Attack (random numbers are not predictable).\n"); break;\r | |
799 | default : PrintAndLog("Unknown Error.\n");\r | |
800 | }\r | |
3fe4ff4f | 801 | free(e_sector);\r |
dc8ba239 | 802 | return 2;\r |
803 | } else {\r | |
9492e0b0 | 804 | calibrate = false;\r |
805 | }\r | |
7906cb41 | 806 | \r |
9ca155ba | 807 | iterations++;\r |
9492e0b0 | 808 | \r |
809 | key64 = bytes_to_num(keyBlock, 6);\r | |
810 | if (key64) {\r | |
43534cba | 811 | PrintAndLog("Found valid key:%012" PRIx64, key64);\r |
baeaf579 | 812 | e_sector[sectorNo].foundKey[trgKeyType] = 1;\r |
813 | e_sector[sectorNo].Key[trgKeyType] = key64;\r | |
a39af1cb | 814 | \r |
275d9e61 | 815 | // try to check this key as a key to the other sectors\r |
0c86cb01 | 816 | mfCheckKeysSec(SectorsCnt, 2, btimeout14a, true, 1, keyBlock, e_sector);\r |
9ca155ba M |
817 | }\r |
818 | }\r | |
9492e0b0 | 819 | }\r |
9ca155ba M |
820 | }\r |
821 | \r | |
adf023ff OM |
822 | // print nested statistic\r |
823 | PrintAndLog("\n\n-----------------------------------------------\nNested statistic:\nIterations count: %d", iterations);\r | |
824 | PrintAndLog("Time in nested: %1.3f (%1.3f sec per key)", ((float)(msclock() - msclock1))/1000.0, ((float)(msclock() - msclock1))/iterations/1000.0);\r | |
a39af1cb | 825 | \r |
adf023ff | 826 | // print result\r |
9ca155ba M |
827 | PrintAndLog("|---|----------------|---|----------------|---|");\r |
828 | PrintAndLog("|sec|key A |res|key B |res|");\r | |
829 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
830 | for (i = 0; i < SectorsCnt; i++) {\r | |
43534cba | 831 | PrintAndLog("|%03d| %012" PRIx64 " | %d | %012" PRIx64 " | %d |", i,\r |
9ca155ba M |
832 | e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);\r |
833 | }\r | |
834 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
7906cb41 | 835 | \r |
adf023ff | 836 | // transfer keys to the emulator memory\r |
8556b852 M |
837 | if (transferToEml) {\r |
838 | for (i = 0; i < SectorsCnt; i++) {\r | |
baeaf579 | 839 | mfEmlGetMem(keyBlock, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);\r |
8556b852 | 840 | if (e_sector[i].foundKey[0])\r |
ab8b654e | 841 | num_to_bytes(e_sector[i].Key[0], 6, keyBlock);\r |
8556b852 M |
842 | if (e_sector[i].foundKey[1])\r |
843 | num_to_bytes(e_sector[i].Key[1], 6, &keyBlock[10]);\r | |
baeaf579 | 844 | mfEmlSetMem(keyBlock, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1);\r |
7906cb41 | 845 | }\r |
adf023ff | 846 | PrintAndLog("Keys transferred to emulator memory.");\r |
8556b852 | 847 | }\r |
7906cb41 | 848 | \r |
21156267 | 849 | // Create dump file\r |
26fdb4ab | 850 | if (createDumpFile) {\r |
7906cb41 | 851 | if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {\r |
aea4d766 | 852 | PrintAndLog("Could not create file dumpkeys.bin");\r |
26fdb4ab | 853 | free(e_sector);\r |
854 | return 1;\r | |
855 | }\r | |
baeaf579 | 856 | PrintAndLog("Printing keys to binary file dumpkeys.bin...");\r |
857 | for(i=0; i<SectorsCnt; i++) {\r | |
21156267 | 858 | if (e_sector[i].foundKey[0]){\r |
859 | num_to_bytes(e_sector[i].Key[0], 6, tempkey);\r | |
860 | fwrite ( tempkey, 1, 6, fkeys );\r | |
861 | }\r | |
862 | else{\r | |
863 | fwrite ( &standart, 1, 6, fkeys );\r | |
864 | }\r | |
26fdb4ab | 865 | }\r |
baeaf579 | 866 | for(i=0; i<SectorsCnt; i++) {\r |
21156267 | 867 | if (e_sector[i].foundKey[1]){\r |
868 | num_to_bytes(e_sector[i].Key[1], 6, tempkey);\r | |
869 | fwrite ( tempkey, 1, 6, fkeys );\r | |
870 | }\r | |
871 | else{\r | |
872 | fwrite ( &standart, 1, 6, fkeys );\r | |
873 | }\r | |
26fdb4ab | 874 | }\r |
875 | fclose(fkeys);\r | |
876 | }\r | |
7906cb41 | 877 | \r |
9ca155ba M |
878 | free(e_sector);\r |
879 | }\r | |
9ca155ba M |
880 | return 0;\r |
881 | }\r | |
882 | \r | |
c48c4d78 | 883 | \r |
884 | int CmdHF14AMfNestedHard(const char *Cmd)\r | |
885 | {\r | |
886 | uint8_t blockNo = 0;\r | |
887 | uint8_t keyType = 0;\r | |
888 | uint8_t trgBlockNo = 0;\r | |
889 | uint8_t trgKeyType = 0;\r | |
890 | uint8_t key[6] = {0, 0, 0, 0, 0, 0};\r | |
891 | uint8_t trgkey[6] = {0, 0, 0, 0, 0, 0};\r | |
7906cb41 | 892 | \r |
c48c4d78 | 893 | char ctmp;\r |
894 | ctmp = param_getchar(Cmd, 0);\r | |
895 | \r | |
896 | if (ctmp != 'R' && ctmp != 'r' && ctmp != 'T' && ctmp != 't' && strlen(Cmd) < 20) {\r | |
897 | PrintAndLog("Usage:");\r | |
898 | PrintAndLog(" hf mf hardnested <block number> <key A|B> <key (12 hex symbols)>");\r | |
899 | PrintAndLog(" <target block number> <target key A|B> [known target key (12 hex symbols)] [w] [s]");\r | |
900 | PrintAndLog(" or hf mf hardnested r [known target key]");\r | |
901 | PrintAndLog(" ");\r | |
902 | PrintAndLog("Options: ");\r | |
903 | PrintAndLog(" w: Acquire nonces and write them to binary file nonces.bin");\r | |
904 | PrintAndLog(" s: Slower acquisition (required by some non standard cards)");\r | |
905 | PrintAndLog(" r: Read nonces.bin and start attack");\r | |
362d2039 | 906 | PrintAndLog(" iX: set type of SIMD instructions. Without this flag programs autodetect it.");\r |
907 | PrintAndLog(" i5: AVX512");\r | |
908 | PrintAndLog(" i2: AVX2");\r | |
909 | PrintAndLog(" ia: AVX");\r | |
910 | PrintAndLog(" is: SSE2");\r | |
911 | PrintAndLog(" im: MMX");\r | |
912 | PrintAndLog(" in: none (use CPU regular instruction set)");\r | |
c48c4d78 | 913 | PrintAndLog(" ");\r |
914 | PrintAndLog(" sample1: hf mf hardnested 0 A FFFFFFFFFFFF 4 A");\r | |
915 | PrintAndLog(" sample2: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w");\r | |
916 | PrintAndLog(" sample3: hf mf hardnested 0 A FFFFFFFFFFFF 4 A w s");\r | |
917 | PrintAndLog(" sample4: hf mf hardnested r");\r | |
918 | PrintAndLog(" ");\r | |
919 | PrintAndLog("Add the known target key to check if it is present in the remaining key space:");\r | |
920 | PrintAndLog(" sample5: hf mf hardnested 0 A A0A1A2A3A4A5 4 A FFFFFFFFFFFF");\r | |
921 | return 0;\r | |
7906cb41 F |
922 | }\r |
923 | \r | |
c48c4d78 | 924 | bool know_target_key = false;\r |
925 | bool nonce_file_read = false;\r | |
926 | bool nonce_file_write = false;\r | |
927 | bool slow = false;\r | |
928 | int tests = 0;\r | |
7906cb41 F |
929 | \r |
930 | \r | |
362d2039 | 931 | uint16_t iindx = 0;\r |
c48c4d78 | 932 | if (ctmp == 'R' || ctmp == 'r') {\r |
933 | nonce_file_read = true;\r | |
362d2039 | 934 | iindx = 1;\r |
c48c4d78 | 935 | if (!param_gethex(Cmd, 1, trgkey, 12)) {\r |
936 | know_target_key = true;\r | |
362d2039 | 937 | iindx = 2;\r |
c48c4d78 | 938 | }\r |
939 | } else if (ctmp == 'T' || ctmp == 't') {\r | |
940 | tests = param_get32ex(Cmd, 1, 100, 10);\r | |
362d2039 | 941 | iindx = 2;\r |
c48c4d78 | 942 | if (!param_gethex(Cmd, 2, trgkey, 12)) {\r |
943 | know_target_key = true;\r | |
362d2039 | 944 | iindx = 3;\r |
c48c4d78 | 945 | }\r |
946 | } else {\r | |
947 | blockNo = param_get8(Cmd, 0);\r | |
948 | ctmp = param_getchar(Cmd, 1);\r | |
949 | if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r | |
950 | PrintAndLog("Key type must be A or B");\r | |
951 | return 1;\r | |
952 | }\r | |
7906cb41 | 953 | if (ctmp != 'A' && ctmp != 'a') {\r |
c48c4d78 | 954 | keyType = 1;\r |
955 | }\r | |
7906cb41 | 956 | \r |
c48c4d78 | 957 | if (param_gethex(Cmd, 2, key, 12)) {\r |
958 | PrintAndLog("Key must include 12 HEX symbols");\r | |
959 | return 1;\r | |
960 | }\r | |
7906cb41 | 961 | \r |
c48c4d78 | 962 | trgBlockNo = param_get8(Cmd, 3);\r |
963 | ctmp = param_getchar(Cmd, 4);\r | |
964 | if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r | |
965 | PrintAndLog("Target key type must be A or B");\r | |
966 | return 1;\r | |
967 | }\r | |
968 | if (ctmp != 'A' && ctmp != 'a') {\r | |
969 | trgKeyType = 1;\r | |
970 | }\r | |
971 | \r | |
972 | uint16_t i = 5;\r | |
973 | \r | |
974 | if (!param_gethex(Cmd, 5, trgkey, 12)) {\r | |
975 | know_target_key = true;\r | |
976 | i++;\r | |
977 | }\r | |
362d2039 | 978 | iindx = i;\r |
c48c4d78 | 979 | \r |
980 | while ((ctmp = param_getchar(Cmd, i))) {\r | |
981 | if (ctmp == 's' || ctmp == 'S') {\r | |
982 | slow = true;\r | |
983 | } else if (ctmp == 'w' || ctmp == 'W') {\r | |
984 | nonce_file_write = true;\r | |
362d2039 | 985 | } else if (param_getlength(Cmd, i) == 2 && ctmp == 'i') {\r |
986 | iindx = i;\r | |
c48c4d78 | 987 | } else {\r |
362d2039 | 988 | PrintAndLog("Possible options are w , s and/or iX");\r |
c48c4d78 | 989 | return 1;\r |
990 | }\r | |
991 | i++;\r | |
992 | }\r | |
993 | }\r | |
a39af1cb | 994 | \r |
362d2039 | 995 | SetSIMDInstr(SIMD_AUTO);\r |
996 | if (iindx > 0) {\r | |
997 | while ((ctmp = param_getchar(Cmd, iindx))) {\r | |
998 | if (param_getlength(Cmd, iindx) == 2 && ctmp == 'i') {\r | |
999 | switch(param_getchar_indx(Cmd, 1, iindx)) {\r | |
1000 | case '5':\r | |
1001 | SetSIMDInstr(SIMD_AVX512);\r | |
1002 | break;\r | |
1003 | case '2':\r | |
1004 | SetSIMDInstr(SIMD_AVX2);\r | |
1005 | break;\r | |
1006 | case 'a':\r | |
1007 | SetSIMDInstr(SIMD_AVX);\r | |
1008 | break;\r | |
1009 | case 's':\r | |
1010 | SetSIMDInstr(SIMD_SSE2);\r | |
1011 | break;\r | |
1012 | case 'm':\r | |
1013 | SetSIMDInstr(SIMD_MMX);\r | |
1014 | break;\r | |
1015 | case 'n':\r | |
1016 | SetSIMDInstr(SIMD_NONE);\r | |
1017 | break;\r | |
1018 | default:\r | |
1019 | PrintAndLog("Unknown SIMD type. %c", param_getchar_indx(Cmd, 1, iindx));\r | |
1020 | return 1;\r | |
1021 | }\r | |
1022 | }\r | |
1023 | iindx++;\r | |
a39af1cb | 1024 | }\r |
362d2039 | 1025 | }\r |
c48c4d78 | 1026 | \r |
7906cb41 F |
1027 | 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 |
1028 | trgBlockNo,\r | |
c48c4d78 | 1029 | trgKeyType?'B':'A',\r |
1030 | trgkey[0], trgkey[1], trgkey[2], trgkey[3], trgkey[4], trgkey[5],\r | |
1031 | know_target_key?"":" (not set)",\r | |
1032 | nonce_file_write?"write":nonce_file_read?"read":"none",\r | |
1033 | slow?"Yes":"No",\r | |
1034 | tests);\r | |
1035 | \r | |
1036 | int16_t isOK = mfnestedhard(blockNo, keyType, key, trgBlockNo, trgKeyType, know_target_key?trgkey:NULL, nonce_file_read, nonce_file_write, slow, tests);\r | |
1037 | \r | |
1038 | if (isOK) {\r | |
1039 | switch (isOK) {\r | |
1040 | case 1 : PrintAndLog("Error: No response from Proxmark.\n"); break;\r | |
1041 | case 2 : PrintAndLog("Button pressed. Aborted.\n"); break;\r | |
1042 | default : break;\r | |
1043 | }\r | |
1044 | return 2;\r | |
1045 | }\r | |
1046 | \r | |
1047 | return 0;\r | |
1048 | }\r | |
1049 | \r | |
1050 | \r | |
9ca155ba M |
1051 | int CmdHF14AMfChk(const char *Cmd)\r |
1052 | {\r | |
90e278d3 | 1053 | if (strlen(Cmd)<3) {\r |
275d9e61 | 1054 | PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d|s|ss] [<key (12 hex symbols)>] [<dic (*.dic)>]");\r |
90e278d3 MHS |
1055 | PrintAndLog(" * - all sectors");\r |
1056 | PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");\r | |
88b3dada | 1057 | PrintAndLog("d - write keys to binary file (not used when <block number> supplied)");\r |
5594c621 | 1058 | PrintAndLog("t - write keys to emulator memory");\r |
1059 | PrintAndLog("s - slow execute. timeout 1ms");\r | |
1060 | PrintAndLog("ss - very slow execute. timeout 5ms");\r | |
90e278d3 MHS |
1061 | PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");\r |
1062 | PrintAndLog(" hf mf chk *1 ? t");\r | |
e3c23565 | 1063 | PrintAndLog(" hf mf chk *1 ? d");\r |
275d9e61 OM |
1064 | PrintAndLog(" hf mf chk *1 ? s");\r |
1065 | PrintAndLog(" hf mf chk *1 ? dss");\r | |
90e278d3 | 1066 | return 0;\r |
7906cb41 | 1067 | }\r |
90e278d3 | 1068 | \r |
88b3dada | 1069 | FILE * f;\r |
1070 | char filename[FILE_PATH_SIZE]={0};\r | |
1071 | char buf[13];\r | |
1072 | uint8_t *keyBlock = NULL, *p;\r | |
1073 | uint16_t stKeyBlock = 20;\r | |
1074 | int i, res;\r | |
1075 | int keycnt = 0;\r | |
1076 | char ctmp = 0x00;\r | |
1077 | int clen = 0;\r | |
1078 | uint8_t blockNo = 0;\r | |
1079 | uint8_t SectorsCnt = 0;\r | |
1080 | uint8_t keyType = 0;\r | |
1081 | uint64_t key64 = 0;\r | |
5594c621 | 1082 | // timeout in units. (ms * 106)/10 or us*0.0106\r |
88b3dada | 1083 | uint8_t btimeout14a = MF_CHKKEYS_DEFTIMEOUT; // fast by default\r |
1084 | bool param3InUse = false;\r | |
1085 | bool transferToEml = 0;\r | |
1086 | bool createDumpFile = 0;\r | |
1087 | bool singleBlock = false; // Flag to ID if a single or multi key check\r | |
1088 | uint8_t keyFoundCount = 0; // Counter to display the number of keys found/transfered to emulator\r | |
1089 | \r | |
275d9e61 | 1090 | sector_t *e_sector = NULL;\r |
9ca155ba | 1091 | \r |
aea4d766 | 1092 | keyBlock = calloc(stKeyBlock, 6);\r |
1093 | if (keyBlock == NULL) return 1;\r | |
1094 | \r | |
275d9e61 OM |
1095 | int defaultKeysSize = MifareDefaultKeysSize;\r |
1096 | for (int defaultKeyCounter = 0; defaultKeyCounter < defaultKeysSize; defaultKeyCounter++){\r | |
1097 | num_to_bytes(MifareDefaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));\r | |
0c12504a | 1098 | }\r |
7906cb41 | 1099 | \r |
aea4d766 | 1100 | if (param_getchar(Cmd, 0)=='*') {\r |
adf023ff | 1101 | SectorsCnt = ParamCardSizeSectors(param_getchar(Cmd + 1, 0));\r |
aea4d766 | 1102 | }\r |
88b3dada | 1103 | else { \r |
aea4d766 | 1104 | blockNo = param_get8(Cmd, 0);\r |
88b3dada | 1105 | // Singe Key check, so Set Sector count to cover sectors (1 to sector that contains the block)\r |
1106 | // 1 and 2 Cards : Sector = blockNo/4 + 1\r | |
1107 | // Sectors 0 - 31 : 4 blocks per sector : Blocks 0 - 127\r | |
1108 | // Sectors 32 - 39 : 16 blocks per sector : Blocks 128 - 255 (4K)\r | |
1109 | if (blockNo < 128) SectorsCnt = (blockNo / 4) + 1;\r | |
1110 | else SectorsCnt = 32 + ((blockNo-128)/16) + 1;\r | |
1111 | \r | |
1112 | singleBlock = true; // Set flag for single key check\r | |
1113 | }\r | |
7906cb41 | 1114 | \r |
9ca155ba | 1115 | ctmp = param_getchar(Cmd, 1);\r |
55b700a0 | 1116 | clen = param_getlength(Cmd, 1);\r |
1117 | if (clen == 1) {\r | |
1118 | switch (ctmp) {\r | |
1119 | case 'a': case 'A':\r | |
1120 | keyType = 0;\r | |
1121 | break;\r | |
1122 | case 'b': case 'B':\r | |
1123 | keyType = 1;\r | |
1124 | break;\r | |
1125 | case '?':\r | |
1126 | keyType = 2;\r | |
1127 | break;\r | |
1128 | default:\r | |
1129 | PrintAndLog("Key type must be A , B or ?");\r | |
1130 | free(keyBlock);\r | |
1131 | return 1;\r | |
1132 | };\r | |
1133 | }\r | |
88b3dada | 1134 | \r |
5594c621 | 1135 | parseParamTDS(Cmd, 2, &transferToEml, &createDumpFile, &btimeout14a);\r |
a39af1cb | 1136 | \r |
88b3dada | 1137 | if (singleBlock & createDumpFile) {\r |
1138 | PrintAndLog (" block key check (<block no>) and write to dump file (d) combination is not supported ");\r | |
1139 | PrintAndLog (" please remove option d and try again");\r | |
1140 | return 1;\r | |
1141 | }\r | |
1142 | \r | |
5594c621 | 1143 | param3InUse = transferToEml | createDumpFile | (btimeout14a != MF_CHKKEYS_DEFTIMEOUT);\r |
7906cb41 | 1144 | \r |
a39af1cb | 1145 | PrintAndLog("--chk keys. sectors:%2d, block no:%3d, key type:%c, eml:%c, dmp=%c checktimeout=%d us",\r |
5594c621 | 1146 | SectorsCnt, blockNo, keyType?'B':'A', transferToEml?'y':'n', createDumpFile?'y':'n', ((int)btimeout14a * 10000) / 106);\r |
a39af1cb | 1147 | \r |
275d9e61 | 1148 | for (i = param3InUse; param_getchar(Cmd, 2 + i); i++) {\r |
aea4d766 | 1149 | if (!param_gethex(Cmd, 2 + i, keyBlock + 6 * keycnt, 12)) {\r |
1150 | if ( stKeyBlock - keycnt < 2) {\r | |
1151 | p = realloc(keyBlock, 6*(stKeyBlock+=10));\r | |
1152 | if (!p) {\r | |
1153 | PrintAndLog("Cannot allocate memory for Keys");\r | |
1154 | free(keyBlock);\r | |
1155 | return 2;\r | |
1156 | }\r | |
1157 | keyBlock = p;\r | |
1158 | }\r | |
baeaf579 | 1159 | PrintAndLog("chk key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r |
88b3dada | 1160 | (keyBlock + 6*keycnt)[0], (keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r |
a39af1cb | 1161 | (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r |
aea4d766 | 1162 | keycnt++;\r |
1163 | } else {\r | |
1164 | // May be a dic file\r | |
874572d4 | 1165 | if ( param_getstr(Cmd, 2 + i, filename, sizeof(filename)) >= FILE_PATH_SIZE ) {\r |
aea4d766 | 1166 | PrintAndLog("File name too long");\r |
1167 | free(keyBlock);\r | |
1168 | return 2;\r | |
1169 | }\r | |
7906cb41 | 1170 | \r |
aea4d766 | 1171 | if ( (f = fopen( filename , "r")) ) {\r |
0c12504a | 1172 | while( fgets(buf, sizeof(buf), f) ){\r |
99a71a0d | 1173 | if (strlen(buf) < 12 || buf[11] == '\n')\r |
1174 | continue;\r | |
7906cb41 | 1175 | \r |
99a71a0d | 1176 | while (fgetc(f) != '\n' && !feof(f)) ; //goto next line\r |
7906cb41 | 1177 | \r |
a39af1cb | 1178 | if( buf[0]=='#' ) continue; //The line start with # is comment, skip\r |
9ca155ba | 1179 | \r |
3ded0f97 | 1180 | if (!isxdigit((unsigned char)buf[0])){\r |
99a71a0d | 1181 | PrintAndLog("File content error. '%s' must include 12 HEX symbols",buf);\r |
aea4d766 | 1182 | continue;\r |
1183 | }\r | |
7906cb41 | 1184 | \r |
99a71a0d | 1185 | buf[12] = 0;\r |
aea4d766 | 1186 | \r |
1187 | if ( stKeyBlock - keycnt < 2) {\r | |
1188 | p = realloc(keyBlock, 6*(stKeyBlock+=10));\r | |
1189 | if (!p) {\r | |
1190 | PrintAndLog("Cannot allocate memory for defKeys");\r | |
1191 | free(keyBlock);\r | |
4c16ae80 | 1192 | fclose(f);\r |
aea4d766 | 1193 | return 2;\r |
1194 | }\r | |
1195 | keyBlock = p;\r | |
1196 | }\r | |
1197 | memset(keyBlock + 6 * keycnt, 0, 6);\r | |
99a71a0d | 1198 | num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);\r |
43534cba | 1199 | PrintAndLog("chk custom key[%2d] %012" PRIx64 , keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r |
aea4d766 | 1200 | keycnt++;\r |
0c12504a | 1201 | memset(buf, 0, sizeof(buf));\r |
aea4d766 | 1202 | }\r |
90e278d3 | 1203 | fclose(f);\r |
aea4d766 | 1204 | } else {\r |
1205 | PrintAndLog("File: %s: not found or locked.", filename);\r | |
1206 | free(keyBlock);\r | |
1207 | return 1;\r | |
aea4d766 | 1208 | }\r |
9ca155ba | 1209 | }\r |
9ca155ba | 1210 | }\r |
7906cb41 | 1211 | \r |
275d9e61 | 1212 | // fill with default keys\r |
9ca155ba | 1213 | if (keycnt == 0) {\r |
baeaf579 | 1214 | PrintAndLog("No key specified, trying default keys");\r |
0c12504a | 1215 | for (;keycnt < defaultKeysSize; keycnt++)\r |
baeaf579 | 1216 | PrintAndLog("chk default key[%2d] %02x%02x%02x%02x%02x%02x", keycnt,\r |
88b3dada | 1217 | (keyBlock + 6*keycnt)[0], (keyBlock + 6*keycnt)[1], (keyBlock + 6*keycnt)[2],\r |
a39af1cb | 1218 | (keyBlock + 6*keycnt)[3], (keyBlock + 6*keycnt)[4], (keyBlock + 6*keycnt)[5], 6);\r |
79db03ef | 1219 | }\r |
7906cb41 | 1220 | \r |
79db03ef | 1221 | // initialize storage for found keys\r |
275d9e61 | 1222 | e_sector = calloc(SectorsCnt, sizeof(sector_t));\r |
44964fd1 | 1223 | if (e_sector == NULL) {\r |
1224 | free(keyBlock);\r | |
1225 | return 1;\r | |
1226 | }\r | |
275d9e61 | 1227 | for (uint8_t keyAB = 0; keyAB < 2; keyAB++) {\r |
79db03ef | 1228 | for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r |
275d9e61 OM |
1229 | e_sector[sectorNo].Key[keyAB] = 0xffffffffffff;\r |
1230 | e_sector[sectorNo].foundKey[keyAB] = 0;\r | |
79db03ef | 1231 | }\r |
9ca155ba | 1232 | }\r |
275d9e61 OM |
1233 | printf("\n");\r |
1234 | \r | |
88b3dada | 1235 | bool foundAKey = false;\r |
1236 | uint32_t max_keys = keycnt > USB_CMD_DATA_SIZE / 6 ? USB_CMD_DATA_SIZE / 6 : keycnt;\r | |
1237 | \r | |
1238 | // !SingleKey, so all key check (if SectorsCnt > 0)\r | |
1239 | if (!singleBlock) { \r | |
275d9e61 OM |
1240 | PrintAndLog("To cancel this operation press the button on the proxmark...");\r |
1241 | printf("--");\r | |
1242 | for (uint32_t c = 0; c < keycnt; c += max_keys) {\r | |
1243 | \r | |
1244 | uint32_t size = keycnt-c > max_keys ? max_keys : keycnt-c;\r | |
5594c621 | 1245 | res = mfCheckKeysSec(SectorsCnt, keyType, btimeout14a, true, size, &keyBlock[6 * c], e_sector); // timeout is (ms * 106)/10 or us*0.0106\r |
7906cb41 | 1246 | \r |
275d9e61 OM |
1247 | if (res != 1) {\r |
1248 | if (!res) {\r | |
1249 | printf("o");\r | |
1250 | foundAKey = true;\r | |
1251 | } else {\r | |
1252 | printf(".");\r | |
1253 | }\r | |
1254 | } else {\r | |
1255 | printf("\n");\r | |
1256 | PrintAndLog("Command execute timeout");\r | |
1257 | }\r | |
1258 | }\r | |
88b3dada | 1259 | } else { \r |
275d9e61 OM |
1260 | int keyAB = keyType;\r |
1261 | do {\r | |
9492e0b0 | 1262 | for (uint32_t c = 0; c < keycnt; c+=max_keys) {\r |
275d9e61 OM |
1263 | \r |
1264 | uint32_t size = keycnt-c > max_keys ? max_keys : keycnt-c;\r | |
a39af1cb | 1265 | res = mfCheckKeys(blockNo, keyAB & 0x01, true, size, &keyBlock[6 * c], &key64);\r |
275d9e61 | 1266 | \r |
baeaf579 | 1267 | if (res != 1) {\r |
88b3dada | 1268 | if (!res) { \r |
1269 | // Use the common format below\r | |
1270 | // PrintAndLog("Found valid key:[%d:%c]%012" PRIx64, blockNo, (keyAB & 0x01)?'B':'A', key64);\r | |
275d9e61 | 1271 | foundAKey = true;\r |
88b3dada | 1272 | \r |
1273 | // Store the Single Key for display list\r | |
1274 | // For a single block check, SectorsCnt = Sector that contains the block\r | |
1275 | e_sector[SectorsCnt-1].foundKey[(keyAB & 0x01)] = true; // flag key found \r | |
1276 | e_sector[SectorsCnt-1].Key[(keyAB & 0x01)] = key64; // Save key data \r | |
1277 | \r | |
7906cb41 | 1278 | }\r |
aea4d766 | 1279 | } else {\r |
1280 | PrintAndLog("Command execute timeout");\r | |
1281 | }\r | |
1282 | }\r | |
275d9e61 | 1283 | } while(--keyAB > 0);\r |
9ca155ba | 1284 | }\r |
a39af1cb | 1285 | \r |
275d9e61 OM |
1286 | // print result\r |
1287 | if (foundAKey) {\r | |
1288 | if (SectorsCnt) {\r | |
1289 | PrintAndLog("");\r | |
1290 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
1291 | PrintAndLog("|sec|key A |res|key B |res|");\r | |
1292 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
1293 | for (i = 0; i < SectorsCnt; i++) {\r | |
88b3dada | 1294 | // If a block key check, only print a line if a key was found.\r |
1295 | if (!singleBlock || (e_sector[i].foundKey[0]) || (e_sector[i].foundKey[1]) ){ \r | |
1296 | PrintAndLog("|%03d| %012" PRIx64 " | %d | %012" PRIx64 " | %d |", i,\r | |
1297 | e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);\r | |
1298 | }\r | |
275d9e61 OM |
1299 | }\r |
1300 | PrintAndLog("|---|----------------|---|----------------|---|");\r | |
1301 | }\r | |
1302 | } else {\r | |
1303 | PrintAndLog("");\r | |
1304 | PrintAndLog("No valid keys found.");\r | |
a39af1cb | 1305 | }\r |
1306 | \r | |
79db03ef | 1307 | if (transferToEml) {\r |
1308 | uint8_t block[16];\r | |
1309 | for (uint16_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r | |
275d9e61 | 1310 | if (e_sector[sectorNo].foundKey[0] || e_sector[sectorNo].foundKey[1]) {\r |
79db03ef | 1311 | mfEmlGetMem(block, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1);\r |
1312 | for (uint16_t t = 0; t < 2; t++) {\r | |
275d9e61 OM |
1313 | if (e_sector[sectorNo].foundKey[t]) {\r |
1314 | num_to_bytes(e_sector[sectorNo].Key[t], 6, block + t * 10);\r | |
88b3dada | 1315 | keyFoundCount++; // Key found count for information\r |
79db03ef | 1316 | }\r |
1317 | }\r | |
1318 | mfEmlSetMem(block, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1);\r | |
1319 | }\r | |
1320 | }\r | |
88b3dada | 1321 | // Updated to show the actual number of keys found/transfered.\r |
1322 | PrintAndLog("%d keys(s) found have been transferred to the emulator memory",keyFoundCount);\r | |
79db03ef | 1323 | }\r |
1324 | \r | |
88b3dada | 1325 | if (createDumpFile && !singleBlock) {\r |
79db03ef | 1326 | FILE *fkeys = fopen("dumpkeys.bin","wb");\r |
7906cb41 | 1327 | if (fkeys == NULL) {\r |
aea4d766 | 1328 | PrintAndLog("Could not create file dumpkeys.bin");\r |
275d9e61 | 1329 | free(e_sector);\r |
79db03ef | 1330 | free(keyBlock);\r |
aea4d766 | 1331 | return 1;\r |
1332 | }\r | |
275d9e61 OM |
1333 | uint8_t mkey[6];\r |
1334 | for (uint8_t t = 0; t < 2; t++) {\r | |
1335 | for (uint8_t sectorNo = 0; sectorNo < SectorsCnt; sectorNo++) {\r | |
1336 | num_to_bytes(e_sector[sectorNo].Key[t], 6, mkey);\r | |
1337 | fwrite(mkey, 1, 6, fkeys);\r | |
1338 | }\r | |
aea4d766 | 1339 | }\r |
1340 | fclose(fkeys);\r | |
79db03ef | 1341 | PrintAndLog("Found keys have been dumped to file dumpkeys.bin. 0xffffffffffff has been inserted for unknown keys.");\r |
aea4d766 | 1342 | }\r |
88b3dada | 1343 | \r |
275d9e61 | 1344 | free(e_sector);\r |
79db03ef | 1345 | free(keyBlock);\r |
3fe4ff4f | 1346 | PrintAndLog("");\r |
79db03ef | 1347 | return 0;\r |
9ca155ba M |
1348 | }\r |
1349 | \r | |
5b5489ba | 1350 | void readerAttack(nonces_t ar_resp[], bool setEmulatorMem, bool doStandardAttack) {\r |
3d542a3d | 1351 | #define ATTACK_KEY_COUNT 7 // keep same as define in iso14443a.c -> Mifare1ksim()\r |
a39af1cb | 1352 | // cannot be more than 7 or it will overrun c.d.asBytes(512)\r |
bbd11876 | 1353 | uint64_t key = 0;\r |
1354 | typedef struct {\r | |
1355 | uint64_t keyA;\r | |
bbd11876 | 1356 | uint64_t keyB;\r |
1357 | } st_t;\r | |
1358 | st_t sector_trailer[ATTACK_KEY_COUNT];\r | |
1359 | memset(sector_trailer, 0x00, sizeof(sector_trailer));\r | |
1360 | \r | |
a39af1cb | 1361 | uint8_t stSector[ATTACK_KEY_COUNT];\r |
bbd11876 | 1362 | memset(stSector, 0x00, sizeof(stSector));\r |
1363 | uint8_t key_cnt[ATTACK_KEY_COUNT];\r | |
1364 | memset(key_cnt, 0x00, sizeof(key_cnt));\r | |
1365 | \r | |
1366 | for (uint8_t i = 0; i<ATTACK_KEY_COUNT; i++) {\r | |
1367 | if (ar_resp[i].ar2 > 0) {\r | |
76ef5273 | 1368 | //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 | 1369 | if (doStandardAttack && mfkey32(ar_resp[i], &key)) {\r |
76ef5273 | 1370 | 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 | 1371 | \r |
1372 | for (uint8_t ii = 0; ii<ATTACK_KEY_COUNT; ii++) {\r | |
1373 | if (key_cnt[ii]==0 || stSector[ii]==ar_resp[i].sector) {\r | |
1374 | if (ar_resp[i].keytype==0) {\r | |
1375 | //keyA\r | |
1376 | sector_trailer[ii].keyA = key;\r | |
1377 | stSector[ii] = ar_resp[i].sector;\r | |
1378 | key_cnt[ii]++;\r | |
1379 | break;\r | |
1380 | } else {\r | |
1381 | //keyB\r | |
1382 | sector_trailer[ii].keyB = key;\r | |
1383 | stSector[ii] = ar_resp[i].sector;\r | |
1384 | key_cnt[ii]++;\r | |
1385 | break;\r | |
1386 | }\r | |
1387 | }\r | |
1388 | }\r | |
7779d73c | 1389 | } else if (mfkey32_moebius(ar_resp[i+ATTACK_KEY_COUNT], &key)) {\r |
5b5489ba MF |
1390 | uint8_t sectorNum = ar_resp[i+ATTACK_KEY_COUNT].sector;\r |
1391 | uint8_t keyType = ar_resp[i+ATTACK_KEY_COUNT].keytype;\r | |
1392 | \r | |
43534cba | 1393 | PrintAndLog("M-Found Key%s for sector %02d: [%012" PRIx64 "]"\r |
5b5489ba MF |
1394 | , keyType ? "B" : "A"\r |
1395 | , sectorNum\r | |
1396 | , key\r | |
1397 | );\r | |
1398 | \r | |
1399 | for (uint8_t ii = 0; ii<ATTACK_KEY_COUNT; ii++) {\r | |
1400 | if (key_cnt[ii]==0 || stSector[ii]==sectorNum) {\r | |
1401 | if (keyType==0) {\r | |
1402 | //keyA\r | |
1403 | sector_trailer[ii].keyA = key;\r | |
1404 | stSector[ii] = sectorNum;\r | |
1405 | key_cnt[ii]++;\r | |
1406 | break;\r | |
1407 | } else {\r | |
1408 | //keyB\r | |
1409 | sector_trailer[ii].keyB = key;\r | |
1410 | stSector[ii] = sectorNum;\r | |
1411 | key_cnt[ii]++;\r | |
1412 | break;\r | |
1413 | }\r | |
1414 | }\r | |
1415 | }\r | |
1416 | continue;\r | |
bbd11876 | 1417 | }\r |
1418 | }\r | |
1419 | }\r | |
1420 | //set emulator memory for keys\r | |
1421 | if (setEmulatorMem) {\r | |
1422 | for (uint8_t i = 0; i<ATTACK_KEY_COUNT; i++) {\r | |
1423 | if (key_cnt[i]>0) {\r | |
a39af1cb | 1424 | uint8_t memBlock[16];\r |
bbd11876 | 1425 | memset(memBlock, 0x00, sizeof(memBlock));\r |
1426 | char cmd1[36];\r | |
1427 | memset(cmd1,0x00,sizeof(cmd1));\r | |
1428 | 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 | |
1429 | PrintAndLog("Setting Emulator Memory Block %02d: [%s]",stSector[i]*4+3, cmd1);\r | |
1430 | if (param_gethex(cmd1, 0, memBlock, 32)) {\r | |
1431 | PrintAndLog("block data must include 32 HEX symbols");\r | |
1432 | return;\r | |
1433 | }\r | |
7906cb41 | 1434 | \r |
bbd11876 | 1435 | UsbCommand c = {CMD_MIFARE_EML_MEMSET, {(stSector[i]*4+3), 1, 0}};\r |
1436 | memcpy(c.d.asBytes, memBlock, 16);\r | |
1437 | clearCommandBuffer();\r | |
7906cb41 | 1438 | SendCommand(&c);\r |
bbd11876 | 1439 | }\r |
1440 | }\r | |
1441 | }\r | |
ef3f88bc | 1442 | /*\r |
1443 | //un-comment to use as well moebius attack\r | |
bbd11876 | 1444 | for (uint8_t i = ATTACK_KEY_COUNT; i<ATTACK_KEY_COUNT*2; i++) {\r |
1445 | if (ar_resp[i].ar2 > 0) {\r | |
1446 | if (tryMfk32_moebius(ar_resp[i], &key)) {\r | |
1447 | 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 | |
1448 | }\r | |
1449 | }\r | |
ef3f88bc | 1450 | }*/\r |
bbd11876 | 1451 | }\r |
1452 | \r | |
a8561e35 | 1453 | int usage_hf14_mfsim(void) {\r |
1454 | PrintAndLog("Usage: hf mf sim [h] [*<card memory>] [u <uid (8, 14, or 20 hex symbols)>] [n <numreads>] [i] [x]");\r | |
c872d8c1 | 1455 | PrintAndLog("options:");\r |
a8561e35 | 1456 | PrintAndLog(" h (Optional) this help");\r |
1457 | PrintAndLog(" card memory: 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other, default> - 1K");\r | |
1458 | PrintAndLog(" u (Optional) UID 4 or 7 bytes. If not specified, the UID 4B from emulator memory will be used");\r | |
c872d8c1 | 1459 | PrintAndLog(" n (Optional) Automatically exit simulation after <numreads> blocks have been read by reader. 0 = infinite");\r |
1460 | PrintAndLog(" i (Optional) Interactive, means that console will not be returned until simulation finishes or is aborted");\r | |
1461 | PrintAndLog(" x (Optional) Crack, performs the 'reader attack', nr/ar attack against a legitimate reader, fishes out the key(s)");\r | |
76ef5273 | 1462 | PrintAndLog(" e (Optional) set keys found from 'reader attack' to emulator memory (implies x and i)");\r |
73ab92d1 | 1463 | PrintAndLog(" f (Optional) get UIDs to use for 'reader attack' from file 'f <filename.txt>' (implies x and i)");\r |
5b5489ba | 1464 | 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 | 1465 | PrintAndLog("samples:");\r |
1466 | PrintAndLog(" hf mf sim u 0a0a0a0a");\r | |
a8561e35 | 1467 | PrintAndLog(" hf mf sim *4");\r |
c872d8c1 | 1468 | PrintAndLog(" hf mf sim u 11223344556677");\r |
76ef5273 | 1469 | PrintAndLog(" hf mf sim f uids.txt");\r |
1470 | PrintAndLog(" hf mf sim u 0a0a0a0a e");\r | |
7906cb41 | 1471 | \r |
c872d8c1 | 1472 | return 0;\r |
1473 | }\r | |
1474 | \r | |
a8561e35 | 1475 | int CmdHF14AMfSim(const char *Cmd) {\r |
73ab92d1 | 1476 | UsbCommand resp;\r |
a8561e35 | 1477 | uint8_t uid[7] = {0};\r |
d2f487af | 1478 | uint8_t exitAfterNReads = 0;\r |
1479 | uint8_t flags = 0;\r | |
c872d8c1 | 1480 | int uidlen = 0;\r |
c872d8c1 | 1481 | bool setEmulatorMem = false;\r |
bbd11876 | 1482 | bool attackFromFile = false;\r |
1483 | FILE *f;\r | |
1484 | char filename[FILE_PATH_SIZE];\r | |
1485 | memset(filename, 0x00, sizeof(filename));\r | |
1486 | int len = 0;\r | |
1487 | char buf[64];\r | |
bbd11876 | 1488 | \r |
1489 | uint8_t cmdp = 0;\r | |
1490 | bool errors = false;\r | |
a8561e35 | 1491 | uint8_t cardsize = '1';\r |
bbd11876 | 1492 | \r |
1493 | while(param_getchar(Cmd, cmdp) != 0x00) {\r | |
1494 | switch(param_getchar(Cmd, cmdp)) {\r | |
a39af1cb | 1495 | case '*':\r |
a8561e35 | 1496 | cardsize = param_getchar(Cmd + 1, cmdp);\r |
1497 | switch(cardsize) {\r | |
1498 | case '0':\r | |
1499 | case '1':\r | |
1500 | case '2':\r | |
1501 | case '4': break;\r | |
1502 | default: cardsize = '1';\r | |
1503 | }\r | |
1504 | cmdp++;\r | |
1505 | break;\r | |
bbd11876 | 1506 | case 'e':\r |
1507 | case 'E':\r | |
1508 | setEmulatorMem = true;\r | |
76ef5273 | 1509 | //implies x and i\r |
1510 | flags |= FLAG_INTERACTIVE;\r | |
1511 | flags |= FLAG_NR_AR_ATTACK;\r | |
bbd11876 | 1512 | cmdp++;\r |
1513 | break;\r | |
1514 | case 'f':\r | |
1515 | case 'F':\r | |
874572d4 | 1516 | len = param_getstr(Cmd, cmdp+1, filename, sizeof(filename));\r |
bbd11876 | 1517 | if (len < 1) {\r |
1518 | PrintAndLog("error no filename found");\r | |
1519 | return 0;\r | |
1520 | }\r | |
1521 | attackFromFile = true;\r | |
76ef5273 | 1522 | //implies x and i\r |
1523 | flags |= FLAG_INTERACTIVE;\r | |
1524 | flags |= FLAG_NR_AR_ATTACK;\r | |
1525 | cmdp += 2;\r | |
bbd11876 | 1526 | break;\r |
1527 | case 'h':\r | |
1528 | case 'H':\r | |
a8561e35 | 1529 | return usage_hf14_mfsim();\r |
bbd11876 | 1530 | case 'i':\r |
1531 | case 'I':\r | |
1532 | flags |= FLAG_INTERACTIVE;\r | |
1533 | cmdp++;\r | |
1534 | break;\r | |
1535 | case 'n':\r | |
1536 | case 'N':\r | |
a8561e35 | 1537 | exitAfterNReads = param_get8(Cmd, cmdp+1);\r |
bbd11876 | 1538 | cmdp += 2;\r |
1539 | break;\r | |
f9c1dcd9 MF |
1540 | case 'r':\r |
1541 | case 'R':\r | |
1542 | flags |= FLAG_RANDOM_NONCE;\r | |
1543 | cmdp++;\r | |
1544 | break;\r | |
bbd11876 | 1545 | case 'u':\r |
1546 | case 'U':\r | |
b8dd1ef6 | 1547 | uidlen = 14;\r |
1548 | if (param_gethex_ex(Cmd, cmdp+1, uid, &uidlen)) {\r | |
1549 | return usage_hf14_mfsim();\r | |
1550 | }\r | |
1551 | switch (uidlen) {\r | |
bbd11876 | 1552 | case 14: flags = FLAG_7B_UID_IN_DATA; break;\r |
1553 | case 8: flags = FLAG_4B_UID_IN_DATA; break;\r | |
a8561e35 | 1554 | default: return usage_hf14_mfsim();\r |
bbd11876 | 1555 | }\r |
76ef5273 | 1556 | cmdp += 2;\r |
bbd11876 | 1557 | break;\r |
1558 | case 'x':\r | |
1559 | case 'X':\r | |
1560 | flags |= FLAG_NR_AR_ATTACK;\r | |
1561 | cmdp++;\r | |
1562 | break;\r | |
1563 | default:\r | |
1564 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));\r | |
1565 | errors = true;\r | |
1566 | break;\r | |
d2f487af | 1567 | }\r |
bbd11876 | 1568 | if(errors) break;\r |
d2f487af | 1569 | }\r |
bbd11876 | 1570 | //Validations\r |
a8561e35 | 1571 | if(errors) return usage_hf14_mfsim();\r |
c872d8c1 | 1572 | \r |
bbd11876 | 1573 | //get uid from file\r |
1574 | if (attackFromFile) {\r | |
1575 | int count = 0;\r | |
1576 | // open file\r | |
1577 | f = fopen(filename, "r");\r | |
1578 | if (f == NULL) {\r | |
1579 | PrintAndLog("File %s not found or locked", filename);\r | |
1580 | return 1;\r | |
1581 | }\r | |
73ab92d1 | 1582 | PrintAndLog("Loading file and simulating. Press keyboard to abort");\r |
1583 | while(!feof(f) && !ukbhit()){\r | |
bbd11876 | 1584 | memset(buf, 0, sizeof(buf));\r |
91f4d531 | 1585 | memset(uid, 0, sizeof(uid));\r |
d2f487af | 1586 | \r |
7906cb41 | 1587 | if (fgets(buf, sizeof(buf), f) == NULL) {\r |
bbd11876 | 1588 | if (count > 0) break;\r |
7906cb41 | 1589 | \r |
bbd11876 | 1590 | PrintAndLog("File reading error.");\r |
1591 | fclose(f);\r | |
1592 | return 2;\r | |
1593 | }\r | |
91f4d531 | 1594 | if(!strlen(buf) && feof(f)) break;\r |
73ab92d1 | 1595 | \r |
91f4d531 | 1596 | uidlen = strlen(buf)-1;\r |
73ab92d1 | 1597 | switch(uidlen) {\r |
91f4d531 | 1598 | case 14: flags |= FLAG_7B_UID_IN_DATA; break;\r |
1599 | case 8: flags |= FLAG_4B_UID_IN_DATA; break;\r | |
7906cb41 | 1600 | default:\r |
91f4d531 | 1601 | PrintAndLog("uid in file wrong length at %d (length: %d) [%s]",count, uidlen, buf);\r |
73ab92d1 | 1602 | fclose(f);\r |
1603 | return 2;\r | |
bbd11876 | 1604 | }\r |
73ab92d1 | 1605 | \r |
bbd11876 | 1606 | for (uint8_t i = 0; i < uidlen; i += 2) {\r |
91f4d531 | 1607 | sscanf(&buf[i], "%02x", (unsigned int *)&uid[i / 2]);\r |
bbd11876 | 1608 | }\r |
7906cb41 | 1609 | \r |
a8561e35 | 1610 | PrintAndLog("mf sim cardsize: %s, uid: %s, numreads:%d, flags:%d (0x%02x) - press button to abort",\r |
1611 | cardsize == '0' ? "Mini" :\r | |
1612 | cardsize == '2' ? "2K" :\r | |
1613 | cardsize == '4' ? "4K" : "1K",\r | |
1614 | flags & FLAG_4B_UID_IN_DATA ? sprint_hex(uid,4):\r | |
a39af1cb | 1615 | flags & FLAG_7B_UID_IN_DATA ? sprint_hex(uid,7): "N/A",\r |
a8561e35 | 1616 | exitAfterNReads,\r |
1617 | flags,\r | |
1618 | flags);\r | |
bbd11876 | 1619 | \r |
a8561e35 | 1620 | UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {flags, exitAfterNReads, cardsize}};\r |
bbd11876 | 1621 | memcpy(c.d.asBytes, uid, sizeof(uid));\r |
1622 | clearCommandBuffer();\r | |
1623 | SendCommand(&c);\r | |
c872d8c1 | 1624 | \r |
a8561e35 | 1625 | while (! WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r |
73ab92d1 | 1626 | //We're waiting only 1.5 s at a time, otherwise we get the\r |
1627 | // annoying message about "Waiting for a response... "\r | |
1628 | }\r | |
1629 | //got a response\r | |
1630 | nonces_t ar_resp[ATTACK_KEY_COUNT*2];\r | |
1631 | memcpy(ar_resp, resp.d.asBytes, sizeof(ar_resp));\r | |
5b5489ba MF |
1632 | // We can skip the standard attack if we have RANDOM_NONCE set.\r |
1633 | readerAttack(ar_resp, setEmulatorMem, !(flags & FLAG_RANDOM_NONCE));\r | |
76ef5273 | 1634 | if ((bool)resp.arg[1]) {\r |
73ab92d1 | 1635 | PrintAndLog("Device button pressed - quitting");\r |
1636 | fclose(f);\r | |
1637 | return 4;\r | |
bbd11876 | 1638 | }\r |
bbd11876 | 1639 | count++;\r |
1640 | }\r | |
1641 | fclose(f);\r | |
d2f487af | 1642 | \r |
a8561e35 | 1643 | } else { //not from file\r |
d2f487af | 1644 | \r |
a8561e35 | 1645 | PrintAndLog("mf sim cardsize: %s, uid: %s, numreads:%d, flags:%d (0x%02x) ",\r |
1646 | cardsize == '0' ? "Mini" :\r | |
1647 | cardsize == '2' ? "2K" :\r | |
1648 | cardsize == '4' ? "4K" : "1K",\r | |
1649 | flags & FLAG_4B_UID_IN_DATA ? sprint_hex(uid,4):\r | |
a39af1cb | 1650 | flags & FLAG_7B_UID_IN_DATA ? sprint_hex(uid,7): "N/A",\r |
a8561e35 | 1651 | exitAfterNReads,\r |
1652 | flags,\r | |
1653 | flags);\r | |
1654 | \r | |
1655 | UsbCommand c = {CMD_SIMULATE_MIFARE_CARD, {flags, exitAfterNReads, cardsize}};\r | |
bbd11876 | 1656 | memcpy(c.d.asBytes, uid, sizeof(uid));\r |
1657 | clearCommandBuffer();\r | |
1658 | SendCommand(&c);\r | |
d2f487af | 1659 | \r |
bbd11876 | 1660 | if(flags & FLAG_INTERACTIVE) {\r |
1661 | PrintAndLog("Press pm3-button to abort simulation");\r | |
a8561e35 | 1662 | while(! WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {\r |
bbd11876 | 1663 | //We're waiting only 1.5 s at a time, otherwise we get the\r |
1664 | // annoying message about "Waiting for a response... "\r | |
79dcb9e0 | 1665 | }\r |
bbd11876 | 1666 | //got a response\r |
1667 | if (flags & FLAG_NR_AR_ATTACK) {\r | |
1668 | nonces_t ar_resp[ATTACK_KEY_COUNT*2];\r | |
1669 | memcpy(ar_resp, resp.d.asBytes, sizeof(ar_resp));\r | |
5b5489ba MF |
1670 | // We can skip the standard attack if we have RANDOM_NONCE set.\r |
1671 | readerAttack(ar_resp, setEmulatorMem, !(flags & FLAG_RANDOM_NONCE));\r | |
79dcb9e0 | 1672 | }\r |
79dcb9e0 | 1673 | }\r |
baeaf579 | 1674 | }\r |
bbd11876 | 1675 | \r |
baeaf579 | 1676 | return 0;\r |
9ca155ba M |
1677 | }\r |
1678 | \r | |
1679 | int CmdHF14AMfDbg(const char *Cmd)\r | |
1680 | {\r | |
8556b852 M |
1681 | int dbgMode = param_get32ex(Cmd, 0, 0, 10);\r |
1682 | if (dbgMode > 4) {\r | |
baeaf579 | 1683 | PrintAndLog("Max debug mode parameter is 4 \n");\r |
8556b852 M |
1684 | }\r |
1685 | \r | |
1686 | if (strlen(Cmd) < 1 || !param_getchar(Cmd, 0) || dbgMode > 4) {\r | |
9ca155ba M |
1687 | PrintAndLog("Usage: hf mf dbg <debug level>");\r |
1688 | PrintAndLog(" 0 - no debug messages");\r | |
1689 | PrintAndLog(" 1 - error messages");\r | |
b03c0f2d | 1690 | PrintAndLog(" 2 - plus information messages");\r |
1691 | PrintAndLog(" 3 - plus debug messages");\r | |
1692 | PrintAndLog(" 4 - print even debug messages in timing critical functions");\r | |
1693 | PrintAndLog(" Note: this option therefore may cause malfunction itself");\r | |
9ca155ba | 1694 | return 0;\r |
7906cb41 | 1695 | }\r |
9ca155ba | 1696 | \r |
8556b852 M |
1697 | UsbCommand c = {CMD_MIFARE_SET_DBGMODE, {dbgMode, 0, 0}};\r |
1698 | SendCommand(&c);\r | |
1699 | \r | |
9ca155ba M |
1700 | return 0;\r |
1701 | }\r | |
1702 | \r | |
1703 | int CmdHF14AMfEGet(const char *Cmd)\r | |
1704 | {\r | |
8556b852 | 1705 | uint8_t blockNo = 0;\r |
5ee70129 | 1706 | uint8_t data[16] = {0x00};\r |
8556b852 M |
1707 | \r |
1708 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
1709 | PrintAndLog("Usage: hf mf eget <block number>");\r | |
1710 | PrintAndLog(" sample: hf mf eget 0 ");\r | |
1711 | return 0;\r | |
7906cb41 F |
1712 | }\r |
1713 | \r | |
8556b852 | 1714 | blockNo = param_get8(Cmd, 0);\r |
8556b852 M |
1715 | \r |
1716 | PrintAndLog(" ");\r | |
baeaf579 | 1717 | if (!mfEmlGetMem(data, blockNo, 1)) {\r |
1718 | PrintAndLog("data[%3d]:%s", blockNo, sprint_hex(data, 16));\r | |
8556b852 M |
1719 | } else {\r |
1720 | PrintAndLog("Command execute timeout");\r | |
1721 | }\r | |
1722 | \r | |
1723 | return 0;\r | |
1724 | }\r | |
1725 | \r | |
1726 | int CmdHF14AMfEClear(const char *Cmd)\r | |
1727 | {\r | |
1728 | if (param_getchar(Cmd, 0) == 'h') {\r | |
1729 | PrintAndLog("Usage: hf mf eclr");\r | |
1730 | PrintAndLog("It set card emulator memory to empty data blocks and key A/B FFFFFFFFFFFF \n");\r | |
1731 | return 0;\r | |
7906cb41 | 1732 | }\r |
8556b852 M |
1733 | \r |
1734 | UsbCommand c = {CMD_MIFARE_EML_MEMCLR, {0, 0, 0}};\r | |
1735 | SendCommand(&c);\r | |
9ca155ba M |
1736 | return 0;\r |
1737 | }\r | |
1738 | \r | |
baeaf579 | 1739 | \r |
9ca155ba M |
1740 | int CmdHF14AMfESet(const char *Cmd)\r |
1741 | {\r | |
8556b852 M |
1742 | uint8_t memBlock[16];\r |
1743 | uint8_t blockNo = 0;\r | |
1744 | \r | |
1745 | memset(memBlock, 0x00, sizeof(memBlock));\r | |
1746 | \r | |
1747 | if (strlen(Cmd) < 3 || param_getchar(Cmd, 0) == 'h') {\r | |
1748 | PrintAndLog("Usage: hf mf eset <block number> <block data (32 hex symbols)>");\r | |
1749 | PrintAndLog(" sample: hf mf eset 1 000102030405060708090a0b0c0d0e0f ");\r | |
1750 | return 0;\r | |
7906cb41 F |
1751 | }\r |
1752 | \r | |
8556b852 | 1753 | blockNo = param_get8(Cmd, 0);\r |
7906cb41 | 1754 | \r |
8556b852 M |
1755 | if (param_gethex(Cmd, 1, memBlock, 32)) {\r |
1756 | PrintAndLog("block data must include 32 HEX symbols");\r | |
1757 | return 1;\r | |
1758 | }\r | |
7906cb41 | 1759 | \r |
8556b852 | 1760 | // 1 - blocks count\r |
36545f0a | 1761 | return mfEmlSetMem(memBlock, blockNo, 1);\r |
9ca155ba M |
1762 | }\r |
1763 | \r | |
baeaf579 | 1764 | \r |
9ca155ba M |
1765 | int CmdHF14AMfELoad(const char *Cmd)\r |
1766 | {\r | |
ab8b654e | 1767 | FILE * f;\r |
b915fda3 | 1768 | char filename[FILE_PATH_SIZE];\r |
baeaf579 | 1769 | char *fnameptr = filename;\r |
5ee70129 | 1770 | char buf[64] = {0x00};\r |
1771 | uint8_t buf8[64] = {0x00};\r | |
b915fda3 | 1772 | int i, len, blockNum, numBlocks;\r |
1773 | int nameParamNo = 1;\r | |
7906cb41 | 1774 | \r |
b915fda3 | 1775 | char ctmp = param_getchar(Cmd, 0);\r |
7906cb41 | 1776 | \r |
b915fda3 | 1777 | if ( ctmp == 'h' || ctmp == 0x00) {\r |
ab8b654e | 1778 | PrintAndLog("It loads emul dump from the file `filename.eml`");\r |
b915fda3 | 1779 | PrintAndLog("Usage: hf mf eload [card memory] <file name w/o `.eml`>");\r |
1780 | PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r | |
1781 | PrintAndLog("");\r | |
ab8b654e | 1782 | PrintAndLog(" sample: hf mf eload filename");\r |
b915fda3 | 1783 | PrintAndLog(" hf mf eload 4 filename");\r |
ab8b654e | 1784 | return 0;\r |
7906cb41 | 1785 | }\r |
ab8b654e | 1786 | \r |
b915fda3 | 1787 | switch (ctmp) {\r |
1788 | case '0' : numBlocks = 5*4; break;\r | |
7906cb41 | 1789 | case '1' :\r |
b915fda3 | 1790 | case '\0': numBlocks = 16*4; break;\r |
1791 | case '2' : numBlocks = 32*4; break;\r | |
1792 | case '4' : numBlocks = 256; break;\r | |
1793 | default: {\r | |
1794 | numBlocks = 16*4;\r | |
1795 | nameParamNo = 0;\r | |
1796 | }\r | |
1797 | }\r | |
1798 | \r | |
a8561e35 | 1799 | len = param_getstr(Cmd, nameParamNo, filename, sizeof(filename));\r |
7906cb41 | 1800 | \r |
4c16ae80 | 1801 | if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r |
ab8b654e | 1802 | \r |
0b14440d | 1803 | fnameptr += len;\r |
ab8b654e | 1804 | \r |
7906cb41 F |
1805 | sprintf(fnameptr, ".eml");\r |
1806 | \r | |
ab8b654e M |
1807 | // open file\r |
1808 | f = fopen(filename, "r");\r | |
1809 | if (f == NULL) {\r | |
3fe4ff4f | 1810 | PrintAndLog("File %s not found or locked", filename);\r |
ab8b654e M |
1811 | return 1;\r |
1812 | }\r | |
7906cb41 | 1813 | \r |
ab8b654e M |
1814 | blockNum = 0;\r |
1815 | while(!feof(f)){\r | |
1816 | memset(buf, 0, sizeof(buf));\r | |
7906cb41 | 1817 | \r |
759c16b3 | 1818 | if (fgets(buf, sizeof(buf), f) == NULL) {\r |
7906cb41 | 1819 | \r |
b915fda3 | 1820 | if (blockNum >= numBlocks) break;\r |
7906cb41 | 1821 | \r |
d2f487af | 1822 | PrintAndLog("File reading error.");\r |
97d582a6 | 1823 | fclose(f);\r |
759c16b3 | 1824 | return 2;\r |
baeaf579 | 1825 | }\r |
7906cb41 | 1826 | \r |
ab8b654e | 1827 | if (strlen(buf) < 32){\r |
aea4d766 | 1828 | if(strlen(buf) && feof(f))\r |
1829 | break;\r | |
ab8b654e | 1830 | PrintAndLog("File content error. Block data must include 32 HEX symbols");\r |
97d582a6 | 1831 | fclose(f);\r |
ab8b654e M |
1832 | return 2;\r |
1833 | }\r | |
7906cb41 | 1834 | \r |
baeaf579 | 1835 | for (i = 0; i < 32; i += 2) {\r |
1836 | sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r | |
baeaf579 | 1837 | }\r |
7906cb41 | 1838 | \r |
ab8b654e | 1839 | if (mfEmlSetMem(buf8, blockNum, 1)) {\r |
baeaf579 | 1840 | PrintAndLog("Cant set emul block: %3d", blockNum);\r |
97d582a6 | 1841 | fclose(f);\r |
ab8b654e M |
1842 | return 3;\r |
1843 | }\r | |
5ee70129 | 1844 | printf(".");\r |
ab8b654e | 1845 | blockNum++;\r |
7906cb41 | 1846 | \r |
b915fda3 | 1847 | if (blockNum >= numBlocks) break;\r |
ab8b654e M |
1848 | }\r |
1849 | fclose(f);\r | |
5ee70129 | 1850 | printf("\n");\r |
7906cb41 | 1851 | \r |
b915fda3 | 1852 | if ((blockNum != numBlocks)) {\r |
1853 | PrintAndLog("File content error. Got %d must be %d blocks.",blockNum, numBlocks);\r | |
ab8b654e M |
1854 | return 4;\r |
1855 | }\r | |
d2f487af | 1856 | PrintAndLog("Loaded %d blocks from file: %s", blockNum, filename);\r |
baeaf579 | 1857 | return 0;\r |
9ca155ba M |
1858 | }\r |
1859 | \r | |
baeaf579 | 1860 | \r |
9ca155ba M |
1861 | int CmdHF14AMfESave(const char *Cmd)\r |
1862 | {\r | |
ab8b654e | 1863 | FILE * f;\r |
b915fda3 | 1864 | char filename[FILE_PATH_SIZE];\r |
ab8b654e M |
1865 | char * fnameptr = filename;\r |
1866 | uint8_t buf[64];\r | |
b915fda3 | 1867 | int i, j, len, numBlocks;\r |
1868 | int nameParamNo = 1;\r | |
7906cb41 | 1869 | \r |
ab8b654e M |
1870 | memset(filename, 0, sizeof(filename));\r |
1871 | memset(buf, 0, sizeof(buf));\r | |
1872 | \r | |
b915fda3 | 1873 | char ctmp = param_getchar(Cmd, 0);\r |
7906cb41 | 1874 | \r |
b915fda3 | 1875 | if ( ctmp == 'h' || ctmp == 'H') {\r |
ab8b654e | 1876 | PrintAndLog("It saves emul dump into the file `filename.eml` or `cardID.eml`");\r |
b915fda3 | 1877 | PrintAndLog(" Usage: hf mf esave [card memory] [file name w/o `.eml`]");\r |
1878 | PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r | |
1879 | PrintAndLog("");\r | |
ab8b654e | 1880 | PrintAndLog(" sample: hf mf esave ");\r |
b915fda3 | 1881 | PrintAndLog(" hf mf esave 4");\r |
1882 | PrintAndLog(" hf mf esave 4 filename");\r | |
ab8b654e | 1883 | return 0;\r |
7906cb41 | 1884 | }\r |
ab8b654e | 1885 | \r |
b915fda3 | 1886 | switch (ctmp) {\r |
1887 | case '0' : numBlocks = 5*4; break;\r | |
7906cb41 | 1888 | case '1' :\r |
b915fda3 | 1889 | case '\0': numBlocks = 16*4; break;\r |
1890 | case '2' : numBlocks = 32*4; break;\r | |
1891 | case '4' : numBlocks = 256; break;\r | |
1892 | default: {\r | |
1893 | numBlocks = 16*4;\r | |
1894 | nameParamNo = 0;\r | |
1895 | }\r | |
1896 | }\r | |
1897 | \r | |
874572d4 | 1898 | len = param_getstr(Cmd,nameParamNo,filename,sizeof(filename));\r |
7906cb41 | 1899 | \r |
4c16ae80 | 1900 | if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r |
7906cb41 | 1901 | \r |
b915fda3 | 1902 | // user supplied filename?\r |
ab8b654e | 1903 | if (len < 1) {\r |
b915fda3 | 1904 | // get filename (UID from memory)\r |
ab8b654e | 1905 | if (mfEmlGetMem(buf, 0, 1)) {\r |
b915fda3 | 1906 | PrintAndLog("Can\'t get UID from block: %d", 0);\r |
0b14440d PL |
1907 | len = sprintf(fnameptr, "dump");\r |
1908 | fnameptr += len;\r | |
1909 | }\r | |
1910 | else {\r | |
1911 | for (j = 0; j < 7; j++, fnameptr += 2)\r | |
1912 | sprintf(fnameptr, "%02X", buf[j]);\r | |
ab8b654e | 1913 | }\r |
ab8b654e | 1914 | } else {\r |
0b14440d | 1915 | fnameptr += len;\r |
ab8b654e M |
1916 | }\r |
1917 | \r | |
b915fda3 | 1918 | // add file extension\r |
7906cb41 F |
1919 | sprintf(fnameptr, ".eml");\r |
1920 | \r | |
ab8b654e M |
1921 | // open file\r |
1922 | f = fopen(filename, "w+");\r | |
1923 | \r | |
b915fda3 | 1924 | if ( !f ) {\r |
1925 | PrintAndLog("Can't open file %s ", filename);\r | |
1926 | return 1;\r | |
1927 | }\r | |
7906cb41 | 1928 | \r |
ab8b654e | 1929 | // put hex\r |
b915fda3 | 1930 | for (i = 0; i < numBlocks; i++) {\r |
ab8b654e M |
1931 | if (mfEmlGetMem(buf, i, 1)) {\r |
1932 | PrintAndLog("Cant get block: %d", i);\r | |
1933 | break;\r | |
1934 | }\r | |
1935 | for (j = 0; j < 16; j++)\r | |
7906cb41 | 1936 | fprintf(f, "%02X", buf[j]);\r |
ab8b654e M |
1937 | fprintf(f,"\n");\r |
1938 | }\r | |
1939 | fclose(f);\r | |
7906cb41 | 1940 | \r |
b915fda3 | 1941 | PrintAndLog("Saved %d blocks to file: %s", numBlocks, filename);\r |
7906cb41 | 1942 | \r |
9ca155ba M |
1943 | return 0;\r |
1944 | }\r | |
1945 | \r | |
baeaf579 | 1946 | \r |
26fdb4ab | 1947 | int CmdHF14AMfECFill(const char *Cmd)\r |
1948 | {\r | |
8556b852 | 1949 | uint8_t keyType = 0;\r |
baeaf579 | 1950 | uint8_t numSectors = 16;\r |
7906cb41 | 1951 | \r |
8556b852 | 1952 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r |
baeaf579 | 1953 | PrintAndLog("Usage: hf mf ecfill <key A/B> [card memory]");\r |
1954 | PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r | |
1955 | PrintAndLog("");\r | |
1956 | PrintAndLog("samples: hf mf ecfill A");\r | |
1957 | PrintAndLog(" hf mf ecfill A 4");\r | |
1958 | PrintAndLog("Read card and transfer its data to emulator memory.");\r | |
1959 | PrintAndLog("Keys must be laid in the emulator memory. \n");\r | |
8556b852 | 1960 | return 0;\r |
7906cb41 | 1961 | }\r |
8556b852 M |
1962 | \r |
1963 | char ctmp = param_getchar(Cmd, 0);\r | |
baeaf579 | 1964 | if (ctmp != 'a' && ctmp != 'A' && ctmp != 'b' && ctmp != 'B') {\r |
8556b852 M |
1965 | PrintAndLog("Key type must be A or B");\r |
1966 | return 1;\r | |
1967 | }\r | |
1968 | if (ctmp != 'A' && ctmp != 'a') keyType = 1;\r | |
1969 | \r | |
baeaf579 | 1970 | ctmp = param_getchar(Cmd, 1);\r |
1971 | switch (ctmp) {\r | |
1972 | case '0' : numSectors = 5; break;\r | |
7906cb41 | 1973 | case '1' :\r |
baeaf579 | 1974 | case '\0': numSectors = 16; break;\r |
1975 | case '2' : numSectors = 32; break;\r | |
1976 | case '4' : numSectors = 40; break;\r | |
1977 | default: numSectors = 16;\r | |
7906cb41 | 1978 | }\r |
baeaf579 | 1979 | \r |
4e002980 | 1980 | printf("--params: numSectors: %d, keyType:%d\n", numSectors, keyType);\r |
baeaf579 | 1981 | UsbCommand c = {CMD_MIFARE_EML_CARDLOAD, {numSectors, keyType, 0}};\r |
1982 | SendCommand(&c);\r | |
1983 | return 0;\r | |
8556b852 M |
1984 | }\r |
1985 | \r | |
a39af1cb | 1986 | \r |
26fdb4ab | 1987 | int CmdHF14AMfEKeyPrn(const char *Cmd)\r |
1988 | {\r | |
baeaf579 | 1989 | int i;\r |
a39af1cb | 1990 | uint8_t numSectors = 16;\r |
8556b852 M |
1991 | uint8_t data[16];\r |
1992 | uint64_t keyA, keyB;\r | |
a39af1cb | 1993 | bool createDumpFile = false;\r |
7906cb41 | 1994 | \r |
b915fda3 | 1995 | if (param_getchar(Cmd, 0) == 'h') {\r |
1996 | PrintAndLog("It prints the keys loaded in the emulator memory");\r | |
a39af1cb | 1997 | PrintAndLog("Usage: hf mf ekeyprn [card memory] [d]");\r |
b915fda3 | 1998 | PrintAndLog(" [card memory]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r |
a39af1cb | 1999 | PrintAndLog(" [d] : write keys to binary file dumpkeys.bin");\r |
b915fda3 | 2000 | PrintAndLog("");\r |
2001 | PrintAndLog(" sample: hf mf ekeyprn 1");\r | |
2002 | return 0;\r | |
7906cb41 | 2003 | }\r |
b915fda3 | 2004 | \r |
a39af1cb | 2005 | uint8_t cmdp = 0;\r |
2006 | while (param_getchar(Cmd, cmdp) != 0x00) {\r | |
2007 | switch (param_getchar(Cmd, cmdp)) {\r | |
2008 | case '0' : numSectors = 5; break;\r | |
2009 | case '1' :\r | |
2010 | case '\0': numSectors = 16; break;\r | |
2011 | case '2' : numSectors = 32; break;\r | |
2012 | case '4' : numSectors = 40; break;\r | |
2013 | case 'd' : \r | |
2014 | case 'D' : createDumpFile = true; break;\r | |
2015 | }\r | |
2016 | cmdp++;\r | |
7906cb41 F |
2017 | }\r |
2018 | \r | |
8556b852 M |
2019 | PrintAndLog("|---|----------------|----------------|");\r |
2020 | PrintAndLog("|sec|key A |key B |");\r | |
2021 | PrintAndLog("|---|----------------|----------------|");\r | |
b915fda3 | 2022 | for (i = 0; i < numSectors; i++) {\r |
baeaf579 | 2023 | if (mfEmlGetMem(data, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1)) {\r |
2024 | PrintAndLog("error get block %d", FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);\r | |
8556b852 M |
2025 | break;\r |
2026 | }\r | |
2027 | keyA = bytes_to_num(data, 6);\r | |
2028 | keyB = bytes_to_num(data + 10, 6);\r | |
43534cba | 2029 | PrintAndLog("|%03d| %012" PRIx64 " | %012" PRIx64 " |", i, keyA, keyB);\r |
8556b852 M |
2030 | }\r |
2031 | PrintAndLog("|---|----------------|----------------|");\r | |
7906cb41 | 2032 | \r |
a39af1cb | 2033 | // Create dump file\r |
2034 | if (createDumpFile) {\r | |
2035 | FILE *fkeys;\r | |
2036 | if ((fkeys = fopen("dumpkeys.bin","wb")) == NULL) {\r | |
2037 | PrintAndLog("Could not create file dumpkeys.bin");\r | |
2038 | return 1;\r | |
2039 | }\r | |
2040 | PrintAndLog("Printing keys to binary file dumpkeys.bin...");\r | |
2041 | for(i = 0; i < numSectors; i++) {\r | |
2042 | if (mfEmlGetMem(data, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1)) {\r | |
2043 | PrintAndLog("error get block %d", FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);\r | |
2044 | break;\r | |
2045 | }\r | |
2046 | fwrite(data+6, 1, 6, fkeys);\r | |
2047 | }\r | |
2048 | for(i = 0; i < numSectors; i++) {\r | |
2049 | if (mfEmlGetMem(data, FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1, 1)) {\r | |
2050 | PrintAndLog("error get block %d", FirstBlockOfSector(i) + NumBlocksPerSector(i) - 1);\r | |
2051 | break;\r | |
2052 | }\r | |
2053 | fwrite(data+10, 1, 6, fkeys);\r | |
2054 | }\r | |
2055 | fclose(fkeys);\r | |
2056 | }\r | |
2057 | \r | |
8556b852 M |
2058 | return 0;\r |
2059 | }\r | |
2060 | \r | |
a39af1cb | 2061 | \r |
0675f200 M |
2062 | int CmdHF14AMfCSetUID(const char *Cmd)\r |
2063 | {\r | |
3fe4ff4f | 2064 | uint8_t uid[8] = {0x00};\r |
2065 | uint8_t oldUid[8] = {0x00};\r | |
3bba7dea JH |
2066 | uint8_t atqa[2] = {0x00};\r |
2067 | uint8_t sak[1] = {0x00};\r | |
3a05a1e7 | 2068 | uint8_t atqaPresent = 0;\r |
0675f200 | 2069 | int res;\r |
3bba7dea | 2070 | \r |
3a05a1e7 OM |
2071 | uint8_t needHelp = 0;\r |
2072 | char cmdp = 1;\r | |
a39af1cb | 2073 | \r |
3a05a1e7 OM |
2074 | if (param_getchar(Cmd, 0) && param_gethex(Cmd, 0, uid, 8)) {\r |
2075 | PrintAndLog("UID must include 8 HEX symbols");\r | |
2076 | return 1;\r | |
2077 | }\r | |
2078 | \r | |
2079 | if (param_getlength(Cmd, 1) > 1 && param_getlength(Cmd, 2) > 1) {\r | |
2080 | atqaPresent = 1;\r | |
2081 | cmdp = 3;\r | |
a39af1cb | 2082 | \r |
3a05a1e7 OM |
2083 | if (param_gethex(Cmd, 1, atqa, 4)) {\r |
2084 | PrintAndLog("ATQA must include 4 HEX symbols");\r | |
2085 | return 1;\r | |
2086 | }\r | |
a39af1cb | 2087 | \r |
3a05a1e7 OM |
2088 | if (param_gethex(Cmd, 2, sak, 2)) {\r |
2089 | PrintAndLog("SAK must include 2 HEX symbols");\r | |
2090 | return 1;\r | |
2091 | }\r | |
2092 | }\r | |
2093 | \r | |
2094 | while(param_getchar(Cmd, cmdp) != 0x00)\r | |
2095 | {\r | |
2096 | switch(param_getchar(Cmd, cmdp))\r | |
2097 | {\r | |
2098 | case 'h':\r | |
2099 | case 'H':\r | |
2100 | needHelp = 1;\r | |
2101 | break;\r | |
2102 | default:\r | |
2103 | PrintAndLog("ERROR: Unknown parameter '%c'", param_getchar(Cmd, cmdp));\r | |
2104 | needHelp = 1;\r | |
2105 | break;\r | |
2106 | }\r | |
2107 | cmdp++;\r | |
2108 | }\r | |
2109 | \r | |
2110 | if (strlen(Cmd) < 1 || needHelp) {\r | |
2111 | PrintAndLog("");\r | |
2112 | PrintAndLog("Usage: hf mf csetuid <UID 8 hex symbols> [ATQA 4 hex symbols SAK 2 hex symbols]");\r | |
3bba7dea | 2113 | PrintAndLog("sample: hf mf csetuid 01020304");\r |
3a05a1e7 | 2114 | PrintAndLog("sample: hf mf csetuid 01020304 0004 08");\r |
3bba7dea | 2115 | PrintAndLog("Set UID, ATQA, and SAK for magic Chinese card (only works with such cards)");\r |
0675f200 | 2116 | return 0;\r |
3bba7dea | 2117 | }\r |
0675f200 | 2118 | \r |
3a05a1e7 OM |
2119 | PrintAndLog("uid:%s", sprint_hex(uid, 4));\r |
2120 | if (atqaPresent) {\r | |
2121 | PrintAndLog("--atqa:%s sak:%02x", sprint_hex(atqa, 2), sak[0]);\r | |
0675f200 | 2122 | }\r |
3bba7dea | 2123 | \r |
3a05a1e7 OM |
2124 | res = mfCSetUID(uid, (atqaPresent)?atqa:NULL, (atqaPresent)?sak:NULL, oldUid);\r |
2125 | if (res) {\r | |
2126 | PrintAndLog("Can't set UID. Error=%d", res);\r | |
2127 | return 1;\r | |
2128 | }\r | |
2129 | \r | |
2130 | PrintAndLog("old UID:%s", sprint_hex(oldUid, 4));\r | |
2131 | PrintAndLog("new UID:%s", sprint_hex(uid, 4));\r | |
2132 | return 0;\r | |
2133 | }\r | |
2134 | \r | |
3a05a1e7 OM |
2135 | int CmdHF14AMfCWipe(const char *Cmd)\r |
2136 | {\r | |
2137 | int res, gen = 0;\r | |
2138 | int numBlocks = 16 * 4;\r | |
2139 | bool wipeCard = false;\r | |
2140 | bool fillCard = false;\r | |
a39af1cb | 2141 | \r |
3a05a1e7 | 2142 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r |
6148817a | 2143 | PrintAndLog("Usage: hf mf cwipe [card size] [w] [f]");\r |
2144 | PrintAndLog("sample: hf mf cwipe 1 w f");\r | |
3a05a1e7 OM |
2145 | PrintAndLog("[card size]: 0 = 320 bytes (Mifare Mini), 1 = 1K (default), 2 = 2K, 4 = 4K");\r |
2146 | PrintAndLog("w - Wipe magic Chinese card (only works with gen:1a cards)");\r | |
2147 | PrintAndLog("f - Fill the card with default data and keys (works with gen:1a and gen:1b cards only)");\r | |
2148 | return 0;\r | |
3bba7dea JH |
2149 | }\r |
2150 | \r | |
3a05a1e7 | 2151 | gen = mfCIdentify();\r |
a39af1cb | 2152 | if ((gen != 1) && (gen != 2))\r |
3a05a1e7 | 2153 | return 1;\r |
a39af1cb | 2154 | \r |
adf023ff | 2155 | numBlocks = ParamCardSizeBlocks(param_getchar(Cmd, 0));\r |
3a05a1e7 OM |
2156 | \r |
2157 | char cmdp = 0;\r | |
2158 | while(param_getchar(Cmd, cmdp) != 0x00){\r | |
2159 | switch(param_getchar(Cmd, cmdp)) {\r | |
2160 | case 'w':\r | |
2161 | case 'W':\r | |
3bba7dea | 2162 | wipeCard = 1;\r |
3a05a1e7 OM |
2163 | break;\r |
2164 | case 'f':\r | |
2165 | case 'F':\r | |
2166 | fillCard = 1;\r | |
2167 | break;\r | |
2168 | default:\r | |
2169 | break;\r | |
3bba7dea | 2170 | }\r |
3a05a1e7 | 2171 | cmdp++;\r |
3bba7dea | 2172 | }\r |
0675f200 | 2173 | \r |
a39af1cb | 2174 | if (!wipeCard && !fillCard)\r |
2ce43a28 | 2175 | wipeCard = true;\r |
0675f200 | 2176 | \r |
3a05a1e7 OM |
2177 | PrintAndLog("--blocks count:%2d wipe:%c fill:%c", numBlocks, (wipeCard)?'y':'n', (fillCard)?'y':'n');\r |
2178 | \r | |
2179 | if (gen == 2) {\r | |
2180 | /* generation 1b magic card */\r | |
2181 | if (wipeCard) {\r | |
2182 | PrintAndLog("WARNING: can't wipe magic card 1b generation");\r | |
0675f200 | 2183 | }\r |
a39af1cb | 2184 | res = mfCWipe(numBlocks, true, false, fillCard);\r |
3a05a1e7 OM |
2185 | } else {\r |
2186 | /* generation 1a magic card by default */\r | |
a39af1cb | 2187 | res = mfCWipe(numBlocks, false, wipeCard, fillCard);\r |
3a05a1e7 | 2188 | }\r |
7906cb41 | 2189 | \r |
3a05a1e7 OM |
2190 | if (res) {\r |
2191 | PrintAndLog("Can't wipe. error=%d", res);\r | |
2192 | return 1;\r | |
2193 | }\r | |
2194 | PrintAndLog("OK");\r | |
0675f200 M |
2195 | return 0;\r |
2196 | }\r | |
2197 | \r | |
2198 | int CmdHF14AMfCSetBlk(const char *Cmd)\r | |
2199 | {\r | |
5ee70129 | 2200 | uint8_t memBlock[16] = {0x00};\r |
f774db95 | 2201 | uint8_t blockNo = 0;\r |
7cb8516c | 2202 | bool wipeCard = false;\r |
7906cb41 | 2203 | int res, gen = 0;\r |
f774db95 M |
2204 | \r |
2205 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
664f6586 | 2206 | PrintAndLog("Usage: hf mf csetblk <block number> <block data (32 hex symbols)> [w]");\r |
f774db95 | 2207 | PrintAndLog("sample: hf mf csetblk 1 01020304050607080910111213141516");\r |
664f6586 | 2208 | PrintAndLog("Set block data for magic Chinese card (only works with such cards)");\r |
2209 | PrintAndLog("If you also want wipe the card then add 'w' at the end of the command line");\r | |
f774db95 | 2210 | return 0;\r |
7906cb41 F |
2211 | }\r |
2212 | \r | |
2213 | gen = mfCIdentify();\r | |
a39af1cb | 2214 | if ((gen != 1) && (gen != 2))\r |
3a05a1e7 | 2215 | return 1;\r |
f774db95 M |
2216 | \r |
2217 | blockNo = param_get8(Cmd, 0);\r | |
f774db95 M |
2218 | \r |
2219 | if (param_gethex(Cmd, 1, memBlock, 32)) {\r | |
2220 | PrintAndLog("block data must include 32 HEX symbols");\r | |
2221 | return 1;\r | |
2222 | }\r | |
2223 | \r | |
664f6586 | 2224 | char ctmp = param_getchar(Cmd, 2);\r |
2225 | wipeCard = (ctmp == 'w' || ctmp == 'W');\r | |
baeaf579 | 2226 | PrintAndLog("--block number:%2d data:%s", blockNo, sprint_hex(memBlock, 16));\r |
f774db95 | 2227 | \r |
7906cb41 F |
2228 | if (gen == 2) {\r |
2229 | /* generation 1b magic card */\r | |
2230 | res = mfCSetBlock(blockNo, memBlock, NULL, wipeCard, CSETBLOCK_SINGLE_OPER | CSETBLOCK_MAGIC_1B);\r | |
2231 | } else {\r | |
2232 | /* generation 1a magic card by default */\r | |
2233 | res = mfCSetBlock(blockNo, memBlock, NULL, wipeCard, CSETBLOCK_SINGLE_OPER);\r | |
2234 | }\r | |
2235 | \r | |
f774db95 | 2236 | if (res) {\r |
664f6586 | 2237 | PrintAndLog("Can't write block. error=%d", res);\r |
2238 | return 1;\r | |
2239 | }\r | |
0675f200 M |
2240 | return 0;\r |
2241 | }\r | |
2242 | \r | |
baeaf579 | 2243 | \r |
0675f200 M |
2244 | int CmdHF14AMfCLoad(const char *Cmd)\r |
2245 | {\r | |
208a0166 | 2246 | FILE * f;\r |
b915fda3 | 2247 | char filename[FILE_PATH_SIZE] = {0x00};\r |
208a0166 | 2248 | char * fnameptr = filename;\r |
7906cb41 F |
2249 | char buf[256] = {0x00};\r |
2250 | uint8_t buf8[256] = {0x00};\r | |
208a0166 | 2251 | uint8_t fillFromEmulator = 0;\r |
7906cb41 F |
2252 | int i, len, blockNum, flags = 0, gen = 0, numblock = 64;\r |
2253 | \r | |
208a0166 | 2254 | if (param_getchar(Cmd, 0) == 'h' || param_getchar(Cmd, 0)== 0x00) {\r |
e3c23565 | 2255 | PrintAndLog("It loads magic Chinese card from the file `filename.eml`");\r |
7906cb41 F |
2256 | PrintAndLog("or from emulator memory (option `e`). 4K card: (option `4`)");\r |
2257 | PrintAndLog("Usage: hf mf cload [file name w/o `.eml`][e][4]");\r | |
2258 | PrintAndLog(" or: hf mf cload e [4]");\r | |
2259 | PrintAndLog("Sample: hf mf cload filename");\r | |
2260 | PrintAndLog(" hf mf cload filname 4");\r | |
2261 | PrintAndLog(" hf mf cload e");\r | |
2262 | PrintAndLog(" hf mf cload e 4");\r | |
208a0166 | 2263 | return 0;\r |
7906cb41 | 2264 | }\r |
208a0166 M |
2265 | \r |
2266 | char ctmp = param_getchar(Cmd, 0);\r | |
2267 | if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r | |
7906cb41 F |
2268 | ctmp = param_getchar(Cmd, 1);\r |
2269 | if (ctmp == '4') numblock = 256;\r | |
2270 | \r | |
2271 | gen = mfCIdentify();\r | |
2272 | PrintAndLog("Loading magic mifare %dK", numblock == 256 ? 4:1);\r | |
2273 | \r | |
208a0166 | 2274 | if (fillFromEmulator) {\r |
7906cb41 | 2275 | for (blockNum = 0; blockNum < numblock; blockNum += 1) {\r |
208a0166 M |
2276 | if (mfEmlGetMem(buf8, blockNum, 1)) {\r |
2277 | PrintAndLog("Cant get block: %d", blockNum);\r | |
2278 | return 2;\r | |
2279 | }\r | |
a39af1cb | 2280 | if (blockNum == 0) flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC; // switch on field and send magic sequence\r |
2281 | if (blockNum == 1) flags = 0; // just write\r | |
2282 | if (blockNum == numblock - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD; // Done. Magic Halt and switch off field.\r | |
208a0166 | 2283 | \r |
7906cb41 F |
2284 | if (gen == 2)\r |
2285 | /* generation 1b magic card */\r | |
2286 | flags |= CSETBLOCK_MAGIC_1B;\r | |
208a0166 M |
2287 | if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r |
2288 | PrintAndLog("Cant set magic card block: %d", blockNum);\r | |
2289 | return 3;\r | |
2290 | }\r | |
2291 | }\r | |
2292 | return 0;\r | |
2293 | } else {\r | |
874572d4 | 2294 | param_getstr(Cmd, 0, filename, sizeof(filename));\r |
7906cb41 F |
2295 | \r |
2296 | len = strlen(filename);\r | |
4c16ae80 | 2297 | if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r |
208a0166 | 2298 | \r |
7906cb41 | 2299 | //memcpy(filename, Cmd, len);\r |
292fe725 | 2300 | fnameptr += len;\r |
208a0166 | 2301 | \r |
7906cb41 F |
2302 | sprintf(fnameptr, ".eml");\r |
2303 | \r | |
208a0166 M |
2304 | // open file\r |
2305 | f = fopen(filename, "r");\r | |
2306 | if (f == NULL) {\r | |
2307 | PrintAndLog("File not found or locked.");\r | |
2308 | return 1;\r | |
2309 | }\r | |
7906cb41 | 2310 | \r |
208a0166 | 2311 | blockNum = 0;\r |
208a0166 | 2312 | while(!feof(f)){\r |
7906cb41 | 2313 | \r |
208a0166 | 2314 | memset(buf, 0, sizeof(buf));\r |
7906cb41 | 2315 | \r |
759c16b3 | 2316 | if (fgets(buf, sizeof(buf), f) == NULL) {\r |
e6432f05 | 2317 | fclose(f);\r |
baeaf579 | 2318 | PrintAndLog("File reading error.");\r |
2319 | return 2;\r | |
2320 | }\r | |
208a0166 | 2321 | \r |
16a95d76 | 2322 | if (strlen(buf) < 32) {\r |
208a0166 M |
2323 | if(strlen(buf) && feof(f))\r |
2324 | break;\r | |
2325 | PrintAndLog("File content error. Block data must include 32 HEX symbols");\r | |
e6432f05 | 2326 | fclose(f);\r |
208a0166 M |
2327 | return 2;\r |
2328 | }\r | |
2329 | for (i = 0; i < 32; i += 2)\r | |
2330 | sscanf(&buf[i], "%02x", (unsigned int *)&buf8[i / 2]);\r | |
2331 | \r | |
a39af1cb | 2332 | if (blockNum == 0) flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC; // switch on field and send magic sequence\r |
2333 | if (blockNum == 1) flags = 0; // just write\r | |
2334 | if (blockNum == numblock - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD; // Done. Switch off field.\r | |
208a0166 | 2335 | \r |
7906cb41 F |
2336 | if (gen == 2)\r |
2337 | /* generation 1b magic card */\r | |
2338 | flags |= CSETBLOCK_MAGIC_1B;\r | |
208a0166 | 2339 | if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r |
baeaf579 | 2340 | PrintAndLog("Can't set magic card block: %d", blockNum);\r |
4c16ae80 | 2341 | fclose(f);\r |
208a0166 M |
2342 | return 3;\r |
2343 | }\r | |
2344 | blockNum++;\r | |
7906cb41 F |
2345 | \r |
2346 | if (blockNum >= numblock) break; // magic card type - mifare 1K 64 blocks, mifare 4k 256 blocks\r | |
208a0166 M |
2347 | }\r |
2348 | fclose(f);\r | |
7906cb41 F |
2349 | \r |
2350 | //if (blockNum != 16 * 4 && blockNum != 32 * 4 + 8 * 16){\r | |
2351 | if (blockNum != numblock){\r | |
2352 | PrintAndLog("File content error. There must be %d blocks", numblock);\r | |
208a0166 M |
2353 | return 4;\r |
2354 | }\r | |
2355 | PrintAndLog("Loaded from file: %s", filename);\r | |
2356 | return 0;\r | |
2357 | }\r | |
e3c23565 | 2358 | return 0;\r |
0675f200 M |
2359 | }\r |
2360 | \r | |
545a1f38 M |
2361 | int CmdHF14AMfCGetBlk(const char *Cmd) {\r |
2362 | uint8_t memBlock[16];\r | |
2363 | uint8_t blockNo = 0;\r | |
7906cb41 | 2364 | int res, gen = 0;\r |
545a1f38 M |
2365 | memset(memBlock, 0x00, sizeof(memBlock));\r |
2366 | \r | |
2367 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
2368 | PrintAndLog("Usage: hf mf cgetblk <block number>");\r | |
2369 | PrintAndLog("sample: hf mf cgetblk 1");\r | |
664f6586 | 2370 | PrintAndLog("Get block data from magic Chinese card (only works with such cards)\n");\r |
545a1f38 | 2371 | return 0;\r |
7906cb41 F |
2372 | }\r |
2373 | \r | |
2374 | gen = mfCIdentify();\r | |
545a1f38 M |
2375 | \r |
2376 | blockNo = param_get8(Cmd, 0);\r | |
545a1f38 | 2377 | \r |
baeaf579 | 2378 | PrintAndLog("--block number:%2d ", blockNo);\r |
545a1f38 | 2379 | \r |
7906cb41 F |
2380 | if (gen == 2) {\r |
2381 | /* generation 1b magic card */\r | |
2382 | res = mfCGetBlock(blockNo, memBlock, CSETBLOCK_SINGLE_OPER | CSETBLOCK_MAGIC_1B);\r | |
2383 | } else {\r | |
2384 | /* generation 1a magic card by default */\r | |
2385 | res = mfCGetBlock(blockNo, memBlock, CSETBLOCK_SINGLE_OPER);\r | |
2386 | }\r | |
545a1f38 M |
2387 | if (res) {\r |
2388 | PrintAndLog("Can't read block. error=%d", res);\r | |
2389 | return 1;\r | |
2390 | }\r | |
7906cb41 | 2391 | \r |
545a1f38 | 2392 | PrintAndLog("block data:%s", sprint_hex(memBlock, 16));\r |
a39af1cb | 2393 | \r |
ac4ecfe3 OM |
2394 | if (mfIsSectorTrailer(blockNo)) {\r |
2395 | PrintAndLogEx(NORMAL, "Trailer decoded:");\r | |
2396 | PrintAndLogEx(NORMAL, "Key A: %s", sprint_hex_inrow(memBlock, 6));\r | |
2397 | PrintAndLogEx(NORMAL, "Key B: %s", sprint_hex_inrow(&memBlock[10], 6));\r | |
2398 | int bln = mfFirstBlockOfSector(mfSectorNum(blockNo));\r | |
2399 | int blinc = (mfNumBlocksPerSector(mfSectorNum(blockNo)) > 4) ? 5 : 1;\r | |
2400 | for (int i = 0; i < 4; i++) {\r | |
2401 | PrintAndLogEx(NORMAL, "Access block %d%s: %s", bln, ((blinc > 1) && (i < 3) ? "+" : "") , mfGetAccessConditionsDesc(i, &memBlock[6]));\r | |
2402 | bln += blinc;\r | |
2403 | }\r | |
2404 | PrintAndLogEx(NORMAL, "UserData: %s", sprint_hex_inrow(&memBlock[9], 1));\r | |
2405 | }\r | |
a39af1cb | 2406 | \r |
545a1f38 M |
2407 | return 0;\r |
2408 | }\r | |
2409 | \r | |
2410 | int CmdHF14AMfCGetSc(const char *Cmd) {\r | |
5ee70129 | 2411 | uint8_t memBlock[16] = {0x00};\r |
545a1f38 | 2412 | uint8_t sectorNo = 0;\r |
7906cb41 | 2413 | int i, res, flags, gen = 0, baseblock = 0, sect_size = 4;\r |
545a1f38 M |
2414 | \r |
2415 | if (strlen(Cmd) < 1 || param_getchar(Cmd, 0) == 'h') {\r | |
2416 | PrintAndLog("Usage: hf mf cgetsc <sector number>");\r | |
2417 | PrintAndLog("sample: hf mf cgetsc 0");\r | |
664f6586 | 2418 | PrintAndLog("Get sector data from magic Chinese card (only works with such cards)\n");\r |
545a1f38 | 2419 | return 0;\r |
7906cb41 | 2420 | }\r |
545a1f38 M |
2421 | \r |
2422 | sectorNo = param_get8(Cmd, 0);\r | |
7906cb41 F |
2423 | \r |
2424 | if (sectorNo > 39) {\r | |
2425 | PrintAndLog("Sector number must be in [0..15] in MIFARE classic 1k and [0..39] in MIFARE classic 4k.");\r | |
545a1f38 M |
2426 | return 1;\r |
2427 | }\r | |
2428 | \r | |
baeaf579 | 2429 | PrintAndLog("--sector number:%d ", sectorNo);\r |
545a1f38 | 2430 | \r |
7906cb41 F |
2431 | gen = mfCIdentify();\r |
2432 | \r | |
545a1f38 | 2433 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r |
7906cb41 F |
2434 | if (sectorNo < 32 ) {\r |
2435 | baseblock = sectorNo * 4;\r | |
2436 | } else {\r | |
2437 | baseblock = 128 + 16 * (sectorNo - 32);\r | |
2438 | \r | |
2439 | }\r | |
2440 | if (sectorNo > 31) sect_size = 16;\r | |
2441 | \r | |
2442 | for (i = 0; i < sect_size; i++) {\r | |
545a1f38 | 2443 | if (i == 1) flags = 0;\r |
7906cb41 | 2444 | if (i == sect_size - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r |
545a1f38 | 2445 | \r |
7906cb41 F |
2446 | if (gen == 2)\r |
2447 | /* generation 1b magic card */\r | |
2448 | flags |= CSETBLOCK_MAGIC_1B;\r | |
2449 | \r | |
2450 | res = mfCGetBlock(baseblock + i, memBlock, flags);\r | |
545a1f38 | 2451 | if (res) {\r |
7906cb41 | 2452 | PrintAndLog("Can't read block. %d error=%d", baseblock + i, res);\r |
545a1f38 M |
2453 | return 1;\r |
2454 | }\r | |
7906cb41 F |
2455 | \r |
2456 | PrintAndLog("block %3d data:%s", baseblock + i, sprint_hex(memBlock, 16));\r | |
a39af1cb | 2457 | \r |
daccbcdc | 2458 | if (mfIsSectorTrailer(baseblock + i)) {\r |
a39af1cb | 2459 | PrintAndLogEx(NORMAL, "Trailer decoded:");\r |
2460 | PrintAndLogEx(NORMAL, "Key A: %s", sprint_hex_inrow(memBlock, 6));\r | |
2461 | PrintAndLogEx(NORMAL, "Key B: %s", sprint_hex_inrow(&memBlock[10], 6));\r | |
2462 | int bln = baseblock;\r | |
2463 | int blinc = (mfNumBlocksPerSector(sectorNo) > 4) ? 5 : 1;\r | |
2464 | for (int i = 0; i < 4; i++) {\r | |
2465 | PrintAndLogEx(NORMAL, "Access block %d%s: %s", bln, ((blinc > 1) && (i < 3) ? "+" : "") , mfGetAccessConditionsDesc(i, &memBlock[6]));\r | |
2466 | bln += blinc;\r | |
2467 | }\r | |
2468 | PrintAndLogEx(NORMAL, "UserData: %s", sprint_hex_inrow(&memBlock[9], 1));\r | |
2469 | }\r | |
545a1f38 M |
2470 | }\r |
2471 | return 0;\r | |
2472 | }\r | |
2473 | \r | |
baeaf579 | 2474 | \r |
545a1f38 M |
2475 | int CmdHF14AMfCSave(const char *Cmd) {\r |
2476 | \r | |
2477 | FILE * f;\r | |
b915fda3 | 2478 | char filename[FILE_PATH_SIZE] = {0x00};\r |
545a1f38 M |
2479 | char * fnameptr = filename;\r |
2480 | uint8_t fillFromEmulator = 0;\r | |
7906cb41 F |
2481 | uint8_t buf[256] = {0x00};\r |
2482 | int i, j, len, flags, gen = 0, numblock = 64;\r | |
2483 | \r | |
b915fda3 | 2484 | // memset(filename, 0, sizeof(filename));\r |
2485 | // memset(buf, 0, sizeof(buf));\r | |
545a1f38 M |
2486 | \r |
2487 | if (param_getchar(Cmd, 0) == 'h') {\r | |
2488 | PrintAndLog("It saves `magic Chinese` card dump into the file `filename.eml` or `cardID.eml`");\r | |
7906cb41 | 2489 | PrintAndLog("or into emulator memory (option `e`). 4K card: (option `4`)");\r |
a2d058f3 F |
2490 | PrintAndLog("Usage: hf mf csave [file name w/o `.eml`][e][4]");\r |
2491 | PrintAndLog("Sample: hf mf csave ");\r | |
2492 | PrintAndLog(" hf mf csave filename");\r | |
2493 | PrintAndLog(" hf mf csave e");\r | |
2494 | PrintAndLog(" hf mf csave 4");\r | |
2495 | PrintAndLog(" hf mf csave filename 4");\r | |
2496 | PrintAndLog(" hf mf csave e 4");\r | |
545a1f38 | 2497 | return 0;\r |
7906cb41 | 2498 | }\r |
545a1f38 M |
2499 | \r |
2500 | char ctmp = param_getchar(Cmd, 0);\r | |
2501 | if (ctmp == 'e' || ctmp == 'E') fillFromEmulator = 1;\r | |
7906cb41 F |
2502 | if (ctmp == '4') numblock = 256;\r |
2503 | ctmp = param_getchar(Cmd, 1);\r | |
2504 | if (ctmp == '4') numblock = 256;\r | |
2505 | \r | |
2506 | gen = mfCIdentify();\r | |
2507 | PrintAndLog("Saving magic mifare %dK", numblock == 256 ? 4:1);\r | |
545a1f38 M |
2508 | \r |
2509 | if (fillFromEmulator) {\r | |
2510 | // put into emulator\r | |
2511 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
7906cb41 | 2512 | for (i = 0; i < numblock; i++) {\r |
545a1f38 | 2513 | if (i == 1) flags = 0;\r |
7906cb41 F |
2514 | if (i == numblock - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r |
2515 | \r | |
2516 | if (gen == 2)\r | |
2517 | /* generation 1b magic card */\r | |
2518 | flags |= CSETBLOCK_MAGIC_1B;\r | |
2519 | \r | |
545a1f38 M |
2520 | if (mfCGetBlock(i, buf, flags)) {\r |
2521 | PrintAndLog("Cant get block: %d", i);\r | |
2522 | break;\r | |
2523 | }\r | |
7906cb41 | 2524 | \r |
545a1f38 M |
2525 | if (mfEmlSetMem(buf, i, 1)) {\r |
2526 | PrintAndLog("Cant set emul block: %d", i);\r | |
2527 | return 3;\r | |
2528 | }\r | |
2529 | }\r | |
2530 | return 0;\r | |
2531 | } else {\r | |
874572d4 | 2532 | param_getstr(Cmd, 0, filename, sizeof(filename));\r |
7906cb41 F |
2533 | \r |
2534 | len = strlen(filename);\r | |
4c16ae80 | 2535 | if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r |
7906cb41 F |
2536 | \r |
2537 | ctmp = param_getchar(Cmd, 0);\r | |
2538 | if (len < 1 || (ctmp == '4')) {\r | |
545a1f38 | 2539 | // get filename\r |
7906cb41 F |
2540 | \r |
2541 | flags = CSETBLOCK_SINGLE_OPER;\r | |
2542 | if (gen == 2)\r | |
2543 | /* generation 1b magic card */\r | |
2544 | flags |= CSETBLOCK_MAGIC_1B;\r | |
2545 | if (mfCGetBlock(0, buf, flags)) {\r | |
545a1f38 | 2546 | PrintAndLog("Cant get block: %d", 0);\r |
1d537ad6 PL |
2547 | len = sprintf(fnameptr, "dump");\r |
2548 | fnameptr += len;\r | |
2549 | }\r | |
2550 | else {\r | |
2551 | for (j = 0; j < 7; j++, fnameptr += 2)\r | |
7906cb41 | 2552 | sprintf(fnameptr, "%02x", buf[j]);\r |
545a1f38 | 2553 | }\r |
545a1f38 | 2554 | } else {\r |
7906cb41 | 2555 | //memcpy(filename, Cmd, len);\r |
545a1f38 M |
2556 | fnameptr += len;\r |
2557 | }\r | |
2558 | \r | |
7906cb41 F |
2559 | sprintf(fnameptr, ".eml");\r |
2560 | \r | |
545a1f38 M |
2561 | // open file\r |
2562 | f = fopen(filename, "w+");\r | |
2563 | \r | |
b915fda3 | 2564 | if (f == NULL) {\r |
2565 | PrintAndLog("File not found or locked.");\r | |
2566 | return 1;\r | |
2567 | }\r | |
2568 | \r | |
545a1f38 M |
2569 | // put hex\r |
2570 | flags = CSETBLOCK_INIT_FIELD + CSETBLOCK_WUPC;\r | |
7906cb41 | 2571 | for (i = 0; i < numblock; i++) {\r |
545a1f38 | 2572 | if (i == 1) flags = 0;\r |
7906cb41 F |
2573 | if (i == numblock - 1) flags = CSETBLOCK_HALT + CSETBLOCK_RESET_FIELD;\r |
2574 | \r | |
2575 | if (gen == 2)\r | |
2576 | /* generation 1b magic card */\r | |
2577 | flags |= CSETBLOCK_MAGIC_1B;\r | |
545a1f38 M |
2578 | if (mfCGetBlock(i, buf, flags)) {\r |
2579 | PrintAndLog("Cant get block: %d", i);\r | |
2580 | break;\r | |
2581 | }\r | |
2582 | for (j = 0; j < 16; j++)\r | |
7906cb41 | 2583 | fprintf(f, "%02x", buf[j]);\r |
545a1f38 M |
2584 | fprintf(f,"\n");\r |
2585 | }\r | |
2586 | fclose(f);\r | |
7906cb41 | 2587 | \r |
545a1f38 | 2588 | PrintAndLog("Saved to file: %s", filename);\r |
7906cb41 | 2589 | \r |
545a1f38 M |
2590 | return 0;\r |
2591 | }\r | |
2592 | }\r | |
2593 | \r | |
baeaf579 | 2594 | \r |
b62a5a84 | 2595 | int CmdHF14AMfSniff(const char *Cmd){\r |
3fe4ff4f | 2596 | \r |
c948cbde M |
2597 | bool wantLogToFile = 0;\r |
2598 | bool wantDecrypt = 0;\r | |
eede7162 | 2599 | //bool wantSaveToEml = 0; TODO\r |
55acbb2a M |
2600 | bool wantSaveToEmlFile = 0;\r |
2601 | \r | |
7906cb41 | 2602 | //var\r |
39864b0b M |
2603 | int res = 0;\r |
2604 | int len = 0;\r | |
a37725fa | 2605 | int parlen = 0;\r |
39864b0b | 2606 | int blockLen = 0;\r |
39864b0b | 2607 | int pckNum = 0;\r |
f71f4deb | 2608 | int num = 0;\r |
2609 | uint8_t uid[7];\r | |
991f13f2 | 2610 | uint8_t uid_len;\r |
5ee70129 | 2611 | uint8_t atqa[2] = {0x00};\r |
39864b0b M |
2612 | uint8_t sak;\r |
2613 | bool isTag;\r | |
f71f4deb | 2614 | uint8_t *buf = NULL;\r |
2615 | uint16_t bufsize = 0;\r | |
2616 | uint8_t *bufPtr = NULL;\r | |
a37725fa | 2617 | uint8_t parity[16];\r |
7906cb41 | 2618 | \r |
e3c23565 | 2619 | char ctmp = param_getchar(Cmd, 0);\r |
2620 | if ( ctmp == 'h' || ctmp == 'H' ) {\r | |
baeaf579 | 2621 | PrintAndLog("It continuously gets data from the field and saves it to: log, emulator, emulator file.");\r |
55acbb2a M |
2622 | PrintAndLog("You can specify:");\r |
2623 | PrintAndLog(" l - save encrypted sequence to logfile `uid.log`");\r | |
2624 | PrintAndLog(" d - decrypt sequence and put it to log file `uid.log`");\r | |
2625 | PrintAndLog(" n/a e - decrypt sequence, collect read and write commands and save the result of the sequence to emulator memory");\r | |
3fe4ff4f | 2626 | PrintAndLog(" f - decrypt sequence, collect read and write commands and save the result of the sequence to emulator dump file `uid.eml`");\r |
2627 | PrintAndLog("Usage: hf mf sniff [l][d][e][f]");\r | |
55acbb2a | 2628 | PrintAndLog(" sample: hf mf sniff l d e");\r |
b62a5a84 | 2629 | return 0;\r |
7906cb41 F |
2630 | }\r |
2631 | \r | |
55acbb2a | 2632 | for (int i = 0; i < 4; i++) {\r |
e3c23565 | 2633 | ctmp = param_getchar(Cmd, i);\r |
55acbb2a M |
2634 | if (ctmp == 'l' || ctmp == 'L') wantLogToFile = true;\r |
2635 | if (ctmp == 'd' || ctmp == 'D') wantDecrypt = true;\r | |
eede7162 | 2636 | //if (ctmp == 'e' || ctmp == 'E') wantSaveToEml = true; TODO\r |
55acbb2a M |
2637 | if (ctmp == 'f' || ctmp == 'F') wantSaveToEmlFile = true;\r |
2638 | }\r | |
7906cb41 | 2639 | \r |
39864b0b M |
2640 | printf("-------------------------------------------------------------------------\n");\r |
2641 | printf("Executing command. \n");\r | |
2642 | printf("Press the key on the proxmark3 device to abort both proxmark3 and client.\n");\r | |
2643 | printf("Press the key on pc keyboard to abort the client.\n");\r | |
2644 | printf("-------------------------------------------------------------------------\n");\r | |
2645 | \r | |
d714d3ef | 2646 | UsbCommand c = {CMD_MIFARE_SNIFFER, {0, 0, 0}};\r |
2647 | clearCommandBuffer();\r | |
2648 | SendCommand(&c);\r | |
b62a5a84 | 2649 | \r |
39864b0b M |
2650 | // wait cycle\r |
2651 | while (true) {\r | |
2652 | printf(".");\r | |
2653 | fflush(stdout);\r | |
2654 | if (ukbhit()) {\r | |
2655 | getchar();\r | |
2656 | printf("\naborted via keyboard!\n");\r | |
2657 | break;\r | |
2658 | }\r | |
7906cb41 | 2659 | \r |
f71f4deb | 2660 | UsbCommand resp;\r |
8ec06f5e | 2661 | if (WaitForResponseTimeoutW(CMD_ACK, &resp, 2000, false)) {\r |
902cb3c0 | 2662 | res = resp.arg[0] & 0xff;\r |
f71f4deb | 2663 | uint16_t traceLen = resp.arg[1];\r |
2664 | len = resp.arg[2];\r | |
2665 | \r | |
a39af1cb | 2666 | if (res == 0) { // we are done\r |
8ec06f5e | 2667 | break;\r |
4c16ae80 | 2668 | }\r |
f71f4deb | 2669 | \r |
a39af1cb | 2670 | if (res == 1) { // there is (more) data to be transferred\r |
2671 | if (pckNum == 0) { // first packet, (re)allocate necessary buffer\r | |
4c16ae80 | 2672 | if (traceLen > bufsize || buf == NULL) {\r |
f71f4deb | 2673 | uint8_t *p;\r |
a39af1cb | 2674 | if (buf == NULL) { // not yet allocated\r |
f71f4deb | 2675 | p = malloc(traceLen);\r |
a39af1cb | 2676 | } else { // need more memory\r |
f71f4deb | 2677 | p = realloc(buf, traceLen);\r |
2678 | }\r | |
2679 | if (p == NULL) {\r | |
2680 | PrintAndLog("Cannot allocate memory for trace");\r | |
2681 | free(buf);\r | |
2682 | return 2;\r | |
2683 | }\r | |
2684 | buf = p;\r | |
2685 | }\r | |
39864b0b | 2686 | bufPtr = buf;\r |
f71f4deb | 2687 | bufsize = traceLen;\r |
2688 | memset(buf, 0x00, traceLen);\r | |
39864b0b | 2689 | }\r |
902cb3c0 | 2690 | memcpy(bufPtr, resp.d.asBytes, len);\r |
39864b0b M |
2691 | bufPtr += len;\r |
2692 | pckNum++;\r | |
2693 | }\r | |
f71f4deb | 2694 | \r |
a39af1cb | 2695 | if (res == 2) { // received all data, start displaying\r |
39864b0b M |
2696 | blockLen = bufPtr - buf;\r |
2697 | bufPtr = buf;\r | |
2698 | printf(">\n");\r | |
2699 | PrintAndLog("received trace len: %d packages: %d", blockLen, pckNum);\r | |
6a1f2d82 | 2700 | while (bufPtr - buf < blockLen) {\r |
a39af1cb | 2701 | bufPtr += 6; // skip (void) timing information\r |
6a1f2d82 | 2702 | len = *((uint16_t *)bufPtr);\r |
2703 | if(len & 0x8000) {\r | |
2704 | isTag = true;\r | |
2705 | len &= 0x7fff;\r | |
2706 | } else {\r | |
2707 | isTag = false;\r | |
2708 | }\r | |
a37725fa | 2709 | parlen = (len - 1) / 8 + 1;\r |
6a1f2d82 | 2710 | bufPtr += 2;\r |
2711 | if ((len == 14) && (bufPtr[0] == 0xff) && (bufPtr[1] == 0xff) && (bufPtr[12] == 0xff) && (bufPtr[13] == 0xff)) {\r | |
39864b0b M |
2712 | memcpy(uid, bufPtr + 2, 7);\r |
2713 | memcpy(atqa, bufPtr + 2 + 7, 2);\r | |
991f13f2 | 2714 | uid_len = (atqa[0] & 0xC0) == 0x40 ? 7 : 4;\r |
39864b0b | 2715 | sak = bufPtr[11];\r |
7906cb41 | 2716 | PrintAndLog("tag select uid:%s atqa:0x%02x%02x sak:0x%02x",\r |
991f13f2 | 2717 | sprint_hex(uid + (7 - uid_len), uid_len),\r |
7906cb41 F |
2718 | atqa[1],\r |
2719 | atqa[0],\r | |
991f13f2 | 2720 | sak);\r |
d714d3ef | 2721 | if (wantLogToFile || wantDecrypt) {\r |
991f13f2 | 2722 | FillFileNameByUID(logHexFileName, uid + (7 - uid_len), ".log", uid_len);\r |
55acbb2a | 2723 | AddLogCurrentDT(logHexFileName);\r |
7906cb41 F |
2724 | }\r |
2725 | if (wantDecrypt)\r | |
3fe4ff4f | 2726 | mfTraceInit(uid, atqa, sak, wantSaveToEmlFile);\r |
39864b0b | 2727 | } else {\r |
a37725fa | 2728 | oddparitybuf(bufPtr, len, parity);\r |
a39af1cb | 2729 | PrintAndLog("%s(%d):%s [%s] c[%s]%c",\r |
2730 | isTag ? "TAG":"RDR",\r | |
2731 | num,\r | |
2732 | sprint_hex(bufPtr, len),\r | |
2733 | printBitsPar(bufPtr + len, len),\r | |
a37725fa OM |
2734 | printBitsPar(parity, len),\r |
2735 | memcmp(bufPtr + len, parity, len / 8 + 1) ? '!' : ' ');\r | |
7906cb41 | 2736 | if (wantLogToFile)\r |
3fe4ff4f | 2737 | AddLogHex(logHexFileName, isTag ? "TAG: ":"RDR: ", bufPtr, len);\r |
7906cb41 | 2738 | if (wantDecrypt)\r |
a37725fa | 2739 | mfTraceDecode(bufPtr, len, bufPtr[len], wantSaveToEmlFile);\r |
7906cb41 | 2740 | num++;\r |
39864b0b M |
2741 | }\r |
2742 | bufPtr += len;\r | |
a39af1cb | 2743 | bufPtr += parlen; // ignore parity\r |
39864b0b | 2744 | }\r |
f71f4deb | 2745 | pckNum = 0;\r |
39864b0b | 2746 | }\r |
3fe4ff4f | 2747 | } // resp not NULL\r |
39864b0b | 2748 | } // while (true)\r |
f71f4deb | 2749 | \r |
2750 | free(buf);\r | |
a39af1cb | 2751 | \r |
8ec06f5e OM |
2752 | msleep(300); // wait for exiting arm side.\r |
2753 | PrintAndLog("Done.");\r | |
d714d3ef | 2754 | return 0;\r |
b62a5a84 M |
2755 | }\r |
2756 | \r | |
1a5a73ab | 2757 | //needs nt, ar, at, Data to decrypt\r |
2758 | int CmdDecryptTraceCmds(const char *Cmd){\r | |
2759 | uint8_t data[50];\r | |
b8dd1ef6 | 2760 | int len = 100;\r |
2761 | param_gethex_ex(Cmd, 3, data, &len);\r | |
2762 | return tryDecryptWord(param_get32ex(Cmd, 0, 0, 16), param_get32ex(Cmd, 1, 0, 16), param_get32ex(Cmd, 2, 0, 16), data, len/2);\r | |
1a5a73ab | 2763 | }\r |
f71f4deb | 2764 | \r |
7dadcc95 OM |
2765 | int CmdHF14AMfAuth4(const char *cmd) {\r |
2766 | uint8_t keyn[20] = {0};\r | |
2767 | int keynlen = 0;\r | |
2768 | uint8_t key[16] = {0};\r | |
2769 | int keylen = 0;\r | |
ae3340a0 | 2770 | \r |
a39af1cb | 2771 | CLIParserInit("hf mf auth4",\r |
2772 | "Executes AES authentication command in ISO14443-4",\r | |
7dadcc95 OM |
2773 | "Usage:\n\thf mf auth4 4000 000102030405060708090a0b0c0d0e0f -> executes authentication\n"\r |
2774 | "\thf mf auth4 9003 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF -> executes authentication\n");\r | |
2775 | \r | |
2776 | void* argtable[] = {\r | |
2777 | arg_param_begin,\r | |
2778 | arg_str1(NULL, NULL, "<Key Num (HEX 2 bytes)>", NULL),\r | |
2779 | arg_str1(NULL, NULL, "<Key Value (HEX 16 bytes)>", NULL),\r | |
2780 | arg_param_end\r | |
2781 | };\r | |
2782 | CLIExecWithReturn(cmd, argtable, true);\r | |
a39af1cb | 2783 | \r |
ae3340a0 OM |
2784 | CLIGetHexWithReturn(1, keyn, &keynlen);\r |
2785 | CLIGetHexWithReturn(2, key, &keylen);\r | |
7dadcc95 | 2786 | CLIParserFree();\r |
a39af1cb | 2787 | \r |
7dadcc95 OM |
2788 | if (keynlen != 2) {\r |
2789 | PrintAndLog("ERROR: <Key Num> must be 2 bytes long instead of: %d", keynlen);\r | |
2790 | return 1;\r | |
2791 | }\r | |
a39af1cb | 2792 | \r |
7dadcc95 OM |
2793 | if (keylen != 16) {\r |
2794 | PrintAndLog("ERROR: <Key Value> must be 16 bytes long instead of: %d", keylen);\r | |
2795 | return 1;\r | |
2796 | }\r | |
2797 | \r | |
ae3340a0 | 2798 | return MifareAuth4(NULL, keyn, key, true, false, true);\r |
7dadcc95 OM |
2799 | }\r |
2800 | \r | |
fdd9395d OM |
2801 | // https://www.nxp.com/docs/en/application-note/AN10787.pdf\r |
2802 | int CmdHF14AMfMAD(const char *cmd) {\r | |
2803 | \r | |
a39af1cb | 2804 | CLIParserInit("hf mf mad",\r |
2805 | "Checks and prints Mifare Application Directory (MAD)",\r | |
2806 | "Usage:\n\thf mf mad -> shows MAD if exists\n"\r | |
2807 | "\thf mf mad -a 03e1 -k ffffffffffff -b -> shows NDEF data if exists. read card with custom key and key B\n");\r | |
2808 | \r | |
2809 | void *argtable[] = {\r | |
2810 | arg_param_begin,\r | |
2811 | arg_lit0("vV", "verbose", "show technical data"),\r | |
2812 | arg_str0("aA", "aid", "print all sectors with aid", NULL),\r | |
2813 | arg_str0("kK", "key", "key for printing sectors", NULL),\r | |
2814 | arg_lit0("bB", "keyb", "use key B for access printing sectors (by default: key A)"),\r | |
2815 | arg_param_end\r | |
2816 | };\r | |
2817 | CLIExecWithReturn(cmd, argtable, true);\r | |
2818 | bool verbose = arg_get_lit(1);\r | |
2819 | uint8_t aid[2] = {0};\r | |
2820 | int aidlen;\r | |
2821 | CLIGetHexWithReturn(2, aid, &aidlen);\r | |
2822 | uint8_t key[6] = {0};\r | |
2823 | int keylen;\r | |
2824 | CLIGetHexWithReturn(3, key, &keylen);\r | |
2825 | bool keyB = arg_get_lit(4);\r | |
2826 | \r | |
2827 | CLIParserFree();\r | |
2828 | \r | |
2829 | if (aidlen != 2 && keylen > 0) {\r | |
2830 | PrintAndLogEx(WARNING, "do not need a key without aid.");\r | |
2831 | }\r | |
2832 | \r | |
2833 | uint8_t sector0[16 * 4] = {0};\r | |
2834 | uint8_t sector10[16 * 4] = {0};\r | |
2835 | if (mfReadSector(MF_MAD1_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, sector0)) {\r | |
2836 | PrintAndLogEx(ERR, "read sector 0 error. card don't have MAD or don't have MAD on default keys.");\r | |
2837 | return 2;\r | |
2838 | }\r | |
2839 | \r | |
2840 | if (verbose) {\r | |
2841 | for (int i = 0; i < 4; i ++)\r | |
2842 | PrintAndLogEx(NORMAL, "[%d] %s", i, sprint_hex(§or0[i * 16], 16));\r | |
2843 | }\r | |
2844 | \r | |
2845 | bool haveMAD2 = false;\r | |
2846 | MAD1DecodeAndPrint(sector0, verbose, &haveMAD2);\r | |
2847 | \r | |
2848 | if (haveMAD2) {\r | |
2849 | if (mfReadSector(MF_MAD2_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, sector10)) {\r | |
2850 | PrintAndLogEx(ERR, "read sector 0x10 error. card don't have MAD or don't have MAD on default keys.");\r | |
2851 | return 2;\r | |
2852 | }\r | |
2853 | \r | |
2854 | MAD2DecodeAndPrint(sector10, verbose);\r | |
2855 | }\r | |
2856 | \r | |
2857 | if (aidlen == 2) {\r | |
2858 | uint16_t aaid = (aid[0] << 8) + aid[1];\r | |
2859 | PrintAndLogEx(NORMAL, "\n-------------- AID 0x%04x ---------------", aaid);\r | |
2860 | \r | |
2861 | uint16_t mad[7 + 8 + 8 + 8 + 8] = {0};\r | |
2862 | size_t madlen = 0;\r | |
2863 | if (MADDecode(sector0, sector10, mad, &madlen)) {\r | |
2864 | PrintAndLogEx(ERR, "can't decode mad.");\r | |
2865 | return 10;\r | |
2866 | }\r | |
2867 | \r | |
2868 | uint8_t akey[6] = {0};\r | |
2869 | memcpy(akey, g_mifare_ndef_key, 6);\r | |
2870 | if (keylen == 6) {\r | |
2871 | memcpy(akey, key, 6);\r | |
2872 | }\r | |
2873 | \r | |
2874 | for (int i = 0; i < madlen; i++) {\r | |
2875 | if (aaid == mad[i]) {\r | |
2876 | uint8_t vsector[16 * 4] = {0};\r | |
2877 | if (mfReadSector(i + 1, keyB ? MF_KEY_B : MF_KEY_A, akey, vsector)) {\r | |
2878 | PrintAndLogEx(NORMAL, "");\r | |
2879 | PrintAndLogEx(ERR, "read sector %d error.", i + 1);\r | |
2880 | return 2;\r | |
2881 | }\r | |
2882 | \r | |
2883 | for (int j = 0; j < (verbose ? 4 : 3); j ++)\r | |
2884 | PrintAndLogEx(NORMAL, " [%03d] %s", (i + 1) * 4 + j, sprint_hex(&vsector[j * 16], 16));\r | |
2885 | }\r | |
2886 | }\r | |
2887 | }\r | |
2888 | \r | |
2889 | return 0;\r | |
fdd9395d OM |
2890 | }\r |
2891 | \r | |
2892 | int CmdHFMFNDEF(const char *cmd) {\r | |
2893 | \r | |
a39af1cb | 2894 | CLIParserInit("hf mf ndef",\r |
2895 | "Prints NFC Data Exchange Format (NDEF)",\r | |
2896 | "Usage:\n\thf mf ndef -> shows NDEF data\n"\r | |
2897 | "\thf mf ndef -a 03e1 -k ffffffffffff -b -> shows NDEF data with custom AID, key and with key B\n");\r | |
2898 | \r | |
2899 | void *argtable[] = {\r | |
2900 | arg_param_begin,\r | |
2901 | arg_litn("vV", "verbose", 0, 2, "show technical data"),\r | |
2902 | arg_str0("aA", "aid", "replace default aid for NDEF", NULL),\r | |
2903 | arg_str0("kK", "key", "replace default key for NDEF", NULL),\r | |
2904 | arg_lit0("bB", "keyb", "use key B for access sectors (by default: key A)"),\r | |
2905 | arg_param_end\r | |
2906 | };\r | |
2907 | CLIExecWithReturn(cmd, argtable, true);\r | |
2908 | \r | |
2909 | bool verbose = arg_get_lit(1);\r | |
2910 | bool verbose2 = arg_get_lit(1) > 1;\r | |
2911 | uint8_t aid[2] = {0};\r | |
2912 | int aidlen;\r | |
2913 | CLIGetHexWithReturn(2, aid, &aidlen);\r | |
2914 | uint8_t key[6] = {0};\r | |
2915 | int keylen;\r | |
2916 | CLIGetHexWithReturn(3, key, &keylen);\r | |
2917 | bool keyB = arg_get_lit(4);\r | |
2918 | \r | |
2919 | CLIParserFree();\r | |
2920 | \r | |
2921 | uint16_t ndefAID = 0x03e1;\r | |
2922 | if (aidlen == 2)\r | |
2923 | ndefAID = (aid[0] << 8) + aid[1];\r | |
2924 | \r | |
2925 | uint8_t ndefkey[6] = {0};\r | |
2926 | memcpy(ndefkey, g_mifare_ndef_key, 6);\r | |
2927 | if (keylen == 6) {\r | |
2928 | memcpy(ndefkey, key, 6);\r | |
2929 | }\r | |
2930 | \r | |
2931 | uint8_t sector0[16 * 4] = {0};\r | |
2932 | uint8_t sector10[16 * 4] = {0};\r | |
2933 | uint8_t data[4096] = {0};\r | |
2934 | int datalen = 0;\r | |
2935 | \r | |
2936 | PrintAndLogEx(NORMAL, "");\r | |
2937 | \r | |
2938 | if (mfReadSector(MF_MAD1_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, sector0)) {\r | |
2939 | PrintAndLogEx(ERR, "read sector 0 error. card don't have MAD or don't have MAD on default keys.");\r | |
2940 | return 2;\r | |
2941 | }\r | |
2942 | \r | |
2943 | bool haveMAD2 = false;\r | |
2944 | int res = MADCheck(sector0, NULL, verbose, &haveMAD2);\r | |
2945 | if (res) {\r | |
2946 | PrintAndLogEx(ERR, "MAD error %d.", res);\r | |
2947 | return res;\r | |
2948 | }\r | |
2949 | \r | |
2950 | if (haveMAD2) {\r | |
2951 | if (mfReadSector(MF_MAD2_SECTOR, MF_KEY_A, (uint8_t *)g_mifare_mad_key, sector10)) {\r | |
2952 | PrintAndLogEx(ERR, "read sector 0x10 error. card don't have MAD or don't have MAD on default keys.");\r | |
2953 | return 2;\r | |
2954 | }\r | |
2955 | }\r | |
2956 | \r | |
2957 | uint16_t mad[7 + 8 + 8 + 8 + 8] = {0};\r | |
2958 | size_t madlen = 0;\r | |
2959 | if (MADDecode(sector0, (haveMAD2 ? sector10 : NULL), mad, &madlen)) {\r | |
2960 | PrintAndLogEx(ERR, "can't decode mad.");\r | |
2961 | return 10;\r | |
2962 | }\r | |
2963 | \r | |
2964 | printf("data reading:");\r | |
2965 | for (int i = 0; i < madlen; i++) {\r | |
2966 | if (ndefAID == mad[i]) {\r | |
2967 | uint8_t vsector[16 * 4] = {0};\r | |
2968 | if (mfReadSector(i + 1, keyB ? MF_KEY_B : MF_KEY_A, ndefkey, vsector)) {\r | |
2969 | PrintAndLogEx(ERR, "read sector %d error.", i + 1);\r | |
2970 | return 2;\r | |
2971 | }\r | |
2972 | \r | |
2973 | memcpy(&data[datalen], vsector, 16 * 3);\r | |
2974 | datalen += 16 * 3;\r | |
2975 | \r | |
2976 | printf(".");\r | |
2977 | }\r | |
2978 | }\r | |
2979 | printf(" OK\n");\r | |
2980 | \r | |
2981 | if (!datalen) {\r | |
2982 | PrintAndLogEx(ERR, "no NDEF data.");\r | |
2983 | return 11;\r | |
2984 | }\r | |
2985 | \r | |
2986 | if (verbose2) {\r | |
2987 | PrintAndLogEx(NORMAL, "NDEF data:");\r | |
2988 | dump_buffer(data, datalen, stdout, 1);\r | |
2989 | }\r | |
2990 | \r | |
2991 | NDEFDecodeAndPrint(data, datalen, verbose);\r | |
2992 | \r | |
2993 | return 0;\r | |
fdd9395d OM |
2994 | }\r |
2995 | \r | |
26fdb4ab | 2996 | static command_t CommandTable[] =\r |
9ca155ba | 2997 | {\r |
c48c4d78 | 2998 | {"help", CmdHelp, 1, "This help"},\r |
2999 | {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},\r | |
3000 | {"rdbl", CmdHF14AMfRdBl, 0, "Read MIFARE classic block"},\r | |
3001 | {"rdsc", CmdHF14AMfRdSc, 0, "Read MIFARE classic sector"},\r | |
3002 | {"dump", CmdHF14AMfDump, 0, "Dump MIFARE classic tag to binary file"},\r | |
a39af1cb | 3003 | {"restore", CmdHF14AMfRestore, 0, "Restore MIFARE classic binary file to BLANK tag"},\r |
c48c4d78 | 3004 | {"wrbl", CmdHF14AMfWrBl, 0, "Write MIFARE classic block"},\r |
7dadcc95 | 3005 | {"auth4", CmdHF14AMfAuth4, 0, "ISO14443-4 AES authentication"},\r |
c48c4d78 | 3006 | {"chk", CmdHF14AMfChk, 0, "Test block keys"},\r |
3007 | {"mifare", CmdHF14AMifare, 0, "Read parity error messages."},\r | |
3008 | {"hardnested", CmdHF14AMfNestedHard, 0, "Nested attack for hardened Mifare cards"},\r | |
3009 | {"nested", CmdHF14AMfNested, 0, "Test nested authentication"},\r | |
3010 | {"sniff", CmdHF14AMfSniff, 0, "Sniff card-reader communication"},\r | |
a8561e35 | 3011 | {"sim", CmdHF14AMfSim, 0, "Simulate MIFARE card"},\r |
3012 | {"eclr", CmdHF14AMfEClear, 0, "Clear simulator memory"},\r | |
c48c4d78 | 3013 | {"eget", CmdHF14AMfEGet, 0, "Get simulator memory block"},\r |
3014 | {"eset", CmdHF14AMfESet, 0, "Set simulator memory block"},\r | |
3015 | {"eload", CmdHF14AMfELoad, 0, "Load from file emul dump"},\r | |
3016 | {"esave", CmdHF14AMfESave, 0, "Save to file emul dump"},\r | |
3017 | {"ecfill", CmdHF14AMfECFill, 0, "Fill simulator memory with help of keys from simulator"},\r | |
3018 | {"ekeyprn", CmdHF14AMfEKeyPrn, 0, "Print keys from simulator memory"},\r | |
3a05a1e7 | 3019 | {"cwipe", CmdHF14AMfCWipe, 0, "Wipe magic Chinese card"},\r |
c48c4d78 | 3020 | {"csetuid", CmdHF14AMfCSetUID, 0, "Set UID for magic Chinese card"},\r |
3021 | {"csetblk", CmdHF14AMfCSetBlk, 0, "Write block - Magic Chinese card"},\r | |
3022 | {"cgetblk", CmdHF14AMfCGetBlk, 0, "Read block - Magic Chinese card"},\r | |
3023 | {"cgetsc", CmdHF14AMfCGetSc, 0, "Read sector - Magic Chinese card"},\r | |
3024 | {"cload", CmdHF14AMfCLoad, 0, "Load dump into magic Chinese card"},\r | |
3025 | {"csave", CmdHF14AMfCSave, 0, "Save dump from magic Chinese card into file or emulator"},\r | |
3026 | {"decrypt", CmdDecryptTraceCmds, 1, "[nt] [ar_enc] [at_enc] [data] - to decrypt snoop or trace"},\r | |
fdd9395d OM |
3027 | {"mad", CmdHF14AMfMAD, 0, "Checks and prints MAD"},\r |
3028 | {"ndef", CmdHFMFNDEF, 0, "Prints NDEF records from card"},\r | |
c48c4d78 | 3029 | {NULL, NULL, 0, NULL}\r |
9ca155ba M |
3030 | };\r |
3031 | \r | |
3032 | int CmdHFMF(const char *Cmd)\r | |
3033 | {\r | |
44964fd1 | 3034 | (void)WaitForResponseTimeout(CMD_ACK,NULL,100);\r |
3035 | CmdsParse(CommandTable, Cmd);\r | |
3036 | return 0;\r | |
9ca155ba M |
3037 | }\r |
3038 | \r | |
3039 | int CmdHelp(const char *Cmd)\r | |
3040 | {\r | |
3041 | CmdsHelp(CommandTable);\r | |
3042 | return 0;\r | |
3043 | }\r |