+ uint8_t keyn[2] = {0};
+ uint8_t key[250] = {0};
+ int keylen = 0;
+
+ CLIParserInit("hf mfp rdbl",
+ "Reads several blocks from Mifare Plus card.",
+ "Usage:\n\thf mfp rdbl 0 000102030405060708090a0b0c0d0e0f -> executes authentication and read block 0 data\n"
+ "\thf mfp rdbl 1 -v -> executes authentication and shows sector 1 data with default key 0xFF..0xFF and some additional data\n");
+
+ void* argtable[] = {
+ arg_param_begin,
+ arg_lit0("vV", "verbose", "show internal data."),
+ arg_int0("nN", "count", "blocks count (by default 1).", NULL),
+ arg_lit0("bB", "keyb", "use key B (by default keyA)."),
+ arg_lit0("pP", "plain", "plain communication mode between reader and card."),
+ arg_int1(NULL, NULL, "<Block Num (0..255)>", NULL),
+ arg_str0(NULL, NULL, "<Key Value (HEX 16 bytes)>", NULL),
+ arg_param_end
+ };
+ CLIExecWithReturn(cmd, argtable, false);
+
+ bool verbose = arg_get_lit(1);
+ int blocksCount = arg_get_int_def(2, 1);
+ bool keyB = arg_get_lit(3);
+ int plain = arg_get_lit(4);
+ uint32_t blockn = arg_get_int(5);
+ CLIGetHexWithReturn(6, key, &keylen);
+ CLIParserFree();
+
+ SetVerboseMode(verbose);
+
+ if (!keylen) {
+ memmove(key, DefaultKey, 16);
+ keylen = 16;
+ }
+
+ if (blockn > 255) {
+ PrintAndLog("ERROR: <Block Num> must be in range [0..255] instead of: %d", blockn);
+ return 1;
+ }
+
+ if (keylen != 16) {
+ PrintAndLog("ERROR: <Key Value> must be 16 bytes long instead of: %d", keylen);
+ return 1;
+ }
+
+ // 3 blocks - wo iso14443-4 chaining
+ if (blocksCount > 3) {
+ PrintAndLog("ERROR: blocks count must be less than 3 instead of: %d", blocksCount);
+ return 1;
+ }
+
+ if (blocksCount > 1 && mfIsSectorTrailer(blockn)) {
+ PrintAndLog("WARNING: trailer!");
+ }
+
+ uint8_t sectorNum = mfSectorNum(blockn & 0xff);
+ uint16_t uKeyNum = 0x4000 + sectorNum * 2 + (keyB ? 1 : 0);
+ keyn[0] = uKeyNum >> 8;
+ keyn[1] = uKeyNum & 0xff;
+ if (verbose)
+ PrintAndLog("--block:%d sector[%d]:%02x key:%04x", blockn, mfNumBlocksPerSector(sectorNum), sectorNum, uKeyNum);
+
+ mf4Session session;
+ int res = MifareAuth4(&session, keyn, key, true, true, verbose);
+ if (res) {
+ PrintAndLog("Authentication error: %d", res);
+ return res;
+ }
+
+ uint8_t data[250] = {0};
+ int datalen = 0;
+ uint8_t mac[8] = {0};
+ res = MFPReadBlock(&session, plain, blockn & 0xff, blocksCount, false, false, data, sizeof(data), &datalen, mac);
+ if (res) {
+ PrintAndLog("Read error: %d", res);
+ return res;
+ }
+
+ if (datalen && data[0] != 0x90) {
+ PrintAndLog("Card read error: %02x %s", data[0], GetErrorDescription(data[0]));
+ return 6;
+ }
+
+ if (datalen != 1 + blocksCount * 16 + 8 + 2) {
+ PrintAndLog("Error return length:%d", datalen);
+ return 5;
+ }
+
+ int indx = blockn;
+ for(int i = 0; i < blocksCount; i++) {
+ PrintAndLog("data[%03d]: %s", indx, sprint_hex(&data[1 + i * 16], 16));
+ indx++;
+ if (mfIsSectorTrailer(indx) && i != blocksCount - 1){
+ PrintAndLog("data[%03d]: ------------------- trailer -------------------", indx);
+ indx++;
+ }
+ }
+
+ if (memcmp(&data[blocksCount * 16 + 1], mac, 8)) {
+ PrintAndLog("WARNING: mac not equal...");
+ PrintAndLog("MAC card: %s", sprint_hex(&data[blocksCount * 16 + 1], 8));
+ PrintAndLog("MAC reader: %s", sprint_hex(mac, 8));
+ } else {
+ if(verbose)
+ PrintAndLog("MAC: %s", sprint_hex(&data[blocksCount * 16 + 1], 8));
+ }