peass-ng/PEASS-ng · error · IndexOutOfRangeException

invalid offset and/or length specified

Error message

invalid offset and/or length specified

What it means

HexEncoder.DecodeStrict throws IndexOutOfRangeException with 'invalid offset and/or length specified' when off < 0, len < 0, or off > str.Length - len — i.e. the requested slice [off, off+len) falls outside the string. This guards the offset/length pair before any table lookups.

Source

Thrown at winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/util/encoders/HexEncoder.cs:221

                }

                length++;
            }

            if (bufOff > 0)
            {
                outStream.Write(buf, 0, bufOff);
            }

            return length;
        }

        internal byte[] DecodeStrict(string str, int off, int len)
        {
            if (null == str)
                throw new ArgumentNullException("str");
            if (off < 0 || len < 0 || off > (str.Length - len))
                throw new IndexOutOfRangeException("invalid offset and/or length specified");
            if (0 != (len & 1))
                throw new ArgumentException("a hexadecimal encoding must have an even number of characters", "len");

            int resultLen = len >> 1;
            byte[] result = new byte[resultLen];

            int strPos = off;
            for (int i = 0; i < resultLen; ++i)
            {
                byte b1 = decodingTable[str[strPos++]];
                byte b2 = decodingTable[str[strPos++]];

                if ((b1 | b2) >= 0x80)
                    throw new IOException("invalid characters encountered in Hex data");

                result[i] = (byte)((b1 << 4) | b2);
            }
            return result;

View on GitHub (pinned to 53fb989abc)

Solutions

  1. Validate off >= 0, len >= 0 and off + len <= str.Length before calling.
  2. Recompute the slice bounds from the actual string length rather than a trusted header.
  3. Clamp or trim len to str.Length - off when the input may be shorter than declared.

Example fix

// before
var bytes = DecodeStrict(s, off, len); // off+len > s.Length
// after
if (off < 0 || len < 0 || off > s.Length - len)
    throw new ArgumentOutOfRangeException(nameof(off), "slice out of bounds");
var bytes = DecodeStrict(s, off, len);
Defensive patterns

Strategy: validation

Validate before calling

bool sliceValid = off >= 0 && len >= 0 && off <= str.Length - len;

Type guard

static bool InBounds(string s, int off, int len) => s != null && off >= 0 && len >= 0 && off <= s.Length - len;

Try / catch

try { DecodeStrict(s, off, len); } catch (IndexOutOfRangeException ex) when (ex.Message.Contains("invalid offset")) { /* re-parse framing */ }

Prevention

When it happens

Trigger: Calling strict decode with off < 0, a negative len, or off+len exceeding the string length (e.g. off computed from a previous parse, or len taken from a length header larger than the actual data).

Common situations: Parsing framing protocols where a length field is corrupt or misinterpreted (endianness mismatch); off-by-one on offsets; reusing offsets after string mutation/trimming.

Related errors


AI-assisted analysis of peass-ng/PEASS-ng@53fb989abc (2026-09-02). Data as JSON: /api/errors/fe351474524db897. Report an issue: GitHub.