+int mfCWipe(uint32_t numSectors, bool gen1b, bool wantWipe, bool wantFill) {\r
+ uint8_t isOK = 0;\r
+ uint8_t cmdParams = wantWipe + wantFill * 0x02 + gen1b * 0x04;\r
+ UsbCommand c = {CMD_MIFARE_CWIPE, {numSectors, cmdParams, 0}};\r
+ SendCommand(&c);\r
+\r
+ UsbCommand resp;\r
+ WaitForResponse(CMD_ACK,&resp);\r
+ isOK = resp.arg[0] & 0xff;\r
+ \r
+ return isOK;\r
+}\r
+\r
+int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID) {\r
+ uint8_t oldblock0[16] = {0x00};\r
+ uint8_t block0[16] = {0x00};\r
+ int gen = 0, res;\r
+\r
+ gen = mfCIdentify();\r
+\r
+ /* generation 1a magic card by default */\r
+ uint8_t cmdParams = CSETBLOCK_SINGLE_OPER;\r
+ if (gen == 2) {\r
+ /* generation 1b magic card */\r
+ cmdParams = CSETBLOCK_SINGLE_OPER | CSETBLOCK_MAGIC_1B;\r
+ }\r
+ \r
+ res = mfCGetBlock(0, oldblock0, cmdParams);\r
+\r
+ if (res == 0) {\r
+ memcpy(block0, oldblock0, 16);\r
+ PrintAndLog("old block 0: %s", sprint_hex(block0,16));\r
+ } else {\r
+ PrintAndLog("Couldn't get old data. Will write over the last bytes of Block 0.");\r
+ }\r
+\r
+ // fill in the new values\r
+ // UID\r
+ memcpy(block0, uid, 4);\r
+ // Mifare UID BCC\r
+ block0[4] = block0[0] ^ block0[1] ^ block0[2] ^ block0[3];\r
+ // mifare classic SAK(byte 5) and ATQA(byte 6 and 7, reversed)\r
+ if (sak != NULL)\r
+ block0[5] = sak[0];\r
+ if (atqa != NULL) {\r
+ block0[6] = atqa[1];\r
+ block0[7] = atqa[0];\r
+ }\r
+ PrintAndLog("new block 0: %s", sprint_hex(block0, 16));\r
+\r
+ res = mfCSetBlock(0, block0, oldUID, false, cmdParams);\r
+ if (res) {\r
+ PrintAndLog("Can't set block 0. Error: %d", res);\r
+ return res;\r
+ }\r
+ \r
+ return 0;\r
+}\r
+\r
+int mfCIdentify()\r
+{\r
+ UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}};\r
+ SendCommand(&c);\r
+\r
+ UsbCommand resp;\r
+ WaitForResponse(CMD_ACK,&resp);\r
+\r
+ // iso14a_card_select_t card;\r
+ // memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));\r
+\r
+ // uint64_t select_status = resp.arg[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision\r
+\r
+ // if(select_status != 0) {\r
+ // uint8_t rats[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0\r
+ // c.arg[0] = ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT;\r
+ // c.arg[1] = 2;\r
+ // c.arg[2] = 0;\r
+ // memcpy(c.d.asBytes, rats, 2);\r
+ // SendCommand(&c);\r
+ // WaitForResponse(CMD_ACK,&resp);\r
+ // }\r
+\r
+ c.cmd = CMD_MIFARE_CIDENT;\r
+ c.arg[0] = 0;\r
+ c.arg[1] = 0;\r
+ c.arg[2] = 0;\r
+ SendCommand(&c);\r
+ WaitForResponse(CMD_ACK,&resp);\r
+\r
+ uint8_t isGeneration = resp.arg[0] & 0xff;\r
+ switch( isGeneration ){\r
+ case 1: PrintAndLog("Chinese magic backdoor commands (GEN 1a) detected"); break;\r
+ case 2: PrintAndLog("Chinese magic backdoor command (GEN 1b) detected"); break;\r
+ default: PrintAndLog("No chinese magic backdoor command detected"); break;\r
+ }\r
+\r
+ // disconnect\r
+ c.cmd = CMD_READER_ISO_14443a;\r
+ c.arg[0] = 0;\r
+ c.arg[1] = 0;\r
+ c.arg[2] = 0;\r
+ SendCommand(&c);\r
+\r
+ return (int) isGeneration;\r
+}\r
+\r
+\r