#!/usr/bin/env bash
# check-beyondtrust-bt2605.sh: compare a BeyondTrust Remote Support (RS) or Privileged Remote Access (PRA) version
# string with BeyondTrust advisory BT26-05 (CVE-2026-102764). Strictly read only and offline: it reads no file, opens
# no network connection, runs no program and prints only the version after a strict check. Pack 1.1.1.
# Bash and PowerShell (Check-BeyondTrustBT2605.ps1) give the same output and exit code.
# BeyondTrust BT26-05: Remote Support all versions <= 26.2.3 affected; fixed by the BT26-05-RS patch (v24.3.1 to
# v26.2.3) or Remote Support 26.3.1 and greater; versions older than 24.3.1 must upgrade before the patch applies.
# Usage: check-beyondtrust-bt2605.sh --product rs|pra --version-string 26.2.3
# Exit codes: 0 FIXED, 2 AFFECTED, 3 REVIEW OR UNKNOWN, 5 USAGE OR READ ERROR (1, 4 and 6 are not used).
set -u
export LC_ALL=C
ADV="https://www.beyondtrust.com/trust-center/security-advisories/bt26-05"
usage() { echo "usage: check-beyondtrust-bt2605.sh --product rs|pra --version-string VERSION"; echo "exit: 0 FIXED, 2 AFFECTED, 3 REVIEW OR UNKNOWN, 5 USAGE OR READ ERROR (1, 4 and 6 are not used)"; }
PROD=""; VSTR=""; seen_v=0
while [ $# -gt 0 ]; do
  case $1 in
    --product) [ $# -ge 2 ] || { echo "ERROR: product option needs a value"; usage; exit 5; }; PROD=$2; shift 2;;
    --version-string) [ $# -ge 2 ] || { echo "ERROR: version option needs a value"; usage; exit 5; }; VSTR=$2; seen_v=1; shift 2;;
    -h|--help) usage; exit 5;;
    *) echo "ERROR: unknown option"; usage; exit 5;;
  esac
done
case $PROD in rs|RS) PROD=rs;; pra|PRA) PROD=pra;; *) echo "ERROR: product must be rs or pra"; usage; exit 5;; esac
[ "$seen_v" = 1 ] || { echo "ERROR: version option needs a value"; usage; exit 5; }
s=$VSTR
while [[ "$s" == [[:space:]]* ]]; do s=${s#?}; done
while [[ "$s" == *[[:space:]] ]]; do s=${s%?}; done
SHOWV="unreadable"; RC=3
if [ "${#s}" -le 12 ] && [[ "$s" =~ ^([0-9]{2})\.([0-9]{1,2})\.([0-9]{1,2})$ ]]; then
  SHOWV=$s; M=$((10#${BASH_REMATCH[1]})); m=$((10#${BASH_REMATCH[2]})); p=$((10#${BASH_REMATCH[3]})); V=$((M * 10000 + m * 100 + p))
  if [ "$PROD" = pra ]; then
    VERD="REVIEW (BT26-05 lists Remote Support only; BeyondTrust says it released a precautionary patch for self-hosted PRA because PRA shares the affected code. Apply it and confirm in the /appliance interface)"
  elif [ "$V" -lt 240301 ]; then
    VERD="AFFECTED (older than 24.3.1; BeyondTrust: upgrade to a newer version before the BT26-05-RS patch can be applied)"; RC=2
  elif [ "$V" -le 260203 ]; then
    VERD="REVIEW (BeyondTrust: all versions up to 26.2.3 are affected unless the BT26-05-RS patch is installed; a version number cannot show the patch, so confirm it in the /appliance interface)"
  elif [ "$V" -ge 260301 ]; then
    VERD="FIXED (BeyondTrust lists Remote Support 26.3.1 and greater as fixed)"; RC=0
  else
    VERD="REVIEW (BT26-05 does not list this version; confirm with BeyondTrust)"
  fi
elif [ -z "$s" ]; then
  SHOWV="not given"; VERD="REVIEW (no version given)"
else
  VERD="REVIEW (the version is not like 26.2.3)"
fi
if [ "$PROD" = rs ]; then echo "PRODUCT: BeyondTrust Remote Support (self-hosted)"; else echo "PRODUCT: BeyondTrust Privileged Remote Access (self-hosted)"; fi
echo "VERSION: $SHOWV"
echo "RESULT: $VERD"
echo "NOTE: CVE-2026-102764 is in CISA KEV (added 16 Oct 2026, catalogue 2026.11.23). BeyondTrust: Remote Support SaaS instances were patched as of 9 Oct 2026."
echo "NOTE: if the appliance was exposed while unpatched, patching does not remove access an attacker already has; review its off-box logs."
echo "SOURCE: $ADV"
echo "EXIT: $RC"
exit "$RC"
