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