diff --git a/CHANGELOG.md b/CHANGELOG.md index 079cb0e1c..c255ce01f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Fixed `trace list` - Topaz commands end at the frames their code takes, ISO14443-A frames in Topaz traces and ISO15693 frames in iCLASS traces get their own CRC, five byte ISO14443-A answers and FeliCa frames get a real CRC check, frames of 256 bytes and more are checked and annotated whole (@matrix) +- Fixed `trace list` annotations - none reads past its frame, SEOS answers are no longer shown as reader commands, contactless S-blocks, 14b slots, Xerox WUP2 and the Hitag ยต field offsets are corrected (@matrix) +- Fixed `trace list -t ht2` and `-c` - a frame over 30 bytes no longer overwrites the Hitag 2 state or stops the client, a frame longer than the rows shown no longer overflows the row buffer (@matrix) +- Fixed `trace extract` - no write past the UID buffer or read past the trace, DESFire AES challenges are extracted again (@matrix) - Fixed `lf fdxb` decoding - ISO 11784:2024 and ISO 24631-6 fields and names, and many more fixes. See commit message (@matrix) - Fixed `lf fdxb clone` and `sim` - a country above 1023 or a national code above 274877906943 is refused instead of cut, `sim` sends the data block flag for `--extended 000000` (@matrix) - Added `lf fdxb clone` and `sim` `--biothermo` and `--no-datablock` - a Bio-Thermo temperature, or any `--extended` trailer, sent with the data block flag clear as Bio-Thermo chips do (@matrix) diff --git a/client/src/cmdhflist.c b/client/src/cmdhflist.c index a4e903dd3..756244712 100644 --- a/client/src/cmdhflist.c +++ b/client/src/cmdhflist.c @@ -78,7 +78,7 @@ static uint8_t *gs_mfuc_key = NULL; * 2 : Not crc-command */ -uint8_t iso14443A_CRC_check(bool isResponse, uint8_t *d, uint8_t n) { +uint8_t iso14443A_CRC_check(bool isResponse, uint8_t *d, uint16_t n) { // Neither half of a bit oriented ANTICOLLISION exchange carries a CRC: the // request stops mid byte and the answer completes it. NVB counts SEL and @@ -106,33 +106,21 @@ uint8_t iso14443A_CRC_check(bool isResponse, uint8_t *d, uint8_t n) { return 2; } - if (isResponse && (n == 5)) { + // an ANTICOLLISION answer, UID and BCC, has no CRC: its BCC tells it from + // a five byte ISO14443-4 answer + if (isResponse && (n == 5) && ((d[0] ^ d[1] ^ d[2] ^ d[3]) == d[4])) { return 2; } return check_crc(CRC_14443_A, d, n); } -uint8_t seos_CRC_check(bool isResponse, uint8_t *d, uint8_t n) { - if (n < 3) { - return 2; - } - - // 5 bytes response Card busy 0xFA have crc, the rest is most likely 14a anticollision - if ((n == 5) && (d[0] != 0xFA)) { - return 2; - } - - if (d[1] == 0x50 && - d[0] >= ISO14443A_CMD_ANTICOLL_OR_SELECT && - d[0] <= ISO14443A_CMD_ANTICOLL_OR_SELECT_3) { - return 2; - } - return check_crc(CRC_14443_A, d, n); +uint8_t seos_CRC_check(bool isResponse, uint8_t *d, uint16_t n) { + return iso14443A_CRC_check(isResponse, d, n); } -uint8_t mifare_CRC_check(bool isResponse, uint8_t *data, uint8_t len) { +uint8_t mifare_CRC_check(bool isResponse, uint8_t *data, uint16_t len) { switch (MifareAuthState) { case masNone: case masError: @@ -156,15 +144,15 @@ uint8_t mifare_CRC_check(bool isResponse, uint8_t *data, uint8_t len) { * 1 : CRC-command, CRC ok * 2 : Not crc-command */ -uint8_t iso14443B_CRC_check(uint8_t *d, uint8_t n) { +uint8_t iso14443B_CRC_check(uint8_t *d, uint16_t n) { return check_crc(CRC_14443_B, d, n); } -uint8_t iso15693_CRC_check(uint8_t *d, uint8_t n) { +uint8_t iso15693_CRC_check(uint8_t *d, uint16_t n) { return check_crc(CRC_15693, d, n); } -uint8_t felica_CRC_check(uint8_t *d, uint8_t n) { +uint8_t felica_CRC_check(uint8_t *d, uint16_t n) { return check_crc(CRC_FELICA, d, n); } @@ -177,7 +165,7 @@ uint8_t felica_CRC_check(uint8_t *d, uint8_t n) { * 1 : CRC-command, CRC ok * 2 : Not crc-command */ -uint8_t iclass_CRC_check(bool isResponse, uint8_t *d, uint8_t n) { +uint8_t iclass_CRC_check(bool isResponse, uint8_t *d, uint16_t n) { //CRC commands (and responses) are all at least 4 bytes if (n < 4) return 2; @@ -225,7 +213,7 @@ uint8_t iclass_CRC_check(bool isResponse, uint8_t *d, uint8_t n) { } -static int annotateEcp(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize) { +static int annotateEcp(char *exp, size_t size, const uint8_t *cmd, uint16_t cmdsize) { if (cmdsize < 7 || cmd[0] != ECP_HEADER) { return PM3_ESOFT; } @@ -265,7 +253,22 @@ static int annotateEcp(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsi return PM3_ESOFT; } -int applyIso14443a(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response) { +// "name(operand)", or the name alone when the frame stops before the operand +static void annotateCmdOperand(char *exp, size_t size, const char *name, bool color, const uint8_t *cmd, uint16_t cmdsize) { + if (cmdsize < 2) { + snprintf(exp, size, "%s", name); + } else if (color) { + snprintf(exp, size, "%s(" _MAGENTA_("%d") ")", name, cmd[1]); + } else { + snprintf(exp, size, "%s(%d)", name, cmd[1]); + } +} + +int applyIso14443a(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool is_response) { + + if (cmdsize == 0) { + return PM3_ESOFT; + } if (is_response == false) { if ((gs_ntag_i2c_state == 1) && (cmdsize == 6) && (memcmp(cmd + 1, "\x00\x00\x00", 3) == 0)) { @@ -334,16 +337,20 @@ int applyIso14443a(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool i snprintf(exp, size, "REQA"); break; case ISO14443A_CMD_READBLOCK: - snprintf(exp, size, "READBLOCK(" _MAGENTA_("%d") ")", cmd[1]); + annotateCmdOperand(exp, size, "READBLOCK", true, cmd, cmdsize); break; case ISO14443A_CMD_WRITEBLOCK: - snprintf(exp, size, "WRITEBLOCK(" _MAGENTA_("%d") ")", cmd[1]); + annotateCmdOperand(exp, size, "WRITEBLOCK", true, cmd, cmdsize); break; case ISO14443A_CMD_HALT: snprintf(exp, size, "HALT"); MifareAuthState = masNone; break; case ISO14443A_CMD_RATS: { + if (cmdsize < 2) { + snprintf(exp, size, "RATS"); + break; + } uint16_t fsdi2fsd[] = {16, 24, 32, 40, 48, 64, 96, 128, 256, 512, 1024, 2048, 4096, 4096, 4096, 4096}; snprintf(exp, size, "RATS - FSDI=%x (FSD=%u), CID=%x", (cmd[1] & 0xF0) >> 4, fsdi2fsd[(cmd[1] & 0xF0) >> 4], (cmd[1] & 0x0F)); break; @@ -357,10 +364,10 @@ int applyIso14443a(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool i snprintf(exp, size, "OPTIONAL TIMESLOT"); break; case MIFARE_CMD_INC: - snprintf(exp, size, "INC(" _MAGENTA_("%d") ")", cmd[1]); + annotateCmdOperand(exp, size, "INC", true, cmd, cmdsize); break; case MIFARE_CMD_DEC: - snprintf(exp, size, "DEC(" _MAGENTA_("%d") ")", cmd[1]); + annotateCmdOperand(exp, size, "DEC", true, cmd, cmdsize); break; case MIFARE_CMD_RESTORE: @@ -379,7 +386,7 @@ int applyIso14443a(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool i break; case MIFARE_CMD_TRANSFER: - snprintf(exp, size, "TRANSFER(" _MAGENTA_("%d") ")", cmd[1]); + annotateCmdOperand(exp, size, "TRANSFER", true, cmd, cmdsize); break; case MIFARE_AUTH_KEYA: { if (cmdsize > 3) { @@ -393,7 +400,7 @@ int applyIso14443a(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool i } case MIFARE_AUTH_KEYB: { MifareAuthState = masNt; - snprintf(exp, size, "AUTH-B(" _MAGENTA_("%d") ")", cmd[1]); + annotateCmdOperand(exp, size, "AUTH-B", true, cmd, cmdsize); break; } case MIFARE_MAGIC_GDM_AUTH_KEY: { @@ -404,7 +411,7 @@ int applyIso14443a(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool i break; } case MIFARE_MAGIC_GDM_WRITEBLOCK: { - snprintf(exp, size, "MAGIC WRITEBLOCK(" _MAGENTA_("%d") ")", cmd[1]); + annotateCmdOperand(exp, size, "MAGIC WRITEBLOCK", true, cmd, cmdsize); break; } case MIFARE_MAGICWUPC1: @@ -445,13 +452,17 @@ int applyIso14443a(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool i case MIFARE_ULEV1_FASTREAD : { if (cmdsize >= 3 && cmd[2] <= 0xE6) snprintf(exp, size, "READ RANGE (" _MAGENTA_("%d-%d") ")", cmd[1], cmd[2]); - else + else if (cmdsize >= 3) // outside limits, useful for some tags... snprintf(exp, size, "READ RANGE (" _MAGENTA_("%d-%d") ") (?)", cmd[1], cmd[2]); + else + snprintf(exp, size, "READ RANGE"); break; } case MIFARE_ULC_WRITE : { - if (cmd[1] < 0x21) + if (cmdsize < 2) + snprintf(exp, size, "WRITEBLOCK"); + else if (cmd[1] < 0x21) snprintf(exp, size, "WRITEBLOCK(" _MAGENTA_("%d") ")", cmd[1]); else if (cmdsize >= 6) // outside limits, useful for some tags... @@ -459,14 +470,14 @@ int applyIso14443a(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool i break; } case MIFARE_ULEV1_READ_CNT : { - if (cmd[1] < 5) + if (cmdsize > 1 && cmd[1] < 5) snprintf(exp, size, "READ CNT(" _MAGENTA_("%d") ")", cmd[1]); else snprintf(exp, size, "?"); break; } case MIFARE_ULEV1_INCR_CNT : { - if (cmd[1] < 5) { + if (cmdsize >= 5 && cmd[1] < 5) { uint32_t v = MemLeToUint3byte(cmd + 2); snprintf(exp, size, "INCR CNT(" _MAGENTA_("%d") ") %u", cmd[1], v); } else { @@ -478,7 +489,7 @@ int applyIso14443a(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool i snprintf(exp, size, "READ SIG"); break; case MIFARE_ULEV1_CHECKTEAR: - snprintf(exp, size, "CHK TEARING(" _MAGENTA_("%d") ")", cmd[1]); + annotateCmdOperand(exp, size, "CHK TEARING", true, cmd, cmdsize); break; case MIFARE_ULEV1_VCSL: snprintf(exp, size, "VCSL"); @@ -487,9 +498,9 @@ int applyIso14443a(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool i snprintf(exp, size, "WRITE SIG"); break; case MIFARE_ULNANO_LOCKSIG: { - if (cmd[1] == 0) + if (cmdsize > 1 && cmd[1] == 0) snprintf(exp, size, "UNLOCK SIG"); - else if (cmd[1] == 2) + else if (cmdsize > 1 && cmd[1] == 2) snprintf(exp, size, "LOCK SIG"); else snprintf(exp, size, "?"); @@ -543,11 +554,11 @@ int applyIso14443a(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool i return PM3_SUCCESS; } -void annotateIso14443a(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response) { +void annotateIso14443a(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool is_response) { applyIso14443a(exp, size, cmd, cmdsize, is_response); } -void annotateIclass(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool isResponse) { +void annotateIclass(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool isResponse) { enum pico_state {PICO_NONE, PICO_SELECT, PICO_AUTH_EPURSE, PICO_AUTH_MACS }; static enum pico_state curr_state = PICO_NONE; @@ -556,6 +567,10 @@ void annotateIclass(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool static uint8_t rmac[4]; static uint8_t tmac[4]; + if (cmdsize == 0) { + return; + } + if (isResponse == false) { uint8_t c = cmd[0] & 0x0F; // uint8_t parity = 0; @@ -582,6 +597,10 @@ void annotateIclass(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool break; case ICLASS_CMD_CHECK: curr_state = PICO_AUTH_MACS; + if (cmdsize < 9) { + snprintf(exp, size, "CHECK"); + break; + } memcpy(rmac, cmd + 1, 4); memcpy(tmac, cmd + 5, 4); @@ -594,7 +613,7 @@ void annotateIclass(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool break; case ICLASS_CMD_READ4: - snprintf(exp, size, "READ4(%d)", cmd[1]); + annotateCmdOperand(exp, size, "READ4", false, cmd, cmdsize); break; case ICLASS_CMD_READ_OR_IDENTIFY: { @@ -606,19 +625,19 @@ void annotateIclass(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool break; } case ICLASS_CMD_PAGESEL: - snprintf(exp, size, "PAGESEL(%d)", cmd[1]); + annotateCmdOperand(exp, size, "PAGESEL", false, cmd, cmdsize); curr_state = PICO_NONE; break; case ICLASS_CMD_UPDATE: - snprintf(exp, size, "UPDATE(%d)", cmd[1]); + annotateCmdOperand(exp, size, "UPDATE", false, cmd, cmdsize); curr_state = PICO_NONE; break; case ICLASS_CMD_READCHECK: if (ICLASS_CREDIT(cmd[0])) { - snprintf(exp, size, "READCHECK[Kc](%d)", cmd[1]); + annotateCmdOperand(exp, size, "READCHECK[Kc]", false, cmd, cmdsize); curr_state = PICO_AUTH_EPURSE; } else { - snprintf(exp, size, "READCHECK[Kd](%d)", cmd[1]); + annotateCmdOperand(exp, size, "READCHECK[Kd]", false, cmd, cmdsize); curr_state = PICO_AUTH_EPURSE; } break; @@ -635,10 +654,14 @@ void annotateIclass(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool } else { if (curr_state == PICO_SELECT) { - memcpy(csn, cmd, 8); + if (cmdsize >= 8) { + memcpy(csn, cmd, 8); + } curr_state = PICO_NONE; } else if (curr_state == PICO_AUTH_EPURSE) { - memcpy(epurse, cmd, 8); + if (cmdsize >= 8) { + memcpy(epurse, cmd, 8); + } } else if (curr_state == PICO_AUTH_MACS) { snprintf(exp, size, _GREEN_("CHECK SUCCESS")); curr_state = PICO_NONE; @@ -647,7 +670,7 @@ void annotateIclass(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool return; } -void annotateIso15693(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { +void annotateIso15693(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize) { if (cmdsize >= 2) { switch (cmd[1]) { @@ -683,8 +706,10 @@ void annotateIso15693(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { case ISO15693_READ_MULTI_BLOCK: if (cmdsize == 6) { snprintf(exp, size, "READ_MULTI_BLOCK(%d-%d)", cmd[2], (cmd[2] + cmd[3])); - } else { + } else if (cmdsize == 6 + 8) { // with UID snprintf(exp, size, "READ_MULTI_BLOCK(%d-%d)", cmd[10], (cmd[10] + cmd[11])); + } else { + snprintf(exp, size, "READ_MULTI_BLOCK"); } return; case ISO15693_WRITE_MULTI_BLOCK: @@ -803,7 +828,10 @@ void annotateIso15693(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { } } -void annotateTopaz(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { +void annotateTopaz(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize) { + if (cmdsize == 0) { + return; + } switch (cmd[0]) { case TOPAZ_REQA: snprintf(exp, size, "REQA"); @@ -844,13 +872,15 @@ void annotateTopaz(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { } } +static bool annotateIso14443_s_r_block(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool is_response, bool show_block_number); + // iso 7816-3 // // contact tells the two framings apart: a contactless frame is always a block // typed by its first byte, while a contact T=0 frame is a bare APDU and a T=1 // frame is NAD PCB LEN INF EDC. Without it a GSM APDU starting with CLA 'A0' // matches the T=CL R-block test. -void annotateIso7816(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response, bool contact) { +void annotateIso7816(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool is_response, bool contact) { if (cmdsize < 2) { return; @@ -930,39 +960,7 @@ not_a_block: // S-block if (blocks && ((cmd[0] & 0xC0) == 0xC0) && ((cmdsize == 3) || (cmdsize == 4))) { - - switch ((cmd[0] & 0x3F)) { - case 0x00 : - snprintf(exp, size, "S-block RESYNCH req"); - break; - case 0x20 : - snprintf(exp, size, "S-block RESYNCH resp"); - break; - case 0x01 : - snprintf(exp, size, "S-block IFS req"); - break; - case 0x21 : - snprintf(exp, size, "S-block IFS resp"); - break; - case 0x02 : - snprintf(exp, size, "S-block ABORT req"); - break; - case 0x22 : - snprintf(exp, size, "S-block ABORT resp"); - break; - case 0x03 : - snprintf(exp, size, "S-block WTX reqt"); - break; - case 0x23 : - snprintf(exp, size, "S-block WTX resp"); - break; - case 0x32: - snprintf(exp, size, "S-block WTX req"); - break; - default : - snprintf(exp, size, "S-block"); - break; - } + annotateIso14443_s_r_block(exp, size, cmd, cmdsize, is_response, false); } // R-block (ack) else if (blocks && ((cmd[0] & 0xD0) == 0x80) && (cmdsize > 2)) { @@ -1056,7 +1054,7 @@ not_a_block: } } -static bool iso14443_4_get_i_block_inf(uint8_t *cmd, uint8_t cmdsize, bool is_response, const uint8_t **inf, size_t *inf_len) { +static bool iso14443_4_get_i_block_inf(uint8_t *cmd, uint16_t cmdsize, bool is_response, const uint8_t **inf, size_t *inf_len) { (void)is_response; size_t frame_len = cmdsize; if (frame_len < 1 || (cmd[0] & 0xC0) != 0x00 || (cmd[0] & 0x02) != 0x02) { @@ -1081,7 +1079,7 @@ static bool iso14443_4_get_i_block_inf(uint8_t *cmd, uint8_t cmdsize, bool is_re return true; } -static bool annotateIso14443_s_r_block(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response, bool show_block_number) { +static bool annotateIso14443_s_r_block(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool is_response, bool show_block_number) { (void)is_response; size_t frame_len = cmdsize; if (frame_len < 1 || frame_len > 4) { @@ -1338,7 +1336,7 @@ static bool annotateCalypsoApdu(char *exp, size_t size, const uint8_t *apdu, siz } } -static bool annotateIso14443bPrime(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, bool is_response) { +static bool annotateIso14443bPrime(char *exp, size_t size, const uint8_t *cmd, uint16_t cmdsize, bool is_response) { if (cmdsize < 2) { return false; } @@ -1398,7 +1396,7 @@ static bool annotateIso14443bPrime(char *exp, size_t size, const uint8_t *cmd, u } } -void annotateCalypso(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response) { +void annotateCalypso(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool is_response) { if (cmdsize < 1) { return; } @@ -1432,7 +1430,7 @@ void annotateCalypso(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool } // MIFARE DESFire -void annotateMfDesfire(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { +void annotateMfDesfire(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize) { // it's basically a ISO14443a tag, so try annotation from there if (applyIso14443a(exp, size, cmd, cmdsize, false) != PM3_SUCCESS) { @@ -1441,7 +1439,7 @@ void annotateMfDesfire(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { return; } // I-block 000xCN1x - else if (((cmd[0] & 0xC0) == 0x00) && (cmdsize > 2)) { + else if ((cmdsize > 2) && ((cmd[0] & 0xC0) == 0x00)) { const uint8_t *inf = NULL; if (iso14443_4_get_i_block_inf(cmd, cmdsize, false, &inf, NULL) == false) { @@ -1451,6 +1449,9 @@ void annotateMfDesfire(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { int pos = inf - cmd; for (uint8_t i = 0; i < 2; i++, pos++) { + if (pos >= cmdsize) { + break; + } bool found_annotation = true; uint8_t *data = cmd + pos + 1; @@ -1458,7 +1459,7 @@ void annotateMfDesfire(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { if (i > 0 && cmd[pos - 1] == 0x90) { data += 3; } - uint8_t data_size = 0; + uint16_t data_size = 0; if (cmdsize > (data - cmd)) { data_size = cmdsize - (data - cmd); } @@ -1590,11 +1591,11 @@ void annotateMfDesfire(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { } break; case MFDES_UPDATE_RECORD: { - snprintf(exp, size, "MFDES_UPDATE_RECORD)"); + snprintf(exp, size, "UPDATE RECORD"); break; } case MFDES_UPDATE_RECORD2: { - snprintf(exp, size, "MFDES_UPDATE_RECORD_2)"); + snprintf(exp, size, "UPDATE RECORD_2"); break; } case MFDES_NOTIFY_TRANSACTION_SUCCESS: @@ -1864,7 +1865,7 @@ const char *mfpGetCommandMacedForCode(uint8_t code) { return "MACed" ; } -void annotateMfPlus(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { +void annotateMfPlus(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize) { // If we are in Mifare Classic Authenticated mode, all the work has already be done elsewhere if ((MifareAuthState != masNone) && (MifareAuthState != masError)) { @@ -1877,7 +1878,7 @@ void annotateMfPlus(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { } // ok this part is copy paste from annotateMfDesfire, it seems to work for MIFARE Plus also - if (((cmd[0] & 0xC0) == 0x00) && (cmdsize > 2)) { + if ((cmdsize > 2) && ((cmd[0] & 0xC0) == 0x00)) { const uint8_t *inf = NULL; if (iso14443_4_get_i_block_inf(cmd, cmdsize, false, &inf, NULL) == false) { @@ -1887,6 +1888,9 @@ void annotateMfPlus(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { int pos = inf - cmd; for (uint8_t i = 0; i < 2; i++, pos++) { + if (pos >= cmdsize) { + break; + } bool found_annotation = true; uint8_t *data = cmd + pos + 1; @@ -1894,7 +1898,7 @@ void annotateMfPlus(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { if (i > 0 && cmd[pos - 1] == 0x90) { data += 3; } - uint8_t data_size = 0; + uint16_t data_size = 0; if (cmdsize > (data - cmd)) { data_size = cmdsize - (data - cmd); } @@ -2144,7 +2148,11 @@ void annotateMfPlus(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { 0F = Completion 0A 11 22 33 44 55 66 = Authenticate (11 22 33 44 55 66 = data to authenticate) **/ -void annotateIso14443b(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response) { +void annotateIso14443b(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool is_response) { + + if (cmdsize == 0) { + return; + } if (annotateIso14443bPrime(exp, size, cmd, cmdsize, is_response)) { return; @@ -2180,7 +2188,7 @@ void annotateIso14443b(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bo snprintf(exp, size, "Slot 2 NACK"); return; case 0xA7: { - snprintf(exp, size, "Slot 0 NACK"); + snprintf(exp, size, "Slot 3 NACK"); return; } default: @@ -2191,6 +2199,11 @@ void annotateIso14443b(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bo switch (cmd[0]) { case ISO14443B_REQB : { + if (cmdsize < 3) { + snprintf(exp, size, "REQB"); + break; + } + switch (cmd[2] & 0x07) { case 0: snprintf(exp, size, "1 slot "); @@ -2208,10 +2221,11 @@ void annotateIso14443b(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bo snprintf(exp, size, "16 slots "); break; } + size_t n = strlen(exp); if ((cmd[2] & 0x8)) - snprintf(exp, size, "WUPB"); + snprintf(exp + n, size - n, "WUPB"); else - snprintf(exp, size, "REQB"); + snprintf(exp + n, size - n, "REQB"); break; } case ISO14443B_ATTRIB: @@ -2224,16 +2238,16 @@ void annotateIso14443b(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bo snprintf(exp, size, "INITIATE"); break; case ISO14443B_SELECT: - snprintf(exp, size, "SELECT(%d)", cmd[1]); + annotateCmdOperand(exp, size, "SELECT", false, cmd, cmdsize); break; case ISO14443B_GET_UID: snprintf(exp, size, "GET UID"); break; case ISO14443B_READ_BLK: - snprintf(exp, size, "READ_BLK(%d)", cmd[1]); + annotateCmdOperand(exp, size, "READ_BLK", false, cmd, cmdsize); break; case ISO14443B_WRITE_BLK: - snprintf(exp, size, "WRITE_BLK(%d)", cmd[1]); + annotateCmdOperand(exp, size, "WRITE_BLK", false, cmd, cmdsize); break; case ISO14443B_RESET: snprintf(exp, size, "RESET"); @@ -2263,6 +2277,9 @@ void annotateIso14443b(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bo snprintf(exp, size, "XEROX EXT_READ_BLK(%u)", cmd[2 + 8]); break; } + // any other 02 frame, an I-block among them, is no Xerox WUP + snprintf(exp, size, "?"); + break; case ISO14443B_XEROX_WUP1: if (cmdsize == 5) { @@ -2271,7 +2288,7 @@ void annotateIso14443b(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bo } case ISO14443B_XEROX_WUP2: if (cmdsize == 5) { - snprintf(exp, size, "XEROX WUP1"); + snprintf(exp, size, "XEROX WUP2"); break; } default: @@ -2284,7 +2301,11 @@ void annotateIso14443b(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bo } // CryptoRF which is based on ISO-14443B -void annotateCryptoRF(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { +void annotateCryptoRF(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize) { + + if (cmdsize == 0) { + return; + } switch (cmd[0]) { case CRYPTORF_SET_USER_ZONE: @@ -2323,7 +2344,16 @@ void annotateCryptoRF(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { } } -void annotateSeos(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool isResponse) { +// the frame holds the bytes of apdu at pos +static bool seos_frame_matches_apdu(const uint8_t *cmd, uint16_t cmdsize, int pos, const char *apdu, size_t len) { + return (((size_t)pos + len) <= cmdsize) && (memcmp(cmd + pos, apdu, len) == 0); +} + +void annotateSeos(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool isResponse) { + + if (cmdsize == 0) { + return; + } if (cmd[0] == 0xFA && cmdsize == 5) { snprintf(exp, size, (isResponse) ? "BUSY" : "DONE?"); @@ -2331,7 +2361,7 @@ void annotateSeos(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is } // it's basically a ISO14443a tag, so try annotation from there - if (applyIso14443a(exp, size, cmd, cmdsize, false) != PM3_SUCCESS) { + if (applyIso14443a(exp, size, cmd, cmdsize, isResponse) != PM3_SUCCESS) { annotateIso7816(exp, size, cmd, cmdsize, isResponse, false); @@ -2349,100 +2379,100 @@ void annotateSeos(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is } } - if (memcmp(cmd + pos, "\x00\xA4\x04\x00", 4) == 0) { - uint8_t n = cmd[pos + 4]; + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\x00\xA4\x04\x00", 4) && (cmdsize > pos + 4)) { + uint8_t n = MIN(cmd[pos + 4], cmdsize - (pos + 5)); snprintf(exp, size, "SELECT AID " _WHITE_("%s"), sprint_hex_inrow(cmd + pos + 4 + 1, n)); // iceman: add a lookup to known SEOS AID's return; } - if (memcmp(cmd + pos, "\x80\xA5\x00\x00", 4) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\x80\xA5\x00\x00", 4)) { snprintf(exp, size, "SELECT GDF"); return; } - if (memcmp(cmd + pos, "\x80\xA5\x04\x00", 4) == 0) { - uint8_t n = cmd[pos + 4 + 2]; + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\x80\xA5\x04\x00", 4) && (cmdsize > pos + 6)) { + uint8_t n = MIN(cmd[pos + 4 + 2], cmdsize - (pos + 7)); snprintf(exp, size, "SELECT OID " _WHITE_("%s"), sprint_hex_inrow(cmd + pos + 4 + 2 + 1, n)); return; } - if (memcmp(cmd + pos, "\x80\xA5\x07", 3) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\x80\xA5\x07", 3) && (cmdsize > pos + 3)) { uint8_t ks = cmd[pos + 3]; snprintf(exp, size, "SELECT GDF " _WHITE_("(") " key " _MAGENTA_("%02X") " )", ks); return; } - if (memcmp(cmd + pos, "\x00\x87\x00", 3) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\x00\x87\x00", 3) && (cmdsize > pos + 3)) { uint8_t ks = cmd[pos + 3]; - if (memcmp(cmd + pos + 3 + 1, "\x04\x7c\x02\x81\x00", 5) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos + 3 + 1, "\x04\x7c\x02\x81\x00", 5)) { snprintf(exp, size, "GET CHALLENGE " _WHITE_("(") " key " _MAGENTA_("%02X") " )", ks); return; } - if (memcmp(cmd + pos + 3 + 1, "\x2C\x7C\x2A\x82\x28", 5) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos + 3 + 1, "\x2C\x7C\x2A\x82\x28", 5)) { snprintf(exp, size, "MUTUAL AUTHENTICATION " _WHITE_("(") " key " _MAGENTA_("%02X") " )", ks); } return; } - if (memcmp(cmd + pos, "\x0C\xCB\x3F\xFF", 4) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\x0C\xCB\x3F\xFF", 4)) { snprintf(exp, size, "GET DATA"); return; } - if (memcmp(cmd + pos, "\x0C\xDB\x3F\xFF", 4) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\x0C\xDB\x3F\xFF", 4)) { snprintf(exp, size, "UPDATE DATA"); return; } - if (memcmp(cmd + pos, "\x0C\xED\x06\x00", 4) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\x0C\xED\x06\x00", 4)) { snprintf(exp, size, "DELETE DATA"); return; } - if (memcmp(cmd + pos, "\x0C\x41\x0C\x03", 4) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\x0C\x41\x0C\x03", 4)) { snprintf(exp, size, "CREATE ADF"); return; } - if (memcmp(cmd + pos, "\x00\xD4\x00\x00\x00", 5) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\x00\xD4\x00\x00\x00", 5)) { snprintf(exp, size, "GET REMAINING CREDITS"); return; } - if (memcmp(cmd + pos, "\xA0\x10\x00\x00\x00", 5) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\xA0\x10\x00\x00\x00", 5)) { snprintf(exp, size, "GET APPLET INFO"); return; } - if (memcmp(cmd + pos, "\xA0\xD3\x00\x00\x00", 5) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\xA0\xD3\x00\x00\x00", 5)) { snprintf(exp, size, "GET APPLET VERSION"); return; } - if (memcmp(cmd + pos, "\xA0\xDA\x04", 3) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\xA0\xDA\x04", 3)) { snprintf(exp, size, "PUSH ENGINE ID"); return; } - if (memcmp(cmd + pos, "\xA0\xDA\x05", 3) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\xA0\xDA\x05", 3)) { snprintf(exp, size, "PUSH USERNAME"); return; } - if (memcmp(cmd + pos, "\xA0\xDA\x10\x00\x01\x01", 6) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\xA0\xDA\x10\x00\x01\x01", 6)) { snprintf(exp, size, "FINAL PERSONALIZE"); return; } - if (memcmp(cmd + pos, "\xA0\xDA\x25\x00\x02\x01", 6) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\xA0\xDA\x25\x00\x02\x01", 6)) { snprintf(exp, size, "UNKNOWN OPCODE ( 0x25 )"); return; } - if (memcmp(cmd + pos, "\xA0\xCA\x00\x00\x00", 5) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\xA0\xCA\x00\x00\x00", 5)) { snprintf(exp, size, "GET DATA"); return ; } @@ -2451,14 +2481,14 @@ void annotateSeos(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is // Tells the card "the next byte is a DESFire native opcode, not an ISO 7816 standard INS." // INS 0x5A DESFire native opcode SelectApplication (3-byte AID). - if (memcmp(cmd + pos, "\x90\x5A\x00\x00", 4) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\x90\x5A\x00\x00", 4) && (cmdsize > pos + 7)) { snprintf(exp, size, "(desfire) SELECT AID " _CYAN_("%02X%02X%02X"), cmd[pos + 5], cmd[pos + 6], cmd[pos + 7]); return; } if (isResponse) { - if (memcmp(cmd + pos, "\xCD\x02", 2) == 0) { + if (seos_frame_matches_apdu(cmd, cmdsize, pos, "\xCD\x02", 2) && (cmdsize > pos + 3)) { uint8_t ea = cmd[pos + 2]; uint8_t ha = cmd[pos + 3]; @@ -2489,7 +2519,10 @@ void annotateSeos(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is // 1 = read // 0 = write // Quite simpel tag -void annotateLegic(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { +void annotateLegic(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize) { + if (cmdsize < 2) { + return; + } uint8_t bitsend = cmd[0]; uint8_t cmdBit = (cmd[1] & 1); switch (bitsend) { @@ -2519,6 +2552,9 @@ void annotateLegic(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { case 9: case 11: { + if (cmdsize < 3) { + break; + } uint16_t address = (cmd[2] << 7) | cmd[1] >> 1; if (cmdBit == LEGIC_READ) { @@ -2531,7 +2567,7 @@ void annotateLegic(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { break; } case 21: { - if (cmdBit == LEGIC_WRITE) { + if ((cmdsize >= 4) && (cmdBit == LEGIC_WRITE)) { uint16_t address = ((cmd[2] << 7) | cmd[1] >> 1) & 0xFF; uint8_t val = (cmd[3] & 1) << 7 | cmd[2] >> 1; snprintf(exp, size, "WRITE Byte(%d) %02X", address, val); @@ -2539,7 +2575,7 @@ void annotateLegic(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { break; } case 23: { - if (cmdBit == LEGIC_WRITE) { + if ((cmdsize >= 4) && (cmdBit == LEGIC_WRITE)) { uint16_t address = ((cmd[2] << 7) | cmd[1] >> 1) & 0x3FF; uint8_t val = (cmd[3] & 0x7) << 5 | cmd[2] >> 3; snprintf(exp, size, "WRITE Byte(%d) %02X", address, val); @@ -2553,7 +2589,7 @@ void annotateLegic(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { } } -static bool annotateFelicaSeac(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, bool is_response) { +static bool annotateFelicaSeac(char *exp, size_t size, const uint8_t *cmd, uint16_t cmdsize, bool is_response) { if (cmdsize < 7 || cmd[4] != 0x01 || cmd[5] != 0x01 || (cmd[6] != 0x01 && cmd[6] < 0x0F)) { return false; @@ -2587,7 +2623,7 @@ static bool annotateFelicaSeac(char *exp, size_t size, const uint8_t *cmd, uint8 return true; } -void annotateFelica(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response) { +void annotateFelica(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool is_response) { if (cmdsize < 4) { snprintf(exp, size, "?"); return; @@ -3051,15 +3087,18 @@ void annotateFelica(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool } } -void annotateLTO(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { +void annotateLTO(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize) { + if (cmdsize == 0) { + return; + } switch (cmd[0]) { case LTO_REQ_STANDARD: snprintf(exp, size, "REQ Standard"); break; case LTO_SELECT: - if (cmd[1] == 0x70) + if (cmdsize > 1 && cmd[1] == 0x70) snprintf(exp, size, "SELECT_UID-2"); - else if (cmd[1] == 0x20) + else if (cmdsize > 1 && cmd[1] == 0x20) snprintf(exp, size, "SELECT"); break; case LTO_REQ_ALL: @@ -3075,7 +3114,7 @@ void annotateLTO(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { snprintf(exp, size, "READWORD"); break; case (LTO_READBLOCK & 0xF0): - snprintf(exp, size, "READBLOCK(%d)", cmd[1]); + annotateCmdOperand(exp, size, "READBLOCK", false, cmd, cmdsize); break; case LTO_READBLOCK_CONT: snprintf(exp, size, "READBLOCK CONT"); @@ -3084,7 +3123,7 @@ void annotateLTO(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { snprintf(exp, size, "WRITEWORD"); break; case (LTO_WRITEBLOCK & 0xF0): - snprintf(exp, size, "WRITEBLOCK(%d)", cmd[1]); + annotateCmdOperand(exp, size, "WRITEBLOCK", false, cmd, cmdsize); break; case LTO_HALT: snprintf(exp, size, "HALT"); @@ -3094,8 +3133,8 @@ void annotateLTO(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { } } -void annotateMifare(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, - const uint8_t *parity, uint8_t paritysize, bool isResponse) { +void annotateMifare(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, + const uint8_t *parity, uint16_t paritysize, bool isResponse) { if (!isResponse && cmdsize == 1) { switch (cmd[0]) { case ISO14443A_CMD_WUPA: @@ -3198,7 +3237,7 @@ static void mf_get_paritybinstr(char *s, uint32_t val, uint8_t par) { s[0] = '\0'; } -bool DecodeMifareData(uint8_t *cmd, uint8_t cmdsize, uint8_t *parity, bool isResponse, uint8_t *mfData, size_t *mfDataLen, const uint64_t *dicKeys, uint32_t dicKeysCount) { +bool DecodeMifareData(uint8_t *cmd, uint16_t cmdsize, uint8_t *parity, bool isResponse, uint8_t *mfData, size_t *mfDataLen, const uint64_t *dicKeys, uint32_t dicKeysCount) { static struct Crypto1State *traceCrypto1; *mfDataLen = 0; @@ -3380,7 +3419,7 @@ bool NTParityChk(AuthData_t *ad, uint32_t ntx) { return true; } -bool NestedCheckKey(uint64_t key, AuthData_t *ad, uint8_t *cmd, uint8_t cmdsize, uint8_t *parity) { +bool NestedCheckKey(uint64_t key, AuthData_t *ad, uint8_t *cmd, uint16_t cmdsize, uint8_t *parity) { uint8_t buf[32] = {0}; struct Crypto1State *pcs; @@ -3418,7 +3457,7 @@ bool NestedCheckKey(uint64_t key, AuthData_t *ad, uint8_t *cmd, uint8_t cmdsize, return true; } -bool CheckCrypto1Parity(const uint8_t *cmd_enc, uint8_t cmdsize, uint8_t *cmd, const uint8_t *parity_enc) { +bool CheckCrypto1Parity(const uint8_t *cmd_enc, uint16_t cmdsize, uint8_t *cmd, const uint8_t *parity_enc) { for (int i = 0; i < cmdsize - 1; i++) { if (oddparity8(cmd[i]) ^ (cmd[i + 1] & 0x01) ^ ((parity_enc[i / 8] >> (7 - i % 8)) & 0x01) ^ (cmd_enc[i + 1] & 0x01)) return false; @@ -3441,7 +3480,7 @@ uint64_t GetCrypto1ProbableKey(AuthData_t *ad) { } // FMCOS 2.0 -void annotateFMCOS20(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { +void annotateFMCOS20(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize) { if (cmdsize < 2) return; @@ -3459,6 +3498,13 @@ void annotateFMCOS20(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { pos = 3; break; } + + if (pos >= cmdsize) { + return; + } + // P1, 0xFF when the frame stops before it + uint8_t p1 = (pos + 1 < cmdsize) ? cmd[pos + 1] : 0xFF; + switch (cmd[pos]) { case FMCOS20_CMD_EXTERNAL_AUTHENTICATION: snprintf(exp, size, "EXT. AUTH"); @@ -3506,9 +3552,9 @@ void annotateFMCOS20(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { snprintf(exp, size, "APP UNBLOCK"); break; case FMCOS20_CMD_APP_BLOCK: - if (cmd[pos + 1] == 0) + if (p1 == 0) snprintf(exp, size, "APP BLOCK (TEMP)"); - else if (cmd[pos + 1] == 1) + else if (p1 == 1) snprintf(exp, size, "APP BLOCK (PERM)"); else snprintf(exp, size, "APP BLOCK"); @@ -3517,34 +3563,34 @@ void annotateFMCOS20(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { snprintf(exp, size, "PIN UNBLOCK"); break; case FMCOS20_CMD_CHANGE_PIN: - if (cmd[pos + 1] == 0) + if (p1 == 0) snprintf(exp, size, "RESET PIN"); - else if (cmd[pos + 1] == 1) + else if (p1 == 1) snprintf(exp, size, "CHANGE PIN"); break; case FMCOS20_CMD_INITIALIZE_TRANSACTION: - if (cmd[pos + 1] == 0) + if (p1 == 0) snprintf(exp, size, "INIT. TRANSACTION (CREDIT)"); - else if (cmd[pos + 1] == 1) + else if (p1 == 1) snprintf(exp, size, "INIT. TRANSACTION (PURCHASE)"); - else if (cmd[pos + 1] == 2) + else if (p1 == 2) snprintf(exp, size, "INIT. TRANSACTION (CASH WITHDRAW)"); - else if (cmd[pos + 1] == 3) + else if (p1 == 3) snprintf(exp, size, "INIT. TRANSACTION (CAPP PURCHASE)"); - else if (cmd[pos + 1] == 4) + else if (p1 == 4) snprintf(exp, size, "INIT. TRANSACTION (OVERDRAFT)"); - else if (cmd[pos + 1] == 5) + else if (p1 == 5) snprintf(exp, size, "INIT. TRANSACTION (WITHDRAW)"); break; case FMCOS20_CMD_CREDIT_LOAD: snprintf(exp, size, "CREDIT LOAD"); break; case FMCOS20_CMD_PURCHASE: - if (cmd[pos + 1] == 0) + if (p1 == 0) snprintf(exp, size, "PURCHASE"); - else if (cmd[pos + 1] == 1) + else if (p1 == 1) snprintf(exp, size, "CAPP PURCHASE / CASH WITHDRAW"); - else if (cmd[pos + 1] == 3) + else if (p1 == 3) snprintf(exp, size, "WITHDRAW"); break; case FMCOS20_CMD_UPDATE_OVERDRAW_LIMIT: @@ -3557,15 +3603,15 @@ void annotateFMCOS20(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) { snprintf(exp, size, "GET BALANCE"); break; case FMCOS20_CMD_INITIALIZE_GREY_LOCK_UNLOCK: - if (cmd[pos + 1] == 8) + if (p1 == 8) snprintf(exp, size, "INIT. GRAY LOCK"); - else if (cmd[pos + 1] == 9) + else if (p1 == 9) snprintf(exp, size, "INIT. GRAY UNLOCK"); break; case FMCOS20_CMD_GREY_LOCK_UNLOCK: - if (cmd[pos + 1] == 8) + if (p1 == 8) snprintf(exp, size, "GRAY LOCK"); - else if (cmd[pos + 1] == 9) + else if (p1 == 9) snprintf(exp, size, "GRAY UNLOCK"); break; case FMCOS20_CMD_DEBIT_UNLOCK: diff --git a/client/src/cmdhflist.h b/client/src/cmdhflist.h index 7a41de402..86447f59f 100644 --- a/client/src/cmdhflist.h +++ b/client/src/cmdhflist.h @@ -40,44 +40,44 @@ typedef struct { void ClearAuthData(void); -uint8_t iso14443A_CRC_check(bool isResponse, uint8_t *d, uint8_t n); -uint8_t iso14443B_CRC_check(uint8_t *d, uint8_t n); -uint8_t felica_CRC_check(uint8_t *d, uint8_t n); -uint8_t mifare_CRC_check(bool isResponse, uint8_t *data, uint8_t len); -uint8_t iso15693_CRC_check(uint8_t *d, uint8_t n); -uint8_t iclass_CRC_check(bool isResponse, uint8_t *d, uint8_t n); -uint8_t seos_CRC_check(bool isResponse, uint8_t *d, uint8_t n); +uint8_t iso14443A_CRC_check(bool isResponse, uint8_t *d, uint16_t n); +uint8_t iso14443B_CRC_check(uint8_t *d, uint16_t n); +uint8_t felica_CRC_check(uint8_t *d, uint16_t n); +uint8_t mifare_CRC_check(bool isResponse, uint8_t *data, uint16_t len); +uint8_t iso15693_CRC_check(uint8_t *d, uint16_t n); +uint8_t iclass_CRC_check(bool isResponse, uint8_t *d, uint16_t n); +uint8_t seos_CRC_check(bool isResponse, uint8_t *d, uint16_t n); -int applyIso14443a(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response); +int applyIso14443a(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool is_response); -void annotateIclass(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool isResponse); -void annotateIso15693(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize); -void annotateTopaz(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize); -void annotateLegic(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize); -void annotateFelica(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response); -void annotateIso7816(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response, bool contact); -void annotateCalypso(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response); -void annotateIso14443b(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response); -void annotateIso14443a(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response); -void annotateMfDesfire(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize); +void annotateIclass(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool isResponse); +void annotateIso15693(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize); +void annotateTopaz(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize); +void annotateLegic(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize); +void annotateFelica(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool is_response); +void annotateIso7816(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool is_response, bool contact); +void annotateCalypso(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool is_response); +void annotateIso14443b(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool is_response); +void annotateIso14443a(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool is_response); +void annotateMfDesfire(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize); const char *mfpGetAnnotationForCode(uint8_t code); const char *mfpGetEncryptedForCode(uint8_t code); const char *mfpGetResponseMacedForCode(uint8_t code); const char *mfpGetCommandMacedForCode(uint8_t code); -void annotateMfPlus(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize); -void annotateMifare(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, - const uint8_t *parity, uint8_t paritysize, bool isResponse); -void annotateLTO(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize); -void annotateCryptoRF(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize); +void annotateMfPlus(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize); +void annotateMifare(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, + const uint8_t *parity, uint16_t paritysize, bool isResponse); +void annotateLTO(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize); +void annotateCryptoRF(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize); -void annotateSeos(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool isResponse); +void annotateSeos(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize, bool isResponse); -bool DecodeMifareData(uint8_t *cmd, uint8_t cmdsize, uint8_t *parity, bool isResponse, uint8_t *mfData, size_t *mfDataLen, const uint64_t *dicKeys, uint32_t dicKeysCount); +bool DecodeMifareData(uint8_t *cmd, uint16_t cmdsize, uint8_t *parity, bool isResponse, uint8_t *mfData, size_t *mfDataLen, const uint64_t *dicKeys, uint32_t dicKeysCount); bool NTParityChk(AuthData_t *ad, uint32_t ntx); -bool NestedCheckKey(uint64_t key, AuthData_t *ad, uint8_t *cmd, uint8_t cmdsize, uint8_t *parity); -bool CheckCrypto1Parity(const uint8_t *cmd_enc, uint8_t cmdsize, uint8_t *cmd, const uint8_t *parity_enc); +bool NestedCheckKey(uint64_t key, AuthData_t *ad, uint8_t *cmd, uint16_t cmdsize, uint8_t *parity); +bool CheckCrypto1Parity(const uint8_t *cmd_enc, uint16_t cmdsize, uint8_t *cmd, const uint8_t *parity_enc); uint64_t GetCrypto1ProbableKey(AuthData_t *ad); -void annotateFMCOS20(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize); +void annotateFMCOS20(char *exp, size_t size, uint8_t *cmd, uint16_t cmdsize); #endif // CMDHFLIST diff --git a/client/src/cmdlfhitag.c b/client/src/cmdlfhitag.c index c7aa90f08..23fba9f0b 100644 --- a/client/src/cmdlfhitag.c +++ b/client/src/cmdlfhitag.c @@ -420,7 +420,7 @@ static void print_hitag2_blocks(uint8_t *d, uint16_t n) { } // Annotate HITAG protocol -void annotateHitag1(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, bool is_response) { +void annotateHitag1(char *exp, size_t size, const uint8_t *cmd, uint16_t cmdsize, bool is_response) { } static struct { @@ -583,7 +583,7 @@ static uint8_t hitag2_get_page(const char *bs) { return 255; } -void hitag2_annotate_plain(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, uint8_t bits) { +void hitag2_annotate_plain(char *exp, size_t size, const uint8_t *cmd, uint16_t cmdsize, uint8_t bits) { if (cmdsize == 0) { return; @@ -598,7 +598,7 @@ void hitag2_annotate_plain(char *exp, size_t size, const uint8_t *cmd, uint8_t c bytes_2_binstr(binstr, cmd, cmdsize); size_t bn = strlen(binstr); - if (bits) { + if ((bits > 0) && (bits <= 8)) { if (cmdsize == 1) { bn = bits; } else if (cmdsize > 1) { @@ -651,7 +651,7 @@ void hitag2_annotate_plain(char *exp, size_t size, const uint8_t *cmd, uint8_t c free(binstr); } -void annotateHitag2(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, uint8_t bits, bool is_response, const uint64_t *keys, uint32_t keycount, bool isdecrypted) { +void annotateHitag2(char *exp, size_t size, const uint8_t *cmd, uint16_t cmdsize, uint8_t bits, bool is_response, const uint64_t *keys, uint32_t keycount, bool isdecrypted) { if (cmdsize == 0) { return; @@ -666,7 +666,7 @@ void annotateHitag2(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, bytes_2_binstr(binstr, cmd, cmdsize); size_t bn = strlen(binstr); - if (bits) { + if ((bits > 0) && (bits <= 8)) { if (cmdsize == 1) { bn = bits; } else if (cmdsize > 1) { @@ -674,12 +674,13 @@ void annotateHitag2(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, } } - memcpy(_ht2state.plain, cmd, cmdsize); - _ht2state.plainlen = cmdsize; + // a frame longer than plain is no Hitag 2 frame, it is kept cut and not decrypted + _ht2state.plainlen = MIN(cmdsize, sizeof(_ht2state.plain)); + memcpy(_ht2state.plain, cmd, _ht2state.plainlen); if (_ht2state.state == STATE_ENCRYPTED || _ht2state.state == STATE_START_ENCRYPTED) { - if (_ht2state.found_key && isdecrypted == false) { + if (_ht2state.found_key && isdecrypted == false && (bn <= (sizeof(_ht2state.plain) * 8))) { ht2_hitag2_cipher_transcrypt(&_ht2state.cipher_state, _ht2state.plain, bn / 8, bn % 8); } } diff --git a/client/src/cmdlfhitag.h b/client/src/cmdlfhitag.h index 5c82613cf..4ecf767c1 100644 --- a/client/src/cmdlfhitag.h +++ b/client/src/cmdlfhitag.h @@ -29,12 +29,12 @@ int CmdLFHitag(const char *Cmd); int ht2_read_uid(void); int ht2_read_paxton(void); -void annotateHitag1(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, bool is_response); -void annotateHitag2(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, uint8_t bits, bool is_response, const uint64_t *keys, uint32_t keycount, bool isdecrypted); +void annotateHitag1(char *exp, size_t size, const uint8_t *cmd, uint16_t cmdsize, bool is_response); +void annotateHitag2(char *exp, size_t size, const uint8_t *cmd, uint16_t cmdsize, uint8_t bits, bool is_response, const uint64_t *keys, uint32_t keycount, bool isdecrypted); void annotateHitag2_init(void); bool hitag2_get_plain(uint8_t *plain, uint8_t *plen); -void hitag2_annotate_plain(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, uint8_t bits); +void hitag2_annotate_plain(char *exp, size_t size, const uint8_t *cmd, uint16_t cmdsize, uint8_t bits); uint8_t hitag1_CRC_check(uint8_t *d, uint32_t nbit); #endif diff --git a/client/src/cmdlfhitaghts.c b/client/src/cmdlfhitaghts.c index 2f52823f8..8fb0d9e79 100644 --- a/client/src/cmdlfhitaghts.c +++ b/client/src/cmdlfhitaghts.c @@ -37,7 +37,7 @@ static int CmdHelp(const char *Cmd); -void annotateHitagS(char *exp, size_t size, const uint8_t *cmd, uint8_t nbits, bool is_response) { +void annotateHitagS(char *exp, size_t size, const uint8_t *cmd, uint16_t nbits, bool is_response) { uint8_t command = 0; // the caller reuses this buffer across frames, and the appends below are @@ -347,7 +347,7 @@ static void hitags_config_print(hitags_config_t config) { PrintAndLogEx(INFO, " Memory type...... " _GREEN_("%s"), (const char *[]) {"Hitag S 32", "Hitag S 256", "Hitag S 2048", "Unknown Hitag S/8211"}[config.MEMT]); - PrintAndLogEx(INFO, " Authenticaion.... %s", config.auth ? _YELLOW_("Yes") : "No"); + PrintAndLogEx(INFO, " Authentication... %s", config.auth ? _YELLOW_("Yes") : "No"); PrintAndLogEx(INFO, " TTF coding....... %s", config.RES3 ? "FSK 0=RF/10 1=RF/8" : (const char *[]) {"Manchester", "Biphase"}[config.TTFC]); diff --git a/client/src/cmdlfhitaghts.h b/client/src/cmdlfhitaghts.h index 522096ea4..5b2d7816a 100644 --- a/client/src/cmdlfhitaghts.h +++ b/client/src/cmdlfhitaghts.h @@ -22,7 +22,7 @@ #include "common.h" #include "hitag.h" -void annotateHitagS(char *exp, size_t size, const uint8_t *cmd, uint8_t nbits, bool is_response); +void annotateHitagS(char *exp, size_t size, const uint8_t *cmd, uint16_t nbits, bool is_response); int CmdLFHitagS(const char *Cmd); diff --git a/client/src/cmdlfhitagu.c b/client/src/cmdlfhitagu.c index 5d0ec275f..aa9111529 100644 --- a/client/src/cmdlfhitagu.c +++ b/client/src/cmdlfhitagu.c @@ -45,11 +45,15 @@ uint8_t hitagu_CRC_check(uint8_t *d, uint32_t nbit) { return (Crc16(d, nbit, 0, CRC16_POLY_CCITT, false, false) == 0); } -void annotateHitagU(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, bool is_response) { +void annotateHitagU(char *exp, size_t size, const uint8_t *cmd, uint16_t cmdsize, bool is_response) { if (is_response) { } else { + // flags and command take 11 bits + if (cmdsize < 2) { + return; + } uint8_t flag = reflect8(cmd[0]) & 0x1F; uint8_t command = ((reflect8(cmd[0]) >> 5) & 0x07) | ((reflect8(cmd[1]) & 0x07) << 3); bool has_uid = false; @@ -102,14 +106,19 @@ void annotateHitagU(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, } else if (cmdsize == (7 + (has_uid * HITAGU_UID_SIZE)) || cmdsize == (9 + (has_uid * HITAGU_UID_SIZE))) { uint8_t mfc = 0; - concatbits(&mfc, 0, cmd, 5 + 6 + 8 + 32, 8, false); + concatbits(&mfc, 0, cmd, 5 + 6, 8, false); snprintf(exp + strlen(exp), size - strlen(exp), "LOGIN mfc:%02x ", mfc); has_mfc = true; } + // flags, command, IC MFC, UID, password + if ((cmdsize * 8) < (5 + 6 + has_mfc * 8 + has_uid * HITAGU_UID_SIZE * 8 + HITAG_PASSWORD_SIZE * 8)) { + break; + } + if (has_uid) { uint8_t uid[HITAGU_UID_SIZE] = {0}; - concatbits(uid, 0, cmd, 5 + 6 + has_mfc * 8 + 32, HITAGU_UID_SIZE * 8, false); + concatbits(uid, 0, cmd, 5 + 6 + has_mfc * 8, HITAGU_UID_SIZE * 8, false); snprintf(exp + strlen(exp), size - strlen(exp), " uid:%s", sprint_hex_inrow(uid, HITAGU_UID_SIZE)); } @@ -123,11 +132,18 @@ void annotateHitagU(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, break; } case HITAGU_CMD_READ_MULTIPLE_BLOCK: { + // an addressed request has the UID before the block number + uint8_t bit_offset = 5 + 6 + has_uid * HITAGU_UID_SIZE * 8; + if ((cmdsize * 8) < (bit_offset + 8 + 8)) { + snprintf(exp + strlen(exp), size - strlen(exp), "READ MULTIPLE BLOCK"); + break; + } + uint8_t block_addr = 0; - concatbits(&block_addr, 0, cmd, 5 + 6, 8, false); + concatbits(&block_addr, 0, cmd, bit_offset, 8, false); uint8_t block_count = 0; - concatbits(&block_count, 0, cmd, 5 + 6 + 8, 8, false); + concatbits(&block_count, 0, cmd, bit_offset + 8, 8, false); snprintf(exp + strlen(exp), size - strlen(exp), "READ MULTIPLE BLOCK start:%d num:%d" , reflect8(block_addr) @@ -136,11 +152,18 @@ void annotateHitagU(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, break; } case HITAGU_CMD_WRITE_SINGLE_BLOCK: { + // an addressed request has the UID before the block number + uint8_t bit_offset = 5 + 6 + has_uid * HITAGU_UID_SIZE * 8; + if ((cmdsize * 8) < (bit_offset + 8 + 32)) { + snprintf(exp + strlen(exp), size - strlen(exp), "WRITE SINGLE BLOCK"); + break; + } + uint8_t block_addr = 0; - concatbits(&block_addr, 0, cmd, 5 + 6, 8, false); + concatbits(&block_addr, 0, cmd, bit_offset, 8, false); uint8_t block_data[4] = {0}; - concatbits(block_data, 0, cmd, 5 + 6 + 8, 32, false); + concatbits(block_data, 0, cmd, bit_offset + 8, 32, false); snprintf(exp + strlen(exp), size - strlen(exp), "WRITE SINGLE BLOCK start:%d data:[%s]" , reflect8(block_addr) diff --git a/client/src/cmdlfhitagu.h b/client/src/cmdlfhitagu.h index af2153083..33cbd4875 100644 --- a/client/src/cmdlfhitagu.h +++ b/client/src/cmdlfhitagu.h @@ -23,7 +23,7 @@ #include "hitag.h" uint8_t hitagu_CRC_check(uint8_t *d, uint32_t nbit); -void annotateHitagU(char *exp, size_t size, const uint8_t *cmd, uint8_t cmdsize, bool is_response); +void annotateHitagU(char *exp, size_t size, const uint8_t *cmd, uint16_t cmdsize, bool is_response); int CmdLFHitagU(const char *Cmd); diff --git a/client/src/cmdtrace.c b/client/src/cmdtrace.c index b8f8b4977..63ce32cf3 100644 --- a/client/src/cmdtrace.c +++ b/client/src/cmdtrace.c @@ -56,10 +56,60 @@ static bool next_record_is_response(uint16_t tracepos, uint8_t *trace) { return (hdr->isResponse); } +// the record at tracepos, NULL when it does not fit in the trace +static const tracelog_hdr_t *get_record(uint16_t tracepos, uint16_t traceLen, const uint8_t *trace) { + if (is_last_record(tracepos, traceLen)) { + return NULL; + } + const tracelog_hdr_t *hdr = (const tracelog_hdr_t *)(trace + tracepos); + if (tracepos + TRACELOG_HDR_LEN + hdr->data_len + TRACELOG_PARITY_LEN(hdr) > traceLen) { + return NULL; + } + return hdr; +} + +// bits of a Hitag frame whose last byte holds last_bits of them, 0 for a whole byte +static uint16_t hitag_frame_bits(uint16_t data_len, uint8_t last_bits) { + if (data_len == 0) { + return 0; + } + if ((last_bits == 0) || (last_bits > 8)) { + return data_len * 8; + } + return ((data_len - 1) * 8) + last_bits; +} + // Topaz reader commands are at most 16 bytes long (RSEG/READ8/WRITE-*8: // cmd + adds + 8 data bytes + 4 UID bytes + 2 CRC bytes) #define MAX_TOPAZ_READER_CMD_LEN 16 +// a 14a frame that lasts less than this has 7 bits or fewer +#define ISO14A_SHORT_FRAME_DURATION (1056 + 32) + +// in a Topaz trace, the reader's last frame was an ISO14443-A one: the answer +// to it is one too +static bool gs_topaz_14a_exchange = false; + +// the frames of a Topaz command, the 7 bit command code included; 0 for an +// unknown code +static uint8_t topaz_reader_cmd_len(uint8_t code) { + switch (code) { + case TOPAZ_RID: + case TOPAZ_RALL: + case TOPAZ_READ: + case TOPAZ_WRITE_E: + case TOPAZ_WRITE_NE: + return 9; + case TOPAZ_RSEG: + case TOPAZ_READ8: + case TOPAZ_WRITE_E8: + case TOPAZ_WRITE_NE8: + return MAX_TOPAZ_READER_CMD_LEN; + default: + return 0; + } +} + static bool merge_topaz_reader_frames(uint32_t timestamp, uint32_t *duration, uint16_t *tracepos, uint16_t traceLen, uint8_t *trace, const uint8_t *frame, uint8_t *topaz_reader_command, uint16_t *data_len) { @@ -67,24 +117,35 @@ static bool merge_topaz_reader_frames(uint32_t timestamp, uint32_t *duration, ui if ((*data_len != 1) || (frame[0] == TOPAZ_WUPA) || (frame[0] == TOPAZ_REQA)) return false; + // a command starts with its 7 bit code: an 8 bit frame is an operand left + // from a command cut short + if (*duration >= ISO14A_SHORT_FRAME_DURATION) return false; + memcpy(topaz_reader_command, frame, *data_len); - while (!is_last_record(*tracepos, traceLen) && !next_record_is_response(*tracepos, trace)) { + // stop at the frames the command code takes, or before when a reader sent + // fewer: the next command starts with its 7 bit code, an operand has 8 bits + uint8_t cmd_len = topaz_reader_cmd_len(frame[0]); + if (cmd_len == 0) { + cmd_len = MAX_TOPAZ_READER_CMD_LEN; + } + + while ((*data_len < cmd_len) && !is_last_record(*tracepos, traceLen) && !next_record_is_response(*tracepos, trace)) { const tracelog_hdr_t *hdr = (tracelog_hdr_t *)(trace + *tracepos); - *tracepos += TRACELOG_HDR_LEN + hdr->data_len; - - if ((hdr->data_len == 1) && (*data_len + hdr->data_len <= MAX_TOPAZ_READER_CMD_LEN)) { - memcpy(topaz_reader_command + *data_len, hdr->frame, hdr->data_len); - *data_len += hdr->data_len; - last_timestamp = hdr->timestamp + hdr->duration; - } else { - // rewind and exit - *tracepos = *tracepos - hdr->data_len - TRACELOG_HDR_LEN; + if ((hdr->data_len != 1) || (hdr->duration < ISO14A_SHORT_FRAME_DURATION)) { break; } - *tracepos += TRACELOG_PARITY_LEN(hdr); + + if (*tracepos + TRACELOG_HDR_LEN + hdr->data_len + TRACELOG_PARITY_LEN(hdr) > traceLen) { + break; + } + + memcpy(topaz_reader_command + *data_len, hdr->frame, hdr->data_len); + *data_len += hdr->data_len; + last_timestamp = hdr->timestamp + hdr->duration; + *tracepos += TRACELOG_HDR_LEN + hdr->data_len + TRACELOG_PARITY_LEN(hdr); } *duration = last_timestamp - timestamp; @@ -130,9 +191,9 @@ static uint8_t extract_epurse[8] = {0}; #define SKIP_TO_NEXT(a) (TRACELOG_HDR_LEN + (a)->data_len + TRACELOG_PARITY_LEN((a))) -static uint16_t extractChall_ev2(uint16_t tracepos, uint8_t *trace, uint8_t cmdpos, uint8_t long_jmp) { - tracelog_hdr_t *next_hdr = (tracelog_hdr_t *)(trace + tracepos); - if (next_hdr->data_len != 21) { +static uint16_t extractChall_ev2(uint16_t tracepos, uint16_t traceLen, uint8_t *trace, uint8_t cmdpos, uint8_t long_jmp) { + const tracelog_hdr_t *next_hdr = get_record(tracepos, traceLen, trace); + if ((next_hdr == NULL) || (next_hdr->data_len != 21)) { return 0; } @@ -140,10 +201,14 @@ static uint16_t extractChall_ev2(uint16_t tracepos, uint8_t *trace, uint8_t cmdp PrintAndLogEx(INFO, "1499999999 %s " NOLF, sprint_hex_inrow(next_hdr->frame + 1, 16)); - next_hdr = (tracelog_hdr_t *)(trace + tracepos); + next_hdr = get_record(tracepos, traceLen, trace); + if (next_hdr == NULL) { + PrintAndLogEx(NORMAL, ""); + return tracepos; + } tracepos += TRACELOG_HDR_LEN + next_hdr->data_len + TRACELOG_PARITY_LEN(next_hdr); - if (next_hdr->frame[cmdpos] == MFDES_ADDITIONAL_FRAME) { + if ((next_hdr->data_len >= cmdpos + long_jmp + 32) && (next_hdr->frame[cmdpos] == MFDES_ADDITIONAL_FRAME)) { PrintAndLogEx(NORMAL, "%s", sprint_hex_inrow(next_hdr->frame + cmdpos + long_jmp, 32)); } else { PrintAndLogEx(NORMAL, ""); @@ -204,7 +269,10 @@ static uint16_t extractChallenges(uint16_t tracepos, uint16_t traceLen, uint8_t } // time to skip to next - tracelog_hdr_t *next_hdr = (tracelog_hdr_t *)(trace + tracepos); + const tracelog_hdr_t *next_hdr = get_record(tracepos, traceLen, trace); + if (next_hdr == NULL) { + break; + } tracepos += SKIP_TO_NEXT(next_hdr); if (next_hdr->data_len != 11) { break; @@ -217,10 +285,12 @@ static uint16_t extractChallenges(uint16_t tracepos, uint16_t traceLen, uint8_t PrintAndLogEx(INFO, "Found a MFU-C authententication attempt"); PrintAndLogEx(INFO, "3DES %s " NOLF, sprint_hex_inrow(next_hdr->frame + 1, 8)); - next_hdr = (tracelog_hdr_t *)(trace + tracepos); - tracepos += SKIP_TO_NEXT(next_hdr); + next_hdr = get_record(tracepos, traceLen, trace); + if (next_hdr != NULL) { + tracepos += SKIP_TO_NEXT(next_hdr); + } - if (next_hdr->frame[0] == MIFARE_ULC_AUTH_2 && next_hdr->data_len == 19) { + if ((next_hdr != NULL) && (next_hdr->data_len == 19) && (next_hdr->frame[0] == MIFARE_ULC_AUTH_2)) { PrintAndLogEx(NORMAL, "%s", sprint_hex_inrow(next_hdr->frame + 1, 16)); } else { PrintAndLogEx(NORMAL, "( " _RED_("partial") " )"); @@ -239,7 +309,10 @@ static uint16_t extractChallenges(uint16_t tracepos, uint16_t traceLen, uint8_t switch (c) { case ICLASS_CMD_SELECT: { - const tracelog_hdr_t *next_hdr = (tracelog_hdr_t *)(trace + tracepos); + const tracelog_hdr_t *next_hdr = get_record(tracepos, traceLen, trace); + if (next_hdr == NULL) { + break; + } tracepos += SKIP_TO_NEXT(next_hdr); if (next_hdr->data_len != 10) { break; @@ -251,8 +324,11 @@ static uint16_t extractChallenges(uint16_t tracepos, uint16_t traceLen, uint8_t case ICLASS_CMD_READCHECK: { // get epurse - if (frame[1] == 2 && data_len == 2) { - const tracelog_hdr_t *next_hdr = (tracelog_hdr_t *)(trace + tracepos); + if (data_len == 2 && frame[1] == 2) { + const tracelog_hdr_t *next_hdr = get_record(tracepos, traceLen, trace); + if (next_hdr == NULL) { + break; + } tracepos += SKIP_TO_NEXT(next_hdr); if (next_hdr->data_len < 8) { break; @@ -283,7 +359,7 @@ static uint16_t extractChallenges(uint16_t tracepos, uint16_t traceLen, uint8_t // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor) // 93 50 = Bit oriented anti-collision (usage: 9350+ up to 5bytes, 9350 answer - up to 5bytes UID+BCC) // 93 70 = Select (usage: 9370+5bytes 9370 answer - answer: 1byte SAK) - if (frame[1] == 0x70) { + if ((data_len >= 6) && (frame[1] == 0x70)) { if (frame[2] == 0x88) { memcpy(extract_uid, frame + 3, 3); extract_uidlen = 3; @@ -299,13 +375,14 @@ static uint16_t extractChallenges(uint16_t tracepos, uint16_t traceLen, uint8_t // 95 20 = Anticollision of cascade level2 // 95 50 = Bit oriented anti-collision level2 // 95 70 = Select of cascade level2 - if (frame[1] == 0x70) { + if ((data_len >= 6) && (frame[1] == 0x70)) { + // cascade level 2 starts at UID byte 3, level 3 at byte 6 if (frame[2] == 0x88) { - memcpy(extract_uid + extract_uidlen, frame + 3, 3); - extract_uidlen += 3; + memcpy(extract_uid + 3, frame + 3, 3); + extract_uidlen = 6; } else { - memcpy(extract_uid + extract_uidlen, frame + 2, 4); - extract_uidlen += 4; + memcpy(extract_uid + 3, frame + 2, 4); + extract_uidlen = 7; PrintAndLogEx(INFO, "UID... " _YELLOW_("%s"), sprint_hex_inrow(extract_uid, extract_uidlen)); } } @@ -315,9 +392,9 @@ static uint16_t extractChallenges(uint16_t tracepos, uint16_t traceLen, uint8_t // 97 20 = Anticollision of cascade level3 // 97 50 = Bit oriented anti-collision level3 // 97 70 = Select of cascade level3 - if (frame[1] == 0x70) { - memcpy(extract_uid + extract_uidlen, frame + 2, 4); - extract_uidlen += 4; + if ((data_len >= 6) && (frame[1] == 0x70)) { + memcpy(extract_uid + 6, frame + 2, 4); + extract_uidlen = 10; PrintAndLogEx(INFO, "UID... " _YELLOW_("%s"), sprint_hex_inrow(extract_uid, extract_uidlen)); } break; @@ -339,28 +416,35 @@ static uint16_t extractChallenges(uint16_t tracepos, uint16_t traceLen, uint8_t for (uint8_t i = 0; i < 2; i++, pos++) { + // the command and its key number + if (pos + long_jmp >= data_len) { + break; + } + switch (frame[pos]) { case MFDES_AUTHENTICATE: { // Assume wrapped or unwrapped PrintAndLogEx(INFO, "Found a MFDES Auth NATIVE (keyNo %d)", frame[pos + long_jmp]); - if (next_record_is_response(tracepos, trace) == false) { + const tracelog_hdr_t *next_hdr = get_record(tracepos, traceLen, trace); + if ((next_hdr == NULL) || (next_hdr->isResponse == false)) { break; } - tracelog_hdr_t *next_hdr = (tracelog_hdr_t *)(trace + tracepos); - if (next_hdr->data_len < 7) { + if (next_hdr->data_len < 9) { break; } tracepos += TRACELOG_HDR_LEN + next_hdr->data_len + TRACELOG_PARITY_LEN(next_hdr); PrintAndLogEx(INFO, "DES 1499999999 %s " NOLF, sprint_hex_inrow(next_hdr->frame + 1, 8)); - next_hdr = (tracelog_hdr_t *)(trace + tracepos); - tracepos += TRACELOG_HDR_LEN + next_hdr->data_len + TRACELOG_PARITY_LEN(next_hdr); + next_hdr = get_record(tracepos, traceLen, trace); + if (next_hdr != NULL) { + tracepos += TRACELOG_HDR_LEN + next_hdr->data_len + TRACELOG_PARITY_LEN(next_hdr); + } - if (next_hdr->frame[pos] == MFDES_ADDITIONAL_FRAME) { + if ((next_hdr != NULL) && (next_hdr->data_len >= pos + long_jmp + 16) && (next_hdr->frame[pos] == MFDES_ADDITIONAL_FRAME)) { PrintAndLogEx(NORMAL, "%s", sprint_hex_inrow(next_hdr->frame + pos + long_jmp, 16)); } else { PrintAndLogEx(NORMAL, ""); @@ -370,13 +454,13 @@ static uint16_t extractChallenges(uint16_t tracepos, uint16_t traceLen, uint8_t case MFDES_AUTHENTICATE_ISO: { // Assume wrapped or unwrapped PrintAndLogEx(INFO, "Found a MFDES Auth ISO (keyNo %d)", frame[pos + long_jmp]); - if (next_record_is_response(tracepos, trace) == false) { + const tracelog_hdr_t *next_hdr = get_record(tracepos, traceLen, trace); + if ((next_hdr == NULL) || (next_hdr->isResponse == false)) { break; } - tracelog_hdr_t *next_hdr = (tracelog_hdr_t *)(trace + tracepos); tracepos += TRACELOG_HDR_LEN + next_hdr->data_len + TRACELOG_PARITY_LEN(next_hdr); - if (next_hdr->data_len < 7) { + if (next_hdr->data_len < 9) { break; } @@ -388,10 +472,12 @@ static uint16_t extractChallenges(uint16_t tracepos, uint16_t traceLen, uint8_t PrintAndLogEx(INFO, "2TDEA 1499999999 %s " NOLF, sprint_hex_inrow(next_hdr->frame + 1, tdea)); } - next_hdr = (tracelog_hdr_t *)(trace + tracepos); - tracepos += TRACELOG_HDR_LEN + next_hdr->data_len + TRACELOG_PARITY_LEN(next_hdr); + next_hdr = get_record(tracepos, traceLen, trace); + if (next_hdr != NULL) { + tracepos += TRACELOG_HDR_LEN + next_hdr->data_len + TRACELOG_PARITY_LEN(next_hdr); + } - if (next_hdr->frame[pos] == MFDES_ADDITIONAL_FRAME) { + if ((next_hdr != NULL) && (next_hdr->data_len >= pos + long_jmp + (tdea << 1)) && (next_hdr->frame[pos] == MFDES_ADDITIONAL_FRAME)) { PrintAndLogEx(NORMAL, "%s", sprint_hex_inrow(next_hdr->frame + pos + long_jmp, (tdea << 1))); } else { PrintAndLogEx(NORMAL, ""); @@ -401,22 +487,24 @@ static uint16_t extractChallenges(uint16_t tracepos, uint16_t traceLen, uint8_t case MFDES_AUTHENTICATE_AES: { // Assume wrapped or unwrapped PrintAndLogEx(INFO, "Found a MFDES Auth AES (keyNo %d)", frame[pos + long_jmp]); - if (next_record_is_response(tracepos, trace)) { + const tracelog_hdr_t *next_hdr = get_record(tracepos, traceLen, trace); + if ((next_hdr == NULL) || (next_hdr->isResponse == false)) { break; } - tracelog_hdr_t *next_hdr = (tracelog_hdr_t *)(trace + tracepos); tracepos += TRACELOG_HDR_LEN + next_hdr->data_len + TRACELOG_PARITY_LEN(next_hdr); - if (next_hdr->data_len < 7) { + if (next_hdr->data_len < 9) { break; } PrintAndLogEx(INFO, "AES 1499999999 %s " NOLF, sprint_hex_inrow(next_hdr->frame + 1, 8)); - next_hdr = (tracelog_hdr_t *)(trace + tracepos); - tracepos += TRACELOG_HDR_LEN + next_hdr->data_len + TRACELOG_PARITY_LEN(next_hdr); + next_hdr = get_record(tracepos, traceLen, trace); + if (next_hdr != NULL) { + tracepos += TRACELOG_HDR_LEN + next_hdr->data_len + TRACELOG_PARITY_LEN(next_hdr); + } - if (next_hdr->frame[pos] == MFDES_ADDITIONAL_FRAME) { + if ((next_hdr != NULL) && (next_hdr->data_len >= pos + long_jmp + 16) && (next_hdr->frame[pos] == MFDES_ADDITIONAL_FRAME)) { PrintAndLogEx(NORMAL, "%s", sprint_hex_inrow(next_hdr->frame + pos + long_jmp, 16)); } else { PrintAndLogEx(NORMAL, ""); @@ -425,7 +513,7 @@ static uint16_t extractChallenges(uint16_t tracepos, uint16_t traceLen, uint8_t } case MFDES_AUTHENTICATE_EV2F: { PrintAndLogEx(INFO, "Found a MFDES Auth EV2 First"); - uint16_t tmp = extractChall_ev2(tracepos, trace, pos, long_jmp); + uint16_t tmp = extractChall_ev2(tracepos, traceLen, trace, pos, long_jmp); if (tmp == 0) break; else @@ -434,7 +522,7 @@ static uint16_t extractChallenges(uint16_t tracepos, uint16_t traceLen, uint8_t } case MFDES_AUTHENTICATE_EV2NF: { PrintAndLogEx(INFO, "Found a MFDES Auth EV2 Non First"); - uint16_t tmp = extractChall_ev2(tracepos, trace, pos, long_jmp); + uint16_t tmp = extractChall_ev2(tracepos, traceLen, trace, pos, long_jmp); if (tmp == 0) break; else @@ -452,7 +540,7 @@ static uint16_t printHexLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trac tracelog_hdr_t *hdr = (tracelog_hdr_t *)(trace + tracepos); - if (TRACELOG_HDR_LEN + hdr->data_len + TRACELOG_PARITY_LEN(hdr) > traceLen) { + if (tracepos + TRACELOG_HDR_LEN + hdr->data_len + TRACELOG_PARITY_LEN(hdr) > traceLen) { return traceLen; } @@ -548,30 +636,63 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr uint8_t *frame = hdr->frame; uint8_t *parityBytes = hdr->frame + data_len; + // Hitag keeps the bit count of the last byte in the first parity byte, 0 for a whole byte + uint8_t hitag_last_bits = (parityBytes[0] <= 8) ? parityBytes[0] : 0; + tracepos += TRACELOG_HDR_LEN + data_len + TRACELOG_PARITY_LEN(hdr); + bool topaz_merged = false; if (protocol == TOPAZ && !hdr->isResponse) { - // topaz reader commands come in 1 or 9 separate frames with 7 or 8 Bits each. + // topaz reader commands come in 1, 9 or 16 separate frames with 7 or 8 Bits each. // merge them: if (merge_topaz_reader_frames(hdr->timestamp, &duration, &tracepos, traceLen, trace, frame, topaz_reader_command, &data_len)) { frame = topaz_reader_command; + topaz_merged = true; } } + // a Topaz command comes one byte per frame: a longer reader frame is an + // ISO14443-A one, the HLTA or the anticollision of a multi protocol reader + bool topaz_14a_frame = (protocol == TOPAZ) && (hdr->isResponse == false) && (topaz_merged == false) && (data_len > 1); + + if ((protocol == TOPAZ) && (hdr->isResponse == false)) { + gs_topaz_14a_exchange = topaz_14a_frame; + } + bool topaz_14a_answer = (protocol == TOPAZ) && hdr->isResponse && gs_topaz_14a_exchange; + + // a lone 8 bit reader frame: an operand of a command cut short, no command code + bool topaz_operand = (protocol == TOPAZ) && (hdr->isResponse == false) && (topaz_merged == false) && + (data_len == 1) && (hdr->duration >= ISO14A_SHORT_FRAME_DURATION); + + // a Topaz command with fewer frames than its code takes, cut by the end of + // the trace or by the reader, carries no CRC to check + bool topaz_cut = topaz_merged && (data_len < topaz_reader_cmd_len(frame[0])); + + // an iCLASS trace also holds the ISO15693 frames of a reader that polls + // both: their CRC covers the whole frame, an iCLASS one the bytes after the command + bool iclass_15693_frame = (protocol == ICLASS) && (hdr->isResponse == false) && (data_len >= 3) && (iso15693_CRC_check(frame, data_len) == 1); + //Check the CRC status trace_crc_status_t crcStatus = TRACE_CRC_NONE; if (data_len > 2) { switch (protocol) { case ICLASS: - crcStatus = iclass_CRC_check(hdr->isResponse, frame, data_len); + crcStatus = (iclass_15693_frame) ? TRACE_CRC_OK : iclass_CRC_check(hdr->isResponse, frame, data_len); break; case ISO_14443B: - case TOPAZ: crcStatus = iso14443B_CRC_check(frame, data_len); break; + case TOPAZ: + if (topaz_cut) { + crcStatus = TRACE_CRC_NONE; + } else { + crcStatus = (topaz_14a_frame || topaz_14a_answer) ? iso14443A_CRC_check(hdr->isResponse, frame, data_len) : iso14443B_CRC_check(frame, data_len); + } + break; case FELICA: - crcStatus = !felica_CRC_check(frame + 2, data_len - 4); + // after the B2 4D sync, the CRC covers LEN to the end + crcStatus = felica_CRC_check(frame + 2, data_len - 2); break; case PROTO_MIFARE: case PROTO_MFPLUS: @@ -622,16 +743,38 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr break; case PROTO_HITAG1: case PROTO_HITAGS: - crcStatus = hitag1_CRC_check(frame, (data_len * 8) - ((8 - parityBytes[0]) % 8)); + crcStatus = hitag1_CRC_check(frame, hitag_frame_bits(data_len, hitag_last_bits)); break; case PROTO_HITAGU: - crcStatus = hitagu_CRC_check(frame, (data_len * 8) - ((8 - parityBytes[0]) % 8)); + crcStatus = hitagu_CRC_check(frame, hitag_frame_bits(data_len, hitag_last_bits)); break; case PROTO_HITAG2: case PROTO_CRYPTORF: default: break; } + } else if (hdr->isResponse == false) { + // an ISO14443-A anticollision answer is told by the request before it, + // ANTICOLL 93 20 among them: the 14a checks see the short frames too + switch (protocol) { + case PROTO_MIFARE: + case PROTO_MFPLUS: + mifare_CRC_check(false, frame, data_len); + break; + case ISO_14443A: + case MFDES: + case LTO: + case SEOS: + iso14443A_CRC_check(false, frame, data_len); + break; + case TOPAZ: + if (topaz_14a_frame) { + iso14443A_CRC_check(false, frame, data_len); + } + break; + default: + break; + } } //0 CRC-command, CRC not ok //1 CRC-command, CRC ok @@ -677,7 +820,7 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr snprintf(line[j / 18] + ((j % 18) * 4), 120, "%02X! ", frame[j]); - } else if (protocol == ICLASS && hdr->isResponse == false) { + } else if (protocol == ICLASS && hdr->isResponse == false && iclass_15693_frame == false) { uint8_t parity = 0; for (int i = 0; i < 6; i++) { @@ -697,7 +840,7 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr // handle partial bytes. The parity array[0] is used to store number of left over bits from NBYTES // The bit count now goes in the CRC column, which is unused for // hitag, so the data field holds nothing but data. - uint8_t nbits = parityBytes[0]; + uint8_t nbits = hitag_last_bits; // only apply this to lesser than one byte // One leading space, so the hex lines up under the "Data" header. @@ -708,6 +851,9 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr } offset = 1; + } else if ((j % 18) == 0) { + // the next rows start with the same leading space + snprintf(line[j / 18], 120, " %02X ", frame[j]); } else { snprintf(line[j / 18] + ((j % 18) * 4) + offset, 120, "%02X ", frame[j]); } @@ -718,14 +864,15 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr } - if (markCRCBytes && data_len > 2) { + // the rows above hold TRACE_MAX_HEX_BYTES * TRACE_MAX_HEX_BYTES bytes, a longer frame has its CRC past them + if (markCRCBytes && (data_len > 2) && (data_len <= (TRACE_MAX_HEX_BYTES * TRACE_MAX_HEX_BYTES))) { // CRC-command if (((protocol == PROTO_HITAG1) || (protocol == PROTO_HITAGS))) { // Note that UID REQUEST response has no CRC, but we don't know // if the response we see is a UID char *pos1 = line[(data_len - 1) / 18] + (((data_len - 1) % 18) * 4) + offset - 1; (*pos1) = '['; - char *pos2 = line[(data_len) / 18] + (((data_len) % 18) * 4) + offset - 2; + char *pos2 = line[(data_len - 1) / 18] + (((data_len - 1) % 18) * 4) + offset + 2; (*pos2) = ']'; (*(pos2 + 1)) = '\0'; } else { @@ -794,16 +941,7 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr if ((protocol == PROTO_HITAG1) || (protocol == PROTO_HITAG2) || (protocol == PROTO_HITAGS) || (protocol == PROTO_HITAGU)) { - uint8_t leftover = parityBytes[0]; - uint16_t total; - if ((data_len == 1) && (leftover != 0)) { - total = leftover; - } else if (leftover == 0) { - total = (uint16_t)(data_len * 8); - } else { - total = (uint16_t)(((data_len - 1) * 8) + leftover); - } - snprintf(hitag_nbits, sizeof(hitag_nbits), "%3u ", total); + snprintf(hitag_nbits, sizeof(hitag_nbits), "%3u ", hitag_frame_bits(data_len, hitag_last_bits)); crc = hitag_nbits; } @@ -814,7 +952,7 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr protocol == THINFILM) { // approximated with 128 * (9 * data_len); - uint16_t bitime = 1056 + 32; + uint16_t bitime = ISO14A_SHORT_FRAME_DURATION; if (duration < bitime) { @@ -827,10 +965,11 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr m--; } - if (data_len) { - line[(data_len - 1) / 16][((data_len - 1) % 16) * 4 + 2] = '('; - line[(data_len - 1) / 16][((data_len - 1) % 16) * 4 + 3] = m + 0x30; - line[(data_len - 1) / 16][((data_len - 1) % 16) * 4 + 4] = ')'; + // only a one byte frame can be that short, a longer one has a wrong duration + if (data_len == 1) { + line[0][2] = '('; + line[0][3] = m + 0x30; + line[0][4] = ')'; } } } @@ -872,10 +1011,10 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr annotateHitag1(explanation, sizeof(explanation), frame, data_len, hdr->isResponse); break; case PROTO_HITAG2: - annotateHitag2(explanation, sizeof(explanation), frame, data_len, parityBytes[0], hdr->isResponse, mfDicKeys, mfDicKeysCount, false); + annotateHitag2(explanation, sizeof(explanation), frame, data_len, hitag_last_bits, hdr->isResponse, mfDicKeys, mfDicKeysCount, false); break; case PROTO_HITAGS: - annotateHitagS(explanation, sizeof(explanation), frame, (data_len * 8) - ((8 - parityBytes[0]) % 8), hdr->isResponse); + annotateHitagS(explanation, sizeof(explanation), frame, hitag_frame_bits(data_len, hitag_last_bits), hdr->isResponse); break; case FELICA: annotateFelica(explanation, sizeof(explanation), frame, data_len, hdr->isResponse); @@ -884,7 +1023,11 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr annotateHitagU(explanation, sizeof(explanation), frame, data_len, hdr->isResponse); break; case ICLASS: - annotateIclass(explanation, sizeof(explanation), frame, data_len, hdr->isResponse); + if (iclass_15693_frame) { + annotateIso15693(explanation, sizeof(explanation), frame, data_len); + } else { + annotateIclass(explanation, sizeof(explanation), frame, data_len, hdr->isResponse); + } break; case SEOS: annotateSeos(explanation, sizeof(explanation), frame, data_len, hdr->isResponse); @@ -905,7 +1048,17 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr annotateMfPlus(explanation, sizeof(explanation), frame, data_len); break; case TOPAZ: - annotateTopaz(explanation, sizeof(explanation), frame, data_len); + if (topaz_14a_frame) { + annotateIso14443a(explanation, sizeof(explanation), frame, data_len, hdr->isResponse); + } else if (topaz_operand) { + snprintf(explanation, sizeof(explanation), "operand"); + } else { + annotateTopaz(explanation, sizeof(explanation), frame, data_len); + if (topaz_cut) { + size_t n = strlen(explanation); + snprintf(explanation + n, sizeof(explanation) - n, " (incomplete)"); + } + } break; case ISO_7816_4: // trace type "7816" is the contact reader, i.e. `smart list` @@ -1099,7 +1252,7 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr // handle partial bytes. The parity array[0] is used to store number of left over bits from NBYTES // This part prints the number of bits in the trace entry for hitag. - uint8_t nbits = parityBytes[0]; + uint8_t nbits = hitag_last_bits; annotateHitag2(explanation, sizeof(explanation), ht2plain, n, nbits, hdr->isResponse, NULL, 0, true); // iceman: colorise crc bytes here will need a refactor of code from above. @@ -1110,7 +1263,7 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr // Bit count goes in the Bit column, same as the frame above. // One leading space so the hex lines up under "Data". - if (n == 1) { + if ((n == 1) && (nbits != 0)) { snprintf(line[0], 120, " %02X ", ht2plain[0] >> (8 - nbits)); } else { snprintf(line[0], 120, " %02X ", ht2plain[0]); @@ -1125,13 +1278,7 @@ static uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *tr num_lines = MIN((n - 1) / TRACE_MAX_HEX_BYTES + 1, TRACE_MAX_HEX_BYTES); char plain_nbits[8] = {0}; - if (n == 1) { - snprintf(plain_nbits, sizeof(plain_nbits), "%3u ", nbits); - } else if (nbits == 0) { - snprintf(plain_nbits, sizeof(plain_nbits), "%3u ", (uint16_t)(n * 8)); - } else { - snprintf(plain_nbits, sizeof(plain_nbits), "%3u ", (uint16_t)(((n - 1) * 8) + nbits)); - } + snprintf(plain_nbits, sizeof(plain_nbits), "%3u ", hitag_frame_bits(n, nbits)); for (int j = 0; j < num_lines ; j++) { bool last = (j == num_lines - 1); @@ -1776,10 +1923,13 @@ int CmdTraceList(const char *Cmd) { } PrintAndLogEx(NORMAL, "------------+------------+-----+-------------------------------------------------------------------------+-----+--------------------"); - // clean authentication data used with the mifare classic decrypt fct - if (protocol == ISO_14443A || protocol == PROTO_MIFARE || protocol == PROTO_MFPLUS) { + // clean authentication data used with the mifare classic decrypt fct, + // and the anticollision state of the ISO14443-A checks + if (protocol == ISO_14443A || protocol == PROTO_MIFARE || protocol == PROTO_MFPLUS || + protocol == MFDES || protocol == LTO || protocol == SEOS || protocol == TOPAZ) { ClearAuthData(); } + gs_topaz_14a_exchange = false; // reset hitag state machine if (protocol == PROTO_HITAG1 || protocol == PROTO_HITAG2 || protocol == PROTO_HITAGS || protocol == PROTO_HITAGU) { diff --git a/tools/pm3_tests.sh b/tools/pm3_tests.sh index 45ec10933..2be27a9e1 100755 --- a/tools/pm3_tests.sh +++ b/tools/pm3_tests.sh @@ -484,6 +484,12 @@ while true; do if ! CheckExecute "trace list 7816 T=0 apdu" "$CLIENTBIN -c 'trace load -f traces/smart_7816.trace; trace list -1 -t 7816;'" "A0 A4 00 00 02 3F 00 .*SELECT FILE"; then break; fi if ! CheckExecute "trace list 7816 getresp" "$CLIENTBIN -c 'trace load -f traces/smart_7816.trace; trace list -1 -t 7816;'" "A0 C0 00 00 22 .*GET RESPONSE"; then break; fi if ! CheckExecute "trace list 7816 T=1 block" "$CLIENTBIN -c 'trace load -f traces/smart_7816.trace; trace list -1 -t 7816;'" "I-block N\(S\)=0"; then break; fi + if ! CheckExecute "trace list 14a five byte answer" "$CLIENTBIN -c 'trace load -f traces/hf_visa_apple_ecp.trace; trace list -1 -t 14a;'" "Tag [|]03 69 86 03 19 +[|] ok"; then break; fi + if ! CheckExecute "trace list 14b slots" "$CLIENTBIN -c 'trace load -f traces/hf_14b_reader.trace; trace list -1 -t 14b;'" "05 00 08 39 73 +[|] ok [|] 1 slot WUPB"; then break; fi + if ! CheckExecute "trace list 14b xerox wup2" "$CLIENTBIN -c 'trace load -f traces/hf_14b_xerox_info.trace; trace list -1 -t 14b;'" "5D 37 21 71 71 +[|] ok [|] XEROX WUP2"; then break; fi + if ! CheckExecute "trace list 14b slot nack" "$CLIENTBIN -c 'trace load -f traces/hf_14b_xerox_info.trace; trace list -1 -t 14b;'" "Rdr [|]A7 +[|] +[|] Slot 3 NACK"; then break; fi + if ! CheckExecute "trace list seos pps crc" "$CLIENTBIN -c 'trace load -f traces/hf_seos_sniff_fc54_cn64001.trace; trace list -1 -t seos;'" "D0 11 00 52 A6 +[|] ok [|] PPS"; then break; fi + if ! CheckExecute "trace list ht2 long frames" "$CLIENTBIN -c 'trace load -f traces/hf_visa_apple_normal.trace; trace list -1 -t ht2;'" "131146992 [|] +131147472 [|] Rdr [|] 07 +[|] +8 [|]"; then break; fi if ! CheckExecute "smart pps help" "$CLIENTBIN -c 'smart pps -h'" "select protocol T=1"; then break; fi if ! CheckExecute "smart raw help t1" "$CLIENTBIN -c 'smart raw -h'" "use protocol T=1"; then break; fi if ! CheckExecute "nfc decode test oob" "$CLIENTBIN -c 'nfc decode -d DA2010016170706C69636174696F6E2F766E642E626C7565746F6F74682E65702E6F6F62301000649201B96DFB0709466C65782032'" "Flex 2"; then break; fi