不应该/ / 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之前,不应该阻止连接吗? 我已经通过阻止我的家用机器进行了testing,在获得login提示之前,它肯定会拒绝我一个连接(是的,我已经移走了我的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->名称查找,那么它会在谨慎的方面发生错误,并且不会阻塞该主机。 是对的吗?

这是sshd的预期,因为它直接链接到libwrap,所以sshd实际上是执行主机检查。 如果你想在sshd被调用之前阻塞连接,你需要在它之前放置一些东西来处理连接尝试,然后把它传递给sshd。 几个选项将是:

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

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

但是,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 (eg 512) using sshd from inetd may be feasible. 

使用iptables或者在你的服务器前放置一个防火墙看起来可能是一个不错的select,但大多数不会执行任何名称parsing,所以你只能通过IP地址来控制访问。 这不一定是坏事,但它不会帮助你想要完成的事情。