#!/bin/bash# Automated security hardening, kernel parameter tuning, and session protection script designed for Fedora KDE.set-euopipefail
# Ensure script is executed with superuser privilegesif[["${EUID}"-ne0]];thenecho"[ERROR] This script must be run as root (or via sudo).">&2exit1fiFEDORA_VERSION=$(./etc/os-release2>/dev/null&&echo"${VERSION_ID:-}")LOG_FILE="/var/log/fedora-kde-hardening.log"exec>>(tee-a"${LOG_FILE}")2>&1echo"================================================================="echo" Starting Fedora ${FEDORA_VERSION} KDE Hardening Script"echo" Timestamp: $(date-u'+%Y-%m-%d %H:%M:%S UTC')"echo"================================================================="# ------------------------------------------------------------------------------# 1. Kernel & Sysctl Parameter Hardening# ------------------------------------------------------------------------------echo"[1/3] Applying kernel & network stack sysctl hardening rules..."cat<<'EOF' >/etc/sysctl.d/99-security-hardening.conf# Restrict kernel pointer access in /proc (Fedora defaults to 1, 2 is stricter)kernel.kptr_restrict = 2# Disable unprivileged eBPF loadingkernel.unprivileged_bpf_disabled = 1# Disable core dumps for SUID binaries (Overrides systemd default of 2)fs.suid_dumpable = 0# Ignore ICMP broadcast requestsnet.ipv4.icmp_echo_ignore_broadcasts = 1# Ignore and do not send ICMP redirects (Mitigates routing spoofing)net.ipv4.conf.all.accept_redirects = 0net.ipv4.conf.default.accept_redirects = 0net.ipv6.conf.all.accept_redirects = 0net.ipv6.conf.default.accept_redirects = 0net.ipv4.conf.all.send_redirects = 0net.ipv4.conf.default.send_redirects = 0EOFsysctl--system>/dev/null
# ------------------------------------------------------------------------------# 2. Core Dump & System Limit Hardening# ------------------------------------------------------------------------------echo"[2/3] Disabling system core dumps..."mkdir-p/etc/systemd/coredump.conf.d
cat<<'EOF' >/etc/systemd/coredump.conf.d/disable-coredump.conf[Coredump]Storage=noneProcessSizeMax=0EOFmkdir-p/etc/security/limits.d
cat<<'EOF' >/etc/security/limits.d/99-disable-coredumps.conf* hard core 0* soft core 0EOFsystemctldaemon-reload
# ------------------------------------------------------------------------------# 3. KDE Plasma Security Environment Defaults (KDE Connect Safe)# ------------------------------------------------------------------------------echo"[3/3] Setting secure system-wide KDE Plasma defaults..."KDE_GLOBAL_CONFIG_DIR="/etc/xdg"# Force screen lock activation on system sleep/suspendmkdir-p"${KDE_GLOBAL_CONFIG_DIR}"cat<<'EOF' >"${KDE_GLOBAL_CONFIG_DIR}/kscreenlockerrc"[Daemon]Autolock=trueTimeout=10LockOnResume=trueEOF# Disable automatic mounting of removable storage (keys from KDE's solid-device-automounter).# KDE Connect's SFTP mounts are not handled by this module, so they keep working.cat<<'EOF' >"${KDE_GLOBAL_CONFIG_DIR}/kded_device_automounterrc"[General]AutomountEnabled=falseAutomountOnLogin=falseAutomountOnPlugin=falseAutomountUnknownDevices=falseEOFecho"================================================================="echo" Hardening Complete! Log saved to ${LOG_FILE}"echo" A system reboot is recommended to ensure all sysctl settings take full effect."echo"================================================================="