fail2ban的使用以及防暴力破解与邮件预警
? fail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是防火墙),而且可以发送e-mail通知系统管理员! fail2ban运行机制:简单来说其功能就是防止暴力破解。工作的原理是通过分析一定时间内的相关服务日志,将满足动作的相关IP利用iptables加入到dorp(丢弃)列表一定时间。 fail2ban 官方网址:http://www.fail2ban.org 文章的主要内容: 1. 安装fail2ban?? 2. 配置fail2ban??? 3. fail2ban的常用命令以及详解? 4.邮件预警? 5.注意事项 1. 安装fail2ban 源码安装: 1.1 下载安装包并解压 cd /tmp && wget https://github.com/fail2ban/fail2ban/archive/0.8.14.tar.gz && tar -xvzf 0.8.14.tar.gz 1.2? 查看安装需要的环境 [root@vultr fail2ban-0.8.14]# cd /tmp/fail2ban* && cat README.md Installation: ------------- **It is possible that Fail2ban is already packaged for your distribution. In this case,you should use it instead.** Required: - [Python >= 2.4](http:www.python.org) Optional: - [pyinotify >= 0.8.3](https:github.com/seb-m/pyinotify) - Linux >= 2.6.13 - [gamin >= 0.0.21](http:www.gnome.org/~veillard/gamin) To install,just do: tar xvfj fail2ban-12.tar.bz2 cd fail2ban-12 python setup.py install This will install Fail2Ban into /usr/share/fail2ban. The executable scripts are placed into /usr/bin,and configuration under /etc/fail2ban. Fail2Ban should be correctly installed now. Just type: fail2ban-client -h to see if everything is alright. You should always use fail2ban-client and never call fail2ban-server directly. 我把README.md里的主要内容贴出来了。 环境检查 [root@localhost ~]# python -V Python 2.7.5 [root@localhost ~]# uname -r 3.10.0-693.el7.x86_64 1.3 安装fail2ban cd /tmp/fail2ban-* && python setup.py install
生成服务启动脚本并开机自启: cp /tmp/fail2ban-*/files/redhat-initd /etc/init.d/fail2ban /sbin/chkconfig fail2ban on yum安装: yum install fail2ban fail2ban的配置文件路径:/etc/fail2ban fail2ban安装目录:/usr/share/fail2ban 日志文件:/var/log/fail2ban.log 达到阈值之后的执行的动作的配置文件:? ?action.d/? 包含所有的过滤规则:filter.d/ 2. 配置fail2ban并实现防暴力破解 官方的文档写到:在配置时,我们应该避免修改由fail2ban安装创建的文件,我们应该去编写具有.local扩展名的新文件。在.local新文件里配置的内容会覆盖jail.conf内容里相同的值。 当我们的配置发生改变了我们可以使用? fail2ban-client reload?,来加载新的配置。 2.1配置fail2ban 编辑配置文件 jail.local? 并实现防暴力破解 vim /etc/fail2ban/jail.d/jail.local
?文件内容: #defalut这里是设定全局设置,如果下面的监控没有设置就以全局设置的值设置。 [DEFAULT] # 用于指定哪些地址ip可以忽略 fail2ban 防御,以空格间隔。 ignoreip = 127.0.0.1/8 # 客户端主机被禁止的时长(默认单位为秒) bantime = 3600 # 过滤的时长(秒) findtime = 600 # 匹配到的阈值(次数) maxretry = 3 [ssh-iptables] # 是否开启 enabled = true # 过滤规则 filter = sshd # 动作 action = iptables[name=SSH,port=ssh,protocol=tcp] # 日志文件的路径 logpath = /var/log/secure # 匹配到的阈值(次数) maxretry = 3 ? 在这里需要注意一点就是:我们上面的action设置的时候,port=ssh,如果我们更改了sshd服务的端口号,我能需要在这里设置对应的端口号,否则配置不生效。 2.2 防暴力破解测试 在上面配置好了之后,我们需要让配置生效: fail2ban-client reload 测试:故意输入错误密码3次,再进行登录时,会拒绝登录 [root@121~]# ssh 192.168.1.121 root@1.121's password: Permission denied,please try again. root@Permission denied (publickey,password). [root@1.121 ssh: connect to host 192.168.1.121 port 22: Connection refused 我们可以查看当前被禁止登陆的ip: [root@121]# fail2ban-client status iptables Status for the jail: iptables |- filter | |- File list: /var/log/secure #日志文件路径 | |- Currently failed: 0 #当前失败次数 | `- Total failed: #总失败次数 `- action |- Currently banned: 1 #当前禁止的ip数量 | `- IP list: 1.112 #当前禁止的ip `- Total banned: 1 #禁止的ip总数 3. fail2ban常用的命令 3.1 启动暂停查看状态 systemctl start/restart/stop/status fail2ban ? 3.2 fail2ban-client
? 还有很多没有列出来,我只在列出了我常用的一些。 3.3 fail2ban-regex 测试筛选规则设否匹配当前的日志格式: fail2ban-regex /var/log/secure /etc/fail2ban/filter.d/sshd.conf 4. fail2ban邮件预警 fail2ban 利用sendmail 进行发邮件也可以用mail进行发邮件,在尝试了sendmail无果后,决定使用mail。 具体步骤: 4.1 在/etc/mail.rc末尾配置发件人的信息: set ssl-verify=ignore set from=djx set smtp=smtps:mail.163.com:25 set smtp-auth-user=dj set smtp-auth-password= set smtp-auth=login set nss-config-dir=/root/.certs ? 4.2 然后在jail.local(自己的配置文件里),加入: mail-whois[name=SSH,dest=1120@qq.com,sender=djx@163.com] dest 是填入收件人邮箱 sender 是填入 发送邮箱 最后重新加载下配置即可。 ?注意的问题: 时区问题: http://www.fail2ban.org/wiki/index.php/FAQ_english:If time reference is not the same everywhere,then fail2ban won't ban any IP! 当我们更改了时区后,然后日志的时间并没有修改过来,导致两者的时间不一致,这样fail2ban的工作就失效了 解决办法:重启日志服务:systemctl restart rsyslog,保证两者的时间一致。 修改端口问题: sshd更改端口号后使用fail2ban需要注意在填写配置的时候也需要更改端口号。否则会出现就算会将ip添加到防火墙,但是由于我更改了端口号,是起不到禁止作用的。 解决方法:配置文件中 ?action = iptables[name=SSH,port=ssh,protocol=tcp]?,port位置修改为新的端口。 ? fail2ban一键安装脚本和fail2ban防cc攻击在端午节后会写出来。 ?
? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 你如何使用systemd的journalctl模式
- 配置NFS网络存储作为cinder的后端存储
- linux-kernel – Linux:来宾VM的内核如何保持时间
- 在linux shell中使用键盘选择文本
- linux – 用于MongoDB分片的mongos和配置服务器的Init脚本
- linux – ossec功能与snort / tripwire兼容pci
- linux – 无法apt-get remove或apt-get install | fopen许可
- linux – 如何在文件中获取包含字符串的行?
- 为什么`sched_setscheduler()`需要root priveledges?
- ssh免密码登陆及其原理