如何防止SSH暴力破解

当你查看你的 SSH 服务日志,可能你会发现充斥着一些不怀好意的尝试性登录。这里有 9 条常规建议(和一些个别特殊策略)可以让你的 SSH 会话更加安全。


其中有很多办法来阻挡这些密码尝试:

0 1、修改端口


vi /etc/ssh/sshd_config

 #Port 22

默认端口为22,为了避免被扫描,去掉#,改成一个其他的端口,比如45632
保存后重启sshd服务.service sshd restart

0 2、健壮的密码


密码的复杂性可以增加破解的难度,大小写混合加上数字并且有足够的密码长度就比较安全了,密码的长度要大于8位最好大于20位。密码的复杂度是密码要尽可能有数字、大小写字母和特殊符号混合组成,唯一的问题就是要牢记密码.

0 3、不允许root账号直接登陆


添加普通账号,授予root的权限
修改sshd 设置,编辑/etc/ssh/sshd_config:

'PermitRootLogin no'

0 4、RSA公钥认证


认证登录是内部的机子可以无密码直接登录,非常方便省事
他的原理是采用RSA加密算法 如果对RSA不是很了解可以点击下面传送带:
http://www.cnblogs.com/demonxian3/p/6261816.html
默认的登录方式是password,如果需要用RSA公钥登录,需要先创建RSA Key

#ssh-keygen -t rsa -b 1024

会生成私钥/home/username/.ssh/id_rsa
同时生成公钥/home/username/.ssh/id_rsa.pub
输入一个加密短语(也可省略)
修改sshd 设置,编辑/etc/ssh/sshd_config:

'PasswordAuthentication no'RSAAuthentication yesPubkeyAuthentication yesAuthorizedKeysFile/    .ssh/id_rsa.pub

将公钥下载到本地计算机,然后在ssh客户端软件Secure CRT中设置好,就可以登录了

0 5、使用iptables脚本


此脚本允许每分钟3个连接.有白名单,并有日志纪录

iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set  --name SSHiptables -A INPUT -p tcp --dport 22 -m state --state NEW -j SSH_WHITELISTiptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update  --seconds 60 -- hitcount 4 --rttl --name SSH -j ULOG --ulog-prefix SSH_brute_forceiptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update  --seconds 60 -- hitcount 4 --rttl --name SSH -j DROP

iptables的版本需要>1.2.11 (1.2不支持 —rttl参数)

0 6、使用sshd日志过滤


有几个软件(脚本)可以做到.
sshfilter http://www.csc.liv.ac.uk/~greg/sshdfilter/
Fail2Ban http://fail2ban.sourceforge.net/
DenyHosts http://denyhosts.sourceforge.net/
需要sshd支持tcp_wrappers此功能.
ldd /usr/sbin/sshd | grep libwrap

libwrap.so.0 => /usr/lib64/libwrap.so.0 (0x0000002a9566c000)

如果有libwrap.so.0这个库说明支持此功能
另一种测试方法:在/etc/hosts.deny加入一行127.0.0.1
然后ssh localhost,如果无法连接,说明支持.

$ tar zxvf DenyHosts-2.6.tar.gz$ cd DenyHosts-2.6

as root:

# python setup.py install#cd /usr/share/denyhost# cp denyhosts.cfg-dist denyhosts.cfg

配置文件

# cp daemon-control-dist daemon-control

启动脚本

# chown root daemon-control# chmod 700 daemon-control# cd /etc/init.d# ln -s /usr/share/denyhosts/daemon-control denyhosts# chkconfig --add denyhosts

添加到开机启动服务中

0 7、使用tcp_wrappers过滤


1 下载脚本http://www.la-samhna.de/misc/sshblock.sh
2 设置可执行权限 chmod 755 /usr/local/bin/sshblock.sh
3 在/etc/hosts.allow中添加以下内容

#__START_SSHBLOCK__#__END_SSHBLOCK__

sshd : ALL : spawn (/usr/local/bin/sshblock.sh %a)&
设置

  
  
  1. DONTBLOCK 白名单

  2. BURST_MAX=5

  3. BURST_TIM=60    60秒内登录5次就封锁

  4. PURGE_TIM=3600    3600秒后解冻


0 8、使用KNOCKD


knockd 监视一个预定义模式在iptables的日志,例如一次击中端口6356,一次击中端口63356,两次击中端口9356,这相当于敲一个关闭的门用一种特殊的暗码来被konckd识别,konckd将使用iptables来打开一个预定义端口例如ssh的22端口在一个预定定义时间.(比如一分钟),如果一个ssh session 在这个时间范围内打开,这个端口会一直保留.直到预定义时间过期后ssh端口被knockd关掉.


缺点:比较复杂的方案,不适合普通人
需要客户端(knockd-client)来实现port knocking”,同时需要knockd-server来响应

实际上,很容易检测到这种通讯模式,一旦***者可以监控你的通讯,这种解决方案无法提供安全防护针对本地***者

0 9、借助第三方工具fail2ban防御


这个工具原理很简单 ,他可以检测我们认证日志,如果发现了有暴力破解的迹象
他就会对相应IP采取动作 比如ban掉IP
工具下载地址:http://www.fail2ban.org
tar zxf fail2ban-0.8.14.tar.gz
cd fail2ban-0.8.14;python setup.py install
记得要把/files/reha-initdt文件 移动到 /etc/init.d/下
编辑配置文件 /etc/fail2ban/jail.conf

[xinetd-fail]enabled = falsefilter = xinetd-failaction = iptables-allports[name=xinetd,protocol=all]logpath = /var/log/daemon*log[ssh-iptables]enabled = truefilter = sshdaction = iptables[name=SSH,port=ssh,protocol=tcp]logpath = /var/log/securemaxretry = 5findtime = 300bantime = 300

启动后查看是否生成规则链
/etc/init.d/fail2ban start
iptables -L -n

  
  
  1. Chain fail2ban-SSH (1 references)

  2. target    prot opt source    destination

  3. RETURN    all -- 0.0.0.0/0    0.0.0.0/0



猜你喜欢

转载自blog.51cto.com/13475644/2336845
今日推荐