From 496bb4be3306b86699abdab9ecf4e677fd61c92b Mon Sep 17 00:00:00 2001 From: pwpiwi Date: Wed, 13 Nov 2019 18:00:33 +0100 Subject: [PATCH] fix 'hf iclass' (#879) * add loooong timeout for UPDATE command * add flags FLAG_ICLASS_READER_INIT and FLAG_ICLASS_READER_CLEARTRACE * don't overwrite trace buffer during 'hf iclass dump' * fix long waiting time when start_time==0 in TransmitTo15693Tag() * remove some additional debug prints * refactoring: move helper functions from protocols.c to cmdhficlass.c * add 'h' and '1' options to 'hf iclass reader' (from RRG repository) * use correct key when only CreditKey is given in 'hf iclass dump' * separate select_and_auth * DropField() on errors * dump last block in 'hf iclass dump' * display correct memory size (number of blocks) in 'hf iclass reader' and dump * more whitespace fixes --- armsrc/iclass.c | 129 ++++------ armsrc/iso15693.c | 15 +- client/cmdhf.c | 2 +- client/cmdhficlass.c | 597 ++++++++++++++++++++++++++----------------- client/cmdhficlass.h | 2 +- common/protocols.c | 123 +-------- common/protocols.h | 7 +- include/usb_cmd.h | 13 +- 8 files changed, 439 insertions(+), 449 deletions(-) diff --git a/armsrc/iclass.c b/armsrc/iclass.c index 1a729f3f..430597c1 100644 --- a/armsrc/iclass.c +++ b/armsrc/iclass.c @@ -66,6 +66,7 @@ #define DELAY_ICLASS_VICC_TO_VCD_READER DELAY_ISO15693_VICC_TO_VCD_READER // times in samples @ 212kHz when acting as reader #define ICLASS_READER_TIMEOUT_ACTALL 330 // 1558us, nominal 330us + 7slots*160us = 1450us +#define ICLASS_READER_TIMEOUT_UPDATE 3390 // 16000us, nominal 4-15ms #define ICLASS_READER_TIMEOUT_OTHERS 80 // 380us, nominal 330us @@ -1338,10 +1339,10 @@ static void ReaderTransmitIClass(uint8_t *frame, int len, uint32_t *start_time) static bool sendCmdGetResponseWithRetries(uint8_t* command, size_t cmdsize, uint8_t* resp, size_t max_resp_size, - uint8_t expected_size, uint8_t retries, uint32_t start_time, uint32_t *eof_time) { + uint8_t expected_size, uint8_t retries, uint32_t start_time, uint32_t timeout, uint32_t *eof_time) { while (retries-- > 0) { ReaderTransmitIClass(command, cmdsize, &start_time); - if (expected_size == GetIso15693AnswerFromTag(resp, max_resp_size, ICLASS_READER_TIMEOUT_OTHERS, eof_time)) { + if (expected_size == GetIso15693AnswerFromTag(resp, max_resp_size, timeout, eof_time)) { return true; } } @@ -1393,7 +1394,7 @@ static bool selectIclassTag(uint8_t *card_data, uint32_t *eof_time) { // Select an iClass tag and read all blocks which are always readable without authentication -void ReaderIClass(uint8_t arg0) { +void ReaderIClass(uint8_t flags) { LED_A_ON(); @@ -1409,16 +1410,16 @@ void ReaderIClass(uint8_t arg0) { uint8_t result_status = 0; - // test flags for what blocks to be sure to read - uint8_t flagReadConfig = arg0 & FLAG_ICLASS_READER_CONF; - uint8_t flagReadCC = arg0 & FLAG_ICLASS_READER_CC; - uint8_t flagReadAA = arg0 & FLAG_ICLASS_READER_AA; + if (flags & FLAG_ICLASS_READER_INIT) { + Iso15693InitReader(); + } - set_tracing(true); - clear_trace(); - Iso15693InitReader(); + if (flags & FLAG_ICLASS_READER_CLEARTRACE) { + set_tracing(true); + clear_trace(); + StartCountSspClk(); + } - StartCountSspClk(); uint32_t start_time = 0; uint32_t eof_time = 0; @@ -1426,12 +1427,12 @@ void ReaderIClass(uint8_t arg0) { result_status = FLAG_ICLASS_READER_CSN; memcpy(card_data, resp, 8); } - + start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER; - + //Read block 1, config - if (flagReadConfig) { - if (sendCmdGetResponseWithRetries(readConf, sizeof(readConf), resp, sizeof(resp), 10, 10, start_time, &eof_time)) { + if (flags & FLAG_ICLASS_READER_CONF) { + if (sendCmdGetResponseWithRetries(readConf, sizeof(readConf), resp, sizeof(resp), 10, 10, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) { result_status |= FLAG_ICLASS_READER_CONF; memcpy(card_data+8, resp, 8); } else { @@ -1441,8 +1442,8 @@ void ReaderIClass(uint8_t arg0) { } //Read block 2, e-purse - if (flagReadCC) { - if (sendCmdGetResponseWithRetries(readEpurse, sizeof(readEpurse), resp, sizeof(resp), 10, 10, start_time, &eof_time)) { + if (flags & FLAG_ICLASS_READER_CC) { + if (sendCmdGetResponseWithRetries(readEpurse, sizeof(readEpurse), resp, sizeof(resp), 10, 10, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) { result_status |= FLAG_ICLASS_READER_CC; memcpy(card_data + (8*2), resp, 8); } else { @@ -1452,8 +1453,8 @@ void ReaderIClass(uint8_t arg0) { } //Read block 5, AA - if (flagReadAA) { - if (sendCmdGetResponseWithRetries(readAA, sizeof(readAA), resp, sizeof(resp), 10, 10, start_time, &eof_time)) { + if (flags & FLAG_ICLASS_READER_AA) { + if (sendCmdGetResponseWithRetries(readAA, sizeof(readAA), resp, sizeof(resp), 10, 10, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) { result_status |= FLAG_ICLASS_READER_AA; memcpy(card_data + (8*5), resp, 8); } else { @@ -1522,13 +1523,13 @@ void ReaderIClass_Replay(uint8_t arg0, uint8_t *MAC) { if (!selectIclassTag(card_data, &eof_time)) continue; start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER; - if (!sendCmdGetResponseWithRetries(readcheck_cc, sizeof(readcheck_cc), resp, sizeof(resp), 8, 3, start_time, &eof_time)) continue; + if (!sendCmdGetResponseWithRetries(readcheck_cc, sizeof(readcheck_cc), resp, sizeof(resp), 8, 3, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) continue; // replay captured auth (cc must not have been updated) memcpy(check+5, MAC, 4); start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER; - if (!sendCmdGetResponseWithRetries(check, sizeof(check), resp, sizeof(resp), 4, 5, start_time, &eof_time)) { + if (!sendCmdGetResponseWithRetries(check, sizeof(check), resp, sizeof(resp), 4, 5, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) { Dbprintf("Error: Authentication Fail!"); continue; } @@ -1540,7 +1541,7 @@ void ReaderIClass_Replay(uint8_t arg0, uint8_t *MAC) { read[3] = crc & 0xff; start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER; - if (!sendCmdGetResponseWithRetries(read, sizeof(read), resp, sizeof(resp), 10, 10, start_time, &eof_time)) { + if (!sendCmdGetResponseWithRetries(read, sizeof(read), resp, sizeof(resp), 10, 10, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) { start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER; Dbprintf("Dump config (block 1) failed"); continue; @@ -1567,7 +1568,7 @@ void ReaderIClass_Replay(uint8_t arg0, uint8_t *MAC) { read[3] = crc & 0xff; start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER; - if (sendCmdGetResponseWithRetries(read, sizeof(read), resp, sizeof(resp), 10, 10, start_time, &eof_time)) { + if (sendCmdGetResponseWithRetries(read, sizeof(read), resp, sizeof(resp), 10, 10, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) { Dbprintf(" %02x: %02x %02x %02x %02x %02x %02x %02x %02x", block, resp[0], resp[1], resp[2], resp[3], resp[4], resp[5], @@ -1622,11 +1623,11 @@ void ReaderIClass_Replay(uint8_t arg0, uint8_t *MAC) { void iClass_Check(uint8_t *MAC) { - uint8_t check[9] = {ICLASS_CMD_CHECK_KD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + uint8_t check[9] = {ICLASS_CMD_CHECK_KD, 0x00}; uint8_t resp[4]; memcpy(check+5, MAC, 4); uint32_t eof_time; - bool isOK = sendCmdGetResponseWithRetries(check, sizeof(check), resp, sizeof(resp), 4, 6, 0, &eof_time); + bool isOK = sendCmdGetResponseWithRetries(check, sizeof(check), resp, sizeof(resp), 4, 6, 0, ICLASS_READER_TIMEOUT_OTHERS, &eof_time); cmd_send(CMD_ACK, isOK, 0, 0, resp, sizeof(resp)); } @@ -1638,7 +1639,7 @@ void iClass_Readcheck(uint8_t block, bool use_credit_key) { } uint8_t resp[8]; uint32_t eof_time; - bool isOK = sendCmdGetResponseWithRetries(readcheck, sizeof(readcheck), resp, sizeof(resp), 8, 6, 0, &eof_time); + bool isOK = sendCmdGetResponseWithRetries(readcheck, sizeof(readcheck), resp, sizeof(resp), 8, 6, 0, ICLASS_READER_TIMEOUT_OTHERS, &eof_time); cmd_send(CMD_ACK, isOK, 0, 0, resp, sizeof(resp)); } @@ -1650,10 +1651,9 @@ static bool iClass_ReadBlock(uint8_t blockNo, uint8_t *readdata) { readcmd[2] = rdCrc >> 8; readcmd[3] = rdCrc & 0xff; uint8_t resp[10]; - bool isOK = false; uint32_t eof_time; - isOK = sendCmdGetResponseWithRetries(readcmd, sizeof(readcmd), resp, sizeof(resp), 10, 10, 0, &eof_time); + bool isOK = sendCmdGetResponseWithRetries(readcmd, sizeof(readcmd), resp, sizeof(resp), 10, 10, 0, ICLASS_READER_TIMEOUT_OTHERS, &eof_time); memcpy(readdata, resp, sizeof(resp)); return isOK; @@ -1665,8 +1665,7 @@ void iClass_ReadBlk(uint8_t blockno) { LED_A_ON(); uint8_t readblockdata[] = {0,0,0,0,0,0,0,0,0,0}; - bool isOK = false; - isOK = iClass_ReadBlock(blockno, readblockdata); + bool isOK = iClass_ReadBlock(blockno, readblockdata); cmd_send(CMD_ACK, isOK, 0, 0, readblockdata, 8); FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); LED_D_OFF(); @@ -1674,43 +1673,28 @@ void iClass_ReadBlk(uint8_t blockno) { LED_A_OFF(); } -void iClass_Dump(uint8_t blockno, uint8_t numblks) { + +void iClass_Dump(uint8_t startblock, uint8_t numblks) { LED_A_ON(); - uint8_t readblockdata[] = {0,0,0,0,0,0,0,0,0,0}; + uint8_t readblockdata[USB_CMD_DATA_SIZE+2] = {0}; bool isOK = false; - uint8_t blkCnt = 0; + uint16_t blkCnt = 0; - BigBuf_free(); - uint8_t *dataout = BigBuf_malloc(255*8); - if (dataout == NULL) { - Dbprintf("out of memory"); - FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); - LED_D_OFF(); - cmd_send(CMD_ACK, 0, 1, 0, 0, 0); - LED_A_OFF(); - return; + if (numblks > USB_CMD_DATA_SIZE / 8) { + numblks = USB_CMD_DATA_SIZE / 8; } - memset(dataout, 0xFF, 255*8); - - for ( ; blkCnt < numblks; blkCnt++) { - isOK = iClass_ReadBlock(blockno+blkCnt, readblockdata); - if (!isOK || (readblockdata[0] == 0xBB || readblockdata[7] == 0xBB || readblockdata[2] == 0xBB)) { //try again - isOK = iClass_ReadBlock(blockno+blkCnt, readblockdata); - if (!isOK) { - Dbprintf("Block %02X failed to read", blkCnt+blockno); - break; - } + + for (blkCnt = 0; blkCnt < numblks; blkCnt++) { + isOK = iClass_ReadBlock(startblock+blkCnt, readblockdata+8*blkCnt); + if (!isOK) { + Dbprintf("Block %02X failed to read", startblock+blkCnt); + break; } - memcpy(dataout + (blkCnt*8), readblockdata, 8); } - //return pointer to dump memory in arg3 - cmd_send(CMD_ACK, isOK, blkCnt, BigBuf_max_traceLen(), 0, 0); - FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); - LED_D_OFF(); - BigBuf_free(); + cmd_send(CMD_ACK, isOK, blkCnt, 0, readblockdata, blkCnt*8); LED_A_OFF(); } @@ -1718,34 +1702,18 @@ void iClass_Dump(uint8_t blockno, uint8_t numblks) { static bool iClass_WriteBlock_ext(uint8_t blockNo, uint8_t *data) { - LED_A_ON(); - - uint8_t write[] = { ICLASS_CMD_UPDATE, blockNo, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - //uint8_t readblockdata[10]; - //write[1] = blockNo; + uint8_t write[16] = {ICLASS_CMD_UPDATE, blockNo}; memcpy(write+2, data, 12); // data + mac - char *wrCmd = (char *)(write+1); - uint16_t wrCrc = iclass_crc16(wrCmd, 13); - write[14] = wrCrc >> 8; - write[15] = wrCrc & 0xff; + AppendCrc(write+1, 13); uint8_t resp[10]; bool isOK = false; uint32_t eof_time = 0; - isOK = sendCmdGetResponseWithRetries(write, sizeof(write), resp, sizeof(resp), 10, 10, 0, &eof_time); - uint32_t start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER; - if (isOK) { //if reader responded correctly - //Dbprintf("WriteResp: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",resp[0],resp[1],resp[2],resp[3],resp[4],resp[5],resp[6],resp[7],resp[8],resp[9]); - if (memcmp(write+2, resp, 8)) { //if response is not equal to write values - if (blockNo != 3 && blockNo != 4) { //if not programming key areas (note key blocks don't get programmed with actual key data it is xor data) - //error try again - isOK = sendCmdGetResponseWithRetries(write, sizeof(write), resp, sizeof(resp), 10, 10, start_time, &eof_time); - } - } + isOK = sendCmdGetResponseWithRetries(write, sizeof(write), resp, sizeof(resp), 10, 10, 0, ICLASS_READER_TIMEOUT_UPDATE, &eof_time); + if (isOK && blockNo != 3 && blockNo != 4 && memcmp(write+2, resp, 8)) { // check response + isOK = false; } - LED_A_OFF(); - return isOK; } @@ -1755,7 +1723,7 @@ void iClass_WriteBlock(uint8_t blockNo, uint8_t *data) { LED_A_ON(); bool isOK = iClass_WriteBlock_ext(blockNo, data); - if (isOK){ + if (isOK) { Dbprintf("Write block [%02x] successful", blockNo); } else { Dbprintf("Write block [%02x] failed", blockNo); @@ -1768,6 +1736,7 @@ void iClass_WriteBlock(uint8_t blockNo, uint8_t *data) { LED_A_OFF(); } + void iClass_Clone(uint8_t startblock, uint8_t endblock, uint8_t *data) { int i; int written = 0; diff --git a/armsrc/iso15693.c b/armsrc/iso15693.c index 3b39d576..bb5bdfe2 100644 --- a/armsrc/iso15693.c +++ b/armsrc/iso15693.c @@ -256,11 +256,10 @@ void TransmitTo15693Tag(const uint8_t *cmd, int len, uint32_t *start_time) { *start_time = (*start_time - DELAY_ARM_TO_TAG) & 0xfffffff0; - while (GetCountSspClk() > *start_time) { // we may miss the intended time - *start_time += 16; // next possible time + if (GetCountSspClk() > *start_time) { // we may miss the intended time + *start_time = (GetCountSspClk() + 16) & 0xfffffff0; // next possible time } - while (GetCountSspClk() < *start_time) /* wait */ ; @@ -449,11 +448,11 @@ static int inline __attribute__((always_inline)) Handle15693SamplesFromTag(uint1 DecodeTag->posCount = 2; DecodeTag->state = STATE_TAG_RECEIVING_DATA; FpgaDisableTracing(); // DEBUGGING - Dbprintf("amplitude = %d, threshold_sof = %d, threshold_half/4 = %d, previous_amplitude = %d", - amplitude, - DecodeTag->threshold_sof, - DecodeTag->threshold_half/4, - DecodeTag->previous_amplitude); // DEBUGGING + // Dbprintf("amplitude = %d, threshold_sof = %d, threshold_half/4 = %d, previous_amplitude = %d", + // amplitude, + // DecodeTag->threshold_sof, + // DecodeTag->threshold_half/4, + // DecodeTag->previous_amplitude); // DEBUGGING LED_C_ON(); } else { DecodeTag->posCount++; diff --git a/client/cmdhf.c b/client/cmdhf.c index 83170cae..5aeb7ce4 100644 --- a/client/cmdhf.c +++ b/client/cmdhf.c @@ -50,7 +50,7 @@ int CmdHFSearch(const char *Cmd){ PrintAndLog("\nValid ISO14443A Tag Found - Quiting Search\n"); return ans; } - ans = HFiClassReader("", false, false); + ans = HFiClassReader(false, false); if (ans) { PrintAndLog("\nValid iClass Tag (or PicoPass Tag) Found - Quiting Search\n"); return ans; diff --git a/client/cmdhficlass.c b/client/cmdhficlass.c index 195a282d..229af0e1 100644 --- a/client/cmdhficlass.c +++ b/client/cmdhficlass.c @@ -54,6 +54,106 @@ typedef struct iclass_block { } iclass_block_t; +// iclass / picopass chip config structures and shared routines +typedef struct { + uint8_t app_limit; //[8] + uint8_t otp[2]; //[9-10] + uint8_t block_writelock;//[11] + uint8_t chip_config; //[12] + uint8_t mem_config; //[13] + uint8_t eas; //[14] + uint8_t fuses; //[15] +} picopass_conf_block; + +typedef struct { + uint8_t csn[8]; + picopass_conf_block conf; + uint8_t epurse[8]; + uint8_t key_d[8]; + uint8_t key_c[8]; + uint8_t app_issuer_area[8]; +} picopass_hdr; + + +static void fuse_config(const picopass_hdr *hdr) { + uint8_t fuses = hdr->conf.fuses; + + if (fuses & FUSE_FPERS) + PrintAndLog(" Mode: Personalization [Programmable]"); + else + PrintAndLog(" Mode: Application [Locked]"); + + if (fuses & FUSE_CODING1) + PrintAndLog("Coding: RFU"); + else { + if (fuses & FUSE_CODING0) + PrintAndLog("Coding: ISO 14443-2 B/ISO 15693"); + else + PrintAndLog("Coding: ISO 14443B only"); + } + if ((fuses & FUSE_CRYPT1) && (fuses & FUSE_CRYPT0)) PrintAndLog(" Crypt: Secured page, keys not locked"); + if ((fuses & FUSE_CRYPT1) && !(fuses & FUSE_CRYPT0)) PrintAndLog(" Crypt: Secured page, keys locked"); + if (!(fuses & FUSE_CRYPT1) && (fuses & FUSE_CRYPT0)) PrintAndLog(" Crypt: Non secured page"); + if (!(fuses & FUSE_CRYPT1) && !(fuses & FUSE_CRYPT0)) PrintAndLog(" Crypt: No auth possible. Read only if RA is enabled"); + + if (fuses & FUSE_RA) + PrintAndLog(" RA: Read access enabled"); + else + PrintAndLog(" RA: Read access not enabled"); +} + + +static void getMemConfig(uint8_t mem_cfg, uint8_t chip_cfg, uint8_t *max_blk, uint8_t *app_areas, uint8_t *kb) { + // mem-bit 5, mem-bit 7, chip-bit 4: defines chip type + if((chip_cfg & 0x10) && !(mem_cfg & 0x80) && !(mem_cfg & 0x20)) { + *kb = 2; + *app_areas = 2; + *max_blk = 31; + } else if((chip_cfg & 0x10) && (mem_cfg & 0x80) && !(mem_cfg & 0x20)) { + *kb = 16; + *app_areas = 2; + *max_blk = 255; //16kb + } else if(!(chip_cfg & 0x10) && !(mem_cfg & 0x80) && !(mem_cfg & 0x20)) { + *kb = 16; + *app_areas = 16; + *max_blk = 255; //16kb + } else if((chip_cfg & 0x10) && (mem_cfg & 0x80) && (mem_cfg & 0x20)) { + *kb = 32; + *app_areas = 3; + *max_blk = 255; //16kb + } else if(!(chip_cfg & 0x10) && !(mem_cfg & 0x80) && (mem_cfg & 0x20)) { + *kb = 32; + *app_areas = 17; + *max_blk = 255; //16kb + } else { + *kb = 32; + *app_areas = 2; + *max_blk = 255; + } +} + + +static void mem_app_config(const picopass_hdr *hdr) { + uint8_t mem = hdr->conf.mem_config; + uint8_t chip = hdr->conf.chip_config; + uint8_t applimit = hdr->conf.app_limit; + if (applimit < 6) applimit = 26; + uint8_t kb = 2; + uint8_t app_areas = 2; + uint8_t max_blk = 31; + getMemConfig(mem, chip, &max_blk, &app_areas, &kb); + PrintAndLog(" Mem: %u KBits/%u App Areas (%u * 8 bytes) [%02X]", kb, app_areas, max_blk+1, mem); + PrintAndLog(" AA1: blocks 06-%02X", applimit); + PrintAndLog(" AA2: blocks %02X-%02X", applimit+1, max_blk); +} + + +static void printIclassDumpInfo(uint8_t* iclass_dump) { + fuse_config((picopass_hdr*)iclass_dump); + mem_app_config((picopass_hdr*)iclass_dump); +} + + static void usage_hf_iclass_chk(void) { PrintAndLog("Checkkeys loads a dictionary text file with 8byte hex keys to test authenticating against a iClass tag"); PrintAndLog("Usage: hf iclass chk [h|e|r] "); @@ -207,33 +307,34 @@ static int CmdHFiClassSim(const char *Cmd) { } -int HFiClassReader(const char *Cmd, bool loop, bool verbose) { +int HFiClassReader(bool loop, bool verbose) { + bool tagFound = false; - UsbCommand c = {CMD_READER_ICLASS, {FLAG_ICLASS_READER_CSN | - FLAG_ICLASS_READER_CC | FLAG_ICLASS_READER_CONF | FLAG_ICLASS_READER_AA} }; - // loop in client not device - else on windows have a communication error + 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 | FLAG_ICLASS_READER_AA} }; UsbCommand resp; - while(!ukbhit()){ + + while (!ukbhit()) { SendCommand(&c); if (WaitForResponseTimeout(CMD_ACK,&resp, 4500)) { uint8_t readStatus = resp.arg[0] & 0xff; uint8_t *data = resp.d.asBytes; - // no tag found or button pressed - if( (readStatus == 0 && !loop) || readStatus == 0xFF) { + // no tag found + if (readStatus == 0 && !loop) { // abort if (verbose) PrintAndLog("Quitting..."); + DropField(); return 0; } - if( readStatus & FLAG_ICLASS_READER_CSN) { + if (readStatus & FLAG_ICLASS_READER_CSN) { PrintAndLog(" CSN: %s",sprint_hex(data,8)); tagFound = true; } - if( readStatus & FLAG_ICLASS_READER_CC) { + if (readStatus & FLAG_ICLASS_READER_CC) { PrintAndLog(" CC: %s",sprint_hex(data+16,8)); } - if( readStatus & FLAG_ICLASS_READER_CONF) { + if (readStatus & FLAG_ICLASS_READER_CONF) { printIclassDumpInfo(data); } if (readStatus & FLAG_ICLASS_READER_AA) { @@ -253,12 +354,31 @@ int HFiClassReader(const char *Cmd, bool loop, bool verbose) { } if (!loop) break; } + + DropField(); return 0; } +static void usage_hf_iclass_reader(void) { + PrintAndLogEx(NORMAL, "Act as a Iclass reader. Look for iClass tags until Enter or the pm3 button is pressed\n"); + PrintAndLogEx(NORMAL, "Usage: hf iclass reader [h] [1]\n"); + PrintAndLogEx(NORMAL, "Options:"); + PrintAndLogEx(NORMAL, " h This help text"); + PrintAndLogEx(NORMAL, " 1 read only 1 tag"); + PrintAndLogEx(NORMAL, "Examples:"); + PrintAndLogEx(NORMAL, " hf iclass reader 1"); +} + + static int CmdHFiClassReader(const char *Cmd) { - return HFiClassReader(Cmd, true, true); + char cmdp = tolower(param_getchar(Cmd, 0)); + if (cmdp == 'h') { + usage_hf_iclass_reader(); + return 0; + } + bool findone = (cmdp == '1') ? false : true; + return HFiClassReader(findone, true); } @@ -514,17 +634,19 @@ static int CmdHFiClassEncryptBlk(const char *Cmd) { static void Calc_wb_mac(uint8_t blockno, uint8_t *data, uint8_t *div_key, uint8_t MAC[4]) { uint8_t WB[9]; WB[0] = blockno; - memcpy(WB + 1,data,8); - doMAC_N(WB,sizeof(WB),div_key,MAC); + memcpy(WB+1, data, 8); + doMAC_N(WB, sizeof(WB), div_key, MAC); //printf("Cal wb mac block [%02x][%02x%02x%02x%02x%02x%02x%02x%02x] : MAC [%02x%02x%02x%02x]",WB[0],WB[1],WB[2],WB[3],WB[4],WB[5],WB[6],WB[7],WB[8],MAC[0],MAC[1],MAC[2],MAC[3]); } -static bool select_only(uint8_t *CSN, bool verbose) { - UsbCommand resp; +static bool iClass_select(uint8_t *CSN, bool verbose, bool cleartrace, bool init) { - UsbCommand c = {CMD_READER_ICLASS, {0}}; + UsbCommand c = {CMD_READER_ICLASS, {FLAG_ICLASS_READER_CSN}}; + if (init) c.arg[0] |= FLAG_ICLASS_READER_INIT; + if (cleartrace) c.arg[0] |= FLAG_ICLASS_READER_CLEARTRACE; + UsbCommand resp; clearCommandBuffer(); SendCommand(&c); if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) { @@ -535,9 +657,8 @@ static bool select_only(uint8_t *CSN, bool verbose) { uint8_t isOK = resp.arg[0] & 0xff; uint8_t *data = resp.d.asBytes; - memcpy(CSN, data, 8); - - if (isOK > 0) { + if (isOK & FLAG_ICLASS_READER_CSN) { + memcpy(CSN, data, 8); if (verbose) PrintAndLog("CSN: %s", sprint_hex(CSN, 8)); } else { PrintAndLog("Failed to select card! Aborting"); @@ -567,12 +688,7 @@ static void HFiClassCalcDivKey(uint8_t *CSN, uint8_t *KEY, uint8_t *div_key, boo } -static bool select_and_auth(uint8_t *KEY, uint8_t *MAC, uint8_t *div_key, bool use_credit_key, bool elite, bool rawkey, bool verbose) { - uint8_t CSN[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; - uint8_t CCNR[12] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; - - if (!select_only(CSN, verbose)) - return false; +static bool iClass_authenticate(uint8_t *CSN, uint8_t *KEY, uint8_t *MAC, uint8_t *div_key, bool use_credit_key, bool elite, bool rawkey, bool verbose) { //get div_key if (rawkey) @@ -597,7 +713,10 @@ static bool select_and_auth(uint8_t *KEY, uint8_t *MAC, uint8_t *div_key, bool u if (verbose) PrintAndLog("Couldn't get Card Challenge"); return false; } + + uint8_t CCNR[12]; memcpy(CCNR, resp.d.asBytes, 8); + memset(CCNR+8, 0x00, 4); // NR = {0, 0, 0, 0} doMAC(CCNR, div_key, MAC); @@ -673,7 +792,7 @@ static int CmdHFiClassReader_Dump(const char *Cmd) { 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 numblks = 0; + uint8_t AA1_maxBlk = 0; uint8_t maxBlk = 31; uint8_t app_areas = 1; uint8_t kb = 2; @@ -690,97 +809,101 @@ static int CmdHFiClassReader_Dump(const char *Cmd) { bool elite = false; bool rawkey = false; bool errors = false; + bool verbose = false; uint8_t cmdp = 0; - while(param_getchar(Cmd, cmdp) != 0x00) - { - 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); + 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 KeyNbr is invalid\n"); + PrintAndLog("\nERROR: Credit Key is incorrect length\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); + 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: Credit KeyNbr is invalid\n"); + errors = true; + } } else { - PrintAndLog("\nERROR: Credit KeyNbr is invalid\n"); + PrintAndLog("\nERROR: Credit Key is incorrect length\n"); errors = true; } - } else { - PrintAndLog("\nERROR: Credit Key is incorrect length\n"); + cmdp += 2; + break; + case 'r': + case 'R': + rawkey = true; + cmdp++; + break; + case 'v': + case 'V': + verbose = true; + cmdp++; + break; + default: + PrintAndLog("Unknown parameter '%c'\n", param_getchar(Cmd, cmdp)); errors = true; - } - cmdp += 2; - break; - case 'r': - case 'R': - rawkey = true; - cmdp++; - break; - default: - PrintAndLog("Unknown parameter '%c'\n", param_getchar(Cmd, cmdp)); - errors = true; - break; - } - if (errors) { - usage_hf_iclass_dump(); - return 0; + break; } } - if (cmdp < 2) { + if (errors || cmdp < 2) { usage_hf_iclass_dump(); return 0; } - // if no debit key given try credit key on AA1 (not for iclass but for some picopass this will work) - if (!have_debit_key && have_credit_key) use_credit_key = true; - //get config and first 3 blocks - UsbCommand c = {CMD_READER_ICLASS, {FLAG_ICLASS_READER_CSN | FLAG_ICLASS_READER_CONF}}; + // 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) { + use_credit_key = true; + 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[255*8]; + uint8_t tag_data[256*8]; clearCommandBuffer(); SendCommand(&c); @@ -789,150 +912,150 @@ static int CmdHFiClassReader_Dump(const char *Cmd) { 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 == 0){ - PrintAndLog("No tag found..."); - DropField(); + if (readStatus != status_mask) { + PrintAndLog("No tag found ..."); return 0; - } - if( readStatus & (FLAG_ICLASS_READER_CSN|FLAG_ICLASS_READER_CONF|FLAG_ICLASS_READER_CC)){ + } else { memcpy(tag_data, data, 8*3); - blockno+=2; // 2 to force re-read of block 2 later. (seems to respond differently..) - numblks = data[8]; + 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 (numblks > maxBlk) numblks = maxBlk; - } - DropField(); - // authenticate debit key and get div_key - later store in dump block 3 - if (!select_and_auth(KEY, MAC, div_key, use_credit_key, elite, rawkey, false)){ - //try twice - for some reason it sometimes fails the first time... - if (!select_and_auth(KEY, MAC, div_key, use_credit_key, elite, rawkey, false)){ - DropField(); - return 0; - } + if (AA1_maxBlk > maxBlk) AA1_maxBlk = maxBlk; } - // begin dump - UsbCommand w = {CMD_ICLASS_DUMP, {blockno, numblks-blockno+1}}; - clearCommandBuffer(); - SendCommand(&w); - if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) { - PrintAndLog("Command execute time-out 1"); - DropField(); - return 1; - } - uint32_t blocksRead = resp.arg[1]; - uint8_t isOK = resp.arg[0] & 0xff; - if (!isOK && !blocksRead) { - PrintAndLog("Read Block Failed"); + // authenticate 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, use_credit_key, elite, rawkey, verbose)){ DropField(); return 0; } - uint32_t startindex = resp.arg[2]; - if (blocksRead*8 > sizeof(tag_data)-(blockno*8)) { - PrintAndLog("Data exceeded Buffer size!"); - blocksRead = (sizeof(tag_data)/8) - blockno; + + // 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); } - // response ok - now get bigbuf content of the dump - GetFromBigBuf(tag_data+(blockno*8), blocksRead*8, startindex, NULL, -1, false); - size_t gotBytes = blocksRead*8 + blockno*8; - // try AA2 - if (have_credit_key) { - //turn off hf field before authenticating with different key - DropField(); - memset(MAC,0,4); - // AA2 authenticate credit key and git c_div_key - later store in dump block 4 - if (!select_and_auth(CreditKEY, MAC, c_div_key, true, false, false, false)){ - //try twice - for some reason it sometimes fails the first time... - if (!select_and_auth(CreditKEY, MAC, c_div_key, true, false, false, false)){ + // 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, verbose)){ DropField(); return 0; } } - // do we still need to read more block? (aa2 enabled?) - if (maxBlk > blockno+numblks+1) { - // setup dump and start - w.arg[0] = blockno + blocksRead; - w.arg[1] = maxBlk - (blockno + blocksRead); + 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 timeout 2"); + PrintAndLog("Command execute time-out 1"); DropField(); - return 0; + return 1; } - uint8_t isOK = resp.arg[0] & 0xff; blocksRead = resp.arg[1]; - if (!isOK && !blocksRead) { - PrintAndLog("Read Block Failed 2"); + bool isOK = resp.arg[0]; + if (!isOK) { + PrintAndLog("Reading AA2 block failed"); DropField(); return 0; } - - startindex = resp.arg[2]; - if (blocksRead*8 > sizeof(tag_data)-gotBytes) { - PrintAndLog("Data exceeded Buffer size!"); - blocksRead = (sizeof(tag_data) - gotBytes)/8; - } - // get dumped data from bigbuf - GetFromBigBuf(tag_data+gotBytes, blocksRead*8, startindex, NULL, -1, false); - - gotBytes += blocksRead*8; - } else { //field is still on - turn it off... - DropField(); + 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); - if (have_credit_key) memcpy(tag_data+(4*8),c_div_key,8); + 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, (gotBytes/8), gotBytes); + printIclassDumpContents(tag_data, 1, blockno-1, blockno*8); - if (filename[0] == 0){ + 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", gotBytes/8); - saveFile(filename, "bin", tag_data, gotBytes); + 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 verbose) { - uint8_t MAC[4]={0x00,0x00,0x00,0x00}; - uint8_t div_key[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; - if (!select_and_auth(KEY, MAC, div_key, use_credit_key, elite, rawkey, 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, verbose)) { + DropField(); return 0; + } UsbCommand resp; - Calc_wb_mac(blockno,bldata,div_key,MAC); + 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)) - { + if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) { PrintAndLog("Write Command execute timeout"); + DropField(); return 0; } - uint8_t isOK = resp.arg[0] & 0xff; + bool isOK = resp.arg[0]; if (!isOK) { PrintAndLog("Write Block Failed"); + DropField(); return 0; } + PrintAndLog("Write Block Successful"); return 1; } @@ -988,9 +1111,8 @@ static int CmdHFiClass_WriteBlock(const char *Cmd) { break; case 'd': case 'D': - if (param_gethex(Cmd, cmdp+1, bldata, 16)) - { - PrintAndLog("KEY must include 16 HEX symbols\n"); + if (param_gethex(Cmd, cmdp+1, bldata, 16)) { + PrintAndLog("Data must include 16 HEX symbols\n"); errors = true; } cmdp += 2; @@ -1029,7 +1151,7 @@ static int CmdHFiClass_WriteBlock(const char *Cmd) { errors = true; break; } - if(errors) { + if (errors) { usage_hf_iclass_writeblock(); return 0; } @@ -1168,7 +1290,7 @@ static int CmdHFiClassCloneTag(const char *Cmd) { } // file handling and reading f = fopen(filename,"rb"); - if(!f) { + if (!f) { PrintAndLog("Failed to read from file '%s'", filename); return 1; } @@ -1183,8 +1305,8 @@ static int CmdHFiClassCloneTag(const char *Cmd) { // then copy to usbcommand->asbytes; the max is 32 - 6 = 24 block 12 bytes each block 288 bytes then we can only accept to clone 21 blocks at the time, // else we have to create a share memory int i; - fseek(f,startblock*8,SEEK_SET); - if ( fread(tag_data,sizeof(iclass_block_t),endblock - startblock + 1,f) == 0 ) { + fseek(f, startblock*8, SEEK_SET); + if (fread(tag_data, sizeof(iclass_block_t), endblock - startblock + 1, f) == 0 ) { PrintAndLog("File reading error."); fclose(f); return 2; @@ -1192,15 +1314,18 @@ static int CmdHFiClassCloneTag(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 CSN[8]; - if (!select_and_auth(KEY, MAC, div_key, use_credit_key, elite, rawkey, true)) + if (!iClass_select(CSN, true, false, false) || !iClass_authenticate(CSN, KEY, MAC, div_key, use_credit_key, elite, rawkey, true)) { + DropField(); return 0; + } - UsbCommand w = {CMD_ICLASS_CLONE,{startblock,endblock}}; + UsbCommand w = {CMD_ICLASS_CLONE, {startblock, endblock}}; uint8_t *ptr; // calculate all mac for every the block we will write for (i = startblock; i <= endblock; i++){ - Calc_wb_mac(i,tag_data[i - startblock].d,div_key,MAC); + Calc_wb_mac(i, tag_data[i - startblock].d, div_key, MAC); // usb command d start pointer = d + (i - 6) * 12 // memcpy(pointer,tag_data[i - 6],8) 8 bytes // memcpy(pointer + 8,mac,sizoof(mac) 4 bytes; @@ -1216,11 +1341,13 @@ static int CmdHFiClassCloneTag(const char *Cmd) { printf(" %02x%02x%02x%02x%02x%02x%02x%02x |",p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7]); printf(" MAC |%02x%02x%02x%02x|\n",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; } return 1; @@ -1231,14 +1358,18 @@ static int ReadBlock(uint8_t *KEY, uint8_t blockno, uint8_t keyType, bool elite, 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 (!select_and_auth(KEY, MAC, div_key, (keyType==0x18), elite, rawkey, verbose)) - return 0; - } else { - uint8_t CSN[8]; - if (!select_only(CSN, verbose)) + if (!iClass_authenticate(CSN, KEY, MAC, div_key, (keyType==0x18), elite, rawkey, verbose)) { + DropField(); return 0; + } } UsbCommand resp; @@ -1247,15 +1378,19 @@ static int ReadBlock(uint8_t *KEY, uint8_t blockno, uint8_t keyType, bool elite, SendCommand(&w); if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) { PrintAndLog("Command execute timeout"); + DropField(); return 0; } - uint8_t isOK = resp.arg[0] & 0xff; + 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)); + if (verbose) + PrintAndLog("Block %02X: %s\n",blockno, sprint_hex(resp.d.asBytes,8)); + return 1; } @@ -1604,8 +1739,11 @@ static int CmdHFiClassCalcNewKey(const char *Cmd) { } if (!givenCSN) - if (!select_only(CSN, true)) + if (!iClass_select(CSN, true, true, true)) { + DropField(); return 0; + } + DropField(); HFiClassCalcNewKey(CSN, OLDKEY, NEWKEY, xor_div_key, elite, oldElite, true); return 0; @@ -1893,41 +2031,44 @@ static int CmdHFiClassCheckKeys(const char *Cmd) { // time uint64_t t1 = msclock(); - for (uint32_t c = 0; c < keycnt; c += 1) { - printf("."); fflush(stdout); - if (ukbhit()) { - int gc = getchar(); (void)gc; - printf("\naborted via keyboard!\n"); - break; - } + uint8_t CSN[8]; + if (!iClass_select(CSN, false, true, true)) { + PrintAndLog("Couldn't select card"); + DropField(); + return 0; + } - memcpy(key, keyBlock + 8 * c , 8); + for (uint32_t c = 0; c < keycnt; c++) { + printf("."); fflush(stdout); + if (ukbhit()) { + int gc = getchar(); (void)gc; + printf("\naborted via keyboard!\n"); + break; + } - // debit key. try twice - for (int foo = 0; foo < 2 && !found_debit; foo++) { - if (!select_and_auth(key, mac, div_key, false, use_elite, use_raw, false)) - continue; + memcpy(key, keyBlock + 8 * c , 8); - // key found. - PrintAndLog("\n--------------------------------------------------------"); - PrintAndLog(" Found AA1 debit key\t\t[%s]", sprint_hex(key, 8)); - found_debit = true; - } + // debit key + if (!iClass_authenticate(CSN, key, mac, div_key, false, use_elite, use_raw, false)) + continue; - // credit key. try twice - for (int foo = 0; foo < 2 && !found_credit; foo++) { - if (!select_and_auth(key, mac, div_key, true, use_elite, use_raw, false)) - continue; + // key found. + PrintAndLog("\n--------------------------------------------------------"); + PrintAndLog(" Found AA1 debit key\t\t[%s]", sprint_hex(key, 8)); + found_debit = true; - // key found - PrintAndLog("\n--------------------------------------------------------"); - PrintAndLog(" Found AA2 credit key\t\t[%s]", sprint_hex(key, 8)); - found_credit = true; - } + // credit key + if (!iClass_authenticate(CSN, key, mac, div_key, true, use_elite, use_raw, false)) + continue; - // both keys found. - if ( found_debit && found_credit ) - break; + // key found + PrintAndLog("\n--------------------------------------------------------"); + PrintAndLog(" Found AA2 credit key\t\t[%s]", sprint_hex(key, 8)); + found_credit = true; + + // both keys found. + if (found_debit && found_credit) + break; } t1 = msclock() - t1; diff --git a/client/cmdhficlass.h b/client/cmdhficlass.h index 09103b33..1362ba01 100644 --- a/client/cmdhficlass.h +++ b/client/cmdhficlass.h @@ -13,6 +13,6 @@ #define CMDHFICLASS_H__ int CmdHFiClass(const char *Cmd); -int HFiClassReader(const char *Cmd, bool loop, bool verbose); +int HFiClassReader(bool loop, bool verbose); #endif diff --git a/common/protocols.c b/common/protocols.c index 795f3eaa..4b2a19f1 100644 --- a/common/protocols.c +++ b/common/protocols.c @@ -1,7 +1,5 @@ -#include -#include -#include #include "protocols.h" +#include // ATA55xx shared presets & routines uint32_t GetT55xxClockBit(uint32_t clock) { @@ -26,122 +24,3 @@ uint32_t GetT55xxClockBit(uint32_t clock) { return 0; } } - -#ifndef ON_DEVICE -#include "ui.h" -#define prnt PrintAndLog - -// iclass / picopass chip config structures and shared routines -typedef struct { - uint8_t app_limit; //[8] - uint8_t otp[2]; //[9-10] - uint8_t block_writelock;//[11] - uint8_t chip_config; //[12] - uint8_t mem_config; //[13] - uint8_t eas; //[14] - uint8_t fuses; //[15] -} picopass_conf_block; - -typedef struct { - uint8_t csn[8]; - picopass_conf_block conf; - uint8_t epurse[8]; - uint8_t key_d[8]; - uint8_t key_c[8]; - uint8_t app_issuer_area[8]; -} picopass_hdr; - -uint8_t isset(uint8_t val, uint8_t mask) { - return (val & mask); -} - -uint8_t notset(uint8_t val, uint8_t mask) { - return !(val & mask); -} - -void fuse_config(const picopass_hdr *hdr) { - uint8_t fuses = hdr->conf.fuses; - - if (isset(fuses,FUSE_FPERS))prnt(" Mode: Personalization [Programmable]"); - else prnt(" Mode: Application [Locked]"); - - if (isset(fuses, FUSE_CODING1)) - prnt("Coding: RFU"); - else - { - if( isset( fuses , FUSE_CODING0)) prnt("Coding: ISO 14443-2 B/ISO 15693"); - else prnt("Coding: ISO 14443B only"); - } - if( isset (fuses,FUSE_CRYPT1 | FUSE_CRYPT0 )) prnt(" Crypt: Secured page, keys not locked"); - if( isset (fuses,FUSE_CRYPT1) && notset( fuses, FUSE_CRYPT0 )) prnt(" Crypt: Secured page, keys not locked"); - if( notset (fuses,FUSE_CRYPT1) && isset( fuses, FUSE_CRYPT0 )) prnt(" Crypt: Non secured page"); - if( notset (fuses,FUSE_CRYPT1) && notset( fuses, FUSE_CRYPT0 )) prnt(" Crypt: No auth possible. Read only if RA is enabled"); - - if( isset( fuses, FUSE_RA)) prnt(" RA: Read access enabled"); - else prnt(" RA: Read access not enabled"); -} - -void getMemConfig(uint8_t mem_cfg, uint8_t chip_cfg, uint8_t *max_blk, uint8_t *app_areas, uint8_t *kb) { - // mem-bit 5, mem-bit 7, chip-bit 4: defines chip type - if(isset(chip_cfg, 0x10) && notset(mem_cfg, 0x80) && notset(mem_cfg, 0x20)) { - *kb = 2; - *app_areas = 2; - *max_blk = 31; - } else if(isset(chip_cfg, 0x10) && isset(mem_cfg, 0x80) && notset(mem_cfg, 0x20)) { - *kb = 16; - *app_areas = 2; - *max_blk = 255; //16kb - } else if(notset(chip_cfg, 0x10) && notset(mem_cfg, 0x80) && notset(mem_cfg, 0x20)) { - *kb = 16; - *app_areas = 16; - *max_blk = 255; //16kb - } else if(isset(chip_cfg, 0x10) && isset(mem_cfg, 0x80) && isset(mem_cfg, 0x20)) { - *kb = 32; - *app_areas = 3; - *max_blk = 255; //16kb - } else if(notset(chip_cfg, 0x10) && notset(mem_cfg, 0x80) && isset(mem_cfg, 0x20)) { - *kb = 32; - *app_areas = 17; - *max_blk = 255; //16kb - } else { - *kb = 32; - *app_areas = 2; - *max_blk = 255; - } -} - -void mem_app_config(const picopass_hdr *hdr) { - uint8_t mem = hdr->conf.mem_config; - uint8_t chip = hdr->conf.chip_config; - uint8_t applimit = hdr->conf.app_limit; - if (applimit < 6) applimit = 26; - uint8_t kb = 2; - uint8_t app_areas = 2; - uint8_t max_blk = 31; - getMemConfig(mem, chip, &max_blk, &app_areas, &kb); - prnt(" Mem: %u KBits/%u App Areas (%u * 8 bytes) [%02X]", kb, app_areas, max_blk, mem); - prnt(" AA1: blocks 06-%02X", applimit); - prnt(" AA2: blocks %02X-%02X", applimit+1, max_blk); -} -void print_picopass_info(const picopass_hdr *hdr) { - fuse_config(hdr); - mem_app_config(hdr); -} -void printIclassDumpInfo(uint8_t* iclass_dump) { - print_picopass_info((picopass_hdr *) iclass_dump); -} - -/* -void test() { - picopass_hdr hdr = {0x27,0xaf,0x48,0x01,0xf9,0xff,0x12,0xe0,0x12,0xff,0xff,0xff,0x7f,0x1f,0xff,0x3c}; - prnt("Picopass configuration:"); - print_picopass_info(&hdr); -} -int main(int argc, char *argv[]) { - test(); - return 0; -} -*/ - -#endif -//ON_DEVICE diff --git a/common/protocols.h b/common/protocols.h index 703855f6..9a854363 100644 --- a/common/protocols.h +++ b/common/protocols.h @@ -1,6 +1,9 @@ #ifndef PROTOCOLS_H #define PROTOCOLS_H +#include + + //The following data is taken from http://www.proxmark.org/forum/viewtopic.php?pid=13501#p13501 /* ISO14443A (usually NFC tags) @@ -304,10 +307,6 @@ NXP/Philips CUSTOM COMMANDS #define ISO7816_MAX_FRAME_SIZE 261 - -void printIclassDumpInfo(uint8_t* iclass_dump); -void getMemConfig(uint8_t mem_cfg, uint8_t chip_cfg, uint8_t *max_blk, uint8_t *app_areas, uint8_t *kb); - /* T55x7 configuration register definitions */ #define T55x7_POR_DELAY 0x00000001 #define T55x7_ST_TERMINATOR 0x00000008 diff --git a/include/usb_cmd.h b/include/usb_cmd.h index 75b7c210..0a463b22 100644 --- a/include/usb_cmd.h +++ b/include/usb_cmd.h @@ -237,11 +237,14 @@ typedef struct{ // iCLASS reader flags -#define FLAG_ICLASS_READER_CC 0x02 -#define FLAG_ICLASS_READER_CSN 0x04 -#define FLAG_ICLASS_READER_CONF 0x08 -#define FLAG_ICLASS_READER_AA 0x10 -#define FLAG_ICLASS_READER_CREDITKEY 0x40 +#define FLAG_ICLASS_READER_INIT (1<<0) +#define FLAG_ICLASS_READER_CC (1<<1) +#define FLAG_ICLASS_READER_CSN (1<<2) +#define FLAG_ICLASS_READER_CONF (1<<3) +#define FLAG_ICLASS_READER_AA (1<<4) +#define FLAG_ICLASS_READER_CREDITKEY (1<<5) +#define FLAG_ICLASS_READER_CLEARTRACE (1<<6) + // iCLASS simulation modes #define ICLASS_SIM_MODE_CSN 0 -- 2.39.5