#!/usr/bin/env bash
# check-claude-code-ext.sh v1.1.0 - read-only checker for the trojanised Anthropic.claude-code 2.1.312 and 2.1.313
# builds served by Cursor's VS Code extension marketplace on 29 Oct 2026 (about 09:27-20:35 GMT). macOS and Linux.
#
# STRICTLY READ-ONLY AND OFFLINE: this script only lists directories, reads files, hashes extension.js and lists
# processes. It never connects to 134.209.82.225 or anything else, sends no network traffic, never writes, moves or
# deletes a file, never removes keys, crontab entries or LaunchAgents, never runs launchctl or crontab -e, and never
# runs Cursor's remediation scripts. It never prints key material: matched lines are cut short and any run of 60 or
# more base64 characters is shown as [REDACTED]; extension code and payload scripts are reported by line number only.
#
# Versions (VENDOR IOC, Cursor security notice revised 30 Oct 2026 18:30 GMT, and Cursor incident update 13 Nov 2026):
#   2.1.313: confirmed malicious by Cursor. Never published by Anthropic on Open VSX, so any 2.1.313 folder is flagged.
#   2.1.312: confirmed malicious by Cursor for the builds its marketplace served in the window. A genuine 2.1.312 is
#            also on Open VSX (27 Oct 2026 20:26 GMT), so OUR PATTERN: 2.1.312 is flagged when extension.js carries
#            the IOCs or the payload hash; a 2.1.312 under Cursor's folders with no IOC is REVIEW (exit 4).
# Other indicators (VENDOR IOC, Cursor): extension.js containing 134.209.82.225 / svc-telemetry; extension.js SHA-256
#   ecb58cb29078d9646ad1dcdbe51526f2d9cfea06be5fb2ab183e8ecc3ab6bf15; authorized_keys line with comment svc-telemetry
#   or key AAAAC3NzaC1lZDI1NTE5AAAAIDdvylCEJ/k2TpezcTfedrkHyFSK9IZ3vmP6vhY4Ss0P; ~/.config/.smt, ~/.config/.smtk;
#   $TMPDIR/.stk_<10 hex> (macOS), /tmp/.stk_<10 hex> (Linux); cron @reboot /bin/sh "<HOME>/.config/.smt" (Linux);
#   ~/Library/LaunchAgents/com.apple.sysmond.plist; ssh -N -R '*:0:localhost:22' / autossh -M 0 to
#   tunnel@134.209.82.225; beacon http://134.209.82.225:8099/beacon.
#
# Verdicts / exit codes: CLEAN 0 | INDICATORS FOUND 1 | UNKNOWN 2 | error 3 | REVIEW 4 (2.1.312 from Cursor, no IOC)
# Precedence: INDICATORS FOUND, then REVIEW, then UNKNOWN, then CLEAN.
set -u
IP='134.209.82.225'
IPRE='134\.209\.82\.225'
KEYBLOB='AAAAC3NzaC1lZDI1NTE5AAAAIDdvylCEJ/k2TpezcTfedrkHyFSK9IZ3vmP6vhY4Ss0P'
BADVER='2.1.313'
SUSVER='2.1.312'
PAYLOAD_SHA256='ecb58cb29078d9646ad1dcdbe51526f2d9cfea06be5fb2ab183e8ecc3ab6bf15'
# Test hook only: one extra SHA-256 treated like the payload hash (the tests cannot reproduce the real file).
TEST_SHA256=${CHECK_TEST_PAYLOAD_SHA256:-}
CONTENT_RE="${IPRE}|svc-telemetry|${KEYBLOB}|:8099/beacon|/\.config/\.smtk?([^a-zA-Z]|$)|\.stk_[0-9a-f]{10}"
CURSOR_DIRS='.cursor/extensions .cursor-server/extensions'
EDITOR_DIRS="$CURSOR_DIRS .vscode/extensions .vscode-insiders/extensions .vscode-server/extensions .vscode-server-insiders/extensions .vscode-oss/extensions .windsurf/extensions .windsurf-server/extensions .vscodium/extensions .trae/extensions .kiro/extensions"

usage() {
  cat <<'U'
Usage: check-claude-code-ext.sh [--home DIR]... [--all-users] [options]
  --home DIR       user home to check (repeatable). Default: $HOME
  --all-users      check every home under /Users and /home (plus /root); run as root to read them all
                   (no user homes found -> error 3)
  --cron-dir DIR   crontab spool to read (default: /var/spool/cron/crontabs, /var/spool/cron, /usr/lib/cron/tabs)
  --tmp-dir DIR    temp dir to look for .stk_<hex> (repeatable; default: $TMPDIR and /tmp)
  --ps-file FILE   read a saved "ps -axo pid=,args=" listing instead of the live process list
  --no-crontab-l   do not fall back to "crontab -l" for the current user
  -q, --quiet      only print findings and the verdict
  -h, --help       this help
Exit: 0 CLEAN, 1 INDICATORS FOUND, 2 UNKNOWN, 3 error, 4 REVIEW. Read-only, no network.
U
}
die() { printf 'ERROR: %s\n' "$*" >&2; exit 3; }
QUIET=0
say() { [ "$QUIET" -eq 1 ] || printf '%s\n' "$*"; }
IND=0; UNK=0; REV=0; FOUND313=0; FOUND312=0
hit() { IND=$((IND + 1)); printf 'INDICATOR: %s\n' "$*"; }
unk() { UNK=$((UNK + 1)); printf 'UNKNOWN: %s\n' "$*"; }
rev() { REV=$((REV + 1)); printf 'REVIEW: %s\n' "$*"; }
# redact: never echo key material. Cuts lines and hides long base64 runs.
redact() { sed -E 's#[A-Za-z0-9+/=]{60,}#[REDACTED]#g' | cut -c1-160; }

HOMES=(); TMPS=(); CRONDIRS=(); ALL=0; PSFILE=""; CRONL=1
while [ $# -gt 0 ]; do
  case "$1" in
    --home) [ $# -ge 2 ] || die "--home needs a value"; HOMES+=("$2"); shift 2 ;;
    --all-users) ALL=1; shift ;;
    --cron-dir) [ $# -ge 2 ] || die "--cron-dir needs a value"; CRONDIRS+=("$2"); shift 2 ;;
    --tmp-dir) [ $# -ge 2 ] || die "--tmp-dir needs a value"; TMPS+=("$2"); shift 2 ;;
    --ps-file) [ $# -ge 2 ] || die "--ps-file needs a value"; PSFILE=$2; shift 2 ;;
    --no-crontab-l) CRONL=0; shift ;;
    -q|--quiet) QUIET=1; shift ;;
    -h|--help) usage; exit 0 ;;
    *) die "unknown argument: $1 (see --help)" ;;
  esac
done

if [ "$ALL" -eq 1 ]; then
  # CHECK_USERS_ROOTS overrides the parent folders of user homes (used by the tests)
  for r in ${CHECK_USERS_ROOTS:-/Users /home}; do
   for d in "$r"/*; do
    [ -d "$d" ] || continue
    case "$d" in /Users/Shared) continue ;; esac
    HOMES+=("$d")
   done
  done
  [ -n "${CHECK_USERS_ROOTS:-}" ] || { [ -d /root ] && HOMES+=(/root); }
fi
if [ ${#HOMES[@]} -eq 0 ]; then
  [ "$ALL" -eq 0 ] || die "--all-users found no user homes"
  HOMES=("${HOME:-}")
fi
for h in "${HOMES[@]}"; do
  if [ -z "$h" ] || [ ! -d "$h" ]; then die "cannot read directory: ${h:-<empty HOME>}"; fi
done
[ -z "$PSFILE" ] || [ -r "$PSFILE" ] || die "cannot read --ps-file: $PSFILE"
if [ ${#CRONDIRS[@]} -gt 0 ]; then
  for c in "${CRONDIRS[@]}"; do [ -d "$c" ] || die "cannot read directory: $c"; done
else
  CRONDIRS=(/var/spool/cron/crontabs /var/spool/cron /usr/lib/cron/tabs)
fi
if [ ${#TMPS[@]} -gt 0 ]; then
  for t in "${TMPS[@]}"; do [ -d "$t" ] || die "cannot read directory: $t"; done
else
  [ -n "${TMPDIR:-}" ] && TMPS+=("${TMPDIR%/}")
  TMPS+=(/tmp)
fi

# sha256 of a file (read-only): sha256sum on Linux, shasum on macOS. Prints nothing if neither exists.
file_sha256() {
  if command -v sha256sum >/dev/null 2>&1; then sha256sum -- "$1" 2>/dev/null | cut -d' ' -f1
  elif command -v shasum >/dev/null 2>&1; then shasum -a 256 -- "$1" 2>/dev/null | cut -d' ' -f1; fi
}

# scan_file <file> <label> [lines|nolines]: grep for content indicators; returns 0 on match.
# With "nolines" only line numbers are printed (extension code can embed key material).
scan_file() {
  local f=$1 label=$2 mode=${3:-lines}
  if [ ! -r "$f" ]; then unk "$label: $f exists but is not readable (run as that user or root)"; return 1; fi
  if grep -Eq -- "$CONTENT_RE" "$f" 2>/dev/null; then
    hit "$label: $f references the attacker IP / svc-telemetry key / payload files"
    if [ "$mode" = nolines ]; then
      printf '      matching line numbers: %s\n' "$(grep -En -- "$CONTENT_RE" "$f" 2>/dev/null | cut -d: -f1 | head -n 5 | tr '\n' ' ')"
    else
      grep -En -- "$CONTENT_RE" "$f" 2>/dev/null | head -n 3 | redact | sed 's/^/      /'
    fi
    return 0
  fi
  return 1
}

say "Claude Code extension 2.1.312 / 2.1.313 checker v1.1.0 (read-only, offline) - $(date '+%Y-%m-%d %H:%M %Z' 2>/dev/null)"
say "Homes: ${HOMES[*]}"

for H in "${HOMES[@]}"; do
  U=$(basename "$H")
  say ""
  say "== $H =="
  # 1. extensions
  n=0
  for rel in $EDITOR_DIRS; do
    ed="$H/$rel"
    [ -d "$ed" ] || continue
    if [ ! -r "$ed" ] || [ ! -x "$ed" ]; then unk "extensions dir not readable: $ed"; continue; fi
    is_cursor=0
    case " $CURSOR_DIRS " in *" $rel "*) is_cursor=1 ;; esac
    for x in "$ed"/[Aa]nthropic.claude-code-*; do
      [ -e "$x" ] || continue
      n=$((n + 1))
      name=$(basename "$x")
      ver=$(printf '%s' "$name" | sed -E 's/^[Aa]nthropic\.claude-code-([0-9]+\.[0-9]+\.[0-9]+).*/\1/')
      jshit=0; jsseen=0
      for js in "$x/extension.js" "$x/dist/extension.js"; do
        [ -f "$js" ] || continue
        jsseen=1
        if scan_file "$js" "extension code" nolines; then jshit=1; fi
        if [ -r "$js" ]; then
          jsh=$(file_sha256 "$js")
          if [ -n "$jsh" ] && { [ "$jsh" = "$PAYLOAD_SHA256" ] || { [ -n "$TEST_SHA256" ] && [ "$jsh" = "$TEST_SHA256" ]; }; }; then
            hit "extension code: $js SHA-256 matches the payload hash published by Cursor ($jsh)"; jshit=1
          fi
        fi
      done
      if [ "$ver" = "$BADVER" ]; then
        FOUND313=1
        hit "malicious version $BADVER installed (Cursor confirms it; never a genuine Anthropic release on Open VSX): $x"
      elif [ "$ver" = "$SUSVER" ] && [ "$jshit" -eq 1 ]; then
        FOUND312=1
        hit "malicious $SUSVER build installed (extension.js carries Cursor's IOCs): $x"
      elif [ "$ver" = "$SUSVER" ] && [ "$is_cursor" -eq 1 ]; then
        note=""; [ "$jsseen" -eq 1 ] || note=" (no extension.js found to scan)"
        rev "$SUSVER installed from Cursor's marketplace: $x. Cursor confirms malicious $SUSVER builds were served on 29 Oct 2026 09:27-20:35 GMT; a genuine $SUSVER also exists. No IOC found in extension.js$note. Check when it was installed and compare the VSIX SHA-256 with Cursor's list."
      elif [ "$ver" = "$SUSVER" ]; then
        say "  extension: $x (version $ver; genuine $SUSVER is on Open VSX and the VS Code Marketplace; no IOC in extension.js)"
      else
        say "  extension: $x (version $ver)"
      fi
    done
  done
  [ "$n" -gt 0 ] || say "  no anthropic.claude-code extension folders found"

  # 2. authorized_keys
  for ak in "$H/.ssh/authorized_keys" "$H/.ssh/authorized_keys2"; do
    [ -e "$ak" ] || continue
    scan_file "$ak" "authorized_keys" || say "  $ak: no svc-telemetry key"
  done
  # 3. known_hosts (plaintext entries only; hashed entries cannot be matched without ssh-keygen -F, not run)
  if [ -e "$H/.ssh/known_hosts" ]; then
    if [ ! -r "$H/.ssh/known_hosts" ]; then unk "known_hosts not readable: $H/.ssh/known_hosts"
    elif grep -Eq -- "(^|[,\[])${IPRE}([],: ]|$)" "$H/.ssh/known_hosts"; then hit "known_hosts has an entry for $IP: $H/.ssh/known_hosts"
    else say "  known_hosts: no plaintext entry for $IP (hashed entries are not checked)"; fi
  fi
  # 4. payload files (existence only; .smt and .smtk hold key material and are never read or printed)
  for p in "$H/.config/.smt" "$H/.config/.smtk" "$H/Library/LaunchAgents/com.apple.sysmond.plist"; do
    { [ -e "$p" ] || [ -L "$p" ]; } && hit "payload/persistence file present: $p"
  done
  # 5. LaunchAgents (any plist referencing the IOCs)
  if [ -d "$H/Library/LaunchAgents" ]; then
    for pl in "$H/Library/LaunchAgents"/*.plist; do
      [ -f "$pl" ] || continue
      scan_file "$pl" "LaunchAgent" || true
    done
  fi
  # 6. crontab spool for this user (Cursor: cron @reboot /bin/sh "<HOME>/.config/.smt" on Linux)
  seen=0
  for cd in "${CRONDIRS[@]}"; do
    cf="$cd/$U"
    [ -e "$cf" ] || continue
    seen=1
    scan_file "$cf" "crontab" || say "  crontab $cf: nothing suspicious"
  done
  if [ "$seen" -eq 0 ] && [ "$CRONL" -eq 1 ] && [ "$H" = "${HOME:-}" ] && command -v crontab >/dev/null 2>&1; then
    ct=$(crontab -l 2>/dev/null || true)
    if printf '%s\n' "$ct" | grep -Eq -- "$CONTENT_RE"; then hit "crontab (crontab -l for $(id -un 2>/dev/null)) references IOCs"; printf '%s\n' "$ct" | grep -E -- "$CONTENT_RE" | head -n 3 | redact | sed 's/^/      /'
    else say "  crontab -l: nothing suspicious"; fi
  fi
done

# 7. temp .stk_ files (existence only, never read)
for t in "${TMPS[@]}"; do
  [ -d "$t" ] || continue
  for s in "$t"/.stk_*; do
    [ -e "$s" ] || continue
    case "$(basename "$s")" in .stk_[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]) hit "tunnel key file present: $s" ;; esac
  done
done

# 8. processes
say ""
if [ -n "$PSFILE" ]; then PS=$(cat "$PSFILE"); src="$PSFILE"
else PS=$(ps -axo pid=,args= 2>/dev/null || ps -eo pid=,args= 2>/dev/null); src="ps"; fi
if [ -z "$PS" ] && [ -z "$PSFILE" ]; then unk "could not list processes"
else
  m=$(printf '%s\n' "$PS" | grep -E -- "(autossh|(^|[ /])ssh( |$)).*(-R|${IPRE})|${IPRE}|\.stk_[0-9a-f]{10}|/\.config/\.smtk?( |\"|$)" | grep -v -- 'check-claude-code-ext' || true)
  if [ -n "$m" ]; then hit "running ssh/autossh tunnel or payload process ($src):"; printf '%s\n' "$m" | head -n 5 | redact | sed 's/^/      /'
  else say "Processes ($src): no ssh -R / autossh / $IP processes"; fi
fi

say ""
if [ "$IND" -gt 0 ]; then
  extra=""
  [ "$FOUND313" -eq 1 ] && extra="$extra - malicious 2.1.313 present"
  [ "$FOUND312" -eq 1 ] && extra="$extra - malicious 2.1.312 build present"
  echo "VERDICT: INDICATORS FOUND ($IND)$extra"
  ec=1
elif [ "$REV" -gt 0 ]; then echo "VERDICT: REVIEW - $REV item(s) need a manual check (2.1.312 from Cursor with no IOC in extension.js)"; ec=4
elif [ "$UNK" -gt 0 ]; then echo "VERDICT: UNKNOWN - $UNK location(s) could not be read; re-run as root or as each user"; ec=2
else echo "VERDICT: CLEAN - no indicators in the locations checked"; ec=0; fi
cat <<'A'
ADVICE (OUR ADVICE): Update Anthropic.claude-code to 2.1.314 or later (Open VSX / VS Code Marketplace). If
indicators were found: isolate the host, preserve evidence, then remove with Cursor's scripts from
https://cursor.com/blog/claude-code-extension-security-notice (remediate-macos.sh; Cursor publishes no Linux script).
Uninstalling the extension is not enough. Consider a rebuild, and rotate credentials and sessions from a trusted
machine. This checker changed nothing.
A
exit "$ec"
