导读:目前正在解读《Fail2ban教程解决网站被扫描、CC攻击、ssh暴力破解、防爬虫等问题》的相关信息,《Fail2ban教程解决网站被扫描、CC攻击、ssh暴力破解、防爬虫等问题》是由用户自行发布的知识型内容!下面请观看由(国外服务器 - www.2bp.net)用户发布《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

#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 = ^<HOST> -.*“(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暴力破解、防爬虫等问题》最后刷新时间 2023-03-27 02:11:27,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《Fail2ban教程解决网站被扫描、CC攻击、ssh暴力破解、防爬虫等问题》该内容的真实性请自行鉴别。