#!/usr/bin/env bash
# check-visio-cve-2026-112732.sh: offline, read-only triage of one Click-to-Run Office installation against the fixed builds
# for CVE-2026-112732 (Microsoft Office Visio remote code execution, CWE-415 double free; Microsoft CVSS 3.1 9.8 Critical).
# Takes a version string (with an optional channel and product IDs), or reads a saved registry export (.reg) of
# HKLM\SOFTWARE\Microsoft\Office\ClickToRun. It never connects to anything, never writes a file, runs no program, and
# prints only versions, channel names, product IDs and cleaned file names. Pack 1.0.0. Bash and PowerShell
# (Check-VisioCve2026112732.ps1) give the same output and exit code; only the PowerShell checker can read a live registry.
#
# Version: Microsoft documents OfficePackageVersion under ClickToRun\Inventory\Office\16.0 as the installed build and
# says VersionToReport (ClickToRun\Configuration) is a telemetry value that can report a newer build than is installed,
# so VersionToReport is used only as a fallback and never yields FIXED. Channel: UpdateChannel, then CDNBaseUrl
# (ClickToRun\Configuration), mapped with the six channel URLs Microsoft documents. Product IDs: OfficeProductReleaseIds
# (Inventory), then ProductReleaseIds (Configuration, [UNVERIFIED] as an input). Fixed builds: MSRC and the 10 Nov 2026
# Office security release notes.
set -u
export LC_ALL=C

ADV="https://msrc.microsoft.com/update-guide/vulnerability/CVE-2026-112732"
MAX_FILE=8388608
MAX_LINE=65536
CFG='hkey_local_machine\software\microsoft\office\clicktorun\configuration'
INV='hkey_local_machine\software\microsoft\office\clicktorun\inventory\office\16.0'

usage() {
  echo 'Usage: check-visio-cve-2026-112732.sh (--reg-file F | --version V [--channel C] [--products P] | --local)'
  echo 'Offline and read-only. F is a .reg export of HKLM\SOFTWARE\Microsoft\Office\ClickToRun. V is like 16.0.20527.20196. C is current, monthly-enterprise, semi-annual, ltsc2021, ltsc2024 or a channel URL. P is product IDs separated by commas. Exit codes: 0 FIXED, 2 AFFECTED, 3 REVIEW, 5 USAGE OR READ ERROR (1 and 4 are not used).'
}
die() { echo "ERROR: $*"; exit 5; }
clean_name() { local b=${1##*/}; [ -n "$b" ] || b=$1; printf '%s' "$b" | tr -c '[:alnum:]._-' '?' | head -c 64; }
trim() { local s=$1; s=${s#"${s%%[![:space:]]*}"}; s=${s%"${s##*[![:space:]]}"}; printf '%s' "$s"; }

MODE=""; FILE=""; VSTR=""; CSTR=""; PSTR=""; HASC=0; HASP=0
two() { [ -z "$MODE" ] || die "give only one of reg-file, version and local"; }
while [ $# -gt 0 ]; do
  case $1 in
    --reg-file|-RegFile) { [ $# -ge 2 ] && [ -n "$2" ]; } || die "reg-file option needs a value"; two; FILE=$2; MODE=f; shift 2;;
    --version|-Version) [ $# -ge 2 ] || die "version option needs a value"; two; VSTR=$2; MODE=v; shift 2;;
    --channel|-Channel) [ $# -ge 2 ] || die "channel option needs a value"; CSTR=$2; HASC=1; shift 2;;
    --products|-Products) [ $# -ge 2 ] || die "products option needs a value"; PSTR=$2; HASP=1; shift 2;;
    --local|-Local) two; MODE=l; shift;;
    -h|--help|-Help) usage; exit 5;;
    *) die "unknown option: $(clean_name "$1")";;
  esac
done
[ -n "$MODE" ] || { usage; exit 5; }
if [ "$MODE" != v ] && { [ "$HASC" = 1 ] || [ "$HASP" = 1 ]; }; then die "channel and products go with the version option only"; fi
[ "$MODE" = l ] && die "the local registry is not available on this system; export it with reg export and use the reg-file option"

chan_name() { # $1 lower-case GUID or keyword; prints a channel name or nothing
  case $1 in
    492350f6-3a01-4f97-b9c0-c7c6ddf67d60|current) echo "Current Channel";;
    55336b82-a18d-4dd6-b5f6-9e5095c314a6|monthly-enterprise) echo "Monthly Enterprise Channel";;
    7ffbc6bf-bc32-4f92-8982-f9dd17fd3114|semi-annual) echo "Semi-Annual Enterprise Channel";;
    64256afe-f5d9-4f86-8936-8840a6a4f5be) echo "Current Channel (Preview)";;
    b8f9b850-328d-4355-9145-c59439a0c4cf) echo "Semi-Annual Enterprise Channel (Preview)";;
    5440fd1f-7ecb-4221-8110-145efaa6372f) echo "Beta Channel";;
    ltsc2021) echo "Office LTSC 2021";;
    ltsc2024) echo "Office LTSC 2024";;
  esac
}
parse_chan() { # $1 raw; sets CHAN
  local s; s=$(trim "$1"); s=${s,,}; CHAN=""
  if [ "${#s}" -le 120 ] && [[ "$s" =~ ^https?://officecdn\.microsoft\.com/pr/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/?$ ]]; then CHAN=$(chan_name "${BASH_REMATCH[1]}")
  elif [[ "$s" =~ ^(current|monthly-enterprise|semi-annual|ltsc2021|ltsc2024)$ ]]; then CHAN=$(chan_name "$s"); fi
  [ -n "$CHAN" ] || CHAN="unrecognised"
}
parse_prod() { # $1 raw; sets PRODS VISIO
  local raw=$1 t n=0 out="" vis=""
  local -; set -f
  local IFS=,
  for t in $raw; do
    t=$(trim "$t")
    [[ "$t" =~ ^[A-Za-z0-9]{1,64}$ ]] || continue
    n=$((n + 1)); [ "$n" -le 20 ] || break
    out="${out:+$out,}$t"
    [[ "$t" == Visio* ]] && vis="${vis:+$vis,}$t"
  done
  PRODS=${out:-none readable}
  if [ -n "$vis" ]; then VISIO="in product IDs ($vis)"; elif [ -n "$out" ]; then VISIO="not in product IDs"; else VISIO="unknown"; fi
}

# Fixed builds (16.0.<build>.<revision>), from the 10 Nov 2026 Office security release notes and MSRC.
classify() { # $1 version, $2 kind of value (installed|telemetry|given); uses CHAN; sets SHOWV W WHY
  local s b r lo hi need
  s=$(trim "$1")
  if [ "${#s}" -le 40 ] && [[ "$s" =~ ^16\.0\.([0-9]{4,5})\.([0-9]{1,5})$ ]]; then
    b=$((10#${BASH_REMATCH[1]})); r=$((10#${BASH_REMATCH[2]})); SHOWV=$s; lo=""; hi=""; need=""
    case $b in
      20527) lo=20192; hi=20196; case $CHAN in "Current Channel") need=20196;; "Monthly Enterprise Channel") need=20194;; "Semi-Annual Enterprise Channel") need=20192;; esac;;
      20430) lo=20262; hi=20264; case $CHAN in "Monthly Enterprise Channel") need=20264;; "Semi-Annual Enterprise Channel") need=20262;; esac;;
      20326) lo=20318; hi=20320; case $CHAN in "Monthly Enterprise Channel") need=20320;; "Semi-Annual Enterprise Channel") need=20318;; esac;;
      17932) lo=21140; hi=21140;;
      14334) lo=21048; hi=21048;;
    esac
    if [ -n "$lo" ]; then
      [ -n "$need" ] || need=$hi
      if [ "$r" -ge "$need" ]; then W=FIXED; WHY="build $b.$r is at or above the fixed build $b.$need"
      elif [ "$r" -lt "$lo" ]; then W=AFFECTED; WHY="build $b.$r is below the fixed build $b.$need"
      else W=REVIEW; WHY="build $b.$r is fixed for some channels but below $b.$need; confirm the channel"; fi
    elif [ "$b" -gt 20527 ]; then W=REVIEW; WHY="build $b is newer than the 10 Nov 2026 builds; confirm it is at or above your channel's November build"
    elif [ "$b" -gt 14334 ]; then W=AFFECTED; WHY="build $b is older than every 10 Nov 2026 fixed build and is not one Microsoft lists"
    else W=REVIEW; WHY="build $b is not Microsoft 365 Apps, Office LTSC 2021 or Office LTSC 2024 as MSRC lists them"; fi
    if [ "$2" = telemetry ] && [ "$W" = FIXED ]; then W=REVIEW; WHY="$WHY, but VersionToReport is a telemetry value Microsoft says can be newer than the installed build; read OfficePackageVersion"; fi
  else
    SHOWV="unreadable"; W=REVIEW; WHY="the version is not like 16.0.20527.20196"
  fi
}


if [ "$MODE" = v ]; then
  LABEL="version string"
  if [ "$HASC" = 1 ]; then parse_chan "$CSTR"; CSRC="channel option"; else CHAN="not given"; CSRC=""; fi
  if [ "$HASP" = 1 ]; then parse_prod "$PSTR"; else PRODS="not given"; VISIO="unknown"; fi
  classify "$VSTR" given; VSRC="version option"
else
  { [ -r "$FILE" ] && [ ! -d "$FILE" ]; } || die "file not found or unreadable: $(clean_name "$FILE")"
  sz=$(head -c "$((MAX_FILE + 1))" < "$FILE" 2>/dev/null | wc -c) || die "file unreadable: $(clean_name "$FILE")"
  [ "$sz" -le "$MAX_FILE" ] || die "file too large: $(clean_name "$FILE")"
  LABEL="reg file $(clean_name "$FILE")"
  sec=""; first=1
  declare -A V=()
  while IFS= read -r l || [ -n "$l" ]; do
    if [ "$first" = 1 ]; then l=${l#$'\xff\xfe'}; l=${l#$'\xef\xbb\xbf'}; first=0; fi
    l=${l%$'\r'}
    [ "${#l}" -le "$MAX_LINE" ] || continue
    if [[ "$l" =~ ^\[(.*)\]$ ]]; then s=${BASH_REMATCH[1]}; s=${s,,}
      if [ "$s" = "$CFG" ]; then sec=c; elif [ "$s" = "$INV" ]; then sec=i; else sec=""; fi; continue; fi
    [ -n "$sec" ] || continue
    if [[ "$l" =~ ^\"([A-Za-z0-9]+)\"=\"([^\"\\]*)\"$ ]]; then
      k="$sec:${BASH_REMATCH[1],,}"; [ -n "${V[$k]+x}" ] || V[$k]=${BASH_REMATCH[2]}
    fi
  done < <(head -c "$MAX_FILE" < "$FILE" | tr -d '\000')
  if [ -n "${V[c:updatechannel]+x}" ]; then parse_chan "${V[c:updatechannel]}"; CSRC="UpdateChannel"
  elif [ -n "${V[c:cdnbaseurl]+x}" ]; then parse_chan "${V[c:cdnbaseurl]}"; CSRC="CDNBaseUrl"
  else CHAN="not found"; CSRC=""; fi
  if [ -n "${V[i:officeproductreleaseids]+x}" ]; then parse_prod "${V[i:officeproductreleaseids]}"; PSRC="OfficeProductReleaseIds"
  elif [ -n "${V[c:productreleaseids]+x}" ]; then parse_prod "${V[c:productreleaseids]}"; PSRC="ProductReleaseIds"
  else PRODS="not found"; VISIO="unknown"; PSRC=""; fi
  if [ -n "${V[i:officepackageversion]+x}" ]; then classify "${V[i:officepackageversion]}" installed; VSRC="OfficePackageVersion"
  elif [ -n "${V[c:versiontoreport]+x}" ]; then classify "${V[c:versiontoreport]}" telemetry; VSRC="VersionToReport, telemetry"
  else SHOWV="not found"; W=REVIEW; WHY="no OfficePackageVersion or VersionToReport in the ClickToRun keys of this export"; VSRC=""; fi
fi
case $W in FIXED) RC=0;; AFFECTED) RC=2;; *) RC=3;; esac
echo "TARGET 1: $LABEL"
echo "  VERSION: $SHOWV${VSRC:+ ($VSRC)}"
echo "  CHANNEL: $CHAN${CSRC:+ ($CSRC)}"
echo "  PRODUCTS: $PRODS${PSRC:+ ($PSRC)}"
echo "  VISIO: $VISIO"
echo "  RESULT: $W ($WHY)"
echo "  CVE-2026-112732 (Microsoft CVSS 3.1: 9.8 Critical): $W"
echo "NOTE: fixed builds (10 Nov 2026): Current Channel 16.0.20527.20196; Monthly Enterprise Channel 16.0.20527.20194, 16.0.20430.20264, 16.0.20326.20320; Semi-Annual Enterprise Channel 16.0.20527.20192, 16.0.20430.20262, 16.0.20326.20318; Office LTSC 2024 16.0.17932.21140; Office LTSC 2021 16.0.14334.21048."
echo "NOTE: MSRC lists Microsoft 365 Apps for Enterprise, Office LTSC 2021 and Office LTSC 2024 (32-bit and 64-bit). Microsoft's FAQ describes an attachment processed for indexing on an Exchange server, but Exchange Server is not in the product list, and this checker does not assess Exchange."
echo "NOTE: no exploitation reported by Microsoft or CISA as of 24 Nov 2026 (Microsoft: Publicly disclosed No, Exploited No, Exploitation Less Likely). CVE-2026-112732 is not in CISA KEV (catalogue 2026.11.24; KEV due dates bind US federal agencies only, BOD 26-04). Patch now regardless."
echo "SOURCE: $ADV"
echo "RESULT: $W (exit $RC)"
exit "$RC"
