iRedMail 邮件服务器加固的三个高频失守点很固定:管理面板 iRedAdmin 直接暴露公网、25/587 端口没有任何速率限制、Dovecot 仍允许明文认证。自建邮局只要 MX 解析生效,日志里 24 小时内就会出现针对 postfix-sasl 的字典爆破,几千次认证失败之后往往跟着一次成功登录,以及最麻烦的「开放中继」滥用——IP 进 RBL 黑名单,正常业务邮件一起被拒。
下面的配置按「端口收口 → 认证加固 → 联动封禁 → 面板隔离」四层推进,每一层都能独立落地,全部可在 iRedMail 1.6/1.7 的默认目录结构上直接执行。
适用场景
- 使用 iRedMail 自建企业邮局,域名 MX 已生效、25 端口对公网开放
- 服务器部署在云上,安全组只做了粗粒度放行,内网任意主机可连 3306/6379 类管理端口
- 需要通过等保 2.0 三级的「身份鉴别、访问控制、入侵防范」条款检查
- 已出现异常发信量、退信暴涨,或出口 IP 被列入 Spamhaus 等 RBL
前置条件
- iRedMail 1.6.x / 1.7.x,CentOS Stream 9、Rocky Linux 9、Ubuntu 22.04 下结构一致
- root 或 sudo 权限;Postfix 3.5+、Dovecot 2.3+
- A 记录、MX 记录、SPF、PTR 已配置完毕(PTR 缺失会让下文的客户端主机名校验大面积误拒)
- 一台外网机器用于 SMTP 验证,装好 swaks:
dnf install -y swaks或apt install -y swaks - 变更窗口 5 分钟:Postfix 与 Dovecot 重载会让进行中的连接断开
原理说明
iRedMail 不是一个服务,而是 Postfix、Dovecot、Roundcube、iRedAdmin、Fail2ban 拼起来的组合。加固前先把攻击面拆清楚:
- 25/tcp:MTA 收信端口,必须对公网开放,是爆破与账号枚举的主要入口
- 587/tcp、465/tcp:MSA 提交端口,必须强制 SASL 认证加 TLS,否则等于开放中继
- 993/995:IMAPS/POP3S,认证失败同样计入爆破统计,常被忽视
- /mail/:Roundcube 登录入口,绕过 SMTP 直接打认证,还能触发 installer 泄露数据库口令
- /iredadmin/:管理面板,一个弱口令等于整台邮局失守,可增删域名与邮箱
Postfix 的访问控制在三个阶段求值:smtpd_client_restrictions(连接建立时)、smtpd_helo_restrictions / smtpd_sender_restrictions(HELO 与 MAIL FROM 阶段)、smtpd_recipient_restrictions(RCPT TO 阶段)。把 reject_unauth_destination 写到错误的阶段不会报错,只会静默失效——这是「配了但没用」最常见的原因。
限速则由 Postfix 内置的 anvil 服务实现,按客户端 IP 统计:smtpd_client_connection_rate_limit 控制建连速率,smtpd_client_message_rate_limit 控制单位时间收件数。这两个参数是压住单 IP 高频投递最直接的手段,比 fail2ban 的事后封禁更早生效。
操作步骤
1. 端口收口:只留必要入口
先看真实监听状态,确认没有意料之外的服务在跑:
ss -lntp | grep -E ':(25|110|143|465|587|993|995)\b'
Dovecot 侧直接不监听明文 110/143,比在防火墙上封更彻底——本机回环也不会留明文入口:
cat >> /etc/dovecot/dovecot.conf <<'EOF'
service imap-login {
inet_listener imap {
port = 0
}
}
service pop3-login {
inet_listener pop3 {
port = 0
}
}
EOF
doveadm reload
systemctl restart dovecot
防火墙只放行五个服务,明文端口显式移除:
firewall-cmd --permanent --add-service=smtp --add-service=smtps \
--add-service=submission --add-service=imaps --add-service=pop3s
firewall-cmd --permanent --remove-service=imap --remove-service=pop3
firewall-cmd --reload
firewall-cmd --list-services
2. Postfix 反滥用策略
用 postconf -e 逐条写入,避免手改 main.cf 时多行参数缩进断裂。以下参数中,smtpd_client_connection_rate_limit 与 smtpd_client_message_rate_limit 是限速核心:
postconf -e 'disable_vrfy_command = yes'
postconf -e 'smtpd_helo_required = yes'
postconf -e 'smtpd_helo_restrictions = permit_mynetworks, reject_invalid_helo_hostname, reject_non_fqdn_helo_hostname'
postconf -e 'smtpd_sender_restrictions = permit_mynetworks, reject_non_fqdn_sender, reject_unknown_sender_domain'
postconf -e 'smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination, reject_non_fqdn_recipient, reject_unknown_recipient_domain'
postconf -e 'smtpd_client_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unknown_client_hostname'
postconf -e 'smtpd_client_connection_rate_limit = 30'
postconf -e 'smtpd_client_message_rate_limit = 60'
postconf -e 'smtpd_client_recipient_rate_limit = 120'
postconf -e 'smtpd_recipient_limit = 200'
postconf -e 'message_size_limit = 52428800'
postconf -e 'smtpd_soft_error_limit = 5'
postconf -e 'smtpd_hard_error_limit = 10'
postfix check && postfix reload
reject_unknown_client_hostname 要求客户端 IP 有可解析且正反向一致的 PTR。国内部分企业出口 IP 没有 PTR,直接上会误拒。灰度方式是用 warn_if_reject 前缀让规则只记日志不拒绝,观察一周后再决定:
postconf -e 'smtpd_client_restrictions = permit_mynetworks, permit_sasl_authenticated, warn_if_reject reject_unknown_client_hostname'
postfix reload
# 一周后统计被标记的客户端占比
grep "Client host rejected" /var/log/maillog | wc -l
3. 587/465 强制认证与 TLS
master.cf 里的 submission 段若缺少 smtpd_sasl_auth_enable=yes,587 就会对未认证会话开放中继,这是被滥用发垃圾邮件最常见的配置缺口:
sed -n '/^submission/,/^$/p' /etc/postfix/master.cf
目标形态如下(-o smtpd_tls_security_level=encrypt 与 -o smtpd_client_restrictions=permit_sasl_authenticated,reject 两行必须存在):
submission inet n - y - - smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINAL
smtps inet n - y - - smtpd
-o syslog_name=postfix/smtps
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
改完执行 postfix reload。注意 465 走的是 wrappermode(连上即 TLS),587 走 STARTTLS,两者不能混用同一段配置。
4. Dovecot 认证加固
cat >> /etc/dovecot/dovecot.conf <<'EOF'
disable_plaintext_auth = yes
auth_failure_delay = 3 secs
mail_max_userip_connections = 10
ssl = required
ssl_min_protocol = TLSv1.2
EOF
doveconf -n | grep -E 'auth_failure_delay|ssl_min_protocol|disable_plaintext_auth'
doveadm reload
auth_failure_delay 每次失败延迟 3 秒,配合 Fail2ban 的 3 次阈值,单 IP 打满阈值的成本从毫秒级升到 9 秒以上,字典爆破的实际速率被压到原来的几十分之一。mail_max_userip_connections 限制单 IP 对单账号的并发连接,防的是「一次认证、多连接并发拉信」拖垮信箱。
disable_plaintext_auth = yes 只禁止明文通道上的 PLAIN/LOGIN,993/995 上加密后仍可用,老客户端只要能开 SSL/TLS 就不受影响。
5. Fail2ban 三类 jail 联动
iRedMail 安装时若勾选 Fail2ban,会生成 /etc/fail2ban/jail.local 骨架。核心是让 postfix-sasl、dovecot、roundcube-auth 三个 jail 全部启用,并统一封禁动作:
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 5
ignoreip = 127.0.0.1/8 10.0.0.0/8
banaction = firewallcmd-rich-rules[actiontype=<multiport>]
[postfix-sasl]
enabled = true
filter = postfix-sasl
port = smtp,465,submission,imap,imaps,pop3,pop3s
logpath = /var/log/maillog
maxretry = 3
[dovecot]
enabled = true
filter = dovecot
logpath = /var/log/dovecot/*.log
maxretry = 5
[roundcube-auth]
enabled = true
filter = roundcube-auth
logpath = /opt/www/roundcubemail/logs/errors.log
maxretry = 5
Ubuntu 的 Postfix 日志在 /var/log/mail.log;若系统日志走 systemd-journald,则去掉 logpath 改成 backend = systemd,否则 jail 会一直显示 0 total。postfix-sasl 的 maxretry 设 3 比 5 更合适:邮件客户端密码连错三次基本就是配置错误或爆破,没有正常业务会错三次。
systemctl enable --now fail2ban
systemctl restart fail2ban
fail2ban-client status
fail2ban-client status postfix-sasl
6. iRedAdmin 与 Roundcube 面板收口
iRedMail 的 Nginx 站点模板在 /etc/nginx/templates/,iredadmin.tmpl 里定义了 location /iredadmin。改成「IP 白名单 + Basic Auth」双层,即便面板爆出 0day,攻击者也拿不到登录框:
location /iredadmin/ {
allow 203.0.113.10; # 替换为运维出口 IP
allow 10.0.0.0/8;
deny all;
auth_basic "iRedAdmin";
auth_basic_user_file /etc/nginx/iredadmin.htpasswd;
include /etc/nginx/templates/fastcgi_iredadmin.tmpl;
}
htpasswd -c /etc/nginx/iredadmin.htpasswd ops
chown root:nginx /etc/nginx/iredadmin.htpasswd
chmod 640 /etc/nginx/iredadmin.htpasswd
nginx -t && systemctl reload nginx
Roundcube 的 installer 目录会直接暴露数据库账号口令,必须关闭并移出 webroot:
cd /opt/www/roundcubemail
sed -i "s/^.*enable_installer.*/\$config['enable_installer'] = false;/" config/config.inc.php
grep -n "enable_installer" config/config.inc.php
mv installer /root/roundcube-installer-bak
7. 日志侧的证据留存
加固之后要能回答「谁在打、打到什么程度」。三条命令覆盖日常巡检:
# 认证失败总量与前十个来源 IP
grep -c "authentication failed" /var/log/maillog
grep "authentication failed" /var/log/maillog | grep -oE '\[[0-9.]+\]' | sort | uniq -c | sort -rn | head -10
# 收发明细与队列积压
/usr/sbin/pflogsumm -d today /var/log/maillog | head -25
postqueue -p | tail -1
配置验证
逐项确认参数已生效,不要只看命令是否报错:
# 1) Postfix 生效参数
postconf -n | grep -E 'disable_vrfy|smtpd_client_connection_rate_limit|smtpd_sasl_auth_enable|smtpd_tls_security_level'
# 2) Dovecot 生效参数
doveconf -n | grep -E 'ssl_min_protocol|auth_failure_delay|disable_plaintext_auth'
# 3) 监听端口:110/143 应已消失
ss -lntp | grep -E ':(25|110|143|465|587|993|995)\b'
# 4) TLS 1.1 必须握手失败
openssl s_client -connect mail.example.com:993 -tls1_1 </dev/null 2>&1 | grep -E 'Protocol|alert'
# 5) 587 错误口令必须被拒
swaks --to test@example.com --server mail.example.com:587 -tls \
--auth LOGIN --auth-user test@example.com --auth-password wrong-pass
# 6) 封禁计数
fail2ban-client status postfix-sasl
swaks 期望返回 535 5.7.8 Error: authentication failed。若返回 554 或直接进入发信流程,说明 587 的 smtpd_sasl_auth_enable 没生效,回去检查 master.cf 的 submission 段。fail2ban-client status postfix-sasl 中 Currently banned 在连续三次失败后应从 0 变 1。openssl s_client 期望看到握手失败告警而不是 Protocol : TLSv1.1。
常见问题
客户端报 554 5.7.1 Client host rejected: cannot find your reverse hostname
触发者是 reject_unknown_client_hostname,它要求客户端 IP 的 PTR 可解析且正反向一致。处理顺序:先为发信出口 IP 配置 PTR(云厂商控制台一般可自助设置);短期用 warn_if_reject reject_unknown_client_hostname 只记日志;不要为省事把客户端网段塞进 mynetworks——mynetworks 等于无条件信任,等同于开放中继。
Fail2ban 的 jail 显示 enabled 但从不封禁
九成是日志后端不匹配:journald 系统上 /var/log/maillog 不存在,fail2ban 静默跳过。先看实际解析到的配置,再验证过滤器能否命中日志行:
fail2ban-client -d | grep -A5 postfix-sasl
fail2ban-regex /var/log/maillog /etc/fail2ban/filter.d/postfix-sasl.conf --print-all-matched | head -20
匹配数为 0 说明日志格式变了(iRedMail 升级后 syslog_name 可能带 postfix/ 前缀),改 filter 的 failregex,或把 logpath 换成 backend = systemd。
云服务器 25 端口出站被封,外发邮件全部超时
多数云厂商默认封禁 25/tcp 出站以抑制垃圾邮件。入站 25 必须保留,否则收不到外部来信;出站改走 587 中继:
postconf -e 'relayhost = [smtp.example.net]:587'
postconf -e 'smtp_sasl_auth_enable = yes'
postconf -e 'smtp_sasl_password_maps = hash:/etc/postfix/relay_passwd'
postconf -e 'smtp_sasl_security_options = noanonymous'
postconf -e 'smtp_tls_security_level = encrypt'
echo '[smtp.example.net]:587 relay-user:relay-pass' > /etc/postfix/relay_passwd
postmap /etc/postfix/relay_passwd
chmod 600 /etc/postfix/relay_passwd /etc/postfix/relay_passwd.db
postfix reload
总结
四层加固的优先级:面板收口 > 提交端口认证 > 限速 > 封禁。面板暴露是最贵的失守(直接拿管理员),587 缺认证会立刻变成中继跳板,限速与封禁只影响攻击成本。
三个长期动作:把 postconf -n 的输出纳入版本管理,每次升级 iRedMail 后 diff 一次;把 fail2ban-client status 与 postqueue -p 接进监控;PTR、SPF、DKIM 与实际出口 IP 每季度复核一遍,换 IP 后忘改 SPF 会让正常邮件直接进垃圾箱。