VPS联盟
我们一直在努力
cloudacead cloudacead cloudacead cloudacead

Fail2ban详细教程,解决网站被扫描、CC攻击、ssh暴力破解、防爬虫等问题

最近网站总是被高频度扫描,导致数据库连接过多,打开页面报“Error establishing a database connection“错误。最开始写了个监控网站的脚本,一旦发现网站打不开,重启数据库就好了。但是这几天网站挂掉的频率越来越高,不得不开始寻找新的办法了。

对于网站被恶意扫描、暴力破解、CC 攻击这一系列攻击,都有相似的特征,即高频率发请求导致主机资源使用率飙高。对于这些问题,必须要做两件事,一个是识别恶意发请求的 IP,并封禁;二个是实现网站缓存、静态化等功能减少数据库查询。今天介绍下识别频繁发请求的 IP 并封 IP 的工具。

调研了 cckiller、Wordfence Security、fail2ban 这三个工具,最终选择了 fail2ban。

fail2ban、cckiller、Wordfence Security 对比分析

首先是 cckiller,是国内大佬实现的 linux 轻量级 cc 防御工具。是根据当前 http 连接数计算并发量,如果某个 ip 的并发量大于某个值则在 iptables 中封禁该 ip。是个不错的工具,但有两个小问题。一个是只能根据某个时刻计算并发量封 IP,不能计算某个时间段的总请求数来封 IP;二个是兼容性不太好,适合在 centos 上运行,但个人习惯用 ubuntu。

其次是 Wordfence Security,wordpress 安全插件。据说可以根据某个时间段访问网站总次数超过阈值后,封禁该 IP。但是问题较多,该插件比较庞大,只为了这个功能安装插件不划算;没有汉化;该功能好像要付费才能用的更流畅。简单试了下,不太合适。

最终选择了 fail2ban,有以下优点。

fail2ban 功能特色

根据实时日志,统计请求数量,如果某个时间段,某种请求超过了阈值,就可以封禁该 IP

支持 yum/apt 一键安装。

配置简单,配置文件几行配置就能实现需求

支持多种监控。简单举几个例子。可以读取 ssh 的登录日志,如果某个 ip 在某个时间段登录失败次数过多,则封该 ip;读取 apache 访问日志,如果某个 ip 在某个时间段防问次数过多,则封该 ip

支持邮件通知。

Fail2ban 安装步骤&使用教程

使用场景:操作系统是 ubuntu,web 服务器为 apache。将 3 分钟内请求数量超过 300 次的 ip 视为恶意扫描 IP 直接封禁。(其他操作系统和 web 服务器配置流程基本一样)

安装 fail2ban

  1. #ubuntu
  2. aptget install fail2ban
  3. #CentOS
  4. yum y install epelrelease
  5. yum y install fail2ban

fail2ban 配置原理介绍

主要是配置/etc/fail2ban/jail.conf 文件

  1. [DEFAULT]
  2. # 忽略 IP,IP 白名单,这些 IP 永远不会被禁
  3. ignoreip = 127.0.0.1/8
  4. #IP 被封禁的时间,单位秒
  5. bantime  = 600
  6. #日志文件中,在 findtime 时间段内,ip 出现超过 maxretry 次数,就会封禁该 IP
  7. findtime = 600
  8. maxretry = 3
  9. # “backend” 指获取日志文件的方法,分为”pyinotify”, “gamin”, “polling”, “auto“四种
  10. # auto 值得是哪种可用就用哪种,默认 polling 可用
  11. backend = auto
  12. # “usedns” specifies if jails should trust hostnames in logs,
  13. usedns = warn
  14. #接受邮件地址
  15. destemail = xxxxxx@gmail.com
  16. # Name of the sender for mta actions
  17. sendername = Fail2Ban
  18. #默认的动作执行行为,在 action.d 目录下有各种行为策略,默认是 iptables-multiport
  19. banaction = iptablesmultiport
  20. # email action. Since 0.8.1 upstream fail2ban uses sendmail
  21. mta = sendmail
  22. # Default protocol
  23. protocol = tcp
  24. # Specify chain where jumps would need to be added in iptables-* actions
  25. chain = INPUT
  26. #定义各种行为参数
  27. #只禁 IP
  28. action_ = %(banaction)s[name=%(__name__)s, port=“%(port)s”, protocol=“%(protocol)s”, chain=“%(chain)s”]
  29. #禁 IP+邮件通知
  30. action_mw = %(banaction)s[name=%(__name__)s, port=“%(port)s”, protocol=“%(protocol)s”, chain=“%(chain)s”]
  31. %(mta)swhois[name=%(__name__)s, dest=“%(destemail)s”, protocol=“%(protocol)s”, chain=“%(chain)s”, sendername=“%(sendername)s”]
  32. # 禁 IP+邮件通知+报告相关日志
  33. action_mwl = %(banaction)s[name=%(__name__)s, port=“%(port)s”, protocol=“%(protocol)s”, chain=“%(chain)s”]
  34. %(mta)swhoislines[name=%(__name__)s, dest=“%(destemail)s”, logpath=%(logpath)s, chain=“%(chain)s”, sendername=“%(sendername)s”]
  35. # 选择行为
  36. action = %(action_)s
  37. #以下是各种应用监控日志的配置,以 ssh 日志和 apache 日志为例说明一下配置原理
  38. [ssh]
  39. enabled  = true
  40. port     = ssh
  41. filter   = sshd
  42. logpath  = /var/log/auth.log
  43. maxretry = 6
  44. [phpurlfopen]
  45. enabled = true
  46. port    = http,https
  47. filter  = phpurlfopen
  48. action = %(action_mwl)s
  49. logpath = /var/log/apache2/access.log
  50. findtime = 180
  51. maxretry = 200

ssh 日志监控

enabled=true,表示开启监控

filter 在/etc/fail2ban/filter.d/sshd.conf 中定义,主要定义了找到错误日志的正则

Logpath ssh 日志存放路径

默认 findtime 为 600 秒

错误次数阈值为 6 次。

该配置意思为:实时监控 ssh 登录日志(/var/log/auth.log),如果某个 ip 在 600 秒内登录错误了 6 次(根据 filter 正则判断),则封禁该 IP

apache 日志监控

enabled=true,表示开启监控

filter 在/etc/fail2ban/filter.d/ php-url-fopen.conf 中定义,主要定义了匹配访问日志的正则。其中 fail2ban 默认的正则有点问题,需要稍微修改下,修改后如下

  1. [Definition]
  2. failregex = ^<HOST&gt; -.*“(GET|POST).* HTTP\/.*$
  3. ignoreregex =

Logpath apache 日志存放路径

findtime 为 180 秒

访问次数阈值为 200 次。

该配置意思为:实时监控 apache 访问日志(/var/log/apache2/access.log),如果某个 ip 在 180 秒内访问了 200 次(根据 filter 正则判断),则封禁该 IP

Fail2ban 管理&常用命令

  1. #fail2ban 服务重启、停止、开启
  2. service fail2ban restart|stop|start
  3. #查看 fail2ban 状态
  4. fail2banclient status
  5. #查看日志监控状态
  6. fail2banclient status phpurlfopen
  7. #查看 iptables 禁 ip 情况
  8. iptables nvL

使用三方 smtp 服务器发送通知邮件

由于腾讯云的 25 端口有限制不能使用系统自带的 sendmail,需要调用三方的 stmp 服务器,如 163 服务器,并使用端口为 465 的 ssl 协议发送邮件。配置如下

安装 mail ,apt-get install heirloom-mailx

修改/etc/nail.rc(ubuntu16.04 为/etc/s-nail.rc、centos 为/etc/mail.rc)

添加

  1. set sslverify=ignore
  2. set nssconfigdir=/etc/pki/nssdb
  3. set from=邮件地址@163.com
  4. set smtp=smtps://smtp.163.com:465
  5. set smtpauthuser=邮件地址@163.com
  6. set smtpauthpassword=e8456ds78c23
  7. set smtpauth=login

发邮件测试

echo “邮件内容”.|mail -v -s “邮件标题” xxxx@qq.com

jail.conf 中 mta 改为 mail,即可

赞(0)
未经允许不得转载:VPS联盟 » Fail2ban详细教程,解决网站被扫描、CC攻击、ssh暴力破解、防爬虫等问题
分享到: 更多 (0)