From 61f5493f554c5f5b5f4ec69ee1c8311d7b5c945b Mon Sep 17 00:00:00 2001 From: Travis Weathers Date: Thu, 18 Dec 2025 14:43:15 -0500 Subject: [PATCH] Fix iCLASS block 7 decoder to strip sentinel bit When decoding iCLASS block 7 Wiegand data, the decoder was showing incorrect bit counts (27 for 26-bit, 36 for 35-bit, etc.) because it wasn't stripping the sentinel bit that's added during encoding. This fix: - Detects when binary string has sentinel bit (leading '1' making length one more than common formats) - Strips the sentinel bit and recreates u96 values from corrected binary string - Uses corrected length for wiegand decoding Fixes decoding for 26-bit, 35-bit, and other formats where sentinel bit causes incorrect format detection. --- client/src/cmdhficlass.c | 56 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/client/src/cmdhficlass.c b/client/src/cmdhficlass.c index d4bc1da45..d876eddce 100644 --- a/client/src/cmdhficlass.c +++ b/client/src/cmdhficlass.c @@ -1511,11 +1511,36 @@ static void iclass_decode_credentials(uint8_t *data) { char binstr[64 + 1]; hextobinstring(binstr, hexstr); char *pbin = binstr; + // Strip leading zeros while (strlen(pbin) && *(++pbin) == '0'); + + size_t binlen = strlen(pbin); + + // Check if we have a sentinel bit (leading '1' that makes length one more than common formats) + // Common formats: 26, 30, 33, 34, 35, 36, 37, 46, 48 + // If we have 27, 31, 34, 35, 36, 37, 38, 47, 49 bits and it starts with '1', + // it's likely a sentinel bit that should be stripped + if (binlen > 0 && pbin[0] == '1' && + (binlen == 27 || binlen == 31 || binlen == 34 || binlen == 35 || + binlen == 36 || binlen == 37 || binlen == 38 || binlen == 47 || binlen == 49)) { + // Strip the sentinel bit by recreating u96 from binary string without leading '1' + char *corrected_bin = pbin + 1; // Skip the leading '1' + size_t corrected_len = strlen(corrected_bin); + + // Recreate u96 values from corrected binary string + top = 0; + mid = 0; + bot = 0; + binstring_to_u96(&top, &mid, &bot, corrected_bin); + + pbin = corrected_bin; + binlen = corrected_len; + } - PrintAndLogEx(SUCCESS, "Binary... " _GREEN_("%s") " ( %zu )", pbin, strlen(pbin)); + PrintAndLogEx(SUCCESS, "Binary... " _GREEN_("%s") " ( %zu )", pbin, binlen); PrintAndLogEx(NORMAL, ""); - decode_wiegand(top, mid, bot, 0); + // Use the corrected length (without sentinel) for decoding + decode_wiegand(top, mid, bot, (int)binlen); } } } @@ -2927,12 +2952,37 @@ static int CmdHFiClass_ReadBlock(const char *Cmd) { char binstr[64 + 1]; hextobinstring(binstr, hexstr); char *pbin = binstr; + // Strip leading zeros while (strlen(pbin) && *(++pbin) == '0'); + + size_t binlen = strlen(pbin); + + // Check if we have a sentinel bit (leading '1' that makes length one more than common formats) + // Common formats: 26, 30, 33, 34, 35, 36, 37, 46, 48 + // If we have 27, 31, 34, 35, 36, 37, 38, 47, 49 bits and it starts with '1', + // it's likely a sentinel bit that should be stripped + if (binlen > 0 && pbin[0] == '1' && + (binlen == 27 || binlen == 31 || binlen == 34 || binlen == 35 || + binlen == 36 || binlen == 37 || binlen == 38 || binlen == 47 || binlen == 49)) { + // Strip the sentinel bit by recreating u96 from binary string without leading '1' + char *corrected_bin = pbin + 1; // Skip the leading '1' + size_t corrected_len = strlen(corrected_bin); + + // Recreate u96 values from corrected binary string + top = 0; + mid = 0; + bot = 0; + binstring_to_u96(&top, &mid, &bot, corrected_bin); + + pbin = corrected_bin; + binlen = corrected_len; + } PrintAndLogEx(SUCCESS, " bin : %s", pbin); PrintAndLogEx(INFO, ""); PrintAndLogEx(INFO, "------------------------------ " _CYAN_("Wiegand") " -------------------------------"); - decode_wiegand(top, mid, bot, 0); + // Use the corrected length (without sentinel) for decoding + decode_wiegand(top, mid, bot, (int)binlen); } } else { PrintAndLogEx(INFO, "no credential found");