/etc/hosts.deny 不应该在它到达 ssh 日志之前拦截它吗?

/etc/hosts.deny 不应该在它到达 ssh 日志之前拦截它吗?

我在 ubuntu 10.04.04 服务器上结合使用了 /etc/hosts.deny 和 ufw。大多数时候,我都会看到来自 .com.cn 域中的机器的 ssh 尝试失败,报告如下:

Failed logins from:
112.114.63.139 (139.63.114.112.broad.km.yn.dynamic.163data.com.cn): 1 time

但是,在 /etc/hosts.deny 中,我有以下规则:

ALL: .com.cn

这难道不应该在到达 ssh 之前就阻止连接吗?我已经通过阻止我的家用机器进行了测试,它肯定会在我收到登录提示之前立即拒绝我的连接(是的,我已经将 ssh 密钥移开,因此这些密钥不受影响)。

这能按预期工作吗?

编辑: James Sneeringer 提醒我更仔细地查看日志,也许我明白了为什么会发生这种情况。来自 auth.log:

Nov  5 09:38:40 mymachine sshd[22864]: warning: /etc/hosts.deny, line 21: can't verify hostname: getaddrinfo(139.63.114.112.broad.km.yn.dynamic.163data.com.cn, AF_INET) failed
Nov  5 09:38:44 mymachine sshd[22864]: reverse mapping checking getaddrinfo for 139.63.114.112.broad.km.yn.dynamic.163data.com.cn [112.114.63.139] failed - POSSIBLE BREAK-IN ATTEMPT!
Nov  5 09:38:45 mymachine sshd[22864]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=112.114.63.139  user=root
Nov  5 09:38:47 mymachine sshd[22864]: Failed password for root from 112.114.63.139 port 37245 ssh2
Nov  5 09:38:47 mymachine sshd[22866]: Connection closed by 112.114.63.139

对我来说,这意味着如果 sshd 不确定 IP->name 查找,那么它会谨慎行事,不会阻止该主机。对吗?

答案1

这对于 sshd 来说是意料之中的,因为它直接链接到 libwrap,所以 sshd 实际上是执行主机检查的。如果您想在调用 sshd 之前阻止连接,您需要在它前面放置一些东西来处理连接尝试,然后再将其传递给 sshd。以下是几个选项:

  • 在 inetd(或 xinetd)下运行 sshd。这将允许您将 sshd 作为 tcpd 的参数调用,而 tcpd 最终将执行实际的主机检查。

  • 在 xinetd 下运行 sshd,它具有提供类似于 /etc/hosts.allow 和 /etc/hosts.deny 的功能的服务选项only_fromno_access

但是 sshd 手册页不鼓励使用这些方法:

 -i      Specifies that sshd is being run from inetd(8).  sshd is normally
         not run from inetd because it needs to generate the server key
         before it can respond to the client, and this may take tens of
         seconds.  Clients would have to wait too long if the key was
         regenerated every time.  However, with small key sizes (e.g. 512)
         using sshd from inetd may be feasible.

使用 iptables 或在服务器前放置防火墙似乎是一个不错的选择,但大多数方法都不执行任何名称解析,因此您只能通过 IP 地址控制访问。这不一定是坏事,但对您想要实现的目标没有帮助。

相关内容