sshd服务防止暴力破解

sshd防止暴力破解几种方式:

1.密码足够复杂

2.修改默认端口号

3.不适用root用户名登录。

1 #是否可以禁止root身份登录?不行,因为有些程序需要使用root什么登录,另外判断一个用户是不是超级管理员,看的是用户的id是否为0
2 # 例如
3 [root@localhost ~]# useradd steven        //创建一个普通用户    
4 [root@localhost ~]# passwd steven          //给用户一个密码
5 [root@localhost ~]# vi /etc/passwd         // 修改用户权限
6 root:x:0:0:root:/root:/sbin/nologin          //禁止用root名登录
7 //没改之前  root:x:0:0:root:/root:/bin/bash     
8 steven:x:0:0::/home/steven:/bin/bash     //
9 //没改之前 steven:x:500:500::/home/steven:/bin/bash

3.暴力破解情况比较严重时,需要把暴力破解的ip给直接封掉。

3.1安装fail2ban

命令lastb 查看登陆失败的用户

1 [root@localhost log]# lastb
2 steven   ssh:notty    10.0.5.172       Sat Nov  3 08:18 - 08:18  (00:00)
3 steven   ssh:notty    10.0.5.172       Sat Nov  3 02:36 - 02:36  (00:00)
4 steven   ssh:notty    10.0.5.172       Fri Nov  2 23:52 - 23:52  (00:00)
5 steven   ssh:notty    10.0.5.172       Fri Nov  2 23:52 - 23:52  (00:00)
6 steven   ssh:notty    10.0.5.172       Fri Nov  2 23:51 - 23:51  (00:00)

ll -h /var/log/   btmp登录失败的用户信息日志  secure登录成功的用户信息日志

暴力破解就算不成功也会导致服务器负载很高,因为暴力破解的时候,系统会不断的认证用户,从而增加了系统资源的额外开销,导致公司网站速度变慢。

fail2ban可以监视你的系统日志,然后匹配日志的错误信息,执行相应的屏蔽动作,一般是防火墙,而且可以发送e-mail通知系统管理员。

fail2ban的工作原理就是通过分析一定时间内的相关服务日至,将满足动作的有关ip利用iptables加入到drop列表。

1 下载软件包:
2 官方地址:http://www.fail2ban.org
3 建议选择stable稳定版的
4 tar -zxvf 0.9.4.tar.gz  //解压5 cd fail2ban-0.9.4/     6 python setup.py install   //此处如果没有安装Python的请先安装python,因为使用Python写的软件

3.2生成服务启动脚本

1 [root@localhost fail2ban-0.9.4]# cp files/redhat-initd /etc/init.d/fail2ban   //copy redhat-initd文件夹到init.d文件夹下
2 [root@localhost fail2ban-0.9.4]# chkconfig --add fail2ban    //可以看一下/etc/inin.d/fail2ban的代码

3.3怎么找到脚本文件的

1 [root@localhost fail2ban-0.9.4]# grep chkconfig ./* -R --color   //通过过滤  chkconfig 
2 ./files/redhat-initd:# chkconfig: - 92 08  

3.4 配置文件说明

/etc/fail2ban/action.d    #动作文件夹,内含默认文件,iptables以及mail等动作。

/etc/fail2ban/fail2ban.conf    # 定义了fail2ban日志级别,日志位置以及sock文件位置

/etc/fail2ban/filter.d  # 条件文件夹,内含默认文件,过滤日志等关键内容

/etc/fail2ban/jail.conf  # 只要配置文件,模块化。主要设置启用ban动作的服务及动作阀

/etc/rc.d/init.d/fail2ban  #启动脚本文件

3.5 实力应用

设置条件:ssh远程登录5分钟内3次密码验证失败,禁止ip用户访问主机1小时,1小时候自动解除,用户可以重新登录。

 3.5.1 修改jail.conf

 1  [root@localhost fail2ban]# vi jail.conf
 2  [DEFAULT]   全局设置
 3  //ifnoreip=127.0.0.1/8  忽略的ip列表,不受限制的ip
 4  //bantime = 600  屏蔽时间s
 5  //findtime = 600  这个时间内超过规定时间会被ban掉
 6  //maxretry = 3 规定时间内重试次数
 7  //backend = auto  自动恢复
 8   
 9  [ssh-iptables]#单个服务检查设置,如设置bantime、findtime等和全局设置冲突,则优先级要大于全局
10  enabled = false  是否启用此服务,改成true
11  filter = sshd   过滤规则filter的名字,对应filt.d目录下的sshd.conf
12  action = iptables[name=SSH,port=ssh,protocol=tcp] 动作参数
13  sender=fail2ban@example.com,sendername='Fai2Ban'] #触发报警的收件人
14  bantime = 600     # 改成要屏蔽的时间36000
15  findtime = 500
16  maxretry = 5
17  logpath = /var/log/secure  

3.5.2 启动服务测试

1 [root@localhost fail2ban]# > /var/log/secure    //清空日志
2 [root@localhost fail2ban]# service fail2ban start   //或者/etc/init.d/fail2ban start
3 启动fail2ban:                                              [确定]  //启动成功  

 3.53 测试 故意输错三次

 3.54 查看状态 

1 [root@localhost ~]# fail2ban-client status
2 Status
3 |- Number of jail:    1
4 `- Jail list:    ssh-iptables
 1 //查看详细状态
 2 Status for the jail: ssh-iptables
 3 |- Filter
 4 |  |- Currently failed:    1
 5 |  |- Total failed:    5
 6 |  `- File list:    /var/log/secure
 7 `- Actions
 8    |- Currently banned:    0
 9    |- Total banned:    3  
10    `- Banned IP list:

 

posted @ 2018-04-15 21:08  Mr-Lee-long  阅读(658)  评论(0编辑  收藏  举报