#!/usr/bin/env bash
# php2shell-check.sh: is this server's PHP patched for PHP2Shell (CVE-2026-109618, CVE-2026-109619)?
#
# Read-only. It runs each PHP binary with -v, reads installed package changelogs, and prints
# PATCHED, VULNERABLE or UNKNOWN for each one. It sends nothing anywhere and never tests the bug.
# Exit code: 0 if nothing is vulnerable, 1 if something is, 2 if nothing could be checked.
#
# Usage:  bash php2shell-check.sh                        # finds php, php-fpm and php-cgi on PATH
#         bash php2shell-check.sh /opt/cpanel/ea-php83/root/usr/bin/php /opt/plesk/php/8.2/bin/php
#
# From https://www.huzaifaa.club/blog/php2shell-ai-php-vulnerability (fixed versions as of 23 October 2026)

set -u
PATH="$PATH:/usr/sbin:/usr/local/sbin:/sbin"

# The first fixed release on each branch php.net still patches.
fixed_for() {
  case "$1" in
    8.5) echo 8.5.12 ;;
    8.4) echo 8.4.27 ;;
    8.3) echo 8.3.36 ;;
    8.2) echo 8.2.35 ;;
    *) echo '' ;;
  esac
}

# Is version $1 at least $2? Both are X.Y.Z.
version_ge() {
  local IFS=. i
  local -a a=($1) b=($2)
  for i in 0 1 2; do
    ((${a[i]:-0} > ${b[i]:-0})) && return 0
    ((${a[i]:-0} < ${b[i]:-0})) && return 1
  done
  return 0
}

# Distros often backport the fix without changing the number php -v prints (Ubuntu 24.04's fixed
# build still says 8.3.6), so a package whose changelog names both CVEs counts as patched.
# Prints the package name when it finds one.
distro_fixed() {
  local real pkg log
  real=$(readlink -f "$1" 2>/dev/null || echo "$1")
  if command -v dpkg >/dev/null 2>&1; then
    pkg=$(dpkg -S "$real" 2>/dev/null | head -n1 | cut -d: -f1)
    log="/usr/share/doc/$pkg/changelog.Debian.gz"
    if [ -n "$pkg" ] && [ -r "$log" ] && zgrep -q CVE-2026-109618 "$log" && zgrep -q CVE-2026-109619 "$log"; then
      echo "$pkg"
      return 0
    fi
  fi
  if command -v rpm >/dev/null 2>&1 && pkg=$(rpm -qf "$real" 2>/dev/null); then
    log=$(rpm -q --changelog "$pkg" 2>/dev/null)
    if grep -q CVE-2026-109618 <<<"$log" && grep -q CVE-2026-109619 <<<"$log"; then
      echo "$pkg"
      return 0
    fi
  fi
  return 1
}

# The PHP binaries to check: the arguments, or every php, php-fpm and php-cgi on PATH.
bins=()
if [ "$#" -gt 0 ]; then
  bins=("$@")
else
  while read -r name; do
    bins+=("$(command -v "$name")")
  done < <(compgen -c | grep -E '^php(-fpm|-cgi)?[0-9.]*$' | sort -u)
fi

echo "PHP2Shell check (CVE-2026-109618, CVE-2026-109619) on $(hostname)"
echo
vulnerable=0
checked=0
for bin in "${bins[@]}"; do
  [ -x "$bin" ] || { printf '%-32s %-9s %-11s %s\n' "$bin" '-' 'UNKNOWN' 'not an executable file'; continue; }
  version=$("$bin" -v 2>/dev/null | head -n1 | sed -n 's/^PHP \([0-9]*\.[0-9]*\.[0-9]*\).*/\1/p')
  if [ -z "$version" ]; then
    printf '%-32s %-9s %-11s %s\n' "$bin" '-' 'UNKNOWN' "couldn't read a version from -v"
    continue
  fi
  checked=$((checked + 1))
  branch=${version%.*}
  fixed=$(fixed_for "$branch")

  if [ -n "$fixed" ] && version_ge "$version" "$fixed"; then
    status=PATCHED note="$branch branch is fixed from $fixed"
  elif pkg=$(distro_fixed "$bin"); then
    status=PATCHED note="distro backport: $pkg's changelog lists both CVEs"
  elif [ -n "$fixed" ]; then
    status=VULNERABLE note="update to $fixed+ (or your distro's patched package), then restart PHP"
  elif [[ "$branch" =~ ^(7\.4|8\.0|8\.1)$ ]]; then
    status=VULNERABLE note="no php.net release for $branch: distro/LTS backport, PHP's source backport, or upgrade"
  else
    status=VULNERABLE note="no php.net fix for $branch: a paid LTS/ESM backport or upgrade; keep it offline until then"
  fi
  [ "$status" = VULNERABLE ] && vulnerable=$((vulnerable + 1))
  printf '%-32s %-9s %-11s %s\n' "$bin" "$version" "$status" "$note"
done

# Apache's mod_php can't be run on its own, so it's checked through its package only.
for mod in /usr/lib/apache2/modules/libphp*.so /usr/lib64/httpd/modules/libphp*.so /etc/httpd/modules/libphp*.so; do
  [ -e "$mod" ] || continue
  if pkg=$(distro_fixed "$mod"); then
    printf '%-32s %-9s %-11s %s\n' "$mod" '-' 'PATCHED' "distro backport: $pkg's changelog lists both CVEs"
  else
    printf '%-32s %-9s %-11s %s\n' "$mod" '-' 'UNKNOWN' 'mod_php: make sure its package matches the patched php version above'
  fi
done

echo
if [ "$checked" -eq 0 ]; then
  echo 'No PHP binary found. Pass the path to yours, for example: bash php2shell-check.sh /usr/local/bin/php'
  exit 2
fi
echo 'A patched binary only protects you after a restart: restart PHP-FPM, Apache or your containers.'
echo 'Also: CVE-2026-115560 (disclosed 23 October) is not fixed yet, even in 8.5.12. It only affects unusual'
echo 'session settings, but PHP advises session.upload_progress.enabled=0 in php.ini or the FPM pool until then.'
[ "$vulnerable" -eq 0 ]
