#!/usr/bin/env bash
# check-expressway-76466.sh: offline, read-only triage of Cisco Expressway CVE-2026-76466 (cisco-sa-vcs-static-cred-hr7EWZ8t).
# Reads an Expressway release string or a saved "xstatus" export, and optionally a saved "xconfiguration" export or an
# explicit --clustered answer. It never connects to anything, never runs a command on the Expressway, never writes a
# file and never prints a configuration value (only the release, a count of peer addresses and a cleaned file name).
# Pack 1.0.0. Bash and PowerShell (Check-Expressway76466.ps1) give the same output and exit code.
#
# Release ranges from the Cisco advisory v1.0 (21 Oct 2026): "X14 and earlier: Migrate to a fixed release." and
# "X15.5.3 and earlier: X15.5.20". Cisco: affected only when the device is configured in a cluster. No workarounds.
# Export formats follow Cisco TAC document 118675 (VCS X7.2.1 example): xstatus 'Version: "X7.2.1"' under Software,
# xconfiguration '*c xConfiguration Alternates Peer 1 Address: ""'. The X15 layout is UNVERIFIED; anything the
# checker cannot read gives UNKNOWN, never FIXED.
set -u
export LC_ALL=C

ADV="https://sec.cloudapps.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-vcs-static-cred-hr7EWZ8t"
MAX_FILE=4194304
MAX_LINE=65536

usage() {
  echo 'Usage: check-expressway-76466.sh (--version-string S | --status-file F) [--config-file F | --clustered yes|no]'
  echo 'Offline and read-only. Exit codes: 0 FIXED, 2 AFFECTED, 3 REVIEW OR UNKNOWN, 4 OUTSIDE CISCO CLUSTER CONDITION (still update), 5 USAGE OR READ ERROR, 6 MIGRATE (X14 or earlier) (1 is not used).'
}
die() { echo "ERROR: $*"; exit 5; }
clean_name() { local b=${1##*/}; [ -n "$b" ] || b=$1; printf '%s' "$b" | tr -c '[:alnum:]._-' '?' | head -c 80; }
# read_lines FILE: fills LINES with the file's lines (NUL becomes a line break, a trailing CR is dropped).
read_lines() {
  local f=$1 sz l
  { [ -r "$f" ] && [ ! -d "$f" ]; } || die "file not found or unreadable: $(clean_name "$f")"
  sz=$(head -c "$((MAX_FILE + 1))" < "$f" 2>/dev/null | wc -c) || die "file unreadable: $(clean_name "$f")"
  [ "$sz" -le "$MAX_FILE" ] || die "file too large: $(clean_name "$f")"
  LINES=()
  while IFS= read -r l || [ -n "$l" ]; do LINES+=("${l%$'\r'}"); done < <(head -c "$MAX_FILE" < "$f" | tr '\000' '\n')
}

VSTR=""; VSET=0; SFILE=""; CFILE=""; CLU=""
need() { [ "$1" -ge 2 ] || die "$2 option needs a value"; }
while [ $# -gt 0 ]; do
  case $1 in
    --version-string|-VersionString) need $# version-string; VSTR=$2; VSET=1; shift 2;;
    --status-file|-StatusFile) need $# status-file; SFILE=$2; [ -n "$SFILE" ] || die "status-file option needs a value"; shift 2;;
    --config-file|-ConfigFile) need $# config-file; CFILE=$2; [ -n "$CFILE" ] || die "config-file option needs a value"; shift 2;;
    --clustered|-Clustered) need $# clustered; CLU=$2; shift 2
      case $CLU in yes|no) ;; *) die "clustered must be yes or no";; esac;;
    -h|--help|-Help) usage; exit 5;;
    *) die "unknown option: $(clean_name "$1")";;
  esac
done
[ -z "$SFILE" ] || [ "$VSET" = 0 ] || die "give --version-string or --status-file, not both"
[ -n "$SFILE" ] || [ "$VSET" = 1 ] || { usage; exit 5; }
[ -z "$CFILE" ] || [ -z "$CLU" ] || die "give --config-file or --clustered, not both"

# ---- Release ----
REL=""; VSRC=""; VNOTE=""
REL_RE='^X?([0-9]{1,2})\.([0-9]{1,2})(\.([0-9]{1,3}))?$'
if [ "$VSET" = 1 ]; then
  VSRC="version string"
  s=$VSTR; s=${s#"${s%%[![:space:]]*}"}; s=${s%"${s##*[![:space:]]}"}
  if [ "${#s}" -le 32 ] && [[ "$s" =~ $REL_RE ]]; then REL="${BASH_REMATCH[1]}.${BASH_REMATCH[2]}.${BASH_REMATCH[4]:-0}"; else VNOTE="the version string is not an Expressway release such as X15.5.3"; fi
else
  read_lines "$SFILE"; VSRC="status file $(clean_name "$SFILE")"
  FOUND=()
  for l in "${LINES[@]}"; do
    [ "${#l}" -le "$MAX_LINE" ] || continue
    rest=$l
    while [[ "$rest" =~ Version:[[:space:]]*\"X([0-9]{1,2})\.([0-9]{1,2})(\.([0-9]{1,3}))?\"(.*)$ ]]; do
      v="${BASH_REMATCH[1]}.${BASH_REMATCH[2]}.${BASH_REMATCH[4]:-0}"; rest=${BASH_REMATCH[5]}
      dup=0; for x in "${FOUND[@]+"${FOUND[@]}"}"; do [ "$x" = "$v" ] && dup=1; done
      [ "$dup" = 1 ] || FOUND+=("$v")
    done
  done
  if [ "${#FOUND[@]}" -eq 1 ]; then REL=${FOUND[0]}
  elif [ "${#FOUND[@]}" -gt 1 ]; then VNOTE="the status file holds ${#FOUND[@]} different Expressway releases; check one node at a time"
  else VNOTE="no line like Version: \"X15.5.3\" in the status file"; fi
fi

# ---- Release verdict ----
# RC_REL: 2 affected, 6 migrate, 0 fixed, 3 review or unknown
if [ -z "$REL" ]; then
  echo "VERSION: UNKNOWN ($VNOTE; from $VSRC)"
  RTXT="UNKNOWN (release not read; read the release from the web interface or xstatus and run again with --version-string)"; RC_REL=3
else
  IFS=. read -r M m p <<< "$REL"; M=$((10#$M)); m=$((10#$m)); p=$((10#$p)); REL="$M.$m.$p"
  echo "VERSION: X$REL (from $VSRC)"
  if [ "$M" -le 14 ]; then RTXT="MIGRATE (X14 and earlier: Cisco says migrate to a fixed release; first fixed X15.5.20)"; RC_REL=6
  elif [ "$M" -eq 15 ] && { [ "$m" -lt 5 ] || { [ "$m" -eq 5 ] && [ "$p" -le 3 ]; }; }; then RTXT="AFFECTED (X$REL is at or below X15.5.3; first fixed X15.5.20)"; RC_REL=2
  elif [ "$M" -eq 15 ] && [ "$m" -eq 5 ] && [ "$p" -lt 20 ]; then RTXT="REVIEW (X$REL is after X15.5.3 but before the first fixed X15.5.20; Cisco's table does not list it)"; RC_REL=3
  elif [ "$M" -eq 15 ] && [ "$m" -eq 5 ]; then RTXT="FIXED (X$REL is X15.5.20 or later in the X15.5 train)"; RC_REL=0
  else RTXT="REVIEW (X$REL is newer than the X15.5 train in Cisco's table; confirm the fix in Cisco's release notes)"; RC_REL=3; fi
fi
echo "RELEASE: $RTXT"

# ---- Cluster ----
# CL: yes, no or unknown
CL=unknown
if [ -n "$CLU" ]; then
  CL=$CLU
  if [ "$CL" = yes ]; then echo "CLUSTER: CLUSTERED (stated with --clustered yes)"; else echo "CLUSTER: NOT CLUSTERED (stated with --clustered no)"; fi
elif [ -n "$CFILE" ]; then
  read_lines "$CFILE"; CSRC="configuration file $(clean_name "$CFILE")"
  seen=0; set_n=0
  for l in "${LINES[@]}"; do
    [ "${#l}" -le "$MAX_LINE" ] || continue
    rest=$l
    while [[ "$rest" =~ xConfiguration[[:space:]]+Alternates[[:space:]]+(Peer[[:space:]]+[1-6][[:space:]]+Address|[1-6][[:space:]]+Peer[[:space:]]+Address):[[:space:]]*\"([^\"]*)\"(.*)$ ]]; do
      seen=$((seen + 1)); val=${BASH_REMATCH[2]}; rest=${BASH_REMATCH[3]}
      val=${val//[[:space:]]/}; [ -z "$val" ] || set_n=$((set_n + 1))
    done
  done
  if [ "$seen" -eq 0 ]; then echo "CLUSTER: UNKNOWN (no Alternates Peer Address lines in the $CSRC)"
  elif [ "$set_n" -gt 0 ]; then CL=yes; echo "CLUSTER: CLUSTERED ($set_n of $seen peer address lines set in the $CSRC; addresses not shown)"
  else CL=no; echo "CLUSTER: NOT CLUSTERED (all $seen peer address lines empty in the $CSRC)"; fi
else
  echo "CLUSTER: UNKNOWN (no --config-file or --clustered given; treated as clustered)"
fi

# ---- Result ----
if [ "$RC_REL" = 0 ]; then RES="FIXED"; RC=0
elif [ "$RC_REL" = 3 ]; then RES="REVIEW"; RC=3
elif [ "$CL" = no ]; then RES="OUTSIDE CISCO CLUSTER CONDITION (vulnerable release, not clustered; update anyway, and re-run if clustering is enabled)"; RC=4
elif [ "$RC_REL" = 6 ]; then RES="MIGRATE (vulnerable release, clustered or cluster state unknown)"; RC=6
else RES="AFFECTED (vulnerable release, clustered or cluster state unknown)"; RC=2; fi
echo "NOTE: Cisco advisory v1.0 (21 Oct 2026): affected only when configured in a cluster; there are no workarounds; restricting cluster ports (TCP 4371 and 4372 from X8.8 onwards) to cluster peers reduces exposure but is not a fix."
echo "NOTE: exploitation reported via SecurityWeek (19 Nov 2026); Cisco's advisory v1.0 reports no known malicious use as of 21 Oct 2026. Patch now regardless."
[ "$RC" = 0 ] || echo "NOTE: X15.5.20 also addresses Cisco's Expressway hardening advisory (cisco-sa-exwy-hardening-FhcqBVcs), which applies regardless of configuration."
if [ "$RC" = 2 ] || [ "$RC" = 6 ]; then echo "NOTE: if a vulnerable clustered node was reachable on TCP 4371 or 4372 from outside its peers, run the hunts in kql/ and review the node for compromise."; fi
echo "SOURCE: $ADV"
echo "RESULT: $RES (exit $RC)"
exit "$RC"
