]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/mifarehost.c
small fix #416
[proxmark3-svn] / client / mifarehost.c
index 3b5247589a86b8e594e6d0102b267f6226ebd12e..a02019a316cbc52f6cbbe4cad8cea733c409f8f8 100644 (file)
@@ -228,17 +228,46 @@ int mfCheckKeys (uint8_t blockNo, uint8_t keyType, bool clear_trace, uint8_t key
 \r
        *key = -1;\r
 \r
-       UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType&0xff)<<8)), clear_trace, keycnt}};\r
+       UsbCommand c = {CMD_MIFARE_CHKKEYS, {((blockNo & 0xff) | ((keyType & 0xff) << 8)), clear_trace, keycnt}}; \r
        memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r
        SendCommand(&c);\r
 \r
        UsbCommand resp;\r
-       if (!WaitForResponseTimeout(CMD_ACK,&resp,3000)) return 1;\r
+       if (!WaitForResponseTimeout(CMD_ACK,&resp,3000)) return 1; \r
        if ((resp.arg[0] & 0xff) != 0x01) return 2;\r
        *key = bytes_to_num(resp.d.asBytes, 6);\r
        return 0;\r
 }\r
 \r
+int mfCheckKeysSec(uint8_t sectorCnt, uint8_t keyType, uint8_t timeout14a, bool clear_trace, uint8_t keycnt, uint8_t * keyBlock, sector_t * e_sector){\r
+\r
+       uint8_t keyPtr = 0;\r
+\r
+       if (e_sector == NULL)\r
+               return -1;\r
+\r
+       UsbCommand c = {CMD_MIFARE_CHKKEYS, {((sectorCnt & 0xff) | ((keyType & 0xff) << 8)), (clear_trace | 0x02)|((timeout14a & 0xff) << 8), keycnt}}; \r
+       memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r
+       SendCommand(&c);\r
+\r
+       UsbCommand resp;\r
+       if (!WaitForResponseTimeoutW(CMD_ACK, &resp, MAX(3000, 1000 + 13 * sectorCnt * keycnt * (keyType == 2 ? 2 : 1)), false)) return 1; // timeout: 13 ms / fail auth\r
+       if ((resp.arg[0] & 0xff) != 0x01) return 2;\r
+       \r
+       bool foundAKey = false;\r
+       for(int sec = 0; sec < sectorCnt; sec++){\r
+               for(int keyAB = 0; keyAB < 2; keyAB++){\r
+                       keyPtr = *(resp.d.asBytes + keyAB * 40 + sec);\r
+                       if (keyPtr){\r
+                               e_sector[sec].foundKey[keyAB] = true;\r
+                               e_sector[sec].Key[keyAB] = bytes_to_num(keyBlock + (keyPtr - 1) * 6, 6);\r
+                               foundAKey = true;\r
+                       }\r
+               }\r
+       }\r
+       return foundAKey ? 0 : 3;\r
+}\r
+\r
 // Compare 16 Bits out of cryptostate\r
 int Compare16Bits(const void * a, const void * b) {\r
        if ((*(uint64_t*)b & 0x00ff000000ff0000) == (*(uint64_t*)a & 0x00ff000000ff0000)) return 0;\r
@@ -418,7 +447,7 @@ int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params) {
        UsbCommand c = {CMD_MIFARE_CGETBLOCK, {params, 0, blockNo}};\r
        SendCommand(&c);\r
 \r
-  UsbCommand resp;\r
+       UsbCommand resp;\r
        if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
                isOK  = resp.arg[0] & 0xff;\r
                memcpy(data, resp.d.asBytes, 16);\r
@@ -437,8 +466,8 @@ int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, bool wantWipe, uin
        memcpy(c.d.asBytes, data, 16);\r
        SendCommand(&c);\r
 \r
-  UsbCommand resp;\r
-       if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
+       UsbCommand resp;\r
+       if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {\r
                isOK  = resp.arg[0] & 0xff;\r
                if (uid != NULL)\r
                        memcpy(uid, resp.d.asBytes, 4);\r
@@ -448,25 +477,40 @@ int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, bool wantWipe, uin
                PrintAndLog("Command execute timeout");\r
                return 1;\r
        }\r
+\r
        return 0;\r
 }\r
 \r
-int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID, bool wantWipe) {\r
+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 old, gen = 0;\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
-               old = mfCGetBlock(0, oldblock0, CSETBLOCK_SINGLE_OPER | CSETBLOCK_MAGIC_1B);\r
-       } else {\r
-               /* generation 1a magic card by default */\r
-               old = mfCGetBlock(0, oldblock0, CSETBLOCK_SINGLE_OPER);\r
+               cmdParams = CSETBLOCK_SINGLE_OPER | CSETBLOCK_MAGIC_1B;\r
        }\r
+       \r
+       res = mfCGetBlock(0, oldblock0, cmdParams);\r
 \r
-       if (old == 0) {\r
+       if (res == 0) {\r
                memcpy(block0, oldblock0, 16);\r
                PrintAndLog("old block 0:  %s", sprint_hex(block0,16));\r
        } else {\r
@@ -477,25 +521,73 @@ int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID, bool w
        // UID\r
        memcpy(block0, uid, 4);\r
        // Mifare UID BCC\r
-       block0[4] = block0[0]^block0[1]^block0[2]^block0[3];\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
+       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
+       PrintAndLog("new block 0:  %s", sprint_hex(block0, 16));\r
 \r
-       if (gen == 2) {\r
-               /* generation 1b magic card */\r
-               return mfCSetBlock(0, block0, oldUID, wantWipe, CSETBLOCK_SINGLE_OPER | CSETBLOCK_MAGIC_1B);\r
-       } else {\r
-               /* generation 1a magic card by default */\r
-               return mfCSetBlock(0, block0, oldUID, wantWipe, CSETBLOCK_SINGLE_OPER);\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
 // SNIFFER\r
 \r
 // constants\r
@@ -820,6 +912,8 @@ int mfTraceDecode(uint8_t *data_src, int len, bool wantSaveToEmlFile) {
        return 0;\r
 }\r
 \r
+// DECODING\r
+\r
 int tryDecryptWord(uint32_t nt, uint32_t ar_enc, uint32_t at_enc, uint8_t *data, int len){\r
        /*\r
        uint32_t nt;      // tag challenge\r
@@ -840,49 +934,3 @@ int tryDecryptWord(uint32_t nt, uint32_t ar_enc, uint32_t at_enc, uint8_t *data,
        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
Impressum, Datenschutz