SMTP 服务器搭建完全指南 / 第 12 章:安全加固
第 12 章:安全加固
安全不是一次性配置,而是持续的过程——从系统层到应用层的全面防护。
12.1 安全加固概览
12.1.1 邮件服务器威胁矩阵
| 威胁类型 | 描述 | 影响 |
|---|---|---|
| 暴力破解 | 猜测用户密码 | 账户被盗 |
| 开放中继 | 利用服务器发送垃圾邮件 | IP 被列入黑名单 |
| 拒绝服务 | 大量连接耗尽资源 | 服务不可用 |
| 邮件欺骗 | 伪造发件人地址 | 钓鱼攻击 |
| 中间人攻击 | 窃听邮件内容 | 信息泄露 |
| 恶意软件 | 附件携带病毒 | 系统感染 |
12.1.2 安全加固层次
┌─────────────────────────────────────┐
│ 第 1 层:网络层安全 │
│ • 防火墙规则 │
│ • 入侵检测 │
│ • DDoS 防护 │
├─────────────────────────────────────┤
│ 第 2 层:系统层安全 │
│ • 系统更新 │
│ • 最小权限原则 │
│ • 安全审计 │
├─────────────────────────────────────┤
│ 第 3 层:应用层安全 │
│ • 速率限制 │
│ • 认证强化 │
│ • 输入验证 │
├─────────────────────────────────────┤
│ 第 4 层:邮件层安全 │
│ • SPF/DKIM/DMARC │
│ • 内容过滤 │
│ • 加密传输 │
└─────────────────────────────────────┘
12.2 系统层安全
12.2.1 系统更新
# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y
# 设置自动安全更新
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
# 配置自动更新
# /etc/apt/apt.conf.d/50unattended-upgrades
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}-security";
};
Unattended-Upgrade::AutoFixInterruptedDpkg "true";
Unattended-Upgrade::Remove-Unused-Dependencies "true";
12.2.2 最小权限配置
# Postfix 以 postfix 用户运行
# 检查运行用户
ps aux | grep postfix
# 文件权限检查
sudo postfix check
# 确保敏感文件权限正确
sudo chmod 600 /etc/postfix/sasl_passwd
sudo chmod 600 /etc/dovecot/users
sudo chmod 600 /etc/opendkim/keys/*/mail.private
12.2.3 禁用不必要的服务
# 列出运行的服务
systemctl list-units --type=service --state=running
# 禁用不必要的服务
sudo systemctl disable --now telnet.socket
sudo systemctl disable --now ftp.service
12.3 防火墙配置
12.3.1 UFW 配置(Ubuntu)
# 安装 UFW
sudo apt install -y ufw
# 默认策略
sudo ufw default deny incoming
sudo ufw default allow outgoing
# 允许 SSH(重要!)
sudo ufw allow 22/tcp
# 允许 SMTP
sudo ufw allow 25/tcp comment 'SMTP'
sudo ufw allow 587/tcp comment 'SMTP Submission'
sudo ufw allow 465/tcp comment 'SMTPS'
# 允许 IMAP/POP3
sudo ufw allow 993/tcp comment 'IMAPS'
sudo ufw allow 995/tcp comment 'POP3S'
# 允许 HTTP/HTTPS(Webmail)
sudo ufw allow 80/tcp comment 'HTTP'
sudo ufw allow 443/tcp comment 'HTTPS'
# 允许 DKIM
sudo ufw allow 8891/tcp comment 'OpenDKIM'
# 启用防火墙
sudo ufw enable
# 查看状态
sudo ufw status verbose
12.3.2 iptables 配置
#!/bin/bash
# iptables-setup.sh — 邮件服务器防火墙规则
# 清空现有规则
iptables -F
iptables -X
iptables -Z
# 默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# 允许回环接口
iptables -A INPUT -i lo -j ACCEPT
# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许 SSH(限制来源)
iptables -A INPUT -p tcp --dport 22 -s 10.0.0.0/8 -j ACCEPT
# 允许 SMTP
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
iptables -A INPUT -p tcp --dport 587 -j ACCEPT
iptables -A INPUT -p tcp --dport 465 -j ACCEPT
# 允许 IMAP/POP3
iptables -A INPUT -p tcp --dport 993 -j ACCEPT
iptables -A INPUT -p tcp --dport 995 -j ACCEPT
# 允许 HTTP/HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许 ICMP(ping)
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
# 速率限制(防 SYN 洪水)
iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -j ACCEPT
# 记录并丢弃其他流量
iptables -A INPUT -j LOG --log-prefix "IPTables-Dropped: "
iptables -A INPUT -j DROP
# 保存规则
iptables-save > /etc/iptables/rules.v4
12.3.3 nftables 配置(现代替代方案)
#!/usr/sbin/nft -f
# /etc/nftables.conf — 邮件服务器防火墙配置
flush ruleset
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
# 允许已建立的连接
ct state established,related accept
# 允许回环
iif lo accept
# 允许 ICMP
ip protocol icmp accept
ip6 nexthdr icmpv6 accept
# 允许 SSH
tcp dport 22 accept
# 允许 SMTP
tcp dport { 25, 587, 465 } accept
# 允许 IMAP/POP3
tcp dport { 993, 995 } accept
# 允许 HTTP/HTTPS
tcp dport { 80, 443 } accept
# 速率限制
tcp flags syn tcp dport 25 limit rate 10/second accept
# 记录并丢弃
log prefix "nftables-dropped: " drop
}
chain forward {
type filter hook forward priority 0; policy drop;
}
chain output {
type filter hook output priority 0; policy accept;
}
}
12.4 Fail2Ban 集成
12.4.1 安装 Fail2Ban
# 安装 Fail2Ban
sudo apt install -y fail2ban
# 启动服务
sudo systemctl enable --now fail2ban
12.4.2 配置 Fail2Ban
# /etc/fail2ban/jail.local — 全局配置
[DEFAULT]
# 封禁时间(秒)
bantime = 3600
# 检测时间窗口
findtime = 600
# 最大失败次数
maxretry = 5
# 使用 iptables 封禁
banaction = iptables-multiport
# 忽略的 IP
ignoreip = 127.0.0.1/8 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
12.4.3 Postfix 专用规则
# /etc/fail2ban/jail.d/postfix.local
[postfix]
enabled = true
port = smtp,465,submission
filter = postfix
logpath = /var/log/mail.log
maxretry = 3
bantime = 3600
[postfix-sasl]
enabled = true
port = smtp,465,submission,imap,imaps,pop3,pop3s
filter = postfix-sasl
logpath = /var/log/mail.log
maxretry = 3
bantime = 3600
[dovecot]
enabled = true
port = imap,imaps,pop3,pop3s
filter = dovecot
logpath = /var/log/mail.log
maxretry = 3
bantime = 3600
[postfix-ddos]
enabled = true
port = smtp,465,submission
filter = postfix-ddos
logpath = /var/log/mail.log
maxretry = 10
findtime = 60
bantime = 7200
12.4.4 自定义 Fail2Ban 过滤器
# /etc/fail2ban/filter.d/postfix-ddos.conf
[Definition]
failregex = ^.*reject: CONNECT from .*\[<HOST>\].*$
^.*reject: RCPT from .*\[<HOST>\].*$
^.*NOQUEUE: reject: .*\[<HOST>\].*$
ignoreregex =
12.4.5 Fail2Ban 管理命令
# 查看状态
sudo fail2ban-client status
# 查看特定 jail 状态
sudo fail2ban-client status postfix-sasl
# 手动封禁 IP
sudo fail2ban-client set postfix-sasl banip 203.0.113.100
# 手动解封 IP
sudo fail2ban-client set postfix-sasl unbanip 203.0.113.100
# 查看封禁日志
sudo tail -f /var/log/fail2ban.log
12.5 Postfix 速率限制
12.5.1 连接速率限制
# /etc/postfix/main.cf — 速率限制配置
# 客户端连接速率限制(次/秒)
smtpd_client_connection_rate_limit = 50
# 客户端并发连接数限制
smtpd_client_connection_count_limit = 20
# 每个会话的消息数限制
smtpd_client_message_rate_limit = 100
# 错误计数限制
smtpd_soft_error_limit = 3
smtpd_hard_error_limit = 10
# 错误后的延迟
smtpd_error_sleep_time = 5s
# 新连接延迟
smtpd_junk_command_limit = 100
12.5.2 使用 anvil 进行速率限制
# Postfix anvil 进程自动跟踪连接速率
# 验证 anvil 运行
ps aux | grep anvil
# 查看连接统计
postfix showq
12.5.3 基于策略的速率限制(postfwd)
# 安装 postfwd
sudo apt install -y postfwd
# /etc/postfwd/postfwd.cf
# 基于发件人的速率限制
id=RATE_LIMIT ; \
sender==. ; \
action=rate(sender/100/600)
# 基于客户端 IP 的速率限制
id=IP_RATE ; \
client_address==. ; \
action=rate(client_address/50/600)
# 基于收件人的速率限制
id=RCPT_RATE ; \
recipient==. ; \
action=rate(recipient/200/600)
# 在 main.cf 中配置
smtpd_recipient_restrictions =
check_policy_service inet:127.0.0.1:10040,
...
12.6 Postfix 连接限制
12.6.1 连接限制参数
# /etc/postfix/main.cf
# 客户端连接限制
smtpd_client_connection_count_limit = 50
smtpd_client_connection_rate_limit = 100
# 并发连接限制
default_process_limit = 100
# 特定服务的连接限制
# 在 master.cf 中配置
# smtp unix - - n - - smtp
# -o default_process_limit=50
12.6.2 基于 IP 的连接限制
# 使用 Postfix 的 anvil 进程
smtpd_client_connection_count_limit = 50
smtpd_client_connection_rate_limit = 100
# 使用 iptables 限制
iptables -A INPUT -p tcp --dport 25 -m connlimit --connlimit-above 10 -j REJECT
# 使用 nftables 限制
nft add rule inet filter input tcp dport 25 ct count over 10 reject
12.7 Postfix 安全配置加固
12.7.1 SMTP 安全选项
# /etc/postfix/main.cf
# 禁用 VRFY 命令(防止用户枚举)
disable_vrfy_command = yes
# 限制 EHLO 命令
smtpd_helo_required = yes
# 拒绝无效的 HELO/EHLO
smtpd_helo_restrictions =
permit_mynetworks,
reject_invalid_helo_hostname,
reject_non_fqdn_helo_hostname
# 限制发件人
smtpd_sender_restrictions =
permit_mynetworks,
reject_unknown_sender_domain,
reject_non_fqdn_sender
# 限制收件人
smtpd_recipient_restrictions =
permit_mynetworks,
permit_sasl_authenticated,
reject_unauth_destination,
reject_unknown_recipient_domain,
reject_non_fqdn_recipient
# 限制数据命令
smtpd_data_restrictions =
reject_unauth_pipelining
12.7.2 TLS 安全强化
# 强制 TLS(仅限提交端口)
# 在 master.cf 中
# submission inet n - y - - smtpd
# -o smtpd_tls_security_level=encrypt
# TLS 协议限制
smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1
smtpd_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1
# 密码套件限制
smtpd_tls_mandatory_ciphers = high
smtpd_tls_exclude_ciphers = aNULL, MD5, DES, 3DES, RC4
12.7.3 隐藏服务器信息
# 修改 SMTP 横幅
smtpd_banner = $myhostname ESMTP
# 禁止显示版本信息
# (默认情况下 Postfix 不会显示版本,但可以进一步自定义)
12.8 业务场景:企业安全加固方案
场景描述
一家金融机构需要符合 PCI-DSS 合规要求的邮件服务器安全配置。
安全加固清单
| 项目 | 配置 | 验证方法 |
|---|---|---|
| 加密传输 | TLS 1.2+ | openssl 测试 |
| 认证 | 强密码 + MFA | 认证日志审计 |
| 访问控制 | IP 白名单 | 防火墙规则 |
| 速率限制 | 连接/消息限制 | 监控指标 |
| 日志审计 | 完整日志记录 | 日志分析 |
| 入侵检测 | Fail2Ban | 封禁日志 |
| 漏洞扫描 | 定期更新 | 安全扫描 |
配置示例
# /etc/postfix/main.cf — 金融级安全配置
# TLS 强制
smtpd_tls_security_level = encrypt
smtp_tls_security_level = verify
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
# 严格的认证要求
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous, noplaintext
smtpd_tls_auth_only = yes
# 速率限制
smtpd_client_connection_rate_limit = 30
smtpd_client_message_rate_limit = 50
# 禁用危险命令
disable_vrfy_command = yes
# 日志增强
maillog_file = /var/log/mail.log
12.9 注意事项
⚠️ 防火墙配置:
- 配置防火墙前确保 SSH 访问不被阻断
- 测试环境先验证规则
- 保留物理/控制台访问作为后备
⚠️ Fail2Ban 配置:
bantime设置过长可能误封合法用户ignoreip中添加管理 IP- 定期检查封禁日志
💡 安全审计建议:
- 定期进行安全扫描
- 检查系统日志
- 验证配置合规性
- 进行渗透测试