MENU

fail2ban联动server酱配置

June 4, 2021 • 安全

云服务器经常会被爆破 ssh 端口,为了防范就用 iptables 联动 fail2ban 对爆破的 ip 进行封禁,最后为了看看都是哪些小兔崽子在爆破,联动 server 酱对封禁操作进行推送。

操作过程

安装 fail2ban

yum -y install epel-release
yum -y install fail2ban

fail2ban 结构

/etc/fail2ban                  ## fail2ban 服务配置目录
/etc/fail2ban/action.d     ## iptables 、mail 等动作文件目录
/etc/fail2ban/filter.d       ## 条件匹配文件目录,过滤日志关键内容
/etc/fail2ban/jail.conf     ## fail2ban 防护配置文件
/etc/fail2ban/fail2ban.conf   ## fail2ban 配置文件,定义日志级别、日志、sock 文件位置等

配置

于目录下创建 jail.local,用以配置封禁规则,因为 fail2ban 每次更新会覆盖 jail.conf,所以要避免直接修改 jail.confjail.local 会自动到 jail.conf 中读取缺省值,以下仅演示开启 ssh 封禁规则。

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

使配置生效

fail2ban-client reload    #重新加载配置文件
fail2ban-client status    #查看当前加载配置状态

联动 server 酱

经过上面加载修改后的配置可以看到 ssh 规则已经生效起来了,具体可以查看 /var/log/fail2ban.log 具体内容来查看运行情况,接下来是通过 server 酱将封禁的操作推送给我们。

编写 action

在上文 /etc/fail2ban/action.d 目录中可以看到是存放动作文件的目录,而我们的 server 酱推送也是一个动作,进入该目录下有一个 npf.conf 文件,里面包含了各种动作的相关注释

# Fail2Ban configuration file
#
# NetBSD npf ban/unban
#
# Author: Nils Ratusznik <nils@NetBSD.org>
# Based on pf.conf action file
#

[Definition]

# Option:  actionstart
# Notes.:  command executed on demand at the first ban (or at the start of Fail2Ban if actionstart_on_demand is set to false).
# Values:  CMD
#
# we don't enable NPF automatically, as it will be enabled elsewhere
actionstart = 


# Option:  actionstop
# Notes.:  command executed at the stop of jail (or at the end of Fail2Ban)
# Values:  CMD
#
# we don't disable NPF automatically either
actionstop = 


# Option:  actioncheck
# Notes.:  command executed once before each actionban command
# Values:  CMD
#
actioncheck = 


# Option:  actionban
# Notes.:  command executed when banning an IP. Take care that the
#          command is executed with Fail2Ban user rights.
# Tags:    <ip>  IP address
#          <failures>  number of failures
#          <time>  unix timestamp of the ban time
# Values:  CMD
#
actionban = /sbin/npfctl table <tablename> add <ip>


# Option:  actionunban
# Notes.:  command executed when unbanning an IP. Take care that the
#          command is executed with Fail2Ban user rights.
# Tags:    <ip>  IP address
#          <failures>  number of failures
#          <time>  unix timestamp of the ban time
# Values:  CMD
#
# note -r option used to remove matching rule
actionunban = /sbin/npfctl table <tablename> rem <ip>

[Init]
# Option:  tablename
# Notes.:  The pf table name.
# Values:  [ STRING ]
#
tablename = fail2ban

通过这一份带注释的 conf 文件可以知道,一个 action 的配置分为两大部分,分别是 [Definition][Init]

[Definition] 其中又包含了执行动作的五个不同时机,根据说明我们可以根据我们的需求来判断逻辑,而我们的需求是在规则执行封禁时,通过 server 酱推送封禁情况给我们,而这一种场景就应该是 actionban 词条下,通过 curl 完成对 server 酱 api 的请求。

[Init] 便是对该动作下需要用到的参数进行定义,比如我们在请求 server 酱 api 时需要带上我们的 sendkey,这就需要定义一个参数来完成操作。

编写 action 文件内容如下

[Definition]

actionstart =
actionstop =
actioncheck =
actionban = curl -sSf "https://sctapi.ftqq.com/<sckey>.send" -d "text=<ip> 关到小黑屋." -d "desp=IP <ip> 已被Fail2Ban封禁,违规动作<failures>次,执行监狱 <name>."
actionunban =

[Init]

name = default
sckey = 你的sendkey

保存该 action 后,记住动作名,比如文件名是 serverjiang.conf,那这个动作名就是 serverjiang

载入配置

此时回到 jail.local 中,在规则里面的 action 处换行对齐添加我们的 server 酱动作名,然后重载配置就可以在 ban 掉大嗨阔们的 ip 后推送给你知道,不过建议将封禁时长留的更长一点,不然隔一会弹一条消息很嗨烦。

常用命令

fail2ban-client
start 启动 fail2ban server 和监狱
reload 重新加载配置文件
stop 暂停 fail2ban 和监狱
status 查看运行的监控服务数量
set loglevel 设置日志等级,有 CRITICAL, ERROR WARNING, NOTICE, INFO DEBUG
get loglevel 获取当前日志的等级
set <JAIL> idle on/off 设置某个监控(监狱)的状态
set <JAIL> addignoreip <IP> 设置某个监控(监狱)可以忽略的 IP
set <JAIL> delignoreip <IP> 删除某个监控(监狱)可以忽略的 IP
set <JAIL> banip <IP> 将 IP 加入监控(监狱)
set <JAIL> unbanip <IP> 将 IP 从监控(监狱)移除
本文作者:Losir
本文链接:fail2ban 联动 server 酱配置 - https://ionssource.cn/archives/63/
版权声明:如无特别声明,本文即为原创文章,仅代表个人观点,版权归 Losir 所有,未经允许不得转载!

开往-友链接力