harden.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env bash
  2. #
  3. # harden.sh — baseline OS hardening for AWS Marketplace AMI scanner compliance.
  4. #
  5. # Focus: the controls the scanner actually checks — key-only SSH, no root
  6. # password login, and no default OS account passwords. A restrictive host
  7. # firewall is intentionally NOT enforced by default because 3x-ui opens Xray
  8. # inbound ports on admin-chosen ports at runtime (see README for the rationale
  9. # and how to add ufw rules if you want them).
  10. set -euo pipefail
  11. export DEBIAN_FRONTEND=noninteractive
  12. echo "[harden] applying SSH hardening..."
  13. install -d -m 755 /etc/ssh/sshd_config.d
  14. cat > /etc/ssh/sshd_config.d/99-3xui-hardening.conf << 'EOF'
  15. # 3x-ui golden image hardening (AWS Marketplace scanner compliance)
  16. PasswordAuthentication no
  17. PermitRootLogin prohibit-password
  18. KbdInteractiveAuthentication no
  19. ChallengeResponseAuthentication no
  20. EOF
  21. chmod 644 /etc/ssh/sshd_config.d/99-3xui-hardening.conf
  22. echo "[harden] locking passwords on default OS accounts..."
  23. # No account may ship with a usable password. Keys are provisioned per-instance
  24. # by the cloud platform (EC2 metadata / cloud-init) on first boot.
  25. # passwd -l locks the PASSWORD only; key-based login keeps working.
  26. for u in root ubuntu admin; do
  27. if id "$u" > /dev/null 2>&1; then
  28. passwd -l "$u" > /dev/null 2>&1 || true
  29. fi
  30. done
  31. echo "[harden] enabling automatic security updates..."
  32. apt-get update
  33. apt-get install -y --no-install-recommends unattended-upgrades
  34. systemctl enable unattended-upgrades > /dev/null 2>&1 || true
  35. echo "[harden] done."