Chuyển tới nội dung chính

Hướng Dẫn Hardening Linux Server - Bảo Mật Toàn Diện

Giới Thiệu

Server hardening là quá trình tăng cường bảo mật hệ thống bằng cách giảm thiểu attack surface, vá các lỗ hổng và triển khai các biện pháp bảo vệ nhiều lớp. Bài viết này sẽ hướng dẫn bạn hardening một Linux server theo chuẩn CIS (Center for Internet Security) Benchmark.

Tầm quan trọng:

  • 🔒 Ngăn chặn 80% các cuộc tấn công phổ biến
  • 🛡️ Bảo vệ dữ liệu nhạy cảm
  • ⚖️ Đáp ứng compliance requirements (PCI-DSS, HIPAA, SOC2)
  • 📊 Giảm risk và incident response time

Thời gian thực hiện: 2-3 giờ
Độ khó: Nâng cao
Áp dụng cho: Ubuntu 22.04, Debian 11, CentOS/RHEL 8+

Threat Model và Attack Vectors

Các Mối Đe Dọa Phổ Biến

┌─────────────────────────────────────────┐
│ Attack Surface │
├─────────────────────────────────────────┤
│ 1. Brute Force SSH │
│ 2. Privilege Escalation │
│ 3. Malware/Rootkits │
│ 4. DDoS Attacks │
│ 5. Zero-day Exploits │
│ 6. Social Engineering │
│ 7. Insider Threats │
└─────────────────────────────────────────┘

![Sơ đồ Attack Vectors - Đặt ảnh tại /static/img/security/attack-vectors.png]

Phần 1: SSH Hardening

SSH là vector tấn công phổ biến nhất. Hardening SSH là bước đầu tiên và quan trọng nhất.

Bước 1: Tạo SSH Key Pair Mạnh

# Tạo ED25519 key (khuyến nghị, mạnh hơn RSA)
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519

# Hoặc RSA 4096 bit
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa

# Set quyền đúng
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

Copy public key lên server:

# Method 1: ssh-copy-id
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server-ip

# Method 2: Manual
cat ~/.ssh/id_ed25519.pub | ssh user@server-ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

# Set permissions trên server
ssh user@server-ip "chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"

Bước 2: Cấu Hình SSH Daemon

# Backup file config gốc
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup

# Edit config
sudo nano /etc/ssh/sshd_config

Cấu hình hardened SSH:

# Basic settings
Port 2222 # Đổi port (không phải 22)
Protocol 2 # Chỉ dùng SSH protocol 2
PermitRootLogin no # Tắt root login
MaxAuthTries 3 # Giới hạn số lần thử
MaxSessions 2 # Giới hạn số session

# Authentication
PubkeyAuthentication yes # Bật SSH key
PasswordAuthentication no # TẮT password login
PermitEmptyPasswords no # Không cho phép password rỗng
ChallengeResponseAuthentication no # Tắt challenge-response

# Restrict users
AllowUsers deployuser admin # Chỉ cho phép specific users
# AllowGroups ssh-users # Hoặc cho phép theo group

# Security options
X11Forwarding no # Tắt X11
PermitUserEnvironment no # Không cho set environment
AllowAgentForwarding no # Tắt agent forwarding
AllowTcpForwarding no # Tắt TCP forwarding
PermitTunnel no # Tắt tunnel

# Timing settings
LoginGraceTime 30 # Timeout cho login
ClientAliveInterval 300 # Ping client mỗi 5 phút
ClientAliveCountMax 2 # Disconnect sau 2 lần không response

# Crypto settings (chỉ dùng thuật toán mạnh)
KexAlgorithms curve25519-sha256,[email protected],diffie-hellman-group-exchange-sha256
Ciphers [email protected],[email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr
MACs [email protected],[email protected],hmac-sha2-512,hmac-sha2-256

# Logging
SyslogFacility AUTH
LogLevel VERBOSE # Log chi tiết để audit

# Banner
Banner /etc/ssh/banner # Hiển thị warning banner

Tạo warning banner:

sudo nano /etc/ssh/banner
***************************************************************************
AUTHORIZED ACCESS ONLY
***************************************************************************
Unauthorized access to this system is forbidden and will be prosecuted by law.
By accessing this system, you agree that your actions may be monitored.
****************************************************************

**Xem thêm:**
- [Khắc phục lỗi cài đặt cPanel/WHM thường gặp](/control-panel/cpanel/loi-thuong-gap-khi-cai-cpanel-va-cach-khac-phuc)
- [Cài Đặt aaPanel trên AlmaLinux 8](/control-panel/aapanel/aapanel-cai-dat-aapanel-tren-almalinux-8)
- [Thiết Lập Addon Domain Trong cPanel](/control-panel/cpanel/huong-dan-addon-domain-trong-cpanel)
- [Lỗ Hổng Bảo Mật Phổ Biến Trên Server và Cách Khắc Phục](/security/cac-lo-hong-bao-mat-pho-bien-tren-server)
- [Khôi Phục Dữ Liệu cPanel Hiệu Quả](/control-panel/cpanel/restore-du-lieu-cpanel-dung-cach)