Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
143 lines
5.3 KiB
Bash
143 lines
5.3 KiB
Bash
#!/usr/bin/env bash
|
|
# BUG-2416 (#360): read-only security audit of the production host.
|
|
#
|
|
# sudo bash /opt/jyotisha-production/deploy/monitor/host-audit.sh
|
|
#
|
|
# It only reads and prints. It never installs, stops, deletes or edits anything,
|
|
# and it never prints environment variables, env files, keys or database URLs.
|
|
# Output is for the owner/coordinator to read; act on it separately.
|
|
set -uo pipefail
|
|
set +x
|
|
umask 077
|
|
export LC_ALL=C
|
|
|
|
section() {
|
|
printf '\n==== %s ====\n' "$1"
|
|
}
|
|
|
|
have() {
|
|
command -v "$1" >/dev/null 2>&1
|
|
}
|
|
|
|
# Command lines can carry tokens (e.g. --password=…). Show at most 160 characters
|
|
# and blank anything that looks like a key/value secret.
|
|
mask_args() {
|
|
sed -E 's/((pass(word)?|token|secret|key|apikey|api_key|authorization)[=: ]+)[^ ]+/\1***/Ig' | cut -c1-160
|
|
}
|
|
|
|
section "host"
|
|
uname -srm 2>/dev/null || true
|
|
uptime 2>/dev/null || true
|
|
|
|
section "unattended-upgrades"
|
|
if have systemctl; then
|
|
systemctl is-enabled unattended-upgrades 2>/dev/null || echo "unattended-upgrades: not installed or not enabled"
|
|
systemctl is-active unattended-upgrades 2>/dev/null || true
|
|
fi
|
|
if [ -r /etc/apt/apt.conf.d/20auto-upgrades ]; then
|
|
grep -E '^APT::Periodic' /etc/apt/apt.conf.d/20auto-upgrades || true
|
|
else
|
|
echo "/etc/apt/apt.conf.d/20auto-upgrades: missing"
|
|
fi
|
|
if [ -r /var/log/unattended-upgrades/unattended-upgrades.log ]; then
|
|
echo "last runs:"
|
|
grep -E 'Starting unattended upgrades|All upgrades installed|No packages found' /var/log/unattended-upgrades/unattended-upgrades.log | tail -5 || true
|
|
fi
|
|
if have apt; then
|
|
echo "pending security updates (count):"
|
|
apt list --upgradable 2>/dev/null | grep -c -- '-security' || true
|
|
fi
|
|
|
|
section "fail2ban"
|
|
if have fail2ban-client; then
|
|
fail2ban-client status 2>/dev/null || echo "fail2ban installed but not answering"
|
|
fail2ban-client status sshd 2>/dev/null | grep -E 'Currently|Total' || true
|
|
else
|
|
echo "fail2ban: not installed"
|
|
fi
|
|
|
|
section "sshd settings (effective)"
|
|
if have sshd; then
|
|
sshd -T 2>/dev/null | grep -Ei '^(port|permitrootlogin|passwordauthentication|pubkeyauthentication|kbdinteractiveauthentication|maxauthtries|allowusers) ' || true
|
|
else
|
|
grep -Ei '^\s*(Port|PermitRootLogin|PasswordAuthentication|PubkeyAuthentication)\b' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*.conf 2>/dev/null || true
|
|
fi
|
|
echo "authorized_keys entries (count, comment only):"
|
|
for file in /root/.ssh/authorized_keys /home/*/.ssh/authorized_keys; do
|
|
[ -r "$file" ] || continue
|
|
printf '%s: %s keys;' "$file" "$(grep -cvE '^\s*(#|$)' "$file")"
|
|
awk '!/^[[:space:]]*(#|$)/ {print " " $NF}' "$file" | tr '\n' ' '
|
|
printf '\n'
|
|
done
|
|
|
|
section "top CPU processes"
|
|
ps -eo pid,user,pcpu,pmem,etime,comm,args --sort=-pcpu 2>/dev/null | head -15 | mask_args
|
|
|
|
section "processes running from temp or hidden paths"
|
|
ps -eo pid,user,args 2>/dev/null | grep -E '(/tmp/|/var/tmp/|/dev/shm/|/\.[^/ ]+/)' | grep -v grep | mask_args || true
|
|
|
|
section "crontabs"
|
|
for file in /etc/crontab /etc/cron.d/*; do
|
|
[ -r "$file" ] || continue
|
|
echo "-- $file"
|
|
grep -vE '^\s*(#|$)' "$file" | cut -c1-200 || true
|
|
done
|
|
for dir in /etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly; do
|
|
[ -d "$dir" ] && printf '%s: %s\n' "$dir" "$(ls -1 "$dir" 2>/dev/null | tr '\n' ' ')"
|
|
done
|
|
if [ -d /var/spool/cron/crontabs ]; then
|
|
for file in /var/spool/cron/crontabs/*; do
|
|
[ -r "$file" ] || continue
|
|
echo "-- user crontab $(basename "$file")"
|
|
grep -vE '^\s*(#|$)' "$file" | cut -c1-200 || true
|
|
done
|
|
fi
|
|
|
|
section "enabled systemd units not shipped by a package"
|
|
if have systemctl; then
|
|
systemctl list-unit-files --state=enabled --no-legend 2>/dev/null | awk '{print $1}' | while read -r unit; do
|
|
path="$(systemctl show -p FragmentPath --value "$unit" 2>/dev/null)"
|
|
[ -n "$path" ] || continue
|
|
if have dpkg && dpkg -S "$path" >/dev/null 2>&1; then
|
|
continue
|
|
fi
|
|
printf '%s %s\n' "$unit" "$path"
|
|
done
|
|
echo "timers:"
|
|
systemctl list-timers --all --no-legend 2>/dev/null | awk '{print $(NF-1), $NF}' | head -20
|
|
fi
|
|
|
|
section "executable files in /tmp /var/tmp /dev/shm"
|
|
for dir in /tmp /var/tmp /dev/shm; do
|
|
[ -d "$dir" ] || continue
|
|
find "$dir" -xdev -type f -perm /111 -printf '%TY-%Tm-%Td %u %s %p\n' 2>/dev/null | head -30
|
|
done
|
|
|
|
section "established outbound connections (not our inbound 22/80/443)"
|
|
if have ss; then
|
|
ss -Htnp state established 2>/dev/null | awk '{
|
|
split($3, l, ":"); lp = l[length(l)];
|
|
if (lp == "22" || lp == "80" || lp == "443" || lp == "3000" || lp == "5200" || lp == "5432") next;
|
|
print $3, "->", $4, $5
|
|
}' | head -40
|
|
fi
|
|
echo "listening ports:"
|
|
ss -Htlnp 2>/dev/null | awk '{print $4, $6}' | head -30 || true
|
|
|
|
section "docker"
|
|
if have docker; then
|
|
docker ps --format '{{.Names}} {{.Image}} {{.Status}}' 2>/dev/null || true
|
|
echo "images:"
|
|
docker images --format '{{.Repository}}:{{.Tag}} {{.ID}} {{.CreatedSince}}' 2>/dev/null | head -30 || true
|
|
echo "containers not from this project:"
|
|
docker ps -a --format '{{.Names}} {{.Label "com.docker.compose.project"}}' 2>/dev/null | awk '$2 != "jyotisha-production" {print $1}' || true
|
|
fi
|
|
|
|
section "recent logins"
|
|
last -n 15 -a 2>/dev/null || true
|
|
echo "failed ssh logins (last 24h, count):"
|
|
journalctl -u ssh -u sshd --since "24 hours ago" 2>/dev/null | grep -c 'Failed password\|Invalid user' || true
|
|
|
|
section "done"
|
|
echo "read-only audit finished; nothing was changed"
|