Files

61 lines
1.7 KiB
Bash

#!/usr/bin/env bash
# Install/update Lynis from upstream and run a full system audit (Debian-family).
set -euo pipefail
LYNIS_DIR="/opt/lynis"
LYNIS_REF="${LYNIS_REF:-master}" # pin to a release tag for reproducibility
REPORT_DIR="/var/log/lynis-reports"
KEEP_DAYS=90
if [[ $EUID -ne 0 ]]; then
echo "Run as root: sudo $0" >&2
exit 1
fi
# Dependencies
if ! command -v git >/dev/null 2>&1; then
apt-get update -qq
apt-get install -y -qq git ca-certificates
fi
# Install or update
if [[ -d "$LYNIS_DIR/.git" ]]; then
git -C "$LYNIS_DIR" fetch --tags --quiet
else
git clone --quiet https://github.com/CISOfy/lynis "$LYNIS_DIR"
fi
git -C "$LYNIS_DIR" checkout --quiet "$LYNIS_REF"
# Fast-forward only if on a branch (not a detached tag checkout)
if git -C "$LYNIS_DIR" symbolic-ref -q HEAD >/dev/null; then
git -C "$LYNIS_DIR" pull --ff-only --quiet
fi
chown -R root:root "$LYNIS_DIR"
# Run audit
mkdir -p "$REPORT_DIR"
chmod 700 "$REPORT_DIR"
STAMP="$(date +%F_%H%M%S)"
LOG="$REPORT_DIR/lynis-$STAMP.log"
DAT="$REPORT_DIR/lynis-$STAMP.dat"
# --quick skips the "press enter" pauses; --cronjob is non-interactive/no colour
if [[ -t 1 ]]; then MODE="--quick"; else MODE="--cronjob"; fi
cd "$LYNIS_DIR"
./lynis audit system "$MODE" --logfile "$LOG" --report-file "$DAT" || true
# Summary
echo
echo "=== Lynis summary ($STAMP) ==="
grep -E '^(lynis_version|hardening_index)=' "$DAT"
echo "warnings: $(grep -c '^warning\[\]=' "$DAT" || true)"
echo "suggestions: $(grep -c '^suggestion\[\]=' "$DAT" || true)"
echo
echo "--- Warnings ---"
grep '^warning\[\]=' "$DAT" | cut -d= -f2- || true
echo
echo "Full report: $DAT"
echo "Full log: $LOG"
# Prune old reports
find "$REPORT_DIR" -type f -name 'lynis-*' -mtime +"$KEEP_DAYS" -delete