bash:多次尝试login失败后阻止用户

我有个问题。

有人试图进入我的服务器,而且经常发生。 例如:

Aug 19 14:11:42 oplot sshd[18373]: input_userauth_request: invalid user oracle Aug 19 14:11:42 oplot sshd[18372]: pam_unix(sshd:auth): check pass; user unknown Aug 19 14:11:42 oplot sshd[18372]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=211.38.137.44 Aug 19 14:11:44 oplot sshd[18372]: Failed password for invalid user oracle from 211.38.137.44 port 36 841 ssh2 Aug 19 14:11:45 oplot sshd[18373]: Received disconnect from 211.38.137.44: 11: Bye Bye Aug 19 14:11:47 oplot sshd[18374]: Invalid user test from 211.38.137.44 Aug 19 14:11:47 oplot sshd[18375]: input_userauth_request: invalid user test Aug 19 14:11:47 oplot sshd[18374]: pam_unix(sshd:auth): check pass; user unknown Aug 19 14:11:47 oplot sshd[18374]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=211.38.137.44 

还有这个

 Aug 19 14:58:51 oplot sshd[19543]: Failed password for root from 202.117.56.29 port 43025 ssh2 Aug 19 14:58:52 oplot sshd[19544]: Received disconnect from 202.117.56.29: 11: Bye Bye Aug 19 14:58:55 oplot sshd[19546]: reverse mapping checking getaddrinfo for 56h29.xjtu.edu.cn [202.117.56.29] failed - POSSIBLE BREAK-IN ATTEMPT! 

你能向我解释一下“反向映射检查getaddrinfo”是什么意思?

而且我怎么能阻止IPs几次这样的尝试?

fail2ban是阻止大量假sshlogin尝试的stream行方式。 我build议你把重点放在阻止人们攻击你的ssh端口,而不是担心坏的反向DNS查找。

什么ssh调用“可能的闯入尝试”只是一个信号,说明有问题的地址是错误configuration的。 你可以检查:

 ~% host 202.117.56.29 29.56.117.202.in-addr.arpa domain name pointer 56h29.xjtu.edu.cn. ~% host 56h29.xjtu.edu.cn Host 56h29.xjtu.edu.cn not found: 3(NXDOMAIN) 

DNS PTRlogging是一个安全问题,因为绝对没有任何东西让你不能告诉你的地址主机名是你想要的,甚至是“google.com”或“nsa.gov”。 所以,SSH做了额外的检查,以确保地址和主机名之间存在正确的双向关系。

如果没有先保护您的SSH服务端口,不要尝试阻止多次尝试。 您应该考虑禁用SSH服务,或至less防火墙。 如果您无法防火墙,请尝试使用端口敲打解决scheme来防止随机攻击。 此外,禁用密码authentication,并强制每个人使用公钥authentication。

退房DenyHost 。 禁止尝试暴力破解SSHlogin的IP是一个很好的方法。 您还可以从尝试在全球其他主机上进行特技的IP共享阻止列表中受益。

如果你更喜欢更轻量级的解决scheme,你可以使用iptables来阻止在ssh上重复尝试。 假设你的iptables规则已经设置了默认的DROP策略并且只允许特定的端口,下面的规则将会打开端口22(ssh),这样,一旦有超过3个连接,它就会暂时阻止来自特定IP的SSH连接每分钟:

 iptables -A INPUT -m hashlimit --hashlimit 3/min --hashlimit-burst 3 --hashlimit-mode srcip --hashlimit-name inputssh -p tcp --dport 22 -j ACCEPT 

请注意,如果它们超过频率阈值,这也将停止成功login,因此您必须select适当的值。

有关更多信息,您可以尝试“man iptables”“iptables -m hashlimit –help” 。 另外,如果你不熟悉iptables,那么使用“iptables教程”和“iptables hashlimit”提供一个很好的起点。