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

本站:VPS参考评测推荐,专注分享VPS服务器优惠信息!若您是商家也可以在本站进行投稿,查看详情!此外我们还提供软文收录、PayPal代付、广告赞助等服务,查看详情!
部分文章发布时间较久远,可能存在未知因素,购买时建议在本站搜索商家名称,可查看相关文章充分了解商家!若非中文页面可使用谷歌浏览器同步翻译!PayPal代付/收录合作
  • fail2ban、cckiller、Wordfence Security 对比分析
  • fail2ban 功能特色
    • Fail2ban 安装步骤使用教程
    • Fail2ban 管理常用命令
    • 使用三方 smtp 主机发送通知邮件

最近网站总是被高频度扫描,导致数据库连接过多,打开页面报“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

  • #ubuntu
  • apt-get install fail2ban
  • #CentOS
  • yum -y install epel-release
  • yum -y install fail2ban
  • fail2ban 配置原理简介

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

  • [DEFAULT]
  • # 忽略 IP,IP 白名单,这些 IP 永远不会被禁
  • ignoreip = 127.0.0.1/8
  • #IP 被封禁的时间,单位秒
  • bantime  = 600
  • #日志文件中,在 findtime 时间段内,ip 出现超过 maxretry 次数,就会封禁该 IP
  • findtime = 600
  • maxretry = 3
  • # "backend" 指获取日志文件的方法,分为"pyinotify", "gamin", "polling", "auto"四种
  • # auto 值得是哪种可用就用哪种,默认 polling 可用
  • backend = auto
  • # "usedns" specifies if jails should trust hostnames in logs,
  • usedns = warn
  • #接受邮件地址
  • destemail = xxxxxx@gmail.com
  • # Name of the sender for mta actions
  • sendername = Fail2Ban
  • #默认的动作执行行为,在 action.d 目录下有各种行为策略,默认是 iptables-multiport
  • banaction = iptables-multiport
  • # email action. Since 0.8.1 upstream fail2ban uses sendmail
  • mta = sendmail
  • # Default protocol
  • protocol = tcp
  • # Specify chain where jumps would need to be added in iptables-* actions
  • chain = INPUT
  • #定义各种行为参数
  • #只禁 IP
  • action_ = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
  • #禁 IP+邮件通知
  • action_mw = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
  • %(mta)s-whois[name=%(__name__)s, dest="%(destemail)s", protocol="%(protocol)s", chain="%(chain)s", sendername="%(sendername)s"]
  • # 禁 IP+邮件通知+报告相关日志
  • action_mwl = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
  • %(mta)s-whois-lines[name=%(__name__)s, dest="%(destemail)s", logpath=%(logpath)s, chain="%(chain)s", sendername="%(sendername)s"]
  • # 选择行为
  • action = %(action_)s
  • #以下是各种应用监控日志的配置,以 ssh 日志和 apache 日志为例说明一下配置原理
  • [ssh]
  • enabled  = true
  • port     = ssh
  • filter   = sshd
  • logpath  = /var/log/auth.log
  • maxretry = 6
  • [php-url-fopen]
  • enabled = true
  • port    = http,https
  • filter  = php-url-fopen
  • action = %(action_mwl)s
  • logpath = /var/log/apache2/access.log
  • findtime = 180
  • 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 默认的正则有点问题,需要稍微修改下,修改后如下

  • [Definition]
  • failregex = ^ -.*"(GET|POST).* HTTP\/.*$
  • ignoreregex =
  • Logpath apache 日志存放路径

    findtime 为 180 秒

    访问次数阈值为 200 次。

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

    Fail2ban 管理常用命令

  • #fail2ban 服务重启、停止、开启
  • service fail2ban restart|stop|start
  • #查看 fail2ban 状态
  • fail2ban-client status
  • #查看日志监控状态
  • fail2ban-client status php-url-fopen
  • #查看 iptables 禁 ip 情况
  • 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)

    添加

  • set ssl-verify=ignore
  • set nss-config-dir=/etc/pki/nssdb
  • set from=邮件地址@163.com
  • set smtp=smtps://smtp.163.com:465
  • set smtp-auth-user=邮件地址@163.com
  • set smtp-auth-password=e8456ds78c23
  • set smtp-auth=login
  • 发邮件测试

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

    jail.conf 中 mta 改为 mail,即可

    本文由本站刊发,转载请注明:Fail2ban详细教程,解决网站被扫描、CC攻击、ssh暴力破解、防爬虫等问题,https://本站.com/2935.html

    推荐站内搜索:韩国独立服务器、icp备案查询网、免费虚拟主机、服务器租赁、php空间、韩国便宜服务器、最便宜的云服务器、网站空间租用、域名如何备案、域名注册查询、