#!/bin/sh # PocketClaw Watch agent, version 1.0.0 # Source: https://pocketclaw.dev/watch/pocketclaw-watch.sh (docs: https://pocketclaw.dev/watch) # # What it does, every 6 hours, as the user that installed it (no root): # - reads the version of the agents it finds: openclaw, hermes, zeroclaw, # nanoclaw, ironclaw, nanobot, and ollama next to them # - lists the TCP ports that listen, with the address they are bound to # (ss, or lsof on macOS, or netstat) # - lists running containers with their published ports (docker ps) # - notes whether Caddy, nginx, Traefik, HAProxy, Tailscale, cloudflared # or ngrok run # - runs `openclaw security audit --json` when OpenClaw is installed and # keeps only the check id and severity of each finding # - sends that text, after a local filter that masks anything looking like # a key or a token, to https://pocketclaw.dev/api/watch/report # # What it never reads or sends: configuration files, environment variables, # keys, tokens, passwords, chat logs, prompts, file contents, the hostname. # It changes nothing on the machine besides its own files. # # Usage: # sh pocketclaw-watch.sh --dry-run print exactly what would be sent, send nothing # sh pocketclaw-watch.sh run collect and send (what the scheduled task runs) # sh pocketclaw-watch.sh uninstall remove the scheduled task, the token and this script set -u LC_ALL=C export LC_ALL WATCH_VERSION=1.0.0 ENDPOINT="${POCKETCLAW_WATCH_URL:-https://pocketclaw.dev/api/watch/report}" DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/pocketclaw-watch" CONF_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/pocketclaw-watch" UNIT_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user" have() { command -v "$1" >/dev/null 2>&1; } # Same filter as the security check collector; the server filters again # and drops any line where it still finds a secret. mask() { sed -E \ -e 's/-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----.*/[REDACTED]/' \ -e 's/sk-[A-Za-z0-9_-]{16,}/[REDACTED]/g' \ -e 's/(sk|rk|pk)_(live|test)_[A-Za-z0-9]{10,}/[REDACTED]/g' \ -e 's/(ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{20,}/[REDACTED]/g' \ -e 's/github_pat_[A-Za-z0-9_]{20,}/[REDACTED]/g' \ -e 's/xox[abprs]-[A-Za-z0-9-]{10,}/[REDACTED]/g' \ -e 's/(AKIA|ASIA)[0-9A-Z]{16}/[REDACTED]/g' \ -e 's/AIza[0-9A-Za-z_-]{30,}/[REDACTED]/g' \ -e 's/(hf|gsk|npm)_[A-Za-z0-9]{20,}/[REDACTED]/g' \ -e 's/tskey-[A-Za-z0-9-]{10,}/[REDACTED]/g' \ -e 's/pcw_[A-Za-z0-9_-]{20,}/[REDACTED]/g' \ -e 's/eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/[REDACTED]/g' \ -e 's/[0-9]{8,10}:[A-Za-z0-9_-]{35}/[REDACTED]/g' \ -e 's#(://[^ :/@]+:)[^ @/]+@#\1[REDACTED]@#g' \ -e 's/(Bearer|Basic|Token) [A-Za-z0-9._~+\/=-]{12,}/\1 [REDACTED]/g' \ -e 's/[A-Za-z0-9+\/_-]{40,}/[REDACTED]/g' } # The name of a listening process. Linux cuts the name ss shows (comm) at 15 # characters ("box-websockify." for box-websockify.py). When this user can # read the process, take the full name from the executable or from the first # two words of its command line, and only when it starts with that short # name: no argument beyond a program or script name is ever kept. Otherwise # the short name, without a dangling dot. proc_name() { short=${1%.} pid=$2 if [ -n "$short" ] && [ -n "$pid" ]; then exe=$(readlink "/proc/$pid/exe" 2>/dev/null) a0="" a1="" if [ -r "/proc/$pid/cmdline" ]; then a0=$(tr '\000' '\n' < "/proc/$pid/cmdline" 2>/dev/null | sed -n 1p) a1=$(tr '\000' '\n' < "/proc/$pid/cmdline" 2>/dev/null | sed -n 2p) fi for cand in "${exe##*/}" "${a0##*/}" "${a1##*/}"; do case "$cand" in "$short"*) if [ "${#cand}" -le 60 ]; then printf '%s\n' "$cand" | tr -cd 'A-Za-z0-9._:/+-' return fi ;; esac done fi printf '%s\n' "$short" | tr -cd 'A-Za-z0-9._:/+-' } # First release number printed by " --version", or "unknown". version_of() { v=$("$1" --version 2>/dev/null | grep -Eo '[0-9]+(\.[0-9]+){1,3}([-+.]?[A-Za-z0-9]+)?' | head -n 1) echo "${v:-unknown}" } collect() { echo "# pocketclaw-watch 1" echo "collector: $WATCH_VERSION" echo "date: $(date -u +%Y-%m-%dT%H:%M:%SZ)" echo "os: $(uname -sr 2>/dev/null | tr -cd 'A-Za-z0-9 ._()+/-')" echo "arch: $(uname -m 2>/dev/null | tr -cd 'A-Za-z0-9_-')" for pair in openclaw:openclaw hermes:hermes-agent zeroclaw:zeroclaw nanoclaw:nanoclaw ironclaw:ironclaw nanobot:nanobot ollama:ollama; do bin=${pair%%:*} slug=${pair#*:} if have "$bin"; then echo "agent: $slug $(version_of "$bin")"; fi done # Listening TCP sockets: address:port and the process name when visible. # lsof only on macOS: busybox ships an lsof that knows none of these flags. if have ss; then ss -ltnpH 2>/dev/null | awk '{ u=$6; n=u; sub(/^users:\(\("/, "", n); sub(/".*/, "", n); gsub(/[^A-Za-z0-9._:\/+-]/, "", n); pid=""; if (match(u, /pid=[0-9]+/)) pid=substr(u, RSTART+4, RLENGTH-4); print $4 " " (n == "" ? "-" : n) " " (pid == "" ? "-" : pid) }' | while read -r addr name pid; do [ "$name" = "-" ] && name="" [ "$pid" = "-" ] && pid="" echo "listen: $addr $(proc_name "$name" "$pid")" done elif [ "$(uname -s 2>/dev/null)" = Darwin ] && have lsof; then lsof +c 0 -nP -iTCP -sTCP:LISTEN 2>/dev/null | awk 'NR>1 { p=$1; gsub(/[^A-Za-z0-9._:\/+-]/, "", p); sub(/\.$/, "", p); print "listen: " $9 " " p }' elif have netstat; then netstat -ltn 2>/dev/null | awk '/LISTEN/ { print "listen: " $4 }' fi | grep -E '^listen: [^ ]*:[0-9]+( [^ ]*)?$' | sort -u | head -n 300 # Containers: image (without digest) and published ports. if have docker; then docker ps --format '{{.Image}} | {{.Ports}}' 2>/dev/null | sed -e 's/@sha256:[0-9a-f]*//' -e 's/^/docker: /' | head -n 120 fi for p in caddy nginx traefik haproxy; do if pgrep -x "$p" >/dev/null 2>&1; then echo "front: $p"; fi done if pgrep -x tailscaled >/dev/null 2>&1; then echo "front: tailscale"; fi for p in cloudflared ngrok; do if pgrep -x "$p" >/dev/null 2>&1; then echo "front: $p"; fi done # OpenClaw's own check: only the check id and severity of each finding. if have openclaw; then if have node; then run="" if have timeout; then run="timeout 120"; fi out=$($run openclaw security audit --json 2>/dev/null | node -e ' let s = ""; process.stdin.on("data", (d) => (s += d)).on("end", () => { try { const j = JSON.parse(s); const r = j && j.report ? j.report : j; const list = Array.isArray(r && r.findings) ? r.findings : []; console.log("audit: ok"); for (const f of list.slice(0, 200)) { let sev = String((f && f.severity) || "").toLowerCase(); if (sev === "warning") sev = "warn"; const id = String((f && f.checkId) || "").replace(/[^A-Za-z0-9._:-]/g, "").slice(0, 120); if (id && ["critical", "warn", "info"].includes(sev)) console.log("finding: " + sev + " " + id); } } catch (e) { console.log("audit: error"); } });' 2>/dev/null) if [ -n "$out" ]; then echo "$out"; else echo "audit: error"; fi else echo "audit: unavailable" fi fi } send() { token_file="$CONF_DIR/token" if [ ! -r "$token_file" ]; then echo "pocketclaw-watch: no token in $token_file. Install with the command on your dashboard." >&2 exit 1 fi if ! have curl; then echo "pocketclaw-watch: curl is required to send the report." >&2 exit 1 fi old_umask=$(umask) umask 077 body=$(mktemp) hdr=$(mktemp) resp=$(mktemp) umask "$old_umask" trap 'rm -f "$body" "$hdr" "$resp"' EXIT INT TERM collect | mask > "$body" # The token goes in a header file, not on the command line (other users see command lines). printf 'Authorization: Bearer %s\n' "$(cat "$token_file")" > "$hdr" code=$(curl -sS -m 30 -o "$resp" -w '%{http_code}' -X POST -H @"$hdr" -H 'Content-Type: text/plain' --data-binary @"$body" "$ENDPOINT" 2>/dev/null) if [ "$code" = "200" ]; then echo "pocketclaw-watch: report sent." else echo "pocketclaw-watch: the server answered ${code:-nothing}: $(head -c 300 "$resp" 2>/dev/null)" >&2 exit 1 fi } uninstall() { if have crontab; then current=$(crontab -l 2>/dev/null) if printf '%s\n' "$current" | grep -q 'pocketclaw-watch'; then printf '%s\n' "$current" | grep -v 'pocketclaw-watch' | crontab - echo "Removed the cron entry." fi fi if [ -f "$UNIT_DIR/pocketclaw-watch.timer" ]; then systemctl --user disable --now pocketclaw-watch.timer >/dev/null 2>&1 rm -f "$UNIT_DIR/pocketclaw-watch.timer" "$UNIT_DIR/pocketclaw-watch.service" systemctl --user daemon-reload >/dev/null 2>&1 echo "Removed the systemd user timer." fi rm -rf "$CONF_DIR" "$DATA_DIR" echo "Removed the token and the agent. Delete the machine on https://pocketclaw.dev/dashboard/watch to drop its history." } case "${1:-}" in --dry-run | dry-run) collect | mask echo "# dry run: nothing was sent" >&2 ;; run) send ;; uninstall | --uninstall) uninstall ;; *) echo "usage: sh pocketclaw-watch.sh --dry-run | run | uninstall" >&2 exit 2 ;; esac