وقتی یه سرور جدید راه میندازید، هکرها ظرف چند ساعت اسکنش میکنن. Server Hardening یا امن‌سازی، فرآیندیه که سرور رو در برابر حملات مقاوم میکنه. تو این مقاله چک‌لیست حرفه‌ای رو یاد میگیرید.

مرحله ۱: کاربر Root رو محدود کنید

Direct root login رو غیرفعال کنید:
/etc/ssh/sshd_config:
PermitRootLogin no

مرحله ۲: کاربر Sudo بسازید

adduser admin
usermod -aG sudo admin

حالا با admin وصل شید و برای root از sudo استفاده کنید.

مرحله ۳: SSH Key Authentication

ssh-keygen -t ed25519
ssh-copy-id admin@server

بعد Password Authentication رو غیرفعال کنید:
PasswordAuthentication no

مرحله ۴: تغییر پورت SSH

پورت 22 اسکن زیاد میشه. عوض کنید:
Port 2222

یا هر پورت دیگه. حتماً تو Firewall باز کنید.

مرحله ۵: Firewall

UFW (Ubuntu)

ufw default deny incoming
ufw default allow outgoing
ufw allow 2222
ufw allow 80
ufw allow 443
ufw enable

firewalld (CentOS)

firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --reload

مرحله ۶: Fail2ban

تلاش‌های ناموفق ورود رو مسدود کنید:
apt install fail2ban
systemctl enable fail2ban

مرحله ۷: Update منظم

apt update && apt upgrade -y
یا برای CentOS: dnf update -y

Automatic Updates:
apt install unattended-upgrades

مرحله ۸: Disable سرویس‌های غیرضروری

systemctl list-unit-files --state=enabled
سرویس‌های غیرضروری رو غیرفعال کنید:
systemctl disable service_name

مرحله ۹: SELinux یا AppArmor

SELinux (CentOS): setenforce 1
AppArmor (Ubuntu): سیستم پیش‌فرض

مرحله ۱۰: Kernel Hardening

/etc/sysctl.conf:
net.ipv4.tcp_syncookies = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.icmp_echo_ignore_all = 1

مرحله ۱۱: File Permissions

chmod 700 /root
chmod 600 /etc/ssh/sshd_config
chmod 640 /var/log/*

مرحله ۱۲: Audit Logging

apt install auditd
systemctl enable auditd

مرحله ۱۳: Log Monitoring

logwatch یا OSSEC برای پایش لاگ‌ها.

مرحله ۱۴: 2FA

Google Authenticator برای SSH:
apt install libpam-google-authenticator

مرحله ۱۵: Rootkit Detection

chkrootkit یا rkhunter نصب کنید.
apt install rkhunter chkrootkit

مرحله ۱۶: Antivirus

ClamAV برای Linux:
apt install clamav clamav-daemon

مرحله ۱۷: Time Sync

ntp یا chrony برای همگام‌سازی زمان.
apt install chrony

مرحله ۱۸: Backup

Regular backup به Off-site.

مرحله ۱۹: Monitoring

Netdata، Prometheus یا Zabbix.

مرحله ۲۰: Intrusion Detection

OSSEC یا Wazuh برای IDS/IPS.

Docker خاص

اگه Docker استفاده میکنید:
- User namespace remapping
- Read-only containers
- Resource limits

Web Server

Apache

ServerTokens Prod
ServerSignature Off
TraceEnable Off

Nginx

server_tokens off;
add_header X-Frame-Options SAMEORIGIN;

Database

MySQL:
- bind-address = 127.0.0.1
- Strong password
- Remove test DB

Application Layer

- Input validation
- CSRF tokens
- HTTPS اجباری

Physical Security

اگه سرور اختصاصی دارید، دیتاسنتر امن انتخاب کنید.

Compliance

PCI DSS، HIPAA، SOC 2 اگه مربوطه.

Regular Audit

ماهانه:
- ssh -Q cipher (کدام cipher استفاده میشه)
- last -a (آخرین ورودها)
- who -a (کاربران فعال)
- netstat -tulpn (پورت‌های باز)

ابزارهای Audit

Lynis: آنالیز امنیتی خودکار
OpenSCAP: بررسی سیاست
Tiger: بررسی امنیت قدیمی ولی هنوز مفید

سباهاست پیشنهاد

سرورهای سباهاست با تنظیمات امنیتی پایه ارائه میشن. تیم فنی در Hardening کمک میکنه.

جمع‌بندی

Server Hardening یه فرآیند مداومه، نه یه بار. با چک‌لیست بالا و پایش دائم، سرور امن نگه دارید.