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