+
+ if (verbose) PrintAndLog("Authenticating with %s: %s", keytypetext, sprint_hex(div_key, 8));
+
+ UsbCommand resp;
+ UsbCommand d = {CMD_ICLASS_READCHECK, {2, use_credit_key, 0}};
+
+ clearCommandBuffer();
+ SendCommand(&d);
+
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) {
+ if (verbose) PrintAndLog("Auth Command (READCHECK[2]) execute timeout");
+ return false;
+ }
+ bool isOK = resp.arg[0];
+ if (!isOK) {
+ if (verbose) PrintAndLog("Couldn't get Card Challenge");
+ return false;
+ }
+
+ if (replay) {
+ memcpy(MAC, KEY+4, 4);
+ } else {
+ uint8_t CCNR[12];
+ memcpy(CCNR, resp.d.asBytes, 8);
+ memset(CCNR+8, 0x00, 4); // default NR = {0, 0, 0, 0}
+ doMAC(CCNR, div_key, MAC);
+ }
+
+ d.cmd = CMD_ICLASS_CHECK;
+ if (replay) {
+ memcpy(d.d.asBytes, KEY, 8);
+ } else {
+ memset(d.d.asBytes, 0x00, 4); // default NR = {0, 0, 0, 0}
+ memcpy(d.d.asBytes+4, MAC, 4);
+ }
+ clearCommandBuffer();
+ SendCommand(&d);
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) {
+ if (verbose) PrintAndLog("Auth Command (CHECK) execute timeout");
+ return false;
+ }
+ isOK = resp.arg[0];
+ if (!isOK) {
+ if (verbose) PrintAndLog("Authentication error");
+ return false;
+ }
+ return true;
+}
+
+
+static void usage_hf_iclass_dump(void) {
+ PrintAndLog("Usage: hf iclass dump f <fileName> k <Key> c <CreditKey> e|r|n\n");
+ PrintAndLog("Options:");
+ PrintAndLog(" f <filename> : specify a filename to save dump to");
+ PrintAndLog(" k <Key> : *Debit Key (AA1) as 16 hex symbols (8 bytes) or 1 hex to select key from memory");
+ PrintAndLog(" c <CreditKey>: Credit Key (AA2) as 16 hex symbols (8 bytes) or 1 hex to select key from memory");
+ PrintAndLog(" e : If 'e' is specified, the keys are interpreted as Elite");
+ PrintAndLog(" Custom Keys (KCus), which can be obtained via reader-attack");
+ PrintAndLog(" See 'hf iclass sim 2'. This key should be on iclass-format");
+ PrintAndLog(" r : If 'r' is specified, keys are interpreted as raw blocks 3/4");
+ PrintAndLog(" n : If 'n' is specified, keys are interpreted as NR/MAC pairs which can be obtained by 'hf iclass snoop'");
+ PrintAndLog(" NOTE: * = required");
+ PrintAndLog("Samples:");
+ PrintAndLog(" hf iclass dump k 001122334455667B");
+ PrintAndLog(" hf iclass dump k AAAAAAAAAAAAAAAA c 001122334455667B");
+ PrintAndLog(" hf iclass dump k AAAAAAAAAAAAAAAA e");
+}
+
+
+static void printIclassDumpContents(uint8_t *iclass_dump, uint8_t startblock, uint8_t endblock, size_t filesize) {
+ uint8_t mem_config;
+ memcpy(&mem_config, iclass_dump + 13,1);
+ uint8_t maxmemcount;
+ uint8_t filemaxblock = filesize / 8;
+ if (mem_config & 0x80)
+ maxmemcount = 255;
+ else
+ maxmemcount = 31;
+ //PrintAndLog ("endblock: %d, filesize: %d, maxmemcount: %d, filemaxblock: %d", endblock,filesize, maxmemcount, filemaxblock);
+
+ if (startblock == 0)
+ startblock = 6;
+ if ((endblock > maxmemcount) || (endblock == 0))
+ endblock = maxmemcount;
+
+ // remember endblock need to relate to zero-index arrays.
+ if (endblock > filemaxblock-1)
+ endblock = filemaxblock;
+
+ int i = startblock;
+ printf("------+--+-------------------------+\n");
+ while (i <= endblock) {
+ uint8_t *blk = iclass_dump + (i * 8);
+ printf("Block |%02X| %s|\n", i, sprint_hex(blk, 8) );
+ i++;
+ }
+ printf("------+--+-------------------------+\n");
+}
+
+
+static int CmdHFiClassReader_Dump(const char *Cmd) {
+
+ uint8_t MAC[4] = {0x00,0x00,0x00,0x00};
+ uint8_t div_key[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ uint8_t c_div_key[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ uint8_t blockno = 0;
+ uint8_t AA1_maxBlk = 0;
+ uint8_t maxBlk = 31;
+ uint8_t app_areas = 1;
+ uint8_t kb = 2;
+ uint8_t KEY[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ uint8_t CreditKEY[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ uint8_t keyNbr = 0;
+ uint8_t dataLen = 0;
+ uint8_t fileNameLen = 0;
+ char filename[FILE_PATH_SIZE]={0};
+ char tempStr[50] = {0};
+ bool have_debit_key = false;
+ bool have_credit_key = false;
+ bool use_credit_key = false;
+ bool elite = false;
+ bool rawkey = false;
+ bool NRMAC_replay = false;
+ bool errors = false;
+ bool verbose = false;
+ uint8_t cmdp = 0;
+
+ while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
+ switch(param_getchar(Cmd, cmdp)) {
+ case 'h':
+ case 'H':
+ usage_hf_iclass_dump();
+ return 0;
+ case 'c':
+ case 'C':
+ have_credit_key = true;
+ dataLen = param_getstr(Cmd, cmdp+1, tempStr, sizeof(tempStr));
+ if (dataLen == 16) {
+ errors = param_gethex(tempStr, 0, CreditKEY, dataLen);
+ } else if (dataLen == 1) {
+ keyNbr = param_get8(Cmd, cmdp+1);
+ if (keyNbr < ICLASS_KEYS_MAX) {
+ memcpy(CreditKEY, iClass_Key_Table[keyNbr], 8);
+ } else {
+ PrintAndLog("\nERROR: Credit KeyNbr is invalid\n");
+ errors = true;
+ }
+ } else {
+ PrintAndLog("\nERROR: Credit Key is incorrect length\n");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 'e':
+ case 'E':
+ elite = true;
+ cmdp++;
+ break;
+ case 'f':
+ case 'F':
+ fileNameLen = param_getstr(Cmd, cmdp+1, filename, sizeof(filename));
+ if (fileNameLen < 1) {
+ PrintAndLog("No filename found after f");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 'k':
+ case 'K':
+ have_debit_key = true;
+ dataLen = param_getstr(Cmd, cmdp+1, tempStr, sizeof(tempStr));
+ if (dataLen == 16) {
+ errors = param_gethex(tempStr, 0, KEY, dataLen);
+ } else if (dataLen == 1) {
+ keyNbr = param_get8(Cmd, cmdp+1);
+ if (keyNbr < ICLASS_KEYS_MAX) {
+ memcpy(KEY, iClass_Key_Table[keyNbr], 8);
+ } else {
+ PrintAndLog("\nERROR: Debit KeyNbr is invalid\n");
+ errors = true;
+ }
+ } else {
+ PrintAndLog("\nERROR: Debit Key is incorrect length\n");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 'r':
+ case 'R':
+ rawkey = true;
+ cmdp++;
+ break;
+ case 'n':
+ case 'N':
+ NRMAC_replay = true;
+ cmdp++;
+ break;
+ case 'v':
+ case 'V':
+ verbose = true;
+ cmdp++;
+ break;
+ default:
+ PrintAndLog("Unknown parameter '%c'\n", param_getchar(Cmd, cmdp));
+ errors = true;
+ break;
+ }
+ }
+
+ if (elite + rawkey + NRMAC_replay > 1) {
+ PrintAndLog("You cannot combine the 'e', 'r', and 'n' options\n");
+ errors = true;
+ }
+
+ if (errors || cmdp < 2) {
+ usage_hf_iclass_dump();
+ return 0;
+ }
+
+ // if only credit key is given: try for AA1 as well (not for iclass but for some picopass this will work)
+ if (!have_debit_key && have_credit_key) {
+ memcpy(KEY, CreditKEY, 8);
+ }
+
+ // clear trace and get first 3 blocks
+ UsbCommand c = {CMD_READER_ICLASS, {FLAG_ICLASS_READER_INIT | FLAG_ICLASS_READER_CLEARTRACE | FLAG_ICLASS_READER_CSN | FLAG_ICLASS_READER_CONF | FLAG_ICLASS_READER_CC}};
+ UsbCommand resp;
+ uint8_t tag_data[256*8];
+
+ clearCommandBuffer();
+ SendCommand(&c);
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) {
+ PrintAndLog("Command execute timeout");
+ DropField();
+ return 0;
+ }
+
+ uint8_t readStatus = resp.arg[0] & 0xff;
+ uint8_t *data = resp.d.asBytes;
+ uint8_t status_mask = FLAG_ICLASS_READER_CSN | FLAG_ICLASS_READER_CONF | FLAG_ICLASS_READER_CC;
+
+ if (readStatus != status_mask) {
+ PrintAndLog("No tag found ...");
+ return 0;
+ } else {
+ memcpy(tag_data, data, 8*3);
+ if (verbose) PrintAndLog("CSN: %s", sprint_hex(tag_data, 8));
+ AA1_maxBlk = data[8];
+ getMemConfig(data[13], data[12], &maxBlk, &app_areas, &kb);
+ // large memory - not able to dump pages currently
+ if (AA1_maxBlk > maxBlk) AA1_maxBlk = maxBlk;
+ }
+
+ // authenticate with debit key (or credit key if we have no debit key) and get div_key - later store in dump block 3
+ if (!iClass_authenticate(tag_data, KEY, MAC, div_key, false, elite, rawkey, NRMAC_replay, verbose)) {
+ DropField();
+ return 0;
+ }
+
+ // read AA1
+ UsbCommand w = {CMD_ICLASS_DUMP};
+ uint32_t blocksRead = 0;
+ for (blockno = 3; blockno <= AA1_maxBlk; blockno += blocksRead) {
+ w.arg[0] = blockno;
+ w.arg[1] = AA1_maxBlk - blockno + 1;
+ clearCommandBuffer();
+ SendCommand(&w);
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) {
+ PrintAndLog("Command execute time-out 1");
+ DropField();
+ return 1;
+ }
+ blocksRead = resp.arg[1];
+ bool isOK = resp.arg[0];
+ if (!isOK) {
+ PrintAndLog("Reading AA1 block failed");
+ DropField();
+ return 0;
+ }
+ memcpy(tag_data + blockno*8, resp.d.asBytes, blocksRead*8);
+ }
+
+ // do we still need to read more blocks (AA2 enabled)?
+ if (have_credit_key && maxBlk > AA1_maxBlk) {
+ if (!use_credit_key) {
+ //turn off hf field before authenticating with different key
+ DropField();
+ // AA2 authenticate credit key and git c_div_key - later store in dump block 4
+ uint8_t CSN[8];
+ if (!iClass_select(CSN, verbose, false, true) || !iClass_authenticate(CSN, CreditKEY, MAC, c_div_key, true, false, false, NRMAC_replay, verbose)){
+ DropField();
+ return 0;
+ }
+ }
+ for ( ; blockno <= maxBlk; blockno += blocksRead) {
+ w.arg[0] = blockno;
+ w.arg[1] = maxBlk - blockno + 1;
+ clearCommandBuffer();
+ SendCommand(&w);
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) {
+ PrintAndLog("Command execute time-out 1");
+ DropField();
+ return 1;
+ }
+ blocksRead = resp.arg[1];
+ bool isOK = resp.arg[0];
+ if (!isOK) {
+ PrintAndLog("Reading AA2 block failed");
+ DropField();
+ return 0;
+ }
+ memcpy(tag_data + blockno*8, resp.d.asBytes, blocksRead*8);
+ }
+ }
+
+ DropField();
+
+ // add diversified keys to dump
+ if (have_debit_key) {
+ memcpy(tag_data + 3*8, div_key, 8);
+ } else {
+ memset(tag_data + 3*8, 0xff, 8);
+ }
+ if (have_credit_key) {
+ memcpy(tag_data + 4*8, c_div_key, 8);
+ } else {
+ memset(tag_data + 4*8, 0xff, 8);
+ }
+
+ // print the dump
+ printf("------+--+-------------------------+\n");
+ printf("CSN |00| %s|\n",sprint_hex(tag_data, 8));
+ printIclassDumpContents(tag_data, 1, blockno-1, blockno*8);
+
+ if (filename[0] == 0) {
+ snprintf(filename, FILE_PATH_SIZE,"iclass_tagdump-%02x%02x%02x%02x%02x%02x%02x%02x",
+ tag_data[0],tag_data[1],tag_data[2],tag_data[3],
+ tag_data[4],tag_data[5],tag_data[6],tag_data[7]);
+ }
+
+ // save the dump to .bin file
+ PrintAndLog("Saving dump file - %d blocks read", blockno);
+ saveFile(filename, "bin", tag_data, blockno*8);
+ return 1;
+}
+
+
+static int WriteBlock(uint8_t blockno, uint8_t *bldata, uint8_t *KEY, bool use_credit_key, bool elite, bool rawkey, bool NRMAC_replay, bool verbose) {
+
+ uint8_t MAC[4] = {0x00,0x00,0x00,0x00};
+ uint8_t div_key[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ uint8_t CSN[8];
+
+ if (!iClass_select(CSN, verbose, true, true) || !iClass_authenticate(CSN, KEY, MAC, div_key, use_credit_key, elite, rawkey, NRMAC_replay, verbose)) {
+ DropField();
+ return 0;
+ }
+
+ UsbCommand resp;
+
+ Calc_wb_mac(blockno, bldata, div_key, MAC);
+
+ UsbCommand w = {CMD_ICLASS_WRITEBLOCK, {blockno}};
+ memcpy(w.d.asBytes, bldata, 8);
+ memcpy(w.d.asBytes + 8, MAC, 4);
+
+ clearCommandBuffer();
+ SendCommand(&w);
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) {
+ PrintAndLog("Write Command execute timeout");
+ DropField();
+ return 0;
+ }
+ bool isOK = resp.arg[0];
+ if (!isOK) {
+ PrintAndLog("Write Block Failed");
+ DropField();
+ return 0;
+ }
+
+ PrintAndLog("Write Block Successful");
+ return 1;
+}
+
+
+static void usage_hf_iclass_writeblock(void) {
+ PrintAndLog("Options:");
+ PrintAndLog(" b <Block> : The block number as 2 hex symbols");
+ PrintAndLog(" d <data> : Set the Data to write as 16 hex symbols");
+ PrintAndLog(" k <Key> : Access Key as 16 hex symbols or 1 hex to select key from memory");
+ PrintAndLog(" c : If 'c' is specified, the key set is assumed to be the credit key\n");
+ PrintAndLog(" e : If 'e' is specified, elite computations applied to key");
+ PrintAndLog(" r : If 'r' is specified, no computations applied to key");
+ PrintAndLog(" o : override protection and allow modification of blocks 0...4");
+ PrintAndLog("Samples:");
+ PrintAndLog(" hf iclass writeblk b 0A d AAAAAAAAAAAAAAAA k 001122334455667B");
+ PrintAndLog(" hf iclass writeblk b 1B d AAAAAAAAAAAAAAAA k 001122334455667B c");
+ PrintAndLog(" hf iclass writeblk b 03 d AAAAAAAAAAAAAAAA k 001122334455667B c o");
+}
+
+
+static int CmdHFiClass_WriteBlock(const char *Cmd) {
+ uint8_t blockno = 0;
+ uint8_t bldata[8] = {0};
+ uint8_t KEY[8] = {0};
+ uint8_t keyNbr = 0;
+ uint8_t dataLen = 0;
+ char tempStr[50] = {0};
+ bool use_credit_key = false;
+ bool elite = false;
+ bool rawkey = false;
+ bool override_protection = false;
+ bool errors = false;
+ uint8_t cmdp = 0;
+
+ while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
+ switch(param_getchar(Cmd, cmdp)) {
+ case 'h':
+ case 'H':
+ usage_hf_iclass_writeblock();
+ return 0;
+ case 'b':
+ case 'B':
+ if (param_gethex(Cmd, cmdp+1, &blockno, 2)) {
+ PrintAndLog("Block No must include 2 HEX symbols\n");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 'c':
+ case 'C':
+ use_credit_key = true;
+ cmdp++;
+ break;
+ case 'd':
+ case 'D':
+ if (param_gethex(Cmd, cmdp+1, bldata, 16)) {
+ PrintAndLog("Data must include 16 HEX symbols\n");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 'e':
+ case 'E':
+ elite = true;
+ cmdp++;
+ break;
+ case 'k':
+ case 'K':
+ dataLen = param_getstr(Cmd, cmdp+1, tempStr, sizeof(tempStr));
+ if (dataLen == 16) {
+ errors = param_gethex(tempStr, 0, KEY, dataLen);
+ } else if (dataLen == 1) {
+ keyNbr = param_get8(Cmd, cmdp+1);
+ if (keyNbr < ICLASS_KEYS_MAX) {
+ memcpy(KEY, iClass_Key_Table[keyNbr], 8);
+ } else {
+ PrintAndLog("\nERROR: Credit KeyNbr is invalid\n");
+ errors = true;
+ }
+ } else {
+ PrintAndLog("\nERROR: Credit Key is incorrect length\n");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 'r':
+ case 'R':
+ rawkey = true;
+ cmdp++;
+ break;
+ case 'o':
+ case 'O':
+ override_protection = true;
+ cmdp++;
+ break;
+ default:
+ PrintAndLog("Unknown parameter '%c'\n", param_getchar(Cmd, cmdp));
+ errors = true;
+ break;
+ }
+ if (errors) {
+ usage_hf_iclass_writeblock();
+ return 0;
+ }
+ }
+
+ if (elite && rawkey) {
+ PrintAndLog("You cannot combine the 'e' and 'r' options\n");
+ errors = true;
+ }
+
+ if (cmdp < 6) {
+ usage_hf_iclass_writeblock();
+ return 0;
+ }
+
+ if (blockno < 5) {
+ if (override_protection) {
+ PrintAndLog("Info: modifying keys, e-purse or configuration block.");
+ } else {
+ PrintAndLog("You are going to modify keys, e-purse or configuration block.");
+ PrintAndLog("You must add the 'o' (override) option to confirm that you know what you are doing");
+ return 0;
+ }
+ }
+
+ int ans = WriteBlock(blockno, bldata, KEY, use_credit_key, elite, rawkey, false, true);
+
+ DropField();
+ return ans;
+}
+
+
+static void usage_hf_iclass_clone(void) {
+ PrintAndLog("Usage: hf iclass clone f <tagfile.bin> b <first block> l <last block> k <KEY> c e|r o");
+ PrintAndLog("Options:");
+ PrintAndLog(" f <filename>: specify a filename to clone from");
+ PrintAndLog(" b <Block> : The first block to clone as 2 hex symbols");
+ PrintAndLog(" l <Last Blk>: The last block to clone as 2 hex symbols");
+ PrintAndLog(" k <Key> : Access Key as 16 hex symbols or 1 hex to select key from memory");
+ PrintAndLog(" c : If 'c' is specified, the key set is assumed to be the credit key\n");
+ PrintAndLog(" e : If 'e' is specified, elite computations applied to key");
+ PrintAndLog(" r : If 'r' is specified, no computations applied to key");
+ PrintAndLog(" o : override protection and allow modification of target blocks 0...4");
+ PrintAndLog("Samples:");
+ PrintAndLog(" hf iclass clone f iclass_tagdump-121345.bin b 06 l 1A k 1122334455667788 e");
+ PrintAndLog(" hf iclass clone f iclass_tagdump-121345.bin b 05 l 19 k 0");
+ PrintAndLog(" hf iclass clone f iclass_tagdump-121345.bin b 06 l 19 k 0 e");
+ PrintAndLog(" hf iclass clone f iclass_tagdump-121345.bin b 06 l 19 k 0 e");
+ PrintAndLog(" hf iclass clone f iclass_tagdump-121345.bin b 03 l 19 k 0 e o");
+}
+
+
+static int CmdHFiClassCloneTag(const char *Cmd) {
+ char filename[FILE_PATH_SIZE] = {0};
+ char tempStr[50]={0};
+ uint8_t KEY[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ uint8_t keyNbr = 0;
+ uint8_t fileNameLen = 0;
+ uint8_t startblock = 0;
+ uint8_t endblock = 0;
+ uint8_t dataLen = 0;
+ bool use_credit_key = false;
+ bool elite = false;
+ bool rawkey = false;
+ bool override_protection = false;
+ bool errors = false;
+ uint8_t cmdp = 0;
+
+ while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
+ switch (param_getchar(Cmd, cmdp)) {
+ case 'h':
+ case 'H':
+ usage_hf_iclass_clone();
+ return 0;
+ case 'b':
+ case 'B':
+ if (param_gethex(Cmd, cmdp+1, &startblock, 2)) {
+ PrintAndLog("Start Block No must include 2 HEX symbols\n");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 'c':
+ case 'C':
+ use_credit_key = true;
+ cmdp++;
+ break;
+ case 'e':
+ case 'E':
+ elite = true;
+ cmdp++;
+ break;
+ case 'f':
+ case 'F':
+ fileNameLen = param_getstr(Cmd, cmdp+1, filename, sizeof(filename));
+ if (fileNameLen < 1) {
+ PrintAndLog("No filename found after f");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 'k':
+ case 'K':
+ dataLen = param_getstr(Cmd, cmdp+1, tempStr, sizeof(tempStr));
+ if (dataLen == 16) {
+ errors = param_gethex(tempStr, 0, KEY, dataLen);
+ } else if (dataLen == 1) {
+ keyNbr = param_get8(Cmd, cmdp+1);
+ if (keyNbr < ICLASS_KEYS_MAX) {
+ memcpy(KEY, iClass_Key_Table[keyNbr], 8);
+ } else {
+ PrintAndLog("\nERROR: Credit KeyNbr is invalid\n");
+ errors = true;
+ }
+ } else {
+ PrintAndLog("\nERROR: Credit Key is incorrect length\n");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 'l':
+ case 'L':
+ if (param_gethex(Cmd, cmdp+1, &endblock, 2)) {
+ PrintAndLog("Last Block No must include 2 HEX symbols\n");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 'r':
+ case 'R':
+ rawkey = true;
+ cmdp++;
+ break;
+ case 'o':
+ case 'O':
+ override_protection = true;
+ cmdp++;
+ break;
+ default:
+ PrintAndLog("Unknown parameter '%c'\n", param_getchar(Cmd, cmdp));
+ errors = true;
+ break;
+ }
+ if (errors) {
+ usage_hf_iclass_clone();
+ return 0;
+ }
+ }
+
+ if (cmdp < 8) {
+ usage_hf_iclass_clone();
+ return 0;
+ }
+
+ if (startblock < 5) {
+ if (override_protection) {
+ PrintAndLog("Info: modifying keys, e-purse or configuration block.");
+ } else {
+ PrintAndLog("You are going to modify keys, e-purse or configuration block.");
+ PrintAndLog("You must add the 'o' (override) option to confirm that you know what you are doing");
+ return 0;
+ }
+ }
+
+ if ((endblock - startblock + 1) * 12 > USB_CMD_DATA_SIZE) {
+ PrintAndLog("Trying to write too many blocks at once. Max: %d", USB_CMD_DATA_SIZE/12);
+ }
+
+ // file handling and reading
+ FILE *f;
+ f = fopen(filename,"rb");
+ if (!f) {
+ PrintAndLog("Failed to read from file '%s'", filename);
+ return 1;
+ }
+
+ uint8_t tag_data[USB_CMD_DATA_SIZE/12][8];
+ fseek(f, startblock*8, SEEK_SET);
+ for (int i = 0; i < endblock - startblock + 1; i++) {
+ if (fread(&tag_data[i], 1, 8, f) == 0 ) {
+ PrintAndLog("File reading error.");
+ fclose(f);
+ return 2;
+ }
+ }
+
+ uint8_t MAC[4] = {0x00, 0x00, 0x00, 0x00};
+ uint8_t div_key[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+ uint8_t CSN[8];
+
+ if (!iClass_select(CSN, true, true, true) || !iClass_authenticate(CSN, KEY, MAC, div_key, use_credit_key, elite, rawkey, false, true)) {
+ DropField();
+ return 0;
+ }
+
+ UsbCommand w = {CMD_ICLASS_CLONE, {startblock, endblock}};
+ uint8_t *ptr;
+ // calculate MAC for every block we will write
+ for (int i = 0; i < endblock - startblock + 1; i++) {
+ Calc_wb_mac(startblock + i, tag_data[i], div_key, MAC);
+ ptr = w.d.asBytes + i * 12;
+ memcpy(ptr, tag_data[i], 8);
+ memcpy(ptr + 8, MAC, 4);
+ }
+
+ uint8_t p[12];
+ PrintAndLog("Cloning");
+ for (int i = 0; i < endblock - startblock + 1; i++){
+ memcpy(p, w.d.asBytes + (i * 12), 12);
+ PrintAndLog("Block |%02x| %02x%02x%02x%02x%02x%02x%02x%02x | MAC |%02x%02x%02x%02x|",
+ i + startblock, p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11]);
+ }
+
+ UsbCommand resp;
+ SendCommand(&w);
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) {
+ PrintAndLog("Command execute timeout");
+ DropField();
+ return 0;
+ }
+
+ DropField();
+ return 1;
+}
+
+
+static int ReadBlock(uint8_t *KEY, uint8_t blockno, uint8_t keyType, bool elite, bool rawkey, bool NRMAC_replay, bool verbose, bool auth) {
+
+ uint8_t MAC[4]={0x00,0x00,0x00,0x00};
+ uint8_t div_key[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ uint8_t CSN[8];
+
+ if (!iClass_select(CSN, verbose, true, true)) {
+ DropField();
+ return 0;
+ }
+
+ if (auth) {
+ if (!iClass_authenticate(CSN, KEY, MAC, div_key, (keyType==0x18), elite, rawkey, NRMAC_replay, verbose)) {
+ DropField();
+ return 0;
+ }
+ }
+
+ UsbCommand resp;
+ UsbCommand w = {CMD_ICLASS_READBLOCK, {blockno}};
+ clearCommandBuffer();
+ SendCommand(&w);
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) {
+ PrintAndLog("Command execute timeout");
+ DropField();
+ return 0;
+ }
+ bool isOK = resp.arg[0];
+ if (!isOK) {
+ PrintAndLog("Read Block Failed");
+ DropField();
+ return 0;
+ }
+ //data read is stored in: resp.d.asBytes[0-15]
+ if (verbose)
+ PrintAndLog("Block %02X: %s\n",blockno, sprint_hex(resp.d.asBytes,8));
+
+ return 1;
+}
+
+
+static void usage_hf_iclass_readblock(void) {
+ PrintAndLog("Usage: hf iclass readblk b <Block> k <Key> [c] [e|r|n]\n");
+ PrintAndLog("Options:");
+ PrintAndLog(" b <Block> : The block number as 2 hex symbols");
+ PrintAndLog(" k <Key> : Access Key as 16 hex symbols or 1 hex to select key from memory");
+ PrintAndLog(" c : If 'c' is specified, the key set is assumed to be the credit key\n");
+ PrintAndLog(" e : If 'e' is specified, elite computations applied to key");
+ PrintAndLog(" r : If 'r' is specified, no computations applied to key");
+ PrintAndLog(" n : If 'n' is specified, <Key> specifies a NR/MAC pair which can be obtained by 'hf iclass snoop'");
+ PrintAndLog("Samples:");
+ PrintAndLog(" hf iclass readblk b 06 k 0011223344556677");
+ PrintAndLog(" hf iclass readblk b 1B k 0011223344556677 c");
+ PrintAndLog(" hf iclass readblk b 0A k 0");
+}
+
+
+static int CmdHFiClass_ReadBlock(const char *Cmd) {
+ uint8_t blockno=0;
+ uint8_t keyType = 0x88; //debit key
+ uint8_t KEY[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ uint8_t keyNbr = 0;
+ uint8_t dataLen = 0;
+ char tempStr[50] = {0};
+ bool elite = false;
+ bool rawkey = false;
+ bool NRMAC_replay = false;
+ bool errors = false;
+ bool auth = false;
+ uint8_t cmdp = 0;
+
+ while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
+ switch (param_getchar(Cmd, cmdp)) {
+ case 'h':
+ case 'H':
+ usage_hf_iclass_readblock();
+ return 0;
+ case 'b':
+ case 'B':
+ if (param_gethex(Cmd, cmdp+1, &blockno, 2)) {
+ PrintAndLog("Block No must include 2 HEX symbols\n");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 'c':
+ case 'C':
+ keyType = 0x18;
+ cmdp++;
+ break;
+ case 'e':
+ case 'E':
+ elite = true;
+ cmdp++;
+ break;
+ case 'k':
+ case 'K':
+ auth = true;
+ dataLen = param_getstr(Cmd, cmdp+1, tempStr, sizeof(tempStr));
+ if (dataLen == 16) {
+ errors = param_gethex(tempStr, 0, KEY, dataLen);
+ } else if (dataLen == 1) {
+ keyNbr = param_get8(Cmd, cmdp+1);
+ if (keyNbr < ICLASS_KEYS_MAX) {
+ memcpy(KEY, iClass_Key_Table[keyNbr], 8);
+ } else {
+ PrintAndLog("\nERROR: KeyNbr is invalid\n");
+ errors = true;
+ }
+ } else {
+ PrintAndLog("\nERROR: Key is incorrect length\n");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 'r':
+ case 'R':
+ rawkey = true;
+ cmdp++;
+ break;
+ case 'n':
+ case 'N':
+ NRMAC_replay = true;
+ cmdp++;
+ break;
+ default:
+ PrintAndLog("Unknown parameter '%c'\n", param_getchar(Cmd, cmdp));
+ errors = true;
+ break;
+ }
+ }
+
+ if (elite + rawkey + NRMAC_replay > 1) {
+ PrintAndLog("You cannot combine the 'e', 'r', and 'n' options\n");
+ errors = true;
+ }
+
+ if (errors) {
+ usage_hf_iclass_readblock();
+ return 0;
+ }
+
+ if (cmdp < 2) {
+ usage_hf_iclass_readblock();
+ return 0;
+ }
+ if (!auth)
+ PrintAndLog("warning: no authentication used with read, only a few specific blocks can be read accurately without authentication.");
+
+ return ReadBlock(KEY, blockno, keyType, elite, rawkey, NRMAC_replay, true, auth);
+}
+
+
+static int CmdHFiClass_loclass(const char *Cmd) {
+ char opt = param_getchar(Cmd, 0);
+
+ if (strlen(Cmd)<1 || opt == 'h') {
+ PrintAndLog("Usage: hf iclass loclass [options]");
+ PrintAndLog("Options:");
+ PrintAndLog("h Show this help");
+ PrintAndLog("t Perform self-test");
+ PrintAndLog("f <filename> Bruteforce iclass dumpfile");
+ PrintAndLog(" An iclass dumpfile is assumed to consist of an arbitrary number of");
+ PrintAndLog(" malicious CSNs, and their protocol responses");
+ PrintAndLog(" The binary format of the file is expected to be as follows: ");
+ PrintAndLog(" <8 byte CSN><8 byte CC><4 byte NR><4 byte MAC>");
+ PrintAndLog(" <8 byte CSN><8 byte CC><4 byte NR><4 byte MAC>");
+ PrintAndLog(" <8 byte CSN><8 byte CC><4 byte NR><4 byte MAC>");
+ PrintAndLog(" ... totalling N*24 bytes");
+ return 0;
+ }
+ char fileName[255] = {0};
+ if(opt == 'f') {
+ if(param_getstr(Cmd, 1, fileName, sizeof(fileName)) > 0) {
+ return bruteforceFileNoKeys(fileName);
+ } else {
+ PrintAndLog("You must specify a filename");
+ }
+ } else if(opt == 't') {
+ int errors = testCipherUtils();
+ errors += testMAC();
+ errors += doKeyTests(0);