sakibmoon.fail2ban
角色名称
一个用于安装和管理 Fail2ban 的 Ansible 角色
要求
Ansible 版本 2.6 或更高
安装
Ansible Galaxy
使用 ansible-galaxy install sakibmoon.fail2ban
Git
使用 git clone https://github.com/sakibmoon/ansible-role-fail2ban.git
基本用法
安装并启用 fail2ban,并配置 ssh 以与 fail2ban 一起使用
- hosts: servers
vars:
fail2ban_services:
- name: "sshd"
enabled: "true"
port: "ssh"
filter: "sshd"
logpath: "/var/log/auth.log"
maxretry: 6
roles:
- sakibmoon.fail2ban
查看 角色变量 和 示例剧本 以获取更复杂的用法以及如何微调所有选项
角色变量
服务监控选项
fail2ban_services
要通过 fail2ban 监控的服务列表。每个选项必须包含服务名称和日志路径。只需指定您想更改的设置,其余配置来自全局监狱配置,详情可见 此处。
服务选项:
name
: 服务名称
logpath
: 要监控的服务的日志路径
port
: 要监控的端口,使用逗号分隔
enabled
: 是否启用此监狱
操作列表
fail2ban_actionlist
要创建的操作列表。操作保存在 /etc/fail2ban/action.d/
目录中。单个操作可以包含以下选项:
name
: 操作名称。文件名称。
sections
: 包含条目字典的节列表,如 Definition
, Init
等
file_ext
: (可选) 文件扩展名。可以是 local
或 conf
。默认值: local
示例:
fail2ban_actionlist:
- name: toy-action1
sections:
- name: Definition
options:
- name: actionstart
value: "ActionStart 值"
- name: actionflush
value: "Actionflush 值"
- name: Init
options:
- name: timeout
value: "超时值"
file_ext: "local"
- name: toy-action2
sections:
- name: INCLUDES
options:
- name: before
value: something-to-include.local
- name: Definition
options:
- name: actionstart
value: "ActionStart 值"
- name: actionflush
value: "Actionflush 值"
- name: Init
options:
- name: timeout
value: "超时值"
过滤器列表
fail2ban_filterlist
要创建的过滤器列表。过滤器保存在 /etc/fail2ban/action.d/
目录中。格式与上面的 fail2ban_actionlist
相同。
Fail2ban 配置选项
变量名称 | Fail2ban 选项名称 | 选项值 | 默认值 | 描述 |
---|---|---|---|---|
fail2ban_confpath |
[FILE] |
/etc/fail2ban/fail2ban.local |
fail2ban 配置文件的路径。 | |
fail2ban_loglevel |
loglevel |
CRITICAL ERROR WARNING INFO DEBUG \ [0-3] |
ERROR (版本 > 0.8.x ) 1 (版本 0.8.x ) |
设置日志输出级别。 |
fail2ban_logtarget |
logtarget |
[FILE] STDOUT STDERR SYSLOG |
/var/log/fail2ban.log |
设置日志目标。只能指定一个日志目标。 如果您更改了日志目标的默认值,并且使用了 logrotate -- 还需要在相应的配置文件中调整或禁用轮换 (例如在 Debian 系统上为 /etc/logrotate.d/fail2ban) |
fail2ban_syslogsocket |
syslogsocket |
auto [FILE] |
auto |
设置 syslog 套接字文件。 仅在 logtarget 设置为 SYSLOG 时使用。 auto 根据平台确定预定义路径 |
fail2ban_socket |
socket |
[FILE] |
/var/run/fail2ban/fail2ban.sock |
设置套接字文件。 此文件用于与守护进程进行通信。当 Fail2ban 运行时,请不要删除此文件。否则将无法与服务器通信。 |
fail2ban_pidfile |
pidfile |
[FILE] |
/var/run/fail2ban/fail2ban.pid |
设置 PID 文件。此文件用于存储 fail2ban 服务器的进程 ID。 |
fail2ban_dbfile |
dbfile |
None :memory: [FILE] |
/var/lib/fail2ban/fail2ban.sqlite3 |
设置 fail2ban 持久数据存储的文件。:memory: 表示数据库仅存储在内存中,停止 fail2ban 时数据会丢失。None 表示禁用数据库。 |
fail2ban_dbpurgeage |
dbpurgeage |
[ SECONDS ] |
86400 |
设置 bans 从数据库中清除的年龄 |
fail2ban_dbmaxmatches |
dbmaxmatches |
[ INT ] |
20 |
每个票据存储在数据库中的匹配数。 |
fail2ban_stacksize |
stacksize |
[ SIZE ] |
0 |
指定用于后续创建的线程的堆栈大小(以 KiB 为单位),必须为 0 或至少为 32 的正整数。 |
全局监狱配置选项
变量名称 | Fail2ban 选项名称 | 选项值 | 默认值 | 描述 |
---|---|---|---|---|
fail2ban_jailpath |
[FILE] |
/etc/fail2ban/jail.local |
写入 Fail2ban 监狱默认配置的文件 | |
fail2ban_ignoreself |
ignoreself |
booleantrue false |
true |
表示是否应防止封禁自己的 IP 地址。 |
fail2ban_ignoreip |
ignoreip |
IP 地址列表 | 不被禁用的 IP 列表。它们还可以包括 DNS 或 CIDR 掩码。该选项还影响 ignoreself(如果为 true),并且不需要包含运行主机的 DNS 或 IP。 | |
fail2ban_ignorecommand |
ignorecommand |
/path/to/command |
外部命令,将标记参数进行忽略,例如 |
|
fail2ban_ignorecache |
ignorecache |
disabled |
提供用于忽略失败检查的缓存参数(ignoreip 、ignoreself 和 ignorecommand 的结果缓存) |
|
fail2ban_bantime |
bantime |
[ SECONDS ] |
600 |
有效的禁令持续时间(以秒为单位)。 |
fail2ban_findtime |
findtime |
[ SECONDS ] |
600 |
在当前时间之前的时间间隔(以秒为单位),失败将计入禁忌。 |
fail2ban_maxretry |
maxretry |
[ INT ] |
5 |
在主机被禁用之前的失败次数。 |
fail2ban_backend |
backend |
pyinotify gamin polling systemd auto |
auto |
指定用于获取文件修改的后端。 |
fail2ban_usedns |
usedns |
yes warn no raw |
warn |
指定监狱是否应信任日志中的主机名,在执行 DNS 查找时警告,或忽略日志中的所有主机名 |
fail2ban_logencoding |
logencoding |
auto ascii utf-8 etc. |
auto |
指定监狱处理的日志文件的编码。默认值 auto 使用当前系统区域设置。 |
fail2ban_mode |
mode |
normal ddos extra aggressive |
normal |
过滤器的模式 |
fail2ban_filter |
filter |
过滤器名称 | %(__name__)s[mode=%(mode)s] |
监狱使用的过滤器。 默认监狱的名称与其过滤器名称匹配 |
fail2ban_logtimezone |
logtimezone |
UTC UTC+0200 GMT-0100 etc. |
强制没有时区的日志行的时区。 如果未指定此选项,则 Fail2ban 将根据其自身系统时区解释没有找到明确时区的日志行。 |
|
fail2ban_banaction |
banaction |
iptables , iptables-new , iptables-multiport , shorewall , 等 |
iptables-multiport |
默认的禁止操作。用于定义 action_* 变量。可以在全局或监狱.local 文件中的每个部分覆盖。 |
fail2ban_banaction_allports |
banaction_allports |
iptables , iptables-new , iptables-multiport , shorewall , 等 |
iptables-allports |
与 banaction 相同,但用于如 "pam-generic" 或 "recidive" 等 "allports" 监狱 |
fail2ban_action |
action |
%(action_)s |
选择默认操作。要更改,请使用所选操作快捷方式的插值覆盖 'action' 的值(例如 action_mw、action_mwl 等)或选择来自 /etc/fail2ban/action.d/ 的操作,不带 .conf/.local 扩展名。 | |
fail2ban_failregex |
failregex |
Python 正则表达式 |
要添加到过滤器失败正则表达式的正则表达式(有关详细信息,请参见 FILTER FILES 部分中的 failregex)。 | |
fail2ban_ignoreregex |
ignoreregex |
如果日志行匹配该正则表达式,则 fail2ban 将忽略该行。即使该行匹配监狱或其任何过滤器的 failregex,也将被忽略。 | ||
fail2ban_maxmatches |
maxmatches |
[INT] |
每个票据在内存中保持的最大匹配日志行数。默认与监狱的 maxretry (或默认值)相同 |
依赖关系
没有依赖关系
支持的平台
该角色应适用于任何基于 Redhat 或 Debian 的 Linux 发行版。已在以下平台上进行了测试:
- CentOS 8
- CentOS 7
- Ubuntu 18.04
- Ubuntu 16.04
- Debian 10
- Debian 9
示例剧本
以下剧本安装、启用 fail2ban 并添加 ssh 监狱
- hosts: servers
vars:
fail2ban_services:
- name: "sshd"
enabled: "true"
port: "ssh"
filter: "sshd"
logpath: "/var/log/auth.log"
maxretry: 6
roles:
- sakibmoon.fail2ban
许可证
MIT
作者信息
此角色由 sakibmoon 创建,创建时间为 2020年