如何阻止 sasl_username root 登录?

如何阻止 sasl_username root 登录?

我的 Postfix 服务器不断被垃圾邮件发送者利用。以下是sudo postcat -vq捕获的最后一条垃圾邮件的输出:

└──> sudo postcat -vq 3513818A8A
postcat: name_mask: all
postcat: inet_addr_local: configured 2 IPv4 addresses
postcat: inet_addr_local: configured 2 IPv6 addresses
*** ENVELOPE RECORDS deferred/3/3513818A8A ***
message_size:            8412             720               1               0            8412
message_arrival_time: Thu Aug 19 13:35:50 2021
create_time: Thu Aug 19 13:35:50 2021
named_attribute: log_ident=3513818A8A
named_attribute: rewrite_context=remote
named_attribute: sasl_method=LOGIN
named_attribute: sasl_username=root
sender: [email protected]
named_attribute: log_client_name=unknown
named_attribute: log_client_address=93.122.252.4
named_attribute: log_client_port=16374
named_attribute: log_message_origin=unknown[93.122.252.4]
named_attribute: log_helo_name=109-166-129-221.orangero.net
named_attribute: log_protocol_name=ESMTP
named_attribute: client_name=unknown
named_attribute: reverse_client_name=unknown
named_attribute: client_address=93.122.252.4
named_attribute: client_port=16374
named_attribute: helo_name=109-166-129-221.orangero.net
named_attribute: protocol_name=ESMTP
named_attribute: client_address_type=2
named_attribute: dsn_orig_rcpt=rfc822;[email protected]
original_recipient: [email protected]
recipient: [email protected]

根据这两行

named_attribute: sasl_method=LOGIN
named_attribute: sasl_username=root

我相信垃圾邮件发送者已成功登录,root并从我的服务器发送垃圾邮件。运行结果证实了这一点,sudo cat /var/log/maillog | grep sasl_username=root结果显示了许多条目,例如:

Aug 19 17:13:15 mail postfix/smtpd[11442]: EA58D18CCD: client=unknown[93.122.252.4], sasl_method=LOGIN, sasl_username=root
Aug 19 17:13:17 mail postfix/smtpd[11442]: BDA4E18D32: client=unknown[93.122.252.4], sasl_method=LOGIN, sasl_username=root
Aug 19 17:13:19 mail postfix/smtpd[11442]: 7387E18D31: client=unknown[93.122.252.4], sasl_method=LOGIN, sasl_username=root
Aug 19 17:13:21 mail postfix/smtpd[11442]: 1C0FB18D34: client=unknown[93.122.252.4], sasl_method=LOGIN, sasl_username=root
Aug 19 17:13:22 mail postfix/smtpd[11442]: DCB4418D36: client=unknown[93.122.252.4], sasl_method=LOGIN, sasl_username=root
Aug 19 17:13:24 mail postfix/smtpd[11442]: B62DD18D39: client=unknown[93.122.252.4], sasl_method=LOGIN, sasl_username=root
Aug 19 17:13:26 mail postfix/smtpd[11442]: 6F52B18D38: client=unknown[93.122.252.4], sasl_method=LOGIN, sasl_username=root
Aug 19 17:13:28 mail postfix/smtpd[11442]: 24DEF18D3A: client=unknown[93.122.252.4], sasl_method=LOGIN, sasl_username=root
Aug 19 17:13:29 mail postfix/smtpd[11442]: A30B418D3C: client=unknown[93.122.252.4], sasl_method=LOGIN, sasl_username=root
Aug 19 17:13:31 mail postfix/smtpd[11442]: 88D8318D3B: client=unknown[93.122.252.4], sasl_method=LOGIN, sasl_username=root
Aug 19 17:13:33 mail postfix/smtpd[11442]: 11F6118D44: client=unknown[93.122.252.4], sasl_method=LOGIN, sasl_username=root

除了 Postfix 之外,我的服务器上还运行着 Dovecot,因此检查/etc/dovecot/conf.d/10-mail.conf文件显示:

# Valid UID range for users, defaults to 500 and above. This is mostly
# to make sure that users can't log in as daemons or other system users.
# Note that denying root logins is hardcoded to dovecot binary and can't
# be done even if first_valid_uid is set to 0.
first_valid_uid = 1000
#last_valid_uid = 0

所以我现在不知道垃圾邮件发送者究竟是如何成功验证身份的root。不用说,root系统已禁用该用户。

有什么建议可以完全阻止 sasl_usernameroot登录系统?

答案1

由于不知道是谁这样设置了该系统以及为什么设置,我无法给出具体的建议。但我可以说,绝大多数 Dovecot 处理 IMAP 身份验证的服务器都可以通过让 Dovecot 处理 SMTP 身份验证来获得良好的服务,而这也是您的 Dovecot 配置first_valid_uid在邮件提交方面变得有效的方法。

相关文件然后考虑配置你的 Postfix 以将身份验证推迟到 Dovecot,通常是以下几行main.cf

smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth

此外,请考虑smtpd_sasl_auth_enable = no在 中设置并删除中的main.cf第一个。身份验证通常仅适用于用于提交的服务(以 smtps inet 和可选的 submission inet 开头的行),因此应仅通过 为一两个此类服务启用身份验证。这会破坏使用端口 25 提交经过身份验证的邮件的客户端 - 但如果您要更改配置,则此类客户端配置可能无论如何都需要检查。smtpd_sasl_auth_enable=yesmaster.cfmaster.cf

答案2

最近,我在最新的 Ubuntu 机器上也遇到了同样的问题。我确定我已经设置了 smtpd_sasl_type = dovecot 和 smtpd_sasl_path = private/auth。

我与用户 102362 有同样的疑问:当我在我的配置中读到这一点时:

# Note that denying root logins is hardcoded to dovecot binary and can't
# be done even if first_valid_uid is set to 0.

我认为无法以 root 身份进行身份验证。但是,为什么可以呢:

Jan 19 21:16:46 VPS postfix/smtpd[9392]: EF1B6BD5B8: client=unknown[213.233.y.x], sasl_method=LOGIN, sasl_username=root

并导致发送大量未经请求的邮件?

无论是否巧合,(成功的)SASL 身份验证来自同一个国家和运营商,因此我决定完全阻止以下阻止:

93.122.128.0/17
213.233.88.0/24
46.97.176.0/21
178.138.64.0/18
109.166.128.0/17

我更改了 root(和其他帐户)的密码。

答案3

实际上,这first_valid_uid只会影响 Dovecot 在 passwd 数据库中的 userdb 查找,而不会影响 passdb 查找。深入研究源文件后,可以清楚地发现有效性检查发生在passwd_iterate_want_pw文件中的函数中core/src/auth/userdb-passwd.c

static bool passwd_iterate_want_pw(struct passwd *pw, const struct auth_settings *set) {
    /* Skip entries not in the valid UID range.
       They're users for daemons and such. */
    if (pw->pw_uid < (uid_t)set->first_valid_uid)
        return FALSE;
    if (pw->pw_uid > (uid_t)set->last_valid_uid && set->last_valid_uid != 0)
        return FALSE;
    if (pw->pw_gid < (gid_t)set->first_valid_gid)
        return FALSE;
    if (pw->pw_gid > (gid_t)set->last_valid_gid && set->last_valid_gid != 0)
        return FALSE;
    return TRUE;
}

如果该函数返回 FALSE,则 userdb 查找将跳过该用户,从而导致错误,提示未找到该用户。

但是,在 passdb 查找过程中,会使用单独的数据库来验证用户名和密码。以默认驱动程序pam为例,其配置文件位于/etc/pam.d/dovecot,查找在 中执行/etc/shadow。passdb 查找不涉及使用 userdb 的 passwd 数据库检查用户名的有效性。

当在 Dovecot 的配置文件中启用该auth_debug = yes设置时,以下是 smtpd 身份验证期间生成的日志示例:

1111-11-11T11:11:41.122920+00:00 myserver dovecot: auth: Debug: client in: AUTH#0114#011plain#011service=smtp#011nologin#011lip=127.0.0.1#011rip=127.0.0.1.87#011secured
1111-11-11T11:11:41.123831+00:00 myserver dovecot: auth: Debug: client passdb out: CONT#0114#011
1111-11-11T11:11:43.185585+00:00 myserver dovecot: auth: Debug: client in: CONT<hidden>
1111-11-11T11:11:43.185703+00:00 myserver dovecot: auth: Debug: pam(root,127.0.0.1.87): Performing passdb lookup
1111-11-11T11:11:43.194570+00:00 myserver dovecot: auth-worker(37592): Debug: Loading modules from directory: /usr/lib/dovecot/modules/auth
1111-11-11T11:11:43.195187+00:00 myserver dovecot: auth-worker(37592): Debug: Module loaded: /usr/lib/dovecot/modules/auth/lib20_auth_var_expand_crypt.so
1111-11-11T11:11:43.195807+00:00 myserver dovecot: auth-worker(37592): Debug: conn unix:auth-worker (pid=37547,uid=107): Server accepted connection (fd=13)
1111-11-11T11:11:43.196014+00:00 myserver dovecot: auth-worker(37592): Debug: conn unix:auth-worker (pid=37547,uid=107): Sending version handshake
1111-11-11T11:11:43.196326+00:00 myserver dovecot: auth-worker(37592): Debug: conn unix:auth-worker (pid=37547,uid=107): auth-worker<1>: Handling PASSV request
1111-11-11T11:11:43.196575+00:00 myserver dovecot: auth-worker(37592): Debug: conn unix:auth-worker (pid=37547,uid=107): auth-worker<1>: pam(root,127.0.0.1.87): Performing passdb lookup
1111-11-11T11:11:43.196757+00:00 myserver dovecot: auth-worker(37592): Debug: conn unix:auth-worker (pid=37547,uid=107): auth-worker<1>: pam(root,127.0.0.1.87): lookup service=dovecot
1111-11-11T11:11:43.199784+00:00 myserver dovecot: auth-worker(37592): Debug: conn unix:auth-worker (pid=37547,uid=107): auth-worker<1>: pam(root,127.0.0.1.87): #1/1 style=1 msg=Password: 
1111-11-11T11:11:45.306854+00:00 myserver dovecot: auth: Debug: pam(root,127.0.0.1.87): Finished passdb lookup

可以看到,pam整个流程只调用了模块,并没有像模块一样判断用户的合法性passwd,因此,用户root只要密码正确就可以通过SMTPD认证,可以正常发送邮件,但收不到邮件。

考虑到启用 root 用户密码登录带来的安全漏洞,建议运行以下命令passwd -l root禁用 的密码。此命令在文件中为用户密码root设置一个不可能的值。因此,无论在 SASL 中尝试输入什么密码,模块都会返回错误,从而阻止通过 SASL 登录。root/etc/shadowrootpamAuthentication failure (Password mismatch?)root

答案4

我们遇到了类似的问题。在我们的案例中,root 密码被无意中删除(passwd -d root-- 而我们应该运行passwd -l root)。幸运的是,这不允许 root ssh 登录(PermitRootLogin no/etc/ssh/sshd_config),但它确实允许垃圾邮件发送者以 root 身份登录到电子邮件服务器并发送垃圾邮件。

当我们使用 禁用 root 密码时,问题就消失了passwd -l root

相关内容