拒绝ssh远程暴力破解
如何判断自己正在遭受ssh暴力破解
ssh登录服务器后使用lastb命令,命令会显示最近错误登录的日志,包括通过ssh服务错误登录的日志。
admin ssh:notty 191.201.86.105 Thu May 3 05:11 – 05:11 (00:00)
admin ssh:notty 191.201.86.105 Thu May 3 05:11 – 05:11 (00:00)
admin ssh:notty 191.201.86.105 Thu May 3 05:10 – 05:10 (00:00)
admin ssh:notty 191.201.86.105 Thu May 3 05:10 – 05:10 (00:00)
admin ssh:notty 191.201.86.105 Thu May 3 05:10 – 05:10 (00:00)
admin ssh:notty 191.201.86.105 Thu May 3 05:10 – 05:10 (00:00)
admin ssh:notty 191.201.86.105 Thu May 3 05:10 – 05:10 (00:00)
admin ssh:notty 191.201.86.105 Thu May 3 05:10 – 05:10 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:10 – 05:10 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:10 – 05:10 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:10 – 05:10 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:10 – 05:10 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:10 – 05:10 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:10 – 05:10 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:10 – 05:10 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:10 – 05:10 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:09 – 05:09 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:09 – 05:09 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:09 – 05:09 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:09 – 05:09 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:09 – 05:09 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:09 – 05:09 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:09 – 05:09 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:09 – 05:09 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:09 – 05:09 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:09 – 05:09 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:08 – 05:08 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:08 – 05:08 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:08 – 05:08 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:08 – 05:08 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:08 – 05:08 (00:00)
root ssh:notty 191.201.86.105 Thu May 3 05:08 – 05:08 (00:00)
如果得到和我上方显示差不多的情况,出现大量的输出说明你的服务器正在遭受黑客们的暴力破解。
如何防范暴力破解
方法一:修改远程登录端口
修改/etc/ssh/sshd_config文件中的Port 将前方的#注释删除,并将22修改为你想要使用远程登录的端口,例如54231。
在防火墙上添加运行端口:
iptables -I INPUT -p tcp –dport 54231 -j ACCEPT
如果您使用的ubuntu系统并使用ufw,可以使用命令:sudo ufw allow 54231
如果您使用CentOS7系统并使用firewalld,可以使用命令:
firewall-cmd –zone=public –add-port=54231/tcp –permanent
如果是使用云服务器,云服务商有提供外部防火墙,例如阿里云的安全组的情况下还要在外部的安全组中修改允许tcp端口54231端口的策略。
最后使用serivce sshd restart重启sshd服务即可。
CentOS7用户使用systemctl restart sshd。
重启服务之后登录服务器都需要输入指定的远程端口才能够登录服务器,大大降低了黑客攻击服务器的概率。
方法二:限制登录IP
如果你的公司或者常常需要登录服务器的工作网络环境拥有固定的IP地址时,限制登录IP地址就可以很好的保护服务器免受黑客攻击。如果没有上述的条件,我们也可以通过搭建堡垒机,将所有服务器都设置为允许堡垒机的IP地址登录即可保护您的服务器。
操作起来相当简单,首先在/etc/hosts.allow中加入如下信息:
sshd:172.16.*.*
这条信息表示sshd服务允许向IP地址为172.16开头的IP地址提供服务。
其次在/etc/hosts.deny中加入如下信息:
sshd:all:deny
这条信息表示sshd服务不允许向所有IP地址提供服务。
如此一来服务器将只有指定的IP地址可以访问。
方法三:使用非root用户登录
禁止root用户登录并且设置允许其他用户登录后切换至root进行操作。这样黑客如果想要通过ssh登录服务器除了密码之外首先他还需要知道服务器允许登录的用户,这样将大大降低黑客攻破ssh的可能性。
首先,在服务器内创建一个用户,learntop:useradd learntop
并且设置密码:passwd learntop ,当然密码需要尽量复杂。
接下来我们需要允许此用户能够以root用户身份运行命令。
使用visudo命令进入文本编辑模式,于92行附近
##
## user MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
learntop ALL=(ALL) NOPASSWD:ALL
[root@localhost ~]# su learntop
[learntop@localhost root]$ sudo su –
Last login: Tue May 22 12:55:36 CST 2018 on pts/1
[root@localhost ~]#whoami
root