Lab 20: Capstone — Hardened Container-Ready Server
Step 1: Kernel Hardening — sysctl Security Parameters
apt-get update -qq && apt-get install -y -qq procps iproute2 auditd apparmor apparmor-utils libpam-apparmor
echo '=== Current security-relevant sysctl values ==='
sysctl kernel.randomize_va_space
sysctl net.ipv4.tcp_syncookies
sysctl net.ipv4.conf.all.rp_filterkernel.randomize_va_space = 2
net.ipv4.tcp_syncookies = 1
net.ipv4.conf.all.rp_filter = 0# Apply comprehensive kernel hardening
cat > /etc/sysctl.d/99-hardening.conf << 'EOF'
# === KERNEL HARDENING ===
# Address space layout randomization (2=full randomization)
kernel.randomize_va_space = 2
# Prevent core dumps from SUID programs
fs.suid_dumpable = 0
# Restrict /proc/PID access to process owner
kernel.yama.ptrace_scope = 1
# Restrict kernel pointer exposure in /proc/kallsyms
kernel.kptr_restrict = 2
# Restrict kernel log access to root
kernel.dmesg_restrict = 1
# Disable magic SysRq key (useful in VMs, disable in prod)
kernel.sysrq = 0
# Restrict unprivileged user namespaces (set to 1 in prod if Docker not needed)
# kernel.unprivileged_userns_clone = 0 # Debian-specific
# === NETWORK HARDENING ===
# Enable TCP SYN cookies (SYN flood protection)
net.ipv4.tcp_syncookies = 1
# Enable reverse path filtering (prevent IP spoofing)
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# Disable IP forwarding (enable only if this is a router)
net.ipv4.ip_forward = 0
net.ipv6.conf.all.forwarding = 0
# Disable ICMP redirects (prevent routing manipulation)
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
# Ignore ICMP ping broadcasts
net.ipv4.icmp_echo_ignore_broadcasts = 1
# Disable source routing
net.ipv4.conf.all.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0
# Log suspicious packets (martians)
net.ipv4.conf.all.log_martians = 1
# Protect against time-wait assassination
net.ipv4.tcp_rfc1337 = 1
# === MEMORY PROTECTION ===
# Minimum address for mmap (prevent NULL pointer dereference exploits)
vm.mmap_min_addr = 65536
EOF
# Apply (some may fail in container — that's expected)
sysctl -p /etc/sysctl.d/99-hardening.conf 2>/dev/null | head -20
echo ''
echo '=== Verify key settings applied ==='
sysctl kernel.randomize_va_space
sysctl net.ipv4.tcp_syncookies
sysctl net.ipv4.conf.all.rp_filter
sysctl net.ipv4.conf.all.accept_redirectsStep 2: Audit Rules — File Integrity Monitoring
Step 3: AppArmor — Mandatory Access Control Profile
Step 4: Resource Limits via cgroups
Step 5: Namespace Isolation Demo
Step 6: LUKS Encryption Concepts and Workflow
Step 7: Hardened systemd Service Unit
Step 8: Final Security Audit Script
Summary
Step
Topic
Key Commands / Files
Last updated
